avcodec: fix memory leaks of frame side-data
[vlc.git] / modules / codec / avcodec / video.c
blobd20276befbcd278b39ec6db8cdf8f7198d173fc4
1 /*****************************************************************************
2 * video.c: video decoder using the libavcodec library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_avcodec.h>
35 #include <vlc_cpu.h>
36 #include <assert.h>
38 #include <libavcodec/avcodec.h>
39 #include <libavutil/mem.h>
40 #include <libavutil/pixdesc.h>
42 #include "avcodec.h"
43 #include "va.h"
45 /*****************************************************************************
46 * decoder_sys_t : decoder descriptor
47 *****************************************************************************/
48 struct decoder_sys_t
50 AVCODEC_COMMON_MEMBERS
52 /* Video decoder specific part */
53 mtime_t i_pts;
55 AVFrame *p_ff_pic;
57 /* for frame skipping algo */
58 bool b_hurry_up;
59 enum AVDiscard i_skip_frame;
60 enum AVDiscard i_skip_idct;
62 /* how many decoded frames are late */
63 int i_late_frames;
64 mtime_t i_late_frames_start;
66 /* for direct rendering */
67 bool b_direct_rendering;
68 int i_direct_rendering_used;
70 bool b_has_b_frames;
72 /* Hack to force display of still pictures */
73 bool b_first_frame;
76 /* */
77 bool palette_sent;
79 /* */
80 bool b_flush;
82 /* VA API */
83 vlc_va_t *p_va;
85 vlc_sem_t sem_mt;
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 )
91 #else
92 # define wait_mt(s)
93 # define post_mt(s)
94 #endif
96 /*****************************************************************************
97 * Local prototypes
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);
103 #else
104 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
105 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
106 #endif
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 /*****************************************************************************
117 * Local Functions
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;
149 else
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
158 * much later
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;
170 else
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 /*****************************************************************************
200 * InitVideo: initialize the video decoder
201 *****************************************************************************
202 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
203 * opened (done after the first decoded frame).
204 *****************************************************************************/
205 int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
206 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
208 decoder_sys_t *p_sys;
209 int i_val;
211 /* Allocate the memory needed to store the decoder's structure */
212 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) ) ) == NULL )
213 return VLC_ENOMEM;
215 p_codec->type = AVMEDIA_TYPE_VIDEO;
216 p_context->codec_type = AVMEDIA_TYPE_VIDEO;
217 p_context->codec_id = i_codec_id;
218 p_sys->p_context = p_context;
219 p_sys->p_codec = p_codec;
220 p_sys->i_codec_id = i_codec_id;
221 p_sys->psz_namecodec = psz_namecodec;
222 p_sys->p_ff_pic = avcodec_alloc_frame();
223 p_sys->b_delayed_open = true;
224 p_sys->p_va = NULL;
225 vlc_sem_init( &p_sys->sem_mt, 0 );
227 /* ***** Fill p_context with init values ***** */
228 p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec );
230 /* ***** Get configuration of ffmpeg plugin ***** */
231 p_sys->p_context->workaround_bugs =
232 var_InheritInteger( p_dec, "avcodec-workaround-bugs" );
233 p_sys->p_context->err_recognition =
234 var_InheritInteger( p_dec, "avcodec-error-resilience" );
236 if( var_CreateGetBool( p_dec, "grayscale" ) )
237 p_sys->p_context->flags |= CODEC_FLAG_GRAY;
239 /* ***** Output always the frames ***** */
240 #if LIBAVCODEC_VERSION_CHECK(55, 23, 1, 40, 101)
241 p_sys->p_context->flags |= CODEC_FLAG_OUTPUT_CORRUPT;
242 #endif
244 i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
245 if( i_val ) p_sys->p_context->debug_mv = i_val;
247 i_val = var_CreateGetInteger( p_dec, "avcodec-skiploopfilter" );
248 if( i_val >= 4 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
249 else if( i_val == 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
250 else if( i_val == 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
251 else if( i_val == 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
253 if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
254 p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
256 /* ***** libavcodec frame skipping ***** */
257 p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
259 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-frame" );
260 if( i_val >= 4 ) p_sys->p_context->skip_frame = AVDISCARD_ALL;
261 else if( i_val == 3 ) p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
262 else if( i_val == 2 ) p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
263 else if( i_val == 1 ) p_sys->p_context->skip_frame = AVDISCARD_NONREF;
264 else if( i_val == -1 ) p_sys->p_context->skip_frame = AVDISCARD_NONE;
265 else p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
266 p_sys->i_skip_frame = p_sys->p_context->skip_frame;
268 i_val = var_CreateGetInteger( p_dec, "avcodec-skip-idct" );
269 if( i_val >= 4 ) p_sys->p_context->skip_idct = AVDISCARD_ALL;
270 else if( i_val == 3 ) p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
271 else if( i_val == 2 ) p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
272 else if( i_val == 1 ) p_sys->p_context->skip_idct = AVDISCARD_NONREF;
273 else if( i_val == -1 ) p_sys->p_context->skip_idct = AVDISCARD_NONE;
274 else p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
275 p_sys->i_skip_idct = p_sys->p_context->skip_idct;
277 /* ***** libavcodec direct rendering ***** */
278 p_sys->b_direct_rendering = false;
279 p_sys->i_direct_rendering_used = -1;
280 if( var_CreateGetBool( p_dec, "avcodec-dr" ) &&
281 (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
282 /* No idea why ... but this fixes flickering on some TSCC streams */
283 p_sys->i_codec_id != AV_CODEC_ID_TSCC && p_sys->i_codec_id != AV_CODEC_ID_CSCD &&
284 p_sys->i_codec_id != AV_CODEC_ID_CINEPAK &&
285 !p_sys->p_context->debug_mv )
287 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
288 * so we need to do another check in ffmpeg_GetFrameBuf() */
289 p_sys->b_direct_rendering = true;
292 /* libavcodec doesn't properly release old pictures when frames are skipped */
293 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = false;
294 if( p_sys->b_direct_rendering )
296 msg_Dbg( p_dec, "trying to use direct rendering" );
297 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
299 else
301 msg_Dbg( p_dec, "direct rendering is disabled" );
304 p_sys->p_context->get_format = ffmpeg_GetFormat;
305 /* Always use our get_buffer wrapper so we can calculate the
306 * PTS correctly */
307 #if LIBAVCODEC_VERSION_MAJOR >= 55
308 p_sys->p_context->get_buffer2 = lavc_GetFrame;
309 #else
310 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
311 p_sys->p_context->reget_buffer = avcodec_default_reget_buffer;
312 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
313 #endif
314 p_sys->p_context->opaque = p_dec;
316 #ifdef HAVE_AVCODEC_MT
317 int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
318 if( i_thread_count <= 0 )
320 i_thread_count = vlc_GetCPUCount();
321 if( i_thread_count > 1 )
322 i_thread_count++;
324 //FIXME: take in count the decoding time
325 i_thread_count = __MIN( i_thread_count, 4 );
327 i_thread_count = __MIN( i_thread_count, 16 );
328 msg_Dbg( p_dec, "allowing %d thread(s) for decoding", i_thread_count );
329 p_sys->p_context->thread_count = i_thread_count;
330 p_sys->p_context->thread_safe_callbacks = true;
332 switch( i_codec_id )
334 case AV_CODEC_ID_MPEG4:
335 case AV_CODEC_ID_H263:
336 p_sys->p_context->thread_type = 0;
337 break;
338 case AV_CODEC_ID_MPEG1VIDEO:
339 case AV_CODEC_ID_MPEG2VIDEO:
340 p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
341 /* fall through */
342 # if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 1, 0))
343 case AV_CODEC_ID_H264:
344 case AV_CODEC_ID_VC1:
345 case AV_CODEC_ID_WMV3:
346 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
347 # endif
350 /* Workaround: frame multithreading is not compatible with
351 * DXVA2. When a frame is being copied to host memory, the frame
352 * is locked and cannot be used as a reference frame
353 * simultaneously and thus decoding fails for some frames. This
354 * causes major image corruption. */
355 # if defined(_WIN32)
356 char *avcodec_hw = var_InheritString( p_dec, "avcodec-hw" );
357 if( avcodec_hw == NULL || strcasecmp( avcodec_hw, "none" ) )
359 msg_Warn( p_dec, "threaded frame decoding is not compatible with DXVA2, disabled" );
360 p_sys->p_context->thread_type &= ~FF_THREAD_FRAME;
362 free( avcodec_hw );
363 # endif
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;
367 #endif
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 p_dec->fmt_out.video.orientation = p_dec->fmt_in.video.orientation;
387 if( p_dec->fmt_in.video.p_palette ) {
388 p_sys->palette_sent = false;
389 p_dec->fmt_out.video.p_palette = malloc( sizeof(video_palette_t) );
390 if( p_dec->fmt_out.video.p_palette )
391 *p_dec->fmt_out.video.p_palette = *p_dec->fmt_in.video.p_palette;
392 } else
393 p_sys->palette_sent = true;
395 /* ***** init this codec with special data ***** */
396 ffmpeg_InitCodec( p_dec );
398 /* ***** Open the codec ***** */
399 if( ffmpeg_OpenCodec( p_dec ) < 0 )
401 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
402 avcodec_free_frame( &p_sys->p_ff_pic );
403 vlc_sem_destroy( &p_sys->sem_mt );
404 free( p_sys );
405 return VLC_EGENERIC;
408 return VLC_SUCCESS;
411 /*****************************************************************************
412 * DecodeVideo: Called to decode one or more frames
413 *****************************************************************************/
414 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
416 decoder_sys_t *p_sys = p_dec->p_sys;
417 AVCodecContext *p_context = p_sys->p_context;
418 int b_drawpicture;
419 block_t *p_block;
421 if( !pp_block )
422 return NULL;
424 if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
426 ffmpeg_InitCodec( p_dec );
427 if( p_sys->b_delayed_open )
429 if( ffmpeg_OpenCodec( p_dec ) )
430 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
434 p_block = *pp_block;
435 if(!p_block && !(p_sys->p_codec->capabilities & CODEC_CAP_DELAY) )
436 return NULL;
438 if( p_sys->b_delayed_open )
440 if( p_block )
441 block_Release( p_block );
442 return NULL;
445 if( p_block)
447 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
449 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
451 p_sys->i_late_frames = 0;
453 post_mt( p_sys );
454 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
455 avcodec_flush_buffers( p_context );
456 wait_mt( p_sys );
458 block_Release( p_block );
459 return NULL;
462 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
464 /* Do not care about late frames when prerolling
465 * TODO avoid decoding of non reference frame
466 * (ie all B except for H264 where it depends only on nal_ref_idc) */
467 p_sys->i_late_frames = 0;
471 if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
472 (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
474 if( p_sys->i_pts > VLC_TS_INVALID )
476 p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
478 if( p_block )
479 block_Release( p_block );
480 p_sys->i_late_frames--;
481 msg_Err( p_dec, "more than 5 seconds of late video -> "
482 "dropping frame (computer too slow ?)" );
483 return NULL;
486 /* A good idea could be to decode all I pictures and see for the other */
487 if( !p_dec->b_pace_control &&
488 p_sys->b_hurry_up &&
489 (p_sys->i_late_frames > 4) )
491 b_drawpicture = 0;
492 if( p_sys->i_late_frames < 12 )
494 p_context->skip_frame =
495 (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
496 AVDISCARD_NONREF : p_sys->i_skip_frame;
498 else
500 /* picture too late, won't decode
501 * but break picture until a new I, and for mpeg4 ...*/
502 p_sys->i_late_frames--; /* needed else it will never be decrease */
503 if( p_block )
504 block_Release( p_block );
505 msg_Warn( p_dec, "More than 4 late frames, dropping frame" );
506 return NULL;
509 else
511 if( p_sys->b_hurry_up )
512 p_context->skip_frame = p_sys->i_skip_frame;
513 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
514 b_drawpicture = 1;
515 else
516 b_drawpicture = 0;
519 if( p_context->width <= 0 || p_context->height <= 0 )
521 if( p_sys->b_hurry_up )
522 p_context->skip_frame = p_sys->i_skip_frame;
524 else if( !b_drawpicture )
526 /* It creates broken picture
527 * FIXME either our parser or ffmpeg is broken */
528 #if 0
529 if( p_sys->b_hurry_up )
530 p_context->skip_frame = __MAX( p_context->skip_frame,
531 AVDISCARD_NONREF );
532 #endif
536 * Do the actual decoding now */
538 /* Don't forget that libavcodec requires a little more bytes
539 * that the real frame size */
540 if( p_block && p_block->i_buffer > 0 )
542 p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
544 p_block = block_Realloc( p_block, 0,
545 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
546 if( !p_block )
547 return NULL;
548 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
549 *pp_block = p_block;
550 memset( p_block->p_buffer + p_block->i_buffer, 0,
551 FF_INPUT_BUFFER_PADDING_SIZE );
554 while( !p_block || p_block->i_buffer > 0 || p_sys->b_flush )
556 int i_used, b_gotpicture;
557 picture_t *p_pic;
558 AVPacket pkt;
560 post_mt( p_sys );
562 av_init_packet( &pkt );
563 if( p_block )
565 pkt.data = p_block->p_buffer;
566 pkt.size = p_block->i_buffer;
567 pkt.pts = p_block->i_pts;
568 pkt.dts = p_block->i_dts;
570 else
572 /* Return delayed frames if codec has CODEC_CAP_DELAY */
573 pkt.data = NULL;
574 pkt.size = 0;
577 if( !p_sys->palette_sent )
579 uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
580 if (pal) {
581 memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE);
582 p_sys->palette_sent = true;
586 /* Make sure we don't reuse the same timestamps twice */
587 if( p_block )
589 p_block->i_pts =
590 p_block->i_dts = VLC_TS_INVALID;
593 i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
594 &b_gotpicture, &pkt );
595 av_free_packet( &pkt );
597 wait_mt( p_sys );
599 if( p_sys->b_flush )
600 p_sys->b_first_frame = true;
602 if( p_block )
604 if( p_block->i_buffer <= 0 )
605 p_sys->b_flush = false;
607 if( i_used < 0 )
609 if( b_drawpicture )
610 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
611 p_block->i_buffer );
612 block_Release( p_block );
613 return NULL;
615 else if( (unsigned)i_used > p_block->i_buffer ||
616 p_context->thread_count > 1 )
618 i_used = p_block->i_buffer;
621 /* Consumed bytes */
622 p_block->i_buffer -= i_used;
623 p_block->p_buffer += i_used;
626 /* Nothing to display */
627 if( !b_gotpicture )
629 if( i_used == 0 ) break;
630 continue;
633 /* Sanity check (seems to be needed for some streams) */
634 if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
636 p_sys->b_has_b_frames = true;
639 /* Compute the PTS */
640 mtime_t i_pts =
641 p_sys->p_ff_pic->pkt_pts;
642 if (i_pts <= VLC_TS_INVALID)
643 i_pts = p_sys->p_ff_pic->pkt_dts;
645 if( i_pts <= VLC_TS_INVALID )
646 i_pts = p_sys->i_pts;
648 /* Interpolate the next PTS */
649 if( i_pts > VLC_TS_INVALID )
650 p_sys->i_pts = i_pts;
651 if( p_sys->i_pts > VLC_TS_INVALID )
653 /* interpolate the next PTS */
654 if( p_dec->fmt_in.video.i_frame_rate > 0 &&
655 p_dec->fmt_in.video.i_frame_rate_base > 0 )
657 p_sys->i_pts += CLOCK_FREQ *
658 (2 + p_sys->p_ff_pic->repeat_pict) *
659 p_dec->fmt_in.video.i_frame_rate_base /
660 (2 * p_dec->fmt_in.video.i_frame_rate);
662 else if( p_context->time_base.den > 0 )
664 int i_tick = p_context->ticks_per_frame;
665 if( i_tick <= 0 )
666 i_tick = 1;
668 p_sys->i_pts += CLOCK_FREQ *
669 (2 + p_sys->p_ff_pic->repeat_pict) *
670 i_tick * p_context->time_base.num /
671 (2 * p_context->time_base.den);
675 /* Update frame late count (except when doing preroll) */
676 mtime_t i_display_date = 0;
677 if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
678 i_display_date = decoder_GetDisplayDate( p_dec, i_pts );
680 if( i_display_date > 0 && i_display_date <= mdate() )
682 p_sys->i_late_frames++;
683 if( p_sys->i_late_frames == 1 )
684 p_sys->i_late_frames_start = mdate();
686 else
688 p_sys->i_late_frames = 0;
691 if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
692 continue;
694 if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
696 /* Get a new picture */
697 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
698 if( !p_pic )
700 if( p_block )
701 block_Release( p_block );
702 return NULL;
705 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
706 * if needed */
707 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
709 else
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)
733 p_pic->date = i_pts;
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;
746 return p_pic;
748 else
750 decoder_DeletePicture( p_dec, p_pic );
754 if( p_block )
755 block_Release( p_block );
756 return NULL;
759 /*****************************************************************************
760 * EndVideo: decoder destruction
761 *****************************************************************************
762 * This function is called when the thread ends after a successful
763 * initialization.
764 *****************************************************************************/
765 void EndVideoDec( decoder_t *p_dec )
767 decoder_sys_t *p_sys = p_dec->p_sys;
769 post_mt( p_sys );
771 /* do not flush buffers if codec hasn't been opened (theora/vorbis/VC1) */
772 if( p_sys->p_context->codec )
773 avcodec_flush_buffers( p_sys->p_context );
775 wait_mt( p_sys );
777 if( p_sys->p_ff_pic )
778 avcodec_free_frame( &p_sys->p_ff_pic );
780 if( p_sys->p_va )
781 vlc_va_Delete( p_sys->p_va );
783 vlc_sem_destroy( &p_sys->sem_mt );
786 /*****************************************************************************
787 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
788 *****************************************************************************/
789 static void ffmpeg_InitCodec( decoder_t *p_dec )
791 decoder_sys_t *p_sys = p_dec->p_sys;
792 int i_size = p_dec->fmt_in.i_extra;
794 if( !i_size ) return;
796 if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
798 uint8_t *p;
800 p_sys->p_context->extradata_size = i_size + 12;
801 p = p_sys->p_context->extradata =
802 av_malloc( p_sys->p_context->extradata_size +
803 FF_INPUT_BUFFER_PADDING_SIZE );
804 if( !p )
805 return;
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 );
820 if( i_size <= 1 )
822 /* FIXME handle 1 as long size */
823 break;
825 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
827 memmove( &p[0x52], psz,
828 &p[p_sys->p_context->extradata_size] - psz );
829 break;
832 psz += i_size;
836 else
838 p_sys->p_context->extradata_size = i_size;
839 p_sys->p_context->extradata =
840 av_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( 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;
860 if( p_sys->p_va )
862 vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic->opaque,
863 p_ff_pic->data[3] );
865 else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
867 int i_plane, i_size, i_line;
868 uint8_t *p_dst, *p_src;
869 int i_src_stride, i_dst_stride;
871 if( p_sys->p_context->pix_fmt == PIX_FMT_PAL8 )
873 if( !p_pic->format.p_palette )
874 p_pic->format.p_palette = calloc( 1, sizeof(video_palette_t) );
876 if( p_pic->format.p_palette )
878 p_pic->format.p_palette->i_entries = AVPALETTE_COUNT;
879 memcpy( p_pic->format.p_palette->palette, p_ff_pic->data[1], AVPALETTE_SIZE );
883 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
885 p_src = p_ff_pic->data[i_plane];
886 p_dst = p_pic->p[i_plane].p_pixels;
887 i_src_stride = p_ff_pic->linesize[i_plane];
888 i_dst_stride = p_pic->p[i_plane].i_pitch;
890 i_size = __MIN( i_src_stride, i_dst_stride );
891 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
892 i_line++ )
894 memcpy( p_dst, p_src, i_size );
895 p_src += i_src_stride;
896 p_dst += i_dst_stride;
900 else
902 const char *name = av_get_pix_fmt_name( p_sys->p_context->pix_fmt );
903 msg_Err( p_dec, "Unsupported decoded output format %d (%s)",
904 p_sys->p_context->pix_fmt, name ? name : "unknown" );
905 p_dec->b_error = 1;
909 #if LIBAVCODEC_VERSION_MAJOR >= 55
910 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
911 int flags)
913 decoder_t *dec = ctx->opaque;
914 decoder_sys_t *sys = dec->p_sys;
915 vlc_va_t *va = sys->p_va;
917 if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
918 ctx->coded_width, ctx->coded_height))
920 msg_Err(dec, "hardware acceleration setup failed");
921 return -1;
923 if (vlc_va_Get(va, &frame->opaque, &frame->data[0]))
925 msg_Err(dec, "hardware acceleration picture allocation failed");
926 return -1;
928 /* data[0] must be non-NULL for libavcodec internal checks.
929 * data[3] actually contains the format-specific surface handle. */
930 frame->data[3] = frame->data[0];
932 frame->buf[0] = av_buffer_create(frame->data[0], 0, va->release,
933 frame->opaque, 0);
934 if (unlikely(frame->buf[0] == NULL))
936 vlc_va_Release(va, frame->opaque, frame->data[0]);
937 return -1;
939 assert(frame->data[0] != NULL);
940 (void) flags;
941 return 0;
944 typedef struct
946 decoder_t *decoder;
947 picture_t *picture;
948 } lavc_pic_ref_t;
950 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
952 lavc_pic_ref_t *ref = opaque;
954 decoder_UnlinkPicture(ref->decoder, ref->picture);
955 free(ref);
956 (void) data;
959 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
960 AVFrame *frame, int flags)
962 decoder_t *dec = (decoder_t *)ctx->opaque;
963 decoder_sys_t *sys = dec->p_sys;
965 if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
966 return NULL;
967 dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
968 if (ctx->pix_fmt == PIX_FMT_PAL8)
969 return NULL;
971 int width = frame->width;
972 int height = frame->height;
973 int aligns[AV_NUM_DATA_POINTERS];
975 avcodec_align_dimensions2(ctx, &width, &height, aligns);
977 picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
978 if (pic == NULL)
979 return NULL;
981 /* Check that the picture is suitable for libavcodec */
982 if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
984 if (sys->i_direct_rendering_used != 0)
985 msg_Dbg(dec, "plane 0: pitch too small (%d/%d*%d)",
986 pic->p[0].i_pitch, width, pic->p[0].i_pixel_pitch);
987 goto no_dr;
990 if (pic->p[0].i_lines < height)
992 if (sys->i_direct_rendering_used != 0)
993 msg_Dbg(dec, "plane 0: lines too few (%d/%d)",
994 pic->p[0].i_lines, height);
995 goto no_dr;
998 for (int i = 0; i < pic->i_planes; i++)
1000 if (pic->p[i].i_pitch % aligns[i])
1002 if (sys->i_direct_rendering_used != 0)
1003 msg_Dbg(dec, "plane %d: pitch not aligned (%d%%%d)",
1004 i, pic->p[i].i_pitch, aligns[i]);
1005 goto no_dr;
1007 if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1009 if (sys->i_direct_rendering_used != 0)
1010 msg_Warn(dec, "plane %d not aligned", i);
1011 goto no_dr;
1015 /* Allocate buffer references */
1016 for (int i = 0; i < pic->i_planes; i++)
1018 lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1019 if (ref == NULL)
1020 goto error;
1021 ref->decoder = dec;
1022 ref->picture = pic;
1023 decoder_LinkPicture(dec, pic);
1025 uint8_t *data = pic->p[i].p_pixels;
1026 int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1028 frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1029 ref, 0);
1030 if (unlikely(frame->buf[i] == NULL))
1032 lavc_dr_ReleaseFrame(ref, data);
1033 goto error;
1036 decoder_UnlinkPicture(dec, pic);
1037 (void) flags;
1038 return pic;
1039 error:
1040 for (unsigned i = 0; frame->buf[i] != NULL; i++)
1041 av_buffer_unref(&frame->buf[i]);
1042 no_dr:
1043 decoder_DeletePicture(dec, pic);
1044 return NULL;
1048 * Callback used by libavcodec to get a frame buffer.
1050 * It is used for direct rendering as well as to get the right PTS for each
1051 * decoded picture (even in indirect rendering mode).
1053 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1055 decoder_t *dec = ctx->opaque;
1056 decoder_sys_t *sys = dec->p_sys;
1057 picture_t *pic;
1059 for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1061 frame->data[i] = NULL;
1062 frame->linesize[i] = 0;
1063 frame->buf[i] = NULL;
1066 if (sys->p_va != NULL)
1067 return lavc_va_GetFrame(ctx, frame, flags);
1069 frame->opaque = NULL;
1070 if (!sys->b_direct_rendering)
1071 return avcodec_default_get_buffer2(ctx, frame, flags);
1073 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1074 * so we need to check for direct rendering again. */
1075 wait_mt(sys);
1076 pic = lavc_dr_GetFrame(ctx, frame, flags);
1077 if (pic == NULL)
1079 if (sys->i_direct_rendering_used != 0)
1081 msg_Warn(dec, "disabling direct rendering");
1082 sys->i_direct_rendering_used = 0;
1084 post_mt(sys);
1085 return avcodec_default_get_buffer2(ctx, frame, flags);
1088 if (sys->i_direct_rendering_used != 1)
1090 msg_Dbg(dec, "enabling direct rendering");
1091 sys->i_direct_rendering_used = 1;
1093 post_mt(sys);
1095 frame->opaque = pic;
1096 static_assert(PICTURE_PLANE_MAX <= AV_NUM_DATA_POINTERS, "Oops!");
1097 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++)
1099 frame->data[i] = pic->p[i].p_pixels;
1100 frame->linesize[i] = pic->p[i].i_pitch;
1102 return 0;
1104 #else
1105 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1107 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1108 decoder_sys_t *p_sys = p_dec->p_sys;
1109 vlc_va_t *p_va = p_sys->p_va;
1111 /* hwaccel_context is not present in old ffmpeg version */
1112 if( vlc_va_Setup( p_va,
1113 &p_context->hwaccel_context, &p_dec->fmt_out.video.i_chroma,
1114 p_context->coded_width, p_context->coded_height ) )
1116 msg_Err( p_dec, "vlc_va_Setup failed" );
1117 return -1;
1120 if( vlc_va_Get( p_va, &p_ff_pic->opaque, &p_ff_pic->data[0] ) )
1122 msg_Err( p_dec, "vlc_va_Get failed" );
1123 return -1;
1126 p_ff_pic->data[3] = p_ff_pic->data[0];
1127 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1128 return 0;
1131 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1133 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1134 decoder_sys_t *p_sys = p_dec->p_sys;
1136 int i_width = p_context->width;
1137 int i_height = p_context->height;
1138 avcodec_align_dimensions( p_context, &i_width, &i_height );
1140 picture_t *p_pic = NULL;
1141 if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1142 goto no_dr;
1144 if (p_context->pix_fmt == PIX_FMT_PAL8)
1145 goto no_dr;
1147 p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1149 p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1150 if( !p_pic )
1151 goto no_dr;
1153 if( p_pic->p[0].i_pitch / p_pic->p[0].i_pixel_pitch < i_width ||
1154 p_pic->p[0].i_lines < i_height )
1155 goto no_dr;
1157 for( int i = 0; i < p_pic->i_planes; i++ )
1159 unsigned i_align;
1160 switch( p_sys->i_codec_id )
1162 case AV_CODEC_ID_SVQ1:
1163 case AV_CODEC_ID_VP5:
1164 case AV_CODEC_ID_VP6:
1165 case AV_CODEC_ID_VP6F:
1166 case AV_CODEC_ID_VP6A:
1167 i_align = 16;
1168 break;
1169 default:
1170 i_align = i == 0 ? 16 : 8;
1171 break;
1173 if( p_pic->p[i].i_pitch % i_align )
1174 goto no_dr;
1175 if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1176 goto no_dr;
1179 if( p_context->pix_fmt == PIX_FMT_YUV422P )
1181 if( 2 * p_pic->p[1].i_pitch != p_pic->p[0].i_pitch ||
1182 2 * p_pic->p[2].i_pitch != p_pic->p[0].i_pitch )
1183 goto no_dr;
1186 return p_pic;
1188 no_dr:
1189 if (p_pic)
1190 decoder_DeletePicture( p_dec, p_pic );
1192 return NULL;
1195 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
1196 AVFrame *p_ff_pic )
1198 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1199 decoder_sys_t *p_sys = p_dec->p_sys;
1201 /* */
1202 p_ff_pic->opaque = NULL;
1203 #if ! LIBAVCODEC_VERSION_CHECK(54, 34, 0, 79, 101)
1204 p_ff_pic->pkt_pts = p_context->pkt ? p_context->pkt->pts : AV_NOPTS_VALUE;
1205 #endif
1207 if( p_sys->p_va )
1208 return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1210 if( !p_sys->b_direct_rendering )
1211 return avcodec_default_get_buffer( p_context, p_ff_pic );
1213 wait_mt( p_sys );
1214 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1215 * so we need to check for direct rendering again. */
1217 picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1218 if (!p_pic) {
1219 if( p_sys->i_direct_rendering_used != 0 )
1221 msg_Warn( p_dec, "disabling direct rendering" );
1222 p_sys->i_direct_rendering_used = 0;
1225 post_mt( p_sys );
1226 return avcodec_default_get_buffer( p_context, p_ff_pic );
1229 if( p_sys->i_direct_rendering_used != 1 ) {
1230 msg_Dbg( p_dec, "using direct rendering" );
1231 p_sys->i_direct_rendering_used = 1;
1234 p_context->draw_horiz_band = NULL;
1235 post_mt( p_sys );
1237 p_ff_pic->opaque = (void*)p_pic;
1238 p_ff_pic->type = FF_BUFFER_TYPE_USER;
1239 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
1240 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
1241 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
1242 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
1244 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
1245 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
1246 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
1247 p_ff_pic->linesize[3] = 0;
1249 return 0;
1252 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1253 AVFrame *p_ff_pic )
1255 decoder_t *p_dec = (decoder_t *)p_context->opaque;
1256 decoder_sys_t *p_sys = p_dec->p_sys;
1258 if( p_sys->p_va )
1259 vlc_va_Release( p_sys->p_va, p_ff_pic->opaque, p_ff_pic->data[0] );
1260 else if( p_ff_pic->opaque )
1261 decoder_UnlinkPicture( p_dec, (picture_t*)p_ff_pic->opaque);
1262 else if( p_ff_pic->type == FF_BUFFER_TYPE_INTERNAL )
1263 /* We can end up here without the AVFrame being allocated by
1264 * avcodec_default_get_buffer() if VA is used and the frame is
1265 * released when the decoder is closed
1267 avcodec_default_release_buffer( p_context, p_ff_pic );
1269 for( int i = 0; i < 4; i++ )
1270 p_ff_pic->data[i] = NULL;
1272 #endif
1274 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1275 const enum PixelFormat *pi_fmt )
1277 decoder_t *p_dec = p_context->opaque;
1278 decoder_sys_t *p_sys = p_dec->p_sys;
1279 vlc_va_t *p_va = p_sys->p_va;
1281 if( p_va != NULL )
1282 vlc_va_Delete( p_va );
1284 /* Enumerate available formats */
1285 bool can_hwaccel = false;
1286 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1288 const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1289 if (dsc == NULL)
1290 continue;
1291 bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1293 msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1294 hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1295 if (hwaccel)
1296 can_hwaccel = true;
1299 if (!can_hwaccel)
1300 goto end;
1302 /* Profile and level information is needed now.
1303 * TODO: avoid code duplication with avcodec.c */
1304 if( p_context->profile != FF_PROFILE_UNKNOWN)
1305 p_dec->fmt_in.i_profile = p_context->profile;
1306 if( p_context->level != FF_LEVEL_UNKNOWN)
1307 p_dec->fmt_in.i_level = p_context->level;
1309 p_va = vlc_va_New( VLC_OBJECT(p_dec), p_context, &p_dec->fmt_in );
1310 if( p_va == NULL )
1311 goto end;
1313 for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1315 if( p_va->pix_fmt != pi_fmt[i] )
1316 continue;
1318 /* We try to call vlc_va_Setup when possible to detect errors when
1319 * possible (later is too late) */
1320 if( p_context->width > 0 && p_context->height > 0
1321 && vlc_va_Setup( p_va, &p_context->hwaccel_context,
1322 &p_dec->fmt_out.video.i_chroma,
1323 p_context->width, p_context->height ) )
1325 msg_Err( p_dec, "acceleration setup failure" );
1326 break;
1329 if( p_va->description )
1330 msg_Info( p_dec, "Using %s for hardware decoding.",
1331 p_va->description );
1333 /* FIXME this will disable direct rendering
1334 * even if a new pixel format is renegotiated
1336 p_sys->b_direct_rendering = false;
1337 p_sys->p_va = p_va;
1338 p_context->draw_horiz_band = NULL;
1339 return pi_fmt[i];
1342 vlc_va_Delete( p_va );
1344 end:
1345 /* Fallback to default behaviour */
1346 p_sys->p_va = NULL;
1347 return avcodec_default_get_format( p_context, pi_fmt );