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>
10 * Part of the file Copyright (C) FFMPEG Project Developers
11 * (mpeg4_default matrixes)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <vlc_codec.h>
39 #include <vlc_dialog.h>
40 #include <vlc_avcodec.h>
45 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
46 # include <libavcodec/avcodec.h>
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 #define RAW_AUDIO_FRAME_SIZE (2048)
61 /*****************************************************************************
63 *****************************************************************************/
64 int OpenEncoder ( vlc_object_t * );
65 void CloseEncoder( vlc_object_t * );
67 static block_t *EncodeVideo( encoder_t *, picture_t * );
68 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
70 struct thread_context_t;
72 /*****************************************************************************
73 * thread_context_t : for multithreaded encoding
74 *****************************************************************************/
75 struct thread_context_t
79 AVCodecContext *p_context;
80 int (* pf_func)(AVCodecContext *c, void *arg);
89 /*****************************************************************************
90 * encoder_sys_t : ffmpeg encoder descriptor
91 *****************************************************************************/
98 AVCodecContext *p_context;
104 uint8_t *p_buffer_out;
110 mtime_t i_last_ref_pts;
111 mtime_t i_buggy_pts_detect;
123 /* Encoding settings */
130 int i_rc_buffer_size;
131 float f_rc_buffer_aggressivity;
134 bool b_interlace, b_interlace_me;
135 float f_i_quant_factor;
136 int i_noise_reduction;
139 int i_quality; /* for VBR */
140 float f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
141 int i_luma_elim, i_chroma_elim;
142 int i_aac_profile; /* AAC profile to use.*/
143 /* Used to work around stupid timestamping behaviour in libavcodec */
145 mtime_t pi_delay_pts[MAX_FRAME_DELAY];
148 static const char *const ppsz_enc_options[] = {
149 "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
150 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
151 "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
152 "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
153 "p-masking", "border-masking", "luma-elim-threshold",
154 "chroma-elim-threshold",
159 static const uint16_t mpa_bitrate_tab[2][15] =
161 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
162 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
165 static const uint16_t mpa_freq_tab[6] =
166 { 44100, 48000, 32000, 22050, 24000, 16000 };
168 static const uint16_t mpeg4_default_intra_matrix[64] = {
169 8, 17, 18, 19, 21, 23, 25, 27,
170 17, 18, 19, 21, 23, 25, 27, 28,
171 20, 21, 22, 23, 24, 26, 28, 30,
172 21, 22, 23, 24, 26, 28, 30, 32,
173 22, 23, 24, 26, 28, 30, 32, 35,
174 23, 24, 26, 28, 30, 32, 35, 38,
175 25, 26, 28, 30, 32, 35, 38, 41,
176 27, 28, 30, 32, 35, 38, 41, 45,
179 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
180 16, 17, 18, 19, 20, 21, 22, 23,
181 17, 18, 19, 20, 21, 22, 23, 24,
182 18, 19, 20, 21, 22, 23, 24, 25,
183 19, 20, 21, 22, 23, 24, 26, 27,
184 20, 21, 22, 23, 25, 26, 27, 28,
185 21, 22, 23, 24, 26, 27, 28, 30,
186 22, 23, 24, 26, 27, 28, 30, 31,
187 23, 24, 25, 27, 28, 30, 31, 33,
190 /*****************************************************************************
191 * OpenEncoder: probe the encoder
192 *****************************************************************************/
194 int OpenEncoder( vlc_object_t *p_this )
196 encoder_t *p_enc = (encoder_t *)p_this;
197 encoder_sys_t *p_sys;
198 AVCodecContext *p_context;
199 AVCodec *p_codec = NULL;
200 int i_codec_id, i_cat;
201 const char *psz_namecodec;
206 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
209 i_codec_id = CODEC_ID_MP3;
210 psz_namecodec = "MPEG I/II Layer 3";
212 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2 )
215 i_codec_id = CODEC_ID_MP2;
216 psz_namecodec = "MPEG I/II Layer 2";
218 else if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
221 if( TestFfmpegChroma( -1, p_enc->fmt_out.i_codec ) != VLC_SUCCESS )
223 /* handed chroma output */
227 i_codec_id = CODEC_ID_RAWVIDEO;
228 psz_namecodec = "Raw video";
231 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
233 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
234 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
235 _("\"%s\" is no video encoder."), psz_namecodec );
239 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
241 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
242 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
243 _("\"%s\" is no audio encoder."), psz_namecodec );
247 if( p_enc->fmt_out.i_cat == SPU_ES )
249 /* We don't support subtitle encoding */
253 /* Initialization must be done before avcodec_find_encoder() */
254 InitLibavcodec( p_this );
256 char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
257 if( psz_encoder && *psz_encoder )
259 p_codec = avcodec_find_encoder_by_name( psz_encoder );
261 msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
262 else if( p_codec->id != i_codec_id )
264 msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
265 psz_encoder, (char*)&p_enc->fmt_out.i_codec );
271 p_codec = avcodec_find_encoder( i_codec_id );
274 msg_Err( p_enc, "cannot find encoder %s\n"
275 "*** Your FFMPEG installation is crippled. ***\n"
276 "*** Please check with your FFMPEG packager. ***\n"
277 "*** This is NOT a VLC media player issue. ***", psz_namecodec );
279 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"), _(
280 /* I have had enough of all these MPEG-3 transcoding bug reports.
281 * Downstream packager, you had better not patch this out, or I will be really
282 * annoyed. Think about it - you don't want to fork the VLC translation files,
283 * do you? -- Courmisch, 2008-10-22 */
284 "It seems your FFMPEG (libavcodec) installation lacks the following encoder:\n"
286 "If you don't know how to fix this, ask for support from your distribution.\n"
288 "This is not an error inside VLC media player.\n"
289 "Do not contact the VideoLAN project about this issue.\n"),
294 /* Allocate the memory needed to store the encoder's structure */
295 if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
297 p_enc->p_sys = p_sys;
298 p_sys->p_codec = p_codec;
300 p_enc->pf_encode_video = EncodeVideo;
301 p_enc->pf_encode_audio = EncodeAudio;
303 p_sys->p_buffer = NULL;
304 p_sys->p_buffer_out = NULL;
305 p_sys->i_buffer_out = 0;
307 p_sys->p_context = p_context = avcodec_alloc_context();
308 p_sys->p_context->codec_id = p_sys->p_codec->id;
309 p_context->debug = var_InheritInteger( p_enc, "ffmpeg-debug" );
310 p_context->opaque = (void *)p_this;
312 /* Set CPU capabilities */
313 unsigned i_cpu = vlc_CPU();
314 p_context->dsp_mask = 0;
315 if( !(i_cpu & CPU_CAPABILITY_MMX) )
317 p_context->dsp_mask |= AV_CPU_FLAG_MMX;
319 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
321 p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
323 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
325 p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
327 if( !(i_cpu & CPU_CAPABILITY_SSE) )
329 p_context->dsp_mask |= AV_CPU_FLAG_SSE;
330 p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
333 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
335 p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
336 p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
337 p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
338 p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
339 p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
340 p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
341 p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
343 if( p_sys->b_hurry_up )
345 /* hurry up mode needs noise reduction, even small */
346 p_sys->i_noise_reduction = 1;
349 p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
350 p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
351 p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
352 p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
353 p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
355 f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
356 if( f_val < 0.01 || f_val > 255.0 ) f_val = 0;
357 p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
359 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
360 p_sys->i_hq = FF_MB_DECISION_RD;
361 if( psz_val && *psz_val )
363 if( !strcmp( psz_val, "rd" ) )
364 p_sys->i_hq = FF_MB_DECISION_RD;
365 else if( !strcmp( psz_val, "bits" ) )
366 p_sys->i_hq = FF_MB_DECISION_BITS;
367 else if( !strcmp( psz_val, "simple" ) )
368 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
370 p_sys->i_hq = FF_MB_DECISION_RD;
373 p_sys->i_hq = FF_MB_DECISION_RD;
376 p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
377 p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
378 p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
380 i_val = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
381 if( i_val < - 1 || i_val > 1 ) i_val = 0;
382 p_context->strict_std_compliance = i_val;
384 p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
385 p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
386 p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
387 p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
388 p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
389 p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
391 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
392 /* ffmpeg uses faac encoder atm, and it has issues with
393 * other than low-complexity profile, so default to that */
394 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
395 if( psz_val && *psz_val )
397 if( !strncmp( psz_val, "main", 4 ) )
398 p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
399 else if( !strncmp( psz_val, "low", 3 ) )
400 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
401 #if 0 /* Not supported by FAAC encoder */
402 else if( !strncmp( psz_val, "ssr", 3 ) )
403 p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
405 else if( !strncmp( psz_val, "ltp", 3 ) )
406 p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
409 msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
410 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
415 if( p_enc->fmt_in.i_cat == VIDEO_ES )
417 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
419 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
420 p_enc->fmt_in.video.i_height );
425 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
427 p_context->width = p_enc->fmt_in.video.i_width;
428 p_context->height = p_enc->fmt_in.video.i_height;
430 p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
431 p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
432 if( p_codec->supported_framerates )
434 AVRational target = {
435 .num = p_enc->fmt_in.video.i_frame_rate,
436 .den = p_enc->fmt_in.video.i_frame_rate_base,
438 int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
440 p_context->time_base.num = p_codec->supported_framerates[idx].den;
441 p_context->time_base.den = p_codec->supported_framerates[idx].num;
444 /* Defaults from ffmpeg.c */
445 p_context->qblur = 0.5;
446 p_context->qcompress = 0.5;
447 p_context->b_quant_offset = 1.25;
448 p_context->b_quant_factor = 1.25;
449 p_context->i_quant_offset = 0.0;
450 p_context->i_quant_factor = -0.8;
452 p_context->lumi_masking = p_sys->f_lumi_masking;
453 p_context->dark_masking = p_sys->f_dark_masking;
454 p_context->p_masking = p_sys->f_p_masking;
455 p_context->border_masking = p_sys->f_border_masking;
456 p_context->luma_elim_threshold = p_sys->i_luma_elim;
457 p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
459 if( p_sys->i_key_int > 0 )
460 p_context->gop_size = p_sys->i_key_int;
461 p_context->max_b_frames =
462 VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
463 p_context->b_frame_strategy = 0;
464 if( !p_context->max_b_frames &&
465 ( p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
466 p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ||
467 p_enc->fmt_out.i_codec == VLC_CODEC_MP1V ) )
468 p_context->flags |= CODEC_FLAG_LOW_DELAY;
470 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2V )
471 p_context->idct_algo = FF_IDCT_LIBMPEG2MMX;
473 av_reduce( &p_context->sample_aspect_ratio.num,
474 &p_context->sample_aspect_ratio.den,
475 p_enc->fmt_in.video.i_sar_num,
476 p_enc->fmt_in.video.i_sar_den, 1 << 30 );
478 p_sys->p_buffer_out = NULL;
480 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
481 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
482 GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
484 if( p_codec->pix_fmts )
486 const enum PixelFormat *p = p_codec->pix_fmts;
487 for( ; *p != -1; p++ )
489 if( *p == p_context->pix_fmt ) break;
491 if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
492 GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
493 p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
497 if ( p_sys->f_i_quant_factor != 0.0 )
498 p_context->i_quant_factor = p_sys->f_i_quant_factor;
500 p_context->noise_reduction = p_sys->i_noise_reduction;
502 if ( p_sys->b_mpeg4_matrix )
504 p_context->intra_matrix = mpeg4_default_intra_matrix;
505 p_context->inter_matrix = mpeg4_default_non_intra_matrix;
508 if ( p_sys->b_pre_me )
510 p_context->pre_me = 1;
511 p_context->me_pre_cmp = FF_CMP_CHROMA;
514 if ( p_sys->b_interlace )
516 if ( p_context->height <= 280 )
518 if ( p_context->height != 16 || p_context->width != 16 )
520 "disabling interlaced video because height=%d <= 280",
525 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
526 if ( p_sys->b_interlace_me )
527 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
531 p_context->trellis = p_sys->b_trellis;
533 if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
534 p_context->flags |= CODEC_FLAG_QSCALE;
535 /* These codecs cause libavcodec to exit if thread_count is > 1.
536 See libavcodec/mpegvideo_enc.c:MPV_encode_init and
537 libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
539 if ( i_codec_id == CODEC_ID_FLV1 ||
540 i_codec_id == CODEC_ID_H261 ||
541 i_codec_id == CODEC_ID_LJPEG ||
542 i_codec_id == CODEC_ID_MJPEG ||
543 i_codec_id == CODEC_ID_H263 ||
544 i_codec_id == CODEC_ID_H263P ||
545 i_codec_id == CODEC_ID_MSMPEG4V1 ||
546 i_codec_id == CODEC_ID_MSMPEG4V2 ||
547 i_codec_id == CODEC_ID_MSMPEG4V3 ||
548 i_codec_id == CODEC_ID_WMV1 ||
549 i_codec_id == CODEC_ID_WMV2 ||
550 i_codec_id == CODEC_ID_RV10 ||
551 i_codec_id == CODEC_ID_RV20 ||
552 i_codec_id == CODEC_ID_SVQ3 )
553 p_enc->i_threads = 1;
555 if( p_sys->i_vtolerance > 0 )
556 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
558 /* usually if someone sets bitrate, he likes more to get that bitrate
559 * over quality should help 'normal' user to get asked bitrate
561 if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
567 if( p_sys->i_qmin > 0 )
569 p_context->qmin = p_sys->i_qmin;
570 p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
572 if( p_sys->i_qmax > 0 )
574 p_context->qmax = p_sys->i_qmax;
575 p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
577 p_context->max_qdiff = 3;
579 p_context->mb_decision = p_sys->i_hq;
581 if( p_sys->i_quality )
583 p_context->flags |= CODEC_FLAG_QSCALE;
584 p_context->global_quality = p_sys->i_quality;
588 p_context->rc_qsquish = 1.0;
589 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
590 p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
591 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
592 /* This is from ffmpeg's ffmpeg.c : */
593 p_context->rc_initial_buffer_occupancy
594 = p_sys->i_rc_buffer_size * 3/4;
595 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
598 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
600 /* work around bug in libmp3lame encoding */
601 if( i_codec_id == CODEC_ID_MP3 && p_enc->fmt_in.audio.i_channels > 2 )
602 p_enc->fmt_in.audio.i_channels = 2;
604 p_context->codec_type = AVMEDIA_TYPE_AUDIO;
605 p_context->sample_fmt = p_codec->sample_fmts ?
606 p_codec->sample_fmts[0] :
608 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
609 p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
610 p_context->time_base.num = 1;
611 p_context->time_base.den = p_context->sample_rate;
612 p_context->channels = p_enc->fmt_out.audio.i_channels;
614 if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
616 /* XXX: FAAC does resample only when setting the INPUT samplerate
617 * to the desired value (-R option of the faac frontend)
618 p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
619 /* vlc should default to low-complexity profile, faac encoder
620 * has bug and aac audio has issues otherwise atm */
621 p_context->profile = p_sys->i_aac_profile;
625 /* Misc parameters */
626 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
628 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 69, 2 )
629 /* Set reasonable defaults to VP8, based on
630 libvpx-720p preset from libvpx ffmpeg-patch */
631 if( i_codec_id == CODEC_ID_VP8 )
633 /* Lets give bitrate tolerance */
634 p_context->bit_rate_tolerance = __MAX(2 * p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
635 /* default to 120 frames between keyframe */
636 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
637 p_context->gop_size = 120;
638 /* Don't set rc-values atm, they were from time before
639 libvpx was officially in ffmpeg */
640 //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
641 //p_context->rc_min_rate = 40 * 1000; // 40k
642 /* seems that ffmpeg presets have 720p as divider for buffers */
643 if( p_enc->fmt_out.video.i_height >= 720 )
645 /* Check that we don't overrun users qmin/qmax values */
646 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
648 p_context->qmin = 10;
649 p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
652 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
654 p_context->qmax = 42;
655 p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
659 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
662 p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
667 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
669 p_context->level = 216;
670 p_context->profile = 0;
671 p_context->rc_buffer_aggressivity = 0.95;
672 p_context->token_partitions = 4;
673 p_context->mb_static_threshold = 0;
678 if( i_codec_id == CODEC_ID_RAWVIDEO )
680 /* XXX: hack: Force same codec (will be handled by transcode) */
681 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
682 GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
685 /* Make sure we get extradata filled by the encoder */
686 p_context->extradata_size = 0;
687 p_context->extradata = NULL;
688 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
690 if( p_enc->i_threads >= 1)
691 p_context->thread_count = p_enc->i_threads;
693 p_context->thread_count = vlc_GetCPUCount();
697 ret = avcodec_open( p_context, p_codec );
698 vlc_avcodec_unlock();
701 if( p_enc->fmt_in.i_cat == AUDIO_ES &&
702 (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
703 || i_codec_id == CODEC_ID_MP3) )
705 if( p_context->channels > 2 )
707 p_context->channels = 2;
708 p_enc->fmt_in.audio.i_channels = 2; // FIXME
709 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
712 if( i_codec_id == CODEC_ID_MP2 || i_codec_id == CODEC_ID_MP3 )
716 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
718 if ( p_enc->fmt_out.audio.i_rate
719 == mpa_freq_tab[i_frequency] )
722 if ( i_frequency == 6 )
724 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
725 p_enc->fmt_out.audio.i_rate );
730 for ( i = 1; i < 14; i++ )
732 if ( p_enc->fmt_out.i_bitrate / 1000
733 <= mpa_bitrate_tab[i_frequency / 3][i] )
736 if ( p_enc->fmt_out.i_bitrate / 1000
737 != mpa_bitrate_tab[i_frequency / 3][i] )
740 "MPEG audio doesn't support bitrate=%d, using %d",
741 p_enc->fmt_out.i_bitrate,
742 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
743 p_enc->fmt_out.i_bitrate =
744 mpa_bitrate_tab[i_frequency / 3][i] * 1000;
745 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
749 p_context->codec = NULL;
751 ret = avcodec_open( p_context, p_codec );
752 vlc_avcodec_unlock();
755 msg_Err( p_enc, "cannot open encoder" );
757 _("Streaming / Transcoding failed"),
758 "%s", _("VLC could not open the encoder.") );
765 msg_Err( p_enc, "cannot open encoder" );
766 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
767 "%s", _("VLC could not open the encoder.") );
773 if( i_codec_id == CODEC_ID_FLAC )
775 p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
776 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
777 if( p_enc->fmt_out.p_extra )
779 uint8_t *p = p_enc->fmt_out.p_extra;
784 p[4] = 0x80; /* streaminfo block, last block before audio */
785 p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
786 p[6] = ( p_context->extradata_size >> 8 ) & 0xff;
787 p[7] = ( p_context->extradata_size ) & 0xff;
788 memcpy( &p[8], p_context->extradata, p_context->extradata_size );
792 p_enc->fmt_out.i_extra = 0;
797 p_enc->fmt_out.i_extra = p_context->extradata_size;
798 if( p_enc->fmt_out.i_extra )
800 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
801 if ( p_enc->fmt_out.p_extra == NULL )
805 memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
806 p_enc->fmt_out.i_extra );
810 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
812 if( p_enc->fmt_in.i_cat == AUDIO_ES )
814 GetVlcAudioFormat( &p_enc->fmt_in.i_codec,
815 &p_enc->fmt_in.audio.i_bitspersample,
816 p_sys->p_context->sample_fmt );
817 p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8) *
819 p_sys->i_frame_size = p_context->frame_size > 1 ?
820 p_context->frame_size :
821 RAW_AUDIO_FRAME_SIZE;
822 p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes );
823 if ( p_sys->p_buffer == NULL )
827 p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
828 p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( vlc_fourcc_GetCodec( AUDIO_ES, p_enc->fmt_out.i_codec ) );
830 if( p_context->frame_size > 1 )
831 p_sys->i_buffer_out = 8 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
833 p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes;
834 p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
835 if ( p_sys->p_buffer_out == NULL )
841 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
845 free( p_enc->fmt_out.p_extra );
846 free( p_sys->p_buffer );
847 free( p_sys->p_buffer_out );
852 /****************************************************************************
853 * EncodeVideo: the whole thing
854 ****************************************************************************/
855 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
857 encoder_sys_t *p_sys = p_enc->p_sys;
860 /* Initialize the video output buffer the first time.
861 * This is done here instead of OpenEncoder() because we need the actual
862 * bits_per_pixel value, without having to assume anything.
864 if ( p_sys->p_buffer_out == NULL )
866 int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
867 p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
869 p_sys->i_buffer_out = p_sys->p_context->height * p_sys->p_context->width
870 * bytesPerPixel + 200; /* some room for potential headers (such as BMP) */
872 if( p_sys->i_buffer_out < FF_MIN_BUFFER_SIZE )
873 p_sys->i_buffer_out = FF_MIN_BUFFER_SIZE;
874 p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
875 if ( p_sys->p_buffer_out == NULL )
879 if( likely(p_pict) ) {
881 memset( &frame, 0, sizeof( AVFrame ) );
882 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
884 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
885 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
888 /* Let ffmpeg select the frame type */
891 frame.repeat_pict = p_pict->i_nb_fields - 2;
892 frame.interlaced_frame = !p_pict->b_progressive;
893 frame.top_field_first = !!p_pict->b_top_field_first;
895 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
896 if( p_enc->fmt_out.i_codec != VLC_CODEC_MP4V )
898 frame.pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE;
900 if ( p_sys->b_hurry_up && frame.pts != (int64_t)AV_NOPTS_VALUE )
902 mtime_t current_date = mdate();
904 if ( current_date + HURRY_UP_GUARD3 > frame.pts )
906 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
907 p_sys->p_context->trellis = 0;
908 msg_Dbg( p_enc, "hurry up mode 3" );
912 p_sys->p_context->mb_decision = p_sys->i_hq;
914 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
916 p_sys->p_context->trellis = 0;
917 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
918 + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
919 msg_Dbg( p_enc, "hurry up mode 2" );
923 p_sys->p_context->trellis = p_sys->b_trellis;
925 p_sys->p_context->noise_reduction =
926 p_sys->i_noise_reduction;
930 if ( current_date + HURRY_UP_GUARD1 > frame.pts )
932 frame.pict_type = FF_P_TYPE;
933 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
939 frame.pts = (int64_t)AV_NOPTS_VALUE;
942 if ( frame.pts != (int64_t)AV_NOPTS_VALUE && frame.pts != 0 )
944 if ( p_sys->i_last_pts == frame.pts )
946 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
947 "same PTS (%"PRId64 ")", frame.pts );
950 else if ( p_sys->i_last_pts > frame.pts )
952 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
953 "past (current: %"PRId64 ", last: %"PRId64")",
954 frame.pts, p_sys->i_last_pts );
959 p_sys->i_last_pts = frame.pts;
963 frame.quality = p_sys->i_quality;
965 /* Ugly work-around for stupid libavcodec behaviour */
967 p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts;
968 frame.pts = p_sys->i_framenum * AV_TIME_BASE *
969 p_enc->fmt_in.video.i_frame_rate_base;
970 frame.pts += p_enc->fmt_in.video.i_frame_rate - 1;
971 frame.pts /= p_enc->fmt_in.video.i_frame_rate;
972 /* End work-around */
974 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
975 p_sys->i_buffer_out, &frame );
979 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
980 p_sys->i_buffer_out, NULL);
986 block_t *p_block = block_New( p_enc, i_out );
987 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
989 /* FIXME, 3-2 pulldown is not handled correctly */
990 p_block->i_length = INT64_C(1000000) *
991 p_enc->fmt_in.video.i_frame_rate_base /
992 p_enc->fmt_in.video.i_frame_rate;
994 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
996 /* No delay -> output pts == input pts */
998 p_block->i_dts = p_pict->date;
999 p_block->i_pts = p_block->i_dts;
1001 else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE &&
1002 p_sys->p_context->coded_frame->pts != 0 &&
1003 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
1005 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
1006 p_block->i_pts = p_sys->p_context->coded_frame->pts;
1008 /* Ugly work-around for stupid libavcodec behaviour */
1010 int64_t i_framenum = p_block->i_pts *
1011 p_enc->fmt_in.video.i_frame_rate /
1012 p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE;
1014 p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY];
1016 /* End work-around */
1018 if( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
1019 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE )
1021 p_block->i_dts = p_block->i_pts;
1025 if( p_sys->i_last_ref_pts )
1027 p_block->i_dts = p_sys->i_last_ref_pts;
1031 /* Let's put something sensible */
1032 p_block->i_dts = p_block->i_pts;
1035 p_sys->i_last_ref_pts = p_block->i_pts;
1040 /* Buggy libavcodec which doesn't update coded_frame->pts
1042 p_block->i_dts = p_block->i_pts = p_pict->date;
1045 switch ( p_sys->p_context->coded_frame->pict_type )
1048 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1051 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1054 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1061 /****************************************************************************
1062 * EncodeAudio: the whole thing
1063 ****************************************************************************/
1064 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
1066 encoder_sys_t *p_sys = p_enc->p_sys;
1068 block_t *p_block, *p_chain = NULL;
1070 uint8_t *p_buffer = p_aout_buf->p_buffer;
1071 int i_samples = p_aout_buf->i_nb_samples;
1072 int i_samples_delay = p_sys->i_samples_delay;
1074 p_sys->i_pts = p_aout_buf->i_pts -
1075 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1076 (mtime_t)p_enc->fmt_in.audio.i_rate;
1078 p_sys->i_samples_delay += i_samples;
1080 while( p_sys->i_samples_delay >= p_sys->i_frame_size )
1085 if( i_samples_delay )
1087 /* Take care of the left-over from last time */
1088 int i_delay_size = i_samples_delay;
1089 int i_size = (p_sys->i_frame_size - i_delay_size) *
1090 p_sys->i_sample_bytes;
1092 memcpy( p_sys->p_buffer + i_delay_size * p_sys->i_sample_bytes,
1094 p_buffer -= i_delay_size * p_sys->i_sample_bytes;
1095 i_samples += i_samples_delay;
1096 i_samples_delay = 0;
1098 p_samples = p_sys->p_buffer;
1102 p_samples = p_buffer;
1105 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
1106 p_sys->i_buffer_out, p_samples );
1109 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
1111 p_buffer += p_sys->i_frame_size * p_sys->i_sample_bytes;
1112 p_sys->i_samples_delay -= p_sys->i_frame_size;
1113 i_samples -= p_sys->i_frame_size;
1118 p_block = block_New( p_enc, i_out );
1119 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1121 p_block->i_length = (mtime_t)1000000 *
1122 (mtime_t)p_sys->i_frame_size /
1123 (mtime_t)p_sys->p_context->sample_rate;
1125 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1128 p_sys->i_pts += p_block->i_length;
1129 block_ChainAppend( &p_chain, p_block );
1132 /* Backup the remaining raw samples */
1135 memcpy( &p_sys->p_buffer[i_samples_delay * p_sys->i_sample_bytes],
1137 i_samples * p_sys->i_sample_bytes );
1143 /*****************************************************************************
1144 * CloseEncoder: ffmpeg encoder destruction
1145 *****************************************************************************/
1146 void CloseEncoder( vlc_object_t *p_this )
1148 encoder_t *p_enc = (encoder_t *)p_this;
1149 encoder_sys_t *p_sys = p_enc->p_sys;
1152 avcodec_close( p_sys->p_context );
1153 vlc_avcodec_unlock();
1154 av_free( p_sys->p_context );
1156 free( p_sys->p_buffer );
1157 free( p_sys->p_buffer_out );