Add MagicYUV decoder
[libav.git] / libavcodec / magicyuv.c
1 /*
2 * MagicYUV decoder
3 * Copyright (c) 2016 Paul B Mahol
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "../libavutil/pixdesc.h"
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "get_bits.h"
30 #include "huffyuvdsp.h"
31 #include "internal.h"
32 #include "thread.h"
33
34 typedef struct Slice {
35 uint32_t start;
36 uint32_t size;
37 } Slice;
38
39 typedef enum Prediction {
40 LEFT = 1,
41 GRADIENT,
42 MEDIAN,
43 } Prediction;
44
45 typedef struct HuffEntry {
46 uint8_t sym;
47 uint8_t len;
48 uint32_t code;
49 } HuffEntry;
50
51 typedef struct MagicYUVContext {
52 AVFrame *p;
53 int slice_height;
54 int nb_slices;
55 int planes; // number of encoded planes in bitstream
56 int decorrelate; // postprocessing work
57 int interlaced; // video is interlaced
58 uint8_t *buf; // pointer to AVPacket->data
59 int hshift[4];
60 int vshift[4];
61 Slice *slices[4]; // slice bitstream positions for each plane
62 unsigned int slices_size[4]; // slice sizes for each plane
63 uint8_t len[4][256]; // table of code lengths for each plane
64 VLC vlc[4]; // VLC for each plane
65 HuffYUVDSPContext hdsp;
66 } MagicYUVContext;
67
68 static int huff_cmp_len(const void *a, const void *b)
69 {
70 const HuffEntry *aa = a, *bb = b;
71 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
72 }
73
74 static int huff_build(VLC *vlc, uint8_t *len)
75 {
76 HuffEntry he[256];
77 uint32_t codes[256];
78 uint8_t bits[256];
79 uint8_t syms[256];
80 uint32_t code;
81 int i;
82
83 for (i = 0; i < 256; i++) {
84 he[i].sym = 255 - i;
85 he[i].len = len[i];
86 }
87 qsort(he, 256, sizeof(HuffEntry), huff_cmp_len);
88
89 code = 1;
90 for (i = 255; i >= 0; i--) {
91 codes[i] = code >> (32 - he[i].len);
92 bits[i] = he[i].len;
93 syms[i] = he[i].sym;
94 code += 0x80000000u >> (he[i].len - 1);
95 }
96
97 ff_free_vlc(vlc);
98 return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
99 bits, sizeof(*bits), sizeof(*bits),
100 codes, sizeof(*codes), sizeof(*codes),
101 syms, sizeof(*syms), sizeof(*syms), 0);
102 }
103
104 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
105 int j, int threadnr)
106 {
107 MagicYUVContext *s = avctx->priv_data;
108 int interlaced = s->interlaced;
109 AVFrame *p = s->p;
110 int i, k, x;
111 GetBitContext gb;
112 uint8_t *dst;
113
114 for (i = 0; i < s->planes; i++) {
115 int left, lefttop, top;
116 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->height - j * s->slice_height), s->vshift[i]);
117 int width = AV_CEIL_RSHIFT(avctx->width, s->hshift[i]);
118 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
119 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
120 ptrdiff_t stride = p->linesize[i];
121 int flags, pred;
122 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
123 s->slices[i][j].size);
124
125 if (ret < 0)
126 return ret;
127
128 flags = get_bits(&gb, 8);
129 pred = get_bits(&gb, 8);
130
131 dst = p->data[i] + j * sheight * stride;
132 if (flags & 1) {
133 for (k = 0; k < height; k++) {
134 for (x = 0; x < width; x++)
135 dst[x] = get_bits(&gb, 8);
136
137 dst += stride;
138 }
139 } else {
140 for (k = 0; k < height; k++) {
141 for (x = 0; x < width; x++) {
142 int pix;
143 if (get_bits_left(&gb) <= 0)
144 return AVERROR_INVALIDDATA;
145
146 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
147 if (pix < 0)
148 return AVERROR_INVALIDDATA;
149
150 dst[x] = 255 - pix;
151 }
152 dst += stride;
153 }
154 }
155
156 switch (pred) {
157 case LEFT:
158 dst = p->data[i] + j * sheight * stride;
159 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
160 dst += stride;
161 if (interlaced) {
162 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
163 dst += stride;
164 }
165 for (k = 1 + interlaced; k < height; k++) {
166 s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);
167 dst += stride;
168 }
169 break;
170 case GRADIENT:
171 dst = p->data[i] + j * sheight * stride;
172 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
173 left = lefttop = 0;
174 dst += stride;
175 if (interlaced) {
176 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
177 left = lefttop = 0;
178 dst += stride;
179 }
180 for (k = 1 + interlaced; k < height; k++) {
181 top = dst[-fake_stride];
182 left = top + dst[0];
183 dst[0] = left;
184 for (x = 1; x < width; x++) {
185 top = dst[x - fake_stride];
186 lefttop = dst[x - (fake_stride + 1)];
187 left += top - lefttop + dst[x];
188 dst[x] = left;
189 }
190 dst += stride;
191 }
192 break;
193 case MEDIAN:
194 dst = p->data[i] + j * sheight * stride;
195 lefttop = left = dst[0];
196 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
197 dst += stride;
198 if (interlaced) {
199 lefttop = left = dst[0];
200 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
201 dst += stride;
202 }
203 for (k = 1 + interlaced; k < height; k++) {
204 s->hdsp.add_hfyu_median_pred(dst, dst - fake_stride,
205 dst, width, &left, &lefttop);
206 lefttop = left = dst[0];
207 dst += stride;
208 }
209 break;
210 default:
211 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
212 }
213 }
214
215 if (s->decorrelate) {
216 int height = FFMIN(s->slice_height, avctx->height - j * s->slice_height);
217 int width = avctx->width;
218 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
219 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
220 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
221
222 for (i = 0; i < height; i++) {
223 s->hdsp.add_bytes(b, g, width);
224 s->hdsp.add_bytes(r, g, width);
225 b += p->linesize[0];
226 g += p->linesize[1];
227 r += p->linesize[2];
228 }
229 }
230
231 return 0;
232 }
233
234 static int magy_decode_frame(AVCodecContext *avctx, void *data,
235 int *got_frame, AVPacket *avpkt)
236 {
237 MagicYUVContext *s = avctx->priv_data;
238 ThreadFrame frame = { .f = data };
239 AVFrame *p = data;
240 GetByteContext gbyte;
241 GetBitContext gbit;
242 uint32_t first_offset, offset, next_offset, header_size, slice_width;
243 int width, height, format, version, table_size;
244 int ret, i, j, k;
245
246 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
247 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
248 return AVERROR_INVALIDDATA;
249
250 header_size = bytestream2_get_le32(&gbyte);
251 if (header_size < 32 || header_size >= avpkt->size) {
252 av_log(avctx, AV_LOG_ERROR,
253 "header or packet too small %"PRIu32"\n", header_size);
254 return AVERROR_INVALIDDATA;
255 }
256
257 version = bytestream2_get_byte(&gbyte);
258 if (version != 7) {
259 avpriv_request_sample(avctx, "Version %d", version);
260 return AVERROR_PATCHWELCOME;
261 }
262
263 s->hshift[1] =
264 s->vshift[1] =
265 s->hshift[2] =
266 s->vshift[2] = 0;
267 s->decorrelate = 0;
268
269 format = bytestream2_get_byte(&gbyte);
270 switch (format) {
271 case 0x65:
272 avctx->pix_fmt = AV_PIX_FMT_GBRP;
273 s->decorrelate = 1;
274 break;
275 case 0x66:
276 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
277 s->decorrelate = 1;
278 break;
279 case 0x67:
280 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
281 break;
282 case 0x68:
283 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
284 s->hshift[1] =
285 s->hshift[2] = 1;
286 break;
287 case 0x69:
288 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
289 s->hshift[1] =
290 s->vshift[1] =
291 s->hshift[2] =
292 s->vshift[2] = 1;
293 break;
294 case 0x6a:
295 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
296 break;
297 case 0x6b:
298 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
299 break;
300 default:
301 avpriv_request_sample(avctx, "Format 0x%X", format);
302 return AVERROR_PATCHWELCOME;
303 }
304 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
305
306 bytestream2_skip(&gbyte, 2);
307 s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);
308 bytestream2_skip(&gbyte, 3);
309
310 width = bytestream2_get_le32(&gbyte);
311 height = bytestream2_get_le32(&gbyte);
312 ret = ff_set_dimensions(avctx, width, height);
313 if (ret < 0)
314 return ret;
315
316 slice_width = bytestream2_get_le32(&gbyte);
317 if (slice_width != width) {
318 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
319 return AVERROR_PATCHWELCOME;
320 }
321 s->slice_height = bytestream2_get_le32(&gbyte);
322 if (s->slice_height <= 0 || s->slice_height > INT_MAX - height) {
323 av_log(avctx, AV_LOG_ERROR,
324 "invalid slice height: %d\n", s->slice_height);
325 return AVERROR_INVALIDDATA;
326 }
327
328 bytestream2_skip(&gbyte, 4);
329
330 s->nb_slices = (height + s->slice_height - 1) / s->slice_height;
331 if (s->nb_slices > INT_MAX / sizeof(Slice)) {
332 av_log(avctx, AV_LOG_ERROR,
333 "invalid number of slices: %d\n", s->nb_slices);
334 return AVERROR_INVALIDDATA;
335 }
336
337 for (i = 0; i < s->planes; i++) {
338 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
339 if (!s->slices[i])
340 return AVERROR(ENOMEM);
341
342 offset = bytestream2_get_le32(&gbyte);
343 if (offset >= avpkt->size - header_size)
344 return AVERROR_INVALIDDATA;
345
346 if (i == 0)
347 first_offset = offset;
348
349 for (j = 0; j < s->nb_slices - 1; j++) {
350 s->slices[i][j].start = offset + header_size;
351
352 next_offset = bytestream2_get_le32(&gbyte);
353 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
354 return AVERROR_INVALIDDATA;
355
356 s->slices[i][j].size = next_offset - offset;
357 offset = next_offset;
358 }
359
360 s->slices[i][j].start = offset + header_size;
361 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
362 }
363
364 if (bytestream2_get_byte(&gbyte) != s->planes)
365 return AVERROR_INVALIDDATA;
366
367 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
368
369 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
370 if (table_size < 2)
371 return AVERROR_INVALIDDATA;
372
373 ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
374 if (ret < 0)
375 return ret;
376
377 memset(s->len, 0, sizeof(s->len));
378 j = i = 0;
379 while (get_bits_left(&gbit) >= 8) {
380 int b = get_bits(&gbit, 4);
381 int x = get_bits(&gbit, 4);
382 int l = get_bitsz(&gbit, b) + 1;
383
384 for (k = 0; k < l; k++)
385 if (j + k < 256)
386 s->len[i][j + k] = x;
387
388 j += l;
389 if (j == 256) {
390 j = 0;
391 if (huff_build(&s->vlc[i], s->len[i])) {
392 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
393 return AVERROR_INVALIDDATA;
394 }
395 i++;
396 if (i == s->planes) {
397 break;
398 }
399 } else if (j > 256) {
400 return AVERROR_INVALIDDATA;
401 }
402 }
403
404 if (i != s->planes) {
405 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
406 return AVERROR_INVALIDDATA;
407 }
408
409 p->pict_type = AV_PICTURE_TYPE_I;
410 p->key_frame = 1;
411
412 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
413 return ret;
414
415 s->buf = avpkt->data;
416 s->p = p;
417 avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);
418
419 if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
420 avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
421 FFSWAP(uint8_t*, p->data[0], p->data[1]);
422 FFSWAP(int, p->linesize[0], p->linesize[1]);
423 }
424
425 *got_frame = 1;
426
427 return avpkt->size;
428 }
429
430 #if HAVE_THREADS
431 static int magy_init_thread_copy(AVCodecContext *avctx)
432 {
433 MagicYUVContext *s = avctx->priv_data;
434 int i;
435
436 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
437 s->slices[i] = NULL;
438 s->slices_size[i] = 0;
439 }
440
441 return 0;
442 }
443 #endif
444
445 static av_cold int magy_decode_init(AVCodecContext *avctx)
446 {
447 MagicYUVContext *s = avctx->priv_data;
448 ff_huffyuvdsp_init(&s->hdsp);
449 return 0;
450 }
451
452 static av_cold int magy_decode_end(AVCodecContext *avctx)
453 {
454 MagicYUVContext * const s = avctx->priv_data;
455 int i;
456
457 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
458 av_freep(&s->slices[i]);
459 s->slices_size[i] = 0;
460 ff_free_vlc(&s->vlc[i]);
461 }
462
463 return 0;
464 }
465
466 AVCodec ff_magicyuv_decoder = {
467 .name = "magicyuv",
468 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
469 .type = AVMEDIA_TYPE_VIDEO,
470 .id = AV_CODEC_ID_MAGICYUV,
471 .priv_data_size = sizeof(MagicYUVContext),
472 .init = magy_decode_init,
473 .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
474 .close = magy_decode_end,
475 .decode = magy_decode_frame,
476 .capabilities = AV_CODEC_CAP_DR1 |
477 AV_CODEC_CAP_FRAME_THREADS |
478 AV_CODEC_CAP_SLICE_THREADS,
479 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
480 };