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 #include "libavutil/pixdesc.h"
26 #include "libavutil/qsort.h"
29 #include "bytestream.h"
31 #include "huffyuvdsp.h"
33 #include "lossless_videodsp.h"
36 typedef struct Slice {
41 typedef enum Prediction {
47 typedef struct HuffEntry {
53 typedef struct MagicYUVContext {
59 int planes; // number of encoded planes in bitstream
60 int decorrelate; // postprocessing work
61 int color_matrix; // video color matrix
63 int interlaced; // video is interlaced
64 uint8_t *buf; // pointer to AVPacket->data
67 Slice *slices[4]; // slice bitstream positions for each plane
68 unsigned int slices_size[4]; // slice sizes for each plane
69 uint8_t len[4][4096]; // table of code lengths for each plane
70 VLC vlc[4]; // VLC for each plane
71 int (*huff_build)(VLC *vlc, uint8_t *len);
72 int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
74 LLVidDSPContext llviddsp;
77 static int huff_cmp_len(const void *a, const void *b)
79 const HuffEntry *aa = a, *bb = b;
80 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
83 static int huff_cmp_len10(const void *a, const void *b)
85 const HuffEntry *aa = a, *bb = b;
86 return (aa->len - bb->len) * 1024 + aa->sym - bb->sym;
89 static int huff_cmp_len12(const void *a, const void *b)
91 const HuffEntry *aa = a, *bb = b;
92 return (aa->len - bb->len) * 4096 + aa->sym - bb->sym;
95 static int huff_build10(VLC *vlc, uint8_t *len)
104 for (i = 0; i < 1024; i++) {
105 he[i].sym = 1023 - i;
107 if (len[i] == 0 || len[i] > 32)
108 return AVERROR_INVALIDDATA;
110 AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
113 for (i = 1023; i >= 0; i--) {
114 codes[i] = code >> (32 - he[i].len);
117 code += 0x80000000u >> (he[i].len - 1);
121 return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
122 bits, sizeof(*bits), sizeof(*bits),
123 codes, sizeof(*codes), sizeof(*codes),
124 syms, sizeof(*syms), sizeof(*syms), 0);
127 static int huff_build12(VLC *vlc, uint8_t *len)
130 uint32_t codes[4096];
136 for (i = 0; i < 4096; i++) {
137 he[i].sym = 4095 - i;
139 if (len[i] == 0 || len[i] > 32)
140 return AVERROR_INVALIDDATA;
142 AV_QSORT(he, 4096, HuffEntry, huff_cmp_len12);
145 for (i = 4095; i >= 0; i--) {
146 codes[i] = code >> (32 - he[i].len);
149 code += 0x80000000u >> (he[i].len - 1);
153 return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 14), 4096,
154 bits, sizeof(*bits), sizeof(*bits),
155 codes, sizeof(*codes), sizeof(*codes),
156 syms, sizeof(*syms), sizeof(*syms), 0);
159 static int huff_build(VLC *vlc, uint8_t *len)
168 for (i = 0; i < 256; i++) {
171 if (len[i] == 0 || len[i] > 32)
172 return AVERROR_INVALIDDATA;
174 AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
177 for (i = 255; i >= 0; i--) {
178 codes[i] = code >> (32 - he[i].len);
181 code += 0x80000000u >> (he[i].len - 1);
185 return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
186 bits, sizeof(*bits), sizeof(*bits),
187 codes, sizeof(*codes), sizeof(*codes),
188 syms, sizeof(*syms), sizeof(*syms), 0);
191 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
192 const uint16_t *diff, intptr_t w,
193 int *left, int *left_top, int max)
201 for (i = 0; i < w; i++) {
202 l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
212 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
215 MagicYUVContext *s = avctx->priv_data;
216 int interlaced = s->interlaced;
217 const int bps = s->bps;
218 const int max = s->max - 1;
224 for (i = 0; i < s->planes; i++) {
225 int left, lefttop, top;
226 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
227 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
228 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
229 ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
230 ptrdiff_t stride = p->linesize[i] / 2;
232 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
233 s->slices[i][j].size);
238 flags = get_bits(&gb, 8);
239 pred = get_bits(&gb, 8);
241 dst = (uint16_t *)p->data[i] + j * sheight * stride;
243 if (get_bits_left(&gb) < bps * width * height)
244 return AVERROR_INVALIDDATA;
245 for (k = 0; k < height; k++) {
246 for (x = 0; x < width; x++)
247 dst[x] = get_bits(&gb, bps);
252 for (k = 0; k < height; k++) {
253 for (x = 0; x < width; x++) {
255 if (get_bits_left(&gb) <= 0)
256 return AVERROR_INVALIDDATA;
258 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
260 return AVERROR_INVALIDDATA;
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 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
283 dst = (uint16_t *)p->data[i] + j * sheight * stride;
284 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
287 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
290 for (k = 1 + interlaced; k < height; k++) {
291 top = dst[-fake_stride];
294 for (x = 1; x < width; x++) {
295 top = dst[x - fake_stride];
296 lefttop = dst[x - (fake_stride + 1)];
297 left += top - lefttop + dst[x];
304 dst = (uint16_t *)p->data[i] + j * sheight * stride;
305 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
308 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
311 lefttop = left = dst[0];
312 for (k = 1 + interlaced; k < height; k++) {
313 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
314 lefttop = left = dst[0];
319 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
323 if (s->decorrelate) {
324 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
325 int width = avctx->coded_width;
326 uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
327 uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
328 uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
330 for (i = 0; i < height; i++) {
331 for (k = 0; k < width; k++) {
332 b[k] = (b[k] + g[k]) & max;
333 r[k] = (r[k] + g[k]) & max;
335 b += p->linesize[0] / 2;
336 g += p->linesize[1] / 2;
337 r += p->linesize[2] / 2;
344 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
347 MagicYUVContext *s = avctx->priv_data;
348 int interlaced = s->interlaced;
350 int i, k, x, min_width;
354 for (i = 0; i < s->planes; i++) {
355 int left, lefttop, top;
356 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
357 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
358 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
359 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
360 ptrdiff_t stride = p->linesize[i];
362 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
363 s->slices[i][j].size);
368 flags = get_bits(&gb, 8);
369 pred = get_bits(&gb, 8);
371 dst = p->data[i] + j * sheight * stride;
373 if (get_bits_left(&gb) < 8* width * height)
374 return AVERROR_INVALIDDATA;
375 for (k = 0; k < height; k++) {
376 for (x = 0; x < width; x++)
377 dst[x] = get_bits(&gb, 8);
382 for (k = 0; k < height; k++) {
383 for (x = 0; x < width; x++) {
385 if (get_bits_left(&gb) <= 0)
386 return AVERROR_INVALIDDATA;
388 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
390 return AVERROR_INVALIDDATA;
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 for (k = 1 + interlaced; k < height; k++) {
408 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
413 dst = p->data[i] + j * sheight * stride;
414 s->llviddsp.add_left_pred(dst, dst, width, 0);
417 s->llviddsp.add_left_pred(dst, dst, width, 0);
420 min_width = FFMIN(width, 32);
421 for (k = 1 + interlaced; k < height; k++) {
422 top = dst[-fake_stride];
425 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
426 top = dst[x - fake_stride];
427 lefttop = dst[x - (fake_stride + 1)];
428 left += top - lefttop + dst[x];
432 s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
437 dst = p->data[i] + j * sheight * stride;
438 s->llviddsp.add_left_pred(dst, dst, width, 0);
441 s->llviddsp.add_left_pred(dst, dst, width, 0);
444 lefttop = left = dst[0];
445 for (k = 1 + interlaced; k < height; k++) {
446 s->llviddsp.add_median_pred(dst, dst - fake_stride,
447 dst, width, &left, &lefttop);
448 lefttop = left = dst[0];
453 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
457 if (s->decorrelate) {
458 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
459 int width = avctx->coded_width;
460 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
461 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
462 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
464 for (i = 0; i < height; i++) {
465 s->llviddsp.add_bytes(b, g, width);
466 s->llviddsp.add_bytes(r, g, width);
476 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
478 MagicYUVContext *s = avctx->priv_data;
481 memset(s->len, 0, sizeof(s->len));
482 while (get_bits_left(gbit) >= 8) {
483 int b = get_bits(gbit, 1);
484 int x = get_bits(gbit, 7);
485 int l = get_bitsz(gbit, b * 8) + 1;
487 for (k = 0; k < l; k++)
489 s->len[i][j + k] = x;
494 if (s->huff_build(&s->vlc[i], s->len[i])) {
495 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
496 return AVERROR_INVALIDDATA;
499 if (i == s->planes) {
502 } else if (j > max) {
503 av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
504 return AVERROR_INVALIDDATA;
508 if (i != s->planes) {
509 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
510 return AVERROR_INVALIDDATA;
516 static int magy_decode_frame(AVCodecContext *avctx, void *data,
517 int *got_frame, AVPacket *avpkt)
519 MagicYUVContext *s = avctx->priv_data;
520 ThreadFrame frame = { .f = data };
522 GetByteContext gbyte;
524 uint32_t first_offset, offset, next_offset, header_size, slice_width;
525 int width, height, format, version, table_size;
528 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
529 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
530 return AVERROR_INVALIDDATA;
532 header_size = bytestream2_get_le32(&gbyte);
533 if (header_size < 32 || header_size >= avpkt->size) {
534 av_log(avctx, AV_LOG_ERROR,
535 "header or packet too small %"PRIu32"\n", header_size);
536 return AVERROR_INVALIDDATA;
539 version = bytestream2_get_byte(&gbyte);
541 avpriv_request_sample(avctx, "Version %d", version);
542 return AVERROR_PATCHWELCOME;
552 s->huff_build = huff_build;
553 s->magy_decode_slice = magy_decode_slice;
555 format = bytestream2_get_byte(&gbyte);
558 avctx->pix_fmt = AV_PIX_FMT_GBRP;
562 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
566 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
569 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
574 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
581 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
584 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
587 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
591 s->huff_build = huff_build10;
592 s->magy_decode_slice = magy_decode_slice10;
596 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
599 s->huff_build = huff_build10;
600 s->magy_decode_slice = magy_decode_slice10;
604 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
607 s->huff_build = huff_build10;
608 s->magy_decode_slice = magy_decode_slice10;
612 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
615 s->huff_build = huff_build12;
616 s->magy_decode_slice = magy_decode_slice10;
620 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
623 s->huff_build = huff_build12;
624 s->magy_decode_slice = magy_decode_slice10;
628 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
630 s->huff_build = huff_build10;
631 s->magy_decode_slice = magy_decode_slice10;
635 avpriv_request_sample(avctx, "Format 0x%X", format);
636 return AVERROR_PATCHWELCOME;
638 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
640 bytestream2_skip(&gbyte, 1);
641 s->color_matrix = bytestream2_get_byte(&gbyte);
642 s->flags = bytestream2_get_byte(&gbyte);
643 s->interlaced = !!(s->flags & 2);
644 bytestream2_skip(&gbyte, 3);
646 width = bytestream2_get_le32(&gbyte);
647 height = bytestream2_get_le32(&gbyte);
648 ret = ff_set_dimensions(avctx, width, height);
652 slice_width = bytestream2_get_le32(&gbyte);
653 if (slice_width != avctx->coded_width) {
654 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
655 return AVERROR_PATCHWELCOME;
657 s->slice_height = bytestream2_get_le32(&gbyte);
658 if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
659 av_log(avctx, AV_LOG_ERROR,
660 "invalid slice height: %d\n", s->slice_height);
661 return AVERROR_INVALIDDATA;
664 bytestream2_skip(&gbyte, 4);
666 s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
667 if (s->nb_slices > INT_MAX / sizeof(Slice)) {
668 av_log(avctx, AV_LOG_ERROR,
669 "invalid number of slices: %d\n", s->nb_slices);
670 return AVERROR_INVALIDDATA;
673 for (i = 0; i < s->planes; i++) {
674 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
676 return AVERROR(ENOMEM);
678 offset = bytestream2_get_le32(&gbyte);
679 if (offset >= avpkt->size - header_size)
680 return AVERROR_INVALIDDATA;
683 first_offset = offset;
685 for (j = 0; j < s->nb_slices - 1; j++) {
686 s->slices[i][j].start = offset + header_size;
688 next_offset = bytestream2_get_le32(&gbyte);
689 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
690 return AVERROR_INVALIDDATA;
692 s->slices[i][j].size = next_offset - offset;
693 offset = next_offset;
696 s->slices[i][j].start = offset + header_size;
697 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
700 if (bytestream2_get_byte(&gbyte) != s->planes)
701 return AVERROR_INVALIDDATA;
703 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
705 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
707 return AVERROR_INVALIDDATA;
709 ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
713 ret = build_huffman(avctx, &gbit, s->max);
717 p->pict_type = AV_PICTURE_TYPE_I;
720 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
723 s->buf = avpkt->data;
725 avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
727 if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
728 avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
729 avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
730 avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
731 avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
732 avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
733 FFSWAP(uint8_t*, p->data[0], p->data[1]);
734 FFSWAP(int, p->linesize[0], p->linesize[1]);
736 switch (s->color_matrix) {
738 p->colorspace = AVCOL_SPC_BT470BG;
741 p->colorspace = AVCOL_SPC_BT709;
744 p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
753 static int magy_init_thread_copy(AVCodecContext *avctx)
755 MagicYUVContext *s = avctx->priv_data;
758 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
760 s->slices_size[i] = 0;
767 static av_cold int magy_decode_init(AVCodecContext *avctx)
769 MagicYUVContext *s = avctx->priv_data;
770 ff_llviddsp_init(&s->llviddsp);
774 static av_cold int magy_decode_end(AVCodecContext *avctx)
776 MagicYUVContext * const s = avctx->priv_data;
779 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
780 av_freep(&s->slices[i]);
781 s->slices_size[i] = 0;
782 ff_free_vlc(&s->vlc[i]);
788 AVCodec ff_magicyuv_decoder = {
790 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
791 .type = AVMEDIA_TYPE_VIDEO,
792 .id = AV_CODEC_ID_MAGICYUV,
793 .priv_data_size = sizeof(MagicYUVContext),
794 .init = magy_decode_init,
795 .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
796 .close = magy_decode_end,
797 .decode = magy_decode_frame,
798 .capabilities = AV_CODEC_CAP_DR1 |
799 AV_CODEC_CAP_FRAME_THREADS |
800 AV_CODEC_CAP_SLICE_THREADS,
801 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,