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> 
   97 #define OUTPUT_PROC_TIMEOUT 50 
   99 #define TIMESTAMP_UNIT 100000 
  101 #define BASE_WAIT 10000 
  103 #define WAIT_UNIT 1000 
  157     { 
"crystalhd_downscale_width",
 
  158       "Turn on downscaling to the specified width",
 
  174         return BC_MSUBTYPE_DIVX;
 
  176         return BC_MSUBTYPE_DIVX311;
 
  178         return BC_MSUBTYPE_MPEG2VIDEO;
 
  180         return BC_MSUBTYPE_VC1;
 
  182         return BC_MSUBTYPE_WMV3;
 
  184         return priv->
is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
 
  186         return BC_MSUBTYPE_INVALID;
 
  194            output->YBuffDoneSz);
 
  196            output->UVBuffDoneSz);
 
  198            output->PicInfo.timeStamp);
 
  200            output->PicInfo.picture_number);
 
  202            output->PicInfo.width);
 
  204            output->PicInfo.height);
 
  206            output->PicInfo.chroma_format);
 
  208            output->PicInfo.pulldown);
 
  210            output->PicInfo.flags);
 
  212            output->PicInfo.frame_rate);
 
  214            output->PicInfo.aspect_ratio);
 
  216            output->PicInfo.colour_primaries);
 
  218            output->PicInfo.picture_meta_payload);
 
  220            output->PicInfo.sess_num);
 
  222            output->PicInfo.ycom);
 
  224            output->PicInfo.custom_aspect_ratio_width_height);
 
  226            output->PicInfo.n_drop);
 
  228            output->PicInfo.other.h264.valid);
 
  242                "Unable to allocate new node in OpaqueList.\n");
 
  247         priv->
head              = newNode;
 
  252     priv->
tail = newNode;
 
  272                "CrystalHD: Attempted to query non-existent timestamps.\n");
 
  302             current->
next = NULL;
 
  310            "CrystalHD: Couldn't match fake_timestamp.\n");
 
  333     DtsFlushInput(priv->
dev, 4);
 
  343     DtsStopDecoder(device);
 
  344     DtsCloseDecoder(device);
 
  345     DtsDeviceClose(device);
 
  387     BC_INPUT_FORMAT format = {
 
  390         .OptFlags    = 0x80000000 | vdecFrameRate59_94 | 0x40,
 
  391         .width       = avctx->
width,
 
  395     BC_MEDIA_SUBTYPE subtype;
 
  397     uint32_t 
mode = DTS_PLAYBACK_MODE |
 
  398                     DTS_LOAD_FILE_PLAY_FW |
 
  399                     DTS_SKIP_TX_CHK_CPB |
 
  400                     DTS_PLAYBACK_DROP_RPT_MODE |
 
  401                     DTS_SINGLE_THREADED_MODE |
 
  402                     DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
 
  419     case BC_MSUBTYPE_AVC1:
 
  428                        "Failed to allocate copy of extradata\n");
 
  437                        "Cannot open the h264_mp4toannexb BSF!\n");
 
  441                                        &dummy_int, NULL, 0, 0);
 
  443         subtype = BC_MSUBTYPE_H264;
 
  445     case BC_MSUBTYPE_H264:
 
  446         format.startCodeSz = 4;
 
  448     case BC_MSUBTYPE_VC1:
 
  449     case BC_MSUBTYPE_WVC1:
 
  450     case BC_MSUBTYPE_WMV3:
 
  451     case BC_MSUBTYPE_WMVA:
 
  452     case BC_MSUBTYPE_MPEG2VIDEO:
 
  453     case BC_MSUBTYPE_DIVX:
 
  454     case BC_MSUBTYPE_DIVX311:
 
  462     format.mSubtype = subtype;
 
  465         format.bEnableScaling = 1;
 
  466         format.ScalingParams.sWidth = priv->
sWidth;
 
  472     ret = DtsDeviceOpen(&priv->
dev, mode);
 
  473     if (ret != BC_STS_SUCCESS) {
 
  478     ret = DtsCrystalHDVersion(priv->
dev, &version);
 
  479     if (ret != BC_STS_SUCCESS) {
 
  481                "CrystalHD: DtsCrystalHDVersion failed\n");
 
  484     priv->
is_70012 = version.device == 0;
 
  487         (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
 
  489                "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
 
  493     ret = DtsSetInputFormat(priv->
dev, &format);
 
  494     if (ret != BC_STS_SUCCESS) {
 
  499     ret = DtsOpenDecoder(priv->
dev, BC_STREAM_TYPE_ES);
 
  500     if (ret != BC_STS_SUCCESS) {
 
  505     ret = DtsSetColorSpace(priv->
dev, OUTPUT_MODE422_YUY2);
 
  506     if (ret != BC_STS_SUCCESS) {
 
  510     ret = DtsStartDecoder(priv->
dev);
 
  511     if (ret != BC_STS_SUCCESS) {
 
  515     ret = DtsStartCapture(priv->
dev);
 
  516     if (ret != BC_STS_SUCCESS) {
 
  525                    "Cannot open the h.264 parser! Interlaced h.264 content " 
  526                    "will not be detected reliably.\n");
 
  540                                  BC_DTS_PROC_OUT *output,
 
  541                                  void *
data, 
int *got_frame)
 
  544     BC_DTS_STATUS decoder_status = { 0, };
 
  552     uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
 
  553                            VDEC_FLAG_BOTTOMFIELD;
 
  554     uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
 
  556     int width    = output->PicInfo.width;
 
  557     int height   = output->PicInfo.height;
 
  564     if (output->PicInfo.timeStamp != 0) {
 
  582                output->PicInfo.timeStamp);
 
  587     ret = DtsGetDriverStatus(priv->
dev, &decoder_status);
 
  588     if (ret != BC_STS_SUCCESS) {
 
  590                "CrystalHD: GetDriverStatus failed: %u\n", ret);
 
  611                        !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
 
  613                        (decoder_status.picNumFlags & ~0x40000000) ==
 
  614                        output->PicInfo.picture_number;
 
  624                "Incorrectly guessed progressive frame. Discarding second field\n");
 
  629     interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
 
  632     if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
 
  634                "Next picture number unknown. Assuming progressive frame.\n");
 
  638            interlaced, trust_interlaced);
 
  656         else if (width <= 1280)
 
  682         for (sY = 0; sY < 
height; dY++, sY++) {
 
  683             memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
 
  710     if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
 
  729            (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
 
  736                                     void *
data, 
int *got_frame)
 
  739     BC_DTS_PROC_OUT output = {
 
  740         .PicInfo.width  = avctx->
width,
 
  741         .PicInfo.height = avctx->
height,
 
  750     if (ret == BC_STS_FMT_CHANGE) {
 
  752         avctx->
width  = output.PicInfo.width;
 
  753         avctx->
height = output.PicInfo.height;
 
  754         switch ( output.PicInfo.aspect_ratio ) {
 
  755         case vdecAspectRatioSquare:
 
  758         case vdecAspectRatio12_11:
 
  761         case vdecAspectRatio10_11:
 
  764         case vdecAspectRatio16_11:
 
  767         case vdecAspectRatio40_33:
 
  770         case vdecAspectRatio24_11:
 
  773         case vdecAspectRatio20_11:
 
  776         case vdecAspectRatio32_11:
 
  779         case vdecAspectRatio80_33:
 
  782         case vdecAspectRatio18_11:
 
  785         case vdecAspectRatio15_11:
 
  788         case vdecAspectRatio64_33:
 
  791         case vdecAspectRatio160_99:
 
  794         case vdecAspectRatio4_3:
 
  797         case vdecAspectRatio16_9:
 
  800         case vdecAspectRatio221_1:
 
  805     } 
else if (ret == BC_STS_SUCCESS) {
 
  807         if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
 
  817                 output.PicInfo.timeStamp == 0 && priv->
bframe_bug) {
 
  819                        "CrystalHD: Not returning packed frame twice.\n");
 
  821                 DtsReleaseOutputBuffs(dev, NULL, 
FALSE);
 
  827             if (priv->
last_picture + 1 < output.PicInfo.picture_number) {
 
  829                        "CrystalHD: Picture Number discontinuity\n");
 
  843             copy_ret = 
copy_frame(avctx, &output, data, got_frame);
 
  844             if (*got_frame > 0) {
 
  859         DtsReleaseOutputBuffs(dev, NULL, 
FALSE);
 
  862     } 
else if (ret == BC_STS_BUSY) {
 
  874     BC_DTS_STATUS decoder_status = { 0, };
 
  890                "CrystalHD: Enabling work-around for packed b-frame bug\n");
 
  897                "CrystalHD: Disabling work-around for packed b-frame bug\n");
 
  910                                                  avpkt->
data, len, 0);
 
  925                            "CrystalHD: Failed to parse h.264 packet to " 
  926                            "detect interlacing.\n");
 
  927                 } 
else if (index != len) {
 
  929                            "CrystalHD: Failed to parse h.264 packet " 
  930                            "completely. Interlaced frames may be " 
  931                            "incorrectly detected.\n");
 
  934                            "CrystalHD: parser picture type %d\n",
 
  940                        "CrystalHD: mp4toannexb filter failed to filter " 
  941                        "packet. Interlaced frames may be incorrectly " 
  946         if (len < tx_free - 1024) {
 
  964                    "input \"pts\": %"PRIu64
"\n", pts);
 
  965             ret = DtsProcInput(dev, in_data, len, pts, 0);
 
  969             if (ret == BC_STS_BUSY) {
 
  971                        "CrystalHD: ProcInput returned busy\n");
 
  974             } 
else if (ret != BC_STS_SUCCESS) {
 
  976                        "CrystalHD: ProcInput failed: %u\n", ret);
 
  995     ret = DtsGetDriverStatus(dev, &decoder_status);
 
  996     if (ret != BC_STS_SUCCESS) {
 
 1010         if (decoder_status.ReadyListCount != 0)
 
 1015     } 
else if (decoder_status.ReadyListCount == 0) {
 
 1030         if (rec_ret == 
RET_OK && *got_frame == 0) {
 
 1052                 ret = DtsGetDriverStatus(dev, &decoder_status);
 
 1053                 if (ret == BC_STS_SUCCESS &&
 
 1054                     decoder_status.ReadyListCount > 0) {
 
 1056                     if ((rec_ret == 
RET_OK && *got_frame > 0) ||
 
 1067                    "Don't output on next decode call.\n");
 
 1084 #if CONFIG_H264_CRYSTALHD_DECODER 
 1092 AVCodec ff_h264_crystalhd_decoder = {
 
 1093     .
name           = 
"h264_crystalhd",
 
 1094     .long_name      = 
NULL_IF_CONFIG_SMALL(
"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
 
 1108 #if CONFIG_MPEG2_CRYSTALHD_DECODER 
 1109 static AVClass mpeg2_class = {
 
 1116 AVCodec ff_mpeg2_crystalhd_decoder = {
 
 1117     .
name           = 
"mpeg2_crystalhd",
 
 1128     .priv_class     = &mpeg2_class,
 
 1132 #if CONFIG_MPEG4_CRYSTALHD_DECODER 
 1140 AVCodec ff_mpeg4_crystalhd_decoder = {
 
 1141     .
name           = 
"mpeg4_crystalhd",
 
 1156 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER 
 1157 static AVClass msmpeg4_class = {
 
 1158     "msmpeg4_crystalhd",
 
 1164 AVCodec ff_msmpeg4_crystalhd_decoder = {
 
 1165     .
name           = 
"msmpeg4_crystalhd",
 
 1166     .long_name      = 
NULL_IF_CONFIG_SMALL(
"MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
 
 1176     .priv_class     = &msmpeg4_class,
 
 1180 #if CONFIG_VC1_CRYSTALHD_DECODER 
 1188 AVCodec ff_vc1_crystalhd_decoder = {
 
 1189     .
name           = 
"vc1_crystalhd",
 
 1200     .priv_class     = &vc1_class,
 
 1204 #if CONFIG_WMV3_CRYSTALHD_DECODER 
 1212 AVCodec ff_wmv3_crystalhd_decoder = {
 
 1213     .
name           = 
"wmv3_crystalhd",
 
 1224     .priv_class     = &wmv3_class,