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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_codecs.h> /* BITMAPINFOHEADER */
35 #include <vlc_avcodec.h>
39 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
40 # include <libavcodec/avcodec.h>
41 # ifdef HAVE_AVCODEC_VAAPI
42 # include <libavcodec/vaapi.h>
44 #elif defined(HAVE_FFMPEG_AVCODEC_H)
45 # include <ffmpeg/avcodec.h>
53 /*****************************************************************************
54 * decoder_sys_t : decoder descriptor
55 *****************************************************************************/
60 /* Video decoder specific part */
67 /* for frame skipping algo */
69 enum AVDiscard i_skip_frame;
70 enum AVDiscard i_skip_idct;
72 /* how many decoded frames are late */
74 mtime_t i_late_frames_start;
76 /* for direct rendering */
77 bool b_direct_rendering;
81 /* Hack to force display of still pictures */
84 int i_buffer_orig, i_buffer;
85 char *p_buffer_orig, *p_buffer;
88 AVPaletteControl palette;
97 /* FIXME (dummy palette for now) */
98 static const AVPaletteControl palette_control;
100 /*****************************************************************************
102 *****************************************************************************/
103 static void ffmpeg_InitCodec ( decoder_t * );
104 static int ffmpeg_OpenCodec ( decoder_t * );
105 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
106 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
107 static int ffmpeg_ReGetFrameBuf( struct AVCodecContext *, AVFrame * );
108 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
109 static void ffmpeg_NextPts( decoder_t * );
111 #ifdef HAVE_AVCODEC_VAAPI
112 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
113 const enum PixelFormat * );
116 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
118 uint8_t *p = (uint8_t*)&fcc;
119 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
122 /*****************************************************************************
124 *****************************************************************************/
126 /* Returns a new picture buffer */
127 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
128 AVCodecContext *p_context )
130 decoder_sys_t *p_sys = p_dec->p_sys;
132 p_dec->fmt_out.video.i_width = p_context->width;
133 p_dec->fmt_out.video.i_height = p_context->height;
135 if( !p_context->width || !p_context->height )
137 return NULL; /* invalid display size */
140 if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
142 /* we are doomed, but not really, because most codecs set their pix_fmt
144 * FIXME does it make sense here ? */
145 p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
147 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
149 /* If an aspect-ratio was specified in the input format then force it */
150 if( p_dec->fmt_in.video.i_aspect )
152 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
156 p_dec->fmt_out.video.i_aspect =
157 VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
158 p_context->width / p_context->height );
159 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
160 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
162 if( p_dec->fmt_out.video.i_aspect == 0 )
164 p_dec->fmt_out.video.i_aspect =
165 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
169 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
170 p_dec->fmt_in.video.i_frame_rate_base > 0 )
172 p_dec->fmt_out.video.i_frame_rate =
173 p_dec->fmt_in.video.i_frame_rate;
174 p_dec->fmt_out.video.i_frame_rate_base =
175 p_dec->fmt_in.video.i_frame_rate_base;
177 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
179 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
180 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num;
183 return decoder_NewPicture( p_dec );
186 /*****************************************************************************
187 * InitVideo: initialize the video decoder
188 *****************************************************************************
189 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
190 * opened (done after the first decoded frame).
191 *****************************************************************************/
192 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
193 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
195 decoder_sys_t *p_sys;
198 /* Allocate the memory needed to store the decoder's structure */
199 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
202 p_sys->p_context = p_context;
203 p_sys->p_codec = p_codec;
204 p_sys->i_codec_id = i_codec_id;
205 p_sys->psz_namecodec = psz_namecodec;
206 p_sys->p_ff_pic = avcodec_alloc_frame();
207 p_sys->b_delayed_open = true;
210 /* ***** Fill p_context with init values ***** */
211 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec );
213 /* ***** Get configuration of ffmpeg plugin ***** */
214 p_sys->p_context->workaround_bugs =
215 config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
216 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
217 p_sys->p_context->error_resilience =
218 config_GetInt( p_dec, "ffmpeg-error-resilience" );
220 p_sys->p_context->error_recognition =
221 config_GetInt( p_dec, "ffmpeg-error-resilience" );
224 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
225 var_Get( p_dec, "grayscale", &val );
226 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
228 var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
229 var_Get( p_dec, "ffmpeg-vismv", &val );
230 if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
232 var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
233 var_Get( p_dec, "ffmpeg-lowres", &val );
234 if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
236 var_Create( p_dec, "ffmpeg-skiploopfilter",
237 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
238 var_Get( p_dec, "ffmpeg-skiploopfilter", &val );
239 if( val.i_int > 0 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
240 if( val.i_int > 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
241 if( val.i_int > 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
242 if( val.i_int > 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
244 bool b_fast = var_CreateGetBool( p_dec, "ffmpeg-fast" );
245 if( b_fast ) p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
247 /* ***** ffmpeg frame skipping ***** */
248 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
249 var_Get( p_dec, "ffmpeg-hurry-up", &val );
250 p_sys->b_hurry_up = val.b_bool;
252 var_Create( p_dec, "ffmpeg-skip-frame", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
253 var_Get( p_dec, "ffmpeg-skip-frame", &val );
257 p_sys->p_context->skip_frame = AVDISCARD_NONE;
260 p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
263 p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
266 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
269 p_sys->p_context->skip_frame = AVDISCARD_ALL;
272 p_sys->p_context->skip_frame = AVDISCARD_NONE;
275 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
277 var_Create( p_dec, "ffmpeg-skip-idct", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
278 var_Get( p_dec, "ffmpeg-skip-idct", &val );
282 p_sys->p_context->skip_idct = AVDISCARD_NONE;
285 p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
288 p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
291 p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
294 p_sys->p_context->skip_idct = AVDISCARD_ALL;
297 p_sys->p_context->skip_idct = AVDISCARD_NONE;
300 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
302 /* ***** ffmpeg direct rendering ***** */
303 p_sys->b_direct_rendering = false;
304 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
305 var_Get( p_dec, "ffmpeg-dr", &val );
306 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
307 /* Apparently direct rendering doesn't work with YUV422P */
308 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
309 /* H264 uses too many reference frames */
310 p_sys->i_codec_id != CODEC_ID_H264 &&
311 /* No idea why ... but this fixes flickering on some TSCC streams */
312 p_sys->i_codec_id != CODEC_ID_TSCC &&
313 !p_sys->p_context->debug_mv )
315 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
316 * so we need to do another check in ffmpeg_GetFrameBuf() */
317 p_sys->b_direct_rendering = true;
320 /* ffmpeg doesn't properly release old pictures when frames are skipped */
321 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = false;
322 if( p_sys->b_direct_rendering )
324 msg_Dbg( p_dec, "using direct rendering" );
325 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
328 /* Always use our get_buffer wrapper so we can calculate the
330 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
331 p_sys->p_context->reget_buffer = ffmpeg_ReGetFrameBuf;
332 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
333 p_sys->p_context->opaque = p_dec;
335 #ifdef HAVE_AVCODEC_VAAPI
336 if( var_CreateGetBool( p_dec, "ffmpeg-hw" ) )
337 p_sys->p_context->get_format = ffmpeg_GetFormat;
340 /* ***** misc init ***** */
341 p_sys->input_pts = p_sys->input_dts = 0;
343 p_sys->b_has_b_frames = false;
344 p_sys->b_first_frame = true;
345 p_sys->b_flush = false;
346 p_sys->i_late_frames = 0;
348 p_sys->i_buffer_orig = 1;
349 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
350 if( !p_sys->p_buffer_orig )
352 av_free( p_sys->p_ff_pic );
357 /* Set output properties */
358 p_dec->fmt_out.i_cat = VIDEO_ES;
359 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
361 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
362 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
364 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
367 memset( &p_sys->palette, 0, sizeof(p_sys->palette) );
368 if( p_dec->fmt_in.video.p_palette )
370 p_sys->palette.palette_changed = 1;
372 for( int i = 0; i < __MIN( AVPALETTE_COUNT, p_dec->fmt_in.video.p_palette->i_entries ); i++ )
378 c.a[0] = p_dec->fmt_in.video.p_palette->palette[i][0];
379 c.a[1] = p_dec->fmt_in.video.p_palette->palette[i][1];
380 c.a[2] = p_dec->fmt_in.video.p_palette->palette[i][2];
381 c.a[3] = p_dec->fmt_in.video.p_palette->palette[i][3];
383 p_sys->palette.palette[i] = c.u;
385 p_sys->p_context->palctrl = &p_sys->palette;
387 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
388 if( p_dec->fmt_out.video.p_palette )
389 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
391 else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
393 p_sys->p_context->palctrl = &p_sys->palette;
396 /* ***** init this codec with special data ***** */
397 ffmpeg_InitCodec( p_dec );
399 /* ***** Open the codec ***** */
400 if( ffmpeg_OpenCodec( p_dec ) < 0 )
402 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
403 av_free( p_sys->p_ff_pic );
404 free( p_sys->p_buffer_orig );
412 /*****************************************************************************
413 * DecodeVideo: Called to decode one or more frames
414 *****************************************************************************/
415 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
417 decoder_sys_t *p_sys = p_dec->p_sys;
419 int b_null_size = false;
422 if( !pp_block || !*pp_block )
425 if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
427 ffmpeg_InitCodec( p_dec );
428 if( p_sys->b_delayed_open )
430 if( ffmpeg_OpenCodec( p_dec ) )
431 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
436 if( p_sys->b_delayed_open )
438 block_Release( p_block );
442 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
445 p_sys->i_pts = 0; /* To make sure we recover properly */
447 p_sys->input_pts = p_sys->input_dts = 0;
448 p_sys->i_late_frames = 0;
450 block_Release( p_block );
452 //if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
453 //avcodec_flush_buffers( p_sys->p_context );
457 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
459 /* Do not care about late frames when prerolling
460 * TODO avoid decoding of non reference frame
461 * (ie all B except for H264 where it depends only on nal_ref_idc) */
462 p_sys->i_late_frames = 0;
465 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
466 (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
470 msg_Err( p_dec, "more than 5 seconds of late video -> "
471 "dropping frame (computer too slow ?)" );
472 p_sys->i_pts = 0; /* To make sure we recover properly */
474 block_Release( p_block );
475 p_sys->i_late_frames--;
479 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
481 p_sys->input_pts = p_block->i_pts;
482 p_sys->input_dts = p_block->i_dts;
484 /* Make sure we don't reuse the same timestamps twice */
485 p_block->i_pts = p_block->i_dts = 0;
488 /* A good idea could be to decode all I pictures and see for the other */
489 if( !p_dec->b_pace_control &&
491 (p_sys->i_late_frames > 4) )
494 if( p_sys->i_late_frames < 12 )
496 p_sys->p_context->skip_frame =
497 (p_sys->i_skip_frame <= AVDISCARD_BIDIR) ?
498 AVDISCARD_BIDIR : p_sys->i_skip_frame;
502 /* picture too late, won't decode
503 * but break picture until a new I, and for mpeg4 ...*/
504 p_sys->i_late_frames--; /* needed else it will never be decrease */
505 block_Release( p_block );
512 if( p_sys->b_hurry_up )
513 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
514 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
520 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
522 if( p_sys->b_hurry_up )
523 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
526 else if( !b_drawpicture )
528 /* It creates broken picture
529 * FIXME either our parser or ffmpeg is broken */
531 if( p_sys->b_hurry_up )
532 p_sys->p_context->skip_frame = __MAX( p_sys->p_context->skip_frame,
538 * Do the actual decoding now
541 /* Don't forget that ffmpeg requires a little more bytes
542 * that the real frame size */
543 if( p_block->i_buffer > 0 )
545 p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
547 p_sys->i_buffer = p_block->i_buffer;
548 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
549 p_sys->i_buffer_orig )
551 free( p_sys->p_buffer_orig );
552 p_sys->i_buffer_orig =
553 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
554 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
556 p_sys->p_buffer = p_sys->p_buffer_orig;
557 p_sys->i_buffer = p_block->i_buffer;
558 if( !p_sys->p_buffer )
560 block_Release( p_block );
563 vlc_memcpy( p_sys->p_buffer, p_block->p_buffer, p_block->i_buffer );
564 memset( p_sys->p_buffer + p_block->i_buffer, 0,
565 FF_INPUT_BUFFER_PADDING_SIZE );
567 p_block->i_buffer = 0;
570 while( p_sys->i_buffer > 0 || p_sys->b_flush )
572 int i_used, b_gotpicture;
575 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
577 p_sys->i_buffer <= 0 && p_sys->b_flush ? NULL : (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
579 if( b_null_size && p_sys->p_context->width > 0 &&
580 p_sys->p_context->height > 0 &&
583 /* Reparse it to not drop the I frame */
585 if( p_sys->b_hurry_up )
586 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
587 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
589 (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
593 p_sys->b_first_frame = true;
595 if( p_sys->i_buffer <= 0 )
596 p_sys->b_flush = false;
601 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
603 block_Release( p_block );
606 else if( i_used > p_sys->i_buffer )
608 i_used = p_sys->i_buffer;
612 p_sys->i_buffer -= i_used;
613 p_sys->p_buffer += i_used;
615 /* Nothing to display */
618 if( i_used == 0 ) break;
623 if( p_sys->p_ff_pic->pts )
624 p_sys->i_pts = p_sys->p_ff_pic->pts;
626 /* Update frame late count (except when doing preroll) */
627 mtime_t i_display_date = 0;
628 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
629 i_display_date = decoder_GetDisplayDate( p_dec, p_sys->i_pts );
631 if( i_display_date > 0 && i_display_date <= mdate() )
633 p_sys->i_late_frames++;
634 if( p_sys->i_late_frames == 1 )
635 p_sys->i_late_frames_start = mdate();
639 p_sys->i_late_frames = 0;
642 if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
644 /* Do not display the picture */
645 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
646 if( !b_drawpicture && p_pic )
647 decoder_DeletePicture( p_dec, p_pic );
649 ffmpeg_NextPts( p_dec );
653 if( !p_sys->p_ff_pic->opaque )
655 /* Get a new picture */
656 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
659 block_Release( p_block );
663 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
665 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
669 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
672 /* Sanity check (seems to be needed for some streams) */
673 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
675 p_sys->b_has_b_frames = true;
678 if( !p_dec->fmt_in.video.i_aspect )
680 /* Fetch again the aspect ratio in case it changed */
681 p_dec->fmt_out.video.i_aspect =
683 * ( av_q2d(p_sys->p_context->sample_aspect_ratio)
684 * p_sys->p_context->width / p_sys->p_context->height );
685 p_dec->fmt_out.video.i_sar_num
686 = p_sys->p_context->sample_aspect_ratio.num;
687 p_dec->fmt_out.video.i_sar_den
688 = p_sys->p_context->sample_aspect_ratio.den;
690 if( p_dec->fmt_out.video.i_aspect == 0 )
692 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR
693 * p_sys->p_context->width / p_sys->p_context->height;
697 /* Send decoded frame to vout */
700 p_pic->date = p_sys->i_pts;
702 ffmpeg_NextPts( p_dec );
704 if( p_sys->b_first_frame )
706 /* Hack to force display of still pictures */
707 p_sys->b_first_frame = false;
708 p_pic->b_force = true;
711 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
712 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
713 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
715 p_pic->i_qstride = p_sys->p_ff_pic->qstride;
716 int i_mb_h = ( p_pic->format.i_height + 15 ) / 16;
717 p_pic->p_q = malloc( p_pic->i_qstride * i_mb_h );
718 memcpy( p_pic->p_q, p_sys->p_ff_pic->qscale_table,
719 p_pic->i_qstride * i_mb_h );
720 switch( p_sys->p_ff_pic->qscale_type )
722 case FF_QSCALE_TYPE_MPEG1:
723 p_pic->i_qtype = QTYPE_MPEG1;
725 case FF_QSCALE_TYPE_MPEG2:
726 p_pic->i_qtype = QTYPE_MPEG2;
728 case FF_QSCALE_TYPE_H264:
729 p_pic->i_qtype = QTYPE_H264;
737 decoder_DeletePicture( p_dec, p_pic );
741 block_Release( p_block );
745 /*****************************************************************************
746 * EndVideo: decoder destruction
747 *****************************************************************************
748 * This function is called when the thread ends after a successful
750 *****************************************************************************/
751 void EndVideoDec( decoder_t *p_dec )
753 decoder_sys_t *p_sys = p_dec->p_sys;
755 avcodec_flush_buffers( p_sys->p_context );
757 if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
758 free( p_sys->p_buffer_orig );
761 VaDelete( p_sys->p_va );
764 /*****************************************************************************
765 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
766 *****************************************************************************/
767 static void ffmpeg_InitCodec( decoder_t *p_dec )
769 decoder_sys_t *p_sys = p_dec->p_sys;
770 int i_size = p_dec->fmt_in.i_extra;
772 if( !i_size ) return;
774 if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
778 p_sys->p_context->extradata_size = i_size + 12;
779 p = p_sys->p_context->extradata =
780 malloc( p_sys->p_context->extradata_size );
784 memcpy( &p[0], "SVQ3", 4 );
785 memset( &p[4], 0, 8 );
786 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
788 /* Now remove all atoms before the SMI one */
789 if( p_sys->p_context->extradata_size > 0x5a &&
790 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
792 uint8_t *psz = &p[0x52];
794 while( psz < &p[p_sys->p_context->extradata_size - 8] )
796 int i_size = GetDWBE( psz );
799 /* FIXME handle 1 as long size */
802 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
804 memmove( &p[0x52], psz,
805 &p[p_sys->p_context->extradata_size] - psz );
815 p_sys->p_context->extradata_size = i_size;
816 p_sys->p_context->extradata =
817 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
818 if( p_sys->p_context->extradata )
820 memcpy( p_sys->p_context->extradata,
821 p_dec->fmt_in.p_extra, i_size );
822 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
823 0, FF_INPUT_BUFFER_PADDING_SIZE );
828 /*****************************************************************************
830 *****************************************************************************/
831 static int ffmpeg_OpenCodec( decoder_t *p_dec )
833 decoder_sys_t *p_sys = p_dec->p_sys;
835 if( p_sys->p_context->extradata_size <= 0 )
837 if( p_sys->i_codec_id == CODEC_ID_VC1 ||
838 p_sys->i_codec_id == CODEC_ID_VORBIS ||
839 p_sys->i_codec_id == CODEC_ID_THEORA )
841 msg_Warn( p_dec, "waiting for extra data for codec %s",
842 p_sys->psz_namecodec );
846 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
847 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
848 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 0, 0)
849 p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
851 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
856 ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
857 vlc_avcodec_unlock();
860 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
862 p_sys->b_delayed_open = false;
866 char psz_version[128];
868 VaVersion( p_sys->p_va, psz_version, sizeof(psz_version) );
869 msg_Info( p_dec, "Using VA API version %s for hardware decoding.", psz_version );
874 /*****************************************************************************
875 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
876 * picture_t structure (when not in direct rendering mode).
877 *****************************************************************************/
878 static void ffmpeg_CopyPicture( decoder_t *p_dec,
879 picture_t *p_pic, AVFrame *p_ff_pic )
881 decoder_sys_t *p_sys = p_dec->p_sys;
885 VaExtract( p_sys->p_va, p_pic, p_ff_pic );
887 else if( TestFfmpegChroma( p_sys->p_context->pix_fmt, -1 ) == VLC_SUCCESS )
889 int i_plane, i_size, i_line;
890 uint8_t *p_dst, *p_src;
891 int i_src_stride, i_dst_stride;
893 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
895 p_src = p_ff_pic->data[i_plane];
896 p_dst = p_pic->p[i_plane].p_pixels;
897 i_src_stride = p_ff_pic->linesize[i_plane];
898 i_dst_stride = p_pic->p[i_plane].i_pitch;
900 i_size = __MIN( i_src_stride, i_dst_stride );
901 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
904 vlc_memcpy( p_dst, p_src, i_size );
905 p_src += i_src_stride;
906 p_dst += i_dst_stride;
912 msg_Err( p_dec, "don't know how to convert chroma %i",
913 p_sys->p_context->pix_fmt );
918 /*****************************************************************************
919 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
920 *****************************************************************************
921 * It is used for direct rendering as well as to get the right PTS for each
922 * decoded picture (even in indirect rendering mode).
923 *****************************************************************************/
924 static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic );
926 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
929 decoder_t *p_dec = (decoder_t *)p_context->opaque;
930 decoder_sys_t *p_sys = p_dec->p_sys;
933 /* Set picture PTS */
934 ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
937 p_ff_pic->opaque = NULL;
941 #ifdef HAVE_AVCODEC_VAAPI
942 /* hwaccel_context is not present in old fffmpeg version */
943 if( VaSetup( p_sys->p_va,
944 &p_sys->p_context->hwaccel_context, &p_dec->fmt_out.video.i_chroma,
945 p_sys->p_context->width, p_sys->p_context->height ) )
947 msg_Err( p_dec, "VaSetup failed" );
955 p_ff_pic->type = FF_BUFFER_TYPE_USER;
956 /* FIXME what is that, should give good value */
957 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
959 if( VaGrabSurface( p_sys->p_va, p_ff_pic ) )
961 msg_Err( p_dec, "VaGrabSurface failed" );
966 else if( !p_sys->b_direct_rendering )
968 /* Not much to do in indirect rendering mode */
969 return avcodec_default_get_buffer( p_context, p_ff_pic );
972 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
973 * so we need to check for direct rendering again. */
975 int i_width = p_sys->p_context->width;
976 int i_height = p_sys->p_context->height;
977 avcodec_align_dimensions( p_sys->p_context, &i_width, &i_height );
979 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS ||
980 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 ||
981 /* We only pad picture up to 16 */
982 PAD(p_sys->p_context->width,16) < i_width || PAD(p_sys->p_context->height,16) < i_height ||
983 p_context->pix_fmt == PIX_FMT_PAL8 )
985 msg_Dbg( p_dec, "disabling direct rendering" );
986 p_sys->b_direct_rendering = false;
987 return avcodec_default_get_buffer( p_context, p_ff_pic );
989 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
991 /* Get a new picture */
992 //p_sys->p_vout->render.b_allow_modify_pics = 0;
993 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
996 p_sys->b_direct_rendering = false;
997 return avcodec_default_get_buffer( p_context, p_ff_pic );
999 p_sys->p_context->draw_horiz_band = NULL;
1001 p_ff_pic->opaque = (void*)p_pic;
1002 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1003 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
1004 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
1005 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
1006 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
1008 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
1009 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
1010 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
1011 p_ff_pic->linesize[3] = 0;
1013 decoder_LinkPicture( p_dec, p_pic );
1015 /* FIXME what is that, should give good value */
1016 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
1020 static int ffmpeg_ReGetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1022 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1026 p_ff_pic->pts = AV_NOPTS_VALUE;
1028 /* We always use default reget function, it works perfectly fine */
1029 i_ret = avcodec_default_reget_buffer( p_context, p_ff_pic );
1031 /* Set picture PTS if avcodec_default_reget_buffer didn't set it (through a
1032 * ffmpeg_GetFrameBuf call) */
1033 if( !i_ret && p_ff_pic->pts == AV_NOPTS_VALUE )
1034 ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
1039 static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic )
1041 decoder_sys_t *p_sys = p_dec->p_sys;
1043 /* Set picture PTS */
1044 if( p_sys->input_pts )
1046 p_ff_pic->pts = p_sys->input_pts;
1048 else if( p_sys->input_dts )
1050 /* Some demuxers only set the dts so let's try to find a useful
1051 * timestamp from this */
1052 if( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
1053 !p_ff_pic->reference || !p_sys->i_pts )
1055 p_ff_pic->pts = p_sys->input_dts;
1067 if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
1069 p_sys->input_pts = p_sys->input_dts = 0;
1073 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1076 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1077 decoder_sys_t *p_sys = p_dec->p_sys;
1081 VaUngrabSurface( p_sys->p_va, p_ff_pic );
1084 for( int i = 0; i < 4; i++ )
1085 p_ff_pic->data[i] = NULL;
1087 else if( !p_ff_pic->opaque )
1089 avcodec_default_release_buffer( p_context, p_ff_pic );
1093 picture_t *p_pic = (picture_t*)p_ff_pic->opaque;
1095 decoder_UnlinkPicture( p_dec, p_pic );
1098 for( int i = 0; i < 4; i++ )
1099 p_ff_pic->data[i] = NULL;
1103 static void ffmpeg_NextPts( decoder_t *p_dec )
1105 decoder_sys_t *p_sys = p_dec->p_sys;
1107 if( p_sys->i_pts <= 0 )
1110 /* interpolate the next PTS */
1111 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
1112 p_dec->fmt_in.video.i_frame_rate_base > 0 )
1114 p_sys->i_pts += INT64_C(1000000) *
1115 (2 + p_sys->p_ff_pic->repeat_pict) *
1116 p_dec->fmt_in.video.i_frame_rate_base /
1117 (2 * p_dec->fmt_in.video.i_frame_rate);
1119 else if( p_sys->p_context->time_base.den > 0 )
1121 p_sys->i_pts += INT64_C(1000000) *
1122 (2 + p_sys->p_ff_pic->repeat_pict) *
1123 p_sys->p_context->time_base.num /
1124 (2 * p_sys->p_context->time_base.den);
1128 #ifdef HAVE_AVCODEC_VAAPI
1129 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_codec,
1130 const enum PixelFormat *pi_fmt )
1132 decoder_t *p_dec = p_codec->opaque;
1133 decoder_sys_t *p_sys = p_dec->p_sys;
1137 VaDelete( p_sys->p_va );
1141 /* Try too look for a supported hw acceleration */
1142 for( int i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1144 static const char *ppsz_name[PIX_FMT_NB] = {
1145 [PIX_FMT_VDPAU_H264] = "PIX_FMT_VDPAU_H264",
1146 [PIX_FMT_VAAPI_IDCT] = "PIX_FMT_VAAPI_IDCT",
1147 [PIX_FMT_VAAPI_VLD] = "PIX_FMT_VAAPI_VLD",
1148 [PIX_FMT_VAAPI_MOCO] = "PIX_FMT_VAAPI_MOCO",
1149 [PIX_FMT_YUYV422] = "PIX_FMT_YUYV422",
1150 [PIX_FMT_YUV420P] = "PIX_FMT_YUV420P",
1152 msg_Dbg( p_dec, "Available decoder output format %d (%s)", pi_fmt[i], ppsz_name[pi_fmt[i]] ?: "Unknown" );
1154 /* Only VLD supported */
1155 if( pi_fmt[i] == PIX_FMT_VAAPI_VLD )
1157 msg_Dbg( p_dec, "Trying VA API" );
1158 p_sys->p_va = VaNew( p_sys->i_codec_id );
1161 /* FIXME this will disabled direct rendering
1162 * even if a new pixel format is renegociated
1164 * FIXME Try to call VaSetup when possible
1165 * to detect errors when possible (later is too late) */
1166 p_sys->b_direct_rendering = false;
1167 p_sys->p_context->draw_horiz_band = NULL;
1170 msg_Warn( p_dec, "Failed to open VA API" );
1174 /* Fallback to default behaviour */
1175 return avcodec_default_get_format( p_codec, pi_fmt );