aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / magicyuv.c
blob027143fc875ec795977139d62188871d97757db4
1 /*
2 * MagicYUV decoder
3 * Copyright (c) 2016 Paul B Mahol
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <stdlib.h>
23 #include <string.h>
25 #include "libavutil/pixdesc.h"
27 #include "avcodec.h"
28 #include "bitstream.h"
29 #include "bytestream.h"
30 #include "huffyuvdsp.h"
31 #include "internal.h"
32 #include "thread.h"
33 #include "vlc.h"
35 typedef struct Slice {
36 uint32_t start;
37 uint32_t size;
38 } Slice;
40 typedef enum Prediction {
41 LEFT = 1,
42 GRADIENT,
43 MEDIAN,
44 } Prediction;
46 typedef struct HuffEntry {
47 uint8_t sym;
48 uint8_t len;
49 uint32_t code;
50 } HuffEntry;
52 typedef struct MagicYUVContext {
53 AVFrame *p;
54 int slice_height;
55 int nb_slices;
56 int planes; // number of encoded planes in bitstream
57 int decorrelate; // postprocessing work
58 int interlaced; // video is interlaced
59 uint8_t *buf; // pointer to AVPacket->data
60 int hshift[4];
61 int vshift[4];
62 Slice *slices[4]; // slice bitstream positions for each plane
63 unsigned int slices_size[4]; // slice sizes for each plane
64 uint8_t len[4][256]; // table of code lengths for each plane
65 VLC vlc[4]; // VLC for each plane
66 HuffYUVDSPContext hdsp;
67 } MagicYUVContext;
69 static int huff_cmp_len(const void *a, const void *b)
71 const HuffEntry *aa = a, *bb = b;
72 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
75 static int huff_build(VLC *vlc, uint8_t *len)
77 HuffEntry he[256];
78 uint32_t codes[256];
79 uint8_t bits[256];
80 uint8_t syms[256];
81 uint32_t code;
82 int i;
84 for (i = 0; i < 256; i++) {
85 he[i].sym = 255 - i;
86 he[i].len = len[i];
88 qsort(he, 256, sizeof(HuffEntry), huff_cmp_len);
90 code = 1;
91 for (i = 255; i >= 0; i--) {
92 codes[i] = code >> (32 - he[i].len);
93 bits[i] = he[i].len;
94 syms[i] = he[i].sym;
95 code += 0x80000000u >> (he[i].len - 1);
98 ff_free_vlc(vlc);
99 return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
100 bits, sizeof(*bits), sizeof(*bits),
101 codes, sizeof(*codes), sizeof(*codes),
102 syms, sizeof(*syms), sizeof(*syms), 0);
105 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
106 int j, int threadnr)
108 MagicYUVContext *s = avctx->priv_data;
109 int interlaced = s->interlaced;
110 AVFrame *p = s->p;
111 int i, k, x;
112 BitstreamContext bc;
113 uint8_t *dst;
115 for (i = 0; i < s->planes; i++) {
116 int left, lefttop, top;
117 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->height - j * s->slice_height), s->vshift[i]);
118 int width = AV_CEIL_RSHIFT(avctx->width, s->hshift[i]);
119 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
120 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
121 ptrdiff_t stride = p->linesize[i];
122 int flags, pred;
123 int ret = bitstream_init8(&bc, s->buf + s->slices[i][j].start,
124 s->slices[i][j].size);
126 if (ret < 0)
127 return ret;
129 flags = bitstream_read(&bc, 8);
130 pred = bitstream_read(&bc, 8);
132 dst = p->data[i] + j * sheight * stride;
133 if (flags & 1) {
134 for (k = 0; k < height; k++) {
135 for (x = 0; x < width; x++)
136 dst[x] = bitstream_read(&bc, 8);
138 dst += stride;
140 } else {
141 for (k = 0; k < height; k++) {
142 for (x = 0; x < width; x++) {
143 int pix;
144 if (bitstream_bits_left(&bc) <= 0)
145 return AVERROR_INVALIDDATA;
147 pix = bitstream_read_vlc(&bc, s->vlc[i].table, s->vlc[i].bits, 3);
148 if (pix < 0)
149 return AVERROR_INVALIDDATA;
151 dst[x] = 255 - pix;
153 dst += stride;
157 switch (pred) {
158 case LEFT:
159 dst = p->data[i] + j * sheight * stride;
160 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
161 dst += stride;
162 if (interlaced) {
163 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
164 dst += stride;
166 for (k = 1 + interlaced; k < height; k++) {
167 s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);
168 dst += stride;
170 break;
171 case GRADIENT:
172 dst = p->data[i] + j * sheight * stride;
173 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
174 left = lefttop = 0;
175 dst += stride;
176 if (interlaced) {
177 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
178 left = lefttop = 0;
179 dst += stride;
181 for (k = 1 + interlaced; k < height; k++) {
182 top = dst[-fake_stride];
183 left = top + dst[0];
184 dst[0] = left;
185 for (x = 1; x < width; x++) {
186 top = dst[x - fake_stride];
187 lefttop = dst[x - (fake_stride + 1)];
188 left += top - lefttop + dst[x];
189 dst[x] = left;
191 dst += stride;
193 break;
194 case MEDIAN:
195 dst = p->data[i] + j * sheight * stride;
196 lefttop = left = dst[0];
197 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
198 dst += stride;
199 if (interlaced) {
200 lefttop = left = dst[0];
201 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
202 dst += stride;
204 for (k = 1 + interlaced; k < height; k++) {
205 s->hdsp.add_hfyu_median_pred(dst, dst - fake_stride,
206 dst, width, &left, &lefttop);
207 lefttop = left = dst[0];
208 dst += stride;
210 break;
211 default:
212 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
216 if (s->decorrelate) {
217 int height = FFMIN(s->slice_height, avctx->height - j * s->slice_height);
218 int width = avctx->width;
219 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
220 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
221 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
223 for (i = 0; i < height; i++) {
224 s->hdsp.add_bytes(b, g, width);
225 s->hdsp.add_bytes(r, g, width);
226 b += p->linesize[0];
227 g += p->linesize[1];
228 r += p->linesize[2];
232 return 0;
235 static int magy_decode_frame(AVCodecContext *avctx, void *data,
236 int *got_frame, AVPacket *avpkt)
238 MagicYUVContext *s = avctx->priv_data;
239 ThreadFrame frame = { .f = data };
240 AVFrame *p = data;
241 GetByteContext gbyte;
242 BitstreamContext bc;
243 uint32_t first_offset, offset, next_offset, header_size, slice_width;
244 int width, height, format, version, table_size;
245 int ret, i, j, k;
247 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
248 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
249 return AVERROR_INVALIDDATA;
251 header_size = bytestream2_get_le32(&gbyte);
252 if (header_size < 32 || header_size >= avpkt->size) {
253 av_log(avctx, AV_LOG_ERROR,
254 "header or packet too small %"PRIu32"\n", header_size);
255 return AVERROR_INVALIDDATA;
258 version = bytestream2_get_byte(&gbyte);
259 if (version != 7) {
260 avpriv_request_sample(avctx, "Version %d", version);
261 return AVERROR_PATCHWELCOME;
264 s->hshift[1] =
265 s->vshift[1] =
266 s->hshift[2] =
267 s->vshift[2] = 0;
268 s->decorrelate = 0;
270 format = bytestream2_get_byte(&gbyte);
271 switch (format) {
272 case 0x65:
273 avctx->pix_fmt = AV_PIX_FMT_GBRP;
274 s->decorrelate = 1;
275 break;
276 case 0x66:
277 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
278 s->decorrelate = 1;
279 break;
280 case 0x67:
281 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
282 break;
283 case 0x68:
284 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
285 s->hshift[1] =
286 s->hshift[2] = 1;
287 break;
288 case 0x69:
289 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
290 s->hshift[1] =
291 s->vshift[1] =
292 s->hshift[2] =
293 s->vshift[2] = 1;
294 break;
295 case 0x6a:
296 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
297 break;
298 case 0x6b:
299 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
300 break;
301 default:
302 avpriv_request_sample(avctx, "Format 0x%X", format);
303 return AVERROR_PATCHWELCOME;
305 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
307 bytestream2_skip(&gbyte, 2);
308 s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);
309 bytestream2_skip(&gbyte, 3);
311 width = bytestream2_get_le32(&gbyte);
312 height = bytestream2_get_le32(&gbyte);
313 ret = ff_set_dimensions(avctx, width, height);
314 if (ret < 0)
315 return ret;
317 slice_width = bytestream2_get_le32(&gbyte);
318 if (slice_width != width) {
319 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
320 return AVERROR_PATCHWELCOME;
322 s->slice_height = bytestream2_get_le32(&gbyte);
323 if (s->slice_height <= 0 || s->slice_height > INT_MAX - height) {
324 av_log(avctx, AV_LOG_ERROR,
325 "invalid slice height: %d\n", s->slice_height);
326 return AVERROR_INVALIDDATA;
329 bytestream2_skip(&gbyte, 4);
331 s->nb_slices = (height + s->slice_height - 1) / s->slice_height;
332 if (s->nb_slices > INT_MAX / sizeof(Slice)) {
333 av_log(avctx, AV_LOG_ERROR,
334 "invalid number of slices: %d\n", s->nb_slices);
335 return AVERROR_INVALIDDATA;
338 for (i = 0; i < s->planes; i++) {
339 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
340 if (!s->slices[i])
341 return AVERROR(ENOMEM);
343 offset = bytestream2_get_le32(&gbyte);
344 if (offset >= avpkt->size - header_size)
345 return AVERROR_INVALIDDATA;
347 if (i == 0)
348 first_offset = offset;
350 for (j = 0; j < s->nb_slices - 1; j++) {
351 s->slices[i][j].start = offset + header_size;
353 next_offset = bytestream2_get_le32(&gbyte);
354 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
355 return AVERROR_INVALIDDATA;
357 s->slices[i][j].size = next_offset - offset;
358 offset = next_offset;
361 s->slices[i][j].start = offset + header_size;
362 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
365 if (bytestream2_get_byte(&gbyte) != s->planes)
366 return AVERROR_INVALIDDATA;
368 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
370 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
371 if (table_size < 2)
372 return AVERROR_INVALIDDATA;
374 ret = bitstream_init8(&bc, avpkt->data + bytestream2_tell(&gbyte), table_size);
375 if (ret < 0)
376 return ret;
378 memset(s->len, 0, sizeof(s->len));
379 j = i = 0;
380 while (bitstream_bits_left(&bc) >= 8) {
381 int b = bitstream_read(&bc, 4);
382 int x = bitstream_read(&bc, 4);
383 int l = bitstream_read(&bc, b) + 1;
385 for (k = 0; k < l; k++)
386 if (j + k < 256)
387 s->len[i][j + k] = x;
389 j += l;
390 if (j == 256) {
391 j = 0;
392 if (huff_build(&s->vlc[i], s->len[i])) {
393 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
394 return AVERROR_INVALIDDATA;
396 i++;
397 if (i == s->planes) {
398 break;
400 } else if (j > 256) {
401 return AVERROR_INVALIDDATA;
405 if (i != s->planes) {
406 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
407 return AVERROR_INVALIDDATA;
410 p->pict_type = AV_PICTURE_TYPE_I;
411 p->key_frame = 1;
413 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
414 return ret;
416 s->buf = avpkt->data;
417 s->p = p;
418 avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);
420 if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
421 avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
422 FFSWAP(uint8_t*, p->data[0], p->data[1]);
423 FFSWAP(int, p->linesize[0], p->linesize[1]);
426 *got_frame = 1;
428 return avpkt->size;
431 #if HAVE_THREADS
432 static int magy_init_thread_copy(AVCodecContext *avctx)
434 MagicYUVContext *s = avctx->priv_data;
435 int i;
437 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
438 s->slices[i] = NULL;
439 s->slices_size[i] = 0;
442 return 0;
444 #endif
446 static av_cold int magy_decode_init(AVCodecContext *avctx)
448 MagicYUVContext *s = avctx->priv_data;
449 ff_huffyuvdsp_init(&s->hdsp);
450 return 0;
453 static av_cold int magy_decode_end(AVCodecContext *avctx)
455 MagicYUVContext * const s = avctx->priv_data;
456 int i;
458 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
459 av_freep(&s->slices[i]);
460 s->slices_size[i] = 0;
461 ff_free_vlc(&s->vlc[i]);
464 return 0;
467 AVCodec ff_magicyuv_decoder = {
468 .name = "magicyuv",
469 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
470 .type = AVMEDIA_TYPE_VIDEO,
471 .id = AV_CODEC_ID_MAGICYUV,
472 .priv_data_size = sizeof(MagicYUVContext),
473 .init = magy_decode_init,
474 .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
475 .close = magy_decode_end,
476 .decode = magy_decode_frame,
477 .capabilities = AV_CODEC_CAP_DR1 |
478 AV_CODEC_CAP_FRAME_THREADS |
479 AV_CODEC_CAP_SLICE_THREADS,
480 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,