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