2 * MidiVid Archive codec
4 * Copyright (c) 2019 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #define CACHED_BITSTREAM_READER !ARCH_X86_32
28 #include "libavutil/intreadwrite.h"
31 #include "bytestream.h"
34 #include "lossless_videodsp.h"
38 typedef struct MVHAContext {
47 LLVidDSPContext llviddsp;
57 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
58 Node *nodes, int node,
59 uint32_t pfx, int pl, int *pos)
65 bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
66 lens[*pos] = FFMAX(pl, 1);
67 xlat[*pos] = s + (pl == 0);
72 get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
75 get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
80 static int build_vlc(AVCodecContext *avctx, VLC *vlc)
82 MVHAContext *s = avctx->priv_data;
87 int cur_node, i, j, pos = 0;
91 for (i = 0; i < s->nb_symbols; i++) {
92 nodes[i].count = s->prob[i];
93 nodes[i].sym = s->symb[i];
99 cur_node = s->nb_symbols;
104 int first_node = cur_node;
105 int second_node = cur_node;
108 nodes[cur_node].count = -1;
111 int val = nodes[new_node].count;
112 if (val && (val < nodes[first_node].count)) {
113 if (val >= nodes[second_node].count) {
114 first_node = new_node;
116 first_node = second_node;
117 second_node = new_node;
121 } while (new_node != cur_node);
123 if (first_node == cur_node)
126 nd = nodes[second_node].count;
127 st = nodes[first_node].count;
128 nodes[second_node].count = 0;
129 nodes[first_node].count = 0;
130 if (nd >= UINT32_MAX - st) {
131 av_log(avctx, AV_LOG_ERROR, "count overflow\n");
132 return AVERROR_INVALIDDATA;
134 nodes[cur_node].count = nd + st;
135 nodes[cur_node].sym = -1;
136 nodes[cur_node].n0 = cur_node;
137 nodes[cur_node].l = first_node;
138 nodes[cur_node].r = second_node;
142 } while (cur_node - s->nb_symbols == j);
144 get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
146 return ff_init_vlc_sparse(vlc, 12, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
149 static int decode_frame(AVCodecContext *avctx,
150 void *data, int *got_frame,
153 MVHAContext *s = avctx->priv_data;
154 AVFrame *frame = data;
158 if (avpkt->size <= 8)
159 return AVERROR_INVALIDDATA;
161 type = AV_RB32(avpkt->data);
162 size = AV_RL32(avpkt->data + 4);
164 if (size < 1 || size >= avpkt->size)
165 return AVERROR_INVALIDDATA;
167 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
170 if (type == MKTAG('L','Z','Y','V')) {
171 ret = inflateReset(&s->zstream);
173 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
174 return AVERROR_EXTERNAL;
177 s->zstream.next_in = avpkt->data + 8;
178 s->zstream.avail_in = avpkt->size - 8;
180 for (int p = 0; p < 3; p++) {
181 for (int y = 0; y < avctx->height; y++) {
182 s->zstream.next_out = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
183 s->zstream.avail_out = avctx->width >> (p > 0);
185 ret = inflate(&s->zstream, Z_SYNC_FLUSH);
186 if (ret != Z_OK && ret != Z_STREAM_END) {
187 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
188 return AVERROR_EXTERNAL;
192 } else if (type == MKTAG('H','U','F','Y')) {
193 GetBitContext *gb = &s->gb;
194 int first_symbol, symbol;
196 ret = init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);
202 first_symbol = get_bits(gb, 8);
203 s->nb_symbols = get_bits(gb, 8) + 1;
205 symbol = first_symbol;
206 for (int i = 0; i < s->nb_symbols; symbol++) {
209 if (get_bits_left(gb) < 4)
210 return AVERROR_INVALIDDATA;
213 prob = get_bits(gb, 12);
215 prob = get_bits(gb, 3);
225 ret = build_vlc(avctx, &s->vlc);
229 for (int p = 0; p < 3; p++) {
230 int width = avctx->width >> (p > 0);
231 ptrdiff_t stride = frame->linesize[p];
234 dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
235 for (int y = 0; y < avctx->height; y++) {
236 if (get_bits_left(gb) < width)
237 return AVERROR_INVALIDDATA;
238 for (int x = 0; x < width; x++) {
239 int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
242 return AVERROR_INVALIDDATA;
250 return AVERROR_INVALIDDATA;
253 for (int p = 0; p < 3; p++) {
255 int width = avctx->width >> (p > 0);
256 ptrdiff_t stride = frame->linesize[p];
259 dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
260 s->llviddsp.add_left_pred(dst, dst, width, 0);
261 if (avctx->height > 1) {
263 lefttop = left = dst[0];
264 for (int y = 1; y < avctx->height; y++) {
265 s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
266 lefttop = left = dst[0];
272 frame->pict_type = AV_PICTURE_TYPE_I;
273 frame->key_frame = 1;
279 static av_cold int decode_init(AVCodecContext *avctx)
281 MVHAContext *s = avctx->priv_data;
284 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
286 s->zstream.zalloc = Z_NULL;
287 s->zstream.zfree = Z_NULL;
288 s->zstream.opaque = Z_NULL;
289 zret = inflateInit(&s->zstream);
291 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
292 return AVERROR_EXTERNAL;
295 ff_llviddsp_init(&s->llviddsp);
300 static av_cold int decode_close(AVCodecContext *avctx)
302 MVHAContext *s = avctx->priv_data;
304 inflateEnd(&s->zstream);
305 ff_free_vlc(&s->vlc);
310 AVCodec ff_mvha_decoder = {
312 .long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
313 .type = AVMEDIA_TYPE_VIDEO,
314 .id = AV_CODEC_ID_MVHA,
315 .priv_data_size = sizeof(MVHAContext),
317 .close = decode_close,
318 .decode = decode_frame,
319 .capabilities = AV_CODEC_CAP_DR1,
320 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
321 FF_CODEC_CAP_INIT_CLEANUP,