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;
129 int aligns[AV_NUM_DATA_POINTERS];
130 int width = p_context->coded_width;
131 int height = p_context->coded_height;
133 avcodec_align_dimensions2(p_context, &width, &height, aligns);
135 if( width == 0 || height == 0)
136 return NULL; /* invalid display size */
138 p_dec->fmt_out.video.i_width = width;
139 p_dec->fmt_out.video.i_height = height;
141 if( width != p_context->width || height != p_context->height )
143 p_dec->fmt_out.video.i_visible_width = p_context->width;
144 p_dec->fmt_out.video.i_visible_height = p_context->height;
147 if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
149 /* we are doomed, but not really, because most codecs set their pix_fmt
151 * FIXME does it make sense here ? */
152 p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
154 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
156 /* If an aspect-ratio was specified in the input format then force it */
157 if( p_dec->fmt_in.video.i_sar_num > 0 && p_dec->fmt_in.video.i_sar_den > 0 )
159 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
160 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
164 p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
165 p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
167 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
169 p_dec->fmt_out.video.i_sar_num = 1;
170 p_dec->fmt_out.video.i_sar_den = 1;
174 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
175 p_dec->fmt_in.video.i_frame_rate_base > 0 )
177 p_dec->fmt_out.video.i_frame_rate =
178 p_dec->fmt_in.video.i_frame_rate;
179 p_dec->fmt_out.video.i_frame_rate_base =
180 p_dec->fmt_in.video.i_frame_rate_base;
182 else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
184 p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
185 p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num * __MAX( p_context->ticks_per_frame, 1 );
188 return decoder_NewPicture( p_dec );
191 /*****************************************************************************
192 * InitVideo: initialize the video decoder
193 *****************************************************************************
194 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
195 * opened (done after the first decoded frame).
196 *****************************************************************************/
197 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
198 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
200 decoder_sys_t *p_sys;
203 /* Allocate the memory needed to store the decoder's structure */
204 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
207 p_codec->type = AVMEDIA_TYPE_VIDEO;
208 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
209 p_context->codec_id = i_codec_id;
210 p_sys->p_context = p_context;
211 p_sys->p_codec = p_codec;
212 p_sys->i_codec_id = i_codec_id;
213 p_sys->psz_namecodec = psz_namecodec;
214 p_sys->p_ff_pic = avcodec_alloc_frame();
215 p_sys->b_delayed_open = true;
217 vlc_sem_init( &p_sys->sem_mt, 0 );
219 /* ***** Fill p_context with init values ***** */
220 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec );
222 /* ***** Get configuration of ffmpeg plugin ***** */
223 p_sys->p_context->workaround_bugs =
224 var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
225 p_sys->p_context->err_recognition =
226 var_InheritInteger( p_dec, "avcodec-error-resilience" );
228 if( var_CreateGetBool( p_dec, "grayscale" ) )
229 p_sys->p_context->flags |= CODEC_FLAG_GRAY;
231 i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
232 if( i_val ) p_sys->p_context->debug_mv = i_val;
234 i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
235 if( i_val >= 4 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
236 else if( i_val == 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
237 else if( i_val == 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
238 else if( i_val == 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
240 if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
241 p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
243 /* ***** libavcodec frame skipping ***** */
244 p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
246 switch( var_CreateGetInteger( p_dec, "avcodec-skip-frame" ) )
249 p_sys->p_context->skip_frame = AVDISCARD_NONE;
252 p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
255 p_sys->p_context->skip_frame = AVDISCARD_NONREF;
258 p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
261 p_sys->p_context->skip_frame = AVDISCARD_ALL;
264 p_sys->p_context->skip_frame = AVDISCARD_NONE;
267 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
269 switch( var_CreateGetInteger( p_dec, "avcodec-skip-idct" ) )
272 p_sys->p_context->skip_idct = AVDISCARD_NONE;
275 p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
278 p_sys->p_context->skip_idct = AVDISCARD_NONREF;
281 p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
284 p_sys->p_context->skip_idct = AVDISCARD_ALL;
287 p_sys->p_context->skip_idct = AVDISCARD_NONE;
290 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
292 /* ***** libavcodec direct rendering ***** */
293 p_sys->b_direct_rendering = false;
294 p_sys->i_direct_rendering_used = -1;
295 if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
296 (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
297 /* No idea why ... but this fixes flickering on some TSCC streams */
298 p_sys->i_codec_id != AV_CODEC_ID_TSCC && p_sys->i_codec_id != AV_CODEC_ID_CSCD &&
299 p_sys->i_codec_id != AV_CODEC_ID_CINEPAK &&
300 !p_sys->p_context->debug_mv )
302 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
303 * so we need to do another check in ffmpeg_GetFrameBuf() */
304 p_sys->b_direct_rendering = true;
307 /* libavcodec doesn't properly release old pictures when frames are skipped */
308 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = false;
309 if( p_sys->b_direct_rendering )
311 msg_Dbg( p_dec, "trying to use direct rendering" );
312 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
316 msg_Dbg( p_dec, "direct rendering is disabled" );
319 p_sys->p_context->get_format = ffmpeg_GetFormat;
320 /* Always use our get_buffer wrapper so we can calculate the
322 #if LIBAVCODEC_VERSION_MAJOR >= 55
323 p_sys->p_context->get_buffer2 = lavc_GetFrame;
325 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
326 p_sys->p_context->reget_buffer = avcodec_default_reget_buffer;
327 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
329 p_sys->p_context->opaque = p_dec;
331 #ifdef HAVE_AVCODEC_MT
332 int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
333 if( i_thread_count <= 0 )
335 i_thread_count = vlc_GetCPUCount();
336 if( i_thread_count > 1 )
339 //FIXME: take in count the decoding time
340 i_thread_count = __MIN( i_thread_count, 4 );
342 i_thread_count = __MIN( i_thread_count, 16 );
343 msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
344 p_sys->p_context->thread_count = i_thread_count;
345 p_sys->p_context->thread_safe_callbacks = true;
349 case AV_CODEC_ID_MPEG4:
350 case AV_CODEC_ID_H263:
351 p_sys->p_context->thread_type = 0;
353 case AV_CODEC_ID_MPEG1VIDEO:
354 case AV_CODEC_ID_MPEG2VIDEO:
355 p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
357 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
358 case AV_CODEC_ID_H264:
359 case AV_CODEC_ID_VC1:
360 case AV_CODEC_ID_WMV3:
361 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
365 if( p_sys->p_context->thread_type & FF_THREAD_FRAME )
366 p_dec->i_extra_picture_buffers = 2 * p_sys->p_context->thread_count;
369 /* ***** misc init ***** */
370 p_sys->i_pts = VLC_TS_INVALID;
371 p_sys->b_has_b_frames = false;
372 p_sys->b_first_frame = true;
373 p_sys->b_flush = false;
374 p_sys->i_late_frames = 0;
376 /* Set output properties */
377 p_dec->fmt_out.i_cat = VIDEO_ES;
378 if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
380 /* we are doomed. but not really, because most codecs set their pix_fmt later on */
381 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
383 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
385 #if LIBAVCODEC_VERSION_MAJOR < 54
387 memset( &p_sys->palette, 0, sizeof(p_sys->palette) );
388 if( p_dec->fmt_in.video.p_palette )
390 p_sys->palette.palette_changed = 1;
392 for( int i = 0; i < __MIN( AVPALETTE_COUNT, p_dec->fmt_in.video.p_palette->i_entries ); i++ )
398 c.a[0] = p_dec->fmt_in.video.p_palette->palette[i][0];
399 c.a[1] = p_dec->fmt_in.video.p_palette->palette[i][1];
400 c.a[2] = p_dec->fmt_in.video.p_palette->palette[i][2];
401 c.a[3] = p_dec->fmt_in.video.p_palette->palette[i][3];
403 p_sys->palette.palette[i] = c.u;
405 p_sys->p_context->palctrl = &p_sys->palette;
407 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
408 if( p_dec->fmt_out.video.p_palette )
409 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
411 else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
413 p_sys->p_context->palctrl = &p_sys->palette;
419 /* ***** init this codec with special data ***** */
420 ffmpeg_InitCodec( p_dec );
422 /* ***** Open the codec ***** */
423 if( ffmpeg_OpenCodec( p_dec ) < 0 )
425 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
426 av_free( p_sys->p_ff_pic );
427 vlc_sem_destroy( &p_sys->sem_mt );
435 /*****************************************************************************
436 * DecodeVideo: Called to decode one or more frames
437 *****************************************************************************/
438 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
440 decoder_sys_t *p_sys = p_dec->p_sys;
441 AVCodecContext *p_context = p_sys->p_context;
443 int b_null_size = false;
446 if( !pp_block || !*pp_block )
449 if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
451 ffmpeg_InitCodec( p_dec );
452 if( p_sys->b_delayed_open )
454 if( ffmpeg_OpenCodec( p_dec ) )
455 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
460 if( p_sys->b_delayed_open )
462 block_Release( p_block );
466 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
468 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
470 p_sys->i_late_frames = 0;
473 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
474 avcodec_flush_buffers( p_context );
477 block_Release( p_block );
481 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
483 /* Do not care about late frames when prerolling
484 * TODO avoid decoding of non reference frame
485 * (ie all B except for H264 where it depends only on nal_ref_idc) */
486 p_sys->i_late_frames = 0;
489 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
490 (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
492 if( p_sys->i_pts > VLC_TS_INVALID )
494 msg_Err( p_dec, "more than 5 seconds of late video -> "
495 "dropping frame (computer too slow ?)" );
496 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
498 block_Release( p_block );
499 p_sys->i_late_frames--;
503 /* A good idea could be to decode all I pictures and see for the other */
504 if( !p_dec->b_pace_control &&
506 (p_sys->i_late_frames > 4) )
509 if( p_sys->i_late_frames < 12 )
511 p_context->skip_frame =
512 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
513 AVDISCARD_NONREF : p_sys->i_skip_frame;
517 /* picture too late, won't decode
518 * but break picture until a new I, and for mpeg4 ...*/
519 p_sys->i_late_frames--; /* needed else it will never be decrease */
520 block_Release( p_block );
526 if( p_sys->b_hurry_up )
527 p_context->skip_frame = p_sys->i_skip_frame;
528 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
534 if( p_context->width <= 0 || p_context->height <= 0 )
536 if( p_sys->b_hurry_up )
537 p_context->skip_frame = p_sys->i_skip_frame;
540 else if( !b_drawpicture )
542 /* It creates broken picture
543 * FIXME either our parser or ffmpeg is broken */
545 if( p_sys->b_hurry_up )
546 p_context->skip_frame = __MAX( p_context->skip_frame,
552 * Do the actual decoding now */
554 /* Don't forget that libavcodec requires a little more bytes
555 * that the real frame size */
556 if( p_block->i_buffer > 0 )
558 p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
560 p_block = block_Realloc( p_block, 0,
561 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
564 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
566 memset( p_block->p_buffer + p_block->i_buffer, 0,
567 FF_INPUT_BUFFER_PADDING_SIZE );
570 while( p_block->i_buffer > 0 || p_sys->b_flush )
572 int i_used, b_gotpicture;
578 av_init_packet( &pkt );
579 pkt.data = p_block->p_buffer;
580 pkt.size = p_block->i_buffer;
581 pkt.pts = p_block->i_pts;
582 pkt.dts = p_block->i_dts;
584 /* Make sure we don't reuse the same timestamps twice */
586 p_block->i_dts = VLC_TS_INVALID;
588 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
589 &b_gotpicture, &pkt );
591 if( b_null_size && !p_sys->b_flush &&
592 p_context->width > 0 && p_context->height > 0 )
594 /* Reparse it to not drop the I frame */
596 if( p_sys->b_hurry_up )
597 p_context->skip_frame = p_sys->i_skip_frame;
598 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
599 &b_gotpicture, &pkt );
604 p_sys->b_first_frame = true;
606 if( p_block->i_buffer <= 0 )
607 p_sys->b_flush = false;
612 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
614 block_Release( p_block );
617 else if( (unsigned)i_used > p_block->i_buffer ||
618 p_context->thread_count > 1 )
620 i_used = p_block->i_buffer;
624 p_block->i_buffer -= i_used;
625 p_block->p_buffer += i_used;
627 /* Nothing to display */
630 if( i_used == 0 ) break;
634 /* Sanity check (seems to be needed for some streams) */
635 if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
637 p_sys->b_has_b_frames = true;
640 /* Compute the PTS */
642 p_sys->p_ff_pic->pkt_pts;
643 if (i_pts <= VLC_TS_INVALID)
644 i_pts = p_sys->p_ff_pic->pkt_dts;
646 if( i_pts <= VLC_TS_INVALID )
647 i_pts = p_sys->i_pts;
649 /* Interpolate the next PTS */
650 if( i_pts > VLC_TS_INVALID )
651 p_sys->i_pts = i_pts;
652 if( p_sys->i_pts > VLC_TS_INVALID )
654 /* interpolate the next PTS */
655 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
656 p_dec->fmt_in.video.i_frame_rate_base > 0 )
658 p_sys->i_pts += INT64_C(1000000) *
659 (2 + p_sys->p_ff_pic->repeat_pict) *
660 p_dec->fmt_in.video.i_frame_rate_base /
661 (2 * p_dec->fmt_in.video.i_frame_rate);
663 else if( p_context->time_base.den > 0 )
665 int i_tick = p_context->ticks_per_frame;
669 p_sys->i_pts += INT64_C(1000000) *
670 (2 + p_sys->p_ff_pic->repeat_pict) *
671 i_tick * p_context->time_base.num /
672 (2 * p_context->time_base.den);
676 /* Update frame late count (except when doing preroll) */
677 mtime_t i_display_date = 0;
678 if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
679 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
681 if( i_display_date > 0 && i_display_date <= mdate() )
683 p_sys->i_late_frames++;
684 if( p_sys->i_late_frames == 1 )
685 p_sys->i_late_frames_start = mdate();
689 p_sys->i_late_frames = 0;
692 if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
695 if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
697 /* Get a new picture */
698 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
701 block_Release( p_block );
705 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
707 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
711 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
712 decoder_LinkPicture( p_dec, p_pic );
715 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
717 /* Fetch again the aspect ratio in case it changed */
718 p_dec->fmt_out.video.i_sar_num
719 = p_context->sample_aspect_ratio.num;
720 p_dec->fmt_out.video.i_sar_den
721 = p_context->sample_aspect_ratio.den;
723 if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
725 p_dec->fmt_out.video.i_sar_num = 1;
726 p_dec->fmt_out.video.i_sar_den = 1;
730 /* Send decoded frame to vout */
731 if( i_pts > VLC_TS_INVALID)
735 if( p_sys->b_first_frame )
737 /* Hack to force display of still pictures */
738 p_sys->b_first_frame = false;
739 p_pic->b_force = true;
742 p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
743 p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
744 p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
750 decoder_DeletePicture( p_dec, p_pic );
754 block_Release( p_block );
758 /*****************************************************************************
759 * EndVideo: decoder destruction
760 *****************************************************************************
761 * This function is called when the thread ends after a successful
763 *****************************************************************************/
764 void EndVideoDec( decoder_t *p_dec )
766 decoder_sys_t *p_sys = p_dec->p_sys;
770 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
771 if( p_sys->p_context->codec )
772 avcodec_flush_buffers( p_sys->p_context );
776 if( p_sys->p_ff_pic )
777 av_free( p_sys->p_ff_pic );
780 vlc_va_Delete( p_sys->p_va );
782 vlc_sem_destroy( &p_sys->sem_mt );
785 /*****************************************************************************
786 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
787 *****************************************************************************/
788 static void ffmpeg_InitCodec( decoder_t *p_dec )
790 decoder_sys_t *p_sys = p_dec->p_sys;
791 int i_size = p_dec->fmt_in.i_extra;
793 if( !i_size ) return;
795 if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
799 p_sys->p_context->extradata_size = i_size + 12;
800 p = p_sys->p_context->extradata =
801 malloc( p_sys->p_context->extradata_size );
805 memcpy( &p[0], "SVQ3", 4 );
806 memset( &p[4], 0, 8 );
807 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
809 /* Now remove all atoms before the SMI one */
810 if( p_sys->p_context->extradata_size > 0x5a &&
811 strncmp( (char*)&p[0x56], "SMI ", 4 ) )
813 uint8_t *psz = &p[0x52];
815 while( psz < &p[p_sys->p_context->extradata_size - 8] )
817 int i_size = GetDWBE( psz );
820 /* FIXME handle 1 as long size */
823 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
825 memmove( &p[0x52], psz,
826 &p[p_sys->p_context->extradata_size] - psz );
836 p_sys->p_context->extradata_size = i_size;
837 p_sys->p_context->extradata =
838 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
839 if( p_sys->p_context->extradata )
841 memcpy( p_sys->p_context->extradata,
842 p_dec->fmt_in.p_extra, i_size );
843 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
844 0, FF_INPUT_BUFFER_PADDING_SIZE );
849 /*****************************************************************************
850 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
851 * picture_t structure (when not in direct rendering mode).
852 *****************************************************************************/
853 static void ffmpeg_CopyPicture( decoder_t *p_dec,
854 picture_t *p_pic, AVFrame *p_ff_pic )
856 decoder_sys_t *p_sys = p_dec->p_sys;
860 vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic->opaque,
863 else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
865 int i_plane, i_size, i_line;
866 uint8_t *p_dst, *p_src;
867 int i_src_stride, i_dst_stride;
869 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
871 p_src = p_ff_pic->data[i_plane];
872 p_dst = p_pic->p[i_plane].p_pixels;
873 i_src_stride = p_ff_pic->linesize[i_plane];
874 i_dst_stride = p_pic->p[i_plane].i_pitch;
876 i_size = __MIN( i_src_stride, i_dst_stride );
877 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
880 memcpy( p_dst, p_src, i_size );
881 p_src += i_src_stride;
882 p_dst += i_dst_stride;
888 const char *name = av_get_pix_fmt_name( p_sys->p_context->pix_fmt );
889 msg_Err( p_dec, "Unsupported decoded output format %d (%s)",
890 p_sys->p_context->pix_fmt, name ? name : "unknown" );
895 #if LIBAVCODEC_VERSION_MAJOR >= 55
896 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
899 decoder_t *dec = ctx->opaque;
900 decoder_sys_t *sys = dec->p_sys;
901 vlc_va_t *va = sys->p_va;
903 if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
904 ctx->coded_width, ctx->coded_height))
906 msg_Err(dec, "hardware acceleration setup failed");
909 if (vlc_va_Get(va, &frame->opaque, &frame->data[0]))
911 msg_Err(dec, "hardware acceleration picture allocation failed");
914 /* data[0] must be non-NULL for libavcodec internal checks.
915 * data[3] actually contains the format-specific surface handle. */
916 frame->data[3] = frame->data[0];
918 frame->buf[0] = av_buffer_create(frame->data[0], 0, va->release,
920 if (unlikely(frame->buf[0] == NULL))
922 vlc_va_Release(va, frame->opaque, frame->data[0]);
925 assert(frame->data[0] != NULL);
936 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
938 lavc_pic_ref_t *ref = opaque;
940 decoder_UnlinkPicture(ref->decoder, ref->picture);
945 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
946 AVFrame *frame, int flags)
948 decoder_t *dec = (decoder_t *)ctx->opaque;
949 decoder_sys_t *sys = dec->p_sys;
951 if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
953 dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
954 if (ctx->pix_fmt == PIX_FMT_PAL8)
957 int width = frame->width;
958 int height = frame->height;
959 int aligns[AV_NUM_DATA_POINTERS];
961 avcodec_align_dimensions2(ctx, &width, &height, aligns);
963 picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
967 /* Check that the picture is suitable for libavcodec */
968 if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
970 if (sys->i_direct_rendering_used != 0)
971 msg_Dbg(dec, "plane 0: pitch too small (%d/%d*%d)",
972 pic->p[0].i_pitch, width, pic->p[0].i_pixel_pitch);
976 if (pic->p[0].i_lines < height)
978 if (sys->i_direct_rendering_used != 0)
979 msg_Dbg(dec, "plane 0: lines too few (%d/%d)",
980 pic->p[0].i_lines, height);
984 for (int i = 0; i < pic->i_planes; i++)
986 if (pic->p[i].i_pitch % aligns[i])
988 if (sys->i_direct_rendering_used != 0)
989 msg_Dbg(dec, "plane %d: pitch not aligned (%d%%%d)",
990 i, pic->p[i].i_pitch, aligns[i]);
993 if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
995 if (sys->i_direct_rendering_used != 0)
996 msg_Warn(dec, "plane %d not aligned", i);
1001 /* Allocate buffer references */
1002 for (int i = 0; i < pic->i_planes; i++)
1004 lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1009 decoder_LinkPicture(dec, pic);
1011 uint8_t *data = pic->p[i].p_pixels;
1012 int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1014 frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1016 if (unlikely(frame->buf[i] == NULL))
1018 lavc_dr_ReleaseFrame(ref, data);
1022 decoder_UnlinkPicture(dec, pic);
1026 for (unsigned i = 0; frame->buf[i] != NULL; i++)
1027 av_buffer_unref(&frame->buf[i]);
1029 decoder_DeletePicture(dec, pic);
1034 * Callback used by libavcodec to get a frame buffer.
1036 * It is used for direct rendering as well as to get the right PTS for each
1037 * decoded picture (even in indirect rendering mode).
1039 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1041 decoder_t *dec = ctx->opaque;
1042 decoder_sys_t *sys = dec->p_sys;
1045 for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1047 frame->data[i] = NULL;
1048 frame->linesize[i] = 0;
1049 frame->buf[i] = NULL;
1052 if (sys->p_va != NULL)
1053 return lavc_va_GetFrame(ctx, frame, flags);
1055 frame->opaque = NULL;
1056 if (!sys->b_direct_rendering)
1057 return avcodec_default_get_buffer2(ctx, frame, flags);
1059 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1060 * so we need to check for direct rendering again. */
1062 pic = lavc_dr_GetFrame(ctx, frame, flags);
1065 if (sys->i_direct_rendering_used != 0)
1067 msg_Warn(dec, "disabling direct rendering");
1068 sys->i_direct_rendering_used = 0;
1071 return avcodec_default_get_buffer2(ctx, frame, flags);
1074 if (sys->i_direct_rendering_used != 1)
1076 msg_Dbg(dec, "enabling direct rendering");
1077 sys->i_direct_rendering_used = 1;
1081 frame->opaque = pic;
1082 static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1083 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++)
1085 frame->data[i] = pic->p[i].p_pixels;
1086 frame->linesize[i] = pic->p[i].i_pitch;
1091 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1093 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1094 decoder_sys_t *p_sys = p_dec->p_sys;
1095 vlc_va_t *p_va = p_sys->p_va;
1097 /* hwaccel_context is not present in old ffmpeg version */
1098 if( vlc_va_Setup( p_va,
1099 &p_context->hwaccel_context, &p_dec->fmt_out.video.i_chroma,
1100 p_context->coded_width, p_context->coded_height ) )
1102 msg_Err( p_dec, "vlc_va_Setup failed" );
1106 if( vlc_va_Get( p_va, &p_ff_pic->opaque, &p_ff_pic->data[0] ) )
1108 msg_Err( p_dec, "vlc_va_Get failed" );
1112 p_ff_pic->data[3] = p_ff_pic->data[0];
1113 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1117 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1119 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1120 decoder_sys_t *p_sys = p_dec->p_sys;
1122 int i_width = p_context->width;
1123 int i_height = p_context->height;
1124 avcodec_align_dimensions( p_context, &i_width, &i_height );
1126 picture_t *p_pic = NULL;
1127 if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1130 if (p_context->pix_fmt == PIX_FMT_PAL8)
1133 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1135 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1139 if( p_pic->p[0].i_pitch / p_pic->p[0].i_pixel_pitch < i_width ||
1140 p_pic->p[0].i_lines < i_height )
1143 for( int i = 0; i < p_pic->i_planes; i++ )
1146 switch( p_sys->i_codec_id )
1148 case AV_CODEC_ID_SVQ1:
1149 case AV_CODEC_ID_VP5:
1150 case AV_CODEC_ID_VP6:
1151 case AV_CODEC_ID_VP6F:
1152 case AV_CODEC_ID_VP6A:
1156 i_align = i == 0 ? 16 : 8;
1159 if( p_pic->p[i].i_pitch % i_align )
1161 if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1165 if( p_context->pix_fmt == PIX_FMT_YUV422P )
1167 if( 2 * p_pic->p[1].i_pitch != p_pic->p[0].i_pitch ||
1168 2 * p_pic->p[2].i_pitch != p_pic->p[0].i_pitch )
1176 decoder_DeletePicture( p_dec, p_pic );
1181 static int ffmpeg_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;
1188 p_ff_pic->opaque = NULL;
1189 #if ! LIBAVCODEC_VERSION_CHECK(54, 34, 0, 79, 101)
1190 p_ff_pic->pkt_pts = p_context->pkt ? p_context->pkt->pts : AV_NOPTS_VALUE;
1192 #if LIBAVCODEC_VERSION_MAJOR < 54
1193 p_ff_pic->age = 256*256*256*64;
1197 return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1199 if( !p_sys->b_direct_rendering )
1200 return avcodec_default_get_buffer( p_context, p_ff_pic );
1203 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1204 * so we need to check for direct rendering again. */
1206 picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1208 if( p_sys->i_direct_rendering_used != 0 )
1210 msg_Warn( p_dec, "disabling direct rendering" );
1211 p_sys->i_direct_rendering_used = 0;
1215 return avcodec_default_get_buffer( p_context, p_ff_pic );
1218 if( p_sys->i_direct_rendering_used != 1 ) {
1219 msg_Dbg( p_dec, "using direct rendering" );
1220 p_sys->i_direct_rendering_used = 1;
1223 p_context->draw_horiz_band = NULL;
1226 p_ff_pic->opaque = (void*)p_pic;
1227 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1228 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
1229 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
1230 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
1231 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
1233 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
1234 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
1235 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
1236 p_ff_pic->linesize[3] = 0;
1241 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1244 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1245 decoder_sys_t *p_sys = p_dec->p_sys;
1248 vlc_va_Release( p_sys->p_va, p_ff_pic->opaque, p_ff_pic->data[0] );
1249 else if( p_ff_pic->opaque )
1250 decoder_UnlinkPicture( p_dec, (picture_t*)p_ff_pic->opaque);
1251 else if( p_ff_pic->type == FF_BUFFER_TYPE_INTERNAL )
1252 /* We can end up here without the AVFrame being allocated by
1253 * avcodec_default_get_buffer() if VA is used and the frame is
1254 * released when the decoder is closed
1256 avcodec_default_release_buffer( p_context, p_ff_pic );
1258 for( int i = 0; i < 4; i++ )
1259 p_ff_pic->data[i] = NULL;
1263 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1264 const enum PixelFormat *pi_fmt )
1266 decoder_t *p_dec = p_context->opaque;
1267 decoder_sys_t *p_sys = p_dec->p_sys;
1268 vlc_va_t *p_va = p_sys->p_va;
1271 vlc_va_Delete( p_va );
1273 /* Enumerate available formats */
1274 bool can_hwaccel = false;
1275 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1277 const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1280 bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1282 msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1283 hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1291 /* Profile and level informations are needed now.
1292 * TODO: avoid code duplication with avcodec.c */
1293 if( p_context->profile != FF_PROFILE_UNKNOWN)
1294 p_dec->fmt_in.i_profile = p_context->profile;
1295 if( p_context->level != FF_LEVEL_UNKNOWN)
1296 p_dec->fmt_in.i_level = p_context->level;
1298 p_va = vlc_va_New( VLC_OBJECT(p_dec), p_sys->i_codec_id, &p_dec->fmt_in );
1302 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1304 if( p_va->pix_fmt != pi_fmt[i] )
1307 /* We try to call vlc_va_Setup when possible to detect errors when
1308 * possible (later is too late) */
1309 if( p_context->width > 0 && p_context->height > 0
1310 && vlc_va_Setup( p_va, &p_context->hwaccel_context,
1311 &p_dec->fmt_out.video.i_chroma,
1312 p_context->width, p_context->height ) )
1314 msg_Err( p_dec, "acceleration setup failure" );
1318 if( p_va->description )
1319 msg_Info( p_dec, "Using %s for hardware decoding.",
1320 p_va->description );
1322 /* FIXME this will disable direct rendering
1323 * even if a new pixel format is renegotiated
1325 p_sys->b_direct_rendering = false;
1327 p_context->draw_horiz_band = NULL;
1331 vlc_va_Delete( p_va );
1334 /* Fallback to default behaviour */
1336 return avcodec_default_get_format( p_context, pi_fmt );