1 /*****************************************************************************
2 * video.c: video decoder using the libavcodec library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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_avcodec.h>
36 #include <vlc_atomic.h>
39 #include <libavcodec/avcodec.h>
40 #include <libavutil/mem.h>
41 #include <libavutil/pixdesc.h>
42 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 16, 101 ) )
43 #include <libavutil/mastering_display_metadata.h>
49 /*****************************************************************************
50 * decoder_sys_t : decoder descriptor
51 *****************************************************************************/
54 AVCODEC_COMMON_MEMBERS
56 /* Video decoder specific part */
59 /* for frame skipping algo */
62 enum AVDiscard i_skip_frame;
64 /* how many decoded frames are late */
66 mtime_t i_late_frames_start;
67 mtime_t i_last_late_delay;
69 /* for direct rendering */
70 bool b_direct_rendering;
71 atomic_bool b_dr_failure;
73 /* Hack to force display of still pictures */
82 enum PixelFormat pix_fmt;
89 static inline void wait_mt(decoder_sys_t *sys)
91 vlc_sem_wait(&sys->sem_mt);
94 static inline void post_mt(decoder_sys_t *sys)
96 vlc_sem_post(&sys->sem_mt);
99 /*****************************************************************************
101 *****************************************************************************/
102 static void ffmpeg_InitCodec ( decoder_t * );
103 static int lavc_GetFrame(struct AVCodecContext *, AVFrame *, int);
104 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
105 const enum PixelFormat * );
106 static int DecodeVideo( decoder_t *, block_t * );
107 static void Flush( decoder_t * );
109 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
111 uint8_t *p = (uint8_t*)&fcc;
112 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
115 /*****************************************************************************
117 *****************************************************************************/
120 * Sets the decoder output format.
122 static int lavc_GetVideoFormat(decoder_t *dec, video_format_t *restrict fmt,
123 AVCodecContext *ctx, enum AVPixelFormat pix_fmt,
124 enum AVPixelFormat sw_pix_fmt)
126 int width = ctx->coded_width;
127 int height = ctx->coded_height;
129 video_format_Init(fmt, 0);
131 if (pix_fmt == sw_pix_fmt)
132 { /* software decoding */
133 int aligns[AV_NUM_DATA_POINTERS];
135 if (GetVlcChroma(fmt, pix_fmt))
138 /* The libavcodec palette can only be fetched when the first output
139 * frame is decoded. Assume that the current chroma is RGB32 while we
140 * are waiting for a valid palette. Indeed, fmt_out.video.p_palette
141 * doesn't trigger a new vout request, but a new chroma yes. */
142 if (pix_fmt == AV_PIX_FMT_PAL8 && !dec->fmt_out.video.p_palette)
143 fmt->i_chroma = VLC_CODEC_RGB32;
145 avcodec_align_dimensions2(ctx, &width, &height, aligns);
147 else /* hardware decoding */
148 fmt->i_chroma = vlc_va_GetChroma(pix_fmt, sw_pix_fmt);
150 if( width == 0 || height == 0 || width > 8192 || height > 8192 )
152 msg_Err(dec, "Invalid frame size %dx%d.", width, height);
153 return -1; /* invalid display size */
156 fmt->i_width = width;
157 fmt->i_height = height;
158 fmt->i_visible_width = ctx->width;
159 fmt->i_visible_height = ctx->height;
161 /* If an aspect-ratio was specified in the input format then force it */
162 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0)
164 fmt->i_sar_num = dec->fmt_in.video.i_sar_num;
165 fmt->i_sar_den = dec->fmt_in.video.i_sar_den;
169 fmt->i_sar_num = ctx->sample_aspect_ratio.num;
170 fmt->i_sar_den = ctx->sample_aspect_ratio.den;
172 if (fmt->i_sar_num == 0 || fmt->i_sar_den == 0)
173 fmt->i_sar_num = fmt->i_sar_den = 1;
176 if (dec->fmt_in.video.i_frame_rate > 0
177 && dec->fmt_in.video.i_frame_rate_base > 0)
179 fmt->i_frame_rate = dec->fmt_in.video.i_frame_rate;
180 fmt->i_frame_rate_base = dec->fmt_in.video.i_frame_rate_base;
182 else if (ctx->framerate.num > 0 && ctx->framerate.den > 0)
184 fmt->i_frame_rate = ctx->framerate.num;
185 fmt->i_frame_rate_base = ctx->framerate.den;
186 # if LIBAVCODEC_VERSION_MICRO < 100
187 // for some reason libav don't thinkg framerate presents actually same thing as in ffmpeg
188 fmt->i_frame_rate_base *= __MAX(ctx->ticks_per_frame, 1);
191 else if (ctx->time_base.num > 0 && ctx->time_base.den > 0)
193 fmt->i_frame_rate = ctx->time_base.den;
194 fmt->i_frame_rate_base = ctx->time_base.num
195 * __MAX(ctx->ticks_per_frame, 1);
198 if( ctx->color_range == AVCOL_RANGE_JPEG )
199 fmt->b_color_range_full = true;
201 switch( ctx->colorspace )
203 case AVCOL_SPC_BT709:
204 fmt->space = COLOR_SPACE_BT709;
206 case AVCOL_SPC_SMPTE170M:
207 case AVCOL_SPC_BT470BG:
208 fmt->space = COLOR_SPACE_BT601;
210 case AVCOL_SPC_BT2020_NCL:
211 case AVCOL_SPC_BT2020_CL:
212 fmt->space = COLOR_SPACE_BT2020;
218 switch( ctx->color_trc )
220 case AVCOL_TRC_LINEAR:
221 fmt->transfer = TRANSFER_FUNC_LINEAR;
223 case AVCOL_TRC_GAMMA22:
224 fmt->transfer = TRANSFER_FUNC_SRGB;
226 case AVCOL_TRC_BT709:
227 fmt->transfer = TRANSFER_FUNC_BT709;
229 case AVCOL_TRC_SMPTE170M:
230 case AVCOL_TRC_BT2020_10:
231 case AVCOL_TRC_BT2020_12:
232 fmt->transfer = TRANSFER_FUNC_BT2020;
234 #if LIBAVUTIL_VERSION_CHECK( 55, 14, 0, 31, 100)
235 case AVCOL_TRC_ARIB_STD_B67:
236 fmt->transfer = TRANSFER_FUNC_ARIB_B67;
239 #if LIBAVUTIL_VERSION_CHECK( 55, 17, 0, 37, 100)
240 case AVCOL_TRC_SMPTE2084:
241 fmt->transfer = TRANSFER_FUNC_SMPTE_ST2084;
243 case AVCOL_TRC_SMPTE240M:
244 fmt->transfer = TRANSFER_FUNC_SMPTE_240;
246 case AVCOL_TRC_GAMMA28:
247 fmt->transfer = TRANSFER_FUNC_BT470_BG;
254 switch( ctx->color_primaries )
256 case AVCOL_PRI_BT709:
257 fmt->primaries = COLOR_PRIMARIES_BT709;
259 case AVCOL_PRI_BT470BG:
260 fmt->primaries = COLOR_PRIMARIES_BT601_625;
262 case AVCOL_PRI_SMPTE170M:
263 case AVCOL_PRI_SMPTE240M:
264 fmt->primaries = COLOR_PRIMARIES_BT601_525;
266 case AVCOL_PRI_BT2020:
267 fmt->primaries = COLOR_PRIMARIES_BT2020;
273 switch( ctx->chroma_sample_location )
275 case AVCHROMA_LOC_LEFT:
276 fmt->chroma_location = CHROMA_LOCATION_LEFT;
278 case AVCHROMA_LOC_CENTER:
279 fmt->chroma_location = CHROMA_LOCATION_CENTER;
281 case AVCHROMA_LOC_TOPLEFT:
282 fmt->chroma_location = CHROMA_LOCATION_TOP_LEFT;
291 static int lavc_UpdateVideoFormat(decoder_t *dec, AVCodecContext *ctx,
292 enum AVPixelFormat fmt,
293 enum AVPixelFormat swfmt)
295 video_format_t fmt_out;
298 val = lavc_GetVideoFormat(dec, &fmt_out, ctx, fmt, swfmt);
302 fmt_out.p_palette = dec->fmt_out.video.p_palette;
303 dec->fmt_out.video.p_palette = NULL;
305 es_format_Clean(&dec->fmt_out);
306 es_format_Init(&dec->fmt_out, VIDEO_ES, fmt_out.i_chroma);
307 dec->fmt_out.video = fmt_out;
308 dec->fmt_out.video.orientation = dec->fmt_in.video.orientation;
309 dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
310 dec->fmt_out.video.pose = dec->fmt_in.video.pose;
311 if ( dec->fmt_in.video.mastering.max_luminance )
312 dec->fmt_out.video.mastering = dec->fmt_in.video.mastering;
313 dec->fmt_out.video.lighting = dec->fmt_in.video.lighting;
314 return decoder_UpdateVideoFormat(dec);
318 * Copies a picture from the libavcodec-allocate buffer to a picture_t.
319 * This is used when not in direct rendering mode.
321 static int lavc_CopyPicture(decoder_t *dec, picture_t *pic, AVFrame *frame)
323 decoder_sys_t *sys = dec->p_sys;
325 if (!FindVlcChroma(sys->p_context->pix_fmt))
327 const char *name = av_get_pix_fmt_name(sys->p_context->pix_fmt);
329 msg_Err(dec, "Unsupported decoded output format %d (%s)",
330 sys->p_context->pix_fmt, (name != NULL) ? name : "unknown");
334 for (int plane = 0; plane < pic->i_planes; plane++)
336 const uint8_t *src = frame->data[plane];
337 uint8_t *dst = pic->p[plane].p_pixels;
338 size_t src_stride = frame->linesize[plane];
339 size_t dst_stride = pic->p[plane].i_pitch;
340 size_t size = __MIN(src_stride, dst_stride);
342 for (int line = 0; line < pic->p[plane].i_visible_lines; line++)
344 memcpy(dst, src, size);
352 static int OpenVideoCodec( decoder_t *p_dec )
354 decoder_sys_t *p_sys = p_dec->p_sys;
357 if( p_sys->p_context->extradata_size <= 0 )
359 if( p_sys->p_codec->id == AV_CODEC_ID_VC1 ||
360 p_sys->p_codec->id == AV_CODEC_ID_THEORA )
362 msg_Warn( p_dec, "waiting for extra data for codec %s",
363 p_sys->p_codec->name );
368 p_sys->p_context->width = p_dec->fmt_in.video.i_visible_width;
369 p_sys->p_context->height = p_dec->fmt_in.video.i_visible_height;
371 p_sys->p_context->coded_width = p_dec->fmt_in.video.i_width;
372 p_sys->p_context->coded_height = p_dec->fmt_in.video.i_height;
374 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
375 p_sys->pix_fmt = AV_PIX_FMT_NONE;
380 ret = ffmpeg_OpenCodec( p_dec );
385 switch( p_sys->p_context->active_thread_type )
387 case FF_THREAD_FRAME:
388 msg_Dbg( p_dec, "using frame thread mode with %d threads",
389 p_sys->p_context->thread_count );
391 case FF_THREAD_SLICE:
392 msg_Dbg( p_dec, "using slice thread mode with %d threads",
393 p_sys->p_context->thread_count );
396 if( p_sys->p_context->thread_count > 1 )
397 msg_Warn( p_dec, "failed to enable threaded decoding" );
400 msg_Warn( p_dec, "using unknown thread mode with %d threads",
401 p_sys->p_context->thread_count );
407 /*****************************************************************************
408 * InitVideo: initialize the video decoder
409 *****************************************************************************
410 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
411 * opened (done after the first decoded frame).
412 *****************************************************************************/
413 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
414 const AVCodec *p_codec )
416 decoder_sys_t *p_sys;
419 /* Allocate the memory needed to store the decoder's structure */
420 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
423 p_sys->p_context = p_context;
424 p_sys->p_codec = p_codec;
425 p_sys->b_delayed_open = true;
427 vlc_sem_init( &p_sys->sem_mt, 0 );
429 /* ***** Fill p_context with init values ***** */
430 p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?
431 p_dec->fmt_in.i_original_fourcc : p_dec->fmt_in.i_codec );
433 /* ***** Get configuration of ffmpeg plugin ***** */
434 p_context->workaround_bugs =
435 var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
436 p_context->err_recognition =
437 var_InheritInteger( p_dec, "avcodec-error-resilience" );
439 if( var_CreateGetBool( p_dec, "grayscale" ) )
440 p_context->flags |= AV_CODEC_FLAG_GRAY;
442 /* ***** Output always the frames ***** */
443 p_context->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT;
445 i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
446 if( i_val >= 4 ) p_context->skip_loop_filter = AVDISCARD_ALL;
447 else if( i_val == 3 ) p_context->skip_loop_filter = AVDISCARD_NONKEY;
448 else if( i_val == 2 ) p_context->skip_loop_filter = AVDISCARD_BIDIR;
449 else if( i_val == 1 ) p_context->skip_loop_filter = AVDISCARD_NONREF;
450 else p_context->skip_loop_filter = AVDISCARD_DEFAULT;
452 if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
453 p_context->flags2 |= AV_CODEC_FLAG2_FAST;
455 /* ***** libavcodec frame skipping ***** */
456 p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
458 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-frame" );
459 if( i_val >= 4 ) p_context->skip_frame = AVDISCARD_ALL;
460 else if( i_val == 3 ) p_context->skip_frame = AVDISCARD_NONKEY;
461 else if( i_val == 2 ) p_context->skip_frame = AVDISCARD_BIDIR;
462 else if( i_val == 1 ) p_context->skip_frame = AVDISCARD_NONREF;
463 else if( i_val == -1 ) p_context->skip_frame = AVDISCARD_NONE;
464 else p_context->skip_frame = AVDISCARD_DEFAULT;
465 p_sys->i_skip_frame = p_context->skip_frame;
467 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-idct" );
468 if( i_val >= 4 ) p_context->skip_idct = AVDISCARD_ALL;
469 else if( i_val == 3 ) p_context->skip_idct = AVDISCARD_NONKEY;
470 else if( i_val == 2 ) p_context->skip_idct = AVDISCARD_BIDIR;
471 else if( i_val == 1 ) p_context->skip_idct = AVDISCARD_NONREF;
472 else if( i_val == -1 ) p_context->skip_idct = AVDISCARD_NONE;
473 else p_context->skip_idct = AVDISCARD_DEFAULT;
475 /* ***** libavcodec direct rendering ***** */
476 p_sys->b_direct_rendering = false;
477 atomic_init(&p_sys->b_dr_failure, false);
478 if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
479 (p_codec->capabilities & AV_CODEC_CAP_DR1) &&
480 /* No idea why ... but this fixes flickering on some TSCC streams */
481 p_sys->p_codec->id != AV_CODEC_ID_TSCC &&
482 p_sys->p_codec->id != AV_CODEC_ID_CSCD &&
483 p_sys->p_codec->id != AV_CODEC_ID_CINEPAK )
485 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
486 * so we need to do another check in ffmpeg_GetFrameBuf() */
487 p_sys->b_direct_rendering = true;
490 p_context->get_format = ffmpeg_GetFormat;
491 /* Always use our get_buffer wrapper so we can calculate the
493 p_context->get_buffer2 = lavc_GetFrame;
494 p_context->refcounted_frames = true;
495 p_context->opaque = p_dec;
497 int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
498 if( i_thread_count <= 0 )
500 i_thread_count = vlc_GetCPUCount();
501 if( i_thread_count > 1 )
504 //FIXME: take in count the decoding time
505 i_thread_count = __MIN( i_thread_count, p_codec->id == AV_CODEC_ID_HEVC ? 6 : 4 );
507 i_thread_count = __MIN( i_thread_count, 16 );
508 msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
509 p_context->thread_count = i_thread_count;
510 p_context->thread_safe_callbacks = true;
512 switch( p_codec->id )
514 case AV_CODEC_ID_MPEG4:
515 case AV_CODEC_ID_H263:
516 p_context->thread_type = 0;
518 case AV_CODEC_ID_MPEG1VIDEO:
519 case AV_CODEC_ID_MPEG2VIDEO:
520 p_context->thread_type &= ~FF_THREAD_SLICE;
522 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
523 case AV_CODEC_ID_H264:
524 case AV_CODEC_ID_VC1:
525 case AV_CODEC_ID_WMV3:
526 p_context->thread_type &= ~FF_THREAD_FRAME;
532 if( p_context->thread_type & FF_THREAD_FRAME )
533 p_dec->i_extra_picture_buffers = 2 * p_context->thread_count;
535 /* ***** misc init ***** */
536 p_sys->i_pts = VLC_TS_INVALID;
537 p_sys->b_first_frame = true;
538 p_sys->i_late_frames = 0;
539 p_sys->b_from_preroll = false;
541 /* Set output properties */
542 p_dec->fmt_out.i_cat = VIDEO_ES;
543 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
545 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
546 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
548 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
550 p_dec->fmt_out.video.orientation = p_dec->fmt_in.video.orientation;
552 if( p_dec->fmt_in.video.p_palette ) {
553 p_sys->palette_sent = false;
554 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
555 if( p_dec->fmt_out.video.p_palette )
556 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
558 p_sys->palette_sent = true;
560 /* ***** init this codec with special data ***** */
561 ffmpeg_InitCodec( p_dec );
563 /* ***** Open the codec ***** */
564 if( OpenVideoCodec( p_dec ) < 0 )
566 vlc_sem_destroy( &p_sys->sem_mt );
571 p_dec->pf_decode = DecodeVideo;
572 p_dec->pf_flush = Flush;
577 /*****************************************************************************
579 *****************************************************************************/
580 static void Flush( decoder_t *p_dec )
582 decoder_sys_t *p_sys = p_dec->p_sys;
583 AVCodecContext *p_context = p_sys->p_context;
585 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
586 p_sys->i_late_frames = 0;
588 /* Abort pictures in order to unblock all avcodec workers threads waiting
589 * for a picture. This will avoid a deadlock between avcodec_flush_buffers
590 * and workers threads */
591 decoder_AbortPictures( p_dec, true );
594 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
595 if( avcodec_is_open( p_context ) )
596 avcodec_flush_buffers( p_context );
599 /* Reset cancel state to false */
600 decoder_AbortPictures( p_dec, false );
603 static bool check_block_validity( decoder_sys_t *p_sys, block_t *block )
608 if( block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
610 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
612 p_sys->i_late_frames = 0;
613 if( block->i_flags & BLOCK_FLAG_CORRUPTED )
615 block_Release( block );
622 static bool check_block_being_late( decoder_sys_t *p_sys, block_t *block, mtime_t current_time)
626 if( block->i_flags & BLOCK_FLAG_PREROLL )
628 /* Do not care about late frames when prerolling
629 * TODO avoid decoding of non reference frame
630 * (ie all B except for H264 where it depends only on nal_ref_idc) */
631 p_sys->i_late_frames = 0;
632 p_sys->b_from_preroll = true;
633 p_sys->i_last_late_delay = INT64_MAX;
636 if( p_sys->i_late_frames <= 0 )
639 if( current_time - p_sys->i_late_frames_start > (5*CLOCK_FREQ))
641 if( p_sys->i_pts > VLC_TS_INVALID )
643 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
646 block_Release( block );
647 p_sys->i_late_frames--;
653 static bool check_frame_should_be_dropped( decoder_sys_t *p_sys, AVCodecContext *p_context, bool *b_need_output_picture )
655 if( p_sys->i_late_frames <= 4)
658 *b_need_output_picture = false;
659 if( p_sys->i_late_frames < 12 )
661 p_context->skip_frame =
662 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
663 AVDISCARD_NONREF : p_sys->i_skip_frame;
667 /* picture too late, won't decode
668 * but break picture until a new I, and for mpeg4 ...*/
669 p_sys->i_late_frames--; /* needed else it will never be decrease */
675 static void interpolate_next_pts( decoder_t *p_dec, AVFrame *frame )
677 decoder_sys_t *p_sys = p_dec->p_sys;
678 AVCodecContext *p_context = p_sys->p_context;
680 if( p_sys->i_pts <= VLC_TS_INVALID )
683 /* interpolate the next PTS */
684 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
685 p_dec->fmt_in.video.i_frame_rate_base > 0 )
687 p_sys->i_pts += CLOCK_FREQ * (2 + frame->repeat_pict) *
688 p_dec->fmt_in.video.i_frame_rate_base /
689 (2 * p_dec->fmt_in.video.i_frame_rate);
691 else if( p_context->time_base.den > 0 )
693 int i_tick = p_context->ticks_per_frame;
697 p_sys->i_pts += CLOCK_FREQ * (2 + frame->repeat_pict) *
698 i_tick * p_context->time_base.num /
699 (2 * p_context->time_base.den);
703 static void update_late_frame_count( decoder_t *p_dec, block_t *p_block, mtime_t current_time, mtime_t i_pts )
705 decoder_sys_t *p_sys = p_dec->p_sys;
706 /* Update frame late count (except when doing preroll) */
707 mtime_t i_display_date = VLC_TS_INVALID;
708 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
709 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
711 if( i_display_date > VLC_TS_INVALID && i_display_date <= current_time )
713 /* Out of preroll, consider only late frames on rising delay */
714 if( p_sys->b_from_preroll )
716 if( p_sys->i_last_late_delay > current_time - i_display_date )
718 p_sys->i_last_late_delay = current_time - i_display_date;
721 p_sys->b_from_preroll = false;
724 p_sys->i_late_frames++;
725 if( p_sys->i_late_frames == 1 )
726 p_sys->i_late_frames_start = current_time;
731 p_sys->i_late_frames = 0;
736 /*****************************************************************************
737 * DecodeBlock: Called to decode one or more frames
738 *****************************************************************************/
739 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block, bool *error )
741 decoder_sys_t *p_sys = p_dec->p_sys;
742 AVCodecContext *p_context = p_sys->p_context;
743 /* Boolean if we assume that we should get valid pic as result */
744 bool b_need_output_picture = true;
746 /* Boolean for END_OF_SEQUENCE */
747 bool eos_spotted = false;
751 mtime_t current_time = VLC_TS_INVALID;
753 if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
755 ffmpeg_InitCodec( p_dec );
756 if( p_sys->b_delayed_open )
757 OpenVideoCodec( p_dec );
760 p_block = pp_block ? *pp_block : NULL;
761 if(!p_block && !(p_sys->p_codec->capabilities & AV_CODEC_CAP_DELAY) )
764 if( p_sys->b_delayed_open )
767 block_Release( p_block );
771 if( !check_block_validity( p_sys, p_block ) )
774 current_time = mdate();
775 if( p_dec->b_frame_drop_allowed && check_block_being_late( p_sys, p_block, current_time) )
777 msg_Err( p_dec, "more than 5 seconds of late video -> "
778 "dropping frame (computer too slow ?)" );
783 /* A good idea could be to decode all I pictures and see for the other */
785 /* Defaults that if we aren't in prerolling, we want output picture
786 same for if we are flushing (p_block==NULL) */
787 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
788 b_need_output_picture = true;
790 b_need_output_picture = false;
792 /* Change skip_frame config only if hurry_up is enabled */
793 if( p_sys->b_hurry_up )
795 p_context->skip_frame = p_sys->i_skip_frame;
797 /* Check also if we should/can drop the block and move to next block
798 as trying to catchup the speed*/
799 if( p_dec->b_frame_drop_allowed &&
800 check_frame_should_be_dropped( p_sys, p_context, &b_need_output_picture ) )
803 block_Release( p_block );
804 msg_Warn( p_dec, "More than 11 late frames, dropping frame" );
808 if( !b_need_output_picture )
810 p_context->skip_frame = __MAX( p_context->skip_frame,
815 * Do the actual decoding now */
817 /* Don't forget that libavcodec requires a little more bytes
818 * that the real frame size */
819 if( p_block && p_block->i_buffer > 0 )
821 eos_spotted = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
823 p_block = block_Realloc( p_block, 0,
824 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
827 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
829 memset( p_block->p_buffer + p_block->i_buffer, 0,
830 FF_INPUT_BUFFER_PADDING_SIZE );
833 while( !p_block || p_block->i_buffer > 0 || eos_spotted )
840 av_init_packet( &pkt );
843 pkt.data = p_block->p_buffer;
844 pkt.size = p_block->i_buffer;
845 pkt.pts = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : AV_NOPTS_VALUE;
846 pkt.dts = p_block->i_dts > VLC_TS_INVALID ? p_block->i_dts : AV_NOPTS_VALUE;
850 /* Return delayed frames if codec has CODEC_CAP_DELAY */
855 if( !p_sys->palette_sent )
857 uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
859 memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE);
860 p_sys->palette_sent = true;
864 /* Make sure we don't reuse the same timestamps twice */
868 p_block->i_dts = VLC_TS_INVALID;
871 #if LIBAVCODEC_VERSION_CHECK( 57, 0, 0xFFFFFFFFU, 64, 101 )
872 if( !b_need_output_picture )
873 pkt.flags |= AV_PKT_FLAG_DISCARD;
876 int ret = avcodec_send_packet(p_context, &pkt);
877 if( ret != 0 && ret != AVERROR(EAGAIN) )
879 if (ret == AVERROR(ENOMEM) || ret == AVERROR(EINVAL))
881 msg_Err(p_dec, "avcodec_send_packet critical error");
884 av_packet_unref( &pkt );
887 i_used = ret != AVERROR(EAGAIN) ? pkt.size : 0;
888 av_packet_unref( &pkt );
890 AVFrame *frame = av_frame_alloc();
891 if (unlikely(frame == NULL))
897 ret = avcodec_receive_frame(p_context, frame);
898 if( ret != 0 && ret != AVERROR(EAGAIN) )
900 if (ret == AVERROR(ENOMEM) || ret == AVERROR(EINVAL))
902 msg_Err(p_dec, "avcodec_receive_frame critical error");
905 av_frame_free(&frame);
908 bool not_received_frame = ret;
913 p_sys->b_first_frame = true;
917 if( p_block->i_buffer <= 0 )
921 p_block->p_buffer += i_used;
922 p_block->i_buffer -= i_used;
925 /* Nothing to display */
926 if( not_received_frame )
928 av_frame_free(&frame);
929 if( i_used == 0 ) break;
933 /* Compute the PTS */
934 #ifdef FF_API_PKT_PTS
935 mtime_t i_pts = frame->pts;
937 mtime_t i_pts = frame->pkt_pts;
939 if (i_pts == AV_NOPTS_VALUE )
940 i_pts = frame->pkt_dts;
942 if( i_pts == AV_NOPTS_VALUE )
943 i_pts = p_sys->i_pts;
945 /* Interpolate the next PTS */
946 if( i_pts > VLC_TS_INVALID )
947 p_sys->i_pts = i_pts;
949 interpolate_next_pts( p_dec, frame );
951 update_late_frame_count( p_dec, p_block, current_time, i_pts);
953 if( ( !p_sys->p_va && !frame->linesize[0] ) ||
954 ( p_dec->b_frame_drop_allowed && (frame->flags & AV_FRAME_FLAG_CORRUPT) ) )
956 av_frame_free(&frame);
960 #if !LIBAVCODEC_VERSION_CHECK( 57, 0, 0xFFFFFFFFU, 64, 101 )
961 if( !b_need_output_picture )
963 av_frame_free(&frame);
968 if( p_context->pix_fmt == AV_PIX_FMT_PAL8
969 && !p_dec->fmt_out.video.p_palette )
971 /* See AV_PIX_FMT_PAL8 comment in avc_GetVideoFormat(): update the
972 * fmt_out palette and change the fmt_out chroma to request a new
974 assert( p_dec->fmt_out.video.i_chroma != VLC_CODEC_RGBP );
976 video_palette_t *p_palette;
977 p_palette = p_dec->fmt_out.video.p_palette
978 = malloc( sizeof(video_palette_t) );
982 av_frame_free(&frame);
985 static_assert( sizeof(p_palette->palette) == AVPALETTE_SIZE,
986 "Palette size mismatch between vlc and libavutil" );
987 assert( frame->data[1] != NULL );
988 memcpy( p_palette->palette, frame->data[1], AVPALETTE_SIZE );
989 p_palette->i_entries = AVPALETTE_COUNT;
990 p_dec->fmt_out.video.i_chroma = VLC_CODEC_RGBP;
991 if( decoder_UpdateVideoFormat( p_dec ) )
993 av_frame_free(&frame);
998 picture_t *p_pic = frame->opaque;
1000 { /* When direct rendering is not used, get_format() and get_buffer()
1001 * might not be called. The output video format must be set here
1002 * then picture buffer can be allocated. */
1003 if (p_sys->p_va == NULL
1004 && lavc_UpdateVideoFormat(p_dec, p_context, p_context->pix_fmt,
1005 p_context->pix_fmt) == 0)
1006 p_pic = decoder_NewPicture(p_dec);
1010 av_frame_free(&frame);
1014 /* Fill picture_t from AVFrame */
1015 if( lavc_CopyPicture( p_dec, p_pic, frame ) != VLC_SUCCESS )
1018 av_frame_free(&frame);
1019 picture_Release( p_pic );
1025 if( p_sys->p_va != NULL )
1026 vlc_va_Extract( p_sys->p_va, p_pic, frame->data[3] );
1027 picture_Hold( p_pic );
1030 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
1032 /* Fetch again the aspect ratio in case it changed */
1033 p_dec->fmt_out.video.i_sar_num
1034 = p_context->sample_aspect_ratio.num;
1035 p_dec->fmt_out.video.i_sar_den
1036 = p_context->sample_aspect_ratio.den;
1038 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
1040 p_dec->fmt_out.video.i_sar_num = 1;
1041 p_dec->fmt_out.video.i_sar_den = 1;
1045 p_pic->date = i_pts;
1046 /* Hack to force display of still pictures */
1047 p_pic->b_force = p_sys->b_first_frame;
1048 p_pic->i_nb_fields = 2 + frame->repeat_pict;
1049 p_pic->b_progressive = !frame->interlaced_frame;
1050 p_pic->b_top_field_first = frame->top_field_first;
1052 bool format_changed = false;
1053 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 16, 101 ) )
1054 #define FROM_AVRAT(default_factor, avrat) \
1055 (uint64_t)(default_factor) * (avrat).num / (avrat).den
1056 const AVFrameSideData *metadata =
1057 av_frame_get_side_data( frame,
1058 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA );
1061 const AVMasteringDisplayMetadata *hdr_meta =
1062 (const AVMasteringDisplayMetadata *) metadata->data;
1063 if ( hdr_meta->has_luminance )
1065 #define ST2086_LUMA_FACTOR 10000
1066 p_pic->format.mastering.max_luminance =
1067 FROM_AVRAT(ST2086_LUMA_FACTOR, hdr_meta->max_luminance);
1068 p_pic->format.mastering.min_luminance =
1069 FROM_AVRAT(ST2086_LUMA_FACTOR, hdr_meta->min_luminance);
1071 if ( hdr_meta->has_primaries )
1073 #define ST2086_RED 2
1074 #define ST2086_GREEN 0
1075 #define ST2086_BLUE 1
1079 #define ST2086_PRIM_FACTOR 50000
1080 p_pic->format.mastering.primaries[ST2086_RED*2 + 0] =
1081 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_RED][0]);
1082 p_pic->format.mastering.primaries[ST2086_RED*2 + 1] =
1083 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_RED][1]);
1084 p_pic->format.mastering.primaries[ST2086_GREEN*2 + 0] =
1085 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_GREEN][0]);
1086 p_pic->format.mastering.primaries[ST2086_GREEN*2 + 1] =
1087 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_GREEN][1]);
1088 p_pic->format.mastering.primaries[ST2086_BLUE*2 + 0] =
1089 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_BLUE][0]);
1090 p_pic->format.mastering.primaries[ST2086_BLUE*2 + 1] =
1091 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_BLUE][1]);
1092 p_pic->format.mastering.white_point[0] =
1093 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->white_point[0]);
1094 p_pic->format.mastering.white_point[1] =
1095 FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->white_point[1]);
1098 if ( memcmp( &p_dec->fmt_out.video.mastering,
1099 &p_pic->format.mastering,
1100 sizeof(p_pic->format.mastering) ) )
1102 p_dec->fmt_out.video.mastering = p_pic->format.mastering;
1103 format_changed = true;
1108 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 60, 100 ) )
1109 const AVFrameSideData *metadata_lt =
1110 av_frame_get_side_data( frame,
1111 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL );
1114 const AVContentLightMetadata *light_meta =
1115 (const AVContentLightMetadata *) metadata_lt->data;
1116 p_pic->format.lighting.MaxCLL = light_meta->MaxCLL;
1117 p_pic->format.lighting.MaxFALL = light_meta->MaxFALL;
1118 if ( memcmp( &p_dec->fmt_out.video.lighting,
1119 &p_pic->format.lighting,
1120 sizeof(p_pic->format.lighting) ) )
1122 p_dec->fmt_out.video.lighting = p_pic->format.lighting;
1123 format_changed = true;
1127 av_frame_free(&frame);
1130 decoder_UpdateVideoFormat( p_dec );
1132 /* Send decoded frame to vout */
1133 if (i_pts > VLC_TS_INVALID)
1135 p_sys->b_first_frame = false;
1139 picture_Release( p_pic );
1143 block_Release( p_block );
1147 static int DecodeVideo( decoder_t *p_dec, block_t *p_block )
1149 block_t **pp_block = p_block ? &p_block : NULL;
1152 while( ( p_pic = DecodeBlock( p_dec, pp_block, &error ) ) != NULL )
1153 decoder_QueueVideo( p_dec, p_pic );
1154 return error ? VLCDEC_ECRITICAL : VLCDEC_SUCCESS;
1157 /*****************************************************************************
1158 * EndVideo: decoder destruction
1159 *****************************************************************************
1160 * This function is called when the thread ends after a successful
1162 *****************************************************************************/
1163 void EndVideoDec( decoder_t *p_dec )
1165 decoder_sys_t *p_sys = p_dec->p_sys;
1169 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
1170 if( avcodec_is_open( p_sys->p_context ) )
1171 avcodec_flush_buffers( p_sys->p_context );
1175 ffmpeg_CloseCodec( p_dec );
1178 vlc_va_Delete( p_sys->p_va, p_sys->p_context );
1180 vlc_sem_destroy( &p_sys->sem_mt );
1183 /*****************************************************************************
1184 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
1185 *****************************************************************************/
1186 static void ffmpeg_InitCodec( decoder_t *p_dec )
1188 decoder_sys_t *p_sys = p_dec->p_sys;
1189 int i_size = p_dec->fmt_in.i_extra;
1191 if( !i_size ) return;
1193 if( p_sys->p_codec->id == AV_CODEC_ID_SVQ3 )
1197 p_sys->p_context->extradata_size = i_size + 12;
1198 p = p_sys->p_context->extradata =
1199 av_malloc( p_sys->p_context->extradata_size +
1200 FF_INPUT_BUFFER_PADDING_SIZE );
1204 memcpy( &p[0], "SVQ3", 4 );
1205 memset( &p[4], 0, 8 );
1206 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
1208 /* Now remove all atoms before the SMI one */
1209 if( p_sys->p_context->extradata_size > 0x5a &&
1210 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
1212 uint8_t *psz = &p[0x52];
1214 while( psz < &p[p_sys->p_context->extradata_size - 8] )
1216 int i_size = GetDWBE( psz );
1219 /* FIXME handle 1 as long size */
1222 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
1224 memmove( &p[0x52], psz,
1225 &p[p_sys->p_context->extradata_size] - psz );
1235 p_sys->p_context->extradata_size = i_size;
1236 p_sys->p_context->extradata =
1237 av_malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
1238 if( p_sys->p_context->extradata )
1240 memcpy( p_sys->p_context->extradata,
1241 p_dec->fmt_in.p_extra, i_size );
1242 memset( p_sys->p_context->extradata + i_size,
1243 0, FF_INPUT_BUFFER_PADDING_SIZE );
1248 static void lavc_ReleaseFrame(void *opaque, uint8_t *data)
1251 picture_t *picture = opaque;
1253 picture_Release(picture);
1256 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
1259 decoder_t *dec = ctx->opaque;
1260 vlc_va_t *va = dec->p_sys->p_va;
1262 if (vlc_va_Get(va, pic, &frame->data[0]))
1264 msg_Err(dec, "hardware acceleration picture allocation failed");
1265 picture_Release(pic);
1268 assert(frame->data[0] != NULL);
1269 /* data[0] must be non-NULL for libavcodec internal checks.
1270 * data[3] actually contains the format-specific surface handle. */
1271 frame->data[3] = frame->data[0];
1273 void (*release)(void *, uint8_t *) = va->release;
1274 if (va->release == NULL)
1275 release = lavc_ReleaseFrame;
1277 frame->buf[0] = av_buffer_create(frame->data[0], 0, release, pic, 0);
1278 if (unlikely(frame->buf[0] == NULL))
1280 release(pic, frame->data[0]);
1284 frame->opaque = pic;
1288 static int lavc_dr_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
1291 decoder_t *dec = (decoder_t *)ctx->opaque;
1292 decoder_sys_t *sys = dec->p_sys;
1294 if (ctx->pix_fmt == AV_PIX_FMT_PAL8)
1297 int width = frame->width;
1298 int height = frame->height;
1299 int aligns[AV_NUM_DATA_POINTERS];
1301 avcodec_align_dimensions2(ctx, &width, &height, aligns);
1303 /* Check that the picture is suitable for libavcodec */
1304 assert(pic->p[0].i_pitch >= width * pic->p[0].i_pixel_pitch);
1305 assert(pic->p[0].i_lines >= height);
1307 for (int i = 0; i < pic->i_planes; i++)
1309 if (pic->p[i].i_pitch % aligns[i])
1311 if (!atomic_exchange(&sys->b_dr_failure, true))
1312 msg_Warn(dec, "plane %d: pitch not aligned (%d%%%d): disabling direct rendering",
1313 i, pic->p[i].i_pitch, aligns[i]);
1316 if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1318 if (!atomic_exchange(&sys->b_dr_failure, true))
1319 msg_Warn(dec, "plane %d not aligned: disabling direct rendering", i);
1324 /* Allocate buffer references and initialize planes */
1325 assert(pic->i_planes < PICTURE_PLANE_MAX);
1326 static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1328 for (int i = 0; i < pic->i_planes; i++)
1330 uint8_t *data = pic->p[i].p_pixels;
1331 int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1333 frame->data[i] = data;
1334 frame->linesize[i] = pic->p[i].i_pitch;
1335 frame->buf[i] = av_buffer_create(data, size, lavc_ReleaseFrame,
1337 if (unlikely(frame->buf[i] == NULL))
1340 av_buffer_unref(&frame->buf[--i]);
1346 frame->opaque = pic;
1347 /* The loop above held one reference to the picture for each plane. */
1348 picture_Release(pic);
1351 picture_Release(pic);
1356 * Callback used by libavcodec to get a frame buffer.
1358 * It is used for direct rendering as well as to get the right PTS for each
1359 * decoded picture (even in indirect rendering mode).
1361 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1363 decoder_t *dec = ctx->opaque;
1364 decoder_sys_t *sys = dec->p_sys;
1367 for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1369 frame->data[i] = NULL;
1370 frame->linesize[i] = 0;
1371 frame->buf[i] = NULL;
1373 frame->opaque = NULL;
1376 if (sys->p_va == NULL)
1378 if (!sys->b_direct_rendering)
1381 return avcodec_default_get_buffer2(ctx, frame, flags);
1384 /* Most unaccelerated decoders do not call get_format(), so we need to
1385 * update the output video format here. The MT semaphore must be held
1386 * to protect p_dec->fmt_out. */
1387 if (lavc_UpdateVideoFormat(dec, ctx, ctx->pix_fmt, ctx->pix_fmt))
1395 pic = decoder_NewPicture(dec);
1399 if (sys->p_va != NULL)
1400 return lavc_va_GetFrame(ctx, frame, pic);
1402 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1403 * so we need to check for direct rendering again. */
1404 int ret = lavc_dr_GetFrame(ctx, frame, pic);
1406 ret = avcodec_default_get_buffer2(ctx, frame, flags);
1410 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1411 const enum PixelFormat *pi_fmt )
1413 decoder_t *p_dec = p_context->opaque;
1414 decoder_sys_t *p_sys = p_dec->p_sys;
1417 /* Enumerate available formats */
1418 enum PixelFormat swfmt = avcodec_default_get_format(p_context, pi_fmt);
1419 bool can_hwaccel = false;
1421 for( size_t i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++ )
1423 const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1426 bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1428 msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1429 hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1434 /* If the format did not actually change (e.g. seeking), try to reuse the
1435 * existing output format, and if present, hardware acceleration back-end.
1436 * This avoids resetting the pipeline downstream. This also avoids
1437 * needlessly probing for hardware acceleration support. */
1438 if (p_sys->pix_fmt != AV_PIX_FMT_NONE
1439 && lavc_GetVideoFormat(p_dec, &fmt, p_context, p_sys->pix_fmt, swfmt) == 0
1440 && fmt.i_width == p_dec->fmt_out.video.i_width
1441 && fmt.i_height == p_dec->fmt_out.video.i_height
1442 && p_context->profile == p_sys->profile
1443 && p_context->level <= p_sys->level)
1445 for (size_t i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++)
1446 if (pi_fmt[i] == p_sys->pix_fmt)
1448 msg_Dbg(p_dec, "reusing decoder output format %d", pi_fmt[i]);
1449 return p_sys->pix_fmt;
1453 if (p_sys->p_va != NULL)
1455 msg_Err(p_dec, "existing hardware acceleration cannot be reused");
1456 vlc_va_Delete(p_sys->p_va, p_context);
1460 p_sys->profile = p_context->profile;
1461 p_sys->level = p_context->level;
1466 #if (LIBAVCODEC_VERSION_MICRO >= 100) \
1467 && (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 83, 101))
1468 if (p_context->active_thread_type)
1470 msg_Warn(p_dec, "thread type %d: disabling hardware acceleration",
1471 p_context->active_thread_type);
1478 for( size_t i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++ )
1480 enum PixelFormat hwfmt = pi_fmt[i];
1482 p_dec->fmt_out.video.i_chroma = vlc_va_GetChroma(hwfmt, swfmt);
1483 if (p_dec->fmt_out.video.i_chroma == 0)
1484 continue; /* Unknown brand of hardware acceleration */
1485 if (p_context->width == 0 || p_context->height == 0)
1486 { /* should never happen */
1487 msg_Err(p_dec, "unspecified video dimensions");
1490 if (lavc_UpdateVideoFormat(p_dec, p_context, hwfmt, swfmt))
1491 continue; /* Unsupported brand of hardware acceleration */
1494 picture_t *test_pic = decoder_NewPicture(p_dec);
1495 assert(!test_pic || test_pic->format.i_chroma == p_dec->fmt_out.video.i_chroma);
1496 vlc_va_t *va = vlc_va_New(VLC_OBJECT(p_dec), p_context, hwfmt,
1498 test_pic ? test_pic->p_sys : NULL);
1500 picture_Release(test_pic);
1504 continue; /* Unsupported codec profile or such */
1507 if (va->description != NULL)
1508 msg_Info(p_dec, "Using %s for hardware decoding", va->description);
1511 p_sys->pix_fmt = hwfmt;
1512 p_context->draw_horiz_band = NULL;
1517 /* Fallback to default behaviour */
1518 p_sys->pix_fmt = swfmt;