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.7 2003/11/16 21:07:31 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_(OpenEncoder) ( vlc_object_t * );
55 void E_(CloseEncoder)( vlc_object_t * );
57 static block_t *EncodeVideo( encoder_t *, picture_t * );
58 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
60 /*****************************************************************************
61 * encoder_sys_t : ffmpeg encoder descriptor
62 *****************************************************************************/
69 AVCodecContext *p_context;
80 mtime_t i_last_ref_pts;
81 mtime_t i_buggy_pts_detect;
91 /*****************************************************************************
92 * OpenEncoder: probe the encoder
93 *****************************************************************************/
94 int E_(OpenEncoder)( vlc_object_t *p_this )
96 encoder_t *p_enc = (encoder_t *)p_this;
97 encoder_sys_t *p_sys = p_enc->p_sys;
98 AVCodecContext *p_context;
100 int i_codec_id, i_cat;
103 if( !E_(GetFfmpegCodec)( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
109 if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
111 msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
115 if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
117 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
121 /* Initialization must be done before avcodec_find_decoder() */
122 E_(InitLibavcodec)(p_this);
124 p_codec = avcodec_find_encoder( i_codec_id );
127 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
131 /* Allocate the memory needed to store the decoder's structure */
132 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
134 msg_Err( p_enc, "out of memory" );
137 p_enc->p_sys = p_sys;
138 p_sys->p_codec = p_codec;
140 p_enc->pf_encode_video = EncodeVideo;
141 p_enc->pf_encode_audio = EncodeAudio;
143 p_sys->p_buffer_out = NULL;
144 p_sys->p_buffer = NULL;
146 p_sys->p_context = p_context = avcodec_alloc_context();
148 /* Set CPU capabilities */
150 p_context->dsp_mask = 0;
151 if( p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
153 p_context->dsp_mask &= FF_MM_MMX;
155 if( p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
157 p_context->dsp_mask &= FF_MM_MMXEXT;
159 if( p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
161 p_context->dsp_mask &= FF_MM_3DNOW;
163 if( p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE )
165 p_context->dsp_mask &= FF_MM_SSE;
166 p_context->dsp_mask &= FF_MM_SSE2; /* FIXME */
168 /* Hack to make sure everything can be disabled **/
169 p_context->dsp_mask &= (FF_MM_FORCE >> 1);
172 /* Make sure we get extradata filled by the encoder */
173 p_context->extradata_size = 0;
174 p_context->extradata = NULL;
175 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
177 if( p_enc->fmt_in.i_cat == VIDEO_ES )
179 p_context->width = p_enc->fmt_in.video.i_width;
180 p_context->height = p_enc->fmt_in.video.i_height;
182 p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
183 #if LIBAVCODEC_BUILD >= 4662
184 p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
187 #if LIBAVCODEC_BUILD >= 4687
188 p_context->sample_aspect_ratio =
189 av_d2q( p_enc->fmt_in.video.i_aspect * p_context->height /
190 p_context->width / VOUT_ASPECT_FACTOR, 255 );
192 p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
196 p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
198 /* Ffmpeg does handle the conversion itself */
199 //p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
201 else if( p_enc->fmt_in.i_cat == AUDIO_ES )
203 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
204 p_context->bit_rate = p_enc->fmt_out.i_bitrate;
205 p_context->sample_rate = p_enc->fmt_in.audio.i_rate;
206 p_context->channels = p_enc->fmt_in.audio.i_channels;
207 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
208 p_sys->p_buffer = malloc( p_sys->i_frame_size );
209 p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
212 /* Misc parameters */
213 p_context->gop_size = p_enc->i_key_int > 0 ? p_enc->i_key_int : 50;
214 p_context->max_b_frames =
215 __MIN( p_enc->i_b_frames, FF_MAX_B_FRAMES );
216 p_context->b_frame_strategy = 0;
217 p_context->b_quant_factor = 2.0;
219 if( p_enc->i_vtolerance > 0 )
221 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
223 p_context->qmin = p_enc->i_qmin;
224 p_context->qmax = p_enc->i_qmax;
226 #if LIBAVCODEC_BUILD >= 4673
227 p_context->mb_decision = p_enc->i_hq;
231 p_context->flags |= CODEC_FLAG_HQ;
235 if( i_codec_id == CODEC_ID_RAWVIDEO )
237 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
240 /* Make sure we get extradata filled by the encoder */
241 p_context->extradata_size = 0;
242 p_context->extradata = NULL;
243 p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
245 if( avcodec_open( p_context, p_codec ) )
247 if( p_enc->fmt_in.i_cat == AUDIO_ES && p_context->channels > 2 )
249 p_context->channels = 2;
250 p_enc->fmt_in.audio.i_channels = 2; // FIXME
251 if( avcodec_open( p_context, p_codec ) )
253 msg_Err( p_enc, "cannot open encoder" );
256 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
260 msg_Err( p_enc, "cannot open encoder" );
265 p_enc->fmt_out.i_extra = p_context->extradata_size;
266 p_enc->fmt_out.p_extra = p_context->extradata;
267 p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
269 if( p_enc->fmt_in.i_cat == AUDIO_ES )
271 p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
272 p_sys->p_buffer = malloc( p_sys->i_frame_size );
275 p_sys->i_last_ref_pts = 0;
276 p_sys->i_buggy_pts_detect = 0;
277 p_sys->i_samples_delay = 0;
280 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
285 /****************************************************************************
286 * EncodeVideo: the whole thing
287 ****************************************************************************/
288 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
290 encoder_sys_t *p_sys = p_enc->p_sys;
294 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
296 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
297 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
300 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
301 if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
302 frame.pts = p_pict->date;
306 /* Let ffmpeg select the frame type */
309 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
310 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
313 block_t *p_block = block_New( p_enc, i_out );
314 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
316 if( p_sys->p_context->coded_frame->pts != 0 &&
317 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
319 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
321 /* FIXME, 3-2 pulldown is not handled correctly */
322 p_block->i_length = I64C(1000000) *
323 p_enc->fmt_in.video.i_frame_rate_base /
324 p_enc->fmt_in.video.i_frame_rate;
325 p_block->i_pts = p_sys->p_context->coded_frame->pts;
327 if( !p_sys->p_context->delay ||
328 ( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
329 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) )
331 p_block->i_dts = p_block->i_pts;
335 if( p_sys->i_last_ref_pts )
337 p_block->i_dts = p_sys->i_last_ref_pts;
341 /* Let's put something sensible */
342 p_block->i_dts = p_block->i_pts;
345 p_sys->i_last_ref_pts = p_block->i_pts;
350 /* Buggy libavcodec which doesn't update coded_frame->pts
352 p_block->i_length = I64C(1000000) *
353 p_enc->fmt_in.video.i_frame_rate_base /
354 p_enc->fmt_in.video.i_frame_rate;
355 p_block->i_dts = p_block->i_pts = p_pict->date;
364 /****************************************************************************
365 * EncodeAudio: the whole thing
366 ****************************************************************************/
367 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
369 encoder_sys_t *p_sys = p_enc->p_sys;
370 block_t *p_block, *p_chain = NULL;
372 char *p_buffer = p_aout_buf->p_buffer;
373 int i_samples = p_aout_buf->i_nb_samples;
374 int i_samples_delay = p_sys->i_samples_delay;
376 p_sys->i_pts = p_aout_buf->start_date -
377 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
378 (mtime_t)p_enc->fmt_in.audio.i_rate;
380 p_sys->i_samples_delay += i_samples;
382 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
387 if( i_samples_delay )
389 /* Take care of the left-over from last time */
390 int i_delay_size = i_samples_delay * 2 *
391 p_sys->p_context->channels;
392 int i_size = p_sys->i_frame_size - i_delay_size;
394 p_samples = (int16_t *)p_sys->p_buffer;
395 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
396 p_buffer -= i_delay_size;
397 i_samples += i_samples_delay;
402 p_samples = (int16_t *)p_buffer;
405 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
406 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
410 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
412 if( i_out < 0 ) break;
414 p_buffer += p_sys->i_frame_size;
415 p_sys->i_samples_delay -= p_sys->p_context->frame_size;
416 i_samples -= p_sys->p_context->frame_size;
418 if( i_out == 0 ) continue;
420 p_block = block_New( p_enc, i_out );
421 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
423 p_block->i_length = (mtime_t)1000000 *
424 (mtime_t)p_sys->p_context->frame_size /
425 (mtime_t)p_sys->p_context->sample_rate;
427 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
430 p_sys->i_pts += p_block->i_length;
431 block_ChainAppend( &p_chain, p_block );
434 /* Backup the remaining raw samples */
437 memcpy( p_sys->p_buffer, p_buffer + i_samples_delay * 2 *
438 p_sys->p_context->channels,
439 i_samples * 2 * p_sys->p_context->channels );
445 /*****************************************************************************
446 * CloseEncoder: ffmpeg encoder destruction
447 *****************************************************************************/
448 void E_(CloseEncoder)( vlc_object_t *p_this )
450 encoder_t *p_enc = (encoder_t *)p_this;
451 encoder_sys_t *p_sys = p_enc->p_sys;
453 avcodec_close( p_sys->p_context );
454 free( p_sys->p_context );
456 if( p_sys->p_buffer ) free( p_sys->p_buffer );
457 if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out );