* modules/codec/ffmpeg/encoder.c: compilation fix for ffmpeg 0.4.8 (not sure it's...
[vlc.git] / modules / codec / ffmpeg / encoder.c
blob71f8b46d7f719872b72c21251fb6a4f973277fcf
1 /*****************************************************************************
2 * encoder.c: video and audio encoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id$
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 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/aout.h>
32 #include <vlc/sout.h>
33 #include <vlc/decoder.h>
35 /* ffmpeg header */
36 #define HAVE_MMX
37 #ifdef HAVE_FFMPEG_AVCODEC_H
38 # include <ffmpeg/avcodec.h>
39 #else
40 # include <avcodec.h>
41 #endif
43 #if LIBAVCODEC_BUILD < 4704
44 # define AV_NOPTS_VALUE 0
45 #endif
46 #if LIBAVCODEC_BUILD < 4684
47 # define FF_QP2LAMBDA 118
48 #endif
50 #include "ffmpeg.h"
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 /*****************************************************************************
60 * Local prototypes
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
80 VLC_COMMON_MEMBERS
82 AVCodecContext *p_context;
83 int (* pf_func)(AVCodecContext *c, void *arg);
84 void *arg;
85 int i_ret;
87 vlc_mutex_t lock;
88 vlc_cond_t cond;
89 vlc_bool_t b_work, b_done;
91 #endif
93 /*****************************************************************************
94 * encoder_sys_t : ffmpeg encoder descriptor
95 *****************************************************************************/
96 struct encoder_sys_t
99 * Ffmpeg properties
101 AVCodec *p_codec;
102 AVCodecContext *p_context;
105 * Common properties
107 char *p_buffer;
108 char *p_buffer_out;
111 * Video properties
113 mtime_t i_last_ref_pts;
114 mtime_t i_buggy_pts_detect;
115 mtime_t i_last_pts;
116 vlc_bool_t b_inited;
119 * Audio properties
121 int i_frame_size;
122 int i_samples_delay;
123 mtime_t i_pts;
125 /* Encoding settings */
126 int i_key_int;
127 int i_b_frames;
128 int i_vtolerance;
129 int i_qmin;
130 int i_qmax;
131 int i_hq;
132 vlc_bool_t b_strict_rc;
133 int i_rc_buffer_size;
134 float f_rc_buffer_aggressivity;
135 vlc_bool_t b_pre_me;
136 vlc_bool_t b_hurry_up;
137 vlc_bool_t b_interlace;
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 */
144 /* Used to work around stupid timestamping behaviour in libavcodec */
145 uint64_t i_framenum;
146 mtime_t pi_delay_pts[MAX_FRAME_DELAY];
149 static const char *ppsz_enc_options[] = {
150 "keyint", "bframes", "vt", "qmin", "qmax", "hq", "strict_rc",
151 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
152 "interlace", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
153 "trellis", "qscale", "strict", NULL
156 /*****************************************************************************
157 * OpenEncoder: probe the encoder
158 *****************************************************************************/
159 extern int16_t ff_mpeg4_default_intra_matrix[];
160 extern int16_t ff_mpeg4_default_non_intra_matrix[];
162 int E_(OpenEncoder)( vlc_object_t *p_this )
164 encoder_t *p_enc = (encoder_t *)p_this;
165 encoder_sys_t *p_sys = p_enc->p_sys;
166 AVCodecContext *p_context;
167 AVCodec *p_codec;
168 int i_codec_id, i_cat;
169 char *psz_namecodec;
170 vlc_value_t val;
172 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
173 &psz_namecodec ) )
175 if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
177 /* handed chroma output */
178 return VLC_EGENERIC;
180 i_cat = VIDEO_ES;
181 i_codec_id = CODEC_ID_RAWVIDEO;
182 psz_namecodec = "Raw video";
186 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
188 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
189 return VLC_EGENERIC;
192 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
194 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
195 return VLC_EGENERIC;
198 /* Initialization must be done before avcodec_find_decoder() */
199 E_(InitLibavcodec)(p_this);
201 p_codec = avcodec_find_encoder( i_codec_id );
202 if( !p_codec )
204 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
205 return VLC_EGENERIC;
208 /* Allocate the memory needed to store the decoder's structure */
209 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
211 msg_Err( p_enc, "out of memory" );
212 return VLC_EGENERIC;
214 memset( p_sys, 0, sizeof(encoder_sys_t) );
215 p_enc->p_sys = p_sys;
216 p_sys->p_codec = p_codec;
218 p_enc->pf_encode_video = EncodeVideo;
219 p_enc->pf_encode_audio = EncodeAudio;
221 p_sys->p_buffer_out = NULL;
222 p_sys->p_buffer = NULL;
224 p_sys->p_context = p_context = avcodec_alloc_context();
226 /* Set CPU capabilities */
227 p_context->dsp_mask = 0;
228 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
230 p_context->dsp_mask |= FF_MM_MMX;
232 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
234 p_context->dsp_mask |= FF_MM_MMXEXT;
236 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
238 p_context->dsp_mask |= FF_MM_3DNOW;
240 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
242 p_context->dsp_mask |= FF_MM_SSE;
243 p_context->dsp_mask |= FF_MM_SSE2;
246 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
248 var_Get( p_enc, ENC_CFG_PREFIX "keyint", &val );
249 p_sys->i_key_int = val.i_int;
251 var_Get( p_enc, ENC_CFG_PREFIX "bframes", &val );
252 p_sys->i_b_frames = val.i_int;
254 var_Get( p_enc, ENC_CFG_PREFIX "vt", &val );
255 p_sys->i_vtolerance = val.i_int;
257 var_Get( p_enc, ENC_CFG_PREFIX "interlace", &val );
258 p_sys->b_interlace = val.b_bool;
260 var_Get( p_enc, ENC_CFG_PREFIX "pre-me", &val );
261 p_sys->b_pre_me = val.b_bool;
263 var_Get( p_enc, ENC_CFG_PREFIX "hurry-up", &val );
264 p_sys->b_hurry_up = val.b_bool;
265 if( p_sys->b_hurry_up )
267 /* hurry up mode needs noise reduction, even small */
268 p_sys->i_noise_reduction = 1;
271 var_Get( p_enc, ENC_CFG_PREFIX "strict-rc", &val );
272 p_sys->b_strict_rc = val.b_bool;
273 var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-size", &val );
274 p_sys->i_rc_buffer_size = val.i_int;
275 var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity", &val );
276 p_sys->f_rc_buffer_aggressivity = val.f_float;
278 var_Get( p_enc, ENC_CFG_PREFIX "i-quant-factor", &val );
279 p_sys->f_i_quant_factor = val.f_float;
281 var_Get( p_enc, ENC_CFG_PREFIX "noise-reduction", &val );
282 p_sys->i_noise_reduction = val.i_int;
284 var_Get( p_enc, ENC_CFG_PREFIX "mpeg4-matrix", &val );
285 p_sys->b_mpeg4_matrix = val.b_bool;
287 var_Get( p_enc, ENC_CFG_PREFIX "qscale", &val );
288 if( val.f_float < 0.01 || val.f_float > 255.0 ) val.f_float = 0;
289 p_sys->i_quality = (int)(FF_QP2LAMBDA * val.f_float + 0.5);
291 var_Get( p_enc, ENC_CFG_PREFIX "hq", &val );
292 if( val.psz_string && *val.psz_string )
294 if( !strcmp( val.psz_string, "rd" ) )
295 p_sys->i_hq = FF_MB_DECISION_RD;
296 else if( !strcmp( val.psz_string, "bits" ) )
297 p_sys->i_hq = FF_MB_DECISION_BITS;
298 else if( !strcmp( val.psz_string, "simple" ) )
299 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
300 else
301 p_sys->i_hq = FF_MB_DECISION_RD;
303 if( val.psz_string ) free( val.psz_string );
305 var_Get( p_enc, ENC_CFG_PREFIX "qmin", &val );
306 p_sys->i_qmin = val.i_int;
307 var_Get( p_enc, ENC_CFG_PREFIX "qmax", &val );
308 p_sys->i_qmax = val.i_int;
309 var_Get( p_enc, ENC_CFG_PREFIX "trellis", &val );
310 p_sys->b_trellis = val.b_bool;
312 var_Get( p_enc, ENC_CFG_PREFIX "strict", &val );
313 if( val.i_int < - 1 || val.i_int > 1 ) val.i_int = 0;
314 p_context->strict_std_compliance = val.i_int;
316 if( p_enc->fmt_in.i_cat == VIDEO_ES )
318 int i_aspect_num, i_aspect_den;
320 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
322 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
323 p_enc->fmt_in.video.i_height );
324 free( p_sys );
325 return VLC_EGENERIC;
328 p_context->width = p_enc->fmt_in.video.i_width;
329 p_context->height = p_enc->fmt_in.video.i_height;
331 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
332 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
334 /* Defaults from ffmpeg.c */
335 p_context->qblur = 0.5;
336 p_context->qcompress = 0.5;
337 p_context->b_quant_offset = 1.25;
338 p_context->b_quant_factor = 1.25;
339 p_context->i_quant_offset = 0.0;
340 p_context->i_quant_factor = -0.8;
342 if( p_sys->i_key_int > 0 )
343 p_context->gop_size = p_sys->i_key_int;
344 p_context->max_b_frames =
345 __MAX( __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES ), 0 );
346 p_context->b_frame_strategy = 0;
348 #if LIBAVCODEC_BUILD >= 4687
349 av_reduce( &i_aspect_num, &i_aspect_den,
350 p_enc->fmt_in.video.i_aspect,
351 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
352 av_reduce( &p_context->sample_aspect_ratio.num,
353 &p_context->sample_aspect_ratio.den,
354 i_aspect_num *
355 (int64_t)p_context->height / p_context->width,
356 i_aspect_den, 1 << 30 /* something big */ );
357 #else
358 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
359 VOUT_ASPECT_FACTOR;
360 #endif
362 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
364 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
366 if ( p_sys->b_strict_rc )
368 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
369 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
370 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
373 if ( p_sys->f_i_quant_factor != 0.0 )
374 p_context->i_quant_factor = p_sys->f_i_quant_factor;
376 #if LIBAVCODEC_BUILD >= 4690
377 p_context->noise_reduction = p_sys->i_noise_reduction;
378 #endif
380 if ( p_sys->b_mpeg4_matrix )
382 p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
383 p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
386 if ( p_sys->b_pre_me )
388 p_context->pre_me = 1;
389 p_context->me_pre_cmp = FF_CMP_CHROMA;
392 if ( p_sys->b_interlace )
394 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
395 #if LIBAVCODEC_BUILD >= 4698
396 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
397 #endif
400 if ( p_sys->b_trellis )
401 p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
403 #if LIBAVCODEC_BUILD >= 4702
404 if ( p_enc->i_threads >= 1 )
405 p_context->thread_count = p_enc->i_threads;
406 #endif
408 if( p_sys->i_vtolerance > 0 )
409 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
411 if( p_sys->i_qmin > 0 )
412 p_context->mb_qmin = p_context->qmin = p_sys->i_qmin;
413 if( p_sys->i_qmax > 0 )
414 p_context->mb_qmax = p_context->qmax = p_sys->i_qmax;
415 p_context->max_qdiff = 3;
417 p_context->mb_decision = p_sys->i_hq;
419 if( p_sys->i_quality )
421 p_context->flags |= CODEC_FLAG_QSCALE;
422 #if LIBAVCODEC_BUILD >= 4668
423 p_context->global_quality = p_sys->i_quality;
424 #endif
427 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
429 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
430 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
431 p_context->channels = p_enc->fmt_in.audio.i_channels;
434 /* Misc parameters */
435 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
437 if( i_codec_id == CODEC_ID_RAWVIDEO )
439 /* XXX: hack: Force same codec (will be handled by transcode) */
440 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
441 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
444 /* Make sure we get extradata filled by the encoder */
445 p_context->extradata_size = 0;
446 p_context->extradata = NULL;
447 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
449 if( avcodec_open( p_context, p_codec ) )
451 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
453 p_context->channels = 2;
454 p_enc->fmt_in.audio.i_channels = 2; // FIXME
455 if( avcodec_open( p_context, p_codec ) )
457 msg_Err( p_enc, "cannot open encoder" );
458 free( p_sys );
459 return VLC_EGENERIC;
461 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
463 else
465 msg_Err( p_enc, "cannot open encoder" );
466 free( p_sys );
467 return VLC_EGENERIC;
471 p_enc->fmt_out.i_extra = p_context->extradata_size;
472 p_enc->fmt_out.p_extra = p_context->extradata;
473 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
475 if( p_enc->fmt_in.i_cat == AUDIO_ES )
477 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
478 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
479 p_sys->p_buffer = malloc( p_sys->i_frame_size );
482 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
484 return VLC_SUCCESS;
487 /****************************************************************************
488 * Ffmpeg threading system
489 ****************************************************************************/
490 #if LIBAVCODEC_BUILD >= 4702
491 static int FfmpegThread( struct thread_context_t *p_context )
493 while ( !p_context->b_die && !p_context->b_error )
495 vlc_mutex_lock( &p_context->lock );
496 while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
498 vlc_cond_wait( &p_context->cond, &p_context->lock );
500 p_context->b_work = 0;
501 vlc_mutex_unlock( &p_context->lock );
502 if ( p_context->b_die || p_context->b_error )
503 break;
505 if ( p_context->pf_func )
507 p_context->i_ret = p_context->pf_func( p_context->p_context,
508 p_context->arg );
511 vlc_mutex_lock( &p_context->lock );
512 p_context->b_done = 1;
513 vlc_cond_signal( &p_context->cond );
514 vlc_mutex_unlock( &p_context->lock );
517 return 0;
520 static int FfmpegExecute( AVCodecContext *s,
521 int (*pf_func)(AVCodecContext *c2, void *arg2),
522 void **arg, int *ret, int count )
524 struct thread_context_t ** pp_contexts =
525 (struct thread_context_t **)s->thread_opaque;
526 int i;
528 /* Note, we can be certain that this is not called with the same
529 * AVCodecContext by different threads at the same time */
530 for ( i = 0; i < count; i++ )
532 vlc_mutex_lock( &pp_contexts[i]->lock );
533 pp_contexts[i]->arg = arg[i];
534 pp_contexts[i]->pf_func = pf_func;
535 pp_contexts[i]->i_ret = 12345;
536 pp_contexts[i]->b_work = 1;
537 vlc_cond_signal( &pp_contexts[i]->cond );
538 vlc_mutex_unlock( &pp_contexts[i]->lock );
540 for ( i = 0; i < count; i++ )
542 vlc_mutex_lock( &pp_contexts[i]->lock );
543 while ( !pp_contexts[i]->b_done )
545 vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
547 pp_contexts[i]->b_done = 0;
548 pp_contexts[i]->pf_func = NULL;
549 vlc_mutex_unlock( &pp_contexts[i]->lock );
551 if ( ret )
553 ret[i] = pp_contexts[i]->i_ret;
557 return 0;
559 #endif
561 /****************************************************************************
562 * EncodeVideo: the whole thing
563 ****************************************************************************/
564 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
566 encoder_sys_t *p_sys = p_enc->p_sys;
567 AVFrame frame;
568 int i_out, i_plane;
570 #if LIBAVCODEC_BUILD >= 4702
571 if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
573 struct thread_context_t ** pp_contexts;
574 int i;
576 p_sys->b_inited = 1;
577 pp_contexts = malloc( sizeof(struct thread_context_t *)
578 * p_enc->i_threads );
579 p_sys->p_context->thread_opaque = (void *)pp_contexts;
581 for ( i = 0; i < p_enc->i_threads; i++ )
583 pp_contexts[i] = vlc_object_create( p_enc,
584 sizeof(struct thread_context_t) );
585 pp_contexts[i]->p_context = p_sys->p_context;
586 vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
587 vlc_cond_init( p_enc, &pp_contexts[i]->cond );
588 pp_contexts[i]->b_work = 0;
589 pp_contexts[i]->b_done = 0;
590 if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
591 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
593 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
594 return NULL;
598 p_sys->p_context->execute = FfmpegExecute;
600 #endif
602 memset( &frame, 0, sizeof( AVFrame ) );
603 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
605 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
606 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
609 /* Let ffmpeg select the frame type */
610 frame.pict_type = 0;
612 frame.repeat_pict = p_pict->i_nb_fields;
614 #if LIBAVCODEC_BUILD >= 4685
615 frame.interlaced_frame = !p_pict->b_progressive;
616 frame.top_field_first = p_pict->b_top_field_first;
617 #endif
619 #if LIBAVCODEC_BUILD < 4702
620 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
621 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
622 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
623 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
624 #else
625 if( 1 )
626 #endif
628 frame.pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
630 if ( p_sys->b_hurry_up && frame.pts != AV_NOPTS_VALUE )
632 mtime_t current_date = mdate();
634 if ( current_date + HURRY_UP_GUARD3 > frame.pts )
636 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
637 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
638 msg_Dbg( p_enc, "hurry up mode 3" );
640 else
642 p_sys->p_context->mb_decision = p_sys->i_hq;
644 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
646 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
647 #if LIBAVCODEC_BUILD >= 4690
648 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
649 + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
650 #endif
651 msg_Dbg( p_enc, "hurry up mode 2" );
653 else
655 if ( p_sys->b_trellis )
656 p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
657 #if LIBAVCODEC_BUILD >= 4690
658 p_sys->p_context->noise_reduction =
659 p_sys->i_noise_reduction;
660 #endif
664 if ( current_date + HURRY_UP_GUARD1 > frame.pts )
666 frame.pict_type = FF_P_TYPE;
667 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
671 else
673 frame.pts = AV_NOPTS_VALUE;
676 if ( frame.pts != AV_NOPTS_VALUE && frame.pts != 0 )
678 if ( p_sys->i_last_pts == frame.pts )
680 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
681 "same PTS (" I64Fd ")", frame.pts );
682 return NULL;
684 else if ( p_sys->i_last_pts > frame.pts )
686 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
687 "past (current: " I64Fd ", last: "I64Fd")",
688 frame.pts, p_sys->i_last_pts );
689 return NULL;
691 else
693 p_sys->i_last_pts = frame.pts;
697 frame.quality = p_sys->i_quality;
699 /* Ugly work-around for stupid libavcodec behaviour */
700 #if LIBAVCODEC_BUILD >= 4722
701 p_sys->i_framenum++;
702 p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts;
703 frame.pts = p_sys->i_framenum * AV_TIME_BASE *
704 p_enc->fmt_in.video.i_frame_rate_base;
705 frame.pts += p_enc->fmt_in.video.i_frame_rate - 1;
706 frame.pts /= p_enc->fmt_in.video.i_frame_rate;
707 #endif
708 /* End work-around */
710 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
711 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
713 if( i_out > 0 )
715 block_t *p_block = block_New( p_enc, i_out );
716 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
718 /* FIXME, 3-2 pulldown is not handled correctly */
719 p_block->i_length = I64C(1000000) *
720 p_enc->fmt_in.video.i_frame_rate_base /
721 p_enc->fmt_in.video.i_frame_rate;
723 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
725 /* No delay -> output pts == input pts */
726 p_block->i_pts = p_block->i_dts = p_pict->date;
728 else if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
729 p_sys->p_context->coded_frame->pts != 0 &&
730 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
732 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
733 p_block->i_pts = p_sys->p_context->coded_frame->pts;
735 /* Ugly work-around for stupid libavcodec behaviour */
736 #if LIBAVCODEC_BUILD >= 4722
738 int64_t i_framenum = p_block->i_pts *
739 p_enc->fmt_in.video.i_frame_rate /
740 p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE;
742 p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY];
744 #endif
745 /* End work-around */
747 if( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
748 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE )
750 p_block->i_dts = p_block->i_pts;
752 else
754 if( p_sys->i_last_ref_pts )
756 p_block->i_dts = p_sys->i_last_ref_pts;
758 else
760 /* Let's put something sensible */
761 p_block->i_dts = p_block->i_pts;
764 p_sys->i_last_ref_pts = p_block->i_pts;
767 else
769 /* Buggy libavcodec which doesn't update coded_frame->pts
770 * correctly */
771 p_block->i_dts = p_block->i_pts = p_pict->date;
774 switch ( p_sys->p_context->coded_frame->pict_type )
776 case FF_I_TYPE:
777 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
778 break;
779 case FF_P_TYPE:
780 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
781 break;
782 case FF_B_TYPE:
783 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
784 break;
787 return p_block;
790 return NULL;
793 /****************************************************************************
794 * EncodeAudio: the whole thing
795 ****************************************************************************/
796 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
798 encoder_sys_t *p_sys = p_enc->p_sys;
799 block_t *p_block, *p_chain = NULL;
801 char *p_buffer = p_aout_buf->p_buffer;
802 int i_samples = p_aout_buf->i_nb_samples;
803 int i_samples_delay = p_sys->i_samples_delay;
805 p_sys->i_pts = p_aout_buf->start_date -
806 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
807 (mtime_t)p_enc->fmt_in.audio.i_rate;
809 p_sys->i_samples_delay += i_samples;
811 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
813 int16_t *p_samples;
814 int i_out;
816 if( i_samples_delay )
818 /* Take care of the left-over from last time */
819 int i_delay_size = i_samples_delay * 2 *
820 p_sys->p_context->channels;
821 int i_size = p_sys->i_frame_size - i_delay_size;
823 p_samples = (int16_t *)p_sys->p_buffer;
824 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
825 p_buffer -= i_delay_size;
826 i_samples += i_samples_delay;
827 i_samples_delay = 0;
829 else
831 p_samples = (int16_t *)p_buffer;
834 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
835 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
836 p_samples );
838 #if 0
839 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
840 #endif
841 if( i_out < 0 ) break;
843 p_buffer += p_sys->i_frame_size;
844 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
845 i_samples -= p_sys->p_context->frame_size;
847 if( i_out == 0 ) continue;
849 p_block = block_New( p_enc, i_out );
850 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
852 p_block->i_length = (mtime_t)1000000 *
853 (mtime_t)p_sys->p_context->frame_size /
854 (mtime_t)p_sys->p_context->sample_rate;
856 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
858 /* Update pts */
859 p_sys->i_pts += p_block->i_length;
860 block_ChainAppend( &p_chain, p_block );
863 /* Backup the remaining raw samples */
864 if( i_samples )
866 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
867 p_sys->p_context->channels, p_buffer,
868 i_samples * 2 * p_sys->p_context->channels );
871 return p_chain;
874 /*****************************************************************************
875 * CloseEncoder: ffmpeg encoder destruction
876 *****************************************************************************/
877 void E_(CloseEncoder)( vlc_object_t *p_this )
879 encoder_t *p_enc = (encoder_t *)p_this;
880 encoder_sys_t *p_sys = p_enc->p_sys;
882 #if LIBAVCODEC_BUILD >= 4702
883 if ( p_sys->b_inited && p_enc->i_threads >= 1 )
885 int i;
886 struct thread_context_t ** pp_contexts =
887 (struct thread_context_t **)p_sys->p_context->thread_opaque;
888 for ( i = 0; i < p_enc->i_threads; i++ )
890 pp_contexts[i]->b_die = 1;
891 vlc_cond_signal( &pp_contexts[i]->cond );
892 vlc_thread_join( pp_contexts[i] );
893 vlc_mutex_destroy( &pp_contexts[i]->lock );
894 vlc_cond_destroy( &pp_contexts[i]->cond );
895 vlc_object_destroy( pp_contexts[i] );
898 free( pp_contexts );
900 #endif
902 avcodec_close( p_sys->p_context );
903 av_free( p_sys->p_context );
905 if( p_sys->p_buffer ) free( p_sys->p_buffer );
906 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );
908 free( p_sys );