3 * Copyright (c) 2016 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #define CACHED_BITSTREAM_READER !ARCH_X86_32
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/qsort.h"
31 #include "bytestream.h"
33 #include "huffyuvdsp.h"
35 #include "lossless_videodsp.h"
38 typedef struct Slice {
43 typedef enum Prediction {
49 typedef struct HuffEntry {
55 typedef struct MagicYUVContext {
61 int planes; // number of encoded planes in bitstream
62 int decorrelate; // postprocessing work
63 int color_matrix; // video color matrix
65 int interlaced; // video is interlaced
66 uint8_t *buf; // pointer to AVPacket->data
69 Slice *slices[4]; // slice bitstream positions for each plane
70 unsigned int slices_size[4]; // slice sizes for each plane
71 uint8_t len[4][4096]; // table of code lengths for each plane
72 VLC vlc[4]; // VLC for each plane
73 int (*huff_build)(VLC *vlc, uint8_t *len);
74 int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
76 LLVidDSPContext llviddsp;
79 static int huff_cmp_len(const void *a, const void *b)
81 const HuffEntry *aa = a, *bb = b;
82 return (aa->len - bb->len) * 256 + bb->sym - aa->sym;
85 static int huff_cmp_len10(const void *a, const void *b)
87 const HuffEntry *aa = a, *bb = b;
88 return (aa->len - bb->len) * 1024 + bb->sym - aa->sym;
91 static int huff_cmp_len12(const void *a, const void *b)
93 const HuffEntry *aa = a, *bb = b;
94 return (aa->len - bb->len) * 4096 + bb->sym - aa->sym;
97 static int huff_build10(VLC *vlc, uint8_t *len)
103 for (i = 0; i < 1024; i++) {
106 if (len[i] == 0 || len[i] > 32)
107 return AVERROR_INVALIDDATA;
109 AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
112 for (i = 1023; i >= 0; i--) {
113 he[i].code = code >> (32 - he[i].len);
114 code += 0x80000000u >> (he[i].len - 1);
118 return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
119 &he[0].len, sizeof(he[0]), sizeof(he[0].len),
120 &he[0].code, sizeof(he[0]), sizeof(he[0].code),
121 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0);
124 static int huff_build12(VLC *vlc, uint8_t *len)
130 for (i = 0; i < 4096; i++) {
133 if (len[i] == 0 || len[i] > 32)
134 return AVERROR_INVALIDDATA;
136 AV_QSORT(he, 4096, HuffEntry, huff_cmp_len12);
139 for (i = 4095; i >= 0; i--) {
140 he[i].code = code >> (32 - he[i].len);
141 code += 0x80000000u >> (he[i].len - 1);
145 return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 12), 4096,
146 &he[0].len, sizeof(he[0]), sizeof(he[0].len),
147 &he[0].code, sizeof(he[0]), sizeof(he[0].code),
148 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0);
151 static int huff_build(VLC *vlc, uint8_t *len)
157 for (i = 0; i < 256; i++) {
160 if (len[i] == 0 || len[i] > 32)
161 return AVERROR_INVALIDDATA;
163 AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
166 for (i = 255; i >= 0; i--) {
167 he[i].code = code >> (32 - he[i].len);
168 code += 0x80000000u >> (he[i].len - 1);
172 return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
173 &he[0].len, sizeof(he[0]), sizeof(he[0].len),
174 &he[0].code, sizeof(he[0]), sizeof(he[0].code),
175 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0);
178 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
179 const uint16_t *diff, intptr_t w,
180 int *left, int *left_top, int max)
188 for (i = 0; i < w; i++) {
189 l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
199 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
202 MagicYUVContext *s = avctx->priv_data;
203 int interlaced = s->interlaced;
204 const int bps = s->bps;
205 const int max = s->max - 1;
211 for (i = 0; i < s->planes; i++) {
212 int left, lefttop, top;
213 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
214 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
215 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
216 ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
217 ptrdiff_t stride = p->linesize[i] / 2;
219 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
220 s->slices[i][j].size);
225 flags = get_bits(&gb, 8);
226 pred = get_bits(&gb, 8);
228 dst = (uint16_t *)p->data[i] + j * sheight * stride;
230 if (get_bits_left(&gb) < bps * width * height)
231 return AVERROR_INVALIDDATA;
232 for (k = 0; k < height; k++) {
233 for (x = 0; x < width; x++)
234 dst[x] = get_bits(&gb, bps);
239 for (k = 0; k < height; k++) {
240 for (x = 0; x < width; x++) {
242 if (get_bits_left(&gb) <= 0)
243 return AVERROR_INVALIDDATA;
245 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
247 return AVERROR_INVALIDDATA;
257 dst = (uint16_t *)p->data[i] + j * sheight * stride;
258 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
261 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
264 for (k = 1 + interlaced; k < height; k++) {
265 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
270 dst = (uint16_t *)p->data[i] + j * sheight * stride;
271 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
274 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
277 for (k = 1 + interlaced; k < height; k++) {
278 top = dst[-fake_stride];
281 for (x = 1; x < width; x++) {
282 top = dst[x - fake_stride];
283 lefttop = dst[x - (fake_stride + 1)];
284 left += top - lefttop + dst[x];
291 dst = (uint16_t *)p->data[i] + j * sheight * stride;
292 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
295 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
298 lefttop = left = dst[0];
299 for (k = 1 + interlaced; k < height; k++) {
300 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
301 lefttop = left = dst[0];
306 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
310 if (s->decorrelate) {
311 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
312 int width = avctx->coded_width;
313 uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
314 uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
315 uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
317 for (i = 0; i < height; i++) {
318 for (k = 0; k < width; k++) {
319 b[k] = (b[k] + g[k]) & max;
320 r[k] = (r[k] + g[k]) & max;
322 b += p->linesize[0] / 2;
323 g += p->linesize[1] / 2;
324 r += p->linesize[2] / 2;
331 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
334 MagicYUVContext *s = avctx->priv_data;
335 int interlaced = s->interlaced;
337 int i, k, x, min_width;
341 for (i = 0; i < s->planes; i++) {
342 int left, lefttop, top;
343 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
344 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
345 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
346 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
347 ptrdiff_t stride = p->linesize[i];
349 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
350 s->slices[i][j].size);
355 flags = get_bits(&gb, 8);
356 pred = get_bits(&gb, 8);
358 dst = p->data[i] + j * sheight * stride;
360 if (get_bits_left(&gb) < 8* width * height)
361 return AVERROR_INVALIDDATA;
362 for (k = 0; k < height; k++) {
363 for (x = 0; x < width; x++)
364 dst[x] = get_bits(&gb, 8);
369 for (k = 0; k < height; k++) {
370 for (x = 0; x < width; x++) {
372 if (get_bits_left(&gb) <= 0)
373 return AVERROR_INVALIDDATA;
375 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
377 return AVERROR_INVALIDDATA;
387 dst = p->data[i] + j * sheight * stride;
388 s->llviddsp.add_left_pred(dst, dst, width, 0);
391 s->llviddsp.add_left_pred(dst, dst, width, 0);
394 for (k = 1 + interlaced; k < height; k++) {
395 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
400 dst = p->data[i] + j * sheight * stride;
401 s->llviddsp.add_left_pred(dst, dst, width, 0);
404 s->llviddsp.add_left_pred(dst, dst, width, 0);
407 min_width = FFMIN(width, 32);
408 for (k = 1 + interlaced; k < height; k++) {
409 top = dst[-fake_stride];
412 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
413 top = dst[x - fake_stride];
414 lefttop = dst[x - (fake_stride + 1)];
415 left += top - lefttop + dst[x];
419 s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
424 dst = p->data[i] + j * sheight * stride;
425 s->llviddsp.add_left_pred(dst, dst, width, 0);
428 s->llviddsp.add_left_pred(dst, dst, width, 0);
431 lefttop = left = dst[0];
432 for (k = 1 + interlaced; k < height; k++) {
433 s->llviddsp.add_median_pred(dst, dst - fake_stride,
434 dst, width, &left, &lefttop);
435 lefttop = left = dst[0];
440 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
444 if (s->decorrelate) {
445 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
446 int width = avctx->coded_width;
447 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
448 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
449 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
451 for (i = 0; i < height; i++) {
452 s->llviddsp.add_bytes(b, g, width);
453 s->llviddsp.add_bytes(r, g, width);
463 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
465 MagicYUVContext *s = avctx->priv_data;
468 memset(s->len, 0, sizeof(s->len));
469 while (get_bits_left(gbit) >= 8) {
470 int b = get_bits(gbit, 1);
471 int x = get_bits(gbit, 7);
472 int l = get_bitsz(gbit, b * 8) + 1;
474 for (k = 0; k < l; k++)
476 s->len[i][j + k] = x;
481 if (s->huff_build(&s->vlc[i], s->len[i])) {
482 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
483 return AVERROR_INVALIDDATA;
486 if (i == s->planes) {
489 } else if (j > max) {
490 av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
491 return AVERROR_INVALIDDATA;
495 if (i != s->planes) {
496 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
497 return AVERROR_INVALIDDATA;
503 static int magy_decode_frame(AVCodecContext *avctx, void *data,
504 int *got_frame, AVPacket *avpkt)
506 MagicYUVContext *s = avctx->priv_data;
507 ThreadFrame frame = { .f = data };
509 GetByteContext gbyte;
511 uint32_t first_offset, offset, next_offset, header_size, slice_width;
512 int width, height, format, version, table_size;
515 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
516 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
517 return AVERROR_INVALIDDATA;
519 header_size = bytestream2_get_le32(&gbyte);
520 if (header_size < 32 || header_size >= avpkt->size) {
521 av_log(avctx, AV_LOG_ERROR,
522 "header or packet too small %"PRIu32"\n", header_size);
523 return AVERROR_INVALIDDATA;
526 version = bytestream2_get_byte(&gbyte);
528 avpriv_request_sample(avctx, "Version %d", version);
529 return AVERROR_PATCHWELCOME;
539 format = bytestream2_get_byte(&gbyte);
542 avctx->pix_fmt = AV_PIX_FMT_GBRP;
546 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
550 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
553 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
558 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
565 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
568 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
571 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
577 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
581 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
586 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
591 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
596 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
601 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
605 avpriv_request_sample(avctx, "Format 0x%X", format);
606 return AVERROR_PATCHWELCOME;
608 s->max = 1 << s->bps;
609 s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
611 s->huff_build = huff_build;
613 s->huff_build = s->bps == 10 ? huff_build10 : huff_build12;
614 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
616 bytestream2_skip(&gbyte, 1);
617 s->color_matrix = bytestream2_get_byte(&gbyte);
618 s->flags = bytestream2_get_byte(&gbyte);
619 s->interlaced = !!(s->flags & 2);
620 bytestream2_skip(&gbyte, 3);
622 width = bytestream2_get_le32(&gbyte);
623 height = bytestream2_get_le32(&gbyte);
624 ret = ff_set_dimensions(avctx, width, height);
628 slice_width = bytestream2_get_le32(&gbyte);
629 if (slice_width != avctx->coded_width) {
630 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
631 return AVERROR_PATCHWELCOME;
633 s->slice_height = bytestream2_get_le32(&gbyte);
634 if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
635 av_log(avctx, AV_LOG_ERROR,
636 "invalid slice height: %d\n", s->slice_height);
637 return AVERROR_INVALIDDATA;
640 bytestream2_skip(&gbyte, 4);
642 s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
643 if (s->nb_slices > INT_MAX / sizeof(Slice)) {
644 av_log(avctx, AV_LOG_ERROR,
645 "invalid number of slices: %d\n", s->nb_slices);
646 return AVERROR_INVALIDDATA;
650 if ((s->slice_height >> s->vshift[1]) < 2) {
651 av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
652 return AVERROR_INVALIDDATA;
654 if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
655 av_log(avctx, AV_LOG_ERROR, "impossible height\n");
656 return AVERROR_INVALIDDATA;
660 for (i = 0; i < s->planes; i++) {
661 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
663 return AVERROR(ENOMEM);
665 offset = bytestream2_get_le32(&gbyte);
666 if (offset >= avpkt->size - header_size)
667 return AVERROR_INVALIDDATA;
670 first_offset = offset;
672 for (j = 0; j < s->nb_slices - 1; j++) {
673 s->slices[i][j].start = offset + header_size;
675 next_offset = bytestream2_get_le32(&gbyte);
676 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
677 return AVERROR_INVALIDDATA;
679 s->slices[i][j].size = next_offset - offset;
680 offset = next_offset;
683 s->slices[i][j].start = offset + header_size;
684 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
687 if (bytestream2_get_byte(&gbyte) != s->planes)
688 return AVERROR_INVALIDDATA;
690 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
692 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
694 return AVERROR_INVALIDDATA;
696 ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
700 ret = build_huffman(avctx, &gbit, s->max);
704 p->pict_type = AV_PICTURE_TYPE_I;
707 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
710 s->buf = avpkt->data;
712 avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
714 if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
715 avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
716 avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
717 avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
718 avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
719 avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
720 FFSWAP(uint8_t*, p->data[0], p->data[1]);
721 FFSWAP(int, p->linesize[0], p->linesize[1]);
723 switch (s->color_matrix) {
725 p->colorspace = AVCOL_SPC_BT470BG;
728 p->colorspace = AVCOL_SPC_BT709;
731 p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
739 static av_cold int magy_decode_init(AVCodecContext *avctx)
741 MagicYUVContext *s = avctx->priv_data;
742 ff_llviddsp_init(&s->llviddsp);
746 static av_cold int magy_decode_end(AVCodecContext *avctx)
748 MagicYUVContext * const s = avctx->priv_data;
751 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
752 av_freep(&s->slices[i]);
753 s->slices_size[i] = 0;
754 ff_free_vlc(&s->vlc[i]);
760 AVCodec ff_magicyuv_decoder = {
762 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
763 .type = AVMEDIA_TYPE_VIDEO,
764 .id = AV_CODEC_ID_MAGICYUV,
765 .priv_data_size = sizeof(MagicYUVContext),
766 .init = magy_decode_init,
767 .close = magy_decode_end,
768 .decode = magy_decode_frame,
769 .capabilities = AV_CODEC_CAP_DR1 |
770 AV_CODEC_CAP_FRAME_THREADS |
771 AV_CODEC_CAP_SLICE_THREADS,
772 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,