1 /*****************************************************************************
2 * encoder.c: video and audio encoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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 *****************************************************************************/
33 #include <vlc/decoder.h>
37 #ifdef HAVE_FFMPEG_AVCODEC_H
38 # include <ffmpeg/avcodec.h>
43 #if LIBAVCODEC_BUILD < 4704
44 # define AV_NOPTS_VALUE 0
46 #if LIBAVCODEC_BUILD < 4684
47 # define FF_QP2LAMBDA 118
52 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
53 #define HURRY_UP_GUARD1 (450000)
54 #define HURRY_UP_GUARD2 (300000)
55 #define HURRY_UP_GUARD3 (100000)
57 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
59 /*****************************************************************************
61 *****************************************************************************/
62 int E_(OpenEncoder) ( vlc_object_t * );
63 void E_(CloseEncoder)( vlc_object_t * );
65 static block_t *EncodeVideo( encoder_t *, picture_t * );
66 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
68 struct thread_context_t;
69 static int FfmpegThread( struct thread_context_t *p_context );
70 static int FfmpegExecute( AVCodecContext *s,
71 int (*pf_func)(AVCodecContext *c2, void *arg2),
72 void **arg, int *ret, int count );
74 /*****************************************************************************
75 * thread_context_t : for multithreaded encoding
76 *****************************************************************************/
77 #if LIBAVCODEC_BUILD >= 4702
78 struct thread_context_t
82 AVCodecContext *p_context;
83 int (* pf_func)(AVCodecContext *c, void *arg);
89 vlc_bool_t b_work, b_done;
93 /*****************************************************************************
94 * encoder_sys_t : ffmpeg encoder descriptor
95 *****************************************************************************/
102 AVCodecContext *p_context;
113 mtime_t i_last_ref_pts;
114 mtime_t i_buggy_pts_detect;
125 /* Encoding settings */
132 vlc_bool_t b_strict_rc;
133 int i_rc_buffer_size;
134 float f_rc_buffer_aggressivity;
136 vlc_bool_t b_hurry_up;
137 vlc_bool_t b_interlace, b_interlace_me;
138 float f_i_quant_factor;
139 int i_noise_reduction;
140 vlc_bool_t b_mpeg4_matrix;
141 vlc_bool_t b_trellis;
142 int i_quality; /* for VBR */
143 float f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
144 int i_luma_elim, i_chroma_elim;
146 /* Used to work around stupid timestamping behaviour in libavcodec */
148 mtime_t pi_delay_pts[MAX_FRAME_DELAY];
151 static const char *ppsz_enc_options[] = {
152 "keyint", "bframes", "vt", "qmin", "qmax", "hq", "strict-rc",
153 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
154 "interlace", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
155 "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
156 "p-masking", "border-masking", "luma-elim-threshold",
157 "chroma-elim-threshold", NULL
160 /*****************************************************************************
161 * OpenEncoder: probe the encoder
162 *****************************************************************************/
163 extern int16_t IMPORT_SYMBOL ff_mpeg4_default_intra_matrix[];
164 extern int16_t IMPORT_SYMBOL ff_mpeg4_default_non_intra_matrix[];
166 int E_(OpenEncoder)( vlc_object_t *p_this )
168 encoder_t *p_enc = (encoder_t *)p_this;
169 encoder_sys_t *p_sys = p_enc->p_sys;
170 AVCodecContext *p_context;
172 int i_codec_id, i_cat;
176 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
179 if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
181 /* handed chroma output */
185 i_codec_id = CODEC_ID_RAWVIDEO;
186 psz_namecodec = "Raw video";
190 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
192 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
196 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
198 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
202 /* Initialization must be done before avcodec_find_decoder() */
203 E_(InitLibavcodec)(p_this);
205 p_codec = avcodec_find_encoder( i_codec_id );
208 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
212 /* Allocate the memory needed to store the decoder's structure */
213 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
215 msg_Err( p_enc, "out of memory" );
218 memset( p_sys, 0, sizeof(encoder_sys_t) );
219 p_enc->p_sys = p_sys;
220 p_sys->p_codec = p_codec;
222 p_enc->pf_encode_video = EncodeVideo;
223 p_enc->pf_encode_audio = EncodeAudio;
225 p_sys->p_buffer_out = NULL;
226 p_sys->p_buffer = NULL;
228 p_sys->p_context = p_context = avcodec_alloc_context();
230 /* Set CPU capabilities */
231 p_context->dsp_mask = 0;
232 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
234 p_context->dsp_mask |= FF_MM_MMX;
236 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
238 p_context->dsp_mask |= FF_MM_MMXEXT;
240 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
242 p_context->dsp_mask |= FF_MM_3DNOW;
244 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
246 p_context->dsp_mask |= FF_MM_SSE;
247 p_context->dsp_mask |= FF_MM_SSE2;
250 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
252 var_Get( p_enc, ENC_CFG_PREFIX "keyint", &val );
253 p_sys->i_key_int = val.i_int;
255 var_Get( p_enc, ENC_CFG_PREFIX "bframes", &val );
256 p_sys->i_b_frames = val.i_int;
258 var_Get( p_enc, ENC_CFG_PREFIX "vt", &val );
259 p_sys->i_vtolerance = val.i_int;
261 var_Get( p_enc, ENC_CFG_PREFIX "interlace", &val );
262 p_sys->b_interlace = val.b_bool;
264 var_Get( p_enc, ENC_CFG_PREFIX "interlace-me", &val );
265 p_sys->b_interlace_me = val.b_bool;
267 var_Get( p_enc, ENC_CFG_PREFIX "pre-me", &val );
268 p_sys->b_pre_me = val.b_bool;
270 var_Get( p_enc, ENC_CFG_PREFIX "hurry-up", &val );
271 p_sys->b_hurry_up = val.b_bool;
272 if( p_sys->b_hurry_up )
274 /* hurry up mode needs noise reduction, even small */
275 p_sys->i_noise_reduction = 1;
278 var_Get( p_enc, ENC_CFG_PREFIX "strict-rc", &val );
279 p_sys->b_strict_rc = val.b_bool;
280 var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-size", &val );
281 p_sys->i_rc_buffer_size = val.i_int;
282 var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity", &val );
283 p_sys->f_rc_buffer_aggressivity = val.f_float;
285 var_Get( p_enc, ENC_CFG_PREFIX "i-quant-factor", &val );
286 p_sys->f_i_quant_factor = val.f_float;
288 var_Get( p_enc, ENC_CFG_PREFIX "noise-reduction", &val );
289 p_sys->i_noise_reduction = val.i_int;
291 var_Get( p_enc, ENC_CFG_PREFIX "mpeg4-matrix", &val );
292 p_sys->b_mpeg4_matrix = val.b_bool;
294 var_Get( p_enc, ENC_CFG_PREFIX "qscale", &val );
295 if( val.f_float < 0.01 || val.f_float > 255.0 ) val.f_float = 0;
296 p_sys->i_quality = (int)(FF_QP2LAMBDA * val.f_float + 0.5);
298 var_Get( p_enc, ENC_CFG_PREFIX "hq", &val );
299 if( val.psz_string && *val.psz_string )
301 if( !strcmp( val.psz_string, "rd" ) )
302 p_sys->i_hq = FF_MB_DECISION_RD;
303 else if( !strcmp( val.psz_string, "bits" ) )
304 p_sys->i_hq = FF_MB_DECISION_BITS;
305 else if( !strcmp( val.psz_string, "simple" ) )
306 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
308 p_sys->i_hq = FF_MB_DECISION_RD;
310 if( val.psz_string ) free( val.psz_string );
312 var_Get( p_enc, ENC_CFG_PREFIX "qmin", &val );
313 p_sys->i_qmin = val.i_int;
314 var_Get( p_enc, ENC_CFG_PREFIX "qmax", &val );
315 p_sys->i_qmax = val.i_int;
316 var_Get( p_enc, ENC_CFG_PREFIX "trellis", &val );
317 p_sys->b_trellis = val.b_bool;
319 var_Get( p_enc, ENC_CFG_PREFIX "strict", &val );
320 if( val.i_int < - 1 || val.i_int > 1 ) val.i_int = 0;
321 p_context->strict_std_compliance = val.i_int;
323 var_Get( p_enc, ENC_CFG_PREFIX "lumi-masking", &val );
324 p_sys->f_lumi_masking = val.f_float;
325 var_Get( p_enc, ENC_CFG_PREFIX "dark-masking", &val );
326 p_sys->f_dark_masking = val.f_float;
327 var_Get( p_enc, ENC_CFG_PREFIX "p-masking", &val );
328 p_sys->f_p_masking = val.f_float;
329 var_Get( p_enc, ENC_CFG_PREFIX "border-masking", &val );
330 p_sys->f_border_masking = val.f_float;
331 var_Get( p_enc, ENC_CFG_PREFIX "luma-elim-threshold", &val );
332 p_sys->i_luma_elim = val.i_int;
333 var_Get( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold", &val );
334 p_sys->i_chroma_elim = val.i_int;
336 if( p_enc->fmt_in.i_cat == VIDEO_ES )
338 int i_aspect_num, i_aspect_den;
340 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
342 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
343 p_enc->fmt_in.video.i_height );
348 p_context->width = p_enc->fmt_in.video.i_width;
349 p_context->height = p_enc->fmt_in.video.i_height;
351 #if LIBAVCODEC_BUILD >= 4754
352 p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
353 p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
355 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
356 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
359 /* Defaults from ffmpeg.c */
360 p_context->qblur = 0.5;
361 p_context->qcompress = 0.5;
362 p_context->b_quant_offset = 1.25;
363 p_context->b_quant_factor = 1.25;
364 p_context->i_quant_offset = 0.0;
365 p_context->i_quant_factor = -0.8;
367 p_context->lumi_masking = p_sys->f_lumi_masking;
368 p_context->dark_masking = p_sys->f_dark_masking;
369 p_context->p_masking = p_sys->f_p_masking;
370 #if LIBAVCODEC_BUILD >= 4741
371 p_context->border_masking = p_sys->f_border_masking;
373 p_context->luma_elim_threshold = p_sys->i_luma_elim;
374 p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
376 if( p_sys->i_key_int > 0 )
377 p_context->gop_size = p_sys->i_key_int;
378 p_context->max_b_frames =
379 __MAX( __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES ), 0 );
380 p_context->b_frame_strategy = 0;
382 #if LIBAVCODEC_BUILD >= 4687
383 av_reduce( &i_aspect_num, &i_aspect_den,
384 p_enc->fmt_in.video.i_aspect,
385 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
386 av_reduce( &p_context->sample_aspect_ratio.num,
387 &p_context->sample_aspect_ratio.den,
388 i_aspect_num * (int64_t)p_context->height,
389 i_aspect_den * (int64_t)p_context->width, 1 << 30 );
391 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
395 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
397 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
398 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
399 #if LIBAVCODEC_BUILD >= 4714
400 if( p_codec->pix_fmts )
402 const enum PixelFormat *p = p_codec->pix_fmts;
403 for( ; *p != -1; p++ )
405 if( *p == p_context->pix_fmt ) break;
407 if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
408 p_enc->fmt_in.i_codec = E_(GetVlcChroma)( p_context->pix_fmt );
411 p_enc->fmt_in.i_codec = E_(GetVlcChroma)( p_context->pix_fmt );
414 if ( p_sys->b_strict_rc )
416 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
417 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
418 /* This is from ffmpeg's ffmpeg.c : */
419 p_context->rc_initial_buffer_occupancy
420 = p_sys->i_rc_buffer_size * 3/4;
421 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
424 if ( p_sys->f_i_quant_factor != 0.0 )
425 p_context->i_quant_factor = p_sys->f_i_quant_factor;
427 #if LIBAVCODEC_BUILD >= 4690
428 p_context->noise_reduction = p_sys->i_noise_reduction;
431 if ( p_sys->b_mpeg4_matrix )
433 p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
434 p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
437 if ( p_sys->b_pre_me )
439 p_context->pre_me = 1;
440 p_context->me_pre_cmp = FF_CMP_CHROMA;
443 if ( p_sys->b_interlace )
445 if ( p_context->height <= 280 )
448 "disabling interlaced video because height=%d <= 280",
453 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
454 #if LIBAVCODEC_BUILD >= 4698
455 if ( p_sys->b_interlace_me )
456 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
461 if ( p_sys->b_trellis )
462 p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
464 if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
465 p_context->flags |= CODEC_FLAG_QSCALE;
467 #if LIBAVCODEC_BUILD >= 4702
468 if ( p_enc->i_threads >= 1 )
469 p_context->thread_count = p_enc->i_threads;
472 if( p_sys->i_vtolerance > 0 )
473 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
475 if( p_sys->i_qmin > 0 )
476 p_context->mb_qmin = p_context->qmin = p_sys->i_qmin;
477 if( p_sys->i_qmax > 0 )
478 p_context->mb_qmax = p_context->qmax = p_sys->i_qmax;
479 p_context->max_qdiff = 3;
481 p_context->mb_decision = p_sys->i_hq;
483 if( p_sys->i_quality )
485 p_context->flags |= CODEC_FLAG_QSCALE;
486 #if LIBAVCODEC_BUILD >= 4668
487 p_context->global_quality = p_sys->i_quality;
491 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
493 /* work around bug in libmp3lame encoding */
494 if( i_codec_id == CODEC_ID_MP3 && p_enc->fmt_in.audio.i_channels > 2 )
495 p_enc->fmt_in.audio.i_channels = 2;
497 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
498 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
499 p_context->channels = p_enc->fmt_in.audio.i_channels;
502 /* Misc parameters */
503 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
505 if( i_codec_id == CODEC_ID_RAWVIDEO )
507 /* XXX: hack: Force same codec (will be handled by transcode) */
508 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
509 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
512 /* Make sure we get extradata filled by the encoder */
513 p_context->extradata_size = 0;
514 p_context->extradata = NULL;
515 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
517 if( avcodec_open( p_context, p_codec ) )
519 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
521 p_context->channels = 2;
522 p_enc->fmt_in.audio.i_channels = 2; // FIXME
523 if( avcodec_open( p_context, p_codec ) )
525 msg_Err( p_enc, "cannot open encoder" );
529 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
533 msg_Err( p_enc, "cannot open encoder" );
539 p_enc->fmt_out.i_extra = p_context->extradata_size;
540 p_enc->fmt_out.p_extra = p_context->extradata;
541 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
543 if( p_enc->fmt_in.i_cat == AUDIO_ES )
545 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
546 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
547 p_sys->p_buffer = malloc( p_sys->i_frame_size );
550 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
555 /****************************************************************************
556 * Ffmpeg threading system
557 ****************************************************************************/
558 #if LIBAVCODEC_BUILD >= 4702
559 static int FfmpegThread( struct thread_context_t *p_context )
561 while ( !p_context->b_die && !p_context->b_error )
563 vlc_mutex_lock( &p_context->lock );
564 while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
566 vlc_cond_wait( &p_context->cond, &p_context->lock );
568 p_context->b_work = 0;
569 vlc_mutex_unlock( &p_context->lock );
570 if ( p_context->b_die || p_context->b_error )
573 if ( p_context->pf_func )
575 p_context->i_ret = p_context->pf_func( p_context->p_context,
579 vlc_mutex_lock( &p_context->lock );
580 p_context->b_done = 1;
581 vlc_cond_signal( &p_context->cond );
582 vlc_mutex_unlock( &p_context->lock );
588 static int FfmpegExecute( AVCodecContext *s,
589 int (*pf_func)(AVCodecContext *c2, void *arg2),
590 void **arg, int *ret, int count )
592 struct thread_context_t ** pp_contexts =
593 (struct thread_context_t **)s->thread_opaque;
596 /* Note, we can be certain that this is not called with the same
597 * AVCodecContext by different threads at the same time */
598 for ( i = 0; i < count; i++ )
600 vlc_mutex_lock( &pp_contexts[i]->lock );
601 pp_contexts[i]->arg = arg[i];
602 pp_contexts[i]->pf_func = pf_func;
603 pp_contexts[i]->i_ret = 12345;
604 pp_contexts[i]->b_work = 1;
605 vlc_cond_signal( &pp_contexts[i]->cond );
606 vlc_mutex_unlock( &pp_contexts[i]->lock );
608 for ( i = 0; i < count; i++ )
610 vlc_mutex_lock( &pp_contexts[i]->lock );
611 while ( !pp_contexts[i]->b_done )
613 vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
615 pp_contexts[i]->b_done = 0;
616 pp_contexts[i]->pf_func = NULL;
617 vlc_mutex_unlock( &pp_contexts[i]->lock );
621 ret[i] = pp_contexts[i]->i_ret;
629 /****************************************************************************
630 * EncodeVideo: the whole thing
631 ****************************************************************************/
632 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
634 encoder_sys_t *p_sys = p_enc->p_sys;
638 #if LIBAVCODEC_BUILD >= 4702
639 if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
641 struct thread_context_t ** pp_contexts;
645 pp_contexts = malloc( sizeof(struct thread_context_t *)
646 * p_enc->i_threads );
647 p_sys->p_context->thread_opaque = (void *)pp_contexts;
649 for ( i = 0; i < p_enc->i_threads; i++ )
651 pp_contexts[i] = vlc_object_create( p_enc,
652 sizeof(struct thread_context_t) );
653 pp_contexts[i]->p_context = p_sys->p_context;
654 vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
655 vlc_cond_init( p_enc, &pp_contexts[i]->cond );
656 pp_contexts[i]->b_work = 0;
657 pp_contexts[i]->b_done = 0;
658 if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
659 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
661 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
666 p_sys->p_context->execute = FfmpegExecute;
670 memset( &frame, 0, sizeof( AVFrame ) );
671 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
673 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
674 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
677 /* Let ffmpeg select the frame type */
680 frame.repeat_pict = p_pict->i_nb_fields - 2;
682 #if LIBAVCODEC_BUILD >= 4685
683 frame.interlaced_frame = !p_pict->b_progressive;
684 frame.top_field_first = !!p_pict->b_top_field_first;
687 #if LIBAVCODEC_BUILD < 4702
688 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
689 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
690 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
691 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
696 frame.pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
698 if ( p_sys->b_hurry_up && frame.pts != AV_NOPTS_VALUE )
700 mtime_t current_date = mdate();
702 if ( current_date + HURRY_UP_GUARD3 > frame.pts )
704 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
705 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
706 msg_Dbg( p_enc, "hurry up mode 3" );
710 p_sys->p_context->mb_decision = p_sys->i_hq;
712 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
714 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
715 #if LIBAVCODEC_BUILD >= 4690
716 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
717 + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
719 msg_Dbg( p_enc, "hurry up mode 2" );
723 if ( p_sys->b_trellis )
724 p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
725 #if LIBAVCODEC_BUILD >= 4690
726 p_sys->p_context->noise_reduction =
727 p_sys->i_noise_reduction;
732 if ( current_date + HURRY_UP_GUARD1 > frame.pts )
734 frame.pict_type = FF_P_TYPE;
735 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
741 frame.pts = AV_NOPTS_VALUE;
744 if ( frame.pts != AV_NOPTS_VALUE && frame.pts != 0 )
746 if ( p_sys->i_last_pts == frame.pts )
748 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
749 "same PTS (" I64Fd ")", frame.pts );
752 else if ( p_sys->i_last_pts > frame.pts )
754 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
755 "past (current: " I64Fd ", last: "I64Fd")",
756 frame.pts, p_sys->i_last_pts );
761 p_sys->i_last_pts = frame.pts;
765 frame.quality = p_sys->i_quality;
767 /* Ugly work-around for stupid libavcodec behaviour */
768 #if LIBAVCODEC_BUILD >= 4722
770 p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts;
771 frame.pts = p_sys->i_framenum * AV_TIME_BASE *
772 p_enc->fmt_in.video.i_frame_rate_base;
773 frame.pts += p_enc->fmt_in.video.i_frame_rate - 1;
774 frame.pts /= p_enc->fmt_in.video.i_frame_rate;
776 /* End work-around */
778 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
779 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
783 block_t *p_block = block_New( p_enc, i_out );
784 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
786 /* FIXME, 3-2 pulldown is not handled correctly */
787 p_block->i_length = I64C(1000000) *
788 p_enc->fmt_in.video.i_frame_rate_base /
789 p_enc->fmt_in.video.i_frame_rate;
791 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
793 /* No delay -> output pts == input pts */
794 p_block->i_pts = p_block->i_dts = p_pict->date;
796 else if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
797 p_sys->p_context->coded_frame->pts != 0 &&
798 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
800 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
801 p_block->i_pts = p_sys->p_context->coded_frame->pts;
803 /* Ugly work-around for stupid libavcodec behaviour */
804 #if LIBAVCODEC_BUILD >= 4722
806 int64_t i_framenum = p_block->i_pts *
807 p_enc->fmt_in.video.i_frame_rate /
808 p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE;
810 p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY];
813 /* End work-around */
815 if( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
816 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE )
818 p_block->i_dts = p_block->i_pts;
822 if( p_sys->i_last_ref_pts )
824 p_block->i_dts = p_sys->i_last_ref_pts;
828 /* Let's put something sensible */
829 p_block->i_dts = p_block->i_pts;
832 p_sys->i_last_ref_pts = p_block->i_pts;
837 /* Buggy libavcodec which doesn't update coded_frame->pts
839 p_block->i_dts = p_block->i_pts = p_pict->date;
842 switch ( p_sys->p_context->coded_frame->pict_type )
845 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
848 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
851 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
861 /****************************************************************************
862 * EncodeAudio: the whole thing
863 ****************************************************************************/
864 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
866 encoder_sys_t *p_sys = p_enc->p_sys;
867 block_t *p_block, *p_chain = NULL;
869 char *p_buffer = p_aout_buf->p_buffer;
870 int i_samples = p_aout_buf->i_nb_samples;
871 int i_samples_delay = p_sys->i_samples_delay;
873 p_sys->i_pts = p_aout_buf->start_date -
874 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
875 (mtime_t)p_enc->fmt_in.audio.i_rate;
877 p_sys->i_samples_delay += i_samples;
879 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
884 if( i_samples_delay )
886 /* Take care of the left-over from last time */
887 int i_delay_size = i_samples_delay * 2 *
888 p_sys->p_context->channels;
889 int i_size = p_sys->i_frame_size - i_delay_size;
891 p_samples = (int16_t *)p_sys->p_buffer;
892 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
893 p_buffer -= i_delay_size;
894 i_samples += i_samples_delay;
899 p_samples = (int16_t *)p_buffer;
902 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
903 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
907 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
909 if( i_out < 0 ) break;
911 p_buffer += p_sys->i_frame_size;
912 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
913 i_samples -= p_sys->p_context->frame_size;
915 if( i_out == 0 ) continue;
917 p_block = block_New( p_enc, i_out );
918 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
920 p_block->i_length = (mtime_t)1000000 *
921 (mtime_t)p_sys->p_context->frame_size /
922 (mtime_t)p_sys->p_context->sample_rate;
924 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
927 p_sys->i_pts += p_block->i_length;
928 block_ChainAppend( &p_chain, p_block );
931 /* Backup the remaining raw samples */
934 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
935 p_sys->p_context->channels, p_buffer,
936 i_samples * 2 * p_sys->p_context->channels );
942 /*****************************************************************************
943 * CloseEncoder: ffmpeg encoder destruction
944 *****************************************************************************/
945 void E_(CloseEncoder)( vlc_object_t *p_this )
947 encoder_t *p_enc = (encoder_t *)p_this;
948 encoder_sys_t *p_sys = p_enc->p_sys;
950 #if LIBAVCODEC_BUILD >= 4702
951 if ( p_sys->b_inited && p_enc->i_threads >= 1 )
954 struct thread_context_t ** pp_contexts =
955 (struct thread_context_t **)p_sys->p_context->thread_opaque;
956 for ( i = 0; i < p_enc->i_threads; i++ )
958 pp_contexts[i]->b_die = 1;
959 vlc_cond_signal( &pp_contexts[i]->cond );
960 vlc_thread_join( pp_contexts[i] );
961 vlc_mutex_destroy( &pp_contexts[i]->lock );
962 vlc_cond_destroy( &pp_contexts[i]->cond );
963 vlc_object_destroy( pp_contexts[i] );
970 avcodec_close( p_sys->p_context );
971 av_free( p_sys->p_context );
973 if( p_sys->p_buffer ) free( p_sys->p_buffer );
974 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );