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