]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/iff.c
avisynth: Fix upside down bug
[ffmpeg] / libavformat / iff.c
index f388ca9e32f4d7cfb2c0dc760b0e985077523f00..9455d96817ead9cc477a9f8faa8f74b8389c1690 100644 (file)
@@ -1,23 +1,22 @@
 /*
- * IFF (.iff) file demuxer
  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -29,6 +28,7 @@
  * http://wiki.multimedia.cx/index.php?title=IFF
  */
 
+#include "libavcodec/bytestream.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
 #include "avformat.h"
@@ -41,6 +41,7 @@
 #define ID_PBM        MKTAG('P','B','M',' ')
 #define ID_ILBM       MKTAG('I','L','B','M')
 #define ID_BMHD       MKTAG('B','M','H','D')
+#define ID_CAMG       MKTAG('C','A','M','G')
 #define ID_CMAP       MKTAG('C','M','A','P')
 
 #define ID_FORM       MKTAG('F','O','R','M')
 #define RIGHT   4
 #define STEREO  6
 
-#define PACKET_SIZE 1024
+/**
+ * This number of bytes if added at the beginning of each AVPacket
+ * which contain additional information about video properties
+ * which has to be shared between demuxer and decoder.
+ * This number may change between frames, e.g. the demuxer might
+ * set it to smallest possible size of 2 to indicate that there's
+ * no extradata changing in this frame.
+ */
+#define IFF_EXTRA_VIDEO_SIZE 9
 
 typedef enum {
     COMP_NONE,
@@ -77,21 +86,15 @@ typedef struct {
     uint32_t  body_size;
     uint32_t  sent_bytes;
     uint32_t  audio_frame_count;
+    svx8_compression_type   svx8_compression;
+    bitmap_compression_type bitmap_compression;  ///< delta compression method used
+    unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
+    unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
+    unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
+    unsigned  transparency; ///< transparency color index in palette
+    unsigned  masking;      ///< masking method used
 } IffDemuxContext;
 
-
-static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
-{
-    uint8_t *end = dest + size;
-    size = size>>1;
-
-    while(dest < end) {
-        *dest++ = *src;
-        *dest++ = *(src+size);
-        src++;
-    }
-}
-
 /* Metadata string read */
 static int get_metadata(AVFormatContext *s,
                         const char *const tag,
@@ -127,8 +130,11 @@ static int iff_read_header(AVFormatContext *s,
     IffDemuxContext *iff = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
+    uint8_t *buf;
     uint32_t chunk_id, data_size;
-    int compression = -1;
+    uint32_t screenmode = 0;
+    unsigned transparency = 0;
+    unsigned masking = 0; // no mask
 
     st = av_new_stream(s, 0);
     if (!st)
@@ -139,7 +145,7 @@ static int iff_read_header(AVFormatContext *s,
     // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
     st->codec->codec_tag = avio_rl32(pb);
 
-    while(!pb->eof_reached) {
+    while(!url_feof(pb)) {
         uint64_t orig_pos;
         int res;
         const char *metadata_tag = NULL;
@@ -157,7 +163,7 @@ static int iff_read_header(AVFormatContext *s,
             st->codec->sample_rate = avio_rb16(pb);
             if (data_size >= 16) {
                 avio_skip(pb, 1);
-                compression        = avio_r8(pb);
+                iff->svx8_compression = avio_r8(pb);
             }
             break;
 
@@ -172,16 +178,23 @@ static int iff_read_header(AVFormatContext *s,
             st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
             break;
 
+        case ID_CAMG:
+            if (data_size < 4)
+                return AVERROR_INVALIDDATA;
+            screenmode                = avio_rb32(pb);
+            break;
+
         case ID_CMAP:
-            st->codec->extradata_size = data_size;
-            st->codec->extradata      = av_malloc(data_size);
+            st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
+            st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
             if (!st->codec->extradata)
                 return AVERROR(ENOMEM);
-            if (avio_read(pb, st->codec->extradata, data_size) < 0)
+            if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
                 return AVERROR(EIO);
             break;
 
         case ID_BMHD:
+            iff->bitmap_compression = -1;
             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
             if (data_size <= 8)
                 return AVERROR_INVALIDDATA;
@@ -189,33 +202,25 @@ static int iff_read_header(AVFormatContext *s,
             st->codec->height                = avio_rb16(pb);
             avio_skip(pb, 4); // x, y offset
             st->codec->bits_per_coded_sample = avio_r8(pb);
-            if (data_size >= 11) {
-                avio_skip(pb, 1); // masking
-                compression                  = avio_r8(pb);
+            if (data_size >= 10)
+                masking                      = avio_r8(pb);
+            if (data_size >= 11)
+                iff->bitmap_compression      = avio_r8(pb);
+            if (data_size >= 14) {
+                avio_skip(pb, 1); // padding
+                transparency                 = avio_rb16(pb);
             }
             if (data_size >= 16) {
-                avio_skip(pb, 3); // paddding, transparent
                 st->sample_aspect_ratio.num  = avio_r8(pb);
                 st->sample_aspect_ratio.den  = avio_r8(pb);
             }
             break;
 
         case ID_ANNO:
-        case ID_TEXT:
-            metadata_tag = "comment";
-            break;
-
-        case ID_AUTH:
-            metadata_tag = "artist";
-            break;
-
-        case ID_COPYRIGHT:
-            metadata_tag = "copyright";
-            break;
-
-        case ID_NAME:
-            metadata_tag = "title";
-            break;
+        case ID_TEXT:      metadata_tag = "comment";   break;
+        case ID_AUTH:      metadata_tag = "artist";    break;
+        case ID_COPYRIGHT: metadata_tag = "copyright"; break;
+        case ID_NAME:      metadata_tag = "title";     break;
         }
 
         if (metadata_tag) {
@@ -233,9 +238,9 @@ static int iff_read_header(AVFormatContext *s,
     case AVMEDIA_TYPE_AUDIO:
         av_set_pts_info(st, 32, 1, st->codec->sample_rate);
 
-        switch(compression) {
+        switch (iff->svx8_compression) {
         case COMP_NONE:
-            st->codec->codec_id = CODEC_ID_PCM_S8;
+            st->codec->codec_id = CODEC_ID_8SVX_RAW;
             break;
         case COMP_FIB:
             st->codec->codec_id = CODEC_ID_8SVX_FIB;
@@ -244,17 +249,42 @@ static int iff_read_header(AVFormatContext *s,
             st->codec->codec_id = CODEC_ID_8SVX_EXP;
             break;
         default:
-            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
+            av_log(s, AV_LOG_ERROR,
+                   "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
             return -1;
         }
 
-        st->codec->bits_per_coded_sample = 8;
+        st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4;
         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
         break;
 
     case AVMEDIA_TYPE_VIDEO:
-        switch (compression) {
+        iff->bpp          = st->codec->bits_per_coded_sample;
+        if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
+            iff->ham      = iff->bpp > 6 ? 6 : 4;
+            st->codec->bits_per_coded_sample = 24;
+        }
+        iff->flags        = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
+        iff->masking      = masking;
+        iff->transparency = transparency;
+
+        if (!st->codec->extradata) {
+            st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
+            st->codec->extradata      = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (!st->codec->extradata)
+                return AVERROR(ENOMEM);
+        }
+        buf = st->codec->extradata;
+        bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
+        bytestream_put_byte(&buf, iff->bitmap_compression);
+        bytestream_put_byte(&buf, iff->bpp);
+        bytestream_put_byte(&buf, iff->ham);
+        bytestream_put_byte(&buf, iff->flags);
+        bytestream_put_be16(&buf, iff->transparency);
+        bytestream_put_byte(&buf, iff->masking);
+
+        switch (iff->bitmap_compression) {
         case BITMAP_RAW:
             st->codec->codec_id = CODEC_ID_IFF_ILBM;
             break;
@@ -262,7 +292,8 @@ static int iff_read_header(AVFormatContext *s,
             st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
             break;
         default:
-            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
+            av_log(s, AV_LOG_ERROR,
+                   "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
             return AVERROR_INVALIDDATA;
         }
         break;
@@ -284,34 +315,27 @@ static int iff_read_packet(AVFormatContext *s,
     if(iff->sent_bytes >= iff->body_size)
         return AVERROR(EIO);
 
-    if(st->codec->channels == 2) {
-        uint8_t sample_buffer[PACKET_SIZE];
+    if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+        ret = av_get_packet(pb, pkt, iff->body_size);
+    } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+        uint8_t *buf;
 
-        ret = avio_read(pb, sample_buffer, PACKET_SIZE);
-        if(av_new_packet(pkt, PACKET_SIZE) < 0) {
-            av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
+        if (av_new_packet(pkt, iff->body_size + 2) < 0) {
             return AVERROR(ENOMEM);
         }
-        interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
-    } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
-        ret = av_get_packet(pb, pkt, iff->body_size);
+
+        buf = pkt->data;
+        bytestream_put_be16(&buf, 2);
+        ret = avio_read(pb, buf, iff->body_size);
     } else {
-        ret = av_get_packet(pb, pkt, PACKET_SIZE);
+        av_abort();
     }
 
     if(iff->sent_bytes == 0)
         pkt->flags |= AV_PKT_FLAG_KEY;
+    iff->sent_bytes = iff->body_size;
 
-    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
-        iff->sent_bytes += PACKET_SIZE;
-    } else {
-        iff->sent_bytes = iff->body_size;
-    }
     pkt->stream_index = 0;
-    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
-        pkt->pts = iff->audio_frame_count;
-        iff->audio_frame_count += ret / st->codec->channels;
-    }
     return ret;
 }