]> 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 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             if (interlaced)
601                 dY++;
602         }
603     } else {
604         av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
605     }
606
607     priv->pic.interlaced_frame = interlaced;
608     if (interlaced)
609         priv->pic.top_field_first = !bottom_first;
610
611     if (output->PicInfo.timeStamp != 0) {
612         priv->pic.pkt_pts = opaque_list_pop(priv, output->PicInfo.timeStamp);
613         av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
614                priv->pic.pkt_pts);
615     }
616
617     if (!priv->need_second_field) {
618         *data_size       = sizeof(AVFrame);
619         *(AVFrame *)data = priv->pic;
620     }
621
622     if (ASSUME_TWO_INPUTS_ONE_OUTPUT &&
623         output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) {
624         av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
625         return RET_SKIP_NEXT_COPY;
626     }
627
628     return RET_OK;
629 }
630
631
632 static inline CopyRet receive_frame(AVCodecContext *avctx,
633                                     void *data, int *data_size,
634                                     uint8_t second_field)
635 {
636     BC_STATUS ret;
637     BC_DTS_PROC_OUT output = {
638         .PicInfo.width  = avctx->width,
639         .PicInfo.height = avctx->height,
640     };
641     CHDContext *priv = avctx->priv_data;
642     HANDLE dev       = priv->dev;
643
644     *data_size = 0;
645
646     // Request decoded data from the driver
647     ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
648     if (ret == BC_STS_FMT_CHANGE) {
649         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
650         avctx->width  = output.PicInfo.width;
651         avctx->height = output.PicInfo.height;
652         return RET_COPY_AGAIN;
653     } else if (ret == BC_STS_SUCCESS) {
654         int copy_ret = -1;
655         if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
656             if (priv->last_picture == -1) {
657                 /*
658                  * Init to one less, so that the incrementing code doesn't
659                  * need to be special-cased.
660                  */
661                 priv->last_picture = output.PicInfo.picture_number - 1;
662             }
663
664             if (avctx->codec->id == CODEC_ID_MPEG4 &&
665                 output.PicInfo.timeStamp == 0) {
666                 av_log(avctx, AV_LOG_VERBOSE,
667                        "CrystalHD: Not returning packed frame twice.\n");
668                 priv->last_picture++;
669                 DtsReleaseOutputBuffs(dev, NULL, FALSE);
670                 return RET_COPY_AGAIN;
671             }
672
673             print_frame_info(priv, &output);
674
675             if (priv->last_picture + 1 < output.PicInfo.picture_number) {
676                 av_log(avctx, AV_LOG_WARNING,
677                        "CrystalHD: Picture Number discontinuity\n");
678                 /*
679                  * Have we lost frames? If so, we need to shrink the
680                  * pipeline length appropriately.
681                  *
682                  * XXX: I have no idea what the semantics of this situation
683                  * are so I don't even know if we've lost frames or which
684                  * ones.
685                  *
686                  * In any case, only warn the first time.
687                  */
688                priv->last_picture = output.PicInfo.picture_number - 1;
689             }
690
691             copy_ret = copy_frame(avctx, &output, data, data_size, second_field);
692             if (*data_size > 0) {
693                 avctx->has_b_frames--;
694                 priv->last_picture++;
695                 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
696                        avctx->has_b_frames);
697             }
698         } else {
699             /*
700              * An invalid frame has been consumed.
701              */
702             av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
703                                         "invalid PIB\n");
704             avctx->has_b_frames--;
705             copy_ret = RET_OK;
706         }
707         DtsReleaseOutputBuffs(dev, NULL, FALSE);
708
709         return copy_ret;
710     } else if (ret == BC_STS_BUSY) {
711         return RET_COPY_AGAIN;
712     } else {
713         av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
714         return RET_ERROR;
715     }
716 }
717
718
719 static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
720 {
721     BC_STATUS ret;
722     BC_DTS_STATUS decoder_status;
723     CopyRet rec_ret;
724     CHDContext *priv   = avctx->priv_data;
725     HANDLE dev         = priv->dev;
726     int len            = avpkt->size;
727
728     av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
729
730     if (len) {
731         int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
732         if (len < tx_free - 1024) {
733             /*
734              * Despite being notionally opaque, either libcrystalhd or
735              * the hardware itself will mangle pts values that are too
736              * small or too large. The docs claim it should be in units
737              * of 100ns. Given that we're nominally dealing with a black
738              * box on both sides, any transform we do has no guarantee of
739              * avoiding mangling so we need to build a mapping to values
740              * we know will not be mangled.
741              */
742             uint64_t pts = opaque_list_push(priv, avctx->pkt->pts);
743             if (!pts) {
744                 return AVERROR(ENOMEM);
745             }
746             av_log(priv->avctx, AV_LOG_VERBOSE,
747                    "input \"pts\": %"PRIu64"\n", pts);
748             ret = DtsProcInput(dev, avpkt->data, len, pts, 0);
749             if (ret == BC_STS_BUSY) {
750                 av_log(avctx, AV_LOG_WARNING,
751                        "CrystalHD: ProcInput returned busy\n");
752                 usleep(BASE_WAIT);
753                 return AVERROR(EBUSY);
754             } else if (ret != BC_STS_SUCCESS) {
755                 av_log(avctx, AV_LOG_ERROR,
756                        "CrystalHD: ProcInput failed: %u\n", ret);
757                 return -1;
758             }
759             avctx->has_b_frames++;
760         } else {
761             av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
762             len = 0; // We didn't consume any bytes.
763         }
764     } else {
765         av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
766     }
767
768     if (priv->skip_next_output) {
769         av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
770         priv->skip_next_output = 0;
771         avctx->has_b_frames--;
772         return len;
773     }
774
775     ret = DtsGetDriverStatus(dev, &decoder_status);
776     if (ret != BC_STS_SUCCESS) {
777         av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
778         return -1;
779     }
780
781     /*
782      * No frames ready. Don't try to extract.
783      *
784      * Empirical testing shows that ReadyListCount can be a damn lie,
785      * and ProcOut still fails when count > 0. The same testing showed
786      * that two more iterations were needed before ProcOutput would
787      * succeed.
788      */
789     if (priv->output_ready < 2) {
790         if (decoder_status.ReadyListCount != 0)
791             priv->output_ready++;
792         usleep(BASE_WAIT);
793         av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
794         return len;
795     } else if (decoder_status.ReadyListCount == 0) {
796         /*
797          * After the pipeline is established, if we encounter a lack of frames
798          * that probably means we're not giving the hardware enough time to
799          * decode them, so start increasing the wait time at the end of a
800          * decode call.
801          */
802         usleep(BASE_WAIT);
803         priv->decode_wait += WAIT_UNIT;
804         av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
805         return len;
806     }
807
808     do {
809         rec_ret = receive_frame(avctx, data, data_size, 0);
810         if (rec_ret == 0 && *data_size == 0) {
811             if (avctx->codec->id == CODEC_ID_H264) {
812                 /*
813                  * This case is for when the encoded fields are stored
814                  * separately and we get a separate avpkt for each one. To keep
815                  * the pipeline stable, we should return nothing and wait for
816                  * the next time round to grab the second field.
817                  * H.264 PAFF is an example of this.
818                  */
819                 av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
820                 avctx->has_b_frames--;
821             } else {
822                 /*
823                  * This case is for when the encoded fields are stored in a
824                  * single avpkt but the hardware returns then separately. Unless
825                  * we grab the second field before returning, we'll slip another
826                  * frame in the pipeline and if that happens a lot, we're sunk.
827                  * So we have to get that second field now.
828                  * Interlaced mpeg2 and vc1 are examples of this.
829                  */
830                 av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
831                 while (1) {
832                     usleep(priv->decode_wait);
833                     ret = DtsGetDriverStatus(dev, &decoder_status);
834                     if (ret == BC_STS_SUCCESS &&
835                         decoder_status.ReadyListCount > 0) {
836                         rec_ret = receive_frame(avctx, data, data_size, 1);
837                         if ((rec_ret == 0 && *data_size > 0) ||
838                             rec_ret == RET_ERROR)
839                             break;
840                     }
841                 }
842                 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
843             }
844         } else if (rec_ret == RET_SKIP_NEXT_COPY) {
845             /*
846              * Two input packets got turned into a field pair. Gawd.
847              */
848             av_log(avctx, AV_LOG_VERBOSE,
849                    "Don't output on next decode call.\n");
850             priv->skip_next_output = 1;
851         }
852         /*
853          * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
854          * a FMT_CHANGE event and need to go around again for the actual frame,
855          * we got a busy status and need to try again, or we're dealing with
856          * packed b-frames, where the hardware strangely returns the packed
857          * p-frame twice. We choose to keep the second copy as it carries the
858          * valid pts.
859          */
860     } while (rec_ret == RET_COPY_AGAIN);
861     usleep(priv->decode_wait);
862     return len;
863 }
864
865
866 #if CONFIG_H264_CRYSTALHD_DECODER
867 AVCodec ff_h264_crystalhd_decoder = {
868     .name           = "h264_crystalhd",
869     .type           = AVMEDIA_TYPE_VIDEO,
870     .id             = CODEC_ID_H264,
871     .priv_data_size = sizeof(CHDContext),
872     .init           = init,
873     .close          = uninit,
874     .decode         = decode,
875     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
876     .flush          = flush,
877     .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
878     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
879 };
880 #endif
881
882 #if CONFIG_MPEG2_CRYSTALHD_DECODER
883 AVCodec ff_mpeg2_crystalhd_decoder = {
884     .name           = "mpeg2_crystalhd",
885     .type           = AVMEDIA_TYPE_VIDEO,
886     .id             = CODEC_ID_MPEG2VIDEO,
887     .priv_data_size = sizeof(CHDContext),
888     .init           = init,
889     .close          = uninit,
890     .decode         = decode,
891     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
892     .flush          = flush,
893     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
894     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
895 };
896 #endif
897
898 #if CONFIG_MPEG4_CRYSTALHD_DECODER
899 AVCodec ff_mpeg4_crystalhd_decoder = {
900     .name           = "mpeg4_crystalhd",
901     .type           = AVMEDIA_TYPE_VIDEO,
902     .id             = CODEC_ID_MPEG4,
903     .priv_data_size = sizeof(CHDContext),
904     .init           = init,
905     .close          = uninit,
906     .decode         = decode,
907     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
908     .flush          = flush,
909     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
910     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
911 };
912 #endif
913
914 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
915 AVCodec ff_msmpeg4_crystalhd_decoder = {
916     .name           = "msmpeg4_crystalhd",
917     .type           = AVMEDIA_TYPE_VIDEO,
918     .id             = CODEC_ID_MSMPEG4V3,
919     .priv_data_size = sizeof(CHDContext),
920     .init           = init,
921     .close          = uninit,
922     .decode         = decode,
923     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
924     .flush          = flush,
925     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
926     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
927 };
928 #endif
929
930 #if CONFIG_VC1_CRYSTALHD_DECODER
931 AVCodec ff_vc1_crystalhd_decoder = {
932     .name           = "vc1_crystalhd",
933     .type           = AVMEDIA_TYPE_VIDEO,
934     .id             = CODEC_ID_VC1,
935     .priv_data_size = sizeof(CHDContext),
936     .init           = init,
937     .close          = uninit,
938     .decode         = decode,
939     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
940     .flush          = flush,
941     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
942     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
943 };
944 #endif
945
946 #if CONFIG_WMV3_CRYSTALHD_DECODER
947 AVCodec ff_wmv3_crystalhd_decoder = {
948     .name           = "wmv3_crystalhd",
949     .type           = AVMEDIA_TYPE_VIDEO,
950     .id             = CODEC_ID_WMV3,
951     .priv_data_size = sizeof(CHDContext),
952     .init           = init,
953     .close          = uninit,
954     .decode         = decode,
955     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
956     .flush          = flush,
957     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
958     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
959 };
960 #endif