~siretart/libav/trusty

« back to all changes in this revision

Viewing changes to libavcodec/txd.c

  • Committer: Reinhard Tartler
  • Date: 2013-10-23 03:04:17 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: siretart@tauware.de-20131023030417-1o6mpkl1l0raifjt
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "libavutil/intreadwrite.h"
25
25
#include "libavutil/imgutils.h"
26
26
#include "avcodec.h"
 
27
#include "bytestream.h"
 
28
#include "internal.h"
27
29
#include "s3tc.h"
28
30
 
29
31
typedef struct TXDContext {
39
41
    return 0;
40
42
}
41
43
 
42
 
static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
 
44
static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
43
45
                            AVPacket *avpkt) {
44
 
    const uint8_t *buf = avpkt->data;
45
46
    TXDContext * const s = avctx->priv_data;
 
47
    GetByteContext gb;
46
48
    AVFrame *picture = data;
47
49
    AVFrame * const p = &s->picture;
48
 
    unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags;
 
50
    unsigned int version, w, h, d3d_format, depth, stride, flags;
49
51
    unsigned int y, v;
50
52
    uint8_t *ptr;
51
 
    const uint8_t *cur = buf;
52
 
    const uint32_t *palette = (const uint32_t *)(cur + 88);
53
53
    uint32_t *pal;
54
54
 
55
 
    version         = AV_RL32(cur);
56
 
    d3d_format      = AV_RL32(cur+76);
57
 
    w               = AV_RL16(cur+80);
58
 
    h               = AV_RL16(cur+82);
59
 
    depth           = AV_RL8 (cur+84);
60
 
    mipmap_count    = AV_RL8 (cur+85);
61
 
    flags           = AV_RL8 (cur+87);
62
 
    cur            += 92;
 
55
    bytestream2_init(&gb, avpkt->data, avpkt->size);
 
56
    version         = bytestream2_get_le32(&gb);
 
57
    bytestream2_skip(&gb, 72);
 
58
    d3d_format      = bytestream2_get_le32(&gb);
 
59
    w               = bytestream2_get_le16(&gb);
 
60
    h               = bytestream2_get_le16(&gb);
 
61
    depth           = bytestream2_get_byte(&gb);
 
62
    bytestream2_skip(&gb, 2);
 
63
    flags           = bytestream2_get_byte(&gb);
63
64
 
64
65
    if (version < 8 || version > 9) {
65
66
        av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
68
69
    }
69
70
 
70
71
    if (depth == 8) {
71
 
        avctx->pix_fmt = PIX_FMT_PAL8;
72
 
        cur += 1024;
73
 
    } else if (depth == 16 || depth == 32)
74
 
        avctx->pix_fmt = PIX_FMT_RGB32;
75
 
    else {
 
72
        avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
73
    } else if (depth == 16 || depth == 32) {
 
74
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
 
75
    } else {
76
76
        av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
77
77
        return -1;
78
78
    }
84
84
        return -1;
85
85
    if (w != avctx->width || h != avctx->height)
86
86
        avcodec_set_dimensions(avctx, w, h);
87
 
    if (avctx->get_buffer(avctx, p) < 0) {
 
87
    if (ff_get_buffer(avctx, p) < 0) {
88
88
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
89
89
        return -1;
90
90
    }
96
96
 
97
97
    if (depth == 8) {
98
98
        pal = (uint32_t *) p->data[1];
99
 
        for (y=0; y<256; y++) {
100
 
            v = AV_RB32(palette+y);
101
 
            pal[y] = (v>>8) + (v<<24);
 
99
        for (y = 0; y < 256; y++) {
 
100
            v = bytestream2_get_be32(&gb);
 
101
            pal[y] = (v >> 8) + (v << 24);
102
102
        }
 
103
        bytestream2_skip(&gb, 4);
103
104
        for (y=0; y<h; y++) {
104
 
            memcpy(ptr, cur, w);
 
105
            bytestream2_get_buffer(&gb, ptr, w);
105
106
            ptr += stride;
106
 
            cur += w;
107
107
        }
108
108
    } else if (depth == 16) {
 
109
        bytestream2_skip(&gb, 4);
109
110
        switch (d3d_format) {
110
111
        case 0:
111
112
            if (!(flags & 1))
112
113
                goto unsupported;
113
114
        case FF_S3TC_DXT1:
114
 
            ff_decode_dxt1(cur, ptr, w, h, stride);
 
115
            ff_decode_dxt1(&gb, ptr, w, h, stride);
115
116
            break;
116
117
        case FF_S3TC_DXT3:
117
 
            ff_decode_dxt3(cur, ptr, w, h, stride);
 
118
            ff_decode_dxt3(&gb, ptr, w, h, stride);
118
119
            break;
119
120
        default:
120
121
            goto unsupported;
124
125
        case 0x15:
125
126
        case 0x16:
126
127
            for (y=0; y<h; y++) {
127
 
                memcpy(ptr, cur, w*4);
 
128
                bytestream2_get_buffer(&gb, ptr, w * 4);
128
129
                ptr += stride;
129
 
                cur += w*4;
130
130
            }
131
131
            break;
132
132
        default:
134
134
        }
135
135
    }
136
136
 
137
 
    for (; mipmap_count > 1; mipmap_count--)
138
 
        cur += AV_RL32(cur) + 4;
139
 
 
140
137
    *picture   = s->picture;
141
 
    *data_size = sizeof(AVPicture);
 
138
    *got_frame = 1;
142
139
 
143
 
    return cur - buf;
 
140
    return avpkt->size;
144
141
 
145
142
unsupported:
146
143
    av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
159
156
AVCodec ff_txd_decoder = {
160
157
    .name           = "txd",
161
158
    .type           = AVMEDIA_TYPE_VIDEO,
162
 
    .id             = CODEC_ID_TXD,
 
159
    .id             = AV_CODEC_ID_TXD,
163
160
    .priv_data_size = sizeof(TXDContext),
164
161
    .init           = txd_init,
165
162
    .close          = txd_end,
166
163
    .decode         = txd_decode_frame,
167
164
    .capabilities   = CODEC_CAP_DR1,
168
 
    .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
 
165
    .long_name      = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
169
166
};