]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/encoder.c
* modules/codec/ffmpeg/encoder.c:
[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: encoder.c,v 1.24 2004/03/03 11:29:26 massiot Exp $
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     /* Make sure we get extradata filled by the encoder */
210     p_context->extradata_size = 0;
211     p_context->extradata = NULL;
212     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
213
214     if( p_enc->fmt_in.i_cat == VIDEO_ES )
215     {
216         if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
217         {
218             msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
219                       p_enc->fmt_in.video.i_height );
220             free( p_sys );
221             return VLC_EGENERIC;
222         }
223
224         p_context->width = p_enc->fmt_in.video.i_width;
225         p_context->height = p_enc->fmt_in.video.i_height;
226
227         p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
228         p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
229
230         /* Defaults from ffmpeg.c */
231         p_context->qblur = 0.5;
232         p_context->qcompress = 0.5;
233         p_context->b_quant_offset = 1.25;
234         p_context->b_quant_factor = 1.25;
235         p_context->i_quant_offset = 0.0;
236         p_context->i_quant_factor = -0.8;
237
238         p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
239         p_context->max_b_frames =
240             __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
241         p_context->b_frame_strategy = 0;
242
243 #if LIBAVCODEC_BUILD >= 4687
244         p_context->sample_aspect_ratio =
245             (AVRational){ p_enc->fmt_in.video.i_aspect *
246                           (int64_t)p_context->height / p_context->width,
247                           VOUT_ASPECT_FACTOR };
248 #else
249         p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
250             VOUT_ASPECT_FACTOR;
251 #endif
252
253         p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
254
255         p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
256
257         if ( p_enc->b_strict_rc )
258         {
259             p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
260             p_context->rc_buffer_size = p_enc->i_rc_buffer_size;
261             p_context->rc_buffer_aggressivity = p_enc->f_rc_buffer_aggressivity;
262         }
263
264         if ( p_enc->f_i_quant_factor != 0.0 )
265         {
266             p_context->i_quant_factor = p_enc->f_i_quant_factor;
267         }
268
269 #if LIBAVCODEC_BUILD >= 4690
270         p_context->noise_reduction = p_enc->i_noise_reduction;
271 #endif
272
273         if ( p_enc->b_mpeg4_matrix )
274         {
275             p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
276             p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
277         }
278
279         if ( p_enc->b_pre_me )
280         {
281             p_context->pre_me = 1;
282             p_context->me_pre_cmp = FF_CMP_CHROMA;
283         }
284
285         if ( p_enc->b_interlace )
286         {
287             p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
288 #if LIBAVCODEC_BUILD >= 4698
289             p_context->flags |= CODEC_FLAG_INTERLACED_ME;
290 #endif
291         }
292
293         if ( p_enc->b_trellis )
294         {
295             p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
296         }
297
298 #if LIBAVCODEC_BUILD >= 4702
299         if ( p_enc->i_threads >= 1 )
300         {
301             p_context->thread_count = p_enc->i_threads;
302         }
303 #endif
304
305         if( p_enc->i_vtolerance > 0 )
306         {
307             p_context->bit_rate_tolerance = p_enc->i_vtolerance;
308         }
309
310         p_context->mb_qmin = p_context->qmin = p_enc->i_qmin;
311         p_context->mb_qmax = p_context->qmax = p_enc->i_qmax;
312         p_context->max_qdiff = 3;
313
314         p_context->mb_decision = p_enc->i_hq;
315     }
316     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
317     {
318         p_enc->fmt_in.i_codec  = AOUT_FMT_S16_NE;
319         p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
320         p_context->channels    = p_enc->fmt_in.audio.i_channels;
321         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
322         p_sys->p_buffer = malloc( p_sys->i_frame_size );
323         p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
324     }
325
326     /* Misc parameters */
327     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
328
329     if( i_codec_id == CODEC_ID_RAWVIDEO )
330     {
331         /* XXX: hack: Force same codec (will be handled by transcode) */
332         p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
333         p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
334     }
335
336     /* Make sure we get extradata filled by the encoder */
337     p_context->extradata_size = 0;
338     p_context->extradata = NULL;
339     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
340
341     if( avcodec_open( p_context, p_codec ) )
342     {
343         if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
344         {
345             p_context->channels = 2;
346             p_enc->fmt_in.audio.i_channels = 2; // FIXME
347             if( avcodec_open( p_context, p_codec ) )
348             {
349                 msg_Err( p_enc, "cannot open encoder" );
350                 free( p_sys );
351                 return VLC_EGENERIC;
352             }
353             msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
354         }
355         else
356         {
357             msg_Err( p_enc, "cannot open encoder" );
358             free( p_sys );
359             return VLC_EGENERIC;
360         }
361     }
362
363     p_enc->fmt_out.i_extra = p_context->extradata_size;
364     p_enc->fmt_out.p_extra = p_context->extradata;
365     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
366
367     if( p_enc->fmt_in.i_cat == AUDIO_ES )
368     {
369         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
370         p_sys->p_buffer = malloc( p_sys->i_frame_size );
371     }
372
373     p_sys->i_last_ref_pts = 0;
374     p_sys->i_buggy_pts_detect = 0;
375     p_sys->i_samples_delay = 0;
376     p_sys->i_pts = 0;
377     p_sys->i_last_pts = 0;
378
379     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
380
381     return VLC_SUCCESS;
382 }
383
384 /****************************************************************************
385  * Ffmpeg threading system
386  ****************************************************************************/
387 #if LIBAVCODEC_BUILD >= 4702
388 static int FfmpegThread( struct thread_context_t *p_context )
389 {
390     while ( !p_context->b_die && !p_context->b_error )
391     {
392         vlc_mutex_lock( &p_context->lock );
393         while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
394         {
395             vlc_cond_wait( &p_context->cond, &p_context->lock );
396         }
397         p_context->b_work = 0;
398         vlc_mutex_unlock( &p_context->lock );
399         if ( p_context->b_die || p_context->b_error )
400             break;
401
402         if ( p_context->pf_func )
403         {
404             p_context->i_ret = p_context->pf_func( p_context->p_context,
405                                                    p_context->arg );
406         }
407
408         vlc_mutex_lock( &p_context->lock );
409         p_context->b_done = 1;
410         vlc_cond_signal( &p_context->cond );
411         vlc_mutex_unlock( &p_context->lock );
412     }
413
414     return 0;
415 }
416
417 static int FfmpegExecute( AVCodecContext *s,
418                           int (*pf_func)(AVCodecContext *c2, void *arg2),
419                           void **arg, int *ret, int count )
420 {
421     struct thread_context_t ** pp_contexts =
422                          (struct thread_context_t **)s->thread_opaque;
423     int i;
424
425     /* Note, we can be certain that this is not called with the same
426      * AVCodecContext by different threads at the same time */
427     for ( i = 0; i < count; i++ )
428     {
429         vlc_mutex_lock( &pp_contexts[i]->lock );
430         pp_contexts[i]->arg = arg[i];
431         pp_contexts[i]->pf_func = pf_func;
432         pp_contexts[i]->i_ret = 12345;
433         pp_contexts[i]->b_work = 1;
434         vlc_cond_signal( &pp_contexts[i]->cond );
435         vlc_mutex_unlock( &pp_contexts[i]->lock );
436     }
437     for ( i = 0; i < count; i++ )
438     {
439         vlc_mutex_lock( &pp_contexts[i]->lock );
440         while ( !pp_contexts[i]->b_done )
441         {
442             vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock );
443         }
444         pp_contexts[i]->b_done = 0;
445         pp_contexts[i]->pf_func = NULL;
446         vlc_mutex_unlock( &pp_contexts[i]->lock );
447
448         if ( ret )
449         {
450             ret[i] = pp_contexts[i]->i_ret;
451         }
452     }
453
454     return 0;
455 }
456 #endif
457
458 /****************************************************************************
459  * EncodeVideo: the whole thing
460  ****************************************************************************/
461 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
462 {
463     encoder_sys_t *p_sys = p_enc->p_sys;
464     AVFrame frame;
465     int i_out, i_plane;
466
467 #if LIBAVCODEC_BUILD >= 4702
468     if ( !p_sys->b_inited && p_enc->i_threads >= 1 )
469     {
470         struct thread_context_t ** pp_contexts;
471         int i;
472
473         p_sys->b_inited = 1;
474         pp_contexts = malloc( sizeof(struct thread_context_t *)
475                                  * p_enc->i_threads );
476         p_sys->p_context->thread_opaque = (void *)pp_contexts;
477
478         for ( i = 0; i < p_enc->i_threads; i++ )
479         {
480             pp_contexts[i] = vlc_object_create( p_enc,
481                                      sizeof(struct thread_context_t) );
482             pp_contexts[i]->p_context = p_sys->p_context;
483             vlc_mutex_init( p_enc, &pp_contexts[i]->lock );
484             vlc_cond_init( p_enc, &pp_contexts[i]->cond );
485             pp_contexts[i]->b_work = 0;
486             pp_contexts[i]->b_done = 0;
487             if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread,
488                                     VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
489             {
490                 msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" );
491                 return NULL;
492             }
493         }
494
495         p_sys->p_context->execute = FfmpegExecute;
496     }
497 #endif
498
499     memset( &frame, 0, sizeof( AVFrame ) );
500     for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
501     {
502         frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
503         frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
504     }
505
506     /* Let ffmpeg select the frame type */
507     frame.pict_type = 0;
508
509     frame.repeat_pict = p_pict->i_nb_fields;
510
511 #if LIBAVCODEC_BUILD >= 4685
512     frame.interlaced_frame = !p_pict->b_progressive;
513     frame.top_field_first = p_pict->b_top_field_first;
514 #endif
515
516     /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
517     if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
518         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
519         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
520     {
521         frame.pts = p_pict->date ? p_pict->date : AV_NOPTS_VALUE;
522
523         if ( p_enc->b_hurry_up && frame.pts != AV_NOPTS_VALUE )
524         {
525             mtime_t current_date = mdate();
526
527             if ( current_date + HURRY_UP_GUARD3 > frame.pts )
528             {
529                 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
530                 p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
531                 msg_Dbg( p_enc, "hurry up mode 3" );
532             }
533             else
534             {
535                 p_sys->p_context->mb_decision = p_enc->i_hq;
536
537                 if ( current_date + HURRY_UP_GUARD2 > frame.pts )
538                 {
539                     p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
540 #if LIBAVCODEC_BUILD >= 4690
541                     p_sys->p_context->noise_reduction = p_enc->i_noise_reduction
542                          + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
543 #endif
544                     msg_Dbg( p_enc, "hurry up mode 2" );
545                 }
546                 else
547                 {
548                     if ( p_enc->b_trellis )
549                         p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
550 #if LIBAVCODEC_BUILD >= 4690
551                     p_sys->p_context->noise_reduction =
552                                     p_enc->i_noise_reduction;
553 #endif
554                 }
555             }
556
557             if ( current_date + HURRY_UP_GUARD1 > frame.pts )
558             {
559                 frame.pict_type = FF_P_TYPE;
560                 /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
561             }
562         }
563     }
564     else
565     {
566         frame.pts = AV_NOPTS_VALUE;
567     }
568
569     if ( frame.pts != AV_NOPTS_VALUE )
570     {
571         if ( p_sys->i_last_pts == frame.pts )
572         {
573             msg_Warn( p_enc,
574                       "almost fed libavcodec with two frames with the same PTS (" I64Fd ")",
575                       frame.pts );
576             return NULL;
577         }
578         else
579         {
580             p_sys->i_last_pts = frame.pts;
581         }
582     }
583
584     i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
585                                   AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
586
587     if( i_out > 0 )
588     {
589         block_t *p_block = block_New( p_enc, i_out );
590         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
591
592         if( p_sys->p_context->coded_frame->pts != AV_NOPTS_VALUE &&
593             p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
594         {
595             p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
596
597             /* FIXME, 3-2 pulldown is not handled correctly */
598             p_block->i_length = I64C(1000000) *
599                 p_enc->fmt_in.video.i_frame_rate_base /
600                 p_enc->fmt_in.video.i_frame_rate;
601             p_block->i_pts    = p_sys->p_context->coded_frame->pts;
602
603             if( !p_sys->p_context->delay ||
604                 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
605                   p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
606             {
607                 p_block->i_dts = p_block->i_pts;
608             }
609             else
610             {
611                 if( p_sys->i_last_ref_pts )
612                 {
613                     p_block->i_dts = p_sys->i_last_ref_pts;
614                 }
615                 else
616                 {
617                     /* Let's put something sensible */
618                     p_block->i_dts = p_block->i_pts;
619                 }
620
621                 p_sys->i_last_ref_pts = p_block->i_pts;
622             }
623         }
624         else
625         {
626             /* Buggy libavcodec which doesn't update coded_frame->pts
627              * correctly */
628             p_block->i_length = I64C(1000000) *
629                 p_enc->fmt_in.video.i_frame_rate_base /
630                 p_enc->fmt_in.video.i_frame_rate;
631             p_block->i_dts = p_block->i_pts = p_pict->date;
632         }
633
634         switch ( p_sys->p_context->coded_frame->pict_type )
635         {
636         case FF_I_TYPE:
637             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
638             break;
639         case FF_P_TYPE:
640             p_block->i_flags |= BLOCK_FLAG_TYPE_P;
641             break;
642         case FF_B_TYPE:
643             p_block->i_flags |= BLOCK_FLAG_TYPE_B;
644             break;
645         }
646
647         return p_block;
648     }
649
650     return NULL;
651 }
652
653 /****************************************************************************
654  * EncodeAudio: the whole thing
655  ****************************************************************************/
656 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
657 {
658     encoder_sys_t *p_sys = p_enc->p_sys;
659     block_t *p_block, *p_chain = NULL;
660
661     char *p_buffer = p_aout_buf->p_buffer;
662     int i_samples = p_aout_buf->i_nb_samples;
663     int i_samples_delay = p_sys->i_samples_delay;
664
665     p_sys->i_pts = p_aout_buf->start_date -
666                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
667                 (mtime_t)p_enc->fmt_in.audio.i_rate;
668
669     p_sys->i_samples_delay += i_samples;
670
671     while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
672     {
673         int16_t *p_samples;
674         int i_out;
675
676         if( i_samples_delay )
677         {
678             /* Take care of the left-over from last time */
679             int i_delay_size = i_samples_delay * 2 *
680                                  p_sys->p_context->channels;
681             int i_size = p_sys->i_frame_size - i_delay_size;
682
683             p_samples = (int16_t *)p_sys->p_buffer;
684             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
685             p_buffer -= i_delay_size;
686             i_samples += i_samples_delay;
687             i_samples_delay = 0;
688         }
689         else
690         {
691             p_samples = (int16_t *)p_buffer;
692         }
693
694         i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
695                                       2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
696                                       p_samples );
697
698 #if 0
699         msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
700 #endif
701         if( i_out < 0 ) break;
702
703         p_buffer += p_sys->i_frame_size;
704         p_sys->i_samples_delay -= p_sys->p_context->frame_size;
705         i_samples -= p_sys->p_context->frame_size;
706
707         if( i_out == 0 ) continue;
708
709         p_block = block_New( p_enc, i_out );
710         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
711
712         p_block->i_length = (mtime_t)1000000 *
713             (mtime_t)p_sys->p_context->frame_size /
714             (mtime_t)p_sys->p_context->sample_rate;
715
716         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
717
718         /* Update pts */
719         p_sys->i_pts += p_block->i_length;
720         block_ChainAppend( &p_chain, p_block );
721     }
722
723     /* Backup the remaining raw samples */
724     if( i_samples )
725     {
726         memcpy( p_sys->p_buffer + i_samples_delay * 2 *
727                 p_sys->p_context->channels, p_buffer,
728                 i_samples * 2 * p_sys->p_context->channels );
729     }
730
731     return p_chain;
732 }
733
734 /*****************************************************************************
735  * CloseEncoder: ffmpeg encoder destruction
736  *****************************************************************************/
737 void E_(CloseEncoder)( vlc_object_t *p_this )
738 {
739     encoder_t *p_enc = (encoder_t *)p_this;
740     encoder_sys_t *p_sys = p_enc->p_sys;
741
742 #if LIBAVCODEC_BUILD >= 4702
743     if ( p_sys->b_inited && p_enc->i_threads >= 1 )
744     {
745         int i;
746         struct thread_context_t ** pp_contexts =
747                 (struct thread_context_t **)p_sys->p_context->thread_opaque;
748         for ( i = 0; i < p_enc->i_threads; i++ )
749         {
750             pp_contexts[i]->b_die = 1;
751             vlc_cond_signal( &pp_contexts[i]->cond );
752             vlc_thread_join( pp_contexts[i] );
753             vlc_mutex_destroy( &pp_contexts[i]->lock );
754             vlc_cond_destroy( &pp_contexts[i]->cond );
755             vlc_object_destroy( pp_contexts[i] );
756         }
757
758         free(pp_contexts);
759     }
760 #endif
761
762     avcodec_close( p_sys->p_context );
763     free( p_sys->p_context );
764
765     if( p_sys->p_buffer ) free( p_sys->p_buffer );
766     if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );
767
768     free( p_sys );
769 }