qpeg.c 11 KB
Newer Older
1 2 3 4
/*
 * QPEG codec
 * Copyright (c) 2004 Konstantin Shishkov
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

22
/**
23
 * @file
24 25
 * QPEG codec.
 */
26

27
#include "avcodec.h"
28
#include "bytestream.h"
29
#include "internal.h"
30 31 32

typedef struct QpegContext{
    AVCodecContext *avctx;
33
    AVFrame *pic, *ref;
34
    uint32_t pal[256];
35
    GetByteContext buffer;
36 37
} QpegContext;

38 39
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
                              int stride, int width, int height)
40 41 42 43 44 45
{
    int i;
    int code;
    int c0, c1;
    int run, copy;
    int filled = 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
46
    int rows_to_go;
47

Kostya Shishkov's avatar
Kostya Shishkov committed
48
    rows_to_go = height;
49 50
    height--;
    dst = dst + height * stride;
51

52 53
    while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
        code = bytestream2_get_byte(&qctx->buffer);
54 55 56 57
        run = copy = 0;
        if(code == 0xFC) /* end-of-picture code */
            break;
        if(code >= 0xF8) { /* very long run */
58 59
            c0 = bytestream2_get_byte(&qctx->buffer);
            c1 = bytestream2_get_byte(&qctx->buffer);
60 61
            run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
        } else if (code >= 0xF0) { /* long run */
62
            c0 = bytestream2_get_byte(&qctx->buffer);
63 64 65 66
            run = ((code & 0xF) << 8) + c0 + 2;
        } else if (code >= 0xE0) { /* short run */
            run = (code & 0x1F) + 2;
        } else if (code >= 0xC0) { /* very long copy */
67 68
            c0 = bytestream2_get_byte(&qctx->buffer);
            c1 = bytestream2_get_byte(&qctx->buffer);
69 70
            copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
        } else if (code >= 0x80) { /* long copy */
71
            c0 = bytestream2_get_byte(&qctx->buffer);
72 73 74 75
            copy = ((code & 0x7F) << 8) + c0 + 1;
        } else { /* short copy */
            copy = code + 1;
        }
76

77 78 79
        /* perform actual run or copy */
        if(run) {
            int p;
80

81
            p = bytestream2_get_byte(&qctx->buffer);
82 83 84 85 86
            for(i = 0; i < run; i++) {
                dst[filled++] = p;
                if (filled >= width) {
                    filled = 0;
                    dst -= stride;
Kostya Shishkov's avatar
Kostya Shishkov committed
87 88 89
                    rows_to_go--;
                    if(rows_to_go <= 0)
                        break;
90 91 92 93
                }
            }
        } else {
            for(i = 0; i < copy; i++) {
94
                dst[filled++] = bytestream2_get_byte(&qctx->buffer);
95 96 97
                if (filled >= width) {
                    filled = 0;
                    dst -= stride;
Kostya Shishkov's avatar
Kostya Shishkov committed
98 99 100
                    rows_to_go--;
                    if(rows_to_go <= 0)
                        break;
101 102 103
                }
            }
        }
104 105 106
    }
}

107
static const int qpeg_table_h[16] =
108
 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
109
static const int qpeg_table_w[16] =
110
 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
111

112
/* Decodes delta frames */
113
static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
114 115 116
                              int stride, int width, int height,
                              int delta, const uint8_t *ctable,
                              uint8_t *refdata)
117 118 119 120
{
    int i, j;
    int code;
    int filled = 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
121
    int orig_height;
122

123 124 125 126 127 128 129
    if (refdata) {
        /* copy prev frame */
        for (i = 0; i < height; i++)
            memcpy(dst + (i * stride), refdata + (i * stride), width);
    } else {
        refdata = dst;
    }
130

Kostya Shishkov's avatar
Kostya Shishkov committed
131
    orig_height = height;
132 133 134
    height--;
    dst = dst + height * stride;

135 136
    while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
        code = bytestream2_get_byte(&qctx->buffer);
137

138 139
        if(delta) {
            /* motion compensation */
140
            while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
141 142 143 144 145
                if(delta == 1) {
                    int me_idx;
                    int me_w, me_h, me_x, me_y;
                    uint8_t *me_plane;
                    int corr, val;
146

147 148 149 150
                    /* get block size by index */
                    me_idx = code & 0xF;
                    me_w = qpeg_table_w[me_idx];
                    me_h = qpeg_table_h[me_idx];
151

152
                    /* extract motion vector */
153
                    corr = bytestream2_get_byte(&qctx->buffer);
154

155 156 157 158
                    val = corr >> 4;
                    if(val > 7)
                        val -= 16;
                    me_x = val;
159

160 161 162 163
                    val = corr & 0xF;
                    if(val > 7)
                        val -= 16;
                    me_y = val;
164

Kostya Shishkov's avatar
Kostya Shishkov committed
165 166
                    /* check motion vector */
                    if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
167
                       (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
Kostya Shishkov's avatar
Kostya Shishkov committed
168 169 170 171 172
                       (filled + me_w > width) || (height - me_h < 0))
                        av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
                               me_x, me_y, me_w, me_h, filled, height);
                    else {
                        /* do motion compensation */
173
                        me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
Kostya Shishkov's avatar
Kostya Shishkov committed
174 175
                        for(j = 0; j < me_h; j++) {
                            for(i = 0; i < me_w; i++)
176
                                dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
Kostya Shishkov's avatar
Kostya Shishkov committed
177
                        }
178 179
                    }
                }
180
                code = bytestream2_get_byte(&qctx->buffer);
181 182
            }
        }
183

184 185 186 187
        if(code == 0xE0) /* end-of-picture code */
            break;
        if(code > 0xE0) { /* run code: 0xE1..0xFF */
            int p;
188

189
            code &= 0x1F;
190
            p = bytestream2_get_byte(&qctx->buffer);
191 192 193 194 195 196
            for(i = 0; i <= code; i++) {
                dst[filled++] = p;
                if(filled >= width) {
                    filled = 0;
                    dst -= stride;
                    height--;
197
                    if (height < 0)
198
                        break;
199 200 201 202
                }
            }
        } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
            code &= 0x1F;
203

204
            if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
205 206
                break;

207
            for(i = 0; i <= code; i++) {
208
                dst[filled++] = bytestream2_get_byte(&qctx->buffer);
209 210 211 212
                if(filled >= width) {
                    filled = 0;
                    dst -= stride;
                    height--;
213
                    if (height < 0)
214
                        break;
215 216 217 218
                }
            }
        } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
            int skip;
219

220 221 222 223
            code &= 0x3F;
            /* codes 0x80 and 0x81 are actually escape codes,
               skip value minus constant is in the next byte */
            if(!code)
224
                skip = bytestream2_get_byte(&qctx->buffer) +  64;
225
            else if(code == 1)
226
                skip = bytestream2_get_byte(&qctx->buffer) + 320;
227 228 229 230 231 232 233
            else
                skip = code;
            filled += skip;
            while( filled >= width) {
                filled -= width;
                dst -= stride;
                height--;
Kostya Shishkov's avatar
Kostya Shishkov committed
234 235
                if(height < 0)
                    break;
236 237 238
            }
        } else {
            /* zero code treated as one-pixel skip */
239
            if(code) {
240
                dst[filled++] = ctable[code & 0x7F];
241
            }
242 243 244 245 246 247 248 249
            else
                filled++;
            if(filled >= width) {
                filled = 0;
                dst -= stride;
                height--;
            }
        }
250 251 252
    }
}

253
static int decode_frame(AVCodecContext *avctx,
254
                        void *data, int *got_frame,
255
                        AVPacket *avpkt)
256
{
257
    uint8_t ctable[128];
258
    QpegContext * const a = avctx->priv_data;
259
    AVFrame * const p = a->pic;
260
    AVFrame * const ref = a->ref;
261
    uint8_t* outdata;
262
    int delta, ret;
263
    const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
264

265 266 267 268 269 270
    if (avpkt->size < 0x86) {
        av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
        return AVERROR_INVALIDDATA;
    }

    bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
271

272 273
    av_frame_unref(ref);
    av_frame_move_ref(ref, p);
274

275
    if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
276
        return ret;
277
    outdata = p->data[0];
278 279 280 281 282 283
    bytestream2_skip(&a->buffer, 4);
    bytestream2_get_buffer(&a->buffer, ctable, 128);
    bytestream2_skip(&a->buffer, 1);

    delta = bytestream2_get_byte(&a->buffer);
    if(delta == 0x10) {
284
        qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
285
    } else {
286
        qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
287 288 289
    }

    /* make the palette available on the way out */
290
    if (pal) {
291
        p->palette_has_changed = 1;
292
        memcpy(a->pal, pal, AVPALETTE_SIZE);
293
    }
294
    memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
295

296
    if ((ret = av_frame_ref(data, p)) < 0)
297 298
        return ret;

299
    *got_frame      = 1;
300

301
    return avpkt->size;
302 303
}

304 305 306 307 308 309 310 311 312 313 314 315
static void decode_flush(AVCodecContext *avctx){
    QpegContext * const a = avctx->priv_data;
    int i, pal_size;
    const uint8_t *pal_src;

    pal_size = FFMIN(1024U, avctx->extradata_size);
    pal_src = avctx->extradata + avctx->extradata_size - pal_size;

    for (i=0; i<pal_size/4; i++)
        a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
}

316 317
static av_cold int decode_end(AVCodecContext *avctx)
{
318
    QpegContext * const a = avctx->priv_data;
319

320
    av_frame_free(&a->pic);
321
    av_frame_free(&a->ref);
322

323 324 325
    return 0;
}

326
static av_cold int decode_init(AVCodecContext *avctx){
327
    QpegContext * const a = avctx->priv_data;
328

329
    a->avctx = avctx;
330
    avctx->pix_fmt= AV_PIX_FMT_PAL8;
331

332 333
    decode_flush(avctx);

334
    a->pic = av_frame_alloc();
335 336
    a->ref = av_frame_alloc();
    if (!a->pic || !a->ref) {
337 338 339
        decode_end(avctx);
        return AVERROR(ENOMEM);
    }
340 341 342 343

    return 0;
}

344
AVCodec ff_qpeg_decoder = {
345
    .name           = "qpeg",
346
    .long_name      = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
347
    .type           = AVMEDIA_TYPE_VIDEO,
348
    .id             = AV_CODEC_ID_QPEG,
349 350 351 352
    .priv_data_size = sizeof(QpegContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
353
    .flush          = decode_flush,
354
    .capabilities   = CODEC_CAP_DR1,
355
};