]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/encoder.c
Add support for font height as a percentage of the video height
[vlc] / modules / codec / avcodec / encoder.c
1 /*****************************************************************************
2  * encoder.c: video and audio encoder using the libavcodec library
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
5  * $Id$
6  *
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)
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_aout.h>
37 #include <vlc_sout.h>
38 #include <vlc_codec.h>
39 #include <vlc_dialog.h>
40 #include <vlc_avcodec.h>
41 #include <vlc_cpu.h>
42
43 #define HAVE_MMX 1
44 #include <libavcodec/avcodec.h>
45
46 #include "avcodec.h"
47 #include "avcommon.h"
48
49 #define HURRY_UP_GUARD1 (450000)
50 #define HURRY_UP_GUARD2 (300000)
51 #define HURRY_UP_GUARD3 (100000)
52
53 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
54
55 #define RAW_AUDIO_FRAME_SIZE (2048)
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 int  OpenEncoder ( vlc_object_t * );
61 void CloseEncoder( vlc_object_t * );
62
63 static block_t *EncodeVideo( encoder_t *, picture_t * );
64 static block_t *EncodeAudio( encoder_t *, block_t * );
65
66 struct thread_context_t;
67
68 /*****************************************************************************
69  * thread_context_t : for multithreaded encoding
70  *****************************************************************************/
71 struct thread_context_t
72 {
73     VLC_COMMON_MEMBERS
74
75     AVCodecContext  *p_context;
76     int             (* pf_func)(AVCodecContext *c, void *arg);
77     void            *arg;
78     int             i_ret;
79
80     vlc_mutex_t     lock;
81     vlc_cond_t      cond;
82     bool            b_work, b_done;
83 };
84
85 /*****************************************************************************
86  * encoder_sys_t : libavcodec encoder descriptor
87  *****************************************************************************/
88 struct encoder_sys_t
89 {
90     /*
91      * libavcodec properties
92      */
93     AVCodec         *p_codec;
94     AVCodecContext  *p_context;
95
96     /*
97      * Common buffer mainly for audio as frame size in there needs usually be constant
98      */
99     char *p_buffer;
100     size_t i_buffer_out;
101
102     /*
103      * Video properties
104      */
105     mtime_t i_last_ref_pts;
106     mtime_t i_buggy_pts_detect;
107     mtime_t i_last_pts;
108     bool    b_inited;
109
110     /*
111      * Audio properties
112      */
113     int i_sample_bytes;
114     int i_frame_size;
115     int i_samples_delay;
116     mtime_t i_pts;
117
118     /* Encoding settings */
119     int        i_key_int;
120     int        i_b_frames;
121     int        i_vtolerance;
122     int        i_qmin;
123     int        i_qmax;
124     int        i_hq;
125     int        i_rc_buffer_size;
126     float      f_rc_buffer_aggressivity;
127     bool       b_pre_me;
128     bool       b_hurry_up;
129     bool       b_interlace, b_interlace_me;
130     float      f_i_quant_factor;
131     int        i_noise_reduction;
132     bool       b_mpeg4_matrix;
133     bool       b_trellis;
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.*/
138 };
139
140 static const char *const ppsz_enc_options[] = {
141     "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
142     "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
143     "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
144     "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
145     "p-masking", "border-masking", "luma-elim-threshold",
146     "chroma-elim-threshold",
147      "aac-profile",
148      NULL
149 };
150
151 static const uint16_t mpa_bitrate_tab[2][15] =
152 {
153     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
154     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
155 };
156
157 static const uint16_t mpa_freq_tab[6] =
158 { 44100, 48000, 32000, 22050, 24000, 16000 };
159
160 static const uint16_t mpeg4_default_intra_matrix[64] = {
161   8, 17, 18, 19, 21, 23, 25, 27,
162  17, 18, 19, 21, 23, 25, 27, 28,
163  20, 21, 22, 23, 24, 26, 28, 30,
164  21, 22, 23, 24, 26, 28, 30, 32,
165  22, 23, 24, 26, 28, 30, 32, 35,
166  23, 24, 26, 28, 30, 32, 35, 38,
167  25, 26, 28, 30, 32, 35, 38, 41,
168  27, 28, 30, 32, 35, 38, 41, 45,
169 };
170
171 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
172  16, 17, 18, 19, 20, 21, 22, 23,
173  17, 18, 19, 20, 21, 22, 23, 24,
174  18, 19, 20, 21, 22, 23, 24, 25,
175  19, 20, 21, 22, 23, 24, 26, 27,
176  20, 21, 22, 23, 25, 26, 27, 28,
177  21, 22, 23, 24, 26, 27, 28, 30,
178  22, 23, 24, 26, 27, 28, 30, 31,
179  23, 24, 25, 27, 28, 30, 31, 33,
180 };
181
182 /*****************************************************************************
183  * OpenEncoder: probe the encoder
184  *****************************************************************************/
185
186 int OpenEncoder( vlc_object_t *p_this )
187 {
188     encoder_t *p_enc = (encoder_t *)p_this;
189     encoder_sys_t *p_sys;
190     AVCodecContext *p_context;
191     AVCodec *p_codec = NULL;
192     int i_codec_id, i_cat;
193     const char *psz_namecodec;
194     float f_val;
195     char *psz_val;
196
197     /* Initialization must be done before avcodec_find_encoder() */
198     vlc_init_avcodec();
199
200     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
201
202     if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
203     {
204         i_cat = AUDIO_ES;
205         i_codec_id = CODEC_ID_MP3;
206         psz_namecodec = "MPEG I/II Layer 3";
207     }
208     else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP2 )
209     {
210         i_cat = AUDIO_ES;
211         i_codec_id = CODEC_ID_MP2;
212         psz_namecodec = "MPEG I/II Layer 2";
213     }
214     else if( p_enc->fmt_out.i_codec == VLC_CODEC_MP1V )
215     {
216         i_cat = VIDEO_ES;
217         i_codec_id = CODEC_ID_MPEG1VIDEO;
218         psz_namecodec = "MPEG-1 video";
219     }
220     else if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
221                              &psz_namecodec ) )
222     {
223         if( TestFfmpegChroma( -1, p_enc->fmt_out.i_codec ) != VLC_SUCCESS )
224         {
225             /* handed chroma output */
226             return VLC_EGENERIC;
227         }
228         i_cat      = VIDEO_ES;
229         i_codec_id = CODEC_ID_RAWVIDEO;
230         psz_namecodec = "Raw video";
231     }
232
233     if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
234     {
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 );
238         return VLC_EGENERIC;
239     }
240
241     if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
242     {
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 );
246         return VLC_EGENERIC;
247     }
248
249     if( p_enc->fmt_out.i_cat == SPU_ES )
250     {
251         /* We don't support subtitle encoding */
252         return VLC_EGENERIC;
253     }
254
255     char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
256     if( psz_encoder && *psz_encoder )
257     {
258         p_codec = avcodec_find_encoder_by_name( psz_encoder );
259         if( !p_codec )
260             msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
261         else if( p_codec->id != i_codec_id )
262         {
263             msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
264                     psz_encoder, (char*)&p_enc->fmt_out.i_codec );
265             p_codec = NULL;
266         }
267     }
268     free( psz_encoder );
269     if( !p_codec )
270         p_codec = avcodec_find_encoder( i_codec_id );
271     if( !p_codec )
272     {
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 );
277
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"
284 "%s.\n"
285 "If you don't know how to fix this, ask for support from your distribution.\n"
286 "\n"
287 "This is not an error inside VLC media player.\n"
288 "Do not contact the VideoLAN project about this issue.\n"),
289             psz_namecodec );
290         return VLC_EGENERIC;
291     }
292
293     /* Allocate the memory needed to store the encoder's structure */
294     if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
295         return VLC_ENOMEM;
296     p_enc->p_sys = p_sys;
297     p_sys->i_samples_delay = 0;
298     p_sys->p_codec = p_codec;
299
300     p_sys->p_buffer = NULL;
301     p_sys->i_buffer_out = 0;
302
303 #if LIBAVCODEC_VERSION_MAJOR < 54
304     p_context = avcodec_alloc_context();
305 #else
306     p_context = avcodec_alloc_context3(p_codec);
307 #endif
308     p_sys->p_context = p_context;
309     p_sys->p_context->codec_id = p_sys->p_codec->id;
310     p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
311     p_context->opaque = (void *)p_this;
312
313     /* set CPU capabilities */
314 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 51, 25, 0 )
315     av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
316 #else
317     p_context->dsp_mask = GetVlcDspMask();
318 #endif
319
320     p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
321     p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
322     p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
323     p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
324     p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
325     p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
326     p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
327
328     if( p_sys->b_hurry_up )
329     {
330         /* hurry up mode needs noise reduction, even small */
331         p_sys->i_noise_reduction = 1;
332     }
333
334     p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
335     p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
336     p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
337     p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
338     p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
339
340     f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
341     if( f_val < 0.01 || f_val > 255.0 ) f_val = 0;
342     p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
343
344     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
345     p_sys->i_hq = FF_MB_DECISION_RD;
346     if( psz_val && *psz_val )
347     {
348         if( !strcmp( psz_val, "rd" ) )
349             p_sys->i_hq = FF_MB_DECISION_RD;
350         else if( !strcmp( psz_val, "bits" ) )
351             p_sys->i_hq = FF_MB_DECISION_BITS;
352         else if( !strcmp( psz_val, "simple" ) )
353             p_sys->i_hq = FF_MB_DECISION_SIMPLE;
354         else
355             p_sys->i_hq = FF_MB_DECISION_RD;
356     }
357     else
358         p_sys->i_hq = FF_MB_DECISION_RD;
359     free( psz_val );
360
361     p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
362     p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
363     p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
364
365     p_context->strict_std_compliance = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
366
367     p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
368     p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
369     p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
370     p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
371     p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
372     p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
373
374     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
375     /* libavcodec uses faac encoder atm, and it has issues with
376      * other than low-complexity profile, so default to that */
377     p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
378     if( psz_val && *psz_val )
379     {
380         if( !strncmp( psz_val, "main", 4 ) )
381             p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
382         else if( !strncmp( psz_val, "low", 3 ) )
383             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
384 #if 0    /* Not supported by FAAC encoder */
385         else if( !strncmp( psz_val, "ssr", 3 ) )
386             p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
387 #endif
388         else if( !strncmp( psz_val, "ltp", 3 ) )
389             p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
390 #if LIBAVCODEC_VERSION_CHECK( 54, 19, 0, 35, 100 )
391 /* These require libavcodec with libfdk-aac */
392         else if( !strncmp( psz_val, "hev2", 4 ) )
393             p_sys->i_aac_profile = FF_PROFILE_AAC_HE_V2;
394         else if( !strncmp( psz_val, "hev1", 4 ) )
395             p_sys->i_aac_profile = FF_PROFILE_AAC_HE;
396 #endif
397         else
398         {
399             msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
400             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
401         }
402     }
403     free( psz_val );
404
405     if( p_enc->fmt_in.i_cat == VIDEO_ES )
406     {
407         if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
408         {
409             msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
410                       p_enc->fmt_in.video.i_height );
411             free( p_sys );
412             return VLC_EGENERIC;
413         }
414
415         p_context->codec_type = AVMEDIA_TYPE_VIDEO;
416
417         p_context->width = p_enc->fmt_in.video.i_width;
418         p_context->height = p_enc->fmt_in.video.i_height;
419
420         p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
421         p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
422         if( p_codec->supported_framerates )
423         {
424             AVRational target = {
425                 .num = p_enc->fmt_in.video.i_frame_rate,
426                 .den = p_enc->fmt_in.video.i_frame_rate_base,
427             };
428             int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
429
430             p_context->time_base.num = p_codec->supported_framerates[idx].den;
431             p_context->time_base.den = p_codec->supported_framerates[idx].num;
432         }
433
434         /* Defaults from ffmpeg.c */
435         p_context->qblur = 0.5;
436         p_context->qcompress = 0.5;
437         p_context->b_quant_offset = 1.25;
438         p_context->b_quant_factor = 1.25;
439         p_context->i_quant_offset = 0.0;
440         p_context->i_quant_factor = -0.8;
441
442         p_context->lumi_masking = p_sys->f_lumi_masking;
443         p_context->dark_masking = p_sys->f_dark_masking;
444         p_context->p_masking = p_sys->f_p_masking;
445         p_context->border_masking = p_sys->f_border_masking;
446         p_context->luma_elim_threshold = p_sys->i_luma_elim;
447         p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
448
449         if( p_sys->i_key_int > 0 )
450             p_context->gop_size = p_sys->i_key_int;
451         p_context->max_b_frames =
452             VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
453         p_context->b_frame_strategy = 0;
454         if( !p_context->max_b_frames  &&
455             (  p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
456                p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ) )
457             p_context->flags |= CODEC_FLAG_LOW_DELAY;
458
459         av_reduce( &p_context->sample_aspect_ratio.num,
460                    &p_context->sample_aspect_ratio.den,
461                    p_enc->fmt_in.video.i_sar_num,
462                    p_enc->fmt_in.video.i_sar_den, 1 << 30 );
463
464
465         p_enc->fmt_in.i_codec = VLC_CODEC_I420;
466         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
467         GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
468
469         if( p_codec->pix_fmts )
470         {
471             const enum PixelFormat *p = p_codec->pix_fmts;
472             for( ; *p != -1; p++ )
473             {
474                 if( *p == p_context->pix_fmt ) break;
475             }
476             if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
477             GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
478             p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
479         }
480
481
482         if ( p_sys->f_i_quant_factor != 0.0 )
483             p_context->i_quant_factor = p_sys->f_i_quant_factor;
484
485         p_context->noise_reduction = p_sys->i_noise_reduction;
486
487         if ( p_sys->b_mpeg4_matrix )
488         {
489             p_context->intra_matrix = mpeg4_default_intra_matrix;
490             p_context->inter_matrix = mpeg4_default_non_intra_matrix;
491         }
492
493         if ( p_sys->b_pre_me )
494         {
495             p_context->pre_me = 1;
496             p_context->me_pre_cmp = FF_CMP_CHROMA;
497         }
498
499         if ( p_sys->b_interlace )
500         {
501             if ( p_context->height <= 280 )
502             {
503                 if ( p_context->height != 16 || p_context->width != 16 )
504                     msg_Warn( p_enc,
505                         "disabling interlaced video because height=%d <= 280",
506                         p_context->height );
507             }
508             else
509             {
510                 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
511                 if ( p_sys->b_interlace_me )
512                     p_context->flags |= CODEC_FLAG_INTERLACED_ME;
513             }
514         }
515
516         p_context->trellis = p_sys->b_trellis;
517
518         if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
519             p_context->flags |= CODEC_FLAG_QSCALE;
520         /* These codecs cause libavcodec to exit if thread_count is > 1.
521            See libavcodec/mpegvideo_enc.c:MPV_encode_init and
522            libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
523          */
524         if ( i_codec_id == CODEC_ID_FLV1 ||
525              i_codec_id == CODEC_ID_H261 ||
526              i_codec_id == CODEC_ID_LJPEG ||
527              i_codec_id == CODEC_ID_MJPEG ||
528              i_codec_id == CODEC_ID_H263 ||
529              i_codec_id == CODEC_ID_H263P ||
530              i_codec_id == CODEC_ID_MSMPEG4V1 ||
531              i_codec_id == CODEC_ID_MSMPEG4V2 ||
532              i_codec_id == CODEC_ID_MSMPEG4V3 ||
533              i_codec_id == CODEC_ID_WMV1 ||
534              i_codec_id == CODEC_ID_WMV2 ||
535              i_codec_id == CODEC_ID_RV10 ||
536              i_codec_id == CODEC_ID_RV20 ||
537              i_codec_id == CODEC_ID_SVQ3 )
538             p_enc->i_threads = 1;
539
540         if( p_sys->i_vtolerance > 0 )
541             p_context->bit_rate_tolerance = p_sys->i_vtolerance;
542
543         /* usually if someone sets bitrate, he likes more to get that bitrate
544          * over quality should help 'normal' user to get asked bitrate
545          */
546         if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
547         {
548             p_sys->i_qmax = 51;
549             p_sys->i_qmin = 3;
550         }
551
552         if( p_sys->i_qmin > 0 )
553         {
554             p_context->qmin = p_sys->i_qmin;
555             p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
556         }
557         if( p_sys->i_qmax > 0 )
558         {
559             p_context->qmax = p_sys->i_qmax;
560             p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
561         }
562         p_context->max_qdiff = 3;
563
564         p_context->mb_decision = p_sys->i_hq;
565
566         if( p_sys->i_quality )
567         {
568             p_context->flags |= CODEC_FLAG_QSCALE;
569             p_context->global_quality = p_sys->i_quality;
570         }
571         else
572         {
573             p_context->rc_qsquish = 1.0;
574             if( p_sys->i_rc_buffer_size )
575             {
576                 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
577                 p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
578             }
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;
584         }
585     }
586     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
587     {
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;
591
592         p_context->codec_type  = AVMEDIA_TYPE_AUDIO;
593         p_context->sample_fmt  = p_codec->sample_fmts ?
594                                     p_codec->sample_fmts[0] :
595                                     AV_SAMPLE_FMT_S16;
596
597         // Try if we can use interleaved format for codec input as VLC doesn't really do planar audio yet
598         // FIXME: Remove when planar/interleaved audio in vlc is equally supported
599         if( av_sample_fmt_is_planar( p_context->sample_fmt ) )
600         {
601             msg_Dbg( p_enc, "Trying to find packet sample format instead of %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
602             for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
603             {
604                 if( !av_sample_fmt_is_planar( p_codec->sample_fmts[i] ) )
605                 {
606                     p_context->sample_fmt = p_codec->sample_fmts[i];
607                     msg_Dbg( p_enc, "Using %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
608                     break;
609                 }
610             }
611         }
612         p_enc->fmt_in.i_codec  = GetVlcAudioFormat( p_context->sample_fmt );
613
614         p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
615         p_context->time_base.num = 1;
616         p_context->time_base.den = p_context->sample_rate;
617         p_context->channels    = p_enc->fmt_out.audio.i_channels;
618
619         if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
620         {
621             /* XXX: FAAC does resample only when setting the INPUT samplerate
622              * to the desired value (-R option of the faac frontend)
623             p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
624             /* vlc should default to low-complexity profile, faac encoder
625              * has bug and aac audio has issues otherwise atm */
626             p_context->profile = p_sys->i_aac_profile;
627         }
628     }
629
630     /* Misc parameters */
631     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
632
633     /* Set reasonable defaults to VP8, based on
634        libvpx-720p preset from libvpx ffmpeg-patch */
635     if( i_codec_id == CODEC_ID_VP8 )
636     {
637         /* Lets give bitrate tolerance */
638         p_context->bit_rate_tolerance = __MAX(2 * (int)p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
639         /* default to 120 frames between keyframe */
640         if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
641             p_context->gop_size = 120;
642         /* Don't set rc-values atm, they were from time before
643            libvpx was officially in FFmpeg */
644         //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
645         //p_context->rc_min_rate = 40 * 1000; // 40k
646         /* seems that FFmpeg presets have 720p as divider for buffers */
647         if( p_enc->fmt_out.video.i_height >= 720 )
648         {
649             /* Check that we don't overrun users qmin/qmax values */
650             if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
651             {
652                 p_context->qmin = 10;
653                 p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
654             }
655
656             if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
657             {
658                 p_context->qmax = 42;
659                 p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
660             }
661
662             } else {
663             if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
664             {
665                 p_context->qmin = 1;
666                 p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
667             }
668         }
669
670
671 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
672         p_context->lag = 16;
673         p_context->level = 216;
674         p_context->profile = 0;
675         p_context->rc_buffer_aggressivity = 0.95;
676         p_context->token_partitions = 4;
677         p_context->mb_static_threshold = 0;
678 #endif
679     }
680
681     if( i_codec_id == CODEC_ID_RAWVIDEO )
682     {
683         /* XXX: hack: Force same codec (will be handled by transcode) */
684         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
685         GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
686     }
687
688     /* Make sure we get extradata filled by the encoder */
689     p_context->extradata_size = 0;
690     p_context->extradata = NULL;
691     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
692
693     if( p_enc->i_threads >= 1)
694         p_context->thread_count = p_enc->i_threads;
695     else
696         p_context->thread_count = vlc_GetCPUCount();
697
698     int ret;
699     vlc_avcodec_lock();
700 #if LIBAVCODEC_VERSION_MAJOR < 54
701     ret = avcodec_open( p_context, p_codec );
702 #else
703     ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
704 #endif
705     vlc_avcodec_unlock();
706     if( ret )
707     {
708         if( p_enc->fmt_in.i_cat == AUDIO_ES &&
709              (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
710                || i_codec_id == CODEC_ID_MP3) )
711         {
712             if( p_context->channels > 2 )
713             {
714                 p_context->channels = 2;
715                 p_enc->fmt_in.audio.i_channels = 2; // FIXME
716                 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
717             }
718
719             if( i_codec_id == CODEC_ID_MP2 || i_codec_id == CODEC_ID_MP3 )
720             {
721                 int i_frequency, i;
722
723                 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
724                 {
725                     if ( p_enc->fmt_out.audio.i_rate
726                             == mpa_freq_tab[i_frequency] )
727                         break;
728                 }
729                 if ( i_frequency == 6 )
730                 {
731                     msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
732                              p_enc->fmt_out.audio.i_rate );
733                     free( p_sys );
734                     return VLC_EGENERIC;
735                 }
736
737                 for ( i = 1; i < 14; i++ )
738                 {
739                     if ( p_enc->fmt_out.i_bitrate / 1000
740                           <= mpa_bitrate_tab[i_frequency / 3][i] )
741                         break;
742                 }
743                 if ( p_enc->fmt_out.i_bitrate / 1000
744                       != mpa_bitrate_tab[i_frequency / 3][i] )
745                 {
746                     msg_Warn( p_enc,
747                               "MPEG audio doesn't support bitrate=%d, using %d",
748                               p_enc->fmt_out.i_bitrate,
749                               mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
750                     p_enc->fmt_out.i_bitrate =
751                         mpa_bitrate_tab[i_frequency / 3][i] * 1000;
752                     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
753                 }
754             }
755
756             p_context->codec = NULL;
757             vlc_avcodec_lock();
758 #if LIBAVCODEC_VERSION_MAJOR < 54
759             ret = avcodec_open( p_context, p_codec );
760 #else
761             ret = avcodec_open2( p_context, p_codec, NULL /* options */ );
762 #endif
763             vlc_avcodec_unlock();
764             if( ret )
765             {
766                 msg_Err( p_enc, "cannot open encoder" );
767                 dialog_Fatal( p_enc,
768                                 _("Streaming / Transcoding failed"),
769                                 "%s", _("VLC could not open the encoder.") );
770                 free( p_sys );
771                 return VLC_EGENERIC;
772             }
773         }
774         else
775         {
776             msg_Err( p_enc, "cannot open encoder" );
777             dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
778                             "%s", _("VLC could not open the encoder.") );
779             free( p_sys );
780             return VLC_EGENERIC;
781         }
782     }
783
784     if( i_codec_id == CODEC_ID_FLAC )
785     {
786         p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
787         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
788         if( p_enc->fmt_out.p_extra )
789         {
790             uint8_t *p = p_enc->fmt_out.p_extra;
791             p[0] = 0x66;    /* f */
792             p[1] = 0x4C;    /* L */
793             p[2] = 0x61;    /* a */
794             p[3] = 0x43;    /* C */
795             p[4] = 0x80;    /* streaminfo block, last block before audio */
796             p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
797             p[6] = ( p_context->extradata_size >>  8 ) & 0xff;
798             p[7] = ( p_context->extradata_size       ) & 0xff;
799             memcpy( &p[8], p_context->extradata, p_context->extradata_size );
800         }
801         else
802         {
803             p_enc->fmt_out.i_extra = 0;
804         }
805     }
806     else
807     {
808         p_enc->fmt_out.i_extra = p_context->extradata_size;
809         if( p_enc->fmt_out.i_extra )
810         {
811             p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
812             if ( p_enc->fmt_out.p_extra == NULL )
813             {
814                 goto error;
815             }
816             memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
817                     p_enc->fmt_out.i_extra );
818         }
819     }
820
821     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
822
823     if( p_enc->fmt_in.i_cat == AUDIO_ES )
824     {
825         p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
826         p_enc->fmt_in.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_in.i_codec );
827
828         p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8) *
829                                 p_context->channels;
830         p_sys->i_frame_size = p_context->frame_size > 1 ?
831                                     p_context->frame_size :
832                                     RAW_AUDIO_FRAME_SIZE;
833         p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes );
834         if ( p_sys->p_buffer == NULL )
835         {
836             goto error;
837         }
838         p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
839         p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_out.i_codec );
840
841         if( p_context->frame_size > 1 )
842             p_sys->i_buffer_out = 8 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
843         else
844             p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes;
845     }
846
847     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
848     p_enc->pf_encode_video = EncodeVideo;
849     p_enc->pf_encode_audio = EncodeAudio;
850
851
852     return VLC_SUCCESS;
853 error:
854     free( p_enc->fmt_out.p_extra );
855     free( p_sys->p_buffer );
856     free( p_sys );
857     return VLC_ENOMEM;
858 }
859
860 /****************************************************************************
861  * EncodeVideo: the whole thing
862  ****************************************************************************/
863 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
864 {
865     encoder_sys_t *p_sys = p_enc->p_sys;
866     int i_out, i_plane;
867
868     /* Initialize the video output buffer the first time.
869      * This is done here instead of OpenEncoder() because we need the actual
870      * bits_per_pixel value, without having to assume anything.
871      */
872     const int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
873                          p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
874     const int blocksize = __MAX( FF_MIN_BUFFER_SIZE,bytesPerPixel * p_sys->p_context->height * p_sys->p_context->width + 200 );
875     block_t *p_block = block_Alloc( blocksize );
876
877     if( likely(p_pict) ) {
878         AVFrame *frame;
879         frame = avcodec_alloc_frame();
880         for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
881         {
882             frame->data[i_plane] = p_pict->p[i_plane].p_pixels;
883             frame->linesize[i_plane] = p_pict->p[i_plane].i_pitch;
884         }
885
886         /* Let libavcodec select the frame type */
887         frame->pict_type = 0;
888
889         frame->repeat_pict = p_pict->i_nb_fields - 2;
890         frame->interlaced_frame = !p_pict->b_progressive;
891         frame->top_field_first = !!p_pict->b_top_field_first;
892
893         /* Set the pts of the frame being encoded */
894         frame->pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE;
895
896         if ( p_sys->b_hurry_up && frame->pts != (int64_t)AV_NOPTS_VALUE )
897         {
898             mtime_t current_date = mdate();
899
900             if ( current_date + HURRY_UP_GUARD3 > frame->pts )
901             {
902                 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
903                 p_sys->p_context->trellis = 0;
904                 msg_Dbg( p_enc, "hurry up mode 3" );
905             }
906             else
907             {
908                 p_sys->p_context->mb_decision = p_sys->i_hq;
909
910                 if ( current_date + HURRY_UP_GUARD2 > frame->pts )
911                 {
912                     p_sys->p_context->trellis = 0;
913                     p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
914                         + (HURRY_UP_GUARD2 + current_date - frame->pts) / 500;
915                     msg_Dbg( p_enc, "hurry up mode 2" );
916                 }
917                 else
918                 {
919                     p_sys->p_context->trellis = p_sys->b_trellis;
920
921                     p_sys->p_context->noise_reduction =
922                        p_sys->i_noise_reduction;
923                 }
924             }
925
926             if ( current_date + HURRY_UP_GUARD1 > frame->pts )
927             {
928                 frame->pict_type = AV_PICTURE_TYPE_P;
929                 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
930             }
931         }
932
933         if ( frame->pts != (int64_t)AV_NOPTS_VALUE && frame->pts != 0 )
934         {
935             if ( p_sys->i_last_pts == frame->pts )
936             {
937                 msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
938                          "same PTS (%"PRId64 ")", frame->pts );
939                 av_freep( &frame );
940                 return NULL;
941             }
942             else if ( p_sys->i_last_pts > frame->pts )
943             {
944                 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
945                          "past (current: %"PRId64 ", last: %"PRId64")",
946                          frame->pts, p_sys->i_last_pts );
947                 av_freep( &frame );
948                 return NULL;
949             }
950             else
951             {
952                 p_sys->i_last_pts = frame->pts;
953             }
954         }
955
956         frame->quality = p_sys->i_quality;
957
958         i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
959                                      p_block->i_buffer, frame );
960         av_freep( &frame );
961     }
962     else
963     {
964         i_out = avcodec_encode_video( p_sys->p_context, p_block->p_buffer,
965                                      p_block->i_buffer, NULL);
966     }
967
968     if( i_out <= 0 )
969     {
970         block_Release( p_block );
971         return NULL;
972     }
973
974     p_block->i_buffer = i_out;
975
976     /* FIXME, 3-2 pulldown is not handled correctly */
977     p_block->i_length = INT64_C(1000000) *
978         p_enc->fmt_in.video.i_frame_rate_base /
979             p_enc->fmt_in.video.i_frame_rate;
980
981     if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
982     {
983         /* No delay -> output pts == input pts */
984         if( p_pict )
985             p_block->i_dts = p_pict->date;
986         p_block->i_pts = p_block->i_dts;
987     }
988     else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE &&
989         p_sys->p_context->coded_frame->pts != 0 &&
990         p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
991     {
992         p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
993         p_block->i_pts = p_sys->p_context->coded_frame->pts;
994
995         if( p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_I &&
996             p_sys->p_context->coded_frame->pict_type != AV_PICTURE_TYPE_P )
997         {
998             p_block->i_dts = p_block->i_pts;
999         }
1000         else
1001         {
1002             if( p_sys->i_last_ref_pts )
1003             {
1004                 p_block->i_dts = p_sys->i_last_ref_pts;
1005             }
1006             else
1007             {
1008                 /* Let's put something sensible */
1009                 p_block->i_dts = p_block->i_pts;
1010             }
1011
1012             p_sys->i_last_ref_pts = p_block->i_pts;
1013         }
1014     }
1015     else if( p_pict )
1016     {
1017         /* Buggy libavcodec which doesn't update coded_frame->pts
1018          * correctly */
1019         p_block->i_dts = p_block->i_pts = p_pict->date;
1020     }
1021
1022     switch ( p_sys->p_context->coded_frame->pict_type )
1023     {
1024     case AV_PICTURE_TYPE_I:
1025         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1026         break;
1027     case AV_PICTURE_TYPE_P:
1028         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1029         break;
1030     case AV_PICTURE_TYPE_B:
1031         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1032         break;
1033
1034     }
1035
1036     return p_block;
1037 }
1038
1039 /****************************************************************************
1040  * EncodeAudio: the whole thing
1041  ****************************************************************************/
1042 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
1043 {
1044     encoder_sys_t *p_sys = p_enc->p_sys;
1045
1046     block_t *p_block, *p_chain = NULL;
1047     AVFrame *frame=NULL;
1048     int got_packet,i_out,i_samples_left=0,i_data_offset = 0;
1049
1050     //i_samples_left is amount of samples we get
1051     i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
1052
1053     if( p_sys->i_samples_delay > 0 )
1054     {
1055         AVPacket packet;
1056         //How much we need to copy from new packet
1057         const int leftover = __MAX(0,__MIN(i_samples_left, (p_sys->i_frame_size - p_sys->i_samples_delay)));
1058
1059         frame = avcodec_alloc_frame();
1060         frame->nb_samples = p_sys->i_samples_delay + leftover;
1061         frame->format     = p_sys->p_context->sample_fmt;
1062
1063         //Copy samples from new packet to buffer to get frame size
1064         if( likely( leftover ) )
1065         {
1066             if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1067                 aout_Deinterleave( p_sys->p_buffer+(p_sys->i_samples_delay*p_sys->i_sample_bytes*p_enc->fmt_in.audio.i_channels ),
1068                                   p_aout_buf->p_buffer, leftover, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec );
1069             else
1070                 memcpy( p_sys->p_buffer+(p_sys->i_samples_delay*p_sys->i_sample_bytes),
1071                         p_aout_buf->p_buffer,
1072                         leftover * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels
1073                        );
1074         }
1075
1076         if( avcodec_fill_audio_frame( frame, p_enc->fmt_in.audio.i_channels,
1077                               p_sys->p_context->sample_fmt,
1078                               p_sys->p_buffer,
1079                               (p_sys->i_samples_delay + leftover) * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1080                               0) < 0 )
1081             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 );
1082
1083         if( likely( p_aout_buf ) )
1084             frame->pts = p_aout_buf->i_pts -
1085                      (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1086                      (mtime_t)p_enc->fmt_in.audio.i_rate;
1087
1088         p_sys->i_samples_delay = 0;
1089         i_data_offset += leftover * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
1090         i_samples_left -= leftover;
1091
1092         p_block = block_Alloc( p_sys->i_buffer_out );
1093         av_init_packet( &packet );
1094         packet.data = p_block->p_buffer;
1095         packet.size = p_block->i_buffer;
1096
1097         i_out = avcodec_encode_audio2( p_sys->p_context, &packet, frame, &got_packet );
1098         p_block->i_buffer = packet.size;
1099
1100
1101         /*FIXME: same as avcodec_free_frame, but we don't require so new avcodec that has it*/
1102         if( frame->extended_data != frame->data )
1103            av_freep( frame->extended_data );
1104         av_freep( &frame );
1105         if( unlikely( !got_packet || ( i_out < 0 ) ) )
1106         {
1107             if( i_out < 0 )
1108             {
1109                 msg_Err( p_enc,"Encoding problem...");
1110                 return p_chain;
1111             }
1112         } else {
1113             p_block->i_buffer = packet.size;
1114
1115             p_block->i_length = (mtime_t)1000000 *
1116              (mtime_t)p_sys->i_frame_size /
1117              (mtime_t)p_sys->p_context->sample_rate;
1118
1119             p_block->i_dts = p_block->i_pts = packet.pts;
1120
1121             block_ChainAppend( &p_chain, p_block );
1122         }
1123     }
1124
1125     if( unlikely( !p_aout_buf ) )
1126     {
1127         msg_Dbg(p_enc,"Flushing..");
1128         do {
1129             AVPacket packet;
1130             p_block = block_Alloc( p_sys->i_buffer_out );
1131             av_init_packet( &packet );
1132             packet.data = p_block->p_buffer;
1133             packet.size = p_block->i_buffer;
1134
1135             i_out = avcodec_encode_audio2( p_sys->p_context, &packet, NULL, &got_packet );
1136             p_block->i_buffer = packet.size;
1137
1138             p_block->i_length = (mtime_t)1000000 *
1139              (mtime_t)p_sys->i_frame_size /
1140              (mtime_t)p_sys->p_context->sample_rate;
1141
1142             p_block->i_dts = p_block->i_pts = packet.pts;
1143
1144             if( !i_out && got_packet )
1145                 block_ChainAppend( &p_chain, p_block );
1146         } while( got_packet && !i_out );
1147         return p_chain;
1148     }
1149
1150     while( i_samples_left >= p_sys->i_frame_size )
1151     {
1152         AVPacket packet = {0};
1153         frame = avcodec_alloc_frame();
1154
1155         frame->nb_samples = p_sys->i_frame_size;
1156         frame->format     = p_sys->p_context->sample_fmt;
1157
1158         if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1159         {
1160             aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer+i_data_offset,
1161                               p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec );
1162             if( avcodec_fill_audio_frame( frame, p_enc->fmt_in.audio.i_channels,
1163                                     p_sys->p_context->sample_fmt,
1164                                     p_sys->p_buffer,
1165                                     p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1166                                     0) < 0 )
1167                  msg_Err( p_enc, "filling error on encode" );
1168         } else {
1169             if( avcodec_fill_audio_frame( frame, p_enc->fmt_in.audio.i_channels,
1170                                     p_sys->p_context->sample_fmt,
1171                                     p_aout_buf->p_buffer+i_data_offset,
1172                                     p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels,
1173                                     0) < 0 )
1174                  msg_Err( p_enc, "filling error on encode" );
1175         }
1176
1177         i_samples_left -= p_sys->i_frame_size;
1178         i_data_offset += p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
1179
1180         frame->pts = p_aout_buf->i_pts;
1181
1182         p_block = block_Alloc( p_sys->i_buffer_out );
1183         av_init_packet( &packet );
1184         packet.data = p_block->p_buffer;
1185         packet.size = p_block->i_buffer;
1186
1187         i_out = avcodec_encode_audio2( p_sys->p_context, &packet, frame, &got_packet );
1188         p_block->i_buffer = packet.size;
1189
1190         if( frame->extended_data != frame->data )
1191            av_freep( frame->extended_data );
1192         av_freep( &frame );
1193         if( unlikely( !got_packet || ( i_out < 0 ) ) )
1194         {
1195             if( i_out < 0 )
1196             {
1197                 msg_Err( p_enc,"Encoding problem..");
1198                 return p_chain;
1199             }
1200             block_Release( p_block );
1201             continue;
1202         }
1203
1204         p_block->i_buffer = packet.size;
1205
1206         p_block->i_length = (mtime_t)1000000 *
1207             (mtime_t)p_sys->i_frame_size /
1208             (mtime_t)p_sys->p_context->sample_rate;
1209
1210         p_block->i_dts = p_block->i_pts = packet.pts;
1211
1212         block_ChainAppend( &p_chain, p_block );
1213     }
1214     if( i_samples_left < 0 )
1215         msg_Err( p_enc, "I_data_left overflow");
1216
1217     // We have leftover samples that don't fill frame_size, and libavcodec doesn't seem to like
1218     // that frame has more data than p_sys->i_frame_size most of the cases currently.
1219     if( i_samples_left > 0 )
1220     {
1221         if( av_sample_fmt_is_planar( p_sys->p_context->sample_fmt ) )
1222             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 );
1223         else
1224             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);
1225         p_sys->i_samples_delay = i_samples_left;
1226     }
1227
1228     return p_chain;
1229 }
1230
1231 /*****************************************************************************
1232  * CloseEncoder: libavcodec encoder destruction
1233  *****************************************************************************/
1234 void CloseEncoder( vlc_object_t *p_this )
1235 {
1236     encoder_t *p_enc = (encoder_t *)p_this;
1237     encoder_sys_t *p_sys = p_enc->p_sys;
1238
1239     vlc_avcodec_lock();
1240     avcodec_close( p_sys->p_context );
1241     vlc_avcodec_unlock();
1242     av_free( p_sys->p_context );
1243
1244     free( p_sys->p_buffer );
1245
1246     free( p_sys );
1247 }