Quoting Vittorio Giovara (2015-03-07 10:03:10)
> Signed-off-by: Vittorio Giovara <[email protected]>
> ---
>  Changelog               |   1 +
>  configure               |   2 +
>  doc/general.texi        |   1 +
>  libavcodec/Makefile     |   1 +
>  libavcodec/allcodecs.c  |   1 +
>  libavcodec/avcodec.h    |   1 +
>  libavcodec/codec_desc.c |   7 +
>  libavcodec/tdsc.c       | 585 
> ++++++++++++++++++++++++++++++++++++++++++++++++
>  libavcodec/version.h    |   2 +-
>  libavformat/riff.c      |   1 +
>  tests/fate/video.mak    |   3 +
>  tests/ref/fate/tdsc     |  42 ++++
>  12 files changed, 646 insertions(+), 1 deletion(-)
>  create mode 100644 libavcodec/tdsc.c
>  create mode 100644 tests/ref/fate/tdsc
> 
> diff --git a/Changelog b/Changelog
> index 31a550d..7dc4d7c 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -22,6 +22,7 @@ version <next>:
>  - Canopus HQX decoder
>  - RTP depacketization of T.140 text (RFC 4103)
>  - VP9 RTP payload format (draft 0) experimental depacketizer
> +- TDSC decoder
>  
>  
>  version 11:
> diff --git a/configure b/configure
> index 0e182b4..1e85ef6 100755
> --- a/configure
> +++ b/configure
> @@ -1905,6 +1905,8 @@ svq1_encoder_select="aandcttables hpeldsp me_cmp 
> mpegvideoenc"
>  svq3_decoder_select="h264_decoder hpeldsp tpeldsp"
>  svq3_decoder_suggest="zlib"
>  tak_decoder_select="audiodsp"
> +tdsc_decoder_deps="zlib"
> +tdsc_decoder_select="mjpeg_decoder"
>  theora_decoder_select="vp3_decoder"
>  thp_decoder_select="mjpeg_decoder"
>  tiff_decoder_suggest="zlib"
> diff --git a/doc/general.texi b/doc/general.texi
> index 21e5fe8..58ce1dc 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -409,6 +409,7 @@ library:
>  @item Sony Wave64 (W64)         @tab   @tab X
>  @item SoX native format         @tab X @tab X
>  @item SUN AU format             @tab X @tab X
> +@item TDSC                      @tab   @tab X
>  @item Text files                @tab   @tab X
>  @item THP                       @tab   @tab X
>      @tab Used on the Nintendo GameCube.
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 1c50f99..b132021 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -377,6 +377,7 @@ OBJS-$(CONFIG_SVQ3_DECODER)            += svq3.o svq13.o 
> mpegutils.o
>  OBJS-$(CONFIG_TAK_DECODER)             += takdec.o tak.o
>  OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
>  OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o rle.o
> +OBJS-$(CONFIG_TDSC_DECODER)            += tdsc.o
>  OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
>  OBJS-$(CONFIG_TIFF_DECODER)            += tiff.o lzw.o faxcompr.o
>  OBJS-$(CONFIG_TIFF_ENCODER)            += tiffenc.o rle.o lzwenc.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 392a2c9..4cba5fb 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -243,6 +243,7 @@ void avcodec_register_all(void)
>      REGISTER_ENCDEC (SVQ1,              svq1);
>      REGISTER_DECODER(SVQ3,              svq3);
>      REGISTER_ENCDEC (TARGA,             targa);
> +    REGISTER_DECODER(TDSC,              tdsc);
>      REGISTER_DECODER(THEORA,            theora);
>      REGISTER_DECODER(THP,               thp);
>      REGISTER_DECODER(TIERTEXSEQVIDEO,   tiertexseqvideo);
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 8b9e21f..e5569e2 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -294,6 +294,7 @@ enum AVCodecID {
>      AV_CODEC_ID_MVC1,
>      AV_CODEC_ID_MVC2,
>      AV_CODEC_ID_HQX,
> +    AV_CODEC_ID_TDSC,
>  
>      /* various PCM "codecs" */
>      AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the 
> start of audio codecs
> diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
> index 747ebe8..de46704 100644
> --- a/libavcodec/codec_desc.c
> +++ b/libavcodec/codec_desc.c
> @@ -1286,6 +1286,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
>          .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
>      },
>      {
> +        .id        = AV_CODEC_ID_TDSC,
> +        .type      = AVMEDIA_TYPE_VIDEO,
> +        .name      = "tdsc",
> +        .long_name = NULL_IF_CONFIG_SMALL("TDSC"),
> +        .props     = AV_CODEC_PROP_LOSSY,
> +    },
> +    {
>          .id        = AV_CODEC_ID_TIFF,
>          .type      = AVMEDIA_TYPE_VIDEO,
>          .name      = "tiff",
> diff --git a/libavcodec/tdsc.c b/libavcodec/tdsc.c
> new file mode 100644
> index 0000000..9f8c475
> --- /dev/null
> +++ b/libavcodec/tdsc.c
> @@ -0,0 +1,585 @@
> +/*
> + * TDSC decoder
> + * Copyright (C) 2015 Vittorio Giovara <[email protected]>
> + *
> + * This file is part of Libav.
> + *
> + * Libav 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.
> + *
> + * Libav 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 Libav; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + */
> +
> +/**
> + * @file
> + * TDSC decoder
> + *
> + * Fourcc: TSDC
> + *
> + * Codec is very simple:
> + *  it codes picture by tiles, storing them in raw BGR24 format or 
> compressing
> + *  them in JPEG. Frames can be full pictures or just updates to the previous
> + *  frame. Cursor is found in its own frame or at the bottom of the picture.
> + *  Every frame is then packed with ZLib.
> + *
> + * Supports: BGR24
> + */
> +
> +#include <stdint.h>
> +#include <zlib.h>
> +
> +#include "libavutil/imgutils.h"
> +
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "internal.h"
> +#include "mjpegdec.h"
> +
> +#define BITMAPINFOHEADER_SIZE 0x28
> +#define TDSF_HEADER_SIZE      0x56
> +#define TDSB_HEADER_SIZE      0x08
> +
> +typedef struct TDSCContext {
> +    MJpegDecodeContext jctx;    // *needs* to be first for inheritance

eeeeew

This smells like mpegvideo, can't this be fixed easily?

> +
> +    int width, height;
> +    GetByteContext gbc;
> +
> +    AVFrame *refframe;          // full decoded frame (without cursor)
> +    AVFrame *jpgframe;          // decoded jpeg tile
> +    uint8_t *tilebuffer;        // buffer containing tile data
> +
> +    // all that is cursor
> +    uint8_t    *cursor;
> +    int        cursor_stride;
> +    int        cursor_w, cursor_h, cursor_x, cursor_y;
> +    int        cursor_hot_x, cursor_hot_y;
> +} TDSCContext;
> +
> +static av_cold int tdsc_close(AVCodecContext *avctx)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +
> +    av_frame_free(&ctx->refframe);
> +    av_frame_free(&ctx->jpgframe);
> +    av_freep(&ctx->tilebuffer);
> +    av_freep(&ctx->cursor);
> +
> +    return ff_mjpeg_decode_end(avctx);
> +}
> +
> +static av_cold int tdsc_init(AVCodecContext *avctx)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    int ret;
> +
> +    ctx->refframe = av_frame_alloc();
> +    ctx->jpgframe = av_frame_alloc();
> +    if (!ctx->jpgframe)
> +        return AVERROR(ENOMEM);
> +
> +    ret = ff_mjpeg_decode_init(avctx);
> +    if (ret < 0) {
> +        tdsc_close(avctx);
> +        return ret;
> +    }
> +
> +    ctx->refframe->format = avctx->pix_fmt = AV_PIX_FMT_BGR24;
> +
> +    return 0;
> +}
> +
> +#define APPLY_ALPHA(src, new, alpha) \
> +    src = (src * (256 - alpha) + new * alpha) >> 8
> +
> +/* Paint a region over a buffer, without drawing out of its bounds. */
> +static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int 
> stride)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    const uint8_t *cursor = ctx->cursor;
> +    int x = ctx->cursor_x - ctx->cursor_hot_x;
> +    int y = ctx->cursor_y - ctx->cursor_hot_y;
> +    int w = ctx->cursor_w;
> +    int h = ctx->cursor_h;
> +    int i, j;
> +
> +    if (!ctx->cursor)
> +        return;
> +
> +    if (x + w > ctx->width)
> +        w = ctx->width - x;
> +    if (y + h > ctx->height)
> +        h = ctx->height - y;
> +    if (x < 0) {
> +        w      +=  x;
> +        cursor += -x * 4;
> +    } else {
> +        dst    +=  x * 3;
> +    }
> +    if (y < 0) {
> +        h      +=  y;
> +        cursor += -y * ctx->cursor_stride;
> +    } else {
> +        dst    +=  y * stride;
> +    }
> +    if (w < 0 || h < 0)
> +        return;
> +
> +    for (j = 0; j < h; j++) {
> +        for (i = 0; i < w; i++) {
> +            uint8_t alpha = cursor[i * 4];
> +            APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
> +            APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
> +            APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
> +        }
> +        dst    += stride;
> +        cursor += ctx->cursor_stride;
> +    }
> +}
> +
> +/* Load cursor data and store it in ABGR mode. */
> +static int tdsc_load_cursor(AVCodecContext *avctx, int action)
> +{
> +    TDSCContext *ctx  = avctx->priv_data;
> +
> +    ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
> +    ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
> +
> +    if (action == 3) {
> +        int i, j, k, bits, cursor_fmt;
> +        uint8_t *dst;
> +
> +        ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc);
> +        ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc);
> +        ctx->cursor_w     = bytestream2_get_le16(&ctx->gbc);
> +        ctx->cursor_h     = bytestream2_get_le16(&ctx->gbc);
> +
> +        ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4;
> +        /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
> +        cursor_fmt = bytestream2_get_le32(&ctx->gbc);
> +
> +        if (ctx->cursor_x >= avctx->width || ctx->cursor_y >= avctx->height) 
> {
> +            av_log(avctx, AV_LOG_ERROR,
> +                   "Invalid cursor position (%d.%d outside %dx%d).\n",
> +                   ctx->cursor_x, ctx->cursor_y, avctx->width, 
> avctx->height);
> +            return AVERROR_INVALIDDATA;
> +        }
> +        if (ctx->cursor_w < 1 || ctx->cursor_w > 256 ||
> +            ctx->cursor_h < 1 || ctx->cursor_h > 256) {
> +            av_log(avctx, AV_LOG_ERROR,
> +                   "Invalid cursor dimensions %dx%d.\n",
> +                   ctx->cursor_w, ctx->cursor_h);
> +            return AVERROR_INVALIDDATA;
> +        }
> +        if (ctx->cursor_hot_x > ctx->cursor_w ||
> +            ctx->cursor_hot_y > ctx->cursor_h) {
> +            av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position 
> %d.%d.\n",
> +                   ctx->cursor_hot_x, ctx->cursor_hot_y);
> +            ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1);
> +            ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1);
> +        }
> +
> +        ctx->cursor = av_realloc(ctx->cursor,
> +                                 ctx->cursor_stride * ctx->cursor_h);
> +        if (!ctx->cursor) {
> +            av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
> +            return AVERROR(ENOMEM);
> +        }
> +
> +        dst = ctx->cursor;
> +        /* here BE bytestream is used, even though frame is stored LE */
> +        switch (cursor_fmt) {
> +        case 0x01010004: // old monochrome
> +            for (j = 0; j < ctx->cursor_h; j++) {
> +                for (i = 0; i < ctx->cursor_w; i += 32) {
> +                    bits = bytestream2_get_be32(&ctx->gbc);
> +                    for (k = 0; k < 32; k++) {
> +                        dst[0] = !!(bits & 0x80000000);
> +                        dst   += 4;
> +                        bits <<= 1;
> +                    }
> +                }
> +                dst += ctx->cursor_stride - ctx->cursor_w * 4;
> +            }
> +
> +            dst = ctx->cursor;
> +            for (j = 0; j < ctx->cursor_h; j++) {
> +                for (i = 0; i < ctx->cursor_w; i += 32) {
> +                    bits = bytestream2_get_be32(&ctx->gbc);
> +                    for (k = 0; k < 32; k++) {
> +                        int mask_bit = !!(bits & 0x80000000);
> +                        switch (dst[0] * 2 + mask_bit) {
> +                        case 0:
> +                            dst[0] = 0xFF;
> +                            dst[1] = 0x00;
> +                            dst[2] = 0x00;
> +                            dst[3] = 0x00;
> +                            break;
> +                        case 1:
> +                            dst[0] = 0xFF;
> +                            dst[1] = 0xFF;
> +                            dst[2] = 0xFF;
> +                            dst[3] = 0xFF;
> +                            break;
> +                        default:
> +                            dst[0] = 0x00;
> +                            dst[1] = 0x00;
> +                            dst[2] = 0x00;
> +                            dst[3] = 0x00;
> +                        }
> +                        dst   += 4;
> +                        bits <<= 1;
> +                    }
> +                }
> +                dst += ctx->cursor_stride - ctx->cursor_w * 4;
> +            }
> +            break;
> +        case 0x20010004: // full colour - BGRA
> +        case 0x20010008: // full colour - RGBA
> +            /* Skip monochrome version of the cursor */
> +            bytestream2_skip(&ctx->gbc,
> +                             ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 
> 3));
> +            if (cursor_fmt & 8) { // RGBA -> ABGR
> +                for (j = 0; j < ctx->cursor_h; j++) {
> +                    for (i = 0; i < ctx->cursor_w; i++) {
> +                        int val = bytestream2_get_be32(&ctx->gbc);
> +                        *dst++ = val >> 24;
> +                        *dst++ = val >> 16;
> +                        *dst++ = val >>  8;
> +                        *dst++ = val >>  0;
> +                    }
> +                    dst += ctx->cursor_stride - ctx->cursor_w * 4;
> +                }
> +            } else { // BGRA -> ABGR
> +                for (j = 0; j < ctx->cursor_h; j++) {
> +                    for (i = 0; i < ctx->cursor_w; i++) {
> +                        int val = bytestream2_get_be32(&ctx->gbc);
> +                        *dst++ = val >>  0;
> +                        *dst++ = val >> 24;
> +                        *dst++ = val >> 16;
> +                        *dst++ = val >>  8;
> +                    }
> +                    dst += ctx->cursor_stride - ctx->cursor_w * 4;
> +                }
> +            }
> +            break;
> +        default:
> +            avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt);
> +            return AVERROR_PATCHWELCOME;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +/* Convert a single YUV pixel to RGB. */
> +static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
> +{
> +    out[0] = av_clip_uint8(Y + (             91881 * V + 32768 >> 16));
> +    out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
> +    out[2] = av_clip_uint8(Y + (116130 * U             + 32768 >> 16));
> +}
> +
> +/* Convert a YUV420 buffer to a RGB buffer. */
> +static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride,
> +                                       const uint8_t *srcy, int srcy_stride,
> +                                       const uint8_t *srcu, const uint8_t 
> *srcv,
> +                                       int srcuv_stride, int width, int 
> height)
> +{
> +    int col, line;
> +    for (line = 0; line < height; line++) {
> +        for (col = 0; col < width; col++)
> +            tdsc_yuv2rgb(dst + col * 3, srcy[col],
> +                         srcu[col >> 1] - 128, srcv[col >> 1] - 128);
> +
> +        dst  +=   dst_stride;
> +        srcy +=  srcy_stride;
> +        srcu += srcuv_stride * (line & 1);
> +        srcv += srcuv_stride * (line & 1);
> +    }
> +}
> +
> +/* Invoke the mjpeg decoder to decode the tile. */
> +static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
> +                                 int x, int y, int w, int h)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    MJpegDecodeContext *s = avctx->priv_data;
> +    AVPacket jpkt;
> +    int got_frame = 0;
> +    int ret;
> +
> +    /* This necessary to make the mjpeg decoder work when it is used with
> +     * different dimensions than the ones it was initialized with,
> +     * otherwise it triggers interlaced mode and doubles the output height */
> +    s->org_height = h;
> +
> +    /* Prepare a packet and send it to mjpeg decoder */
> +    av_init_packet(&jpkt);
> +    jpkt.data = ctx->tilebuffer;
> +    jpkt.size = tile_size;
> +
> +    ret = ff_mjpeg_decode_frame(avctx, ctx->jpgframe, &got_frame, &jpkt);
> +    if (ret < 0 || !got_frame || ctx->jpgframe->format != 
> AV_PIX_FMT_YUVJ420P) {
> +        av_log(avctx, AV_LOG_ERROR,
> +               "Jpeg decoding error (%d) for (%d) frame.\n",
> +               ret, got_frame);
> +
> +        /* Normally skip, error if explode */
> +        return -(avctx->err_recognition & AV_EF_EXPLODE);
> +    }
> +
> +    /* Let's paint ont the buffer */
> +    tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * 
> y,
> +              ctx->refframe->linesize[0],
> +              ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
> +              ctx->jpgframe->data[1], ctx->jpgframe->data[2],
> +              ctx->jpgframe->linesize[1], w, h);
> +
> +    av_frame_unref(ctx->jpgframe);
> +
> +    return 0;
> +}
> +
> +/* Parse frame and either copy data or decode jpeg. */
> +static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    int i, width, height;
> +
> +    /* Keep track of the correct context size */
> +    width  = avctx->width;
> +    height = avctx->height;
> +
> +    /* Iterate over the number of tiles */
> +    for (i = 0; i < number_tiles; i++) {
> +        int tile_size;
> +        int tile_mode;
> +        int x, y, w, h;
> +        int ret;
> +
> +        if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
> +            bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
> +            bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) {
> +            av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        tile_size = bytestream2_get_le32(&ctx->gbc);
> +        if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size)
> +            return AVERROR_INVALIDDATA;
> +
> +        tile_mode = bytestream2_get_le32(&ctx->gbc);
> +        bytestream2_skip(&ctx->gbc, 4); // unknown
> +        x = bytestream2_get_le32(&ctx->gbc);
> +        y = bytestream2_get_le32(&ctx->gbc);
> +        w = bytestream2_get_le32(&ctx->gbc) - x;
> +        h = bytestream2_get_le32(&ctx->gbc) - y;
> +
> +        if (x >= width || y >= height) {
> +            av_log(avctx, AV_LOG_ERROR,
> +                   "Invalid tile position (%d.%d outside %dx%d).\n",
> +                   x, y, width, height);
> +            return AVERROR_INVALIDDATA;
> +        }
> +        if (x + w > width || y + h > height) {
> +            av_log(avctx, AV_LOG_ERROR,
> +                   "Invalid tile size %dx%d\n", w, h);
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        ctx->tilebuffer = av_realloc(ctx->tilebuffer, tile_size);
> +        if (!ctx->tilebuffer)
> +            return AVERROR(ENOMEM);
> +
> +        bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
> +
> +        if (tile_mode == MKTAG('G','E','P','J')) {
> +            /* Decode jpeg tile and copy it in the reference frame */
> +            ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
> +            if (ret < 0)
> +                return ret;
> +        } else if (tile_mode == MKTAG(' ','W','A','R')) {
> +            /* Just copy the buffer to output */
> +            av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
> +                                ctx->refframe->linesize[0] * y,
> +                                ctx->refframe->linesize[0], ctx->tilebuffer,
> +                                w * 3, w * 3, h);
> +        } else {
> +            av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", 
> tile_mode);
> +            return AVERROR_INVALIDDATA;
> +        }
> +        av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, 
> y);
> +    }
> +
> +    /* Restore context format and dimensions because of mjpeg overlap */
> +    avctx->pix_fmt = ctx->refframe->format;
> +    return ff_set_dimensions(avctx, width, height);

Ugh, this is so ugly.
Can't you do either of
1) refactor the jpeg decoder to make it properly embeddable
2) use a full separate internal AVCodecContext

> +}
> +
> +static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    int ret;
> +
> +    /* BITMAPINFOHEADER
> +     * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx 
> */
> +    if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE)
> +        return AVERROR_INVALIDDATA;
> +
> +    ctx->refframe->width  = ctx->width  =  bytestream2_get_le32(&ctx->gbc);
> +    ctx->refframe->height = ctx->height = -bytestream2_get_le32(&ctx->gbc);
> +
> +    if (bytestream2_get_le16(&ctx->gbc) != 1 ||  // 1 plane
> +        bytestream2_get_le16(&ctx->gbc) != 24)   // BGR24
> +        return AVERROR_INVALIDDATA;
> +
> +    bytestream2_skip(&ctx->gbc, 24); // unused fields
> +
> +    ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
> +    if (ret < 0)
> +        return ret;
> +
> +    /* Allocate the reference frame if not already done */
> +    if (!ctx->refframe->data[0]) {

You must also check that the dimensions didn't change and reset the refs
if they did.

> +        ret = av_frame_get_buffer(ctx->refframe, 32);
> +        if (ret < 0)
> +            return ret;
> +    }
> +
> +    /* Decode alllll tiles in a frame */

wedged key?

> +    return tdsc_decode_tiles(avctx, number_tiles);
> +}
> +
> +static int tdsc_parse_dtsm(AVCodecContext *avctx)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    int ret;
> +    int action = bytestream2_get_le32(&ctx->gbc);
> +
> +    bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
> +
> +    if (action == 2 || action == 3) {
> +        ret = tdsc_load_cursor(avctx, action);
> +        /* Do not consider cursor errors fatal unless explode */
> +        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
> +            return ret;
> +    } else {
> +        avpriv_request_sample(avctx, "Cursor action %d", action);
> +    }
> +
> +    return 0;
> +}
> +
> +static int tdsc_decode_frame(AVCodecContext *avctx, void *data,
> +                             int *got_frame, AVPacket *avpkt)
> +{
> +    TDSCContext *ctx = avctx->priv_data;
> +    AVFrame *frame = data;
> +    int ret, tag_header, keyframe = 0;
> +    uLongf dlen = avctx->width * avctx->height * 4; // large enough for RAW

This apparently assumes the dimensions are set by the caller. You must
that they really are in init.

> +    uint8_t *buf = av_malloc(dlen);

The buffer size is constant, is it not? So why not store it in the
context?

> +
> +    if (!buf)
> +        return AVERROR(ENOMEM);
> +
> +    /* Frames are deflated, need to inflate them first */
> +    ret = uncompress(buf, &dlen, avpkt->data, avpkt->size);
> +    if (ret) {
> +        av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
> +        ret = AVERROR_UNKNOWN;
> +        goto out;
> +    }
> +
> +    bytestream2_init(&ctx->gbc, buf, dlen);
> +
> +    /* Check for tag and for size info */
> +    if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
> +        av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
> +        ret = AVERROR_INVALIDDATA;
> +        goto out;
> +    }
> +
> +    /* Read tag */
> +    tag_header = bytestream2_get_le32(&ctx->gbc);
> +
> +    if (tag_header == MKTAG('T','D','S','F')) {
> +        int number_tiles;
> +        if (bytestream2_get_bytes_left(&ctx->gbc) < TDSF_HEADER_SIZE) {
> +            av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
> +            ret = AVERROR_INVALIDDATA;
> +            goto out;
> +        }
> +        /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame 
> */
> +        number_tiles = bytestream2_get_le32(&ctx->gbc);
> +
> +        bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
> +        keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
> +
> +        ret = tdsc_parse_tdsf(avctx, number_tiles);
> +        if (ret < 0)
> +            goto out;
> +
> +        /* Check if there is anything else we are able to parse */
> +        if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
> +            tag_header = bytestream2_get_le32(&ctx->gbc);
> +    }
> +
> +    /* This tag can be after a TDSF block or on its own frame */
> +    if (tag_header == MKTAG('D','T','S','M')) {
> +        /* First 4 bytes here are the total size in bytes for this frame */
> +        int tag_size = bytestream2_get_le32(&ctx->gbc);
> +
> +        if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
> +            av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
> +            ret = AVERROR_INVALIDDATA;
> +            goto out;
> +        }
> +
> +        ret = tdsc_parse_dtsm(avctx);
> +        if (ret < 0)
> +            goto out;
> +    }
> +
> +    /* Duplicate frame and copy the reference frame */
> +    ret = ff_get_buffer(avctx, frame, 0);
> +    if (ret < 0)
> +        goto out;
> +    ret = av_frame_copy(frame, ctx->refframe);
> +    if (ret < 0)
> +        goto out;
> +
> +    /* Paint the cursor on the output frame */
> +    tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
> +
> +    /* Frame is ready to be output */
> +    if (keyframe) {
> +        frame->pict_type = AV_PICTURE_TYPE_I;
> +        frame->key_frame = 1;

If you bother setting the frame type at all, might as well do it for P
frames too.

> +    }
> +    *got_frame = 1;
> +out:
> +    av_freep(&buf);
> +    return ret;
> +}
> +
> +AVCodec ff_tdsc_decoder = {
> +    .name           = "tdsc",
> +    .long_name      = NULL_IF_CONFIG_SMALL("TDSC"),
> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = AV_CODEC_ID_TDSC,
> +    .init           = tdsc_init,
> +    .decode         = tdsc_decode_frame,
> +    .close          = tdsc_close,
> +    .priv_data_size = sizeof(TDSCContext),
> +    .capabilities   = CODEC_CAP_DR1,
> +};
> diff --git a/libavcodec/version.h b/libavcodec/version.h
> index 5392e05..816d7ea 100644
> --- a/libavcodec/version.h
> +++ b/libavcodec/version.h
> @@ -29,7 +29,7 @@
>  #include "libavutil/version.h"
>  
>  #define LIBAVCODEC_VERSION_MAJOR 56
> -#define LIBAVCODEC_VERSION_MINOR 17
> +#define LIBAVCODEC_VERSION_MINOR 18
>  #define LIBAVCODEC_VERSION_MICRO  0
>  
>  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
> diff --git a/libavformat/riff.c b/libavformat/riff.c
> index d5b3346..db91749 100644
> --- a/libavformat/riff.c
> +++ b/libavformat/riff.c
> @@ -352,6 +352,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
>      { AV_CODEC_ID_PRORES,       MKTAG('A', 'P', 'C', 'H') },
>      { AV_CODEC_ID_QTRLE,        MKTAG('r', 'l', 'e', ' ') },
>      { AV_CODEC_ID_HQX,          MKTAG('C', 'H', 'Q', 'X') },
> +    { AV_CODEC_ID_TDSC,         MKTAG('T', 'D', 'S', 'C') },
>      { AV_CODEC_ID_NONE,         0 }
>  };
>  
> diff --git a/tests/fate/video.mak b/tests/fate/video.mak
> index 1080c1c..0232f60 100644
> --- a/tests/fate/video.mak
> +++ b/tests/fate/video.mak
> @@ -248,6 +248,9 @@ fate-sp5x: CMD = framecrc -idct simple -i 
> $(TARGET_SAMPLES)/sp5x/sp5x_problem.av
>  FATE_SAMPLES_AVCONV-$(call DEMDEC, SRT, SRT) += fate-sub-srt
>  fate-sub-srt: CMD = md5 -i 
> $(TARGET_SAMPLES)/sub/SubRip_capability_tester.srt -f ass
>  
> +FATE_SAMPLES_AVCONV-$(call DEMDEC, ASF, TDSC) += fate-tdsc
> +fate-tdsc: CMD = framecrc -i $(TARGET_SAMPLES)/tdsc/tdsc.asf -an
> +
>  FATE_SAMPLES_AVCONV-$(call DEMDEC, THP, THP) += fate-thp
>  fate-thp: CMD = framecrc -idct simple -i 
> $(TARGET_SAMPLES)/thp/pikmin2-opening1-partial.thp -an
>  
> diff --git a/tests/ref/fate/tdsc b/tests/ref/fate/tdsc
> new file mode 100644
> index 0000000..945933f
> --- /dev/null
> +++ b/tests/ref/fate/tdsc
> @@ -0,0 +1,42 @@
> +#tb 0: 1/1000
> +0,          0,          0,        0,  3888000, 0x9c498657
> +0,        233,        233,        0,  3888000, 0x72a2ae22
> +0,        266,        266,        0,  3888000, 0x72a2ae22
> +0,        333,        333,        0,  3888000, 0x72a2ae22
> +0,        533,        533,        0,  3888000, 0x72a2ae22
> +0,        566,        566,        0,  3888000, 0x72a2ae22
> +0,        666,        666,        0,  3888000, 0x550e417b
> +0,        966,        966,        0,  3888000, 0x550e417b
> +0,        999,        999,        0,  3888000, 0x550e417b
> +0,       1033,       1033,        0,  3888000, 0x550e417b
> +0,       1066,       1066,        0,  3888000, 0x550e417b
> +0,       1133,       1133,        0,  3888000, 0x550e417b
> +0,       1166,       1166,        0,  3888000, 0x38dcde13
> +0,       1566,       1566,        0,  3888000, 0x2b7c0edd
> +0,       1599,       1599,        0,  3888000, 0xaaaf3c7b
> +0,       1633,       1633,        0,  3888000, 0x26d1710f
> +0,       1666,       1666,        0,  3888000, 0xa6609f3f
> +0,       1699,       1699,        0,  3888000, 0xaa41c6f3
> +0,       1733,       1733,        0,  3888000, 0xc0ffd4d5
> +0,       1766,       1766,        0,  3888000, 0x44d4f383
> +0,       1833,       1833,        0,  3888000, 0x517047eb
> +0,       1866,       1866,        0,  3888000, 0x1d5a4d5b
> +0,       1899,       1899,        0,  3888000, 0x7d2da2f6
> +0,       1933,       1933,        0,  3888000, 0x27f7a2f6
> +0,       1966,       1966,        0,  3888000, 0x9de49edb
> +0,       1999,       1999,        0,  3888000, 0x5ccb9f38
> +0,       2033,       2033,        0,  3888000, 0x88069fb2
> +0,       2066,       2066,        0,  3888000, 0x1d059fd3
> +0,       2099,       2099,        0,  3888000, 0xe16d9fd3
> +0,       2133,       2133,        0,  3888000, 0xb6a69fd3
> +0,       2166,       2166,        0,  3888000, 0xb6a69fd3
> +0,       2199,       2199,        0,  3888000, 0x61709fd3
> +0,       2233,       2233,        0,  3888000, 0xb6f59fd3
> +0,       2266,       2266,        0,  3888000, 0x5c7b9fd3
> +0,       2299,       2299,        0,  3888000, 0x57869fd3
> +0,       2333,       2333,        0,  3888000, 0x9d3f9fd3
> +0,       2433,       2433,        0,  3888000, 0x5e6082a5
> +0,       2466,       2466,        0,  3888000, 0x5e6082a5
> +0,       2499,       2499,        0,  3888000, 0x5e6082a5
> +0,       2533,       2533,        0,  3888000, 0x48ce82f3
> +0,       2566,       2566,        0,  3888000, 0x4c5ebeaf
> -- 
> 1.9.3 (Apple Git-50)
> 
> _______________________________________________
> libav-devel mailing list
> [email protected]
> https://lists.libav.org/mailman/listinfo/libav-devel

-- 
Anton Khirnov
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to