aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / hq_hqa.c
blob2afe8531beeb75746f7b1287282ca62faaf4dadb
1 /*
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
21 #include <stdint.h>
23 #include "libavutil/attributes.h"
24 #include "libavutil/intreadwrite.h"
26 #include "avcodec.h"
27 #include "bitstream.h"
28 #include "canopus.h"
29 #include "internal.h"
31 #include "hq_hqa.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
40 * X.......X.
41 * ......X...
42 * ....X.....
43 * ..X.......
44 * etc.
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, BitstreamContext *bc, int16_t block[64],
63 int qsel, int is_chroma, int is_hqa)
65 const int32_t *q;
66 int val, pos = 1;
68 memset(block, 0, 64 * sizeof(*block));
70 if (!is_hqa) {
71 block[0] = bitstream_read_signed(bc, 9) << 6;
72 q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)];
73 } else {
74 q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)];
75 block[0] = bitstream_read_signed(bc, 9) << 6;
78 for (;;) {
79 val = bitstream_read_vlc(bc, c->hq_ac_vlc.table, 9, 2);
80 if (val < 0)
81 return AVERROR_INVALIDDATA;
83 pos += ff_hq_ac_skips[val];
84 if (pos >= 64)
85 break;
86 block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
87 pos++;
90 return 0;
93 static int hq_decode_mb(HQContext *c, AVFrame *pic,
94 BitstreamContext *bc, int x, int y)
96 int qgroup, flag;
97 int i, ret;
99 qgroup = bitstream_read(bc, 4);
100 flag = bitstream_read_bit(bc);
102 for (i = 0; i < 8; i++) {
103 ret = hq_decode_block(c, bc, c->block[i], qgroup, i >= 4, 0);
104 if (ret < 0)
105 return ret;
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]);
113 return 0;
116 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
117 int prof_num, size_t data_size)
119 const HQProfile *profile;
120 BitstreamContext bc;
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 (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 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);
141 if (ret < 0) {
142 av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
143 return ret;
146 /* Offsets are stored from CUV position, so adjust them accordingly. */
147 for (i = 0; i < profile->num_slices + 1; i++)
148 slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
150 next_off = 0;
151 for (slice = 0; slice < profile->num_slices; slice++) {
152 start_off = next_off;
153 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
154 perm = profile->perm_tab + start_off * profile->tab_w * 2;
156 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
157 slice_off[slice] >= slice_off[slice + 1] ||
158 slice_off[slice + 1] > data_size) {
159 av_log(ctx->avctx, AV_LOG_ERROR,
160 "Invalid slice size %zu.\n", data_size);
161 break;
163 bitstream_init8(&bc, src + slice_off[slice],
164 slice_off[slice + 1] - slice_off[slice]);
166 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
167 ret = hq_decode_mb(ctx, pic, &bc, perm[0] * 16, perm[1] * 16);
168 if (ret < 0) {
169 av_log(ctx->avctx, AV_LOG_ERROR,
170 "Error decoding macroblock %d at slice %d.\n", i, slice);
171 return ret;
173 perm += 2;
177 return 0;
180 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
181 BitstreamContext *bc, int x, int y)
183 int flag = 0;
184 int i, ret, cbp;
186 cbp = bitstream_read_vlc(bc, c->hqa_cbp_vlc.table, 5, 1);
188 for (i = 0; i < 12; i++)
189 memset(c->block[i], 0, sizeof(*c->block));
190 for (i = 0; i < 12; i++)
191 c->block[i][0] = -128 * (1 << 6);
193 if (cbp) {
194 flag = bitstream_read_bit(bc);
196 cbp |= cbp << 4;
197 if (cbp & 0x3)
198 cbp |= 0x500;
199 if (cbp & 0xC)
200 cbp |= 0xA00;
201 for (i = 0; i < 12; i++) {
202 if (!(cbp & (1 << i)))
203 continue;
204 ret = hq_decode_block(c, bc, c->block[i], qgroup, i >= 8, 1);
205 if (ret < 0)
206 return ret;
210 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
211 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
212 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
213 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
214 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
215 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
217 return 0;
220 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, BitstreamContext *bc,
221 int quant, int slice_no, int w, int h)
223 int i, j, off;
224 int ret;
226 for (i = 0; i < h; i += 16) {
227 off = (slice_no * 16 + i * 3) & 0x70;
228 for (j = off; j < w; j += 128) {
229 ret = hqa_decode_mb(ctx, pic, quant, bc, j, i);
230 if (ret < 0) {
231 av_log(ctx->avctx, AV_LOG_ERROR,
232 "Error decoding macroblock at %dx%d.\n", i, j);
233 return ret;
238 return 0;
241 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
243 BitstreamContext bc;
244 const int num_slices = 8;
245 uint32_t slice_off[9];
246 int i, slice, ret;
247 int width, height, quant;
248 const uint8_t *src = ctx->gbc.buffer;
250 width = bytestream2_get_be16(&ctx->gbc);
251 height = bytestream2_get_be16(&ctx->gbc);
253 ctx->avctx->coded_width = FFALIGN(width, 16);
254 ctx->avctx->coded_height = FFALIGN(height, 16);
255 ctx->avctx->width = width;
256 ctx->avctx->height = height;
257 ctx->avctx->bits_per_raw_sample = 8;
258 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
260 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
262 quant = bytestream2_get_byte(&ctx->gbc);
263 bytestream2_skip(&ctx->gbc, 3);
264 if (quant >= NUM_HQ_QUANTS) {
265 av_log(ctx->avctx, AV_LOG_ERROR,
266 "Invalid quantization matrix %d.\n", quant);
267 return AVERROR_INVALIDDATA;
270 ret = ff_get_buffer(ctx->avctx, pic, 0);
271 if (ret < 0) {
272 av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
273 return ret;
276 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
277 for (i = 0; i < num_slices + 1; i++)
278 slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
280 for (slice = 0; slice < num_slices; slice++) {
281 if (slice_off[slice] < (num_slices + 1) * 3 ||
282 slice_off[slice] >= slice_off[slice + 1] ||
283 slice_off[slice + 1] > data_size) {
284 av_log(ctx->avctx, AV_LOG_ERROR,
285 "Invalid slice size %zu.\n", data_size);
286 break;
288 bitstream_init8(&bc, src + slice_off[slice],
289 slice_off[slice + 1] - slice_off[slice]);
291 ret = hqa_decode_slice(ctx, pic, &bc, quant, slice, width, height);
292 if (ret < 0)
293 return ret;
296 return 0;
299 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
300 int *got_frame, AVPacket *avpkt)
302 HQContext *ctx = avctx->priv_data;
303 AVFrame *pic = data;
304 uint32_t info_tag;
305 unsigned int data_size;
306 int tag, ret;
308 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
309 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
310 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
311 return AVERROR_INVALIDDATA;
314 info_tag = bytestream2_peek_le32(&ctx->gbc);
315 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
316 int info_size;
317 bytestream2_skip(&ctx->gbc, 4);
318 info_size = bytestream2_get_le32(&ctx->gbc);
319 if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
320 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
321 return AVERROR_INVALIDDATA;
323 ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
325 bytestream2_skip(&ctx->gbc, info_size);
328 data_size = bytestream2_get_bytes_left(&ctx->gbc);
329 if (data_size < 4) {
330 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
331 return AVERROR_INVALIDDATA;
334 /* HQ defines dimensions and number of slices, and thus slice traversal
335 * order. HQA has no size constraint and a fixed number of slices, so it
336 * needs a separate scheme for it. */
337 tag = bytestream2_get_le32(&ctx->gbc);
338 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
339 ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
340 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
341 ret = hqa_decode_frame(ctx, pic, data_size);
342 } else {
343 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
344 return AVERROR_INVALIDDATA;
346 if (ret < 0) {
347 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
348 return ret;
351 pic->key_frame = 1;
352 pic->pict_type = AV_PICTURE_TYPE_I;
354 *got_frame = 1;
356 return avpkt->size;
359 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
361 HQContext *ctx = avctx->priv_data;
362 ctx->avctx = avctx;
364 ff_hqdsp_init(&ctx->hqhqadsp);
366 return ff_hq_init_vlcs(ctx);
369 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
371 HQContext *ctx = avctx->priv_data;
373 ff_free_vlc(&ctx->hq_ac_vlc);
374 ff_free_vlc(&ctx->hqa_cbp_vlc);
376 return 0;
379 AVCodec ff_hq_hqa_decoder = {
380 .name = "hq_hqa",
381 .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
382 .type = AVMEDIA_TYPE_VIDEO,
383 .id = AV_CODEC_ID_HQ_HQA,
384 .priv_data_size = sizeof(HQContext),
385 .init = hq_hqa_decode_init,
386 .decode = hq_hqa_decode_frame,
387 .close = hq_hqa_decode_close,
388 .capabilities = AV_CODEC_CAP_DR1,
389 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
390 FF_CODEC_CAP_INIT_CLEANUP,