]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/encoder.c
5ae1c844eb653882c6dd195fd55394ce9416122a
[vlc] / modules / codec / ffmpeg / encoder.c
1 /*****************************************************************************
2  * encoder.c: video and audio encoder using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/aout.h>
32 #include <vlc/decoder.h>
33
34 /* ffmpeg header */
35 #define HAVE_MMX
36 #ifdef HAVE_FFMPEG_AVCODEC_H
37 #   include <ffmpeg/avcodec.h>
38 #else
39 #   include <avcodec.h>
40 #endif
41
42 #if LIBAVCODEC_BUILD < 4704
43 #   define AV_NOPTS_VALUE 0
44 #endif
45
46 #include "ffmpeg.h"
47
48 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
49 #define HURRY_UP_GUARD1 (450000)
50 #define HURRY_UP_GUARD2 (300000)
51 #define HURRY_UP_GUARD3 (100000)
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 int  E_(OpenEncoder) ( vlc_object_t * );
57 void E_(CloseEncoder)( vlc_object_t * );
58
59 static block_t *EncodeVideo( encoder_t *, picture_t * );
60 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
61
62 struct thread_context_t;
63 static int FfmpegThread( struct thread_context_t *p_context );
64 static int FfmpegExecute( AVCodecContext *s,
65                           int (*pf_func)(AVCodecContext *c2, void *arg2),
66                           void **arg, int *ret, int count );
67
68 /*****************************************************************************
69  * thread_context_t : for multithreaded encoding
70  *****************************************************************************/
71 #if LIBAVCODEC_BUILD >= 4702
72 struct thread_context_t
73 {
74     VLC_COMMON_MEMBERS
75
76     AVCodecContext  *p_context;
77     int             (* pf_func)(AVCodecContext *c, void *arg);
78     void            *arg;
79     int             i_ret;
80
81     vlc_mutex_t     lock;
82     vlc_cond_t      cond;
83     vlc_bool_t      b_work, b_done;
84 };
85 #endif
86
87 /*****************************************************************************
88  * encoder_sys_t : ffmpeg encoder descriptor
89  *****************************************************************************/
90 struct encoder_sys_t
91 {
92     /*
93      * Ffmpeg properties
94      */
95     AVCodec         *p_codec;
96     AVCodecContext  *p_context;
97
98     /*
99      * Common properties
100      */
101     char *p_buffer;
102     char *p_buffer_out;
103
104     /*
105      * Video properties
106      */
107     mtime_t i_last_ref_pts;
108     mtime_t i_buggy_pts_detect;
109     mtime_t i_last_pts;
110     vlc_bool_t b_inited;
111
112     /*
113      * Audio properties
114      */
115     int i_frame_size;
116     int i_samples_delay;
117     mtime_t i_pts;
118 };
119
120 /*****************************************************************************
121  * OpenEncoder: probe the encoder
122  *****************************************************************************/
123 extern int16_t ff_mpeg4_default_intra_matrix[];
124 extern int16_t ff_mpeg4_default_non_intra_matrix[];
125
126 int E_(OpenEncoder)( vlc_object_t *p_this )
127 {
128     encoder_t *p_enc = (encoder_t *)p_this;
129     encoder_sys_t *p_sys = p_enc->p_sys;
130     AVCodecContext *p_context;
131     AVCodec *p_codec;
132     int i_codec_id, i_cat;
133     char *psz_namecodec;
134
135     if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
136                              &psz_namecodec ) )
137     {
138         if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
139         {
140             /* handed chroma output */
141             return VLC_EGENERIC;
142         }
143         i_cat      = VIDEO_ES;
144         i_codec_id = CODEC_ID_RAWVIDEO;
145         psz_namecodec = "Raw video";
146     }
147
148
149     if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
150     {
151         msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
152         return VLC_EGENERIC;
153     }
154
155     if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
156     {
157         msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
158         return VLC_EGENERIC;
159     }
160
161     /* Initialization must be done before avcodec_find_decoder() */
162     E_(InitLibavcodec)(p_this);
163
164     p_codec = avcodec_find_encoder( i_codec_id );
165     if( !p_codec )
166     {
167         msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
168         return VLC_EGENERIC;
169     }
170
171     /* Allocate the memory needed to store the decoder's structure */
172     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
173     {
174         msg_Err( p_enc, "out of memory" );
175         return VLC_EGENERIC;
176     }
177     p_enc->p_sys = p_sys;
178     p_sys->p_codec = p_codec;
179
180     p_enc->pf_encode_video = EncodeVideo;
181     p_enc->pf_encode_audio = EncodeAudio;
182
183     p_sys->p_buffer_out = NULL;
184     p_sys->p_buffer = NULL;
185     p_sys->b_inited = 0;
186
187     p_sys->p_context = p_context = avcodec_alloc_context();
188
189     /* Set CPU capabilities */
190     p_context->dsp_mask = 0;
191     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
192     {
193         p_context->dsp_mask |= FF_MM_MMX;
194     }
195     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
196     {
197         p_context->dsp_mask |= FF_MM_MMXEXT;
198     }
199     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
200     {
201         p_context->dsp_mask |= FF_MM_3DNOW;
202     }
203     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
204     {
205         p_context->dsp_mask |= FF_MM_SSE;
206         p_context->dsp_mask |= FF_MM_SSE2;
207     }
208
209     if( p_enc->fmt_in.i_cat == VIDEO_ES )
210     {
211         if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
212         {
213             msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
214                       p_enc->fmt_in.video.i_height );
215             free( p_sys );
216             return VLC_EGENERIC;
217         }
218
219         p_context->width = p_enc->fmt_in.video.i_width;
220         p_context->height = p_enc->fmt_in.video.i_height;
221
222         p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
223         p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
224
225         /* Defaults from ffmpeg.c */
226         p_context->qblur = 0.5;
227         p_context->qcompress = 0.5;
228         p_context->b_quant_offset = 1.25;
229         p_context->b_quant_factor = 1.25;
230         p_context->i_quant_offset = 0.0;
231         p_context->i_quant_factor = -0.8;
232
233         p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
234         p_context->max_b_frames =
235             __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
236         p_context->b_frame_strategy = 0;
237
238 #if LIBAVCODEC_BUILD >= 4687
239         p_context->sample_aspect_ratio =
240             (AVRational){ p_enc->fmt_in.video.i_aspect *
241                           (int64_t)p_context->height / p_context->width,
242                           VOUT_ASPECT_FACTOR };
243 #else
244         p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
245             VOUT_ASPECT_FACTOR;
246 #endif
247
248         p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
249
250         p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
251
252         if ( p_enc->b_strict_rc )
253         {
254             p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
255             p_context->rc_buffer_size = p_enc->i_rc_buffer_size;
256             p_context->rc_buffer_aggressivity = p_enc->f_rc_buffer_aggressivity;
257         }
258
259         if ( p_enc->f_i_quant_factor != 0.0 )
260         {
261             p_context->i_quant_factor = p_enc->f_i_quant_factor;
262         }
263
264 #if LIBAVCODEC_BUILD >= 4690
265         p_context->noise_reduction = p_enc->i_noise_reduction;
266 #endif
267
268         if ( p_enc->b_mpeg4_matrix )
269         {
270             p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
271             p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
272         }
273
274         if ( p_enc->b_pre_me )
275         {
276             p_context->pre_me = 1;
277             p_context->me_pre_cmp = FF_CMP_CHROMA;
278         }
279
280         if ( p_enc->b_interlace )
281         {
282             p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
283 #if LIBAVCODEC_BUILD >= 4698
284             p_context->flags |= CODEC_FLAG_INTERLACED_ME;
285 #endif
286         }
287
288         if ( p_enc->b_trellis )
289         {
290             p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
291         }
292
293 #if LIBAVCODEC_BUILD >= 4702
294         if ( p_enc->i_threads >= 1 )
295         {
296             p_context->thread_count = p_enc->i_threads;
297         }
298 #endif
299
300         if( p_enc->i_vtolerance > 0 )
301         {
302             p_context->bit_rate_tolerance = p_enc->i_vtolerance;
303         }
304
305         p_context->mb_qmin = p_context->qmin = p_enc->i_qmin;
306         p_context->mb_qmax = p_context->qmax = p_enc->i_qmax;
307         p_context->max_qdiff = 3;
308
309         p_context->mb_decision = p_enc->i_hq;
310     }
311     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
312     {
313         p_enc->fmt_in.i_codec  = AOUT_FMT_S16_NE;
314         p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
315         p_context->channels    = p_enc->fmt_in.audio.i_channels;
316         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
317         p_sys->p_buffer = malloc( p_sys->i_frame_size );
318         p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
319     }
320
321     /* Misc parameters */
322     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
323
324     if( i_codec_id == CODEC_ID_RAWVIDEO )
325     {
326         /* XXX: hack: Force same codec (will be handled by transcode) */
327         p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
328         p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
329     }
330
331     /* Make sure we get extradata filled by the encoder */
332     p_context->extradata_size = 0;
333     p_context->extradata = NULL;
334     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
335
336     if( avcodec_open( p_context, p_codec ) )
337     {
338         if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
339         {
340             p_context->channels = 2;
341             p_enc->fmt_in.audio.i_channels = 2; // FIXME
342             if( avcodec_open( p_context, p_codec ) )
343             {
344                 msg_Err( p_enc, "cannot open encoder" );
345                 free( p_sys );
346                 return VLC_EGENERIC;
347             }
348             msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
349         }
350         else
351         {
352             msg_Err( p_enc, "cannot open encoder" );
353             free( p_sys );
354             return VLC_EGENERIC;
355         }
356     }
357
358     p_enc->fmt_out.i_extra = p_context->extradata_size;
359     p_enc->fmt_out.p_extra = p_context->extradata;
360     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
361
362     if( p_enc->fmt_in.i_cat == AUDIO_ES )
363     {
364         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
365         p_sys->p_buffer = malloc( p_sys->i_frame_size );
366     }
367
368     p_sys->i_last_ref_pts = 0;
369     p_sys->i_buggy_pts_detect = 0;
370     p_sys->i_samples_delay = 0;
371     p_sys->i_pts = 0;
372     p_sys->i_last_pts = 0;
373
374     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
375
376     return VLC_SUCCESS;
377 }
378
379 /****************************************************************************
380  * Ffmpeg threading system
381  ****************************************************************************/
382 #if LIBAVCODEC_BUILD >= 4702
383 static int FfmpegThread( struct thread_context_t *p_context )
384 {
385     while ( !p_context->b_die && !p_context->b_error )
386     {
387         vlc_mutex_lock( &p_context->lock );
388         while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
389         {
390             vlc_cond_wait( &p_context->cond, &p_context->lock );
391         }
392         p_context->b_work = 0;
393         vlc_mutex_unlock( &p_context->lock );
394         if ( p_context->b_die || p_context->b_error )
395             break;
396
397         if ( p_context->pf_func )
398         {
399             p_context->i_ret = p_context->pf_func( p_context->p_context,
400                                                    p_context->arg );
401         }
402
403         vlc_mutex_lock( &p_context->lock );
404         p_context->b_done = 1;
405         vlc_cond_signal( &p_context->cond );
406         vlc_mutex_unlock( &p_context->lock );
407     }
408
409     return 0;
410 }
411
412 static int FfmpegExecute( AVCodecContext *s,
413                           int (*pf_func)(AVCodecContext *c2, void *arg2),
414                           void **arg, int *ret, int count )
415 {
416     struct thread_context_t ** pp_contexts =
417                          (struct thread_context_t **)s->thread_opaque;
418     int i;
419
420     /* Note, we can be certain that this is not called with the same
421      * AVCodecContext by different threads at the same time */
422     for ( i = 0; i < count; i++ )
423     {
424         vlc_mutex_lock( &pp_contexts[i]->lock );
425         pp_contexts[i]->arg = arg[i];
426         pp_contexts[i]->pf_func = pf_func;
427         pp_contexts[i]->i_ret = 12345;
428         pp_contexts[i]->b_work = 1;
429         vlc_cond_signal( &pp_contexts[i]->cond );
430         vlc_mutex_unlock( &pp_contexts[i]->lock );
431     }
432     for ( i = 0; i < count; i++ )
433     {
434         vlc_mutex_lock( &pp_contexts[i]->lock );
435         while ( !pp_contexts[i]->b_done )
436         {
437             vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
438         }
439         pp_contexts[i]->b_done = 0;
440         pp_contexts[i]->pf_func = NULL;
441         vlc_mutex_unlock( &pp_contexts[i]->lock );
442
443         if ( ret )
444         {
445             ret[i] = pp_contexts[i]->i_ret;
446         }
447     }
448
449     return 0;
450 }
451 #endif
452
453 /****************************************************************************
454  * EncodeVideo: the whole thing
455  ****************************************************************************/
456 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
457 {
458     encoder_sys_t *p_sys = p_enc->p_sys;
459     AVFrame frame;
460     int i_out, i_plane;
461
462 #if LIBAVCODEC_BUILD >= 4702
463     if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
464     {
465         struct thread_context_t ** pp_contexts;
466         int i;
467
468         p_sys->b_inited = 1;
469         pp_contexts = malloc( sizeof(struct thread_context_t *)
470                                  * p_enc->i_threads );
471         p_sys->p_context->thread_opaque = (void *)pp_contexts;
472
473         for ( i = 0; i < p_enc->i_threads; i++ )
474         {
475             pp_contexts[i] = vlc_object_create( p_enc,
476                                      sizeof(struct thread_context_t) );
477             pp_contexts[i]->p_context = p_sys->p_context;
478             vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
479             vlc_cond_init( p_enc, &pp_contexts[i]->cond );
480             pp_contexts[i]->b_work = 0;
481             pp_contexts[i]->b_done = 0;
482             if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
483                                     VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
484             {
485                 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
486                 return NULL;
487             }
488         }
489
490         p_sys->p_context->execute = FfmpegExecute;
491     }
492 #endif
493
494     memset( &frame, 0, sizeof( AVFrame ) );
495     for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
496     {
497         frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
498         frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
499     }
500
501     /* Let ffmpeg select the frame type */
502     frame.pict_type = 0;
503
504     frame.repeat_pict = p_pict->i_nb_fields;
505
506 #if LIBAVCODEC_BUILD >= 4685
507     frame.interlaced_frame = !p_pict->b_progressive;
508     frame.top_field_first = p_pict->b_top_field_first;
509 #endif
510
511     /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
512     if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
513         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
514         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
515     {
516         frame.pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
517
518         if ( p_enc->b_hurry_up && frame.pts != AV_NOPTS_VALUE )
519         {
520             mtime_t current_date = mdate();
521
522             if ( current_date + HURRY_UP_GUARD3 > frame.pts )
523             {
524                 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
525                 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
526                 msg_Dbg( p_enc, "hurry up mode 3" );
527             }
528             else
529             {
530                 p_sys->p_context->mb_decision = p_enc->i_hq;
531
532                 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
533                 {
534                     p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
535 #if LIBAVCODEC_BUILD >= 4690
536                     p_sys->p_context->noise_reduction = p_enc->i_noise_reduction
537                          + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
538 #endif
539                     msg_Dbg( p_enc, "hurry up mode 2" );
540                 }
541                 else
542                 {
543                     if ( p_enc->b_trellis )
544                         p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
545 #if LIBAVCODEC_BUILD >= 4690
546                     p_sys->p_context->noise_reduction =
547                                     p_enc->i_noise_reduction;
548 #endif
549                 }
550             }
551
552             if ( current_date + HURRY_UP_GUARD1 > frame.pts )
553             {
554                 frame.pict_type = FF_P_TYPE;
555                 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
556             }
557         }
558     }
559     else
560     {
561         frame.pts = 0;
562     }
563
564     if ( frame.pts != AV_NOPTS_VALUE && frame.pts != 0 )
565     {
566         if ( p_sys->i_last_pts == frame.pts )
567         {
568             msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
569                       "same PTS (" I64Fd ")", frame.pts );
570             return NULL;
571         }
572         else
573         {
574             p_sys->i_last_pts = frame.pts;
575         }
576     }
577
578     i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
579                                   AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
580
581     if( i_out > 0 )
582     {
583         block_t *p_block = block_New( p_enc, i_out );
584         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
585
586         if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
587             p_sys->p_context->coded_frame->pts != 0 &&
588             p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
589         {
590             p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
591
592             /* FIXME, 3-2 pulldown is not handled correctly */
593             p_block->i_length = I64C(1000000) *
594                 p_enc->fmt_in.video.i_frame_rate_base /
595                 p_enc->fmt_in.video.i_frame_rate;
596             p_block->i_pts    = p_sys->p_context->coded_frame->pts;
597
598             if( !p_sys->p_context->delay ||
599                 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
600                   p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
601             {
602                 p_block->i_dts = p_block->i_pts;
603             }
604             else
605             {
606                 if( p_sys->i_last_ref_pts )
607                 {
608                     p_block->i_dts = p_sys->i_last_ref_pts;
609                 }
610                 else
611                 {
612                     /* Let's put something sensible */
613                     p_block->i_dts = p_block->i_pts;
614                 }
615
616                 p_sys->i_last_ref_pts = p_block->i_pts;
617             }
618         }
619         else
620         {
621             /* Buggy libavcodec which doesn't update coded_frame->pts
622              * correctly */
623             p_block->i_length = I64C(1000000) *
624                 p_enc->fmt_in.video.i_frame_rate_base /
625                 p_enc->fmt_in.video.i_frame_rate;
626             p_block->i_dts = p_block->i_pts = p_pict->date;
627         }
628
629         switch ( p_sys->p_context->coded_frame->pict_type )
630         {
631         case FF_I_TYPE:
632             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
633             break;
634         case FF_P_TYPE:
635             p_block->i_flags |= BLOCK_FLAG_TYPE_P;
636             break;
637         case FF_B_TYPE:
638             p_block->i_flags |= BLOCK_FLAG_TYPE_B;
639             break;
640         }
641
642         return p_block;
643     }
644
645     return NULL;
646 }
647
648 /****************************************************************************
649  * EncodeAudio: the whole thing
650  ****************************************************************************/
651 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
652 {
653     encoder_sys_t *p_sys = p_enc->p_sys;
654     block_t *p_block, *p_chain = NULL;
655
656     char *p_buffer = p_aout_buf->p_buffer;
657     int i_samples = p_aout_buf->i_nb_samples;
658     int i_samples_delay = p_sys->i_samples_delay;
659
660     p_sys->i_pts = p_aout_buf->start_date -
661                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
662                 (mtime_t)p_enc->fmt_in.audio.i_rate;
663
664     p_sys->i_samples_delay += i_samples;
665
666     while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
667     {
668         int16_t *p_samples;
669         int i_out;
670
671         if( i_samples_delay )
672         {
673             /* Take care of the left-over from last time */
674             int i_delay_size = i_samples_delay * 2 *
675                                  p_sys->p_context->channels;
676             int i_size = p_sys->i_frame_size - i_delay_size;
677
678             p_samples = (int16_t *)p_sys->p_buffer;
679             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
680             p_buffer -= i_delay_size;
681             i_samples += i_samples_delay;
682             i_samples_delay = 0;
683         }
684         else
685         {
686             p_samples = (int16_t *)p_buffer;
687         }
688
689         i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
690                                       2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
691                                       p_samples );
692
693 #if 0
694         msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
695 #endif
696         if( i_out < 0 ) break;
697
698         p_buffer += p_sys->i_frame_size;
699         p_sys->i_samples_delay -= p_sys->p_context->frame_size;
700         i_samples -= p_sys->p_context->frame_size;
701
702         if( i_out == 0 ) continue;
703
704         p_block = block_New( p_enc, i_out );
705         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
706
707         p_block->i_length = (mtime_t)1000000 *
708             (mtime_t)p_sys->p_context->frame_size /
709             (mtime_t)p_sys->p_context->sample_rate;
710
711         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
712
713         /* Update pts */
714         p_sys->i_pts += p_block->i_length;
715         block_ChainAppend( &p_chain, p_block );
716     }
717
718     /* Backup the remaining raw samples */
719     if( i_samples )
720     {
721         memcpy( p_sys->p_buffer + i_samples_delay * 2 *
722                 p_sys->p_context->channels, p_buffer,
723                 i_samples * 2 * p_sys->p_context->channels );
724     }
725
726     return p_chain;
727 }
728
729 /*****************************************************************************
730  * CloseEncoder: ffmpeg encoder destruction
731  *****************************************************************************/
732 void E_(CloseEncoder)( vlc_object_t *p_this )
733 {
734     encoder_t *p_enc = (encoder_t *)p_this;
735     encoder_sys_t *p_sys = p_enc->p_sys;
736
737 #if LIBAVCODEC_BUILD >= 4702
738     if ( p_sys->b_inited && p_enc->i_threads >= 1 )
739     {
740         int i;
741         struct thread_context_t ** pp_contexts =
742                 (struct thread_context_t **)p_sys->p_context->thread_opaque;
743         for ( i = 0; i < p_enc->i_threads; i++ )
744         {
745             pp_contexts[i]->b_die = 1;
746             vlc_cond_signal( &pp_contexts[i]->cond );
747             vlc_thread_join( pp_contexts[i] );
748             vlc_mutex_destroy( &pp_contexts[i]->lock );
749             vlc_cond_destroy( &pp_contexts[i]->cond );
750             vlc_object_destroy( pp_contexts[i] );
751         }
752
753         free(pp_contexts);
754     }
755 #endif
756
757     avcodec_close( p_sys->p_context );
758     free( p_sys->p_context );
759
760     if( p_sys->p_buffer ) free( p_sys->p_buffer );
761     if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );
762
763     free( p_sys );
764 }