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"
30 #include "bytestream.h"
32 #include "huffyuvdsp.h"
34 #include "lossless_videodsp.h"
37 typedef struct Slice {
42 typedef enum Prediction {
48 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 const 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 VLC vlc[4]; // VLC for each plane
70 int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
72 LLVidDSPContext llviddsp;
75 static int huff_build(const uint8_t len[], uint16_t codes_pos[33],
76 VLC *vlc, int nb_elems, void *logctx)
80 for (int i = 31; i > 0; i--)
81 codes_pos[i] += codes_pos[i + 1];
83 for (unsigned i = nb_elems; i-- > 0;)
84 he[--codes_pos[len[i]]] = (HuffEntry){ len[i], i };
87 return ff_init_vlc_from_lengths(vlc, FFMIN(he[0].len, 12), nb_elems,
88 &he[0].len, sizeof(he[0]),
89 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym),
93 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
94 const uint16_t *diff, intptr_t w,
95 int *left, int *left_top, int max)
103 for (i = 0; i < w; i++) {
104 l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
114 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
117 MagicYUVContext *s = avctx->priv_data;
118 int interlaced = s->interlaced;
119 const int bps = s->bps;
120 const int max = s->max - 1;
126 for (i = 0; i < s->planes; i++) {
127 int left, lefttop, top;
128 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
129 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
130 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
131 ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
132 ptrdiff_t stride = p->linesize[i] / 2;
134 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
135 s->slices[i][j].size);
140 flags = get_bits(&gb, 8);
141 pred = get_bits(&gb, 8);
143 dst = (uint16_t *)p->data[i] + j * sheight * stride;
145 if (get_bits_left(&gb) < bps * width * height)
146 return AVERROR_INVALIDDATA;
147 for (k = 0; k < height; k++) {
148 for (x = 0; x < width; x++)
149 dst[x] = get_bits(&gb, bps);
154 for (k = 0; k < height; k++) {
155 for (x = 0; x < width; x++) {
157 if (get_bits_left(&gb) <= 0)
158 return AVERROR_INVALIDDATA;
160 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
162 return AVERROR_INVALIDDATA;
172 dst = (uint16_t *)p->data[i] + j * sheight * stride;
173 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
176 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
179 for (k = 1 + interlaced; k < height; k++) {
180 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
185 dst = (uint16_t *)p->data[i] + j * sheight * stride;
186 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
189 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
192 for (k = 1 + interlaced; k < height; k++) {
193 top = dst[-fake_stride];
196 for (x = 1; x < width; x++) {
197 top = dst[x - fake_stride];
198 lefttop = dst[x - (fake_stride + 1)];
199 left += top - lefttop + dst[x];
206 dst = (uint16_t *)p->data[i] + j * sheight * stride;
207 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
210 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
213 lefttop = left = dst[0];
214 for (k = 1 + interlaced; k < height; k++) {
215 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
216 lefttop = left = dst[0];
221 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
225 if (s->decorrelate) {
226 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
227 int width = avctx->coded_width;
228 uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
229 uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
230 uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
232 for (i = 0; i < height; i++) {
233 for (k = 0; k < width; k++) {
234 b[k] = (b[k] + g[k]) & max;
235 r[k] = (r[k] + g[k]) & max;
237 b += p->linesize[0] / 2;
238 g += p->linesize[1] / 2;
239 r += p->linesize[2] / 2;
246 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
249 MagicYUVContext *s = avctx->priv_data;
250 int interlaced = s->interlaced;
252 int i, k, x, min_width;
256 for (i = 0; i < s->planes; i++) {
257 int left, lefttop, top;
258 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
259 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
260 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
261 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
262 ptrdiff_t stride = p->linesize[i];
263 const uint8_t *slice = s->buf + s->slices[i][j].start;
266 flags = bytestream_get_byte(&slice);
267 pred = bytestream_get_byte(&slice);
269 dst = p->data[i] + j * sheight * stride;
271 if (s->slices[i][j].size - 2 < width * height)
272 return AVERROR_INVALIDDATA;
273 for (k = 0; k < height; k++) {
274 bytestream_get_buffer(&slice, dst, width);
278 int ret = init_get_bits8(&gb, slice, s->slices[i][j].size - 2);
283 for (k = 0; k < height; k++) {
284 for (x = 0; x < width; x++) {
286 if (get_bits_left(&gb) <= 0)
287 return AVERROR_INVALIDDATA;
289 pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
291 return AVERROR_INVALIDDATA;
301 dst = p->data[i] + j * sheight * stride;
302 s->llviddsp.add_left_pred(dst, dst, width, 0);
305 s->llviddsp.add_left_pred(dst, dst, width, 0);
308 for (k = 1 + interlaced; k < height; k++) {
309 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
314 dst = p->data[i] + j * sheight * stride;
315 s->llviddsp.add_left_pred(dst, dst, width, 0);
318 s->llviddsp.add_left_pred(dst, dst, width, 0);
321 min_width = FFMIN(width, 32);
322 for (k = 1 + interlaced; k < height; k++) {
323 top = dst[-fake_stride];
326 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
327 top = dst[x - fake_stride];
328 lefttop = dst[x - (fake_stride + 1)];
329 left += top - lefttop + dst[x];
333 s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
338 dst = p->data[i] + j * sheight * stride;
339 s->llviddsp.add_left_pred(dst, dst, width, 0);
342 s->llviddsp.add_left_pred(dst, dst, width, 0);
345 lefttop = left = dst[0];
346 for (k = 1 + interlaced; k < height; k++) {
347 s->llviddsp.add_median_pred(dst, dst - fake_stride,
348 dst, width, &left, &lefttop);
349 lefttop = left = dst[0];
354 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
358 if (s->decorrelate) {
359 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
360 int width = avctx->coded_width;
361 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
362 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
363 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
365 for (i = 0; i < height; i++) {
366 s->llviddsp.add_bytes(b, g, width);
367 s->llviddsp.add_bytes(r, g, width);
377 static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
378 int table_size, int max)
380 MagicYUVContext *s = avctx->priv_data;
383 uint16_t length_count[33] = { 0 };
386 bytestream2_init(&gb, table, table_size);
388 while (bytestream2_get_bytes_left(&gb) > 0) {
389 int b = bytestream2_peek_byteu(&gb) & 0x80;
390 int x = bytestream2_get_byteu(&gb) & ~0x80;
394 if (bytestream2_get_bytes_left(&gb) <= 0)
396 l += bytestream2_get_byteu(&gb);
399 if (k > max || x == 0 || x > 32) {
400 av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
401 return AVERROR_INVALIDDATA;
404 length_count[x] += l;
410 if (huff_build(len, length_count, &s->vlc[i], max, avctx)) {
411 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
412 return AVERROR_INVALIDDATA;
415 if (i == s->planes) {
418 memset(length_count, 0, sizeof(length_count));
422 if (i != s->planes) {
423 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
424 return AVERROR_INVALIDDATA;
430 static int magy_decode_frame(AVCodecContext *avctx, void *data,
431 int *got_frame, AVPacket *avpkt)
433 MagicYUVContext *s = avctx->priv_data;
434 ThreadFrame frame = { .f = data };
437 uint32_t first_offset, offset, next_offset, header_size, slice_width;
438 int width, height, format, version, table_size;
441 if (avpkt->size < 36)
442 return AVERROR_INVALIDDATA;
444 bytestream2_init(&gb, avpkt->data, avpkt->size);
445 if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
446 return AVERROR_INVALIDDATA;
448 header_size = bytestream2_get_le32u(&gb);
449 if (header_size < 32 || header_size >= avpkt->size) {
450 av_log(avctx, AV_LOG_ERROR,
451 "header or packet too small %"PRIu32"\n", header_size);
452 return AVERROR_INVALIDDATA;
455 version = bytestream2_get_byteu(&gb);
457 avpriv_request_sample(avctx, "Version %d", version);
458 return AVERROR_PATCHWELCOME;
468 format = bytestream2_get_byteu(&gb);
471 avctx->pix_fmt = AV_PIX_FMT_GBRP;
475 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
479 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
482 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
487 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
494 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
497 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
500 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
506 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
510 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
515 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
520 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
525 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
530 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
534 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
542 avpriv_request_sample(avctx, "Format 0x%X", format);
543 return AVERROR_PATCHWELCOME;
545 s->max = 1 << s->bps;
546 s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
547 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
549 bytestream2_skipu(&gb, 1);
550 s->color_matrix = bytestream2_get_byteu(&gb);
551 s->flags = bytestream2_get_byteu(&gb);
552 s->interlaced = !!(s->flags & 2);
553 bytestream2_skipu(&gb, 3);
555 width = bytestream2_get_le32u(&gb);
556 height = bytestream2_get_le32u(&gb);
557 ret = ff_set_dimensions(avctx, width, height);
561 slice_width = bytestream2_get_le32u(&gb);
562 if (slice_width != avctx->coded_width) {
563 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
564 return AVERROR_PATCHWELCOME;
566 s->slice_height = bytestream2_get_le32u(&gb);
567 if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
568 av_log(avctx, AV_LOG_ERROR,
569 "invalid slice height: %d\n", s->slice_height);
570 return AVERROR_INVALIDDATA;
573 bytestream2_skipu(&gb, 4);
575 s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
576 if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
577 av_log(avctx, AV_LOG_ERROR,
578 "invalid number of slices: %d\n", s->nb_slices);
579 return AVERROR_INVALIDDATA;
583 if ((s->slice_height >> s->vshift[1]) < 2) {
584 av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
585 return AVERROR_INVALIDDATA;
587 if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
588 av_log(avctx, AV_LOG_ERROR, "impossible height\n");
589 return AVERROR_INVALIDDATA;
593 if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
594 return AVERROR_INVALIDDATA;
595 for (i = 0; i < s->planes; i++) {
596 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
598 return AVERROR(ENOMEM);
600 offset = bytestream2_get_le32u(&gb);
601 if (offset >= avpkt->size - header_size)
602 return AVERROR_INVALIDDATA;
605 first_offset = offset;
607 for (j = 0; j < s->nb_slices - 1; j++) {
608 s->slices[i][j].start = offset + header_size;
610 next_offset = bytestream2_get_le32u(&gb);
611 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
612 return AVERROR_INVALIDDATA;
614 s->slices[i][j].size = next_offset - offset;
615 if (s->slices[i][j].size < 2)
616 return AVERROR_INVALIDDATA;
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;
623 if (s->slices[i][j].size < 2)
624 return AVERROR_INVALIDDATA;
627 if (bytestream2_get_byteu(&gb) != s->planes)
628 return AVERROR_INVALIDDATA;
630 bytestream2_skipu(&gb, s->nb_slices * s->planes);
632 table_size = header_size + first_offset - bytestream2_tell(&gb);
634 return AVERROR_INVALIDDATA;
636 ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
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,