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 VLC vlc[4]; // VLC for each plane
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) * 4096 + bb->sym - aa->sym;
83 static int huff_build(HuffEntry he[], VLC *vlc, int nb_elems)
88 AV_QSORT(he, nb_elems, HuffEntry, huff_cmp_len);
91 for (i = nb_elems - 1; i >= 0; i--) {
92 he[i].code = code >> (32 - he[i].len);
93 code += 0x80000000u >> (he[i].len - 1);
97 return ff_init_vlc_sparse(vlc, FFMIN(he[nb_elems - 1].len, 12), nb_elems,
98 &he[0].len, sizeof(he[0]), sizeof(he[0].len),
99 &he[0].code, sizeof(he[0]), sizeof(he[0].code),
100 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0);
103 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
104 const uint16_t *diff, intptr_t w,
105 int *left, int *left_top, int max)
113 for (i = 0; i < w; i++) {
114 l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
124 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
127 MagicYUVContext *s = avctx->priv_data;
128 int interlaced = s->interlaced;
129 const int bps = s->bps;
130 const int max = s->max - 1;
136 for (i = 0; i < s->planes; i++) {
137 int left, lefttop, top;
138 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
139 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
140 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
141 ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
142 ptrdiff_t stride = p->linesize[i] / 2;
144 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
145 s->slices[i][j].size);
150 flags = get_bits(&gb, 8);
151 pred = get_bits(&gb, 8);
153 dst = (uint16_t *)p->data[i] + j * sheight * stride;
155 if (get_bits_left(&gb) < bps * width * height)
156 return AVERROR_INVALIDDATA;
157 for (k = 0; k < height; k++) {
158 for (x = 0; x < width; x++)
159 dst[x] = get_bits(&gb, bps);
164 for (k = 0; k < height; k++) {
165 for (x = 0; x < width; x++) {
167 if (get_bits_left(&gb) <= 0)
168 return AVERROR_INVALIDDATA;
170 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
172 return AVERROR_INVALIDDATA;
182 dst = (uint16_t *)p->data[i] + j * sheight * stride;
183 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
186 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
189 for (k = 1 + interlaced; k < height; k++) {
190 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
195 dst = (uint16_t *)p->data[i] + j * sheight * stride;
196 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
199 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
202 for (k = 1 + interlaced; k < height; k++) {
203 top = dst[-fake_stride];
206 for (x = 1; x < width; x++) {
207 top = dst[x - fake_stride];
208 lefttop = dst[x - (fake_stride + 1)];
209 left += top - lefttop + dst[x];
216 dst = (uint16_t *)p->data[i] + j * sheight * stride;
217 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
220 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
223 lefttop = left = dst[0];
224 for (k = 1 + interlaced; k < height; k++) {
225 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
226 lefttop = left = dst[0];
231 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
235 if (s->decorrelate) {
236 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
237 int width = avctx->coded_width;
238 uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
239 uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
240 uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
242 for (i = 0; i < height; i++) {
243 for (k = 0; k < width; k++) {
244 b[k] = (b[k] + g[k]) & max;
245 r[k] = (r[k] + g[k]) & max;
247 b += p->linesize[0] / 2;
248 g += p->linesize[1] / 2;
249 r += p->linesize[2] / 2;
256 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
259 MagicYUVContext *s = avctx->priv_data;
260 int interlaced = s->interlaced;
262 int i, k, x, min_width;
266 for (i = 0; i < s->planes; i++) {
267 int left, lefttop, top;
268 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
269 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
270 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
271 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
272 ptrdiff_t stride = p->linesize[i];
274 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
275 s->slices[i][j].size);
280 flags = get_bits(&gb, 8);
281 pred = get_bits(&gb, 8);
283 dst = p->data[i] + j * sheight * stride;
285 if (get_bits_left(&gb) < 8* width * height)
286 return AVERROR_INVALIDDATA;
287 for (k = 0; k < height; k++) {
288 for (x = 0; x < width; x++)
289 dst[x] = get_bits(&gb, 8);
294 for (k = 0; k < height; k++) {
295 for (x = 0; x < width; x++) {
297 if (get_bits_left(&gb) <= 0)
298 return AVERROR_INVALIDDATA;
300 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
302 return AVERROR_INVALIDDATA;
312 dst = p->data[i] + j * sheight * stride;
313 s->llviddsp.add_left_pred(dst, dst, width, 0);
316 s->llviddsp.add_left_pred(dst, dst, width, 0);
319 for (k = 1 + interlaced; k < height; k++) {
320 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
325 dst = p->data[i] + j * sheight * stride;
326 s->llviddsp.add_left_pred(dst, dst, width, 0);
329 s->llviddsp.add_left_pred(dst, dst, width, 0);
332 min_width = FFMIN(width, 32);
333 for (k = 1 + interlaced; k < height; k++) {
334 top = dst[-fake_stride];
337 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
338 top = dst[x - fake_stride];
339 lefttop = dst[x - (fake_stride + 1)];
340 left += top - lefttop + dst[x];
344 s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
349 dst = p->data[i] + j * sheight * stride;
350 s->llviddsp.add_left_pred(dst, dst, width, 0);
353 s->llviddsp.add_left_pred(dst, dst, width, 0);
356 lefttop = left = dst[0];
357 for (k = 1 + interlaced; k < height; k++) {
358 s->llviddsp.add_median_pred(dst, dst - fake_stride,
359 dst, width, &left, &lefttop);
360 lefttop = left = dst[0];
365 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
369 if (s->decorrelate) {
370 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
371 int width = avctx->coded_width;
372 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
373 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
374 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
376 for (i = 0; i < height; i++) {
377 s->llviddsp.add_bytes(b, g, width);
378 s->llviddsp.add_bytes(r, g, width);
388 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
390 MagicYUVContext *s = avctx->priv_data;
394 while (get_bits_left(gbit) >= 8) {
395 int b = get_bits(gbit, 1);
396 int x = get_bits(gbit, 7);
400 if (get_bits_left(gbit) < 8)
402 l += get_bits(gbit, 8);
405 if (k > max || x == 0 || x > 32) {
406 av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
407 return AVERROR_INVALIDDATA;
417 if (huff_build(he, &s->vlc[i], max)) {
418 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
419 return AVERROR_INVALIDDATA;
422 if (i == s->planes) {
428 if (i != s->planes) {
429 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
430 return AVERROR_INVALIDDATA;
436 static int magy_decode_frame(AVCodecContext *avctx, void *data,
437 int *got_frame, AVPacket *avpkt)
439 MagicYUVContext *s = avctx->priv_data;
440 ThreadFrame frame = { .f = data };
442 GetByteContext gbyte;
444 uint32_t first_offset, offset, next_offset, header_size, slice_width;
445 int width, height, format, version, table_size;
448 bytestream2_init(&gbyte, avpkt->data, avpkt->size);
449 if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
450 return AVERROR_INVALIDDATA;
452 header_size = bytestream2_get_le32(&gbyte);
453 if (header_size < 32 || header_size >= avpkt->size) {
454 av_log(avctx, AV_LOG_ERROR,
455 "header or packet too small %"PRIu32"\n", header_size);
456 return AVERROR_INVALIDDATA;
459 version = bytestream2_get_byte(&gbyte);
461 avpriv_request_sample(avctx, "Version %d", version);
462 return AVERROR_PATCHWELCOME;
472 format = bytestream2_get_byte(&gbyte);
475 avctx->pix_fmt = AV_PIX_FMT_GBRP;
479 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
483 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
486 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
491 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
498 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
501 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
504 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
510 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
514 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
519 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
524 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
529 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
534 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
538 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
546 avpriv_request_sample(avctx, "Format 0x%X", format);
547 return AVERROR_PATCHWELCOME;
549 s->max = 1 << s->bps;
550 s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
551 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
553 bytestream2_skip(&gbyte, 1);
554 s->color_matrix = bytestream2_get_byte(&gbyte);
555 s->flags = bytestream2_get_byte(&gbyte);
556 s->interlaced = !!(s->flags & 2);
557 bytestream2_skip(&gbyte, 3);
559 width = bytestream2_get_le32(&gbyte);
560 height = bytestream2_get_le32(&gbyte);
561 ret = ff_set_dimensions(avctx, width, height);
565 slice_width = bytestream2_get_le32(&gbyte);
566 if (slice_width != avctx->coded_width) {
567 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
568 return AVERROR_PATCHWELCOME;
570 s->slice_height = bytestream2_get_le32(&gbyte);
571 if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
572 av_log(avctx, AV_LOG_ERROR,
573 "invalid slice height: %d\n", s->slice_height);
574 return AVERROR_INVALIDDATA;
577 bytestream2_skip(&gbyte, 4);
579 s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
580 if (s->nb_slices > INT_MAX / sizeof(Slice)) {
581 av_log(avctx, AV_LOG_ERROR,
582 "invalid number of slices: %d\n", s->nb_slices);
583 return AVERROR_INVALIDDATA;
587 if ((s->slice_height >> s->vshift[1]) < 2) {
588 av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
589 return AVERROR_INVALIDDATA;
591 if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
592 av_log(avctx, AV_LOG_ERROR, "impossible height\n");
593 return AVERROR_INVALIDDATA;
597 for (i = 0; i < s->planes; i++) {
598 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
600 return AVERROR(ENOMEM);
602 offset = bytestream2_get_le32(&gbyte);
603 if (offset >= avpkt->size - header_size)
604 return AVERROR_INVALIDDATA;
607 first_offset = offset;
609 for (j = 0; j < s->nb_slices - 1; j++) {
610 s->slices[i][j].start = offset + header_size;
612 next_offset = bytestream2_get_le32(&gbyte);
613 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
614 return AVERROR_INVALIDDATA;
616 s->slices[i][j].size = next_offset - offset;
617 offset = next_offset;
620 s->slices[i][j].start = offset + header_size;
621 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
624 if (bytestream2_get_byte(&gbyte) != s->planes)
625 return AVERROR_INVALIDDATA;
627 bytestream2_skip(&gbyte, s->nb_slices * s->planes);
629 table_size = header_size + first_offset - bytestream2_tell(&gbyte);
631 return AVERROR_INVALIDDATA;
633 ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
637 ret = build_huffman(avctx, &gbit, s->max);
641 p->pict_type = AV_PICTURE_TYPE_I;
644 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
647 s->buf = avpkt->data;
649 avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
651 if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
652 avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
653 avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
654 avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
655 avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
656 avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
657 FFSWAP(uint8_t*, p->data[0], p->data[1]);
658 FFSWAP(int, p->linesize[0], p->linesize[1]);
660 switch (s->color_matrix) {
662 p->colorspace = AVCOL_SPC_BT470BG;
665 p->colorspace = AVCOL_SPC_BT709;
668 p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
676 static av_cold int magy_decode_init(AVCodecContext *avctx)
678 MagicYUVContext *s = avctx->priv_data;
679 ff_llviddsp_init(&s->llviddsp);
683 static av_cold int magy_decode_end(AVCodecContext *avctx)
685 MagicYUVContext * const s = avctx->priv_data;
688 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
689 av_freep(&s->slices[i]);
690 s->slices_size[i] = 0;
691 ff_free_vlc(&s->vlc[i]);
697 AVCodec ff_magicyuv_decoder = {
699 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
700 .type = AVMEDIA_TYPE_VIDEO,
701 .id = AV_CODEC_ID_MAGICYUV,
702 .priv_data_size = sizeof(MagicYUVContext),
703 .init = magy_decode_init,
704 .close = magy_decode_end,
705 .decode = magy_decode_frame,
706 .capabilities = AV_CODEC_CAP_DR1 |
707 AV_CODEC_CAP_FRAME_THREADS |
708 AV_CODEC_CAP_SLICE_THREADS,
709 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,