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.4 2003/11/05 17:46:21 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include <vlc/decoder.h>
35 #include <vlc/input.h>
38 #include "aout_internal.h"
41 #ifdef HAVE_FFMPEG_AVCODEC_H
42 # include <ffmpeg/avcodec.h>
49 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
51 /*****************************************************************************
53 *****************************************************************************/
54 int E_(OpenVideoEncoder) ( vlc_object_t * );
55 void E_(CloseVideoEncoder)( vlc_object_t * );
57 int E_(OpenAudioEncoder) ( vlc_object_t * );
58 void E_(CloseAudioEncoder)( vlc_object_t * );
60 static block_t *EncodeVideo( encoder_t *, picture_t * );
61 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
63 /*****************************************************************************
64 * encoder_sys_t : ffmpeg encoder descriptor
65 *****************************************************************************/
72 AVCodecContext *p_context;
83 mtime_t i_last_ref_pts;
84 mtime_t i_buggy_pts_detect;
94 /*****************************************************************************
95 * OpenVideoEncoder: probe the encoder
96 *****************************************************************************/
97 int E_(OpenVideoEncoder)( vlc_object_t *p_this )
99 encoder_t *p_enc = (encoder_t *)p_this;
100 encoder_sys_t *p_sys = p_enc->p_sys;
101 AVCodecContext *p_context;
103 int i_codec_id, i_cat;
106 if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
112 if( i_cat != VIDEO_ES )
114 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
118 /* Initialization must be done before avcodec_find_decoder() */
119 E_(InitLibavcodec)(p_this);
121 p_codec = avcodec_find_encoder( i_codec_id );
124 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
128 /* Allocate the memory needed to store the decoder's structure */
129 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
131 msg_Err( p_enc, "out of memory" );
134 p_enc->p_sys = p_sys;
135 p_sys->p_codec = p_codec;
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');
141 if( p_enc->i_fourcc == VLC_FOURCC( 'm','p','1','v' ) ||
142 p_enc->i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
144 p_enc->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
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;
152 p_context->frame_rate = p_enc->i_frame_rate;
153 p_context->frame_rate_base= p_enc->i_frame_rate_base;
155 p_context->gop_size = p_enc->i_key_int >= 0 ? p_enc->i_key_int : 50;
156 p_context->max_b_frames =
157 __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
158 p_context->b_frame_strategy = 0;
159 p_context->b_quant_factor = 2.0;
161 if( p_enc->i_vtolerance >= 0 )
163 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
165 p_context->qmin = p_enc->i_qmin;
166 p_context->qmax = p_enc->i_qmax;
168 #if LIBAVCODEC_BUILD >= 4673
169 p_context->mb_decision = p_enc->i_hq;
173 p_context->flags |= CODEC_FLAG_HQ;
177 if( i_codec_id == CODEC_ID_RAWVIDEO )
179 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->i_fourcc );
182 /* Make sure we get extradata filled by the encoder */
183 p_context->extradata_size = 0;
184 p_context->extradata = NULL;
185 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
187 if( avcodec_open( p_context, p_sys->p_codec ) )
189 msg_Err( p_enc, "cannot open encoder" );
193 p_enc->i_extra_data = p_context->extradata_size;
194 p_enc->p_extra_data = p_context->extradata;
195 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
197 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
198 p_sys->i_last_ref_pts = 0;
199 p_sys->i_buggy_pts_detect = 0;
201 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
206 /****************************************************************************
207 * EncodeVideo: the whole thing
208 ****************************************************************************/
209 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
211 encoder_sys_t *p_sys = p_enc->p_sys;
215 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
217 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
218 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
221 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
222 if( p_enc->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
223 frame.pts = p_pict->date;
227 /* Let ffmpeg select the frame type */
230 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
231 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
234 block_t *p_block = block_New( p_enc, i_out );
235 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
237 if( p_sys->p_context->coded_frame->pts != 0 &&
238 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
240 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
242 /* FIXME, 3-2 pulldown is not handled correctly */
243 p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
245 p_block->i_pts = p_sys->p_context->coded_frame->pts;
247 if( !p_sys->p_context->delay ||
248 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
249 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
251 p_block->i_dts = p_block->i_pts;
255 if( p_sys->i_last_ref_pts )
257 p_block->i_dts = p_sys->i_last_ref_pts;
261 /* Let's put something sensible */
262 p_block->i_dts = p_block->i_pts;
265 p_sys->i_last_ref_pts = p_block->i_pts;
270 /* Buggy libavcodec which doesn't update coded_frame->pts
272 p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
274 p_block->i_dts = p_block->i_pts = p_pict->date;
283 /*****************************************************************************
284 * CloseVideoEncoder: ffmpeg video encoder destruction
285 *****************************************************************************/
286 void E_(CloseVideoEncoder)( vlc_object_t *p_this )
288 encoder_t *p_enc = (encoder_t *)p_this;
289 encoder_sys_t *p_sys = p_enc->p_sys;
291 avcodec_close( p_sys->p_context );
292 free( p_sys->p_context );
293 free( p_sys->p_buffer_out );
297 /*****************************************************************************
298 * OpenAudioEncoder: probe the encoder
299 *****************************************************************************/
300 int E_(OpenAudioEncoder)( vlc_object_t *p_this )
302 encoder_t *p_enc = (encoder_t *)p_this;
303 encoder_sys_t *p_sys;
304 AVCodecContext *p_context;
306 int i_codec_id, i_cat;
309 if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
315 if( i_cat != AUDIO_ES )
317 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
321 /* Initialization must be done before avcodec_find_decoder() */
322 E_(InitLibavcodec)(p_this);
324 p_codec = avcodec_find_encoder( i_codec_id );
327 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
331 /* Allocate the memory needed to store the decoder's structure */
332 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
334 msg_Err( p_enc, "out of memory" );
337 p_enc->p_sys = p_sys;
338 p_sys->p_codec = p_codec;
340 p_enc->pf_header = NULL;
341 p_enc->pf_encode_audio = EncodeAudio;
342 p_enc->format.audio.i_format = VLC_FOURCC('s','1','6','n');
344 p_sys->p_context = p_context = avcodec_alloc_context();
345 p_context->bit_rate = p_enc->i_bitrate;
346 p_context->sample_rate = p_enc->format.audio.i_rate;
347 p_context->channels =
348 aout_FormatNbChannels( &p_enc->format.audio );
350 /* Make sure we get extradata filled by the encoder */
351 p_context->extradata_size = 0;
352 p_context->extradata = NULL;
353 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
355 if( avcodec_open( p_context, p_codec ) < 0 )
357 if( p_context->channels > 2 )
359 p_context->channels = 2;
360 //id->f_dst.i_channels = 2;
361 if( avcodec_open( p_context, p_codec ) < 0 )
363 msg_Err( p_enc, "cannot open encoder" );
366 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
370 msg_Err( p_enc, "cannot open encoder" );
375 p_enc->i_extra_data = p_context->extradata_size;
376 p_enc->p_extra_data = p_context->extradata;
377 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
379 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
380 p_sys->p_buffer = malloc( p_sys->i_frame_size );
381 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
383 msg_Warn( p_enc, "avcodec_setup_audio: %d %d %d %d",
384 p_context->frame_size, p_context->bit_rate, p_context->channels,
385 p_context->sample_rate );
387 p_sys->i_samples_delay = 0;
390 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
395 /****************************************************************************
396 * EncodeAudio: the whole thing
397 ****************************************************************************/
398 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
400 encoder_sys_t *p_sys = p_enc->p_sys;
401 block_t *p_block, *p_chain = NULL;
403 char *p_buffer = p_aout_buf->p_buffer;
404 int i_samples = p_aout_buf->i_nb_samples;
405 int i_samples_delay = p_sys->i_samples_delay;
407 p_sys->i_pts = p_aout_buf->start_date -
408 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
409 (mtime_t)p_enc->format.audio.i_rate;
411 p_sys->i_samples_delay += i_samples;
413 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
418 if( i_samples_delay )
420 /* Take care of the left-over from last time */
421 int i_delay_size = i_samples_delay * 2 *
422 p_sys->p_context->channels;
423 int i_size = p_sys->i_frame_size - i_delay_size;
425 p_samples = (int16_t *)p_sys->p_buffer;
426 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
427 p_buffer -= i_delay_size;
428 i_samples += i_samples_delay;
433 p_samples = (int16_t *)p_buffer;
436 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
437 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
441 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
443 if( i_out < 0 ) break;
445 p_buffer += p_sys->i_frame_size;
446 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
447 i_samples -= p_sys->p_context->frame_size;
449 if( i_out == 0 ) continue;
451 p_block = block_New( p_enc, i_out );
452 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
454 p_block->i_length = (mtime_t)1000000 *
455 (mtime_t)p_sys->p_context->frame_size /
456 (mtime_t)p_sys->p_context->sample_rate;
458 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
461 p_sys->i_pts += p_block->i_length;
462 block_ChainAppend( &p_chain, p_block );
465 /* Backup the remaining raw samples */
468 memcpy( p_sys->p_buffer, p_buffer + i_samples_delay * 2 *
469 p_sys->p_context->channels,
470 i_samples * 2 * p_sys->p_context->channels );
476 /*****************************************************************************
477 * CloseAudioEncoder: ffmpeg audio encoder destruction
478 *****************************************************************************/
479 void E_(CloseAudioEncoder)( vlc_object_t *p_this )
481 encoder_t *p_enc = (encoder_t *)p_this;
482 encoder_sys_t *p_sys = p_enc->p_sys;
484 avcodec_close( p_sys->p_context );
485 free( p_sys->p_context );
486 free( p_sys->p_buffer );
487 free( p_sys->p_buffer_out );