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