GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/clearvideo.c Lines: 0 486 0.0 %
Date: 2019-11-22 03:34:36 Branches: 0 292 0.0 %

Line Branch Exec Source
1
/*
2
 * ClearVideo decoder
3
 * Copyright (c) 2012-2018 Konstantin Shishkov
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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
 * ClearVideo decoder
25
 */
26
27
#include "avcodec.h"
28
#include "bytestream.h"
29
#include "get_bits.h"
30
#include "idctdsp.h"
31
#include "internal.h"
32
#include "mathops.h"
33
#include "clearvideodata.h"
34
35
typedef struct LevelCodes {
36
    uint16_t    mv_esc;
37
    uint16_t    bias_esc;
38
    VLC         flags_cb;
39
    VLC         mv_cb;
40
    VLC         bias_cb;
41
} LevelCodes;
42
43
typedef struct MV {
44
    int16_t x, y;
45
} MV;
46
47
static const MV zero_mv = { 0 };
48
49
typedef struct MVInfo {
50
    int mb_w;
51
    int mb_h;
52
    int mb_size;
53
    int mb_stride;
54
    int top;
55
    MV  *mv;
56
} MVInfo;
57
58
typedef struct TileInfo {
59
    uint16_t        flags;
60
    int16_t         bias;
61
    MV              mv;
62
    struct TileInfo *child[4];
63
} TileInfo;
64
65
typedef struct CLVContext {
66
    AVCodecContext *avctx;
67
    IDCTDSPContext idsp;
68
    AVFrame        *pic;
69
    AVFrame        *prev;
70
    GetBitContext  gb;
71
    int            mb_width, mb_height;
72
    int            pmb_width, pmb_height;
73
    MVInfo         mvi;
74
    int            tile_size;
75
    int            tile_shift;
76
    VLC            dc_vlc, ac_vlc;
77
    LevelCodes     ylev[4], ulev[3], vlev[3];
78
    int            luma_dc_quant, chroma_dc_quant, ac_quant;
79
    DECLARE_ALIGNED(16, int16_t, block)[64];
80
    int            top_dc[3], left_dc[4];
81
} CLVContext;
82
83
static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
84
                               int ac_quant)
85
{
86
    GetBitContext *gb = &ctx->gb;
87
    int idx = 1, last = 0, val, skip;
88
89
    memset(blk, 0, sizeof(*blk) * 64);
90
    blk[0] = get_vlc2(gb, ctx->dc_vlc.table, 9, 3);
91
    if (blk[0] < 0)
92
        return AVERROR_INVALIDDATA;
93
    blk[0] -= 63;
94
95
    if (!has_ac)
96
        return 0;
97
98
    while (idx < 64 && !last) {
99
        val = get_vlc2(gb, ctx->ac_vlc.table, 9, 2);
100
        if (val < 0)
101
            return AVERROR_INVALIDDATA;
102
        if (val != 0x1BFF) {
103
            last =  val >> 12;
104
            skip = (val >> 4) & 0xFF;
105
            val &= 0xF;
106
            if (get_bits1(gb))
107
                val = -val;
108
        } else {
109
            last = get_bits1(gb);
110
            skip = get_bits(gb, 6);
111
            val  = get_sbits(gb, 8);
112
        }
113
        if (val) {
114
            int aval = FFABS(val), sign = val < 0;
115
            val = ac_quant * (2 * aval + 1);
116
            if (!(ac_quant & 1))
117
                val--;
118
            if (sign)
119
                val = -val;
120
        }
121
        idx += skip;
122
        if (idx >= 64)
123
            return AVERROR_INVALIDDATA;
124
        blk[ff_zigzag_direct[idx++]] = val;
125
    }
126
127
    return (idx <= 64 && last) ? 0 : -1;
128
}
129
130
#define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP)                \
131
    const int t0 = OP(2841 * blk[1 * step] +  565 * blk[7 * step]);     \
132
    const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]);     \
133
    const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]);     \
134
    const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]);     \
135
    const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]);     \
136
    const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]);     \
137
    const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias;  \
138
    const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias;  \
139
    const int t8 = t0 + t2;                                             \
140
    const int t9 = t0 - t2;                                             \
141
    const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8;          \
142
    const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8;          \
143
    const int tC = t1 + t3;                                             \
144
                                                                        \
145
    blk[0 * step] = (t6 + t5 + t8) >> shift;                            \
146
    blk[1 * step] = (t7 + t4 + tA) >> shift;                            \
147
    blk[2 * step] = (t7 - t4 + tB) >> shift;                            \
148
    blk[3 * step] = (t6 - t5 + tC) >> shift;                            \
149
    blk[4 * step] = (t6 - t5 - tC) >> shift;                            \
150
    blk[5 * step] = (t7 - t4 - tB) >> shift;                            \
151
    blk[6 * step] = (t7 + t4 - tA) >> shift;                            \
152
    blk[7 * step] = (t6 + t5 - t8) >> shift;                            \
153
154
#define ROP(x) x
155
#define COP(x) (((x) + 4) >> 3)
156
157
static void clv_dct(int16_t *block)
158
{
159
    int i;
160
    int16_t *ptr;
161
162
    ptr = block;
163
    for (i = 0; i < 8; i++) {
164
        DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
165
        ptr += 8;
166
    }
167
168
    ptr = block;
169
    for (i = 0; i < 8; i++) {
170
        DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
171
        ptr++;
172
    }
173
}
174
175
static int decode_mb(CLVContext *c, int x, int y)
176
{
177
    int i, has_ac[6], off;
178
179
    for (i = 0; i < 6; i++)
180
        has_ac[i] = get_bits1(&c->gb);
181
182
    off = x * 16 + y * 16 * c->pic->linesize[0];
183
    for (i = 0; i < 4; i++) {
184
        if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
185
            return AVERROR_INVALIDDATA;
186
        if (!x && !(i & 1)) {
187
            c->block[0] += c->top_dc[0];
188
            c->top_dc[0] = c->block[0];
189
        } else {
190
            c->block[0] += c->left_dc[(i & 2) >> 1];
191
        }
192
        c->left_dc[(i & 2) >> 1] = c->block[0];
193
        c->block[0]             *= c->luma_dc_quant;
194
        clv_dct(c->block);
195
        if (i == 2)
196
            off += c->pic->linesize[0] * 8;
197
        c->idsp.put_pixels_clamped(c->block,
198
                                   c->pic->data[0] + off + (i & 1) * 8,
199
                                   c->pic->linesize[0]);
200
    }
201
202
    off = x * 8 + y * 8 * c->pic->linesize[1];
203
    for (i = 1; i < 3; i++) {
204
        if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
205
            return AVERROR_INVALIDDATA;
206
        if (!x) {
207
            c->block[0] += c->top_dc[i];
208
            c->top_dc[i] = c->block[0];
209
        } else {
210
            c->block[0] += c->left_dc[i + 1];
211
        }
212
        c->left_dc[i + 1] = c->block[0];
213
        c->block[0]      *= c->chroma_dc_quant;
214
        clv_dct(c->block);
215
        c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
216
                                   c->pic->linesize[i]);
217
    }
218
219
    return 0;
220
}
221
222
static int copy_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
223
                      int plane, int x, int y, int dx, int dy, int size)
224
{
225
    int shift = plane > 0;
226
    int sx = x + dx;
227
    int sy = y + dy;
228
    int sstride, dstride, soff, doff;
229
    uint8_t *sbuf, *dbuf;
230
    int i;
231
232
    if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
233
        x + size > avctx->coded_width >> shift ||
234
        y + size > avctx->coded_height >> shift ||
235
        sx + size > avctx->coded_width >> shift ||
236
        sy + size > avctx->coded_height >> shift)
237
        return AVERROR_INVALIDDATA;
238
239
    sstride = src->linesize[plane];
240
    dstride = dst->linesize[plane];
241
    soff    = sx + sy * sstride;
242
    sbuf    = src->data[plane];
243
    doff    = x + y * dstride;
244
    dbuf    = dst->data[plane];
245
246
    for (i = 0; i < size; i++) {
247
        uint8_t *dptr = &dbuf[doff];
248
        uint8_t *sptr = &sbuf[soff];
249
250
        memcpy(dptr, sptr, size);
251
        doff += dstride;
252
        soff += sstride;
253
    }
254
255
    return 0;
256
}
257
258
static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
259
                         int plane, int x, int y, int dx, int dy, int size, int bias)
260
{
261
    int shift = plane > 0;
262
    int sx = x + dx;
263
    int sy = y + dy;
264
    int sstride   = src->linesize[plane];
265
    int dstride   = dst->linesize[plane];
266
    int soff      = sx + sy * sstride;
267
    uint8_t *sbuf = src->data[plane];
268
    int doff      = x + y * dstride;
269
    uint8_t *dbuf = dst->data[plane];
270
    int i, j;
271
272
    if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
273
        x + size > avctx->coded_width >> shift ||
274
        y + size > avctx->coded_height >> shift ||
275
        sx + size > avctx->coded_width >> shift ||
276
        sy + size > avctx->coded_height >> shift)
277
        return AVERROR_INVALIDDATA;
278
279
    for (j = 0; j < size; j++) {
280
        uint8_t *dptr = &dbuf[doff];
281
        uint8_t *sptr = &sbuf[soff];
282
283
        for (i = 0; i < size; i++) {
284
            int val = sptr[i] + bias;
285
286
            dptr[i] = av_clip_uint8(val);
287
        }
288
289
        doff += dstride;
290
        soff += sstride;
291
    }
292
293
    return 0;
294
}
295
296
static MV mvi_predict(MVInfo *mvi, int mb_x, int mb_y, MV diff)
297
{
298
    MV res, pred_mv;
299
    int left_mv, right_mv, top_mv, bot_mv;
300
301
    if (mvi->top) {
302
        if (mb_x > 0) {
303
            pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1];
304
        } else {
305
            pred_mv = zero_mv;
306
        }
307
    } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) {
308
        pred_mv = mvi->mv[mb_x];
309
    } else {
310
        MV A = mvi->mv[mvi->mb_stride + mb_x - 1];
311
        MV B = mvi->mv[                 mb_x    ];
312
        MV C = mvi->mv[                 mb_x + 1];
313
        pred_mv.x = mid_pred(A.x, B.x, C.x);
314
        pred_mv.y = mid_pred(A.y, B.y, C.y);
315
    }
316
317
    res = pred_mv;
318
319
    left_mv = -((mb_x * mvi->mb_size));
320
    right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size);
321
    if (res.x < left_mv) {
322
        res.x = left_mv;
323
    }
324
    if (res.x > right_mv) {
325
        res.x = right_mv;
326
    }
327
    top_mv = -((mb_y * mvi->mb_size));
328
    bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size);
329
    if (res.y < top_mv) {
330
        res.y = top_mv;
331
    }
332
    if (res.y > bot_mv) {
333
        res.y = bot_mv;
334
    }
335
336
    mvi->mv[mvi->mb_stride + mb_x].x = res.x + diff.x;
337
    mvi->mv[mvi->mb_stride + mb_x].y = res.y + diff.y;
338
339
    return res;
340
}
341
342
static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
343
{
344
    mvi->top       = 1;
345
    mvi->mb_w      = mb_w;
346
    mvi->mb_h      = mb_h;
347
    mvi->mb_size   = mb_size;
348
    mvi->mb_stride = mb_w;
349
    memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2);
350
}
351
352
static void mvi_update_row(MVInfo *mvi)
353
{
354
    int i;
355
356
    mvi->top = 0;
357
    for (i = 0 ; i < mvi->mb_stride; i++) {
358
        mvi->mv[i] = mvi->mv[mvi->mb_stride + i];
359
    }
360
}
361
362
static TileInfo* decode_tile_info(GetBitContext *gb, LevelCodes *lc, int level)
363
{
364
    TileInfo *ti;
365
    int i, flags = 0;
366
    int16_t bias = 0;
367
    MV mv = { 0 };
368
369
    if (lc[level].flags_cb.table) {
370
        flags = get_vlc2(gb, lc[level].flags_cb.table, lc[level].flags_cb.bits, 2);
371
    }
372
373
    if (lc[level].mv_cb.table) {
374
        uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, lc[level].mv_cb.bits, 3);
375
376
        if (mv_code != lc[level].mv_esc) {
377
            mv.x = (int8_t)(mv_code & 0xff);
378
            mv.y = (int8_t)(mv_code >> 8);
379
        } else {
380
            mv.x = get_sbits(gb, 8);
381
            mv.y = get_sbits(gb, 8);
382
        }
383
    }
384
385
    if (lc[level].bias_cb.table) {
386
        uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, lc[level].bias_cb.bits, 2);
387
388
        if (bias_val != lc[level].bias_esc) {
389
            bias = (int16_t)(bias_val);
390
        } else {
391
            bias = get_sbits(gb, 16);
392
        }
393
    }
394
395
    ti = av_calloc(1, sizeof(*ti));
396
    if (!ti)
397
        return NULL;
398
399
    ti->flags = flags;
400
    ti->mv = mv;
401
    ti->bias = bias;
402
403
    if (ti->flags) {
404
        for (i = 0; i < 4; i++) {
405
            if (ti->flags & (1 << i)) {
406
                TileInfo *subti = decode_tile_info(gb, lc, level + 1);
407
                ti->child[i] = subti;
408
            }
409
        }
410
    }
411
412
    return ti;
413
}
414
415
static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
416
                         int plane, int x, int y, int dx, int dy, int size, int bias)
417
{
418
    int ret;
419
420
    if (!bias) {
421
        ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size);
422
    } else {
423
        ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias);
424
    }
425
426
    return ret;
427
}
428
429
static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
430
                        int plane, int x, int y, int size,
431
                        TileInfo *tile, MV root_mv)
432
{
433
    int ret;
434
    MV mv;
435
436
    mv.x = root_mv.x + tile->mv.x;
437
    mv.y = root_mv.y + tile->mv.y;
438
439
    if (!tile->flags) {
440
        ret = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, tile->bias);
441
    } else {
442
        int i, hsize = size >> 1;
443
444
        for (i = 0; i < 4; i++) {
445
            int xoff = (i & 2) == 0 ? 0 : hsize;
446
            int yoff = (i & 1) == 0 ? 0 : hsize;
447
448
            if (tile->child[i]) {
449
                ret = restore_tree(avctx, dst, src, plane, x + xoff, y + yoff, hsize, tile->child[i], root_mv);
450
                av_freep(&tile->child[i]);
451
            } else {
452
                ret = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, mv.x, mv.y, hsize, tile->bias);
453
            }
454
        }
455
    }
456
457
    return ret;
458
}
459
460
static void extend_edges(AVFrame *buf, int tile_size)
461
{
462
    int comp, i, j;
463
464
    for (comp = 0; comp < 3; comp++) {
465
        int shift = comp > 0;
466
        int w = buf->width  >> shift;
467
        int h = buf->height >> shift;
468
        int size = comp == 0 ? tile_size : tile_size >> 1;
469
        int stride = buf->linesize[comp];
470
        uint8_t *framebuf = buf->data[comp];
471
472
        int right  = size - (w & (size - 1));
473
        int bottom = size - (h & (size - 1));
474
475
        if ((right == size) && (bottom == size)) {
476
            return;
477
        }
478
        if (right != size) {
479
            int off = w;
480
            for (j = 0; j < h; j++) {
481
                for (i = 0; i < right; i++) {
482
                    framebuf[off + i] = 0x80;
483
                }
484
                off += stride;
485
            }
486
        }
487
        if (bottom != size) {
488
            int off = h * stride;
489
            for (j = 0; j < bottom; j++) {
490
                for (i = 0; i < stride; i++) {
491
                    framebuf[off + i] = 0x80;
492
                }
493
                off += stride;
494
            }
495
        }
496
    }
497
}
498
499
static int clv_decode_frame(AVCodecContext *avctx, void *data,
500
                            int *got_frame, AVPacket *avpkt)
501
{
502
    const uint8_t *buf = avpkt->data;
503
    int buf_size = avpkt->size;
504
    CLVContext *c = avctx->priv_data;
505
    GetByteContext gb;
506
    uint32_t frame_type;
507
    int i, j, ret;
508
    int mb_ret = 0;
509
510
    bytestream2_init(&gb, buf, buf_size);
511
    if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
512
        int skip = bytestream2_get_byte(&gb);
513
        bytestream2_skip(&gb, (skip + 1) * 8);
514
    }
515
516
    frame_type = bytestream2_get_byte(&gb);
517
518
    if ((frame_type & 0x7f) == 0x30) {
519
        *got_frame = 0;
520
        return buf_size;
521
    } else if (frame_type & 0x2) {
522
        if (buf_size < c->mb_width * c->mb_height) {
523
            av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
524
            return AVERROR_INVALIDDATA;
525
        }
526
527
        if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
528
            return ret;
529
530
        c->pic->key_frame = 1;
531
        c->pic->pict_type = AV_PICTURE_TYPE_I;
532
533
        bytestream2_get_be32(&gb); // frame size;
534
        c->ac_quant        = bytestream2_get_byte(&gb);
535
        c->luma_dc_quant   = 32;
536
        c->chroma_dc_quant = 32;
537
538
        if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
539
                                  buf_size - bytestream2_tell(&gb))) < 0)
540
            return ret;
541
542
        for (i = 0; i < 3; i++)
543
            c->top_dc[i] = 32;
544
        for (i = 0; i < 4; i++)
545
            c->left_dc[i] = 32;
546
547
        for (j = 0; j < c->mb_height; j++) {
548
            for (i = 0; i < c->mb_width; i++) {
549
                ret = decode_mb(c, i, j);
550
                if (ret < 0)
551
                    mb_ret = ret;
552
            }
553
        }
554
        extend_edges(c->pic, c->tile_size);
555
    } else {
556
        int plane;
557
558
        if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
559
            return AVERROR_INVALIDDATA;
560
561
        if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
562
            return ret;
563
564
        ret = av_frame_copy(c->pic, c->prev);
565
        if (ret < 0)
566
            return ret;
567
568
        if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
569
                                  buf_size - bytestream2_tell(&gb))) < 0)
570
            return ret;
571
572
        mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift);
573
574
        for (j = 0; j < c->pmb_height; j++) {
575
            for (i = 0; i < c->pmb_width; i++) {
576
                if (get_bits_left(&c->gb) <= 0)
577
                    return AVERROR_INVALIDDATA;
578
                if (get_bits1(&c->gb)) {
579
                    MV mv = mvi_predict(&c->mvi, i, j, zero_mv);
580
581
                    for (plane = 0; plane < 3; plane++) {
582
                        int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
583
                        int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
584
                        int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
585
                        int16_t mx = plane == 0 ? mv.x : mv.x / 2;
586
                        int16_t my = plane == 0 ? mv.y : mv.y / 2;
587
588
                        ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
589
                        if (ret < 0)
590
                            mb_ret = ret;
591
                    }
592
                } else {
593
                    int x = i << c->tile_shift;
594
                    int y = j << c->tile_shift;
595
                    int size = 1 << c->tile_shift;
596
                    TileInfo *tile;
597
                    MV mv, cmv;
598
599
                    tile = decode_tile_info(&c->gb, c->ylev, 0);
600
                    if (!tile)
601
                        return AVERROR(ENOMEM);
602
                    mv = mvi_predict(&c->mvi, i, j, tile->mv);
603
                    ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv);
604
                    if (ret < 0)
605
                        mb_ret = ret;
606
                    x = i << (c->tile_shift - 1);
607
                    y = j << (c->tile_shift - 1);
608
                    size = 1 << (c->tile_shift - 1);
609
                    cmv.x = mv.x + tile->mv.x;
610
                    cmv.y = mv.y + tile->mv.y;
611
                    cmv.x /= 2;
612
                    cmv.y /= 2;
613
                    av_freep(&tile);
614
                    tile = decode_tile_info(&c->gb, c->ulev, 0);
615
                    if (!tile)
616
                        return AVERROR(ENOMEM);
617
                    ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv);
618
                    if (ret < 0)
619
                        mb_ret = ret;
620
                    av_freep(&tile);
621
                    tile = decode_tile_info(&c->gb, c->vlev, 0);
622
                    if (!tile)
623
                        return AVERROR(ENOMEM);
624
                    ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv);
625
                    if (ret < 0)
626
                        mb_ret = ret;
627
                    av_freep(&tile);
628
                }
629
            }
630
            mvi_update_row(&c->mvi);
631
        }
632
        extend_edges(c->pic, c->tile_size);
633
634
        c->pic->key_frame = 0;
635
        c->pic->pict_type = AV_PICTURE_TYPE_P;
636
    }
637
638
    if ((ret = av_frame_ref(data, c->pic)) < 0)
639
        return ret;
640
641
    FFSWAP(AVFrame *, c->pic, c->prev);
642
643
    *got_frame = 1;
644
645
    if (get_bits_left(&c->gb) < 0)
646
        av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb));
647
648
    return mb_ret < 0 ? mb_ret : buf_size;
649
}
650
651
static av_cold int clv_decode_init(AVCodecContext *avctx)
652
{
653
    CLVContext *const c = avctx->priv_data;
654
    int ret, w, h;
655
656
    if (avctx->extradata_size == 110) {
657
        c->tile_size = AV_RL32(&avctx->extradata[94]);
658
    } else if (avctx->extradata_size == 150) {
659
        c->tile_size = AV_RB32(&avctx->extradata[134]);
660
    } else if (!avctx->extradata_size) {
661
        c->tile_size = 16;
662
    } else {
663
        av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
664
        return AVERROR_INVALIDDATA;
665
    }
666
667
    c->tile_shift = av_log2(c->tile_size);
668
    if (1U << c->tile_shift != c->tile_size) {
669
        av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2.\n", c->tile_size);
670
        return AVERROR_INVALIDDATA;
671
    }
672
673
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
674
    w = avctx->width;
675
    h = avctx->height;
676
    ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
677
    if (ret < 0)
678
        return ret;
679
    avctx->width  = w;
680
    avctx->height = h;
681
682
    c->avctx           = avctx;
683
    c->mb_width        = FFALIGN(avctx->width,  16) >> 4;
684
    c->mb_height       = FFALIGN(avctx->height, 16) >> 4;
685
    c->pmb_width       = (w + c->tile_size - 1) >> c->tile_shift;
686
    c->pmb_height      = (h + c->tile_size - 1) >> c->tile_shift;
687
    c->pic             = av_frame_alloc();
688
    c->prev            = av_frame_alloc();
689
    c->mvi.mv          = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
690
    if (!c->pic || !c->prev || !c->mvi.mv)
691
        return AVERROR(ENOMEM);
692
693
    ff_idctdsp_init(&c->idsp, avctx);
694
    ret = init_vlc(&c->dc_vlc, 9, NUM_DC_CODES,
695
                   clv_dc_bits,  1, 1,
696
                   clv_dc_codes, 1, 1, 0);
697
    if (ret) {
698
        av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
699
        return ret;
700
    }
701
    ret = ff_init_vlc_sparse(&c->ac_vlc, 9, NUM_AC_CODES,
702
                             clv_ac_bits,  1, 1,
703
                             clv_ac_codes, 1, 1,
704
                             clv_ac_syms,  2, 2, 0);
705
    if (ret) {
706
        av_log(avctx, AV_LOG_ERROR, "Error initialising AC VLC\n");
707
        return ret;
708
    }
709
710
    ret = init_vlc(&c->ylev[0].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsy_0_bits),
711
                   clv_flagsy_0_bits,  1, 1,
712
                   clv_flagsy_0_codes, 2, 2, 0);
713
    if (ret)
714
        return ret;
715
716
    ret = init_vlc(&c->ylev[1].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsy_1_bits),
717
                   clv_flagsy_1_bits,  1, 1,
718
                   clv_flagsy_1_codes, 2, 2, 0);
719
    if (ret)
720
        return ret;
721
722
    ret = init_vlc(&c->ylev[2].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsy_2_bits),
723
                   clv_flagsy_2_bits,  1, 1,
724
                   clv_flagsy_2_codes, 2, 2, 0);
725
    if (ret)
726
        return ret;
727
728
    ret = init_vlc(&c->ulev[0].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsu_0_bits),
729
                   clv_flagsu_0_bits,  1, 1,
730
                   clv_flagsu_0_codes, 2, 2, 0);
731
    if (ret)
732
        return ret;
733
734
    ret = init_vlc(&c->ulev[1].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsu_1_bits),
735
                   clv_flagsu_1_bits,  1, 1,
736
                   clv_flagsu_1_codes, 2, 2, 0);
737
    if (ret)
738
        return ret;
739
740
    ret = init_vlc(&c->vlev[0].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsv_0_bits),
741
                   clv_flagsv_0_bits,  1, 1,
742
                   clv_flagsv_0_codes, 2, 2, 0);
743
    if (ret)
744
        return ret;
745
746
    ret = init_vlc(&c->vlev[1].flags_cb, 9, FF_ARRAY_ELEMS(clv_flagsv_1_bits),
747
                   clv_flagsv_1_bits,  1, 1,
748
                   clv_flagsv_1_codes, 2, 2, 0);
749
    if (ret)
750
        return ret;
751
752
    ret = ff_init_vlc_sparse(&c->ylev[0].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvy_0_bits),
753
                             clv_mvy_0_bits,  1, 1,
754
                             clv_mvy_0_codes, 2, 2,
755
                             clv_mvy_0_syms,  2, 2, 0);
756
    if (ret)
757
        return ret;
758
759
    ret = ff_init_vlc_sparse(&c->ylev[1].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvy_1_bits),
760
                             clv_mvy_1_bits,  1, 1,
761
                             clv_mvy_1_codes, 2, 2,
762
                             clv_mvy_1_syms,  2, 2, 0);
763
    if (ret)
764
        return ret;
765
766
    ret = ff_init_vlc_sparse(&c->ylev[2].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvy_2_bits),
767
                             clv_mvy_2_bits,  1, 1,
768
                             clv_mvy_2_codes, 2, 2,
769
                             clv_mvy_2_syms,  2, 2, 0);
770
    if (ret)
771
        return ret;
772
773
    ret = ff_init_vlc_sparse(&c->ylev[3].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvy_3_bits),
774
                             clv_mvy_3_bits,  1, 1,
775
                             clv_mvy_3_codes, 2, 2,
776
                             clv_mvy_3_syms,  2, 2, 0);
777
    if (ret)
778
        return ret;
779
780
    ret = ff_init_vlc_sparse(&c->ulev[1].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvu_1_bits),
781
                             clv_mvu_1_bits,  1, 1,
782
                             clv_mvu_1_codes, 2, 2,
783
                             clv_mvu_1_syms,  2, 2, 0);
784
    if (ret)
785
        return ret;
786
787
    ret = ff_init_vlc_sparse(&c->ulev[2].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvu_2_bits),
788
                             clv_mvu_2_bits,  1, 1,
789
                             clv_mvu_2_codes, 2, 2,
790
                             clv_mvu_2_syms,  2, 2, 0);
791
    if (ret)
792
        return ret;
793
794
    ret = ff_init_vlc_sparse(&c->vlev[1].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvv_1_bits),
795
                             clv_mvv_1_bits,  1, 1,
796
                             clv_mvv_1_codes, 2, 2,
797
                             clv_mvv_1_syms,  2, 2, 0);
798
    if (ret)
799
        return ret;
800
801
    ret = ff_init_vlc_sparse(&c->vlev[2].mv_cb, 9, FF_ARRAY_ELEMS(clv_mvv_2_bits),
802
                             clv_mvv_2_bits,  1, 1,
803
                             clv_mvv_2_codes, 2, 2,
804
                             clv_mvv_2_syms,  2, 2, 0);
805
    if (ret)
806
        return ret;
807
808
    ret = ff_init_vlc_sparse(&c->ylev[1].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasy_1_bits),
809
                             clv_biasy_1_bits,  1, 1,
810
                             clv_biasy_1_codes, 2, 2,
811
                             clv_biasy_1_syms,  2, 2, 0);
812
    if (ret)
813
        return ret;
814
815
    ret = ff_init_vlc_sparse(&c->ylev[2].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasy_2_bits),
816
                             clv_biasy_2_bits,  1, 1,
817
                             clv_biasy_2_codes, 2, 2,
818
                             clv_biasy_2_syms,  2, 2, 0);
819
    if (ret)
820
        return ret;
821
822
    ret = ff_init_vlc_sparse(&c->ylev[3].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasy_3_bits),
823
                             clv_biasy_3_bits,  1, 1,
824
                             clv_biasy_3_codes, 2, 2,
825
                             clv_biasy_3_syms,  2, 2, 0);
826
    if (ret)
827
        return ret;
828
829
    ret = ff_init_vlc_sparse(&c->ulev[1].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasu_1_bits),
830
                             clv_biasu_1_bits,  1, 1,
831
                             clv_biasu_1_codes, 2, 2,
832
                             clv_biasu_1_syms,  2, 2, 0);
833
    if (ret)
834
        return ret;
835
836
    ret = ff_init_vlc_sparse(&c->ulev[2].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasu_2_bits),
837
                             clv_biasu_2_bits,  1, 1,
838
                             clv_biasu_2_codes, 2, 2,
839
                             clv_biasu_2_syms,  2, 2, 0);
840
    if (ret)
841
        return ret;
842
843
    ret = ff_init_vlc_sparse(&c->vlev[1].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasv_1_bits),
844
                             clv_biasv_1_bits,  1, 1,
845
                             clv_biasv_1_codes, 2, 2,
846
                             clv_biasv_1_syms,  2, 2, 0);
847
    if (ret)
848
        return ret;
849
850
    ret = ff_init_vlc_sparse(&c->vlev[2].bias_cb, 9, FF_ARRAY_ELEMS(clv_biasv_2_bits),
851
                             clv_biasv_2_bits,  1, 1,
852
                             clv_biasv_2_codes, 2, 2,
853
                             clv_biasv_2_syms,  2, 2, 0);
854
    if (ret)
855
        return ret;
856
857
    c->ylev[0].mv_esc = 0x0909;
858
    c->ylev[1].mv_esc = 0x0A0A;
859
    c->ylev[2].mv_esc = 0x1010;
860
    c->ylev[3].mv_esc = 0x1313;
861
    c->ulev[1].mv_esc = 0x0808;
862
    c->ulev[2].mv_esc = 0x0B0B;
863
    c->vlev[1].mv_esc = 0x0808;
864
    c->vlev[2].mv_esc = 0x0B0B;
865
866
    c->ylev[1].bias_esc = 0x100;
867
    c->ylev[2].bias_esc = 0x100;
868
    c->ylev[3].bias_esc = 0x100;
869
    c->ulev[1].bias_esc = 0x100;
870
    c->ulev[2].bias_esc = 0x100;
871
    c->vlev[1].bias_esc = 0x100;
872
    c->vlev[2].bias_esc = 0x100;
873
874
    return 0;
875
}
876
877
static av_cold int clv_decode_end(AVCodecContext *avctx)
878
{
879
    CLVContext *const c = avctx->priv_data;
880
    int i;
881
882
    av_frame_free(&c->prev);
883
    av_frame_free(&c->pic);
884
885
    av_freep(&c->mvi.mv);
886
887
    ff_free_vlc(&c->dc_vlc);
888
    ff_free_vlc(&c->ac_vlc);
889
    for (i = 0; i < 4; i++) {
890
        ff_free_vlc(&c->ylev[i].mv_cb);
891
        ff_free_vlc(&c->ylev[i].flags_cb);
892
        ff_free_vlc(&c->ylev[i].bias_cb);
893
    }
894
    for (i = 0; i < 3; i++) {
895
        ff_free_vlc(&c->ulev[i].mv_cb);
896
        ff_free_vlc(&c->ulev[i].flags_cb);
897
        ff_free_vlc(&c->ulev[i].bias_cb);
898
        ff_free_vlc(&c->vlev[i].mv_cb);
899
        ff_free_vlc(&c->vlev[i].flags_cb);
900
        ff_free_vlc(&c->vlev[i].bias_cb);
901
    }
902
903
    return 0;
904
}
905
906
AVCodec ff_clearvideo_decoder = {
907
    .name           = "clearvideo",
908
    .long_name      = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
909
    .type           = AVMEDIA_TYPE_VIDEO,
910
    .id             = AV_CODEC_ID_CLEARVIDEO,
911
    .priv_data_size = sizeof(CLVContext),
912
    .init           = clv_decode_init,
913
    .close          = clv_decode_end,
914
    .decode         = clv_decode_frame,
915
    .capabilities   = AV_CODEC_CAP_DR1,
916
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
917
};