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 #include "../codec/cc.h"
51 /*****************************************************************************
52 * decoder_sys_t : decoder descriptor
53 *****************************************************************************/
56 AVCODEC_COMMON_MEMBERS
58 /* Video decoder specific part */
61 /* Closed captions for decoders */
64 /* for frame skipping algo */
66 bool b_show_corrupted
;
68 enum AVDiscard i_skip_frame
;
70 /* how many decoded frames are late */
72 mtime_t i_late_frames_start
;
73 mtime_t i_last_late_delay
;
75 /* for direct rendering */
76 bool b_direct_rendering
;
77 atomic_bool b_dr_failure
;
79 /* Hack to force display of still pictures */
88 enum PixelFormat pix_fmt
;
95 static inline void wait_mt(decoder_sys_t
*sys
)
97 vlc_sem_wait(&sys
->sem_mt
);
100 static inline void post_mt(decoder_sys_t
*sys
)
102 vlc_sem_post(&sys
->sem_mt
);
105 /*****************************************************************************
107 *****************************************************************************/
108 static void ffmpeg_InitCodec ( decoder_t
* );
109 static int lavc_GetFrame(struct AVCodecContext
*, AVFrame
*, int);
110 static enum PixelFormat
ffmpeg_GetFormat( AVCodecContext
*,
111 const enum PixelFormat
* );
112 static int DecodeVideo( decoder_t
*, block_t
* );
113 static void Flush( decoder_t
* );
115 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc
)
117 uint8_t *p
= (uint8_t*)&fcc
;
118 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
121 /*****************************************************************************
123 *****************************************************************************/
126 * Sets the decoder output format.
128 static int lavc_GetVideoFormat(decoder_t
*dec
, video_format_t
*restrict fmt
,
129 AVCodecContext
*ctx
, enum AVPixelFormat pix_fmt
,
130 enum AVPixelFormat sw_pix_fmt
)
132 int width
= ctx
->coded_width
;
133 int height
= ctx
->coded_height
;
135 video_format_Init(fmt
, 0);
137 if (pix_fmt
== sw_pix_fmt
)
138 { /* software decoding */
139 int aligns
[AV_NUM_DATA_POINTERS
];
141 if (GetVlcChroma(fmt
, pix_fmt
))
144 /* The libavcodec palette can only be fetched when the first output
145 * frame is decoded. Assume that the current chroma is RGB32 while we
146 * are waiting for a valid palette. Indeed, fmt_out.video.p_palette
147 * doesn't trigger a new vout request, but a new chroma yes. */
148 if (pix_fmt
== AV_PIX_FMT_PAL8
&& !dec
->fmt_out
.video
.p_palette
)
149 fmt
->i_chroma
= VLC_CODEC_RGB32
;
151 avcodec_align_dimensions2(ctx
, &width
, &height
, aligns
);
153 else /* hardware decoding */
154 fmt
->i_chroma
= vlc_va_GetChroma(pix_fmt
, sw_pix_fmt
);
156 if( width
== 0 || height
== 0 || width
> 8192 || height
> 8192 )
158 msg_Err(dec
, "Invalid frame size %dx%d.", width
, height
);
159 return -1; /* invalid display size */
162 fmt
->i_width
= width
;
163 fmt
->i_height
= height
;
164 fmt
->i_visible_width
= ctx
->width
;
165 fmt
->i_visible_height
= ctx
->height
;
167 /* If an aspect-ratio was specified in the input format then force it */
168 if (dec
->fmt_in
.video
.i_sar_num
> 0 && dec
->fmt_in
.video
.i_sar_den
> 0)
170 fmt
->i_sar_num
= dec
->fmt_in
.video
.i_sar_num
;
171 fmt
->i_sar_den
= dec
->fmt_in
.video
.i_sar_den
;
175 fmt
->i_sar_num
= ctx
->sample_aspect_ratio
.num
;
176 fmt
->i_sar_den
= ctx
->sample_aspect_ratio
.den
;
178 if (fmt
->i_sar_num
== 0 || fmt
->i_sar_den
== 0)
179 fmt
->i_sar_num
= fmt
->i_sar_den
= 1;
182 if (dec
->fmt_in
.video
.i_frame_rate
> 0
183 && dec
->fmt_in
.video
.i_frame_rate_base
> 0)
185 fmt
->i_frame_rate
= dec
->fmt_in
.video
.i_frame_rate
;
186 fmt
->i_frame_rate_base
= dec
->fmt_in
.video
.i_frame_rate_base
;
188 else if (ctx
->framerate
.num
> 0 && ctx
->framerate
.den
> 0)
190 fmt
->i_frame_rate
= ctx
->framerate
.num
;
191 fmt
->i_frame_rate_base
= ctx
->framerate
.den
;
192 # if LIBAVCODEC_VERSION_MICRO < 100
193 // for some reason libav don't thinkg framerate presents actually same thing as in ffmpeg
194 fmt
->i_frame_rate_base
*= __MAX(ctx
->ticks_per_frame
, 1);
197 else if (ctx
->time_base
.num
> 0 && ctx
->time_base
.den
> 0)
199 fmt
->i_frame_rate
= ctx
->time_base
.den
;
200 fmt
->i_frame_rate_base
= ctx
->time_base
.num
201 * __MAX(ctx
->ticks_per_frame
, 1);
204 if( ctx
->color_range
== AVCOL_RANGE_JPEG
)
205 fmt
->b_color_range_full
= true;
207 switch( ctx
->colorspace
)
209 case AVCOL_SPC_BT709
:
210 fmt
->space
= COLOR_SPACE_BT709
;
212 case AVCOL_SPC_SMPTE170M
:
213 case AVCOL_SPC_BT470BG
:
214 fmt
->space
= COLOR_SPACE_BT601
;
216 case AVCOL_SPC_BT2020_NCL
:
217 case AVCOL_SPC_BT2020_CL
:
218 fmt
->space
= COLOR_SPACE_BT2020
;
224 switch( ctx
->color_trc
)
226 case AVCOL_TRC_LINEAR
:
227 fmt
->transfer
= TRANSFER_FUNC_LINEAR
;
229 case AVCOL_TRC_GAMMA22
:
230 fmt
->transfer
= TRANSFER_FUNC_SRGB
;
232 case AVCOL_TRC_BT709
:
233 fmt
->transfer
= TRANSFER_FUNC_BT709
;
235 case AVCOL_TRC_SMPTE170M
:
236 case AVCOL_TRC_BT2020_10
:
237 case AVCOL_TRC_BT2020_12
:
238 fmt
->transfer
= TRANSFER_FUNC_BT2020
;
240 #if LIBAVUTIL_VERSION_CHECK( 55, 14, 0, 31, 100)
241 case AVCOL_TRC_ARIB_STD_B67
:
242 fmt
->transfer
= TRANSFER_FUNC_ARIB_B67
;
245 #if LIBAVUTIL_VERSION_CHECK( 55, 17, 0, 37, 100)
246 case AVCOL_TRC_SMPTE2084
:
247 fmt
->transfer
= TRANSFER_FUNC_SMPTE_ST2084
;
249 case AVCOL_TRC_SMPTE240M
:
250 fmt
->transfer
= TRANSFER_FUNC_SMPTE_240
;
252 case AVCOL_TRC_GAMMA28
:
253 fmt
->transfer
= TRANSFER_FUNC_BT470_BG
;
260 switch( ctx
->color_primaries
)
262 case AVCOL_PRI_BT709
:
263 fmt
->primaries
= COLOR_PRIMARIES_BT709
;
265 case AVCOL_PRI_BT470BG
:
266 fmt
->primaries
= COLOR_PRIMARIES_BT601_625
;
268 case AVCOL_PRI_SMPTE170M
:
269 case AVCOL_PRI_SMPTE240M
:
270 fmt
->primaries
= COLOR_PRIMARIES_BT601_525
;
272 case AVCOL_PRI_BT2020
:
273 fmt
->primaries
= COLOR_PRIMARIES_BT2020
;
279 switch( ctx
->chroma_sample_location
)
281 case AVCHROMA_LOC_LEFT
:
282 fmt
->chroma_location
= CHROMA_LOCATION_LEFT
;
284 case AVCHROMA_LOC_CENTER
:
285 fmt
->chroma_location
= CHROMA_LOCATION_CENTER
;
287 case AVCHROMA_LOC_TOPLEFT
:
288 fmt
->chroma_location
= CHROMA_LOCATION_TOP_LEFT
;
297 static int lavc_UpdateVideoFormat(decoder_t
*dec
, AVCodecContext
*ctx
,
298 enum AVPixelFormat fmt
,
299 enum AVPixelFormat swfmt
)
301 video_format_t fmt_out
;
304 val
= lavc_GetVideoFormat(dec
, &fmt_out
, ctx
, fmt
, swfmt
);
308 const int i_cc_reorder
= dec
->fmt_out
.subs
.cc
.i_reorder_depth
;
309 fmt_out
.p_palette
= dec
->fmt_out
.video
.p_palette
;
310 dec
->fmt_out
.video
.p_palette
= NULL
;
312 es_format_Clean(&dec
->fmt_out
);
313 es_format_Init(&dec
->fmt_out
, VIDEO_ES
, fmt_out
.i_chroma
);
314 dec
->fmt_out
.video
= fmt_out
;
315 dec
->fmt_out
.video
.orientation
= dec
->fmt_in
.video
.orientation
;
316 dec
->fmt_out
.video
.projection_mode
= dec
->fmt_in
.video
.projection_mode
;
317 dec
->fmt_out
.video
.pose
= dec
->fmt_in
.video
.pose
;
318 if ( dec
->fmt_in
.video
.mastering
.max_luminance
)
319 dec
->fmt_out
.video
.mastering
= dec
->fmt_in
.video
.mastering
;
320 dec
->fmt_out
.video
.lighting
= dec
->fmt_in
.video
.lighting
;
322 dec
->fmt_out
.subs
.cc
.i_reorder_depth
= i_cc_reorder
;
324 return decoder_UpdateVideoFormat(dec
);
328 * Copies a picture from the libavcodec-allocate buffer to a picture_t.
329 * This is used when not in direct rendering mode.
331 static int lavc_CopyPicture(decoder_t
*dec
, picture_t
*pic
, AVFrame
*frame
)
333 decoder_sys_t
*sys
= dec
->p_sys
;
335 if (!FindVlcChroma(sys
->p_context
->pix_fmt
))
337 const char *name
= av_get_pix_fmt_name(sys
->p_context
->pix_fmt
);
339 msg_Err(dec
, "Unsupported decoded output format %d (%s)",
340 sys
->p_context
->pix_fmt
, (name
!= NULL
) ? name
: "unknown");
344 for (int plane
= 0; plane
< pic
->i_planes
; plane
++)
346 const uint8_t *src
= frame
->data
[plane
];
347 uint8_t *dst
= pic
->p
[plane
].p_pixels
;
348 size_t src_stride
= frame
->linesize
[plane
];
349 size_t dst_stride
= pic
->p
[plane
].i_pitch
;
350 size_t size
= __MIN(src_stride
, dst_stride
);
352 for (int line
= 0; line
< pic
->p
[plane
].i_visible_lines
; line
++)
354 memcpy(dst
, src
, size
);
362 static int OpenVideoCodec( decoder_t
*p_dec
)
364 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
367 if( p_sys
->p_context
->extradata_size
<= 0 )
369 if( p_sys
->p_codec
->id
== AV_CODEC_ID_VC1
||
370 p_sys
->p_codec
->id
== AV_CODEC_ID_THEORA
)
372 msg_Warn( p_dec
, "waiting for extra data for codec %s",
373 p_sys
->p_codec
->name
);
378 p_sys
->p_context
->width
= p_dec
->fmt_in
.video
.i_visible_width
;
379 p_sys
->p_context
->height
= p_dec
->fmt_in
.video
.i_visible_height
;
381 p_sys
->p_context
->coded_width
= p_dec
->fmt_in
.video
.i_width
;
382 p_sys
->p_context
->coded_height
= p_dec
->fmt_in
.video
.i_height
;
384 p_sys
->p_context
->bits_per_coded_sample
= p_dec
->fmt_in
.video
.i_bits_per_pixel
;
385 p_sys
->pix_fmt
= AV_PIX_FMT_NONE
;
388 cc_Init( &p_sys
->cc
);
391 ret
= ffmpeg_OpenCodec( p_dec
);
396 switch( p_sys
->p_context
->active_thread_type
)
398 case FF_THREAD_FRAME
:
399 msg_Dbg( p_dec
, "using frame thread mode with %d threads",
400 p_sys
->p_context
->thread_count
);
402 case FF_THREAD_SLICE
:
403 msg_Dbg( p_dec
, "using slice thread mode with %d threads",
404 p_sys
->p_context
->thread_count
);
407 if( p_sys
->p_context
->thread_count
> 1 )
408 msg_Warn( p_dec
, "failed to enable threaded decoding" );
411 msg_Warn( p_dec
, "using unknown thread mode with %d threads",
412 p_sys
->p_context
->thread_count
);
418 /*****************************************************************************
419 * InitVideo: initialize the video decoder
420 *****************************************************************************
421 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
422 * opened (done after the first decoded frame).
423 *****************************************************************************/
424 int InitVideoDec( decoder_t
*p_dec
, AVCodecContext
*p_context
,
425 const AVCodec
*p_codec
)
427 decoder_sys_t
*p_sys
;
430 /* Allocate the memory needed to store the decoder's structure */
431 if( ( p_dec
->p_sys
= p_sys
= calloc( 1, sizeof(decoder_sys_t
) ) ) == NULL
)
434 p_sys
->p_context
= p_context
;
435 p_sys
->p_codec
= p_codec
;
436 p_sys
->b_delayed_open
= true;
438 vlc_sem_init( &p_sys
->sem_mt
, 0 );
440 /* ***** Fill p_context with init values ***** */
441 p_context
->codec_tag
= ffmpeg_CodecTag( p_dec
->fmt_in
.i_original_fourcc
?
442 p_dec
->fmt_in
.i_original_fourcc
: p_dec
->fmt_in
.i_codec
);
444 /* ***** Get configuration of ffmpeg plugin ***** */
445 p_context
->workaround_bugs
=
446 var_InheritInteger( p_dec
, "avcodec-workaround-bugs" );
447 p_context
->err_recognition
=
448 var_InheritInteger( p_dec
, "avcodec-error-resilience" );
450 if( var_CreateGetBool( p_dec
, "grayscale" ) )
451 p_context
->flags
|= AV_CODEC_FLAG_GRAY
;
453 /* ***** Output always the frames ***** */
454 p_context
->flags
|= AV_CODEC_FLAG_OUTPUT_CORRUPT
;
456 i_val
= var_CreateGetInteger( p_dec
, "avcodec-skiploopfilter" );
457 if( i_val
>= 4 ) p_context
->skip_loop_filter
= AVDISCARD_ALL
;
458 else if( i_val
== 3 ) p_context
->skip_loop_filter
= AVDISCARD_NONKEY
;
459 else if( i_val
== 2 ) p_context
->skip_loop_filter
= AVDISCARD_BIDIR
;
460 else if( i_val
== 1 ) p_context
->skip_loop_filter
= AVDISCARD_NONREF
;
461 else p_context
->skip_loop_filter
= AVDISCARD_DEFAULT
;
463 if( var_CreateGetBool( p_dec
, "avcodec-fast" ) )
464 p_context
->flags2
|= AV_CODEC_FLAG2_FAST
;
466 /* ***** libavcodec frame skipping ***** */
467 p_sys
->b_hurry_up
= var_CreateGetBool( p_dec
, "avcodec-hurry-up" );
468 p_sys
->b_show_corrupted
= var_CreateGetBool( p_dec
, "avcodec-corrupted" );
470 i_val
= var_CreateGetInteger( p_dec
, "avcodec-skip-frame" );
471 if( i_val
>= 4 ) p_context
->skip_frame
= AVDISCARD_ALL
;
472 else if( i_val
== 3 ) p_context
->skip_frame
= AVDISCARD_NONKEY
;
473 else if( i_val
== 2 ) p_context
->skip_frame
= AVDISCARD_BIDIR
;
474 else if( i_val
== 1 ) p_context
->skip_frame
= AVDISCARD_NONREF
;
475 else if( i_val
== -1 ) p_context
->skip_frame
= AVDISCARD_NONE
;
476 else p_context
->skip_frame
= AVDISCARD_DEFAULT
;
477 p_sys
->i_skip_frame
= p_context
->skip_frame
;
479 i_val
= var_CreateGetInteger( p_dec
, "avcodec-skip-idct" );
480 if( i_val
>= 4 ) p_context
->skip_idct
= AVDISCARD_ALL
;
481 else if( i_val
== 3 ) p_context
->skip_idct
= AVDISCARD_NONKEY
;
482 else if( i_val
== 2 ) p_context
->skip_idct
= AVDISCARD_BIDIR
;
483 else if( i_val
== 1 ) p_context
->skip_idct
= AVDISCARD_NONREF
;
484 else if( i_val
== -1 ) p_context
->skip_idct
= AVDISCARD_NONE
;
485 else p_context
->skip_idct
= AVDISCARD_DEFAULT
;
487 /* ***** libavcodec direct rendering ***** */
488 p_sys
->b_direct_rendering
= false;
489 atomic_init(&p_sys
->b_dr_failure
, false);
490 if( var_CreateGetBool( p_dec
, "avcodec-dr" ) &&
491 (p_codec
->capabilities
& AV_CODEC_CAP_DR1
) &&
492 /* No idea why ... but this fixes flickering on some TSCC streams */
493 p_sys
->p_codec
->id
!= AV_CODEC_ID_TSCC
&&
494 p_sys
->p_codec
->id
!= AV_CODEC_ID_CSCD
&&
495 p_sys
->p_codec
->id
!= AV_CODEC_ID_CINEPAK
)
497 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
498 * so we need to do another check in ffmpeg_GetFrameBuf() */
499 p_sys
->b_direct_rendering
= true;
502 p_context
->get_format
= ffmpeg_GetFormat
;
503 /* Always use our get_buffer wrapper so we can calculate the
505 p_context
->get_buffer2
= lavc_GetFrame
;
506 p_context
->refcounted_frames
= true;
507 p_context
->opaque
= p_dec
;
509 int i_thread_count
= var_InheritInteger( p_dec
, "avcodec-threads" );
510 if( i_thread_count
<= 0 )
512 i_thread_count
= vlc_GetCPUCount();
513 if( i_thread_count
> 1 )
516 //FIXME: take in count the decoding time
517 i_thread_count
= __MIN( i_thread_count
, p_codec
->id
== AV_CODEC_ID_HEVC
? 6 : 4 );
519 i_thread_count
= __MIN( i_thread_count
, 16 );
520 msg_Dbg( p_dec
, "allowing %d thread(s) for decoding", i_thread_count
);
521 p_context
->thread_count
= i_thread_count
;
522 p_context
->thread_safe_callbacks
= true;
524 switch( p_codec
->id
)
526 case AV_CODEC_ID_MPEG4
:
527 case AV_CODEC_ID_H263
:
528 p_context
->thread_type
= 0;
530 case AV_CODEC_ID_MPEG1VIDEO
:
531 case AV_CODEC_ID_MPEG2VIDEO
:
532 p_context
->thread_type
&= ~FF_THREAD_SLICE
;
534 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
535 case AV_CODEC_ID_H264
:
536 case AV_CODEC_ID_VC1
:
537 case AV_CODEC_ID_WMV3
:
538 p_context
->thread_type
&= ~FF_THREAD_FRAME
;
544 if( p_context
->thread_type
& FF_THREAD_FRAME
)
545 p_dec
->i_extra_picture_buffers
= 2 * p_context
->thread_count
;
547 /* ***** misc init ***** */
548 p_sys
->i_pts
= VLC_TS_INVALID
;
549 p_sys
->b_first_frame
= true;
550 p_sys
->i_late_frames
= 0;
551 p_sys
->b_from_preroll
= false;
553 /* Set output properties */
554 p_dec
->fmt_out
.i_cat
= VIDEO_ES
;
555 if( GetVlcChroma( &p_dec
->fmt_out
.video
, p_context
->pix_fmt
) != VLC_SUCCESS
)
557 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
558 p_dec
->fmt_out
.i_codec
= VLC_CODEC_I420
;
560 p_dec
->fmt_out
.i_codec
= p_dec
->fmt_out
.video
.i_chroma
;
562 p_dec
->fmt_out
.video
.orientation
= p_dec
->fmt_in
.video
.orientation
;
564 if( p_dec
->fmt_in
.video
.p_palette
) {
565 p_sys
->palette_sent
= false;
566 p_dec
->fmt_out
.video
.p_palette
= malloc( sizeof(video_palette_t
) );
567 if( p_dec
->fmt_out
.video
.p_palette
)
568 *p_dec
->fmt_out
.video
.p_palette
= *p_dec
->fmt_in
.video
.p_palette
;
570 p_sys
->palette_sent
= true;
572 /* ***** init this codec with special data ***** */
573 ffmpeg_InitCodec( p_dec
);
575 /* ***** Open the codec ***** */
576 if( OpenVideoCodec( p_dec
) < 0 )
578 vlc_sem_destroy( &p_sys
->sem_mt
);
583 p_dec
->pf_decode
= DecodeVideo
;
584 p_dec
->pf_flush
= Flush
;
589 /*****************************************************************************
591 *****************************************************************************/
592 static void Flush( decoder_t
*p_dec
)
594 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
595 AVCodecContext
*p_context
= p_sys
->p_context
;
597 p_sys
->i_pts
= VLC_TS_INVALID
; /* To make sure we recover properly */
598 p_sys
->i_late_frames
= 0;
599 cc_Flush( &p_sys
->cc
);
601 /* Abort pictures in order to unblock all avcodec workers threads waiting
602 * for a picture. This will avoid a deadlock between avcodec_flush_buffers
603 * and workers threads */
604 decoder_AbortPictures( p_dec
, true );
607 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
608 if( avcodec_is_open( p_context
) )
609 avcodec_flush_buffers( p_context
);
612 /* Reset cancel state to false */
613 decoder_AbortPictures( p_dec
, false );
616 static bool check_block_validity( decoder_sys_t
*p_sys
, block_t
*block
)
621 if( block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
623 p_sys
->i_pts
= VLC_TS_INVALID
; /* To make sure we recover properly */
624 cc_Flush( &p_sys
->cc
);
626 p_sys
->i_late_frames
= 0;
627 if( block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
629 block_Release( block
);
636 static bool check_block_being_late( decoder_sys_t
*p_sys
, block_t
*block
, mtime_t current_time
)
640 if( block
->i_flags
& BLOCK_FLAG_PREROLL
)
642 /* Do not care about late frames when prerolling
643 * TODO avoid decoding of non reference frame
644 * (ie all B except for H264 where it depends only on nal_ref_idc) */
645 p_sys
->i_late_frames
= 0;
646 p_sys
->b_from_preroll
= true;
647 p_sys
->i_last_late_delay
= INT64_MAX
;
650 if( p_sys
->i_late_frames
<= 0 )
653 if( current_time
- p_sys
->i_late_frames_start
> (5*CLOCK_FREQ
))
655 if( p_sys
->i_pts
> VLC_TS_INVALID
)
657 p_sys
->i_pts
= VLC_TS_INVALID
; /* To make sure we recover properly */
660 block_Release( block
);
661 p_sys
->i_late_frames
--;
667 static bool check_frame_should_be_dropped( decoder_sys_t
*p_sys
, AVCodecContext
*p_context
, bool *b_need_output_picture
)
669 if( p_sys
->i_late_frames
<= 4)
672 *b_need_output_picture
= false;
673 if( p_sys
->i_late_frames
< 12 )
675 p_context
->skip_frame
=
676 (p_sys
->i_skip_frame
<= AVDISCARD_NONREF
) ?
677 AVDISCARD_NONREF
: p_sys
->i_skip_frame
;
681 /* picture too late, won't decode
682 * but break picture until a new I, and for mpeg4 ...*/
683 p_sys
->i_late_frames
--; /* needed else it will never be decrease */
689 static void interpolate_next_pts( decoder_t
*p_dec
, AVFrame
*frame
)
691 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
692 AVCodecContext
*p_context
= p_sys
->p_context
;
694 if( p_sys
->i_pts
<= VLC_TS_INVALID
)
697 /* interpolate the next PTS */
698 if( p_dec
->fmt_in
.video
.i_frame_rate
> 0 &&
699 p_dec
->fmt_in
.video
.i_frame_rate_base
> 0 )
701 p_sys
->i_pts
+= CLOCK_FREQ
* (2 + frame
->repeat_pict
) *
702 p_dec
->fmt_in
.video
.i_frame_rate_base
/
703 (2 * p_dec
->fmt_in
.video
.i_frame_rate
);
705 else if( p_context
->time_base
.den
> 0 )
707 int i_tick
= p_context
->ticks_per_frame
;
711 p_sys
->i_pts
+= CLOCK_FREQ
* (2 + frame
->repeat_pict
) *
712 i_tick
* p_context
->time_base
.num
/
713 (2 * p_context
->time_base
.den
);
717 static void update_late_frame_count( decoder_t
*p_dec
, block_t
*p_block
, mtime_t current_time
, mtime_t i_pts
)
719 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
720 /* Update frame late count (except when doing preroll) */
721 mtime_t i_display_date
= VLC_TS_INVALID
;
722 if( !p_block
|| !(p_block
->i_flags
& BLOCK_FLAG_PREROLL
) )
723 i_display_date
= decoder_GetDisplayDate( p_dec
, i_pts
);
725 if( i_display_date
> VLC_TS_INVALID
&& i_display_date
<= current_time
)
727 /* Out of preroll, consider only late frames on rising delay */
728 if( p_sys
->b_from_preroll
)
730 if( p_sys
->i_last_late_delay
> current_time
- i_display_date
)
732 p_sys
->i_last_late_delay
= current_time
- i_display_date
;
735 p_sys
->b_from_preroll
= false;
738 p_sys
->i_late_frames
++;
739 if( p_sys
->i_late_frames
== 1 )
740 p_sys
->i_late_frames_start
= current_time
;
745 p_sys
->i_late_frames
= 0;
750 static void DecodeSidedata( decoder_t
*p_dec
, const AVFrame
*frame
, picture_t
*p_pic
)
752 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
753 bool format_changed
= false;
755 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 16, 101 ) )
756 #define FROM_AVRAT(default_factor, avrat) \
757 (uint64_t)(default_factor) * (avrat).num / (avrat).den
758 const AVFrameSideData
*metadata
=
759 av_frame_get_side_data( frame
,
760 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
);
763 const AVMasteringDisplayMetadata
*hdr_meta
=
764 (const AVMasteringDisplayMetadata
*) metadata
->data
;
765 if ( hdr_meta
->has_luminance
)
767 #define ST2086_LUMA_FACTOR 10000
768 p_pic
->format
.mastering
.max_luminance
=
769 FROM_AVRAT(ST2086_LUMA_FACTOR
, hdr_meta
->max_luminance
);
770 p_pic
->format
.mastering
.min_luminance
=
771 FROM_AVRAT(ST2086_LUMA_FACTOR
, hdr_meta
->min_luminance
);
773 if ( hdr_meta
->has_primaries
)
776 #define ST2086_GREEN 0
777 #define ST2086_BLUE 1
781 #define ST2086_PRIM_FACTOR 50000
782 p_pic
->format
.mastering
.primaries
[ST2086_RED
*2 + 0] =
783 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_RED
][0]);
784 p_pic
->format
.mastering
.primaries
[ST2086_RED
*2 + 1] =
785 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_RED
][1]);
786 p_pic
->format
.mastering
.primaries
[ST2086_GREEN
*2 + 0] =
787 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_GREEN
][0]);
788 p_pic
->format
.mastering
.primaries
[ST2086_GREEN
*2 + 1] =
789 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_GREEN
][1]);
790 p_pic
->format
.mastering
.primaries
[ST2086_BLUE
*2 + 0] =
791 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_BLUE
][0]);
792 p_pic
->format
.mastering
.primaries
[ST2086_BLUE
*2 + 1] =
793 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->display_primaries
[LAV_BLUE
][1]);
794 p_pic
->format
.mastering
.white_point
[0] =
795 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->white_point
[0]);
796 p_pic
->format
.mastering
.white_point
[1] =
797 FROM_AVRAT(ST2086_PRIM_FACTOR
, hdr_meta
->white_point
[1]);
800 if ( memcmp( &p_dec
->fmt_out
.video
.mastering
,
801 &p_pic
->format
.mastering
,
802 sizeof(p_pic
->format
.mastering
) ) )
804 p_dec
->fmt_out
.video
.mastering
= p_pic
->format
.mastering
;
805 format_changed
= true;
810 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 60, 100 ) )
811 const AVFrameSideData
*metadata_lt
=
812 av_frame_get_side_data( frame
,
813 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
);
816 const AVContentLightMetadata
*light_meta
=
817 (const AVContentLightMetadata
*) metadata_lt
->data
;
818 p_pic
->format
.lighting
.MaxCLL
= light_meta
->MaxCLL
;
819 p_pic
->format
.lighting
.MaxFALL
= light_meta
->MaxFALL
;
820 if ( memcmp( &p_dec
->fmt_out
.video
.lighting
,
821 &p_pic
->format
.lighting
,
822 sizeof(p_pic
->format
.lighting
) ) )
824 p_dec
->fmt_out
.video
.lighting
= p_pic
->format
.lighting
;
825 format_changed
= true;
831 decoder_UpdateVideoFormat( p_dec
);
833 const AVFrameSideData
*p_avcc
= av_frame_get_side_data( frame
, AV_FRAME_DATA_A53_CC
);
836 cc_Extract( &p_sys
->cc
, CC_PAYLOAD_RAW
, true, p_avcc
->data
, p_avcc
->size
);
837 if( p_sys
->cc
.i_data
)
839 block_t
*p_cc
= block_Alloc( p_sys
->cc
.i_data
);
842 memcpy( p_cc
->p_buffer
, p_sys
->cc
.p_data
, p_sys
->cc
.i_data
);
843 if( p_sys
->cc
.b_reorder
)
844 p_cc
->i_dts
= p_cc
->i_pts
= p_pic
->date
;
846 p_cc
->i_pts
= p_cc
->i_dts
;
847 decoder_QueueCc( p_dec
, p_cc
, p_sys
->cc
.pb_present
, 4 );
849 cc_Flush( &p_sys
->cc
);
854 /*****************************************************************************
855 * DecodeBlock: Called to decode one or more frames
856 *****************************************************************************/
857 static picture_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
, bool *error
)
859 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
860 AVCodecContext
*p_context
= p_sys
->p_context
;
861 /* Boolean if we assume that we should get valid pic as result */
862 bool b_need_output_picture
= true;
864 /* Boolean for END_OF_SEQUENCE */
865 bool eos_spotted
= false;
869 mtime_t current_time
= VLC_TS_INVALID
;
871 if( !p_context
->extradata_size
&& p_dec
->fmt_in
.i_extra
)
873 ffmpeg_InitCodec( p_dec
);
874 if( p_sys
->b_delayed_open
)
875 OpenVideoCodec( p_dec
);
878 p_block
= pp_block
? *pp_block
: NULL
;
879 if(!p_block
&& !(p_sys
->p_codec
->capabilities
& AV_CODEC_CAP_DELAY
) )
882 if( p_sys
->b_delayed_open
)
885 block_Release( p_block
);
889 if( !check_block_validity( p_sys
, p_block
) )
892 current_time
= mdate();
893 if( p_dec
->b_frame_drop_allowed
&& check_block_being_late( p_sys
, p_block
, current_time
) )
895 msg_Err( p_dec
, "more than 5 seconds of late video -> "
896 "dropping frame (computer too slow ?)" );
901 /* A good idea could be to decode all I pictures and see for the other */
903 /* Defaults that if we aren't in prerolling, we want output picture
904 same for if we are flushing (p_block==NULL) */
905 if( !p_block
|| !(p_block
->i_flags
& BLOCK_FLAG_PREROLL
) )
906 b_need_output_picture
= true;
908 b_need_output_picture
= false;
910 /* Change skip_frame config only if hurry_up is enabled */
911 if( p_sys
->b_hurry_up
)
913 p_context
->skip_frame
= p_sys
->i_skip_frame
;
915 /* Check also if we should/can drop the block and move to next block
916 as trying to catchup the speed*/
917 if( p_dec
->b_frame_drop_allowed
&&
918 check_frame_should_be_dropped( p_sys
, p_context
, &b_need_output_picture
) )
921 block_Release( p_block
);
922 msg_Warn( p_dec
, "More than 11 late frames, dropping frame" );
926 if( !b_need_output_picture
)
928 p_context
->skip_frame
= __MAX( p_context
->skip_frame
,
933 * Do the actual decoding now */
935 /* Don't forget that libavcodec requires a little more bytes
936 * that the real frame size */
937 if( p_block
&& p_block
->i_buffer
> 0 )
939 eos_spotted
= ( p_block
->i_flags
& BLOCK_FLAG_END_OF_SEQUENCE
) != 0;
941 p_block
= block_Realloc( p_block
, 0,
942 p_block
->i_buffer
+ FF_INPUT_BUFFER_PADDING_SIZE
);
945 p_block
->i_buffer
-= FF_INPUT_BUFFER_PADDING_SIZE
;
947 memset( p_block
->p_buffer
+ p_block
->i_buffer
, 0,
948 FF_INPUT_BUFFER_PADDING_SIZE
);
951 while( !p_block
|| p_block
->i_buffer
> 0 || eos_spotted
)
958 av_init_packet( &pkt
);
961 pkt
.data
= p_block
->p_buffer
;
962 pkt
.size
= p_block
->i_buffer
;
963 pkt
.pts
= p_block
->i_pts
> VLC_TS_INVALID
? p_block
->i_pts
: AV_NOPTS_VALUE
;
964 pkt
.dts
= p_block
->i_dts
> VLC_TS_INVALID
? p_block
->i_dts
: AV_NOPTS_VALUE
;
968 /* Return delayed frames if codec has CODEC_CAP_DELAY */
973 if( !p_sys
->palette_sent
)
975 uint8_t *pal
= av_packet_new_side_data(&pkt
, AV_PKT_DATA_PALETTE
, AVPALETTE_SIZE
);
977 memcpy(pal
, p_dec
->fmt_in
.video
.p_palette
->palette
, AVPALETTE_SIZE
);
978 p_sys
->palette_sent
= true;
982 /* Make sure we don't reuse the same timestamps twice */
986 p_block
->i_dts
= VLC_TS_INVALID
;
989 #if LIBAVCODEC_VERSION_CHECK( 57, 0, 0xFFFFFFFFU, 64, 101 )
990 if( !b_need_output_picture
)
991 pkt
.flags
|= AV_PKT_FLAG_DISCARD
;
994 int ret
= avcodec_send_packet(p_context
, &pkt
);
995 if( ret
!= 0 && ret
!= AVERROR(EAGAIN
) )
997 if (ret
== AVERROR(ENOMEM
) || ret
== AVERROR(EINVAL
))
999 msg_Err(p_dec
, "avcodec_send_packet critical error");
1002 av_packet_unref( &pkt
);
1005 i_used
= ret
!= AVERROR(EAGAIN
) ? pkt
.size
: 0;
1006 av_packet_unref( &pkt
);
1008 AVFrame
*frame
= av_frame_alloc();
1009 if (unlikely(frame
== NULL
))
1015 ret
= avcodec_receive_frame(p_context
, frame
);
1016 if( ret
!= 0 && ret
!= AVERROR(EAGAIN
) )
1018 if (ret
== AVERROR(ENOMEM
) || ret
== AVERROR(EINVAL
))
1020 msg_Err(p_dec
, "avcodec_receive_frame critical error");
1023 av_frame_free(&frame
);
1024 /* After draining, we need to reset decoder with a flush */
1025 if( ret
== AVERROR_EOF
)
1026 avcodec_flush_buffers( p_sys
->p_context
);
1029 bool not_received_frame
= ret
;
1034 p_sys
->b_first_frame
= true;
1038 if( p_block
->i_buffer
<= 0 )
1039 eos_spotted
= false;
1041 /* Consumed bytes */
1042 p_block
->p_buffer
+= i_used
;
1043 p_block
->i_buffer
-= i_used
;
1046 /* Nothing to display */
1047 if( not_received_frame
)
1049 av_frame_free(&frame
);
1050 if( i_used
== 0 ) break;
1054 /* Compute the PTS */
1055 #ifdef FF_API_PKT_PTS
1056 mtime_t i_pts
= frame
->pts
;
1058 mtime_t i_pts
= frame
->pkt_pts
;
1060 if (i_pts
== AV_NOPTS_VALUE
)
1061 i_pts
= frame
->pkt_dts
;
1063 if( i_pts
== AV_NOPTS_VALUE
)
1064 i_pts
= p_sys
->i_pts
;
1066 /* Interpolate the next PTS */
1067 if( i_pts
> VLC_TS_INVALID
)
1068 p_sys
->i_pts
= i_pts
;
1070 interpolate_next_pts( p_dec
, frame
);
1072 update_late_frame_count( p_dec
, p_block
, current_time
, i_pts
);
1074 if( ( !p_sys
->p_va
&& !frame
->linesize
[0] ) ||
1075 ( p_dec
->b_frame_drop_allowed
&& (frame
->flags
& AV_FRAME_FLAG_CORRUPT
) &&
1076 !p_sys
->b_show_corrupted
) )
1078 av_frame_free(&frame
);
1082 #if !LIBAVCODEC_VERSION_CHECK( 57, 0, 0xFFFFFFFFU, 64, 101 )
1083 if( !b_need_output_picture
)
1085 av_frame_free(&frame
);
1090 if( p_context
->pix_fmt
== AV_PIX_FMT_PAL8
1091 && !p_dec
->fmt_out
.video
.p_palette
)
1093 /* See AV_PIX_FMT_PAL8 comment in avc_GetVideoFormat(): update the
1094 * fmt_out palette and change the fmt_out chroma to request a new
1096 assert( p_dec
->fmt_out
.video
.i_chroma
!= VLC_CODEC_RGBP
);
1098 video_palette_t
*p_palette
;
1099 p_palette
= p_dec
->fmt_out
.video
.p_palette
1100 = malloc( sizeof(video_palette_t
) );
1104 av_frame_free(&frame
);
1107 static_assert( sizeof(p_palette
->palette
) == AVPALETTE_SIZE
,
1108 "Palette size mismatch between vlc and libavutil" );
1109 assert( frame
->data
[1] != NULL
);
1110 memcpy( p_palette
->palette
, frame
->data
[1], AVPALETTE_SIZE
);
1111 p_palette
->i_entries
= AVPALETTE_COUNT
;
1112 p_dec
->fmt_out
.video
.i_chroma
= VLC_CODEC_RGBP
;
1113 if( decoder_UpdateVideoFormat( p_dec
) )
1115 av_frame_free(&frame
);
1120 picture_t
*p_pic
= frame
->opaque
;
1122 { /* When direct rendering is not used, get_format() and get_buffer()
1123 * might not be called. The output video format must be set here
1124 * then picture buffer can be allocated. */
1125 if (p_sys
->p_va
== NULL
1126 && lavc_UpdateVideoFormat(p_dec
, p_context
, p_context
->pix_fmt
,
1127 p_context
->pix_fmt
) == 0)
1128 p_pic
= decoder_NewPicture(p_dec
);
1132 av_frame_free(&frame
);
1136 /* Fill picture_t from AVFrame */
1137 if( lavc_CopyPicture( p_dec
, p_pic
, frame
) != VLC_SUCCESS
)
1140 av_frame_free(&frame
);
1141 picture_Release( p_pic
);
1147 if( p_sys
->p_va
!= NULL
)
1148 vlc_va_Extract( p_sys
->p_va
, p_pic
, frame
->data
[3] );
1149 picture_Hold( p_pic
);
1152 if( !p_dec
->fmt_in
.video
.i_sar_num
|| !p_dec
->fmt_in
.video
.i_sar_den
)
1154 /* Fetch again the aspect ratio in case it changed */
1155 p_dec
->fmt_out
.video
.i_sar_num
1156 = p_context
->sample_aspect_ratio
.num
;
1157 p_dec
->fmt_out
.video
.i_sar_den
1158 = p_context
->sample_aspect_ratio
.den
;
1160 if( !p_dec
->fmt_out
.video
.i_sar_num
|| !p_dec
->fmt_out
.video
.i_sar_den
)
1162 p_dec
->fmt_out
.video
.i_sar_num
= 1;
1163 p_dec
->fmt_out
.video
.i_sar_den
= 1;
1167 p_pic
->date
= i_pts
;
1168 /* Hack to force display of still pictures */
1169 p_pic
->b_force
= p_sys
->b_first_frame
;
1170 p_pic
->i_nb_fields
= 2 + frame
->repeat_pict
;
1171 p_pic
->b_progressive
= !frame
->interlaced_frame
;
1172 p_pic
->b_top_field_first
= frame
->top_field_first
;
1174 DecodeSidedata( p_dec
, frame
, p_pic
);
1176 av_frame_free(&frame
);
1178 /* Send decoded frame to vout */
1179 if (i_pts
> VLC_TS_INVALID
)
1181 p_sys
->b_first_frame
= false;
1185 picture_Release( p_pic
);
1189 block_Release( p_block
);
1193 static int DecodeVideo( decoder_t
*p_dec
, block_t
*p_block
)
1195 block_t
**pp_block
= p_block
? &p_block
: NULL
;
1198 while( ( p_pic
= DecodeBlock( p_dec
, pp_block
, &error
) ) != NULL
)
1199 decoder_QueueVideo( p_dec
, p_pic
);
1200 return error
? VLCDEC_ECRITICAL
: VLCDEC_SUCCESS
;
1203 /*****************************************************************************
1204 * EndVideo: decoder destruction
1205 *****************************************************************************
1206 * This function is called when the thread ends after a successful
1208 *****************************************************************************/
1209 void EndVideoDec( decoder_t
*p_dec
)
1211 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1215 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
1216 if( avcodec_is_open( p_sys
->p_context
) )
1217 avcodec_flush_buffers( p_sys
->p_context
);
1221 cc_Flush( &p_sys
->cc
);
1223 ffmpeg_CloseCodec( p_dec
);
1226 vlc_va_Delete( p_sys
->p_va
, p_sys
->p_context
);
1228 vlc_sem_destroy( &p_sys
->sem_mt
);
1231 /*****************************************************************************
1232 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
1233 *****************************************************************************/
1234 static void ffmpeg_InitCodec( decoder_t
*p_dec
)
1236 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1237 int i_size
= p_dec
->fmt_in
.i_extra
;
1239 if( !i_size
) return;
1241 if( p_sys
->p_codec
->id
== AV_CODEC_ID_SVQ3
)
1245 p_sys
->p_context
->extradata_size
= i_size
+ 12;
1246 p
= p_sys
->p_context
->extradata
=
1247 av_malloc( p_sys
->p_context
->extradata_size
+
1248 FF_INPUT_BUFFER_PADDING_SIZE
);
1252 memcpy( &p
[0], "SVQ3", 4 );
1253 memset( &p
[4], 0, 8 );
1254 memcpy( &p
[12], p_dec
->fmt_in
.p_extra
, i_size
);
1256 /* Now remove all atoms before the SMI one */
1257 if( p_sys
->p_context
->extradata_size
> 0x5a &&
1258 strncmp( (char*)&p
[0x56], "SMI ", 4 ) )
1260 uint8_t *psz
= &p
[0x52];
1262 while( psz
< &p
[p_sys
->p_context
->extradata_size
- 8] )
1264 int i_size
= GetDWBE( psz
);
1267 /* FIXME handle 1 as long size */
1270 if( !strncmp( (char*)&psz
[4], "SMI ", 4 ) )
1272 memmove( &p
[0x52], psz
,
1273 &p
[p_sys
->p_context
->extradata_size
] - psz
);
1283 p_sys
->p_context
->extradata_size
= i_size
;
1284 p_sys
->p_context
->extradata
=
1285 av_malloc( i_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
1286 if( p_sys
->p_context
->extradata
)
1288 memcpy( p_sys
->p_context
->extradata
,
1289 p_dec
->fmt_in
.p_extra
, i_size
);
1290 memset( p_sys
->p_context
->extradata
+ i_size
,
1291 0, FF_INPUT_BUFFER_PADDING_SIZE
);
1296 static void lavc_ReleaseFrame(void *opaque
, uint8_t *data
)
1299 picture_t
*picture
= opaque
;
1301 picture_Release(picture
);
1304 static int lavc_va_GetFrame(struct AVCodecContext
*ctx
, AVFrame
*frame
,
1307 decoder_t
*dec
= ctx
->opaque
;
1308 vlc_va_t
*va
= dec
->p_sys
->p_va
;
1310 if (vlc_va_Get(va
, pic
, &frame
->data
[0]))
1312 msg_Err(dec
, "hardware acceleration picture allocation failed");
1313 picture_Release(pic
);
1316 assert(frame
->data
[0] != NULL
);
1317 /* data[0] must be non-NULL for libavcodec internal checks.
1318 * data[3] actually contains the format-specific surface handle. */
1319 frame
->data
[3] = frame
->data
[0];
1321 void (*release
)(void *, uint8_t *) = va
->release
;
1322 if (va
->release
== NULL
)
1323 release
= lavc_ReleaseFrame
;
1325 frame
->buf
[0] = av_buffer_create(frame
->data
[0], 0, release
, pic
, 0);
1326 if (unlikely(frame
->buf
[0] == NULL
))
1328 release(pic
, frame
->data
[0]);
1332 frame
->opaque
= pic
;
1336 static int lavc_dr_GetFrame(struct AVCodecContext
*ctx
, AVFrame
*frame
,
1339 decoder_t
*dec
= (decoder_t
*)ctx
->opaque
;
1340 decoder_sys_t
*sys
= dec
->p_sys
;
1342 if (ctx
->pix_fmt
== AV_PIX_FMT_PAL8
)
1345 int width
= frame
->width
;
1346 int height
= frame
->height
;
1347 int aligns
[AV_NUM_DATA_POINTERS
];
1349 avcodec_align_dimensions2(ctx
, &width
, &height
, aligns
);
1351 /* Check that the picture is suitable for libavcodec */
1352 assert(pic
->p
[0].i_pitch
>= width
* pic
->p
[0].i_pixel_pitch
);
1353 assert(pic
->p
[0].i_lines
>= height
);
1355 for (int i
= 0; i
< pic
->i_planes
; i
++)
1357 if (pic
->p
[i
].i_pitch
% aligns
[i
])
1359 if (!atomic_exchange(&sys
->b_dr_failure
, true))
1360 msg_Warn(dec
, "plane %d: pitch not aligned (%d%%%d): disabling direct rendering",
1361 i
, pic
->p
[i
].i_pitch
, aligns
[i
]);
1364 if (((uintptr_t)pic
->p
[i
].p_pixels
) % aligns
[i
])
1366 if (!atomic_exchange(&sys
->b_dr_failure
, true))
1367 msg_Warn(dec
, "plane %d not aligned: disabling direct rendering", i
);
1372 /* Allocate buffer references and initialize planes */
1373 assert(pic
->i_planes
< PICTURE_PLANE_MAX
);
1374 static_assert(PICTURE_PLANE_MAX
<= AV_NUM_DATA_POINTERS
, "Oops!");
1376 for (int i
= 0; i
< pic
->i_planes
; i
++)
1378 uint8_t *data
= pic
->p
[i
].p_pixels
;
1379 int size
= pic
->p
[i
].i_pitch
* pic
->p
[i
].i_lines
;
1381 frame
->data
[i
] = data
;
1382 frame
->linesize
[i
] = pic
->p
[i
].i_pitch
;
1383 frame
->buf
[i
] = av_buffer_create(data
, size
, lavc_ReleaseFrame
,
1385 if (unlikely(frame
->buf
[i
] == NULL
))
1388 av_buffer_unref(&frame
->buf
[--i
]);
1394 frame
->opaque
= pic
;
1395 /* The loop above held one reference to the picture for each plane. */
1396 picture_Release(pic
);
1399 picture_Release(pic
);
1404 * Callback used by libavcodec to get a frame buffer.
1406 * It is used for direct rendering as well as to get the right PTS for each
1407 * decoded picture (even in indirect rendering mode).
1409 static int lavc_GetFrame(struct AVCodecContext
*ctx
, AVFrame
*frame
, int flags
)
1411 decoder_t
*dec
= ctx
->opaque
;
1412 decoder_sys_t
*sys
= dec
->p_sys
;
1415 for (unsigned i
= 0; i
< AV_NUM_DATA_POINTERS
; i
++)
1417 frame
->data
[i
] = NULL
;
1418 frame
->linesize
[i
] = 0;
1419 frame
->buf
[i
] = NULL
;
1421 frame
->opaque
= NULL
;
1424 if (sys
->p_va
== NULL
)
1426 if (!sys
->b_direct_rendering
)
1429 return avcodec_default_get_buffer2(ctx
, frame
, flags
);
1432 /* Most unaccelerated decoders do not call get_format(), so we need to
1433 * update the output video format here. The MT semaphore must be held
1434 * to protect p_dec->fmt_out. */
1435 if (lavc_UpdateVideoFormat(dec
, ctx
, ctx
->pix_fmt
, ctx
->pix_fmt
))
1443 pic
= decoder_NewPicture(dec
);
1447 if (sys
->p_va
!= NULL
)
1448 return lavc_va_GetFrame(ctx
, frame
, pic
);
1450 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1451 * so we need to check for direct rendering again. */
1452 int ret
= lavc_dr_GetFrame(ctx
, frame
, pic
);
1454 ret
= avcodec_default_get_buffer2(ctx
, frame
, flags
);
1458 static enum PixelFormat
ffmpeg_GetFormat( AVCodecContext
*p_context
,
1459 const enum PixelFormat
*pi_fmt
)
1461 decoder_t
*p_dec
= p_context
->opaque
;
1462 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1465 /* Enumerate available formats */
1466 enum PixelFormat swfmt
= avcodec_default_get_format(p_context
, pi_fmt
);
1467 bool can_hwaccel
= false;
1469 for( size_t i
= 0; pi_fmt
[i
] != AV_PIX_FMT_NONE
; i
++ )
1471 const AVPixFmtDescriptor
*dsc
= av_pix_fmt_desc_get(pi_fmt
[i
]);
1474 bool hwaccel
= (dsc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
) != 0;
1476 msg_Dbg( p_dec
, "available %sware decoder output format %d (%s)",
1477 hwaccel
? "hard" : "soft", pi_fmt
[i
], dsc
->name
);
1482 /* If the format did not actually change (e.g. seeking), try to reuse the
1483 * existing output format, and if present, hardware acceleration back-end.
1484 * This avoids resetting the pipeline downstream. This also avoids
1485 * needlessly probing for hardware acceleration support. */
1486 if (p_sys
->pix_fmt
!= AV_PIX_FMT_NONE
1487 && lavc_GetVideoFormat(p_dec
, &fmt
, p_context
, p_sys
->pix_fmt
, swfmt
) == 0
1488 && fmt
.i_width
== p_dec
->fmt_out
.video
.i_width
1489 && fmt
.i_height
== p_dec
->fmt_out
.video
.i_height
1490 && p_context
->profile
== p_sys
->profile
1491 && p_context
->level
<= p_sys
->level
)
1493 for (size_t i
= 0; pi_fmt
[i
] != AV_PIX_FMT_NONE
; i
++)
1494 if (pi_fmt
[i
] == p_sys
->pix_fmt
)
1496 msg_Dbg(p_dec
, "reusing decoder output format %d", pi_fmt
[i
]);
1497 return p_sys
->pix_fmt
;
1501 if (p_sys
->p_va
!= NULL
)
1503 msg_Err(p_dec
, "existing hardware acceleration cannot be reused");
1504 vlc_va_Delete(p_sys
->p_va
, p_context
);
1508 p_sys
->profile
= p_context
->profile
;
1509 p_sys
->level
= p_context
->level
;
1514 #if (LIBAVCODEC_VERSION_MICRO >= 100) \
1515 && (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 83, 101))
1516 if (p_context
->active_thread_type
)
1518 msg_Warn(p_dec
, "thread type %d: disabling hardware acceleration",
1519 p_context
->active_thread_type
);
1526 for( size_t i
= 0; pi_fmt
[i
] != AV_PIX_FMT_NONE
; i
++ )
1528 enum PixelFormat hwfmt
= pi_fmt
[i
];
1530 p_dec
->fmt_out
.video
.i_chroma
= vlc_va_GetChroma(hwfmt
, swfmt
);
1531 if (p_dec
->fmt_out
.video
.i_chroma
== 0)
1532 continue; /* Unknown brand of hardware acceleration */
1533 if (p_context
->width
== 0 || p_context
->height
== 0)
1534 { /* should never happen */
1535 msg_Err(p_dec
, "unspecified video dimensions");
1538 if (lavc_UpdateVideoFormat(p_dec
, p_context
, hwfmt
, swfmt
))
1539 continue; /* Unsupported brand of hardware acceleration */
1542 picture_t
*test_pic
= decoder_NewPicture(p_dec
);
1543 assert(!test_pic
|| test_pic
->format
.i_chroma
== p_dec
->fmt_out
.video
.i_chroma
);
1544 vlc_va_t
*va
= vlc_va_New(VLC_OBJECT(p_dec
), p_context
, hwfmt
,
1546 test_pic
? test_pic
->p_sys
: NULL
);
1548 picture_Release(test_pic
);
1552 continue; /* Unsupported codec profile or such */
1555 if (va
->description
!= NULL
)
1556 msg_Info(p_dec
, "Using %s for hardware decoding", va
->description
);
1559 p_sys
->pix_fmt
= hwfmt
;
1560 p_context
->draw_horiz_band
= NULL
;
1565 /* Fallback to default behaviour */
1566 p_sys
->pix_fmt
= swfmt
;