1 /*
2 * Sierra VMD Audio & Video Decoders
3 * Copyright (C) 2004 the ffmpeg project
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 /**
23 * @file
24 * Sierra VMD audio & video decoders
25 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 * for more information on the Sierra VMD format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 *
29 * The video decoder outputs PAL8 colorspace data. The decoder expects
30 * a 0x330-byte VMD file header to be transmitted via extradata during
31 * codec initialization. Each encoded frame that is sent to this decoder
32 * is expected to be prepended with the appropriate 16-byte frame
33 * information record from the VMD file.
34 *
35 * The audio decoder, like the video decoder, expects each encoded data
36 * chunk to be prepended with the appropriate 16-byte frame information
37 * record from the VMD file. It does not require the 0x330-byte VMD file
38 * header, but it does need the audio setup parameters passed in through
39 * normal libavcodec API means.
40 */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
53
54 #define VMD_HEADER_SIZE 0x330
55 #define PALETTE_COUNT 256
56
57 /*
58 * Video Decoder
59 */
60
62
65
66 const unsigned char *
buf;
68
72
75
76 #define QUEUE_SIZE 0x1000
77 #define QUEUE_MASK 0x0FFF
78
80 unsigned char *dest, int dest_len)
81 {
82 unsigned char *d;
83 unsigned char *d_end;
85 unsigned int qpos;
86 unsigned int dataleft;
87 unsigned int chainofs;
88 unsigned int chainlen;
89 unsigned int speclen;
91 unsigned int i, j;
93
95 d = dest;
96 d_end = d + dest_len;
97 dataleft = bytestream2_get_le32(&gb);
100 return;
101 if (bytestream2_peek_le32(&gb) == 0x56781234) {
103 qpos = 0x111;
104 speclen = 0xF + 3;
105 } else {
106 qpos = 0xFEE;
107 speclen = 100; /* no speclen */
108 }
109
111 tag = bytestream2_get_byteu(&gb);
112 if ((tag == 0xFF) && (dataleft > 8)) {
114 return;
115 for (i = 0; i < 8; i++) {
116 queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
118 }
119 dataleft -= 8;
120 } else {
121 for (i = 0; i < 8; i++) {
122 if (dataleft == 0)
123 break;
124 if (tag & 0x01) {
126 return;
127 queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
129 dataleft--;
130 } else {
131 chainofs = bytestream2_get_byte(&gb);
132 chainofs |= ((bytestream2_peek_byte(&gb) & 0xF0) << 4);
133 chainlen = (bytestream2_get_byte(&gb) & 0x0F) + 3;
134 if (chainlen == speclen) {
135 chainlen = bytestream2_get_byte(&gb) + 0xF + 3;
136 }
137 if (d_end - d < chainlen)
138 return;
139 for (j = 0; j < chainlen; j++) {
141 queue[qpos++] = *d++;
143 }
144 dataleft -= chainlen;
145 }
146 tag >>= 1;
147 }
148 }
149 }
150 }
152 int src_count, int src_size, int dest_len)
153 {
154 unsigned char *pd;
155 int i, l, used = 0;
156 unsigned char *dest_end = dest + dest_len;
158 uint16_t run_val;
159
161 pd = dest;
162 if (src_count & 1) {
164 return 0;
165 *pd++ = bytestream2_get_byteu(&gb);
166 used++;
167 }
168
169 do {
171 break;
172 l = bytestream2_get_byteu(&gb);
173 if (l & 0x80) {
174 l = (l & 0x7F) * 2;
178 pd += l;
179 } else {
183 for (i = 0; i < l; i++) {
185 pd += 2;
186 }
187 l *= 2;
188 }
189 used += l;
190 } while (used < src_count);
191
193 }
194
196 {
197 int i;
198 unsigned int *palette32;
199 unsigned char r,
g,
b;
200
202
203 unsigned char meth;
204 unsigned char *dp; /* pointer to current frame */
205 unsigned char *pp; /* pointer to previous frame */
207 int ofs;
208
209 int frame_x, frame_y;
210 int frame_width, frame_height;
211
214 frame_width =
AV_RL16(&s->
buf[10]) - frame_x + 1;
215 frame_height =
AV_RL16(&s->
buf[12]) - frame_y + 1;
216
218 (frame_x || frame_y)) {
219
222 }
225
226 if (frame_x < 0 || frame_width < 0 ||
231 "Invalid horizontal range %d-%d\n",
232 frame_x, frame_width);
234 }
235 if (frame_y < 0 || frame_height < 0 ||
240 "Invalid vertical range %d-%d\n",
241 frame_x, frame_width);
243 }
244
245 /* if only a certain region will be updated, copy the entire previous
246 * frame before the decode */
248 (frame_x || frame_y || (frame_width != s->
avctx->
width) ||
250
253 }
254
255 /* check if there is a new palette */
257 if (s->
buf[15] & 0x02) {
259 palette32 = (
unsigned int *)s->
palette;
262 r = bytestream2_get_byteu(&gb) * 4;
263 g = bytestream2_get_byteu(&gb) * 4;
264 b = bytestream2_get_byteu(&gb) * 4;
265 palette32[i] = 0xFF
U << 24 | (r << 16) | (g << 8) | (
b);
266 palette32[i] |= palette32[i] >> 6 & 0x30303;
267 }
268 } else {
271 }
272 }
273
275 return 0;
276
277 /* originally UnpackFrame in VAG's code */
280 meth = bytestream2_get_byteu(&gb);
281 if (meth & 0x80) {
284 "Trying to unpack LZ-compressed frame with no LZ buffer\n");
286 }
289 meth &= 0x7F;
291 }
292
293 dp = &frame->
data[0][frame_y * frame->
linesize[0] + frame_x];
295 switch (meth) {
296 case 1:
297 for (i = 0; i < frame_height; i++) {
298 ofs = 0;
299 do {
300 len = bytestream2_get_byte(&gb);
301 if (len & 0x80) {
302 len = (len & 0x7F) + 1;
303 if (ofs + len > frame_width ||
308 } else {
309 /* interframe pixel copy */
312 memcpy(&dp[ofs], &pp[ofs], len + 1);
313 ofs += len + 1;
314 }
315 } while (ofs < frame_width);
316 if (ofs > frame_width) {
318 "offset > width (%d > %d)\n",
319 ofs, frame_width);
321 }
324 }
325 break;
326
327 case 2:
328 for (i = 0; i < frame_height; i++) {
332 }
333 break;
334
335 case 3:
336 for (i = 0; i < frame_height; i++) {
337 ofs = 0;
338 do {
339 len = bytestream2_get_byte(&gb);
340 if (len & 0x80) {
341 len = (len & 0x7F) + 1;
342 if (bytestream2_peek_byte(&gb) == 0xFF) {
344 bytestream2_get_byte(&gb);
347 frame_width - ofs);
348 ofs += slen;
350 } else {
353 }
354 } else {
355 /* interframe pixel copy */
358 memcpy(&dp[ofs], &pp[ofs], len + 1);
359 ofs += len + 1;
360 }
361 } while (ofs < frame_width);
362 if (ofs > frame_width) {
364 "offset > width (%d > %d)\n",
365 ofs, frame_width);
367 }
370 }
371 break;
372 }
373 return 0;
374 }
375
377 {
379
382
383 return 0;
384 }
385
387 {
389 int i;
390 unsigned int *palette32;
391 int palette_index = 0;
392 unsigned char r,
g,
b;
393 unsigned char *vmd_header;
394 unsigned char *raw_palette;
395
398
399 /* make sure the VMD header made it */
404 }
405 vmd_header = (
unsigned char *)avctx->
extradata;
406
412 }
413
414 /* load up the initial palette */
415 raw_palette = &vmd_header[28];
416 palette32 = (
unsigned int *)s->
palette;
418 r = raw_palette[palette_index++] * 4;
419 g = raw_palette[palette_index++] * 4;
420 b = raw_palette[palette_index++] * 4;
421 palette32[i] = 0xFF
U << 24 | (r << 16) | (g << 8) | (
b);
422 palette32[i] |= palette32[i] >> 6 & 0x30303;
423 }
424
429 }
430
431 return 0;
432 }
433
435 void *
data,
int *got_frame,
437 {
439 int buf_size = avpkt->
size;
443
446
447 if (buf_size < 16)
449
452
455
456 /* make the palette available on the way out */
458
459 /* shuffle frames */
463
464 *got_frame = 1;
465
466 /* report that the buffer was completely consumed */
467 return buf_size;
468 }
469
470 /*
471 * Audio Decoder
472 */
473
474 #define BLOCK_TYPE_AUDIO 1
475 #define BLOCK_TYPE_INITIAL 2
476 #define BLOCK_TYPE_SILENCE 3
477
482
484 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
485 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
486 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
487 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
488 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
489 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
490 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
491 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
492 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
493 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
494 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
495 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
496 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
497 };
498
500 {
502
506 }
510 }
511
514
517 else
520
522
524 "block align = %d, sample rate = %d\n",
527
528 return 0;
529 }
530
532 int channels)
533 {
534 int ch;
535 const uint8_t *buf_end = buf + buf_size;
537 int st = channels - 1;
538
539 /* decode initial raw sample */
540 for (ch = 0; ch < channels; ch++) {
541 predictor[ch] = (int16_t)
AV_RL16(buf);
542 buf += 2;
543 *out++ = predictor[ch];
544 }
545
546 /* decode DPCM samples */
547 ch = 0;
548 while (buf < buf_end) {
550 if (b & 0x80)
552 else
554 predictor[ch] = av_clip_int16(predictor[ch]);
555 *out++ = predictor[ch];
556 ch ^= st;
557 }
558 }
559
561 int *got_frame_ptr,
AVPacket *avpkt)
562 {
566 int buf_size = avpkt->
size;
568 int block_type, silent_chunks, audio_chunks;
571 int16_t *output_samples_s16;
572
573 if (buf_size < 16) {
575 *got_frame_ptr = 0;
576 return buf_size;
577 }
578
579 block_type = buf[6];
583 }
584 buf += 16;
585 buf_size -= 16;
586
587 /* get number of silent chunks */
588 silent_chunks = 0;
591 if (buf_size < 4) {
594 }
596 silent_chunks = av_popcount(flags);
597 buf += 4;
598 buf_size -= 4;
599 } else if (block_type == BLOCK_TYPE_SILENCE) {
600 silent_chunks = 1;
601 buf_size = 0; // should already be zero but set it just to be sure
602 }
603
604 /* ensure output buffer is large enough */
606
607 /* drop incomplete chunks */
609
610 /* get output buffer */
615 output_samples_u8 = frame->
data[0];
616 output_samples_s16 = (int16_t *)frame->
data[0];
617
618 /* decode silent chunks */
619 if (silent_chunks > 0) {
620 int silent_size = avctx->
block_align * silent_chunks;
622
624 memset(output_samples_s16, 0x00, silent_size * 2);
625 output_samples_s16 += silent_size;
626 } else {
627 memset(output_samples_u8, 0x80, silent_size);
628 output_samples_u8 += silent_size;
629 }
630 }
631
632 /* decode audio chunks */
633 if (audio_chunks > 0) {
634 buf_end = buf + buf_size;
641 } else {
642 memcpy(output_samples_u8, buf, s->
chunk_size);
644 }
646 }
647 }
648
649 *got_frame_ptr = 1;
650
652 }
653
654
655 /*
656 * Public Data Structures
657 */
658
669 };
670
680 };