1 /*
2 * MidiVid Archive codec
3 *
4 * Copyright (c) 2019 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define CACHED_BITSTREAM_READER !ARCH_X86_32
28 #include "libavutil/intreadwrite.h"
29
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33 #include "get_bits.h"
34 #include "internal.h"
35 #include "lossless_videodsp.h"
36 #include "zlib_wrapper.h"
37
38 #include <zlib.h>
39
40 typedef struct MVHAContext {
41 GetBitContext gb;
42 int nb_symbols;
43
44 uint8_t symb[256];
45 uint32_t prob[256];
46 VLC vlc;
47
48 FFZStream zstream;
49 LLVidDSPContext llviddsp;
50 } MVHAContext;
51
52 typedef struct Node {
53 int16_t sym;
54 int16_t n0;
55 int16_t l, r;
56 uint32_t count;
57 } Node;
58
get_tree_codes(uint32_t * bits,int16_t * lens,uint8_t * xlat,Node * nodes,int node,uint32_t pfx,int pl,int * pos)59 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
60 Node *nodes, int node,
61 uint32_t pfx, int pl, int *pos)
62 {
63 int s;
64
65 s = nodes[node].sym;
66 if (s != -1) {
67 bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
68 lens[*pos] = FFMAX(pl, 1);
69 xlat[*pos] = s + (pl == 0);
70 (*pos)++;
71 } else {
72 pfx <<= 1;
73 pl++;
74 get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
75 pos);
76 pfx |= 1;
77 get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
78 pos);
79 }
80 }
81
build_vlc(AVCodecContext * avctx,VLC * vlc)82 static int build_vlc(AVCodecContext *avctx, VLC *vlc)
83 {
84 MVHAContext *s = avctx->priv_data;
85 Node nodes[512];
86 uint32_t bits[256];
87 int16_t lens[256];
88 uint8_t xlat[256];
89 int cur_node, i, j, pos = 0;
90
91 ff_free_vlc(vlc);
92
93 for (i = 0; i < s->nb_symbols; i++) {
94 nodes[i].count = s->prob[i];
95 nodes[i].sym = s->symb[i];
96 nodes[i].n0 = -2;
97 nodes[i].l = i;
98 nodes[i].r = i;
99 }
100
101 cur_node = s->nb_symbols;
102 j = 0;
103 do {
104 for (i = 0; ; i++) {
105 int new_node = j;
106 int first_node = cur_node;
107 int second_node = cur_node;
108 unsigned nd, st;
109
110 nodes[cur_node].count = -1;
111
112 do {
113 int val = nodes[new_node].count;
114 if (val && (val < nodes[first_node].count)) {
115 if (val >= nodes[second_node].count) {
116 first_node = new_node;
117 } else {
118 first_node = second_node;
119 second_node = new_node;
120 }
121 }
122 new_node += 1;
123 } while (new_node != cur_node);
124
125 if (first_node == cur_node)
126 break;
127
128 nd = nodes[second_node].count;
129 st = nodes[first_node].count;
130 nodes[second_node].count = 0;
131 nodes[first_node].count = 0;
132 if (nd >= UINT32_MAX - st) {
133 av_log(avctx, AV_LOG_ERROR, "count overflow\n");
134 return AVERROR_INVALIDDATA;
135 }
136 nodes[cur_node].count = nd + st;
137 nodes[cur_node].sym = -1;
138 nodes[cur_node].n0 = cur_node;
139 nodes[cur_node].l = first_node;
140 nodes[cur_node].r = second_node;
141 cur_node++;
142 }
143 j++;
144 } while (cur_node - s->nb_symbols == j);
145
146 get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
147
148 return ff_init_vlc_sparse(vlc, 12, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
149 }
150
decode_frame(AVCodecContext * avctx,AVFrame * frame,int * got_frame,AVPacket * avpkt)151 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
152 int *got_frame, AVPacket *avpkt)
153 {
154 MVHAContext *s = avctx->priv_data;
155 uint32_t type, size;
156 int ret;
157
158 if (avpkt->size <= 8)
159 return AVERROR_INVALIDDATA;
160
161 type = AV_RB32(avpkt->data);
162 size = AV_RL32(avpkt->data + 4);
163
164 if (size < 1 || size >= avpkt->size)
165 return AVERROR_INVALIDDATA;
166
167 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
168 return ret;
169
170 if (type == MKTAG('L','Z','Y','V')) {
171 z_stream *const zstream = &s->zstream.zstream;
172 ret = inflateReset(zstream);
173 if (ret != Z_OK) {
174 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
175 return AVERROR_EXTERNAL;
176 }
177
178 zstream->next_in = avpkt->data + 8;
179 zstream->avail_in = avpkt->size - 8;
180
181 for (int p = 0; p < 3; p++) {
182 for (int y = 0; y < avctx->height; y++) {
183 zstream->next_out = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
184 zstream->avail_out = avctx->width >> (p > 0);
185
186 ret = inflate(zstream, Z_SYNC_FLUSH);
187 if (ret != Z_OK && ret != Z_STREAM_END) {
188 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
189 return AVERROR_EXTERNAL;
190 }
191 }
192 }
193 } else if (type == MKTAG('H','U','F','Y')) {
194 GetBitContext *gb = &s->gb;
195 int first_symbol, symbol;
196
197 ret = init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);
198 if (ret < 0)
199 return ret;
200
201 skip_bits(gb, 24);
202
203 first_symbol = get_bits(gb, 8);
204 s->nb_symbols = get_bits(gb, 8) + 1;
205
206 symbol = first_symbol;
207 for (int i = 0; i < s->nb_symbols; symbol++) {
208 int prob;
209
210 if (get_bits_left(gb) < 4)
211 return AVERROR_INVALIDDATA;
212
213 if (get_bits1(gb)) {
214 prob = get_bits(gb, 12);
215 } else {
216 prob = get_bits(gb, 3);
217 }
218
219 if (prob) {
220 s->symb[i] = symbol;
221 s->prob[i] = prob;
222 i++;
223 }
224 }
225
226 ret = build_vlc(avctx, &s->vlc);
227 if (ret < 0)
228 return ret;
229
230 for (int p = 0; p < 3; p++) {
231 int width = avctx->width >> (p > 0);
232 ptrdiff_t stride = frame->linesize[p];
233 uint8_t *dst;
234
235 dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
236 for (int y = 0; y < avctx->height; y++) {
237 if (get_bits_left(gb) < width)
238 return AVERROR_INVALIDDATA;
239 for (int x = 0; x < width; x++) {
240 int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
241
242 if (v < 0)
243 return AVERROR_INVALIDDATA;
244
245 dst[x] = v;
246 }
247 dst -= stride;
248 }
249 }
250 } else {
251 return AVERROR_INVALIDDATA;
252 }
253
254 for (int p = 0; p < 3; p++) {
255 int left, lefttop;
256 int width = avctx->width >> (p > 0);
257 ptrdiff_t stride = frame->linesize[p];
258 uint8_t *dst;
259
260 dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
261 s->llviddsp.add_left_pred(dst, dst, width, 0);
262 if (avctx->height > 1) {
263 dst -= stride;
264 lefttop = left = dst[0];
265 for (int y = 1; y < avctx->height; y++) {
266 s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
267 lefttop = left = dst[0];
268 dst -= stride;
269 }
270 }
271 }
272
273 frame->pict_type = AV_PICTURE_TYPE_I;
274 frame->key_frame = 1;
275 *got_frame = 1;
276
277 return avpkt->size;
278 }
279
decode_init(AVCodecContext * avctx)280 static av_cold int decode_init(AVCodecContext *avctx)
281 {
282 MVHAContext *s = avctx->priv_data;
283
284 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
285
286 ff_llviddsp_init(&s->llviddsp);
287
288 return ff_inflate_init(&s->zstream, avctx);
289 }
290
decode_close(AVCodecContext * avctx)291 static av_cold int decode_close(AVCodecContext *avctx)
292 {
293 MVHAContext *s = avctx->priv_data;
294
295 ff_inflate_end(&s->zstream);
296 ff_free_vlc(&s->vlc);
297
298 return 0;
299 }
300
301 const FFCodec ff_mvha_decoder = {
302 .p.name = "mvha",
303 .p.long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
304 .p.type = AVMEDIA_TYPE_VIDEO,
305 .p.id = AV_CODEC_ID_MVHA,
306 .priv_data_size = sizeof(MVHAContext),
307 .init = decode_init,
308 .close = decode_close,
309 FF_CODEC_DECODE_CB(decode_frame),
310 .p.capabilities = AV_CODEC_CAP_DR1,
311 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
312 FF_CODEC_CAP_INIT_CLEANUP,
313 };
314