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 $
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;
75 * Packetizer output properties
77 sout_packetizer_input_t *p_sout_input;
78 sout_format_t sout_format;
89 mtime_t i_last_ref_pts;
90 mtime_t i_buggy_pts_detect;
100 /*****************************************************************************
101 * OpenVideoEncoder: probe the encoder
102 *****************************************************************************/
103 int E_(OpenVideoEncoder)( vlc_object_t *p_this )
105 encoder_t *p_enc = (encoder_t *)p_this;
106 encoder_sys_t *p_sys = p_enc->p_sys;
107 AVCodecContext *p_context;
109 int i_codec_id, i_cat;
112 if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
118 if( i_cat != VIDEO_ES )
120 msg_Err( p_enc, "\"%s\" is not a video 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_header = NULL;
144 p_enc->pf_encode_video = EncodeVideo;
145 p_enc->format.video.i_chroma = VLC_FOURCC('I','4','2','0');
147 if( p_enc->i_fourcc == VLC_FOURCC( 'm','p','1','v' ) ||
148 p_enc->i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
150 p_enc->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
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;
158 p_context->frame_rate = p_enc->i_frame_rate;
159 p_context->frame_rate_base= p_enc->i_frame_rate_base;
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;
167 if( p_enc->i_vtolerance >= 0 )
169 p_context->bit_rate_tolerance = p_enc->i_vtolerance;
171 p_context->qmin = p_enc->i_qmin;
172 p_context->qmax = p_enc->i_qmax;
174 #if LIBAVCODEC_BUILD >= 4673
175 p_context->mb_decision = p_enc->i_hq;
179 p_context->flags |= CODEC_FLAG_HQ;
183 if( i_codec_id == CODEC_ID_RAWVIDEO )
185 p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->i_fourcc );
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;
193 if( avcodec_open( p_context, p_sys->p_codec ) )
195 msg_Err( p_enc, "cannot open encoder" );
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;
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;
207 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
212 /****************************************************************************
213 * EncodeVideo: the whole thing
214 ****************************************************************************/
215 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
217 encoder_sys_t *p_sys = p_enc->p_sys;
221 for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
223 frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
224 frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
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;
233 /* Let ffmpeg select the frame type */
236 i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
237 AVCODEC_MAX_VIDEO_FRAME_SIZE, &frame );
240 block_t *p_block = block_New( p_enc, i_out );
241 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
243 if( p_sys->p_context->coded_frame->pts != 0 &&
244 p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
246 p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
248 /* FIXME, 3-2 pulldown is not handled correctly */
249 p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
251 p_block->i_pts = p_sys->p_context->coded_frame->pts;
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 ) )
257 p_block->i_dts = p_block->i_pts;
261 if( p_sys->i_last_ref_pts )
263 p_block->i_dts = p_sys->i_last_ref_pts;
267 /* Let's put something sensible */
268 p_block->i_dts = p_block->i_pts;
271 p_sys->i_last_ref_pts = p_block->i_pts;
276 /* Buggy libavcodec which doesn't update coded_frame->pts
278 p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
280 p_block->i_dts = p_block->i_pts = p_pict->date;
289 /*****************************************************************************
290 * CloseVideoEncoder: ffmpeg video encoder destruction
291 *****************************************************************************/
292 void E_(CloseVideoEncoder)( vlc_object_t *p_this )
294 encoder_t *p_enc = (encoder_t *)p_this;
295 encoder_sys_t *p_sys = p_enc->p_sys;
297 avcodec_close( p_sys->p_context );
298 free( p_sys->p_context );
299 free( p_sys->p_buffer_out );
303 /*****************************************************************************
304 * OpenAudioEncoder: probe the encoder
305 *****************************************************************************/
306 int E_(OpenAudioEncoder)( vlc_object_t *p_this )
308 encoder_t *p_enc = (encoder_t *)p_this;
309 encoder_sys_t *p_sys;
310 AVCodecContext *p_context;
312 int i_codec_id, i_cat;
315 if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
321 if( i_cat != AUDIO_ES )
323 msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
327 /* Initialization must be done before avcodec_find_decoder() */
328 E_(InitLibavcodec)(p_this);
330 p_codec = avcodec_find_encoder( i_codec_id );
333 msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
337 /* Allocate the memory needed to store the decoder's structure */
338 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
340 msg_Err( p_enc, "out of memory" );
343 p_enc->p_sys = p_sys;
344 p_sys->p_codec = p_codec;
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');
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 );
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;
361 if( avcodec_open( p_context, p_codec ) < 0 )
363 if( p_context->channels > 2 )
365 p_context->channels = 2;
366 //id->f_dst.i_channels = 2;
367 if( avcodec_open( p_context, p_codec ) < 0 )
369 msg_Err( p_enc, "cannot open encoder" );
372 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
376 msg_Err( p_enc, "cannot open encoder" );
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;
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 );
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 );
393 p_sys->i_samples_delay = 0;
396 msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
401 /****************************************************************************
402 * EncodeAudio: the whole thing
403 ****************************************************************************/
404 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
406 encoder_sys_t *p_sys = p_enc->p_sys;
407 block_t *p_block, *p_chain = NULL;
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;
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;
417 p_sys->i_samples_delay += i_samples;
419 while( p_sys->i_samples_delay >= p_sys->p_context->frame_size )
424 if( i_samples_delay )
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;
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;
439 p_samples = (int16_t *)p_buffer;
442 i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
443 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
447 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
449 if( i_out < 0 ) break;
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;
455 if( i_out == 0 ) continue;
457 p_block = block_New( p_enc, i_out );
458 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
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;
464 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
467 p_sys->i_pts += p_block->i_length;
468 block_ChainAppend( &p_chain, p_block );
471 /* Backup the remaining raw samples */
474 memcpy( p_sys->p_buffer, p_buffer + i_samples_delay,
475 i_samples * 2 * p_sys->p_context->channels );
481 /*****************************************************************************
482 * CloseAudioEncoder: ffmpeg audio encoder destruction
483 *****************************************************************************/
484 void E_(CloseAudioEncoder)( vlc_object_t *p_this )
486 encoder_t *p_enc = (encoder_t *)p_this;
487 encoder_sys_t *p_sys = p_enc->p_sys;
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 );