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