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>
54 #define HURRY_UP_GUARD1 (450000)
55 #define HURRY_UP_GUARD2 (300000)
56 #define HURRY_UP_GUARD3 (100000)
58 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
60 #define RAW_AUDIO_FRAME_SIZE (2048)
62 /*****************************************************************************
64 *****************************************************************************/
65 int OpenEncoder ( vlc_object_t * );
66 void CloseEncoder( vlc_object_t * );
68 static block_t *EncodeVideo( encoder_t *, picture_t * );
69 static block_t *EncodeAudio( encoder_t *, block_t * );
71 struct thread_context_t;
73 /*****************************************************************************
74 * thread_context_t : for multithreaded encoding
75 *****************************************************************************/
76 struct thread_context_t
80 AVCodecContext *p_context;
81 int (* pf_func)(AVCodecContext *c, void *arg);
90 /*****************************************************************************
91 * encoder_sys_t : ffmpeg encoder descriptor
92 *****************************************************************************/
99 AVCodecContext *p_context;
105 uint8_t *p_buffer_out;
111 mtime_t i_last_ref_pts;
112 mtime_t i_buggy_pts_detect;
124 /* Encoding settings */
131 int i_rc_buffer_size;
132 float f_rc_buffer_aggressivity;
135 bool b_interlace, b_interlace_me;
136 float f_i_quant_factor;
137 int i_noise_reduction;
140 int i_quality; /* for VBR */
141 float f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
142 int i_luma_elim, i_chroma_elim;
143 int i_aac_profile; /* AAC profile to use.*/
144 /* Used to work around stupid timestamping behaviour in libavcodec */
146 mtime_t pi_delay_pts[MAX_FRAME_DELAY];
149 static const char *const ppsz_enc_options[] = {
150 "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
151 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
152 "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
153 "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
154 "p-masking", "border-masking", "luma-elim-threshold",
155 "chroma-elim-threshold",
160 static const uint16_t mpa_bitrate_tab[2][15] =
162 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
163 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
166 static const uint16_t mpa_freq_tab[6] =
167 { 44100, 48000, 32000, 22050, 24000, 16000 };
169 static const uint16_t mpeg4_default_intra_matrix[64] = {
170 8, 17, 18, 19, 21, 23, 25, 27,
171 17, 18, 19, 21, 23, 25, 27, 28,
172 20, 21, 22, 23, 24, 26, 28, 30,
173 21, 22, 23, 24, 26, 28, 30, 32,
174 22, 23, 24, 26, 28, 30, 32, 35,
175 23, 24, 26, 28, 30, 32, 35, 38,
176 25, 26, 28, 30, 32, 35, 38, 41,
177 27, 28, 30, 32, 35, 38, 41, 45,
180 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
181 16, 17, 18, 19, 20, 21, 22, 23,
182 17, 18, 19, 20, 21, 22, 23, 24,
183 18, 19, 20, 21, 22, 23, 24, 25,
184 19, 20, 21, 22, 23, 24, 26, 27,
185 20, 21, 22, 23, 25, 26, 27, 28,
186 21, 22, 23, 24, 26, 27, 28, 30,
187 22, 23, 24, 26, 27, 28, 30, 31,
188 23, 24, 25, 27, 28, 30, 31, 33,
191 /*****************************************************************************
192 * OpenEncoder: probe the encoder
193 *****************************************************************************/
195 int OpenEncoder( vlc_object_t *p_this )
197 encoder_t *p_enc = (encoder_t *)p_this;
198 encoder_sys_t *p_sys;
199 AVCodecContext *p_context;
200 AVCodec *p_codec = NULL;
201 int i_codec_id, i_cat;
202 const char *psz_namecodec;
206 /* Initialization must be done before avcodec_find_encoder() */
209 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
211 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
214 i_codec_id = CODEC_ID_MP3;
215 psz_namecodec = "MPEG I/II Layer 3";
217 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2 )
220 i_codec_id = CODEC_ID_MP2;
221 psz_namecodec = "MPEG I/II Layer 2";
223 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP1V )
226 i_codec_id = CODEC_ID_MPEG1VIDEO;
227 psz_namecodec = "MPEG-1 video";
229 else if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
232 if( TestFfmpegChroma( -1, p_enc->fmt_out.i_codec ) != VLC_SUCCESS )
234 /* handed chroma output */
238 i_codec_id = CODEC_ID_RAWVIDEO;
239 psz_namecodec = "Raw video";
242 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
244 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
245 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
246 _("\"%s\" is no video encoder."), psz_namecodec );
250 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
252 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
253 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
254 _("\"%s\" is no audio encoder."), psz_namecodec );
258 if( p_enc->fmt_out.i_cat == SPU_ES )
260 /* We don't support subtitle encoding */
264 char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
265 if( psz_encoder && *psz_encoder )
267 p_codec = avcodec_find_encoder_by_name( psz_encoder );
269 msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
270 else if( p_codec->id != i_codec_id )
272 msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
273 psz_encoder, (char*)&p_enc->fmt_out.i_codec );
279 p_codec = avcodec_find_encoder( i_codec_id );
282 msg_Err( p_enc, "cannot find encoder %s\n"
283 "*** Your FFMPEG installation is crippled. ***\n"
284 "*** Please check with your FFMPEG packager. ***\n"
285 "*** This is NOT a VLC media player issue. ***", psz_namecodec );
287 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"), _(
288 /* I have had enough of all these MPEG-3 transcoding bug reports.
289 * Downstream packager, you had better not patch this out, or I will be really
290 * annoyed. Think about it - you don't want to fork the VLC translation files,
291 * do you? -- Courmisch, 2008-10-22 */
292 "It seems your FFMPEG (libavcodec) installation lacks the following encoder:\n"
294 "If you don't know how to fix this, ask for support from your distribution.\n"
296 "This is not an error inside VLC media player.\n"
297 "Do not contact the VideoLAN project about this issue.\n"),
302 /* Allocate the memory needed to store the encoder's structure */
303 if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
305 p_enc->p_sys = p_sys;
306 p_sys->p_codec = p_codec;
308 p_enc->pf_encode_video = EncodeVideo;
309 p_enc->pf_encode_audio = EncodeAudio;
311 p_sys->p_buffer = NULL;
312 p_sys->p_buffer_out = NULL;
313 p_sys->i_buffer_out = 0;
315 #if LIBAVCODEC_VERSION_MAJOR < 54
316 p_context = avcodec_alloc_context();
318 p_context = avcodec_alloc_context3(p_codec);
320 p_sys->p_context = p_context;
321 p_sys->p_context->codec_id = p_sys->p_codec->id;
322 p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
323 p_context->opaque = (void *)p_this;
324 p_context->dsp_mask = GetVlcDspMask(); /* set CPU capabilities */
326 p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
327 p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
328 p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
329 p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
330 p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
331 p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
332 p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
334 if( p_sys->b_hurry_up )
336 /* hurry up mode needs noise reduction, even small */
337 p_sys->i_noise_reduction = 1;
340 p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
341 p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
342 p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
343 p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
344 p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
346 f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
347 if( f_val < 0.01 || f_val > 255.0 ) f_val = 0;
348 p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
350 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
351 p_sys->i_hq = FF_MB_DECISION_RD;
352 if( psz_val && *psz_val )
354 if( !strcmp( psz_val, "rd" ) )
355 p_sys->i_hq = FF_MB_DECISION_RD;
356 else if( !strcmp( psz_val, "bits" ) )
357 p_sys->i_hq = FF_MB_DECISION_BITS;
358 else if( !strcmp( psz_val, "simple" ) )
359 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
361 p_sys->i_hq = FF_MB_DECISION_RD;
364 p_sys->i_hq = FF_MB_DECISION_RD;
367 p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
368 p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
369 p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
371 p_context->strict_std_compliance = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
373 p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
374 p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
375 p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
376 p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
377 p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
378 p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
380 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
381 /* ffmpeg uses faac encoder atm, and it has issues with
382 * other than low-complexity profile, so default to that */
383 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
384 if( psz_val && *psz_val )
386 if( !strncmp( psz_val, "main", 4 ) )
387 p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
388 else if( !strncmp( psz_val, "low", 3 ) )
389 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
390 #if 0 /* Not supported by FAAC encoder */
391 else if( !strncmp( psz_val, "ssr", 3 ) )
392 p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
394 else if( !strncmp( psz_val, "ltp", 3 ) )
395 p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
398 msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
399 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
404 if( p_enc->fmt_in.i_cat == VIDEO_ES )
406 if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
408 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
409 p_enc->fmt_in.video.i_height );
414 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
416 p_context->width = p_enc->fmt_in.video.i_width;
417 p_context->height = p_enc->fmt_in.video.i_height;
419 p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
420 p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
421 if( p_codec->supported_framerates )
423 AVRational target = {
424 .num = p_enc->fmt_in.video.i_frame_rate,
425 .den = p_enc->fmt_in.video.i_frame_rate_base,
427 int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
429 p_context->time_base.num = p_codec->supported_framerates[idx].den;
430 p_context->time_base.den = p_codec->supported_framerates[idx].num;
433 /* Defaults from ffmpeg.c */
434 p_context->qblur = 0.5;
435 p_context->qcompress = 0.5;
436 p_context->b_quant_offset = 1.25;
437 p_context->b_quant_factor = 1.25;
438 p_context->i_quant_offset = 0.0;
439 p_context->i_quant_factor = -0.8;
441 p_context->lumi_masking = p_sys->f_lumi_masking;
442 p_context->dark_masking = p_sys->f_dark_masking;
443 p_context->p_masking = p_sys->f_p_masking;
444 p_context->border_masking = p_sys->f_border_masking;
445 p_context->luma_elim_threshold = p_sys->i_luma_elim;
446 p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
448 if( p_sys->i_key_int > 0 )
449 p_context->gop_size = p_sys->i_key_int;
450 p_context->max_b_frames =
451 VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
452 p_context->b_frame_strategy = 0;
453 if( !p_context->max_b_frames &&
454 ( p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
455 p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ) )
456 p_context->flags |= CODEC_FLAG_LOW_DELAY;
458 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2V )
459 p_context->idct_algo = FF_IDCT_LIBMPEG2MMX;
461 av_reduce( &p_context->sample_aspect_ratio.num,
462 &p_context->sample_aspect_ratio.den,
463 p_enc->fmt_in.video.i_sar_num,
464 p_enc->fmt_in.video.i_sar_den, 1 << 30 );
466 p_sys->p_buffer_out = NULL;
468 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
469 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
470 GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
472 if( p_codec->pix_fmts )
474 const enum PixelFormat *p = p_codec->pix_fmts;
475 for( ; *p != -1; p++ )
477 if( *p == p_context->pix_fmt ) break;
479 if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
480 GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
481 p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
485 if ( p_sys->f_i_quant_factor != 0.0 )
486 p_context->i_quant_factor = p_sys->f_i_quant_factor;
488 p_context->noise_reduction = p_sys->i_noise_reduction;
490 if ( p_sys->b_mpeg4_matrix )
492 p_context->intra_matrix = mpeg4_default_intra_matrix;
493 p_context->inter_matrix = mpeg4_default_non_intra_matrix;
496 if ( p_sys->b_pre_me )
498 p_context->pre_me = 1;
499 p_context->me_pre_cmp = FF_CMP_CHROMA;
502 if ( p_sys->b_interlace )
504 if ( p_context->height <= 280 )
506 if ( p_context->height != 16 || p_context->width != 16 )
508 "disabling interlaced video because height=%d <= 280",
513 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
514 if ( p_sys->b_interlace_me )
515 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
519 p_context->trellis = p_sys->b_trellis;
521 if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
522 p_context->flags |= CODEC_FLAG_QSCALE;
523 /* These codecs cause libavcodec to exit if thread_count is > 1.
524 See libavcodec/mpegvideo_enc.c:MPV_encode_init and
525 libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
527 if ( i_codec_id == CODEC_ID_FLV1 ||
528 i_codec_id == CODEC_ID_H261 ||
529 i_codec_id == CODEC_ID_LJPEG ||
530 i_codec_id == CODEC_ID_MJPEG ||
531 i_codec_id == CODEC_ID_H263 ||
532 i_codec_id == CODEC_ID_H263P ||
533 i_codec_id == CODEC_ID_MSMPEG4V1 ||
534 i_codec_id == CODEC_ID_MSMPEG4V2 ||
535 i_codec_id == CODEC_ID_MSMPEG4V3 ||
536 i_codec_id == CODEC_ID_WMV1 ||
537 i_codec_id == CODEC_ID_WMV2 ||
538 i_codec_id == CODEC_ID_RV10 ||
539 i_codec_id == CODEC_ID_RV20 ||
540 i_codec_id == CODEC_ID_SVQ3 )
541 p_enc->i_threads = 1;
543 if( p_sys->i_vtolerance > 0 )
544 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
546 /* usually if someone sets bitrate, he likes more to get that bitrate
547 * over quality should help 'normal' user to get asked bitrate
549 if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
555 if( p_sys->i_qmin > 0 )
557 p_context->qmin = p_sys->i_qmin;
558 p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
560 if( p_sys->i_qmax > 0 )
562 p_context->qmax = p_sys->i_qmax;
563 p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
565 p_context->max_qdiff = 3;
567 p_context->mb_decision = p_sys->i_hq;
569 if( p_sys->i_quality )
571 p_context->flags |= CODEC_FLAG_QSCALE;
572 p_context->global_quality = p_sys->i_quality;
576 p_context->rc_qsquish = 1.0;
577 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
578 p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
579 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
580 /* This is from ffmpeg's ffmpeg.c : */
581 p_context->rc_initial_buffer_occupancy
582 = p_sys->i_rc_buffer_size * 3/4;
583 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
586 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
588 /* work around bug in libmp3lame encoding */
589 if( i_codec_id == CODEC_ID_MP3 && p_enc->fmt_in.audio.i_channels > 2 )
590 p_enc->fmt_in.audio.i_channels = 2;
592 p_context->codec_type = AVMEDIA_TYPE_AUDIO;
593 p_context->sample_fmt = p_codec->sample_fmts ?
594 p_codec->sample_fmts[0] :
596 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
597 p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
598 p_context->time_base.num = 1;
599 p_context->time_base.den = p_context->sample_rate;
600 p_context->channels = p_enc->fmt_out.audio.i_channels;
602 if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
604 /* XXX: FAAC does resample only when setting the INPUT samplerate
605 * to the desired value (-R option of the faac frontend)
606 p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
607 /* vlc should default to low-complexity profile, faac encoder
608 * has bug and aac audio has issues otherwise atm */
609 p_context->profile = p_sys->i_aac_profile;
613 /* Misc parameters */
614 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
616 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 69, 2 )
617 /* Set reasonable defaults to VP8, based on
618 libvpx-720p preset from libvpx ffmpeg-patch */
619 if( i_codec_id == CODEC_ID_VP8 )
621 /* Lets give bitrate tolerance */
622 p_context->bit_rate_tolerance = __MAX(2 * (int)p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
623 /* default to 120 frames between keyframe */
624 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
625 p_context->gop_size = 120;
626 /* Don't set rc-values atm, they were from time before
627 libvpx was officially in ffmpeg */
628 //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
629 //p_context->rc_min_rate = 40 * 1000; // 40k
630 /* seems that ffmpeg presets have 720p as divider for buffers */
631 if( p_enc->fmt_out.video.i_height >= 720 )
633 /* Check that we don't overrun users qmin/qmax values */
634 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
636 p_context->qmin = 10;
637 p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
640 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
642 p_context->qmax = 42;
643 p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
647 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
650 p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
655 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
657 p_context->level = 216;
658 p_context->profile = 0;
659 p_context->rc_buffer_aggressivity = 0.95;
660 p_context->token_partitions = 4;
661 p_context->mb_static_threshold = 0;
666 if( i_codec_id == CODEC_ID_RAWVIDEO )
668 /* XXX: hack: Force same codec (will be handled by transcode) */
669 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
670 GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
673 /* Make sure we get extradata filled by the encoder */
674 p_context->extradata_size = 0;
675 p_context->extradata = NULL;
676 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
678 if( p_enc->i_threads >= 1)
679 p_context->thread_count = p_enc->i_threads;
681 p_context->thread_count = vlc_GetCPUCount();
685 #if LIBAVCODEC_VERSION_MAJOR < 54
686 ret = avcodec_open( p_context, p_codec );
688 ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
690 vlc_avcodec_unlock();
693 if( p_enc->fmt_in.i_cat == AUDIO_ES &&
694 (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
695 || i_codec_id == CODEC_ID_MP3) )
697 if( p_context->channels > 2 )
699 p_context->channels = 2;
700 p_enc->fmt_in.audio.i_channels = 2; // FIXME
701 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
704 if( i_codec_id == CODEC_ID_MP2 || i_codec_id == CODEC_ID_MP3 )
708 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
710 if ( p_enc->fmt_out.audio.i_rate
711 == mpa_freq_tab[i_frequency] )
714 if ( i_frequency == 6 )
716 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
717 p_enc->fmt_out.audio.i_rate );
722 for ( i = 1; i < 14; i++ )
724 if ( p_enc->fmt_out.i_bitrate / 1000
725 <= mpa_bitrate_tab[i_frequency / 3][i] )
728 if ( p_enc->fmt_out.i_bitrate / 1000
729 != mpa_bitrate_tab[i_frequency / 3][i] )
732 "MPEG audio doesn't support bitrate=%d, using %d",
733 p_enc->fmt_out.i_bitrate,
734 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
735 p_enc->fmt_out.i_bitrate =
736 mpa_bitrate_tab[i_frequency / 3][i] * 1000;
737 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
741 p_context->codec = NULL;
743 #if LIBAVCODEC_VERSION_MAJOR < 54
744 ret = avcodec_open( p_context, p_codec );
746 ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
748 vlc_avcodec_unlock();
751 msg_Err( p_enc, "cannot open encoder" );
753 _("Streaming / Transcoding failed"),
754 "%s", _("VLC could not open the encoder.") );
761 msg_Err( p_enc, "cannot open encoder" );
762 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
763 "%s", _("VLC could not open the encoder.") );
769 if( i_codec_id == CODEC_ID_FLAC )
771 p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
772 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
773 if( p_enc->fmt_out.p_extra )
775 uint8_t *p = p_enc->fmt_out.p_extra;
780 p[4] = 0x80; /* streaminfo block, last block before audio */
781 p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
782 p[6] = ( p_context->extradata_size >> 8 ) & 0xff;
783 p[7] = ( p_context->extradata_size ) & 0xff;
784 memcpy( &p[8], p_context->extradata, p_context->extradata_size );
788 p_enc->fmt_out.i_extra = 0;
793 p_enc->fmt_out.i_extra = p_context->extradata_size;
794 if( p_enc->fmt_out.i_extra )
796 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
797 if ( p_enc->fmt_out.p_extra == NULL )
801 memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
802 p_enc->fmt_out.i_extra );
806 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
808 if( p_enc->fmt_in.i_cat == AUDIO_ES )
810 GetVlcAudioFormat( &p_enc->fmt_in.i_codec,
811 &p_enc->fmt_in.audio.i_bitspersample,
812 p_sys->p_context->sample_fmt );
813 p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8) *
815 p_sys->i_frame_size = p_context->frame_size > 1 ?
816 p_context->frame_size :
817 RAW_AUDIO_FRAME_SIZE;
818 p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes );
819 if ( p_sys->p_buffer == NULL )
823 p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
824 p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( vlc_fourcc_GetCodec( AUDIO_ES, p_enc->fmt_out.i_codec ) );
826 if( p_context->frame_size > 1 )
827 p_sys->i_buffer_out = 8 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
829 p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes;
830 p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
831 if ( p_sys->p_buffer_out == NULL )
837 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
841 free( p_enc->fmt_out.p_extra );
842 free( p_sys->p_buffer );
843 free( p_sys->p_buffer_out );
848 /****************************************************************************
849 * EncodeVideo: the whole thing
850 ****************************************************************************/
851 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
853 encoder_sys_t *p_sys = p_enc->p_sys;
856 /* Initialize the video output buffer the first time.
857 * This is done here instead of OpenEncoder() because we need the actual
858 * bits_per_pixel value, without having to assume anything.
860 const int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
861 p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
862 const int blocksize = __MAX( FF_MIN_BUFFER_SIZE,bytesPerPixel * p_sys->p_context->height * p_sys->p_context->width + 200 );
863 block_t *p_block = block_New( p_enc, blocksize );
865 if( likely(p_pict) ) {
867 memset( &frame, 0, sizeof( AVFrame ) );
868 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
870 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
871 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
874 /* Let ffmpeg select the frame type */
877 frame.repeat_pict = p_pict->i_nb_fields - 2;
878 frame.interlaced_frame = !p_pict->b_progressive;
879 frame.top_field_first = !!p_pict->b_top_field_first;
881 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
882 if( p_enc->fmt_out.i_codec != VLC_CODEC_MP4V )
884 frame.pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE;
886 if ( p_sys->b_hurry_up && frame.pts != (int64_t)AV_NOPTS_VALUE )
888 mtime_t current_date = mdate();
890 if ( current_date + HURRY_UP_GUARD3 > frame.pts )
892 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
893 p_sys->p_context->trellis = 0;
894 msg_Dbg( p_enc, "hurry up mode 3" );
898 p_sys->p_context->mb_decision = p_sys->i_hq;
900 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
902 p_sys->p_context->trellis = 0;
903 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
904 + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
905 msg_Dbg( p_enc, "hurry up mode 2" );
909 p_sys->p_context->trellis = p_sys->b_trellis;
911 p_sys->p_context->noise_reduction =
912 p_sys->i_noise_reduction;
916 if ( current_date + HURRY_UP_GUARD1 > frame.pts )
918 frame.pict_type = AV_PICTURE_TYPE_P;
919 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
925 frame.pts = (int64_t)AV_NOPTS_VALUE;
928 if ( frame.pts != (int64_t)AV_NOPTS_VALUE && frame.pts != 0 )
930 if ( p_sys->i_last_pts == frame.pts )
932 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
933 "same PTS (%"PRId64 ")", frame.pts );
936 else if ( p_sys->i_last_pts > frame.pts )
938 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
939 "past (current: %"PRId64 ", last: %"PRId64")",
940 frame.pts, p_sys->i_last_pts );
945 p_sys->i_last_pts = frame.pts;
949 frame.quality = p_sys->i_quality;
951 /* Ugly work-around for stupid libavcodec behaviour */
953 p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts;
954 frame.pts = p_sys->i_framenum * AV_TIME_BASE *
955 p_enc->fmt_in.video.i_frame_rate_base;
956 frame.pts += p_enc->fmt_in.video.i_frame_rate - 1;
957 frame.pts /= p_enc->fmt_in.video.i_frame_rate;
958 /* End work-around */
960 i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
961 p_block->i_buffer, &frame );
965 i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
966 p_block->i_buffer, NULL);
971 block_Release( p_block );
975 p_block->i_buffer = i_out;
977 /* FIXME, 3-2 pulldown is not handled correctly */
978 p_block->i_length = INT64_C(1000000) *
979 p_enc->fmt_in.video.i_frame_rate_base /
980 p_enc->fmt_in.video.i_frame_rate;
982 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
984 /* No delay -> output pts == input pts */
986 p_block->i_dts = p_pict->date;
987 p_block->i_pts = p_block->i_dts;
989 else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE &&
990 p_sys->p_context->coded_frame->pts != 0 &&
991 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
993 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
994 p_block->i_pts = p_sys->p_context->coded_frame->pts;
996 /* Ugly work-around for stupid libavcodec behaviour */
998 int64_t i_framenum = p_block->i_pts *
999 p_enc->fmt_in.video.i_frame_rate /
1000 p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE;
1002 p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY];
1004 /* End work-around */
1006 if( p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_I &&
1007 p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_P )
1009 p_block->i_dts = p_block->i_pts;
1013 if( p_sys->i_last_ref_pts )
1015 p_block->i_dts = p_sys->i_last_ref_pts;
1019 /* Let's put something sensible */
1020 p_block->i_dts = p_block->i_pts;
1023 p_sys->i_last_ref_pts = p_block->i_pts;
1028 /* Buggy libavcodec which doesn't update coded_frame->pts
1030 p_block->i_dts = p_block->i_pts = p_pict->date;
1033 switch ( p_sys->p_context->coded_frame->pict_type )
1035 case AV_PICTURE_TYPE_I:
1036 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1038 case AV_PICTURE_TYPE_P:
1039 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1041 case AV_PICTURE_TYPE_B:
1042 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1050 /****************************************************************************
1051 * EncodeAudio: the whole thing
1052 ****************************************************************************/
1053 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
1055 encoder_sys_t *p_sys = p_enc->p_sys;
1057 block_t *p_block, *p_chain = NULL;
1059 uint8_t *p_buffer = p_aout_buf->p_buffer;
1060 int i_samples = p_aout_buf->i_nb_samples;
1061 int i_samples_delay = p_sys->i_samples_delay;
1063 p_sys->i_pts = p_aout_buf->i_pts -
1064 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1065 (mtime_t)p_enc->fmt_in.audio.i_rate;
1067 p_sys->i_samples_delay += i_samples;
1069 while( p_sys->i_samples_delay >= p_sys->i_frame_size )
1073 p_block = block_New( p_enc, p_sys->i_buffer_out );
1075 if( i_samples_delay )
1077 /* Take care of the left-over from last time */
1078 int i_delay_size = i_samples_delay;
1079 int i_size = (p_sys->i_frame_size - i_delay_size) *
1080 p_sys->i_sample_bytes;
1082 memcpy( p_sys->p_buffer + i_delay_size * p_sys->i_sample_bytes,
1084 p_buffer -= i_delay_size * p_sys->i_sample_bytes;
1085 i_samples += i_samples_delay;
1086 i_samples_delay = 0;
1088 p_samples = p_sys->p_buffer;
1092 p_samples = p_buffer;
1095 i_out = avcodec_encode_audio( p_sys->p_context, p_block->p_buffer,
1096 p_block->i_buffer, p_samples );
1099 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
1101 p_buffer += p_sys->i_frame_size * p_sys->i_sample_bytes;
1102 p_sys->i_samples_delay -= p_sys->i_frame_size;
1103 i_samples -= p_sys->i_frame_size;
1107 block_Release( p_block );
1111 p_block->i_buffer = i_out;
1113 p_block->i_length = (mtime_t)1000000 *
1114 (mtime_t)p_sys->i_frame_size /
1115 (mtime_t)p_sys->p_context->sample_rate;
1117 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1120 p_sys->i_pts += p_block->i_length;
1121 block_ChainAppend( &p_chain, p_block );
1124 /* Backup the remaining raw samples */
1127 memcpy( &p_sys->p_buffer[i_samples_delay * p_sys->i_sample_bytes],
1129 i_samples * p_sys->i_sample_bytes );
1135 /*****************************************************************************
1136 * CloseEncoder: ffmpeg encoder destruction
1137 *****************************************************************************/
1138 void CloseEncoder( vlc_object_t *p_this )
1140 encoder_t *p_enc = (encoder_t *)p_this;
1141 encoder_sys_t *p_sys = p_enc->p_sys;
1144 avcodec_close( p_sys->p_context );
1145 vlc_avcodec_unlock();
1146 av_free( p_sys->p_context );
1148 free( p_sys->p_buffer );
1149 free( p_sys->p_buffer_out );