]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/video.c
hwdummy: remove no-op
[vlc] / modules / codec / avcodec / video.c
1 /*****************************************************************************
2  * video.c: video decoder using the libavcodec library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_avcodec.h>
35 #include <vlc_cpu.h>
36 #include <assert.h>
37
38 #include <libavcodec/avcodec.h>
39 #include <libavutil/mem.h>
40 #include <libavutil/pixdesc.h>
41
42 #include "avcodec.h"
43 #include "va.h"
44
45 /*****************************************************************************
46  * decoder_sys_t : decoder descriptor
47  *****************************************************************************/
48 struct decoder_sys_t
49 {
50     AVCODEC_COMMON_MEMBERS
51
52     /* Video decoder specific part */
53     mtime_t i_pts;
54
55     AVFrame          *p_ff_pic;
56
57     /* for frame skipping algo */
58     bool b_hurry_up;
59     enum AVDiscard i_skip_frame;
60     enum AVDiscard i_skip_idct;
61
62     /* how many decoded frames are late */
63     int     i_late_frames;
64     mtime_t i_late_frames_start;
65
66     /* for direct rendering */
67     bool b_direct_rendering;
68     int  i_direct_rendering_used;
69
70     bool b_has_b_frames;
71
72     /* Hack to force display of still pictures */
73     bool b_first_frame;
74
75
76     /* */
77 #if LIBAVCODEC_VERSION_MAJOR < 54
78     AVPaletteControl palette;
79 #else
80 # warning FIXME
81 #endif
82
83     /* */
84     bool b_flush;
85
86     /* VA API */
87     vlc_va_t *p_va;
88
89     vlc_sem_t sem_mt;
90 };
91
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 )
95 #else
96 #   define wait_mt(s)
97 #   define post_mt(s)
98 #endif
99
100 /*****************************************************************************
101  * Local prototypes
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);
107 #else
108 static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
109 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
110 #endif
111 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *,
112                                           const enum PixelFormat * );
113
114 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
115 {
116     uint8_t *p = (uint8_t*)&fcc;
117     return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
118 }
119
120 /*****************************************************************************
121  * Local Functions
122  *****************************************************************************/
123
124 /* Returns a new picture buffer */
125 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
126                                             AVCodecContext *p_context )
127 {
128     decoder_sys_t *p_sys = p_dec->p_sys;
129
130     if( p_context->coded_width != p_context->width ||
131         p_context->coded_height != p_context->height )
132     {
133         p_dec->fmt_out.video.i_visible_width = p_context->width;
134         p_dec->fmt_out.video.i_visible_height = p_context->height;
135     }
136     p_dec->fmt_out.video.i_width = p_context->coded_width;
137     p_dec->fmt_out.video.i_height = p_context->coded_height;
138
139     if( !p_context->width || !p_context->height )
140     {
141         return NULL; /* invalid display size */
142     }
143
144     if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
145     {
146         /* we are doomed, but not really, because most codecs set their pix_fmt
147          * much later
148          * FIXME does it make sense here ? */
149         p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
150     }
151     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
152
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 )
155     {
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;
158     }
159     else
160     {
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;
163
164         if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
165         {
166             p_dec->fmt_out.video.i_sar_num = 1;
167             p_dec->fmt_out.video.i_sar_den = 1;
168         }
169     }
170
171     if( p_dec->fmt_in.video.i_frame_rate > 0 &&
172         p_dec->fmt_in.video.i_frame_rate_base > 0 )
173     {
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;
178     }
179     else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
180     {
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 );
183     }
184
185     return decoder_NewPicture( p_dec );
186 }
187
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 )
196 {
197     decoder_sys_t *p_sys;
198     int i_val;
199
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 )
202         return VLC_ENOMEM;
203
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;
213     p_sys->p_va = NULL;
214     vlc_sem_init( &p_sys->sem_mt, 0 );
215
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 );
218
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" );
224
225     if( var_CreateGetBool( p_dec, "grayscale" ) )
226         p_sys->p_context->flags |= CODEC_FLAG_GRAY;
227
228     i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
229     if( i_val ) p_sys->p_context->debug_mv = i_val;
230
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;
236
237     if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
238         p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
239
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;
243 #endif
244
245     /* ***** libavcodec frame skipping ***** */
246     p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
247
248     switch( var_CreateGetInteger( p_dec, "avcodec-skip-frame" ) )
249     {
250         case -1:
251             p_sys->p_context->skip_frame = AVDISCARD_NONE;
252             break;
253         case 0:
254             p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
255             break;
256         case 1:
257             p_sys->p_context->skip_frame = AVDISCARD_NONREF;
258             break;
259         case 2:
260             p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
261             break;
262         case 3:
263             p_sys->p_context->skip_frame = AVDISCARD_ALL;
264             break;
265         default:
266             p_sys->p_context->skip_frame = AVDISCARD_NONE;
267             break;
268     }
269     p_sys->i_skip_frame = p_sys->p_context->skip_frame;
270
271     switch( var_CreateGetInteger( p_dec, "avcodec-skip-idct" ) )
272     {
273         case -1:
274             p_sys->p_context->skip_idct = AVDISCARD_NONE;
275             break;
276         case 0:
277             p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
278             break;
279         case 1:
280             p_sys->p_context->skip_idct = AVDISCARD_NONREF;
281             break;
282         case 2:
283             p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
284             break;
285         case 3:
286             p_sys->p_context->skip_idct = AVDISCARD_ALL;
287             break;
288         default:
289             p_sys->p_context->skip_idct = AVDISCARD_NONE;
290             break;
291     }
292     p_sys->i_skip_idct = p_sys->p_context->skip_idct;
293
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 )
303     {
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;
307     }
308
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 )
312     {
313         msg_Dbg( p_dec, "trying to use direct rendering" );
314         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
315     }
316     else
317     {
318         msg_Dbg( p_dec, "direct rendering is disabled" );
319     }
320
321     p_sys->p_context->get_format = ffmpeg_GetFormat;
322     /* Always use our get_buffer wrapper so we can calculate the
323      * PTS correctly */
324 #if LIBAVCODEC_VERSION_MAJOR >= 55
325     p_sys->p_context->get_buffer2 = lavc_GetFrame;
326 #else
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;
330 #endif
331     p_sys->p_context->opaque = p_dec;
332
333 #ifdef HAVE_AVCODEC_MT
334     int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
335     if( i_thread_count <= 0 )
336     {
337         i_thread_count = vlc_GetCPUCount();
338         if( i_thread_count > 1 )
339             i_thread_count++;
340
341         //FIXME: take in count the decoding time
342         i_thread_count = __MIN( i_thread_count, 4 );
343     }
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;
348
349     switch( i_codec_id )
350     {
351         case AV_CODEC_ID_MPEG4:
352         case AV_CODEC_ID_H263:
353             p_sys->p_context->thread_type = 0;
354             break;
355         case AV_CODEC_ID_MPEG1VIDEO:
356         case AV_CODEC_ID_MPEG2VIDEO:
357             p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
358             /* fall through */
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;
364 # endif
365     }
366
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;
369 #endif
370
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;
377
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 )
381     {
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;
384     }
385     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
386
387 #if LIBAVCODEC_VERSION_MAJOR < 54
388     /* Setup palette */
389     memset( &p_sys->palette, 0, sizeof(p_sys->palette) );
390     if( p_dec->fmt_in.video.p_palette )
391     {
392         p_sys->palette.palette_changed = 1;
393
394         for( int i = 0; i < __MIN( AVPALETTE_COUNT, p_dec->fmt_in.video.p_palette->i_entries ); i++ )
395         {
396             union {
397                 uint32_t u;
398                 uint8_t a[4];
399             } c;
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];
404
405             p_sys->palette.palette[i] = c.u;
406         }
407         p_sys->p_context->palctrl = &p_sys->palette;
408
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;
412     }
413     else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
414     {
415         p_sys->p_context->palctrl = &p_sys->palette;
416     }
417 #else
418 # warning FIXME
419 #endif
420
421     /* ***** init this codec with special data ***** */
422     ffmpeg_InitCodec( p_dec );
423
424     /* ***** Open the codec ***** */
425     if( ffmpeg_OpenCodec( p_dec ) < 0 )
426     {
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 );
430         free( p_sys );
431         return VLC_EGENERIC;
432     }
433
434     return VLC_SUCCESS;
435 }
436
437 /*****************************************************************************
438  * DecodeVideo: Called to decode one or more frames
439  *****************************************************************************/
440 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
441 {
442     decoder_sys_t *p_sys = p_dec->p_sys;
443     AVCodecContext *p_context = p_sys->p_context;
444     int b_drawpicture;
445     int b_null_size = false;
446     block_t *p_block;
447
448     if( !pp_block || !*pp_block )
449         return NULL;
450
451     if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
452     {
453         ffmpeg_InitCodec( p_dec );
454         if( p_sys->b_delayed_open )
455         {
456             if( ffmpeg_OpenCodec( p_dec ) )
457                 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
458         }
459     }
460
461     p_block = *pp_block;
462     if( p_sys->b_delayed_open )
463     {
464         block_Release( p_block );
465         return NULL;
466     }
467
468     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
469     {
470         p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
471
472         p_sys->i_late_frames = 0;
473
474         post_mt( p_sys );
475         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
476             avcodec_flush_buffers( p_context );
477         wait_mt( p_sys );
478
479         block_Release( p_block );
480         return NULL;
481     }
482
483     if( p_block->i_flags & BLOCK_FLAG_PREROLL )
484     {
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;
489     }
490
491     if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
492         (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
493     {
494         if( p_sys->i_pts > VLC_TS_INVALID )
495         {
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 */
499         }
500         block_Release( p_block );
501         p_sys->i_late_frames--;
502         return NULL;
503     }
504
505     /* A good idea could be to decode all I pictures and see for the other */
506     if( !p_dec->b_pace_control &&
507         p_sys->b_hurry_up &&
508         (p_sys->i_late_frames > 4) )
509     {
510         b_drawpicture = 0;
511         if( p_sys->i_late_frames < 12 )
512         {
513             p_context->skip_frame =
514                     (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
515                     AVDISCARD_NONREF : p_sys->i_skip_frame;
516         }
517         else
518         {
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 );
523             return NULL;
524         }
525     }
526     else
527     {
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) )
531             b_drawpicture = 1;
532         else
533             b_drawpicture = 0;
534     }
535
536     if( p_context->width <= 0 || p_context->height <= 0 )
537     {
538         if( p_sys->b_hurry_up )
539             p_context->skip_frame = p_sys->i_skip_frame;
540         b_null_size = true;
541     }
542     else if( !b_drawpicture )
543     {
544         /* It creates broken picture
545          * FIXME either our parser or ffmpeg is broken */
546 #if 0
547         if( p_sys->b_hurry_up )
548             p_context->skip_frame = __MAX( p_context->skip_frame,
549                                                   AVDISCARD_NONREF );
550 #endif
551     }
552
553     /*
554      * Do the actual decoding now */
555
556     /* Don't forget that libavcodec requires a little more bytes
557      * that the real frame size */
558     if( p_block->i_buffer > 0 )
559     {
560         p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
561
562         p_block = block_Realloc( p_block, 0,
563                             p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
564         if( !p_block )
565             return NULL;
566         p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
567         *pp_block = p_block;
568         memset( p_block->p_buffer + p_block->i_buffer, 0,
569                 FF_INPUT_BUFFER_PADDING_SIZE );
570     }
571
572     while( p_block->i_buffer > 0 || p_sys->b_flush )
573     {
574         int i_used, b_gotpicture;
575         picture_t *p_pic;
576         AVPacket pkt;
577
578         post_mt( p_sys );
579
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;
585
586         /* Make sure we don't reuse the same timestamps twice */
587         p_block->i_pts =
588         p_block->i_dts = VLC_TS_INVALID;
589
590         i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
591                                        &b_gotpicture, &pkt );
592
593         if( b_null_size && !p_sys->b_flush &&
594             p_context->width > 0 && p_context->height > 0 )
595         {
596             /* Reparse it to not drop the I frame */
597             b_null_size = false;
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 );
602         }
603         wait_mt( p_sys );
604
605         if( p_sys->b_flush )
606             p_sys->b_first_frame = true;
607
608         if( p_block->i_buffer <= 0 )
609             p_sys->b_flush = false;
610
611         if( i_used < 0 )
612         {
613             if( b_drawpicture )
614                 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
615                           p_block->i_buffer );
616             block_Release( p_block );
617             return NULL;
618         }
619         else if( (unsigned)i_used > p_block->i_buffer ||
620                  p_context->thread_count > 1 )
621         {
622             i_used = p_block->i_buffer;
623         }
624
625         /* Consumed bytes */
626         p_block->i_buffer -= i_used;
627         p_block->p_buffer += i_used;
628
629         /* Nothing to display */
630         if( !b_gotpicture )
631         {
632             if( i_used == 0 ) break;
633             continue;
634         }
635
636         /* Sanity check (seems to be needed for some streams) */
637         if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
638         {
639             p_sys->b_has_b_frames = true;
640         }
641
642         /* Compute the PTS */
643         mtime_t i_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;
647
648         if( i_pts <= VLC_TS_INVALID )
649             i_pts = p_sys->i_pts;
650
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 )
655         {
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 )
659             {
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);
664             }
665             else if( p_context->time_base.den > 0 )
666             {
667                 int i_tick = p_context->ticks_per_frame;
668                 if( i_tick <= 0 )
669                     i_tick = 1;
670
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);
675             }
676         }
677
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 );
682
683         if( i_display_date > 0 && i_display_date <= mdate() )
684         {
685             p_sys->i_late_frames++;
686             if( p_sys->i_late_frames == 1 )
687                 p_sys->i_late_frames_start = mdate();
688         }
689         else
690         {
691             p_sys->i_late_frames = 0;
692         }
693
694         if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
695             continue;
696
697         if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
698         {
699             /* Get a new picture */
700             p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
701             if( !p_pic )
702             {
703                 block_Release( p_block );
704                 return NULL;
705             }
706
707             /* Fill p_picture_t from AVVideoFrame and do chroma conversion
708              * if needed */
709             ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
710         }
711         else
712         {
713             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
714             decoder_LinkPicture( p_dec, p_pic );
715         }
716
717         if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
718         {
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;
724
725             if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
726             {
727                 p_dec->fmt_out.video.i_sar_num = 1;
728                 p_dec->fmt_out.video.i_sar_den = 1;
729             }
730         }
731
732         /* Send decoded frame to vout */
733         if( i_pts > VLC_TS_INVALID)
734         {
735             p_pic->date = i_pts;
736
737             if( p_sys->b_first_frame )
738             {
739                 /* Hack to force display of still pictures */
740                 p_sys->b_first_frame = false;
741                 p_pic->b_force = true;
742             }
743
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;
747
748             return p_pic;
749         }
750         else
751         {
752             decoder_DeletePicture( p_dec, p_pic );
753         }
754     }
755
756     block_Release( p_block );
757     return NULL;
758 }
759
760 /*****************************************************************************
761  * EndVideo: decoder destruction
762  *****************************************************************************
763  * This function is called when the thread ends after a successful
764  * initialization.
765  *****************************************************************************/
766 void EndVideoDec( decoder_t *p_dec )
767 {
768     decoder_sys_t *p_sys = p_dec->p_sys;
769
770     post_mt( p_sys );
771
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 );
775
776     wait_mt( p_sys );
777
778     if( p_sys->p_ff_pic )
779         av_free( p_sys->p_ff_pic );
780
781     if( p_sys->p_va )
782         vlc_va_Delete( p_sys->p_va );
783
784     vlc_sem_destroy( &p_sys->sem_mt );
785 }
786
787 /*****************************************************************************
788  * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
789  *****************************************************************************/
790 static void ffmpeg_InitCodec( decoder_t *p_dec )
791 {
792     decoder_sys_t *p_sys = p_dec->p_sys;
793     int i_size = p_dec->fmt_in.i_extra;
794
795     if( !i_size ) return;
796
797     if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
798     {
799         uint8_t *p;
800
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 );
804         if( !p )
805             return;
806
807         memcpy( &p[0],  "SVQ3", 4 );
808         memset( &p[4], 0, 8 );
809         memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
810
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 ) )
814         {
815             uint8_t *psz = &p[0x52];
816
817             while( psz < &p[p_sys->p_context->extradata_size - 8] )
818             {
819                 int i_size = GetDWBE( psz );
820                 if( i_size <= 1 )
821                 {
822                     /* FIXME handle 1 as long size */
823                     break;
824                 }
825                 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
826                 {
827                     memmove( &p[0x52], psz,
828                              &p[p_sys->p_context->extradata_size] - psz );
829                     break;
830                 }
831
832                 psz += i_size;
833             }
834         }
835     }
836     else
837     {
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 )
842         {
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 );
847         }
848     }
849 }
850
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 )
857 {
858     decoder_sys_t *p_sys = p_dec->p_sys;
859
860     if( p_sys->p_va )
861     {
862         vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic );
863     }
864     else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
865     {
866         int i_plane, i_size, i_line;
867         uint8_t *p_dst, *p_src;
868         int i_src_stride, i_dst_stride;
869
870         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
871         {
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;
876
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;
879                  i_line++ )
880             {
881                 memcpy( p_dst, p_src, i_size );
882                 p_src += i_src_stride;
883                 p_dst += i_dst_stride;
884             }
885         }
886     }
887     else
888     {
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" );
892         p_dec->b_error = 1;
893     }
894 }
895
896 #if LIBAVCODEC_VERSION_MAJOR >= 55
897 typedef struct
898 {
899     vlc_va_t *va;
900     AVFrame *frame;
901 } lavc_hw_ref_t;
902
903 static void lavc_va_ReleaseFrame(void *opaque, uint8_t *data)
904 {
905     lavc_hw_ref_t *ref = opaque;
906
907     vlc_va_Release(ref->va, ref->frame);
908     free(ref);
909     (void) data;
910 }
911
912 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
913                             int flags)
914 {
915     decoder_t *dec = ctx->opaque;
916     decoder_sys_t *sys = dec->p_sys;
917     vlc_va_t *va = sys->p_va;
918
919     if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
920                      ctx->coded_width, ctx->coded_height))
921     {
922         msg_Err(dec, "hardware acceleration setup failed");
923         return -1;
924     }
925     if (vlc_va_Get(va, frame))
926     {
927         msg_Err(dec, "hardware acceleration picture allocation failed");
928         return -1;
929     }
930
931     lavc_hw_ref_t *ref = malloc(sizeof (*ref));
932     if (unlikely(ref == NULL))
933     {
934         vlc_va_Release(va, frame);
935         return -1;
936     }
937     ref->va = va;
938     ref->frame = frame;
939
940     frame->buf[0] = av_buffer_create(frame->data[0], 0, lavc_va_ReleaseFrame,
941                                      ref, 0);
942     if (unlikely(frame->buf[0] == NULL))
943     {
944         lavc_va_ReleaseFrame(ref, frame->data[0]);
945         return -1;
946     }
947     assert(frame->data[0] != NULL);
948     assert(frame->data[3] != NULL);
949     (void) flags;
950     return 0;
951 }
952
953 typedef struct
954 {
955     decoder_t *decoder;
956     picture_t *picture;
957 } lavc_pic_ref_t;
958
959 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
960 {
961     lavc_pic_ref_t *ref = opaque;
962
963     decoder_UnlinkPicture(ref->decoder, ref->picture);
964     free(ref);
965     (void) data;
966 }
967
968 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
969                                    AVFrame *frame, int flags)
970 {
971     decoder_t *dec = (decoder_t *)ctx->opaque;
972
973     if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
974         return NULL;
975     dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
976     if (ctx->pix_fmt == PIX_FMT_PAL8)
977         return NULL;
978
979     int width = frame->width;
980     int height = frame->height;
981     int aligns[AV_NUM_DATA_POINTERS];
982
983     avcodec_align_dimensions2(ctx, &width, &height, aligns);
984
985     picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
986     if (pic == NULL)
987         return NULL;
988
989     /* Check that the picture is suitable for libavcodec */
990     if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
991     {
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);
994         goto no_dr;
995     }
996
997     if (pic->p[0].i_lines < height)
998     {
999         msg_Dbg(dec, "plane 0: lines too few (%d/%d)",
1000                 pic->p[0].i_lines, height);
1001         goto no_dr;
1002     }
1003
1004     for (int i = 0; i < pic->i_planes; i++)
1005     {
1006         if (pic->p[i].i_pitch % aligns[i])
1007         {
1008             msg_Dbg(dec, "plane %d: pitch not aligned (%d%%%d)",
1009                     i, pic->p[i].i_pitch, aligns[i]);
1010             goto no_dr;
1011         }
1012         if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
1013         {
1014             msg_Warn(dec, "plane %d not aligned", i);
1015             goto no_dr;
1016         }
1017     }
1018
1019     /* Allocate buffer references */
1020     for (int i = 0; i < pic->i_planes; i++)
1021     {
1022         lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1023         if (ref == NULL)
1024             goto error;
1025         ref->decoder = dec;
1026         ref->picture = pic;
1027         decoder_LinkPicture(dec, pic);
1028
1029         uint8_t *data = pic->p[i].p_pixels;
1030         int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1031
1032         frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1033                                          ref, 0);
1034         if (unlikely(frame->buf[i] == NULL))
1035         {
1036             lavc_dr_ReleaseFrame(ref, data);
1037             goto error;
1038         }
1039     }
1040     decoder_UnlinkPicture(dec, pic);
1041     (void) flags;
1042     return pic;
1043 error:
1044     for (unsigned i = 0; frame->buf[i] != NULL; i++)
1045         av_buffer_unref(&frame->buf[i]);
1046 no_dr:
1047     decoder_DeletePicture(dec, pic);
1048     return NULL;
1049 }
1050
1051 /**
1052  * Callback used by libavcodec to get a frame buffer.
1053  *
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).
1056  */
1057 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1058 {
1059     decoder_t *dec = ctx->opaque;
1060     decoder_sys_t *sys = dec->p_sys;
1061     picture_t *pic;
1062
1063     for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1064     {
1065         frame->data[i] = NULL;
1066         frame->linesize[i] = 0;
1067         frame->buf[i] = NULL;
1068     }
1069
1070     if (sys->p_va != NULL)
1071         return lavc_va_GetFrame(ctx, frame, flags);
1072
1073     frame->opaque = NULL;
1074     if (!sys->b_direct_rendering)
1075         return avcodec_default_get_buffer2(ctx, frame, flags);
1076
1077     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1078      * so we need to check for direct rendering again. */
1079     wait_mt(sys);
1080     pic = lavc_dr_GetFrame(ctx, frame, flags);
1081     if (pic == NULL)
1082     {
1083         if (sys->i_direct_rendering_used != 0)
1084         {
1085             msg_Warn(dec, "disabling direct rendering");
1086             sys->i_direct_rendering_used = 0;
1087         }
1088         post_mt(sys);
1089         return avcodec_default_get_buffer2(ctx, frame, flags);
1090     }
1091
1092     if (sys->i_direct_rendering_used != 1)
1093     {
1094         msg_Dbg(dec, "enabling direct rendering");
1095         sys->i_direct_rendering_used = 1;
1096     }
1097     post_mt(sys);
1098
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++)
1102     {
1103         frame->data[i] = pic->p[i].p_pixels;
1104         frame->linesize[i] = pic->p[i].i_pitch;
1105     }
1106     return 0;
1107 }
1108 #else
1109 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1110 {
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;
1114
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 ) )
1119     {
1120         msg_Err( p_dec, "vlc_va_Setup failed" );
1121         return -1;
1122     }
1123
1124     if( vlc_va_Get( p_va, p_ff_pic ) )
1125     {
1126         msg_Err( p_dec, "vlc_va_Get failed" );
1127         return -1;
1128     }
1129
1130     p_ff_pic->type = FF_BUFFER_TYPE_USER;
1131     return 0;
1132 }
1133
1134 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1135 {
1136     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1137     decoder_sys_t *p_sys = p_dec->p_sys;
1138
1139     int i_width = p_context->width;
1140     int i_height = p_context->height;
1141     avcodec_align_dimensions( p_context, &i_width, &i_height );
1142
1143     picture_t *p_pic = NULL;
1144     if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1145         goto no_dr;
1146
1147     if (p_context->pix_fmt == PIX_FMT_PAL8)
1148         goto no_dr;
1149
1150     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1151
1152     p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1153     if( !p_pic )
1154         goto no_dr;
1155
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 )
1158         goto no_dr;
1159
1160     for( int i = 0; i < p_pic->i_planes; i++ )
1161     {
1162         unsigned i_align;
1163         switch( p_sys->i_codec_id )
1164         {
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:
1170             i_align = 16;
1171             break;
1172         default:
1173             i_align = i == 0 ? 16 : 8;
1174             break;
1175         }
1176         if( p_pic->p[i].i_pitch % i_align )
1177             goto no_dr;
1178         if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1179             goto no_dr;
1180     }
1181
1182     if( p_context->pix_fmt == PIX_FMT_YUV422P )
1183     {
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 )
1186             goto no_dr;
1187     }
1188
1189     return p_pic;
1190
1191 no_dr:
1192     if (p_pic)
1193         decoder_DeletePicture( p_dec, p_pic );
1194
1195     return NULL;
1196 }
1197
1198 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
1199                                AVFrame *p_ff_pic )
1200 {
1201     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1202     decoder_sys_t *p_sys = p_dec->p_sys;
1203
1204     /* */
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;
1208 #endif
1209 #if LIBAVCODEC_VERSION_MAJOR < 54
1210     p_ff_pic->age = 256*256*256*64;
1211 #endif
1212
1213     if( p_sys->p_va )
1214         return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1215
1216     if( !p_sys->b_direct_rendering )
1217         return avcodec_default_get_buffer( p_context, p_ff_pic );
1218
1219     wait_mt( p_sys );
1220     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1221      * so we need to check for direct rendering again. */
1222
1223     picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1224     if (!p_pic) {
1225         if( p_sys->i_direct_rendering_used != 0 )
1226         {
1227             msg_Warn( p_dec, "disabling direct rendering" );
1228             p_sys->i_direct_rendering_used = 0;
1229         }
1230
1231         post_mt( p_sys );
1232         return avcodec_default_get_buffer( p_context, p_ff_pic );
1233     }
1234
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;
1238     }
1239
1240     p_context->draw_horiz_band = NULL;
1241     post_mt( p_sys );
1242
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 */
1249
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;
1254
1255     return 0;
1256 }
1257
1258 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1259                                     AVFrame *p_ff_pic )
1260 {
1261     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1262     decoder_sys_t *p_sys = p_dec->p_sys;
1263
1264     if( p_sys->p_va )
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
1272          */
1273         avcodec_default_release_buffer( p_context, p_ff_pic );
1274
1275     for( int i = 0; i < 4; i++ )
1276         p_ff_pic->data[i] = NULL;
1277 }
1278 #endif
1279
1280 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1281                                           const enum PixelFormat *pi_fmt )
1282 {
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;
1286
1287     if( p_va != NULL )
1288         vlc_va_Delete( p_va );
1289
1290     /* Enumerate available formats */
1291     bool can_hwaccel = false;
1292     for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1293     {
1294         const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1295         if (dsc == NULL)
1296             continue;
1297         bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1298
1299         msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1300                  hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1301         if (hwaccel)
1302             can_hwaccel = true;
1303     }
1304
1305     if (!can_hwaccel)
1306         goto end;
1307
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;
1314
1315     p_va = vlc_va_New( VLC_OBJECT(p_dec), p_sys->i_codec_id, &p_dec->fmt_in );
1316     if( p_va == NULL )
1317         goto end;
1318
1319     for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1320     {
1321         if( p_va->pix_fmt != pi_fmt[i] )
1322             continue;
1323
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 ) )
1330         {
1331             msg_Err( p_dec, "acceleration setup failure" );
1332             break;
1333         }
1334
1335         if( p_va->description )
1336             msg_Info( p_dec, "Using %s for hardware decoding.",
1337                       p_va->description );
1338
1339         /* FIXME this will disable direct rendering
1340          * even if a new pixel format is renegotiated
1341          */
1342         p_sys->b_direct_rendering = false;
1343         p_sys->p_va = p_va;
1344         p_context->draw_horiz_band = NULL;
1345         return pi_fmt[i];
1346     }
1347
1348     vlc_va_Delete( p_va );
1349
1350 end:
1351     /* Fallback to default behaviour */
1352     p_sys->p_va = NULL;
1353     return avcodec_default_get_format( p_context, pi_fmt );
1354 }