2016-05-22 15:18:30 +02:00
|
|
|
/*
|
|
|
|
* MagicYUV decoder
|
|
|
|
* Copyright (c) 2016 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-08-31 16:23:23 +02:00
|
|
|
#define CACHED_BITSTREAM_READER !ARCH_X86_32
|
|
|
|
|
2016-07-14 18:39:46 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2016-06-19 00:23:28 +02:00
|
|
|
|
2016-05-22 15:18:30 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
#include "get_bits.h"
|
|
|
|
#include "huffyuvdsp.h"
|
|
|
|
#include "internal.h"
|
2016-12-20 00:21:49 +02:00
|
|
|
#include "lossless_videodsp.h"
|
2016-05-22 15:18:30 +02:00
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
typedef struct Slice {
|
|
|
|
uint32_t start;
|
|
|
|
uint32_t size;
|
|
|
|
} Slice;
|
|
|
|
|
|
|
|
typedef enum Prediction {
|
|
|
|
LEFT = 1,
|
|
|
|
GRADIENT,
|
|
|
|
MEDIAN,
|
|
|
|
} Prediction;
|
|
|
|
|
|
|
|
typedef struct HuffEntry {
|
|
|
|
uint8_t len;
|
2020-11-09 06:11:31 +02:00
|
|
|
uint16_t sym;
|
2016-05-22 15:18:30 +02:00
|
|
|
} HuffEntry;
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
typedef struct MagicYUVContext {
|
|
|
|
AVFrame *p;
|
2016-12-20 00:21:49 +02:00
|
|
|
int max;
|
2017-07-09 18:40:13 +02:00
|
|
|
int bps;
|
2016-06-19 00:23:28 +02:00
|
|
|
int slice_height;
|
|
|
|
int nb_slices;
|
|
|
|
int planes; // number of encoded planes in bitstream
|
|
|
|
int decorrelate; // postprocessing work
|
2016-12-26 18:34:57 +02:00
|
|
|
int color_matrix; // video color matrix
|
|
|
|
int flags;
|
2016-06-19 00:23:28 +02:00
|
|
|
int interlaced; // video is interlaced
|
2020-09-23 08:00:24 +02:00
|
|
|
const uint8_t *buf; // pointer to AVPacket->data
|
2016-06-19 00:23:28 +02:00
|
|
|
int hshift[4];
|
|
|
|
int vshift[4];
|
|
|
|
Slice *slices[4]; // slice bitstream positions for each plane
|
|
|
|
unsigned int slices_size[4]; // slice sizes for each plane
|
|
|
|
VLC vlc[4]; // VLC for each plane
|
2016-12-20 00:21:49 +02:00
|
|
|
int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
|
|
|
|
int j, int threadnr);
|
|
|
|
LLVidDSPContext llviddsp;
|
2016-06-19 00:23:28 +02:00
|
|
|
} MagicYUVContext;
|
|
|
|
|
2020-11-09 06:11:31 +02:00
|
|
|
static int huff_build(const uint8_t len[], uint16_t codes_pos[33],
|
|
|
|
VLC *vlc, int nb_elems, void *logctx)
|
2017-07-09 18:40:13 +02:00
|
|
|
{
|
2020-11-09 06:11:31 +02:00
|
|
|
HuffEntry he[4096];
|
|
|
|
|
|
|
|
for (int i = 31; i > 0; i--)
|
|
|
|
codes_pos[i] += codes_pos[i + 1];
|
|
|
|
|
|
|
|
for (unsigned i = nb_elems; i-- > 0;)
|
|
|
|
he[--codes_pos[len[i]]] = (HuffEntry){ len[i], i };
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-11-02 02:21:25 +02:00
|
|
|
ff_free_vlc(vlc);
|
2020-11-09 06:11:31 +02:00
|
|
|
return ff_init_vlc_from_lengths(vlc, FFMIN(he[0].len, 12), nb_elems,
|
|
|
|
&he[0].len, sizeof(he[0]),
|
|
|
|
&he[0].sym, sizeof(he[0]), sizeof(he[0].sym),
|
|
|
|
0, 0, logctx);
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 18:40:13 +02:00
|
|
|
static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
|
2016-12-20 00:21:49 +02:00
|
|
|
const uint16_t *diff, intptr_t w,
|
2017-07-09 18:40:13 +02:00
|
|
|
int *left, int *left_top, int max)
|
2016-12-20 00:21:49 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16_t l, lt;
|
|
|
|
|
|
|
|
l = *left;
|
|
|
|
lt = *left_top;
|
|
|
|
|
|
|
|
for (i = 0; i < w; i++) {
|
|
|
|
l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
|
2017-07-09 18:40:13 +02:00
|
|
|
l &= max;
|
2016-12-20 00:21:49 +02:00
|
|
|
lt = src1[i];
|
|
|
|
dst[i] = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
*left = l;
|
|
|
|
*left_top = lt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
|
|
|
|
int j, int threadnr)
|
|
|
|
{
|
|
|
|
MagicYUVContext *s = avctx->priv_data;
|
|
|
|
int interlaced = s->interlaced;
|
2017-07-09 18:40:13 +02:00
|
|
|
const int bps = s->bps;
|
|
|
|
const int max = s->max - 1;
|
2016-12-20 00:21:49 +02:00
|
|
|
AVFrame *p = s->p;
|
|
|
|
int i, k, x;
|
|
|
|
GetBitContext gb;
|
|
|
|
uint16_t *dst;
|
|
|
|
|
|
|
|
for (i = 0; i < s->planes; i++) {
|
|
|
|
int left, lefttop, top;
|
|
|
|
int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
|
|
|
|
int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
|
|
|
|
int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
|
|
|
|
ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
|
|
|
|
ptrdiff_t stride = p->linesize[i] / 2;
|
|
|
|
int flags, pred;
|
|
|
|
int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
|
|
|
|
s->slices[i][j].size);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
flags = get_bits(&gb, 8);
|
|
|
|
pred = get_bits(&gb, 8);
|
|
|
|
|
|
|
|
dst = (uint16_t *)p->data[i] + j * sheight * stride;
|
|
|
|
if (flags & 1) {
|
2018-06-23 23:37:10 +02:00
|
|
|
if (get_bits_left(&gb) < bps * width * height)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-12-20 00:21:49 +02:00
|
|
|
for (k = 0; k < height; k++) {
|
|
|
|
for (x = 0; x < width; x++)
|
2017-07-09 18:40:13 +02:00
|
|
|
dst[x] = get_bits(&gb, bps);
|
2016-12-20 00:21:49 +02:00
|
|
|
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (k = 0; k < height; k++) {
|
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
int pix;
|
|
|
|
if (get_bits_left(&gb) <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
|
|
|
|
if (pix < 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2020-08-31 16:19:55 +02:00
|
|
|
dst[x] = pix;
|
2016-12-20 00:21:49 +02:00
|
|
|
}
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pred) {
|
|
|
|
case LEFT:
|
|
|
|
dst = (uint16_t *)p->data[i] + j * sheight * stride;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GRADIENT:
|
|
|
|
dst = (uint16_t *)p->data[i] + j * sheight * stride;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
|
|
|
top = dst[-fake_stride];
|
|
|
|
left = top + dst[0];
|
2017-07-09 18:40:13 +02:00
|
|
|
dst[0] = left & max;
|
2016-12-20 00:21:49 +02:00
|
|
|
for (x = 1; x < width; x++) {
|
|
|
|
top = dst[x - fake_stride];
|
|
|
|
lefttop = dst[x - (fake_stride + 1)];
|
|
|
|
left += top - lefttop + dst[x];
|
2017-07-09 18:40:13 +02:00
|
|
|
dst[x] = left & max;
|
2016-12-20 00:21:49 +02:00
|
|
|
}
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MEDIAN:
|
|
|
|
dst = (uint16_t *)p->data[i] + j * sheight * stride;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-07-09 18:40:13 +02:00
|
|
|
s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
|
2016-12-20 00:21:49 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
2017-10-28 23:17:44 +02:00
|
|
|
lefttop = left = dst[0];
|
2016-12-20 00:21:49 +02:00
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
2017-07-09 18:40:13 +02:00
|
|
|
magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
|
2016-12-20 00:21:49 +02:00
|
|
|
lefttop = left = dst[0];
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->decorrelate) {
|
|
|
|
int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
|
|
|
|
int width = avctx->coded_width;
|
|
|
|
uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
|
|
|
|
uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
|
|
|
|
uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
for (k = 0; k < width; k++) {
|
2017-07-09 18:40:13 +02:00
|
|
|
b[k] = (b[k] + g[k]) & max;
|
|
|
|
r[k] = (r[k] + g[k]) & max;
|
2016-12-20 00:21:49 +02:00
|
|
|
}
|
|
|
|
b += p->linesize[0] / 2;
|
|
|
|
g += p->linesize[1] / 2;
|
|
|
|
r += p->linesize[2] / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
|
|
|
|
int j, int threadnr)
|
2016-05-22 15:18:30 +02:00
|
|
|
{
|
|
|
|
MagicYUVContext *s = avctx->priv_data;
|
|
|
|
int interlaced = s->interlaced;
|
|
|
|
AVFrame *p = s->p;
|
2017-12-09 20:26:47 +02:00
|
|
|
int i, k, x, min_width;
|
2016-06-19 00:23:28 +02:00
|
|
|
GetBitContext gb;
|
2016-05-22 15:18:30 +02:00
|
|
|
uint8_t *dst;
|
|
|
|
|
|
|
|
for (i = 0; i < s->planes; i++) {
|
2016-06-19 00:23:28 +02:00
|
|
|
int left, lefttop, top;
|
2016-05-22 15:18:30 +02:00
|
|
|
int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
|
|
|
|
int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
|
|
|
|
int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
|
2016-06-19 00:23:28 +02:00
|
|
|
ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
|
|
|
|
ptrdiff_t stride = p->linesize[i];
|
2020-09-23 06:42:24 +02:00
|
|
|
const uint8_t *slice = s->buf + s->slices[i][j].start;
|
2016-06-02 06:59:46 +02:00
|
|
|
int flags, pred;
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 06:42:24 +02:00
|
|
|
flags = bytestream_get_byte(&slice);
|
|
|
|
pred = bytestream_get_byte(&slice);
|
2016-06-02 06:59:46 +02:00
|
|
|
|
2016-05-22 15:18:30 +02:00
|
|
|
dst = p->data[i] + j * sheight * stride;
|
2016-06-02 06:59:46 +02:00
|
|
|
if (flags & 1) {
|
2020-09-23 06:42:24 +02:00
|
|
|
if (s->slices[i][j].size - 2 < width * height)
|
2018-06-23 23:37:10 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-02 06:59:46 +02:00
|
|
|
for (k = 0; k < height; k++) {
|
2020-09-23 06:42:24 +02:00
|
|
|
bytestream_get_buffer(&slice, dst, width);
|
2016-06-02 06:59:46 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-23 06:42:24 +02:00
|
|
|
int ret = init_get_bits8(&gb, slice, s->slices[i][j].size - 2);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-06-02 06:59:46 +02:00
|
|
|
for (k = 0; k < height; k++) {
|
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
int pix;
|
2016-06-19 00:23:28 +02:00
|
|
|
if (get_bits_left(&gb) <= 0)
|
2016-06-02 06:59:46 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-19 00:23:28 +02:00
|
|
|
|
|
|
|
pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
|
|
|
|
if (pix < 0)
|
2016-06-02 06:59:46 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-19 00:23:28 +02:00
|
|
|
|
2020-08-31 16:19:55 +02:00
|
|
|
dst[x] = pix;
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
2016-06-02 06:59:46 +02:00
|
|
|
dst += stride;
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
switch (pred) {
|
|
|
|
case LEFT:
|
2016-05-22 15:18:30 +02:00
|
|
|
dst = p->data[i] + j * sheight * stride;
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
2016-06-19 00:23:28 +02:00
|
|
|
break;
|
|
|
|
case GRADIENT:
|
2016-05-22 15:18:30 +02:00
|
|
|
dst = p->data[i] + j * sheight * stride;
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
2017-12-09 20:26:47 +02:00
|
|
|
min_width = FFMIN(width, 32);
|
2016-05-22 15:18:30 +02:00
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
|
|
|
top = dst[-fake_stride];
|
|
|
|
left = top + dst[0];
|
|
|
|
dst[0] = left;
|
2017-12-09 20:26:47 +02:00
|
|
|
for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
|
2016-05-22 15:18:30 +02:00
|
|
|
top = dst[x - fake_stride];
|
|
|
|
lefttop = dst[x - (fake_stride + 1)];
|
|
|
|
left += top - lefttop + dst[x];
|
|
|
|
dst[x] = left;
|
|
|
|
}
|
2017-12-09 20:26:47 +02:00
|
|
|
if (width > 32)
|
|
|
|
s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
2016-06-19 00:23:28 +02:00
|
|
|
break;
|
|
|
|
case MEDIAN:
|
2016-05-22 15:18:30 +02:00
|
|
|
dst = p->data[i] + j * sheight * stride;
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
if (interlaced) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_left_pred(dst, dst, width, 0);
|
2016-05-22 15:18:30 +02:00
|
|
|
dst += stride;
|
|
|
|
}
|
2017-10-28 23:17:44 +02:00
|
|
|
lefttop = left = dst[0];
|
2016-05-22 15:18:30 +02:00
|
|
|
for (k = 1 + interlaced; k < height; k++) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_median_pred(dst, dst - fake_stride,
|
2016-06-19 00:23:28 +02:00
|
|
|
dst, width, &left, &lefttop);
|
2016-05-22 15:18:30 +02:00
|
|
|
lefttop = left = dst[0];
|
|
|
|
dst += stride;
|
|
|
|
}
|
2016-06-19 00:23:28 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->decorrelate) {
|
|
|
|
int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
|
|
|
|
int width = avctx->coded_width;
|
|
|
|
uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
|
|
|
|
uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
|
|
|
|
uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
2017-01-08 00:04:39 +02:00
|
|
|
s->llviddsp.add_bytes(b, g, width);
|
|
|
|
s->llviddsp.add_bytes(r, g, width);
|
2016-05-22 15:18:30 +02:00
|
|
|
b += p->linesize[0];
|
|
|
|
g += p->linesize[1];
|
|
|
|
r += p->linesize[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:42:24 +02:00
|
|
|
static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
|
|
|
|
int table_size, int max)
|
2016-12-20 00:21:49 +02:00
|
|
|
{
|
|
|
|
MagicYUVContext *s = avctx->priv_data;
|
2020-09-23 06:42:24 +02:00
|
|
|
GetByteContext gb;
|
2020-11-09 06:11:31 +02:00
|
|
|
uint8_t len[4096];
|
2020-09-23 15:17:33 +02:00
|
|
|
uint16_t length_count[33] = { 0 };
|
2016-12-20 00:21:49 +02:00
|
|
|
int i = 0, j = 0, k;
|
|
|
|
|
2020-09-23 06:42:24 +02:00
|
|
|
bytestream2_init(&gb, table, table_size);
|
|
|
|
|
|
|
|
while (bytestream2_get_bytes_left(&gb) > 0) {
|
|
|
|
int b = bytestream2_peek_byteu(&gb) & 0x80;
|
|
|
|
int x = bytestream2_get_byteu(&gb) & ~0x80;
|
2020-09-23 05:42:31 +02:00
|
|
|
int l = 1;
|
2016-12-20 00:21:49 +02:00
|
|
|
|
2020-09-23 05:42:31 +02:00
|
|
|
if (b) {
|
2020-09-23 06:42:24 +02:00
|
|
|
if (bytestream2_get_bytes_left(&gb) <= 0)
|
2020-09-23 05:42:31 +02:00
|
|
|
break;
|
2020-09-23 06:42:24 +02:00
|
|
|
l += bytestream2_get_byteu(&gb);
|
2020-09-23 05:42:31 +02:00
|
|
|
}
|
2020-08-31 19:47:50 +02:00
|
|
|
k = j + l;
|
2020-08-31 21:14:54 +02:00
|
|
|
if (k > max || x == 0 || x > 32) {
|
2020-08-31 19:47:50 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:17:33 +02:00
|
|
|
length_count[x] += l;
|
2020-09-23 16:50:02 +02:00
|
|
|
for (; j < k; j++)
|
2020-11-09 06:11:31 +02:00
|
|
|
len[j] = x;
|
2016-12-20 00:21:49 +02:00
|
|
|
|
|
|
|
if (j == max) {
|
|
|
|
j = 0;
|
2020-11-09 06:11:31 +02:00
|
|
|
if (huff_build(len, length_count, &s->vlc[i], max, avctx)) {
|
2016-12-20 00:21:49 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if (i == s->planes) {
|
|
|
|
break;
|
|
|
|
}
|
2020-09-23 15:17:33 +02:00
|
|
|
memset(length_count, 0, sizeof(length_count));
|
2016-12-20 00:21:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != s->planes) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
static int magy_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2016-05-22 15:18:30 +02:00
|
|
|
{
|
|
|
|
MagicYUVContext *s = avctx->priv_data;
|
|
|
|
AVFrame *p = data;
|
2020-09-23 08:28:59 +02:00
|
|
|
GetByteContext gb;
|
2016-06-19 00:23:28 +02:00
|
|
|
uint32_t first_offset, offset, next_offset, header_size, slice_width;
|
|
|
|
int width, height, format, version, table_size;
|
2016-12-20 00:21:49 +02:00
|
|
|
int ret, i, j;
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
if (avpkt->size < 36)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
bytestream2_init(&gb, avpkt->data, avpkt->size);
|
|
|
|
if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
header_size = bytestream2_get_le32u(&gb);
|
2016-06-19 00:23:28 +02:00
|
|
|
if (header_size < 32 || header_size >= avpkt->size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"header or packet too small %"PRIu32"\n", header_size);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-19 00:23:28 +02:00
|
|
|
}
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
version = bytestream2_get_byteu(&gb);
|
2016-05-22 15:18:30 +02:00
|
|
|
if (version != 7) {
|
2016-06-19 00:23:28 +02:00
|
|
|
avpriv_request_sample(avctx, "Version %d", version);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
s->hshift[1] =
|
|
|
|
s->vshift[1] =
|
|
|
|
s->hshift[2] =
|
|
|
|
s->vshift[2] = 0;
|
2016-05-22 15:18:30 +02:00
|
|
|
s->decorrelate = 0;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->bps = 8;
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
format = bytestream2_get_byteu(&gb);
|
2016-05-22 15:18:30 +02:00
|
|
|
switch (format) {
|
|
|
|
case 0x65:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP;
|
|
|
|
s->decorrelate = 1;
|
|
|
|
break;
|
|
|
|
case 0x66:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRAP;
|
|
|
|
s->decorrelate = 1;
|
|
|
|
break;
|
|
|
|
case 0x67:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV444P;
|
|
|
|
break;
|
|
|
|
case 0x68:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV422P;
|
2016-06-19 00:23:28 +02:00
|
|
|
s->hshift[1] =
|
|
|
|
s->hshift[2] = 1;
|
2016-05-22 15:18:30 +02:00
|
|
|
break;
|
|
|
|
case 0x69:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
2016-06-19 00:23:28 +02:00
|
|
|
s->hshift[1] =
|
|
|
|
s->vshift[1] =
|
|
|
|
s->hshift[2] =
|
|
|
|
s->vshift[2] = 1;
|
2016-05-22 15:18:30 +02:00
|
|
|
break;
|
|
|
|
case 0x6a:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
|
|
|
|
break;
|
|
|
|
case 0x6b:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY8;
|
|
|
|
break;
|
2016-12-20 00:21:49 +02:00
|
|
|
case 0x6c:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
|
|
|
|
s->hshift[1] =
|
|
|
|
s->hshift[2] = 1;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->bps = 10;
|
2016-12-20 00:21:49 +02:00
|
|
|
break;
|
2019-07-11 16:53:09 +02:00
|
|
|
case 0x76:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
|
|
|
|
s->bps = 10;
|
|
|
|
break;
|
2016-12-20 00:21:49 +02:00
|
|
|
case 0x6d:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP10;
|
|
|
|
s->decorrelate = 1;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->bps = 10;
|
2016-12-20 00:21:49 +02:00
|
|
|
break;
|
|
|
|
case 0x6e:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
|
|
|
|
s->decorrelate = 1;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->bps = 10;
|
|
|
|
break;
|
|
|
|
case 0x6f:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP12;
|
|
|
|
s->decorrelate = 1;
|
|
|
|
s->bps = 12;
|
|
|
|
break;
|
|
|
|
case 0x70:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
|
|
|
|
s->decorrelate = 1;
|
|
|
|
s->bps = 12;
|
2016-12-20 00:21:49 +02:00
|
|
|
break;
|
|
|
|
case 0x73:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY10;
|
2017-07-09 18:40:13 +02:00
|
|
|
s->bps = 10;
|
2016-12-20 00:21:49 +02:00
|
|
|
break;
|
2020-09-22 18:31:14 +02:00
|
|
|
case 0x7b:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
|
|
|
|
s->hshift[1] =
|
|
|
|
s->vshift[1] =
|
|
|
|
s->hshift[2] =
|
|
|
|
s->vshift[2] = 1;
|
|
|
|
s->bps = 10;
|
|
|
|
break;
|
2016-05-22 15:18:30 +02:00
|
|
|
default:
|
2016-06-19 00:23:28 +02:00
|
|
|
avpriv_request_sample(avctx, "Format 0x%X", format);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
2019-09-24 03:13:52 +02:00
|
|
|
s->max = 1 << s->bps;
|
|
|
|
s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
|
2016-06-19 00:23:28 +02:00
|
|
|
s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
bytestream2_skipu(&gb, 1);
|
|
|
|
s->color_matrix = bytestream2_get_byteu(&gb);
|
|
|
|
s->flags = bytestream2_get_byteu(&gb);
|
2016-12-26 18:34:57 +02:00
|
|
|
s->interlaced = !!(s->flags & 2);
|
2020-09-23 08:28:59 +02:00
|
|
|
bytestream2_skipu(&gb, 3);
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
width = bytestream2_get_le32u(&gb);
|
|
|
|
height = bytestream2_get_le32u(&gb);
|
2016-06-19 00:23:28 +02:00
|
|
|
ret = ff_set_dimensions(avctx, width, height);
|
|
|
|
if (ret < 0)
|
2016-06-19 09:43:22 +02:00
|
|
|
return ret;
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
slice_width = bytestream2_get_le32u(&gb);
|
2016-05-22 15:18:30 +02:00
|
|
|
if (slice_width != avctx->coded_width) {
|
2016-06-19 00:23:28 +02:00
|
|
|
avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
2020-09-23 08:28:59 +02:00
|
|
|
s->slice_height = bytestream2_get_le32u(&gb);
|
2016-07-14 18:39:46 +02:00
|
|
|
if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
|
2016-06-19 00:23:28 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"invalid slice height: %d\n", s->slice_height);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
bytestream2_skipu(&gb, 4);
|
2016-05-22 15:18:30 +02:00
|
|
|
|
|
|
|
s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
|
2020-09-23 08:28:59 +02:00
|
|
|
if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
|
2016-06-19 00:23:28 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"invalid number of slices: %d\n", s->nb_slices);
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:42:30 +02:00
|
|
|
if (s->interlaced) {
|
|
|
|
if ((s->slice_height >> s->vshift[1]) < 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "impossible height\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-05-22 15:18:30 +02:00
|
|
|
for (i = 0; i < s->planes; i++) {
|
|
|
|
av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
|
|
|
|
if (!s->slices[i])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
offset = bytestream2_get_le32u(&gb);
|
2016-05-22 15:18:30 +02:00
|
|
|
if (offset >= avpkt->size - header_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
first_offset = offset;
|
|
|
|
|
|
|
|
for (j = 0; j < s->nb_slices - 1; j++) {
|
|
|
|
s->slices[i][j].start = offset + header_size;
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
next_offset = bytestream2_get_le32u(&gb);
|
2016-06-19 00:23:28 +02:00
|
|
|
if (next_offset <= offset || next_offset >= avpkt->size - header_size)
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-19 00:23:28 +02:00
|
|
|
|
|
|
|
s->slices[i][j].size = next_offset - offset;
|
2020-09-23 06:24:03 +02:00
|
|
|
if (s->slices[i][j].size < 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-06-19 00:23:28 +02:00
|
|
|
offset = next_offset;
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->slices[i][j].start = offset + header_size;
|
2016-06-02 22:40:57 +02:00
|
|
|
s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
|
2020-10-23 20:39:33 +02:00
|
|
|
|
|
|
|
if (s->slices[i][j].size < 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
if (bytestream2_get_byteu(&gb) != s->planes)
|
2016-05-22 15:18:30 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
bytestream2_skipu(&gb, s->nb_slices * s->planes);
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
table_size = header_size + first_offset - bytestream2_tell(&gb);
|
2016-05-22 15:18:30 +02:00
|
|
|
if (table_size < 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2020-09-23 08:28:59 +02:00
|
|
|
ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
|
2020-09-23 06:42:24 +02:00
|
|
|
table_size, s->max);
|
2016-12-20 00:21:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-05-22 15:18:30 +02:00
|
|
|
|
|
|
|
p->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
p->key_frame = 1;
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
|
2016-05-22 15:18:30 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->buf = avpkt->data;
|
|
|
|
s->p = p;
|
2016-12-20 00:21:49 +02:00
|
|
|
avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2016-12-20 00:21:49 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
|
2017-07-09 18:40:13 +02:00
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
|
2016-05-22 15:18:30 +02:00
|
|
|
FFSWAP(uint8_t*, p->data[0], p->data[1]);
|
|
|
|
FFSWAP(int, p->linesize[0], p->linesize[1]);
|
2016-12-26 18:34:57 +02:00
|
|
|
} else {
|
|
|
|
switch (s->color_matrix) {
|
|
|
|
case 1:
|
|
|
|
p->colorspace = AVCOL_SPC_BT470BG;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
p->colorspace = AVCOL_SPC_BT709;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
|
2016-05-22 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
static av_cold int magy_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MagicYUVContext *s = avctx->priv_data;
|
2017-01-08 16:48:05 +02:00
|
|
|
ff_llviddsp_init(&s->llviddsp);
|
2016-06-19 00:23:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int magy_decode_end(AVCodecContext *avctx)
|
2016-05-22 15:18:30 +02:00
|
|
|
{
|
|
|
|
MagicYUVContext * const s = avctx->priv_data;
|
2016-06-19 00:23:28 +02:00
|
|
|
int i;
|
2016-05-22 15:18:30 +02:00
|
|
|
|
2016-06-19 00:23:28 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
|
|
|
|
av_freep(&s->slices[i]);
|
|
|
|
s->slices_size[i] = 0;
|
|
|
|
ff_free_vlc(&s->vlc[i]);
|
|
|
|
}
|
2016-05-22 15:18:30 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-25 11:50:26 +02:00
|
|
|
const AVCodec ff_magicyuv_decoder = {
|
2016-05-22 15:18:30 +02:00
|
|
|
.name = "magicyuv",
|
2016-06-19 00:23:28 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
|
2016-05-22 15:18:30 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_MAGICYUV,
|
|
|
|
.priv_data_size = sizeof(MagicYUVContext),
|
2016-06-19 00:23:28 +02:00
|
|
|
.init = magy_decode_init,
|
|
|
|
.close = magy_decode_end,
|
|
|
|
.decode = magy_decode_frame,
|
|
|
|
.capabilities = AV_CODEC_CAP_DR1 |
|
|
|
|
AV_CODEC_CAP_FRAME_THREADS |
|
|
|
|
AV_CODEC_CAP_SLICE_THREADS,
|
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
|
2016-05-22 15:18:30 +02:00
|
|
|
};
|