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.19 2003/12/07 12:11:13 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 *****************************************************************************/
31 #include <vlc/decoder.h>
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 # include <ffmpeg/avcodec.h>
43 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
44 #define HURRY_UP_GUARD (200000)
46 /*****************************************************************************
48 *****************************************************************************/
49 int E_(OpenEncoder) ( vlc_object_t * );
50 void E_(CloseEncoder)( vlc_object_t * );
52 static block_t *EncodeVideo( encoder_t *, picture_t * );
53 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
55 /*****************************************************************************
56 * encoder_sys_t : ffmpeg encoder descriptor
57 *****************************************************************************/
64 AVCodecContext *p_context;
75 mtime_t i_last_ref_pts;
76 mtime_t i_buggy_pts_detect;
86 /*****************************************************************************
87 * OpenEncoder: probe the encoder
88 *****************************************************************************/
89 int E_(OpenEncoder)( vlc_object_t *p_this )
91 encoder_t *p_enc = (encoder_t *)p_this;
92 encoder_sys_t *p_sys = p_enc->p_sys;
93 AVCodecContext *p_context;
95 int i_codec_id, i_cat;
98 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
101 if( E_(GetFfmpegChroma)( p_enc->fmt_out.i_codec ) < 0 )
103 /* handed chroma output */
107 i_codec_id = CODEC_ID_RAWVIDEO;
108 psz_namecodec = "Raw video";
112 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
114 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
118 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
120 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
124 /* Initialization must be done before avcodec_find_decoder() */
125 E_(InitLibavcodec)(p_this);
127 p_codec = avcodec_find_encoder( i_codec_id );
130 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
134 /* Allocate the memory needed to store the decoder's structure */
135 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
137 msg_Err( p_enc, "out of memory" );
140 p_enc->p_sys = p_sys;
141 p_sys->p_codec = p_codec;
143 p_enc->pf_encode_video = EncodeVideo;
144 p_enc->pf_encode_audio = EncodeAudio;
146 p_sys->p_buffer_out = NULL;
147 p_sys->p_buffer = NULL;
149 p_sys->p_context = p_context = avcodec_alloc_context();
151 /* Set CPU capabilities */
152 p_context->dsp_mask = 0;
153 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
155 p_context->dsp_mask |= FF_MM_MMX;
157 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
159 p_context->dsp_mask |= FF_MM_MMXEXT;
161 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
163 p_context->dsp_mask |= FF_MM_3DNOW;
165 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
167 p_context->dsp_mask |= FF_MM_SSE;
168 p_context->dsp_mask |= FF_MM_SSE2;
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;
176 if( p_enc->fmt_in.i_cat == VIDEO_ES )
178 p_context->width = p_enc->fmt_in.video.i_width;
179 p_context->height = p_enc->fmt_in.video.i_height;
181 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
182 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
184 #if LIBAVCODEC_BUILD >= 4687
185 p_context->sample_aspect_ratio =
186 (AVRational){ p_enc->fmt_in.video.i_aspect *
187 (int64_t)p_context->height / p_context->width,
188 VOUT_ASPECT_FACTOR };
190 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
194 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
196 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
198 if ( p_enc->b_strict_rc )
200 p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
201 p_context->rc_buffer_size = p_context->bit_rate / 2;
202 p_context->rc_buffer_aggressivity = 1000.0; /* FIXME */
205 if ( p_enc->b_pre_me )
207 p_context->pre_me = 1;
208 p_context->me_pre_cmp = FF_CMP_CHROMA;
211 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
213 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
214 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
215 p_context->channels = p_enc->fmt_in.audio.i_channels;
216 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
217 p_sys->p_buffer = malloc( p_sys->i_frame_size );
218 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
221 /* Misc parameters */
222 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
223 p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
224 p_context->max_b_frames =
225 __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
226 p_context->b_frame_strategy = 0;
227 p_context->b_quant_factor = 2.0;
229 if( p_enc->i_vtolerance > 0 )
231 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
233 p_context->qmin = p_enc->i_qmin;
234 p_context->qmax = p_enc->i_qmax;
236 p_context->mb_decision = p_enc->i_hq;
238 if( i_codec_id == CODEC_ID_RAWVIDEO )
240 /* XXX: hack: Force same codec (will be handled by transcode) */
241 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
242 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
245 /* Make sure we get extradata filled by the encoder */
246 p_context->extradata_size = 0;
247 p_context->extradata = NULL;
248 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
250 if( avcodec_open( p_context, p_codec ) )
252 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
254 p_context->channels = 2;
255 p_enc->fmt_in.audio.i_channels = 2; // FIXME
256 if( avcodec_open( p_context, p_codec ) )
258 msg_Err( p_enc, "cannot open encoder" );
261 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
265 msg_Err( p_enc, "cannot open encoder" );
270 p_enc->fmt_out.i_extra = p_context->extradata_size;
271 p_enc->fmt_out.p_extra = p_context->extradata;
272 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
274 if( p_enc->fmt_in.i_cat == AUDIO_ES )
276 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
277 p_sys->p_buffer = malloc( p_sys->i_frame_size );
280 p_sys->i_last_ref_pts = 0;
281 p_sys->i_buggy_pts_detect = 0;
282 p_sys->i_samples_delay = 0;
285 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
290 /****************************************************************************
291 * EncodeVideo: the whole thing
292 ****************************************************************************/
293 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
295 encoder_sys_t *p_sys = p_enc->p_sys;
298 vlc_bool_t b_hurry_up = 0;
300 memset( &frame, 0, sizeof( AVFrame ) );
301 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
303 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
304 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
307 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
308 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ||
309 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) ||
310 p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
312 frame.pts = p_pict->date;
313 if ( frame.pts && mdate() + HURRY_UP_GUARD > frame.pts
314 && p_enc->b_hurry_up )
316 msg_Dbg( p_enc, "hurry up mode" );
317 p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
326 /* Let ffmpeg select the frame type */
328 frame.repeat_pict = p_pict->i_nb_fields;
330 #if LIBAVCODEC_BUILD >= 4684
331 frame.interlaced_frame = !p_pict->b_progressive;
332 frame.top_field_first = p_pict->b_top_field_first;
335 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
336 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
340 p_sys->p_context->mb_decision = p_enc->i_hq;
345 block_t *p_block = block_New( p_enc, i_out );
346 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
348 if( p_sys->p_context->coded_frame->pts != 0 &&
349 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
351 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
353 /* FIXME, 3-2 pulldown is not handled correctly */
354 p_block->i_length = I64C(1000000) *
355 p_enc->fmt_in.video.i_frame_rate_base /
356 p_enc->fmt_in.video.i_frame_rate;
357 p_block->i_pts = p_sys->p_context->coded_frame->pts;
359 if( !p_sys->p_context->delay ||
360 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
361 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
363 p_block->i_dts = p_block->i_pts;
367 if( p_sys->i_last_ref_pts )
369 p_block->i_dts = p_sys->i_last_ref_pts;
373 /* Let's put something sensible */
374 p_block->i_dts = p_block->i_pts;
377 p_sys->i_last_ref_pts = p_block->i_pts;
382 /* Buggy libavcodec which doesn't update coded_frame->pts
384 p_block->i_length = I64C(1000000) *
385 p_enc->fmt_in.video.i_frame_rate_base /
386 p_enc->fmt_in.video.i_frame_rate;
387 p_block->i_dts = p_block->i_pts = p_pict->date;
396 /****************************************************************************
397 * EncodeAudio: the whole thing
398 ****************************************************************************/
399 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
401 encoder_sys_t *p_sys = p_enc->p_sys;
402 block_t *p_block, *p_chain = NULL;
404 char *p_buffer = p_aout_buf->p_buffer;
405 int i_samples = p_aout_buf->i_nb_samples;
406 int i_samples_delay = p_sys->i_samples_delay;
408 p_sys->i_pts = p_aout_buf->start_date -
409 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
410 (mtime_t)p_enc->fmt_in.audio.i_rate;
412 p_sys->i_samples_delay += i_samples;
414 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
419 if( i_samples_delay )
421 /* Take care of the left-over from last time */
422 int i_delay_size = i_samples_delay * 2 *
423 p_sys->p_context->channels;
424 int i_size = p_sys->i_frame_size - i_delay_size;
426 p_samples = (int16_t *)p_sys->p_buffer;
427 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
428 p_buffer -= i_delay_size;
429 i_samples += i_samples_delay;
434 p_samples = (int16_t *)p_buffer;
437 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
438 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
442 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
444 if( i_out < 0 ) break;
446 p_buffer += p_sys->i_frame_size;
447 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
448 i_samples -= p_sys->p_context->frame_size;
450 if( i_out == 0 ) continue;
452 p_block = block_New( p_enc, i_out );
453 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
455 p_block->i_length = (mtime_t)1000000 *
456 (mtime_t)p_sys->p_context->frame_size /
457 (mtime_t)p_sys->p_context->sample_rate;
459 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
462 p_sys->i_pts += p_block->i_length;
463 block_ChainAppend( &p_chain, p_block );
466 /* Backup the remaining raw samples */
469 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
470 p_sys->p_context->channels, p_buffer,
471 i_samples * 2 * p_sys->p_context->channels );
477 /*****************************************************************************
478 * CloseEncoder: ffmpeg encoder destruction
479 *****************************************************************************/
480 void E_(CloseEncoder)( vlc_object_t *p_this )
482 encoder_t *p_enc = (encoder_t *)p_this;
483 encoder_sys_t *p_sys = p_enc->p_sys;
485 avcodec_close( p_sys->p_context );
486 free( p_sys->p_context );
488 if( p_sys->p_buffer ) free( p_sys->p_buffer );
489 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );