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
25 #include "../libavutil/pixdesc.h"
28 #include "bytestream.h"
30 #include "huffyuvdsp.h"
34 typedef struct Slice {
39 typedef enum Prediction {
45 typedef struct HuffEntry {
51 typedef struct MagicYUVContext {
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
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;
68 static int huff_cmp_len(const void *a, const void *b)
70 const HuffEntry *aa = a, *bb = b;
71 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
74 static int huff_build(VLC *vlc, uint8_t *len)
83 for (i = 0; i < 256; i++) {
87 qsort(he, 256, sizeof(HuffEntry), huff_cmp_len);
90 for (i = 255; i >= 0; i--) {
91 codes[i] = code >> (32 - he[i].len);
94 code += 0x80000000u >> (he[i].len - 1);
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);
104 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
107 MagicYUVContext *s = avctx->priv_data;
108 int interlaced = s->interlaced;
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];
122 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
123 s->slices[i][j].size);
128 flags = get_bits(&gb, 8);
129 pred = get_bits(&gb, 8);
131 dst = p->data[i] + j * sheight * stride;
133 for (k = 0; k < height; k++) {
134 for (x = 0; x < width; x++)
135 dst[x] = get_bits(&gb, 8);
140 for (k = 0; k < height; k++) {
141 for (x = 0; x < width; x++) {
143 if (get_bits_left(&gb) <= 0)
144 return AVERROR_INVALIDDATA;
146 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
148 return AVERROR_INVALIDDATA;
158 dst = p->data[i] + j * sheight * stride;
159 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
162 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
165 for (k = 1 + interlaced; k < height; k++) {
166 s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);
171 dst = p->data[i] + j * sheight * stride;
172 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
176 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
180 for (k = 1 + interlaced; k < height; k++) {
181 top = dst[-fake_stride];
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];
194 dst = p->data[i] + j * sheight * stride;
195 lefttop = left = dst[0];
196 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
199 lefttop = left = dst[0];
200 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
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];
211 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
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];
222 for (i = 0; i < height; i++) {
223 s->hdsp.add_bytes(b, g, width);
224 s->hdsp.add_bytes(r, g, width);
234 static int magy_decode_frame(AVCodecContext *avctx, void *data,
235 int *got_frame, AVPacket *avpkt)
237 MagicYUVContext *s = avctx->priv_data;
238 ThreadFrame frame = { .f = data };
240 GetByteContext gbyte;
242 uint32_t first_offset, offset, next_offset, header_size, slice_width;
243 int width, height, format, version, table_size;
246 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
247 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
248 return AVERROR_INVALIDDATA;
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;
257 version = bytestream2_get_byte(&gbyte);
259 avpriv_request_sample(avctx, "Version %d", version);
260 return AVERROR_PATCHWELCOME;
269 format = bytestream2_get_byte(&gbyte);
272 avctx->pix_fmt = AV_PIX_FMT_GBRP;
276 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
280 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
283 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
288 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
295 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
298 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
301 avpriv_request_sample(avctx, "Format 0x%X", format);
302 return AVERROR_PATCHWELCOME;
304 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
306 bytestream2_skip(&gbyte, 2);
307 s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);
308 bytestream2_skip(&gbyte, 3);
310 width = bytestream2_get_le32(&gbyte);
311 height = bytestream2_get_le32(&gbyte);
312 ret = ff_set_dimensions(avctx, width, height);
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;
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;
328 bytestream2_skip(&gbyte, 4);
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;
337 for (i = 0; i < s->planes; i++) {
338 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
340 return AVERROR(ENOMEM);
342 offset = bytestream2_get_le32(&gbyte);
343 if (offset >= avpkt->size - header_size)
344 return AVERROR_INVALIDDATA;
347 first_offset = offset;
349 for (j = 0; j < s->nb_slices - 1; j++) {
350 s->slices[i][j].start = offset + header_size;
352 next_offset = bytestream2_get_le32(&gbyte);
353 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
354 return AVERROR_INVALIDDATA;
356 s->slices[i][j].size = next_offset - offset;
357 offset = next_offset;
360 s->slices[i][j].start = offset + header_size;
361 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
364 if (bytestream2_get_byte(&gbyte) != s->planes)
365 return AVERROR_INVALIDDATA;
367 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
369 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
371 return AVERROR_INVALIDDATA;
373 ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
377 memset(s->len, 0, sizeof(s->len));
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;
384 for (k = 0; k < l; k++)
386 s->len[i][j + k] = x;
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;
396 if (i == s->planes) {
399 } else if (j > 256) {
400 return AVERROR_INVALIDDATA;
404 if (i != s->planes) {
405 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
406 return AVERROR_INVALIDDATA;
409 p->pict_type = AV_PICTURE_TYPE_I;
412 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
415 s->buf = avpkt->data;
417 avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);
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]);
431 static int magy_init_thread_copy(AVCodecContext *avctx)
433 MagicYUVContext *s = avctx->priv_data;
436 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
438 s->slices_size[i] = 0;
445 static av_cold int magy_decode_init(AVCodecContext *avctx)
447 MagicYUVContext *s = avctx->priv_data;
448 ff_huffyuvdsp_init(&s->hdsp);
452 static av_cold int magy_decode_end(AVCodecContext *avctx)
454 MagicYUVContext * const s = avctx->priv_data;
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]);
466 AVCodec ff_magicyuv_decoder = {
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,