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