]> git.sesse.net Git - ffmpeg/blob - libavcodec/vmdav.c
cook: use av_dlog() for debug logging instead of av_log() with AV_LOG_ERROR
[ffmpeg] / libavcodec / vmdav.c
1 /*
2  * Sierra VMD Audio & Video Decoders
3  * Copyright (C) 2004 the ffmpeg project
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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
46 #include "libavutil/common.h"
47 #include "libavutil/intreadwrite.h"
48 #include "avcodec.h"
49
50 #define VMD_HEADER_SIZE 0x330
51 #define PALETTE_COUNT 256
52
53 /*
54  * Video Decoder
55  */
56
57 typedef struct VmdVideoContext {
58
59     AVCodecContext *avctx;
60     AVFrame frame;
61     AVFrame prev_frame;
62
63     const unsigned char *buf;
64     int size;
65
66     unsigned char palette[PALETTE_COUNT * 4];
67     unsigned char *unpack_buffer;
68     int unpack_buffer_size;
69
70     int x_off, y_off;
71 } VmdVideoContext;
72
73 #define QUEUE_SIZE 0x1000
74 #define QUEUE_MASK 0x0FFF
75
76 static void lz_unpack(const unsigned char *src, int src_len,
77                       unsigned char *dest, int dest_len)
78 {
79     const unsigned char *s;
80     unsigned int s_len;
81     unsigned char *d;
82     unsigned char *d_end;
83     unsigned char queue[QUEUE_SIZE];
84     unsigned int qpos;
85     unsigned int dataleft;
86     unsigned int chainofs;
87     unsigned int chainlen;
88     unsigned int speclen;
89     unsigned char tag;
90     unsigned int i, j;
91
92     s = src;
93     s_len = src_len;
94     d = dest;
95     d_end = d + dest_len;
96     dataleft = AV_RL32(s);
97     s += 4; s_len -= 4;
98     memset(queue, 0x20, QUEUE_SIZE);
99     if (s_len < 4)
100         return;
101     if (AV_RL32(s) == 0x56781234) {
102         s += 4; s_len -= 4;
103         qpos = 0x111;
104         speclen = 0xF + 3;
105     } else {
106         qpos = 0xFEE;
107         speclen = 100;  /* no speclen */
108     }
109
110     while (dataleft > 0 && s_len > 0) {
111         tag = *s++; s_len--;
112         if ((tag == 0xFF) && (dataleft > 8)) {
113             if (d + 8 > d_end || s_len < 8)
114                 return;
115             for (i = 0; i < 8; i++) {
116                 queue[qpos++] = *d++ = *s++;
117                 qpos &= QUEUE_MASK;
118             }
119             s_len -= 8;
120             dataleft -= 8;
121         } else {
122             for (i = 0; i < 8; i++) {
123                 if (dataleft == 0)
124                     break;
125                 if (tag & 0x01) {
126                     if (d + 1 > d_end || s_len < 1)
127                         return;
128                     queue[qpos++] = *d++ = *s++;
129                     qpos &= QUEUE_MASK;
130                     dataleft--;
131                     s_len--;
132                 } else {
133                     if (s_len < 2)
134                         return;
135                     chainofs = *s++;
136                     chainofs |= ((*s & 0xF0) << 4);
137                     chainlen = (*s++ & 0x0F) + 3;
138                     s_len -= 2;
139                     if (chainlen == speclen) {
140                         if (s_len < 1)
141                             return;
142                         chainlen = *s++ + 0xF + 3;
143                         s_len--;
144                     }
145                     if (d + chainlen > d_end)
146                         return;
147                     for (j = 0; j < chainlen; j++) {
148                         *d = queue[chainofs++ & QUEUE_MASK];
149                         queue[qpos++] = *d++;
150                         qpos &= QUEUE_MASK;
151                     }
152                     dataleft -= chainlen;
153                 }
154                 tag >>= 1;
155             }
156         }
157     }
158 }
159
160 static int rle_unpack(const unsigned char *src, unsigned char *dest,
161     int src_count, int src_size, int dest_len)
162 {
163     const unsigned char *ps;
164     unsigned char *pd;
165     int i, l;
166     unsigned char *dest_end = dest + dest_len;
167
168     ps = src;
169     pd = dest;
170     if (src_count & 1) {
171         if (src_size < 1)
172             return 0;
173         *pd++ = *ps++;
174         src_size--;
175     }
176
177     src_count >>= 1;
178     i = 0;
179     do {
180         if (src_size < 1)
181             break;
182         l = *ps++;
183         src_size--;
184         if (l & 0x80) {
185             l = (l & 0x7F) * 2;
186             if (pd + l > dest_end || src_size < l)
187                 return ps - src;
188             memcpy(pd, ps, l);
189             ps += l;
190             src_size -= l;
191             pd += l;
192         } else {
193             if (pd + i > dest_end || src_size < 2)
194                 return ps - src;
195             for (i = 0; i < l; i++) {
196                 *pd++ = ps[0];
197                 *pd++ = ps[1];
198             }
199             ps += 2;
200             src_size -= 2;
201         }
202         i += l;
203     } while (i < src_count);
204
205     return ps - src;
206 }
207
208 static void vmd_decode(VmdVideoContext *s)
209 {
210     int i;
211     unsigned int *palette32;
212     unsigned char r, g, b;
213
214     /* point to the start of the encoded data */
215     const unsigned char *p = s->buf + 16;
216
217     const unsigned char *pb;
218     unsigned int pb_size;
219     unsigned char meth;
220     unsigned char *dp;   /* pointer to current frame */
221     unsigned char *pp;   /* pointer to previous frame */
222     unsigned char len;
223     int ofs;
224
225     int frame_x, frame_y;
226     int frame_width, frame_height;
227
228     frame_x = AV_RL16(&s->buf[6]);
229     frame_y = AV_RL16(&s->buf[8]);
230     frame_width = AV_RL16(&s->buf[10]) - frame_x + 1;
231     frame_height = AV_RL16(&s->buf[12]) - frame_y + 1;
232     if (frame_x < 0 || frame_width < 0 ||
233         frame_x >= s->avctx->width ||
234         frame_width > s->avctx->width ||
235         frame_x + frame_width > s->avctx->width)
236         return;
237     if (frame_y < 0 || frame_height < 0 ||
238         frame_y >= s->avctx->height ||
239         frame_height > s->avctx->height ||
240         frame_y + frame_height > s->avctx->height)
241         return;
242
243     if ((frame_width == s->avctx->width && frame_height == s->avctx->height) &&
244         (frame_x || frame_y)) {
245
246         s->x_off = frame_x;
247         s->y_off = frame_y;
248     }
249     frame_x -= s->x_off;
250     frame_y -= s->y_off;
251
252     /* if only a certain region will be updated, copy the entire previous
253      * frame before the decode */
254     if (s->prev_frame.data[0] &&
255         (frame_x || frame_y || (frame_width != s->avctx->width) ||
256         (frame_height != s->avctx->height))) {
257
258         memcpy(s->frame.data[0], s->prev_frame.data[0],
259             s->avctx->height * s->frame.linesize[0]);
260     }
261
262     /* check if there is a new palette */
263     if (s->buf[15] & 0x02) {
264         p += 2;
265         palette32 = (unsigned int *)s->palette;
266         for (i = 0; i < PALETTE_COUNT; i++) {
267             r = *p++ * 4;
268             g = *p++ * 4;
269             b = *p++ * 4;
270             palette32[i] = (r << 16) | (g << 8) | (b);
271         }
272         s->size -= (256 * 3 + 2);
273     }
274     if (s->size > 0) {
275         /* originally UnpackFrame in VAG's code */
276         pb = p;
277         pb_size = s->buf + s->size - pb;
278         if (pb_size < 1)
279             return;
280         meth = *pb++; pb_size--;
281         if (meth & 0x80) {
282             lz_unpack(pb, pb_size,
283                       s->unpack_buffer, s->unpack_buffer_size);
284             meth &= 0x7F;
285             pb = s->unpack_buffer;
286             pb_size = s->unpack_buffer_size;
287         }
288
289         dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
290         pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
291         switch (meth) {
292         case 1:
293             for (i = 0; i < frame_height; i++) {
294                 ofs = 0;
295                 do {
296                     if (pb_size < 1)
297                         return;
298                     len = *pb++;
299                     pb_size--;
300                     if (len & 0x80) {
301                         len = (len & 0x7F) + 1;
302                         if (ofs + len > frame_width || pb_size < len)
303                             return;
304                         memcpy(&dp[ofs], pb, len);
305                         pb += len;
306                         pb_size -= len;
307                         ofs += len;
308                     } else {
309                         /* interframe pixel copy */
310                         if (ofs + len + 1 > frame_width || !s->prev_frame.data[0])
311                             return;
312                         memcpy(&dp[ofs], &pp[ofs], len + 1);
313                         ofs += len + 1;
314                     }
315                 } while (ofs < frame_width);
316                 if (ofs > frame_width) {
317                     av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
318                         ofs, frame_width);
319                     break;
320                 }
321                 dp += s->frame.linesize[0];
322                 pp += s->prev_frame.linesize[0];
323             }
324             break;
325
326         case 2:
327             for (i = 0; i < frame_height; i++) {
328                 if (pb_size < frame_width)
329                     return;
330                 memcpy(dp, pb, frame_width);
331                 pb += frame_width;
332                 pb_size -= frame_width;
333                 dp += s->frame.linesize[0];
334                 pp += s->prev_frame.linesize[0];
335             }
336             break;
337
338         case 3:
339             for (i = 0; i < frame_height; i++) {
340                 ofs = 0;
341                 do {
342                     if (pb_size < 1)
343                         return;
344                     len = *pb++;
345                     pb_size--;
346                     if (len & 0x80) {
347                         len = (len & 0x7F) + 1;
348                         if (pb_size < 1)
349                             return;
350                         if (*pb++ == 0xFF)
351                             len = rle_unpack(pb, &dp[ofs], len, pb_size, frame_width - ofs);
352                         else {
353                             if (pb_size < len)
354                                 return;
355                             memcpy(&dp[ofs], pb, len);
356                         }
357                         pb += len;
358                         pb_size -= 1 + len;
359                         ofs += len;
360                     } else {
361                         /* interframe pixel copy */
362                         if (ofs + len + 1 > frame_width || !s->prev_frame.data[0])
363                             return;
364                         memcpy(&dp[ofs], &pp[ofs], len + 1);
365                         ofs += len + 1;
366                     }
367                 } while (ofs < frame_width);
368                 if (ofs > frame_width) {
369                     av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
370                         ofs, frame_width);
371                 }
372                 dp += s->frame.linesize[0];
373                 pp += s->prev_frame.linesize[0];
374             }
375             break;
376         }
377     }
378 }
379
380 static av_cold int vmdvideo_decode_init(AVCodecContext *avctx)
381 {
382     VmdVideoContext *s = avctx->priv_data;
383     int i;
384     unsigned int *palette32;
385     int palette_index = 0;
386     unsigned char r, g, b;
387     unsigned char *vmd_header;
388     unsigned char *raw_palette;
389
390     s->avctx = avctx;
391     avctx->pix_fmt = AV_PIX_FMT_PAL8;
392
393     /* make sure the VMD header made it */
394     if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
395         av_log(s->avctx, AV_LOG_ERROR, "VMD video: expected extradata size of %d\n",
396             VMD_HEADER_SIZE);
397         return -1;
398     }
399     vmd_header = (unsigned char *)avctx->extradata;
400
401     s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
402     s->unpack_buffer = av_malloc(s->unpack_buffer_size);
403     if (!s->unpack_buffer)
404         return -1;
405
406     /* load up the initial palette */
407     raw_palette = &vmd_header[28];
408     palette32 = (unsigned int *)s->palette;
409     for (i = 0; i < PALETTE_COUNT; i++) {
410         r = raw_palette[palette_index++] * 4;
411         g = raw_palette[palette_index++] * 4;
412         b = raw_palette[palette_index++] * 4;
413         palette32[i] = (r << 16) | (g << 8) | (b);
414     }
415
416     return 0;
417 }
418
419 static int vmdvideo_decode_frame(AVCodecContext *avctx,
420                                  void *data, int *data_size,
421                                  AVPacket *avpkt)
422 {
423     const uint8_t *buf = avpkt->data;
424     int buf_size = avpkt->size;
425     VmdVideoContext *s = avctx->priv_data;
426
427     s->buf = buf;
428     s->size = buf_size;
429
430     if (buf_size < 16)
431         return buf_size;
432
433     s->frame.reference = 1;
434     if (avctx->get_buffer(avctx, &s->frame)) {
435         av_log(s->avctx, AV_LOG_ERROR, "VMD Video: get_buffer() failed\n");
436         return -1;
437     }
438
439     vmd_decode(s);
440
441     /* make the palette available on the way out */
442     memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
443
444     /* shuffle frames */
445     FFSWAP(AVFrame, s->frame, s->prev_frame);
446     if (s->frame.data[0])
447         avctx->release_buffer(avctx, &s->frame);
448
449     *data_size = sizeof(AVFrame);
450     *(AVFrame*)data = s->prev_frame;
451
452     /* report that the buffer was completely consumed */
453     return buf_size;
454 }
455
456 static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
457 {
458     VmdVideoContext *s = avctx->priv_data;
459
460     if (s->prev_frame.data[0])
461         avctx->release_buffer(avctx, &s->prev_frame);
462     av_free(s->unpack_buffer);
463
464     return 0;
465 }
466
467
468 /*
469  * Audio Decoder
470  */
471
472 #define BLOCK_TYPE_AUDIO    1
473 #define BLOCK_TYPE_INITIAL  2
474 #define BLOCK_TYPE_SILENCE  3
475
476 typedef struct VmdAudioContext {
477     AVFrame frame;
478     int out_bps;
479     int chunk_size;
480 } VmdAudioContext;
481
482 static const uint16_t vmdaudio_table[128] = {
483     0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
484     0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
485     0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
486     0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
487     0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
488     0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
489     0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
490     0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
491     0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
492     0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
493     0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
494     0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
495     0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
496 };
497
498 static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
499 {
500     VmdAudioContext *s = avctx->priv_data;
501
502     if (avctx->channels < 1 || avctx->channels > 2) {
503         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
504         return AVERROR(EINVAL);
505     }
506     if (avctx->block_align < 1) {
507         av_log(avctx, AV_LOG_ERROR, "invalid block align\n");
508         return AVERROR(EINVAL);
509     }
510
511     if (avctx->bits_per_coded_sample == 16)
512         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
513     else
514         avctx->sample_fmt = AV_SAMPLE_FMT_U8;
515     s->out_bps = av_get_bytes_per_sample(avctx->sample_fmt);
516
517     s->chunk_size = avctx->block_align + avctx->channels * (s->out_bps == 2);
518
519     avcodec_get_frame_defaults(&s->frame);
520     avctx->coded_frame = &s->frame;
521
522     av_log(avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, "
523            "block align = %d, sample rate = %d\n",
524            avctx->channels, avctx->bits_per_coded_sample, avctx->block_align,
525            avctx->sample_rate);
526
527     return 0;
528 }
529
530 static void decode_audio_s16(int16_t *out, const uint8_t *buf, int buf_size,
531                              int channels)
532 {
533     int ch;
534     const uint8_t *buf_end = buf + buf_size;
535     int predictor[2];
536     int st = channels - 1;
537
538     /* decode initial raw sample */
539     for (ch = 0; ch < channels; ch++) {
540         predictor[ch] = (int16_t)AV_RL16(buf);
541         buf += 2;
542         *out++ = predictor[ch];
543     }
544
545     /* decode DPCM samples */
546     ch = 0;
547     while (buf < buf_end) {
548         uint8_t b = *buf++;
549         if (b & 0x80)
550             predictor[ch] -= vmdaudio_table[b & 0x7F];
551         else
552             predictor[ch] += vmdaudio_table[b];
553         predictor[ch] = av_clip_int16(predictor[ch]);
554         *out++ = predictor[ch];
555         ch ^= st;
556     }
557 }
558
559 static int vmdaudio_decode_frame(AVCodecContext *avctx, void *data,
560                                  int *got_frame_ptr, AVPacket *avpkt)
561 {
562     const uint8_t *buf = avpkt->data;
563     const uint8_t *buf_end;
564     int buf_size = avpkt->size;
565     VmdAudioContext *s = avctx->priv_data;
566     int block_type, silent_chunks, audio_chunks;
567     int ret;
568     uint8_t *output_samples_u8;
569     int16_t *output_samples_s16;
570
571     if (buf_size < 16) {
572         av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");
573         *got_frame_ptr = 0;
574         return buf_size;
575     }
576
577     block_type = buf[6];
578     if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {
579         av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);
580         return AVERROR(EINVAL);
581     }
582     buf      += 16;
583     buf_size -= 16;
584
585     /* get number of silent chunks */
586     silent_chunks = 0;
587     if (block_type == BLOCK_TYPE_INITIAL) {
588         uint32_t flags;
589         if (buf_size < 4) {
590             av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
591             return AVERROR(EINVAL);
592         }
593         flags         = AV_RB32(buf);
594         silent_chunks = av_popcount(flags);
595         buf      += 4;
596         buf_size -= 4;
597     } else if (block_type == BLOCK_TYPE_SILENCE) {
598         silent_chunks = 1;
599         buf_size = 0; // should already be zero but set it just to be sure
600     }
601
602     /* ensure output buffer is large enough */
603     audio_chunks = buf_size / s->chunk_size;
604
605     /* get output buffer */
606     s->frame.nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) / avctx->channels;
607     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
608         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
609         return ret;
610     }
611     output_samples_u8  = s->frame.data[0];
612     output_samples_s16 = (int16_t *)s->frame.data[0];
613
614     /* decode silent chunks */
615     if (silent_chunks > 0) {
616         int silent_size = avctx->block_align * silent_chunks;
617         if (s->out_bps == 2) {
618             memset(output_samples_s16, 0x00, silent_size * 2);
619             output_samples_s16 += silent_size;
620         } else {
621             memset(output_samples_u8,  0x80, silent_size);
622             output_samples_u8 += silent_size;
623         }
624     }
625
626     /* decode audio chunks */
627     if (audio_chunks > 0) {
628         buf_end = buf + buf_size;
629         while (buf < buf_end) {
630             if (s->out_bps == 2) {
631                 decode_audio_s16(output_samples_s16, buf, s->chunk_size,
632                                  avctx->channels);
633                 output_samples_s16 += avctx->block_align;
634             } else {
635                 memcpy(output_samples_u8, buf, s->chunk_size);
636                 output_samples_u8  += avctx->block_align;
637             }
638             buf += s->chunk_size;
639         }
640     }
641
642     *got_frame_ptr   = 1;
643     *(AVFrame *)data = s->frame;
644
645     return avpkt->size;
646 }
647
648
649 /*
650  * Public Data Structures
651  */
652
653 AVCodec ff_vmdvideo_decoder = {
654     .name           = "vmdvideo",
655     .type           = AVMEDIA_TYPE_VIDEO,
656     .id             = AV_CODEC_ID_VMDVIDEO,
657     .priv_data_size = sizeof(VmdVideoContext),
658     .init           = vmdvideo_decode_init,
659     .close          = vmdvideo_decode_end,
660     .decode         = vmdvideo_decode_frame,
661     .capabilities   = CODEC_CAP_DR1,
662     .long_name      = NULL_IF_CONFIG_SMALL("Sierra VMD video"),
663 };
664
665 AVCodec ff_vmdaudio_decoder = {
666     .name           = "vmdaudio",
667     .type           = AVMEDIA_TYPE_AUDIO,
668     .id             = AV_CODEC_ID_VMDAUDIO,
669     .priv_data_size = sizeof(VmdAudioContext),
670     .init           = vmdaudio_decode_init,
671     .decode         = vmdaudio_decode_frame,
672     .capabilities   = CODEC_CAP_DR1,
673     .long_name      = NULL_IF_CONFIG_SMALL("Sierra VMD audio"),
674 };