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"
26 #include "bytestream.h"
28 #include "codec_internal.h"
33 #include "hq_hqadsp.h"
35 /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
36 * they usually form a pseudorandom pattern (probably because it is
37 * nicer to display on partial decode).
39 * For HQA it just happens that each slice is on every 8th macroblock,
40 * but they can be on any frame width like
47 * The original decoder has special handling for edge macroblocks,
48 * while lavc simply aligns coded_width and coded_height.
51 static inline void put_blocks(HQContext *c, AVFrame *pic,
52 int plane, int x, int y, int ilace,
53 int16_t *block0, int16_t *block1)
55 uint8_t *p = pic->data[plane] + x;
57 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
58 pic->linesize[plane] << ilace, block0);
59 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
60 pic->linesize[plane] << ilace, block1);
63 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
64 int qsel, int is_chroma, int is_hqa)
69 memset(block, 0, 64 * sizeof(*block));
72 block[0] = get_sbits(gb, 9) * 64;
73 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
75 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
76 block[0] = get_sbits(gb, 9) * 64;
80 val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
82 return AVERROR_INVALIDDATA;
84 pos += ff_hq_ac_skips[val];
87 block[ff_zigzag_direct[pos]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
94 static int hq_decode_mb(HQContext *c, AVFrame *pic,
95 GetBitContext *gb, int x, int y)
100 qgroup = get_bits(gb, 4);
101 flag = get_bits1(gb);
103 for (i = 0; i < 8; i++) {
104 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
109 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
110 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
111 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
112 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
117 static int hq_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc,
118 int prof_num, size_t data_size)
120 const HQProfile *profile;
122 const uint8_t *perm, *src = gbc->buffer;
123 uint32_t slice_off[21];
124 int slice, start_off, next_off, i, ret;
126 if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
127 profile = &ff_hq_profile[0];
128 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
130 profile = &ff_hq_profile[prof_num];
131 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
134 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
135 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
136 ctx->avctx->width = profile->width;
137 ctx->avctx->height = profile->height;
138 ctx->avctx->bits_per_raw_sample = 8;
139 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
141 ret = ff_get_buffer(ctx->avctx, pic, 0);
145 /* Offsets are stored from CUV position, so adjust them accordingly. */
146 for (i = 0; i < profile->num_slices + 1; i++)
147 slice_off[i] = bytestream2_get_be24(gbc) - 4;
150 for (slice = 0; slice < profile->num_slices; slice++) {
151 start_off = next_off;
152 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
153 perm = profile->perm_tab + start_off * profile->tab_w * 2;
155 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
156 slice_off[slice] >= slice_off[slice + 1] ||
157 slice_off[slice + 1] > data_size) {
158 av_log(ctx->avctx, AV_LOG_ERROR,
159 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
162 init_get_bits(&gb, src + slice_off[slice],
163 (slice_off[slice + 1] - slice_off[slice]) * 8);
165 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
166 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
168 av_log(ctx->avctx, AV_LOG_ERROR,
169 "Error decoding macroblock %d at slice %d.\n", i, slice);
179 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
180 GetBitContext *gb, int x, int y)
185 if (get_bits_left(gb) < 1)
186 return AVERROR_INVALIDDATA;
188 cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
190 for (i = 0; i < 12; i++)
191 memset(c->block[i], 0, sizeof(*c->block));
192 for (i = 0; i < 12; i++)
193 c->block[i][0] = -128 * (1 << 6);
196 flag = get_bits1(gb);
203 for (i = 0; i < 12; i++) {
204 if (!(cbp & (1 << i)))
206 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
212 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
213 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
214 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
215 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
216 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
217 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
222 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
223 int quant, int slice_no, int w, int h)
228 for (i = 0; i < h; i += 16) {
229 off = (slice_no * 16 + i * 3) & 0x70;
230 for (j = off; j < w; j += 128) {
231 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
233 av_log(ctx->avctx, AV_LOG_ERROR,
234 "Error decoding macroblock at %dx%d.\n", i, j);
243 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc, size_t data_size)
246 const int num_slices = 8;
247 uint32_t slice_off[9];
249 int width, height, quant;
250 const uint8_t *src = gbc->buffer;
252 if (bytestream2_get_bytes_left(gbc) < 8 + 4*(num_slices + 1))
253 return AVERROR_INVALIDDATA;
255 width = bytestream2_get_be16(gbc);
256 height = bytestream2_get_be16(gbc);
258 ret = ff_set_dimensions(ctx->avctx, width, height);
262 ctx->avctx->coded_width = FFALIGN(width, 16);
263 ctx->avctx->coded_height = FFALIGN(height, 16);
264 ctx->avctx->bits_per_raw_sample = 8;
265 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
267 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
269 quant = bytestream2_get_byte(gbc);
270 bytestream2_skip(gbc, 3);
271 if (quant >= NUM_HQ_QUANTS) {
272 av_log(ctx->avctx, AV_LOG_ERROR,
273 "Invalid quantization matrix %d.\n", quant);
274 return AVERROR_INVALIDDATA;
277 ret = ff_get_buffer(ctx->avctx, pic, 0);
281 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
282 for (i = 0; i < num_slices + 1; i++)
283 slice_off[i] = bytestream2_get_be32(gbc) - 4;
285 for (slice = 0; slice < num_slices; slice++) {
286 if (slice_off[slice] < (num_slices + 1) * 3 ||
287 slice_off[slice] >= slice_off[slice + 1] ||
288 slice_off[slice + 1] > data_size) {
289 av_log(ctx->avctx, AV_LOG_ERROR,
290 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
293 init_get_bits(&gb, src + slice_off[slice],
294 (slice_off[slice + 1] - slice_off[slice]) * 8);
296 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
304 static int hq_hqa_decode_frame(AVCodecContext *avctx, AVFrame *pic,
305 int *got_frame, AVPacket *avpkt)
307 HQContext *ctx = avctx->priv_data;
308 GetByteContext gbc0, *const gbc = &gbc0;
310 unsigned int data_size;
314 bytestream2_init(gbc, avpkt->data, avpkt->size);
315 if (bytestream2_get_bytes_left(gbc) < 4 + 4) {
316 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
317 return AVERROR_INVALIDDATA;
320 info_tag = bytestream2_peek_le32(gbc);
321 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
323 bytestream2_skip(gbc, 4);
324 info_size = bytestream2_get_le32(gbc);
325 if (info_size < 0 || bytestream2_get_bytes_left(gbc) < info_size) {
326 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
327 return AVERROR_INVALIDDATA;
329 ff_canopus_parse_info_tag(avctx, gbc->buffer, info_size);
331 bytestream2_skip(gbc, info_size);
334 data_size = bytestream2_get_bytes_left(gbc);
336 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
337 return AVERROR_INVALIDDATA;
340 /* HQ defines dimensions and number of slices, and thus slice traversal
341 * order. HQA has no size constraint and a fixed number of slices, so it
342 * needs a separate scheme for it. */
343 tag = bytestream2_get_le32(gbc);
344 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
345 ret = hq_decode_frame(ctx, pic, gbc, tag >> 24, data_size);
346 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
347 ret = hqa_decode_frame(ctx, pic, gbc, data_size);
349 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
350 return AVERROR_INVALIDDATA;
353 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
357 pic->flags |= AV_FRAME_FLAG_KEY;
358 pic->pict_type = AV_PICTURE_TYPE_I;
365 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
367 HQContext *ctx = avctx->priv_data;
370 ff_hqdsp_init(&ctx->hqhqadsp);
372 return ff_hq_init_vlcs(ctx);
375 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
377 HQContext *ctx = avctx->priv_data;
379 ff_vlc_free(&ctx->hq_ac_vlc);
380 ff_vlc_free(&ctx->hqa_cbp_vlc);
385 const FFCodec ff_hq_hqa_decoder = {
387 CODEC_LONG_NAME("Canopus HQ/HQA"),
388 .p.type = AVMEDIA_TYPE_VIDEO,
389 .p.id = AV_CODEC_ID_HQ_HQA,
390 .priv_data_size = sizeof(HQContext),
391 .init = hq_hqa_decode_init,
392 FF_CODEC_DECODE_CB(hq_hqa_decode_frame),
393 .close = hq_hqa_decode_close,
394 .p.capabilities = AV_CODEC_CAP_DR1,
395 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,