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 *****************************************************************************/
33 #include <vlc_codec.h>
35 #include <vlc_input.h> /* hmmm, just for INPUT_RATE_DEFAULT */
38 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
39 # include <libavcodec/avcodec.h>
40 #elif defined(HAVE_FFMPEG_AVCODEC_H)
41 # include <ffmpeg/avcodec.h>
48 /*****************************************************************************
49 * decoder_sys_t : decoder descriptor
50 *****************************************************************************/
55 /* Video decoder specific part */
61 BITMAPINFOHEADER *p_format;
63 /* for frame skipping algo */
65 enum AVDiscard i_skip_frame;
66 enum AVDiscard i_skip_idct;
68 /* how many decoded frames are late */
70 mtime_t i_late_frames_start;
72 /* for direct rendering */
73 int b_direct_rendering;
75 vlc_bool_t b_has_b_frames;
77 /* Hack to force display of still pictures */
78 vlc_bool_t b_first_frame;
80 int i_buffer_orig, i_buffer;
81 char *p_buffer_orig, *p_buffer;
83 /* Postprocessing handle */
86 vlc_bool_t b_pp_async;
90 /* FIXME (dummy palette for now) */
91 static AVPaletteControl palette_control;
93 /*****************************************************************************
95 *****************************************************************************/
96 static void ffmpeg_InitCodec ( decoder_t * );
97 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
98 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
99 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
101 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
103 uint8_t *p = (uint8_t*)&fcc;
104 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
107 /*****************************************************************************
109 *****************************************************************************/
110 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
112 switch( i_ff_chroma )
114 case PIX_FMT_YUV420P:
115 case PIX_FMT_YUVJ420P: /* Hacky but better then chroma conversion */
116 return VLC_FOURCC('I','4','2','0');
117 case PIX_FMT_YUV422P:
118 case PIX_FMT_YUVJ422P: /* Hacky but better then chroma conversion */
119 return VLC_FOURCC('I','4','2','2');
120 case PIX_FMT_YUV444P:
121 case PIX_FMT_YUVJ444P: /* Hacky but better then chroma conversion */
122 return VLC_FOURCC('I','4','4','4');
125 return VLC_FOURCC('Y','U','Y','2');
127 #if defined(WORDS_BIGENDIAN)
129 return VLC_FOURCC('R','G','B','8');
131 return VLC_FOURCC('R','V','1','5');
133 return VLC_FOURCC('R','V','1','6');
135 return VLC_FOURCC('R','V','2','4');
138 return VLC_FOURCC('R','G','B','8');
140 return VLC_FOURCC('R','V','1','5');
142 return VLC_FOURCC('R','V','1','6');
144 return VLC_FOURCC('R','V','2','4');
147 return VLC_FOURCC('R','V','3','2');
150 return VLC_FOURCC('R','G','B','A');
153 return VLC_FOURCC('G','R','E','Y');
155 case PIX_FMT_YUV410P:
156 case PIX_FMT_YUV411P:
162 /* Returns a new picture buffer */
163 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
164 AVCodecContext *p_context )
166 decoder_sys_t *p_sys = p_dec->p_sys;
169 p_dec->fmt_out.video.i_width = p_context->width;
170 p_dec->fmt_out.video.i_height = p_context->height;
171 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
173 if( !p_context->width || !p_context->height )
175 return NULL; /* invalid display size */
178 if( !p_dec->fmt_out.i_codec )
180 /* we make conversion if possible*/
181 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
184 /* If an aspect-ratio was specified in the input format then force it */
185 if( p_dec->fmt_in.video.i_aspect )
187 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
191 p_dec->fmt_out.video.i_aspect =
192 VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
193 p_context->width / p_context->height );
194 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
195 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
197 if( p_dec->fmt_out.video.i_aspect == 0 )
199 p_dec->fmt_out.video.i_aspect =
200 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
204 if( p_dec->fmt_out.video.i_frame_rate > 0 &&
205 p_dec->fmt_out.video.i_frame_rate_base > 0 )
207 p_dec->fmt_out.video.i_frame_rate =
208 p_dec->fmt_in.video.i_frame_rate;
209 p_dec->fmt_out.video.i_frame_rate_base =
210 p_dec->fmt_in.video.i_frame_rate_base;
212 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
214 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
215 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num;
218 p_pic = p_dec->pf_vout_buffer_new( p_dec );
220 if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
222 E_(InitPostproc)( p_sys->p_pp, p_context->width,
223 p_context->height, p_context->pix_fmt );
224 p_sys->b_pp_init = VLC_TRUE;
230 /*****************************************************************************
231 * InitVideo: initialize the video decoder
232 *****************************************************************************
233 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
234 * opened (done after the first decoded frame).
235 *****************************************************************************/
236 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
237 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
239 decoder_sys_t *p_sys;
242 /* Allocate the memory needed to store the decoder's structure */
243 if( ( p_dec->p_sys = p_sys =
244 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
246 msg_Err( p_dec, "out of memory" );
249 memset( p_sys, 0, sizeof(decoder_sys_t) );
251 p_dec->p_sys->p_context = p_context;
252 p_dec->p_sys->p_codec = p_codec;
253 p_dec->p_sys->i_codec_id = i_codec_id;
254 p_dec->p_sys->psz_namecodec = psz_namecodec;
255 p_sys->p_ff_pic = avcodec_alloc_frame();
257 /* ***** Fill p_context with init values ***** */
258 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
259 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
260 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
261 p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
263 /* ***** Get configuration of ffmpeg plugin ***** */
264 p_sys->p_context->workaround_bugs =
265 config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
266 p_sys->p_context->error_resilience =
267 config_GetInt( p_dec, "ffmpeg-error-resilience" );
269 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
270 var_Get( p_dec, "grayscale", &val );
271 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
273 var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
274 var_Get( p_dec, "ffmpeg-vismv", &val );
275 if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
277 var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
278 var_Get( p_dec, "ffmpeg-lowres", &val );
279 if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
281 var_Create( p_dec, "ffmpeg-skiploopfilter",
282 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
283 var_Get( p_dec, "ffmpeg-skiploopfilter", &val );
284 if( val.i_int > 0 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
285 if( val.i_int > 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
286 if( val.i_int > 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
287 if( val.i_int > 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
289 /* ***** ffmpeg frame skipping ***** */
290 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
291 var_Get( p_dec, "ffmpeg-hurry-up", &val );
292 p_sys->b_hurry_up = val.b_bool;
294 var_Create( p_dec, "ffmpeg-skip-frame", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
295 var_Get( p_dec, "ffmpeg-skip-frame", &val );
299 p_sys->p_context->skip_frame = AVDISCARD_NONE;
302 p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
305 p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
308 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
311 p_sys->p_context->skip_frame = AVDISCARD_ALL;
314 p_sys->p_context->skip_frame = AVDISCARD_NONE;
317 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
319 var_Create( p_dec, "ffmpeg-skip-idct", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
320 var_Get( p_dec, "ffmpeg-skip-idct", &val );
324 p_sys->p_context->skip_idct = AVDISCARD_NONE;
327 p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
330 p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
333 p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
336 p_sys->p_context->skip_idct = AVDISCARD_ALL;
339 p_sys->p_context->skip_idct = AVDISCARD_NONE;
342 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
344 /* ***** ffmpeg direct rendering ***** */
345 p_sys->b_direct_rendering = 0;
346 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
347 var_Get( p_dec, "ffmpeg-dr", &val );
348 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
349 /* Apparently direct rendering doesn't work with YUV422P */
350 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
351 /* H264 uses too many reference frames */
352 p_sys->i_codec_id != CODEC_ID_H264 &&
353 !p_sys->p_context->debug_mv )
355 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
356 * so we need to do another check in ffmpeg_GetFrameBuf() */
357 p_sys->b_direct_rendering = 1;
361 p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
362 p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
364 /* ffmpeg doesn't properly release old pictures when frames are skipped */
365 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
366 if( p_sys->b_direct_rendering )
368 msg_Dbg( p_dec, "using direct rendering" );
369 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
372 /* Always use our get_buffer wrapper so we can calculate the
374 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
375 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
376 p_sys->p_context->opaque = p_dec;
378 /* ***** init this codec with special data ***** */
379 ffmpeg_InitCodec( p_dec );
381 /* ***** misc init ***** */
382 p_sys->input_pts = p_sys->input_dts = 0;
384 p_sys->b_has_b_frames = VLC_FALSE;
385 p_sys->b_first_frame = VLC_TRUE;
386 p_sys->i_late_frames = 0;
388 p_sys->i_buffer_orig = 1;
389 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
391 /* Set output properties */
392 p_dec->fmt_out.i_cat = VIDEO_ES;
393 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
396 if( p_dec->fmt_in.video.p_palette )
397 p_sys->p_context->palctrl =
398 (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
400 p_sys->p_context->palctrl = &palette_control;
402 /* ***** Open the codec ***** */
403 vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
410 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
412 vlc_mutex_unlock( lock );
413 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
417 vlc_mutex_unlock( lock );
418 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
424 /*****************************************************************************
425 * DecodeVideo: Called to decode one or more frames
426 *****************************************************************************/
427 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
429 decoder_sys_t *p_sys = p_dec->p_sys;
431 int b_null_size = VLC_FALSE;
434 if( !pp_block || !*pp_block ) return NULL;
436 if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
437 ffmpeg_InitCodec( p_dec );
441 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
444 p_sys->i_pts = 0; /* To make sure we recover properly */
446 p_sys->input_pts = p_sys->input_dts = 0;
447 p_sys->i_late_frames = 0;
449 block_Release( p_block );
451 //if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
452 //avcodec_flush_buffers( p_sys->p_context );
456 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
458 /* Do not care about late frames when prerolling
459 * TODO avoid decoding of non reference frame
460 * (ie all B except for H264 where it depends only on nal_ref_idc) */
461 p_sys->i_late_frames = 0;
464 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
465 (mdate() - p_sys->i_late_frames_start > I64C(5000000)) )
469 msg_Err( p_dec, "more than 5 seconds of late video -> "
470 "dropping frame (computer too slow ?)" );
471 p_sys->i_pts = 0; /* To make sure we recover properly */
473 block_Release( p_block );
474 p_sys->i_late_frames--;
478 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
480 p_sys->input_pts = p_block->i_pts;
481 p_sys->input_dts = p_block->i_dts;
483 /* Make sure we don't reuse the same timestamps twice */
484 p_block->i_pts = p_block->i_dts = 0;
487 /* A good idea could be to decode all I pictures and see for the other */
488 if( !p_dec->b_pace_control &&
490 (p_sys->i_late_frames > 4) )
493 if( p_sys->i_late_frames < 8 )
495 p_sys->p_context->skip_frame =
496 (p_sys->i_skip_frame <= AVDISCARD_BIDIR) ?
497 AVDISCARD_BIDIR : p_sys->i_skip_frame;
501 /* picture too late, won't decode
502 * but break picture until a new I, and for mpeg4 ...*/
503 p_sys->i_late_frames--; /* needed else it will never be decrease */
504 block_Release( p_block );
511 if( p_sys->b_hurry_up )
512 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
513 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
519 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
521 if( p_sys->b_hurry_up )
522 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
523 b_null_size = VLC_TRUE;
527 * Do the actual decoding now
530 /* Check if post-processing was enabled */
531 p_sys->b_pp = p_sys->b_pp_async;
533 /* Don't forget that ffmpeg requires a little more bytes
534 * that the real frame size */
535 if( p_block->i_buffer > 0 )
537 p_sys->i_buffer = p_block->i_buffer;
538 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
539 p_sys->i_buffer_orig )
541 free( p_sys->p_buffer_orig );
542 p_sys->i_buffer_orig =
543 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
544 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
546 p_sys->p_buffer = p_sys->p_buffer_orig;
547 p_sys->i_buffer = p_block->i_buffer;
548 p_dec->p_libvlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
550 memset( p_sys->p_buffer + p_block->i_buffer, 0,
551 FF_INPUT_BUFFER_PADDING_SIZE );
553 p_block->i_buffer = 0;
556 while( p_sys->i_buffer > 0 )
558 int i_used, b_gotpicture;
561 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
563 (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
564 if( b_null_size && p_sys->p_context->width > 0 &&
565 p_sys->p_context->height > 0 )
567 /* Reparse it to not drop the I frame */
568 b_null_size = VLC_FALSE;
569 if( p_sys->b_hurry_up )
570 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
571 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
573 (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
578 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
580 block_Release( p_block );
583 else if( i_used > p_sys->i_buffer )
585 i_used = p_sys->i_buffer;
589 p_sys->i_buffer -= i_used;
590 p_sys->p_buffer += i_used;
592 /* Nothing to display */
595 if( i_used == 0 ) break;
599 /* Update frame late count (except when doing preroll) */
600 if( p_sys->i_pts && decoder_GetDisplayDate(p_dec, p_sys->i_pts) <= mdate() &&
601 !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
603 p_sys->i_late_frames++;
604 if( p_sys->i_late_frames == 1 )
605 p_sys->i_late_frames_start = mdate();
609 p_sys->i_late_frames = 0;
612 if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
614 /* Do not display the picture */
615 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
616 if( !b_drawpicture && p_pic )
617 p_dec->pf_vout_buffer_del( p_dec, p_pic );
621 if( !p_sys->p_ff_pic->opaque )
623 /* Get a new picture */
624 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
627 block_Release( p_block );
631 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
633 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
637 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
641 if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
643 /* Sanity check (seems to be needed for some streams) */
644 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
646 p_sys->b_has_b_frames = VLC_TRUE;
649 if( !p_dec->fmt_in.video.i_aspect )
651 /* Fetch again the aspect ratio in case it changed */
652 p_dec->fmt_out.video.i_aspect =
654 * ( av_q2d(p_sys->p_context->sample_aspect_ratio)
655 * p_sys->p_context->width / p_sys->p_context->height );
656 p_dec->fmt_out.video.i_sar_num
657 = p_sys->p_context->sample_aspect_ratio.num;
658 p_dec->fmt_out.video.i_sar_den
659 = p_sys->p_context->sample_aspect_ratio.den;
661 if( p_dec->fmt_out.video.i_aspect == 0 )
663 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR
664 * p_sys->p_context->width / p_sys->p_context->height;
668 /* Send decoded frame to vout */
671 p_pic->date = p_sys->i_pts;
673 /* interpolate the next PTS */
674 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
675 p_dec->fmt_in.video.i_frame_rate_base > 0 )
677 p_sys->i_pts += I64C(1000000) *
678 (2 + p_sys->p_ff_pic->repeat_pict) *
679 p_dec->fmt_in.video.i_frame_rate_base *
680 p_block->i_rate / INPUT_RATE_DEFAULT /
681 (2 * p_dec->fmt_in.video.i_frame_rate);
683 else if( p_sys->p_context->time_base.den > 0 )
685 p_sys->i_pts += I64C(1000000) *
686 (2 + p_sys->p_ff_pic->repeat_pict) *
687 p_sys->p_context->time_base.num *
688 p_block->i_rate / INPUT_RATE_DEFAULT /
689 (2 * p_sys->p_context->time_base.den);
692 if( p_sys->b_first_frame )
694 /* Hack to force display of still pictures */
695 p_sys->b_first_frame = VLC_FALSE;
696 p_pic->b_force = VLC_TRUE;
699 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
700 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
701 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
707 p_dec->pf_vout_buffer_del( p_dec, p_pic );
711 block_Release( p_block );
715 /*****************************************************************************
716 * EndVideo: decoder destruction
717 *****************************************************************************
718 * This function is called when the thread ends after a successful
720 *****************************************************************************/
721 void E_(EndVideoDec)( decoder_t *p_dec )
723 decoder_sys_t *p_sys = p_dec->p_sys;
725 if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
726 E_(ClosePostproc)( p_dec, p_sys->p_pp );
727 free( p_sys->p_buffer_orig );
730 /*****************************************************************************
731 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
732 *****************************************************************************/
733 static void ffmpeg_InitCodec( decoder_t *p_dec )
735 decoder_sys_t *p_sys = p_dec->p_sys;
736 int i_size = p_dec->fmt_in.i_extra;
738 if( !i_size ) return;
740 if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
744 p_sys->p_context->extradata_size = i_size + 12;
745 p = p_sys->p_context->extradata =
746 malloc( p_sys->p_context->extradata_size );
748 memcpy( &p[0], "SVQ3", 4 );
749 memset( &p[4], 0, 8 );
750 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
752 /* Now remove all atoms before the SMI one */
753 if( p_sys->p_context->extradata_size > 0x5a &&
754 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
756 uint8_t *psz = &p[0x52];
758 while( psz < &p[p_sys->p_context->extradata_size - 8] )
760 int i_size = GetDWBE( psz );
763 /* FIXME handle 1 as long size */
766 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
768 memmove( &p[0x52], psz,
769 &p[p_sys->p_context->extradata_size] - psz );
777 else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
778 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
779 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
781 if( p_dec->fmt_in.i_extra == 8 )
783 p_sys->p_context->extradata_size = 8;
784 p_sys->p_context->extradata = malloc( 8 );
786 memcpy( p_sys->p_context->extradata,
787 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
788 p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
790 msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
791 p_sys->p_context->sub_id );
796 p_sys->p_context->extradata_size = i_size;
797 p_sys->p_context->extradata =
798 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
799 memcpy( p_sys->p_context->extradata,
800 p_dec->fmt_in.p_extra, i_size );
801 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
802 0, FF_INPUT_BUFFER_PADDING_SIZE );
806 /*****************************************************************************
807 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
808 * picture_t structure (when not in direct rendering mode).
809 *****************************************************************************/
810 static void ffmpeg_CopyPicture( decoder_t *p_dec,
811 picture_t *p_pic, AVFrame *p_ff_pic )
813 decoder_sys_t *p_sys = p_dec->p_sys;
815 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
817 int i_plane, i_size, i_line;
818 uint8_t *p_dst, *p_src;
819 int i_src_stride, i_dst_stride;
821 if( p_sys->p_pp && p_sys->b_pp )
822 E_(PostprocPict)( p_sys->p_pp, p_pic, p_ff_pic );
825 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
827 p_src = p_ff_pic->data[i_plane];
828 p_dst = p_pic->p[i_plane].p_pixels;
829 i_src_stride = p_ff_pic->linesize[i_plane];
830 i_dst_stride = p_pic->p[i_plane].i_pitch;
832 i_size = __MIN( i_src_stride, i_dst_stride );
833 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
836 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_size );
837 p_src += i_src_stride;
838 p_dst += i_dst_stride;
848 /* we need to convert to I420 */
849 switch( p_sys->p_context->pix_fmt )
851 case PIX_FMT_YUV410P:
852 case PIX_FMT_YUV411P:
860 for( i = 0; i < p_pic->i_planes; i++ )
862 dest_pic.data[i] = p_pic->p[i].p_pixels;
863 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
865 #if !defined(HAVE_LIBSWSCALE_SWSCALE_H) && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
866 img_convert( &dest_pic, PIX_FMT_YUV420P,
867 (AVPicture *)p_ff_pic,
868 p_sys->p_context->pix_fmt,
869 p_sys->p_context->width,
870 p_sys->p_context->height );
874 msg_Err( p_dec, "don't know how to convert chroma %i",
875 p_sys->p_context->pix_fmt );
882 /*****************************************************************************
883 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
884 *****************************************************************************
885 * It is used for direct rendering as well as to get the right PTS for each
886 * decoded picture (even in indirect rendering mode).
887 *****************************************************************************/
888 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
891 decoder_t *p_dec = (decoder_t *)p_context->opaque;
892 decoder_sys_t *p_sys = p_dec->p_sys;
895 /* Set picture PTS */
896 if( p_sys->input_pts )
898 p_ff_pic->pts = p_sys->input_pts;
900 else if( p_sys->input_dts )
902 /* Some demuxers only set the dts so let's try to find a useful
903 * timestamp from this */
904 if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
905 !p_ff_pic->reference || !p_sys->i_pts )
907 p_ff_pic->pts = p_sys->input_dts;
909 else p_ff_pic->pts = 0;
911 else p_ff_pic->pts = 0;
913 if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
915 p_sys->input_pts = p_sys->input_dts = 0;
918 p_ff_pic->opaque = 0;
920 /* Not much to do in indirect rendering mode */
921 if( !p_sys->b_direct_rendering || p_sys->b_pp )
923 return avcodec_default_get_buffer( p_context, p_ff_pic );
926 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
927 * so this check is necessary. */
928 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
929 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
931 msg_Dbg( p_dec, "disabling direct rendering" );
932 p_sys->b_direct_rendering = 0;
933 return avcodec_default_get_buffer( p_context, p_ff_pic );
936 /* Get a new picture */
937 //p_sys->p_vout->render.b_allow_modify_pics = 0;
938 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
941 p_sys->b_direct_rendering = 0;
942 return avcodec_default_get_buffer( p_context, p_ff_pic );
944 p_sys->p_context->draw_horiz_band = NULL;
946 p_ff_pic->opaque = (void*)p_pic;
947 p_ff_pic->type = FF_BUFFER_TYPE_USER;
948 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
949 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
950 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
951 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
953 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
954 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
955 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
956 p_ff_pic->linesize[3] = 0;
958 if( p_ff_pic->reference != 0 ||
959 p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
961 p_dec->pf_picture_link( p_dec, p_pic );
964 /* FIXME what is that, should give good value */
965 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
970 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
973 decoder_t *p_dec = (decoder_t *)p_context->opaque;
976 if( !p_ff_pic->opaque )
978 avcodec_default_release_buffer( p_context, p_ff_pic );
982 p_pic = (picture_t*)p_ff_pic->opaque;
984 p_ff_pic->data[0] = NULL;
985 p_ff_pic->data[1] = NULL;
986 p_ff_pic->data[2] = NULL;
987 p_ff_pic->data[3] = NULL;
989 if( p_ff_pic->reference != 0 ||
990 p_dec->p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
992 p_dec->pf_picture_unlink( p_dec, p_pic );