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