GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/hq_hqa.c Lines: 161 192 83.9 %
Date: 2019-11-22 03:34:36 Branches: 63 88 71.6 %

Line Branch Exec Source
1
/*
2
 * Canopus HQ/HQA decoder
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include <stdint.h>
22
23
#include "libavutil/attributes.h"
24
#include "libavutil/intreadwrite.h"
25
26
#include "avcodec.h"
27
#include "canopus.h"
28
#include "get_bits.h"
29
#include "internal.h"
30
31
#include "hq_hqa.h"
32
#include "hq_hqadsp.h"
33
34
/* HQ/HQA slices are a set of macroblocks belonging to a frame, and
35
 * they usually form a pseudorandom pattern (probably because it is
36
 * nicer to display on partial decode).
37
 *
38
 * For HQA it just happens that each slice is on every 8th macroblock,
39
 * but they can be on any frame width like
40
 *   X.......X.
41
 *   ......X...
42
 *   ....X.....
43
 *   ..X.......
44
 * etc.
45
 *
46
 * The original decoder has special handling for edge macroblocks,
47
 * while lavc simply aligns coded_width and coded_height.
48
 */
49
50
221520
static inline void put_blocks(HQContext *c, AVFrame *pic,
51
                              int plane, int x, int y, int ilace,
52
                              int16_t *block0, int16_t *block1)
53
{
54
221520
    uint8_t *p = pic->data[plane] + x;
55
56
221520
    c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
57
221520
                         pic->linesize[plane] << ilace, block0);
58
221520
    c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
59
221520
                         pic->linesize[plane] << ilace, block1);
60
221520
}
61
62
365300
static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
63
                           int qsel, int is_chroma, int is_hqa)
64
{
65
    const int32_t *q;
66
365300
    int val, pos = 1;
67
68
365300
    memset(block, 0, 64 * sizeof(*block));
69
70
365300
    if (!is_hqa) {
71
247200
        block[0] = get_sbits(gb, 9) * 64;
72
247200
        q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
73
    } else {
74
118100
        q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
75
118100
        block[0] = get_sbits(gb, 9) * 64;
76
    }
77
78
    for (;;) {
79
5841374
        val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
80
5841374
        if (val < 0)
81
            return AVERROR_INVALIDDATA;
82
83
5841374
        pos += ff_hq_ac_skips[val];
84
5841374
        if (pos >= 64)
85
365300
            break;
86
5476074
        block[ff_zigzag_direct[pos]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
87
5476074
        pos++;
88
    }
89
90
365300
    return 0;
91
}
92
93
30900
static int hq_decode_mb(HQContext *c, AVFrame *pic,
94
                        GetBitContext *gb, int x, int y)
95
{
96
    int qgroup, flag;
97
    int i, ret;
98
99
30900
    qgroup = get_bits(gb, 4);
100
30900
    flag = get_bits1(gb);
101
102
278100
    for (i = 0; i < 8; i++) {
103
247200
        ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
104
247200
        if (ret < 0)
105
            return ret;
106
    }
107
108
30900
    put_blocks(c, pic, 0, x,      y, flag, c->block[0], c->block[2]);
109
30900
    put_blocks(c, pic, 0, x + 8,  y, flag, c->block[1], c->block[3]);
110
30900
    put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
111
30900
    put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
112
113
30900
    return 0;
114
}
115
116
11
static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
117
                           int prof_num, size_t data_size)
118
{
119
    const HQProfile *profile;
120
    GetBitContext gb;
121
11
    const uint8_t *perm, *src = ctx->gbc.buffer;
122
    uint32_t slice_off[21];
123
    int slice, start_off, next_off, i, ret;
124
125
11
    if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
126
        profile = &ff_hq_profile[0];
127
        avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
128
    } else {
129
11
        profile = &ff_hq_profile[prof_num];
130
11
        av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
131
    }
132
133
11
    ctx->avctx->coded_width         = FFALIGN(profile->width,  16);
134
11
    ctx->avctx->coded_height        = FFALIGN(profile->height, 16);
135
11
    ctx->avctx->width               = profile->width;
136
11
    ctx->avctx->height              = profile->height;
137
11
    ctx->avctx->bits_per_raw_sample = 8;
138
11
    ctx->avctx->pix_fmt             = AV_PIX_FMT_YUV422P;
139
140
11
    ret = ff_get_buffer(ctx->avctx, pic, 0);
141
11
    if (ret < 0)
142
        return ret;
143
144
    /* Offsets are stored from CUV position, so adjust them accordingly. */
145
134
    for (i = 0; i < profile->num_slices + 1; i++)
146
123
        slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
147
148
11
    next_off = 0;
149
123
    for (slice = 0; slice < profile->num_slices; slice++) {
150
112
        start_off = next_off;
151
112
        next_off  = profile->tab_h * (slice + 1) / profile->num_slices;
152
112
        perm = profile->perm_tab + start_off * profile->tab_w * 2;
153
154
112
        if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
155
112
            slice_off[slice] >= slice_off[slice + 1] ||
156
112
            slice_off[slice + 1] > data_size) {
157
            av_log(ctx->avctx, AV_LOG_ERROR,
158
                   "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
159
            break;
160
        }
161
112
        init_get_bits(&gb, src + slice_off[slice],
162
112
                      (slice_off[slice + 1] - slice_off[slice]) * 8);
163
164
31012
        for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
165
30900
            ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
166
30900
            if (ret < 0) {
167
                av_log(ctx->avctx, AV_LOG_ERROR,
168
                       "Error decoding macroblock %d at slice %d.\n", i, slice);
169
                return ret;
170
            }
171
30900
            perm += 2;
172
        }
173
    }
174
175
11
    return 0;
176
}
177
178
16320
static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
179
                         GetBitContext *gb, int x, int y)
180
{
181
16320
    int flag = 0;
182
    int i, ret, cbp;
183
184
16320
    if (get_bits_left(gb) < 1)
185
        return AVERROR_INVALIDDATA;
186
187
16320
    cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
188
189
212160
    for (i = 0; i < 12; i++)
190
195840
        memset(c->block[i], 0, sizeof(*c->block));
191
212160
    for (i = 0; i < 12; i++)
192
195840
        c->block[i][0] = -128 * (1 << 6);
193
194
16320
    if (cbp) {
195
9978
        flag = get_bits1(gb);
196
197
9978
        cbp |= cbp << 4;
198
9978
        if (cbp & 0x3)
199
9890
            cbp |= 0x500;
200
9978
        if (cbp & 0xC)
201
9874
            cbp |= 0xA00;
202
129714
        for (i = 0; i < 12; i++) {
203
119736
            if (!(cbp & (1 << i)))
204
1636
                continue;
205
118100
            ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
206
118100
            if (ret < 0)
207
                return ret;
208
        }
209
    }
210
211
16320
    put_blocks(c, pic, 3, x,      y, flag, c->block[ 0], c->block[ 2]);
212
16320
    put_blocks(c, pic, 3, x + 8,  y, flag, c->block[ 1], c->block[ 3]);
213
16320
    put_blocks(c, pic, 0, x,      y, flag, c->block[ 4], c->block[ 6]);
214
16320
    put_blocks(c, pic, 0, x + 8,  y, flag, c->block[ 5], c->block[ 7]);
215
16320
    put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
216
16320
    put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
217
218
16320
    return 0;
219
}
220
221
16
static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
222
                            int quant, int slice_no, int w, int h)
223
{
224
    int i, j, off;
225
    int ret;
226
227
1104
    for (i = 0; i < h; i += 16) {
228
1088
        off = (slice_no * 16 + i * 3) & 0x70;
229
17408
        for (j = off; j < w; j += 128) {
230
16320
            ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
231
16320
            if (ret < 0) {
232
                av_log(ctx->avctx, AV_LOG_ERROR,
233
                       "Error decoding macroblock at %dx%d.\n", i, j);
234
                return ret;
235
            }
236
        }
237
    }
238
239
16
    return 0;
240
}
241
242
2
static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
243
{
244
    GetBitContext gb;
245
2
    const int num_slices = 8;
246
    uint32_t slice_off[9];
247
    int i, slice, ret;
248
    int width, height, quant;
249
2
    const uint8_t *src = ctx->gbc.buffer;
250
251
2
    if (bytestream2_get_bytes_left(&ctx->gbc) < 8 + 4*(num_slices + 1))
252
        return AVERROR_INVALIDDATA;
253
254
2
    width  = bytestream2_get_be16(&ctx->gbc);
255
2
    height = bytestream2_get_be16(&ctx->gbc);
256
257
2
    ret = ff_set_dimensions(ctx->avctx, width, height);
258
2
    if (ret < 0)
259
        return ret;
260
261
2
    ctx->avctx->coded_width         = FFALIGN(width,  16);
262
2
    ctx->avctx->coded_height        = FFALIGN(height, 16);
263
2
    ctx->avctx->bits_per_raw_sample = 8;
264
2
    ctx->avctx->pix_fmt             = AV_PIX_FMT_YUVA422P;
265
266
2
    av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
267
268
2
    quant = bytestream2_get_byte(&ctx->gbc);
269
2
    bytestream2_skip(&ctx->gbc, 3);
270
2
    if (quant >= NUM_HQ_QUANTS) {
271
        av_log(ctx->avctx, AV_LOG_ERROR,
272
               "Invalid quantization matrix %d.\n", quant);
273
        return AVERROR_INVALIDDATA;
274
    }
275
276
2
    ret = ff_get_buffer(ctx->avctx, pic, 0);
277
2
    if (ret < 0)
278
        return ret;
279
280
    /* Offsets are stored from HQA1 position, so adjust them accordingly. */
281
20
    for (i = 0; i < num_slices + 1; i++)
282
18
        slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
283
284
18
    for (slice = 0; slice < num_slices; slice++) {
285
16
        if (slice_off[slice] < (num_slices + 1) * 3 ||
286
16
            slice_off[slice] >= slice_off[slice + 1] ||
287
16
            slice_off[slice + 1] > data_size) {
288
            av_log(ctx->avctx, AV_LOG_ERROR,
289
                   "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
290
            break;
291
        }
292
16
        init_get_bits(&gb, src + slice_off[slice],
293
16
                      (slice_off[slice + 1] - slice_off[slice]) * 8);
294
295
16
        ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
296
16
        if (ret < 0)
297
            return ret;
298
    }
299
300
2
    return 0;
301
}
302
303
13
static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
304
                               int *got_frame, AVPacket *avpkt)
305
{
306
13
    HQContext *ctx = avctx->priv_data;
307
13
    AVFrame *pic = data;
308
    uint32_t info_tag;
309
    unsigned int data_size;
310
    int ret;
311
    unsigned tag;
312
313
13
    bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
314
13
    if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
315
        av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
316
        return AVERROR_INVALIDDATA;
317
    }
318
319
13
    info_tag = bytestream2_peek_le32(&ctx->gbc);
320
13
    if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
321
        int info_size;
322
13
        bytestream2_skip(&ctx->gbc, 4);
323
13
        info_size = bytestream2_get_le32(&ctx->gbc);
324
13
        if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
325
            av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
326
            return AVERROR_INVALIDDATA;
327
        }
328
13
        ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
329
330
13
        bytestream2_skip(&ctx->gbc, info_size);
331
    }
332
333
13
    data_size = bytestream2_get_bytes_left(&ctx->gbc);
334
13
    if (data_size < 4) {
335
        av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
336
        return AVERROR_INVALIDDATA;
337
    }
338
339
    /* HQ defines dimensions and number of slices, and thus slice traversal
340
     * order. HQA has no size constraint and a fixed number of slices, so it
341
     * needs a separate scheme for it. */
342
13
    tag = bytestream2_get_le32(&ctx->gbc);
343
13
    if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
344
11
        ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
345
2
    } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
346
2
        ret = hqa_decode_frame(ctx, pic, data_size);
347
    } else {
348
        av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
349
        return AVERROR_INVALIDDATA;
350
    }
351
13
    if (ret < 0) {
352
        av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
353
        return ret;
354
    }
355
356
13
    pic->key_frame = 1;
357
13
    pic->pict_type = AV_PICTURE_TYPE_I;
358
359
13
    *got_frame = 1;
360
361
13
    return avpkt->size;
362
}
363
364
6
static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
365
{
366
6
    HQContext *ctx = avctx->priv_data;
367
6
    ctx->avctx = avctx;
368
369
6
    ff_hqdsp_init(&ctx->hqhqadsp);
370
371
6
    return ff_hq_init_vlcs(ctx);
372
}
373
374
6
static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
375
{
376
6
    HQContext *ctx = avctx->priv_data;
377
378
6
    ff_free_vlc(&ctx->hq_ac_vlc);
379
6
    ff_free_vlc(&ctx->hqa_cbp_vlc);
380
381
6
    return 0;
382
}
383
384
AVCodec ff_hq_hqa_decoder = {
385
    .name           = "hq_hqa",
386
    .long_name      = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
387
    .type           = AVMEDIA_TYPE_VIDEO,
388
    .id             = AV_CODEC_ID_HQ_HQA,
389
    .priv_data_size = sizeof(HQContext),
390
    .init           = hq_hqa_decode_init,
391
    .decode         = hq_hqa_decode_frame,
392
    .close          = hq_hqa_decode_close,
393
    .capabilities   = AV_CODEC_CAP_DR1,
394
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
395
                      FF_CODEC_CAP_INIT_CLEANUP,
396
};