escape130.c 10.4 KB
Newer Older
Eli Friedman's avatar
Eli Friedman committed
1
/*
Eli Friedman's avatar
Eli Friedman committed
2
 * Escape 130 video decoder
Eli Friedman's avatar
Eli Friedman committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2008 Eli Friedman (eli.friedman <at> gmail.com)
 *
 * 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
 */

Eli Friedman's avatar
Eli Friedman committed
22 23
#include "libavutil/attributes.h"
#include "libavutil/mem.h"
Eli Friedman's avatar
Eli Friedman committed
24
#include "avcodec.h"
25
#define BITSTREAM_READER_LE
26
#include "get_bits.h"
27 28
#include "internal.h"

Eli Friedman's avatar
Eli Friedman committed
29
typedef struct Escape130Context {
Eli Friedman's avatar
Eli Friedman committed
30 31 32 33 34 35 36 37
    uint8_t *old_y_avg;

    uint8_t *new_y, *old_y;
    uint8_t *new_u, *old_u;
    uint8_t *new_v, *old_v;

    uint8_t *buf1, *buf2;
    int     linesize[3];
Eli Friedman's avatar
Eli Friedman committed
38 39
} Escape130Context;

Eli Friedman's avatar
Eli Friedman committed
40 41 42 43 44 45 46 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static const uint8_t offset_table[] = { 2, 4, 10, 20 };
static const int8_t sign_table[64][4] = {
    {  0,  0,  0,  0 },
    { -1,  1,  0,  0 },
    {  1, -1,  0,  0 },
    { -1,  0,  1,  0 },
    { -1,  1,  1,  0 },
    {  0, -1,  1,  0 },
    {  1, -1,  1,  0 },
    { -1, -1,  1,  0 },
    {  1,  0, -1,  0 },
    {  0,  1, -1,  0 },
    {  1,  1, -1,  0 },
    { -1,  1, -1,  0 },
    {  1, -1, -1,  0 },
    { -1,  0,  0,  1 },
    { -1,  1,  0,  1 },
    {  0, -1,  0,  1 },

    {  0,  0,  0,  0 },
    {  1, -1,  0,  1 },
    { -1, -1,  0,  1 },
    { -1,  0,  1,  1 },
    { -1,  1,  1,  1 },
    {  0, -1,  1,  1 },
    {  1, -1,  1,  1 },
    { -1, -1,  1,  1 },
    {  0,  0, -1,  1 },
    {  1,  0, -1,  1 },
    { -1,  0, -1,  1 },
    {  0,  1, -1,  1 },
    {  1,  1, -1,  1 },
    { -1,  1, -1,  1 },
    {  0, -1, -1,  1 },
    {  1, -1, -1,  1 },

    {  0,  0,  0,  0 },
    { -1, -1, -1,  1 },
    {  1,  0,  0, -1 },
    {  0,  1,  0, -1 },
    {  1,  1,  0, -1 },
    { -1,  1,  0, -1 },
    {  1, -1,  0, -1 },
    {  0,  0,  1, -1 },
    {  1,  0,  1, -1 },
    { -1,  0,  1, -1 },
    {  0,  1,  1, -1 },
    {  1,  1,  1, -1 },
    { -1,  1,  1, -1 },
    {  0, -1,  1, -1 },
    {  1, -1,  1, -1 },
    { -1, -1,  1, -1 },

    {  0,  0,  0,  0 },
    {  1,  0, -1, -1 },
    {  0,  1, -1, -1 },
    {  1,  1, -1, -1 },
    { -1,  1, -1, -1 },
    {  1, -1, -1, -1 }
};

static const int8_t luma_adjust[] = { -4, -3, -2, -1, 1, 2, 3, 4 };

static const int8_t chroma_adjust[2][8] = {
    { 1, 1, 0, -1, -1, -1,  0,  1 },
    { 0, 1, 1,  1,  0, -1, -1, -1 }
};

108
static const uint8_t chroma_vals[] = {
Eli Friedman's avatar
Eli Friedman committed
109 110 111 112 113 114
     20,  28,  36,  44,  52,  60,  68,  76,
     84,  92, 100, 106, 112, 116, 120, 124,
    128, 132, 136, 140, 144, 150, 156, 164,
    172, 180, 188, 196, 204, 212, 220, 228
};

Eli Friedman's avatar
Eli Friedman committed
115 116
static av_cold int escape130_decode_init(AVCodecContext *avctx)
{
117
    Escape130Context *s = avctx->priv_data;
118
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
Eli Friedman's avatar
Eli Friedman committed
119

Eli Friedman's avatar
Eli Friedman committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    if ((avctx->width & 1) || (avctx->height & 1)) {
        av_log(avctx, AV_LOG_ERROR,
               "Dimensions should be a multiple of two.\n");
        return AVERROR_INVALIDDATA;
    }

    s->old_y_avg = av_malloc(avctx->width * avctx->height / 4);
    s->buf1      = av_malloc(avctx->width * avctx->height * 3 / 2);
    s->buf2      = av_malloc(avctx->width * avctx->height * 3 / 2);
    if (!s->old_y_avg || !s->buf1 || !s->buf2) {
        av_freep(&s->old_y_avg);
        av_freep(&s->buf1);
        av_freep(&s->buf2);
        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
        return AVERROR(ENOMEM);
135 136
    }

Eli Friedman's avatar
Eli Friedman committed
137 138 139 140 141 142 143 144 145 146 147 148 149
    s->linesize[0] = avctx->width;
    s->linesize[1] =
    s->linesize[2] = avctx->width / 2;

    s->new_y = s->buf1;
    s->new_u = s->new_y + avctx->width * avctx->height;
    s->new_v = s->new_u + avctx->width * avctx->height / 4;
    s->old_y = s->buf2;
    s->old_u = s->old_y + avctx->width * avctx->height;
    s->old_v = s->old_u + avctx->width * avctx->height / 4;
    memset(s->old_y, 0,    avctx->width * avctx->height);
    memset(s->old_u, 0x10, avctx->width * avctx->height / 4);
    memset(s->old_v, 0x10, avctx->width * avctx->height / 4);
150

Eli Friedman's avatar
Eli Friedman committed
151 152 153 154 155 156 157
    return 0;
}

static av_cold int escape130_decode_close(AVCodecContext *avctx)
{
    Escape130Context *s = avctx->priv_data;

Eli Friedman's avatar
Eli Friedman committed
158 159 160
    av_freep(&s->old_y_avg);
    av_freep(&s->buf1);
    av_freep(&s->buf2);
161

Eli Friedman's avatar
Eli Friedman committed
162 163 164
    return 0;
}

Eli Friedman's avatar
Eli Friedman committed
165 166 167 168
static int decode_skip_count(GetBitContext* gb)
{
    int value;

169
    if (get_bits_left(gb) < 1+3)
Eli Friedman's avatar
Eli Friedman committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        return -1;

    value = get_bits1(gb);
    if (value)
        return 0;

    value = get_bits(gb, 3);
    if (value)
        return value;

    value = get_bits(gb, 8);
    if (value)
        return value + 7;

    value = get_bits(gb, 15);
    if (value)
        return value + 262;

    return -1;
}

Eli Friedman's avatar
Eli Friedman committed
191 192
static int escape130_decode_frame(AVCodecContext *avctx, void *data,
                                  int *got_frame, AVPacket *avpkt)
Eli Friedman's avatar
Eli Friedman committed
193
{
Eli Friedman's avatar
Eli Friedman committed
194
    int buf_size        = avpkt->size;
Eli Friedman's avatar
Eli Friedman committed
195
    Escape130Context *s = avctx->priv_data;
Eli Friedman's avatar
Eli Friedman committed
196
    AVFrame *pic        = data;
Eli Friedman's avatar
Eli Friedman committed
197
    GetBitContext gb;
198
    int ret;
Eli Friedman's avatar
Eli Friedman committed
199

200
    uint8_t *old_y, *old_cb, *old_cr,
Eli Friedman's avatar
Eli Friedman committed
201
            *new_y, *new_cb, *new_cr;
Eli Friedman's avatar
Eli Friedman committed
202
    uint8_t *dstY, *dstU, *dstV;
Eli Friedman's avatar
Eli Friedman committed
203 204 205
    unsigned old_y_stride, old_cb_stride, old_cr_stride,
             new_y_stride, new_cb_stride, new_cr_stride;
    unsigned total_blocks = avctx->width * avctx->height / 4,
Eli Friedman's avatar
Eli Friedman committed
206 207 208 209 210 211 212 213 214 215
             block_index, block_x = 0;
    unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
    int skip = -1, y_avg = 0, i, j;
    uint8_t *ya = s->old_y_avg;

    // first 16 bytes are header; no useful information in here
    if (buf_size <= 16) {
        av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
        return AVERROR_INVALIDDATA;
    }
Eli Friedman's avatar
Eli Friedman committed
216

Eli Friedman's avatar
Eli Friedman committed
217
    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
218
        return ret;
219

220 221 222
    if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
        return ret;
    skip_bits_long(&gb, 16 * 8);
Eli Friedman's avatar
Eli Friedman committed
223 224 225 226 227 228 229 230 231 232 233 234 235

    new_y  = s->new_y;
    new_cb = s->new_u;
    new_cr = s->new_v;
    new_y_stride  = s->linesize[0];
    new_cb_stride = s->linesize[1];
    new_cr_stride = s->linesize[2];
    old_y  = s->old_y;
    old_cb = s->old_u;
    old_cr = s->old_v;
    old_y_stride  = s->linesize[0];
    old_cb_stride = s->linesize[1];
    old_cr_stride = s->linesize[2];
Eli Friedman's avatar
Eli Friedman committed
236 237 238

    for (block_index = 0; block_index < total_blocks; block_index++) {
        // Note that this call will make us skip the rest of the blocks
Eli Friedman's avatar
Eli Friedman committed
239
        // if the frame ends prematurely.
Eli Friedman's avatar
Eli Friedman committed
240 241
        if (skip == -1)
            skip = decode_skip_count(&gb);
Eli Friedman's avatar
Eli Friedman committed
242 243 244 245
        if (skip == -1) {
            av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n");
            return AVERROR_INVALIDDATA;
        }
Eli Friedman's avatar
Eli Friedman committed
246 247

        if (skip) {
Eli Friedman's avatar
Eli Friedman committed
248 249 250 251 252 253 254
            y[0] = old_y[0];
            y[1] = old_y[1];
            y[2] = old_y[old_y_stride];
            y[3] = old_y[old_y_stride + 1];
            y_avg = ya[0];
            cb = old_cb[0];
            cr = old_cr[0];
Eli Friedman's avatar
Eli Friedman committed
255 256
        } else {
            if (get_bits1(&gb)) {
Eli Friedman's avatar
Eli Friedman committed
257
                unsigned sign_selector       = get_bits(&gb, 6);
258
                unsigned difference_selector = get_bits(&gb, 2);
Eli Friedman's avatar
Eli Friedman committed
259
                y_avg = 2 * get_bits(&gb, 5);
Eli Friedman's avatar
Eli Friedman committed
260
                for (i = 0; i < 4; i++) {
Eli Friedman's avatar
Eli Friedman committed
261 262
                    y[i] = av_clip(y_avg + offset_table[difference_selector] *
                                   sign_table[sign_selector][i], 0, 63);
Eli Friedman's avatar
Eli Friedman committed
263 264 265
                }
            } else if (get_bits1(&gb)) {
                if (get_bits1(&gb)) {
Eli Friedman's avatar
Eli Friedman committed
266
                    y_avg = get_bits(&gb, 6);
Eli Friedman's avatar
Eli Friedman committed
267 268
                } else {
                    unsigned adjust_index = get_bits(&gb, 3);
Eli Friedman's avatar
Eli Friedman committed
269
                    y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
Eli Friedman's avatar
Eli Friedman committed
270
                }
271
                for (i = 0; i < 4; i++)
Eli Friedman's avatar
Eli Friedman committed
272
                    y[i] = y_avg;
Eli Friedman's avatar
Eli Friedman committed
273 274 275 276 277 278 279 280
            }

            if (get_bits1(&gb)) {
                if (get_bits1(&gb)) {
                    cb = get_bits(&gb, 5);
                    cr = get_bits(&gb, 5);
                } else {
                    unsigned adjust_index = get_bits(&gb, 3);
Eli Friedman's avatar
Eli Friedman committed
281 282
                    cb = (cb + chroma_adjust[0][adjust_index]) & 31;
                    cr = (cr + chroma_adjust[1][adjust_index]) & 31;
Eli Friedman's avatar
Eli Friedman committed
283 284 285
                }
            }
        }
Eli Friedman's avatar
Eli Friedman committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        *ya++ = y_avg;

        new_y[0]                = y[0];
        new_y[1]                = y[1];
        new_y[new_y_stride]     = y[2];
        new_y[new_y_stride + 1] = y[3];
        *new_cb = cb;
        *new_cr = cr;

        old_y += 2;
        old_cb++;
        old_cr++;
        new_y += 2;
        new_cb++;
        new_cr++;
        block_x++;
        if (block_x * 2 == avctx->width) {
            block_x = 0;
            old_y  += old_y_stride * 2  - avctx->width;
            old_cb += old_cb_stride     - avctx->width / 2;
            old_cr += old_cr_stride     - avctx->width / 2;
Eli Friedman's avatar
Eli Friedman committed
307
            new_y  += new_y_stride * 2  - avctx->width;
Eli Friedman's avatar
Eli Friedman committed
308 309
            new_cb += new_cb_stride     - avctx->width / 2;
            new_cr += new_cr_stride     - avctx->width / 2;
Eli Friedman's avatar
Eli Friedman committed
310 311 312 313 314
        }

        skip--;
    }

Eli Friedman's avatar
Eli Friedman committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    new_y  = s->new_y;
    new_cb = s->new_u;
    new_cr = s->new_v;
    dstY   = pic->data[0];
    dstU   = pic->data[1];
    dstV   = pic->data[2];
    for (j = 0; j < avctx->height; j++) {
        for (i = 0; i < avctx->width; i++)
            dstY[i] = new_y[i] << 2;
        dstY  += pic->linesize[0];
        new_y += new_y_stride;
    }
    for (j = 0; j < avctx->height / 2; j++) {
        for (i = 0; i < avctx->width / 2; i++) {
            dstU[i] = chroma_vals[new_cb[i]];
            dstV[i] = chroma_vals[new_cr[i]];
        }
        dstU   += pic->linesize[1];
        dstV   += pic->linesize[2];
        new_cb += new_cb_stride;
        new_cr += new_cr_stride;
    }

    av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
            buf_size, get_bits_count(&gb) >> 3);
Eli Friedman's avatar
Eli Friedman committed
340

Eli Friedman's avatar
Eli Friedman committed
341 342 343
    FFSWAP(uint8_t*, s->old_y, s->new_y);
    FFSWAP(uint8_t*, s->old_u, s->new_u);
    FFSWAP(uint8_t*, s->old_v, s->new_v);
Eli Friedman's avatar
Eli Friedman committed
344

345
    *got_frame = 1;
Eli Friedman's avatar
Eli Friedman committed
346 347 348 349

    return buf_size;
}

350 351
AVCodec ff_escape130_decoder = {
    .name           = "escape130",
352
    .long_name      = NULL_IF_CONFIG_SMALL("Escape 130"),
353
    .type           = AVMEDIA_TYPE_VIDEO,
354
    .id             = AV_CODEC_ID_ESCAPE130,
355 356 357 358 359
    .priv_data_size = sizeof(Escape130Context),
    .init           = escape130_decode_init,
    .close          = escape130_decode_close,
    .decode         = escape130_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
Eli Friedman's avatar
Eli Friedman committed
360
};