1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: transcode.c,v 1.75 2004/02/18 13:21:33 fenrir 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 *****************************************************************************/
32 #include <vlc/input.h>
35 #include <vlc/decoder.h>
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 # include <ffmpeg/avcodec.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
51 static int Del ( sout_stream_t *, sout_stream_id_t * );
52 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
54 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
55 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
56 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
58 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
59 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
60 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
62 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
64 static int pi_channels_maps[6] =
67 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
68 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
69 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
70 | AOUT_CHAN_REARRIGHT,
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
72 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
75 /*****************************************************************************
77 *****************************************************************************/
79 set_description( _("Transcode stream output") );
80 set_capability( "sout stream", 50 );
81 add_shortcut( "transcode" );
82 set_callbacks( Open, Close );
85 struct sout_stream_sys_t
89 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
94 vlc_fourcc_t i_vcodec; /* " video " " " " */
105 vlc_bool_t b_deinterlace;
106 vlc_bool_t b_strict_rc;
108 vlc_bool_t b_hurry_up;
116 mtime_t i_output_pts;
119 /*****************************************************************************
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 sout_stream_t *p_stream = (sout_stream_t*)p_this;
125 sout_stream_sys_t *p_sys;
128 p_sys = malloc( sizeof( sout_stream_sys_t ) );
129 memset( p_sys, 0, sizeof(struct sout_stream_sys_t) );
130 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
133 p_sys->i_vtolerance = -1;
134 p_sys->i_key_int = -1;
137 #if LIBAVCODEC_BUILD >= 4673
138 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
140 p_sys->i_hq = VLC_FALSE;
143 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
148 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
150 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
152 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
154 p_sys->i_sample_rate = atoi( val );
156 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
158 p_sys->i_channels = atoi( val );
160 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
162 p_sys->i_abitrate = atoi( val );
163 if( p_sys->i_abitrate < 4000 )
165 p_sys->i_abitrate *= 1000;
169 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
170 p_sys->i_sample_rate, p_sys->i_channels,
171 p_sys->i_abitrate / 1024 );
174 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
179 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
181 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
183 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "scale" ) ) )
185 p_sys->f_scale = atof( val );
187 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
189 p_sys->i_width = atoi( val );
191 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
193 p_sys->i_height = atoi( val );
195 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
197 p_sys->i_vbitrate = atoi( val );
198 if( p_sys->i_vbitrate < 16000 )
200 p_sys->i_vbitrate *= 1000;
203 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
205 p_sys->i_vtolerance = atoi( val );
207 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
209 p_sys->b_deinterlace = VLC_TRUE;
211 if( sout_cfg_find( p_stream->p_cfg, "strict_rc" ) )
213 p_sys->b_strict_rc = VLC_TRUE;
215 if( sout_cfg_find( p_stream->p_cfg, "pre_me" ) )
217 p_sys->b_pre_me = VLC_TRUE;
219 if( sout_cfg_find( p_stream->p_cfg, "hurry_up" ) )
221 p_sys->b_hurry_up = VLC_TRUE;
224 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
226 p_sys->i_crop_top = atoi( val );
228 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
230 p_sys->i_crop_bottom = atoi( val );
232 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
234 p_sys->i_crop_left = atoi( val );
236 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
238 p_sys->i_crop_right = atoi( val );
240 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
242 p_sys->i_key_int = atoi( val );
244 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
246 p_sys->i_b_frames = atoi( val );
248 #if LIBAVCODEC_BUILD >= 4673
249 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
251 if( !strcmp( val, "rd" ) )
253 p_sys->i_hq = FF_MB_DECISION_RD;
255 else if( !strcmp( val, "bits" ) )
257 p_sys->i_hq = FF_MB_DECISION_BITS;
259 else if( !strcmp( val, "simple" ) )
261 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
265 p_sys->i_hq = FF_MB_DECISION_RD;
269 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
271 p_sys->i_hq = VLC_TRUE;
274 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
276 p_sys->i_qmin = atoi( val );
278 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
280 p_sys->i_qmax = atoi( val );
283 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
284 fcc, p_sys->i_width, p_sys->i_height, p_sys->f_scale,
285 p_sys->i_vbitrate / 1024 );
290 msg_Err( p_stream, "cannot create chain" );
295 p_stream->pf_add = Add;
296 p_stream->pf_del = Del;
297 p_stream->pf_send = Send;
298 p_stream->p_sys = p_sys;
301 avcodec_register_all();
303 /* ffmpeg needs some padding at the end of each buffer */
304 p_stream->p_sout->i_padding += FF_INPUT_BUFFER_PADDING_SIZE;
309 /*****************************************************************************
311 *****************************************************************************/
312 static void Close( vlc_object_t * p_this )
314 sout_stream_t *p_stream = (sout_stream_t*)p_this;
315 sout_stream_sys_t *p_sys = p_stream->p_sys;
317 sout_stream_delete( p_sys->p_out );
321 struct sout_stream_id_t
323 vlc_fourcc_t b_transcode;
324 es_format_t f_src; /* only if transcoding */
325 es_format_t f_dst; /* " " " */
326 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
328 /* id of the out stream */
332 encoder_t *p_encoder;
333 vlc_fourcc_t b_enc_inited;
337 AVCodecContext *ff_dec_c;
347 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
348 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
349 AVFrame *p_ff_pic_tmp2; /* to do resample */
351 ImgReSampleContext *p_vresample;
355 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
357 sout_stream_sys_t *p_sys = p_stream->p_sys;
358 sout_stream_id_t *id;
360 id = malloc( sizeof( sout_stream_id_t ) );
363 id->p_encoder = NULL;
365 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
368 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
369 (char*)&p_fmt->i_codec,
370 (char*)&p_sys->i_acodec );
373 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
375 /* create dst format */
376 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
377 id->f_dst.i_id = id->f_src.i_id;
378 id->f_dst.i_group = id->f_src.i_group;
379 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
380 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
381 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
382 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
383 id->f_dst.audio.i_blockalign = 0;
384 id->f_dst.i_extra = 0;
385 id->f_dst.p_extra = NULL;
387 /* build decoder -> filter -> encoder */
388 if( transcode_audio_ffmpeg_new( p_stream, id ) )
390 msg_Err( p_stream, "cannot create audio chain" );
395 /* open output stream */
396 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
397 id->b_transcode = VLC_TRUE;
405 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
408 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
409 (char*)&p_fmt->i_codec,
410 (char*)&p_sys->i_vcodec );
412 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
414 /* create dst format */
415 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
416 id->f_dst.i_id = id->f_src.i_id;
417 id->f_dst.i_group = id->f_src.i_group;
418 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
419 id->f_dst.video.i_width = p_sys->i_width;
420 id->f_dst.video.i_height= p_sys->i_height;
421 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
422 id->f_dst.i_extra = 0;
423 id->f_dst.p_extra = NULL;
425 /* build decoder -> filter -> encoder */
426 if( transcode_video_ffmpeg_new( p_stream, id ) )
428 msg_Err( p_stream, "cannot create video chain" );
433 /* open output stream */
434 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
436 id->b_transcode = VLC_TRUE;
440 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
441 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
442 id->b_transcode = VLC_FALSE;
454 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
456 sout_stream_sys_t *p_sys = p_stream->p_sys;
458 if( id->b_transcode )
460 if( id->f_src.i_cat == AUDIO_ES )
462 transcode_audio_ffmpeg_close( p_stream, id );
464 else if( id->f_src.i_cat == VIDEO_ES )
466 transcode_video_ffmpeg_close( p_stream, id );
470 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
476 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
477 sout_buffer_t *p_buffer )
479 sout_stream_sys_t *p_sys = p_stream->p_sys;
481 if( id->b_transcode )
483 sout_buffer_t *p_buffer_out;
484 if( id->f_src.i_cat == AUDIO_ES )
486 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
489 else if( id->f_src.i_cat == VIDEO_ES )
491 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
492 &p_buffer_out ) != VLC_SUCCESS )
494 sout_BufferDelete( p_stream->p_sout, p_buffer );
498 sout_BufferDelete( p_stream->p_sout, p_buffer );
502 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
506 else if( id->id != NULL )
508 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
512 sout_BufferDelete( p_stream->p_sout, p_buffer );
517 /****************************************************************************
518 * ffmpeg decoder reencocdr part
519 ****************************************************************************/
525 } fourcc_to_ff_code[] =
528 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
529 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
530 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
531 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
532 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
533 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
534 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
535 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
536 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
539 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
540 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
541 #if LIBAVCODEC_BUILD >= 4676
542 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
544 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
545 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
546 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
547 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
548 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
549 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
550 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
551 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
552 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
553 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
554 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
555 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
556 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
557 #if LIBAVCODEC_BUILD >= 4666
558 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
561 /* raw video code, only used for 'encoding' */
562 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
563 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
564 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
565 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
566 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
567 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
568 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
569 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
570 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
571 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
573 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
576 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
580 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
582 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
584 return fourcc_to_ff_code[i].i_ff_codec;
591 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
595 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
596 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
597 case VLC_FOURCC( 'I', '4', '2', '0' ):
598 return PIX_FMT_YUV420P;
599 case VLC_FOURCC( 'I', '4', '2', '2' ):
600 return PIX_FMT_YUV422P;
601 case VLC_FOURCC( 'I', '4', '4', '4' ):
602 return PIX_FMT_YUV444P;
603 case VLC_FOURCC( 'R', 'V', '1', '5' ):
604 return PIX_FMT_RGB555;
605 case VLC_FOURCC( 'R', 'V', '1', '6' ):
606 return PIX_FMT_RGB565;
607 case VLC_FOURCC( 'R', 'V', '2', '4' ):
608 return PIX_FMT_BGR24;
609 case VLC_FOURCC( 'R', 'V', '3', '2' ):
610 return PIX_FMT_RGBA32;
611 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
612 return PIX_FMT_GRAY8;
613 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
614 return PIX_FMT_YUV422;
620 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
624 case PIX_FMT_YUV420P:
625 return VLC_FOURCC('I','4','2','0');
626 case PIX_FMT_YUV422P:
627 return VLC_FOURCC('I','4','2','2');
628 case PIX_FMT_YUV444P:
629 return VLC_FOURCC('I','4','4','4');
632 return VLC_FOURCC('Y','U','Y','2');
635 return VLC_FOURCC('R','V','1','5');
637 return VLC_FOURCC('R','V','1','6');
639 return VLC_FOURCC('R','V','2','4');
641 return VLC_FOURCC('R','V','3','2');
643 return VLC_FOURCC('G','R','E','Y');
645 case PIX_FMT_YUV410P:
646 case PIX_FMT_YUV411P:
653 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
654 sout_stream_id_t *id )
658 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
659 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
660 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
661 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
665 id->ff_dec_c = avcodec_alloc_context();
666 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
667 id->ff_dec_c->channels = id->f_src.audio.i_channels;
668 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
669 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
674 i_ff_codec = get_ff_codec( id->f_src.i_codec );
675 if( i_ff_codec == 0 )
677 msg_Err( p_stream, "cannot find decoder id" );
681 id->ff_dec = avcodec_find_decoder( i_ff_codec );
684 msg_Err( p_stream, "cannot find decoder (avcodec)" );
688 id->ff_dec_c = avcodec_alloc_context();
689 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
690 id->ff_dec_c->channels = id->f_src.audio.i_channels;
691 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
692 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
694 id->ff_dec_c->extradata_size = id->f_src.i_extra;
695 id->ff_dec_c->extradata = id->f_src.p_extra;
696 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
698 msg_Err( p_stream, "cannot open decoder" );
703 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
704 id->i_buffer_pos = 0;
705 id->p_buffer = malloc( id->i_buffer );
707 /* Sanity check for audio channels */
708 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
711 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
713 /* Initialization of encoder format structures */
714 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
715 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
716 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
717 id->p_encoder->fmt_in.audio.i_physical_channels =
718 id->p_encoder->fmt_in.audio.i_original_channels =
719 pi_channels_maps[id->f_dst.audio.i_channels];
720 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
722 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
723 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
724 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
726 id->p_encoder->p_module =
727 module_Need( id->p_encoder, "encoder", NULL );
728 if( !id->p_encoder->p_module )
730 vlc_object_destroy( id->p_encoder );
731 msg_Err( p_stream, "cannot open encoder" );
735 id->b_enc_inited = VLC_FALSE;
737 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
738 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
740 /* Hack for mp3 transcoding support */
741 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
743 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
749 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
750 sout_stream_id_t *id )
754 avcodec_close( id->ff_dec_c );
755 free( id->ff_dec_c );
758 module_Unneed( id->p_encoder, id->p_encoder->p_module );
759 vlc_object_destroy( id->p_encoder );
761 free( id->p_buffer );
764 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
765 sout_stream_id_t *id,
767 sout_buffer_t **out )
769 aout_buffer_t aout_buf;
771 int i_buffer = in->i_size;
772 char *p_buffer = in->p_buffer;
773 id->i_dts = in->i_dts;
778 id->i_buffer_pos = 0;
780 /* decode as much data as possible */
785 i_used = avcodec_decode_audio( id->ff_dec_c,
786 (int16_t*)id->p_buffer, &id->i_buffer_pos,
787 p_buffer, i_buffer );
790 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
795 msg_Warn( p_stream, "error audio decoding");
804 int16_t *sout = (int16_t*)id->p_buffer;
806 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
807 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
809 int8_t *sin = (int8_t*)p_buffer;
810 int i_used = __MIN( id->i_buffer/2, i_buffer );
811 int i_samples = i_used;
813 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
814 while( i_samples > 0 )
816 *sout++ = ( *sin++ ) << 8;
820 while( i_samples > 0 )
822 *sout++ = ( *sin++ - 128 ) << 8;
828 id->i_buffer_pos = i_used * 2;
830 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
831 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
833 int16_t *sin = (int16_t*)p_buffer;
834 int i_used = __MIN( id->i_buffer, i_buffer );
835 int i_samples = i_used / 2;
838 memcpy( sout, sin, i_used );
840 #ifdef WORDS_BIGENDIAN
841 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
843 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
846 uint8_t *dat = (uint8_t*)sout;
848 while( i_samples > 0 )
863 id->i_buffer_pos = i_used;
867 if( id->i_buffer_pos == 0 ) continue;
869 /* Encode as much data as possible */
870 if( !id->b_enc_inited && id->p_encoder->pf_header )
872 p_block = id->p_encoder->pf_header( id->p_encoder );
875 sout_buffer_t *p_out;
876 block_t *p_prev_block = p_block;
878 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
879 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
880 p_out->i_dts = p_out->i_pts = in->i_dts;
882 sout_BufferChain( out, p_out );
884 p_block = p_block->p_next;
885 block_Release( p_prev_block );
888 id->b_enc_inited = VLC_TRUE;
891 aout_buf.p_buffer = id->p_buffer;
892 aout_buf.i_nb_bytes = id->i_buffer_pos;
893 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
894 aout_buf.start_date = id->i_dts;
895 aout_buf.end_date = id->i_dts;
897 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
898 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
900 if( id->f_src.audio.i_channels !=
901 id->p_encoder->fmt_in.audio.i_channels )
906 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
907 static const int translation[7][6] =
908 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
909 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
910 { 0, 1, 0, 0, 0, 0 }, /* 2 */
911 { 1, 2, 0, 0, 0, 0 }, /* 3 */
912 { 1, 3, 2, 0, 0, 0 }, /* 4 */
913 { 1, 3, 4, 2, 0, 0 }, /* 5 */
914 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
916 /* dumb downmixing */
917 for( i = 0; i < aout_buf.i_nb_samples; i++ )
919 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
920 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
922 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
923 p_buffer[i*id->f_src.audio.i_channels+
924 translation[id->f_src.audio.i_channels][j]];
927 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
930 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
933 sout_buffer_t *p_out;
934 block_t *p_prev_block = p_block;
936 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
937 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
938 p_out->i_dts = p_block->i_dts;
939 p_out->i_pts = p_block->i_pts;
940 p_out->i_length = p_block->i_length;
941 sout_BufferChain( out, p_out );
943 p_block = p_block->p_next;
944 block_Release( p_prev_block );
955 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
956 sout_stream_id_t *id )
958 sout_stream_sys_t *p_sys = p_stream->p_sys;
963 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
964 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
965 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
966 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
967 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
968 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
969 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
970 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
971 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
972 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
973 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
976 id->ff_dec_c = avcodec_alloc_context();
977 id->ff_dec_c->width = id->f_src.video.i_width;
978 id->ff_dec_c->height = id->f_src.video.i_height;
979 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
984 i_ff_codec = get_ff_codec( id->f_src.i_codec );
985 if( i_ff_codec == 0 )
987 msg_Err( p_stream, "cannot find decoder" );
991 id->ff_dec = avcodec_find_decoder( i_ff_codec );
994 msg_Err( p_stream, "cannot find decoder" );
998 id->ff_dec_c = avcodec_alloc_context();
999 id->ff_dec_c->width = id->f_src.video.i_width;
1000 id->ff_dec_c->height = id->f_src.video.i_height;
1001 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1002 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1003 id->ff_dec_c->extradata = id->f_src.p_extra;
1004 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1005 id->ff_dec_c->error_resilience= -1;
1006 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1007 id->ff_dec_c->opaque = p_sys;
1009 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1011 msg_Err( p_stream, "cannot open decoder" );
1012 return VLC_EGENERIC;
1015 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1019 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1020 FF_INPUT_BUFFER_PADDING_SIZE );
1022 memcpy( p_vol, id->ff_dec_c->extradata,
1023 id->ff_dec_c->extradata_size );
1024 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1025 FF_INPUT_BUFFER_PADDING_SIZE );
1027 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1028 id->ff_dec_c->extradata,
1029 id->ff_dec_c->extradata_size );
1035 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1037 /* Initialization of encoder format structures */
1038 es_format_Init( &id->p_encoder->fmt_in,
1039 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1041 /* The dimensions will be set properly later on.
1042 * Just put sensible values so we can test if there is an encoder. */
1043 id->p_encoder->fmt_in.video.i_width = 16;
1044 id->p_encoder->fmt_in.video.i_height = 16;
1046 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1047 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1050 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1051 #if LIBAVCODEC_BUILD >= 4662
1052 id->p_encoder->fmt_in.video.i_frame_rate_base =
1053 id->ff_dec_c->frame_rate_base;
1056 #if LIBAVCODEC_BUILD >= 4687
1057 if( id->ff_dec_c->height )
1058 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1059 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1060 id->ff_dec_c->width / id->ff_dec_c->height );
1062 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1063 id->ff_dec_c->aspect_ratio;
1067 /* Check whether a particular aspect ratio was requested */
1068 if( id->f_src.video.i_aspect )
1070 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1071 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1074 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1075 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1076 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1078 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1079 id->p_encoder->i_key_int = p_sys->i_key_int;
1080 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1081 id->p_encoder->i_qmin = p_sys->i_qmin;
1082 id->p_encoder->i_qmax = p_sys->i_qmax;
1083 id->p_encoder->i_hq = p_sys->i_hq;
1084 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1085 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1086 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1088 id->p_ff_pic = avcodec_alloc_frame();
1089 id->p_ff_pic_tmp0 = NULL;
1090 id->p_ff_pic_tmp1 = NULL;
1091 id->p_ff_pic_tmp2 = NULL;
1092 id->p_vresample = NULL;
1094 id->p_encoder->p_module =
1095 module_Need( id->p_encoder, "encoder", NULL );
1097 if( !id->p_encoder->p_module )
1099 vlc_object_destroy( id->p_encoder );
1100 msg_Err( p_stream, "cannot find encoder" );
1101 return VLC_EGENERIC;
1104 /* Close the encoder.
1105 * We'll open it only when we have the first frame */
1106 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1107 id->p_encoder->p_module = NULL;
1109 id->b_enc_inited = VLC_FALSE;
1114 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1115 sout_stream_id_t *id )
1120 avcodec_close( id->ff_dec_c );
1121 free( id->ff_dec_c );
1125 if( id->p_encoder->p_module )
1126 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1127 vlc_object_destroy( id->p_encoder );
1132 free( id->p_ff_pic );
1135 if( id->p_ff_pic_tmp0 )
1137 free( id->p_ff_pic_tmp0->data[0] );
1138 free( id->p_ff_pic_tmp0 );
1140 if( id->p_ff_pic_tmp1)
1142 free( id->p_ff_pic_tmp1->data[0] );
1143 free( id->p_ff_pic_tmp1 );
1145 if( id->p_ff_pic_tmp2)
1147 free( id->p_ff_pic_tmp2->data[0] );
1148 free( id->p_ff_pic_tmp2 );
1150 if( id->p_vresample )
1152 img_resample_close( id->p_vresample );
1156 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1157 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1159 sout_stream_sys_t *p_sys = p_stream->p_sys;
1169 i_data = in->i_size;
1170 p_data = in->p_buffer;
1179 frame = id->p_ff_pic;
1180 p_sys->i_input_pts = in->i_pts;
1183 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1190 avpicture_fill( (AVPicture*)frame, p_data,
1191 id->ff_dec_c->pix_fmt,
1192 id->ff_dec_c->width, id->ff_dec_c->height );
1197 frame->pts = p_sys->i_input_pts;
1202 msg_Warn( p_stream, "error");
1203 return VLC_EGENERIC;
1213 /* Get the pts of the decoded frame if any, otherwise keep the
1214 * interpolated one */
1215 if( frame->pts > 0 )
1217 p_sys->i_output_pts = frame->pts;
1220 if( !id->b_enc_inited )
1222 /* Hack because of the copy packetizer which can fail to detect the
1223 * proper size (which forces us to wait until the 1st frame
1225 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1226 p_sys->i_crop_right;
1227 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1228 p_sys->i_crop_bottom;
1230 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1233 /* Apply the scaling */
1234 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1235 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1237 else if( id->f_dst.video.i_width > 0 &&
1238 id->f_dst.video.i_height <= 0 )
1240 id->f_dst.video.i_height =
1241 id->f_dst.video.i_width / (double)i_width * i_height;
1243 else if( id->f_dst.video.i_width <= 0 &&
1244 id->f_dst.video.i_height > 0 )
1246 id->f_dst.video.i_width =
1247 id->f_dst.video.i_height / (double)i_height * i_width;
1250 id->p_encoder->fmt_in.video.i_width =
1251 id->p_encoder->fmt_out.video.i_width =
1252 id->f_dst.video.i_width;
1253 id->p_encoder->fmt_in.video.i_height =
1254 id->p_encoder->fmt_out.video.i_height =
1255 id->f_dst.video.i_height;
1257 id->p_encoder->fmt_out.i_extra = 0;
1258 id->p_encoder->fmt_out.p_extra = NULL;
1260 id->p_encoder->p_module =
1261 module_Need( id->p_encoder, "encoder", NULL );
1262 if( !id->p_encoder->p_module )
1264 vlc_object_destroy( id->p_encoder );
1265 msg_Err( p_stream, "cannot find encoder" );
1266 id->b_transcode = VLC_FALSE;
1267 return VLC_EGENERIC;
1270 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1271 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1273 /* Hack for mp2v/mp1v transcoding support */
1274 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1275 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1277 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1281 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1284 msg_Err( p_stream, "cannot add this stream" );
1285 transcode_video_ffmpeg_close( p_stream, id );
1286 id->b_transcode = VLC_FALSE;
1287 return VLC_EGENERIC;
1290 if( id->p_encoder->pf_header )
1292 p_block = id->p_encoder->pf_header( id->p_encoder );
1295 sout_buffer_t *p_out;
1296 block_t *p_prev_block = p_block;
1298 p_out = sout_BufferNew( p_stream->p_sout,
1299 p_block->i_buffer );
1300 memcpy( p_out->p_buffer, p_block->p_buffer,
1302 p_out->i_dts = p_out->i_pts = in->i_dts;
1303 p_out->i_length = 0;
1304 sout_BufferChain( out, p_out );
1306 p_block = p_block->p_next;
1307 block_Release( p_prev_block );
1311 id->i_inter_pixfmt =
1312 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1314 id->b_enc_inited = VLC_TRUE;
1318 if( p_stream->p_sys->b_deinterlace )
1320 if( id->p_ff_pic_tmp0 == NULL )
1324 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1325 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1326 id->ff_dec_c->width, id->ff_dec_c->height );
1328 buf = malloc( i_size );
1330 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1332 id->ff_dec_c->width, id->ff_dec_c->height );
1335 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1336 id->ff_dec_c->pix_fmt,
1337 id->ff_dec_c->width, id->ff_dec_c->height );
1339 frame = id->p_ff_pic_tmp0;
1342 /* convert pix format */
1343 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1345 if( id->p_ff_pic_tmp1 == NULL )
1349 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1350 i_size = avpicture_get_size( id->i_inter_pixfmt,
1351 id->ff_dec_c->width,
1352 id->ff_dec_c->height );
1354 buf = malloc( i_size );
1356 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1358 id->ff_dec_c->width, id->ff_dec_c->height );
1361 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1362 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1363 id->ff_dec_c->width, id->ff_dec_c->height );
1365 frame = id->p_ff_pic_tmp1;
1368 /* convert size and crop */
1369 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1370 id->ff_dec_c->height != id->f_dst.video.i_height ||
1371 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1372 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1374 if( id->p_ff_pic_tmp2 == NULL )
1378 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1379 i_size = avpicture_get_size( id->i_inter_pixfmt,
1380 id->f_dst.video.i_width,
1381 id->f_dst.video.i_height );
1383 buf = malloc( i_size );
1385 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1387 id->f_dst.video.i_width, id->f_dst.video.i_height );
1390 img_resample_full_init( id->f_dst.video.i_width,
1391 id->f_dst.video.i_height,
1392 id->ff_dec_c->width, id->ff_dec_c->height,
1393 p_stream->p_sys->i_crop_top,
1394 p_stream->p_sys->i_crop_bottom,
1395 p_stream->p_sys->i_crop_left,
1396 p_stream->p_sys->i_crop_right );
1399 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1400 (AVPicture*)frame );
1402 frame = id->p_ff_pic_tmp2;
1406 vout_InitPicture( VLC_OBJECT(p_stream), &pic,
1407 id->p_encoder->fmt_in.i_codec,
1408 id->f_dst.video.i_width, id->f_dst.video.i_height,
1409 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1410 id->f_dst.video.i_height );
1412 for( i_plane = 0; i_plane < pic.i_planes; i_plane++ )
1414 pic.p[i_plane].p_pixels = frame->data[i_plane];
1415 pic.p[i_plane].i_pitch = frame->linesize[i_plane];
1418 /* Set the pts of the frame being encoded */
1419 pic.date = p_sys->i_output_pts;
1421 pic.b_progressive = 1; /* ffmpeg doesn't support interlaced encoding */
1422 pic.i_nb_fields = frame->repeat_pict;
1423 #if LIBAVCODEC_BUILD >= 4684
1424 pic.b_top_field_first = frame->top_field_first;
1427 /* Interpolate the next PTS
1428 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1429 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1431 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1432 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1435 p_block = id->p_encoder->pf_encode_video( id->p_encoder, &pic );
1438 sout_buffer_t *p_out;
1439 block_t *p_prev_block = p_block;
1441 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1442 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1443 p_out->i_dts = p_block->i_dts;
1444 p_out->i_pts = p_block->i_pts;
1445 p_out->i_length = p_block->i_length;
1446 sout_BufferChain( out, p_out );
1448 p_block = p_block->p_next;
1449 block_Release( p_prev_block );
1461 /*****************************************************************************
1462 * transcode_video_ffmpeg_getframebuf:
1464 * Callback used by ffmpeg to get a frame buffer.
1465 * We use it to get the right PTS for each decoded picture.
1466 *****************************************************************************/
1467 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1470 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1473 p_frame->pts = p_sys->i_input_pts;
1475 return avcodec_default_get_buffer( p_context, p_frame );