]> git.sesse.net Git - ffmpeg/blob - libavcodec/crystalhd.c
Merge remote-tracking branch 'newdev/master'
[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 outputing
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 "libavutil/imgutils.h"
88 #include "libavutil/intreadwrite.h"
89
90 /** Timeout parameter passed to DtsProcOutput() in us */
91 #define OUTPUT_PROC_TIMEOUT 50
92 /** Step between fake timestamps passed to hardware in units of 100ns */
93 #define TIMESTAMP_UNIT 100000
94 /** Initial value in us of the wait in decode() */
95 #define BASE_WAIT 10000
96 /** Increment in us to adjust wait in decode() */
97 #define WAIT_UNIT 1000
98
99
100 /*****************************************************************************
101  * Module private data
102  ****************************************************************************/
103
104 typedef enum {
105     RET_ERROR          = -1,
106     RET_OK             = 0,
107     RET_COPY_AGAIN     = 1,
108     RET_SKIP_NEXT_COPY = 2,
109 } CopyRet;
110
111 typedef struct OpaqueList {
112     struct OpaqueList *next;
113     uint64_t fake_timestamp;
114     uint64_t reordered_opaque;
115 } OpaqueList;
116
117 typedef struct {
118     AVCodecContext *avctx;
119     AVFrame pic;
120     HANDLE dev;
121
122     uint8_t is_70012;
123     uint8_t *sps_pps_buf;
124     uint32_t sps_pps_size;
125     uint8_t is_nal;
126     uint8_t output_ready;
127     uint8_t need_second_field;
128     uint8_t skip_next_output;
129     uint64_t decode_wait;
130
131     uint64_t last_picture;
132
133     OpaqueList *head;
134     OpaqueList *tail;
135 } CHDContext;
136
137
138 /*****************************************************************************
139  * Helper functions
140  ****************************************************************************/
141
142 static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum CodecID id)
143 {
144     switch (id) {
145     case CODEC_ID_MPEG4:
146         return BC_MSUBTYPE_DIVX;
147     case CODEC_ID_MSMPEG4V3:
148         return BC_MSUBTYPE_DIVX311;
149     case CODEC_ID_MPEG2VIDEO:
150         return BC_MSUBTYPE_MPEG2VIDEO;
151     case CODEC_ID_VC1:
152         return BC_MSUBTYPE_VC1;
153     case CODEC_ID_WMV3:
154         return BC_MSUBTYPE_WMV3;
155     case CODEC_ID_H264:
156         return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
157     default:
158         return BC_MSUBTYPE_INVALID;
159     }
160 }
161
162 static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
163 {
164     av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
165     av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
166            output->YBuffDoneSz);
167     av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
168            output->UVBuffDoneSz);
169     av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n",
170            output->PicInfo.timeStamp);
171     av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
172            output->PicInfo.picture_number);
173     av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
174            output->PicInfo.width);
175     av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
176            output->PicInfo.height);
177     av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
178            output->PicInfo.chroma_format);
179     av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
180            output->PicInfo.pulldown);
181     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
182            output->PicInfo.flags);
183     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
184            output->PicInfo.frame_rate);
185     av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
186            output->PicInfo.aspect_ratio);
187     av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
188            output->PicInfo.colour_primaries);
189     av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
190            output->PicInfo.picture_meta_payload);
191     av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
192            output->PicInfo.sess_num);
193     av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
194            output->PicInfo.ycom);
195     av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
196            output->PicInfo.custom_aspect_ratio_width_height);
197     av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
198            output->PicInfo.n_drop);
199     av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
200            output->PicInfo.other.h264.valid);
201 }
202
203
204 /*****************************************************************************
205  * OpaqueList functions
206  ****************************************************************************/
207
208 static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque)
209 {
210     OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
211     if (!newNode) {
212         av_log(priv->avctx, AV_LOG_ERROR,
213                "Unable to allocate new node in OpaqueList.\n");
214         return 0;
215     }
216     if (!priv->head) {
217         newNode->fake_timestamp = TIMESTAMP_UNIT;
218         priv->head              = newNode;
219     } else {
220         newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
221         priv->tail->next        = newNode;
222     }
223     priv->tail = newNode;
224     newNode->reordered_opaque = reordered_opaque;
225
226     return newNode->fake_timestamp;
227 }
228
229 /*
230  * The OpaqueList is built in decode order, while elements will be removed
231  * in presentation order. If frames are reordered, this means we must be
232  * able to remove elements that are not the first element.
233  */
234 static uint64_t opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
235 {
236     OpaqueList *node = priv->head;
237
238     if (!priv->head) {
239         av_log(priv->avctx, AV_LOG_ERROR,
240                "CrystalHD: Attempted to query non-existent timestamps.\n");
241         return AV_NOPTS_VALUE;
242     }
243
244     /*
245      * The first element is special-cased because we have to manipulate
246      * the head pointer rather than the previous element in the list.
247      */
248     if (priv->head->fake_timestamp == fake_timestamp) {
249         uint64_t reordered_opaque = node->reordered_opaque;
250         priv->head = node->next;
251         av_free(node);
252
253         if (!priv->head->next)
254             priv->tail = priv->head;
255
256         return reordered_opaque;
257     }
258
259     /*
260      * The list is processed at arm's length so that we have the
261      * previous element available to rewrite its next pointer.
262      */
263     while (node->next) {
264         OpaqueList *next = node->next;
265         if (next->fake_timestamp == fake_timestamp) {
266             uint64_t reordered_opaque = next->reordered_opaque;
267             node->next = next->next;
268             av_free(next);
269
270             if (!node->next)
271                priv->tail = node;
272
273             return reordered_opaque;
274         } else {
275             node = next;
276         }
277     }
278
279     av_log(priv->avctx, AV_LOG_VERBOSE,
280            "CrystalHD: Couldn't match fake_timestamp.\n");
281     return AV_NOPTS_VALUE;
282 }
283
284
285 /*****************************************************************************
286  * Video decoder API function definitions
287  ****************************************************************************/
288
289 static void flush(AVCodecContext *avctx)
290 {
291     CHDContext *priv = avctx->priv_data;
292
293     avctx->has_b_frames     = 0;
294     priv->last_picture      = -1;
295     priv->output_ready      = 0;
296     priv->need_second_field = 0;
297     priv->skip_next_output  = 0;
298     priv->decode_wait       = BASE_WAIT;
299
300     if (priv->pic.data[0])
301         avctx->release_buffer(avctx, &priv->pic);
302
303     /* Flush mode 4 flushes all software and hardware buffers. */
304     DtsFlushInput(priv->dev, 4);
305 }
306
307
308 static av_cold int uninit(AVCodecContext *avctx)
309 {
310     CHDContext *priv = avctx->priv_data;
311     HANDLE device;
312
313     device = priv->dev;
314     DtsStopDecoder(device);
315     DtsCloseDecoder(device);
316     DtsDeviceClose(device);
317
318     av_free(priv->sps_pps_buf);
319
320     if (priv->pic.data[0])
321         avctx->release_buffer(avctx, &priv->pic);
322
323     if (priv->head) {
324        OpaqueList *node = priv->head;
325        while (node) {
326           OpaqueList *next = node->next;
327           av_free(node);
328           node = next;
329        }
330     }
331
332     return 0;
333 }
334
335
336 static av_cold int init(AVCodecContext *avctx)
337 {
338     CHDContext* priv;
339     BC_STATUS ret;
340     BC_INFO_CRYSTAL version;
341     BC_INPUT_FORMAT format = {
342         .FGTEnable   = FALSE,
343         .Progressive = TRUE,
344         .OptFlags    = 0x80000000 | vdecFrameRate59_94 | 0x40,
345         .width       = avctx->width,
346         .height      = avctx->height,
347     };
348
349     BC_MEDIA_SUBTYPE subtype;
350
351     uint32_t mode = DTS_PLAYBACK_MODE |
352                     DTS_LOAD_FILE_PLAY_FW |
353                     DTS_SKIP_TX_CHK_CPB |
354                     DTS_PLAYBACK_DROP_RPT_MODE |
355                     DTS_SINGLE_THREADED_MODE |
356                     DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
357
358     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
359            avctx->codec->name);
360
361     avctx->pix_fmt = PIX_FMT_YUYV422;
362
363     /* Initialize the library */
364     priv               = avctx->priv_data;
365     priv->avctx        = avctx;
366     priv->is_nal       = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
367     priv->last_picture = -1;
368     priv->decode_wait  = BASE_WAIT;
369
370     subtype = id2subtype(priv, avctx->codec->id);
371     switch (subtype) {
372     case BC_MSUBTYPE_AVC1:
373         {
374             uint8_t *dummy_p;
375             int dummy_int;
376             AVBitStreamFilterContext *bsfc;
377
378             uint32_t orig_data_size = avctx->extradata_size;
379             uint8_t *orig_data = av_malloc(orig_data_size);
380             if (!orig_data) {
381                 av_log(avctx, AV_LOG_ERROR,
382                        "Failed to allocate copy of extradata\n");
383                 return AVERROR(ENOMEM);
384             }
385             memcpy(orig_data, avctx->extradata, orig_data_size);
386
387
388             bsfc = av_bitstream_filter_init("h264_mp4toannexb");
389             if (!bsfc) {
390                 av_log(avctx, AV_LOG_ERROR,
391                        "Cannot open the h264_mp4toannexb BSF!\n");
392                 av_free(orig_data);
393                 return AVERROR_BSF_NOT_FOUND;
394             }
395             av_bitstream_filter_filter(bsfc, avctx, NULL, &dummy_p,
396                                        &dummy_int, NULL, 0, 0);
397             av_bitstream_filter_close(bsfc);
398
399             priv->sps_pps_buf     = avctx->extradata;
400             priv->sps_pps_size    = avctx->extradata_size;
401             avctx->extradata      = orig_data;
402             avctx->extradata_size = orig_data_size;
403
404             format.pMetaData   = priv->sps_pps_buf;
405             format.metaDataSz  = priv->sps_pps_size;
406             format.startCodeSz = (avctx->extradata[4] & 0x03) + 1;
407         }
408         break;
409     case BC_MSUBTYPE_H264:
410         format.startCodeSz = 4;
411         // Fall-through
412     case BC_MSUBTYPE_VC1:
413     case BC_MSUBTYPE_WVC1:
414     case BC_MSUBTYPE_WMV3:
415     case BC_MSUBTYPE_WMVA:
416     case BC_MSUBTYPE_MPEG2VIDEO:
417     case BC_MSUBTYPE_DIVX:
418     case BC_MSUBTYPE_DIVX311:
419         format.pMetaData  = avctx->extradata;
420         format.metaDataSz = avctx->extradata_size;
421         break;
422     default:
423         av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
424         return AVERROR(EINVAL);
425     }
426     format.mSubtype = subtype;
427
428     /* Get a decoder instance */
429     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
430     // Initialize the Link and Decoder devices
431     ret = DtsDeviceOpen(&priv->dev, mode);
432     if (ret != BC_STS_SUCCESS) {
433         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
434         goto fail;
435     }
436
437     ret = DtsCrystalHDVersion(priv->dev, &version);
438     if (ret != BC_STS_SUCCESS) {
439         av_log(avctx, AV_LOG_VERBOSE,
440                "CrystalHD: DtsCrystalHDVersion failed\n");
441         goto fail;
442     }
443     priv->is_70012 = version.device == 0;
444
445     if (priv->is_70012 &&
446         (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
447         av_log(avctx, AV_LOG_VERBOSE,
448                "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
449         goto fail;
450     }
451
452     ret = DtsSetInputFormat(priv->dev, &format);
453     if (ret != BC_STS_SUCCESS) {
454         av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
455         goto fail;
456     }
457
458     ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
459     if (ret != BC_STS_SUCCESS) {
460         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
461         goto fail;
462     }
463
464     ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
465     if (ret != BC_STS_SUCCESS) {
466         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
467         goto fail;
468     }
469     ret = DtsStartDecoder(priv->dev);
470     if (ret != BC_STS_SUCCESS) {
471         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
472         goto fail;
473     }
474     ret = DtsStartCapture(priv->dev);
475     if (ret != BC_STS_SUCCESS) {
476         av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
477         goto fail;
478     }
479
480     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
481
482     return 0;
483
484  fail:
485     uninit(avctx);
486     return -1;
487 }
488
489
490 /*
491  * The CrystalHD doesn't report interlaced H.264 content in a way that allows
492  * us to distinguish between specific cases that require different handling.
493  * So, for now, we have to hard-code the behaviour we want.
494  *
495  * The default behaviour is to assume MBAFF with input and output fieldpairs.
496  *
497  * Define ASSUME_PAFF_OVER_MBAFF to treat input as PAFF with separate input
498  * and output fields.
499  *
500  * Define ASSUME_TWO_INPUTS_ONE_OUTPUT to treat input as separate fields but
501  * output as a single fieldpair.
502  *
503  * Define both to mess up your playback.
504  */
505 #define ASSUME_PAFF_OVER_MBAFF 0
506 #define ASSUME_TWO_INPUTS_ONE_OUTPUT 0
507 static inline CopyRet copy_frame(AVCodecContext *avctx,
508                                  BC_DTS_PROC_OUT *output,
509                                  void *data, int *data_size,
510                                  uint8_t second_field)
511 {
512     BC_STATUS ret;
513     BC_DTS_STATUS decoder_status;
514     uint8_t is_paff;
515     uint8_t next_frame_same;
516     uint8_t interlaced;
517
518     CHDContext *priv = avctx->priv_data;
519
520     uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
521                            VDEC_FLAG_BOTTOMFIELD;
522     uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
523
524     int width    = output->PicInfo.width;
525     int height   = output->PicInfo.height;
526     int bwidth;
527     uint8_t *src = output->Ybuff;
528     int sStride;
529     uint8_t *dst;
530     int dStride;
531
532     ret = DtsGetDriverStatus(priv->dev, &decoder_status);
533     if (ret != BC_STS_SUCCESS) {
534         av_log(avctx, AV_LOG_ERROR,
535                "CrystalHD: GetDriverStatus failed: %u\n", ret);
536        return RET_ERROR;
537     }
538
539     is_paff           = ASSUME_PAFF_OVER_MBAFF ||
540                         !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC);
541     next_frame_same   = output->PicInfo.picture_number ==
542                         (decoder_status.picNumFlags & ~0x40000000);
543     interlaced        = ((output->PicInfo.flags &
544                           VDEC_FLAG_INTERLACED_SRC) && is_paff) ||
545                          next_frame_same || bottom_field || second_field;
546
547     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: next_frame_same: %u | %u | %u\n",
548            next_frame_same, output->PicInfo.picture_number,
549            decoder_status.picNumFlags & ~0x40000000);
550
551     if (priv->pic.data[0] && !priv->need_second_field)
552         avctx->release_buffer(avctx, &priv->pic);
553
554     priv->need_second_field = interlaced && !priv->need_second_field;
555
556     priv->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
557                              FF_BUFFER_HINTS_REUSABLE;
558     if (!priv->pic.data[0]) {
559         if (avctx->get_buffer(avctx, &priv->pic) < 0) {
560             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
561             return RET_ERROR;
562         }
563     }
564
565     bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
566     if (priv->is_70012) {
567         int pStride;
568
569         if (width <= 720)
570             pStride = 720;
571         else if (width <= 1280)
572             pStride = 1280;
573         else if (width <= 1080)
574             pStride = 1080;
575         sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
576     } else {
577         sStride = bwidth;
578     }
579
580     dStride = priv->pic.linesize[0];
581     dst     = priv->pic.data[0];
582
583     av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
584
585     if (interlaced) {
586         int dY = 0;
587         int sY = 0;
588
589         height /= 2;
590         if (bottom_field) {
591             av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
592             dY = 1;
593         } else {
594             av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
595             dY = 0;
596         }
597
598         for (sY = 0; sY < height; dY++, sY++) {
599             memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
600             dY++;
601         }
602     } else {
603         av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
604     }
605
606     priv->pic.interlaced_frame = interlaced;
607     if (interlaced)
608         priv->pic.top_field_first = !bottom_first;
609
610     if (output->PicInfo.timeStamp != 0) {
611         priv->pic.pkt_pts = opaque_list_pop(priv, output->PicInfo.timeStamp);
612         av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
613                priv->pic.pkt_pts);
614     }
615
616     if (!priv->need_second_field) {
617         *data_size       = sizeof(AVFrame);
618         *(AVFrame *)data = priv->pic;
619     }
620
621     if (ASSUME_TWO_INPUTS_ONE_OUTPUT &&
622         output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) {
623         av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
624         return RET_SKIP_NEXT_COPY;
625     }
626
627     return RET_OK;
628 }
629
630
631 static inline CopyRet receive_frame(AVCodecContext *avctx,
632                                     void *data, int *data_size,
633                                     uint8_t second_field)
634 {
635     BC_STATUS ret;
636     BC_DTS_PROC_OUT output = {
637         .PicInfo.width  = avctx->width,
638         .PicInfo.height = avctx->height,
639     };
640     CHDContext *priv = avctx->priv_data;
641     HANDLE dev       = priv->dev;
642
643     *data_size = 0;
644
645     // Request decoded data from the driver
646     ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
647     if (ret == BC_STS_FMT_CHANGE) {
648         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
649         avctx->width  = output.PicInfo.width;
650         avctx->height = output.PicInfo.height;
651         return RET_COPY_AGAIN;
652     } else if (ret == BC_STS_SUCCESS) {
653         int copy_ret = -1;
654         if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
655             if (priv->last_picture == -1) {
656                 /*
657                  * Init to one less, so that the incrementing code doesn't
658                  * need to be special-cased.
659                  */
660                 priv->last_picture = output.PicInfo.picture_number - 1;
661             }
662
663             if (avctx->codec->id == CODEC_ID_MPEG4 &&
664                 output.PicInfo.timeStamp == 0) {
665                 av_log(avctx, AV_LOG_VERBOSE,
666                        "CrystalHD: Not returning packed frame twice.\n");
667                 priv->last_picture++;
668                 DtsReleaseOutputBuffs(dev, NULL, FALSE);
669                 return RET_COPY_AGAIN;
670             }
671
672             print_frame_info(priv, &output);
673
674             if (priv->last_picture + 1 < output.PicInfo.picture_number) {
675                 av_log(avctx, AV_LOG_WARNING,
676                        "CrystalHD: Picture Number discontinuity\n");
677                 /*
678                  * Have we lost frames? If so, we need to shrink the
679                  * pipeline length appropriately.
680                  *
681                  * XXX: I have no idea what the semantics of this situation
682                  * are so I don't even know if we've lost frames or which
683                  * ones.
684                  *
685                  * In any case, only warn the first time.
686                  */
687                priv->last_picture = output.PicInfo.picture_number - 1;
688             }
689
690             copy_ret = copy_frame(avctx, &output, data, data_size, second_field);
691             if (*data_size > 0) {
692                 avctx->has_b_frames--;
693                 priv->last_picture++;
694                 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
695                        avctx->has_b_frames);
696             }
697         } else {
698             /*
699              * An invalid frame has been consumed.
700              */
701             av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
702                                         "invalid PIB\n");
703             avctx->has_b_frames--;
704             copy_ret = RET_OK;
705         }
706         DtsReleaseOutputBuffs(dev, NULL, FALSE);
707
708         return copy_ret;
709     } else if (ret == BC_STS_BUSY) {
710         return RET_COPY_AGAIN;
711     } else {
712         av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
713         return RET_ERROR;
714     }
715 }
716
717
718 static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
719 {
720     BC_STATUS ret;
721     BC_DTS_STATUS decoder_status;
722     CopyRet rec_ret;
723     CHDContext *priv   = avctx->priv_data;
724     HANDLE dev         = priv->dev;
725     int len            = avpkt->size;
726
727     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
728
729     if (len) {
730         int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
731         if (len < tx_free - 1024) {
732             /*
733              * Despite being notionally opaque, either libcrystalhd or
734              * the hardware itself will mangle pts values that are too
735              * small or too large. The docs claim it should be in units
736              * of 100ns. Given that we're nominally dealing with a black
737              * box on both sides, any transform we do has no guarantee of
738              * avoiding mangling so we need to build a mapping to values
739              * we know will not be mangled.
740              */
741             uint64_t pts = opaque_list_push(priv, avctx->pkt->pts);
742             if (!pts) {
743                 return AVERROR(ENOMEM);
744             }
745             av_log(priv->avctx, AV_LOG_VERBOSE,
746                    "input \"pts\": %"PRIu64"\n", pts);
747             ret = DtsProcInput(dev, avpkt->data, len, pts, 0);
748             if (ret == BC_STS_BUSY) {
749                 av_log(avctx, AV_LOG_WARNING,
750                        "CrystalHD: ProcInput returned busy\n");
751                 usleep(BASE_WAIT);
752                 return AVERROR(EBUSY);
753             } else if (ret != BC_STS_SUCCESS) {
754                 av_log(avctx, AV_LOG_ERROR,
755                        "CrystalHD: ProcInput failed: %u\n", ret);
756                 return -1;
757             }
758             avctx->has_b_frames++;
759         } else {
760             av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
761             len = 0; // We didn't consume any bytes.
762         }
763     } else {
764         av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
765     }
766
767     if (priv->skip_next_output) {
768         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
769         priv->skip_next_output = 0;
770         avctx->has_b_frames--;
771         return len;
772     }
773
774     ret = DtsGetDriverStatus(dev, &decoder_status);
775     if (ret != BC_STS_SUCCESS) {
776         av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
777         return -1;
778     }
779
780     /*
781      * No frames ready. Don't try to extract.
782      *
783      * Empirical testing shows that ReadyListCount can be a damn lie,
784      * and ProcOut still fails when count > 0. The same testing showed
785      * that two more iterations were needed before ProcOutput would
786      * succeed.
787      */
788     if (priv->output_ready < 2) {
789         if (decoder_status.ReadyListCount != 0)
790             priv->output_ready++;
791         usleep(BASE_WAIT);
792         av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
793         return len;
794     } else if (decoder_status.ReadyListCount == 0) {
795         /*
796          * After the pipeline is established, if we encounter a lack of frames
797          * that probably means we're not giving the hardware enough time to
798          * decode them, so start increasing the wait time at the end of a
799          * decode call.
800          */
801         usleep(BASE_WAIT);
802         priv->decode_wait += WAIT_UNIT;
803         av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
804         return len;
805     }
806
807     do {
808         rec_ret = receive_frame(avctx, data, data_size, 0);
809         if (rec_ret == 0 && *data_size == 0) {
810             if (avctx->codec->id == CODEC_ID_H264) {
811                 /*
812                  * This case is for when the encoded fields are stored
813                  * separately and we get a separate avpkt for each one. To keep
814                  * the pipeline stable, we should return nothing and wait for
815                  * the next time round to grab the second field.
816                  * H.264 PAFF is an example of this.
817                  */
818                 av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
819                 avctx->has_b_frames--;
820             } else {
821                 /*
822                  * This case is for when the encoded fields are stored in a
823                  * single avpkt but the hardware returns then separately. Unless
824                  * we grab the second field before returning, we'll slip another
825                  * frame in the pipeline and if that happens a lot, we're sunk.
826                  * So we have to get that second field now.
827                  * Interlaced mpeg2 and vc1 are examples of this.
828                  */
829                 av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
830                 while (1) {
831                     usleep(priv->decode_wait);
832                     ret = DtsGetDriverStatus(dev, &decoder_status);
833                     if (ret == BC_STS_SUCCESS &&
834                         decoder_status.ReadyListCount > 0) {
835                         rec_ret = receive_frame(avctx, data, data_size, 1);
836                         if ((rec_ret == 0 && *data_size > 0) ||
837                             rec_ret == RET_ERROR)
838                             break;
839                     }
840                 }
841                 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
842             }
843         } else if (rec_ret == RET_SKIP_NEXT_COPY) {
844             /*
845              * Two input packets got turned into a field pair. Gawd.
846              */
847             av_log(avctx, AV_LOG_VERBOSE,
848                    "Don't output on next decode call.\n");
849             priv->skip_next_output = 1;
850         }
851         /*
852          * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
853          * a FMT_CHANGE event and need to go around again for the actual frame,
854          * we got a busy status and need to try again, or we're dealing with
855          * packed b-frames, where the hardware strangely returns the packed
856          * p-frame twice. We choose to keep the second copy as it carries the
857          * valid pts.
858          */
859     } while (rec_ret == RET_COPY_AGAIN);
860     usleep(priv->decode_wait);
861     return len;
862 }
863
864
865 #if CONFIG_H264_CRYSTALHD_DECODER
866 AVCodec ff_h264_crystalhd_decoder = {
867     .name           = "h264_crystalhd",
868     .type           = AVMEDIA_TYPE_VIDEO,
869     .id             = CODEC_ID_H264,
870     .priv_data_size = sizeof(CHDContext),
871     .init           = init,
872     .close          = uninit,
873     .decode         = decode,
874     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
875     .flush          = flush,
876     .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
877     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
878 };
879 #endif
880
881 #if CONFIG_MPEG2_CRYSTALHD_DECODER
882 AVCodec ff_mpeg2_crystalhd_decoder = {
883     .name           = "mpeg2_crystalhd",
884     .type           = AVMEDIA_TYPE_VIDEO,
885     .id             = CODEC_ID_MPEG2VIDEO,
886     .priv_data_size = sizeof(CHDContext),
887     .init           = init,
888     .close          = uninit,
889     .decode         = decode,
890     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
891     .flush          = flush,
892     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
893     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
894 };
895 #endif
896
897 #if CONFIG_MPEG4_CRYSTALHD_DECODER
898 AVCodec ff_mpeg4_crystalhd_decoder = {
899     .name           = "mpeg4_crystalhd",
900     .type           = AVMEDIA_TYPE_VIDEO,
901     .id             = CODEC_ID_MPEG4,
902     .priv_data_size = sizeof(CHDContext),
903     .init           = init,
904     .close          = uninit,
905     .decode         = decode,
906     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
907     .flush          = flush,
908     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
909     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
910 };
911 #endif
912
913 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
914 AVCodec ff_msmpeg4_crystalhd_decoder = {
915     .name           = "msmpeg4_crystalhd",
916     .type           = AVMEDIA_TYPE_VIDEO,
917     .id             = CODEC_ID_MSMPEG4V3,
918     .priv_data_size = sizeof(CHDContext),
919     .init           = init,
920     .close          = uninit,
921     .decode         = decode,
922     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
923     .flush          = flush,
924     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
925     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
926 };
927 #endif
928
929 #if CONFIG_VC1_CRYSTALHD_DECODER
930 AVCodec ff_vc1_crystalhd_decoder = {
931     .name           = "vc1_crystalhd",
932     .type           = AVMEDIA_TYPE_VIDEO,
933     .id             = CODEC_ID_VC1,
934     .priv_data_size = sizeof(CHDContext),
935     .init           = init,
936     .close          = uninit,
937     .decode         = decode,
938     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
939     .flush          = flush,
940     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
941     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
942 };
943 #endif
944
945 #if CONFIG_WMV3_CRYSTALHD_DECODER
946 AVCodec ff_wmv3_crystalhd_decoder = {
947     .name           = "wmv3_crystalhd",
948     .type           = AVMEDIA_TYPE_VIDEO,
949     .id             = CODEC_ID_WMV3,
950     .priv_data_size = sizeof(CHDContext),
951     .init           = init,
952     .close          = uninit,
953     .decode         = decode,
954     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
955     .flush          = flush,
956     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
957     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
958 };
959 #endif