]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/wc3movie.c
Support raw TrueHD files
[ffmpeg] / libavformat / wc3movie.c
index 9cca580ea76535025b1616cbf5d6921114ab25a0..59b11ae2b232b165e24702ffd0eafd550e1f60f3 100644 (file)
@@ -2,36 +2,39 @@
  * Wing Commander III Movie (.mve) File Demuxer
  * Copyright (c) 2003 The ffmpeg Project
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * 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 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library 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 this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
- * @file wc3movie.c
+ * @file libavformat/wc3movie.c
  * Wing Commander III Movie file demuxer
  * by Mike Melanson (melanson@pcisys.net)
  * for more information on the WC3 .mve file format, visit:
  *   http://www.pcisys.net/~melanson/codecs/
  */
 
+#include "libavutil/intreadwrite.h"
 #include "avformat.h"
 
 #define WC3_PREAMBLE_SIZE 8
 
 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
-#define _PC__TAG MKTAG('_', 'P', 'C', '_')
+#define  PC__TAG MKTAG('_', 'P', 'C', '_')
 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
@@ -53,7 +56,7 @@
 #define WC3_AUDIO_BITS 16
 
 /* nice, constant framerate */
-#define WC3_FRAME_PTS_INC (90000 / 15)
+#define WC3_FRAME_FPS 15
 
 #define PALETTE_SIZE (256 * 3)
 #define PALETTE_COUNT 256
@@ -113,8 +116,8 @@ static int wc3_probe(AVProbeData *p)
     if (p->buf_size < 12)
         return 0;
 
-    if ((LE_32(&p->buf[0]) != FORM_TAG) ||
-        (LE_32(&p->buf[8]) != MOVE_TAG))
+    if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
+        (AV_RL32(&p->buf[8]) != MOVE_TAG))
         return 0;
 
     return AVPROBE_SCORE_MAX;
@@ -123,12 +126,13 @@ static int wc3_probe(AVProbeData *p)
 static int wc3_read_header(AVFormatContext *s,
                            AVFormatParameters *ap)
 {
-    Wc3DemuxContext *wc3 = (Wc3DemuxContext *)s->priv_data;
-    ByteIOContext *pb = &s->pb;
+    Wc3DemuxContext *wc3 = s->priv_data;
+    ByteIOContext *pb = s->pb;
     unsigned int fourcc_tag;
     unsigned int size;
     AVStream *st;
     unsigned char preamble[WC3_PREAMBLE_SIZE];
+    char buffer[513];
     int ret = 0;
     int current_palette = 0;
     int bytes_to_read;
@@ -150,9 +154,9 @@ static int wc3_read_header(AVFormatContext *s,
      * the first BRCH tag */
     if ((ret = get_buffer(pb, preamble, WC3_PREAMBLE_SIZE)) !=
         WC3_PREAMBLE_SIZE)
-        return AVERROR_IO;
-    fourcc_tag = LE_32(&preamble[0]);
-    size = (BE_32(&preamble[4]) + 1) & (~1);
+        return AVERROR(EIO);
+    fourcc_tag = AV_RL32(&preamble[0]);
+    size = (AV_RB32(&preamble[4]) + 1) & (~1);
 
     do {
         switch (fourcc_tag) {
@@ -163,12 +167,12 @@ static int wc3_read_header(AVFormatContext *s,
             url_fseek(pb, size, SEEK_CUR);
             break;
 
-        case _PC__TAG:
+        case PC__TAG:
             /* need the number of palettes */
             url_fseek(pb, 8, SEEK_CUR);
             if ((ret = get_buffer(pb, preamble, 4)) != 4)
-                return AVERROR_IO;
-            wc3->palette_count = LE_32(&preamble[0]);
+                return AVERROR(EIO);
+            wc3->palette_count = AV_RL32(&preamble[0]);
             if((unsigned)wc3->palette_count >= UINT_MAX / PALETTE_SIZE){
                 wc3->palette_count= 0;
                 return -1;
@@ -182,17 +186,19 @@ static int wc3_read_header(AVFormatContext *s,
                 bytes_to_read = size;
             else
                 bytes_to_read = 512;
-            if ((ret = get_buffer(pb, s->title, bytes_to_read)) != bytes_to_read)
-                return AVERROR_IO;
+            if ((ret = get_buffer(pb, buffer, bytes_to_read)) != bytes_to_read)
+                return AVERROR(EIO);
+            buffer[bytes_to_read] = 0;
+            av_metadata_set(&s->metadata, "title", buffer);
             break;
 
         case SIZE_TAG:
             /* video resolution override */
             if ((ret = get_buffer(pb, preamble, WC3_PREAMBLE_SIZE)) !=
                 WC3_PREAMBLE_SIZE)
-                return AVERROR_IO;
-            wc3->width = LE_32(&preamble[0]);
-            wc3->height = LE_32(&preamble[4]);
+                return AVERROR(EIO);
+            wc3->width = AV_RL32(&preamble[0]);
+            wc3->height = AV_RL32(&preamble[4]);
             break;
 
         case PALT_TAG:
@@ -202,7 +208,7 @@ static int wc3_read_header(AVFormatContext *s,
             if ((ret = get_buffer(pb,
                 &wc3->palettes[current_palette * PALETTE_SIZE],
                 PALETTE_SIZE)) != PALETTE_SIZE)
-                return AVERROR_IO;
+                return AVERROR(EIO);
 
             /* transform the current palette in place */
             for (i = current_palette * PALETTE_SIZE;
@@ -226,18 +232,18 @@ static int wc3_read_header(AVFormatContext *s,
 
         if ((ret = get_buffer(pb, preamble, WC3_PREAMBLE_SIZE)) !=
             WC3_PREAMBLE_SIZE)
-            return AVERROR_IO;
-        fourcc_tag = LE_32(&preamble[0]);
+            return AVERROR(EIO);
+        fourcc_tag = AV_RL32(&preamble[0]);
         /* chunk sizes are 16-bit aligned */
-        size = (BE_32(&preamble[4]) + 1) & (~1);
+        size = (AV_RB32(&preamble[4]) + 1) & (~1);
 
     } while (fourcc_tag != BRCH_TAG);
 
     /* initialize the decoder streams */
     st = av_new_stream(s, 0);
     if (!st)
-        return AVERROR_NOMEM;
-    av_set_pts_info(st, 33, 1, 90000);
+        return AVERROR(ENOMEM);
+    av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
     wc3->video_stream_index = st->index;
     st->codec->codec_type = CODEC_TYPE_VIDEO;
     st->codec->codec_id = CODEC_ID_XAN_WC3;
@@ -250,17 +256,17 @@ static int wc3_read_header(AVFormatContext *s,
 
     st = av_new_stream(s, 0);
     if (!st)
-        return AVERROR_NOMEM;
-    av_set_pts_info(st, 33, 1, 90000);
+        return AVERROR(ENOMEM);
+    av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
     wc3->audio_stream_index = st->index;
     st->codec->codec_type = CODEC_TYPE_AUDIO;
     st->codec->codec_id = CODEC_ID_PCM_S16LE;
     st->codec->codec_tag = 1;
     st->codec->channels = WC3_AUDIO_CHANNELS;
-    st->codec->bits_per_sample = WC3_AUDIO_BITS;
+    st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
     st->codec->sample_rate = WC3_SAMPLE_RATE;
     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
-        st->codec->bits_per_sample;
+        st->codec->bits_per_coded_sample;
     st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
 
     return 0;
@@ -269,8 +275,8 @@ static int wc3_read_header(AVFormatContext *s,
 static int wc3_read_packet(AVFormatContext *s,
                            AVPacket *pkt)
 {
-    Wc3DemuxContext *wc3 = (Wc3DemuxContext *)s->priv_data;
-    ByteIOContext *pb = &s->pb;
+    Wc3DemuxContext *wc3 = s->priv_data;
+    ByteIOContext *pb = s->pb;
     unsigned int fourcc_tag;
     unsigned int size;
     int packet_read = 0;
@@ -287,11 +293,11 @@ static int wc3_read_packet(AVFormatContext *s,
         /* get the next chunk preamble */
         if ((ret = get_buffer(pb, preamble, WC3_PREAMBLE_SIZE)) !=
             WC3_PREAMBLE_SIZE)
-            ret = AVERROR_IO;
+            return AVERROR(EIO);
 
-        fourcc_tag = LE_32(&preamble[0]);
+        fourcc_tag = AV_RL32(&preamble[0]);
         /* chunk sizes are 16-bit aligned */
-        size = (BE_32(&preamble[4]) + 1) & (~1);
+        size = (AV_RB32(&preamble[4]) + 1) & (~1);
 
         switch (fourcc_tag) {
 
@@ -302,8 +308,8 @@ static int wc3_read_packet(AVFormatContext *s,
         case SHOT_TAG:
             /* load up new palette */
             if ((ret = get_buffer(pb, preamble, 4)) != 4)
-                return AVERROR_IO;
-            palette_number = LE_32(&preamble[0]);
+                return AVERROR(EIO);
+            palette_number = AV_RL32(&preamble[0]);
             if (palette_number >= wc3->palette_count)
                 return AVERROR_INVALIDDATA;
             base_palette_index = palette_number * PALETTE_COUNT * 3;
@@ -322,7 +328,7 @@ static int wc3_read_packet(AVFormatContext *s,
             pkt->stream_index = wc3->video_stream_index;
             pkt->pts = wc3->pts;
             if (ret != size)
-                ret = AVERROR_IO;
+                ret = AVERROR(EIO);
             packet_read = 1;
             break;
 
@@ -332,7 +338,7 @@ static int wc3_read_packet(AVFormatContext *s,
             url_fseek(pb, size, SEEK_CUR);
 #else
             if ((unsigned)size > sizeof(text) || (ret = get_buffer(pb, text, size)) != size)
-                ret = AVERROR_IO;
+                ret = AVERROR(EIO);
             else {
                 int i = 0;
                 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
@@ -351,10 +357,10 @@ static int wc3_read_packet(AVFormatContext *s,
             pkt->stream_index = wc3->audio_stream_index;
             pkt->pts = wc3->pts;
             if (ret != size)
-                ret = AVERROR_IO;
+                ret = AVERROR(EIO);
 
             /* time to advance pts */
-            wc3->pts += WC3_FRAME_PTS_INC;
+            wc3->pts++;
 
             packet_read = 1;
             break;
@@ -374,25 +380,19 @@ static int wc3_read_packet(AVFormatContext *s,
 
 static int wc3_read_close(AVFormatContext *s)
 {
-    Wc3DemuxContext *wc3 = (Wc3DemuxContext *)s->priv_data;
+    Wc3DemuxContext *wc3 = s->priv_data;
 
     av_free(wc3->palettes);
 
     return 0;
 }
 
-static AVInputFormat wc3_iformat = {
+AVInputFormat wc3_demuxer = {
     "wc3movie",
-    "Wing Commander III movie format",
+    NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
     sizeof(Wc3DemuxContext),
     wc3_probe,
     wc3_read_header,
     wc3_read_packet,
     wc3_read_close,
 };
-
-int wc3_init(void)
-{
-    av_register_input_format(&wc3_iformat);
-    return 0;
-}