1 /*****************************************************************************
2 * encoder.c: video and audio encoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: encoder.c,v 1.22 2004/02/20 18:34:28 massiot Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/decoder.h>
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 # include <ffmpeg/avcodec.h>
43 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
44 #define HURRY_UP_GUARD1 (1000000)
45 #define HURRY_UP_GUARD2 (1000000)
46 #define HURRY_UP_GUARD3 (200000)
48 /*****************************************************************************
50 *****************************************************************************/
51 int E_(OpenEncoder) ( vlc_object_t * );
52 void E_(CloseEncoder)( vlc_object_t * );
54 static block_t *EncodeVideo( encoder_t *, picture_t * );
55 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
57 struct thread_context_t;
58 static int FfmpegThread( struct thread_context_t *p_context );
59 static int FfmpegExecute( AVCodecContext *s,
60 int (*pf_func)(AVCodecContext *c2, void *arg2),
61 void **arg, int *ret, int count );
63 /*****************************************************************************
64 * thread_context_t : for multithreaded encoding
65 *****************************************************************************/
66 #if LIBAVCODEC_BUILD >= 4702
67 struct thread_context_t
71 AVCodecContext *p_context;
72 int (* pf_func)(AVCodecContext *c, void *arg);
78 vlc_bool_t b_work, b_done;
82 /*****************************************************************************
83 * encoder_sys_t : ffmpeg encoder descriptor
84 *****************************************************************************/
91 AVCodecContext *p_context;
102 mtime_t i_last_ref_pts;
103 mtime_t i_buggy_pts_detect;
114 /*****************************************************************************
115 * OpenEncoder: probe the encoder
116 *****************************************************************************/
117 extern const int16_t ff_mpeg4_default_intra_matrix[];
118 extern const int16_t ff_mpeg4_default_non_intra_matrix[];
120 int E_(OpenEncoder)( vlc_object_t *p_this )
122 encoder_t *p_enc = (encoder_t *)p_this;
123 encoder_sys_t *p_sys = p_enc->p_sys;
124 AVCodecContext *p_context;
126 int i_codec_id, i_cat;
129 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
132 if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
134 /* handed chroma output */
138 i_codec_id = CODEC_ID_RAWVIDEO;
139 psz_namecodec = "Raw video";
143 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
145 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
149 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
151 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
155 /* Initialization must be done before avcodec_find_decoder() */
156 E_(InitLibavcodec)(p_this);
158 p_codec = avcodec_find_encoder( i_codec_id );
161 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
165 /* Allocate the memory needed to store the decoder's structure */
166 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
168 msg_Err( p_enc, "out of memory" );
171 p_enc->p_sys = p_sys;
172 p_sys->p_codec = p_codec;
174 p_enc->pf_encode_video = EncodeVideo;
175 p_enc->pf_encode_audio = EncodeAudio;
177 p_sys->p_buffer_out = NULL;
178 p_sys->p_buffer = NULL;
181 p_sys->p_context = p_context = avcodec_alloc_context();
183 /* Set CPU capabilities */
184 p_context->dsp_mask = 0;
185 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
187 p_context->dsp_mask |= FF_MM_MMX;
189 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
191 p_context->dsp_mask |= FF_MM_MMXEXT;
193 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
195 p_context->dsp_mask |= FF_MM_3DNOW;
197 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
199 p_context->dsp_mask |= FF_MM_SSE;
200 p_context->dsp_mask |= FF_MM_SSE2;
203 /* Make sure we get extradata filled by the encoder */
204 p_context->extradata_size = 0;
205 p_context->extradata = NULL;
206 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
208 if( p_enc->fmt_in.i_cat == VIDEO_ES )
210 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
212 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
213 p_enc->fmt_in.video.i_height );
218 p_context->width = p_enc->fmt_in.video.i_width;
219 p_context->height = p_enc->fmt_in.video.i_height;
221 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
222 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
224 /* Defaults from ffmpeg.c */
225 p_context->qblur = 0.5;
226 p_context->qcompress = 0.5;
227 p_context->b_quant_offset = 1.25;
228 p_context->b_quant_factor = 1.25;
229 p_context->i_quant_offset = 0.0;
230 p_context->i_quant_factor = -0.8;
232 p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
233 p_context->max_b_frames =
234 __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
235 p_context->b_frame_strategy = 0;
237 #if LIBAVCODEC_BUILD >= 4687
238 p_context->sample_aspect_ratio =
239 (AVRational){ p_enc->fmt_in.video.i_aspect *
240 (int64_t)p_context->height / p_context->width,
241 VOUT_ASPECT_FACTOR };
243 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
247 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
249 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
251 if ( p_enc->b_strict_rc )
253 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
254 p_context->rc_buffer_size = p_enc->i_rc_buffer_size;
255 p_context->rc_buffer_aggressivity = p_enc->f_rc_buffer_aggressivity;
258 if ( p_enc->f_i_quant_factor != 0.0 )
260 p_context->i_quant_factor = p_enc->f_i_quant_factor;
263 p_context->noise_reduction = p_enc->i_noise_reduction;
265 if ( p_enc->b_mpeg4_matrix )
267 p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
268 p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
271 if ( p_enc->b_pre_me )
273 p_context->pre_me = 1;
274 p_context->me_pre_cmp = FF_CMP_CHROMA;
277 if ( p_enc->b_interlace )
279 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
280 #if LIBAVCODEC_BUILD >= 4698
281 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
285 if ( p_enc->b_trellis )
287 p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
290 if ( p_enc->i_threads >= 1 )
292 p_context->thread_count = p_enc->i_threads;
295 if( p_enc->i_vtolerance > 0 )
297 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
300 p_context->mb_qmin = p_context->qmin = p_enc->i_qmin;
301 p_context->mb_qmax = p_context->qmax = p_enc->i_qmax;
302 p_context->max_qdiff = 3;
304 p_context->mb_decision = p_enc->i_hq;
306 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
308 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
309 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
310 p_context->channels = p_enc->fmt_in.audio.i_channels;
311 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
312 p_sys->p_buffer = malloc( p_sys->i_frame_size );
313 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
316 /* Misc parameters */
317 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
319 if( i_codec_id == CODEC_ID_RAWVIDEO )
321 /* XXX: hack: Force same codec (will be handled by transcode) */
322 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
323 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
326 /* Make sure we get extradata filled by the encoder */
327 p_context->extradata_size = 0;
328 p_context->extradata = NULL;
329 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
331 if( avcodec_open( p_context, p_codec ) )
333 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
335 p_context->channels = 2;
336 p_enc->fmt_in.audio.i_channels = 2; // FIXME
337 if( avcodec_open( p_context, p_codec ) )
339 msg_Err( p_enc, "cannot open encoder" );
343 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
347 msg_Err( p_enc, "cannot open encoder" );
353 p_enc->fmt_out.i_extra = p_context->extradata_size;
354 p_enc->fmt_out.p_extra = p_context->extradata;
355 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
357 if( p_enc->fmt_in.i_cat == AUDIO_ES )
359 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
360 p_sys->p_buffer = malloc( p_sys->i_frame_size );
363 p_sys->i_last_ref_pts = 0;
364 p_sys->i_buggy_pts_detect = 0;
365 p_sys->i_samples_delay = 0;
368 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
373 /****************************************************************************
374 * Ffmpeg threading system
375 ****************************************************************************/
376 #if LIBAVCODEC_BUILD >= 4702
377 static int FfmpegThread( struct thread_context_t *p_context )
379 while ( !p_context->b_die && !p_context->b_error )
381 vlc_mutex_lock( &p_context->lock );
382 while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
384 vlc_cond_wait( &p_context->cond, &p_context->lock );
386 p_context->b_work = 0;
387 vlc_mutex_unlock( &p_context->lock );
388 if ( p_context->b_die || p_context->b_error )
391 if ( p_context->pf_func )
393 p_context->i_ret = p_context->pf_func( p_context->p_context,
397 vlc_mutex_lock( &p_context->lock );
398 p_context->b_done = 1;
399 vlc_cond_signal( &p_context->cond );
400 vlc_mutex_unlock( &p_context->lock );
406 static int FfmpegExecute( AVCodecContext *s,
407 int (*pf_func)(AVCodecContext *c2, void *arg2),
408 void **arg, int *ret, int count )
410 struct thread_context_t ** pp_contexts =
411 (struct thread_context_t **)s->thread_opaque;
414 /* Note, we can be certain that this is not called with the same
415 * AVCodecContext by different threads at the same time */
416 for ( i = 0; i < count; i++ )
418 vlc_mutex_lock( &pp_contexts[i]->lock );
419 pp_contexts[i]->arg = arg[i];
420 pp_contexts[i]->pf_func = pf_func;
421 pp_contexts[i]->i_ret = 12345;
422 pp_contexts[i]->b_work = 1;
423 vlc_cond_signal( &pp_contexts[i]->cond );
424 vlc_mutex_unlock( &pp_contexts[i]->lock );
426 for ( i = 0; i < count; i++ )
428 vlc_mutex_lock( &pp_contexts[i]->lock );
429 while ( !pp_contexts[i]->b_done )
431 vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
433 pp_contexts[i]->b_done = 0;
434 pp_contexts[i]->pf_func = NULL;
435 vlc_mutex_unlock( &pp_contexts[i]->lock );
439 ret[i] = pp_contexts[i]->i_ret;
447 /****************************************************************************
448 * EncodeVideo: the whole thing
449 ****************************************************************************/
450 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
452 encoder_sys_t *p_sys = p_enc->p_sys;
456 #if LIBAVCODEC_BUILD >= 4702
457 if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
459 struct thread_context_t ** pp_contexts;
463 pp_contexts = malloc( sizeof(struct thread_context_t *)
464 * p_enc->i_threads );
465 p_sys->p_context->thread_opaque = (void *)pp_contexts;
467 for ( i = 0; i < p_enc->i_threads; i++ )
469 pp_contexts[i] = vlc_object_create( p_enc,
470 sizeof(struct thread_context_t) );
471 pp_contexts[i]->p_context = p_sys->p_context;
472 vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
473 vlc_cond_init( p_enc, &pp_contexts[i]->cond );
474 pp_contexts[i]->b_work = 0;
475 pp_contexts[i]->b_done = 0;
476 if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
477 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
479 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
484 p_sys->p_context->execute = FfmpegExecute;
488 memset( &frame, 0, sizeof( AVFrame ) );
489 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
491 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
492 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
495 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
496 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
497 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
498 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
500 frame.pts = p_pict->date;
502 if ( p_enc->b_hurry_up )
504 mtime_t current_date = mdate();
505 #if LIBAVCODEC_BUILD >= 4702
506 struct thread_context_t ** pp_contexts =
507 (struct thread_context_t **)p_sys->p_context->thread_opaque;
510 if ( frame.pts && current_date + HURRY_UP_GUARD3 > frame.pts )
512 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
513 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
514 #if LIBAVCODEC_BUILD >= 4702
515 if ( p_enc->i_threads >= 2 )
519 for ( i = 0; i < p_enc->i_threads; i++ )
521 vlc_thread_set_priority( pp_contexts[i],
522 VLC_THREAD_PRIORITY_VIDEO + 4 );
526 msg_Dbg( p_enc, "hurry up mode 3" );
530 p_sys->p_context->mb_decision = p_enc->i_hq;
532 if ( frame.pts && current_date + HURRY_UP_GUARD2 > frame.pts )
534 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
535 #if LIBAVCODEC_BUILD >= 4702
536 if ( p_enc->i_threads >= 2 )
540 for ( i = 0; i < p_enc->i_threads; i++ )
542 vlc_thread_set_priority( pp_contexts[i],
543 VLC_THREAD_PRIORITY_VIDEO + 2 );
547 msg_Dbg( p_enc, "hurry up mode 2" );
551 if ( p_enc->b_trellis )
552 p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
554 #if LIBAVCODEC_BUILD >= 4702
555 if ( p_enc->i_threads >= 2 )
559 for ( i = 0; i < p_enc->i_threads; i++ )
561 vlc_thread_set_priority( pp_contexts[i],
562 VLC_THREAD_PRIORITY_VIDEO );
569 if ( frame.pts && current_date + HURRY_UP_GUARD1 > frame.pts )
571 p_sys->p_context->noise_reduction = p_enc->i_noise_reduction
572 + (HURRY_UP_GUARD1 + current_date - frame.pts) / 1500;
576 p_sys->p_context->noise_reduction = p_enc->i_noise_reduction;
585 /* Let ffmpeg select the frame type */
588 frame.interlaced_frame = !p_pict->b_progressive;
589 frame.repeat_pict = p_pict->i_nb_fields;
591 #if LIBAVCODEC_BUILD >= 4685
592 frame.top_field_first = p_pict->b_top_field_first;
595 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
596 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
600 block_t *p_block = block_New( p_enc, i_out );
601 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
603 if( p_sys->p_context->coded_frame->pts != 0 &&
604 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
606 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
608 /* FIXME, 3-2 pulldown is not handled correctly */
609 p_block->i_length = I64C(1000000) *
610 p_enc->fmt_in.video.i_frame_rate_base /
611 p_enc->fmt_in.video.i_frame_rate;
612 p_block->i_pts = p_sys->p_context->coded_frame->pts;
614 if( !p_sys->p_context->delay ||
615 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
616 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
618 p_block->i_dts = p_block->i_pts;
622 if( p_sys->i_last_ref_pts )
624 p_block->i_dts = p_sys->i_last_ref_pts;
628 /* Let's put something sensible */
629 p_block->i_dts = p_block->i_pts;
632 p_sys->i_last_ref_pts = p_block->i_pts;
637 /* Buggy libavcodec which doesn't update coded_frame->pts
639 p_block->i_length = I64C(1000000) *
640 p_enc->fmt_in.video.i_frame_rate_base /
641 p_enc->fmt_in.video.i_frame_rate;
642 p_block->i_dts = p_block->i_pts = p_pict->date;
651 /****************************************************************************
652 * EncodeAudio: the whole thing
653 ****************************************************************************/
654 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
656 encoder_sys_t *p_sys = p_enc->p_sys;
657 block_t *p_block, *p_chain = NULL;
659 char *p_buffer = p_aout_buf->p_buffer;
660 int i_samples = p_aout_buf->i_nb_samples;
661 int i_samples_delay = p_sys->i_samples_delay;
663 p_sys->i_pts = p_aout_buf->start_date -
664 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
665 (mtime_t)p_enc->fmt_in.audio.i_rate;
667 p_sys->i_samples_delay += i_samples;
669 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
674 if( i_samples_delay )
676 /* Take care of the left-over from last time */
677 int i_delay_size = i_samples_delay * 2 *
678 p_sys->p_context->channels;
679 int i_size = p_sys->i_frame_size - i_delay_size;
681 p_samples = (int16_t *)p_sys->p_buffer;
682 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
683 p_buffer -= i_delay_size;
684 i_samples += i_samples_delay;
689 p_samples = (int16_t *)p_buffer;
692 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
693 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
697 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
699 if( i_out < 0 ) break;
701 p_buffer += p_sys->i_frame_size;
702 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
703 i_samples -= p_sys->p_context->frame_size;
705 if( i_out == 0 ) continue;
707 p_block = block_New( p_enc, i_out );
708 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
710 p_block->i_length = (mtime_t)1000000 *
711 (mtime_t)p_sys->p_context->frame_size /
712 (mtime_t)p_sys->p_context->sample_rate;
714 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
717 p_sys->i_pts += p_block->i_length;
718 block_ChainAppend( &p_chain, p_block );
721 /* Backup the remaining raw samples */
724 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
725 p_sys->p_context->channels, p_buffer,
726 i_samples * 2 * p_sys->p_context->channels );
732 /*****************************************************************************
733 * CloseEncoder: ffmpeg encoder destruction
734 *****************************************************************************/
735 void E_(CloseEncoder)( vlc_object_t *p_this )
737 encoder_t *p_enc = (encoder_t *)p_this;
738 encoder_sys_t *p_sys = p_enc->p_sys;
740 #if LIBAVCODEC_BUILD >= 4702
741 if ( p_sys->b_inited && p_enc->i_threads >= 1 )
744 struct thread_context_t ** pp_contexts =
745 (struct thread_context_t **)p_sys->p_context->thread_opaque;
746 for ( i = 0; i < p_enc->i_threads; i++ )
748 pp_contexts[i]->b_die = 1;
749 vlc_cond_signal( &pp_contexts[i]->cond );
750 vlc_thread_join( pp_contexts[i] );
751 vlc_mutex_destroy( &pp_contexts[i]->lock );
752 vlc_cond_destroy( &pp_contexts[i]->cond );
753 vlc_object_destroy( pp_contexts[i] );
760 avcodec_close( p_sys->p_context );
761 free( p_sys->p_context );
763 if( p_sys->p_buffer ) free( p_sys->p_buffer );
764 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );