1 /*****************************************************************************
2 * encoder.c: video and audio encoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc/decoder.h>
36 #ifdef HAVE_FFMPEG_AVCODEC_H
37 # include <ffmpeg/avcodec.h>
42 #if LIBAVCODEC_BUILD < 4704
43 # define AV_NOPTS_VALUE 0
48 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
49 #define HURRY_UP_GUARD1 (450000)
50 #define HURRY_UP_GUARD2 (300000)
51 #define HURRY_UP_GUARD3 (100000)
53 /*****************************************************************************
55 *****************************************************************************/
56 int E_(OpenEncoder) ( vlc_object_t * );
57 void E_(CloseEncoder)( vlc_object_t * );
59 static block_t *EncodeVideo( encoder_t *, picture_t * );
60 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
62 struct thread_context_t;
63 static int FfmpegThread( struct thread_context_t *p_context );
64 static int FfmpegExecute( AVCodecContext *s,
65 int (*pf_func)(AVCodecContext *c2, void *arg2),
66 void **arg, int *ret, int count );
68 /*****************************************************************************
69 * thread_context_t : for multithreaded encoding
70 *****************************************************************************/
71 #if LIBAVCODEC_BUILD >= 4702
72 struct thread_context_t
76 AVCodecContext *p_context;
77 int (* pf_func)(AVCodecContext *c, void *arg);
83 vlc_bool_t b_work, b_done;
87 /*****************************************************************************
88 * encoder_sys_t : ffmpeg encoder descriptor
89 *****************************************************************************/
96 AVCodecContext *p_context;
107 mtime_t i_last_ref_pts;
108 mtime_t i_buggy_pts_detect;
120 /*****************************************************************************
121 * OpenEncoder: probe the encoder
122 *****************************************************************************/
123 extern int16_t ff_mpeg4_default_intra_matrix[];
124 extern int16_t ff_mpeg4_default_non_intra_matrix[];
126 int E_(OpenEncoder)( vlc_object_t *p_this )
128 encoder_t *p_enc = (encoder_t *)p_this;
129 encoder_sys_t *p_sys = p_enc->p_sys;
130 AVCodecContext *p_context;
132 int i_codec_id, i_cat;
135 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
138 if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
140 /* handed chroma output */
144 i_codec_id = CODEC_ID_RAWVIDEO;
145 psz_namecodec = "Raw video";
149 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
151 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
155 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
157 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
161 /* Initialization must be done before avcodec_find_decoder() */
162 E_(InitLibavcodec)(p_this);
164 p_codec = avcodec_find_encoder( i_codec_id );
167 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
171 /* Allocate the memory needed to store the decoder's structure */
172 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
174 msg_Err( p_enc, "out of memory" );
177 p_enc->p_sys = p_sys;
178 p_sys->p_codec = p_codec;
180 p_enc->pf_encode_video = EncodeVideo;
181 p_enc->pf_encode_audio = EncodeAudio;
183 p_sys->p_buffer_out = NULL;
184 p_sys->p_buffer = NULL;
187 p_sys->p_context = p_context = avcodec_alloc_context();
189 /* Set CPU capabilities */
190 p_context->dsp_mask = 0;
191 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
193 p_context->dsp_mask |= FF_MM_MMX;
195 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
197 p_context->dsp_mask |= FF_MM_MMXEXT;
199 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
201 p_context->dsp_mask |= FF_MM_3DNOW;
203 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
205 p_context->dsp_mask |= FF_MM_SSE;
206 p_context->dsp_mask |= FF_MM_SSE2;
209 if( p_enc->fmt_in.i_cat == VIDEO_ES )
211 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
213 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
214 p_enc->fmt_in.video.i_height );
219 p_context->width = p_enc->fmt_in.video.i_width;
220 p_context->height = p_enc->fmt_in.video.i_height;
222 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
223 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
225 /* Defaults from ffmpeg.c */
226 p_context->qblur = 0.5;
227 p_context->qcompress = 0.5;
228 p_context->b_quant_offset = 1.25;
229 p_context->b_quant_factor = 1.25;
230 p_context->i_quant_offset = 0.0;
231 p_context->i_quant_factor = -0.8;
233 p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
234 p_context->max_b_frames =
235 __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
236 p_context->b_frame_strategy = 0;
238 #if LIBAVCODEC_BUILD >= 4687
239 p_context->sample_aspect_ratio =
240 (AVRational){ p_enc->fmt_in.video.i_aspect *
241 (int64_t)p_context->height / p_context->width,
242 VOUT_ASPECT_FACTOR };
244 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
248 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
250 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
252 if ( p_enc->b_strict_rc )
254 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
255 p_context->rc_buffer_size = p_enc->i_rc_buffer_size;
256 p_context->rc_buffer_aggressivity = p_enc->f_rc_buffer_aggressivity;
259 if ( p_enc->f_i_quant_factor != 0.0 )
261 p_context->i_quant_factor = p_enc->f_i_quant_factor;
264 #if LIBAVCODEC_BUILD >= 4690
265 p_context->noise_reduction = p_enc->i_noise_reduction;
268 if ( p_enc->b_mpeg4_matrix )
270 p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
271 p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
274 if ( p_enc->b_pre_me )
276 p_context->pre_me = 1;
277 p_context->me_pre_cmp = FF_CMP_CHROMA;
280 if ( p_enc->b_interlace )
282 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
283 #if LIBAVCODEC_BUILD >= 4698
284 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
288 if ( p_enc->b_trellis )
290 p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
293 #if LIBAVCODEC_BUILD >= 4702
294 if ( p_enc->i_threads >= 1 )
296 p_context->thread_count = p_enc->i_threads;
300 if( p_enc->i_vtolerance > 0 )
302 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
305 p_context->mb_qmin = p_context->qmin = p_enc->i_qmin;
306 p_context->mb_qmax = p_context->qmax = p_enc->i_qmax;
307 p_context->max_qdiff = 3;
309 p_context->mb_decision = p_enc->i_hq;
311 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
313 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
314 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
315 p_context->channels = p_enc->fmt_in.audio.i_channels;
318 /* Misc parameters */
319 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
321 if( i_codec_id == CODEC_ID_RAWVIDEO )
323 /* XXX: hack: Force same codec (will be handled by transcode) */
324 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
325 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
328 /* Make sure we get extradata filled by the encoder */
329 p_context->extradata_size = 0;
330 p_context->extradata = NULL;
331 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
333 if( avcodec_open( p_context, p_codec ) )
335 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
337 p_context->channels = 2;
338 p_enc->fmt_in.audio.i_channels = 2; // FIXME
339 if( avcodec_open( p_context, p_codec ) )
341 msg_Err( p_enc, "cannot open encoder" );
345 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
349 msg_Err( p_enc, "cannot open encoder" );
355 p_enc->fmt_out.i_extra = p_context->extradata_size;
356 p_enc->fmt_out.p_extra = p_context->extradata;
357 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
359 if( p_enc->fmt_in.i_cat == AUDIO_ES )
361 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
362 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
363 p_sys->p_buffer = malloc( p_sys->i_frame_size );
366 p_sys->i_last_ref_pts = 0;
367 p_sys->i_buggy_pts_detect = 0;
368 p_sys->i_samples_delay = 0;
370 p_sys->i_last_pts = 0;
372 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
377 /****************************************************************************
378 * Ffmpeg threading system
379 ****************************************************************************/
380 #if LIBAVCODEC_BUILD >= 4702
381 static int FfmpegThread( struct thread_context_t *p_context )
383 while ( !p_context->b_die && !p_context->b_error )
385 vlc_mutex_lock( &p_context->lock );
386 while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
388 vlc_cond_wait( &p_context->cond, &p_context->lock );
390 p_context->b_work = 0;
391 vlc_mutex_unlock( &p_context->lock );
392 if ( p_context->b_die || p_context->b_error )
395 if ( p_context->pf_func )
397 p_context->i_ret = p_context->pf_func( p_context->p_context,
401 vlc_mutex_lock( &p_context->lock );
402 p_context->b_done = 1;
403 vlc_cond_signal( &p_context->cond );
404 vlc_mutex_unlock( &p_context->lock );
410 static int FfmpegExecute( AVCodecContext *s,
411 int (*pf_func)(AVCodecContext *c2, void *arg2),
412 void **arg, int *ret, int count )
414 struct thread_context_t ** pp_contexts =
415 (struct thread_context_t **)s->thread_opaque;
418 /* Note, we can be certain that this is not called with the same
419 * AVCodecContext by different threads at the same time */
420 for ( i = 0; i < count; i++ )
422 vlc_mutex_lock( &pp_contexts[i]->lock );
423 pp_contexts[i]->arg = arg[i];
424 pp_contexts[i]->pf_func = pf_func;
425 pp_contexts[i]->i_ret = 12345;
426 pp_contexts[i]->b_work = 1;
427 vlc_cond_signal( &pp_contexts[i]->cond );
428 vlc_mutex_unlock( &pp_contexts[i]->lock );
430 for ( i = 0; i < count; i++ )
432 vlc_mutex_lock( &pp_contexts[i]->lock );
433 while ( !pp_contexts[i]->b_done )
435 vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
437 pp_contexts[i]->b_done = 0;
438 pp_contexts[i]->pf_func = NULL;
439 vlc_mutex_unlock( &pp_contexts[i]->lock );
443 ret[i] = pp_contexts[i]->i_ret;
451 /****************************************************************************
452 * EncodeVideo: the whole thing
453 ****************************************************************************/
454 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
456 encoder_sys_t *p_sys = p_enc->p_sys;
460 #if LIBAVCODEC_BUILD >= 4702
461 if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
463 struct thread_context_t ** pp_contexts;
467 pp_contexts = malloc( sizeof(struct thread_context_t *)
468 * p_enc->i_threads );
469 p_sys->p_context->thread_opaque = (void *)pp_contexts;
471 for ( i = 0; i < p_enc->i_threads; i++ )
473 pp_contexts[i] = vlc_object_create( p_enc,
474 sizeof(struct thread_context_t) );
475 pp_contexts[i]->p_context = p_sys->p_context;
476 vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
477 vlc_cond_init( p_enc, &pp_contexts[i]->cond );
478 pp_contexts[i]->b_work = 0;
479 pp_contexts[i]->b_done = 0;
480 if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
481 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
483 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
488 p_sys->p_context->execute = FfmpegExecute;
492 memset( &frame, 0, sizeof( AVFrame ) );
493 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
495 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
496 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
499 /* Let ffmpeg select the frame type */
502 frame.repeat_pict = p_pict->i_nb_fields;
504 #if LIBAVCODEC_BUILD >= 4685
505 frame.interlaced_frame = !p_pict->b_progressive;
506 frame.top_field_first = p_pict->b_top_field_first;
509 #if LIBAVCODEC_BUILD < 4702
510 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
511 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
512 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
513 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
518 frame.pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
520 if ( p_enc->b_hurry_up && frame.pts != AV_NOPTS_VALUE )
522 mtime_t current_date = mdate();
524 if ( current_date + HURRY_UP_GUARD3 > frame.pts )
526 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
527 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
528 msg_Dbg( p_enc, "hurry up mode 3" );
532 p_sys->p_context->mb_decision = p_enc->i_hq;
534 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
536 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
537 #if LIBAVCODEC_BUILD >= 4690
538 p_sys->p_context->noise_reduction = p_enc->i_noise_reduction
539 + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
541 msg_Dbg( p_enc, "hurry up mode 2" );
545 if ( p_enc->b_trellis )
546 p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
547 #if LIBAVCODEC_BUILD >= 4690
548 p_sys->p_context->noise_reduction =
549 p_enc->i_noise_reduction;
554 if ( current_date + HURRY_UP_GUARD1 > frame.pts )
556 frame.pict_type = FF_P_TYPE;
557 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
563 frame.pts = AV_NOPTS_VALUE;
566 if ( frame.pts != AV_NOPTS_VALUE && frame.pts != 0 )
568 if ( p_sys->i_last_pts == frame.pts )
570 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
571 "same PTS (" I64Fd ")", frame.pts );
576 p_sys->i_last_pts = frame.pts;
580 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
581 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
585 block_t *p_block = block_New( p_enc, i_out );
586 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
588 if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
589 p_sys->p_context->coded_frame->pts != 0 &&
590 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
592 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
594 /* FIXME, 3-2 pulldown is not handled correctly */
595 p_block->i_length = I64C(1000000) *
596 p_enc->fmt_in.video.i_frame_rate_base /
597 p_enc->fmt_in.video.i_frame_rate;
598 p_block->i_pts = p_sys->p_context->coded_frame->pts;
600 if( !p_sys->p_context->delay ||
601 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
602 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
604 p_block->i_dts = p_block->i_pts;
608 if( p_sys->i_last_ref_pts )
610 p_block->i_dts = p_sys->i_last_ref_pts;
614 /* Let's put something sensible */
615 p_block->i_dts = p_block->i_pts;
618 p_sys->i_last_ref_pts = p_block->i_pts;
623 /* Buggy libavcodec which doesn't update coded_frame->pts
625 p_block->i_length = I64C(1000000) *
626 p_enc->fmt_in.video.i_frame_rate_base /
627 p_enc->fmt_in.video.i_frame_rate;
628 p_block->i_dts = p_block->i_pts = p_pict->date;
631 switch ( p_sys->p_context->coded_frame->pict_type )
634 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
637 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
640 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
650 /****************************************************************************
651 * EncodeAudio: the whole thing
652 ****************************************************************************/
653 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
655 encoder_sys_t *p_sys = p_enc->p_sys;
656 block_t *p_block, *p_chain = NULL;
658 char *p_buffer = p_aout_buf->p_buffer;
659 int i_samples = p_aout_buf->i_nb_samples;
660 int i_samples_delay = p_sys->i_samples_delay;
662 p_sys->i_pts = p_aout_buf->start_date -
663 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
664 (mtime_t)p_enc->fmt_in.audio.i_rate;
666 p_sys->i_samples_delay += i_samples;
668 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
673 if( i_samples_delay )
675 /* Take care of the left-over from last time */
676 int i_delay_size = i_samples_delay * 2 *
677 p_sys->p_context->channels;
678 int i_size = p_sys->i_frame_size - i_delay_size;
680 p_samples = (int16_t *)p_sys->p_buffer;
681 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
682 p_buffer -= i_delay_size;
683 i_samples += i_samples_delay;
688 p_samples = (int16_t *)p_buffer;
691 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
692 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
696 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
698 if( i_out < 0 ) break;
700 p_buffer += p_sys->i_frame_size;
701 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
702 i_samples -= p_sys->p_context->frame_size;
704 if( i_out == 0 ) continue;
706 p_block = block_New( p_enc, i_out );
707 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
709 p_block->i_length = (mtime_t)1000000 *
710 (mtime_t)p_sys->p_context->frame_size /
711 (mtime_t)p_sys->p_context->sample_rate;
713 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
716 p_sys->i_pts += p_block->i_length;
717 block_ChainAppend( &p_chain, p_block );
720 /* Backup the remaining raw samples */
723 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
724 p_sys->p_context->channels, p_buffer,
725 i_samples * 2 * p_sys->p_context->channels );
731 /*****************************************************************************
732 * CloseEncoder: ffmpeg encoder destruction
733 *****************************************************************************/
734 void E_(CloseEncoder)( vlc_object_t *p_this )
736 encoder_t *p_enc = (encoder_t *)p_this;
737 encoder_sys_t *p_sys = p_enc->p_sys;
739 #if LIBAVCODEC_BUILD >= 4702
740 if ( p_sys->b_inited && p_enc->i_threads >= 1 )
743 struct thread_context_t ** pp_contexts =
744 (struct thread_context_t **)p_sys->p_context->thread_opaque;
745 for ( i = 0; i < p_enc->i_threads; i++ )
747 pp_contexts[i]->b_die = 1;
748 vlc_cond_signal( &pp_contexts[i]->cond );
749 vlc_thread_join( pp_contexts[i] );
750 vlc_mutex_destroy( &pp_contexts[i]->lock );
751 vlc_cond_destroy( &pp_contexts[i]->cond );
752 vlc_object_destroy( pp_contexts[i] );
759 avcodec_close( p_sys->p_context );
760 av_free( p_sys->p_context );
762 if( p_sys->p_buffer ) free( p_sys->p_buffer );
763 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );