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>
38 #include <libavcodec/avcodec.h>
39 #include <libavutil/mem.h>
40 #include <libavutil/pixdesc.h>
45 /*****************************************************************************
46 * decoder_sys_t : decoder descriptor
47 *****************************************************************************/
50 AVCODEC_COMMON_MEMBERS
52 /* Video decoder specific part */
57 /* for frame skipping algo */
59 enum AVDiscard i_skip_frame;
60 enum AVDiscard i_skip_idct;
62 /* how many decoded frames are late */
64 mtime_t i_late_frames_start;
66 /* for direct rendering */
67 bool b_direct_rendering;
68 int i_direct_rendering_used;
72 /* Hack to force display of still pictures */
88 #ifdef HAVE_AVCODEC_MT
89 # define wait_mt(s) vlc_sem_wait( &s->sem_mt )
90 # define post_mt(s) vlc_sem_post( &s->sem_mt )
96 /*****************************************************************************
98 *****************************************************************************/
99 static void ffmpeg_InitCodec ( decoder_t * );
100 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
101 #if LIBAVCODEC_VERSION_MAJOR >= 55
102 static int lavc_GetFrame(struct AVCodecContext *, AVFrame *, int);
104 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
105 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
107 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
108 const enum PixelFormat * );
110 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
112 uint8_t *p = (uint8_t*)&fcc;
113 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
116 /*****************************************************************************
118 *****************************************************************************/
120 /* Returns a new picture buffer */
121 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
122 AVCodecContext *p_context )
124 decoder_sys_t *p_sys = p_dec->p_sys;
125 int width = p_context->coded_width;
126 int height = p_context->coded_height;
128 if( p_sys->p_va == NULL )
130 int aligns[AV_NUM_DATA_POINTERS];
132 avcodec_align_dimensions2(p_context, &width, &height, aligns);
136 if( width == 0 || height == 0 || width > 8192 || height > 8192 )
138 msg_Err( p_dec, "Invalid frame size %dx%d.", width, height );
139 return NULL; /* invalid display size */
141 p_dec->fmt_out.video.i_width = width;
142 p_dec->fmt_out.video.i_height = height;
144 if( width != p_context->width || height != p_context->height )
146 p_dec->fmt_out.video.i_visible_width = p_context->width;
147 p_dec->fmt_out.video.i_visible_height = p_context->height;
151 p_dec->fmt_out.video.i_visible_width = width;
152 p_dec->fmt_out.video.i_visible_height = height;
155 if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
157 /* we are doomed, but not really, because most codecs set their pix_fmt
159 * FIXME does it make sense here ? */
160 p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
162 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
164 /* If an aspect-ratio was specified in the input format then force it */
165 if( p_dec->fmt_in.video.i_sar_num > 0 && p_dec->fmt_in.video.i_sar_den > 0 )
167 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
168 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
172 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
173 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
175 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
177 p_dec->fmt_out.video.i_sar_num = 1;
178 p_dec->fmt_out.video.i_sar_den = 1;
182 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
183 p_dec->fmt_in.video.i_frame_rate_base > 0 )
185 p_dec->fmt_out.video.i_frame_rate =
186 p_dec->fmt_in.video.i_frame_rate;
187 p_dec->fmt_out.video.i_frame_rate_base =
188 p_dec->fmt_in.video.i_frame_rate_base;
190 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
192 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
193 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num * __MAX( p_context->ticks_per_frame, 1 );
196 return decoder_NewPicture( p_dec );
199 static int OpenVideoCodec( decoder_t *p_dec )
201 decoder_sys_t *p_sys = p_dec->p_sys;
203 if( p_sys->p_context->extradata_size <= 0 )
205 if( p_sys->i_codec_id == AV_CODEC_ID_VC1 ||
206 p_sys->i_codec_id == AV_CODEC_ID_THEORA )
208 msg_Warn( p_dec, "waiting for extra data for codec %s",
209 p_sys->psz_namecodec );
214 p_sys->p_context->width = p_dec->fmt_in.video.i_visible_width;
215 p_sys->p_context->height = p_dec->fmt_in.video.i_visible_height;
216 if (p_sys->p_context->width == 0)
217 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
218 else if (p_sys->p_context->width != p_dec->fmt_in.video.i_width)
219 p_sys->p_context->coded_width = p_dec->fmt_in.video.i_width;
220 if (p_sys->p_context->height == 0)
221 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
222 else if (p_sys->p_context->height != p_dec->fmt_in.video.i_height)
223 p_sys->p_context->coded_height = p_dec->fmt_in.video.i_height;
224 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
226 int ret = ffmpeg_OpenCodec( p_dec );
230 #ifdef HAVE_AVCODEC_MT
231 switch( p_sys->p_context->active_thread_type )
233 case FF_THREAD_FRAME:
234 msg_Dbg( p_dec, "using frame thread mode with %d threads",
235 p_sys->p_context->thread_count );
237 case FF_THREAD_SLICE:
238 msg_Dbg( p_dec, "using slice thread mode with %d threads",
239 p_sys->p_context->thread_count );
242 if( p_sys->p_context->thread_count > 1 )
243 msg_Warn( p_dec, "failed to enable threaded decoding" );
246 msg_Warn( p_dec, "using unknown thread mode with %d threads",
247 p_sys->p_context->thread_count );
254 /*****************************************************************************
255 * InitVideo: initialize the video decoder
256 *****************************************************************************
257 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
258 * opened (done after the first decoded frame).
259 *****************************************************************************/
260 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
261 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
263 decoder_sys_t *p_sys;
266 /* Allocate the memory needed to store the decoder's structure */
267 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
270 p_codec->type = AVMEDIA_TYPE_VIDEO;
271 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
272 p_context->codec_id = i_codec_id;
273 p_sys->p_context = p_context;
274 p_sys->p_codec = p_codec;
275 p_sys->i_codec_id = i_codec_id;
276 p_sys->psz_namecodec = psz_namecodec;
277 p_sys->p_ff_pic = avcodec_alloc_frame();
278 p_sys->b_delayed_open = true;
280 vlc_sem_init( &p_sys->sem_mt, 0 );
282 /* ***** Fill p_context with init values ***** */
283 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec );
285 /* ***** Get configuration of ffmpeg plugin ***** */
286 p_sys->p_context->workaround_bugs =
287 var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
288 p_sys->p_context->err_recognition =
289 var_InheritInteger( p_dec, "avcodec-error-resilience" );
291 if( var_CreateGetBool( p_dec, "grayscale" ) )
292 p_sys->p_context->flags |= CODEC_FLAG_GRAY;
294 /* ***** Output always the frames ***** */
295 #if LIBAVCODEC_VERSION_CHECK(55, 23, 1, 40, 101)
296 p_sys->p_context->flags |= CODEC_FLAG_OUTPUT_CORRUPT;
299 i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
300 if( i_val ) p_sys->p_context->debug_mv = i_val;
302 i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
303 if( i_val >= 4 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
304 else if( i_val == 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
305 else if( i_val == 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
306 else if( i_val == 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
308 if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
309 p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
311 /* ***** libavcodec frame skipping ***** */
312 p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
314 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-frame" );
315 if( i_val >= 4 ) p_sys->p_context->skip_frame = AVDISCARD_ALL;
316 else if( i_val == 3 ) p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
317 else if( i_val == 2 ) p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
318 else if( i_val == 1 ) p_sys->p_context->skip_frame = AVDISCARD_NONREF;
319 else if( i_val == -1 ) p_sys->p_context->skip_frame = AVDISCARD_NONE;
320 else p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
321 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
323 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-idct" );
324 if( i_val >= 4 ) p_sys->p_context->skip_idct = AVDISCARD_ALL;
325 else if( i_val == 3 ) p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
326 else if( i_val == 2 ) p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
327 else if( i_val == 1 ) p_sys->p_context->skip_idct = AVDISCARD_NONREF;
328 else if( i_val == -1 ) p_sys->p_context->skip_idct = AVDISCARD_NONE;
329 else p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
330 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
332 /* ***** libavcodec direct rendering ***** */
333 p_sys->b_direct_rendering = false;
334 p_sys->i_direct_rendering_used = -1;
335 if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
336 (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
337 /* No idea why ... but this fixes flickering on some TSCC streams */
338 p_sys->i_codec_id != AV_CODEC_ID_TSCC && p_sys->i_codec_id != AV_CODEC_ID_CSCD &&
339 p_sys->i_codec_id != AV_CODEC_ID_CINEPAK &&
340 !p_sys->p_context->debug_mv )
342 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
343 * so we need to do another check in ffmpeg_GetFrameBuf() */
344 p_sys->b_direct_rendering = true;
347 /* libavcodec doesn't properly release old pictures when frames are skipped */
348 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = false;
349 if( p_sys->b_direct_rendering )
351 msg_Dbg( p_dec, "trying to use direct rendering" );
352 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
356 msg_Dbg( p_dec, "direct rendering is disabled" );
359 p_sys->p_context->get_format = ffmpeg_GetFormat;
360 /* Always use our get_buffer wrapper so we can calculate the
362 #if LIBAVCODEC_VERSION_MAJOR >= 55
363 p_sys->p_context->get_buffer2 = lavc_GetFrame;
365 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
366 p_sys->p_context->reget_buffer = avcodec_default_reget_buffer;
367 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
369 p_sys->p_context->opaque = p_dec;
371 #ifdef HAVE_AVCODEC_MT
372 int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
373 if( i_thread_count <= 0 )
375 i_thread_count = vlc_GetCPUCount();
376 if( i_thread_count > 1 )
379 //FIXME: take in count the decoding time
380 i_thread_count = __MIN( i_thread_count, 4 );
382 i_thread_count = __MIN( i_thread_count, 16 );
383 msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
384 p_sys->p_context->thread_count = i_thread_count;
385 p_sys->p_context->thread_safe_callbacks = true;
389 case AV_CODEC_ID_MPEG4:
390 case AV_CODEC_ID_H263:
391 p_sys->p_context->thread_type = 0;
393 case AV_CODEC_ID_MPEG1VIDEO:
394 case AV_CODEC_ID_MPEG2VIDEO:
395 p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
397 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
398 case AV_CODEC_ID_H264:
399 case AV_CODEC_ID_VC1:
400 case AV_CODEC_ID_WMV3:
401 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
405 /* Workaround: frame multithreading is not compatible with
406 * DXVA2. When a frame is being copied to host memory, the frame
407 * is locked and cannot be used as a reference frame
408 * simultaneously and thus decoding fails for some frames. This
409 * causes major image corruption. */
411 char *avcodec_hw = var_InheritString( p_dec, "avcodec-hw" );
412 if( avcodec_hw == NULL || strcasecmp( avcodec_hw, "none" ) )
414 msg_Warn( p_dec, "threaded frame decoding is not compatible with DXVA2, disabled" );
415 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
420 if( p_sys->p_context->thread_type & FF_THREAD_FRAME )
421 p_dec->i_extra_picture_buffers = 2 * p_sys->p_context->thread_count;
424 /* ***** misc init ***** */
425 p_sys->i_pts = VLC_TS_INVALID;
426 p_sys->b_has_b_frames = false;
427 p_sys->b_first_frame = true;
428 p_sys->b_flush = false;
429 p_sys->i_late_frames = 0;
431 /* Set output properties */
432 p_dec->fmt_out.i_cat = VIDEO_ES;
433 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
435 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
436 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
438 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
440 p_dec->fmt_out.video.orientation = p_dec->fmt_in.video.orientation;
442 if( p_dec->fmt_in.video.p_palette ) {
443 p_sys->palette_sent = false;
444 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
445 if( p_dec->fmt_out.video.p_palette )
446 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
448 p_sys->palette_sent = true;
450 /* ***** init this codec with special data ***** */
451 ffmpeg_InitCodec( p_dec );
453 /* ***** Open the codec ***** */
454 if( OpenVideoCodec( p_dec ) < 0 )
456 avcodec_free_frame( &p_sys->p_ff_pic );
457 vlc_sem_destroy( &p_sys->sem_mt );
465 /*****************************************************************************
466 * DecodeVideo: Called to decode one or more frames
467 *****************************************************************************/
468 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
470 decoder_sys_t *p_sys = p_dec->p_sys;
471 AVCodecContext *p_context = p_sys->p_context;
478 if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
480 ffmpeg_InitCodec( p_dec );
481 if( p_sys->b_delayed_open )
482 OpenVideoCodec( p_dec );
486 if(!p_block && !(p_sys->p_codec->capabilities & CODEC_CAP_DELAY) )
489 if( p_sys->b_delayed_open )
492 block_Release( p_block );
498 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
500 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
502 p_sys->i_late_frames = 0;
505 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
506 avcodec_flush_buffers( p_context );
509 block_Release( p_block );
513 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
515 /* Do not care about late frames when prerolling
516 * TODO avoid decoding of non reference frame
517 * (ie all B except for H264 where it depends only on nal_ref_idc) */
518 p_sys->i_late_frames = 0;
522 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
523 (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
525 if( p_sys->i_pts > VLC_TS_INVALID )
527 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
530 block_Release( p_block );
531 p_sys->i_late_frames--;
532 msg_Err( p_dec, "more than 5 seconds of late video -> "
533 "dropping frame (computer too slow ?)" );
537 /* A good idea could be to decode all I pictures and see for the other */
538 if( !p_dec->b_pace_control &&
540 (p_sys->i_late_frames > 4) )
543 if( p_sys->i_late_frames < 12 )
545 p_context->skip_frame =
546 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
547 AVDISCARD_NONREF : p_sys->i_skip_frame;
551 /* picture too late, won't decode
552 * but break picture until a new I, and for mpeg4 ...*/
553 p_sys->i_late_frames--; /* needed else it will never be decrease */
555 block_Release( p_block );
556 msg_Warn( p_dec, "More than 4 late frames, dropping frame" );
562 if( p_sys->b_hurry_up )
563 p_context->skip_frame = p_sys->i_skip_frame;
564 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
570 if( p_context->width <= 0 || p_context->height <= 0 )
572 if( p_sys->b_hurry_up )
573 p_context->skip_frame = p_sys->i_skip_frame;
575 else if( !b_drawpicture )
577 /* It creates broken picture
578 * FIXME either our parser or ffmpeg is broken */
580 if( p_sys->b_hurry_up )
581 p_context->skip_frame = __MAX( p_context->skip_frame,
587 * Do the actual decoding now */
589 /* Don't forget that libavcodec requires a little more bytes
590 * that the real frame size */
591 if( p_block && p_block->i_buffer > 0 )
593 p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
595 p_block = block_Realloc( p_block, 0,
596 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
599 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
601 memset( p_block->p_buffer + p_block->i_buffer, 0,
602 FF_INPUT_BUFFER_PADDING_SIZE );
605 while( !p_block || p_block->i_buffer > 0 || p_sys->b_flush )
607 int i_used, b_gotpicture;
613 av_init_packet( &pkt );
616 pkt.data = p_block->p_buffer;
617 pkt.size = p_block->i_buffer;
618 pkt.pts = p_block->i_pts;
619 pkt.dts = p_block->i_dts;
623 /* Return delayed frames if codec has CODEC_CAP_DELAY */
628 if( !p_sys->palette_sent )
630 uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
632 memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE);
633 p_sys->palette_sent = true;
637 /* Make sure we don't reuse the same timestamps twice */
641 p_block->i_dts = VLC_TS_INVALID;
644 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
645 &b_gotpicture, &pkt );
646 av_free_packet( &pkt );
651 p_sys->b_first_frame = true;
655 if( p_block->i_buffer <= 0 )
656 p_sys->b_flush = false;
661 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
663 block_Release( p_block );
666 else if( (unsigned)i_used > p_block->i_buffer ||
667 p_context->thread_count > 1 )
669 i_used = p_block->i_buffer;
673 p_block->i_buffer -= i_used;
674 p_block->p_buffer += i_used;
677 /* Nothing to display */
680 if( i_used == 0 ) break;
684 /* Sanity check (seems to be needed for some streams) */
685 if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
687 p_sys->b_has_b_frames = true;
690 /* Compute the PTS */
692 p_sys->p_ff_pic->pkt_pts;
693 if (i_pts <= VLC_TS_INVALID)
694 i_pts = p_sys->p_ff_pic->pkt_dts;
696 if( i_pts <= VLC_TS_INVALID )
697 i_pts = p_sys->i_pts;
699 /* Interpolate the next PTS */
700 if( i_pts > VLC_TS_INVALID )
701 p_sys->i_pts = i_pts;
702 if( p_sys->i_pts > VLC_TS_INVALID )
704 /* interpolate the next PTS */
705 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
706 p_dec->fmt_in.video.i_frame_rate_base > 0 )
708 p_sys->i_pts += CLOCK_FREQ *
709 (2 + p_sys->p_ff_pic->repeat_pict) *
710 p_dec->fmt_in.video.i_frame_rate_base /
711 (2 * p_dec->fmt_in.video.i_frame_rate);
713 else if( p_context->time_base.den > 0 )
715 int i_tick = p_context->ticks_per_frame;
719 p_sys->i_pts += CLOCK_FREQ *
720 (2 + p_sys->p_ff_pic->repeat_pict) *
721 i_tick * p_context->time_base.num /
722 (2 * p_context->time_base.den);
726 /* Update frame late count (except when doing preroll) */
727 mtime_t i_display_date = 0;
728 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
729 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
731 if( i_display_date > 0 && i_display_date <= mdate() )
733 p_sys->i_late_frames++;
734 if( p_sys->i_late_frames == 1 )
735 p_sys->i_late_frames_start = mdate();
739 p_sys->i_late_frames = 0;
742 if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
745 if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
747 /* Get a new picture */
748 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
752 block_Release( p_block );
756 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
758 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
762 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
763 decoder_LinkPicture( p_dec, p_pic );
766 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
768 /* Fetch again the aspect ratio in case it changed */
769 p_dec->fmt_out.video.i_sar_num
770 = p_context->sample_aspect_ratio.num;
771 p_dec->fmt_out.video.i_sar_den
772 = p_context->sample_aspect_ratio.den;
774 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
776 p_dec->fmt_out.video.i_sar_num = 1;
777 p_dec->fmt_out.video.i_sar_den = 1;
781 /* Send decoded frame to vout */
782 if( i_pts > VLC_TS_INVALID)
786 if( p_sys->b_first_frame )
788 /* Hack to force display of still pictures */
789 p_sys->b_first_frame = false;
790 p_pic->b_force = true;
793 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
794 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
795 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
801 decoder_DeletePicture( p_dec, p_pic );
806 block_Release( p_block );
810 /*****************************************************************************
811 * EndVideo: decoder destruction
812 *****************************************************************************
813 * This function is called when the thread ends after a successful
815 *****************************************************************************/
816 void EndVideoDec( decoder_t *p_dec )
818 decoder_sys_t *p_sys = p_dec->p_sys;
822 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
823 if( p_sys->p_context->codec )
824 avcodec_flush_buffers( p_sys->p_context );
828 if( p_sys->p_ff_pic )
829 avcodec_free_frame( &p_sys->p_ff_pic );
832 vlc_va_Delete( p_sys->p_va );
834 vlc_sem_destroy( &p_sys->sem_mt );
837 /*****************************************************************************
838 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
839 *****************************************************************************/
840 static void ffmpeg_InitCodec( decoder_t *p_dec )
842 decoder_sys_t *p_sys = p_dec->p_sys;
843 int i_size = p_dec->fmt_in.i_extra;
845 if( !i_size ) return;
847 if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
851 p_sys->p_context->extradata_size = i_size + 12;
852 p = p_sys->p_context->extradata =
853 av_malloc( p_sys->p_context->extradata_size +
854 FF_INPUT_BUFFER_PADDING_SIZE );
858 memcpy( &p[0], "SVQ3", 4 );
859 memset( &p[4], 0, 8 );
860 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
862 /* Now remove all atoms before the SMI one */
863 if( p_sys->p_context->extradata_size > 0x5a &&
864 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
866 uint8_t *psz = &p[0x52];
868 while( psz < &p[p_sys->p_context->extradata_size - 8] )
870 int i_size = GetDWBE( psz );
873 /* FIXME handle 1 as long size */
876 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
878 memmove( &p[0x52], psz,
879 &p[p_sys->p_context->extradata_size] - psz );
889 p_sys->p_context->extradata_size = i_size;
890 p_sys->p_context->extradata =
891 av_malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
892 if( p_sys->p_context->extradata )
894 memcpy( p_sys->p_context->extradata,
895 p_dec->fmt_in.p_extra, i_size );
896 memset( p_sys->p_context->extradata + i_size,
897 0, FF_INPUT_BUFFER_PADDING_SIZE );
902 /*****************************************************************************
903 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
904 * picture_t structure (when not in direct rendering mode).
905 *****************************************************************************/
906 static void ffmpeg_CopyPicture( decoder_t *p_dec,
907 picture_t *p_pic, AVFrame *p_ff_pic )
909 decoder_sys_t *p_sys = p_dec->p_sys;
913 vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic->opaque,
916 else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
918 int i_plane, i_size, i_line;
919 uint8_t *p_dst, *p_src;
920 int i_src_stride, i_dst_stride;
922 if( p_sys->p_context->pix_fmt == PIX_FMT_PAL8 )
924 if( !p_pic->format.p_palette )
925 p_pic->format.p_palette = calloc( 1, sizeof(video_palette_t) );
927 if( p_pic->format.p_palette )
929 p_pic->format.p_palette->i_entries = AVPALETTE_COUNT;
930 memcpy( p_pic->format.p_palette->palette, p_ff_pic->data[1], AVPALETTE_SIZE );
934 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
936 p_src = p_ff_pic->data[i_plane];
937 p_dst = p_pic->p[i_plane].p_pixels;
938 i_src_stride = p_ff_pic->linesize[i_plane];
939 i_dst_stride = p_pic->p[i_plane].i_pitch;
941 i_size = __MIN( i_src_stride, i_dst_stride );
942 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
945 memcpy( p_dst, p_src, i_size );
946 p_src += i_src_stride;
947 p_dst += i_dst_stride;
953 const char *name = av_get_pix_fmt_name( p_sys->p_context->pix_fmt );
954 msg_Err( p_dec, "Unsupported decoded output format %d (%s)",
955 p_sys->p_context->pix_fmt, name ? name : "unknown" );
960 #if LIBAVCODEC_VERSION_MAJOR >= 55
961 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
964 decoder_t *dec = ctx->opaque;
965 decoder_sys_t *sys = dec->p_sys;
966 vlc_va_t *va = sys->p_va;
968 if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
969 ctx->coded_width, ctx->coded_height))
971 msg_Err(dec, "hardware acceleration setup failed");
974 if (vlc_va_Get(va, &frame->opaque, &frame->data[0]))
976 msg_Err(dec, "hardware acceleration picture allocation failed");
979 /* data[0] must be non-NULL for libavcodec internal checks.
980 * data[3] actually contains the format-specific surface handle. */
981 frame->data[3] = frame->data[0];
983 frame->buf[0] = av_buffer_create(frame->data[0], 0, va->release,
985 if (unlikely(frame->buf[0] == NULL))
987 vlc_va_Release(va, frame->opaque, frame->data[0]);
990 assert(frame->data[0] != NULL);
1001 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
1003 lavc_pic_ref_t *ref = opaque;
1005 decoder_UnlinkPicture(ref->decoder, ref->picture);
1010 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
1011 AVFrame *frame, int flags)
1013 decoder_t *dec = (decoder_t *)ctx->opaque;
1014 decoder_sys_t *sys = dec->p_sys;
1016 if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
1018 dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
1019 if (ctx->pix_fmt == PIX_FMT_PAL8)
1022 int width = frame->width;
1023 int height = frame->height;
1024 int aligns[AV_NUM_DATA_POINTERS];
1026 avcodec_align_dimensions2(ctx, &width, &height, aligns);
1028 picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
1032 /* Check that the picture is suitable for libavcodec */
1033 if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
1035 if (sys->i_direct_rendering_used != 0)
1036 msg_Dbg(dec, "plane 0: pitch too small (%d/%d*%d)",
1037 pic->p[0].i_pitch, width, pic->p[0].i_pixel_pitch);
1041 if (pic->p[0].i_lines < height)
1043 if (sys->i_direct_rendering_used != 0)
1044 msg_Dbg(dec, "plane 0: lines too few (%d/%d)",
1045 pic->p[0].i_lines, height);
1049 for (int i = 0; i < pic->i_planes; i++)
1051 if (pic->p[i].i_pitch % aligns[i])
1053 if (sys->i_direct_rendering_used != 0)
1054 msg_Dbg(dec, "plane %d: pitch not aligned (%d%%%d)",
1055 i, pic->p[i].i_pitch, aligns[i]);
1058 if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1060 if (sys->i_direct_rendering_used != 0)
1061 msg_Warn(dec, "plane %d not aligned", i);
1066 /* Allocate buffer references */
1067 for (int i = 0; i < pic->i_planes; i++)
1069 lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1074 decoder_LinkPicture(dec, pic);
1076 uint8_t *data = pic->p[i].p_pixels;
1077 int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1079 frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1081 if (unlikely(frame->buf[i] == NULL))
1083 lavc_dr_ReleaseFrame(ref, data);
1087 decoder_UnlinkPicture(dec, pic);
1091 for (unsigned i = 0; frame->buf[i] != NULL; i++)
1092 av_buffer_unref(&frame->buf[i]);
1094 decoder_DeletePicture(dec, pic);
1099 * Callback used by libavcodec to get a frame buffer.
1101 * It is used for direct rendering as well as to get the right PTS for each
1102 * decoded picture (even in indirect rendering mode).
1104 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1106 decoder_t *dec = ctx->opaque;
1107 decoder_sys_t *sys = dec->p_sys;
1110 for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1112 frame->data[i] = NULL;
1113 frame->linesize[i] = 0;
1114 frame->buf[i] = NULL;
1117 if (sys->p_va != NULL)
1118 return lavc_va_GetFrame(ctx, frame, flags);
1120 frame->opaque = NULL;
1121 if (!sys->b_direct_rendering)
1122 return avcodec_default_get_buffer2(ctx, frame, flags);
1124 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1125 * so we need to check for direct rendering again. */
1127 pic = lavc_dr_GetFrame(ctx, frame, flags);
1130 if (sys->i_direct_rendering_used != 0)
1132 msg_Warn(dec, "disabling direct rendering");
1133 sys->i_direct_rendering_used = 0;
1136 return avcodec_default_get_buffer2(ctx, frame, flags);
1139 if (sys->i_direct_rendering_used != 1)
1141 msg_Dbg(dec, "enabling direct rendering");
1142 sys->i_direct_rendering_used = 1;
1146 frame->opaque = pic;
1147 static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1148 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++)
1150 frame->data[i] = pic->p[i].p_pixels;
1151 frame->linesize[i] = pic->p[i].i_pitch;
1156 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1158 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1159 decoder_sys_t *p_sys = p_dec->p_sys;
1160 vlc_va_t *p_va = p_sys->p_va;
1162 /* hwaccel_context is not present in old ffmpeg version */
1163 if( vlc_va_Setup( p_va,
1164 &p_context->hwaccel_context, &p_dec->fmt_out.video.i_chroma,
1165 p_context->coded_width, p_context->coded_height ) )
1167 msg_Err( p_dec, "vlc_va_Setup failed" );
1171 if( vlc_va_Get( p_va, &p_ff_pic->opaque, &p_ff_pic->data[0] ) )
1173 msg_Err( p_dec, "vlc_va_Get failed" );
1177 p_ff_pic->data[3] = p_ff_pic->data[0];
1178 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1182 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1184 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1185 decoder_sys_t *p_sys = p_dec->p_sys;
1187 int i_width = p_context->width;
1188 int i_height = p_context->height;
1189 avcodec_align_dimensions( p_context, &i_width, &i_height );
1191 picture_t *p_pic = NULL;
1192 if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1195 if (p_context->pix_fmt == PIX_FMT_PAL8)
1198 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1200 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1204 if( p_pic->p[0].i_pitch / p_pic->p[0].i_pixel_pitch < i_width ||
1205 p_pic->p[0].i_lines < i_height )
1208 for( int i = 0; i < p_pic->i_planes; i++ )
1211 switch( p_sys->i_codec_id )
1213 case AV_CODEC_ID_SVQ1:
1214 case AV_CODEC_ID_VP5:
1215 case AV_CODEC_ID_VP6:
1216 case AV_CODEC_ID_VP6F:
1217 case AV_CODEC_ID_VP6A:
1221 i_align = i == 0 ? 16 : 8;
1224 if( p_pic->p[i].i_pitch % i_align )
1226 if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1230 if( p_context->pix_fmt == PIX_FMT_YUV422P )
1232 if( 2 * p_pic->p[1].i_pitch != p_pic->p[0].i_pitch ||
1233 2 * p_pic->p[2].i_pitch != p_pic->p[0].i_pitch )
1241 decoder_DeletePicture( p_dec, p_pic );
1246 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
1249 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1250 decoder_sys_t *p_sys = p_dec->p_sys;
1253 p_ff_pic->opaque = NULL;
1254 #if ! LIBAVCODEC_VERSION_CHECK(54, 34, 0, 79, 101)
1255 p_ff_pic->pkt_pts = p_context->pkt ? p_context->pkt->pts : AV_NOPTS_VALUE;
1259 return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1261 if( !p_sys->b_direct_rendering )
1262 return avcodec_default_get_buffer( p_context, p_ff_pic );
1265 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1266 * so we need to check for direct rendering again. */
1268 picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1270 if( p_sys->i_direct_rendering_used != 0 )
1272 msg_Warn( p_dec, "disabling direct rendering" );
1273 p_sys->i_direct_rendering_used = 0;
1277 return avcodec_default_get_buffer( p_context, p_ff_pic );
1280 if( p_sys->i_direct_rendering_used != 1 ) {
1281 msg_Dbg( p_dec, "using direct rendering" );
1282 p_sys->i_direct_rendering_used = 1;
1285 p_context->draw_horiz_band = NULL;
1288 p_ff_pic->opaque = (void*)p_pic;
1289 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1290 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
1291 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
1292 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
1293 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
1295 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
1296 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
1297 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
1298 p_ff_pic->linesize[3] = 0;
1303 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1306 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1307 decoder_sys_t *p_sys = p_dec->p_sys;
1310 vlc_va_Release( p_sys->p_va, p_ff_pic->opaque, p_ff_pic->data[0] );
1311 else if( p_ff_pic->opaque )
1312 decoder_UnlinkPicture( p_dec, (picture_t*)p_ff_pic->opaque);
1313 else if( p_ff_pic->type == FF_BUFFER_TYPE_INTERNAL )
1314 /* We can end up here without the AVFrame being allocated by
1315 * avcodec_default_get_buffer() if VA is used and the frame is
1316 * released when the decoder is closed
1318 avcodec_default_release_buffer( p_context, p_ff_pic );
1320 for( int i = 0; i < 4; i++ )
1321 p_ff_pic->data[i] = NULL;
1325 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1326 const enum PixelFormat *pi_fmt )
1328 decoder_t *p_dec = p_context->opaque;
1329 decoder_sys_t *p_sys = p_dec->p_sys;
1330 vlc_va_t *p_va = p_sys->p_va;
1333 vlc_va_Delete( p_va );
1335 /* Enumerate available formats */
1336 bool can_hwaccel = false;
1337 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1339 const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1342 bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1344 msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1345 hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1353 /* Profile and level information is needed now.
1354 * TODO: avoid code duplication with avcodec.c */
1355 if( p_context->profile != FF_PROFILE_UNKNOWN)
1356 p_dec->fmt_in.i_profile = p_context->profile;
1357 if( p_context->level != FF_LEVEL_UNKNOWN)
1358 p_dec->fmt_in.i_level = p_context->level;
1360 p_va = vlc_va_New( VLC_OBJECT(p_dec), p_context, &p_dec->fmt_in );
1364 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1366 if( p_va->pix_fmt != pi_fmt[i] )
1369 /* We try to call vlc_va_Setup when possible to detect errors when
1370 * possible (later is too late) */
1371 if( p_context->width > 0 && p_context->height > 0
1372 && vlc_va_Setup( p_va, &p_context->hwaccel_context,
1373 &p_dec->fmt_out.video.i_chroma,
1374 p_context->width, p_context->height ) )
1376 msg_Err( p_dec, "acceleration setup failure" );
1380 if( p_va->description )
1381 msg_Info( p_dec, "Using %s for hardware decoding.",
1382 p_va->description );
1384 /* FIXME this will disable direct rendering
1385 * even if a new pixel format is renegotiated
1387 p_sys->b_direct_rendering = false;
1389 p_context->draw_horiz_band = NULL;
1393 vlc_va_Delete( p_va );
1396 /* Fallback to default behaviour */
1398 return avcodec_default_get_format( p_context, pi_fmt );