History

avcodec: add MV30 decoder

Paul B Mahol committed 6 years ago

Commit 481ebb11 parent df08db08 files · +763 −2Browse files

Unified · Split

8 files
  1. Changelog +1 −0
  2. libavcodec/Makefile +1 −0
  3. libavcodec/allcodecs.c +1 −0
  4. libavcodec/avcodec.h +1 −0
  5. libavcodec/codec_desc.c +7 −0
  6. libavcodec/mv30.c +749 −0
  7. libavcodec/version.h +2 −2
  8. libavformat/riff.c +1 −0
Changelog+1−0
@@ -56,6 +56,7 @@ version <next>: · 55 unmodified lines
5656 - CRI HCA demuxer
5757 - overlay_cuda filter
5858 - switch from AvxSynth to AviSynth+ on Linux
59+- mv30 decoder
5960
6061
6162 version 4.2:
libavcodec/Makefile+1−0
@@ -494,6 +494,7 @@ OBJS-$(CONFIG_MSVIDEO1_DECODER) += msvideo1.o · 493 unmodified lines
494494 OBJS-$(CONFIG_MSVIDEO1_ENCODER) += msvideo1enc.o elbg.o
495495 OBJS-$(CONFIG_MSZH_DECODER) += lcldec.o
496496 OBJS-$(CONFIG_MTS2_DECODER) += mss4.o
497+OBJS-$(CONFIG_MV30_DECODER) += mv30.o
497498 OBJS-$(CONFIG_MVC1_DECODER) += mvcdec.o
498499 OBJS-$(CONFIG_MVC2_DECODER) += mvcdec.o
499500 OBJS-$(CONFIG_MVDV_DECODER) += midivid.o
libavcodec/allcodecs.c+1−0
@@ -217,6 +217,7 @@ extern AVCodec ff_msvideo1_encoder; · 216 unmodified lines
217217 extern AVCodec ff_msvideo1_decoder;
218218 extern AVCodec ff_mszh_decoder;
219219 extern AVCodec ff_mts2_decoder;
220+extern AVCodec ff_mv30_decoder;
220221 extern AVCodec ff_mvc1_decoder;
221222 extern AVCodec ff_mvc2_decoder;
222223 extern AVCodec ff_mvdv_decoder;
libavcodec/avcodec.h+1−0
@@ -463,6 +463,7 @@ enum AVCodecID { · 462 unmodified lines
463463 AV_CODEC_ID_MVDV,
464464 AV_CODEC_ID_MVHA,
465465 AV_CODEC_ID_CDTOONS,
466+ AV_CODEC_ID_MV30,
466467
467468 /* various PCM "codecs" */
468469 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
libavcodec/codec_desc.c+7−0
@@ -1754,6 +1754,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1753 unmodified lines
17541754 .long_name = NULL_IF_CONFIG_SMALL("CDToons video"),
17551755 .props = AV_CODEC_PROP_LOSSLESS,
17561756 },
1757+ {
1758+ .id = AV_CODEC_ID_MV30,
1759+ .type = AVMEDIA_TYPE_VIDEO,
1760+ .name = "mv30",
1761+ .long_name = NULL_IF_CONFIG_SMALL("MidiVid 3.0"),
1762+ .props = AV_CODEC_PROP_LOSSY,
1763+ },
17571764
17581765 /* various PCM "codecs" */
17591766 {
libavcodec/mv30.c+749−0
@@ -0,0 +1,749 @@
1+/*
2+ * MidiVid MV30 decoder
3+ *
4+ * Copyright (c) 2020 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+#include "libavutil/thread.h"
28+
29+#include "avcodec.h"
30+#include "bytestream.h"
31+#include "copy_block.h"
32+#include "mathops.h"
33+#include "blockdsp.h"
34+#include "get_bits.h"
35+#include "internal.h"
36+
37+typedef struct MV30Context {
38+ GetBitContext gb;
39+
40+ int intra_quant;
41+ int inter_quant;
42+ int is_inter;
43+ int mode_size;
44+ int nb_mvectors;
45+
46+ int block[6][64];
47+ int16_t *mvectors;
48+ unsigned int mvectors_size;
49+ int16_t *coeffs;
50+ unsigned int coeffs_size;
51+
52+ int16_t intraq_tab[2][64];
53+ int16_t interq_tab[2][64];
54+
55+ BlockDSPContext bdsp;
56+ AVFrame *prev_frame;
57+} MV30Context;
58+
59+static VLC cbp_tab;
60+
61+static const int16_t scale_tab[] = {
62+ 16384, 22725, 21407, 19266,
63+ 16384, 12873, 8867, 4520,
64+ 22725, 31521, 29692, 26722,
65+ 22725, 17855, 12299, 6270,
66+ 21407, 29692, 27969, 25172,
67+ 21407, 16819, 11585, 5906,
68+ 19266, 26722, 25172, 22654,
69+ 19266, 15137, 10426, 5315,
70+ 16384, 22725, 21407, 19266,
71+ 16384, 12873, 8867, 4520,
72+ 12873, 17855, 16819, 15137,
73+ 12873, 10114, 6967, 3552,
74+ 8867, 12299, 11585, 10426,
75+ 8867, 6967, 4799, 2446,
76+ 4520, 6270, 5906, 5315,
77+ 4520, 3552, 2446, 1247,
78+};
79+
80+static const uint8_t luma_tab[] = {
81+ 12, 12, 15, 19, 25, 34, 40, 48,
82+ 12, 12, 18, 22, 27, 44, 47, 46,
83+ 17, 18, 21, 26, 35, 46, 52, 47,
84+ 18, 20, 24, 28, 40, 61, 59, 51,
85+ 20, 24, 32, 43, 50, 72, 72, 63,
86+ 25, 31, 42, 48, 58, 72, 81, 75,
87+ 38, 46, 54, 61, 71, 84, 88, 85,
88+ 50, 61, 65, 68, 79, 78, 86, 91,
89+ 12, 12, 16, 18, 20, 30, 40, 45,
90+ 12, 12, 16, 18, 30, 40, 45, 50,
91+ 16, 16, 20, 30, 40, 45, 50, 55,
92+ 18, 18, 35, 40, 45, 50, 55, 60,
93+ 20, 30, 40, 45, 50, 55, 60, 65,
94+ 30, 40, 45, 50, 55, 60, 65, 70,
95+ 40, 45, 50, 55, 60, 65, 70, 75,
96+ 45, 50, 55, 60, 65, 70, 75, 80,
97+};
98+
99+static const uint8_t chroma_tab[] = {
100+ 12, 16, 24, 47, 99, 99, 99, 99,
101+ 16, 21, 26, 66, 99, 99, 99, 99,
102+ 24, 26, 56, 99, 99, 99, 99, 99,
103+ 47, 66, 99, 99, 99, 99, 99, 99,
104+ 99, 99, 99, 99, 99, 99, 99, 99,
105+ 99, 99, 99, 99, 99, 99, 99, 99,
106+ 99, 99, 99, 99, 99, 99, 99, 99,
107+ 99, 99, 99, 99, 99, 99, 99, 99,
108+ 12, 16, 20, 24, 28, 36, 40, 44,
109+ 16, 20, 24, 28, 36, 40, 44, 50,
110+ 20, 24, 28, 36, 40, 44, 50, 60,
111+ 24, 28, 36, 40, 44, 50, 60, 80,
112+ 28, 36, 40, 44, 50, 60, 80, 99,
113+ 36, 40, 44, 50, 60, 80, 99, 99,
114+ 40, 44, 50, 60, 80, 99, 99, 99,
115+ 44, 50, 60, 80, 99, 99, 99, 99,
116+};
117+
118+static const uint8_t zigzag[] = {
119+ 0, 1, 8, 9, 16, 2, 3, 10,
120+ 17, 24, 32, 25, 18, 11, 4, 5,
121+ 12, 19, 26, 33, 40, 48, 41, 34,
122+ 27, 20, 13, 6, 7, 14, 21, 28,
123+ 35, 42, 49, 56, 57, 50, 43, 36,
124+ 29, 22, 15, 23, 30, 37, 44, 51,
125+ 58, 59, 52, 45, 38, 31, 39, 46,
126+ 53, 60, 61, 54, 47, 55, 62, 63,
127+};
128+
129+static void get_qtable(int16_t *table, int quant, const uint8_t *quant_tab)
130+{
131+ int factor = quant < 50 ? 5000 / FFMAX(quant, 1) : 200 - FFMIN(quant, 100) * 2;
132+
133+ for (int i = 0; i < 64; i++) {
134+ table[i] = av_clip((quant_tab[i] * factor + 0x32) / 100, 1, 0x7fff);
135+ table[i] = ((int)scale_tab[i] * (int)table[i] + 0x800) >> 12;
136+ }
137+}
138+
139+static inline void idct_1d(int *blk, int step)
140+{
141+ const int t0 = blk[0 * step] + blk[4 * step];
142+ const int t1 = blk[0 * step] - blk[4 * step];
143+ const int t2 = blk[2 * step] + blk[6 * step];
144+ const int t3 = (((blk[2 * step] - blk[6 * step]) * 362) >> 8) - t2;
145+ const int t4 = t0 + t2;
146+ const int t5 = t0 - t2;
147+ const int t6 = t1 + t3;
148+ const int t7 = t1 - t3;
149+ const int t8 = blk[5 * step] + blk[3 * step];
150+ const int t9 = blk[5 * step] - blk[3 * step];
151+ const int tA = blk[1 * step] + blk[7 * step];
152+ const int tB = blk[1 * step] - blk[7 * step];
153+ const int tC = t8 + tA;
154+ const int tD = (tB + t9) * 473 >> 8;
155+ const int tE = ((t9 * -669 >> 8) - tC) + tD;
156+ const int tF = ((tA - t8) * 362 >> 8) - tE;
157+ const int t10 = ((tB * 277 >> 8) - tD) + tF;
158+
159+ blk[0 * step] = t4 + tC;
160+ blk[1 * step] = t6 + tE;
161+ blk[2 * step] = t7 + tF;
162+ blk[3 * step] = t5 - t10;
163+ blk[4 * step] = t5 + t10;
164+ blk[5 * step] = t7 - tF;
165+ blk[6 * step] = t6 - tE;
166+ blk[7 * step] = t4 - tC;
167+}
168+
169+static void idct_put(uint8_t *dst, int stride, int *block)
170+{
171+ for (int i = 0; i < 8; i++) {
172+ if ((block[0x08 + i] |
173+ block[0x10 + i] |
174+ block[0x18 + i] |
175+ block[0x20 + i] |
176+ block[0x28 + i] |
177+ block[0x30 + i] |
178+ block[0x38 + i]) == 0) {
179+ block[0x08 + i] = block[i];
180+ block[0x10 + i] = block[i];
181+ block[0x18 + i] = block[i];
182+ block[0x20 + i] = block[i];
183+ block[0x28 + i] = block[i];
184+ block[0x30 + i] = block[i];
185+ block[0x38 + i] = block[i];
186+ } else {
187+ idct_1d(block + i, 8);
188+ }
189+ }
190+
191+ for (int i = 0; i < 8; i++) {
192+ idct_1d(block, 1);
193+ for (int j = 0; j < 8; j++)
194+ dst[j] = av_clip_uint8((block[j] >> 5) + 128);
195+ block += 8;
196+ dst += stride;
197+ }
198+}
199+
200+static void idct_add(uint8_t *dst, int stride,
201+ const uint8_t *src, int in_linesize, int *block)
202+{
203+ for (int i = 0; i < 8; i++) {
204+ if ((block[0x08 + i] |
205+ block[0x10 + i] |
206+ block[0x18 + i] |
207+ block[0x20 + i] |
208+ block[0x28 + i] |
209+ block[0x30 + i] |
210+ block[0x38 + i]) == 0) {
211+ block[0x08 + i] = block[i];
212+ block[0x10 + i] = block[i];
213+ block[0x18 + i] = block[i];
214+ block[0x20 + i] = block[i];
215+ block[0x28 + i] = block[i];
216+ block[0x30 + i] = block[i];
217+ block[0x38 + i] = block[i];
218+ } else {
219+ idct_1d(block + i, 8);
220+ }
221+ }
222+
223+ for (int i = 0; i < 8; i++) {
224+ idct_1d(block, 1);
225+ for (int j = 0; j < 8; j++)
226+ dst[j] = av_clip_uint8((block[j] >> 5) + src[j]);
227+ block += 8;
228+ dst += stride;
229+ src += in_linesize;
230+ }
231+}
232+
233+static inline void idct2_1d(int *blk, int step)
234+{
235+ const int t0 = blk[0 * step];
236+ const int t1 = blk[1 * step];
237+ const int t2 = t1 * 473 >> 8;
238+ const int t3 = t2 - t1;
239+ const int t4 = (t1 * 362 >> 8) - t3;
240+ const int t5 = ((t1 * 277 >> 8) - t2) + t4;
241+
242+ blk[0 * step] = t1 + t0;
243+ blk[1 * step] = t0 + t3;
244+ blk[2 * step] = t4 + t0;
245+ blk[3 * step] = t0 - t5;
246+ blk[4 * step] = t5 + t0;
247+ blk[5 * step] = t0 - t4;
248+ blk[6 * step] = t0 - t3;
249+ blk[7 * step] = t0 - t1;
250+}
251+
252+static void idct2_put(uint8_t *dst, int stride, int *block)
253+{
254+ for (int i = 0; i < 2; i++) {
255+ if ((block[0x08 + i]) == 0) {
256+ block[0x08 + i] = block[i];
257+ block[0x10 + i] = block[i];
258+ block[0x18 + i] = block[i];
259+ block[0x20 + i] = block[i];
260+ block[0x28 + i] = block[i];
261+ block[0x30 + i] = block[i];
262+ block[0x38 + i] = block[i];
263+ } else {
264+ idct2_1d(block + i, 8);
265+ }
266+ }
267+
268+ for (int i = 0; i < 8; i++) {
269+ if (block[1] == 0) {
270+ for (int j = 0; j < 8; j++)
271+ dst[j] = av_clip_uint8((block[0] >> 5) + 128);
272+ } else {
273+ idct2_1d(block, 1);
274+ for (int j = 0; j < 8; j++)
275+ dst[j] = av_clip_uint8((block[j] >> 5) + 128);
276+ }
277+ block += 8;
278+ dst += stride;
279+ }
280+}
281+
282+static void idct2_add(uint8_t *dst, int stride,
283+ const uint8_t *src, int in_linesize,
284+ int *block)
285+{
286+ for (int i = 0; i < 2; i++) {
287+ if ((block[0x08 + i]) == 0) {
288+ block[0x08 + i] = block[i];
289+ block[0x10 + i] = block[i];
290+ block[0x18 + i] = block[i];
291+ block[0x20 + i] = block[i];
292+ block[0x28 + i] = block[i];
293+ block[0x30 + i] = block[i];
294+ block[0x38 + i] = block[i];
295+ } else {
296+ idct2_1d(block + i, 8);
297+ }
298+ }
299+
300+ for (int i = 0; i < 8; i++) {
301+ if (block[1] == 0) {
302+ for (int j = 0; j < 8; j++)
303+ dst[j] = av_clip_uint8((block[0] >> 5) + src[j]);
304+ } else {
305+ idct2_1d(block, 1);
306+ for (int j = 0; j < 8; j++)
307+ dst[j] = av_clip_uint8((block[j] >> 5) + src[j]);
308+ }
309+ block += 8;
310+ dst += stride;
311+ src += in_linesize;
312+ }
313+}
314+
315+static void update_inter_block(uint8_t *dst, int stride,
316+ const uint8_t *src, int in_linesize,
317+ int block)
318+{
319+ for (int i = 0; i < 8; i++) {
320+ for (int j = 0; j < 8; j++)
321+ dst[j] = av_clip_uint8(block + src[j]);
322+ dst += stride;
323+ src += in_linesize;
324+ }
325+}
326+
327+static int decode_intra_block(AVCodecContext *avctx, int mode,
328+ GetByteContext *gbyte, int16_t *qtab,
329+ int *block, int *pfill,
330+ uint8_t *dst, int linesize)
331+{
332+ MV30Context *s = avctx->priv_data;
333+ int fill;
334+
335+ switch (mode) {
336+ case 0:
337+ s->bdsp.fill_block_tab[1](dst, 128, linesize, 8);
338+ break;
339+ case 1:
340+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
341+ pfill[0] += fill;
342+ block[0] = ((pfill[0] * qtab[0]) >> 5) + 128;
343+ s->bdsp.fill_block_tab[1](dst, block[0], linesize, 8);
344+ break;
345+ case 2:
346+ memset(block, 0, sizeof(*block) * 64);
347+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
348+ pfill[0] += fill;
349+ block[0] = pfill[0] * qtab[0];
350+ block[1] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[1];
351+ block[8] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[8];
352+ block[9] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[9];
353+ idct2_put(dst, linesize, block);
354+ break;
355+ case 3:
356+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
357+ pfill[0] += fill;
358+ block[0] = pfill[0] * qtab[0];
359+ for (int i = 1; i < 64; i++)
360+ block[zigzag[i]] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[zigzag[i]];
361+ idct_put(dst, linesize, block);
362+ break;
363+ }
364+
365+ return 0;
366+}
367+
368+static int decode_inter_block(AVCodecContext *avctx, int mode,
369+ GetByteContext *gbyte, int16_t *qtab,
370+ int *block, int *pfill,
371+ uint8_t *dst, int linesize,
372+ const uint8_t *src, int in_linesize)
373+{
374+ int fill;
375+
376+ switch (mode) {
377+ case 0:
378+ copy_block8(dst, src, linesize, in_linesize, 8);
379+ break;
380+ case 1:
381+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
382+ pfill[0] += fill;
383+ block[0] = (pfill[0] * qtab[0]) >> 5;
384+ update_inter_block(dst, linesize, src, in_linesize, block[0]);
385+ break;
386+ case 2:
387+ memset(block, 0, sizeof(*block) * 64);
388+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
389+ pfill[0] += fill;
390+ block[0] = pfill[0] * qtab[0];
391+ block[1] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[1];
392+ block[8] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[8];
393+ block[9] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[9];
394+ idct2_add(dst, linesize, src, in_linesize, block);
395+ break;
396+ case 3:
397+ fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
398+ pfill[0] += fill;
399+ block[0] = pfill[0] * qtab[0];
400+ for (int i = 1; i < 64; i++)
401+ block[zigzag[i]] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[zigzag[i]];
402+ idct_add(dst, linesize, src, in_linesize, block);
403+ break;
404+ }
405+
406+ return 0;
407+}
408+
409+static int decode_coeffs(GetBitContext *gb, int16_t *coeffs, int nb_codes)
410+{
411+ memset(coeffs, 0, nb_codes * sizeof(*coeffs));
412+
413+ for (int i = 0; i < nb_codes;) {
414+ int value = get_vlc2(gb, cbp_tab.table, cbp_tab.bits, 1);
415+
416+ if (value < 0)
417+ return AVERROR_INVALIDDATA;
418+
419+ if (value > 0) {
420+ int x = get_bits(gb, value);
421+
422+ if (x < (1 << value) / 2) {
423+ x = (1 << (value - 1)) + (x & ((1 << value) - 1 >> 1));
424+ } else {
425+ x = -(1 << (value - 1)) - (x & ((1 << value) - 1 >> 1));
426+ }
427+ coeffs[i++] = x;
428+ } else {
429+ int flag = get_bits1(gb);
430+
431+ i += get_bits(gb, 3 + flag * 3) + 1 + flag * 8;
432+ }
433+ }
434+
435+ return 0;
436+}
437+
438+static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
439+{
440+ MV30Context *s = avctx->priv_data;
441+ GetBitContext mgb;
442+ uint8_t *dst[6];
443+ int linesize[6];
444+ int ret;
445+
446+ mgb = *gb;
447+ skip_bits_long(gb, s->mode_size * 8);
448+
449+ linesize[0] = frame->linesize[0];
450+ linesize[1] = frame->linesize[0];
451+ linesize[2] = frame->linesize[0];
452+ linesize[3] = frame->linesize[0];
453+ linesize[4] = frame->linesize[1];
454+ linesize[5] = frame->linesize[2];
455+
456+ for (int y = 0; y < avctx->height; y += 16) {
457+ GetByteContext gbyte;
458+ int pfill[3][1] = { 0 };
459+ int nb_codes = get_bits(gb, 16);
460+
461+ av_fast_padded_malloc(&s->coeffs, &s->coeffs_size, nb_codes * sizeof(*s->coeffs));
462+ if (!s->coeffs)
463+ return AVERROR(ENOMEM);
464+ ret = decode_coeffs(gb, s->coeffs, nb_codes);
465+ if (ret < 0)
466+ return ret;
467+
468+ bytestream2_init(&gbyte, (uint8_t *)s->coeffs, nb_codes * sizeof(*s->coeffs));
469+
470+ for (int x = 0; x < avctx->width; x += 16) {
471+ dst[0] = frame->data[0] + linesize[0] * y + x;
472+ dst[1] = frame->data[0] + linesize[0] * y + x + 8;
473+ dst[2] = frame->data[0] + linesize[0] * (y + 8) + x;
474+ dst[3] = frame->data[0] + linesize[0] * (y + 8) + x + 8;
475+ dst[4] = frame->data[1] + linesize[4] * (y >> 1) + (x >> 1);
476+ dst[5] = frame->data[2] + linesize[5] * (y >> 1) + (x >> 1);
477+
478+ for (int b = 0; b < 6; b++) {
479+ int mode = get_bits_le(&mgb, 2);
480+
481+ ret = decode_intra_block(avctx, mode, &gbyte, s->intraq_tab[b >= 4],
482+ s->block[b],
483+ pfill[(b >= 4) + (b >= 5)],
484+ dst[b], linesize[b]);
485+ if (ret < 0)
486+ return ret;
487+ }
488+ }
489+ }
490+
491+ return 0;
492+}
493+
494+static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
495+ AVFrame *frame, AVFrame *prev)
496+{
497+ MV30Context *s = avctx->priv_data;
498+ GetBitContext mask;
499+ GetBitContext mgb;
500+ GetByteContext mv;
501+ const int mask_size = ((avctx->height >> 4) * (avctx->width >> 4) * 2 + 7) / 8;
502+ uint8_t *dst[6], *src[6];
503+ int in_linesize[6];
504+ int linesize[6];
505+ int ret, cnt = 0;
506+ int flags = 0;
507+
508+ in_linesize[0] = prev->linesize[0];
509+ in_linesize[1] = prev->linesize[0];
510+ in_linesize[2] = prev->linesize[0];
511+ in_linesize[3] = prev->linesize[0];
512+ in_linesize[4] = prev->linesize[1];
513+ in_linesize[5] = prev->linesize[2];
514+
515+ linesize[0] = frame->linesize[0];
516+ linesize[1] = frame->linesize[0];
517+ linesize[2] = frame->linesize[0];
518+ linesize[3] = frame->linesize[0];
519+ linesize[4] = frame->linesize[1];
520+ linesize[5] = frame->linesize[2];
521+
522+ av_fast_padded_malloc(&s->mvectors, &s->mvectors_size, 2 * s->nb_mvectors * sizeof(*s->mvectors));
523+ if (!s->mvectors) {
524+ ret = AVERROR(ENOMEM);
525+ goto fail;
526+ }
527+
528+ mask = *gb;
529+ skip_bits_long(gb, mask_size * 8);
530+ mgb = *gb;
531+ skip_bits_long(gb, s->mode_size * 8);
532+
533+ ret = decode_coeffs(gb, s->mvectors, 2 * s->nb_mvectors);
534+ if (ret < 0)
535+ goto fail;
536+
537+ bytestream2_init(&mv, (uint8_t *)s->mvectors, 2 * s->nb_mvectors * sizeof(*s->mvectors));
538+
539+ for (int y = 0; y < avctx->height; y += 16) {
540+ GetByteContext gbyte;
541+ int pfill[3][1] = { 0 };
542+ int nb_codes = get_bits(gb, 16);
543+
544+ skip_bits(gb, 8);
545+ if (get_bits_left(gb) < 0) {
546+ ret = AVERROR_INVALIDDATA;
547+ goto fail;
548+ }
549+
550+ av_fast_padded_malloc(&s->coeffs, &s->coeffs_size, nb_codes * sizeof(*s->coeffs));
551+ if (!s->coeffs) {
552+ ret = AVERROR(ENOMEM);
553+ goto fail;
554+ }
555+
556+ ret = decode_coeffs(gb, s->coeffs, nb_codes);
557+ if (ret < 0)
558+ goto fail;
559+
560+ bytestream2_init(&gbyte, (uint8_t *)s->coeffs, nb_codes * sizeof(*s->coeffs));
561+
562+ for (int x = 0; x < avctx->width; x += 16) {
563+ if (cnt >= 4)
564+ cnt = 0;
565+ if (cnt == 0)
566+ flags = get_bits(&mask, 8);
567+
568+ dst[0] = frame->data[0] + linesize[0] * y + x;
569+ dst[1] = frame->data[0] + linesize[0] * y + x + 8;
570+ dst[2] = frame->data[0] + linesize[0] * (y + 8) + x;
571+ dst[3] = frame->data[0] + linesize[0] * (y + 8) + x + 8;
572+ dst[4] = frame->data[1] + linesize[4] * (y >> 1) + (x >> 1);
573+ dst[5] = frame->data[2] + linesize[5] * (y >> 1) + (x >> 1);
574+
575+ if ((flags >> (cnt)) & 1) {
576+ int mv_x = sign_extend(bytestream2_get_ne16(&mv), 16);
577+ int mv_y = sign_extend(bytestream2_get_ne16(&mv), 16);
578+
579+ int px = x + mv_x;
580+ int py = y + mv_y;
581+
582+ if (px < 0 || px >= avctx->width ||
583+ py < 0 || py >= avctx->height)
584+ return AVERROR_INVALIDDATA;
585+
586+ src[0] = prev->data[0] + in_linesize[0] * py + px;
587+ src[1] = prev->data[0] + in_linesize[0] * py + px + 8;
588+ src[2] = prev->data[0] + in_linesize[0] * (py + 8) + px;
589+ src[3] = prev->data[0] + in_linesize[0] * (py + 8) + px + 8;
590+ src[4] = prev->data[1] + in_linesize[4] * (py >> 1) + (px >> 1);
591+ src[5] = prev->data[2] + in_linesize[5] * (py >> 1) + (px >> 1);
592+
593+ if ((flags >> (cnt + 4)) & 1) {
594+ for (int b = 0; b < 6; b++)
595+ copy_block8(dst[b], src[b], linesize[b], in_linesize[b], 8);
596+ } else {
597+ for (int b = 0; b < 6; b++) {
598+ int mode = get_bits_le(&mgb, 2);
599+
600+ ret = decode_inter_block(avctx, mode, &gbyte, s->interq_tab[b >= 4],
601+ s->block[b],
602+ pfill[(b >= 4) + (b >= 5)],
603+ dst[b], linesize[b],
604+ src[b], in_linesize[b]);
605+ if (ret < 0)
606+ goto fail;
607+ }
608+ }
609+ } else {
610+ for (int b = 0; b < 6; b++) {
611+ int mode = get_bits_le(&mgb, 2);
612+
613+ ret = decode_intra_block(avctx, mode, &gbyte, s->intraq_tab[b >= 4],
614+ s->block[b],
615+ pfill[(b >= 4) + (b >= 5)],
616+ dst[b], linesize[b]);
617+ if (ret < 0)
618+ goto fail;
619+ }
620+ }
621+
622+ cnt++;
623+ }
624+ }
625+
626+fail:
627+ return ret;
628+}
629+
630+static int decode_frame(AVCodecContext *avctx, void *data,
631+ int *got_frame, AVPacket *avpkt)
632+{
633+ MV30Context *s = avctx->priv_data;
634+ GetBitContext *gb = &s->gb;
635+ AVFrame *frame = data;
636+ int ret;
637+
638+ if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
639+ return ret;
640+
641+ if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
642+ return ret;
643+
644+ s->intra_quant = get_bits(gb, 8);
645+ s->inter_quant = s->intra_quant + get_sbits(gb, 8);
646+ s->is_inter = get_bits_le(gb, 16);
647+ s->mode_size = get_bits_le(gb, 16);
648+ if (s->is_inter)
649+ s->nb_mvectors = get_bits_le(gb, 16);
650+
651+ get_qtable(s->intraq_tab[0], s->intra_quant, luma_tab);
652+ get_qtable(s->intraq_tab[1], s->intra_quant, chroma_tab);
653+
654+ frame->key_frame = s->is_inter == 0;
655+
656+ if (frame->key_frame) {
657+ ret = decode_intra(avctx, gb, frame);
658+ if (ret < 0)
659+ return ret;
660+ } else {
661+ get_qtable(s->interq_tab[0], s->inter_quant, luma_tab);
662+ get_qtable(s->interq_tab[1], s->inter_quant, chroma_tab);
663+
664+ if (!s->prev_frame->data[0]) {
665+ av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
666+ return AVERROR_INVALIDDATA;
667+ }
668+
669+ ret = decode_inter(avctx, gb, frame, s->prev_frame);
670+ if (ret < 0)
671+ return ret;
672+ }
673+
674+ av_frame_unref(s->prev_frame);
675+ if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
676+ return ret;
677+
678+ *got_frame = 1;
679+
680+ return avpkt->size;
681+}
682+
683+static const uint16_t cbp_codes[] = {
684+ 0, 1, 4, 5, 6, 0xE, 0x1E, 0x3E, 0x7E, 0xFE, 0x1FE, 0x1FF,
685+};
686+
687+static const uint8_t cbp_bits[] = {
688+ 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 9,
689+};
690+
691+static av_cold void init_static_data(void)
692+{
693+ INIT_VLC_SPARSE_STATIC(&cbp_tab, 9, FF_ARRAY_ELEMS(cbp_bits),
694+ cbp_bits, 1, 1, cbp_codes, 2, 2, NULL, 0, 0, 512);
695+}
696+
697+static av_cold int decode_init(AVCodecContext *avctx)
698+{
699+ MV30Context *s = avctx->priv_data;
700+ static AVOnce init_static_once = AV_ONCE_INIT;
701+
702+ avctx->pix_fmt = AV_PIX_FMT_YUV420P;
703+ avctx->color_range = AVCOL_RANGE_JPEG;
704+
705+ ff_blockdsp_init(&s->bdsp, avctx);
706+
707+ s->prev_frame = av_frame_alloc();
708+ if (!s->prev_frame)
709+ return AVERROR(ENOMEM);
710+
711+ ff_thread_once(&init_static_once, init_static_data);
712+
713+ return 0;
714+}
715+
716+static void decode_flush(AVCodecContext *avctx)
717+{
718+ MV30Context *s = avctx->priv_data;
719+
720+ av_frame_unref(s->prev_frame);
721+}
722+
723+static av_cold int decode_close(AVCodecContext *avctx)
724+{
725+ MV30Context *s = avctx->priv_data;
726+
727+ av_frame_free(&s->prev_frame);
728+ av_freep(&s->coeffs);
729+ s->coeffs_size = 0;
730+ av_freep(&s->mvectors);
731+ s->mvectors_size = 0;
732+
733+ return 0;
734+}
735+
736+AVCodec ff_mv30_decoder = {
737+ .name = "mv30",
738+ .long_name = NULL_IF_CONFIG_SMALL("MidiVid 3.0"),
739+ .type = AVMEDIA_TYPE_VIDEO,
740+ .id = AV_CODEC_ID_MV30,
741+ .priv_data_size = sizeof(MV30Context),
742+ .init = decode_init,
743+ .close = decode_close,
744+ .decode = decode_frame,
745+ .flush = decode_flush,
746+ .capabilities = AV_CODEC_CAP_DR1,
747+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
748+ FF_CODEC_CAP_INIT_CLEANUP,
749+};
libavcodec/version.h+2−2
@@ -28,8 +28,8 @@ · 27 unmodified lines
2828 #include "libavutil/version.h"
2929
3030 #define LIBAVCODEC_VERSION_MAJOR 58
31−#define LIBAVCODEC_VERSION_MINOR 77
32−#define LIBAVCODEC_VERSION_MICRO 101
31+#define LIBAVCODEC_VERSION_MINOR 78
32+#define LIBAVCODEC_VERSION_MICRO 100
3333
3434 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3535 LIBAVCODEC_VERSION_MINOR, \
libavformat/riff.c+1−0
@@ -492,6 +492,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { · 491 unmodified lines
492492 { AV_CODEC_ID_IMM5, MKTAG('I', 'M', 'M', '5') },
493493 { AV_CODEC_ID_MVDV, MKTAG('M', 'V', 'D', 'V') },
494494 { AV_CODEC_ID_MVHA, MKTAG('M', 'V', 'H', 'A') },
495+ { AV_CODEC_ID_MV30, MKTAG('M', 'V', '3', '0') },
495496 { AV_CODEC_ID_NONE, 0 }
496497 };
497498

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t