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 */
77 #if LIBAVCODEC_VERSION_MAJOR < 54
78 AVPaletteControl palette;
92 #ifdef HAVE_AVCODEC_MT
93 # define wait_mt(s) vlc_sem_wait( &s->sem_mt )
94 # define post_mt(s) vlc_sem_post( &s->sem_mt )
100 /*****************************************************************************
102 *****************************************************************************/
103 static void ffmpeg_InitCodec ( decoder_t * );
104 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
105 #if LIBAVCODEC_VERSION_MAJOR >= 55
106 static int lavc_GetFrame(struct AVCodecContext *, AVFrame *, int);
108 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
109 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
111 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
112 const enum PixelFormat * );
114 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
116 uint8_t *p = (uint8_t*)&fcc;
117 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
120 /*****************************************************************************
122 *****************************************************************************/
124 /* Returns a new picture buffer */
125 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
126 AVCodecContext *p_context )
128 decoder_sys_t *p_sys = p_dec->p_sys;
130 if( p_context->coded_width != p_context->width ||
131 p_context->coded_height != p_context->height )
133 p_dec->fmt_out.video.i_visible_width = p_context->width;
134 p_dec->fmt_out.video.i_visible_height = p_context->height;
136 p_dec->fmt_out.video.i_width = p_context->coded_width;
137 p_dec->fmt_out.video.i_height = p_context->coded_height;
139 if( !p_context->width || !p_context->height )
141 return NULL; /* invalid display size */
144 if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
146 /* we are doomed, but not really, because most codecs set their pix_fmt
148 * FIXME does it make sense here ? */
149 p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
151 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
153 /* If an aspect-ratio was specified in the input format then force it */
154 if( p_dec->fmt_in.video.i_sar_num > 0 && p_dec->fmt_in.video.i_sar_den > 0 )
156 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
157 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
161 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
162 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
164 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
166 p_dec->fmt_out.video.i_sar_num = 1;
167 p_dec->fmt_out.video.i_sar_den = 1;
171 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
172 p_dec->fmt_in.video.i_frame_rate_base > 0 )
174 p_dec->fmt_out.video.i_frame_rate =
175 p_dec->fmt_in.video.i_frame_rate;
176 p_dec->fmt_out.video.i_frame_rate_base =
177 p_dec->fmt_in.video.i_frame_rate_base;
179 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
181 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
182 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num * __MAX( p_context->ticks_per_frame, 1 );
185 return decoder_NewPicture( p_dec );
188 /*****************************************************************************
189 * InitVideo: initialize the video decoder
190 *****************************************************************************
191 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
192 * opened (done after the first decoded frame).
193 *****************************************************************************/
194 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
195 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
197 decoder_sys_t *p_sys;
200 /* Allocate the memory needed to store the decoder's structure */
201 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
204 p_codec->type = AVMEDIA_TYPE_VIDEO;
205 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
206 p_context->codec_id = i_codec_id;
207 p_sys->p_context = p_context;
208 p_sys->p_codec = p_codec;
209 p_sys->i_codec_id = i_codec_id;
210 p_sys->psz_namecodec = psz_namecodec;
211 p_sys->p_ff_pic = avcodec_alloc_frame();
212 p_sys->b_delayed_open = true;
214 vlc_sem_init( &p_sys->sem_mt, 0 );
216 /* ***** Fill p_context with init values ***** */
217 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec );
219 /* ***** Get configuration of ffmpeg plugin ***** */
220 p_sys->p_context->workaround_bugs =
221 var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
222 p_sys->p_context->err_recognition =
223 var_InheritInteger( p_dec, "avcodec-error-resilience" );
225 if( var_CreateGetBool( p_dec, "grayscale" ) )
226 p_sys->p_context->flags |= CODEC_FLAG_GRAY;
228 i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
229 if( i_val ) p_sys->p_context->debug_mv = i_val;
231 i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
232 if( i_val >= 4 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
233 else if( i_val == 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
234 else if( i_val == 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
235 else if( i_val == 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
237 if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
238 p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
240 #if LIBAVCODEC_VERSION_CHECK( 54, 41, 0, 91, 100 )
241 if( var_InheritBool( p_dec, "avcodec-ignorecrop" ) )
242 p_sys->p_context->flags2 |= CODEC_FLAG2_IGNORE_CROP;
245 /* ***** libavcodec frame skipping ***** */
246 p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
248 switch( var_CreateGetInteger( p_dec, "avcodec-skip-frame" ) )
251 p_sys->p_context->skip_frame = AVDISCARD_NONE;
254 p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
257 p_sys->p_context->skip_frame = AVDISCARD_NONREF;
260 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
263 p_sys->p_context->skip_frame = AVDISCARD_ALL;
266 p_sys->p_context->skip_frame = AVDISCARD_NONE;
269 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
271 switch( var_CreateGetInteger( p_dec, "avcodec-skip-idct" ) )
274 p_sys->p_context->skip_idct = AVDISCARD_NONE;
277 p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
280 p_sys->p_context->skip_idct = AVDISCARD_NONREF;
283 p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
286 p_sys->p_context->skip_idct = AVDISCARD_ALL;
289 p_sys->p_context->skip_idct = AVDISCARD_NONE;
292 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
294 /* ***** libavcodec direct rendering ***** */
295 p_sys->b_direct_rendering = false;
296 p_sys->i_direct_rendering_used = -1;
297 if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
298 (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
299 /* No idea why ... but this fixes flickering on some TSCC streams */
300 p_sys->i_codec_id != AV_CODEC_ID_TSCC && p_sys->i_codec_id != AV_CODEC_ID_CSCD &&
301 p_sys->i_codec_id != AV_CODEC_ID_CINEPAK &&
302 !p_sys->p_context->debug_mv )
304 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
305 * so we need to do another check in ffmpeg_GetFrameBuf() */
306 p_sys->b_direct_rendering = true;
309 /* libavcodec doesn't properly release old pictures when frames are skipped */
310 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = false;
311 if( p_sys->b_direct_rendering )
313 msg_Dbg( p_dec, "trying to use direct rendering" );
314 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
318 msg_Dbg( p_dec, "direct rendering is disabled" );
321 p_sys->p_context->get_format = ffmpeg_GetFormat;
322 /* Always use our get_buffer wrapper so we can calculate the
324 #if LIBAVCODEC_VERSION_MAJOR >= 55
325 p_sys->p_context->get_buffer2 = lavc_GetFrame;
327 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
328 p_sys->p_context->reget_buffer = avcodec_default_reget_buffer;
329 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
331 p_sys->p_context->opaque = p_dec;
333 #ifdef HAVE_AVCODEC_MT
334 int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
335 if( i_thread_count <= 0 )
337 i_thread_count = vlc_GetCPUCount();
338 if( i_thread_count > 1 )
341 //FIXME: take in count the decoding time
342 i_thread_count = __MIN( i_thread_count, 4 );
344 i_thread_count = __MIN( i_thread_count, 16 );
345 msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
346 p_sys->p_context->thread_count = i_thread_count;
347 p_sys->p_context->thread_safe_callbacks = true;
351 case AV_CODEC_ID_MPEG4:
352 case AV_CODEC_ID_H263:
353 p_sys->p_context->thread_type = 0;
355 case AV_CODEC_ID_MPEG1VIDEO:
356 case AV_CODEC_ID_MPEG2VIDEO:
357 p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
359 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
360 case AV_CODEC_ID_H264:
361 case AV_CODEC_ID_VC1:
362 case AV_CODEC_ID_WMV3:
363 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
367 if( p_sys->p_context->thread_type & FF_THREAD_FRAME )
368 p_dec->i_extra_picture_buffers = 2 * p_sys->p_context->thread_count;
371 /* ***** misc init ***** */
372 p_sys->i_pts = VLC_TS_INVALID;
373 p_sys->b_has_b_frames = false;
374 p_sys->b_first_frame = true;
375 p_sys->b_flush = false;
376 p_sys->i_late_frames = 0;
378 /* Set output properties */
379 p_dec->fmt_out.i_cat = VIDEO_ES;
380 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
382 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
383 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
385 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
387 #if LIBAVCODEC_VERSION_MAJOR < 54
389 memset( &p_sys->palette, 0, sizeof(p_sys->palette) );
390 if( p_dec->fmt_in.video.p_palette )
392 p_sys->palette.palette_changed = 1;
394 for( int i = 0; i < __MIN( AVPALETTE_COUNT, p_dec->fmt_in.video.p_palette->i_entries ); i++ )
400 c.a[0] = p_dec->fmt_in.video.p_palette->palette[i][0];
401 c.a[1] = p_dec->fmt_in.video.p_palette->palette[i][1];
402 c.a[2] = p_dec->fmt_in.video.p_palette->palette[i][2];
403 c.a[3] = p_dec->fmt_in.video.p_palette->palette[i][3];
405 p_sys->palette.palette[i] = c.u;
407 p_sys->p_context->palctrl = &p_sys->palette;
409 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
410 if( p_dec->fmt_out.video.p_palette )
411 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
413 else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
415 p_sys->p_context->palctrl = &p_sys->palette;
421 /* ***** init this codec with special data ***** */
422 ffmpeg_InitCodec( p_dec );
424 /* ***** Open the codec ***** */
425 if( ffmpeg_OpenCodec( p_dec ) < 0 )
427 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
428 av_free( p_sys->p_ff_pic );
429 vlc_sem_destroy( &p_sys->sem_mt );
437 /*****************************************************************************
438 * DecodeVideo: Called to decode one or more frames
439 *****************************************************************************/
440 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
442 decoder_sys_t *p_sys = p_dec->p_sys;
443 AVCodecContext *p_context = p_sys->p_context;
445 int b_null_size = false;
448 if( !pp_block || !*pp_block )
451 if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
453 ffmpeg_InitCodec( p_dec );
454 if( p_sys->b_delayed_open )
456 if( ffmpeg_OpenCodec( p_dec ) )
457 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
462 if( p_sys->b_delayed_open )
464 block_Release( p_block );
468 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
470 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
472 p_sys->i_late_frames = 0;
475 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
476 avcodec_flush_buffers( p_context );
479 block_Release( p_block );
483 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
485 /* Do not care about late frames when prerolling
486 * TODO avoid decoding of non reference frame
487 * (ie all B except for H264 where it depends only on nal_ref_idc) */
488 p_sys->i_late_frames = 0;
491 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
492 (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
494 if( p_sys->i_pts > VLC_TS_INVALID )
496 msg_Err( p_dec, "more than 5 seconds of late video -> "
497 "dropping frame (computer too slow ?)" );
498 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
500 block_Release( p_block );
501 p_sys->i_late_frames--;
505 /* A good idea could be to decode all I pictures and see for the other */
506 if( !p_dec->b_pace_control &&
508 (p_sys->i_late_frames > 4) )
511 if( p_sys->i_late_frames < 12 )
513 p_context->skip_frame =
514 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
515 AVDISCARD_NONREF : p_sys->i_skip_frame;
519 /* picture too late, won't decode
520 * but break picture until a new I, and for mpeg4 ...*/
521 p_sys->i_late_frames--; /* needed else it will never be decrease */
522 block_Release( p_block );
528 if( p_sys->b_hurry_up )
529 p_context->skip_frame = p_sys->i_skip_frame;
530 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
536 if( p_context->width <= 0 || p_context->height <= 0 )
538 if( p_sys->b_hurry_up )
539 p_context->skip_frame = p_sys->i_skip_frame;
542 else if( !b_drawpicture )
544 /* It creates broken picture
545 * FIXME either our parser or ffmpeg is broken */
547 if( p_sys->b_hurry_up )
548 p_context->skip_frame = __MAX( p_context->skip_frame,
554 * Do the actual decoding now */
556 /* Don't forget that libavcodec requires a little more bytes
557 * that the real frame size */
558 if( p_block->i_buffer > 0 )
560 p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
562 p_block = block_Realloc( p_block, 0,
563 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
566 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
568 memset( p_block->p_buffer + p_block->i_buffer, 0,
569 FF_INPUT_BUFFER_PADDING_SIZE );
572 while( p_block->i_buffer > 0 || p_sys->b_flush )
574 int i_used, b_gotpicture;
580 av_init_packet( &pkt );
581 pkt.data = p_block->p_buffer;
582 pkt.size = p_block->i_buffer;
583 pkt.pts = p_block->i_pts;
584 pkt.dts = p_block->i_dts;
586 /* Make sure we don't reuse the same timestamps twice */
588 p_block->i_dts = VLC_TS_INVALID;
590 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
591 &b_gotpicture, &pkt );
593 if( b_null_size && !p_sys->b_flush &&
594 p_context->width > 0 && p_context->height > 0 )
596 /* Reparse it to not drop the I frame */
598 if( p_sys->b_hurry_up )
599 p_context->skip_frame = p_sys->i_skip_frame;
600 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
601 &b_gotpicture, &pkt );
606 p_sys->b_first_frame = true;
608 if( p_block->i_buffer <= 0 )
609 p_sys->b_flush = false;
614 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
616 block_Release( p_block );
619 else if( (unsigned)i_used > p_block->i_buffer ||
620 p_context->thread_count > 1 )
622 i_used = p_block->i_buffer;
626 p_block->i_buffer -= i_used;
627 p_block->p_buffer += i_used;
629 /* Nothing to display */
632 if( i_used == 0 ) break;
636 /* Sanity check (seems to be needed for some streams) */
637 if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
639 p_sys->b_has_b_frames = true;
642 /* Compute the PTS */
644 p_sys->p_ff_pic->pkt_pts;
645 if (i_pts <= VLC_TS_INVALID)
646 i_pts = p_sys->p_ff_pic->pkt_dts;
648 if( i_pts <= VLC_TS_INVALID )
649 i_pts = p_sys->i_pts;
651 /* Interpolate the next PTS */
652 if( i_pts > VLC_TS_INVALID )
653 p_sys->i_pts = i_pts;
654 if( p_sys->i_pts > VLC_TS_INVALID )
656 /* interpolate the next PTS */
657 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
658 p_dec->fmt_in.video.i_frame_rate_base > 0 )
660 p_sys->i_pts += INT64_C(1000000) *
661 (2 + p_sys->p_ff_pic->repeat_pict) *
662 p_dec->fmt_in.video.i_frame_rate_base /
663 (2 * p_dec->fmt_in.video.i_frame_rate);
665 else if( p_context->time_base.den > 0 )
667 int i_tick = p_context->ticks_per_frame;
671 p_sys->i_pts += INT64_C(1000000) *
672 (2 + p_sys->p_ff_pic->repeat_pict) *
673 i_tick * p_context->time_base.num /
674 (2 * p_context->time_base.den);
678 /* Update frame late count (except when doing preroll) */
679 mtime_t i_display_date = 0;
680 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
681 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
683 if( i_display_date > 0 && i_display_date <= mdate() )
685 p_sys->i_late_frames++;
686 if( p_sys->i_late_frames == 1 )
687 p_sys->i_late_frames_start = mdate();
691 p_sys->i_late_frames = 0;
694 if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
697 if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
699 /* Get a new picture */
700 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
703 block_Release( p_block );
707 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
709 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
713 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
714 decoder_LinkPicture( p_dec, p_pic );
717 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
719 /* Fetch again the aspect ratio in case it changed */
720 p_dec->fmt_out.video.i_sar_num
721 = p_context->sample_aspect_ratio.num;
722 p_dec->fmt_out.video.i_sar_den
723 = p_context->sample_aspect_ratio.den;
725 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
727 p_dec->fmt_out.video.i_sar_num = 1;
728 p_dec->fmt_out.video.i_sar_den = 1;
732 /* Send decoded frame to vout */
733 if( i_pts > VLC_TS_INVALID)
737 if( p_sys->b_first_frame )
739 /* Hack to force display of still pictures */
740 p_sys->b_first_frame = false;
741 p_pic->b_force = true;
744 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
745 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
746 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
752 decoder_DeletePicture( p_dec, p_pic );
756 block_Release( p_block );
760 /*****************************************************************************
761 * EndVideo: decoder destruction
762 *****************************************************************************
763 * This function is called when the thread ends after a successful
765 *****************************************************************************/
766 void EndVideoDec( decoder_t *p_dec )
768 decoder_sys_t *p_sys = p_dec->p_sys;
772 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
773 if( p_sys->p_context->codec )
774 avcodec_flush_buffers( p_sys->p_context );
778 if( p_sys->p_ff_pic )
779 av_free( p_sys->p_ff_pic );
782 vlc_va_Delete( p_sys->p_va );
784 vlc_sem_destroy( &p_sys->sem_mt );
787 /*****************************************************************************
788 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
789 *****************************************************************************/
790 static void ffmpeg_InitCodec( decoder_t *p_dec )
792 decoder_sys_t *p_sys = p_dec->p_sys;
793 int i_size = p_dec->fmt_in.i_extra;
795 if( !i_size ) return;
797 if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
801 p_sys->p_context->extradata_size = i_size + 12;
802 p = p_sys->p_context->extradata =
803 malloc( p_sys->p_context->extradata_size );
807 memcpy( &p[0], "SVQ3", 4 );
808 memset( &p[4], 0, 8 );
809 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
811 /* Now remove all atoms before the SMI one */
812 if( p_sys->p_context->extradata_size > 0x5a &&
813 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
815 uint8_t *psz = &p[0x52];
817 while( psz < &p[p_sys->p_context->extradata_size - 8] )
819 int i_size = GetDWBE( psz );
822 /* FIXME handle 1 as long size */
825 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
827 memmove( &p[0x52], psz,
828 &p[p_sys->p_context->extradata_size] - psz );
838 p_sys->p_context->extradata_size = i_size;
839 p_sys->p_context->extradata =
840 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
841 if( p_sys->p_context->extradata )
843 memcpy( p_sys->p_context->extradata,
844 p_dec->fmt_in.p_extra, i_size );
845 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
846 0, FF_INPUT_BUFFER_PADDING_SIZE );
851 /*****************************************************************************
852 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
853 * picture_t structure (when not in direct rendering mode).
854 *****************************************************************************/
855 static void ffmpeg_CopyPicture( decoder_t *p_dec,
856 picture_t *p_pic, AVFrame *p_ff_pic )
858 decoder_sys_t *p_sys = p_dec->p_sys;
862 vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic );
864 else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
866 int i_plane, i_size, i_line;
867 uint8_t *p_dst, *p_src;
868 int i_src_stride, i_dst_stride;
870 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
872 p_src = p_ff_pic->data[i_plane];
873 p_dst = p_pic->p[i_plane].p_pixels;
874 i_src_stride = p_ff_pic->linesize[i_plane];
875 i_dst_stride = p_pic->p[i_plane].i_pitch;
877 i_size = __MIN( i_src_stride, i_dst_stride );
878 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
881 memcpy( p_dst, p_src, i_size );
882 p_src += i_src_stride;
883 p_dst += i_dst_stride;
889 const char *name = av_get_pix_fmt_name( p_sys->p_context->pix_fmt );
890 msg_Err( p_dec, "Unsupported decoded output format %d (%s)",
891 p_sys->p_context->pix_fmt, name ? name : "unknown" );
896 #if LIBAVCODEC_VERSION_MAJOR >= 55
903 static void lavc_va_ReleaseFrame(void *opaque, uint8_t *data)
905 lavc_hw_ref_t *ref = opaque;
907 vlc_va_Release(ref->va, ref->frame);
912 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
915 decoder_t *dec = ctx->opaque;
916 decoder_sys_t *sys = dec->p_sys;
917 vlc_va_t *va = sys->p_va;
919 if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
920 ctx->coded_width, ctx->coded_height))
922 msg_Err(dec, "hardware acceleration setup failed");
925 if (vlc_va_Get(va, frame))
927 msg_Err(dec, "hardware acceleration picture allocation failed");
931 lavc_hw_ref_t *ref = malloc(sizeof (*ref));
932 if (unlikely(ref == NULL))
934 vlc_va_Release(va, frame);
940 frame->buf[0] = av_buffer_create(frame->data[0], 0, lavc_va_ReleaseFrame,
942 if (unlikely(frame->buf[0] == NULL))
944 lavc_va_ReleaseFrame(ref, frame->data[0]);
947 assert(frame->data[0] != NULL);
948 assert(frame->data[3] != NULL);
959 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
961 lavc_pic_ref_t *ref = opaque;
963 decoder_UnlinkPicture(ref->decoder, ref->picture);
968 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
969 AVFrame *frame, int flags)
971 decoder_t *dec = (decoder_t *)ctx->opaque;
973 if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
975 dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
976 if (ctx->pix_fmt == PIX_FMT_PAL8)
979 int width = frame->width;
980 int height = frame->height;
981 int aligns[AV_NUM_DATA_POINTERS];
983 avcodec_align_dimensions2(ctx, &width, &height, aligns);
985 picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
989 /* Check that the picture is suitable for libavcodec */
990 if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
992 msg_Dbg(dec, "plane 0: pitch too small (%d/%d*%d)",
993 pic->p[0].i_pitch, width, pic->p[0].i_pixel_pitch);
997 if (pic->p[0].i_lines < height)
999 msg_Dbg(dec, "plane 0: lines too few (%d/%d)",
1000 pic->p[0].i_lines, height);
1004 for (int i = 0; i < pic->i_planes; i++)
1006 if (pic->p[i].i_pitch % aligns[i])
1008 msg_Dbg(dec, "plane %d: pitch not aligned (%d%%%d)",
1009 i, pic->p[i].i_pitch, aligns[i]);
1012 if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1014 msg_Warn(dec, "plane %d not aligned", i);
1019 /* Allocate buffer references */
1020 for (int i = 0; i < pic->i_planes; i++)
1022 lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1027 decoder_LinkPicture(dec, pic);
1029 uint8_t *data = pic->p[i].p_pixels;
1030 int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1032 frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1034 if (unlikely(frame->buf[i] == NULL))
1036 lavc_dr_ReleaseFrame(ref, data);
1040 decoder_UnlinkPicture(dec, pic);
1044 for (unsigned i = 0; frame->buf[i] != NULL; i++)
1045 av_buffer_unref(&frame->buf[i]);
1047 decoder_DeletePicture(dec, pic);
1052 * Callback used by libavcodec to get a frame buffer.
1054 * It is used for direct rendering as well as to get the right PTS for each
1055 * decoded picture (even in indirect rendering mode).
1057 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1059 decoder_t *dec = ctx->opaque;
1060 decoder_sys_t *sys = dec->p_sys;
1063 for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1065 frame->data[i] = NULL;
1066 frame->linesize[i] = 0;
1067 frame->buf[i] = NULL;
1070 if (sys->p_va != NULL)
1071 return lavc_va_GetFrame(ctx, frame, flags);
1073 frame->opaque = NULL;
1074 if (!sys->b_direct_rendering)
1075 return avcodec_default_get_buffer2(ctx, frame, flags);
1077 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1078 * so we need to check for direct rendering again. */
1080 pic = lavc_dr_GetFrame(ctx, frame, flags);
1083 if (sys->i_direct_rendering_used != 0)
1085 msg_Warn(dec, "disabling direct rendering");
1086 sys->i_direct_rendering_used = 0;
1089 return avcodec_default_get_buffer2(ctx, frame, flags);
1092 if (sys->i_direct_rendering_used != 1)
1094 msg_Dbg(dec, "enabling direct rendering");
1095 sys->i_direct_rendering_used = 1;
1099 frame->opaque = pic;
1100 static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1101 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++)
1103 frame->data[i] = pic->p[i].p_pixels;
1104 frame->linesize[i] = pic->p[i].i_pitch;
1109 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1111 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1112 decoder_sys_t *p_sys = p_dec->p_sys;
1113 vlc_va_t *p_va = p_sys->p_va;
1115 /* hwaccel_context is not present in old ffmpeg version */
1116 if( vlc_va_Setup( p_va,
1117 &p_context->hwaccel_context, &p_dec->fmt_out.video.i_chroma,
1118 p_context->coded_width, p_context->coded_height ) )
1120 msg_Err( p_dec, "vlc_va_Setup failed" );
1124 if( vlc_va_Get( p_va, p_ff_pic ) )
1126 msg_Err( p_dec, "vlc_va_Get failed" );
1130 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1134 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1136 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1137 decoder_sys_t *p_sys = p_dec->p_sys;
1139 int i_width = p_context->width;
1140 int i_height = p_context->height;
1141 avcodec_align_dimensions( p_context, &i_width, &i_height );
1143 picture_t *p_pic = NULL;
1144 if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1147 if (p_context->pix_fmt == PIX_FMT_PAL8)
1150 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1152 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1156 if( p_pic->p[0].i_pitch / p_pic->p[0].i_pixel_pitch < i_width ||
1157 p_pic->p[0].i_lines < i_height )
1160 for( int i = 0; i < p_pic->i_planes; i++ )
1163 switch( p_sys->i_codec_id )
1165 case AV_CODEC_ID_SVQ1:
1166 case AV_CODEC_ID_VP5:
1167 case AV_CODEC_ID_VP6:
1168 case AV_CODEC_ID_VP6F:
1169 case AV_CODEC_ID_VP6A:
1173 i_align = i == 0 ? 16 : 8;
1176 if( p_pic->p[i].i_pitch % i_align )
1178 if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1182 if( p_context->pix_fmt == PIX_FMT_YUV422P )
1184 if( 2 * p_pic->p[1].i_pitch != p_pic->p[0].i_pitch ||
1185 2 * p_pic->p[2].i_pitch != p_pic->p[0].i_pitch )
1193 decoder_DeletePicture( p_dec, p_pic );
1198 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
1201 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1202 decoder_sys_t *p_sys = p_dec->p_sys;
1205 p_ff_pic->opaque = NULL;
1206 #if ! LIBAVCODEC_VERSION_CHECK(54, 34, 0, 79, 101)
1207 p_ff_pic->pkt_pts = p_context->pkt ? p_context->pkt->pts : AV_NOPTS_VALUE;
1209 #if LIBAVCODEC_VERSION_MAJOR < 54
1210 p_ff_pic->age = 256*256*256*64;
1214 return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1216 if( !p_sys->b_direct_rendering )
1217 return avcodec_default_get_buffer( p_context, p_ff_pic );
1220 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1221 * so we need to check for direct rendering again. */
1223 picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1225 if( p_sys->i_direct_rendering_used != 0 )
1227 msg_Warn( p_dec, "disabling direct rendering" );
1228 p_sys->i_direct_rendering_used = 0;
1232 return avcodec_default_get_buffer( p_context, p_ff_pic );
1235 if( p_sys->i_direct_rendering_used != 1 ) {
1236 msg_Dbg( p_dec, "using direct rendering" );
1237 p_sys->i_direct_rendering_used = 1;
1240 p_context->draw_horiz_band = NULL;
1243 p_ff_pic->opaque = (void*)p_pic;
1244 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1245 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
1246 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
1247 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
1248 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
1250 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
1251 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
1252 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
1253 p_ff_pic->linesize[3] = 0;
1258 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1261 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1262 decoder_sys_t *p_sys = p_dec->p_sys;
1265 vlc_va_Release( p_sys->p_va, p_ff_pic );
1266 else if( p_ff_pic->opaque )
1267 decoder_UnlinkPicture( p_dec, (picture_t*)p_ff_pic->opaque);
1268 else if( p_ff_pic->type == FF_BUFFER_TYPE_INTERNAL )
1269 /* We can end up here without the AVFrame being allocated by
1270 * avcodec_default_get_buffer() if VA is used and the frame is
1271 * released when the decoder is closed
1273 avcodec_default_release_buffer( p_context, p_ff_pic );
1275 for( int i = 0; i < 4; i++ )
1276 p_ff_pic->data[i] = NULL;
1280 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1281 const enum PixelFormat *pi_fmt )
1283 decoder_t *p_dec = p_context->opaque;
1284 decoder_sys_t *p_sys = p_dec->p_sys;
1285 vlc_va_t *p_va = p_sys->p_va;
1288 vlc_va_Delete( p_va );
1290 /* Enumerate available formats */
1291 bool can_hwaccel = false;
1292 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1294 const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1297 bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1299 msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1300 hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1308 /* Profile and level informations are needed now.
1309 * TODO: avoid code duplication with avcodec.c */
1310 if( p_context->profile != FF_PROFILE_UNKNOWN)
1311 p_dec->fmt_in.i_profile = p_context->profile;
1312 if( p_context->level != FF_LEVEL_UNKNOWN)
1313 p_dec->fmt_in.i_level = p_context->level;
1315 p_va = vlc_va_New( VLC_OBJECT(p_dec), p_sys->i_codec_id, &p_dec->fmt_in );
1319 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1321 if( p_va->pix_fmt != pi_fmt[i] )
1324 /* We try to call vlc_va_Setup when possible to detect errors when
1325 * possible (later is too late) */
1326 if( p_context->width > 0 && p_context->height > 0
1327 && vlc_va_Setup( p_va, &p_context->hwaccel_context,
1328 &p_dec->fmt_out.video.i_chroma,
1329 p_context->width, p_context->height ) )
1331 msg_Err( p_dec, "acceleration setup failure" );
1335 if( p_va->description )
1336 msg_Info( p_dec, "Using %s for hardware decoding.",
1337 p_va->description );
1339 /* FIXME this will disable direct rendering
1340 * even if a new pixel format is renegotiated
1342 p_sys->b_direct_rendering = false;
1344 p_context->draw_horiz_band = NULL;
1348 vlc_va_Delete( p_va );
1351 /* Fallback to default behaviour */
1353 return avcodec_default_get_format( p_context, pi_fmt );