aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / cllc.c
blob3c476f71f0014219d22f4655aef2c9a114ab4ab9
1 /*
2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012-2013 Derek Buitenhuis
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <inttypes.h>
25 #include "libavutil/intreadwrite.h"
27 #include "bitstream.h"
28 #include "bswapdsp.h"
29 #include "canopus.h"
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "vlc.h"
34 typedef struct CLLCContext {
35 AVCodecContext *avctx;
36 BswapDSPContext bdsp;
38 uint8_t *swapped_buf;
39 int swapped_buf_size;
40 } CLLCContext;
42 static int read_code_table(CLLCContext *ctx, BitstreamContext *bc, VLC *vlc)
44 uint8_t symbols[256];
45 uint8_t bits[256];
46 uint16_t codes[256];
47 int num_lens, num_codes, num_codes_sum, prefix;
48 int i, j, count;
50 prefix = 0;
51 count = 0;
52 num_codes_sum = 0;
54 num_lens = bitstream_read(bc, 5);
56 for (i = 0; i < num_lens; i++) {
57 num_codes = bitstream_read(bc, 9);
58 num_codes_sum += num_codes;
60 if (num_codes_sum > 256) {
61 vlc->table = NULL;
63 av_log(ctx->avctx, AV_LOG_ERROR,
64 "Too many VLCs (%d) to be read.\n", num_codes_sum);
65 return AVERROR_INVALIDDATA;
68 for (j = 0; j < num_codes; j++) {
69 symbols[count] = bitstream_read(bc, 8);
70 bits[count] = i + 1;
71 codes[count] = prefix++;
73 count++;
76 prefix <<= 1;
79 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
80 codes, 2, 2, symbols, 1, 1, 0);
84 * Unlike the RGB24 read/restore, which reads in a component at a time,
85 * ARGB read/restore reads in ARGB quads.
87 static int read_argb_line(CLLCContext *ctx, BitstreamContext *bc, int *top_left,
88 VLC *vlc, uint8_t *outbuf)
90 uint8_t *dst;
91 int pred[4];
92 int code;
93 int i;
95 dst = outbuf;
96 pred[0] = top_left[0];
97 pred[1] = top_left[1];
98 pred[2] = top_left[2];
99 pred[3] = top_left[3];
101 for (i = 0; i < ctx->avctx->width; i++) {
102 /* Always get the alpha component */
103 code = bitstream_read_vlc(bc, vlc[0].table, 7, 2);
105 pred[0] += code;
106 dst[0] = pred[0];
108 /* Skip the components if they are entirely transparent */
109 if (dst[0]) {
110 /* Red */
111 code = bitstream_read_vlc(bc, vlc[1].table, 7, 2);
113 pred[1] += code;
114 dst[1] = pred[1];
116 /* Green */
117 code = bitstream_read_vlc(bc, vlc[2].table, 7, 2);
119 pred[2] += code;
120 dst[2] = pred[2];
122 /* Blue */
123 code = bitstream_read_vlc(bc, vlc[3].table, 7, 2);
125 pred[3] += code;
126 dst[3] = pred[3];
127 } else {
128 dst[1] = 0;
129 dst[2] = 0;
130 dst[3] = 0;
133 dst += 4;
136 top_left[0] = outbuf[0];
138 /* Only stash components if they are not transparent */
139 if (top_left[0]) {
140 top_left[1] = outbuf[1];
141 top_left[2] = outbuf[2];
142 top_left[3] = outbuf[3];
145 return 0;
148 static int read_rgb24_component_line(CLLCContext *ctx, BitstreamContext *bc,
149 int *top_left, VLC *vlc, uint8_t *outbuf)
151 uint8_t *dst;
152 int pred, code;
153 int i;
155 dst = outbuf;
156 pred = *top_left;
158 /* Simultaneously read and restore the line */
159 for (i = 0; i < ctx->avctx->width; i++) {
160 code = bitstream_read_vlc(bc, vlc->table, 7, 2);
162 pred += code;
163 dst[0] = pred;
164 dst += 3;
167 /* Stash the first pixel */
168 *top_left = outbuf[0];
170 return 0;
173 static int read_yuv_component_line(CLLCContext *ctx, BitstreamContext *bc,
174 int *top_left, VLC *vlc, uint8_t *outbuf,
175 int is_chroma)
177 int pred, code;
178 int i;
180 pred = *top_left;
182 /* Simultaneously read and restore the line */
183 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
184 code = bitstream_read_vlc(bc, vlc->table, 7, 2);
186 pred += code;
187 outbuf[i] = pred;
190 /* Stash the first pixel */
191 *top_left = outbuf[0];
193 return 0;
196 static int decode_argb_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic)
198 AVCodecContext *avctx = ctx->avctx;
199 uint8_t *dst;
200 int pred[4];
201 int ret;
202 int i, j;
203 VLC vlc[4];
205 pred[0] = 0;
206 pred[1] = 0x80;
207 pred[2] = 0x80;
208 pred[3] = 0x80;
210 dst = pic->data[0];
212 bitstream_skip(bc, 16);
214 /* Read in code table for each plane */
215 for (i = 0; i < 4; i++) {
216 ret = read_code_table(ctx, bc, &vlc[i]);
217 if (ret < 0) {
218 for (j = 0; j <= i; j++)
219 ff_free_vlc(&vlc[j]);
221 av_log(ctx->avctx, AV_LOG_ERROR,
222 "Could not read code table %d.\n", i);
223 return ret;
227 /* Read in and restore every line */
228 for (i = 0; i < avctx->height; i++) {
229 read_argb_line(ctx, bc, pred, vlc, dst);
231 dst += pic->linesize[0];
234 for (i = 0; i < 4; i++)
235 ff_free_vlc(&vlc[i]);
237 return 0;
240 static int decode_rgb24_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic)
242 AVCodecContext *avctx = ctx->avctx;
243 uint8_t *dst;
244 int pred[3];
245 int ret;
246 int i, j;
247 VLC vlc[3];
249 pred[0] = 0x80;
250 pred[1] = 0x80;
251 pred[2] = 0x80;
253 dst = pic->data[0];
255 bitstream_skip(bc, 16);
257 /* Read in code table for each plane */
258 for (i = 0; i < 3; i++) {
259 ret = read_code_table(ctx, bc, &vlc[i]);
260 if (ret < 0) {
261 for (j = 0; j <= i; j++)
262 ff_free_vlc(&vlc[j]);
264 av_log(ctx->avctx, AV_LOG_ERROR,
265 "Could not read code table %d.\n", i);
266 return ret;
270 /* Read in and restore every line */
271 for (i = 0; i < avctx->height; i++) {
272 for (j = 0; j < 3; j++)
273 read_rgb24_component_line(ctx, bc, &pred[j], &vlc[j], &dst[j]);
275 dst += pic->linesize[0];
278 for (i = 0; i < 3; i++)
279 ff_free_vlc(&vlc[i]);
281 return 0;
284 static int decode_yuv_frame(CLLCContext *ctx, BitstreamContext *bc, AVFrame *pic)
286 AVCodecContext *avctx = ctx->avctx;
287 uint8_t block;
288 uint8_t *dst[3];
289 int pred[3];
290 int ret;
291 int i, j;
292 VLC vlc[2];
294 pred[0] = 0x80;
295 pred[1] = 0x80;
296 pred[2] = 0x80;
298 dst[0] = pic->data[0];
299 dst[1] = pic->data[1];
300 dst[2] = pic->data[2];
302 bitstream_skip(bc, 8);
304 block = bitstream_read(bc, 8);
305 if (block) {
306 avpriv_request_sample(ctx->avctx, "Blocked YUV");
307 return AVERROR_PATCHWELCOME;
310 /* Read in code table for luma and chroma */
311 for (i = 0; i < 2; i++) {
312 ret = read_code_table(ctx, bc, &vlc[i]);
313 if (ret < 0) {
314 for (j = 0; j <= i; j++)
315 ff_free_vlc(&vlc[j]);
317 av_log(ctx->avctx, AV_LOG_ERROR,
318 "Could not read code table %d.\n", i);
319 return ret;
323 /* Read in and restore every line */
324 for (i = 0; i < avctx->height; i++) {
325 read_yuv_component_line(ctx, bc, &pred[0], &vlc[0], dst[0], 0); /* Y */
326 read_yuv_component_line(ctx, bc, &pred[1], &vlc[1], dst[1], 1); /* U */
327 read_yuv_component_line(ctx, bc, &pred[2], &vlc[1], dst[2], 1); /* V */
329 for (j = 0; j < 3; j++)
330 dst[j] += pic->linesize[j];
333 for (i = 0; i < 2; i++)
334 ff_free_vlc(&vlc[i]);
336 return 0;
339 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
340 int *got_picture_ptr, AVPacket *avpkt)
342 CLLCContext *ctx = avctx->priv_data;
343 AVFrame *pic = data;
344 uint8_t *src = avpkt->data;
345 uint32_t info_tag, info_offset;
346 int data_size;
347 BitstreamContext bc;
348 int coding_type, ret;
350 if (avpkt->size < 4 + 4) {
351 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
352 return AVERROR_INVALIDDATA;
355 info_offset = 0;
356 info_tag = AV_RL32(src);
357 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
358 info_offset = AV_RL32(src + 4);
359 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
360 av_log(avctx, AV_LOG_ERROR,
361 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
362 info_offset);
363 return AVERROR_INVALIDDATA;
365 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
367 info_offset += 8;
368 src += info_offset;
371 data_size = (avpkt->size - info_offset) & ~1;
373 /* Make sure our bswap16'd buffer is big enough */
374 av_fast_padded_malloc(&ctx->swapped_buf,
375 &ctx->swapped_buf_size, data_size);
376 if (!ctx->swapped_buf) {
377 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
378 return AVERROR(ENOMEM);
381 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
382 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
383 data_size / 2);
385 bitstream_init8(&bc, ctx->swapped_buf, data_size);
388 * Read in coding type. The types are as follows:
390 * 0 - YUY2
391 * 1 - BGR24 (Triples)
392 * 2 - BGR24 (Quads)
393 * 3 - BGRA
395 coding_type = (AV_RL32(src) >> 8) & 0xFF;
396 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
398 switch (coding_type) {
399 case 0:
400 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
401 avctx->bits_per_raw_sample = 8;
403 ret = ff_get_buffer(avctx, pic, 0);
404 if (ret < 0) {
405 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
406 return ret;
409 ret = decode_yuv_frame(ctx, &bc, pic);
410 if (ret < 0)
411 return ret;
413 break;
414 case 1:
415 case 2:
416 avctx->pix_fmt = AV_PIX_FMT_RGB24;
417 avctx->bits_per_raw_sample = 8;
419 ret = ff_get_buffer(avctx, pic, 0);
420 if (ret < 0) {
421 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
422 return ret;
425 ret = decode_rgb24_frame(ctx, &bc, pic);
426 if (ret < 0)
427 return ret;
429 break;
430 case 3:
431 avctx->pix_fmt = AV_PIX_FMT_ARGB;
432 avctx->bits_per_raw_sample = 8;
434 ret = ff_get_buffer(avctx, pic, 0);
435 if (ret < 0) {
436 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
437 return ret;
440 ret = decode_argb_frame(ctx, &bc, pic);
441 if (ret < 0)
442 return ret;
444 break;
445 default:
446 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
447 return AVERROR_INVALIDDATA;
450 pic->key_frame = 1;
451 pic->pict_type = AV_PICTURE_TYPE_I;
453 *got_picture_ptr = 1;
455 return avpkt->size;
458 static av_cold int cllc_decode_close(AVCodecContext *avctx)
460 CLLCContext *ctx = avctx->priv_data;
462 av_freep(&ctx->swapped_buf);
464 return 0;
467 static av_cold int cllc_decode_init(AVCodecContext *avctx)
469 CLLCContext *ctx = avctx->priv_data;
471 /* Initialize various context values */
472 ctx->avctx = avctx;
473 ctx->swapped_buf = NULL;
474 ctx->swapped_buf_size = 0;
476 ff_bswapdsp_init(&ctx->bdsp);
478 return 0;
481 AVCodec ff_cllc_decoder = {
482 .name = "cllc",
483 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
484 .type = AVMEDIA_TYPE_VIDEO,
485 .id = AV_CODEC_ID_CLLC,
486 .priv_data_size = sizeof(CLLCContext),
487 .init = cllc_decode_init,
488 .decode = cllc_decode_frame,
489 .close = cllc_decode_close,
490 .capabilities = AV_CODEC_CAP_DR1,
491 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,