]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/encoder.c
8fdcc4a712e12dbddc515410404aa07f52b7d69b
[vlc] / modules / codec / ffmpeg / encoder.c
1 /*****************************************************************************
2  * encoder.c: video and audio encoder using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: encoder.c,v 1.15 2003/11/29 13:12:11 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/aout.h>
31 #include <vlc/decoder.h>
32
33 /* ffmpeg header */
34 #define HAVE_MMX
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 #   include <ffmpeg/avcodec.h>
37 #else
38 #   include <avcodec.h>
39 #endif
40
41 #include "ffmpeg.h"
42
43 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
44 #define HURRY_UP_GUARD (200000)
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 int  E_(OpenEncoder) ( vlc_object_t * );
50 void E_(CloseEncoder)( vlc_object_t * );
51
52 static block_t *EncodeVideo( encoder_t *, picture_t * );
53 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
54
55 /*****************************************************************************
56  * encoder_sys_t : ffmpeg encoder descriptor
57  *****************************************************************************/
58 struct encoder_sys_t
59 {
60     /*
61      * Ffmpeg properties
62      */
63     AVCodec         *p_codec;
64     AVCodecContext  *p_context;
65
66     /*
67      * Common properties
68      */
69     char *p_buffer;
70     char *p_buffer_out;
71
72     /*
73      * Videoo properties
74      */
75     mtime_t i_last_ref_pts;
76     mtime_t i_buggy_pts_detect;
77
78     /*
79      * Audio properties
80      */
81     int i_frame_size;
82     int i_samples_delay;
83     mtime_t i_pts;
84 };
85
86 /*****************************************************************************
87  * OpenEncoder: probe the encoder
88  *****************************************************************************/
89 int E_(OpenEncoder)( vlc_object_t *p_this )
90 {
91     encoder_t *p_enc = (encoder_t *)p_this;
92     encoder_sys_t *p_sys = p_enc->p_sys;
93     AVCodecContext *p_context;
94     AVCodec *p_codec;
95     int i_codec_id, i_cat;
96     char *psz_namecodec;
97
98     if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
99                              &psz_namecodec ) )
100     {
101         if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
102         {
103             /* handed chroma output */
104             return VLC_EGENERIC;
105         }
106         i_cat      = VIDEO_ES;
107         i_codec_id = CODEC_ID_RAWVIDEO;
108         psz_namecodec = "Raw video";
109     }
110
111
112     if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
113     {
114         msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
115         return VLC_EGENERIC;
116     }
117
118     if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
119     {
120         msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
121         return VLC_EGENERIC;
122     }
123
124     /* Initialization must be done before avcodec_find_decoder() */
125     E_(InitLibavcodec)(p_this);
126
127     p_codec = avcodec_find_encoder( i_codec_id );
128     if( !p_codec )
129     {
130         msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
131         return VLC_EGENERIC;
132     }
133
134     /* Allocate the memory needed to store the decoder's structure */
135     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
136     {
137         msg_Err( p_enc, "out of memory" );
138         return VLC_EGENERIC;
139     }
140     p_enc->p_sys = p_sys;
141     p_sys->p_codec = p_codec;
142
143     p_enc->pf_encode_video = EncodeVideo;
144     p_enc->pf_encode_audio = EncodeAudio;
145
146     p_sys->p_buffer_out = NULL;
147     p_sys->p_buffer = NULL;
148
149     p_sys->p_context = p_context = avcodec_alloc_context();
150
151     /* Set CPU capabilities */
152     p_context->dsp_mask = 0;
153     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
154     {
155         p_context->dsp_mask |= FF_MM_MMX;
156     }
157     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
158     {
159         p_context->dsp_mask |= FF_MM_MMXEXT;
160     }
161     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
162     {
163         p_context->dsp_mask |= FF_MM_3DNOW;
164     }
165     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
166     {
167         p_context->dsp_mask |= FF_MM_SSE;
168         p_context->dsp_mask |= FF_MM_SSE2;
169     }
170
171     /* Make sure we get extradata filled by the encoder */
172     p_context->extradata_size = 0;
173     p_context->extradata = NULL;
174     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
175
176     if( p_enc->fmt_in.i_cat == VIDEO_ES )
177     {
178         p_context->width = p_enc->fmt_in.video.i_width;
179         p_context->height = p_enc->fmt_in.video.i_height;
180
181         p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
182 #if LIBAVCODEC_BUILD >= 4662
183         p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
184 #endif
185
186 #if LIBAVCODEC_BUILD >= 4687
187         p_context->sample_aspect_ratio =
188             av_d2q( p_enc->fmt_in.video.i_aspect * p_context->height /
189                     p_context->width / VOUT_ASPECT_FACTOR, 255 );
190 #else
191         p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
192             VOUT_ASPECT_FACTOR;
193 #endif
194
195         p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
196
197         p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
198
199         if ( p_enc->b_strict_rc )
200         {
201             p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
202             p_context->rc_buffer_size = p_context->bit_rate / 2;
203             p_context->rc_buffer_aggressivity = 1000.0; /* FIXME */
204         }
205
206         if ( p_enc->b_pre_me )
207         {
208             p_context->pre_me = 1;
209             p_context->me_pre_cmp = FF_CMP_CHROMA;
210         }
211     }
212     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
213     {
214         p_enc->fmt_in.i_codec  = AOUT_FMT_S16_NE;
215         p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
216         p_context->channels    = p_enc->fmt_in.audio.i_channels;
217         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
218         p_sys->p_buffer = malloc( p_sys->i_frame_size );
219         p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
220     }
221
222     /* Misc parameters */
223     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
224     p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
225     p_context->max_b_frames =
226         __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
227     p_context->b_frame_strategy = 0;
228     p_context->b_quant_factor = 2.0;
229
230     if( p_enc->i_vtolerance > 0 )
231     {
232         p_context->bit_rate_tolerance = p_enc->i_vtolerance;
233     }
234     p_context->qmin = p_enc->i_qmin;
235     p_context->qmax = p_enc->i_qmax;
236
237 #if LIBAVCODEC_BUILD >= 4673
238     p_context->mb_decision = p_enc->i_hq;
239 #else
240     if( p_enc->i_hq )
241     {
242         p_context->flags |= CODEC_FLAG_HQ;
243     }
244 #endif
245
246     if( i_codec_id == CODEC_ID_RAWVIDEO )
247     {
248         /* XXX: hack: Force same codec (will be handled by transcode) */
249         p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
250         p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
251     }
252
253     /* Make sure we get extradata filled by the encoder */
254     p_context->extradata_size = 0;
255     p_context->extradata = NULL;
256     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
257
258     if( avcodec_open( p_context, p_codec ) )
259     {
260         if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
261         {
262             p_context->channels = 2;
263             p_enc->fmt_in.audio.i_channels = 2; // FIXME
264             if( avcodec_open( p_context, p_codec ) )
265             {
266                 msg_Err( p_enc, "cannot open encoder" );
267                 return VLC_EGENERIC;
268             }
269             msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
270         }
271         else
272         {
273             msg_Err( p_enc, "cannot open encoder" );
274             return VLC_EGENERIC;
275         }
276     }
277
278     p_enc->fmt_out.i_extra = p_context->extradata_size;
279     p_enc->fmt_out.p_extra = p_context->extradata;
280     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
281
282     if( p_enc->fmt_in.i_cat == AUDIO_ES )
283     {
284         p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
285         p_sys->p_buffer = malloc( p_sys->i_frame_size );
286     }
287
288     p_sys->i_last_ref_pts = 0;
289     p_sys->i_buggy_pts_detect = 0;
290     p_sys->i_samples_delay = 0;
291     p_sys->i_pts = 0;
292
293     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
294
295     return VLC_SUCCESS;
296 }
297
298 /****************************************************************************
299  * EncodeVideo: the whole thing
300  ****************************************************************************/
301 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
302 {
303     encoder_sys_t *p_sys = p_enc->p_sys;
304     AVFrame frame;
305     int i_out, i_plane;
306     vlc_bool_t b_hurry_up = 0;
307
308     memset( &frame, 0, sizeof( AVFrame ) );
309     for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
310     {
311         frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
312         frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
313     }
314
315     /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
316     if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
317         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
318         p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
319     {
320         frame.pts = p_pict->date;
321 #if LIBAVCODEC_BUILD >= 4673
322         if ( frame.pts && mdate() + HURRY_UP_GUARD > frame.pts
323               && p_enc->b_hurry_up )
324         {
325             msg_Dbg( p_enc, "hurry up mode" );
326             p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
327             b_hurry_up = 1;
328         }
329 #endif
330     }
331     else
332     {
333         frame.pts = 0;
334     }
335
336     /* Let ffmpeg select the frame type */
337     frame.pict_type = 0;
338     frame.repeat_pict = p_pict->i_nb_fields;
339
340 #if LIBAVCODEC_BUILD >= 468
341     frame.interlaced_frame = !p_pict->b_progressive;
342     frame.top_field_first = p_pict->b_top_field_first;
343 #endif
344
345     i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
346                                   AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
347
348 #if LIBAVCODEC_BUILD >= 4673
349     if ( b_hurry_up )
350     {
351         p_sys->p_context->mb_decision = p_enc->i_hq;
352     }
353 #endif
354
355     if( i_out > 0 )
356     {
357         block_t *p_block = block_New( p_enc, i_out );
358         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
359
360         if( p_sys->p_context->coded_frame->pts != 0 &&
361             p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
362         {
363             p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
364
365             /* FIXME, 3-2 pulldown is not handled correctly */
366             p_block->i_length = I64C(1000000) *
367                 p_enc->fmt_in.video.i_frame_rate_base /
368                 p_enc->fmt_in.video.i_frame_rate;
369             p_block->i_pts    = p_sys->p_context->coded_frame->pts;
370
371             if( !p_sys->p_context->delay ||
372                 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
373                   p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
374             {
375                 p_block->i_dts = p_block->i_pts;
376             }
377             else
378             {
379                 if( p_sys->i_last_ref_pts )
380                 {
381                     p_block->i_dts = p_sys->i_last_ref_pts;
382                 }
383                 else
384                 {
385                     /* Let's put something sensible */
386                     p_block->i_dts = p_block->i_pts;
387                 }
388
389                 p_sys->i_last_ref_pts = p_block->i_pts;
390             }
391         }
392         else
393         {
394             /* Buggy libavcodec which doesn't update coded_frame->pts
395              * correctly */
396             p_block->i_length = I64C(1000000) *
397                 p_enc->fmt_in.video.i_frame_rate_base /
398                 p_enc->fmt_in.video.i_frame_rate;
399             p_block->i_dts = p_block->i_pts = p_pict->date;
400         }
401
402         return p_block;
403     }
404
405     return NULL;
406 }
407
408 /****************************************************************************
409  * EncodeAudio: the whole thing
410  ****************************************************************************/
411 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
412 {
413     encoder_sys_t *p_sys = p_enc->p_sys;
414     block_t *p_block, *p_chain = NULL;
415
416     char *p_buffer = p_aout_buf->p_buffer;
417     int i_samples = p_aout_buf->i_nb_samples;
418     int i_samples_delay = p_sys->i_samples_delay;
419
420     p_sys->i_pts = p_aout_buf->start_date -
421                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
422                 (mtime_t)p_enc->fmt_in.audio.i_rate;
423
424     p_sys->i_samples_delay += i_samples;
425
426     while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
427     {
428         int16_t *p_samples;
429         int i_out;
430
431         if( i_samples_delay )
432         {
433             /* Take care of the left-over from last time */
434             int i_delay_size = i_samples_delay * 2 *
435                                  p_sys->p_context->channels;
436             int i_size = p_sys->i_frame_size - i_delay_size;
437
438             p_samples = (int16_t *)p_sys->p_buffer;
439             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
440             p_buffer -= i_delay_size;
441             i_samples += i_samples_delay;
442             i_samples_delay = 0;
443         }
444         else
445         {
446             p_samples = (int16_t *)p_buffer;
447         }
448
449         i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
450                                       2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
451                                       p_samples );
452
453 #if 0
454         msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
455 #endif
456         if( i_out < 0 ) break;
457
458         p_buffer += p_sys->i_frame_size;
459         p_sys->i_samples_delay -= p_sys->p_context->frame_size;
460         i_samples -= p_sys->p_context->frame_size;
461
462         if( i_out == 0 ) continue;
463
464         p_block = block_New( p_enc, i_out );
465         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
466
467         p_block->i_length = (mtime_t)1000000 *
468             (mtime_t)p_sys->p_context->frame_size /
469             (mtime_t)p_sys->p_context->sample_rate;
470
471         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
472
473         /* Update pts */
474         p_sys->i_pts += p_block->i_length;
475         block_ChainAppend( &p_chain, p_block );
476     }
477
478     /* Backup the remaining raw samples */
479     if( i_samples )
480     {
481         memcpy( p_sys->p_buffer, p_buffer + i_samples_delay * 2 *
482                 p_sys->p_context->channels,
483                 i_samples * 2 * p_sys->p_context->channels );
484     }
485
486     return p_chain;
487 }
488
489 /*****************************************************************************
490  * CloseEncoder: ffmpeg encoder destruction
491  *****************************************************************************/
492 void E_(CloseEncoder)( vlc_object_t *p_this )
493 {
494     encoder_t *p_enc = (encoder_t *)p_this;
495     encoder_sys_t *p_sys = p_enc->p_sys;
496
497     avcodec_close( p_sys->p_context );
498     free( p_sys->p_context );
499
500     if( p_sys->p_buffer ) free( p_sys->p_buffer );
501     if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );
502
503     free( p_sys );
504 }