crystalhd.c 42.6 KB
Newer Older
1 2 3
/*
 * - CrystalHD decoder module -
 *
4
 * Copyright(C) 2010,2011 Philip Langdale <ffmpeg.philipl@overt.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * - Principles of Operation -
 *
 * The CrystalHD decoder operates at the bitstream level - which is an even
 * higher level than the decoding hardware you typically see in modern GPUs.
 * This means it has a very simple interface, in principle. You feed demuxed
 * packets in one end and get decoded picture (fields/frames) out the other.
 *
 * Of course, nothing is ever that simple. Due, at the very least, to b-frame
 * dependencies in the supported formats, the hardware has a delay between
 * when a packet goes in, and when a picture comes out. Furthermore, this delay
 * is not just a function of time, but also one of the dependency on additional
 * frames being fed into the decoder to satisfy the b-frame dependencies.
 *
 * As such, a pipeline will build up that is roughly equivalent to the required
 * DPB for the file being played. If that was all it took, things would still
 * be simple - so, of course, it isn't.
 *
 * The hardware has a way of indicating that a picture is ready to be copied out,
 * but this is unreliable - and sometimes the attempt will still fail so, based
 * on testing, the code will wait until 3 pictures are ready before starting
 * 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 * frames, the software should never fail to return a frame from a decode(),
 * this is a hard assertion to make, because the stream may switch between
 * differently encoded content (number of b-frames, interlacing, etc) which
 * might require a longer pipeline than before. If that happened, you could
 * deadlock trying to retrieve a frame that can't be decoded without feeding
 * in additional packets.
 *
 * As such, the code will return in the event that a picture cannot be copied
 * out, leading to an increase in the length of the pipeline. This in turn,
 * means we have to be sensitive to the time it takes to decode a picture;
 * We do not want to give up just because the hardware needed a little more
 * time to prepare the picture! For this reason, there are delays included
 * in the decode() path that ensure that, under normal conditions, the hardware
 * will only fail to return a frame if it really needs additional packets to
 * complete the decoding.
 *
 * Finally, to be explicit, we do not want the pipeline to grow without bound
 * for two reasons: 1) The hardware can only buffer a finite number of packets,
 * and 2) The client application may not be able to cope with arbitrarily long
 * delays in the video path relative to the audio path. For example. MPlayer
 * can only handle a 20 picture delay (although this is arbitrary, and needs
 * to be extended to fully support the CrystalHD where the delay could be up
 * to 32 pictures - consider PAFF H.264 content with 16 b-frames).
 */

/*****************************************************************************
 * Includes
 ****************************************************************************/

#define _XOPEN_SOURCE 600
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

#include <libcrystalhd/bc_dts_types.h>
#include <libcrystalhd/bc_dts_defs.h>
#include <libcrystalhd/libcrystalhd_if.h>

#include "avcodec.h"
86
#include "h264.h"
87
#include "internal.h"
88 89
#include "libavutil/imgutils.h"
#include "libavutil/intreadwrite.h"
90
#include "libavutil/opt.h"
91

92 93 94 95
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

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 104 105 106 107 108 109 110
#define WAIT_UNIT 1000


/*****************************************************************************
 * Module private data
 ****************************************************************************/

typedef enum {
111 112 113 114 115
    RET_ERROR           = -1,
    RET_OK              = 0,
    RET_COPY_AGAIN      = 1,
    RET_SKIP_NEXT_COPY  = 2,
    RET_COPY_NEXT_FIELD = 3,
116 117 118 119 120 121
} CopyRet;

typedef struct OpaqueList {
    struct OpaqueList *next;
    uint64_t fake_timestamp;
    uint64_t reordered_opaque;
122
    uint8_t pic_type;
123 124 125
} OpaqueList;

typedef struct {
126
    AVClass *av_class;
127
    AVCodecContext *avctx;
128
    AVFrame *pic;
129 130
    HANDLE dev;

131 132 133
    uint8_t *orig_extradata;
    uint32_t orig_extradata_size;

134
    AVBitStreamFilterContext *bsfc;
135 136
    AVCodecParserContext *parser;

137 138 139 140 141 142 143 144 145 146 147 148 149
    uint8_t is_70012;
    uint8_t *sps_pps_buf;
    uint32_t sps_pps_size;
    uint8_t is_nal;
    uint8_t output_ready;
    uint8_t need_second_field;
    uint8_t skip_next_output;
    uint64_t decode_wait;

    uint64_t last_picture;

    OpaqueList *head;
    OpaqueList *tail;
150 151 152

    /* Options */
    uint32_t sWidth;
153
    uint8_t bframe_bug;
154 155
} CHDContext;

156 157 158 159
static const AVOption options[] = {
    { "crystalhd_downscale_width",
      "Turn on downscaling to the specified width",
      offsetof(CHDContext, sWidth),
160
      AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
161 162 163 164
      AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM, },
    { NULL, },
};

165 166 167 168 169

/*****************************************************************************
 * Helper functions
 ****************************************************************************/

170
static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
171 172
{
    switch (id) {
173
    case AV_CODEC_ID_MPEG4:
174
        return BC_MSUBTYPE_DIVX;
175
    case AV_CODEC_ID_MSMPEG4V3:
176
        return BC_MSUBTYPE_DIVX311;
177
    case AV_CODEC_ID_MPEG2VIDEO:
178
        return BC_MSUBTYPE_MPEG2VIDEO;
179
    case AV_CODEC_ID_VC1:
180
        return BC_MSUBTYPE_VC1;
181
    case AV_CODEC_ID_WMV3:
182
        return BC_MSUBTYPE_WMV3;
183
    case AV_CODEC_ID_H264:
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
    default:
        return BC_MSUBTYPE_INVALID;
    }
}

static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
{
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
           output->YBuffDoneSz);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
           output->UVBuffDoneSz);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n",
           output->PicInfo.timeStamp);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
           output->PicInfo.picture_number);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
           output->PicInfo.width);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
           output->PicInfo.height);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
           output->PicInfo.chroma_format);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
           output->PicInfo.pulldown);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
           output->PicInfo.flags);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
           output->PicInfo.frame_rate);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
           output->PicInfo.aspect_ratio);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
           output->PicInfo.colour_primaries);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
           output->PicInfo.picture_meta_payload);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
           output->PicInfo.sess_num);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
           output->PicInfo.ycom);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
           output->PicInfo.custom_aspect_ratio_width_height);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
           output->PicInfo.n_drop);
    av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
           output->PicInfo.other.h264.valid);
}


/*****************************************************************************
 * OpaqueList functions
 ****************************************************************************/

236 237
static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque,
                                 uint8_t pic_type)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
    OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
    if (!newNode) {
        av_log(priv->avctx, AV_LOG_ERROR,
               "Unable to allocate new node in OpaqueList.\n");
        return 0;
    }
    if (!priv->head) {
        newNode->fake_timestamp = TIMESTAMP_UNIT;
        priv->head              = newNode;
    } else {
        newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
        priv->tail->next        = newNode;
    }
    priv->tail = newNode;
    newNode->reordered_opaque = reordered_opaque;
254
    newNode->pic_type = pic_type;
255 256 257 258 259 260 261 262

    return newNode->fake_timestamp;
}

/*
 * The OpaqueList is built in decode order, while elements will be removed
 * in presentation order. If frames are reordered, this means we must be
 * able to remove elements that are not the first element.
263 264
 *
 * Returned node must be freed by caller.
265
 */
266
static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
267 268 269 270 271 272
{
    OpaqueList *node = priv->head;

    if (!priv->head) {
        av_log(priv->avctx, AV_LOG_ERROR,
               "CrystalHD: Attempted to query non-existent timestamps.\n");
273
        return NULL;
274 275 276 277 278 279 280 281 282 283 284 285
    }

    /*
     * The first element is special-cased because we have to manipulate
     * the head pointer rather than the previous element in the list.
     */
    if (priv->head->fake_timestamp == fake_timestamp) {
        priv->head = node->next;

        if (!priv->head->next)
            priv->tail = priv->head;

286 287
        node->next = NULL;
        return node;
288 289 290 291 292 293 294
    }

    /*
     * The list is processed at arm's length so that we have the
     * previous element available to rewrite its next pointer.
     */
    while (node->next) {
295 296 297
        OpaqueList *current = node->next;
        if (current->fake_timestamp == fake_timestamp) {
            node->next = current->next;
298 299 300 301

            if (!node->next)
               priv->tail = node;

302 303
            current->next = NULL;
            return current;
304
        } else {
305
            node = current;
306 307 308 309 310
        }
    }

    av_log(priv->avctx, AV_LOG_VERBOSE,
           "CrystalHD: Couldn't match fake_timestamp.\n");
311
    return NULL;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}


/*****************************************************************************
 * Video decoder API function definitions
 ****************************************************************************/

static void flush(AVCodecContext *avctx)
{
    CHDContext *priv = avctx->priv_data;

    avctx->has_b_frames     = 0;
    priv->last_picture      = -1;
    priv->output_ready      = 0;
    priv->need_second_field = 0;
    priv->skip_next_output  = 0;
    priv->decode_wait       = BASE_WAIT;

330
    av_frame_unref (priv->pic);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    /* Flush mode 4 flushes all software and hardware buffers. */
    DtsFlushInput(priv->dev, 4);
}


static av_cold int uninit(AVCodecContext *avctx)
{
    CHDContext *priv = avctx->priv_data;
    HANDLE device;

    device = priv->dev;
    DtsStopDecoder(device);
    DtsCloseDecoder(device);
    DtsDeviceClose(device);

347 348 349 350 351 352 353 354 355 356 357 358 359
    /*
     * Restore original extradata, so that if the decoder is
     * reinitialised, the bitstream detection and filtering
     * will work as expected.
     */
    if (priv->orig_extradata) {
        av_free(avctx->extradata);
        avctx->extradata = priv->orig_extradata;
        avctx->extradata_size = priv->orig_extradata_size;
        priv->orig_extradata = NULL;
        priv->orig_extradata_size = 0;
    }

360
    av_parser_close(priv->parser);
361 362 363
    if (priv->bsfc) {
        av_bitstream_filter_close(priv->bsfc);
    }
364

365
    av_freep(&priv->sps_pps_buf);
366

367
    av_frame_free (&priv->pic);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

    if (priv->head) {
       OpaqueList *node = priv->head;
       while (node) {
          OpaqueList *next = node->next;
          av_free(node);
          node = next;
       }
    }

    return 0;
}


static av_cold int init(AVCodecContext *avctx)
{
    CHDContext* priv;
    BC_STATUS ret;
    BC_INFO_CRYSTAL version;
    BC_INPUT_FORMAT format = {
        .FGTEnable   = FALSE,
        .Progressive = TRUE,
        .OptFlags    = 0x80000000 | vdecFrameRate59_94 | 0x40,
        .width       = avctx->width,
        .height      = avctx->height,
    };

    BC_MEDIA_SUBTYPE subtype;

    uint32_t mode = DTS_PLAYBACK_MODE |
                    DTS_LOAD_FILE_PLAY_FW |
                    DTS_SKIP_TX_CHK_CPB |
                    DTS_PLAYBACK_DROP_RPT_MODE |
                    DTS_SINGLE_THREADED_MODE |
                    DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);

    av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
           avctx->codec->name);

407
    avctx->pix_fmt = AV_PIX_FMT_YUYV422;
408 409 410 411 412 413 414

    /* Initialize the library */
    priv               = avctx->priv_data;
    priv->avctx        = avctx;
    priv->is_nal       = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
    priv->last_picture = -1;
    priv->decode_wait  = BASE_WAIT;
415
    priv->pic          = av_frame_alloc();
416 417 418 419 420 421 422 423

    subtype = id2subtype(priv, avctx->codec->id);
    switch (subtype) {
    case BC_MSUBTYPE_AVC1:
        {
            uint8_t *dummy_p;
            int dummy_int;

424
            /* Back up the extradata so it can be restored at close time. */
425
            priv->orig_extradata = av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
426 427 428 429 430 431 432 433
            if (!priv->orig_extradata) {
                av_log(avctx, AV_LOG_ERROR,
                       "Failed to allocate copy of extradata\n");
                return AVERROR(ENOMEM);
            }
            priv->orig_extradata_size = avctx->extradata_size;
            memcpy(priv->orig_extradata, avctx->extradata, avctx->extradata_size);

434 435
            priv->bsfc = av_bitstream_filter_init("h264_mp4toannexb");
            if (!priv->bsfc) {
436 437 438 439
                av_log(avctx, AV_LOG_ERROR,
                       "Cannot open the h264_mp4toannexb BSF!\n");
                return AVERROR_BSF_NOT_FOUND;
            }
440
            av_bitstream_filter_filter(priv->bsfc, avctx, NULL, &dummy_p,
441 442
                                       &dummy_int, NULL, 0, 0);
        }
443 444
        subtype = BC_MSUBTYPE_H264;
        // Fall-through
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    case BC_MSUBTYPE_H264:
        format.startCodeSz = 4;
        // Fall-through
    case BC_MSUBTYPE_VC1:
    case BC_MSUBTYPE_WVC1:
    case BC_MSUBTYPE_WMV3:
    case BC_MSUBTYPE_WMVA:
    case BC_MSUBTYPE_MPEG2VIDEO:
    case BC_MSUBTYPE_DIVX:
    case BC_MSUBTYPE_DIVX311:
        format.pMetaData  = avctx->extradata;
        format.metaDataSz = avctx->extradata_size;
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
        return AVERROR(EINVAL);
    }
    format.mSubtype = subtype;

464 465 466 467 468
    if (priv->sWidth) {
        format.bEnableScaling = 1;
        format.ScalingParams.sWidth = priv->sWidth;
    }

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    /* Get a decoder instance */
    av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
    // Initialize the Link and Decoder devices
    ret = DtsDeviceOpen(&priv->dev, mode);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
        goto fail;
    }

    ret = DtsCrystalHDVersion(priv->dev, &version);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_VERBOSE,
               "CrystalHD: DtsCrystalHDVersion failed\n");
        goto fail;
    }
    priv->is_70012 = version.device == 0;

    if (priv->is_70012 &&
        (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
        av_log(avctx, AV_LOG_VERBOSE,
               "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
        goto fail;
    }

    ret = DtsSetInputFormat(priv->dev, &format);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
        goto fail;
    }

    ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
        goto fail;
    }

    ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
        goto fail;
    }
    ret = DtsStartDecoder(priv->dev);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
        goto fail;
    }
    ret = DtsStartCapture(priv->dev);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
        goto fail;
    }

521
    if (avctx->codec->id == AV_CODEC_ID_H264) {
522 523 524 525 526
        priv->parser = av_parser_init(avctx->codec->id);
        if (!priv->parser)
            av_log(avctx, AV_LOG_WARNING,
                   "Cannot open the h.264 parser! Interlaced h.264 content "
                   "will not be detected reliably.\n");
527
        priv->parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
528
    }
529 530 531 532 533 534 535 536 537 538 539 540
    av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");

    return 0;

 fail:
    uninit(avctx);
    return -1;
}


static inline CopyRet copy_frame(AVCodecContext *avctx,
                                 BC_DTS_PROC_OUT *output,
541
                                 void *data, int *got_frame)
542 543
{
    BC_STATUS ret;
544
    BC_DTS_STATUS decoder_status = { 0, };
545
    uint8_t trust_interlaced;
546 547 548
    uint8_t interlaced;

    CHDContext *priv = avctx->priv_data;
549
    int64_t pkt_pts  = AV_NOPTS_VALUE;
550
    uint8_t pic_type = 0;
551 552 553 554 555 556 557 558 559 560 561 562 563

    uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
                           VDEC_FLAG_BOTTOMFIELD;
    uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);

    int width    = output->PicInfo.width;
    int height   = output->PicInfo.height;
    int bwidth;
    uint8_t *src = output->Ybuff;
    int sStride;
    uint8_t *dst;
    int dStride;

564 565 566 567
    if (output->PicInfo.timeStamp != 0) {
        OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
        if (node) {
            pkt_pts = node->reordered_opaque;
568
            pic_type = node->pic_type;
569
            av_free(node);
570 571 572 573 574 575 576 577 578 579
        } else {
            /*
             * We will encounter a situation where a timestamp cannot be
             * popped if a second field is being returned. In this case,
             * each field has the same timestamp and the first one will
             * cause it to be popped. To keep subsequent calculations
             * simple, pic_type should be set a FIELD value - doesn't
             * matter which, but I chose BOTTOM.
             */
            pic_type = PICT_BOTTOM_FIELD;
580 581 582
        }
        av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
               output->PicInfo.timeStamp);
583 584
        av_log(avctx, AV_LOG_VERBOSE, "output picture type %d\n",
               pic_type);
585 586
    }

587 588 589 590 591 592 593
    ret = DtsGetDriverStatus(priv->dev, &decoder_status);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR,
               "CrystalHD: GetDriverStatus failed: %u\n", ret);
       return RET_ERROR;
    }

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    /*
     * For most content, we can trust the interlaced flag returned
     * by the hardware, but sometimes we can't. These are the
     * conditions under which we can trust the flag:
     *
     * 1) It's not h.264 content
     * 2) The UNKNOWN_SRC flag is not set
     * 3) We know we're expecting a second field
     * 4) The hardware reports this picture and the next picture
     *    have the same picture number.
     *
     * Note that there can still be interlaced content that will
     * fail this check, if the hardware hasn't decoded the next
     * picture or if there is a corruption in the stream. (In either
     * case a 0 will be returned for the next picture number)
     */
610
    trust_interlaced = avctx->codec->id != AV_CODEC_ID_H264 ||
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
                       !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
                       priv->need_second_field ||
                       (decoder_status.picNumFlags & ~0x40000000) ==
                       output->PicInfo.picture_number;

    /*
     * If we got a false negative for trust_interlaced on the first field,
     * we will realise our mistake here when we see that the picture number is that
     * of the previous picture. We cannot recover the frame and should discard the
     * second field to keep the correct number of output frames.
     */
    if (output->PicInfo.picture_number == priv->last_picture && !priv->need_second_field) {
        av_log(avctx, AV_LOG_WARNING,
               "Incorrectly guessed progressive frame. Discarding second field\n");
        /* Returning without providing a picture. */
        return RET_OK;
    }

    interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
                 trust_interlaced;
631

632 633 634 635 636 637 638
    if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
        av_log(avctx, AV_LOG_VERBOSE,
               "Next picture number unknown. Assuming progressive frame.\n");
    }

    av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d | trust_interlaced %d\n",
           interlaced, trust_interlaced);
639

640 641
    if (priv->pic->data[0] && !priv->need_second_field)
        av_frame_unref(priv->pic);
642 643 644

    priv->need_second_field = interlaced && !priv->need_second_field;

645
    if (!priv->pic->data[0]) {
646
        if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
647 648 649 650 651 652 653 654 655 656 657
            return RET_ERROR;
    }

    bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
    if (priv->is_70012) {
        int pStride;

        if (width <= 720)
            pStride = 720;
        else if (width <= 1280)
            pStride = 1280;
sebist's avatar
sebist committed
658
        else pStride = 1920;
659 660 661 662 663
        sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
    } else {
        sStride = bwidth;
    }

664 665
    dStride = priv->pic->linesize[0];
    dst     = priv->pic->data[0];
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

    av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");

    if (interlaced) {
        int dY = 0;
        int sY = 0;

        height /= 2;
        if (bottom_field) {
            av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
            dY = 1;
        } else {
            av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
            dY = 0;
        }

        for (sY = 0; sY < height; dY++, sY++) {
            memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
684
            dY++;
685 686 687 688 689
        }
    } else {
        av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
    }

690
    priv->pic->interlaced_frame = interlaced;
691
    if (interlaced)
692
        priv->pic->top_field_first = !bottom_first;
693

694
    priv->pic->pkt_pts = pkt_pts;
695 696

    if (!priv->need_second_field) {
697
        *got_frame       = 1;
698 699 700
        if ((ret = av_frame_ref(data, priv->pic)) < 0) {
            return ret;
        }
701 702
    }

703 704 705 706 707 708 709 710 711
    /*
     * Two types of PAFF content have been observed. One form causes the
     * hardware to return a field pair and the other individual fields,
     * even though the input is always individual fields. We must skip
     * copying on the next decode() call to maintain pipeline length in
     * the first case.
     */
    if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
        (pic_type == PICT_TOP_FIELD || pic_type == PICT_BOTTOM_FIELD)) {
712 713 714 715
        av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
        return RET_SKIP_NEXT_COPY;
    }

716
    /*
717 718 719 720 721 722 723 724 725 726
     * The logic here is purely based on empirical testing with samples.
     * If we need a second field, it could come from a second input packet,
     * or it could come from the same field-pair input packet at the current
     * field. In the first case, we should return and wait for the next time
     * round to get the second field, while in the second case, we should
     * ask the decoder for it immediately.
     *
     * Testing has shown that we are dealing with the fieldpair -> two fields
     * case if the VDEC_FLAG_UNKNOWN_SRC is not set or if the input picture
     * type was PICT_FRAME (in this second case, the flag might still be set)
727 728
     */
    return priv->need_second_field &&
729 730
           (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
            pic_type == PICT_FRAME) ?
731
           RET_COPY_NEXT_FIELD : RET_OK;
732 733 734 735
}


static inline CopyRet receive_frame(AVCodecContext *avctx,
736
                                    void *data, int *got_frame)
737 738 739 740 741 742 743 744 745
{
    BC_STATUS ret;
    BC_DTS_PROC_OUT output = {
        .PicInfo.width  = avctx->width,
        .PicInfo.height = avctx->height,
    };
    CHDContext *priv = avctx->priv_data;
    HANDLE dev       = priv->dev;

746
    *got_frame = 0;
747 748 749 750 751 752 753

    // Request decoded data from the driver
    ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
    if (ret == BC_STS_FMT_CHANGE) {
        av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
        avctx->width  = output.PicInfo.width;
        avctx->height = output.PicInfo.height;
sebist's avatar
sebist committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
        switch ( output.PicInfo.aspect_ratio ) {
        case vdecAspectRatioSquare:
            avctx->sample_aspect_ratio = (AVRational) {  1,  1};
            break;
        case vdecAspectRatio12_11:
            avctx->sample_aspect_ratio = (AVRational) { 12, 11};
            break;
        case vdecAspectRatio10_11:
            avctx->sample_aspect_ratio = (AVRational) { 10, 11};
            break;
        case vdecAspectRatio16_11:
            avctx->sample_aspect_ratio = (AVRational) { 16, 11};
            break;
        case vdecAspectRatio40_33:
            avctx->sample_aspect_ratio = (AVRational) { 40, 33};
            break;
        case vdecAspectRatio24_11:
            avctx->sample_aspect_ratio = (AVRational) { 24, 11};
            break;
        case vdecAspectRatio20_11:
            avctx->sample_aspect_ratio = (AVRational) { 20, 11};
            break;
        case vdecAspectRatio32_11:
            avctx->sample_aspect_ratio = (AVRational) { 32, 11};
            break;
        case vdecAspectRatio80_33:
            avctx->sample_aspect_ratio = (AVRational) { 80, 33};
            break;
        case vdecAspectRatio18_11:
            avctx->sample_aspect_ratio = (AVRational) { 18, 11};
            break;
        case vdecAspectRatio15_11:
            avctx->sample_aspect_ratio = (AVRational) { 15, 11};
            break;
        case vdecAspectRatio64_33:
            avctx->sample_aspect_ratio = (AVRational) { 64, 33};
            break;
        case vdecAspectRatio160_99:
            avctx->sample_aspect_ratio = (AVRational) {160, 99};
            break;
        case vdecAspectRatio4_3:
            avctx->sample_aspect_ratio = (AVRational) {  4,  3};
            break;
        case vdecAspectRatio16_9:
            avctx->sample_aspect_ratio = (AVRational) { 16,  9};
            break;
        case vdecAspectRatio221_1:
            avctx->sample_aspect_ratio = (AVRational) {221,  1};
            break;
        }
804 805 806 807 808 809 810 811 812 813 814 815
        return RET_COPY_AGAIN;
    } else if (ret == BC_STS_SUCCESS) {
        int copy_ret = -1;
        if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
            if (priv->last_picture == -1) {
                /*
                 * Init to one less, so that the incrementing code doesn't
                 * need to be special-cased.
                 */
                priv->last_picture = output.PicInfo.picture_number - 1;
            }

816
            if (avctx->codec->id == AV_CODEC_ID_MPEG4 &&
817
                output.PicInfo.timeStamp == 0 && priv->bframe_bug) {
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
                av_log(avctx, AV_LOG_VERBOSE,
                       "CrystalHD: Not returning packed frame twice.\n");
                priv->last_picture++;
                DtsReleaseOutputBuffs(dev, NULL, FALSE);
                return RET_COPY_AGAIN;
            }

            print_frame_info(priv, &output);

            if (priv->last_picture + 1 < output.PicInfo.picture_number) {
                av_log(avctx, AV_LOG_WARNING,
                       "CrystalHD: Picture Number discontinuity\n");
                /*
                 * Have we lost frames? If so, we need to shrink the
                 * pipeline length appropriately.
                 *
                 * XXX: I have no idea what the semantics of this situation
                 * are so I don't even know if we've lost frames or which
                 * ones.
                 *
                 * In any case, only warn the first time.
                 */
               priv->last_picture = output.PicInfo.picture_number - 1;
            }

843 844
            copy_ret = copy_frame(avctx, &output, data, got_frame);
            if (*got_frame > 0) {
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
                avctx->has_b_frames--;
                priv->last_picture++;
                av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
                       avctx->has_b_frames);
            }
        } else {
            /*
             * An invalid frame has been consumed.
             */
            av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
                                        "invalid PIB\n");
            avctx->has_b_frames--;
            copy_ret = RET_OK;
        }
        DtsReleaseOutputBuffs(dev, NULL, FALSE);

        return copy_ret;
    } else if (ret == BC_STS_BUSY) {
        return RET_COPY_AGAIN;
    } else {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
        return RET_ERROR;
    }
}


871
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
872 873
{
    BC_STATUS ret;
874
    BC_DTS_STATUS decoder_status = { 0, };
875 876 877
    CopyRet rec_ret;
    CHDContext *priv   = avctx->priv_data;
    HANDLE dev         = priv->dev;
878
    uint8_t *in_data   = avpkt->data;
879
    int len            = avpkt->size;
880
    int free_data      = 0;
881
    uint8_t pic_type   = 0;
882 883 884

    av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    if (avpkt->size == 7 && !priv->bframe_bug) {
        /*
         * The use of a drop frame triggers the bug
         */
        av_log(avctx, AV_LOG_INFO,
               "CrystalHD: Enabling work-around for packed b-frame bug\n");
        priv->bframe_bug = 1;
    } else if (avpkt->size == 8 && priv->bframe_bug) {
        /*
         * Delay frames don't trigger the bug
         */
        av_log(avctx, AV_LOG_INFO,
               "CrystalHD: Disabling work-around for packed b-frame bug\n");
        priv->bframe_bug = 0;
    }

901 902
    if (len) {
        int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
903 904

        if (priv->parser) {
905
            int ret = 0;
906

907 908
            if (priv->bsfc) {
                ret = av_bitstream_filter_filter(priv->bsfc, avctx, NULL,
909
                                                 &in_data, &len,
910 911
                                                 avpkt->data, len, 0);
            }
912
            free_data = ret > 0;
913 914 915 916

            if (ret >= 0) {
                uint8_t *pout;
                int psize;
917
                int index;
918 919
                H264Context *h = priv->parser->priv_data;

920
                index = av_parser_parse2(priv->parser, avctx, &pout, &psize,
921 922
                                         in_data, len, avctx->internal->pkt->pts,
                                         avctx->internal->pkt->dts, 0);
923 924 925 926
                if (index < 0) {
                    av_log(avctx, AV_LOG_WARNING,
                           "CrystalHD: Failed to parse h.264 packet to "
                           "detect interlacing.\n");
927
                } else if (index != len) {
928 929 930
                    av_log(avctx, AV_LOG_WARNING,
                           "CrystalHD: Failed to parse h.264 packet "
                           "completely. Interlaced frames may be "
931
                           "incorrectly detected.\n");
932 933 934
                } else {
                    av_log(avctx, AV_LOG_VERBOSE,
                           "CrystalHD: parser picture type %d\n",
935 936
                           h->picture_structure);
                    pic_type = h->picture_structure;
937 938 939 940 941 942 943
                }
            } else {
                av_log(avctx, AV_LOG_WARNING,
                       "CrystalHD: mp4toannexb filter failed to filter "
                       "packet. Interlaced frames may be incorrectly "
                       "detected.\n");
            }
944 945
        }

946 947 948 949 950 951 952 953 954 955
        if (len < tx_free - 1024) {
            /*
             * Despite being notionally opaque, either libcrystalhd or
             * the hardware itself will mangle pts values that are too
             * small or too large. The docs claim it should be in units
             * of 100ns. Given that we're nominally dealing with a black
             * box on both sides, any transform we do has no guarantee of
             * avoiding mangling so we need to build a mapping to values
             * we know will not be mangled.
             */
956
            uint64_t pts = opaque_list_push(priv, avctx->internal->pkt->pts, pic_type);
957
            if (!pts) {
958 959 960
                if (free_data) {
                    av_freep(&in_data);
                }
961 962 963 964
                return AVERROR(ENOMEM);
            }
            av_log(priv->avctx, AV_LOG_VERBOSE,
                   "input \"pts\": %"PRIu64"\n", pts);
965 966 967 968
            ret = DtsProcInput(dev, in_data, len, pts, 0);
            if (free_data) {
                av_freep(&in_data);
            }
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
            if (ret == BC_STS_BUSY) {
                av_log(avctx, AV_LOG_WARNING,
                       "CrystalHD: ProcInput returned busy\n");
                usleep(BASE_WAIT);
                return AVERROR(EBUSY);
            } else if (ret != BC_STS_SUCCESS) {
                av_log(avctx, AV_LOG_ERROR,
                       "CrystalHD: ProcInput failed: %u\n", ret);
                return -1;
            }
            avctx->has_b_frames++;
        } else {
            av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
            len = 0; // We didn't consume any bytes.
        }
    } else {
        av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
    }

    if (priv->skip_next_output) {
        av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
        priv->skip_next_output = 0;
        avctx->has_b_frames--;
        return len;
    }

    ret = DtsGetDriverStatus(dev, &decoder_status);
    if (ret != BC_STS_SUCCESS) {
        av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
        return -1;
    }

    /*
     * No frames ready. Don't try to extract.
     *
     * Empirical testing shows that ReadyListCount can be a damn lie,
     * and ProcOut still fails when count > 0. The same testing showed
     * that two more iterations were needed before ProcOutput would
     * succeed.
     */
    if (priv->output_ready < 2) {
        if (decoder_status.ReadyListCount != 0)
            priv->output_ready++;
        usleep(BASE_WAIT);
        av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
        return len;
    } else if (decoder_status.ReadyListCount == 0) {
        /*
         * After the pipeline is established, if we encounter a lack of frames
         * that probably means we're not giving the hardware enough time to
         * decode them, so start increasing the wait time at the end of a
         * decode call.
         */
        usleep(BASE_WAIT);
        priv->decode_wait += WAIT_UNIT;
        av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
        return len;
    }

    do {
1029 1030
        rec_ret = receive_frame(avctx, data, got_frame);
        if (rec_ret == RET_OK && *got_frame == 0) {
1031 1032 1033 1034 1035 1036 1037 1038 1039
            /*
             * This case is for when the encoded fields are stored
             * separately and we get a separate avpkt for each one. To keep
             * the pipeline stable, we should return nothing and wait for
             * the next time round to grab the second field.
             * H.264 PAFF is an example of this.
             */
            av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
            avctx->has_b_frames--;
1040
        } else if (rec_ret == RET_COPY_NEXT_FIELD) {
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
            /*
             * This case is for when the encoded fields are stored in a
             * single avpkt but the hardware returns then separately. Unless
             * we grab the second field before returning, we'll slip another
             * frame in the pipeline and if that happens a lot, we're sunk.
             * So we have to get that second field now.
             * Interlaced mpeg2 and vc1 are examples of this.
             */
            av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
            while (1) {
                usleep(priv->decode_wait);
                ret = DtsGetDriverStatus(dev, &decoder_status);
                if (ret == BC_STS_SUCCESS &&
                    decoder_status.ReadyListCount > 0) {
1055 1056
                    rec_ret = receive_frame(avctx, data, got_frame);
                    if ((rec_ret == RET_OK && *got_frame > 0) ||
1057 1058
                        rec_ret == RET_ERROR)
                        break;
1059
                }
1060 1061
            }
            av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
        } else if (rec_ret == RET_SKIP_NEXT_COPY) {
            /*
             * Two input packets got turned into a field pair. Gawd.
             */
            av_log(avctx, AV_LOG_VERBOSE,
                   "Don't output on next decode call.\n");
            priv->skip_next_output = 1;
        }
        /*
         * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
         * a FMT_CHANGE event and need to go around again for the actual frame,
         * we got a busy status and need to try again, or we're dealing with
         * packed b-frames, where the hardware strangely returns the packed
         * p-frame twice. We choose to keep the second copy as it carries the
         * valid pts.
         */
    } while (rec_ret == RET_COPY_AGAIN);
    usleep(priv->decode_wait);
    return len;
}


#if CONFIG_H264_CRYSTALHD_DECODER
1085 1086 1087 1088 1089 1090 1091
static AVClass h264_class = {
    "h264_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1092 1093
AVCodec ff_h264_crystalhd_decoder = {
    .name           = "h264_crystalhd",
1094
    .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1095
    .type           = AVMEDIA_TYPE_VIDEO,
1096
    .id             = AV_CODEC_ID_H264,
1097 1098 1099 1100
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
1101
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1102
    .flush          = flush,
1103
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1104
    .priv_class     = &h264_class,
1105 1106 1107 1108
};
#endif

#if CONFIG_MPEG2_CRYSTALHD_DECODER
1109 1110 1111 1112 1113 1114 1115
static AVClass mpeg2_class = {
    "mpeg2_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1116 1117
AVCodec ff_mpeg2_crystalhd_decoder = {
    .name           = "mpeg2_crystalhd",
1118
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
1119
    .type           = AVMEDIA_TYPE_VIDEO,
1120
    .id             = AV_CODEC_ID_MPEG2VIDEO,
1121 1122 1123 1124
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
1125
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1126
    .flush          = flush,
1127
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1128
    .priv_class     = &mpeg2_class,
1129 1130 1131 1132
};
#endif

#if CONFIG_MPEG4_CRYSTALHD_DECODER
1133 1134 1135 1136 1137 1138 1139
static AVClass mpeg4_class = {
    "mpeg4_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1140 1141
AVCodec ff_mpeg4_crystalhd_decoder = {
    .name           = "mpeg4_crystalhd",
1142
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
1143
    .type           = AVMEDIA_TYPE_VIDEO,
1144
    .id             = AV_CODEC_ID_MPEG4,
1145 1146 1147 1148
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
1149
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1150
    .flush          = flush,
1151
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1152
    .priv_class     = &mpeg4_class,
1153 1154 1155 1156
};
#endif

#if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1157 1158 1159 1160 1161 1162 1163
static AVClass msmpeg4_class = {
    "msmpeg4_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1164 1165
AVCodec ff_msmpeg4_crystalhd_decoder = {
    .name           = "msmpeg4_crystalhd",
1166
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1167
    .type           = AVMEDIA_TYPE_VIDEO,
1168
    .id             = AV_CODEC_ID_MSMPEG4V3,
1169 1170 1171 1172 1173 1174
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
    .flush          = flush,
1175
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1176
    .priv_class     = &msmpeg4_class,
1177 1178 1179 1180
};
#endif

#if CONFIG_VC1_CRYSTALHD_DECODER
1181 1182 1183 1184 1185 1186 1187
static AVClass vc1_class = {
    "vc1_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1188 1189
AVCodec ff_vc1_crystalhd_decoder = {
    .name           = "vc1_crystalhd",
1190
    .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
1191
    .type           = AVMEDIA_TYPE_VIDEO,
1192
    .id             = AV_CODEC_ID_VC1,
1193 1194 1195 1196
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
1197
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1198
    .flush          = flush,
1199
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1200
    .priv_class     = &vc1_class,
1201 1202 1203 1204
};
#endif

#if CONFIG_WMV3_CRYSTALHD_DECODER
1205 1206 1207 1208 1209 1210 1211
static AVClass wmv3_class = {
    "wmv3_crystalhd",
    av_default_item_name,
    options,
    LIBAVUTIL_VERSION_INT,
};

1212 1213
AVCodec ff_wmv3_crystalhd_decoder = {
    .name           = "wmv3_crystalhd",
1214
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
1215
    .type           = AVMEDIA_TYPE_VIDEO,
1216
    .id             = AV_CODEC_ID_WMV3,
1217 1218 1219 1220
    .priv_data_size = sizeof(CHDContext),
    .init           = init,
    .close          = uninit,
    .decode         = decode,
1221
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1222
    .flush          = flush,
1223
    .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE},
1224
    .priv_class     = &wmv3_class,
1225 1226
};
#endif