1 /*****************************************************************************
2 * encoder.c: video and audio encoder using the libavcodec library
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * 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>
44 #include <libavcodec/avcodec.h>
49 #define HURRY_UP_GUARD1 (450000)
50 #define HURRY_UP_GUARD2 (300000)
51 #define HURRY_UP_GUARD3 (100000)
53 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
55 #define RAW_AUDIO_FRAME_SIZE (2048)
57 /*****************************************************************************
59 *****************************************************************************/
60 int OpenEncoder ( vlc_object_t * );
61 void CloseEncoder( vlc_object_t * );
63 static block_t *EncodeVideo( encoder_t *, picture_t * );
64 static block_t *EncodeAudio( encoder_t *, block_t * );
66 struct thread_context_t;
68 /*****************************************************************************
69 * thread_context_t : for multithreaded encoding
70 *****************************************************************************/
71 struct thread_context_t
75 AVCodecContext *p_context;
76 int (* pf_func)(AVCodecContext *c, void *arg);
85 /*****************************************************************************
86 * encoder_sys_t : libavcodec encoder descriptor
87 *****************************************************************************/
91 * libavcodec properties
94 AVCodecContext *p_context;
97 * Common buffer mainly for audio as frame size in there needs usually be constant
105 mtime_t i_last_ref_pts;
106 mtime_t i_buggy_pts_detect;
118 /* Encoding settings */
125 int i_rc_buffer_size;
126 float f_rc_buffer_aggressivity;
129 bool b_interlace, b_interlace_me;
130 float f_i_quant_factor;
131 int i_noise_reduction;
134 int i_quality; /* for VBR */
135 float f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
136 int i_luma_elim, i_chroma_elim;
137 int i_aac_profile; /* AAC profile to use.*/
142 static const char *const ppsz_enc_options[] = {
143 "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
144 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
145 "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
146 "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
147 "p-masking", "border-masking", "luma-elim-threshold",
148 "chroma-elim-threshold",
153 static const uint16_t mpa_bitrate_tab[2][15] =
155 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
156 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
159 static const uint16_t mpa_freq_tab[6] =
160 { 44100, 48000, 32000, 22050, 24000, 16000 };
162 static const uint16_t mpeg4_default_intra_matrix[64] = {
163 8, 17, 18, 19, 21, 23, 25, 27,
164 17, 18, 19, 21, 23, 25, 27, 28,
165 20, 21, 22, 23, 24, 26, 28, 30,
166 21, 22, 23, 24, 26, 28, 30, 32,
167 22, 23, 24, 26, 28, 30, 32, 35,
168 23, 24, 26, 28, 30, 32, 35, 38,
169 25, 26, 28, 30, 32, 35, 38, 41,
170 27, 28, 30, 32, 35, 38, 41, 45,
173 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
174 16, 17, 18, 19, 20, 21, 22, 23,
175 17, 18, 19, 20, 21, 22, 23, 24,
176 18, 19, 20, 21, 22, 23, 24, 25,
177 19, 20, 21, 22, 23, 24, 26, 27,
178 20, 21, 22, 23, 25, 26, 27, 28,
179 21, 22, 23, 24, 26, 27, 28, 30,
180 22, 23, 24, 26, 27, 28, 30, 31,
181 23, 24, 25, 27, 28, 30, 31, 33,
184 /*****************************************************************************
185 * OpenEncoder: probe the encoder
186 *****************************************************************************/
188 int OpenEncoder( vlc_object_t *p_this )
190 encoder_t *p_enc = (encoder_t *)p_this;
191 encoder_sys_t *p_sys;
192 AVCodecContext *p_context;
193 AVCodec *p_codec = NULL;
194 int i_codec_id, i_cat;
195 const char *psz_namecodec;
199 /* Initialization must be done before avcodec_find_encoder() */
202 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
204 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
207 i_codec_id = AV_CODEC_ID_MP3;
208 psz_namecodec = "MPEG I/II Layer 3";
210 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2 )
213 i_codec_id = AV_CODEC_ID_MP2;
214 psz_namecodec = "MPEG I/II Layer 2";
216 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP1V )
219 i_codec_id = AV_CODEC_ID_MPEG1VIDEO;
220 psz_namecodec = "MPEG-1 video";
222 else if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
225 if( FindFfmpegChroma( p_enc->fmt_out.i_codec ) == PIX_FMT_NONE )
226 return VLC_EGENERIC; /* handed chroma output */
229 i_codec_id = AV_CODEC_ID_RAWVIDEO;
230 psz_namecodec = "Raw video";
233 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
235 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
236 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
237 _("\"%s\" is no video encoder."), psz_namecodec );
241 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
243 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
244 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
245 _("\"%s\" is no audio encoder."), psz_namecodec );
249 if( p_enc->fmt_out.i_cat == SPU_ES )
251 /* We don't support subtitle encoding */
255 char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
256 if( psz_encoder && *psz_encoder )
258 p_codec = avcodec_find_encoder_by_name( psz_encoder );
260 msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
261 else if( p_codec->id != i_codec_id )
263 msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
264 psz_encoder, (char*)&p_enc->fmt_out.i_codec );
270 p_codec = avcodec_find_encoder( i_codec_id );
273 msg_Err( p_enc, "cannot find encoder %s\n"
274 "*** Your Libav/FFmpeg installation is crippled. ***\n"
275 "*** Please check with your Libav/FFmpeg packager. ***\n"
276 "*** This is NOT a VLC media player issue. ***", psz_namecodec );
278 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"), _(
279 /* I have had enough of all these MPEG-3 transcoding bug reports.
280 * Downstream packager, you had better not patch this out, or I will be really
281 * annoyed. Think about it - you don't want to fork the VLC translation files,
282 * do you? -- Courmisch, 2008-10-22 */
283 "It seems your Libav/FFmpeg (libavcodec) installation lacks the following encoder:\n"
285 "If you don't know how to fix this, ask for support from your distribution.\n"
287 "This is not an error inside VLC media player.\n"
288 "Do not contact the VideoLAN project about this issue.\n"),
293 /* Allocate the memory needed to store the encoder's structure */
294 if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
296 p_enc->p_sys = p_sys;
297 p_sys->i_samples_delay = 0;
298 p_sys->p_codec = p_codec;
300 p_sys->p_buffer = NULL;
301 p_sys->i_buffer_out = 0;
303 p_context = avcodec_alloc_context3(p_codec);
304 p_sys->p_context = p_context;
305 p_sys->p_context->codec_id = p_sys->p_codec->id;
306 p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
307 p_context->opaque = (void *)p_this;
309 /* set CPU capabilities */
310 #if LIBAVUTIL_VERSION_CHECK(51, 25, 0, 42, 100)
311 av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
313 p_context->dsp_mask = GetVlcDspMask();
316 p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
317 p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
318 p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
319 p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
320 p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
321 p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
322 p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
324 if( p_sys->b_hurry_up )
326 /* hurry up mode needs noise reduction, even small */
327 p_sys->i_noise_reduction = 1;
330 p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
331 p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
332 p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
333 p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
334 p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
336 f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
337 if( f_val < 0.01 || f_val > 255.0 ) f_val = 0;
338 p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
340 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
341 p_sys->i_hq = FF_MB_DECISION_RD;
342 if( psz_val && *psz_val )
344 if( !strcmp( psz_val, "rd" ) )
345 p_sys->i_hq = FF_MB_DECISION_RD;
346 else if( !strcmp( psz_val, "bits" ) )
347 p_sys->i_hq = FF_MB_DECISION_BITS;
348 else if( !strcmp( psz_val, "simple" ) )
349 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
351 p_sys->i_hq = FF_MB_DECISION_RD;
354 p_sys->i_hq = FF_MB_DECISION_RD;
357 p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
358 p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
359 p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
361 p_context->strict_std_compliance = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
363 p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
364 p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
365 p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
366 p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
367 p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
368 p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
370 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
371 /* libavcodec uses faac encoder atm, and it has issues with
372 * other than low-complexity profile, so default to that */
373 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
374 if( psz_val && *psz_val )
376 if( !strncmp( psz_val, "main", 4 ) )
377 p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
378 else if( !strncmp( psz_val, "low", 3 ) )
379 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
380 #if 0 /* Not supported by FAAC encoder */
381 else if( !strncmp( psz_val, "ssr", 3 ) )
382 p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
384 else if( !strncmp( psz_val, "ltp", 3 ) )
385 p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
386 /* These require libavcodec with libfdk-aac */
387 else if( !strncmp( psz_val, "hev2", 4 ) )
388 p_sys->i_aac_profile = FF_PROFILE_AAC_HE_V2;
389 else if( !strncmp( psz_val, "hev1", 4 ) )
390 p_sys->i_aac_profile = FF_PROFILE_AAC_HE;
393 msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
394 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
399 if( p_enc->fmt_in.i_cat == VIDEO_ES )
401 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
403 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
404 p_enc->fmt_in.video.i_height );
409 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
411 p_context->width = p_enc->fmt_in.video.i_width;
412 p_context->height = p_enc->fmt_in.video.i_height;
414 p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
415 p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
416 if( p_codec->supported_framerates )
418 AVRational target = {
419 .num = p_enc->fmt_in.video.i_frame_rate,
420 .den = p_enc->fmt_in.video.i_frame_rate_base,
422 int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
424 p_context->time_base.num = p_codec->supported_framerates[idx].den;
425 p_context->time_base.den = p_codec->supported_framerates[idx].num;
428 /* Defaults from ffmpeg.c */
429 p_context->qblur = 0.5;
430 p_context->qcompress = 0.5;
431 p_context->b_quant_offset = 1.25;
432 p_context->b_quant_factor = 1.25;
433 p_context->i_quant_offset = 0.0;
434 p_context->i_quant_factor = -0.8;
436 p_context->lumi_masking = p_sys->f_lumi_masking;
437 p_context->dark_masking = p_sys->f_dark_masking;
438 p_context->p_masking = p_sys->f_p_masking;
439 p_context->border_masking = p_sys->f_border_masking;
440 p_context->luma_elim_threshold = p_sys->i_luma_elim;
441 p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
443 if( p_sys->i_key_int > 0 )
444 p_context->gop_size = p_sys->i_key_int;
445 p_context->max_b_frames =
446 VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
447 p_context->b_frame_strategy = 0;
448 if( !p_context->max_b_frames &&
449 ( p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
450 p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ) )
451 p_context->flags |= CODEC_FLAG_LOW_DELAY;
453 av_reduce( &p_context->sample_aspect_ratio.num,
454 &p_context->sample_aspect_ratio.den,
455 p_enc->fmt_in.video.i_sar_num,
456 p_enc->fmt_in.video.i_sar_den, 1 << 30 );
459 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
460 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
461 GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
463 if( p_codec->pix_fmts )
465 const enum PixelFormat *p = p_codec->pix_fmts;
466 for( ; *p != -1; p++ )
468 if( *p == p_context->pix_fmt ) break;
470 if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
471 GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
472 p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
476 if ( p_sys->f_i_quant_factor != 0.0 )
477 p_context->i_quant_factor = p_sys->f_i_quant_factor;
479 p_context->noise_reduction = p_sys->i_noise_reduction;
481 if ( p_sys->b_mpeg4_matrix )
483 p_context->intra_matrix = mpeg4_default_intra_matrix;
484 p_context->inter_matrix = mpeg4_default_non_intra_matrix;
487 if ( p_sys->b_pre_me )
489 p_context->pre_me = 1;
490 p_context->me_pre_cmp = FF_CMP_CHROMA;
493 if ( p_sys->b_interlace )
495 if ( p_context->height <= 280 )
497 if ( p_context->height != 16 || p_context->width != 16 )
499 "disabling interlaced video because height=%d <= 280",
504 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
505 if ( p_sys->b_interlace_me )
506 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
510 p_context->trellis = p_sys->b_trellis;
512 if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
513 p_context->flags |= CODEC_FLAG_QSCALE;
514 /* These codecs cause libavcodec to exit if thread_count is > 1.
515 See libavcodec/mpegvideo_enc.c:MPV_encode_init and
516 libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
518 if ( i_codec_id == AV_CODEC_ID_FLV1 ||
519 i_codec_id == AV_CODEC_ID_H261 ||
520 i_codec_id == AV_CODEC_ID_LJPEG ||
521 i_codec_id == AV_CODEC_ID_MJPEG ||
522 i_codec_id == AV_CODEC_ID_H263 ||
523 i_codec_id == AV_CODEC_ID_H263P ||
524 i_codec_id == AV_CODEC_ID_MSMPEG4V1 ||
525 i_codec_id == AV_CODEC_ID_MSMPEG4V2 ||
526 i_codec_id == AV_CODEC_ID_MSMPEG4V3 ||
527 i_codec_id == AV_CODEC_ID_WMV1 ||
528 i_codec_id == AV_CODEC_ID_WMV2 ||
529 i_codec_id == AV_CODEC_ID_RV10 ||
530 i_codec_id == AV_CODEC_ID_RV20 ||
531 i_codec_id == AV_CODEC_ID_SVQ3 )
532 p_enc->i_threads = 1;
534 if( p_sys->i_vtolerance > 0 )
535 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
537 /* usually if someone sets bitrate, he likes more to get that bitrate
538 * over quality should help 'normal' user to get asked bitrate
540 if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
546 if( p_sys->i_qmin > 0 )
548 p_context->qmin = p_sys->i_qmin;
549 p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
551 if( p_sys->i_qmax > 0 )
553 p_context->qmax = p_sys->i_qmax;
554 p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
556 p_context->max_qdiff = 3;
558 p_context->mb_decision = p_sys->i_hq;
560 if( p_sys->i_quality )
562 p_context->flags |= CODEC_FLAG_QSCALE;
563 p_context->global_quality = p_sys->i_quality;
567 p_context->rc_qsquish = 1.0;
568 if( p_sys->i_rc_buffer_size )
570 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
571 p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
573 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
574 /* This is from ffmpeg's ffmpeg.c : */
575 p_context->rc_initial_buffer_occupancy
576 = p_sys->i_rc_buffer_size * 3/4;
577 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
580 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
582 /* work around bug in libmp3lame encoding */
583 if( i_codec_id == AV_CODEC_ID_MP3 && p_enc->fmt_in.audio.i_channels > 2 )
584 p_enc->fmt_in.audio.i_channels = 2;
586 p_context->codec_type = AVMEDIA_TYPE_AUDIO;
587 p_context->sample_fmt = p_codec->sample_fmts ?
588 p_codec->sample_fmts[0] :
591 // Try if we can use interleaved format for codec input as VLC doesn't really do planar audio yet
592 // FIXME: Remove when planar/interleaved audio in vlc is equally supported
593 if( av_sample_fmt_is_planar( p_context->sample_fmt ) )
595 msg_Dbg( p_enc, "Trying to find packet sample format instead of %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
596 for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
598 if( !av_sample_fmt_is_planar( p_codec->sample_fmts[i] ) )
600 p_context->sample_fmt = p_codec->sample_fmts[i];
601 msg_Dbg( p_enc, "Using %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
606 p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_context->sample_fmt );
608 p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
609 p_context->time_base.num = 1;
610 p_context->time_base.den = p_context->sample_rate;
611 p_context->channels = p_enc->fmt_out.audio.i_channels;
613 if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
615 /* XXX: FAAC does resample only when setting the INPUT samplerate
616 * to the desired value (-R option of the faac frontend)
617 p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
618 /* vlc should default to low-complexity profile, faac encoder
619 * has bug and aac audio has issues otherwise atm */
620 p_context->profile = p_sys->i_aac_profile;
624 /* Misc parameters */
625 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
627 /* Set reasonable defaults to VP8, based on
628 libvpx-720p preset from libvpx ffmpeg-patch */
629 if( i_codec_id == AV_CODEC_ID_VP8 )
631 /* Lets give bitrate tolerance */
632 p_context->bit_rate_tolerance = __MAX(2 * (int)p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
633 /* default to 120 frames between keyframe */
634 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
635 p_context->gop_size = 120;
636 /* Don't set rc-values atm, they were from time before
637 libvpx was officially in FFmpeg */
638 //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
639 //p_context->rc_min_rate = 40 * 1000; // 40k
640 /* seems that FFmpeg presets have 720p as divider for buffers */
641 if( p_enc->fmt_out.video.i_height >= 720 )
643 /* Check that we don't overrun users qmin/qmax values */
644 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
646 p_context->qmin = 10;
647 p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
650 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
652 p_context->qmax = 42;
653 p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
657 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
660 p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
665 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
667 p_context->level = 216;
668 p_context->profile = 0;
669 p_context->rc_buffer_aggressivity = 0.95;
670 p_context->token_partitions = 4;
671 p_context->mb_static_threshold = 0;
675 if( i_codec_id == AV_CODEC_ID_RAWVIDEO )
677 /* XXX: hack: Force same codec (will be handled by transcode) */
678 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
679 GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
682 /* Make sure we get extradata filled by the encoder */
683 p_context->extradata_size = 0;
684 p_context->extradata = NULL;
685 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
687 if( p_enc->i_threads >= 1)
688 p_context->thread_count = p_enc->i_threads;
690 p_context->thread_count = vlc_GetCPUCount();
694 ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
695 vlc_avcodec_unlock();
698 if( p_enc->fmt_in.i_cat == AUDIO_ES &&
699 (p_context->channels > 2 || i_codec_id == AV_CODEC_ID_MP2
700 || i_codec_id == AV_CODEC_ID_MP3) )
702 if( p_context->channels > 2 )
704 p_context->channels = 2;
705 p_enc->fmt_in.audio.i_channels = 2; // FIXME
706 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
709 if( i_codec_id == AV_CODEC_ID_MP2 || i_codec_id == AV_CODEC_ID_MP3 )
713 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
715 if ( p_enc->fmt_out.audio.i_rate
716 == mpa_freq_tab[i_frequency] )
719 if ( i_frequency == 6 )
721 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
722 p_enc->fmt_out.audio.i_rate );
727 for ( i = 1; i < 14; i++ )
729 if ( p_enc->fmt_out.i_bitrate / 1000
730 <= mpa_bitrate_tab[i_frequency / 3][i] )
733 if ( p_enc->fmt_out.i_bitrate / 1000
734 != mpa_bitrate_tab[i_frequency / 3][i] )
737 "MPEG audio doesn't support bitrate=%d, using %d",
738 p_enc->fmt_out.i_bitrate,
739 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
740 p_enc->fmt_out.i_bitrate =
741 mpa_bitrate_tab[i_frequency / 3][i] * 1000;
742 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
746 p_context->codec = NULL;
748 ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
749 vlc_avcodec_unlock();
752 msg_Err( p_enc, "cannot open encoder" );
754 _("Streaming / Transcoding failed"),
755 "%s", _("VLC could not open the encoder.") );
762 msg_Err( p_enc, "cannot open encoder" );
763 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
764 "%s", _("VLC could not open the encoder.") );
770 if( i_codec_id == AV_CODEC_ID_FLAC )
772 p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
773 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
774 if( p_enc->fmt_out.p_extra )
776 uint8_t *p = p_enc->fmt_out.p_extra;
781 p[4] = 0x80; /* streaminfo block, last block before audio */
782 p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
783 p[6] = ( p_context->extradata_size >> 8 ) & 0xff;
784 p[7] = ( p_context->extradata_size ) & 0xff;
785 memcpy( &p[8], p_context->extradata, p_context->extradata_size );
789 p_enc->fmt_out.i_extra = 0;
794 p_enc->fmt_out.i_extra = p_context->extradata_size;
795 if( p_enc->fmt_out.i_extra )
797 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
798 if ( p_enc->fmt_out.p_extra == NULL )
802 memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
803 p_enc->fmt_out.i_extra );
807 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
809 if( p_enc->fmt_in.i_cat == AUDIO_ES )
811 p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
812 p_enc->fmt_in.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_in.i_codec );
814 p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8) *
816 p_sys->i_frame_size = p_context->frame_size > 1 ?
817 p_context->frame_size :
818 RAW_AUDIO_FRAME_SIZE;
819 p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes );
820 if ( p_sys->p_buffer == NULL )
824 p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
825 p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_out.i_codec );
827 if( p_context->frame_size > 1 )
828 p_sys->i_buffer_out = 8 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
830 p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes;
833 p_sys->frame = avcodec_alloc_frame();
838 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
840 p_enc->pf_encode_video = EncodeVideo;
841 p_enc->pf_encode_audio = EncodeAudio;
846 free( p_enc->fmt_out.p_extra );
847 free( p_sys->p_buffer );
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 const int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
865 p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
866 const int blocksize = __MAX( FF_MIN_BUFFER_SIZE,bytesPerPixel * p_sys->p_context->height * p_sys->p_context->width + 200 );
867 block_t *p_block = block_Alloc( blocksize );
869 if( likely(p_pict) ) {
870 avcodec_get_frame_defaults( p_sys->frame );
871 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
873 p_sys->frame->data[i_plane] = p_pict->p[i_plane].p_pixels;
874 p_sys->frame->linesize[i_plane] = p_pict->p[i_plane].i_pitch;
877 /* Let libavcodec select the frame type */
878 p_sys->frame->pict_type = 0;
880 p_sys->frame->repeat_pict = p_pict->i_nb_fields - 2;
881 p_sys->frame->interlaced_frame = !p_pict->b_progressive;
882 p_sys->frame->top_field_first = !!p_pict->b_top_field_first;
884 /* Set the pts of the frame being encoded */
885 p_sys->frame->pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE;
887 if ( p_sys->b_hurry_up && p_sys->frame->pts != (int64_t)AV_NOPTS_VALUE )
889 mtime_t current_date = mdate();
891 if ( current_date + HURRY_UP_GUARD3 > p_sys->frame->pts )
893 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
894 p_sys->p_context->trellis = 0;
895 msg_Dbg( p_enc, "hurry up mode 3" );
899 p_sys->p_context->mb_decision = p_sys->i_hq;
901 if ( current_date + HURRY_UP_GUARD2 > p_sys->frame->pts )
903 p_sys->p_context->trellis = 0;
904 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
905 + (HURRY_UP_GUARD2 + current_date - p_sys->frame->pts) / 500;
906 msg_Dbg( p_enc, "hurry up mode 2" );
910 p_sys->p_context->trellis = p_sys->b_trellis;
912 p_sys->p_context->noise_reduction =
913 p_sys->i_noise_reduction;
917 if ( current_date + HURRY_UP_GUARD1 > p_sys->frame->pts )
919 p_sys->frame->pict_type = AV_PICTURE_TYPE_P;
920 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
924 if ( p_sys->frame->pts != (int64_t)AV_NOPTS_VALUE && p_sys->frame->pts != 0 )
926 if ( p_sys->i_last_pts == p_sys->frame->pts )
928 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
929 "same PTS (%"PRId64 ")", p_sys->frame->pts );
932 else if ( p_sys->i_last_pts > p_sys->frame->pts )
934 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
935 "past (current: %"PRId64 ", last: %"PRId64")",
936 p_sys->frame->pts, p_sys->i_last_pts );
941 p_sys->i_last_pts = p_sys->frame->pts;
945 p_sys->frame->quality = p_sys->i_quality;
947 i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
948 p_block->i_buffer, p_sys->frame );
952 i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
953 p_block->i_buffer, NULL);
958 block_Release( p_block );
962 p_block->i_buffer = i_out;
964 /* FIXME, 3-2 pulldown is not handled correctly */
965 p_block->i_length = INT64_C(1000000) *
966 p_enc->fmt_in.video.i_frame_rate_base /
967 p_enc->fmt_in.video.i_frame_rate;
969 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
971 /* No delay -> output pts == input pts */
973 p_block->i_dts = p_pict->date;
974 p_block->i_pts = p_block->i_dts;
976 else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE &&
977 p_sys->p_context->coded_frame->pts != 0 &&
978 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
980 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
981 p_block->i_pts = p_sys->p_context->coded_frame->pts;
983 if( p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_I &&
984 p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_P )
986 p_block->i_dts = p_block->i_pts;
990 if( p_sys->i_last_ref_pts )
992 p_block->i_dts = p_sys->i_last_ref_pts;
996 /* Let's put something sensible */
997 p_block->i_dts = p_block->i_pts;
1000 p_sys->i_last_ref_pts = p_block->i_pts;
1005 /* Buggy libavcodec which doesn't update coded_frame->pts
1007 p_block->i_dts = p_block->i_pts = p_pict->date;
1010 switch ( p_sys->p_context->coded_frame->pict_type )
1012 case AV_PICTURE_TYPE_I:
1013 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1015 case AV_PICTURE_TYPE_P:
1016 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1018 case AV_PICTURE_TYPE_B:
1019 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1027 /****************************************************************************
1028 * EncodeAudio: the whole thing
1029 ****************************************************************************/
1030 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
1032 encoder_sys_t *p_sys = p_enc->p_sys;
1034 block_t *p_block, *p_chain = NULL;
1035 int got_packet,i_out,i_samples_left=0,i_data_offset = 0;
1037 //i_samples_left is amount of samples we get
1038 i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
1040 if( p_sys->i_samples_delay > 0 )
1043 //How much we need to copy from new packet
1044 const int leftover = __MAX(0,__MIN(i_samples_left, (p_sys->i_frame_size - p_sys->i_samples_delay)));
1046 avcodec_get_frame_defaults( p_sys->frame );
1047 p_sys->frame->nb_samples = p_sys->i_samples_delay + leftover;
1048 p_sys->frame->format = p_sys->p_context->sample_fmt;
1050 //Copy samples from new packet to buffer to get frame size
1051 if( likely( leftover ) )
1053 if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1054 aout_Deinterleave( p_sys->p_buffer+(p_sys->i_samples_delay*p_sys->i_sample_bytes*p_enc->fmt_in.audio.i_channels ),
1055 p_aout_buf->p_buffer, leftover, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec );
1057 memcpy( p_sys->p_buffer+(p_sys->i_samples_delay*p_sys->i_sample_bytes),
1058 p_aout_buf->p_buffer,
1059 leftover * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels
1063 if( avcodec_fill_audio_frame( p_sys->frame, p_enc->fmt_in.audio.i_channels,
1064 p_sys->p_context->sample_fmt,
1066 (p_sys->i_samples_delay + leftover) * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1068 msg_Err( p_enc, "Filling on leftovers error i_leftover %d i_samples_left %d samples_delay %d frame size %d", leftover, i_samples_left, p_sys->i_samples_delay, p_sys->i_frame_size );
1070 if( likely( p_aout_buf ) )
1071 p_sys->frame->pts = p_aout_buf->i_pts -
1072 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1073 (mtime_t)p_enc->fmt_in.audio.i_rate;
1075 p_sys->i_samples_delay = 0;
1076 i_data_offset += leftover * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
1077 i_samples_left -= leftover;
1079 p_block = block_Alloc( p_sys->i_buffer_out );
1080 av_init_packet( &packet );
1081 packet.data = p_block->p_buffer;
1082 packet.size = p_block->i_buffer;
1084 i_out = avcodec_encode_audio2( p_sys->p_context, &packet, p_sys->frame, &got_packet );
1085 p_block->i_buffer = packet.size;
1088 if( unlikely( !got_packet || ( i_out < 0 ) ) )
1092 msg_Err( p_enc,"Encoding problem...");
1096 p_block->i_buffer = packet.size;
1098 p_block->i_length = (mtime_t)1000000 *
1099 (mtime_t)p_sys->i_frame_size /
1100 (mtime_t)p_sys->p_context->sample_rate;
1102 p_block->i_dts = p_block->i_pts = packet.pts;
1104 block_ChainAppend( &p_chain, p_block );
1108 if( unlikely( !p_aout_buf ) )
1110 msg_Dbg(p_enc,"Flushing..");
1113 p_block = block_Alloc( p_sys->i_buffer_out );
1114 av_init_packet( &packet );
1115 packet.data = p_block->p_buffer;
1116 packet.size = p_block->i_buffer;
1118 i_out = avcodec_encode_audio2( p_sys->p_context, &packet, NULL, &got_packet );
1119 p_block->i_buffer = packet.size;
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 = packet.pts;
1127 if( !i_out && got_packet )
1128 block_ChainAppend( &p_chain, p_block );
1129 } while( got_packet && !i_out );
1133 while( i_samples_left >= p_sys->i_frame_size )
1135 AVPacket packet = {0};
1137 p_sys->frame->nb_samples = p_sys->i_frame_size;
1138 p_sys->frame->format = p_sys->p_context->sample_fmt;
1140 if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1142 aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer+i_data_offset,
1143 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec );
1144 if( avcodec_fill_audio_frame( p_sys->frame, p_enc->fmt_in.audio.i_channels,
1145 p_sys->p_context->sample_fmt,
1147 p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1149 msg_Err( p_enc, "filling error on encode" );
1151 if( avcodec_fill_audio_frame( p_sys->frame, p_enc->fmt_in.audio.i_channels,
1152 p_sys->p_context->sample_fmt,
1153 p_aout_buf->p_buffer+i_data_offset,
1154 p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1156 msg_Err( p_enc, "filling error on encode" );
1159 i_samples_left -= p_sys->i_frame_size;
1160 i_data_offset += p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
1162 p_sys->frame->pts = p_aout_buf->i_pts;
1164 p_block = block_Alloc( p_sys->i_buffer_out );
1165 av_init_packet( &packet );
1166 packet.data = p_block->p_buffer;
1167 packet.size = p_block->i_buffer;
1169 i_out = avcodec_encode_audio2( p_sys->p_context, &packet, p_sys->frame, &got_packet );
1170 p_block->i_buffer = packet.size;
1172 if( unlikely( !got_packet || ( i_out < 0 ) ) )
1176 msg_Err( p_enc,"Encoding problem..");
1179 block_Release( p_block );
1183 p_block->i_buffer = packet.size;
1185 p_block->i_length = (mtime_t)1000000 *
1186 (mtime_t)p_sys->i_frame_size /
1187 (mtime_t)p_sys->p_context->sample_rate;
1189 p_block->i_dts = p_block->i_pts = packet.pts;
1191 block_ChainAppend( &p_chain, p_block );
1193 if( i_samples_left < 0 )
1194 msg_Err( p_enc, "I_data_left overflow");
1196 // We have leftover samples that don't fill frame_size, and libavcodec doesn't seem to like
1197 // that frame has more data than p_sys->i_frame_size most of the cases currently.
1198 if( i_samples_left > 0 )
1200 if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1201 aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer+i_data_offset, i_samples_left, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec );
1203 memcpy( p_sys->p_buffer, p_aout_buf->p_buffer+i_data_offset , i_samples_left*p_sys->i_sample_bytes*p_enc->fmt_in.audio.i_channels);
1204 p_sys->i_samples_delay = i_samples_left;
1210 /*****************************************************************************
1211 * CloseEncoder: libavcodec encoder destruction
1212 *****************************************************************************/
1213 void CloseEncoder( vlc_object_t *p_this )
1215 encoder_t *p_enc = (encoder_t *)p_this;
1216 encoder_sys_t *p_sys = p_enc->p_sys;
1218 /*FIXME: we should use avcodec_free_frame, but we don't require so new avcodec that has it*/
1219 av_freep( &p_sys->frame );
1222 avcodec_close( p_sys->p_context );
1223 vlc_avcodec_unlock();
1224 av_free( p_sys->p_context );
1227 free( p_sys->p_buffer );