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"
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);
79 pos += ff_hq_ac_skips[val];
82 block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
89 static int hq_decode_mb(HQContext *c, AVFrame *pic,
90 GetBitContext *gb, int x, int y)
95 qgroup = get_bits(gb, 4);
98 for (i = 0; i < 8; i++) {
99 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
104 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
105 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
106 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
107 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
112 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
113 int prof_num, size_t data_size)
115 const HQProfile *profile;
117 const uint8_t *perm, *src = ctx->gbc.buffer;
118 uint32_t slice_off[21];
119 int slice, start_off, next_off, i, ret;
121 if (prof_num >= NUM_HQ_PROFILES) {
122 profile = &ff_hq_profile[0];
123 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
125 profile = &ff_hq_profile[prof_num];
126 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
129 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
130 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
131 ctx->avctx->width = profile->width;
132 ctx->avctx->height = profile->height;
133 ctx->avctx->bits_per_raw_sample = 8;
134 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
136 ret = ff_get_buffer(ctx->avctx, pic, 0);
140 /* Offsets are stored from CUV position, so adjust them accordingly. */
141 for (i = 0; i < profile->num_slices + 1; i++)
142 slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
145 for (slice = 0; slice < profile->num_slices; slice++) {
146 start_off = next_off;
147 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
148 perm = profile->perm_tab + start_off * profile->tab_w * 2;
150 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
151 slice_off[slice] >= slice_off[slice + 1] ||
152 slice_off[slice + 1] > data_size) {
153 av_log(ctx->avctx, AV_LOG_ERROR,
154 "Invalid slice size %zu.\n", data_size);
157 init_get_bits(&gb, src + slice_off[slice],
158 (slice_off[slice + 1] - slice_off[slice]) * 8);
160 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
161 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
163 av_log(ctx->avctx, AV_LOG_ERROR,
164 "Error decoding macroblock %d at slice %d.\n", i, slice);
174 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
175 GetBitContext *gb, int x, int y)
180 cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
182 for (i = 0; i < 12; i++)
183 memset(c->block[i], 0, sizeof(*c->block));
184 for (i = 0; i < 12; i++)
185 c->block[i][0] = -128 * (1 << 6);
188 flag = get_bits1(gb);
195 for (i = 0; i < 12; i++) {
196 if (!(cbp & (1 << i)))
198 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
204 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
205 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
206 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
207 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
208 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
209 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
214 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
215 int quant, int slice_no, int w, int h)
220 for (i = 0; i < h; i += 16) {
221 off = (slice_no * 16 + i * 3) & 0x70;
222 for (j = off; j < w; j += 128) {
223 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
225 av_log(ctx->avctx, AV_LOG_ERROR,
226 "Error decoding macroblock at %dx%d.\n", i, j);
235 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
238 const int num_slices = 8;
239 uint32_t slice_off[9];
241 int width, height, quant;
242 const uint8_t *src = ctx->gbc.buffer;
244 width = bytestream2_get_be16(&ctx->gbc);
245 height = bytestream2_get_be16(&ctx->gbc);
247 ctx->avctx->coded_width = FFALIGN(width, 16);
248 ctx->avctx->coded_height = FFALIGN(height, 16);
249 ctx->avctx->width = width;
250 ctx->avctx->height = height;
251 ctx->avctx->bits_per_raw_sample = 8;
252 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
254 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
256 quant = bytestream2_get_byte(&ctx->gbc);
257 bytestream2_skip(&ctx->gbc, 3);
258 if (quant >= NUM_HQ_QUANTS) {
259 av_log(ctx->avctx, AV_LOG_ERROR,
260 "Invalid quantization matrix %d.\n", quant);
261 return AVERROR_INVALIDDATA;
264 ret = ff_get_buffer(ctx->avctx, pic, 0);
268 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
269 for (i = 0; i < num_slices + 1; i++)
270 slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
272 for (slice = 0; slice < num_slices; slice++) {
273 if (slice_off[slice] < (num_slices + 1) * 3 ||
274 slice_off[slice] >= slice_off[slice + 1] ||
275 slice_off[slice + 1] > data_size) {
276 av_log(ctx->avctx, AV_LOG_ERROR,
277 "Invalid slice size %zu.\n", data_size);
280 init_get_bits(&gb, src + slice_off[slice],
281 (slice_off[slice + 1] - slice_off[slice]) * 8);
283 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
291 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
292 int *got_frame, AVPacket *avpkt)
294 HQContext *ctx = avctx->priv_data;
297 unsigned int data_size;
300 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
301 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
302 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
303 return AVERROR_INVALIDDATA;
306 info_tag = bytestream2_get_le32(&ctx->gbc);
307 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
308 int info_size = bytestream2_get_le32(&ctx->gbc);
309 if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
310 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
311 return AVERROR_INVALIDDATA;
313 ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
315 bytestream2_skip(&ctx->gbc, info_size);
318 data_size = bytestream2_get_bytes_left(&ctx->gbc);
320 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
321 return AVERROR_INVALIDDATA;
324 /* HQ defines dimensions and number of slices, and thus slice traversal
325 * order. HQA has no size constraint and a fixed number of slices, so it
326 * needs a separate scheme for it. */
327 tag = bytestream2_get_le32(&ctx->gbc);
328 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
329 ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
330 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
331 ret = hqa_decode_frame(ctx, pic, data_size);
333 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
334 return AVERROR_INVALIDDATA;
337 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
342 pic->pict_type = AV_PICTURE_TYPE_I;
349 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
351 HQContext *ctx = avctx->priv_data;
354 ff_hqdsp_init(&ctx->hqhqadsp);
356 return ff_hq_init_vlcs(ctx);
359 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
361 HQContext *ctx = avctx->priv_data;
363 ff_free_vlc(&ctx->hq_ac_vlc);
364 ff_free_vlc(&ctx->hqa_cbp_vlc);
369 AVCodec ff_hq_hqa_decoder = {
371 .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
372 .type = AVMEDIA_TYPE_VIDEO,
373 .id = AV_CODEC_ID_HQ_HQA,
374 .priv_data_size = sizeof(HQContext),
375 .init = hq_hqa_decode_init,
376 .decode = hq_hqa_decode_frame,
377 .close = hq_hqa_decode_close,
378 .capabilities = CODEC_CAP_DR1,
379 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
380 FF_CODEC_CAP_INIT_CLEANUP,