2 * Canopus HQ/HQA decoder
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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"
31 #include "hq_hqadsp.h"
33 /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
34 * they usually form a pseudorandom pattern (probably because it is
35 * nicer to display on partial decode).
37 * For HQA it just happens that each slice is on every 8th macroblock,
38 * but they can be on any frame width like
45 * The original decoder has special handling for edge macroblocks,
46 * while lavc simply aligns coded_width and coded_height.
49 static inline void put_blocks(HQContext *c, AVFrame *pic,
50 int plane, int x, int y, int ilace,
51 int16_t *block0, int16_t *block1)
53 uint8_t *p = pic->data[plane] + x;
55 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
56 pic->linesize[plane] << ilace, block0);
57 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
58 pic->linesize[plane] << ilace, block1);
61 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
62 int qsel, int is_chroma, int is_hqa)
67 memset(block, 0, 64 * sizeof(*block));
70 block[0] = get_sbits(gb, 9) << 6;
71 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
73 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
74 block[0] = get_sbits(gb, 9) << 6;
78 val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
80 return AVERROR_INVALIDDATA;
82 pos += ff_hq_ac_skips[val];
85 block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
92 static int hq_decode_mb(HQContext *c, AVFrame *pic,
93 GetBitContext *gb, int x, int y)
98 qgroup = get_bits(gb, 4);
101 for (i = 0; i < 8; i++) {
102 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
107 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
108 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
109 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
110 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
115 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
116 int prof_num, size_t data_size)
118 const HQProfile *profile;
120 const uint8_t *perm, *src = ctx->gbc.buffer;
121 uint32_t slice_off[21];
122 int slice, start_off, next_off, i, ret;
124 if (prof_num >= NUM_HQ_PROFILES) {
125 profile = &ff_hq_profile[0];
126 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
128 profile = &ff_hq_profile[prof_num];
129 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
132 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
133 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
134 ctx->avctx->width = profile->width;
135 ctx->avctx->height = profile->height;
136 ctx->avctx->bits_per_raw_sample = 8;
137 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
139 ret = ff_get_buffer(ctx->avctx, pic, 0);
141 av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
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(&ctx->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 %zu.\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 cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
187 for (i = 0; i < 12; i++)
188 memset(c->block[i], 0, sizeof(*c->block));
189 for (i = 0; i < 12; i++)
190 c->block[i][0] = -128 * (1 << 6);
193 flag = get_bits1(gb);
200 for (i = 0; i < 12; i++) {
201 if (!(cbp & (1 << i)))
203 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
209 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
210 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
211 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
212 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
213 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
214 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
219 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
220 int quant, int slice_no, int w, int h)
225 for (i = 0; i < h; i += 16) {
226 off = (slice_no * 16 + i * 3) & 0x70;
227 for (j = off; j < w; j += 128) {
228 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
230 av_log(ctx->avctx, AV_LOG_ERROR,
231 "Error decoding macroblock at %dx%d.\n", i, j);
240 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
243 const int num_slices = 8;
244 uint32_t slice_off[9];
246 int width, height, quant;
247 const uint8_t *src = ctx->gbc.buffer;
249 width = bytestream2_get_be16(&ctx->gbc);
250 height = bytestream2_get_be16(&ctx->gbc);
252 ctx->avctx->coded_width = FFALIGN(width, 16);
253 ctx->avctx->coded_height = FFALIGN(height, 16);
254 ctx->avctx->width = width;
255 ctx->avctx->height = height;
256 ctx->avctx->bits_per_raw_sample = 8;
257 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
259 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
261 quant = bytestream2_get_byte(&ctx->gbc);
262 bytestream2_skip(&ctx->gbc, 3);
263 if (quant >= NUM_HQ_QUANTS) {
264 av_log(ctx->avctx, AV_LOG_ERROR,
265 "Invalid quantization matrix %d.\n", quant);
266 return AVERROR_INVALIDDATA;
269 ret = ff_get_buffer(ctx->avctx, pic, 0);
271 av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
275 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
276 for (i = 0; i < num_slices + 1; i++)
277 slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
279 for (slice = 0; slice < num_slices; slice++) {
280 if (slice_off[slice] < (num_slices + 1) * 3 ||
281 slice_off[slice] >= slice_off[slice + 1] ||
282 slice_off[slice + 1] > data_size) {
283 av_log(ctx->avctx, AV_LOG_ERROR,
284 "Invalid slice size %zu.\n", data_size);
287 init_get_bits(&gb, src + slice_off[slice],
288 (slice_off[slice + 1] - slice_off[slice]) * 8);
290 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
298 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
299 int *got_frame, AVPacket *avpkt)
301 HQContext *ctx = avctx->priv_data;
304 unsigned int data_size;
307 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
308 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
309 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
310 return AVERROR_INVALIDDATA;
313 info_tag = bytestream2_get_le32(&ctx->gbc);
314 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
315 int info_size = bytestream2_get_le32(&ctx->gbc);
316 if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
317 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
318 return AVERROR_INVALIDDATA;
320 ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
322 bytestream2_skip(&ctx->gbc, info_size);
325 data_size = bytestream2_get_bytes_left(&ctx->gbc);
327 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
328 return AVERROR_INVALIDDATA;
331 /* HQ defines dimensions and number of slices, and thus slice traversal
332 * order. HQA has no size constraint and a fixed number of slices, so it
333 * needs a separate scheme for it. */
334 tag = bytestream2_get_le32(&ctx->gbc);
335 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
336 ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
337 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
338 ret = hqa_decode_frame(ctx, pic, data_size);
340 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
341 return AVERROR_INVALIDDATA;
344 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
349 pic->pict_type = AV_PICTURE_TYPE_I;
356 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
358 HQContext *ctx = avctx->priv_data;
361 ff_hqdsp_init(&ctx->hqhqadsp);
363 return ff_hq_init_vlcs(ctx);
366 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
368 HQContext *ctx = avctx->priv_data;
370 ff_free_vlc(&ctx->hq_ac_vlc);
371 ff_free_vlc(&ctx->hqa_cbp_vlc);
376 AVCodec ff_hq_hqa_decoder = {
378 .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
379 .type = AVMEDIA_TYPE_VIDEO,
380 .id = AV_CODEC_ID_HQ_HQA,
381 .priv_data_size = sizeof(HQContext),
382 .init = hq_hqa_decode_init,
383 .decode = hq_hqa_decode_frame,
384 .close = hq_hqa_decode_close,
385 .capabilities = CODEC_CAP_DR1,
386 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
387 FF_CODEC_CAP_INIT_CLEANUP,