]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/video.c
avcodec: pass aligned picture dimensions to video output
[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     int aligns[AV_NUM_DATA_POINTERS];
130     int width = p_context->coded_width;
131     int height = p_context->coded_height;
132
133     avcodec_align_dimensions2(p_context, &width, &height, aligns);
134
135     if( width == 0 || height == 0)
136         return NULL; /* invalid display size */
137
138     p_dec->fmt_out.video.i_width = width;
139     p_dec->fmt_out.video.i_height = height;
140
141     if( width != p_context->width || height != p_context->height )
142     {
143         p_dec->fmt_out.video.i_visible_width = p_context->width;
144         p_dec->fmt_out.video.i_visible_height = p_context->height;
145     }
146
147     if( !p_sys->p_va && GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) )
148     {
149         /* we are doomed, but not really, because most codecs set their pix_fmt
150          * much later
151          * FIXME does it make sense here ? */
152         p_dec->fmt_out.video.i_chroma = VLC_CODEC_I420;
153     }
154     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
155
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 )
158     {
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;
161     }
162     else
163     {
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;
166
167         if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
168         {
169             p_dec->fmt_out.video.i_sar_num = 1;
170             p_dec->fmt_out.video.i_sar_den = 1;
171         }
172     }
173
174     if( p_dec->fmt_in.video.i_frame_rate > 0 &&
175         p_dec->fmt_in.video.i_frame_rate_base > 0 )
176     {
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;
181     }
182     else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
183     {
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 );
186     }
187
188     return decoder_NewPicture( p_dec );
189 }
190
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 )
199 {
200     decoder_sys_t *p_sys;
201     int i_val;
202
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 )
205         return VLC_ENOMEM;
206
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;
216     p_sys->p_va = NULL;
217     vlc_sem_init( &p_sys->sem_mt, 0 );
218
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 );
221
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" );
227
228     if( var_CreateGetBool( p_dec, "grayscale" ) )
229         p_sys->p_context->flags |= CODEC_FLAG_GRAY;
230
231     i_val = var_CreateGetInteger( p_dec, "avcodec-vismv" );
232     if( i_val ) p_sys->p_context->debug_mv = i_val;
233
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;
239
240     if( var_CreateGetBool( p_dec, "avcodec-fast" ) )
241         p_sys->p_context->flags2 |= CODEC_FLAG2_FAST;
242
243     /* ***** libavcodec frame skipping ***** */
244     p_sys->b_hurry_up = var_CreateGetBool( p_dec, "avcodec-hurry-up" );
245
246     switch( var_CreateGetInteger( p_dec, "avcodec-skip-frame" ) )
247     {
248         case -1:
249             p_sys->p_context->skip_frame = AVDISCARD_NONE;
250             break;
251         case 0:
252             p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
253             break;
254         case 1:
255             p_sys->p_context->skip_frame = AVDISCARD_NONREF;
256             break;
257         case 2:
258             p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
259             break;
260         case 3:
261             p_sys->p_context->skip_frame = AVDISCARD_ALL;
262             break;
263         default:
264             p_sys->p_context->skip_frame = AVDISCARD_NONE;
265             break;
266     }
267     p_sys->i_skip_frame = p_sys->p_context->skip_frame;
268
269     switch( var_CreateGetInteger( p_dec, "avcodec-skip-idct" ) )
270     {
271         case -1:
272             p_sys->p_context->skip_idct = AVDISCARD_NONE;
273             break;
274         case 0:
275             p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
276             break;
277         case 1:
278             p_sys->p_context->skip_idct = AVDISCARD_NONREF;
279             break;
280         case 2:
281             p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
282             break;
283         case 3:
284             p_sys->p_context->skip_idct = AVDISCARD_ALL;
285             break;
286         default:
287             p_sys->p_context->skip_idct = AVDISCARD_NONE;
288             break;
289     }
290     p_sys->i_skip_idct = p_sys->p_context->skip_idct;
291
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 )
301     {
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;
305     }
306
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 )
310     {
311         msg_Dbg( p_dec, "trying to use direct rendering" );
312         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
313     }
314     else
315     {
316         msg_Dbg( p_dec, "direct rendering is disabled" );
317     }
318
319     p_sys->p_context->get_format = ffmpeg_GetFormat;
320     /* Always use our get_buffer wrapper so we can calculate the
321      * PTS correctly */
322 #if LIBAVCODEC_VERSION_MAJOR >= 55
323     p_sys->p_context->get_buffer2 = lavc_GetFrame;
324 #else
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;
328 #endif
329     p_sys->p_context->opaque = p_dec;
330
331 #ifdef HAVE_AVCODEC_MT
332     int i_thread_count = var_InheritInteger( p_dec, "avcodec-threads" );
333     if( i_thread_count <= 0 )
334     {
335         i_thread_count = vlc_GetCPUCount();
336         if( i_thread_count > 1 )
337             i_thread_count++;
338
339         //FIXME: take in count the decoding time
340         i_thread_count = __MIN( i_thread_count, 4 );
341     }
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;
346
347     switch( i_codec_id )
348     {
349         case AV_CODEC_ID_MPEG4:
350         case AV_CODEC_ID_H263:
351             p_sys->p_context->thread_type = 0;
352             break;
353         case AV_CODEC_ID_MPEG1VIDEO:
354         case AV_CODEC_ID_MPEG2VIDEO:
355             p_sys->p_context->thread_type &= ~FF_THREAD_SLICE;
356             /* fall through */
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;
362 # endif
363     }
364
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
368
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;
375
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 )
379     {
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;
382     }
383     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
384
385 #if LIBAVCODEC_VERSION_MAJOR < 54
386     /* Setup palette */
387     memset( &p_sys->palette, 0, sizeof(p_sys->palette) );
388     if( p_dec->fmt_in.video.p_palette )
389     {
390         p_sys->palette.palette_changed = 1;
391
392         for( int i = 0; i < __MIN( AVPALETTE_COUNT, p_dec->fmt_in.video.p_palette->i_entries ); i++ )
393         {
394             union {
395                 uint32_t u;
396                 uint8_t a[4];
397             } c;
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];
402
403             p_sys->palette.palette[i] = c.u;
404         }
405         p_sys->p_context->palctrl = &p_sys->palette;
406
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;
410     }
411     else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
412     {
413         p_sys->p_context->palctrl = &p_sys->palette;
414     }
415 #else
416 # warning FIXME
417 #endif
418
419     /* ***** init this codec with special data ***** */
420     ffmpeg_InitCodec( p_dec );
421
422     /* ***** Open the codec ***** */
423     if( ffmpeg_OpenCodec( p_dec ) < 0 )
424     {
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 );
428         free( p_sys );
429         return VLC_EGENERIC;
430     }
431
432     return VLC_SUCCESS;
433 }
434
435 /*****************************************************************************
436  * DecodeVideo: Called to decode one or more frames
437  *****************************************************************************/
438 picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
439 {
440     decoder_sys_t *p_sys = p_dec->p_sys;
441     AVCodecContext *p_context = p_sys->p_context;
442     int b_drawpicture;
443     int b_null_size = false;
444     block_t *p_block;
445
446     if( !pp_block || !*pp_block )
447         return NULL;
448
449     if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
450     {
451         ffmpeg_InitCodec( p_dec );
452         if( p_sys->b_delayed_open )
453         {
454             if( ffmpeg_OpenCodec( p_dec ) )
455                 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
456         }
457     }
458
459     p_block = *pp_block;
460     if( p_sys->b_delayed_open )
461     {
462         block_Release( p_block );
463         return NULL;
464     }
465
466     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
467     {
468         p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
469
470         p_sys->i_late_frames = 0;
471
472         post_mt( p_sys );
473         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
474             avcodec_flush_buffers( p_context );
475         wait_mt( p_sys );
476
477         block_Release( p_block );
478         return NULL;
479     }
480
481     if( p_block->i_flags & BLOCK_FLAG_PREROLL )
482     {
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;
487     }
488
489     if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
490         (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
491     {
492         if( p_sys->i_pts > VLC_TS_INVALID )
493         {
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 */
497         }
498         block_Release( p_block );
499         p_sys->i_late_frames--;
500         return NULL;
501     }
502
503     /* A good idea could be to decode all I pictures and see for the other */
504     if( !p_dec->b_pace_control &&
505         p_sys->b_hurry_up &&
506         (p_sys->i_late_frames > 4) )
507     {
508         b_drawpicture = 0;
509         if( p_sys->i_late_frames < 12 )
510         {
511             p_context->skip_frame =
512                     (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
513                     AVDISCARD_NONREF : p_sys->i_skip_frame;
514         }
515         else
516         {
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 );
521             return NULL;
522         }
523     }
524     else
525     {
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) )
529             b_drawpicture = 1;
530         else
531             b_drawpicture = 0;
532     }
533
534     if( p_context->width <= 0 || p_context->height <= 0 )
535     {
536         if( p_sys->b_hurry_up )
537             p_context->skip_frame = p_sys->i_skip_frame;
538         b_null_size = true;
539     }
540     else if( !b_drawpicture )
541     {
542         /* It creates broken picture
543          * FIXME either our parser or ffmpeg is broken */
544 #if 0
545         if( p_sys->b_hurry_up )
546             p_context->skip_frame = __MAX( p_context->skip_frame,
547                                                   AVDISCARD_NONREF );
548 #endif
549     }
550
551     /*
552      * Do the actual decoding now */
553
554     /* Don't forget that libavcodec requires a little more bytes
555      * that the real frame size */
556     if( p_block->i_buffer > 0 )
557     {
558         p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
559
560         p_block = block_Realloc( p_block, 0,
561                             p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
562         if( !p_block )
563             return NULL;
564         p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
565         *pp_block = p_block;
566         memset( p_block->p_buffer + p_block->i_buffer, 0,
567                 FF_INPUT_BUFFER_PADDING_SIZE );
568     }
569
570     while( p_block->i_buffer > 0 || p_sys->b_flush )
571     {
572         int i_used, b_gotpicture;
573         picture_t *p_pic;
574         AVPacket pkt;
575
576         post_mt( p_sys );
577
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;
583
584         /* Make sure we don't reuse the same timestamps twice */
585         p_block->i_pts =
586         p_block->i_dts = VLC_TS_INVALID;
587
588         i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic,
589                                        &b_gotpicture, &pkt );
590
591         if( b_null_size && !p_sys->b_flush &&
592             p_context->width > 0 && p_context->height > 0 )
593         {
594             /* Reparse it to not drop the I frame */
595             b_null_size = false;
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 );
600         }
601         wait_mt( p_sys );
602
603         if( p_sys->b_flush )
604             p_sys->b_first_frame = true;
605
606         if( p_block->i_buffer <= 0 )
607             p_sys->b_flush = false;
608
609         if( i_used < 0 )
610         {
611             if( b_drawpicture )
612                 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
613                           p_block->i_buffer );
614             block_Release( p_block );
615             return NULL;
616         }
617         else if( (unsigned)i_used > p_block->i_buffer ||
618                  p_context->thread_count > 1 )
619         {
620             i_used = p_block->i_buffer;
621         }
622
623         /* Consumed bytes */
624         p_block->i_buffer -= i_used;
625         p_block->p_buffer += i_used;
626
627         /* Nothing to display */
628         if( !b_gotpicture )
629         {
630             if( i_used == 0 ) break;
631             continue;
632         }
633
634         /* Sanity check (seems to be needed for some streams) */
635         if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B)
636         {
637             p_sys->b_has_b_frames = true;
638         }
639
640         /* Compute the PTS */
641         mtime_t i_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;
645
646         if( i_pts <= VLC_TS_INVALID )
647             i_pts = p_sys->i_pts;
648
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 )
653         {
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 )
657             {
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);
662             }
663             else if( p_context->time_base.den > 0 )
664             {
665                 int i_tick = p_context->ticks_per_frame;
666                 if( i_tick <= 0 )
667                     i_tick = 1;
668
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);
673             }
674         }
675
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 );
680
681         if( i_display_date > 0 && i_display_date <= mdate() )
682         {
683             p_sys->i_late_frames++;
684             if( p_sys->i_late_frames == 1 )
685                 p_sys->i_late_frames_start = mdate();
686         }
687         else
688         {
689             p_sys->i_late_frames = 0;
690         }
691
692         if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
693             continue;
694
695         if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL )
696         {
697             /* Get a new picture */
698             p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
699             if( !p_pic )
700             {
701                 block_Release( p_block );
702                 return NULL;
703             }
704
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 );
708         }
709         else
710         {
711             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
712             decoder_LinkPicture( p_dec, p_pic );
713         }
714
715         if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
716         {
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;
722
723             if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
724             {
725                 p_dec->fmt_out.video.i_sar_num = 1;
726                 p_dec->fmt_out.video.i_sar_den = 1;
727             }
728         }
729
730         /* Send decoded frame to vout */
731         if( i_pts > VLC_TS_INVALID)
732         {
733             p_pic->date = i_pts;
734
735             if( p_sys->b_first_frame )
736             {
737                 /* Hack to force display of still pictures */
738                 p_sys->b_first_frame = false;
739                 p_pic->b_force = true;
740             }
741
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;
745
746             return p_pic;
747         }
748         else
749         {
750             decoder_DeletePicture( p_dec, p_pic );
751         }
752     }
753
754     block_Release( p_block );
755     return NULL;
756 }
757
758 /*****************************************************************************
759  * EndVideo: decoder destruction
760  *****************************************************************************
761  * This function is called when the thread ends after a successful
762  * initialization.
763  *****************************************************************************/
764 void EndVideoDec( decoder_t *p_dec )
765 {
766     decoder_sys_t *p_sys = p_dec->p_sys;
767
768     post_mt( p_sys );
769
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 );
773
774     wait_mt( p_sys );
775
776     if( p_sys->p_ff_pic )
777         av_free( p_sys->p_ff_pic );
778
779     if( p_sys->p_va )
780         vlc_va_Delete( p_sys->p_va );
781
782     vlc_sem_destroy( &p_sys->sem_mt );
783 }
784
785 /*****************************************************************************
786  * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
787  *****************************************************************************/
788 static void ffmpeg_InitCodec( decoder_t *p_dec )
789 {
790     decoder_sys_t *p_sys = p_dec->p_sys;
791     int i_size = p_dec->fmt_in.i_extra;
792
793     if( !i_size ) return;
794
795     if( p_sys->i_codec_id == AV_CODEC_ID_SVQ3 )
796     {
797         uint8_t *p;
798
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 );
802         if( !p )
803             return;
804
805         memcpy( &p[0],  "SVQ3", 4 );
806         memset( &p[4], 0, 8 );
807         memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
808
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 ) )
812         {
813             uint8_t *psz = &p[0x52];
814
815             while( psz < &p[p_sys->p_context->extradata_size - 8] )
816             {
817                 int i_size = GetDWBE( psz );
818                 if( i_size <= 1 )
819                 {
820                     /* FIXME handle 1 as long size */
821                     break;
822                 }
823                 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
824                 {
825                     memmove( &p[0x52], psz,
826                              &p[p_sys->p_context->extradata_size] - psz );
827                     break;
828                 }
829
830                 psz += i_size;
831             }
832         }
833     }
834     else
835     {
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 )
840         {
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 );
845         }
846     }
847 }
848
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 )
855 {
856     decoder_sys_t *p_sys = p_dec->p_sys;
857
858     if( p_sys->p_va )
859     {
860         vlc_va_Extract( p_sys->p_va, p_pic, p_ff_pic->opaque,
861                         p_ff_pic->data[3] );
862     }
863     else if( FindVlcChroma( p_sys->p_context->pix_fmt ) )
864     {
865         int i_plane, i_size, i_line;
866         uint8_t *p_dst, *p_src;
867         int i_src_stride, i_dst_stride;
868
869         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
870         {
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;
875
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;
878                  i_line++ )
879             {
880                 memcpy( p_dst, p_src, i_size );
881                 p_src += i_src_stride;
882                 p_dst += i_dst_stride;
883             }
884         }
885     }
886     else
887     {
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" );
891         p_dec->b_error = 1;
892     }
893 }
894
895 #if LIBAVCODEC_VERSION_MAJOR >= 55
896 static int lavc_va_GetFrame(struct AVCodecContext *ctx, AVFrame *frame,
897                             int flags)
898 {
899     decoder_t *dec = ctx->opaque;
900     decoder_sys_t *sys = dec->p_sys;
901     vlc_va_t *va = sys->p_va;
902
903     if (vlc_va_Setup(va, &ctx->hwaccel_context, &dec->fmt_out.video.i_chroma,
904                      ctx->coded_width, ctx->coded_height))
905     {
906         msg_Err(dec, "hardware acceleration setup failed");
907         return -1;
908     }
909     if (vlc_va_Get(va, &frame->opaque, &frame->data[0]))
910     {
911         msg_Err(dec, "hardware acceleration picture allocation failed");
912         return -1;
913     }
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];
917
918     frame->buf[0] = av_buffer_create(frame->data[0], 0, va->release,
919                                      frame->opaque, 0);
920     if (unlikely(frame->buf[0] == NULL))
921     {
922         vlc_va_Release(va, frame->opaque, frame->data[0]);
923         return -1;
924     }
925     assert(frame->data[0] != NULL);
926     (void) flags;
927     return 0;
928 }
929
930 typedef struct
931 {
932     decoder_t *decoder;
933     picture_t *picture;
934 } lavc_pic_ref_t;
935
936 static void lavc_dr_ReleaseFrame(void *opaque, uint8_t *data)
937 {
938     lavc_pic_ref_t *ref = opaque;
939
940     decoder_UnlinkPicture(ref->decoder, ref->picture);
941     free(ref);
942     (void) data;
943 }
944
945 static picture_t *lavc_dr_GetFrame(struct AVCodecContext *ctx,
946                                    AVFrame *frame, int flags)
947 {
948     decoder_t *dec = (decoder_t *)ctx->opaque;
949     decoder_sys_t *sys = dec->p_sys;
950
951     if (GetVlcChroma(&dec->fmt_out.video, ctx->pix_fmt) != VLC_SUCCESS)
952         return NULL;
953     dec->fmt_out.i_codec = dec->fmt_out.video.i_chroma;
954     if (ctx->pix_fmt == PIX_FMT_PAL8)
955         return NULL;
956
957     int width = frame->width;
958     int height = frame->height;
959     int aligns[AV_NUM_DATA_POINTERS];
960
961     avcodec_align_dimensions2(ctx, &width, &height, aligns);
962
963     picture_t *pic = ffmpeg_NewPictBuf(dec, ctx);
964     if (pic == NULL)
965         return NULL;
966
967     /* Check that the picture is suitable for libavcodec */
968     if (pic->p[0].i_pitch < width * pic->p[0].i_pixel_pitch)
969     {
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);
973         goto no_dr;
974     }
975
976     if (pic->p[0].i_lines < height)
977     {
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);
981         goto no_dr;
982     }
983
984     for (int i = 0; i < pic->i_planes; i++)
985     {
986         if (pic->p[i].i_pitch % aligns[i])
987         {
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]);
991             goto no_dr;
992         }
993         if (((uintptr_t)pic->p[i].p_pixels) % aligns[i])
994         {
995             if (sys->i_direct_rendering_used != 0)
996                 msg_Warn(dec, "plane %d not aligned", i);
997             goto no_dr;
998         }
999     }
1000
1001     /* Allocate buffer references */
1002     for (int i = 0; i < pic->i_planes; i++)
1003     {
1004         lavc_pic_ref_t *ref = malloc(sizeof (*ref));
1005         if (ref == NULL)
1006             goto error;
1007         ref->decoder = dec;
1008         ref->picture = pic;
1009         decoder_LinkPicture(dec, pic);
1010
1011         uint8_t *data = pic->p[i].p_pixels;
1012         int size = pic->p[i].i_pitch * pic->p[i].i_lines;
1013
1014         frame->buf[i] = av_buffer_create(data, size, lavc_dr_ReleaseFrame,
1015                                          ref, 0);
1016         if (unlikely(frame->buf[i] == NULL))
1017         {
1018             lavc_dr_ReleaseFrame(ref, data);
1019             goto error;
1020         }
1021     }
1022     decoder_UnlinkPicture(dec, pic);
1023     (void) flags;
1024     return pic;
1025 error:
1026     for (unsigned i = 0; frame->buf[i] != NULL; i++)
1027         av_buffer_unref(&frame->buf[i]);
1028 no_dr:
1029     decoder_DeletePicture(dec, pic);
1030     return NULL;
1031 }
1032
1033 /**
1034  * Callback used by libavcodec to get a frame buffer.
1035  *
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).
1038  */
1039 static int lavc_GetFrame(struct AVCodecContext *ctx, AVFrame *frame, int flags)
1040 {
1041     decoder_t *dec = ctx->opaque;
1042     decoder_sys_t *sys = dec->p_sys;
1043     picture_t *pic;
1044
1045     for (unsigned i = 0; i < AV_NUM_DATA_POINTERS; i++)
1046     {
1047         frame->data[i] = NULL;
1048         frame->linesize[i] = 0;
1049         frame->buf[i] = NULL;
1050     }
1051
1052     if (sys->p_va != NULL)
1053         return lavc_va_GetFrame(ctx, frame, flags);
1054
1055     frame->opaque = NULL;
1056     if (!sys->b_direct_rendering)
1057         return avcodec_default_get_buffer2(ctx, frame, flags);
1058
1059     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1060      * so we need to check for direct rendering again. */
1061     wait_mt(sys);
1062     pic = lavc_dr_GetFrame(ctx, frame, flags);
1063     if (pic == NULL)
1064     {
1065         if (sys->i_direct_rendering_used != 0)
1066         {
1067             msg_Warn(dec, "disabling direct rendering");
1068             sys->i_direct_rendering_used = 0;
1069         }
1070         post_mt(sys);
1071         return avcodec_default_get_buffer2(ctx, frame, flags);
1072     }
1073
1074     if (sys->i_direct_rendering_used != 1)
1075     {
1076         msg_Dbg(dec, "enabling direct rendering");
1077         sys->i_direct_rendering_used = 1;
1078     }
1079     post_mt(sys);
1080
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++)
1084     {
1085         frame->data[i] = pic->p[i].p_pixels;
1086         frame->linesize[i] = pic->p[i].i_pitch;
1087     }
1088     return 0;
1089 }
1090 #else
1091 static int ffmpeg_va_GetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
1092 {
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;
1096
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 ) )
1101     {
1102         msg_Err( p_dec, "vlc_va_Setup failed" );
1103         return -1;
1104     }
1105
1106     if( vlc_va_Get( p_va, &p_ff_pic->opaque, &p_ff_pic->data[0] ) )
1107     {
1108         msg_Err( p_dec, "vlc_va_Get failed" );
1109         return -1;
1110     }
1111
1112     p_ff_pic->data[3] = p_ff_pic->data[0];
1113     p_ff_pic->type = FF_BUFFER_TYPE_USER;
1114     return 0;
1115 }
1116
1117 static picture_t *ffmpeg_dr_GetFrameBuf(struct AVCodecContext *p_context)
1118 {
1119     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1120     decoder_sys_t *p_sys = p_dec->p_sys;
1121
1122     int i_width = p_context->width;
1123     int i_height = p_context->height;
1124     avcodec_align_dimensions( p_context, &i_width, &i_height );
1125
1126     picture_t *p_pic = NULL;
1127     if (GetVlcChroma(&p_dec->fmt_out.video, p_context->pix_fmt) != VLC_SUCCESS)
1128         goto no_dr;
1129
1130     if (p_context->pix_fmt == PIX_FMT_PAL8)
1131         goto no_dr;
1132
1133     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
1134
1135     p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
1136     if( !p_pic )
1137         goto no_dr;
1138
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 )
1141         goto no_dr;
1142
1143     for( int i = 0; i < p_pic->i_planes; i++ )
1144     {
1145         unsigned i_align;
1146         switch( p_sys->i_codec_id )
1147         {
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:
1153             i_align = 16;
1154             break;
1155         default:
1156             i_align = i == 0 ? 16 : 8;
1157             break;
1158         }
1159         if( p_pic->p[i].i_pitch % i_align )
1160             goto no_dr;
1161         if( (intptr_t)p_pic->p[i].p_pixels % i_align )
1162             goto no_dr;
1163     }
1164
1165     if( p_context->pix_fmt == PIX_FMT_YUV422P )
1166     {
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 )
1169             goto no_dr;
1170     }
1171
1172     return p_pic;
1173
1174 no_dr:
1175     if (p_pic)
1176         decoder_DeletePicture( p_dec, p_pic );
1177
1178     return NULL;
1179 }
1180
1181 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
1182                                AVFrame *p_ff_pic )
1183 {
1184     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1185     decoder_sys_t *p_sys = p_dec->p_sys;
1186
1187     /* */
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;
1191 #endif
1192 #if LIBAVCODEC_VERSION_MAJOR < 54
1193     p_ff_pic->age = 256*256*256*64;
1194 #endif
1195
1196     if( p_sys->p_va )
1197         return ffmpeg_va_GetFrameBuf(p_context, p_ff_pic);
1198
1199     if( !p_sys->b_direct_rendering )
1200         return avcodec_default_get_buffer( p_context, p_ff_pic );
1201
1202     wait_mt( p_sys );
1203     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
1204      * so we need to check for direct rendering again. */
1205
1206     picture_t *p_pic = ffmpeg_dr_GetFrameBuf(p_context);
1207     if (!p_pic) {
1208         if( p_sys->i_direct_rendering_used != 0 )
1209         {
1210             msg_Warn( p_dec, "disabling direct rendering" );
1211             p_sys->i_direct_rendering_used = 0;
1212         }
1213
1214         post_mt( p_sys );
1215         return avcodec_default_get_buffer( p_context, p_ff_pic );
1216     }
1217
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;
1221     }
1222
1223     p_context->draw_horiz_band = NULL;
1224     post_mt( p_sys );
1225
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 */
1232
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;
1237
1238     return 0;
1239 }
1240
1241 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
1242                                     AVFrame *p_ff_pic )
1243 {
1244     decoder_t *p_dec = (decoder_t *)p_context->opaque;
1245     decoder_sys_t *p_sys = p_dec->p_sys;
1246
1247     if( p_sys->p_va )
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
1255          */
1256         avcodec_default_release_buffer( p_context, p_ff_pic );
1257
1258     for( int i = 0; i < 4; i++ )
1259         p_ff_pic->data[i] = NULL;
1260 }
1261 #endif
1262
1263 static enum PixelFormat ffmpeg_GetFormat( AVCodecContext *p_context,
1264                                           const enum PixelFormat *pi_fmt )
1265 {
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;
1269
1270     if( p_va != NULL )
1271         vlc_va_Delete( p_va );
1272
1273     /* Enumerate available formats */
1274     bool can_hwaccel = false;
1275     for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1276     {
1277         const AVPixFmtDescriptor *dsc = av_pix_fmt_desc_get(pi_fmt[i]);
1278         if (dsc == NULL)
1279             continue;
1280         bool hwaccel = (dsc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
1281
1282         msg_Dbg( p_dec, "available %sware decoder output format %d (%s)",
1283                  hwaccel ? "hard" : "soft", pi_fmt[i], dsc->name );
1284         if (hwaccel)
1285             can_hwaccel = true;
1286     }
1287
1288     if (!can_hwaccel)
1289         goto end;
1290
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;
1297
1298     p_va = vlc_va_New( VLC_OBJECT(p_dec), p_sys->i_codec_id, &p_dec->fmt_in );
1299     if( p_va == NULL )
1300         goto end;
1301
1302     for( size_t i = 0; pi_fmt[i] != PIX_FMT_NONE; i++ )
1303     {
1304         if( p_va->pix_fmt != pi_fmt[i] )
1305             continue;
1306
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 ) )
1313         {
1314             msg_Err( p_dec, "acceleration setup failure" );
1315             break;
1316         }
1317
1318         if( p_va->description )
1319             msg_Info( p_dec, "Using %s for hardware decoding.",
1320                       p_va->description );
1321
1322         /* FIXME this will disable direct rendering
1323          * even if a new pixel format is renegotiated
1324          */
1325         p_sys->b_direct_rendering = false;
1326         p_sys->p_va = p_va;
1327         p_context->draw_horiz_band = NULL;
1328         return pi_fmt[i];
1329     }
1330
1331     vlc_va_Delete( p_va );
1332
1333 end:
1334     /* Fallback to default behaviour */
1335     p_sys->p_va = NULL;
1336     return avcodec_default_get_format( p_context, pi_fmt );
1337 }