]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/encoder.c
* include/vlc_codec.h, modules/codec/ffmpeg/encoder.c, modules/stream_out/transcode...
[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.5 2003/11/05 18:59:01 gbazin 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 <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33 #include <vlc/aout.h>
34 #include <vlc/decoder.h>
35 #include <vlc/input.h>
36 #include <vlc/sout.h>
37
38 #include "aout_internal.h"
39
40 /* ffmpeg header */
41 #ifdef HAVE_FFMPEG_AVCODEC_H
42 #   include <ffmpeg/avcodec.h>
43 #else
44 #   include <avcodec.h>
45 #endif
46
47 #include "ffmpeg.h"
48
49 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 int  E_(OpenVideoEncoder) ( vlc_object_t * );
55 void E_(CloseVideoEncoder)( vlc_object_t * );
56
57 int  E_(OpenAudioEncoder) ( vlc_object_t * );
58 void E_(CloseAudioEncoder)( vlc_object_t * );
59
60 static block_t *EncodeVideo( encoder_t *, picture_t * );
61 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
62
63 /*****************************************************************************
64  * encoder_sys_t : ffmpeg encoder descriptor
65  *****************************************************************************/
66 struct encoder_sys_t
67 {
68     /*
69      * Ffmpeg properties
70      */
71     AVCodec         *p_codec;
72     AVCodecContext  *p_context;
73
74     /*
75      * Common properties
76      */
77     char *p_buffer;
78     char *p_buffer_out;
79
80     /*
81      * Videoo properties
82      */
83     mtime_t i_last_ref_pts;
84     mtime_t i_buggy_pts_detect;
85
86     /*
87      * Audio properties
88      */
89     int i_frame_size;
90     int i_samples_delay;
91     mtime_t i_pts;
92 };
93
94 /*****************************************************************************
95  * OpenVideoEncoder: probe the encoder
96  *****************************************************************************/
97 int E_(OpenVideoEncoder)( vlc_object_t *p_this )
98 {
99     encoder_t *p_enc = (encoder_t *)p_this;
100     encoder_sys_t *p_sys = p_enc->p_sys;
101     AVCodecContext  *p_context;
102     AVCodec *p_codec;
103     int i_codec_id, i_cat;
104     char *psz_namecodec;
105
106     if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
107                              &psz_namecodec ) )
108     {
109         return VLC_EGENERIC;
110     }
111
112     if( 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     /* Initialization must be done before avcodec_find_decoder() */
119     E_(InitLibavcodec)(p_this);
120
121     p_codec = avcodec_find_encoder( i_codec_id );
122     if( !p_codec )
123     {
124         msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
125         return VLC_EGENERIC;
126     }
127
128     /* Allocate the memory needed to store the decoder's structure */
129     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
130     {
131         msg_Err( p_enc, "out of memory" );
132         return VLC_EGENERIC;
133     }
134     p_enc->p_sys = p_sys;
135     p_sys->p_codec = p_codec;
136
137     p_enc->pf_header = NULL;
138     p_enc->pf_encode_video = EncodeVideo;
139     p_enc->format.video.i_chroma = VLC_FOURCC('I','4','2','0');
140
141     if( p_enc->i_fourcc == VLC_FOURCC( 'm','p','1','v' ) ||
142         p_enc->i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
143     {
144         p_enc->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
145     }
146
147     p_sys->p_context = p_context = avcodec_alloc_context();
148     p_context->width = p_enc->format.video.i_width;
149     p_context->height = p_enc->format.video.i_height;
150     p_context->bit_rate = p_enc->i_bitrate;
151
152     p_context->frame_rate = p_enc->i_frame_rate;
153 #if LIBAVCODEC_BUILD >= 4662
154     p_context->frame_rate_base= p_enc->i_frame_rate_base;
155 #endif
156
157 #if LIBAVCODEC_BUILD >= 4687
158     p_context->sample_aspect_ratio =
159         av_d2q( p_enc->i_aspect * p_context->height / p_context->width /
160                 VOUT_ASPECT_FACTOR, 255 );
161 #else
162     p_context->aspect_ratio = ((float)p_enc->i_aspect) / VOUT_ASPECT_FACTOR );
163 #endif
164
165     p_context->gop_size = p_enc->i_key_int >= 0 ? p_enc->i_key_int : 50;
166     p_context->max_b_frames =
167         __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
168     p_context->b_frame_strategy = 0;
169     p_context->b_quant_factor = 2.0;
170
171     if( p_enc->i_vtolerance >= 0 )
172     {
173         p_context->bit_rate_tolerance = p_enc->i_vtolerance;
174     }
175     p_context->qmin = p_enc->i_qmin;
176     p_context->qmax = p_enc->i_qmax;
177
178 #if LIBAVCODEC_BUILD >= 4673
179     p_context->mb_decision = p_enc->i_hq;
180 #else
181     if( p_enc->i_hq )
182     {
183         p_context->flags |= CODEC_FLAG_HQ;
184     }
185 #endif
186
187     if( i_codec_id == CODEC_ID_RAWVIDEO )
188     {
189         p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->i_fourcc );
190     }
191
192     /* Make sure we get extradata filled by the encoder */
193     p_context->extradata_size = 0;
194     p_context->extradata = NULL;
195     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
196
197     if( avcodec_open( p_context, p_sys->p_codec ) )
198     {
199         msg_Err( p_enc, "cannot open encoder" );
200         return VLC_EGENERIC;
201     }
202
203     p_enc->i_extra_data = p_context->extradata_size;
204     p_enc->p_extra_data = p_context->extradata;
205     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
206
207     p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
208     p_sys->i_last_ref_pts = 0;
209     p_sys->i_buggy_pts_detect = 0;
210
211     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
212
213     return VLC_SUCCESS;
214 }
215
216 /****************************************************************************
217  * EncodeVideo: the whole thing
218  ****************************************************************************/
219 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
220 {
221     encoder_sys_t *p_sys = p_enc->p_sys;
222     AVFrame frame;
223     int i_out, i_plane;
224
225     for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
226     {
227         frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
228         frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
229     }
230
231     /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
232     if( p_enc->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
233         frame.pts = p_pict->date;
234     else
235         frame.pts = 0;
236
237     /* Let ffmpeg select the frame type */
238     frame.pict_type = 0;
239
240     i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
241                                   AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
242     if( i_out > 0 )
243     {
244         block_t *p_block = block_New( p_enc, i_out );
245         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
246
247         if( p_sys->p_context->coded_frame->pts != 0 &&
248             p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
249         {
250             p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
251
252             /* FIXME, 3-2 pulldown is not handled correctly */
253             p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
254                                   p_enc->i_frame_rate;
255             p_block->i_pts    = p_sys->p_context->coded_frame->pts;
256
257             if( !p_sys->p_context->delay ||
258                 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
259                   p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
260             {
261                 p_block->i_dts = p_block->i_pts;
262             }
263             else
264             {
265                 if( p_sys->i_last_ref_pts )
266                 {
267                     p_block->i_dts = p_sys->i_last_ref_pts;
268                 }
269                 else
270                 {
271                     /* Let's put something sensible */
272                     p_block->i_dts = p_block->i_pts;
273                 }
274
275                 p_sys->i_last_ref_pts = p_block->i_pts;
276             }
277         }
278         else
279         {
280             /* Buggy libavcodec which doesn't update coded_frame->pts
281              * correctly */
282             p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
283                                   p_enc->i_frame_rate;
284             p_block->i_dts = p_block->i_pts = p_pict->date;
285         }
286
287         return p_block;
288     }
289
290     return NULL;
291 }
292
293 /*****************************************************************************
294  * CloseVideoEncoder: ffmpeg video encoder destruction
295  *****************************************************************************/
296 void E_(CloseVideoEncoder)( vlc_object_t *p_this )
297 {
298     encoder_t *p_enc = (encoder_t *)p_this;
299     encoder_sys_t *p_sys = p_enc->p_sys;
300
301     avcodec_close( p_sys->p_context );
302     free( p_sys->p_context );
303     free( p_sys->p_buffer_out );
304     free( p_sys );
305 }
306
307 /*****************************************************************************
308  * OpenAudioEncoder: probe the encoder
309  *****************************************************************************/
310 int E_(OpenAudioEncoder)( vlc_object_t *p_this )
311 {
312     encoder_t *p_enc = (encoder_t *)p_this;
313     encoder_sys_t *p_sys;
314     AVCodecContext *p_context;
315     AVCodec *p_codec;
316     int i_codec_id, i_cat;
317     char *psz_namecodec;
318
319     if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
320                              &psz_namecodec ) )
321     {
322         return VLC_EGENERIC;
323     }
324
325     if( i_cat != AUDIO_ES )
326     {
327         msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
328         return VLC_EGENERIC;
329     }
330
331     /* Initialization must be done before avcodec_find_decoder() */
332     E_(InitLibavcodec)(p_this);
333
334     p_codec = avcodec_find_encoder( i_codec_id );
335     if( !p_codec )
336     {
337         msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
338         return VLC_EGENERIC;
339     }
340
341     /* Allocate the memory needed to store the decoder's structure */
342     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
343     {
344         msg_Err( p_enc, "out of memory" );
345         return VLC_EGENERIC;
346     }
347     p_enc->p_sys = p_sys;
348     p_sys->p_codec = p_codec;
349
350     p_enc->pf_header = NULL;
351     p_enc->pf_encode_audio = EncodeAudio;
352     p_enc->format.audio.i_format = VLC_FOURCC('s','1','6','n');
353
354     p_sys->p_context = p_context = avcodec_alloc_context();
355     p_context->bit_rate    = p_enc->i_bitrate;
356     p_context->sample_rate = p_enc->format.audio.i_rate;
357     p_context->channels    =
358         aout_FormatNbChannels( &p_enc->format.audio );
359
360     /* Make sure we get extradata filled by the encoder */
361     p_context->extradata_size = 0;
362     p_context->extradata = NULL;
363     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
364
365     if( avcodec_open( p_context, p_codec ) < 0 )
366     {
367         if( p_context->channels > 2 )
368         {
369             p_context->channels = 2;
370             //id->f_dst.i_channels   = 2;
371             if( avcodec_open( p_context, p_codec ) < 0 )
372             {
373                 msg_Err( p_enc, "cannot open encoder" );
374                 return VLC_EGENERIC;
375             }
376             msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
377         }
378         else
379         {
380             msg_Err( p_enc, "cannot open encoder" );
381             return VLC_EGENERIC;
382         }
383     }
384
385     p_enc->i_extra_data = p_context->extradata_size;
386     p_enc->p_extra_data = p_context->extradata;
387     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
388
389     p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
390     p_sys->p_buffer = malloc( p_sys->i_frame_size );
391     p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
392
393     msg_Warn( p_enc, "avcodec_setup_audio: %d %d %d %d",
394               p_context->frame_size, p_context->bit_rate, p_context->channels,
395               p_context->sample_rate );
396
397     p_sys->i_samples_delay = 0;
398     p_sys->i_pts = 0;
399
400     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
401
402     return VLC_SUCCESS;
403 }
404
405 /****************************************************************************
406  * EncodeAudio: the whole thing
407  ****************************************************************************/
408 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
409 {
410     encoder_sys_t *p_sys = p_enc->p_sys;
411     block_t *p_block, *p_chain = NULL;
412
413     char *p_buffer = p_aout_buf->p_buffer;
414     int i_samples = p_aout_buf->i_nb_samples;
415     int i_samples_delay = p_sys->i_samples_delay;
416
417     p_sys->i_pts = p_aout_buf->start_date -
418                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
419                 (mtime_t)p_enc->format.audio.i_rate;
420
421     p_sys->i_samples_delay += i_samples;
422
423     while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
424     {
425         int16_t *p_samples;
426         int i_out;
427
428         if( i_samples_delay )
429         {
430             /* Take care of the left-over from last time */
431             int i_delay_size = i_samples_delay * 2 *
432                                  p_sys->p_context->channels;
433             int i_size = p_sys->i_frame_size - i_delay_size;
434
435             p_samples = (int16_t *)p_sys->p_buffer;
436             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
437             p_buffer -= i_delay_size;
438             i_samples += i_samples_delay;
439             i_samples_delay = 0;
440         }
441         else
442         {
443             p_samples = (int16_t *)p_buffer;
444         }
445
446         i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
447                                       2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
448                                       p_samples );
449
450 #if 0
451         msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
452 #endif
453         if( i_out < 0 ) break;
454
455         p_buffer += p_sys->i_frame_size;
456         p_sys->i_samples_delay -= p_sys->p_context->frame_size;
457         i_samples -= p_sys->p_context->frame_size;
458
459         if( i_out == 0 ) continue;
460
461         p_block = block_New( p_enc, i_out );
462         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
463
464         p_block->i_length = (mtime_t)1000000 *
465             (mtime_t)p_sys->p_context->frame_size /
466             (mtime_t)p_sys->p_context->sample_rate;
467
468         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
469
470         /* Update pts */
471         p_sys->i_pts += p_block->i_length;
472         block_ChainAppend( &p_chain, p_block );
473     }
474
475     /* Backup the remaining raw samples */
476     if( i_samples )
477     {
478         memcpy( p_sys->p_buffer, p_buffer + i_samples_delay * 2 *
479                 p_sys->p_context->channels,
480                 i_samples * 2 * p_sys->p_context->channels );
481     }
482
483     return p_chain;
484 }
485
486 /*****************************************************************************
487  * CloseAudioEncoder: ffmpeg audio encoder destruction
488  *****************************************************************************/
489 void E_(CloseAudioEncoder)( vlc_object_t *p_this )
490 {
491     encoder_t *p_enc = (encoder_t *)p_this;
492     encoder_sys_t *p_sys = p_enc->p_sys;
493
494     avcodec_close( p_sys->p_context );
495     free( p_sys->p_context );
496     free( p_sys->p_buffer );
497     free( p_sys->p_buffer_out );
498     free( p_sys );
499 }