]> git.sesse.net Git - ffmpeg/blob - libavcodec/crystalhd.c
avcodec/mediacodec: Factor duplicate include
[ffmpeg] / libavcodec / crystalhd.c
1 /*
2  * - CrystalHD decoder module -
3  *
4  * Copyright(C) 2010,2011 Philip Langdale <ffmpeg.philipl@overt.org>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /*
24  * - Principles of Operation -
25  *
26  * The CrystalHD decoder operates at the bitstream level - which is an even
27  * higher level than the decoding hardware you typically see in modern GPUs.
28  * This means it has a very simple interface, in principle. You feed demuxed
29  * packets in one end and get decoded picture (fields/frames) out the other.
30  *
31  * Of course, nothing is ever that simple. Due, at the very least, to b-frame
32  * dependencies in the supported formats, the hardware has a delay between
33  * when a packet goes in, and when a picture comes out. Furthermore, this delay
34  * is not just a function of time, but also one of the dependency on additional
35  * frames being fed into the decoder to satisfy the b-frame dependencies.
36  *
37  * As such, a pipeline will build up that is roughly equivalent to the required
38  * DPB for the file being played. If that was all it took, things would still
39  * be simple - so, of course, it isn't.
40  *
41  * The hardware has a way of indicating that a picture is ready to be copied out,
42  * but this is unreliable - and sometimes the attempt will still fail so, based
43  * on testing, the code will wait until 3 pictures are ready before starting
44  * to copy out - and this has the effect of extending the pipeline.
45  *
46  * Finally, while it is tempting to say that once the decoder starts outputting
47  * frames, the software should never fail to return a frame from a decode(),
48  * this is a hard assertion to make, because the stream may switch between
49  * differently encoded content (number of b-frames, interlacing, etc) which
50  * might require a longer pipeline than before. If that happened, you could
51  * deadlock trying to retrieve a frame that can't be decoded without feeding
52  * in additional packets.
53  *
54  * As such, the code will return in the event that a picture cannot be copied
55  * out, leading to an increase in the length of the pipeline. This in turn,
56  * means we have to be sensitive to the time it takes to decode a picture;
57  * We do not want to give up just because the hardware needed a little more
58  * time to prepare the picture! For this reason, there are delays included
59  * in the decode() path that ensure that, under normal conditions, the hardware
60  * will only fail to return a frame if it really needs additional packets to
61  * complete the decoding.
62  *
63  * Finally, to be explicit, we do not want the pipeline to grow without bound
64  * for two reasons: 1) The hardware can only buffer a finite number of packets,
65  * and 2) The client application may not be able to cope with arbitrarily long
66  * delays in the video path relative to the audio path. For example. MPlayer
67  * can only handle a 20 picture delay (although this is arbitrary, and needs
68  * to be extended to fully support the CrystalHD where the delay could be up
69  * to 32 pictures - consider PAFF H.264 content with 16 b-frames).
70  */
71
72 /*****************************************************************************
73  * Includes
74  ****************************************************************************/
75
76 #define _XOPEN_SOURCE 600
77 #include <inttypes.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80
81 #include <libcrystalhd/bc_dts_types.h>
82 #include <libcrystalhd/bc_dts_defs.h>
83 #include <libcrystalhd/libcrystalhd_if.h>
84
85 #include "avcodec.h"
86 #include "h264dec.h"
87 #include "internal.h"
88 #include "libavutil/imgutils.h"
89 #include "libavutil/intreadwrite.h"
90 #include "libavutil/opt.h"
91
92 #if HAVE_UNISTD_H
93 #include <unistd.h>
94 #endif
95
96 /** Timeout parameter passed to DtsProcOutput() in us */
97 #define OUTPUT_PROC_TIMEOUT 50
98 /** Step between fake timestamps passed to hardware in units of 100ns */
99 #define TIMESTAMP_UNIT 100000
100 /** Initial value in us of the wait in decode() */
101 #define BASE_WAIT 10000
102 /** Increment in us to adjust wait in decode() */
103 #define WAIT_UNIT 1000
104
105
106 /*****************************************************************************
107  * Module private data
108  ****************************************************************************/
109
110 typedef enum {
111     RET_ERROR           = -1,
112     RET_OK              = 0,
113     RET_COPY_AGAIN      = 1,
114     RET_SKIP_NEXT_COPY  = 2,
115     RET_COPY_NEXT_FIELD = 3,
116 } CopyRet;
117
118 typedef struct OpaqueList {
119     struct OpaqueList *next;
120     uint64_t fake_timestamp;
121     uint64_t reordered_opaque;
122     uint8_t pic_type;
123 } OpaqueList;
124
125 typedef struct {
126     AVClass *av_class;
127     AVCodecContext *avctx;
128     AVFrame *pic;
129     HANDLE dev;
130
131     uint8_t *orig_extradata;
132     uint32_t orig_extradata_size;
133
134     AVBSFContext *bsfc;
135     AVCodecParserContext *parser;
136
137     uint8_t is_70012;
138     uint8_t *sps_pps_buf;
139     uint32_t sps_pps_size;
140     uint8_t is_nal;
141     uint8_t output_ready;
142     uint8_t need_second_field;
143     uint8_t skip_next_output;
144     uint64_t decode_wait;
145
146     uint64_t last_picture;
147
148     OpaqueList *head;
149     OpaqueList *tail;
150
151     /* Options */
152     uint32_t sWidth;
153 } CHDContext;
154
155 static const AVOption options[] = {
156     { "crystalhd_downscale_width",
157       "Turn on downscaling to the specified width",
158       offsetof(CHDContext, sWidth),
159       AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
160       AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM, },
161     { NULL, },
162 };
163
164
165 /*****************************************************************************
166  * Helper functions
167  ****************************************************************************/
168
169 static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
170 {
171     switch (id) {
172     case AV_CODEC_ID_MPEG4:
173         return BC_MSUBTYPE_DIVX;
174     case AV_CODEC_ID_MSMPEG4V3:
175         return BC_MSUBTYPE_DIVX311;
176     case AV_CODEC_ID_MPEG2VIDEO:
177         return BC_MSUBTYPE_MPEG2VIDEO;
178     case AV_CODEC_ID_VC1:
179         return BC_MSUBTYPE_VC1;
180     case AV_CODEC_ID_WMV3:
181         return BC_MSUBTYPE_WMV3;
182     case AV_CODEC_ID_H264:
183         return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
184     default:
185         return BC_MSUBTYPE_INVALID;
186     }
187 }
188
189 static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
190 {
191     av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
192     av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
193            output->YBuffDoneSz);
194     av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
195            output->UVBuffDoneSz);
196     av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n",
197            output->PicInfo.timeStamp);
198     av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
199            output->PicInfo.picture_number);
200     av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
201            output->PicInfo.width);
202     av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
203            output->PicInfo.height);
204     av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
205            output->PicInfo.chroma_format);
206     av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
207            output->PicInfo.pulldown);
208     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
209            output->PicInfo.flags);
210     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
211            output->PicInfo.frame_rate);
212     av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
213            output->PicInfo.aspect_ratio);
214     av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
215            output->PicInfo.colour_primaries);
216     av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
217            output->PicInfo.picture_meta_payload);
218     av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
219            output->PicInfo.sess_num);
220     av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
221            output->PicInfo.ycom);
222     av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
223            output->PicInfo.custom_aspect_ratio_width_height);
224     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
225            output->PicInfo.n_drop);
226     av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
227            output->PicInfo.other.h264.valid);
228 }
229
230
231 /*****************************************************************************
232  * OpaqueList functions
233  ****************************************************************************/
234
235 static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque,
236                                  uint8_t pic_type)
237 {
238     OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
239     if (!newNode) {
240         av_log(priv->avctx, AV_LOG_ERROR,
241                "Unable to allocate new node in OpaqueList.\n");
242         return 0;
243     }
244     if (!priv->head) {
245         newNode->fake_timestamp = TIMESTAMP_UNIT;
246         priv->head              = newNode;
247     } else {
248         newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
249         priv->tail->next        = newNode;
250     }
251     priv->tail = newNode;
252     newNode->reordered_opaque = reordered_opaque;
253     newNode->pic_type = pic_type;
254
255     return newNode->fake_timestamp;
256 }
257
258 /*
259  * The OpaqueList is built in decode order, while elements will be removed
260  * in presentation order. If frames are reordered, this means we must be
261  * able to remove elements that are not the first element.
262  *
263  * Returned node must be freed by caller.
264  */
265 static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
266 {
267     OpaqueList *node = priv->head;
268
269     if (!priv->head) {
270         av_log(priv->avctx, AV_LOG_ERROR,
271                "CrystalHD: Attempted to query non-existent timestamps.\n");
272         return NULL;
273     }
274
275     /*
276      * The first element is special-cased because we have to manipulate
277      * the head pointer rather than the previous element in the list.
278      */
279     if (priv->head->fake_timestamp == fake_timestamp) {
280         priv->head = node->next;
281
282         if (!priv->head->next)
283             priv->tail = priv->head;
284
285         node->next = NULL;
286         return node;
287     }
288
289     /*
290      * The list is processed at arm's length so that we have the
291      * previous element available to rewrite its next pointer.
292      */
293     while (node->next) {
294         OpaqueList *current = node->next;
295         if (current->fake_timestamp == fake_timestamp) {
296             node->next = current->next;
297
298             if (!node->next)
299                priv->tail = node;
300
301             current->next = NULL;
302             return current;
303         } else {
304             node = current;
305         }
306     }
307
308     av_log(priv->avctx, AV_LOG_VERBOSE,
309            "CrystalHD: Couldn't match fake_timestamp.\n");
310     return NULL;
311 }
312
313
314 /*****************************************************************************
315  * Video decoder API function definitions
316  ****************************************************************************/
317
318 static void flush(AVCodecContext *avctx)
319 {
320     CHDContext *priv = avctx->priv_data;
321
322     avctx->has_b_frames     = 0;
323     priv->last_picture      = -1;
324     priv->output_ready      = 0;
325     priv->need_second_field = 0;
326     priv->skip_next_output  = 0;
327     priv->decode_wait       = BASE_WAIT;
328
329     av_frame_unref (priv->pic);
330
331     /* Flush mode 4 flushes all software and hardware buffers. */
332     DtsFlushInput(priv->dev, 4);
333 }
334
335
336 static av_cold int uninit(AVCodecContext *avctx)
337 {
338     CHDContext *priv = avctx->priv_data;
339     HANDLE device;
340
341     device = priv->dev;
342     DtsStopDecoder(device);
343     DtsCloseDecoder(device);
344     DtsDeviceClose(device);
345
346     /*
347      * Restore original extradata, so that if the decoder is
348      * reinitialised, the bitstream detection and filtering
349      * will work as expected.
350      */
351     if (priv->orig_extradata) {
352         av_free(avctx->extradata);
353         avctx->extradata = priv->orig_extradata;
354         avctx->extradata_size = priv->orig_extradata_size;
355         priv->orig_extradata = NULL;
356         priv->orig_extradata_size = 0;
357     }
358
359     av_parser_close(priv->parser);
360     if (priv->bsfc) {
361         av_bsf_free(&priv->bsfc);
362     }
363
364     av_freep(&priv->sps_pps_buf);
365
366     av_frame_free (&priv->pic);
367
368     if (priv->head) {
369        OpaqueList *node = priv->head;
370        while (node) {
371           OpaqueList *next = node->next;
372           av_free(node);
373           node = next;
374        }
375     }
376
377     return 0;
378 }
379
380
381 static av_cold int init_bsf(AVCodecContext *avctx, const char *bsf_name)
382 {
383     CHDContext *priv = avctx->priv_data;
384     const AVBitStreamFilter *bsf;
385     int avret;
386     void *extradata = NULL;
387     size_t size = 0;
388
389     bsf = av_bsf_get_by_name(bsf_name);
390     if (!bsf) {
391         av_log(avctx, AV_LOG_ERROR,
392                "Cannot open the %s BSF!\n", bsf_name);
393         return AVERROR_BSF_NOT_FOUND;
394     }
395
396     avret = av_bsf_alloc(bsf, &priv->bsfc);
397     if (avret != 0) {
398         return avret;
399     }
400
401     avret = avcodec_parameters_from_context(priv->bsfc->par_in, avctx);
402     if (avret != 0) {
403         return avret;
404     }
405
406     avret = av_bsf_init(priv->bsfc);
407     if (avret != 0) {
408         return avret;
409     }
410
411     /* Back up the extradata so it can be restored at close time. */
412     priv->orig_extradata = avctx->extradata;
413     priv->orig_extradata_size = avctx->extradata_size;
414
415     size = priv->bsfc->par_out->extradata_size;
416     extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
417     if (!extradata) {
418         av_log(avctx, AV_LOG_ERROR,
419                "Failed to allocate copy of extradata\n");
420         return AVERROR(ENOMEM);
421     }
422     memcpy(extradata, priv->bsfc->par_out->extradata, size);
423
424     avctx->extradata = extradata;
425     avctx->extradata_size = size;
426
427     return 0;
428 }
429
430 static av_cold int init(AVCodecContext *avctx)
431 {
432     CHDContext* priv;
433     int avret;
434     BC_STATUS ret;
435     BC_INFO_CRYSTAL version;
436     BC_INPUT_FORMAT format = {
437         .FGTEnable   = FALSE,
438         .Progressive = TRUE,
439         .OptFlags    = 0x80000000 | vdecFrameRate59_94 | 0x40,
440         .width       = avctx->width,
441         .height      = avctx->height,
442     };
443
444     BC_MEDIA_SUBTYPE subtype;
445
446     uint32_t mode = DTS_PLAYBACK_MODE |
447                     DTS_LOAD_FILE_PLAY_FW |
448                     DTS_SKIP_TX_CHK_CPB |
449                     DTS_PLAYBACK_DROP_RPT_MODE |
450                     DTS_SINGLE_THREADED_MODE |
451                     DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
452
453     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
454            avctx->codec->name);
455
456     avctx->pix_fmt = AV_PIX_FMT_YUYV422;
457
458     /* Initialize the library */
459     priv               = avctx->priv_data;
460     priv->avctx        = avctx;
461     priv->is_nal       = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
462     priv->last_picture = -1;
463     priv->decode_wait  = BASE_WAIT;
464     priv->pic          = av_frame_alloc();
465
466     subtype = id2subtype(priv, avctx->codec->id);
467     switch (subtype) {
468     case BC_MSUBTYPE_AVC1:
469         avret = init_bsf(avctx, "h264_mp4toannexb");
470         if (avret != 0) {
471             return avret;
472         }
473         subtype = BC_MSUBTYPE_H264;
474         format.startCodeSz = 4;
475         format.pMetaData  = avctx->extradata;
476         format.metaDataSz = avctx->extradata_size;
477         break;
478     case BC_MSUBTYPE_DIVX:
479         avret = init_bsf(avctx, "mpeg4_unpack_bframes");
480         if (avret != 0) {
481             return avret;
482         }
483         format.pMetaData  = avctx->extradata;
484         format.metaDataSz = avctx->extradata_size;
485         break;
486     case BC_MSUBTYPE_H264:
487         format.startCodeSz = 4;
488         // Fall-through
489     case BC_MSUBTYPE_VC1:
490     case BC_MSUBTYPE_WVC1:
491     case BC_MSUBTYPE_WMV3:
492     case BC_MSUBTYPE_WMVA:
493     case BC_MSUBTYPE_MPEG2VIDEO:
494     case BC_MSUBTYPE_DIVX311:
495         format.pMetaData  = avctx->extradata;
496         format.metaDataSz = avctx->extradata_size;
497         break;
498     default:
499         av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
500         return AVERROR(EINVAL);
501     }
502     format.mSubtype = subtype;
503
504     if (priv->sWidth) {
505         format.bEnableScaling = 1;
506         format.ScalingParams.sWidth = priv->sWidth;
507     }
508
509     /* Get a decoder instance */
510     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
511     // Initialize the Link and Decoder devices
512     ret = DtsDeviceOpen(&priv->dev, mode);
513     if (ret != BC_STS_SUCCESS) {
514         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
515         goto fail;
516     }
517
518     ret = DtsCrystalHDVersion(priv->dev, &version);
519     if (ret != BC_STS_SUCCESS) {
520         av_log(avctx, AV_LOG_VERBOSE,
521                "CrystalHD: DtsCrystalHDVersion failed\n");
522         goto fail;
523     }
524     priv->is_70012 = version.device == 0;
525
526     if (priv->is_70012 &&
527         (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
528         av_log(avctx, AV_LOG_VERBOSE,
529                "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
530         goto fail;
531     }
532
533     ret = DtsSetInputFormat(priv->dev, &format);
534     if (ret != BC_STS_SUCCESS) {
535         av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
536         goto fail;
537     }
538
539     ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
540     if (ret != BC_STS_SUCCESS) {
541         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
542         goto fail;
543     }
544
545     ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
546     if (ret != BC_STS_SUCCESS) {
547         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
548         goto fail;
549     }
550     ret = DtsStartDecoder(priv->dev);
551     if (ret != BC_STS_SUCCESS) {
552         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
553         goto fail;
554     }
555     ret = DtsStartCapture(priv->dev);
556     if (ret != BC_STS_SUCCESS) {
557         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
558         goto fail;
559     }
560
561     if (avctx->codec->id == AV_CODEC_ID_H264) {
562         priv->parser = av_parser_init(avctx->codec->id);
563         if (!priv->parser)
564             av_log(avctx, AV_LOG_WARNING,
565                    "Cannot open the h.264 parser! Interlaced h.264 content "
566                    "will not be detected reliably.\n");
567         priv->parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
568     }
569     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
570
571     return 0;
572
573  fail:
574     uninit(avctx);
575     return -1;
576 }
577
578
579 static inline CopyRet copy_frame(AVCodecContext *avctx,
580                                  BC_DTS_PROC_OUT *output,
581                                  void *data, int *got_frame)
582 {
583     BC_STATUS ret;
584     BC_DTS_STATUS decoder_status = { 0, };
585     uint8_t trust_interlaced;
586     uint8_t interlaced;
587
588     CHDContext *priv = avctx->priv_data;
589     int64_t pkt_pts  = AV_NOPTS_VALUE;
590     uint8_t pic_type = 0;
591
592     uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
593                            VDEC_FLAG_BOTTOMFIELD;
594     uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
595
596     int width    = output->PicInfo.width;
597     int height   = output->PicInfo.height;
598     int bwidth;
599     uint8_t *src = output->Ybuff;
600     int sStride;
601     uint8_t *dst;
602     int dStride;
603
604     if (output->PicInfo.timeStamp != 0) {
605         OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
606         if (node) {
607             pkt_pts = node->reordered_opaque;
608             pic_type = node->pic_type;
609             av_free(node);
610         } else {
611             /*
612              * We will encounter a situation where a timestamp cannot be
613              * popped if a second field is being returned. In this case,
614              * each field has the same timestamp and the first one will
615              * cause it to be popped. To keep subsequent calculations
616              * simple, pic_type should be set a FIELD value - doesn't
617              * matter which, but I chose BOTTOM.
618              */
619             pic_type = PICT_BOTTOM_FIELD;
620         }
621         av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
622                output->PicInfo.timeStamp);
623         av_log(avctx, AV_LOG_VERBOSE, "output picture type %d\n",
624                pic_type);
625     }
626
627     ret = DtsGetDriverStatus(priv->dev, &decoder_status);
628     if (ret != BC_STS_SUCCESS) {
629         av_log(avctx, AV_LOG_ERROR,
630                "CrystalHD: GetDriverStatus failed: %u\n", ret);
631        return RET_ERROR;
632     }
633
634     /*
635      * For most content, we can trust the interlaced flag returned
636      * by the hardware, but sometimes we can't. These are the
637      * conditions under which we can trust the flag:
638      *
639      * 1) It's not h.264 content
640      * 2) The UNKNOWN_SRC flag is not set
641      * 3) We know we're expecting a second field
642      * 4) The hardware reports this picture and the next picture
643      *    have the same picture number.
644      *
645      * Note that there can still be interlaced content that will
646      * fail this check, if the hardware hasn't decoded the next
647      * picture or if there is a corruption in the stream. (In either
648      * case a 0 will be returned for the next picture number)
649      */
650     trust_interlaced = avctx->codec->id != AV_CODEC_ID_H264 ||
651                        !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
652                        priv->need_second_field ||
653                        (decoder_status.picNumFlags & ~0x40000000) ==
654                        output->PicInfo.picture_number;
655
656     /*
657      * If we got a false negative for trust_interlaced on the first field,
658      * we will realise our mistake here when we see that the picture number is that
659      * of the previous picture. We cannot recover the frame and should discard the
660      * second field to keep the correct number of output frames.
661      */
662     if (output->PicInfo.picture_number == priv->last_picture && !priv->need_second_field) {
663         av_log(avctx, AV_LOG_WARNING,
664                "Incorrectly guessed progressive frame. Discarding second field\n");
665         /* Returning without providing a picture. */
666         return RET_OK;
667     }
668
669     interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
670                  trust_interlaced;
671
672     if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
673         av_log(avctx, AV_LOG_VERBOSE,
674                "Next picture number unknown. Assuming progressive frame.\n");
675     }
676
677     av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d | trust_interlaced %d\n",
678            interlaced, trust_interlaced);
679
680     if (priv->pic->data[0] && !priv->need_second_field)
681         av_frame_unref(priv->pic);
682
683     priv->need_second_field = interlaced && !priv->need_second_field;
684
685     if (!priv->pic->data[0]) {
686         if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
687             return RET_ERROR;
688     }
689
690     bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
691     if (priv->is_70012) {
692         int pStride;
693
694         if (width <= 720)
695             pStride = 720;
696         else if (width <= 1280)
697             pStride = 1280;
698         else pStride = 1920;
699         sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
700     } else {
701         sStride = bwidth;
702     }
703
704     dStride = priv->pic->linesize[0];
705     dst     = priv->pic->data[0];
706
707     av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
708
709     if (interlaced) {
710         int dY = 0;
711         int sY = 0;
712
713         height /= 2;
714         if (bottom_field) {
715             av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
716             dY = 1;
717         } else {
718             av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
719             dY = 0;
720         }
721
722         for (sY = 0; sY < height; dY++, sY++) {
723             memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
724             dY++;
725         }
726     } else {
727         av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
728     }
729
730     priv->pic->interlaced_frame = interlaced;
731     if (interlaced)
732         priv->pic->top_field_first = !bottom_first;
733
734     if (pkt_pts != AV_NOPTS_VALUE) {
735         priv->pic->pts = pkt_pts;
736 #if FF_API_PKT_PTS
737 FF_DISABLE_DEPRECATION_WARNINGS
738         priv->pic->pkt_pts = pkt_pts;
739 FF_ENABLE_DEPRECATION_WARNINGS
740 #endif
741     }
742     av_frame_set_pkt_pos(priv->pic, -1);
743     av_frame_set_pkt_duration(priv->pic, 0);
744     av_frame_set_pkt_size(priv->pic, -1);
745
746     if (!priv->need_second_field) {
747         *got_frame       = 1;
748         if ((ret = av_frame_ref(data, priv->pic)) < 0) {
749             return ret;
750         }
751     }
752
753     /*
754      * Two types of PAFF content have been observed. One form causes the
755      * hardware to return a field pair and the other individual fields,
756      * even though the input is always individual fields. We must skip
757      * copying on the next decode() call to maintain pipeline length in
758      * the first case.
759      */
760     if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
761         (pic_type == PICT_TOP_FIELD || pic_type == PICT_BOTTOM_FIELD)) {
762         av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
763         return RET_SKIP_NEXT_COPY;
764     }
765
766     /*
767      * The logic here is purely based on empirical testing with samples.
768      * If we need a second field, it could come from a second input packet,
769      * or it could come from the same field-pair input packet at the current
770      * field. In the first case, we should return and wait for the next time
771      * round to get the second field, while in the second case, we should
772      * ask the decoder for it immediately.
773      *
774      * Testing has shown that we are dealing with the fieldpair -> two fields
775      * case if the VDEC_FLAG_UNKNOWN_SRC is not set or if the input picture
776      * type was PICT_FRAME (in this second case, the flag might still be set)
777      */
778     return priv->need_second_field &&
779            (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
780             pic_type == PICT_FRAME) ?
781            RET_COPY_NEXT_FIELD : RET_OK;
782 }
783
784
785 static inline CopyRet receive_frame(AVCodecContext *avctx,
786                                     void *data, int *got_frame)
787 {
788     BC_STATUS ret;
789     BC_DTS_PROC_OUT output = {
790         .PicInfo.width  = avctx->width,
791         .PicInfo.height = avctx->height,
792     };
793     CHDContext *priv = avctx->priv_data;
794     HANDLE dev       = priv->dev;
795
796     *got_frame = 0;
797
798     // Request decoded data from the driver
799     ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
800     if (ret == BC_STS_FMT_CHANGE) {
801         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
802         avctx->width  = output.PicInfo.width;
803         avctx->height = output.PicInfo.height;
804         switch ( output.PicInfo.aspect_ratio ) {
805         case vdecAspectRatioSquare:
806             avctx->sample_aspect_ratio = (AVRational) {  1,  1};
807             break;
808         case vdecAspectRatio12_11:
809             avctx->sample_aspect_ratio = (AVRational) { 12, 11};
810             break;
811         case vdecAspectRatio10_11:
812             avctx->sample_aspect_ratio = (AVRational) { 10, 11};
813             break;
814         case vdecAspectRatio16_11:
815             avctx->sample_aspect_ratio = (AVRational) { 16, 11};
816             break;
817         case vdecAspectRatio40_33:
818             avctx->sample_aspect_ratio = (AVRational) { 40, 33};
819             break;
820         case vdecAspectRatio24_11:
821             avctx->sample_aspect_ratio = (AVRational) { 24, 11};
822             break;
823         case vdecAspectRatio20_11:
824             avctx->sample_aspect_ratio = (AVRational) { 20, 11};
825             break;
826         case vdecAspectRatio32_11:
827             avctx->sample_aspect_ratio = (AVRational) { 32, 11};
828             break;
829         case vdecAspectRatio80_33:
830             avctx->sample_aspect_ratio = (AVRational) { 80, 33};
831             break;
832         case vdecAspectRatio18_11:
833             avctx->sample_aspect_ratio = (AVRational) { 18, 11};
834             break;
835         case vdecAspectRatio15_11:
836             avctx->sample_aspect_ratio = (AVRational) { 15, 11};
837             break;
838         case vdecAspectRatio64_33:
839             avctx->sample_aspect_ratio = (AVRational) { 64, 33};
840             break;
841         case vdecAspectRatio160_99:
842             avctx->sample_aspect_ratio = (AVRational) {160, 99};
843             break;
844         case vdecAspectRatio4_3:
845             avctx->sample_aspect_ratio = (AVRational) {  4,  3};
846             break;
847         case vdecAspectRatio16_9:
848             avctx->sample_aspect_ratio = (AVRational) { 16,  9};
849             break;
850         case vdecAspectRatio221_1:
851             avctx->sample_aspect_ratio = (AVRational) {221,  1};
852             break;
853         }
854         return RET_COPY_AGAIN;
855     } else if (ret == BC_STS_SUCCESS) {
856         int copy_ret = -1;
857         if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
858             if (priv->last_picture == -1) {
859                 /*
860                  * Init to one less, so that the incrementing code doesn't
861                  * need to be special-cased.
862                  */
863                 priv->last_picture = output.PicInfo.picture_number - 1;
864             }
865
866             print_frame_info(priv, &output);
867
868             if (priv->last_picture + 1 < output.PicInfo.picture_number) {
869                 av_log(avctx, AV_LOG_WARNING,
870                        "CrystalHD: Picture Number discontinuity\n");
871                 /*
872                  * Have we lost frames? If so, we need to shrink the
873                  * pipeline length appropriately.
874                  *
875                  * XXX: I have no idea what the semantics of this situation
876                  * are so I don't even know if we've lost frames or which
877                  * ones.
878                  *
879                  * In any case, only warn the first time.
880                  */
881                priv->last_picture = output.PicInfo.picture_number - 1;
882             }
883
884             copy_ret = copy_frame(avctx, &output, data, got_frame);
885             if (*got_frame > 0) {
886                 avctx->has_b_frames--;
887                 priv->last_picture++;
888                 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
889                        avctx->has_b_frames);
890             }
891         } else {
892             /*
893              * An invalid frame has been consumed.
894              */
895             av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
896                                         "invalid PIB\n");
897             avctx->has_b_frames--;
898             copy_ret = RET_OK;
899         }
900         DtsReleaseOutputBuffs(dev, NULL, FALSE);
901
902         return copy_ret;
903     } else if (ret == BC_STS_BUSY) {
904         return RET_COPY_AGAIN;
905     } else {
906         av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
907         return RET_ERROR;
908     }
909 }
910
911
912 static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
913 {
914     BC_STATUS ret;
915     BC_DTS_STATUS decoder_status = { 0, };
916     CopyRet rec_ret;
917     CHDContext *priv   = avctx->priv_data;
918     HANDLE dev         = priv->dev;
919     uint8_t *in_data   = avpkt->data;
920     int len            = avpkt->size;
921     int free_data      = 0;
922     uint8_t pic_type   = 0;
923
924     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
925
926     if (len) {
927         int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
928
929         if (priv->bsfc) {
930             int ret = 0;
931             AVPacket filter_packet = { 0 };
932             AVPacket filtered_packet = { 0 };
933
934             ret = av_packet_ref(&filter_packet, avpkt);
935             if (ret < 0) {
936                 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
937                        "failed to ref input packet\n");
938                 return ret;
939             }
940
941               ret = av_bsf_send_packet(priv->bsfc, &filter_packet);
942               if (ret < 0) {
943                 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
944                        "failed to send input packet\n");
945                 return ret;
946             }
947
948             ret = av_bsf_receive_packet(priv->bsfc, &filtered_packet);
949             if (ret < 0) {
950                 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
951                        "failed to receive output packet\n");
952                 return ret;
953             }
954
955             in_data = filtered_packet.data;
956             len = filtered_packet.size;
957
958             av_packet_unref(&filter_packet);
959         }
960
961         if (priv->parser) {
962             int ret = 0;
963
964             free_data = ret > 0;
965
966             if (ret >= 0) {
967                 uint8_t *pout;
968                 int psize;
969                 int index;
970                 H264Context *h = priv->parser->priv_data;
971
972                 index = av_parser_parse2(priv->parser, avctx, &pout, &psize,
973                                          in_data, len, avpkt->pts,
974                                          avpkt->dts, 0);
975                 if (index < 0) {
976                     av_log(avctx, AV_LOG_WARNING,
977                            "CrystalHD: Failed to parse h.264 packet to "
978                            "detect interlacing.\n");
979                 } else if (index != len) {
980                     av_log(avctx, AV_LOG_WARNING,
981                            "CrystalHD: Failed to parse h.264 packet "
982                            "completely. Interlaced frames may be "
983                            "incorrectly detected.\n");
984                 } else {
985                     av_log(avctx, AV_LOG_VERBOSE,
986                            "CrystalHD: parser picture type %d\n",
987                            h->picture_structure);
988                     pic_type = h->picture_structure;
989                 }
990             } else {
991                 av_log(avctx, AV_LOG_WARNING,
992                        "CrystalHD: mp4toannexb filter failed to filter "
993                        "packet. Interlaced frames may be incorrectly "
994                        "detected.\n");
995             }
996         }
997
998         if (len < tx_free - 1024) {
999             /*
1000              * Despite being notionally opaque, either libcrystalhd or
1001              * the hardware itself will mangle pts values that are too
1002              * small or too large. The docs claim it should be in units
1003              * of 100ns. Given that we're nominally dealing with a black
1004              * box on both sides, any transform we do has no guarantee of
1005              * avoiding mangling so we need to build a mapping to values
1006              * we know will not be mangled.
1007              */
1008             int64_t safe_pts = avpkt->pts == AV_NOPTS_VALUE ? 0 : avpkt->pts;
1009             uint64_t pts = opaque_list_push(priv, safe_pts, pic_type);
1010             if (!pts) {
1011                 if (free_data) {
1012                     av_freep(&in_data);
1013                 }
1014                 return AVERROR(ENOMEM);
1015             }
1016             av_log(priv->avctx, AV_LOG_VERBOSE,
1017                    "input \"pts\": %"PRIu64"\n", pts);
1018             ret = DtsProcInput(dev, in_data, len, pts, 0);
1019             if (free_data) {
1020                 av_freep(&in_data);
1021             }
1022             if (ret == BC_STS_BUSY) {
1023                 av_log(avctx, AV_LOG_WARNING,
1024                        "CrystalHD: ProcInput returned busy\n");
1025                 usleep(BASE_WAIT);
1026                 return AVERROR(EBUSY);
1027             } else if (ret != BC_STS_SUCCESS) {
1028                 av_log(avctx, AV_LOG_ERROR,
1029                        "CrystalHD: ProcInput failed: %u\n", ret);
1030                 return -1;
1031             }
1032             avctx->has_b_frames++;
1033         } else {
1034             av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
1035             len = 0; // We didn't consume any bytes.
1036         }
1037     } else {
1038         av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
1039     }
1040
1041     if (priv->skip_next_output) {
1042         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
1043         priv->skip_next_output = 0;
1044         avctx->has_b_frames--;
1045         return len;
1046     }
1047
1048     ret = DtsGetDriverStatus(dev, &decoder_status);
1049     if (ret != BC_STS_SUCCESS) {
1050         av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
1051         return -1;
1052     }
1053
1054     /*
1055      * No frames ready. Don't try to extract.
1056      *
1057      * Empirical testing shows that ReadyListCount can be a damn lie,
1058      * and ProcOut still fails when count > 0. The same testing showed
1059      * that two more iterations were needed before ProcOutput would
1060      * succeed.
1061      */
1062     if (priv->output_ready < 2) {
1063         if (decoder_status.ReadyListCount != 0)
1064             priv->output_ready++;
1065         usleep(BASE_WAIT);
1066         av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
1067         return len;
1068     } else if (decoder_status.ReadyListCount == 0) {
1069         /*
1070          * After the pipeline is established, if we encounter a lack of frames
1071          * that probably means we're not giving the hardware enough time to
1072          * decode them, so start increasing the wait time at the end of a
1073          * decode call.
1074          */
1075         usleep(BASE_WAIT);
1076         priv->decode_wait += WAIT_UNIT;
1077         av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
1078         return len;
1079     }
1080
1081     do {
1082         rec_ret = receive_frame(avctx, data, got_frame);
1083         if (rec_ret == RET_OK && *got_frame == 0) {
1084             /*
1085              * This case is for when the encoded fields are stored
1086              * separately and we get a separate avpkt for each one. To keep
1087              * the pipeline stable, we should return nothing and wait for
1088              * the next time round to grab the second field.
1089              * H.264 PAFF is an example of this.
1090              */
1091             av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
1092             avctx->has_b_frames--;
1093         } else if (rec_ret == RET_COPY_NEXT_FIELD) {
1094             /*
1095              * This case is for when the encoded fields are stored in a
1096              * single avpkt but the hardware returns then separately. Unless
1097              * we grab the second field before returning, we'll slip another
1098              * frame in the pipeline and if that happens a lot, we're sunk.
1099              * So we have to get that second field now.
1100              * Interlaced mpeg2 and vc1 are examples of this.
1101              */
1102             av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
1103             while (1) {
1104                 usleep(priv->decode_wait);
1105                 ret = DtsGetDriverStatus(dev, &decoder_status);
1106                 if (ret == BC_STS_SUCCESS &&
1107                     decoder_status.ReadyListCount > 0) {
1108                     rec_ret = receive_frame(avctx, data, got_frame);
1109                     if ((rec_ret == RET_OK && *got_frame > 0) ||
1110                         rec_ret == RET_ERROR)
1111                         break;
1112                 }
1113             }
1114             av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
1115         } else if (rec_ret == RET_SKIP_NEXT_COPY) {
1116             /*
1117              * Two input packets got turned into a field pair. Gawd.
1118              */
1119             av_log(avctx, AV_LOG_VERBOSE,
1120                    "Don't output on next decode call.\n");
1121             priv->skip_next_output = 1;
1122         }
1123         /*
1124          * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
1125          * a FMT_CHANGE event and need to go around again for the actual frame,
1126          * we got a busy status and need to try again, or we're dealing with
1127          * packed b-frames, where the hardware strangely returns the packed
1128          * p-frame twice. We choose to keep the second copy as it carries the
1129          * valid pts.
1130          */
1131     } while (rec_ret == RET_COPY_AGAIN);
1132     usleep(priv->decode_wait);
1133     return len;
1134 }
1135
1136
1137 #if CONFIG_H264_CRYSTALHD_DECODER
1138 static AVClass h264_class = {
1139     "h264_crystalhd",
1140     av_default_item_name,
1141     options,
1142     LIBAVUTIL_VERSION_INT,
1143 };
1144
1145 AVCodec ff_h264_crystalhd_decoder = {
1146     .name           = "h264_crystalhd",
1147     .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1148     .type           = AVMEDIA_TYPE_VIDEO,
1149     .id             = AV_CODEC_ID_H264,
1150     .priv_data_size = sizeof(CHDContext),
1151     .init           = init,
1152     .close          = uninit,
1153     .decode         = decode,
1154     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1155     .flush          = flush,
1156     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1157     .priv_class     = &h264_class,
1158 };
1159 #endif
1160
1161 #if CONFIG_MPEG2_CRYSTALHD_DECODER
1162 static AVClass mpeg2_class = {
1163     "mpeg2_crystalhd",
1164     av_default_item_name,
1165     options,
1166     LIBAVUTIL_VERSION_INT,
1167 };
1168
1169 AVCodec ff_mpeg2_crystalhd_decoder = {
1170     .name           = "mpeg2_crystalhd",
1171     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
1172     .type           = AVMEDIA_TYPE_VIDEO,
1173     .id             = AV_CODEC_ID_MPEG2VIDEO,
1174     .priv_data_size = sizeof(CHDContext),
1175     .init           = init,
1176     .close          = uninit,
1177     .decode         = decode,
1178     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1179     .flush          = flush,
1180     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1181     .priv_class     = &mpeg2_class,
1182 };
1183 #endif
1184
1185 #if CONFIG_MPEG4_CRYSTALHD_DECODER
1186 static AVClass mpeg4_class = {
1187     "mpeg4_crystalhd",
1188     av_default_item_name,
1189     options,
1190     LIBAVUTIL_VERSION_INT,
1191 };
1192
1193 AVCodec ff_mpeg4_crystalhd_decoder = {
1194     .name           = "mpeg4_crystalhd",
1195     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
1196     .type           = AVMEDIA_TYPE_VIDEO,
1197     .id             = AV_CODEC_ID_MPEG4,
1198     .priv_data_size = sizeof(CHDContext),
1199     .init           = init,
1200     .close          = uninit,
1201     .decode         = decode,
1202     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1203     .flush          = flush,
1204     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1205     .priv_class     = &mpeg4_class,
1206 };
1207 #endif
1208
1209 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1210 static AVClass msmpeg4_class = {
1211     "msmpeg4_crystalhd",
1212     av_default_item_name,
1213     options,
1214     LIBAVUTIL_VERSION_INT,
1215 };
1216
1217 AVCodec ff_msmpeg4_crystalhd_decoder = {
1218     .name           = "msmpeg4_crystalhd",
1219     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1220     .type           = AVMEDIA_TYPE_VIDEO,
1221     .id             = AV_CODEC_ID_MSMPEG4V3,
1222     .priv_data_size = sizeof(CHDContext),
1223     .init           = init,
1224     .close          = uninit,
1225     .decode         = decode,
1226     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1227     .flush          = flush,
1228     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1229     .priv_class     = &msmpeg4_class,
1230 };
1231 #endif
1232
1233 #if CONFIG_VC1_CRYSTALHD_DECODER
1234 static AVClass vc1_class = {
1235     "vc1_crystalhd",
1236     av_default_item_name,
1237     options,
1238     LIBAVUTIL_VERSION_INT,
1239 };
1240
1241 AVCodec ff_vc1_crystalhd_decoder = {
1242     .name           = "vc1_crystalhd",
1243     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
1244     .type           = AVMEDIA_TYPE_VIDEO,
1245     .id             = AV_CODEC_ID_VC1,
1246     .priv_data_size = sizeof(CHDContext),
1247     .init           = init,
1248     .close          = uninit,
1249     .decode         = decode,
1250     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1251     .flush          = flush,
1252     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1253     .priv_class     = &vc1_class,
1254 };
1255 #endif
1256
1257 #if CONFIG_WMV3_CRYSTALHD_DECODER
1258 static AVClass wmv3_class = {
1259     "wmv3_crystalhd",
1260     av_default_item_name,
1261     options,
1262     LIBAVUTIL_VERSION_INT,
1263 };
1264
1265 AVCodec ff_wmv3_crystalhd_decoder = {
1266     .name           = "wmv3_crystalhd",
1267     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
1268     .type           = AVMEDIA_TYPE_VIDEO,
1269     .id             = AV_CODEC_ID_WMV3,
1270     .priv_data_size = sizeof(CHDContext),
1271     .init           = init,
1272     .close          = uninit,
1273     .decode         = decode,
1274     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1275     .flush          = flush,
1276     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1277     .priv_class     = &wmv3_class,
1278 };
1279 #endif