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