History

avcodec: ViewQuest VQC decoder

Peter Ross committed 4 years ago

Commit 3141dbb1 parent e253bc49 files · +446 −2Browse files

Unified · Split

9 files
  1. Changelog +1 −0
  2. doc/general_contents.texi +1 −0
  3. libavcodec/Makefile +1 −0
  4. libavcodec/allcodecs.c +1 −0
  5. libavcodec/codec_desc.c +7 −0
  6. libavcodec/codec_id.h +1 −0
  7. libavcodec/version.h +2 −2
  8. libavcodec/vqcdec.c +430 −0
  9. libavformat/riff.c +2 −0
Changelog+1−0
@@ -17,6 +17,7 @@ version <next>: · 16 unmodified lines
17- APAC decoder and demuxer17- APAC decoder and demuxer
18- Media 100i decoders18- Media 100i decoders
19- DTS to PTS reorder bsf19- DTS to PTS reorder bsf
20- ViewQuest VQC decoder
20 21
21 22
22version 5.1:23version 5.1:
doc/general_contents.texi+1−0
@@ -1339,6 +1339,7 @@ following image formats are supported: · 1338 unmodified lines
1339@item TwinVQ (VQF flavor) @tab @tab X1339@item TwinVQ (VQF flavor) @tab @tab X
1340@item VIMA @tab @tab X1340@item VIMA @tab @tab X
1341 @tab Used in LucasArts SMUSH animations.1341 @tab Used in LucasArts SMUSH animations.
1342@item ViewQuest VQC @tab @tab X
1342@item Vorbis @tab E @tab X1343@item Vorbis @tab E @tab X
1343 @tab A native but very primitive encoder exists.1344 @tab A native but very primitive encoder exists.
1344@item Voxware MetaSound @tab @tab X1345@item Voxware MetaSound @tab @tab X
libavcodec/Makefile+1−0
@@ -761,6 +761,7 @@ OBJS-$(CONFIG_VP9_QSV_ENCODER) += qsvenc_vp9.o · 760 unmodified lines
761OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o761OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o
762OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o762OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o
763OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o763OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o
764OBJS-$(CONFIG_VQC_DECODER) += vqcdec.o
764OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o wavpackdata.o dsd.o765OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o wavpackdata.o dsd.o
765OBJS-$(CONFIG_WAVPACK_ENCODER) += wavpackdata.o wavpackenc.o766OBJS-$(CONFIG_WAVPACK_ENCODER) += wavpackdata.o wavpackenc.o
766OBJS-$(CONFIG_WBMP_DECODER) += wbmpdec.o767OBJS-$(CONFIG_WBMP_DECODER) += wbmpdec.o
libavcodec/allcodecs.c+1−0
@@ -381,6 +381,7 @@ extern const FFCodec ff_vp9_decoder; · 380 unmodified lines
381extern const FFCodec ff_vp9_rkmpp_decoder;381extern const FFCodec ff_vp9_rkmpp_decoder;
382extern const FFCodec ff_vp9_v4l2m2m_decoder;382extern const FFCodec ff_vp9_v4l2m2m_decoder;
383extern const FFCodec ff_vqa_decoder;383extern const FFCodec ff_vqa_decoder;
384extern const FFCodec ff_vqc_decoder;
384extern const FFCodec ff_wbmp_decoder;385extern const FFCodec ff_wbmp_decoder;
385extern const FFCodec ff_wbmp_encoder;386extern const FFCodec ff_wbmp_encoder;
386extern const FFCodec ff_webp_decoder;387extern const FFCodec ff_webp_decoder;
libavcodec/codec_desc.c+7−0
@@ -1916,6 +1916,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1915 unmodified lines
1916 .long_name = NULL_IF_CONFIG_SMALL("Media 100i"),1916 .long_name = NULL_IF_CONFIG_SMALL("Media 100i"),
1917 .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,1917 .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
1918 },1918 },
1919 {
1920 .id = AV_CODEC_ID_VQC,
1921 .type = AVMEDIA_TYPE_VIDEO,
1922 .name = "vqc",
1923 .long_name = NULL_IF_CONFIG_SMALL("ViewQuest VQC"),
1924 .props = AV_CODEC_PROP_LOSSY,
1925 },
1919 1926
1920 /* various PCM "codecs" */1927 /* various PCM "codecs" */
1921 {1928 {
libavcodec/codec_id.h+1−0
@@ -319,6 +319,7 @@ enum AVCodecID { · 318 unmodified lines
319 AV_CODEC_ID_RADIANCE_HDR,319 AV_CODEC_ID_RADIANCE_HDR,
320 AV_CODEC_ID_WBMP,320 AV_CODEC_ID_WBMP,
321 AV_CODEC_ID_MEDIA100,321 AV_CODEC_ID_MEDIA100,
322 AV_CODEC_ID_VQC,
322 323
323 /* various PCM "codecs" */324 /* various PCM "codecs" */
324 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs325 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
libavcodec/version.h+2−2
@@ -29,8 +29,8 @@ · 28 unmodified lines
29 29
30#include "version_major.h"30#include "version_major.h"
31 31
32#define LIBAVCODEC_VERSION_MINOR 5032#define LIBAVCODEC_VERSION_MINOR 51
33#define LIBAVCODEC_VERSION_MICRO 10133#define LIBAVCODEC_VERSION_MICRO 100
34 34
35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 LIBAVCODEC_VERSION_MINOR, \36 LIBAVCODEC_VERSION_MINOR, \
libavcodec/vqcdec.c+430−0
@@ -0,0 +1,430 @@
1/*
2 * ViewQuest VQC decoder
3 * Copyright (C) 2022 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "get_bits.h"
24#include "codec_internal.h"
25#include "decode.h"
26#include "libavutil/thread.h"
27
28#define VECTOR_VLC_BITS 6
29
30static const uint8_t vector_nbits[] = {
31 2, 4, 4, 4, 4, 2, 4, 4,
32 6, 6, 6, 6, 6, 6, 6, 6
33};
34
35enum {
36 SKIP_3 = 0x10,
37 SKIP_4,
38 SKIP_5,
39 SKIP_6,
40 STOP_RUN,
41 SIGNED_8BIT,
42 SIGNED_6BIT
43};
44
45/* vector symbols are signed, but returned unsigned by get_vlc2()
46 codebook indexes are cast as uint8_t in seed_codebook() to compensate */
47static const int8_t vector_symbols[] = {
48 0, SKIP_3, SKIP_4, SKIP_5, SKIP_6, STOP_RUN, 1, -1,
49 2, 3, 4, SIGNED_8BIT, -2, -3, -4, SIGNED_6BIT
50};
51
52static VLC vector_vlc;
53
54static av_cold void vqc_init_static_data(void)
55{
56 INIT_VLC_STATIC_FROM_LENGTHS(&vector_vlc, VECTOR_VLC_BITS, FF_ARRAY_ELEMS(vector_nbits),
57 vector_nbits, 1,
58 vector_symbols, 1, 1,
59 0, 0, 1 << VECTOR_VLC_BITS);
60}
61
62typedef struct VqcContext {
63 AVFrame *frame;
64 uint8_t * vectors;
65 int16_t * coeff, *tmp1, *tmp2;
66 int16_t codebook[4][256];
67} VqcContext;
68
69static av_cold int vqc_decode_init(AVCodecContext * avctx)
70{
71 static AVOnce init_static_once = AV_ONCE_INIT;
72 VqcContext *s = avctx->priv_data;
73
74 s->vectors = av_malloc((avctx->width * avctx->height * 3) / 2);
75 if (!s->vectors)
76 return AVERROR(ENOMEM);
77
78 s->coeff = av_malloc_array(2 * avctx->width, sizeof(s->coeff[0]));
79 if (!s->coeff)
80 return AVERROR(ENOMEM);
81
82 s->tmp1 = av_malloc_array(avctx->width / 2, sizeof(s->tmp1[0]));
83 if (!s->tmp1)
84 return AVERROR(ENOMEM);
85
86 s->tmp2 = av_malloc_array(avctx->width / 2, sizeof(s->tmp2[0]));
87 if (!s->tmp2)
88 return AVERROR(ENOMEM);
89
90 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
91 s->frame = av_frame_alloc();
92 if (!s->frame)
93 return AVERROR(ENOMEM);
94
95 ff_thread_once(&init_static_once, vqc_init_static_data);
96
97 return 0;
98}
99
100static int seed_pow1(int x)
101{
102 return x >= 1 && x <= 5 ? 1 << x : 0;
103}
104
105static int seed_pow2(int x)
106{
107 return x >= 1 && x <= 4 ? 1 << x : 1;
108}
109
110static int bias(int x, int c)
111{
112 if (x < 0)
113 return x - c;
114 else if (x > 0)
115 return x + c;
116 else
117 return 0;
118}
119
120static void seed_codebooks(VqcContext * s, const int * seed)
121{
122 int book1 = -256 * seed[3];
123 int book2 = -128 * seed[4];
124 int book3 = -128 * seed[5];
125 int book4 = -128 * seed[6];
126
127 for (int i = -128; i < 128; i++) {
128 s->codebook[0][(uint8_t)i] = book1;
129 s->codebook[1][(uint8_t)i] = bias(book2, seed[0]);
130 s->codebook[2][(uint8_t)i] = bias(book3, seed[1]);
131 s->codebook[3][(uint8_t)i] = bias(book4, seed[2]);
132
133 book1 += 2 * seed[3];
134 book2 += seed[4];
135 book3 += seed[5];
136 book4 += seed[6];
137 }
138}
139
140static void decode_vectors(VqcContext * s, const uint8_t * buf, int size, int width, int height)
141{
142 GetBitContext gb;
143 uint8_t * vectors = s->vectors;
144 uint8_t * vectors_end = s->vectors + (width * height * 3) / 2;
145
146 memset(vectors, 0, 3 * width * height / 2);
147
148 init_get_bits8(&gb, buf, size);
149
150 for (int i = 0; i < 3 * width * height / 2 / 32; i++) {
151 uint8_t * dst = vectors;
152 int symbol;
153
154 *dst++ = get_bits(&gb, 8);
155 *dst++ = get_bits(&gb, 8);
156
157 while (show_bits(&gb, 2) != 2) {
158
159 if (dst >= vectors_end - 1)
160 return;
161
162 if (!show_bits(&gb, 4)) {
163 *dst++ = 0;
164 *dst++ = 0;
165 skip_bits(&gb, 4);
166 continue;
167 }
168
169 symbol = get_vlc2(&gb, vector_vlc.table, VECTOR_VLC_BITS, 1);
170 switch(symbol) {
171 case SKIP_3: dst += 3; break;
172 case SKIP_4: dst += 4; break;
173 case SKIP_5: dst += 5; break;
174 case SKIP_6: dst += 6; break;
175 case SIGNED_8BIT: *dst++ = get_sbits(&gb, 8); break;
176 case SIGNED_6BIT: *dst++ = get_sbits(&gb, 6); break;
177 default:
178 *dst++ = symbol;
179 }
180 }
181
182 skip_bits(&gb, 2);
183 vectors += 32;
184 }
185}
186
187static void load_coeffs(VqcContext * s, const uint8_t * v, int width, int coeff_width)
188{
189 int16_t * c0 = s->coeff;
190 int16_t * c1 = s->coeff + coeff_width;
191 int16_t * c0_125 = s->coeff + (coeff_width >> 3);
192 int16_t * c1_125 = s->coeff + coeff_width + (coeff_width >> 3);
193 int16_t * c0_25 = s->coeff + (coeff_width >> 2);
194 int16_t * c1_25 = s->coeff + coeff_width + (coeff_width >> 2);
195 int16_t * c0_5 = s->coeff + (coeff_width >> 1);
196 int16_t * c1_5 = s->coeff + coeff_width + (coeff_width >> 1);
197
198 for (int i = 0; i < width; i++) {
199 c0[0] = s->codebook[0][v[0]];
200 c0[1] = s->codebook[0][v[1]];
201 c0 += 2;
202
203 c1[0] = s->codebook[0][v[2]];
204 c1[1] = s->codebook[0][v[3]];
205 c1 += 2;
206
207 c0_125[0] = s->codebook[1][v[4]];
208 c0_125[1] = s->codebook[1][v[5]];
209 c0_125 += 2;
210
211 c1_125[0] = s->codebook[1][v[6]];
212 c1_125[1] = s->codebook[1][v[7]];
213 c1_125 += 2;
214
215 c0_25[0] = s->codebook[2][v[8]];
216 c0_25[1] = s->codebook[2][v[9]];
217 c0_25[2] = s->codebook[2][v[10]];
218 c0_25[3] = s->codebook[2][v[11]];
219 c0_25 += 4;
220
221 c1_25[0] = s->codebook[2][v[12]];
222 c1_25[1] = s->codebook[2][v[13]];
223 c1_25[2] = s->codebook[2][v[14]];
224 c1_25[3] = s->codebook[2][v[15]];
225 c1_25 += 4;
226
227 if (v[16] | v[17] | v[18] | v[19]) {
228 c0_5[0] = s->codebook[3][v[16]];
229 c0_5[1] = s->codebook[3][v[17]];
230 c0_5[2] = s->codebook[3][v[18]];
231 c0_5[3] = s->codebook[3][v[19]];
232 } else {
233 c0_5[0] = c0_5[1] = c0_5[2] = c0_5[3] = 0;
234 }
235
236 if (v[20] | v[21] | v[22] | v[23]) {
237 c0_5[4] = s->codebook[3][v[20]];
238 c0_5[5] = s->codebook[3][v[21]];
239 c0_5[6] = s->codebook[3][v[22]];
240 c0_5[7] = s->codebook[3][v[23]];
241 } else {
242 c0_5[4] = c0_5[5] = c0_5[6] = c0_5[7] = 0;
243 }
244 c0_5 += 8;
245
246 if (v[24] | v[25] | v[26] | v[27]) {
247 c1_5[0] = s->codebook[3][v[24]];
248 c1_5[1] = s->codebook[3][v[25]];
249 c1_5[2] = s->codebook[3][v[26]];
250 c1_5[3] = s->codebook[3][v[27]];
251 } else {
252 c1_5[0] = c1_5[1] = c1_5[2] = c1_5[3] = 0;
253 }
254
255 if (v[28] | v[29] | v[30] | v[31]) {
256 c1_5[4] = s->codebook[3][v[28]];
257 c1_5[5] = s->codebook[3][v[29]];
258 c1_5[6] = s->codebook[3][v[30]];
259 c1_5[7] = s->codebook[3][v[31]];
260 } else {
261 c1_5[4] = c1_5[5] = c1_5[6] = c1_5[7] = 0;
262 }
263 c1_5 += 8;
264
265 v += 32;
266 }
267}
268
269static void transform1(const int16_t * a, const int16_t * b, int16_t * dst, int width)
270{
271 int s0 = a[0] + (b[0] >> 1);
272
273 for (int i = 0; i < width / 2 - 1; i++) {
274 dst[i * 2] = s0;
275 s0 = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
276 dst[i * 2 + 1] = ((dst[i * 2] + s0) >> 1) - 2 * b[i];
277 }
278
279 dst[width - 2] = s0;
280 dst[width - 1] = a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1];
281}
282
283static uint8_t clip(int x)
284{
285 return x >= -128 ? x <= 127 ? x + 0x80 : 0x00 : 0xFF;
286}
287
288static void transform2(const int16_t * a, const int16_t * b, uint8_t * dst, int width)
289{
290 int s0 = a[0] + (b[0] >> 1);
291 int tmp;
292
293 for (int i = 0; i < width / 2 - 1; i++) {
294 dst[i * 2] = av_clip_uint8(s0 + 0x80);
295 tmp = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
296 dst[i * 2 + 1] = av_clip_uint8(((tmp + s0) >> 1) - 2 * b[i] + 0x80);
297 s0 = tmp;
298 }
299
300 dst[width - 2] = clip(s0);
301 dst[width - 1] = clip(a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1]);
302}
303
304static void decode_strip(VqcContext * s, uint8_t * dst, int stride, int width)
305{
306 const int16_t * coeff;
307
308 for (int i = 0; i < width; i++) {
309 int v0 = s->coeff[i];
310 int v1 = s->coeff[width + i];
311 s->coeff[i] = v0 - v1;
312 s->coeff[width + i] = v0 + v1;
313 }
314
315 coeff = s->coeff;
316
317 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
318 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
319 transform2(s->tmp2, coeff + width / 2, dst, width);
320
321 coeff += width;
322 dst += stride;
323
324 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
325 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
326 transform2(s->tmp2, coeff + width / 2, dst, width);
327}
328
329static void decode_frame(VqcContext * s, int width, int height)
330{
331 uint8_t * vectors = s->vectors;
332 uint8_t * y = s->frame->data[0];
333 uint8_t * u = s->frame->data[1];
334 uint8_t * v = s->frame->data[2];
335
336 for (int j = 0; j < height / 4; j++) {
337 load_coeffs(s, vectors, width / 16, width);
338 decode_strip(s, y, s->frame->linesize[0], width);
339 vectors += 2 * width;
340 y += 2 * s->frame->linesize[0];
341
342 load_coeffs(s, vectors, width / 32, width / 2);
343 decode_strip(s, u, s->frame->linesize[1], width / 2);
344 vectors += width;
345 u += 2 * s->frame->linesize[1];
346
347 load_coeffs(s, vectors, width / 16, width);
348 decode_strip(s, y, s->frame->linesize[0], width);
349 vectors += 2 * width;
350 y += 2 * s->frame->linesize[0];
351
352 load_coeffs(s, vectors, width / 32, width / 2);
353 decode_strip(s, v, s->frame->linesize[2], width / 2);
354 vectors += width;
355 v += 2 * s->frame->linesize[2];
356 }
357}
358
359static int vqc_decode_frame(AVCodecContext *avctx, AVFrame * rframe,
360 int * got_frame, AVPacket * avpkt)
361{
362 VqcContext *s = avctx->priv_data;
363 int ret;
364 const uint8_t * buf = avpkt->data;
365 int cache, seed[7], gamma, contrast;
366
367 if (avpkt->size < 7)
368 return AVERROR_INVALIDDATA;
369
370 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
371 return ret;
372
373 av_log(avctx, AV_LOG_DEBUG, "VQC%d format\n", (buf[2] & 1) + 1);
374
375 if (((buf[0] >> 1) & 7) != 5) {
376 avpriv_request_sample(avctx, "subversion != 5\n");
377 return AVERROR_PATCHWELCOME;
378 }
379
380 cache = AV_RL24(buf + 4);
381 seed[2] = seed_pow1((cache >> 1) & 7);
382 seed[1] = seed_pow1((cache >> 4) & 7);
383 seed[0] = seed_pow1((cache >> 7) & 7);
384 seed[6] = seed_pow2((cache >> 10) & 7);
385 seed[5] = seed_pow2((cache >> 13) & 7);
386 seed[4] = seed_pow2((cache >> 16) & 7);
387 seed[3] = seed_pow2((cache >> 19) & 7);
388
389 gamma = buf[0] >> 4;
390 contrast = AV_RL16(buf + 2) >> 1;
391 if (gamma || contrast)
392 avpriv_request_sample(avctx, "gamma=0x%x, contrast=0x%x\n", gamma, contrast);
393
394 seed_codebooks(s, seed);
395 decode_vectors(s, buf + 7, avpkt->size - 7, avctx->width, avctx->height);
396 decode_frame(s, avctx->width, avctx->height);
397
398 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
399 return ret;
400
401 *got_frame = 1;
402
403 return avpkt->size;
404}
405
406static av_cold int vqc_decode_end(AVCodecContext * avctx)
407{
408 VqcContext *s = avctx->priv_data;
409
410 av_freep(&s->vectors);
411 av_freep(&s->coeff);
412 av_freep(&s->tmp1);
413 av_freep(&s->tmp2);
414 av_frame_free(&s->frame);
415
416 return 0;
417}
418
419const FFCodec ff_vqc_decoder = {
420 .p.name = "vqc",
421 CODEC_LONG_NAME("ViewQuest VQC"),
422 .p.type = AVMEDIA_TYPE_VIDEO,
423 .p.id = AV_CODEC_ID_VQC,
424 .priv_data_size = sizeof(VqcContext),
425 .init = vqc_decode_init,
426 .close = vqc_decode_end,
427 FF_CODEC_DECODE_CB(vqc_decode_frame),
428 .p.capabilities = AV_CODEC_CAP_DR1,
429 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
430};
libavformat/riff.c+2−0
@@ -499,6 +499,8 @@ const AVCodecTag ff_codec_bmp_tags[] = { · 498 unmodified lines
499 { AV_CODEC_ID_MVHA, MKTAG('M', 'V', 'H', 'A') },499 { AV_CODEC_ID_MVHA, MKTAG('M', 'V', 'H', 'A') },
500 { AV_CODEC_ID_MV30, MKTAG('M', 'V', '3', '0') },500 { AV_CODEC_ID_MV30, MKTAG('M', 'V', '3', '0') },
501 { AV_CODEC_ID_NOTCHLC, MKTAG('n', 'l', 'c', '1') },501 { AV_CODEC_ID_NOTCHLC, MKTAG('n', 'l', 'c', '1') },
502 { AV_CODEC_ID_VQC, MKTAG('V', 'Q', 'C', '1') },
503 { AV_CODEC_ID_VQC, MKTAG('V', 'Q', 'C', '2') },
502 { AV_CODEC_ID_NONE, 0 }504 { AV_CODEC_ID_NONE, 0 }
503};505};
504 506

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