2 * Canopus HQ/HQA decoder
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/attributes.h"
24 #include "libavutil/intreadwrite.h"
32 #include "hq_hqadsp.h"
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).
38 * For HQA it just happens that each slice is on every 8th macroblock,
39 * but they can be on any frame width like
46 * The original decoder has special handling for edge macroblocks,
47 * while lavc simply aligns coded_width and coded_height.
50 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)
54 uint8_t *p = pic->data[plane] + x;
56 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
57 pic->linesize[plane] << ilace, block0);
58 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
59 pic->linesize[plane] << ilace, block1);
62 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
63 int qsel, int is_chroma, int is_hqa)
68 memset(block, 0, 64 * sizeof(*block));
71 block[0] = get_sbits(gb, 9) * 64;
72 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
74 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
75 block[0] = get_sbits(gb, 9) * 64;
79 val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
81 return AVERROR_INVALIDDATA;
83 pos += ff_hq_ac_skips[val];
86 block[ff_zigzag_direct[pos]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
93 static int hq_decode_mb(HQContext *c, AVFrame *pic,
94 GetBitContext *gb, int x, int y)
99 qgroup = get_bits(gb, 4);
100 flag = get_bits1(gb);
102 for (i = 0; i < 8; i++) {
103 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
108 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
109 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
110 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
111 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
116 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
117 int prof_num, size_t data_size)
119 const HQProfile *profile;
121 const uint8_t *perm, *src = ctx->gbc.buffer;
122 uint32_t slice_off[21];
123 int slice, start_off, next_off, i, ret;
125 if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
126 profile = &ff_hq_profile[0];
127 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
129 profile = &ff_hq_profile[prof_num];
130 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
133 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
134 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
135 ctx->avctx->width = profile->width;
136 ctx->avctx->height = profile->height;
137 ctx->avctx->bits_per_raw_sample = 8;
138 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
140 ret = ff_get_buffer(ctx->avctx, pic, 0);
144 /* Offsets are stored from CUV position, so adjust them accordingly. */
145 for (i = 0; i < profile->num_slices + 1; i++)
146 slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
149 for (slice = 0; slice < profile->num_slices; slice++) {
150 start_off = next_off;
151 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
152 perm = profile->perm_tab + start_off * profile->tab_w * 2;
154 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
155 slice_off[slice] >= slice_off[slice + 1] ||
156 slice_off[slice + 1] > data_size) {
157 av_log(ctx->avctx, AV_LOG_ERROR,
158 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
161 init_get_bits(&gb, src + slice_off[slice],
162 (slice_off[slice + 1] - slice_off[slice]) * 8);
164 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
165 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
167 av_log(ctx->avctx, AV_LOG_ERROR,
168 "Error decoding macroblock %d at slice %d.\n", i, slice);
178 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
179 GetBitContext *gb, int x, int y)
184 if (get_bits_left(gb) < 1)
185 return AVERROR_INVALIDDATA;
187 cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
189 for (i = 0; i < 12; i++)
190 memset(c->block[i], 0, sizeof(*c->block));
191 for (i = 0; i < 12; i++)
192 c->block[i][0] = -128 * (1 << 6);
195 flag = get_bits1(gb);
202 for (i = 0; i < 12; i++) {
203 if (!(cbp & (1 << i)))
205 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
211 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
212 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
213 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
214 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
215 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
216 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
221 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
222 int quant, int slice_no, int w, int h)
227 for (i = 0; i < h; i += 16) {
228 off = (slice_no * 16 + i * 3) & 0x70;
229 for (j = off; j < w; j += 128) {
230 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
232 av_log(ctx->avctx, AV_LOG_ERROR,
233 "Error decoding macroblock at %dx%d.\n", i, j);
242 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
245 const int num_slices = 8;
246 uint32_t slice_off[9];
248 int width, height, quant;
249 const uint8_t *src = ctx->gbc.buffer;
251 if (bytestream2_get_bytes_left(&ctx->gbc) < 8 + 4*(num_slices + 1))
252 return AVERROR_INVALIDDATA;
254 width = bytestream2_get_be16(&ctx->gbc);
255 height = bytestream2_get_be16(&ctx->gbc);
257 ret = ff_set_dimensions(ctx->avctx, width, height);
261 ctx->avctx->coded_width = FFALIGN(width, 16);
262 ctx->avctx->coded_height = FFALIGN(height, 16);
263 ctx->avctx->bits_per_raw_sample = 8;
264 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
266 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
268 quant = bytestream2_get_byte(&ctx->gbc);
269 bytestream2_skip(&ctx->gbc, 3);
270 if (quant >= NUM_HQ_QUANTS) {
271 av_log(ctx->avctx, AV_LOG_ERROR,
272 "Invalid quantization matrix %d.\n", quant);
273 return AVERROR_INVALIDDATA;
276 ret = ff_get_buffer(ctx->avctx, pic, 0);
280 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
281 for (i = 0; i < num_slices + 1; i++)
282 slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
284 for (slice = 0; slice < num_slices; slice++) {
285 if (slice_off[slice] < (num_slices + 1) * 3 ||
286 slice_off[slice] >= slice_off[slice + 1] ||
287 slice_off[slice + 1] > data_size) {
288 av_log(ctx->avctx, AV_LOG_ERROR,
289 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
292 init_get_bits(&gb, src + slice_off[slice],
293 (slice_off[slice + 1] - slice_off[slice]) * 8);
295 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
303 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
304 int *got_frame, AVPacket *avpkt)
306 HQContext *ctx = avctx->priv_data;
309 unsigned int data_size;
313 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
314 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;
319 info_tag = bytestream2_peek_le32(&ctx->gbc);
320 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
322 bytestream2_skip(&ctx->gbc, 4);
323 info_size = bytestream2_get_le32(&ctx->gbc);
324 if (info_size < 0 || 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;
328 ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
330 bytestream2_skip(&ctx->gbc, info_size);
333 data_size = bytestream2_get_bytes_left(&ctx->gbc);
335 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
336 return AVERROR_INVALIDDATA;
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 tag = bytestream2_get_le32(&ctx->gbc);
343 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
344 ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
345 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
346 ret = hqa_decode_frame(ctx, pic, data_size);
348 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
349 return AVERROR_INVALIDDATA;
352 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
357 pic->pict_type = AV_PICTURE_TYPE_I;
364 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
366 HQContext *ctx = avctx->priv_data;
369 ff_hqdsp_init(&ctx->hqhqadsp);
371 return ff_hq_init_vlcs(ctx);
374 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
376 HQContext *ctx = avctx->priv_data;
378 ff_free_vlc(&ctx->hq_ac_vlc);
379 ff_free_vlc(&ctx->hqa_cbp_vlc);
384 const AVCodec ff_hq_hqa_decoder = {
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,