1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc_codec.h>
31 #include <vlc_input.h> /* hmmm, just for INPUT_RATE_DEFAULT */
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 # include <ffmpeg/avcodec.h>
42 /*****************************************************************************
43 * decoder_sys_t : decoder descriptor
44 *****************************************************************************/
49 /* Video decoder specific part */
55 BITMAPINFOHEADER *p_format;
57 /* for frame skipping algo */
59 enum AVDiscard i_skip_frame;
60 enum AVDiscard i_skip_idct;
62 /* how many decoded frames are late */
64 mtime_t i_late_frames_start;
66 /* for direct rendering */
67 int b_direct_rendering;
69 vlc_bool_t b_has_b_frames;
71 /* Hack to force display of still pictures */
72 vlc_bool_t b_first_frame;
74 int i_buffer_orig, i_buffer;
75 char *p_buffer_orig, *p_buffer;
77 /* Postprocessing handle */
80 vlc_bool_t b_pp_async;
84 /* FIXME (dummy palette for now) */
85 static AVPaletteControl palette_control;
87 /*****************************************************************************
89 *****************************************************************************/
90 static void ffmpeg_InitCodec ( decoder_t * );
91 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
92 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
93 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
95 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
97 uint8_t *p = (uint8_t*)&fcc;
98 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
101 /*****************************************************************************
103 *****************************************************************************/
104 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
106 switch( i_ff_chroma )
108 case PIX_FMT_YUV420P:
109 case PIX_FMT_YUVJ420P: /* Hacky but better then chroma conversion */
110 return VLC_FOURCC('I','4','2','0');
111 case PIX_FMT_YUV422P:
112 case PIX_FMT_YUVJ422P: /* Hacky but better then chroma conversion */
113 return VLC_FOURCC('I','4','2','2');
114 case PIX_FMT_YUV444P:
115 case PIX_FMT_YUVJ444P: /* Hacky but better then chroma conversion */
116 return VLC_FOURCC('I','4','4','4');
119 return VLC_FOURCC('Y','U','Y','2');
122 return VLC_FOURCC('R','V','1','5');
124 return VLC_FOURCC('R','V','1','6');
126 return VLC_FOURCC('R','V','2','4');
128 return VLC_FOURCC('R','V','3','2');
130 return VLC_FOURCC('G','R','E','Y');
132 case PIX_FMT_YUV410P:
133 case PIX_FMT_YUV411P:
140 /* Returns a new picture buffer */
141 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
142 AVCodecContext *p_context )
144 decoder_sys_t *p_sys = p_dec->p_sys;
147 p_dec->fmt_out.video.i_width = p_context->width;
148 p_dec->fmt_out.video.i_height = p_context->height;
149 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
151 if( !p_context->width || !p_context->height )
153 return NULL; /* invalid display size */
156 if( !p_dec->fmt_out.i_codec )
158 /* we make conversion if possible*/
159 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
162 /* If an aspect-ratio was specified in the input format then force it */
163 if( p_dec->fmt_in.video.i_aspect )
165 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
169 p_dec->fmt_out.video.i_aspect =
170 VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
171 p_context->width / p_context->height );
172 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
173 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
175 if( p_dec->fmt_out.video.i_aspect == 0 )
177 p_dec->fmt_out.video.i_aspect =
178 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
182 if( p_dec->fmt_out.video.i_frame_rate > 0 &&
183 p_dec->fmt_out.video.i_frame_rate_base > 0 )
185 p_dec->fmt_out.video.i_frame_rate =
186 p_dec->fmt_in.video.i_frame_rate;
187 p_dec->fmt_out.video.i_frame_rate_base =
188 p_dec->fmt_in.video.i_frame_rate_base;
190 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
192 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
193 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num;
196 p_pic = p_dec->pf_vout_buffer_new( p_dec );
198 if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
200 E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
201 p_context->height, p_context->pix_fmt );
202 p_sys->b_pp_init = VLC_TRUE;
208 /*****************************************************************************
209 * InitVideo: initialize the video decoder
210 *****************************************************************************
211 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
212 * opened (done after the first decoded frame).
213 *****************************************************************************/
214 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
215 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
217 decoder_sys_t *p_sys;
220 /* Allocate the memory needed to store the decoder's structure */
221 if( ( p_dec->p_sys = p_sys =
222 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
224 msg_Err( p_dec, "out of memory" );
227 memset( p_sys, 0, sizeof(decoder_sys_t) );
229 p_dec->p_sys->p_context = p_context;
230 p_dec->p_sys->p_codec = p_codec;
231 p_dec->p_sys->i_codec_id = i_codec_id;
232 p_dec->p_sys->psz_namecodec = psz_namecodec;
233 p_sys->p_ff_pic = avcodec_alloc_frame();
235 /* ***** Fill p_context with init values ***** */
236 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
237 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
238 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
239 p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
241 /* ***** Get configuration of ffmpeg plugin ***** */
242 p_sys->p_context->workaround_bugs =
243 config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
244 p_sys->p_context->error_resilience =
245 config_GetInt( p_dec, "ffmpeg-error-resilience" );
247 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248 var_Get( p_dec, "grayscale", &val );
249 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
251 var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252 var_Get( p_dec, "ffmpeg-vismv", &val );
253 if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
255 var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256 var_Get( p_dec, "ffmpeg-lowres", &val );
257 if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
259 var_Create( p_dec, "ffmpeg-skiploopfilter",
260 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
261 var_Get( p_dec, "ffmpeg-skiploopfilter", &val );
262 if( val.i_int > 0 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
263 if( val.i_int > 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
264 if( val.i_int > 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
265 if( val.i_int > 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
267 /* ***** ffmpeg frame skipping ***** */
268 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
269 var_Get( p_dec, "ffmpeg-hurry-up", &val );
270 p_sys->b_hurry_up = val.b_bool;
272 var_Create( p_dec, "ffmpeg-skip-frame", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
273 var_Get( p_dec, "ffmpeg-skip-frame", &val );
277 p_sys->p_context->skip_frame = AVDISCARD_NONE;
280 p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
283 p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
286 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
289 p_sys->p_context->skip_frame = AVDISCARD_ALL;
292 p_sys->p_context->skip_frame = AVDISCARD_NONE;
295 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
297 var_Create( p_dec, "ffmpeg-skip-idct", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
298 var_Get( p_dec, "ffmpeg-skip-idct", &val );
302 p_sys->p_context->skip_idct = AVDISCARD_NONE;
305 p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
308 p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
311 p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
314 p_sys->p_context->skip_idct = AVDISCARD_ALL;
317 p_sys->p_context->skip_idct = AVDISCARD_NONE;
320 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
322 /* ***** ffmpeg direct rendering ***** */
323 p_sys->b_direct_rendering = 0;
324 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
325 var_Get( p_dec, "ffmpeg-dr", &val );
326 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
327 /* Apparently direct rendering doesn't work with YUV422P */
328 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
329 /* H264 uses too many reference frames */
330 p_sys->i_codec_id != CODEC_ID_H264 &&
331 !p_sys->p_context->debug_mv )
333 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
334 * so we need to do another check in ffmpeg_GetFrameBuf() */
335 p_sys->b_direct_rendering = 1;
339 p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
340 p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
342 /* ffmpeg doesn't properly release old pictures when frames are skipped */
343 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
344 if( p_sys->b_direct_rendering )
346 msg_Dbg( p_dec, "using direct rendering" );
347 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
350 /* Always use our get_buffer wrapper so we can calculate the
352 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
353 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
354 p_sys->p_context->opaque = p_dec;
356 /* ***** init this codec with special data ***** */
357 ffmpeg_InitCodec( p_dec );
359 /* ***** misc init ***** */
360 p_sys->input_pts = p_sys->input_dts = 0;
362 p_sys->b_has_b_frames = VLC_FALSE;
363 p_sys->b_first_frame = VLC_TRUE;
364 p_sys->i_late_frames = 0;
366 p_sys->i_buffer_orig = 1;
367 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
369 /* Set output properties */
370 p_dec->fmt_out.i_cat = VIDEO_ES;
371 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
374 if( p_dec->fmt_in.video.p_palette )
375 p_sys->p_context->palctrl =
376 (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
378 p_sys->p_context->palctrl = &palette_control;
380 /* ***** Open the codec ***** */
381 vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
388 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
390 vlc_mutex_unlock( lock );
391 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
395 vlc_mutex_unlock( lock );
396 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
402 /*****************************************************************************
403 * DecodeVideo: Called to decode one or more frames
404 *****************************************************************************/
405 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
407 decoder_sys_t *p_sys = p_dec->p_sys;
409 int b_null_size = VLC_FALSE;
412 if( !pp_block || !*pp_block ) return NULL;
414 if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
415 ffmpeg_InitCodec( p_dec );
419 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
422 p_sys->i_pts = 0; /* To make sure we recover properly */
424 p_sys->input_pts = p_sys->input_dts = 0;
425 p_sys->i_late_frames = 0;
427 block_Release( p_block );
429 //if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
430 //avcodec_flush_buffers( p_sys->p_context );
434 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
436 /* Do not care about late frames when prerolling
437 * TODO avoid decoding of non reference frame
438 * (ie all B except for H264 where it depends only on nal_ref_idc) */
439 p_sys->i_late_frames = 0;
442 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
443 (mdate() - p_sys->i_late_frames_start > I64C(5000000)) )
447 msg_Err( p_dec, "more than 5 seconds of late video -> "
448 "dropping frame (computer too slow ?)" );
449 p_sys->i_pts = 0; /* To make sure we recover properly */
451 block_Release( p_block );
452 p_sys->i_late_frames--;
456 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
458 p_sys->input_pts = p_block->i_pts;
459 p_sys->input_dts = p_block->i_dts;
461 /* Make sure we don't reuse the same timestamps twice */
462 p_block->i_pts = p_block->i_dts = 0;
465 /* A good idea could be to decode all I pictures and see for the other */
466 if( !p_dec->b_pace_control &&
468 (p_sys->i_late_frames > 4) )
471 if( p_sys->i_late_frames < 8 )
473 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
477 /* picture too late, won't decode
478 * but break picture until a new I, and for mpeg4 ...*/
479 p_sys->i_late_frames--; /* needed else it will never be decrease */
480 block_Release( p_block );
487 if( p_sys->b_hurry_up )
488 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
489 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
495 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
497 if( p_sys->b_hurry_up )
498 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
499 b_null_size = VLC_TRUE;
503 * Do the actual decoding now
506 /* Check if post-processing was enabled */
507 p_sys->b_pp = p_sys->b_pp_async;
509 /* Don't forget that ffmpeg requires a little more bytes
510 * that the real frame size */
511 if( p_block->i_buffer > 0 )
513 p_sys->i_buffer = p_block->i_buffer;
514 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
515 p_sys->i_buffer_orig )
517 free( p_sys->p_buffer_orig );
518 p_sys->i_buffer_orig =
519 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
520 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
522 p_sys->p_buffer = p_sys->p_buffer_orig;
523 p_sys->i_buffer = p_block->i_buffer;
524 p_dec->p_libvlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
526 memset( p_sys->p_buffer + p_block->i_buffer, 0,
527 FF_INPUT_BUFFER_PADDING_SIZE );
529 p_block->i_buffer = 0;
532 while( p_sys->i_buffer > 0 )
534 int i_used, b_gotpicture;
537 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
539 (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
540 if( b_null_size && p_sys->p_context->width > 0 &&
541 p_sys->p_context->height > 0 )
543 /* Reparse it to not drop the I frame */
544 b_null_size = VLC_FALSE;
545 if( p_sys->b_hurry_up )
546 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
547 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
549 (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
554 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
556 block_Release( p_block );
559 else if( i_used > p_sys->i_buffer )
561 i_used = p_sys->i_buffer;
565 p_sys->i_buffer -= i_used;
566 p_sys->p_buffer += i_used;
568 /* Nothing to display */
571 if( i_used == 0 ) break;
575 /* Update frame late count (except when doing preroll) */
576 if( p_sys->i_pts && decoder_GetDisplayDate(p_sys->i_pts) <= mdate() &&
577 !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
579 p_sys->i_late_frames++;
580 if( p_sys->i_late_frames == 1 )
581 p_sys->i_late_frames_start = mdate();
585 p_sys->i_late_frames = 0;
588 if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
590 /* Do not display the picture */
591 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
592 if( !b_drawpicture && p_pic )
593 p_dec->pf_vout_buffer_del( p_dec, p_pic );
597 if( !p_sys->p_ff_pic->opaque )
599 /* Get a new picture */
600 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
603 block_Release( p_block );
607 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
609 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
613 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
617 if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
619 /* Sanity check (seems to be needed for some streams) */
620 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
622 p_sys->b_has_b_frames = VLC_TRUE;
625 if( !p_dec->fmt_in.video.i_aspect )
627 /* Fetch again the aspect ratio in case it changed */
628 p_dec->fmt_out.video.i_aspect =
630 * ( av_q2d(p_sys->p_context->sample_aspect_ratio)
631 * p_sys->p_context->width / p_sys->p_context->height );
632 p_dec->fmt_out.video.i_sar_num
633 = p_sys->p_context->sample_aspect_ratio.num;
634 p_dec->fmt_out.video.i_sar_den
635 = p_sys->p_context->sample_aspect_ratio.den;
637 if( p_dec->fmt_out.video.i_aspect == 0 )
639 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR
640 * p_sys->p_context->width / p_sys->p_context->height;
644 /* Send decoded frame to vout */
647 p_pic->date = p_sys->i_pts;
649 /* interpolate the next PTS */
650 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
651 p_dec->fmt_in.video.i_frame_rate_base > 0 )
653 p_sys->i_pts += I64C(1000000) *
654 (2 + p_sys->p_ff_pic->repeat_pict) *
655 p_dec->fmt_in.video.i_frame_rate_base *
656 p_block->i_rate / INPUT_RATE_DEFAULT /
657 (2 * p_dec->fmt_in.video.i_frame_rate);
659 else if( p_sys->p_context->time_base.den > 0 )
661 p_sys->i_pts += I64C(1000000) *
662 (2 + p_sys->p_ff_pic->repeat_pict) *
663 p_sys->p_context->time_base.num *
664 p_block->i_rate / INPUT_RATE_DEFAULT /
665 (2 * p_sys->p_context->time_base.den);
668 if( p_sys->b_first_frame )
670 /* Hack to force display of still pictures */
671 p_sys->b_first_frame = VLC_FALSE;
672 p_pic->b_force = VLC_TRUE;
675 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
676 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
677 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
683 p_dec->pf_vout_buffer_del( p_dec, p_pic );
687 block_Release( p_block );
691 /*****************************************************************************
692 * EndVideo: decoder destruction
693 *****************************************************************************
694 * This function is called when the thread ends after a successful
696 *****************************************************************************/
697 void E_(EndVideoDec)( decoder_t *p_dec )
699 decoder_sys_t *p_sys = p_dec->p_sys;
701 if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
702 E_(ClosePostproc)( p_dec, p_sys->p_pp );
703 free( p_sys->p_buffer_orig );
706 /*****************************************************************************
707 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
708 *****************************************************************************/
709 static void ffmpeg_InitCodec( decoder_t *p_dec )
711 decoder_sys_t *p_sys = p_dec->p_sys;
712 int i_size = p_dec->fmt_in.i_extra;
714 if( !i_size ) return;
716 if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
720 p_sys->p_context->extradata_size = i_size + 12;
721 p = p_sys->p_context->extradata =
722 malloc( p_sys->p_context->extradata_size );
724 memcpy( &p[0], "SVQ3", 4 );
725 memset( &p[4], 0, 8 );
726 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
728 /* Now remove all atoms before the SMI one */
729 if( p_sys->p_context->extradata_size > 0x5a &&
730 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
732 uint8_t *psz = &p[0x52];
734 while( psz < &p[p_sys->p_context->extradata_size - 8] )
736 int i_size = GetDWBE( psz );
739 /* FIXME handle 1 as long size */
742 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
744 memmove( &p[0x52], psz,
745 &p[p_sys->p_context->extradata_size] - psz );
753 else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
754 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
755 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
757 if( p_dec->fmt_in.i_extra == 8 )
759 p_sys->p_context->extradata_size = 8;
760 p_sys->p_context->extradata = malloc( 8 );
762 memcpy( p_sys->p_context->extradata,
763 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
764 p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
766 msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
767 p_sys->p_context->sub_id );
772 p_sys->p_context->extradata_size = i_size;
773 p_sys->p_context->extradata =
774 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
775 memcpy( p_sys->p_context->extradata,
776 p_dec->fmt_in.p_extra, i_size );
777 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
778 0, FF_INPUT_BUFFER_PADDING_SIZE );
782 /*****************************************************************************
783 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
784 * picture_t structure (when not in direct rendering mode).
785 *****************************************************************************/
786 static void ffmpeg_CopyPicture( decoder_t *p_dec,
787 picture_t *p_pic, AVFrame *p_ff_pic )
789 decoder_sys_t *p_sys = p_dec->p_sys;
791 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
793 int i_plane, i_size, i_line;
794 uint8_t *p_dst, *p_src;
795 int i_src_stride, i_dst_stride;
797 if( p_sys->p_pp && p_sys->b_pp )
798 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
801 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
803 p_src = p_ff_pic->data[i_plane];
804 p_dst = p_pic->p[i_plane].p_pixels;
805 i_src_stride = p_ff_pic->linesize[i_plane];
806 i_dst_stride = p_pic->p[i_plane].i_pitch;
808 i_size = __MIN( i_src_stride, i_dst_stride );
809 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
812 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_size );
813 p_src += i_src_stride;
814 p_dst += i_dst_stride;
824 /* we need to convert to I420 */
825 switch( p_sys->p_context->pix_fmt )
827 case PIX_FMT_YUV410P:
828 case PIX_FMT_YUV411P:
831 for( i = 0; i < p_pic->i_planes; i++ )
833 dest_pic.data[i] = p_pic->p[i].p_pixels;
834 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
836 #if !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
837 img_convert( &dest_pic, PIX_FMT_YUV420P,
838 (AVPicture *)p_ff_pic,
839 p_sys->p_context->pix_fmt,
840 p_sys->p_context->width,
841 p_sys->p_context->height );
845 msg_Err( p_dec, "don't know how to convert chroma %i",
846 p_sys->p_context->pix_fmt );
853 /*****************************************************************************
854 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
855 *****************************************************************************
856 * It is used for direct rendering as well as to get the right PTS for each
857 * decoded picture (even in indirect rendering mode).
858 *****************************************************************************/
859 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
862 decoder_t *p_dec = (decoder_t *)p_context->opaque;
863 decoder_sys_t *p_sys = p_dec->p_sys;
866 /* Set picture PTS */
867 if( p_sys->input_pts )
869 p_ff_pic->pts = p_sys->input_pts;
871 else if( p_sys->input_dts )
873 /* Some demuxers only set the dts so let's try to find a useful
874 * timestamp from this */
875 if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
876 !p_ff_pic->reference || !p_sys->i_pts )
878 p_ff_pic->pts = p_sys->input_dts;
880 else p_ff_pic->pts = 0;
882 else p_ff_pic->pts = 0;
884 if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
886 p_sys->input_pts = p_sys->input_dts = 0;
889 p_ff_pic->opaque = 0;
891 /* Not much to do in indirect rendering mode */
892 if( !p_sys->b_direct_rendering || p_sys->b_pp )
894 return avcodec_default_get_buffer( p_context, p_ff_pic );
897 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
898 * so this check is necessary. */
899 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
900 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
902 msg_Dbg( p_dec, "disabling direct rendering" );
903 p_sys->b_direct_rendering = 0;
904 return avcodec_default_get_buffer( p_context, p_ff_pic );
907 /* Get a new picture */
908 //p_sys->p_vout->render.b_allow_modify_pics = 0;
909 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
912 p_sys->b_direct_rendering = 0;
913 return avcodec_default_get_buffer( p_context, p_ff_pic );
915 p_sys->p_context->draw_horiz_band = NULL;
917 p_ff_pic->opaque = (void*)p_pic;
918 p_ff_pic->type = FF_BUFFER_TYPE_USER;
919 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
920 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
921 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
922 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
924 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
925 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
926 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
927 p_ff_pic->linesize[3] = 0;
929 if( p_ff_pic->reference != 0 ||
930 p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
932 p_dec->pf_picture_link( p_dec, p_pic );
935 /* FIXME what is that, should give good value */
936 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
941 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
944 decoder_t *p_dec = (decoder_t *)p_context->opaque;
947 if( !p_ff_pic->opaque )
949 avcodec_default_release_buffer( p_context, p_ff_pic );
953 p_pic = (picture_t*)p_ff_pic->opaque;
955 p_ff_pic->data[0] = NULL;
956 p_ff_pic->data[1] = NULL;
957 p_ff_pic->data[2] = NULL;
958 p_ff_pic->data[3] = NULL;
960 if( p_ff_pic->reference != 0 ||
961 p_dec->p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
963 p_dec->pf_picture_unlink( p_dec, p_pic );