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