]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vmdav.c
interpret H264 VUI timing info correctly
[ffmpeg] / libavcodec / vmdav.c
index a5c7f450c2661c4417b13620297b9fcaa6106702..34685b676dae9352712125bdb17008c5503c5ad7 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  */
 
  * @file vmdvideo.c
  * Sierra VMD audio & video decoders
  * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
+ * for more information on the Sierra VMD format, visit:
+ *   http://www.pcisys.net/~melanson/codecs/
  *
  * The video decoder outputs PAL8 colorspace data. The decoder expects
  * a 0x330-byte VMD file header to be transmitted via extradata during
  * codec initialization. Each encoded frame that is sent to this decoder
- * is expected to be prepended with the appropriate 16-byte frame 
+ * is expected to be prepended with the appropriate 16-byte frame
  * information record from the VMD file.
  *
  * The audio decoder, like the video decoder, expects each encoded data
- * chunk to be prepended with the approriate 16-byte frame information
+ * chunk to be prepended with the appropriate 16-byte frame information
  * record from the VMD file. It does not require the 0x330-byte VMD file
  * header, but it does need the audio setup parameters passed in through
  * normal libavcodec API means.
 #define VMD_HEADER_SIZE 0x330
 #define PALETTE_COUNT 256
 
-#define LE_16(x)  ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
-#define LE_32(x)  ((((uint8_t*)(x))[3] << 24) | \
-                   (((uint8_t*)(x))[2] << 16) | \
-                   (((uint8_t*)(x))[1] << 8) | \
-                    ((uint8_t*)(x))[0])
-
 /*
  * Video Decoder
  */
@@ -70,16 +66,18 @@ typedef struct VmdVideoContext {
 
     unsigned char palette[PALETTE_COUNT * 4];
     unsigned char *unpack_buffer;
+    int unpack_buffer_size;
 
 } VmdVideoContext;
 
 #define QUEUE_SIZE 0x1000
 #define QUEUE_MASK 0x0FFF
 
-static void lz_unpack(unsigned char *src, unsigned char *dest)
+static void lz_unpack(unsigned char *src, unsigned char *dest, int dest_len)
 {
     unsigned char *s;
     unsigned char *d;
+    unsigned char *d_end;
     unsigned char queue[QUEUE_SIZE];
     unsigned int qpos;
     unsigned int dataleft;
@@ -91,6 +89,7 @@ static void lz_unpack(unsigned char *src, unsigned char *dest)
 
     s = src;
     d = dest;
+    d_end = d + dest_len;
     dataleft = LE_32(s);
     s += 4;
     memset(queue, QUEUE_SIZE, 0x20);
@@ -106,6 +105,8 @@ static void lz_unpack(unsigned char *src, unsigned char *dest)
     while (dataleft > 0) {
         tag = *s++;
         if ((tag == 0xFF) && (dataleft > 8)) {
+            if (d + 8 > d_end)
+                return;
             for (i = 0; i < 8; i++) {
                 queue[qpos++] = *d++ = *s++;
                 qpos &= QUEUE_MASK;
@@ -116,6 +117,8 @@ static void lz_unpack(unsigned char *src, unsigned char *dest)
                 if (dataleft == 0)
                     break;
                 if (tag & 0x01) {
+                    if (d + 1 > d_end)
+                        return;
                     queue[qpos++] = *d++ = *s++;
                     qpos &= QUEUE_MASK;
                     dataleft--;
@@ -125,6 +128,8 @@ static void lz_unpack(unsigned char *src, unsigned char *dest)
                     chainlen = (*s++ & 0x0F) + 3;
                     if (chainlen == speclen)
                         chainlen = *s++ + 0xF + 3;
+                    if (d + chainlen > d_end)
+                        return;
                     for (j = 0; j < chainlen; j++) {
                         *d = queue[chainofs++ & QUEUE_MASK];
                         queue[qpos++] = *d++;
@@ -138,27 +143,33 @@ static void lz_unpack(unsigned char *src, unsigned char *dest)
     }
 }
 
-static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
+static int rle_unpack(unsigned char *src, unsigned char *dest,
+    int src_len, int dest_len)
 {
     unsigned char *ps;
     unsigned char *pd;
     int i, l;
+    unsigned char *dest_end = dest + dest_len;
 
     ps = src;
     pd = dest;
-    if (len & 1)
+    if (src_len & 1)
         *pd++ = *ps++;
 
-    len >>= 1;
+    src_len >>= 1;
     i = 0;
     do {
         l = *ps++;
         if (l & 0x80) {
             l = (l & 0x7F) * 2;
+            if (pd + l > dest_end)
+                return (ps - src);
             memcpy(pd, ps, l);
             ps += l;
             pd += l;
         } else {
+            if (pd + i > dest_end)
+                return (ps - src);
             for (i = 0; i < l; i++) {
                 *pd++ = ps[0];
                 *pd++ = ps[1];
@@ -166,7 +177,7 @@ static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
             ps += 2;
         }
         i += l;
-    } while (i < len);
+    } while (i < src_len);
 
     return (ps - src);
 }
@@ -189,6 +200,7 @@ static void vmd_decode(VmdVideoContext *s)
 
     int frame_x, frame_y;
     int frame_width, frame_height;
+    int dp_size;
 
     frame_x = LE_16(&s->buf[6]);
     frame_y = LE_16(&s->buf[8]);
@@ -200,7 +212,7 @@ static void vmd_decode(VmdVideoContext *s)
     if (frame_x || frame_y || (frame_width != s->avctx->width) ||
         (frame_height != s->avctx->height)) {
 
-        memcpy(s->frame.data[0], s->prev_frame.data[0], 
+        memcpy(s->frame.data[0], s->prev_frame.data[0],
             s->avctx->height * s->frame.linesize[0]);
     }
 
@@ -221,12 +233,13 @@ static void vmd_decode(VmdVideoContext *s)
         pb = p;
         meth = *pb++;
         if (meth & 0x80) {
-            lz_unpack(pb, s->unpack_buffer);
+            lz_unpack(pb, s->unpack_buffer, s->unpack_buffer_size);
             meth &= 0x7F;
             pb = s->unpack_buffer;
         }
 
         dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
+        dp_size = s->frame.linesize[0] * s->avctx->height;
         pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
         switch (meth) {
         case 1:
@@ -236,17 +249,21 @@ static void vmd_decode(VmdVideoContext *s)
                     len = *pb++;
                     if (len & 0x80) {
                         len = (len & 0x7F) + 1;
+                        if (ofs + len > frame_width)
+                            return;
                         memcpy(&dp[ofs], pb, len);
                         pb += len;
                         ofs += len;
                     } else {
                         /* interframe pixel copy */
+                        if (ofs + len + 1 > frame_width)
+                            return;
                         memcpy(&dp[ofs], &pp[ofs], len + 1);
                         ofs += len + 1;
                     }
                 } while (ofs < frame_width);
                 if (ofs > frame_width) {
-                    printf (" VMD video: offset > width (%d > %d)\n",
+                    av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
                         ofs, frame_width);
                     break;
                 }
@@ -272,19 +289,21 @@ static void vmd_decode(VmdVideoContext *s)
                     if (len & 0x80) {
                         len = (len & 0x7F) + 1;
                         if (*pb++ == 0xFF)
-                            len = rle_unpack(pb, dp, len);
+                            len = rle_unpack(pb, &dp[ofs], len, frame_width - ofs);
                         else
                             memcpy(&dp[ofs], pb, len);
                         pb += len;
                         ofs += len;
                     } else {
                         /* interframe pixel copy */
+                        if (ofs + len + 1 > frame_width)
+                            return;
                         memcpy(&dp[ofs], &pp[ofs], len + 1);
                         ofs += len + 1;
                     }
                 } while (ofs < frame_width);
                 if (ofs > frame_width) {
-                    printf (" VMD video: offset > width (%d > %d)\n",
+                    av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
                         ofs, frame_width);
                 }
                 dp += s->frame.linesize[0];
@@ -312,13 +331,14 @@ static int vmdvideo_decode_init(AVCodecContext *avctx)
 
     /* make sure the VMD header made it */
     if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
-        printf("  VMD video: expected extradata size of %d\n", 
+        av_log(s->avctx, AV_LOG_ERROR, "VMD video: expected extradata size of %d\n",
             VMD_HEADER_SIZE);
         return -1;
     }
     vmd_header = (unsigned char *)avctx->extradata;
 
-    s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
+    s->unpack_buffer_size = LE_32(&vmd_header[800]);
+    s->unpack_buffer = av_malloc(s->unpack_buffer_size);
     if (!s->unpack_buffer)
         return -1;
 
@@ -346,9 +366,12 @@ static int vmdvideo_decode_frame(AVCodecContext *avctx,
     s->buf = buf;
     s->size = buf_size;
 
+    if (buf_size < 16)
+        return buf_size;
+
     s->frame.reference = 1;
     if (avctx->get_buffer(avctx, &s->frame)) {
-        printf ("  VMD Video: get_buffer() failed\n");
+        av_log(s->avctx, AV_LOG_ERROR, "VMD Video: get_buffer() failed\n");
         return -1;
     }
 
@@ -387,6 +410,7 @@ static int vmdvideo_decode_end(AVCodecContext *avctx)
  */
 
 typedef struct VmdAudioContext {
+    AVCodecContext *avctx;
     int channels;
     int bits;
     int block_align;
@@ -401,12 +425,13 @@ static int vmdaudio_decode_init(AVCodecContext *avctx)
     VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
     int i;
 
+    s->avctx = avctx;
     s->channels = avctx->channels;
     s->bits = avctx->bits_per_sample;
     s->block_align = avctx->block_align;
 
-printf ("  %d channels, %d bits/sample, block align = %d\n",
-  s->channels, s->bits, s->block_align);
+    av_log(s->avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, block align = %d, sample rate = %d\n",
+            s->channels, s->bits, s->block_align, avctx->sample_rate);
 
     /* set up the steps8 and steps16 tables */
     for (i = 0; i < 8; i++) {
@@ -457,10 +482,17 @@ static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data,
 
 }
 
-static void vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
+static int vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
     uint8_t *buf, int silence)
 {
+    int bytes_decoded = 0;
+    int i;
+
+    if (silence)
+        av_log(s->avctx, AV_LOG_INFO, "silent block!\n");
     if (s->channels == 2) {
+
+        /* stereo handling */
         if ((s->block_align & 0x01) == 0) {
             if (silence)
                 memset(data, 0, s->block_align * 2);
@@ -469,11 +501,31 @@ static void vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
         } else {
             if (silence)
                 memset(data, 0, s->block_align * 2);
-//            else
-//                vmdaudio_decode_audio(s, data, buf, 1);
+            else
+                vmdaudio_decode_audio(s, data, buf, 1);
         }
     } else {
+
+        /* mono handling */
+        if (silence) {
+            if (s->bits == 16) {
+                memset(data, 0, s->block_align * 2);
+                bytes_decoded = s->block_align * 2;
+            } else {
+//                memset(data, 0x00, s->block_align);
+//                bytes_decoded = s->block_align;
+memset(data, 0x00, s->block_align * 2);
+bytes_decoded = s->block_align * 2;
+            }
+        } else {
+            /* copy the data but convert it to signed */
+            for (i = 0; i < s->block_align; i++)
+                data[i * 2 + 1] = buf[i] + 0x80;
+            bytes_decoded = s->block_align * 2;
+        }
     }
+
+    return bytes_decoded;
 }
 
 static int vmdaudio_decode_frame(AVCodecContext *avctx,
@@ -488,9 +540,12 @@ static int vmdaudio_decode_frame(AVCodecContext *avctx,
     unsigned char *p = buf + 16;
     unsigned char *p_end = buf + buf_size;
 
+    if (buf_size < 16)
+        return buf_size;
+
     if (buf[6] == 1) {
         /* the chunk contains audio */
-        vmdaudio_loadsound(s, output_samples, p, 0);
+        *data_size = vmdaudio_loadsound(s, output_samples, p, 0);
     } else if (buf[6] == 2) {
         /* the chunk contains audio and silence mixed together */
         sound_flags = LE_32(p);
@@ -500,22 +555,21 @@ static int vmdaudio_decode_frame(AVCodecContext *avctx,
 
         while (p < p_end) {
             if (sound_flags & 0x01)
-                /* audio */
-                vmdaudio_loadsound(s, output_samples, p, 1);
-            else
                 /* silence */
-                vmdaudio_loadsound(s, output_samples, p, 0);
-            p += s->block_align;
+                *data_size += vmdaudio_loadsound(s, output_samples, p, 1);
+            else {
+                /* audio */
+                *data_size += vmdaudio_loadsound(s, output_samples, p, 0);
+                p += s->block_align;
+            }
             output_samples += (s->block_align * s->bits / 8);
             sound_flags >>= 1;
         }
     } else if (buf[6] == 3) {
         /* silent chunk */
-        vmdaudio_loadsound(s, output_samples, p, 1);
+        *data_size = vmdaudio_loadsound(s, output_samples, p, 1);
     }
 
-
-//    *datasize = ;
     return buf_size;
 }