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>
43 #include <libavcodec/avcodec.h>
48 #if LIBAVUTIL_VERSION_CHECK( 52,2,6,0,0 )
49 # include <libavutil/channel_layout.h>
52 #define HURRY_UP_GUARD1 (450000)
53 #define HURRY_UP_GUARD2 (300000)
54 #define HURRY_UP_GUARD3 (100000)
56 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
58 #define RAW_AUDIO_FRAME_SIZE (2048)
60 /*****************************************************************************
62 *****************************************************************************/
63 int OpenEncoder ( vlc_object_t * );
64 void CloseEncoder( vlc_object_t * );
66 static block_t *EncodeVideo( encoder_t *, picture_t * );
67 static block_t *EncodeAudio( encoder_t *, block_t * );
69 struct thread_context_t;
71 /*****************************************************************************
72 * thread_context_t : for multithreaded encoding
73 *****************************************************************************/
74 struct thread_context_t
78 AVCodecContext *p_context;
79 int (* pf_func)(AVCodecContext *c, void *arg);
88 /*****************************************************************************
89 * encoder_sys_t : libavcodec encoder descriptor
90 *****************************************************************************/
94 * libavcodec properties
97 AVCodecContext *p_context;
100 * Common buffer mainly for audio as frame size in there needs usually be constant
104 uint8_t *p_interleave_buf;
109 mtime_t i_last_ref_pts;
110 mtime_t i_buggy_pts_detect;
117 size_t i_sample_bytes;
119 size_t i_samples_delay; //How much samples in delay buffer
121 bool b_variable; //Encoder can be fed with any size frames not just frame_size
125 /* Encoding settings */
132 int i_rc_buffer_size;
133 float f_rc_buffer_aggressivity;
136 bool b_interlace, b_interlace_me;
137 float f_i_quant_factor;
138 int i_noise_reduction;
141 int i_quality; /* for VBR */
142 float f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
143 #if (LIBAVCODEC_VERSION_MAJOR < 55)
144 int i_luma_elim, i_chroma_elim;
146 int i_aac_profile; /* AAC profile to use.*/
151 static const char *const ppsz_enc_options[] = {
152 "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
153 "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
154 "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
155 "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
156 "p-masking", "border-masking",
157 #if (LIBAVCODEC_VERSION_MAJOR < 55)
158 "luma-elim-threshold", "chroma-elim-threshold",
160 "aac-profile", "options",
164 static const uint16_t mpa_bitrate_tab[2][15] =
166 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
167 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
170 static const uint16_t mpa_freq_tab[6] =
171 { 44100, 48000, 32000, 22050, 24000, 16000 };
173 static const uint16_t mpeg4_default_intra_matrix[64] = {
174 8, 17, 18, 19, 21, 23, 25, 27,
175 17, 18, 19, 21, 23, 25, 27, 28,
176 20, 21, 22, 23, 24, 26, 28, 30,
177 21, 22, 23, 24, 26, 28, 30, 32,
178 22, 23, 24, 26, 28, 30, 32, 35,
179 23, 24, 26, 28, 30, 32, 35, 38,
180 25, 26, 28, 30, 32, 35, 38, 41,
181 27, 28, 30, 32, 35, 38, 41, 45,
184 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
185 16, 17, 18, 19, 20, 21, 22, 23,
186 17, 18, 19, 20, 21, 22, 23, 24,
187 18, 19, 20, 21, 22, 23, 24, 25,
188 19, 20, 21, 22, 23, 24, 26, 27,
189 20, 21, 22, 23, 25, 26, 27, 28,
190 21, 22, 23, 24, 26, 27, 28, 30,
191 22, 23, 24, 26, 27, 28, 30, 31,
192 23, 24, 25, 27, 28, 30, 31, 33,
195 #if LIBAVUTIL_VERSION_CHECK( 51, 27, 2, 46, 100 )
196 static const int DEFAULT_ALIGN = 0;
198 static const int DEFAULT_ALIGN = 1;
202 /*****************************************************************************
203 * OpenEncoder: probe the encoder
204 *****************************************************************************/
206 int OpenEncoder( vlc_object_t *p_this )
208 encoder_t *p_enc = (encoder_t *)p_this;
209 encoder_sys_t *p_sys;
210 AVCodecContext *p_context;
211 AVCodec *p_codec = NULL;
214 const char *psz_namecodec;
218 /* Initialization must be done before avcodec_find_encoder() */
221 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
223 if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
226 i_codec_id = AV_CODEC_ID_MP3;
227 psz_namecodec = "MPEG I/II Layer 3";
229 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2 )
232 i_codec_id = AV_CODEC_ID_MP2;
233 psz_namecodec = "MPEG I/II Layer 2";
235 else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP1V )
238 i_codec_id = AV_CODEC_ID_MPEG1VIDEO;
239 psz_namecodec = "MPEG-1 video";
241 else if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
244 if( FindFfmpegChroma( p_enc->fmt_out.i_codec ) == PIX_FMT_NONE )
245 return VLC_EGENERIC; /* handed chroma output */
248 i_codec_id = AV_CODEC_ID_RAWVIDEO;
249 psz_namecodec = "Raw video";
252 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
254 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
255 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
256 _("\"%s\" is no video encoder."), psz_namecodec );
260 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
262 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
263 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
264 _("\"%s\" is no audio encoder."), psz_namecodec );
268 if( p_enc->fmt_out.i_cat == SPU_ES )
270 /* We don't support subtitle encoding */
274 char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
275 if( psz_encoder && *psz_encoder )
277 p_codec = avcodec_find_encoder_by_name( psz_encoder );
279 msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
280 else if( p_codec->id != i_codec_id )
282 msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
283 psz_encoder, (char*)&p_enc->fmt_out.i_codec );
289 p_codec = avcodec_find_encoder( i_codec_id );
292 msg_Err( p_enc, "cannot find encoder %s\n"
293 "*** Your Libav/FFmpeg installation is crippled. ***\n"
294 "*** Please check with your Libav/FFmpeg packager. ***\n"
295 "*** This is NOT a VLC media player issue. ***", psz_namecodec );
297 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"), _(
298 /* I have had enough of all these MPEG-3 transcoding bug reports.
299 * Downstream packager, you had better not patch this out, or I will be really
300 * annoyed. Think about it - you don't want to fork the VLC translation files,
301 * do you? -- Courmisch, 2008-10-22 */
302 "It seems your Libav/FFmpeg (libavcodec) installation lacks the following encoder:\n"
304 "If you don't know how to fix this, ask for support from your distribution.\n"
306 "This is not an error inside VLC media player.\n"
307 "Do not contact the VideoLAN project about this issue.\n"),
312 /* Allocate the memory needed to store the encoder's structure */
313 if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
315 p_enc->p_sys = p_sys;
316 p_sys->i_samples_delay = 0;
317 p_sys->p_codec = p_codec;
318 p_sys->b_planar = false;
320 p_sys->p_buffer = NULL;
321 p_sys->p_interleave_buf = NULL;
322 p_sys->i_buffer_out = 0;
324 p_context = avcodec_alloc_context3(p_codec);
325 p_sys->p_context = p_context;
326 p_sys->p_context->codec_id = p_sys->p_codec->id;
327 p_context->thread_type = 0;
328 p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
329 p_context->opaque = (void *)p_this;
331 /* set CPU capabilities */
332 #if LIBAVUTIL_VERSION_CHECK(51, 25, 0, 42, 100)
333 av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
335 p_context->dsp_mask = GetVlcDspMask();
338 p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
339 p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
340 p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
341 p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
342 p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
343 p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
344 p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
346 if( p_sys->b_hurry_up )
348 /* hurry up mode needs noise reduction, even small */
349 p_sys->i_noise_reduction = 1;
352 p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
353 p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
354 p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
355 p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
356 p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
358 f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
360 p_sys->i_quality = 0;
361 if( f_val < 0.01 || f_val > 255.0 )
364 p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
366 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
367 p_sys->i_hq = FF_MB_DECISION_RD;
368 if( psz_val && *psz_val )
370 if( !strcmp( psz_val, "rd" ) )
371 p_sys->i_hq = FF_MB_DECISION_RD;
372 else if( !strcmp( psz_val, "bits" ) )
373 p_sys->i_hq = FF_MB_DECISION_BITS;
374 else if( !strcmp( psz_val, "simple" ) )
375 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
377 p_sys->i_hq = FF_MB_DECISION_RD;
380 p_sys->i_hq = FF_MB_DECISION_RD;
383 p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
384 p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
385 p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
387 p_context->strict_std_compliance = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
389 p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
390 p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
391 p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
392 p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
393 #if (LIBAVCODEC_VERSION_MAJOR < 55)
394 p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
395 p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
398 psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
399 /* libavcodec uses faac encoder atm, and it has issues with
400 * other than low-complexity profile, so default to that */
401 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
402 if( psz_val && *psz_val )
404 if( !strncmp( psz_val, "main", 4 ) )
405 p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
406 else if( !strncmp( psz_val, "low", 3 ) )
407 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
408 #if 0 /* Not supported by FAAC encoder */
409 else if( !strncmp( psz_val, "ssr", 3 ) )
410 p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
412 else if( !strncmp( psz_val, "ltp", 3 ) )
413 p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
414 #if LIBAVCODEC_VERSION_CHECK( 54, 19, 0, 35, 100 )
415 /* These require libavcodec with libfdk-aac */
416 else if( !strncmp( psz_val, "hev2", 4 ) )
417 p_sys->i_aac_profile = FF_PROFILE_AAC_HE_V2;
418 else if( !strncmp( psz_val, "hev1", 4 ) )
419 p_sys->i_aac_profile = FF_PROFILE_AAC_HE;
423 msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
424 p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
429 if( p_enc->fmt_in.i_cat == VIDEO_ES )
431 if( !p_enc->fmt_in.video.i_visible_width || !p_enc->fmt_in.video.i_visible_height )
433 msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_visible_width,
434 p_enc->fmt_in.video.i_visible_height );
439 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
441 p_context->width = p_enc->fmt_in.video.i_visible_width;
442 p_context->height = p_enc->fmt_in.video.i_visible_height;
444 /* if we don't have i_frame_rate_base, we are probing and just checking if we can find codec
445 * so set fps to 25 as some codecs (DIV3 atleast) needs time_base data */
446 p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base ? p_enc->fmt_in.video.i_frame_rate_base : 1;
447 p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate_base ? p_enc->fmt_in.video.i_frame_rate : 25 ;
448 if( p_codec->supported_framerates )
450 AVRational target = {
451 .num = p_enc->fmt_in.video.i_frame_rate,
452 .den = p_enc->fmt_in.video.i_frame_rate_base,
454 int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
456 p_context->time_base.num = p_codec->supported_framerates[idx].den;
457 p_context->time_base.den = p_codec->supported_framerates[idx].num;
459 msg_Dbg( p_enc, "Time base set to %d/%d", p_context->time_base.num, p_context->time_base.den );
461 /* Defaults from ffmpeg.c */
462 p_context->qblur = 0.5;
463 p_context->qcompress = 0.5;
464 p_context->b_quant_offset = 1.25;
465 p_context->b_quant_factor = 1.25;
466 p_context->i_quant_offset = 0.0;
467 p_context->i_quant_factor = -0.8;
469 p_context->lumi_masking = p_sys->f_lumi_masking;
470 p_context->dark_masking = p_sys->f_dark_masking;
471 p_context->p_masking = p_sys->f_p_masking;
472 p_context->border_masking = p_sys->f_border_masking;
473 #if (LIBAVCODEC_VERSION_MAJOR < 55)
474 p_context->luma_elim_threshold = p_sys->i_luma_elim;
475 p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
478 if( p_sys->i_key_int > 0 )
479 p_context->gop_size = p_sys->i_key_int;
480 p_context->max_b_frames =
481 VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
482 p_context->b_frame_strategy = 0;
483 if( !p_context->max_b_frames &&
484 ( p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
485 p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ) )
486 p_context->flags |= CODEC_FLAG_LOW_DELAY;
488 av_reduce( &p_context->sample_aspect_ratio.num,
489 &p_context->sample_aspect_ratio.den,
490 p_enc->fmt_in.video.i_sar_num,
491 p_enc->fmt_in.video.i_sar_den, 1 << 30 );
494 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
495 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
496 GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
498 if( p_codec->pix_fmts )
500 const enum PixelFormat *p = p_codec->pix_fmts;
501 for( ; *p != -1; p++ )
503 if( *p == p_context->pix_fmt ) break;
505 if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
506 GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
507 p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
511 if ( p_sys->f_i_quant_factor != 0.0 )
512 p_context->i_quant_factor = p_sys->f_i_quant_factor;
514 p_context->noise_reduction = p_sys->i_noise_reduction;
516 if ( p_sys->b_mpeg4_matrix )
518 p_context->intra_matrix = mpeg4_default_intra_matrix;
519 p_context->inter_matrix = mpeg4_default_non_intra_matrix;
522 if ( p_sys->b_pre_me )
524 p_context->pre_me = 1;
525 p_context->me_pre_cmp = FF_CMP_CHROMA;
528 if ( p_sys->b_interlace )
530 if ( p_context->height <= 280 )
532 if ( p_context->height != 16 || p_context->width != 16 )
534 "disabling interlaced video because height=%d <= 280",
539 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
540 if ( p_sys->b_interlace_me )
541 p_context->flags |= CODEC_FLAG_INTERLACED_ME;
545 p_context->trellis = p_sys->b_trellis;
547 if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
548 p_context->flags |= CODEC_FLAG_QSCALE;
549 /* These codecs cause libavcodec to exit if thread_count is > 1.
550 See libavcodec/mpegvideo_enc.c:MPV_encode_init and
551 libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
553 if ( i_codec_id == AV_CODEC_ID_FLV1 ||
554 i_codec_id == AV_CODEC_ID_H261 ||
555 i_codec_id == AV_CODEC_ID_LJPEG ||
556 i_codec_id == AV_CODEC_ID_MJPEG ||
557 i_codec_id == AV_CODEC_ID_H263 ||
558 i_codec_id == AV_CODEC_ID_H263P ||
559 i_codec_id == AV_CODEC_ID_MSMPEG4V1 ||
560 i_codec_id == AV_CODEC_ID_MSMPEG4V2 ||
561 i_codec_id == AV_CODEC_ID_MSMPEG4V3 ||
562 i_codec_id == AV_CODEC_ID_WMV1 ||
563 i_codec_id == AV_CODEC_ID_WMV2 ||
564 i_codec_id == AV_CODEC_ID_RV10 ||
565 i_codec_id == AV_CODEC_ID_RV20 ||
566 i_codec_id == AV_CODEC_ID_SVQ3 )
567 p_enc->i_threads = 1;
569 if( p_sys->i_vtolerance > 0 )
570 p_context->bit_rate_tolerance = p_sys->i_vtolerance;
572 /* usually if someone sets bitrate, he likes more to get that bitrate
573 * over quality should help 'normal' user to get asked bitrate
575 if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
581 if( p_sys->i_qmin > 0 )
583 p_context->qmin = p_sys->i_qmin;
584 p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
586 if( p_sys->i_qmax > 0 )
588 p_context->qmax = p_sys->i_qmax;
589 p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
591 p_context->max_qdiff = 3;
593 p_context->mb_decision = p_sys->i_hq;
595 if( p_sys->i_quality )
597 p_context->flags |= CODEC_FLAG_QSCALE;
598 p_context->global_quality = p_sys->i_quality;
602 p_context->rc_qsquish = 1.0;
603 /* Default to 1/2 second buffer for given bitrate unless defined otherwise*/
604 if( !p_sys->i_rc_buffer_size )
606 p_sys->i_rc_buffer_size = p_enc->fmt_out.i_bitrate * 8 / 2;
608 msg_Dbg( p_enc, "rc buffer size %d bits", p_sys->i_rc_buffer_size );
609 /* Set maxrate/minrate to bitrate to try to get CBR */
610 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
611 p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
612 p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
613 /* This is from ffmpeg's ffmpeg.c : */
614 p_context->rc_initial_buffer_occupancy
615 = p_sys->i_rc_buffer_size * 3/4;
616 p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
619 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
621 /* work around bug in libmp3lame encoding */
622 if( i_codec_id == AV_CODEC_ID_MP3 && p_enc->fmt_out.audio.i_channels > 2 )
623 p_enc->fmt_out.audio.i_channels = 2;
624 p_context->codec_type = AVMEDIA_TYPE_AUDIO;
625 p_context->sample_fmt = p_codec->sample_fmts ?
626 p_codec->sample_fmts[0] :
629 /* Try to match avcodec input format to vlc format so we could avoid one
631 if( GetVlcAudioFormat( p_context->sample_fmt ) != p_enc->fmt_in.i_codec )
633 msg_Dbg( p_enc, "Trying to find more suitable sample format instead of %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
634 for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
636 if( GetVlcAudioFormat( p_codec->sample_fmts[i] ) == p_enc->fmt_in.i_codec )
638 p_context->sample_fmt = p_codec->sample_fmts[i];
639 msg_Dbg( p_enc, "Using %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
644 p_sys->b_planar = av_sample_fmt_is_planar( p_context->sample_fmt );
645 // Try if we can use interleaved format for codec input as VLC doesn't really do planar audio yet
646 // FIXME: Remove when planar/interleaved audio in vlc is equally supported
647 if( p_sys->b_planar )
649 msg_Dbg( p_enc, "Trying to find packet sample format instead of planar %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
650 for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
652 if( !av_sample_fmt_is_planar( p_codec->sample_fmts[i] ) )
654 p_context->sample_fmt = p_codec->sample_fmts[i];
655 msg_Dbg( p_enc, "Changing to packet format %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
660 msg_Dbg( p_enc, "Ended up using %s as sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
661 p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_context->sample_fmt );
662 p_sys->b_planar = av_sample_fmt_is_planar( p_context->sample_fmt );
664 p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
665 date_Init( &p_sys->buffer_date, p_enc->fmt_out.audio.i_rate, 1 );
666 date_Set( &p_sys->buffer_date, AV_NOPTS_VALUE );
667 p_context->time_base.num = 1;
668 p_context->time_base.den = p_context->sample_rate;
669 p_context->channels = p_enc->fmt_out.audio.i_channels;
670 #if LIBAVUTIL_VERSION_CHECK( 52, 2, 6, 0, 0)
671 p_context->channel_layout = av_get_default_channel_layout( p_context->channels );
674 if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
676 /* XXX: FAAC does resample only when setting the INPUT samplerate
677 * to the desired value (-R option of the faac frontend)
678 p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
679 /* vlc should default to low-complexity profile, faac encoder
680 * has bug and aac audio has issues otherwise atm */
681 p_context->profile = p_sys->i_aac_profile;
685 /* Misc parameters */
686 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
688 /* Set reasonable defaults to VP8, based on
689 libvpx-720p preset from libvpx ffmpeg-patch */
690 if( i_codec_id == AV_CODEC_ID_VP8 )
692 /* Lets give bitrate tolerance */
693 p_context->bit_rate_tolerance = __MAX(2 * (int)p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
694 /* default to 120 frames between keyframe */
695 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
696 p_context->gop_size = 120;
697 /* Don't set rc-values atm, they were from time before
698 libvpx was officially in FFmpeg */
699 //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
700 //p_context->rc_min_rate = 40 * 1000; // 40k
701 /* seems that FFmpeg presets have 720p as divider for buffers */
702 if( p_enc->fmt_out.video.i_visible_height >= 720 )
704 /* Check that we don't overrun users qmin/qmax values */
705 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
707 p_context->qmin = 10;
708 p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
711 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
713 p_context->qmax = 42;
714 p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
718 if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
721 p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
726 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
728 p_context->level = 216;
729 p_context->profile = 0;
730 p_context->rc_buffer_aggressivity = 0.95;
731 p_context->token_partitions = 4;
732 p_context->mb_static_threshold = 0;
736 if( i_codec_id == AV_CODEC_ID_RAWVIDEO )
738 /* XXX: hack: Force same codec (will be handled by transcode) */
739 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
740 GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
743 /* Make sure we get extradata filled by the encoder */
744 p_context->extradata_size = 0;
745 p_context->extradata = NULL;
746 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
748 if( p_enc->i_threads >= 1)
749 p_context->thread_count = p_enc->i_threads;
751 p_context->thread_count = vlc_GetCPUCount();
754 char *psz_opts = var_InheritString(p_enc, ENC_CFG_PREFIX "options");
755 AVDictionary *options = NULL;
756 if (psz_opts && *psz_opts)
757 options = vlc_av_get_options(psz_opts);
761 ret = avcodec_open2( p_context, p_codec, options ? &options : NULL );
762 vlc_avcodec_unlock();
764 AVDictionaryEntry *t = NULL;
765 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
766 msg_Err(p_enc, "Unknown option \"%s\"", t->key);
771 if( p_enc->fmt_in.i_cat != AUDIO_ES ||
772 (p_context->channels <= 2 && i_codec_id != AV_CODEC_ID_MP2
773 && i_codec_id != AV_CODEC_ID_MP3) )
776 static const char types[][12] = {
777 [UNKNOWN_ES] = N_("unknown"), [VIDEO_ES] = N_("video"),
778 [AUDIO_ES] = N_("audio"), [SPU_ES] = N_("subpicture"),
780 const char *type = types[0];
785 } fcc = { .value = p_enc->fmt_out.i_codec };
787 if (likely((unsigned)p_enc->fmt_in.i_cat < sizeof (types) / sizeof (types[0])))
788 type = types[p_enc->fmt_in.i_cat];
789 msg_Err( p_enc, "cannot open %4.4s %s encoder", fcc.txt, type );
790 dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
791 _("VLC could not open the %4.4s %s encoder."),
792 fcc.txt, vlc_gettext(type) );
793 av_dict_free(&options);
797 if( p_context->channels > 2 )
799 p_context->channels = 2;
800 p_enc->fmt_in.audio.i_channels = 2; // FIXME
801 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
804 if( i_codec_id == AV_CODEC_ID_MP2 || i_codec_id == AV_CODEC_ID_MP3 )
807 es_format_t *fmt = &p_enc->fmt_out;
809 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
810 if ( fmt->audio.i_rate == mpa_freq_tab[i_frequency] )
813 if ( i_frequency == 6 )
815 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
817 av_dict_free(&options);
821 for ( i = 1; i < 14; i++ )
822 if (fmt->i_bitrate/1000 <= mpa_bitrate_tab[i_frequency / 3][i])
825 if (fmt->i_bitrate / 1000 != mpa_bitrate_tab[i_frequency / 3][i])
828 "MPEG audio doesn't support bitrate=%d, using %d",
830 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
831 fmt->i_bitrate = mpa_bitrate_tab[i_frequency / 3][i] * 1000;
832 p_context->bit_rate = fmt->i_bitrate;
836 p_context->codec = NULL;
838 ret = avcodec_open2( p_context, p_codec, options ? &options : NULL );
839 vlc_avcodec_unlock();
844 av_dict_free(&options);
846 if( i_codec_id == AV_CODEC_ID_FLAC )
848 p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
849 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
850 if( p_enc->fmt_out.p_extra )
852 uint8_t *p = p_enc->fmt_out.p_extra;
857 p[4] = 0x80; /* streaminfo block, last block before audio */
858 p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
859 p[6] = ( p_context->extradata_size >> 8 ) & 0xff;
860 p[7] = ( p_context->extradata_size ) & 0xff;
861 memcpy( &p[8], p_context->extradata, p_context->extradata_size );
865 p_enc->fmt_out.i_extra = 0;
870 p_enc->fmt_out.i_extra = p_context->extradata_size;
871 if( p_enc->fmt_out.i_extra )
873 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
874 if ( p_enc->fmt_out.p_extra == NULL )
878 memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
879 p_enc->fmt_out.i_extra );
883 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
885 if( p_enc->fmt_in.i_cat == AUDIO_ES )
887 p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
888 p_enc->fmt_in.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_in.i_codec );
890 p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8);
891 p_sys->i_frame_size = p_context->frame_size > 1 ?
892 p_context->frame_size :
894 p_sys->i_buffer_out = av_samples_get_buffer_size(NULL,
895 p_sys->p_context->channels, p_sys->i_frame_size,
896 p_sys->p_context->sample_fmt, DEFAULT_ALIGN);
897 p_sys->p_buffer = malloc( p_sys->i_buffer_out );
898 if ( unlikely( p_sys->p_buffer == NULL ) )
902 p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
903 p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_out.i_codec );
904 //b_variable tells if we can feed any size frames to encoder
905 p_sys->b_variable = p_context->frame_size ? false : true;
908 if( p_sys->b_planar )
910 p_sys->p_interleave_buf = malloc( p_sys->i_buffer_out );
911 if( unlikely( p_sys->p_interleave_buf == NULL ) )
916 p_sys->frame = avcodec_alloc_frame();
921 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
923 p_enc->pf_encode_video = EncodeVideo;
924 p_enc->pf_encode_audio = EncodeAudio;
929 free( p_enc->fmt_out.p_extra );
930 free( p_sys->p_buffer );
931 free( p_sys->p_interleave_buf );
936 /****************************************************************************
937 * EncodeVideo: the whole thing
938 ****************************************************************************/
939 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
941 encoder_sys_t *p_sys = p_enc->p_sys;
943 /* Initialize the video output buffer the first time.
944 * This is done here instead of OpenEncoder() because we need the actual
945 * bits_per_pixel value, without having to assume anything.
947 const int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
948 p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
949 const int blocksize = __MAX( FF_MIN_BUFFER_SIZE,bytesPerPixel * p_sys->p_context->height * p_sys->p_context->width + 200 );
950 block_t *p_block = block_Alloc( blocksize );
951 if( unlikely(p_block == NULL) )
954 AVFrame *frame = NULL;
955 if( likely(p_pict) ) {
956 frame = p_sys->frame;
957 avcodec_get_frame_defaults( frame );
958 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
960 p_sys->frame->data[i_plane] = p_pict->p[i_plane].p_pixels;
961 p_sys->frame->linesize[i_plane] = p_pict->p[i_plane].i_pitch;
964 /* Let libavcodec select the frame type */
965 frame->pict_type = 0;
967 frame->repeat_pict = p_pict->i_nb_fields - 2;
968 frame->interlaced_frame = !p_pict->b_progressive;
969 frame->top_field_first = !!p_pict->b_top_field_first;
971 /* Set the pts of the frame being encoded */
972 frame->pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
974 if ( p_sys->b_hurry_up && frame->pts != AV_NOPTS_VALUE )
976 mtime_t current_date = mdate();
978 if ( current_date + HURRY_UP_GUARD3 > frame->pts )
980 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
981 p_sys->p_context->trellis = 0;
982 msg_Dbg( p_enc, "hurry up mode 3" );
986 p_sys->p_context->mb_decision = p_sys->i_hq;
988 if ( current_date + HURRY_UP_GUARD2 > frame->pts )
990 p_sys->p_context->trellis = 0;
991 p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
992 + (HURRY_UP_GUARD2 + current_date - frame->pts) / 500;
993 msg_Dbg( p_enc, "hurry up mode 2" );
997 p_sys->p_context->trellis = p_sys->b_trellis;
999 p_sys->p_context->noise_reduction =
1000 p_sys->i_noise_reduction;
1004 if ( current_date + HURRY_UP_GUARD1 > frame->pts )
1006 frame->pict_type = AV_PICTURE_TYPE_P;
1007 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
1011 if ( frame->pts != AV_NOPTS_VALUE && frame->pts != 0 )
1013 if ( p_sys->i_last_pts == frame->pts )
1015 msg_Warn( p_enc, "almost fed libavcodec with two frames with "
1016 "the same PTS (%"PRId64 ")", frame->pts );
1019 else if ( p_sys->i_last_pts > frame->pts )
1021 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
1022 "past (current: %"PRId64 ", last: %"PRId64")",
1023 frame->pts, p_sys->i_last_pts );
1027 p_sys->i_last_pts = frame->pts;
1030 frame->quality = p_sys->i_quality;
1033 #if (LIBAVCODEC_VERSION_MAJOR >= 54)
1037 av_init_packet( &av_pkt );
1038 av_pkt.data = p_block->p_buffer;
1039 av_pkt.size = p_block->i_buffer;
1041 if( avcodec_encode_video2( p_sys->p_context, &av_pkt, frame, &is_data ) < 0
1044 block_Release( p_block );
1048 p_block->i_buffer = av_pkt.size;
1049 p_block->i_length = av_pkt.duration / p_sys->p_context->time_base.den;
1050 p_block->i_pts = av_pkt.pts;
1051 p_block->i_dts = av_pkt.dts;
1052 if( unlikely( av_pkt.flags & AV_PKT_FLAG_CORRUPT ) )
1053 p_block->i_flags |= BLOCK_FLAG_CORRUPTED;
1056 int i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
1057 p_block->i_buffer, frame );
1060 block_Release( p_block );
1064 p_block->i_buffer = i_out;
1066 /* FIXME, 3-2 pulldown is not handled correctly */
1067 p_block->i_length = INT64_C(1000000) *
1068 p_enc->fmt_in.video.i_frame_rate_base /
1069 p_enc->fmt_in.video.i_frame_rate;
1071 if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
1073 /* No delay -> output pts == input pts */
1075 p_block->i_dts = p_pict->date;
1076 p_block->i_pts = p_block->i_dts;
1078 else if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
1079 p_sys->p_context->coded_frame->pts != 0 &&
1080 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
1082 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
1083 p_block->i_pts = p_sys->p_context->coded_frame->pts;
1085 if( p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_I &&
1086 p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_P )
1088 p_block->i_dts = p_block->i_pts;
1092 if( p_sys->i_last_ref_pts )
1094 p_block->i_dts = p_sys->i_last_ref_pts;
1098 /* Let's put something sensible */
1099 p_block->i_dts = p_block->i_pts;
1102 p_sys->i_last_ref_pts = p_block->i_pts;
1107 /* Buggy libavcodec which doesn't update coded_frame->pts
1109 p_block->i_dts = p_block->i_pts = p_pict->date;
1113 switch ( p_sys->p_context->coded_frame->pict_type )
1115 case AV_PICTURE_TYPE_I:
1116 case AV_PICTURE_TYPE_SI:
1117 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1119 case AV_PICTURE_TYPE_P:
1120 case AV_PICTURE_TYPE_SP:
1121 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1123 case AV_PICTURE_TYPE_B:
1124 case AV_PICTURE_TYPE_BI:
1125 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1128 p_block->i_flags |= BLOCK_FLAG_TYPE_PB;
1134 static block_t *encode_audio_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, AVFrame *frame )
1136 int got_packet, i_out;
1138 AVPacket packet = {0};
1139 block_t *p_block = block_Alloc( p_sys->i_buffer_out );
1140 av_init_packet( &packet );
1141 packet.data = p_block->p_buffer;
1142 packet.size = p_block->i_buffer;
1144 i_out = avcodec_encode_audio2( p_sys->p_context, &packet, frame, &got_packet );
1145 if( unlikely( !got_packet || ( i_out < 0 ) ) )
1149 msg_Err( p_enc,"Encoding problem..");
1151 block_Release( p_block );
1154 p_block->i_buffer = packet.size;
1156 p_block->i_length = (mtime_t)1000000 *
1157 (mtime_t)p_sys->i_frame_size /
1158 (mtime_t)p_sys->p_context->sample_rate;
1160 if( likely( packet.pts != AV_NOPTS_VALUE ) )
1161 p_block->i_dts = p_block->i_pts = packet.pts;
1163 p_block->i_dts = p_block->i_pts = VLC_TS_INVALID;
1167 static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, int buffer_delay, block_t *p_aout_buf, int leftover_samples )
1169 block_t *p_block = NULL;
1170 //How much we need to copy from new packet
1171 const int leftover = leftover_samples * p_sys->p_context->channels * p_sys->i_sample_bytes;
1173 avcodec_get_frame_defaults( p_sys->frame );
1174 p_sys->frame->format = p_sys->p_context->sample_fmt;
1175 p_sys->frame->nb_samples = leftover_samples + p_sys->i_samples_delay;
1178 p_sys->frame->pts = date_Get( &p_sys->buffer_date );
1179 if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1180 date_Increment( &p_sys->buffer_date, p_sys->frame->nb_samples );
1182 if( likely( p_aout_buf ) )
1185 p_aout_buf->i_nb_samples -= leftover_samples;
1186 memcpy( p_sys->p_buffer+buffer_delay, p_aout_buf->p_buffer, leftover );
1188 // We need to deinterleave from p_aout_buf to p_buffer the leftover bytes
1189 if( p_sys->b_planar )
1190 aout_Deinterleave( p_sys->p_interleave_buf, p_sys->p_buffer,
1191 p_sys->i_frame_size, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
1193 memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer, leftover);
1195 p_aout_buf->p_buffer += leftover;
1196 p_aout_buf->i_buffer -= leftover;
1197 if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1198 p_aout_buf->i_pts = date_Get( &p_sys->buffer_date );
1201 if(unlikely( ( (leftover + buffer_delay) < p_sys->i_buffer_out ) &&
1202 !(p_sys->p_codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME )))
1204 msg_Dbg( p_enc, "No small last frame support, padding");
1205 size_t padding_size = p_sys->i_buffer_out - (leftover+buffer_delay);
1206 memset( p_sys->p_buffer + (leftover+buffer_delay), 0, padding_size );
1207 buffer_delay += padding_size;
1209 if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
1210 p_sys->p_context->sample_fmt, p_sys->b_planar ? p_sys->p_interleave_buf : p_sys->p_buffer,
1211 p_sys->i_buffer_out,
1212 DEFAULT_ALIGN) < 0 )
1214 msg_Err( p_enc, "filling error on fillup" );
1215 p_sys->frame->nb_samples = 0;
1219 p_sys->i_samples_delay = 0;
1221 p_block = encode_audio_buffer( p_enc, p_sys, p_sys->frame );
1226 /****************************************************************************
1227 * EncodeAudio: the whole thing
1228 ****************************************************************************/
1229 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
1231 encoder_sys_t *p_sys = p_enc->p_sys;
1233 block_t *p_block, *p_chain = NULL;
1234 size_t buffer_delay = 0, i_samples_left = 0;
1237 //i_bytes_left is amount of bytes we get
1238 i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
1239 buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_sys->p_context->channels;
1241 //p_sys->i_buffer_out = p_sys->i_frame_size * chan * p_sys->i_sample_bytes
1242 //Calculate how many bytes we would need from current buffer to fill frame
1243 size_t leftover_samples = __MAX(0,__MIN((ssize_t)i_samples_left, (ssize_t)(p_sys->i_frame_size - p_sys->i_samples_delay)));
1245 if( ( p_aout_buf && ( p_aout_buf->i_pts > VLC_TS_INVALID ) &&
1246 ((p_aout_buf->i_pts - p_sys->i_samples_delay) != date_Get( &p_sys->buffer_date ) ) ) )
1248 date_Set( &p_sys->buffer_date, p_aout_buf->i_dts );
1249 /* take back amount we have leftover from previous buffer*/
1250 if( p_sys->i_samples_delay > 0 )
1251 date_Decrement( &p_sys->buffer_date, p_sys->i_samples_delay );
1254 // Check if we have enough samples in delay_buffer and current p_aout_buf to fill frame
1255 // Or if we are cleaning up
1256 if( ( buffer_delay > 0 ) &&
1257 ( ( p_aout_buf && ( leftover_samples <= p_aout_buf->i_nb_samples ) &&
1258 ( (leftover_samples + p_sys->i_samples_delay ) >= p_sys->i_frame_size )
1264 p_chain = handle_delay_buffer( p_enc, p_sys, buffer_delay, p_aout_buf, leftover_samples );
1266 if( unlikely( !p_chain ) )
1270 if( unlikely( !p_aout_buf ) )
1272 msg_Dbg(p_enc,"Flushing..");
1274 p_block = encode_audio_buffer( p_enc, p_sys, NULL );
1275 if( likely( p_block ) )
1277 block_ChainAppend( &p_chain, p_block );
1284 while( ( p_aout_buf->i_nb_samples >= p_sys->i_frame_size ) ||
1285 ( p_sys->b_variable && p_aout_buf->i_nb_samples ) )
1287 avcodec_get_frame_defaults( p_sys->frame );
1288 if( p_sys->b_variable )
1289 p_sys->frame->nb_samples = p_aout_buf->i_nb_samples;
1291 p_sys->frame->nb_samples = p_sys->i_frame_size;
1292 p_sys->frame->format = p_sys->p_context->sample_fmt;
1293 p_sys->frame->pts = date_Get( &p_sys->buffer_date );
1295 const int in_bytes = p_sys->frame->nb_samples *
1296 p_sys->p_context->channels * p_sys->i_sample_bytes;
1298 if( p_sys->b_planar )
1300 aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer,
1301 p_sys->frame->nb_samples, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
1306 memcpy(p_sys->p_buffer, p_aout_buf->p_buffer, in_bytes);
1309 if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
1310 p_sys->p_context->sample_fmt,
1312 p_sys->i_buffer_out,
1313 DEFAULT_ALIGN) < 0 )
1315 msg_Err( p_enc, "filling error on encode" );
1316 p_sys->frame->nb_samples = 0;
1319 p_aout_buf->p_buffer += in_bytes;
1320 p_aout_buf->i_buffer -= in_bytes;
1321 p_aout_buf->i_nb_samples -= p_sys->frame->nb_samples;
1322 if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1323 date_Increment( &p_sys->buffer_date, p_sys->frame->nb_samples );
1325 p_block = encode_audio_buffer( p_enc, p_sys, p_sys->frame );
1326 if( likely( p_block ) )
1327 block_ChainAppend( &p_chain, p_block );
1330 // We have leftover samples that don't fill frame_size, and libavcodec doesn't seem to like
1331 // that frame has more data than p_sys->i_frame_size most of the cases currently.
1332 if( p_aout_buf->i_nb_samples > 0 )
1334 memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer,
1335 p_aout_buf->i_nb_samples * p_sys->i_sample_bytes * p_sys->p_context->channels);
1336 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
1342 /*****************************************************************************
1343 * CloseEncoder: libavcodec encoder destruction
1344 *****************************************************************************/
1345 void CloseEncoder( vlc_object_t *p_this )
1347 encoder_t *p_enc = (encoder_t *)p_this;
1348 encoder_sys_t *p_sys = p_enc->p_sys;
1350 /*FIXME: we should use avcodec_free_frame, but we don't require so new avcodec that has it*/
1351 av_freep( &p_sys->frame );
1354 avcodec_close( p_sys->p_context );
1355 vlc_avcodec_unlock();
1356 av_free( p_sys->p_context );
1359 free( p_sys->p_interleave_buf );
1360 free( p_sys->p_buffer );