1 /*****************************************************************************
2  * video.c: video decoder using the libavcodec library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VLC authors and VideoLAN
5  * $Id: b99eb56e4a7f8d3e0ad893eca210c3e23c7032eb $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_avcodec.h>
35 #include <vlc_cpu.h>
36 #include <vlc_atomic.h>
37 #include <assert.h>
38 
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>
44 #endif
45 
46 #include "avcodec.h"
47 #include "va.h"
48 
49 #include "../codec/cc.h"
50 
51 /*****************************************************************************
52  * decoder_sys_t : decoder descriptor
53  *****************************************************************************/
54 struct decoder_sys_t
55 {
56     AVCodecContext *p_context;
57     const AVCodec  *p_codec;
58 
59     /* Video decoder specific part */
60     date_t  pts;
61 
62     /* Closed captions for decoders */
63     cc_data_t cc;
64 
65     /* for frame skipping algo */
66     bool b_hurry_up;
67     bool b_show_corrupted;
68     bool b_from_preroll;
69     enum AVDiscard i_skip_frame;
70 
71     /* how many decoded frames are late */
72     int     i_late_frames;
73     mtime_t i_late_frames_start;
74     mtime_t i_last_late_delay;
75 
76     /* for direct rendering */
77     bool        b_direct_rendering;
78     atomic_bool b_dr_failure;
79 
80     /* Hack to force display of still pictures */
81     bool b_first_frame;
82 
83     bool b_draining;
84 
85     /* */
86     bool palette_sent;
87 
88     /* VA API */
89     vlc_va_t *p_va;
90     enum PixelFormat pix_fmt;
91     int profile;
92     int level;
93 
94     vlc_sem_t sem_mt;
95 };
96 
wait_mt(decoder_sys_t * sys)97 static inline void wait_mt(decoder_sys_t *sys)
98 {
99     vlc_sem_wait(&sys->sem_mt);
100 }
101 
post_mt(decoder_sys_t * sys)102 static inline void post_mt(decoder_sys_t *sys)
103 {
104     vlc_sem_post(&sys->sem_mt);
105 }
106 
107 /*****************************************************************************
108  * Local prototypes
109  *****************************************************************************/
110 static void ffmpeg_InitCodec      ( decoder_t * );
111 static int lavc_GetFrame(struct AVCodecContext *, AVFrame *, int);
112 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
113                                           const enum PixelFormat * );
114 static int  DecodeVideo( decoder_t *, block_t * );
115 static void Flush( decoder_t * );
116 
ffmpeg_CodecTag(vlc_fourcc_t fcc)117 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
118 {
119     uint8_t *p = (uint8_t*)&fcc;
120     return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
121 }
122 
123 /*****************************************************************************
124  * Local Functions
125  *****************************************************************************/
126 
127 /**
128  * Sets the decoder output format.
129  */
lavc_GetVideoFormat(decoder_t * dec,video_format_t * restrict fmt,AVCodecContext * ctx,enum AVPixelFormat pix_fmt,enum AVPixelFormat sw_pix_fmt)130 static int lavc_GetVideoFormat(decoder_t *dec, video_format_t *restrict fmt,
131                                AVCodecContext *ctx, enum AVPixelFormat pix_fmt,
132                                enum AVPixelFormat sw_pix_fmt)
133 {
134     int width = ctx->coded_width;
135     int height = ctx->coded_height;
136 
137     video_format_Init(fmt, 0);
138 
139     if (pix_fmt == sw_pix_fmt)
140     {   /* software decoding */
141         int aligns[AV_NUM_DATA_POINTERS];
142 
143         if (GetVlcChroma(fmt, pix_fmt))
144             return -1;
145 
146         /* The libavcodec palette can only be fetched when the first output
147          * frame is decoded. Assume that the current chroma is RGB32 while we
148          * are waiting for a valid palette. Indeed, fmt_out.video.p_palette
149          * doesn't trigger a new vout request, but a new chroma yes. */
150         if (pix_fmt == AV_PIX_FMT_PAL8 && !dec->fmt_out.video.p_palette)
151             fmt->i_chroma = VLC_CODEC_RGB32;
152 
153         avcodec_align_dimensions2(ctx, &width, &height, aligns);
154     }
155     else /* hardware decoding */
156         fmt->i_chroma = vlc_va_GetChroma(pix_fmt, sw_pix_fmt);
157 
158     if( width == 0 || height == 0 || width > 8192 || height > 8192 ||
159         width < ctx->width || height < ctx->height )
160     {
161         msg_Err(dec, "Invalid frame size %dx%d vsz %dx%d",
162                      width, height, ctx->width, ctx->height );
163         return -1; /* invalid display size */
164     }
165 
166     fmt->i_width = width;
167     fmt->i_height = height;
168     fmt->i_visible_width = ctx->width;
169     fmt->i_visible_height = ctx->height;
170 
171     /* If an aspect-ratio was specified in the input format then force it */
172     if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0)
173     {
174         fmt->i_sar_num = dec->fmt_in.video.i_sar_num;
175         fmt->i_sar_den = dec->fmt_in.video.i_sar_den;
176     }
177     else
178     {
179         fmt->i_sar_num = ctx->sample_aspect_ratio.num;
180         fmt->i_sar_den = ctx->sample_aspect_ratio.den;
181 
182         if (fmt->i_sar_num == 0 || fmt->i_sar_den == 0)
183             fmt->i_sar_num = fmt->i_sar_den = 1;
184     }
185 
186     if (dec->fmt_in.video.i_frame_rate > 0
187      && dec->fmt_in.video.i_frame_rate_base > 0)
188     {
189         fmt->i_frame_rate = dec->fmt_in.video.i_frame_rate;
190         fmt->i_frame_rate_base = dec->fmt_in.video.i_frame_rate_base;
191     }
192     else if (ctx->framerate.num > 0 && ctx->framerate.den > 0)
193     {
194         fmt->i_frame_rate = ctx->framerate.num;
195         fmt->i_frame_rate_base = ctx->framerate.den;
196 # if LIBAVCODEC_VERSION_MICRO <  100
197         // for some reason libav don't thinkg framerate presents actually same thing as in ffmpeg
198         fmt->i_frame_rate_base *= __MAX(ctx->ticks_per_frame, 1);
199 # endif
200     }
201     else if (ctx->time_base.num > 0 && ctx->time_base.den > 0)
202     {
203         fmt->i_frame_rate = ctx->time_base.den;
204         fmt->i_frame_rate_base = ctx->time_base.num
205                                  * __MAX(ctx->ticks_per_frame, 1);
206     }
207 
208     /* FIXME we should only set the known values and let the core decide
209      * later of fallbacks, but we can't do that with a boolean */
210     switch ( ctx->color_range )
211     {
212     case AVCOL_RANGE_JPEG:
213         fmt->b_color_range_full = true;
214         break;
215     case AVCOL_RANGE_UNSPECIFIED:
216         fmt->b_color_range_full = !vlc_fourcc_IsYUV( fmt->i_chroma );
217         break;
218     case AVCOL_RANGE_MPEG:
219     default:
220         fmt->b_color_range_full = false;
221         break;
222     }
223 
224     switch( ctx->colorspace )
225     {
226         case AVCOL_SPC_BT709:
227             fmt->space = COLOR_SPACE_BT709;
228             break;
229         case AVCOL_SPC_SMPTE170M:
230         case AVCOL_SPC_BT470BG:
231             fmt->space = COLOR_SPACE_BT601;
232             break;
233         case AVCOL_SPC_BT2020_NCL:
234         case AVCOL_SPC_BT2020_CL:
235             fmt->space = COLOR_SPACE_BT2020;
236             break;
237         default:
238             break;
239     }
240 
241     switch( ctx->color_trc )
242     {
243         case AVCOL_TRC_LINEAR:
244             fmt->transfer = TRANSFER_FUNC_LINEAR;
245             break;
246         case AVCOL_TRC_GAMMA22:
247             fmt->transfer = TRANSFER_FUNC_SRGB;
248             break;
249         case AVCOL_TRC_BT709:
250             fmt->transfer = TRANSFER_FUNC_BT709;
251             break;
252         case AVCOL_TRC_SMPTE170M:
253         case AVCOL_TRC_BT2020_10:
254         case AVCOL_TRC_BT2020_12:
255             fmt->transfer = TRANSFER_FUNC_BT2020;
256             break;
257 #if LIBAVUTIL_VERSION_CHECK( 55, 14, 0, 31, 100)
258         case AVCOL_TRC_ARIB_STD_B67:
259             fmt->transfer = TRANSFER_FUNC_ARIB_B67;
260             break;
261 #endif
262 #if LIBAVUTIL_VERSION_CHECK( 55, 17, 0, 37, 100)
263         case AVCOL_TRC_SMPTE2084:
264             fmt->transfer = TRANSFER_FUNC_SMPTE_ST2084;
265             break;
266         case AVCOL_TRC_SMPTE240M:
267             fmt->transfer = TRANSFER_FUNC_SMPTE_240;
268             break;
269         case AVCOL_TRC_GAMMA28:
270             fmt->transfer = TRANSFER_FUNC_BT470_BG;
271             break;
272 #endif
273         default:
274             break;
275     }
276 
277     switch( ctx->color_primaries )
278     {
279         case AVCOL_PRI_BT709:
280             fmt->primaries = COLOR_PRIMARIES_BT709;
281             break;
282         case AVCOL_PRI_BT470BG:
283             fmt->primaries = COLOR_PRIMARIES_BT601_625;
284             break;
285         case AVCOL_PRI_SMPTE170M:
286         case AVCOL_PRI_SMPTE240M:
287             fmt->primaries = COLOR_PRIMARIES_BT601_525;
288             break;
289         case AVCOL_PRI_BT2020:
290             fmt->primaries = COLOR_PRIMARIES_BT2020;
291             break;
292         default:
293             break;
294     }
295 
296     switch( ctx->chroma_sample_location )
297     {
298         case AVCHROMA_LOC_LEFT:
299             fmt->chroma_location = CHROMA_LOCATION_LEFT;
300             break;
301         case AVCHROMA_LOC_CENTER:
302             fmt->chroma_location = CHROMA_LOCATION_CENTER;
303             break;
304         case AVCHROMA_LOC_TOPLEFT:
305             fmt->chroma_location = CHROMA_LOCATION_TOP_LEFT;
306             break;
307         default:
308             break;
309     }
310 
311     return 0;
312 }
313 
lavc_UpdateVideoFormat(decoder_t * dec,AVCodecContext * ctx,enum AVPixelFormat fmt,enum AVPixelFormat swfmt)314 static int lavc_UpdateVideoFormat(decoder_t *dec, AVCodecContext *ctx,
315                                   enum AVPixelFormat fmt,
316                                   enum AVPixelFormat swfmt)
317 {
318     video_format_t fmt_out;
319     int val;
320 
321     val = lavc_GetVideoFormat(dec, &fmt_out, ctx, fmt, swfmt);
322     if (val)
323         return val;
324 
325     /* always have date in fields/ticks units */
326     if(dec->p_sys->pts.i_divider_num)
327         date_Change(&dec->p_sys->pts, fmt_out.i_frame_rate *
328                                       __MAX(ctx->ticks_per_frame, 1),
329                                       fmt_out.i_frame_rate_base);
330     else
331         date_Init(&dec->p_sys->pts, fmt_out.i_frame_rate *
332                                     __MAX(ctx->ticks_per_frame, 1),
333                                     fmt_out.i_frame_rate_base);
334 
335     fmt_out.p_palette = dec->fmt_out.video.p_palette;
336     dec->fmt_out.video.p_palette = NULL;
337 
338     es_format_Change(&dec->fmt_out, VIDEO_ES, fmt_out.i_chroma);
339     dec->fmt_out.video = fmt_out;
340     dec->fmt_out.video.orientation = dec->fmt_in.video.orientation;
341     dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
342     dec->fmt_out.video.multiview_mode = dec->fmt_in.video.multiview_mode;
343     dec->fmt_out.video.pose = dec->fmt_in.video.pose;
344     if ( dec->fmt_in.video.mastering.max_luminance )
345         dec->fmt_out.video.mastering = dec->fmt_in.video.mastering;
346     dec->fmt_out.video.lighting = dec->fmt_in.video.lighting;
347 
348     return decoder_UpdateVideoFormat(dec);
349 }
350 
351 /**
352  * Copies a picture from the libavcodec-allocate buffer to a picture_t.
353  * This is used when not in direct rendering mode.
354  */
lavc_CopyPicture(decoder_t * dec,picture_t * pic,AVFrame * frame)355 static int lavc_CopyPicture(decoder_t *dec, picture_t *pic, AVFrame *frame)
356 {
357     decoder_sys_t *sys = dec->p_sys;
358 
359     vlc_fourcc_t fourcc = FindVlcChroma(frame->format);
360     if (!fourcc)
361     {
362         const char *name = av_get_pix_fmt_name(frame->format);
363 
364         msg_Err(dec, "Unsupported decoded output format %d (%s)",
365                 sys->p_context->pix_fmt, (name != NULL) ? name : "unknown");
366         return VLC_EGENERIC;
367     } else if (fourcc != pic->format.i_chroma
368      /* ensure we never read more than dst lines/pixels from src */
369      || frame->width != (int) pic->format.i_visible_width
370      || frame->height < (int) pic->format.i_visible_height)
371     {
372         msg_Warn(dec, "dropping frame because the vout changed");
373         return VLC_EGENERIC;
374     }
375 
376     for (int plane = 0; plane < pic->i_planes; plane++)
377     {
378         const uint8_t *src = frame->data[plane];
379         uint8_t *dst = pic->p[plane].p_pixels;
380         size_t src_stride = frame->linesize[plane];
381         size_t dst_stride = pic->p[plane].i_pitch;
382         size_t size = __MIN(src_stride, dst_stride);
383 
384         for (int line = 0; line < pic->p[plane].i_visible_lines; line++)
385         {
386             memcpy(dst, src, size);
387             src += src_stride;
388             dst += dst_stride;
389         }
390     }
391     return VLC_SUCCESS;
392 }
393 
OpenVideoCodec(decoder_t * p_dec)394 static int OpenVideoCodec( decoder_t *p_dec )
395 {
396     decoder_sys_t *p_sys = p_dec->p_sys;
397     AVCodecContext *ctx = p_sys->p_context;
398     const AVCodec *codec = p_sys->p_codec;
399     int ret;
400 
401     if( ctx->extradata_size <= 0 )
402     {
403         if( codec->id == AV_CODEC_ID_VC1 ||
404             codec->id == AV_CODEC_ID_THEORA )
405         {
406             msg_Warn( p_dec, "waiting for extra data for codec %s",
407                       codec->name );
408             return 1;
409         }
410     }
411 
412     ctx->width  = p_dec->fmt_in.video.i_visible_width;
413     ctx->height = p_dec->fmt_in.video.i_visible_height;
414 
415     ctx->coded_width = p_dec->fmt_in.video.i_width;
416     ctx->coded_height = p_dec->fmt_in.video.i_height;
417 
418     ctx->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
419     p_sys->pix_fmt = AV_PIX_FMT_NONE;
420     p_sys->profile = -1;
421     p_sys->level = -1;
422     cc_Init( &p_sys->cc );
423 
424     set_video_color_settings( &p_dec->fmt_in.video, ctx );
425     if( p_dec->fmt_in.video.i_frame_rate_base &&
426         p_dec->fmt_in.video.i_frame_rate &&
427         (double) p_dec->fmt_in.video.i_frame_rate /
428                  p_dec->fmt_in.video.i_frame_rate_base < 6 )
429     {
430         ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
431     }
432 
433     post_mt( p_sys );
434     ret = ffmpeg_OpenCodec( p_dec, ctx, codec );
435     wait_mt( p_sys );
436     if( ret < 0 )
437         return ret;
438 
439     switch( ctx->active_thread_type )
440     {
441         case FF_THREAD_FRAME:
442             msg_Dbg( p_dec, "using frame thread mode with %d threads",
443                      ctx->thread_count );
444             break;
445         case FF_THREAD_SLICE:
446             msg_Dbg( p_dec, "using slice thread mode with %d threads",
447                      ctx->thread_count );
448             break;
449         case 0:
450             if( ctx->thread_count > 1 )
451                 msg_Warn( p_dec, "failed to enable threaded decoding" );
452             break;
453         default:
454             msg_Warn( p_dec, "using unknown thread mode with %d threads",
455                       ctx->thread_count );
456             break;
457     }
458     return 0;
459 }
460 
461 /*****************************************************************************
462  * InitVideo: initialize the video decoder
463  *****************************************************************************
464  * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
465  * opened (done after the first decoded frame).
466  *****************************************************************************/
InitVideoDec(vlc_object_t * obj)467 int InitVideoDec( vlc_object_t *obj )
468 {
469     decoder_t *p_dec = (decoder_t *)obj;
470     const AVCodec *p_codec;
471     AVCodecContext *p_context = ffmpeg_AllocContext( p_dec, &p_codec );
472     if( p_context == NULL )
473         return VLC_EGENERIC;
474 
475     int i_val;
476 
477     /* Allocate the memory needed to store the decoder's structure */
478     decoder_sys_t *p_sys = calloc( 1, sizeof(*p_sys) );
479     if( unlikely(p_sys == NULL) )
480     {
481         avcodec_free_context( &p_context );
482         return VLC_ENOMEM;
483     }
484 
485     p_dec->p_sys = p_sys;
486     p_sys->p_context = p_context;
487     p_sys->p_codec = p_codec;
488     p_sys->p_va = NULL;
489     vlc_sem_init( &p_sys->sem_mt, 0 );
490 
491     /* ***** Fill p_context with init values ***** */
492     p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?
493                                 p_dec->fmt_in.i_original_fourcc : p_dec->fmt_in.i_codec );
494 
495     /*  ***** Get configuration of ffmpeg plugin ***** */
496     p_context->workaround_bugs =
497         var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
498     p_context->err_recognition =
499         var_InheritInteger( p_dec, "avcodec-error-resilience" );
500 
501     if( var_CreateGetBool( p_dec, "grayscale" ) )
502         p_context->flags |= AV_CODEC_FLAG_GRAY;
503 
504     /* ***** Output always the frames ***** */
505     p_context->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT;
506 
507     i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
508     if( i_val >= 4 ) p_context->skip_loop_filter = AVDISCARD_ALL;
509     else if( i_val == 3 ) p_context->skip_loop_filter = AVDISCARD_NONKEY;
510     else if( i_val == 2 ) p_context->skip_loop_filter = AVDISCARD_BIDIR;
511     else if( i_val == 1 ) p_context->skip_loop_filter = AVDISCARD_NONREF;
512     else p_context->skip_loop_filter = AVDISCARD_DEFAULT;
513 
514     if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
515         p_context->flags2 |= AV_CODEC_FLAG2_FAST;
516 
517     /* ***** libavcodec frame skipping ***** */
518     p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
519     p_sys->b_show_corrupted = var_CreateGetBool( p_dec, "avcodec-corrupted" );
520 
521     i_val = var_CreateGetInteger( p_dec, "avcodec-skip-frame" );
522     if( i_val >= 4 ) p_sys->i_skip_frame = AVDISCARD_ALL;
523     else if( i_val == 3 ) p_sys->i_skip_frame = AVDISCARD_NONKEY;
524     else if( i_val == 2 ) p_sys->i_skip_frame = AVDISCARD_BIDIR;
525     else if( i_val == 1 ) p_sys->i_skip_frame = AVDISCARD_NONREF;
526     else if( i_val == -1 ) p_sys->i_skip_frame = AVDISCARD_NONE;
527     else p_sys->i_skip_frame = AVDISCARD_DEFAULT;
528     p_context->skip_frame = p_sys->i_skip_frame;
529 
530     i_val = var_CreateGetInteger( p_dec, "avcodec-skip-idct" );
531     if( i_val >= 4 ) p_context->skip_idct = AVDISCARD_ALL;
532     else if( i_val == 3 ) p_context->skip_idct = AVDISCARD_NONKEY;
533     else if( i_val == 2 ) p_context->skip_idct = AVDISCARD_BIDIR;
534     else if( i_val == 1 ) p_context->skip_idct = AVDISCARD_NONREF;
535     else if( i_val == -1 ) p_context->skip_idct = AVDISCARD_NONE;
536     else p_context->skip_idct = AVDISCARD_DEFAULT;
537 
538     /* ***** libavcodec direct rendering ***** */
539     p_sys->b_direct_rendering = false;
540     atomic_init(&p_sys->b_dr_failure, false);
541     if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
542        (p_codec->capabilities & AV_CODEC_CAP_DR1) &&
543         /* No idea why ... but this fixes flickering on some TSCC streams */
544         p_sys->p_codec->id != AV_CODEC_ID_TSCC &&
545         p_sys->p_codec->id != AV_CODEC_ID_CSCD &&
546         p_sys->p_codec->id != AV_CODEC_ID_CINEPAK )
547     {
548         /* Some codecs set pix_fmt only after the 1st frame has been decoded,
549          * so we need to do another check in ffmpeg_GetFrameBuf() */
550         p_sys->b_direct_rendering = true;
551     }
552 
553     p_context->get_format = ffmpeg_GetFormat;
554     /* Always use our get_buffer wrapper so we can calculate the
555      * PTS correctly */
556     p_context->get_buffer2 = lavc_GetFrame;
557     p_context->opaque = p_dec;
558 
559     int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
560     if( i_thread_count <= 0 )
561     {
562         i_thread_count = vlc_GetCPUCount();
563         if( i_thread_count > 1 )
564             i_thread_count++;
565 
566         //FIXME: take in count the decoding time
567 #if VLC_WINSTORE_APP
568         i_thread_count = __MIN( i_thread_count, 6 );
569 #else
570         i_thread_count = __MIN( i_thread_count, p_codec->id == AV_CODEC_ID_HEVC ? 10 : 6 );
571 #endif
572     }
573     i_thread_count = __MIN( i_thread_count, p_codec->id == AV_CODEC_ID_HEVC ? 32 : 16 );
574     msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
575     p_context->thread_count = i_thread_count;
576     p_context->thread_safe_callbacks = true;
577 
578     switch( p_codec->id )
579     {
580         case AV_CODEC_ID_MPEG4:
581         case AV_CODEC_ID_H263:
582             p_context->thread_type = 0;
583             break;
584         case AV_CODEC_ID_MPEG1VIDEO:
585         case AV_CODEC_ID_MPEG2VIDEO:
586             p_context->thread_type &= ~FF_THREAD_SLICE;
587             /* fall through */
588 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
589         case AV_CODEC_ID_H264:
590         case AV_CODEC_ID_VC1:
591         case AV_CODEC_ID_WMV3:
592             p_context->thread_type &= ~FF_THREAD_FRAME;
593 # endif
594         default:
595             break;
596     }
597 
598     if( p_context->thread_type & FF_THREAD_FRAME )
599         p_dec->i_extra_picture_buffers = 2 * p_context->thread_count;
600 
601     /* ***** misc init ***** */
602     date_Init(&p_sys->pts, 1, 30001);
603     date_Set(&p_sys->pts, VLC_TS_INVALID);
604     p_sys->b_first_frame = true;
605     p_sys->i_late_frames = 0;
606     p_sys->b_from_preroll = false;
607     p_sys->b_draining = false;
608 
609     /* Set output properties */
610     if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
611     {
612         /* we are doomed. but not really, because most codecs set their pix_fmt later on */
613         p_dec->fmt_out.i_codec = VLC_CODEC_I420;
614     }
615     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
616 
617     p_dec->fmt_out.video.orientation = p_dec->fmt_in.video.orientation;
618 
619     if( p_dec->fmt_in.video.p_palette ) {
620         p_sys->palette_sent = false;
621         p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
622         if( p_dec->fmt_out.video.p_palette )
623             *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
624     } else
625         p_sys->palette_sent = true;
626 
627     /* ***** init this codec with special data ***** */
628     ffmpeg_InitCodec( p_dec );
629 
630     /* ***** Open the codec ***** */
631     if( OpenVideoCodec( p_dec ) < 0 )
632     {
633         vlc_sem_destroy( &p_sys->sem_mt );
634         free( p_sys );
635         avcodec_free_context( &p_context );
636         return VLC_EGENERIC;
637     }
638 
639     p_dec->pf_decode = DecodeVideo;
640     p_dec->pf_flush  = Flush;
641 
642     /* XXX: Writing input format makes little sense. */
643     if( p_context->profile != FF_PROFILE_UNKNOWN )
644         p_dec->fmt_in.i_profile = p_context->profile;
645     if( p_context->level != FF_LEVEL_UNKNOWN )
646         p_dec->fmt_in.i_level = p_context->level;
647     return VLC_SUCCESS;
648 }
649 
650 /*****************************************************************************
651  * Flush:
652  *****************************************************************************/
Flush(decoder_t * p_dec)653 static void Flush( decoder_t *p_dec )
654 {
655     decoder_sys_t *p_sys = p_dec->p_sys;
656     AVCodecContext *p_context = p_sys->p_context;
657 
658     date_Set(&p_sys->pts, VLC_TS_INVALID); /* To make sure we recover properly */
659     p_sys->i_late_frames = 0;
660     p_sys->b_draining = false;
661     cc_Flush( &p_sys->cc );
662 
663     /* Abort pictures in order to unblock all avcodec workers threads waiting
664      * for a picture. This will avoid a deadlock between avcodec_flush_buffers
665      * and workers threads */
666     decoder_AbortPictures( p_dec, true );
667 
668     post_mt( p_sys );
669     /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
670     if( avcodec_is_open( p_context ) )
671         avcodec_flush_buffers( p_context );
672     wait_mt( p_sys );
673 
674     /* Reset cancel state to false */
675     decoder_AbortPictures( p_dec, false );
676 }
677 
check_block_validity(decoder_sys_t * p_sys,block_t * block)678 static bool check_block_validity( decoder_sys_t *p_sys, block_t *block )
679 {
680     if( !block)
681         return true;
682 
683     if( block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
684     {
685         date_Set( &p_sys->pts, VLC_TS_INVALID ); /* To make sure we recover properly */
686         cc_Flush( &p_sys->cc );
687 
688         p_sys->i_late_frames = 0;
689         if( block->i_flags & BLOCK_FLAG_CORRUPTED )
690         {
691             block_Release( block );
692             return false;
693         }
694     }
695     return true;
696 }
697 
check_block_being_late(decoder_sys_t * p_sys,block_t * block,mtime_t current_time)698 static bool check_block_being_late( decoder_sys_t *p_sys, block_t *block, mtime_t current_time)
699 {
700     if( !block )
701         return false;
702     if( block->i_flags & BLOCK_FLAG_PREROLL )
703     {
704         /* Do not care about late frames when prerolling
705          * TODO avoid decoding of non reference frame
706          * (ie all B except for H264 where it depends only on nal_ref_idc) */
707         p_sys->i_late_frames = 0;
708         p_sys->b_from_preroll = true;
709         p_sys->i_last_late_delay = INT64_MAX;
710     }
711 
712     if( p_sys->i_late_frames <= 0 )
713         return false;
714 
715     if( current_time - p_sys->i_late_frames_start > (5*CLOCK_FREQ))
716     {
717         date_Set( &p_sys->pts, VLC_TS_INVALID ); /* To make sure we recover properly */
718         block_Release( block );
719         p_sys->i_late_frames--;
720         return true;
721     }
722     return false;
723 }
724 
check_frame_should_be_dropped(decoder_sys_t * p_sys,AVCodecContext * p_context,bool * b_need_output_picture)725 static bool check_frame_should_be_dropped( decoder_sys_t *p_sys, AVCodecContext *p_context, bool *b_need_output_picture )
726 {
727     if( p_sys->i_late_frames <= 4)
728         return false;
729 
730     *b_need_output_picture = false;
731     if( p_sys->i_late_frames < 12 )
732     {
733         p_context->skip_frame =
734                 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
735                 AVDISCARD_NONREF : p_sys->i_skip_frame;
736     }
737     else
738     {
739         /* picture too late, won't decode
740          * but break picture until a new I, and for mpeg4 ...*/
741         p_sys->i_late_frames--; /* needed else it will never be decrease */
742         return true;
743     }
744     return false;
745 }
746 
interpolate_next_pts(decoder_t * p_dec,AVFrame * frame)747 static mtime_t interpolate_next_pts( decoder_t *p_dec, AVFrame *frame )
748 {
749     decoder_sys_t *p_sys = p_dec->p_sys;
750     AVCodecContext *p_context = p_sys->p_context;
751 
752     if( date_Get( &p_sys->pts ) == VLC_TS_INVALID ||
753         p_sys->pts.i_divider_num == 0 )
754         return VLC_TS_INVALID;
755 
756     int i_tick = p_context->ticks_per_frame;
757     if( i_tick <= 0 )
758         i_tick = 1;
759 
760     /* interpolate the next PTS */
761     return date_Increment( &p_sys->pts, i_tick + frame->repeat_pict );
762 }
763 
update_late_frame_count(decoder_t * p_dec,block_t * p_block,mtime_t current_time,mtime_t i_pts,mtime_t i_next_pts)764 static void update_late_frame_count( decoder_t *p_dec, block_t *p_block,
765                                      mtime_t current_time, mtime_t i_pts,
766                                      mtime_t i_next_pts )
767 {
768     decoder_sys_t *p_sys = p_dec->p_sys;
769    /* Update frame late count (except when doing preroll) */
770    mtime_t i_display_date = VLC_TS_INVALID;
771    if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
772        i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
773 
774    mtime_t i_threshold = i_next_pts != VLC_TS_INVALID ? (i_next_pts - i_pts) / 2 : 20000;
775 
776    if( i_display_date > VLC_TS_INVALID && i_display_date + i_threshold <= current_time )
777    {
778        /* Out of preroll, consider only late frames on rising delay */
779        if( p_sys->b_from_preroll )
780        {
781            if( p_sys->i_last_late_delay > current_time - i_display_date )
782            {
783                p_sys->i_last_late_delay = current_time - i_display_date;
784                return;
785            }
786            p_sys->b_from_preroll = false;
787        }
788 
789        p_sys->i_late_frames++;
790        if( p_sys->i_late_frames == 1 )
791            p_sys->i_late_frames_start = current_time;
792 
793    }
794    else
795    {
796        p_sys->i_late_frames = 0;
797    }
798 }
799 
800 
DecodeSidedata(decoder_t * p_dec,const AVFrame * frame,picture_t * p_pic)801 static int DecodeSidedata( decoder_t *p_dec, const AVFrame *frame, picture_t *p_pic )
802 {
803     decoder_sys_t *p_sys = p_dec->p_sys;
804     bool format_changed = false;
805 
806 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 16, 101 ) )
807 #define FROM_AVRAT(default_factor, avrat) \
808 (uint64_t)(default_factor) * (avrat).num / (avrat).den
809     const AVFrameSideData *metadata =
810             av_frame_get_side_data( frame,
811                                     AV_FRAME_DATA_MASTERING_DISPLAY_METADATA );
812     if ( metadata )
813     {
814         const AVMasteringDisplayMetadata *hdr_meta =
815                 (const AVMasteringDisplayMetadata *) metadata->data;
816         if ( hdr_meta->has_luminance )
817         {
818 #define ST2086_LUMA_FACTOR 10000
819             p_pic->format.mastering.max_luminance =
820                     FROM_AVRAT(ST2086_LUMA_FACTOR, hdr_meta->max_luminance);
821             p_pic->format.mastering.min_luminance =
822                     FROM_AVRAT(ST2086_LUMA_FACTOR, hdr_meta->min_luminance);
823         }
824         if ( hdr_meta->has_primaries )
825         {
826 #define ST2086_RED   2
827 #define ST2086_GREEN 0
828 #define ST2086_BLUE  1
829 #define LAV_RED    0
830 #define LAV_GREEN  1
831 #define LAV_BLUE   2
832 #define ST2086_PRIM_FACTOR 50000
833             p_pic->format.mastering.primaries[ST2086_RED*2   + 0] =
834                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_RED][0]);
835             p_pic->format.mastering.primaries[ST2086_RED*2   + 1] =
836                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_RED][1]);
837             p_pic->format.mastering.primaries[ST2086_GREEN*2 + 0] =
838                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_GREEN][0]);
839             p_pic->format.mastering.primaries[ST2086_GREEN*2 + 1] =
840                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_GREEN][1]);
841             p_pic->format.mastering.primaries[ST2086_BLUE*2  + 0] =
842                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_BLUE][0]);
843             p_pic->format.mastering.primaries[ST2086_BLUE*2  + 1] =
844                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->display_primaries[LAV_BLUE][1]);
845             p_pic->format.mastering.white_point[0] =
846                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->white_point[0]);
847             p_pic->format.mastering.white_point[1] =
848                     FROM_AVRAT(ST2086_PRIM_FACTOR, hdr_meta->white_point[1]);
849         }
850 
851         if ( memcmp( &p_dec->fmt_out.video.mastering,
852                      &p_pic->format.mastering,
853                      sizeof(p_pic->format.mastering) ) )
854         {
855             p_dec->fmt_out.video.mastering = p_pic->format.mastering;
856             format_changed = true;
857         }
858 #undef FROM_AVRAT
859     }
860 #endif
861 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 60, 100 ) )
862     const AVFrameSideData *metadata_lt =
863             av_frame_get_side_data( frame,
864                                     AV_FRAME_DATA_CONTENT_LIGHT_LEVEL );
865     if ( metadata_lt )
866     {
867         const AVContentLightMetadata *light_meta =
868                 (const AVContentLightMetadata *) metadata_lt->data;
869         p_pic->format.lighting.MaxCLL = light_meta->MaxCLL;
870         p_pic->format.lighting.MaxFALL = light_meta->MaxFALL;
871         if ( memcmp( &p_dec->fmt_out.video.lighting,
872                      &p_pic->format.lighting,
873                      sizeof(p_pic->format.lighting) ) )
874         {
875             p_dec->fmt_out.video.lighting  = p_pic->format.lighting;
876             format_changed = true;
877         }
878     }
879 #endif
880 
881     if (format_changed && decoder_UpdateVideoFormat( p_dec ))
882         return -1;
883 
884     const AVFrameSideData *p_avcc = av_frame_get_side_data( frame, AV_FRAME_DATA_A53_CC );
885     if( p_avcc )
886     {
887         cc_Extract( &p_sys->cc, CC_PAYLOAD_RAW, true, p_avcc->data, p_avcc->size );
888         if( p_sys->cc.b_reorder || p_sys->cc.i_data )
889         {
890             block_t *p_cc = block_Alloc( p_sys->cc.i_data );
891             if( p_cc )
892             {
893                 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
894                 if( p_sys->cc.b_reorder )
895                     p_cc->i_dts = p_cc->i_pts = p_pic->date;
896                 else
897                     p_cc->i_pts = p_cc->i_dts;
898                 decoder_cc_desc_t desc;
899                 desc.i_608_channels = p_sys->cc.i_608channels;
900                 desc.i_708_channels = p_sys->cc.i_708channels;
901                 desc.i_reorder_depth = 4;
902                 decoder_QueueCc( p_dec, p_cc, &desc );
903             }
904             cc_Flush( &p_sys->cc );
905         }
906     }
907     return 0;
908 }
909 
910 /*****************************************************************************
911  * DecodeBlock: Called to decode one or more frames
912  *****************************************************************************/
DecodeBlock(decoder_t * p_dec,block_t ** pp_block,bool * error)913 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block, bool *error )
914 {
915     decoder_sys_t *p_sys = p_dec->p_sys;
916     AVCodecContext *p_context = p_sys->p_context;
917     /* Boolean if we assume that we should get valid pic as result */
918     bool b_need_output_picture = true;
919 
920     /* Boolean for END_OF_SEQUENCE */
921     bool eos_spotted = false;
922 
923 
924     block_t *p_block;
925     mtime_t current_time;
926 
927     if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
928     {
929         ffmpeg_InitCodec( p_dec );
930         if( !avcodec_is_open( p_context ) )
931             OpenVideoCodec( p_dec );
932     }
933 
934     p_block = pp_block ? *pp_block : NULL;
935     if(!p_block && !(p_sys->p_codec->capabilities & AV_CODEC_CAP_DELAY) )
936         return NULL;
937 
938     if( !avcodec_is_open( p_context ) )
939     {
940         if( p_block )
941             block_Release( p_block );
942         return NULL;
943     }
944 
945     if( !check_block_validity( p_sys, p_block ) )
946         return NULL;
947 
948     current_time = mdate();
949     if( p_dec->b_frame_drop_allowed &&  check_block_being_late( p_sys, p_block, current_time) )
950     {
951         msg_Err( p_dec, "more than 5 seconds of late video -> "
952                  "dropping frame (computer too slow ?)" );
953         return NULL;
954     }
955 
956 
957     /* A good idea could be to decode all I pictures and see for the other */
958 
959     /* Defaults that if we aren't in prerolling, we want output picture
960        same for if we are flushing (p_block==NULL) */
961     if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
962         b_need_output_picture = true;
963     else
964         b_need_output_picture = false;
965 
966     /* Change skip_frame config only if hurry_up is enabled */
967     if( p_sys->b_hurry_up )
968     {
969         p_context->skip_frame = p_sys->i_skip_frame;
970 
971         /* Check also if we should/can drop the block and move to next block
972             as trying to catchup the speed*/
973         if( p_dec->b_frame_drop_allowed &&
974             check_frame_should_be_dropped( p_sys, p_context, &b_need_output_picture ) )
975         {
976             if( p_block )
977                 block_Release( p_block );
978             msg_Warn( p_dec, "More than 11 late frames, dropping frame" );
979             return NULL;
980         }
981     }
982     if( !b_need_output_picture )
983     {
984         p_context->skip_frame = __MAX( p_context->skip_frame,
985                                               AVDISCARD_NONREF );
986     }
987 
988     /*
989      * Do the actual decoding now */
990 
991     /* Don't forget that libavcodec requires a little more bytes
992      * that the real frame size */
993     if( p_block && p_block->i_buffer > 0 )
994     {
995         eos_spotted = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
996 
997         p_block = block_Realloc( p_block, 0,
998                             p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
999         if( !p_block )
1000             return NULL;
1001         p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
1002         *pp_block = p_block;
1003         memset( p_block->p_buffer + p_block->i_buffer, 0,
1004                 FF_INPUT_BUFFER_PADDING_SIZE );
1005     }
1006 
1007     do
1008     {
1009         int ret;
1010         int i_used = 0;
1011         const bool b_has_data = ( p_block && p_block->i_buffer > 0 );
1012         const bool b_start_drain = ((pp_block == NULL) || eos_spotted) && !p_sys->b_draining;
1013 
1014         post_mt( p_sys );
1015 
1016         if( b_has_data || b_start_drain )
1017         {
1018             AVPacket pkt;
1019             av_init_packet( &pkt );
1020             if( b_has_data )
1021             {
1022                 pkt.data = p_block->p_buffer;
1023                 pkt.size = p_block->i_buffer;
1024                 pkt.pts = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : AV_NOPTS_VALUE;
1025                 pkt.dts = p_block->i_dts > VLC_TS_INVALID ? p_block->i_dts : AV_NOPTS_VALUE;
1026 
1027                 /* Make sure we don't reuse the same timestamps twice */
1028                 p_block->i_pts =
1029                 p_block->i_dts = VLC_TS_INVALID;
1030             }
1031             else /* start drain */
1032             {
1033                 /* Return delayed frames if codec has CODEC_CAP_DELAY */
1034                 pkt.data = NULL;
1035                 pkt.size = 0;
1036                 p_sys->b_draining = true;
1037             }
1038 
1039             if( !p_sys->palette_sent )
1040             {
1041                 uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
1042                 if (pal) {
1043                     memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE);
1044                     p_sys->palette_sent = true;
1045                 }
1046             }
1047 
1048             ret = avcodec_send_packet(p_context, &pkt);
1049             if( ret != 0 && ret != AVERROR(EAGAIN) )
1050             {
1051                 if (ret == AVERROR(ENOMEM) || ret == AVERROR(EINVAL))
1052                 {
1053                     msg_Err(p_dec, "avcodec_send_packet critical error");
1054                     *error = true;
1055                 }
1056                 av_packet_unref( &pkt );
1057                 break;
1058             }
1059             i_used = ret != AVERROR(EAGAIN) ? pkt.size : 0;
1060             av_packet_unref( &pkt );
1061         }
1062 
1063         AVFrame *frame = av_frame_alloc();
1064         if (unlikely(frame == NULL))
1065         {
1066             *error = true;
1067             break;
1068         }
1069 
1070         ret = avcodec_receive_frame(p_context, frame);
1071         if( ret != 0 && ret != AVERROR(EAGAIN) )
1072         {
1073             if (ret == AVERROR(ENOMEM) || ret == AVERROR(EINVAL))
1074             {
1075                 msg_Err(p_dec, "avcodec_receive_frame critical error");
1076                 *error = true;
1077             }
1078             av_frame_free(&frame);
1079             /* After draining, we need to reset decoder with a flush */
1080             if( ret == AVERROR_EOF )
1081             {
1082                 avcodec_flush_buffers( p_sys->p_context );
1083                 p_sys->b_draining = false;
1084             }
1085             break;
1086         }
1087         bool not_received_frame = ret;
1088 
1089         wait_mt( p_sys );
1090 
1091         if( eos_spotted )
1092             p_sys->b_first_frame = true;
1093 
1094         if( p_block )
1095         {
1096             if( p_block->i_buffer <= 0 )
1097                 eos_spotted = false;
1098 
1099             /* Consumed bytes */
1100             p_block->p_buffer += i_used;
1101             p_block->i_buffer -= i_used;
1102         }
1103 
1104         /* Nothing to display */
1105         if( not_received_frame )
1106         {
1107             av_frame_free(&frame);
1108             if( i_used == 0 ) break;
1109             continue;
1110         }
1111 
1112         /* Compute the PTS */
1113 #ifdef FF_API_PKT_PTS
1114         mtime_t i_pts = frame->pts;
1115 #else
1116         mtime_t i_pts = frame->pkt_pts;
1117 #endif
1118         if (i_pts == AV_NOPTS_VALUE )
1119             i_pts = frame->pkt_dts;
1120 
1121         if( i_pts == AV_NOPTS_VALUE )
1122             i_pts = date_Get( &p_sys->pts );
1123 
1124         /* Interpolate the next PTS */
1125         if( i_pts > VLC_TS_INVALID )
1126             date_Set( &p_sys->pts, i_pts );
1127 
1128         const mtime_t i_next_pts = interpolate_next_pts(p_dec, frame);
1129 
1130         update_late_frame_count( p_dec, p_block, current_time, i_pts, i_next_pts);
1131 
1132         if( !b_need_output_picture ||
1133            ( !p_sys->p_va && !frame->linesize[0] ) ||
1134            ( p_dec->b_frame_drop_allowed && (frame->flags & AV_FRAME_FLAG_CORRUPT) &&
1135              !p_sys->b_show_corrupted ) )
1136         {
1137             av_frame_free(&frame);
1138             continue;
1139         }
1140 
1141         if( p_context->pix_fmt == AV_PIX_FMT_PAL8
1142          && !p_dec->fmt_out.video.p_palette )
1143         {
1144             /* See AV_PIX_FMT_PAL8 comment in avc_GetVideoFormat(): update the
1145              * fmt_out palette and change the fmt_out chroma to request a new
1146              * vout */
1147             assert( p_dec->fmt_out.video.i_chroma != VLC_CODEC_RGBP );
1148 
1149             video_palette_t *p_palette;
1150             p_palette = p_dec->fmt_out.video.p_palette
1151                       = malloc( sizeof(video_palette_t) );
1152             if( !p_palette )
1153             {
1154                 *error = true;
1155                 av_frame_free(&frame);
1156                 break;
1157             }
1158             static_assert( sizeof(p_palette->palette) == AVPALETTE_SIZE,
1159                            "Palette size mismatch between vlc and libavutil" );
1160             assert( frame->data[1] != NULL );
1161             memcpy( p_palette->palette, frame->data[1], AVPALETTE_SIZE );
1162             p_palette->i_entries = AVPALETTE_COUNT;
1163             p_dec->fmt_out.video.i_chroma = VLC_CODEC_RGBP;
1164             if( decoder_UpdateVideoFormat( p_dec ) )
1165             {
1166                 av_frame_free(&frame);
1167                 continue;
1168             }
1169         }
1170 
1171         picture_t *p_pic = frame->opaque;
1172         if( p_pic == NULL )
1173         {   /* When direct rendering is not used, get_format() and get_buffer()
1174              * might not be called. The output video format must be set here
1175              * then picture buffer can be allocated. */
1176             if (p_sys->p_va == NULL
1177              && lavc_UpdateVideoFormat(p_dec, p_context, p_context->pix_fmt,
1178                                        p_context->pix_fmt) == 0)
1179                 p_pic = decoder_NewPicture(p_dec);
1180 
1181             if( !p_pic )
1182             {
1183                 av_frame_free(&frame);
1184                 break;
1185             }
1186 
1187             /* Fill picture_t from AVFrame */
1188             if( lavc_CopyPicture( p_dec, p_pic, frame ) != VLC_SUCCESS )
1189             {
1190                 av_frame_free(&frame);
1191                 picture_Release( p_pic );
1192                 break;
1193             }
1194         }
1195         else
1196         {
1197             /* Some codecs can return the same frame multiple times. By the
1198              * time that the same frame is returned a second time, it will be
1199              * too late to clone the underlying picture. So clone proactively.
1200              * A single picture CANNOT be queued multiple times.
1201              */
1202             p_pic = picture_Clone( p_pic );
1203             if( unlikely(p_pic == NULL) )
1204             {
1205                 av_frame_free(&frame);
1206                 break;
1207             }
1208         }
1209 
1210         if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
1211         {
1212             /* Fetch again the aspect ratio in case it changed */
1213             p_dec->fmt_out.video.i_sar_num
1214                 = p_context->sample_aspect_ratio.num;
1215             p_dec->fmt_out.video.i_sar_den
1216                 = p_context->sample_aspect_ratio.den;
1217 
1218             if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
1219             {
1220                 p_dec->fmt_out.video.i_sar_num = 1;
1221                 p_dec->fmt_out.video.i_sar_den = 1;
1222             }
1223         }
1224 
1225         p_pic->date = i_pts;
1226         /* Hack to force display of still pictures */
1227         p_pic->b_force = p_sys->b_first_frame;
1228         p_pic->i_nb_fields = 2 + frame->repeat_pict;
1229         p_pic->b_progressive = !frame->interlaced_frame;
1230         p_pic->b_top_field_first = frame->top_field_first;
1231 
1232         if (DecodeSidedata(p_dec, frame, p_pic))
1233             i_pts = VLC_TS_INVALID;
1234 
1235         av_frame_free(&frame);
1236 
1237         /* Send decoded frame to vout */
1238         if (i_pts > VLC_TS_INVALID)
1239         {
1240             p_sys->b_first_frame = false;
1241             return p_pic;
1242         }
1243         else
1244             picture_Release( p_pic );
1245     } while( true );
1246 
1247     if( p_block )
1248         block_Release( p_block );
1249     if( p_sys->b_draining )
1250     {
1251         avcodec_flush_buffers( p_sys->p_context );
1252         p_sys->b_draining = false;
1253     }
1254     return NULL;
1255 }
1256 
DecodeVideo(decoder_t * p_dec,block_t * p_block)1257 static int DecodeVideo( decoder_t *p_dec, block_t *p_block )
1258 {
1259     block_t **pp_block = p_block ? &p_block : NULL;
1260     picture_t *p_pic;
1261     bool error = false;
1262     while( ( p_pic = DecodeBlock( p_dec, pp_block, &error ) ) != NULL )
1263         decoder_QueueVideo( p_dec, p_pic );
1264     return error ? VLCDEC_ECRITICAL : VLCDEC_SUCCESS;
1265 }
1266 
1267 /*****************************************************************************
1268  * EndVideo: decoder destruction
1269  *****************************************************************************
1270  * This function is called when the thread ends after a successful
1271  * initialization.
1272  *****************************************************************************/
EndVideoDec(vlc_object_t * obj)1273 void EndVideoDec( vlc_object_t *obj )
1274 {
1275     decoder_t *p_dec = (decoder_t *)obj;
1276     decoder_sys_t *p_sys = p_dec->p_sys;
1277     AVCodecContext *ctx = p_sys->p_context;
1278     void *hwaccel_context;
1279 
1280     post_mt( p_sys );
1281 
1282     /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
1283     if( avcodec_is_open( ctx ) )
1284         avcodec_flush_buffers( ctx );
1285 
1286     wait_mt( p_sys );
1287 
1288     cc_Flush( &p_sys->cc );
1289 
1290     hwaccel_context = ctx->hwaccel_context;
1291     avcodec_free_context( &ctx );
1292 
1293     if( p_sys->p_va )
1294         vlc_va_Delete( p_sys->p_va, &hwaccel_context );
1295 
1296     vlc_sem_destroy( &p_sys->sem_mt );
1297     free( p_sys );
1298 }
1299 
1300 /*****************************************************************************
1301  * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
1302  *****************************************************************************/
ffmpeg_InitCodec(decoder_t * p_dec)1303 static void ffmpeg_InitCodec( decoder_t *p_dec )
1304 {
1305     decoder_sys_t *p_sys = p_dec->p_sys;
1306     size_t i_size = p_dec->fmt_in.i_extra;
1307 
1308     if( !i_size ) return;
1309 
1310     if( p_sys->p_codec->id == AV_CODEC_ID_SVQ3 )
1311     {
1312         uint8_t *p;
1313 
1314         p_sys->p_context->extradata_size = i_size + 12;
1315         p = p_sys->p_context->extradata =
1316             av_malloc( p_sys->p_context->extradata_size +
1317                        FF_INPUT_BUFFER_PADDING_SIZE );
1318         if( !p )
1319             return;
1320 
1321         memcpy( &p[0],  "SVQ3", 4 );
1322         memset( &p[4], 0, 8 );
1323         memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
1324 
1325         /* Now remove all atoms before the SMI one */
1326         if( p_sys->p_context->extradata_size > 0x5a &&
1327             strncmp( (char*)&p[0x56], "SMI ", 4 ) )
1328         {
1329             uint8_t *psz = &p[0x52];
1330 
1331             while( psz < &p[p_sys->p_context->extradata_size - 8] )
1332             {
1333                 uint_fast32_t atom_size = GetDWBE( psz );
1334                 if( atom_size <= 1 )
1335                 {
1336                     /* FIXME handle 1 as long size */
1337                     break;
1338                 }
1339                 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
1340                 {
1341                     memmove( &p[0x52], psz,
1342                              &p[p_sys->p_context->extradata_size] - psz );
1343                     break;
1344                 }
1345 
1346                 psz += atom_size;
1347             }
1348         }
1349     }
1350     else
1351     {
1352         p_sys->p_context->extradata_size = i_size;
1353         p_sys->p_context->extradata =
1354             av_malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
1355         if( p_sys->p_context->extradata )
1356         {
1357             memcpy( p_sys->p_context->extradata,
1358                     p_dec->fmt_in.p_extra, i_size );
1359             memset( p_sys->p_context->extradata + i_size,
1360                     0, FF_INPUT_BUFFER_PADDING_SIZE );
1361         }
1362     }
1363 }
1364 
lavc_ReleaseFrame(void * opaque,uint8_t * data)1365 static void lavc_ReleaseFrame(void *opaque, uint8_t *data)
1366 {
1367     (void) data;
1368     picture_t *picture = opaque;
1369 
1370     picture_Release(picture);
1371 }
1372 
lavc_va_GetFrame(struct AVCodecContext * ctx,AVFrame * frame,picture_t * pic)1373 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
1374                             picture_t *pic)
1375 {
1376     decoder_t *dec = ctx->opaque;
1377     vlc_va_t *va = dec->p_sys->p_va;
1378 
1379     if (vlc_va_Get(va, pic, &frame->data[0]))
1380     {
1381         msg_Err(dec, "hardware acceleration picture allocation failed");
1382         picture_Release(pic);
1383         return -1;
1384     }
1385     assert(frame->data[0] != NULL);
1386     /* data[0] must be non-NULL for libavcodec internal checks.
1387      * data[3] actually contains the format-specific surface handle. */
1388     frame->data[3] = frame->data[0];
1389 
1390     frame->buf[0] = av_buffer_create(frame->data[0], 0, lavc_ReleaseFrame, pic, 0);
1391     if (unlikely(frame->buf[0] == NULL))
1392     {
1393         lavc_ReleaseFrame(pic, frame->data[0]);
1394         return -1;
1395     }
1396 
1397     frame->opaque = pic;
1398     return 0;
1399 }
1400 
lavc_dr_GetFrame(struct AVCodecContext * ctx,AVFrame * frame,picture_t * pic)1401 static int lavc_dr_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
1402                             picture_t *pic)
1403 {
1404     decoder_t *dec = (decoder_t *)ctx->opaque;
1405     decoder_sys_t *sys = dec->p_sys;
1406 
1407     if (ctx->pix_fmt == AV_PIX_FMT_PAL8)
1408         goto error;
1409 
1410     int width = frame->width;
1411     int height = frame->height;
1412     int aligns[AV_NUM_DATA_POINTERS];
1413 
1414     avcodec_align_dimensions2(ctx, &width, &height, aligns);
1415 
1416     /* Check that the picture is suitable for libavcodec */
1417     assert(pic->p[0].i_pitch >= width * pic->p[0].i_pixel_pitch);
1418     assert(pic->p[0].i_lines >= height);
1419 
1420     for (int i = 0; i < pic->i_planes; i++)
1421     {
1422         if (pic->p[i].i_pitch % aligns[i])
1423         {
1424             if (!atomic_exchange(&sys->b_dr_failure, true))
1425                 msg_Warn(dec, "plane %d: pitch not aligned (%d%%%d): disabling direct rendering",
1426                          i, pic->p[i].i_pitch, aligns[i]);
1427             goto error;
1428         }
1429         if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1430         {
1431             if (!atomic_exchange(&sys->b_dr_failure, true))
1432                 msg_Warn(dec, "plane %d not aligned: disabling direct rendering", i);
1433             goto error;
1434         }
1435     }
1436 
1437     /* Allocate buffer references and initialize planes */
1438     assert(pic->i_planes < PICTURE_PLANE_MAX);
1439     static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1440 
1441     for (int i = 0; i < pic->i_planes; i++)
1442     {
1443         uint8_t *data = pic->p[i].p_pixels;
1444         int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1445 
1446         frame->data[i] = data;
1447         frame->linesize[i] = pic->p[i].i_pitch;
1448         frame->buf[i] = av_buffer_create(data, size, lavc_ReleaseFrame,
1449                                          pic, 0);
1450         if (unlikely(frame->buf[i] == NULL))
1451         {
1452             while (i > 0)
1453                 av_buffer_unref(&frame->buf[--i]);
1454             goto error;
1455         }
1456         picture_Hold(pic);
1457     }
1458 
1459     frame->opaque = pic;
1460     /* The loop above held one reference to the picture for each plane. */
1461     picture_Release(pic);
1462     return 0;
1463 error:
1464     picture_Release(pic);
1465     return -1;
1466 }
1467 
1468 /**
1469  * Callback used by libavcodec to get a frame buffer.
1470  *
1471  * It is used for direct rendering as well as to get the right PTS for each
1472  * decoded picture (even in indirect rendering mode).
1473  */
lavc_GetFrame(struct AVCodecContext * ctx,AVFrame * frame,int flags)1474 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1475 {
1476     decoder_t *dec = ctx->opaque;
1477     decoder_sys_t *sys = dec->p_sys;
1478     picture_t *pic;
1479 
1480     for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1481     {
1482         frame->data[i] = NULL;
1483         frame->linesize[i] = 0;
1484         frame->buf[i] = NULL;
1485     }
1486     frame->opaque = NULL;
1487 
1488     wait_mt(sys);
1489     if (sys->p_va == NULL)
1490     {
1491         if (!sys->b_direct_rendering)
1492         {
1493             post_mt(sys);
1494             return avcodec_default_get_buffer2(ctx, frame, flags);
1495         }
1496 
1497         /* Most unaccelerated decoders do not call get_format(), so we need to
1498          * update the output video format here. The MT semaphore must be held
1499          * to protect p_dec->fmt_out. */
1500         if (lavc_UpdateVideoFormat(dec, ctx, ctx->pix_fmt, ctx->pix_fmt))
1501         {
1502             post_mt(sys);
1503             return -1;
1504         }
1505     }
1506     post_mt(sys);
1507 
1508     pic = decoder_NewPicture(dec);
1509     if (pic == NULL)
1510         return -ENOMEM;
1511 
1512     if (sys->p_va != NULL)
1513         return lavc_va_GetFrame(ctx, frame, pic);
1514 
1515     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1516      * so we need to check for direct rendering again. */
1517     int ret = lavc_dr_GetFrame(ctx, frame, pic);
1518     if (ret)
1519         ret = avcodec_default_get_buffer2(ctx, frame, flags);
1520     return ret;
1521 }
1522 
ffmpeg_GetFormat(AVCodecContext * p_context,const enum PixelFormat * pi_fmt)1523 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1524                                           const enum PixelFormat *pi_fmt )
1525 {
1526     decoder_t *p_dec = p_context->opaque;
1527     decoder_sys_t *p_sys = p_dec->p_sys;
1528     video_format_t fmt;
1529 
1530     /* Enumerate available formats */
1531     enum PixelFormat swfmt = avcodec_default_get_format(p_context, pi_fmt);
1532     bool can_hwaccel = false;
1533 
1534     for (size_t i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++)
1535     {
1536         const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1537         if (dsc == NULL)
1538             continue;
1539         bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1540 
1541         msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1542                  hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1543         if (hwaccel)
1544             can_hwaccel = true;
1545     }
1546 
1547     if (p_sys->pix_fmt == AV_PIX_FMT_NONE)
1548         goto no_reuse;
1549 
1550     /* If the format did not actually change (e.g. seeking), try to reuse the
1551      * existing output format, and if present, hardware acceleration back-end.
1552      * This avoids resetting the pipeline downstream. This also avoids
1553      * needlessly probing for hardware acceleration support. */
1554      if (lavc_GetVideoFormat(p_dec, &fmt, p_context, p_sys->pix_fmt, swfmt) != 0)
1555      {
1556          msg_Dbg(p_dec, "get format failed");
1557          goto no_reuse;
1558      }
1559      if (fmt.i_width  != p_dec->fmt_out.video.i_width ||
1560          fmt.i_height != p_dec->fmt_out.video.i_height)
1561      {
1562          msg_Dbg(p_dec, "mismatched dimensions %ux%u was %ux%u", fmt.i_width, fmt.i_height,
1563                  p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height);
1564          goto no_reuse;
1565      }
1566      if (p_context->profile != p_sys->profile || p_context->level > p_sys->level)
1567      {
1568          msg_Dbg(p_dec, "mismatched profile level %d/%d was %d/%d", p_context->profile,
1569                  p_context->level, p_sys->profile, p_sys->level);
1570          goto no_reuse;
1571      }
1572 
1573      for (size_t i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++)
1574         if (pi_fmt[i] == p_sys->pix_fmt)
1575         {
1576             if (lavc_UpdateVideoFormat(p_dec, p_context, p_sys->pix_fmt, swfmt) == 0)
1577             {
1578                 msg_Dbg(p_dec, "reusing decoder output format %d", pi_fmt[i]);
1579                 return p_sys->pix_fmt;
1580             }
1581         }
1582 
1583 no_reuse:
1584     if (p_sys->p_va != NULL)
1585     {
1586         msg_Err(p_dec, "existing hardware acceleration cannot be reused");
1587         vlc_va_Delete(p_sys->p_va, &p_context->hwaccel_context);
1588         p_sys->p_va = NULL;
1589     }
1590 
1591     p_sys->profile = p_context->profile;
1592     p_sys->level = p_context->level;
1593 
1594     if (!can_hwaccel)
1595         return swfmt;
1596 
1597 #if (LIBAVCODEC_VERSION_MICRO >= 100) \
1598   && (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 83, 101))
1599     if (p_context->active_thread_type)
1600     {
1601         msg_Warn(p_dec, "thread type %d: disabling hardware acceleration",
1602                  p_context->active_thread_type);
1603         return swfmt;
1604     }
1605 #endif
1606 
1607     wait_mt(p_sys);
1608 
1609     static const enum PixelFormat hwfmts[] =
1610     {
1611 #ifdef _WIN32
1612 #if LIBAVUTIL_VERSION_CHECK(54, 13, 1, 24, 100)
1613         AV_PIX_FMT_D3D11VA_VLD,
1614 #endif
1615         AV_PIX_FMT_DXVA2_VLD,
1616 #endif
1617         AV_PIX_FMT_VAAPI_VLD,
1618 #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 4, 0))
1619         AV_PIX_FMT_VDPAU,
1620 #endif
1621         AV_PIX_FMT_NONE,
1622     };
1623 
1624     for( size_t i = 0; hwfmts[i] != AV_PIX_FMT_NONE; i++ )
1625     {
1626         enum PixelFormat hwfmt = AV_PIX_FMT_NONE;
1627         for( size_t j = 0; hwfmt == AV_PIX_FMT_NONE && pi_fmt[j] != AV_PIX_FMT_NONE; j++ )
1628             if( hwfmts[i] == pi_fmt[j] )
1629                 hwfmt = hwfmts[i];
1630 
1631         if( hwfmt == AV_PIX_FMT_NONE )
1632             continue;
1633 
1634         p_dec->fmt_out.video.i_chroma = vlc_va_GetChroma(hwfmt, swfmt);
1635         if (p_dec->fmt_out.video.i_chroma == 0)
1636             continue; /* Unknown brand of hardware acceleration */
1637         if (p_context->width == 0 || p_context->height == 0)
1638         {   /* should never happen */
1639             msg_Err(p_dec, "unspecified video dimensions");
1640             continue;
1641         }
1642         const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(hwfmt);
1643         msg_Dbg(p_dec, "trying format %s", dsc ? dsc->name : "unknown");
1644         if (lavc_UpdateVideoFormat(p_dec, p_context, hwfmt, swfmt))
1645             continue; /* Unsupported brand of hardware acceleration */
1646         post_mt(p_sys);
1647 
1648         picture_t *test_pic = decoder_NewPicture(p_dec);
1649         assert(!test_pic || test_pic->format.i_chroma == p_dec->fmt_out.video.i_chroma);
1650         vlc_va_t *va = vlc_va_New(VLC_OBJECT(p_dec), p_context, hwfmt,
1651                                   &p_dec->fmt_in,
1652                                   test_pic ? test_pic->p_sys : NULL);
1653         if (test_pic)
1654             picture_Release(test_pic);
1655         if (va == NULL)
1656         {
1657             wait_mt(p_sys);
1658             continue; /* Unsupported codec profile or such */
1659         }
1660 
1661         if (va->description != NULL)
1662             msg_Info(p_dec, "Using %s for hardware decoding", va->description);
1663 
1664         p_sys->p_va = va;
1665         p_sys->pix_fmt = hwfmt;
1666         p_context->draw_horiz_band = NULL;
1667         return hwfmt;
1668     }
1669 
1670     post_mt(p_sys);
1671     /* Fallback to default behaviour */
1672     p_sys->pix_fmt = swfmt;
1673     return swfmt;
1674 }
1675