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