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 outputting
47 * frames, the software should never fail to return a frame from a decode(),
48 * this is a hard assertion to make, because the stream may switch between
49 * differently encoded content (number of b-frames, interlacing, etc) which
50 * might require a longer pipeline than before. If that happened, you could
51 * deadlock trying to retrieve a frame that can't be decoded without feeding
52 * in additional packets.
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
81 #include <libcrystalhd/bc_dts_types.h>
82 #include <libcrystalhd/bc_dts_defs.h>
83 #include <libcrystalhd/libcrystalhd_if.h>
88 #include "libavutil/imgutils.h"
89 #include "libavutil/intreadwrite.h"
90 #include "libavutil/opt.h"
96 /** Timeout parameter passed to DtsProcOutput() in us */
97 #define OUTPUT_PROC_TIMEOUT 50
98 /** Step between fake timestamps passed to hardware in units of 100ns */
99 #define TIMESTAMP_UNIT 100000
100 /** Initial value in us of the wait in decode() */
101 #define BASE_WAIT 10000
102 /** Increment in us to adjust wait in decode() */
103 #define WAIT_UNIT 1000
106 /*****************************************************************************
107 * Module private data
108 ****************************************************************************/
114 RET_SKIP_NEXT_COPY = 2,
115 RET_COPY_NEXT_FIELD = 3,
118 typedef struct OpaqueList {
119 struct OpaqueList *next;
120 uint64_t fake_timestamp;
121 uint64_t reordered_opaque;
127 AVCodecContext *avctx;
131 uint8_t *orig_extradata;
132 uint32_t orig_extradata_size;
135 AVCodecParserContext *parser;
138 uint8_t *sps_pps_buf;
139 uint32_t sps_pps_size;
141 uint8_t output_ready;
142 uint8_t need_second_field;
143 uint8_t skip_next_output;
144 uint64_t decode_wait;
146 uint64_t last_picture;
155 static const AVOption options[] = {
156 { "crystalhd_downscale_width",
157 "Turn on downscaling to the specified width",
158 offsetof(CHDContext, sWidth),
159 AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
160 AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM, },
165 /*****************************************************************************
167 ****************************************************************************/
169 static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
172 case AV_CODEC_ID_MPEG4:
173 return BC_MSUBTYPE_DIVX;
174 case AV_CODEC_ID_MSMPEG4V3:
175 return BC_MSUBTYPE_DIVX311;
176 case AV_CODEC_ID_MPEG2VIDEO:
177 return BC_MSUBTYPE_MPEG2VIDEO;
178 case AV_CODEC_ID_VC1:
179 return BC_MSUBTYPE_VC1;
180 case AV_CODEC_ID_WMV3:
181 return BC_MSUBTYPE_WMV3;
182 case AV_CODEC_ID_H264:
183 return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
185 return BC_MSUBTYPE_INVALID;
189 static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
191 av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
192 av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
193 output->YBuffDoneSz);
194 av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
195 output->UVBuffDoneSz);
196 av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n",
197 output->PicInfo.timeStamp);
198 av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
199 output->PicInfo.picture_number);
200 av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
201 output->PicInfo.width);
202 av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
203 output->PicInfo.height);
204 av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
205 output->PicInfo.chroma_format);
206 av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
207 output->PicInfo.pulldown);
208 av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
209 output->PicInfo.flags);
210 av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
211 output->PicInfo.frame_rate);
212 av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
213 output->PicInfo.aspect_ratio);
214 av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
215 output->PicInfo.colour_primaries);
216 av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
217 output->PicInfo.picture_meta_payload);
218 av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
219 output->PicInfo.sess_num);
220 av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
221 output->PicInfo.ycom);
222 av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
223 output->PicInfo.custom_aspect_ratio_width_height);
224 av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
225 output->PicInfo.n_drop);
226 av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
227 output->PicInfo.other.h264.valid);
231 /*****************************************************************************
232 * OpaqueList functions
233 ****************************************************************************/
235 static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque,
238 OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
240 av_log(priv->avctx, AV_LOG_ERROR,
241 "Unable to allocate new node in OpaqueList.\n");
245 newNode->fake_timestamp = TIMESTAMP_UNIT;
246 priv->head = newNode;
248 newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
249 priv->tail->next = newNode;
251 priv->tail = newNode;
252 newNode->reordered_opaque = reordered_opaque;
253 newNode->pic_type = pic_type;
255 return newNode->fake_timestamp;
259 * The OpaqueList is built in decode order, while elements will be removed
260 * in presentation order. If frames are reordered, this means we must be
261 * able to remove elements that are not the first element.
263 * Returned node must be freed by caller.
265 static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
267 OpaqueList *node = priv->head;
270 av_log(priv->avctx, AV_LOG_ERROR,
271 "CrystalHD: Attempted to query non-existent timestamps.\n");
276 * The first element is special-cased because we have to manipulate
277 * the head pointer rather than the previous element in the list.
279 if (priv->head->fake_timestamp == fake_timestamp) {
280 priv->head = node->next;
282 if (!priv->head->next)
283 priv->tail = priv->head;
290 * The list is processed at arm's length so that we have the
291 * previous element available to rewrite its next pointer.
294 OpaqueList *current = node->next;
295 if (current->fake_timestamp == fake_timestamp) {
296 node->next = current->next;
301 current->next = NULL;
308 av_log(priv->avctx, AV_LOG_VERBOSE,
309 "CrystalHD: Couldn't match fake_timestamp.\n");
314 /*****************************************************************************
315 * Video decoder API function definitions
316 ****************************************************************************/
318 static void flush(AVCodecContext *avctx)
320 CHDContext *priv = avctx->priv_data;
322 avctx->has_b_frames = 0;
323 priv->last_picture = -1;
324 priv->output_ready = 0;
325 priv->need_second_field = 0;
326 priv->skip_next_output = 0;
327 priv->decode_wait = BASE_WAIT;
329 av_frame_unref (priv->pic);
331 /* Flush mode 4 flushes all software and hardware buffers. */
332 DtsFlushInput(priv->dev, 4);
336 static av_cold int uninit(AVCodecContext *avctx)
338 CHDContext *priv = avctx->priv_data;
342 DtsStopDecoder(device);
343 DtsCloseDecoder(device);
344 DtsDeviceClose(device);
347 * Restore original extradata, so that if the decoder is
348 * reinitialised, the bitstream detection and filtering
349 * will work as expected.
351 if (priv->orig_extradata) {
352 av_free(avctx->extradata);
353 avctx->extradata = priv->orig_extradata;
354 avctx->extradata_size = priv->orig_extradata_size;
355 priv->orig_extradata = NULL;
356 priv->orig_extradata_size = 0;
359 av_parser_close(priv->parser);
361 av_bsf_free(&priv->bsfc);
364 av_freep(&priv->sps_pps_buf);
366 av_frame_free (&priv->pic);
369 OpaqueList *node = priv->head;
371 OpaqueList *next = node->next;
381 static av_cold int init_bsf(AVCodecContext *avctx, const char *bsf_name)
383 CHDContext *priv = avctx->priv_data;
384 const AVBitStreamFilter *bsf;
386 void *extradata = NULL;
389 bsf = av_bsf_get_by_name(bsf_name);
391 av_log(avctx, AV_LOG_ERROR,
392 "Cannot open the %s BSF!\n", bsf_name);
393 return AVERROR_BSF_NOT_FOUND;
396 avret = av_bsf_alloc(bsf, &priv->bsfc);
401 avret = avcodec_parameters_from_context(priv->bsfc->par_in, avctx);
406 avret = av_bsf_init(priv->bsfc);
411 /* Back up the extradata so it can be restored at close time. */
412 priv->orig_extradata = avctx->extradata;
413 priv->orig_extradata_size = avctx->extradata_size;
415 size = priv->bsfc->par_out->extradata_size;
416 extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
418 av_log(avctx, AV_LOG_ERROR,
419 "Failed to allocate copy of extradata\n");
420 return AVERROR(ENOMEM);
422 memcpy(extradata, priv->bsfc->par_out->extradata, size);
424 avctx->extradata = extradata;
425 avctx->extradata_size = size;
430 static av_cold int init(AVCodecContext *avctx)
435 BC_INFO_CRYSTAL version;
436 BC_INPUT_FORMAT format = {
439 .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
440 .width = avctx->width,
441 .height = avctx->height,
444 BC_MEDIA_SUBTYPE subtype;
446 uint32_t mode = DTS_PLAYBACK_MODE |
447 DTS_LOAD_FILE_PLAY_FW |
448 DTS_SKIP_TX_CHK_CPB |
449 DTS_PLAYBACK_DROP_RPT_MODE |
450 DTS_SINGLE_THREADED_MODE |
451 DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
453 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
456 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
458 /* Initialize the library */
459 priv = avctx->priv_data;
461 priv->is_nal = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
462 priv->last_picture = -1;
463 priv->decode_wait = BASE_WAIT;
464 priv->pic = av_frame_alloc();
466 subtype = id2subtype(priv, avctx->codec->id);
468 case BC_MSUBTYPE_AVC1:
469 avret = init_bsf(avctx, "h264_mp4toannexb");
473 subtype = BC_MSUBTYPE_H264;
474 format.startCodeSz = 4;
475 format.pMetaData = avctx->extradata;
476 format.metaDataSz = avctx->extradata_size;
478 case BC_MSUBTYPE_DIVX:
479 avret = init_bsf(avctx, "mpeg4_unpack_bframes");
483 format.pMetaData = avctx->extradata;
484 format.metaDataSz = avctx->extradata_size;
486 case BC_MSUBTYPE_H264:
487 format.startCodeSz = 4;
489 case BC_MSUBTYPE_VC1:
490 case BC_MSUBTYPE_WVC1:
491 case BC_MSUBTYPE_WMV3:
492 case BC_MSUBTYPE_WMVA:
493 case BC_MSUBTYPE_MPEG2VIDEO:
494 case BC_MSUBTYPE_DIVX311:
495 format.pMetaData = avctx->extradata;
496 format.metaDataSz = avctx->extradata_size;
499 av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
500 return AVERROR(EINVAL);
502 format.mSubtype = subtype;
505 format.bEnableScaling = 1;
506 format.ScalingParams.sWidth = priv->sWidth;
509 /* Get a decoder instance */
510 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
511 // Initialize the Link and Decoder devices
512 ret = DtsDeviceOpen(&priv->dev, mode);
513 if (ret != BC_STS_SUCCESS) {
514 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
518 ret = DtsCrystalHDVersion(priv->dev, &version);
519 if (ret != BC_STS_SUCCESS) {
520 av_log(avctx, AV_LOG_VERBOSE,
521 "CrystalHD: DtsCrystalHDVersion failed\n");
524 priv->is_70012 = version.device == 0;
526 if (priv->is_70012 &&
527 (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
528 av_log(avctx, AV_LOG_VERBOSE,
529 "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
533 ret = DtsSetInputFormat(priv->dev, &format);
534 if (ret != BC_STS_SUCCESS) {
535 av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
539 ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
540 if (ret != BC_STS_SUCCESS) {
541 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
545 ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
546 if (ret != BC_STS_SUCCESS) {
547 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
550 ret = DtsStartDecoder(priv->dev);
551 if (ret != BC_STS_SUCCESS) {
552 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
555 ret = DtsStartCapture(priv->dev);
556 if (ret != BC_STS_SUCCESS) {
557 av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
561 if (avctx->codec->id == AV_CODEC_ID_H264) {
562 priv->parser = av_parser_init(avctx->codec->id);
564 av_log(avctx, AV_LOG_WARNING,
565 "Cannot open the h.264 parser! Interlaced h.264 content "
566 "will not be detected reliably.\n");
567 priv->parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
569 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
579 static inline CopyRet copy_frame(AVCodecContext *avctx,
580 BC_DTS_PROC_OUT *output,
581 void *data, int *got_frame)
584 BC_DTS_STATUS decoder_status = { 0, };
585 uint8_t trust_interlaced;
588 CHDContext *priv = avctx->priv_data;
589 int64_t pkt_pts = AV_NOPTS_VALUE;
590 uint8_t pic_type = 0;
592 uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
593 VDEC_FLAG_BOTTOMFIELD;
594 uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
596 int width = output->PicInfo.width;
597 int height = output->PicInfo.height;
599 uint8_t *src = output->Ybuff;
604 if (output->PicInfo.timeStamp != 0) {
605 OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
607 pkt_pts = node->reordered_opaque;
608 pic_type = node->pic_type;
612 * We will encounter a situation where a timestamp cannot be
613 * popped if a second field is being returned. In this case,
614 * each field has the same timestamp and the first one will
615 * cause it to be popped. To keep subsequent calculations
616 * simple, pic_type should be set a FIELD value - doesn't
617 * matter which, but I chose BOTTOM.
619 pic_type = PICT_BOTTOM_FIELD;
621 av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
622 output->PicInfo.timeStamp);
623 av_log(avctx, AV_LOG_VERBOSE, "output picture type %d\n",
627 ret = DtsGetDriverStatus(priv->dev, &decoder_status);
628 if (ret != BC_STS_SUCCESS) {
629 av_log(avctx, AV_LOG_ERROR,
630 "CrystalHD: GetDriverStatus failed: %u\n", ret);
635 * For most content, we can trust the interlaced flag returned
636 * by the hardware, but sometimes we can't. These are the
637 * conditions under which we can trust the flag:
639 * 1) It's not h.264 content
640 * 2) The UNKNOWN_SRC flag is not set
641 * 3) We know we're expecting a second field
642 * 4) The hardware reports this picture and the next picture
643 * have the same picture number.
645 * Note that there can still be interlaced content that will
646 * fail this check, if the hardware hasn't decoded the next
647 * picture or if there is a corruption in the stream. (In either
648 * case a 0 will be returned for the next picture number)
650 trust_interlaced = avctx->codec->id != AV_CODEC_ID_H264 ||
651 !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
652 priv->need_second_field ||
653 (decoder_status.picNumFlags & ~0x40000000) ==
654 output->PicInfo.picture_number;
657 * If we got a false negative for trust_interlaced on the first field,
658 * we will realise our mistake here when we see that the picture number is that
659 * of the previous picture. We cannot recover the frame and should discard the
660 * second field to keep the correct number of output frames.
662 if (output->PicInfo.picture_number == priv->last_picture && !priv->need_second_field) {
663 av_log(avctx, AV_LOG_WARNING,
664 "Incorrectly guessed progressive frame. Discarding second field\n");
665 /* Returning without providing a picture. */
669 interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
672 if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
673 av_log(avctx, AV_LOG_VERBOSE,
674 "Next picture number unknown. Assuming progressive frame.\n");
677 av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d | trust_interlaced %d\n",
678 interlaced, trust_interlaced);
680 if (priv->pic->data[0] && !priv->need_second_field)
681 av_frame_unref(priv->pic);
683 priv->need_second_field = interlaced && !priv->need_second_field;
685 if (!priv->pic->data[0]) {
686 if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
690 bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
691 if (priv->is_70012) {
696 else if (width <= 1280)
699 sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
704 dStride = priv->pic->linesize[0];
705 dst = priv->pic->data[0];
707 av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
715 av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
718 av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
722 for (sY = 0; sY < height; dY++, sY++) {
723 memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
727 av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
730 priv->pic->interlaced_frame = interlaced;
732 priv->pic->top_field_first = !bottom_first;
734 if (pkt_pts != AV_NOPTS_VALUE) {
735 priv->pic->pts = pkt_pts;
737 FF_DISABLE_DEPRECATION_WARNINGS
738 priv->pic->pkt_pts = pkt_pts;
739 FF_ENABLE_DEPRECATION_WARNINGS
742 av_frame_set_pkt_pos(priv->pic, -1);
743 av_frame_set_pkt_duration(priv->pic, 0);
744 av_frame_set_pkt_size(priv->pic, -1);
746 if (!priv->need_second_field) {
748 if ((ret = av_frame_ref(data, priv->pic)) < 0) {
754 * Two types of PAFF content have been observed. One form causes the
755 * hardware to return a field pair and the other individual fields,
756 * even though the input is always individual fields. We must skip
757 * copying on the next decode() call to maintain pipeline length in
760 if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
761 (pic_type == PICT_TOP_FIELD || pic_type == PICT_BOTTOM_FIELD)) {
762 av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
763 return RET_SKIP_NEXT_COPY;
767 * The logic here is purely based on empirical testing with samples.
768 * If we need a second field, it could come from a second input packet,
769 * or it could come from the same field-pair input packet at the current
770 * field. In the first case, we should return and wait for the next time
771 * round to get the second field, while in the second case, we should
772 * ask the decoder for it immediately.
774 * Testing has shown that we are dealing with the fieldpair -> two fields
775 * case if the VDEC_FLAG_UNKNOWN_SRC is not set or if the input picture
776 * type was PICT_FRAME (in this second case, the flag might still be set)
778 return priv->need_second_field &&
779 (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
780 pic_type == PICT_FRAME) ?
781 RET_COPY_NEXT_FIELD : RET_OK;
785 static inline CopyRet receive_frame(AVCodecContext *avctx,
786 void *data, int *got_frame)
789 BC_DTS_PROC_OUT output = {
790 .PicInfo.width = avctx->width,
791 .PicInfo.height = avctx->height,
793 CHDContext *priv = avctx->priv_data;
794 HANDLE dev = priv->dev;
798 // Request decoded data from the driver
799 ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
800 if (ret == BC_STS_FMT_CHANGE) {
801 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
802 avctx->width = output.PicInfo.width;
803 avctx->height = output.PicInfo.height;
804 switch ( output.PicInfo.aspect_ratio ) {
805 case vdecAspectRatioSquare:
806 avctx->sample_aspect_ratio = (AVRational) { 1, 1};
808 case vdecAspectRatio12_11:
809 avctx->sample_aspect_ratio = (AVRational) { 12, 11};
811 case vdecAspectRatio10_11:
812 avctx->sample_aspect_ratio = (AVRational) { 10, 11};
814 case vdecAspectRatio16_11:
815 avctx->sample_aspect_ratio = (AVRational) { 16, 11};
817 case vdecAspectRatio40_33:
818 avctx->sample_aspect_ratio = (AVRational) { 40, 33};
820 case vdecAspectRatio24_11:
821 avctx->sample_aspect_ratio = (AVRational) { 24, 11};
823 case vdecAspectRatio20_11:
824 avctx->sample_aspect_ratio = (AVRational) { 20, 11};
826 case vdecAspectRatio32_11:
827 avctx->sample_aspect_ratio = (AVRational) { 32, 11};
829 case vdecAspectRatio80_33:
830 avctx->sample_aspect_ratio = (AVRational) { 80, 33};
832 case vdecAspectRatio18_11:
833 avctx->sample_aspect_ratio = (AVRational) { 18, 11};
835 case vdecAspectRatio15_11:
836 avctx->sample_aspect_ratio = (AVRational) { 15, 11};
838 case vdecAspectRatio64_33:
839 avctx->sample_aspect_ratio = (AVRational) { 64, 33};
841 case vdecAspectRatio160_99:
842 avctx->sample_aspect_ratio = (AVRational) {160, 99};
844 case vdecAspectRatio4_3:
845 avctx->sample_aspect_ratio = (AVRational) { 4, 3};
847 case vdecAspectRatio16_9:
848 avctx->sample_aspect_ratio = (AVRational) { 16, 9};
850 case vdecAspectRatio221_1:
851 avctx->sample_aspect_ratio = (AVRational) {221, 1};
854 return RET_COPY_AGAIN;
855 } else if (ret == BC_STS_SUCCESS) {
857 if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
858 if (priv->last_picture == -1) {
860 * Init to one less, so that the incrementing code doesn't
861 * need to be special-cased.
863 priv->last_picture = output.PicInfo.picture_number - 1;
866 print_frame_info(priv, &output);
868 if (priv->last_picture + 1 < output.PicInfo.picture_number) {
869 av_log(avctx, AV_LOG_WARNING,
870 "CrystalHD: Picture Number discontinuity\n");
872 * Have we lost frames? If so, we need to shrink the
873 * pipeline length appropriately.
875 * XXX: I have no idea what the semantics of this situation
876 * are so I don't even know if we've lost frames or which
879 * In any case, only warn the first time.
881 priv->last_picture = output.PicInfo.picture_number - 1;
884 copy_ret = copy_frame(avctx, &output, data, got_frame);
885 if (*got_frame > 0) {
886 avctx->has_b_frames--;
887 priv->last_picture++;
888 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
889 avctx->has_b_frames);
893 * An invalid frame has been consumed.
895 av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
897 avctx->has_b_frames--;
900 DtsReleaseOutputBuffs(dev, NULL, FALSE);
903 } else if (ret == BC_STS_BUSY) {
904 return RET_COPY_AGAIN;
906 av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
912 static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
915 BC_DTS_STATUS decoder_status = { 0, };
917 CHDContext *priv = avctx->priv_data;
918 HANDLE dev = priv->dev;
919 uint8_t *in_data = avpkt->data;
920 int len = avpkt->size;
922 uint8_t pic_type = 0;
924 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
927 int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
931 AVPacket filter_packet = { 0 };
932 AVPacket filtered_packet = { 0 };
934 ret = av_packet_ref(&filter_packet, avpkt);
936 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
937 "failed to ref input packet\n");
941 ret = av_bsf_send_packet(priv->bsfc, &filter_packet);
943 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
944 "failed to send input packet\n");
948 ret = av_bsf_receive_packet(priv->bsfc, &filtered_packet);
950 av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
951 "failed to receive output packet\n");
955 in_data = filtered_packet.data;
956 len = filtered_packet.size;
958 av_packet_unref(&filter_packet);
970 H264Context *h = priv->parser->priv_data;
972 index = av_parser_parse2(priv->parser, avctx, &pout, &psize,
973 in_data, len, avpkt->pts,
976 av_log(avctx, AV_LOG_WARNING,
977 "CrystalHD: Failed to parse h.264 packet to "
978 "detect interlacing.\n");
979 } else if (index != len) {
980 av_log(avctx, AV_LOG_WARNING,
981 "CrystalHD: Failed to parse h.264 packet "
982 "completely. Interlaced frames may be "
983 "incorrectly detected.\n");
985 av_log(avctx, AV_LOG_VERBOSE,
986 "CrystalHD: parser picture type %d\n",
987 h->picture_structure);
988 pic_type = h->picture_structure;
991 av_log(avctx, AV_LOG_WARNING,
992 "CrystalHD: mp4toannexb filter failed to filter "
993 "packet. Interlaced frames may be incorrectly "
998 if (len < tx_free - 1024) {
1000 * Despite being notionally opaque, either libcrystalhd or
1001 * the hardware itself will mangle pts values that are too
1002 * small or too large. The docs claim it should be in units
1003 * of 100ns. Given that we're nominally dealing with a black
1004 * box on both sides, any transform we do has no guarantee of
1005 * avoiding mangling so we need to build a mapping to values
1006 * we know will not be mangled.
1008 int64_t safe_pts = avpkt->pts == AV_NOPTS_VALUE ? 0 : avpkt->pts;
1009 uint64_t pts = opaque_list_push(priv, safe_pts, pic_type);
1014 return AVERROR(ENOMEM);
1016 av_log(priv->avctx, AV_LOG_VERBOSE,
1017 "input \"pts\": %"PRIu64"\n", pts);
1018 ret = DtsProcInput(dev, in_data, len, pts, 0);
1022 if (ret == BC_STS_BUSY) {
1023 av_log(avctx, AV_LOG_WARNING,
1024 "CrystalHD: ProcInput returned busy\n");
1026 return AVERROR(EBUSY);
1027 } else if (ret != BC_STS_SUCCESS) {
1028 av_log(avctx, AV_LOG_ERROR,
1029 "CrystalHD: ProcInput failed: %u\n", ret);
1032 avctx->has_b_frames++;
1034 av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
1035 len = 0; // We didn't consume any bytes.
1038 av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
1041 if (priv->skip_next_output) {
1042 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
1043 priv->skip_next_output = 0;
1044 avctx->has_b_frames--;
1048 ret = DtsGetDriverStatus(dev, &decoder_status);
1049 if (ret != BC_STS_SUCCESS) {
1050 av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
1055 * No frames ready. Don't try to extract.
1057 * Empirical testing shows that ReadyListCount can be a damn lie,
1058 * and ProcOut still fails when count > 0. The same testing showed
1059 * that two more iterations were needed before ProcOutput would
1062 if (priv->output_ready < 2) {
1063 if (decoder_status.ReadyListCount != 0)
1064 priv->output_ready++;
1066 av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
1068 } else if (decoder_status.ReadyListCount == 0) {
1070 * After the pipeline is established, if we encounter a lack of frames
1071 * that probably means we're not giving the hardware enough time to
1072 * decode them, so start increasing the wait time at the end of a
1076 priv->decode_wait += WAIT_UNIT;
1077 av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
1082 rec_ret = receive_frame(avctx, data, got_frame);
1083 if (rec_ret == RET_OK && *got_frame == 0) {
1085 * This case is for when the encoded fields are stored
1086 * separately and we get a separate avpkt for each one. To keep
1087 * the pipeline stable, we should return nothing and wait for
1088 * the next time round to grab the second field.
1089 * H.264 PAFF is an example of this.
1091 av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
1092 avctx->has_b_frames--;
1093 } else if (rec_ret == RET_COPY_NEXT_FIELD) {
1095 * This case is for when the encoded fields are stored in a
1096 * single avpkt but the hardware returns then separately. Unless
1097 * we grab the second field before returning, we'll slip another
1098 * frame in the pipeline and if that happens a lot, we're sunk.
1099 * So we have to get that second field now.
1100 * Interlaced mpeg2 and vc1 are examples of this.
1102 av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
1104 usleep(priv->decode_wait);
1105 ret = DtsGetDriverStatus(dev, &decoder_status);
1106 if (ret == BC_STS_SUCCESS &&
1107 decoder_status.ReadyListCount > 0) {
1108 rec_ret = receive_frame(avctx, data, got_frame);
1109 if ((rec_ret == RET_OK && *got_frame > 0) ||
1110 rec_ret == RET_ERROR)
1114 av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
1115 } else if (rec_ret == RET_SKIP_NEXT_COPY) {
1117 * Two input packets got turned into a field pair. Gawd.
1119 av_log(avctx, AV_LOG_VERBOSE,
1120 "Don't output on next decode call.\n");
1121 priv->skip_next_output = 1;
1124 * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
1125 * a FMT_CHANGE event and need to go around again for the actual frame,
1126 * we got a busy status and need to try again, or we're dealing with
1127 * packed b-frames, where the hardware strangely returns the packed
1128 * p-frame twice. We choose to keep the second copy as it carries the
1131 } while (rec_ret == RET_COPY_AGAIN);
1132 usleep(priv->decode_wait);
1137 #if CONFIG_H264_CRYSTALHD_DECODER
1138 static AVClass h264_class = {
1140 av_default_item_name,
1142 LIBAVUTIL_VERSION_INT,
1145 AVCodec ff_h264_crystalhd_decoder = {
1146 .name = "h264_crystalhd",
1147 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1148 .type = AVMEDIA_TYPE_VIDEO,
1149 .id = AV_CODEC_ID_H264,
1150 .priv_data_size = sizeof(CHDContext),
1154 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1156 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1157 .priv_class = &h264_class,
1161 #if CONFIG_MPEG2_CRYSTALHD_DECODER
1162 static AVClass mpeg2_class = {
1164 av_default_item_name,
1166 LIBAVUTIL_VERSION_INT,
1169 AVCodec ff_mpeg2_crystalhd_decoder = {
1170 .name = "mpeg2_crystalhd",
1171 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
1172 .type = AVMEDIA_TYPE_VIDEO,
1173 .id = AV_CODEC_ID_MPEG2VIDEO,
1174 .priv_data_size = sizeof(CHDContext),
1178 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1180 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1181 .priv_class = &mpeg2_class,
1185 #if CONFIG_MPEG4_CRYSTALHD_DECODER
1186 static AVClass mpeg4_class = {
1188 av_default_item_name,
1190 LIBAVUTIL_VERSION_INT,
1193 AVCodec ff_mpeg4_crystalhd_decoder = {
1194 .name = "mpeg4_crystalhd",
1195 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
1196 .type = AVMEDIA_TYPE_VIDEO,
1197 .id = AV_CODEC_ID_MPEG4,
1198 .priv_data_size = sizeof(CHDContext),
1202 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1204 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1205 .priv_class = &mpeg4_class,
1209 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1210 static AVClass msmpeg4_class = {
1211 "msmpeg4_crystalhd",
1212 av_default_item_name,
1214 LIBAVUTIL_VERSION_INT,
1217 AVCodec ff_msmpeg4_crystalhd_decoder = {
1218 .name = "msmpeg4_crystalhd",
1219 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1220 .type = AVMEDIA_TYPE_VIDEO,
1221 .id = AV_CODEC_ID_MSMPEG4V3,
1222 .priv_data_size = sizeof(CHDContext),
1226 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1228 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1229 .priv_class = &msmpeg4_class,
1233 #if CONFIG_VC1_CRYSTALHD_DECODER
1234 static AVClass vc1_class = {
1236 av_default_item_name,
1238 LIBAVUTIL_VERSION_INT,
1241 AVCodec ff_vc1_crystalhd_decoder = {
1242 .name = "vc1_crystalhd",
1243 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
1244 .type = AVMEDIA_TYPE_VIDEO,
1245 .id = AV_CODEC_ID_VC1,
1246 .priv_data_size = sizeof(CHDContext),
1250 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1252 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1253 .priv_class = &vc1_class,
1257 #if CONFIG_WMV3_CRYSTALHD_DECODER
1258 static AVClass wmv3_class = {
1260 av_default_item_name,
1262 LIBAVUTIL_VERSION_INT,
1265 AVCodec ff_wmv3_crystalhd_decoder = {
1266 .name = "wmv3_crystalhd",
1267 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
1268 .type = AVMEDIA_TYPE_VIDEO,
1269 .id = AV_CODEC_ID_WMV3,
1270 .priv_data_size = sizeof(CHDContext),
1274 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1276 .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1277 .priv_class = &wmv3_class,