2 * - CrystalHD decoder module -
4 * Copyright(C) 2010,2011 Philip Langdale <ffmpeg.philipl@overt.org>
6 * This file is part of FFmpeg.
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.
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.
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
24 * - Principles of Operation -
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.
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.
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.
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.
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.
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.
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).
72 /*****************************************************************************
74 ****************************************************************************/
76 #define _XOPEN_SOURCE 600
82 #include <libcrystalhd/bc_dts_types.h>
83 #include <libcrystalhd/bc_dts_defs.h>
84 #include <libcrystalhd/libcrystalhd_if.h>
87 #include "libavutil/imgutils.h"
88 #include "libavutil/intreadwrite.h"
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
100 /*****************************************************************************
101 * Module private data
102 ****************************************************************************/
108 RET_SKIP_NEXT_COPY = 2,
111 typedef struct OpaqueList {
112 struct OpaqueList *next;
113 uint64_t fake_timestamp;
114 uint64_t reordered_opaque;
118 AVCodecContext *avctx;
123 uint8_t *sps_pps_buf;
124 uint32_t sps_pps_size;
126 uint8_t output_ready;
127 uint8_t need_second_field;
128 uint8_t skip_next_output;
129 uint64_t decode_wait;
131 uint64_t last_picture;
138 /*****************************************************************************
140 ****************************************************************************/
142 static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum CodecID id)
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;
152 return BC_MSUBTYPE_VC1;
154 return BC_MSUBTYPE_WMV3;
156 return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
158 return BC_MSUBTYPE_INVALID;
162 static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
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);
204 /*****************************************************************************
205 * OpaqueList functions
206 ****************************************************************************/
208 static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque)
210 OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
212 av_log(priv->avctx, AV_LOG_ERROR,
213 "Unable to allocate new node in OpaqueList.\n");
217 newNode->fake_timestamp = TIMESTAMP_UNIT;
218 priv->head = newNode;
220 newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
221 priv->tail->next = newNode;
223 priv->tail = newNode;
224 newNode->reordered_opaque = reordered_opaque;
226 return newNode->fake_timestamp;
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.
234 static uint64_t opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
236 OpaqueList *node = priv->head;
239 av_log(priv->avctx, AV_LOG_ERROR,
240 "CrystalHD: Attempted to query non-existent timestamps.\n");
241 return AV_NOPTS_VALUE;
245 * The first element is special-cased because we have to manipulate
246 * the head pointer rather than the previous element in the list.
248 if (priv->head->fake_timestamp == fake_timestamp) {
249 uint64_t reordered_opaque = node->reordered_opaque;
250 priv->head = node->next;
253 if (!priv->head->next)
254 priv->tail = priv->head;
256 return reordered_opaque;
260 * The list is processed at arm's length so that we have the
261 * previous element available to rewrite its next pointer.
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;
273 return reordered_opaque;
279 av_log(priv->avctx, AV_LOG_VERBOSE,
280 "CrystalHD: Couldn't match fake_timestamp.\n");
281 return AV_NOPTS_VALUE;
285 /*****************************************************************************
286 * Video decoder API function definitions
287 ****************************************************************************/
289 static void flush(AVCodecContext *avctx)
291 CHDContext *priv = avctx->priv_data;
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;
300 if (priv->pic.data[0])
301 avctx->release_buffer(avctx, &priv->pic);
303 /* Flush mode 4 flushes all software and hardware buffers. */
304 DtsFlushInput(priv->dev, 4);
308 static av_cold int uninit(AVCodecContext *avctx)
310 CHDContext *priv = avctx->priv_data;
314 DtsStopDecoder(device);
315 DtsCloseDecoder(device);
316 DtsDeviceClose(device);
318 av_free(priv->sps_pps_buf);
320 if (priv->pic.data[0])
321 avctx->release_buffer(avctx, &priv->pic);
324 OpaqueList *node = priv->head;
326 OpaqueList *next = node->next;
336 static av_cold int init(AVCodecContext *avctx)
340 BC_INFO_CRYSTAL version;
341 BC_INPUT_FORMAT format = {
344 .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
345 .width = avctx->width,
346 .height = avctx->height,
349 BC_MEDIA_SUBTYPE subtype;
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);
358 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
361 avctx->pix_fmt = PIX_FMT_YUYV422;
363 /* Initialize the library */
364 priv = avctx->priv_data;
366 priv->is_nal = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
367 priv->last_picture = -1;
368 priv->decode_wait = BASE_WAIT;
370 subtype = id2subtype(priv, avctx->codec->id);
372 case BC_MSUBTYPE_AVC1:
376 AVBitStreamFilterContext *bsfc;
378 uint32_t orig_data_size = avctx->extradata_size;
379 uint8_t *orig_data = av_malloc(orig_data_size);
381 av_log(avctx, AV_LOG_ERROR,
382 "Failed to allocate copy of extradata\n");
383 return AVERROR(ENOMEM);
385 memcpy(orig_data, avctx->extradata, orig_data_size);
388 bsfc = av_bitstream_filter_init("h264_mp4toannexb");
390 av_log(avctx, AV_LOG_ERROR,
391 "Cannot open the h264_mp4toannexb BSF!\n");
393 return AVERROR_BSF_NOT_FOUND;
395 av_bitstream_filter_filter(bsfc, avctx, NULL, &dummy_p,
396 &dummy_int, NULL, 0, 0);
397 av_bitstream_filter_close(bsfc);
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;
404 format.pMetaData = priv->sps_pps_buf;
405 format.metaDataSz = priv->sps_pps_size;
406 format.startCodeSz = (avctx->extradata[4] & 0x03) + 1;
409 case BC_MSUBTYPE_H264:
410 format.startCodeSz = 4;
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;
423 av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
424 return AVERROR(EINVAL);
426 format.mSubtype = subtype;
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");
437 ret = DtsCrystalHDVersion(priv->dev, &version);
438 if (ret != BC_STS_SUCCESS) {
439 av_log(avctx, AV_LOG_VERBOSE,
440 "CrystalHD: DtsCrystalHDVersion failed\n");
443 priv->is_70012 = version.device == 0;
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");
452 ret = DtsSetInputFormat(priv->dev, &format);
453 if (ret != BC_STS_SUCCESS) {
454 av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
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");
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");
469 ret = DtsStartDecoder(priv->dev);
470 if (ret != BC_STS_SUCCESS) {
471 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
474 ret = DtsStartCapture(priv->dev);
475 if (ret != BC_STS_SUCCESS) {
476 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
480 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
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.
495 * The default behaviour is to assume MBAFF with input and output fieldpairs.
497 * Define ASSUME_PAFF_OVER_MBAFF to treat input as PAFF with separate input
500 * Define ASSUME_TWO_INPUTS_ONE_OUTPUT to treat input as separate fields but
501 * output as a single fieldpair.
503 * Define both to mess up your playback.
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)
513 BC_DTS_STATUS decoder_status;
515 uint8_t next_frame_same;
518 CHDContext *priv = avctx->priv_data;
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);
524 int width = output->PicInfo.width;
525 int height = output->PicInfo.height;
527 uint8_t *src = output->Ybuff;
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);
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;
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);
551 if (priv->pic.data[0] && !priv->need_second_field)
552 avctx->release_buffer(avctx, &priv->pic);
554 priv->need_second_field = interlaced && !priv->need_second_field;
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");
565 bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
566 if (priv->is_70012) {
571 else if (width <= 1280)
573 else if (width <= 1080)
575 sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
580 dStride = priv->pic.linesize[0];
581 dst = priv->pic.data[0];
583 av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
591 av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
594 av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
598 for (sY = 0; sY < height; dY++, sY++) {
599 memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
603 av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
606 priv->pic.interlaced_frame = interlaced;
608 priv->pic.top_field_first = !bottom_first;
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",
616 if (!priv->need_second_field) {
617 *data_size = sizeof(AVFrame);
618 *(AVFrame *)data = priv->pic;
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;
631 static inline CopyRet receive_frame(AVCodecContext *avctx,
632 void *data, int *data_size,
633 uint8_t second_field)
636 BC_DTS_PROC_OUT output = {
637 .PicInfo.width = avctx->width,
638 .PicInfo.height = avctx->height,
640 CHDContext *priv = avctx->priv_data;
641 HANDLE dev = priv->dev;
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) {
654 if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
655 if (priv->last_picture == -1) {
657 * Init to one less, so that the incrementing code doesn't
658 * need to be special-cased.
660 priv->last_picture = output.PicInfo.picture_number - 1;
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;
672 print_frame_info(priv, &output);
674 if (priv->last_picture + 1 < output.PicInfo.picture_number) {
675 av_log(avctx, AV_LOG_WARNING,
676 "CrystalHD: Picture Number discontinuity\n");
678 * Have we lost frames? If so, we need to shrink the
679 * pipeline length appropriately.
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
685 * In any case, only warn the first time.
687 priv->last_picture = output.PicInfo.picture_number - 1;
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);
699 * An invalid frame has been consumed.
701 av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
703 avctx->has_b_frames--;
706 DtsReleaseOutputBuffs(dev, NULL, FALSE);
709 } else if (ret == BC_STS_BUSY) {
710 return RET_COPY_AGAIN;
712 av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
718 static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
721 BC_DTS_STATUS decoder_status;
723 CHDContext *priv = avctx->priv_data;
724 HANDLE dev = priv->dev;
725 int len = avpkt->size;
727 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
730 int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
731 if (len < tx_free - 1024) {
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.
741 uint64_t pts = opaque_list_push(priv, avctx->pkt->pts);
743 return AVERROR(ENOMEM);
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");
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);
758 avctx->has_b_frames++;
760 av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
761 len = 0; // We didn't consume any bytes.
764 av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
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--;
774 ret = DtsGetDriverStatus(dev, &decoder_status);
775 if (ret != BC_STS_SUCCESS) {
776 av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
781 * No frames ready. Don't try to extract.
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
788 if (priv->output_ready < 2) {
789 if (decoder_status.ReadyListCount != 0)
790 priv->output_ready++;
792 av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
794 } else if (decoder_status.ReadyListCount == 0) {
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
802 priv->decode_wait += WAIT_UNIT;
803 av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
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) {
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.
818 av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
819 avctx->has_b_frames--;
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.
829 av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
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)
841 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
843 } else if (rec_ret == RET_SKIP_NEXT_COPY) {
845 * Two input packets got turned into a field pair. Gawd.
847 av_log(avctx, AV_LOG_VERBOSE,
848 "Don't output on next decode call.\n");
849 priv->skip_next_output = 1;
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
859 } while (rec_ret == RET_COPY_AGAIN);
860 usleep(priv->decode_wait);
865 #if CONFIG_H264_CRYSTALHD_DECODER
866 AVCodec ff_h264_crystalhd_decoder = {
867 .name = "h264_crystalhd",
868 .type = AVMEDIA_TYPE_VIDEO,
870 .priv_data_size = sizeof(CHDContext),
874 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
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},
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),
890 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
892 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
893 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
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),
906 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
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},
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),
922 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
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},
929 #if CONFIG_VC1_CRYSTALHD_DECODER
930 AVCodec ff_vc1_crystalhd_decoder = {
931 .name = "vc1_crystalhd",
932 .type = AVMEDIA_TYPE_VIDEO,
934 .priv_data_size = sizeof(CHDContext),
938 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
940 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
941 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE},
945 #if CONFIG_WMV3_CRYSTALHD_DECODER
946 AVCodec ff_wmv3_crystalhd_decoder = {
947 .name = "wmv3_crystalhd",
948 .type = AVMEDIA_TYPE_VIDEO,
950 .priv_data_size = sizeof(CHDContext),
954 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
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},