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>
94 #define OUTPUT_PROC_TIMEOUT 50
96 #define TIMESTAMP_UNIT 100000
98 #define BASE_WAIT 10000
100 #define WAIT_UNIT 1000
154 {
"crystalhd_downscale_width",
155 "Turn on downscaling to the specified width",
171 return BC_MSUBTYPE_DIVX;
173 return BC_MSUBTYPE_DIVX311;
175 return BC_MSUBTYPE_MPEG2VIDEO;
177 return BC_MSUBTYPE_VC1;
179 return BC_MSUBTYPE_WMV3;
181 return priv->
is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
183 return BC_MSUBTYPE_INVALID;
191 output->YBuffDoneSz);
193 output->UVBuffDoneSz);
195 output->PicInfo.timeStamp);
197 output->PicInfo.picture_number);
199 output->PicInfo.width);
201 output->PicInfo.height);
203 output->PicInfo.chroma_format);
205 output->PicInfo.pulldown);
207 output->PicInfo.flags);
209 output->PicInfo.frame_rate);
211 output->PicInfo.aspect_ratio);
213 output->PicInfo.colour_primaries);
215 output->PicInfo.picture_meta_payload);
217 output->PicInfo.sess_num);
219 output->PicInfo.ycom);
221 output->PicInfo.custom_aspect_ratio_width_height);
223 output->PicInfo.n_drop);
225 output->PicInfo.other.h264.valid);
239 "Unable to allocate new node in OpaqueList.\n");
244 priv->
head = newNode;
249 priv->
tail = newNode;
269 "CrystalHD: Attempted to query non-existent timestamps.\n");
299 current->
next = NULL;
307 "CrystalHD: Couldn't match fake_timestamp.\n");
330 DtsFlushInput(priv->
dev, 4);
340 DtsStopDecoder(device);
341 DtsCloseDecoder(device);
342 DtsDeviceClose(device);
384 BC_INPUT_FORMAT format = {
387 .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
388 .width = avctx->
width,
392 BC_MEDIA_SUBTYPE subtype;
394 uint32_t
mode = DTS_PLAYBACK_MODE |
395 DTS_LOAD_FILE_PLAY_FW |
396 DTS_SKIP_TX_CHK_CPB |
397 DTS_PLAYBACK_DROP_RPT_MODE |
398 DTS_SINGLE_THREADED_MODE |
399 DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
416 case BC_MSUBTYPE_AVC1:
425 "Failed to allocate copy of extradata\n");
434 "Cannot open the h264_mp4toannexb BSF!\n");
438 &dummy_int, NULL, 0, 0);
440 subtype = BC_MSUBTYPE_H264;
442 case BC_MSUBTYPE_H264:
443 format.startCodeSz = 4;
445 case BC_MSUBTYPE_VC1:
446 case BC_MSUBTYPE_WVC1:
447 case BC_MSUBTYPE_WMV3:
448 case BC_MSUBTYPE_WMVA:
449 case BC_MSUBTYPE_MPEG2VIDEO:
450 case BC_MSUBTYPE_DIVX:
451 case BC_MSUBTYPE_DIVX311:
459 format.mSubtype = subtype;
462 format.bEnableScaling = 1;
463 format.ScalingParams.sWidth = priv->
sWidth;
469 ret = DtsDeviceOpen(&priv->
dev, mode);
470 if (ret != BC_STS_SUCCESS) {
475 ret = DtsCrystalHDVersion(priv->
dev, &version);
476 if (ret != BC_STS_SUCCESS) {
478 "CrystalHD: DtsCrystalHDVersion failed\n");
481 priv->
is_70012 = version.device == 0;
484 (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
486 "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
490 ret = DtsSetInputFormat(priv->
dev, &format);
491 if (ret != BC_STS_SUCCESS) {
496 ret = DtsOpenDecoder(priv->
dev, BC_STREAM_TYPE_ES);
497 if (ret != BC_STS_SUCCESS) {
502 ret = DtsSetColorSpace(priv->
dev, OUTPUT_MODE422_YUY2);
503 if (ret != BC_STS_SUCCESS) {
507 ret = DtsStartDecoder(priv->
dev);
508 if (ret != BC_STS_SUCCESS) {
512 ret = DtsStartCapture(priv->
dev);
513 if (ret != BC_STS_SUCCESS) {
522 "Cannot open the h.264 parser! Interlaced h.264 content "
523 "will not be detected reliably.\n");
537 BC_DTS_PROC_OUT *output,
538 void *
data,
int *got_frame)
541 BC_DTS_STATUS decoder_status = { 0, };
549 uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
550 VDEC_FLAG_BOTTOMFIELD;
551 uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
553 int width = output->PicInfo.width;
554 int height = output->PicInfo.height;
561 if (output->PicInfo.timeStamp != 0) {
579 output->PicInfo.timeStamp);
584 ret = DtsGetDriverStatus(priv->
dev, &decoder_status);
585 if (ret != BC_STS_SUCCESS) {
587 "CrystalHD: GetDriverStatus failed: %u\n", ret);
608 !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
610 (decoder_status.picNumFlags & ~0x40000000) ==
611 output->PicInfo.picture_number;
621 "Incorrectly guessed progressive frame. Discarding second field\n");
626 interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
629 if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
631 "Next picture number unknown. Assuming progressive frame.\n");
635 interlaced, trust_interlaced);
653 else if (width <= 1280)
679 for (sY = 0; sY <
height; dY++, sY++) {
680 memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
707 if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
726 (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
733 void *
data,
int *got_frame)
736 BC_DTS_PROC_OUT output = {
737 .PicInfo.width = avctx->
width,
738 .PicInfo.height = avctx->
height,
747 if (ret == BC_STS_FMT_CHANGE) {
749 avctx->
width = output.PicInfo.width;
750 avctx->
height = output.PicInfo.height;
751 switch ( output.PicInfo.aspect_ratio ) {
752 case vdecAspectRatioSquare:
755 case vdecAspectRatio12_11:
758 case vdecAspectRatio10_11:
761 case vdecAspectRatio16_11:
764 case vdecAspectRatio40_33:
767 case vdecAspectRatio24_11:
770 case vdecAspectRatio20_11:
773 case vdecAspectRatio32_11:
776 case vdecAspectRatio80_33:
779 case vdecAspectRatio18_11:
782 case vdecAspectRatio15_11:
785 case vdecAspectRatio64_33:
788 case vdecAspectRatio160_99:
791 case vdecAspectRatio4_3:
794 case vdecAspectRatio16_9:
797 case vdecAspectRatio221_1:
802 }
else if (ret == BC_STS_SUCCESS) {
804 if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
814 output.PicInfo.timeStamp == 0 && priv->
bframe_bug) {
816 "CrystalHD: Not returning packed frame twice.\n");
818 DtsReleaseOutputBuffs(dev, NULL,
FALSE);
824 if (priv->
last_picture + 1 < output.PicInfo.picture_number) {
826 "CrystalHD: Picture Number discontinuity\n");
840 copy_ret =
copy_frame(avctx, &output, data, got_frame);
841 if (*got_frame > 0) {
856 DtsReleaseOutputBuffs(dev, NULL,
FALSE);
859 }
else if (ret == BC_STS_BUSY) {
871 BC_DTS_STATUS decoder_status = { 0, };
887 "CrystalHD: Enabling work-around for packed b-frame bug\n");
894 "CrystalHD: Disabling work-around for packed b-frame bug\n");
907 avpkt->
data, len, 0);
918 in_data, len, avctx->
pkt->
pts,
922 "CrystalHD: Failed to parse h.264 packet to "
923 "detect interlacing.\n");
924 }
else if (index != len) {
926 "CrystalHD: Failed to parse h.264 packet "
927 "completely. Interlaced frames may be "
928 "incorrectly detected.\n");
931 "CrystalHD: parser picture type %d\n",
937 "CrystalHD: mp4toannexb filter failed to filter "
938 "packet. Interlaced frames may be incorrectly "
943 if (len < tx_free - 1024) {
961 "input \"pts\": %"PRIu64
"\n", pts);
962 ret = DtsProcInput(dev, in_data, len, pts, 0);
966 if (ret == BC_STS_BUSY) {
968 "CrystalHD: ProcInput returned busy\n");
971 }
else if (ret != BC_STS_SUCCESS) {
973 "CrystalHD: ProcInput failed: %u\n", ret);
992 ret = DtsGetDriverStatus(dev, &decoder_status);
993 if (ret != BC_STS_SUCCESS) {
1007 if (decoder_status.ReadyListCount != 0)
1012 }
else if (decoder_status.ReadyListCount == 0) {
1027 if (rec_ret ==
RET_OK && *got_frame == 0) {
1049 ret = DtsGetDriverStatus(dev, &decoder_status);
1050 if (ret == BC_STS_SUCCESS &&
1051 decoder_status.ReadyListCount > 0) {
1053 if ((rec_ret ==
RET_OK && *got_frame > 0) ||
1064 "Don't output on next decode call.\n");
1081 #if CONFIG_H264_CRYSTALHD_DECODER
1089 AVCodec ff_h264_crystalhd_decoder = {
1090 .
name =
"h264_crystalhd",
1099 .long_name =
NULL_IF_CONFIG_SMALL(
"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1105 #if CONFIG_MPEG2_CRYSTALHD_DECODER
1106 static AVClass mpeg2_class = {
1113 AVCodec ff_mpeg2_crystalhd_decoder = {
1114 .
name =
"mpeg2_crystalhd",
1125 .priv_class = &mpeg2_class,
1129 #if CONFIG_MPEG4_CRYSTALHD_DECODER
1137 AVCodec ff_mpeg4_crystalhd_decoder = {
1138 .
name =
"mpeg4_crystalhd",
1153 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1154 static AVClass msmpeg4_class = {
1155 "msmpeg4_crystalhd",
1161 AVCodec ff_msmpeg4_crystalhd_decoder = {
1162 .
name =
"msmpeg4_crystalhd",
1171 .long_name =
NULL_IF_CONFIG_SMALL(
"MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1173 .priv_class = &msmpeg4_class,
1177 #if CONFIG_VC1_CRYSTALHD_DECODER
1185 AVCodec ff_vc1_crystalhd_decoder = {
1186 .
name =
"vc1_crystalhd",
1197 .priv_class = &vc1_class,
1201 #if CONFIG_WMV3_CRYSTALHD_DECODER
1209 AVCodec ff_wmv3_crystalhd_decoder = {
1210 .
name =
"wmv3_crystalhd",
1221 .priv_class = &wmv3_class,