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