../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 149
0.0%
Branches:
0 / 74
0.0%
Functions:
0 / 5
0.0%

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