]> git.sesse.net Git - ffmpeg/blob - libavformat/wc3movie.c
ARM: silence some annoying armcc warnings
[ffmpeg] / libavformat / wc3movie.c
1 /*
2  * Wing Commander III Movie (.mve) File Demuxer
3  * Copyright (c) 2003 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  * Wing Commander III Movie file demuxer
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the WC3 .mve file format, visit:
27  *   http://www.pcisys.net/~melanson/codecs/
28  */
29
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/dict.h"
32 #include "avformat.h"
33
34 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
35 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
36 #define  PC__TAG MKTAG('_', 'P', 'C', '_')
37 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
38 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
39 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
40 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
41 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
42 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
43 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
44 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
45 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
46 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
47
48 /* video resolution unless otherwise specified */
49 #define WC3_DEFAULT_WIDTH 320
50 #define WC3_DEFAULT_HEIGHT 165
51
52 /* always use the same PCM audio parameters */
53 #define WC3_SAMPLE_RATE 22050
54 #define WC3_AUDIO_CHANNELS 1
55 #define WC3_AUDIO_BITS 16
56
57 /* nice, constant framerate */
58 #define WC3_FRAME_FPS 15
59
60 #define PALETTE_SIZE (256 * 3)
61
62 typedef struct Wc3DemuxContext {
63     int width;
64     int height;
65     int64_t pts;
66     int video_stream_index;
67     int audio_stream_index;
68
69     AVPacket vpkt;
70
71 } Wc3DemuxContext;
72
73 static int wc3_probe(AVProbeData *p)
74 {
75     if (p->buf_size < 12)
76         return 0;
77
78     if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
79         (AV_RL32(&p->buf[8]) != MOVE_TAG))
80         return 0;
81
82     return AVPROBE_SCORE_MAX;
83 }
84
85 static int wc3_read_header(AVFormatContext *s,
86                            AVFormatParameters *ap)
87 {
88     Wc3DemuxContext *wc3 = s->priv_data;
89     AVIOContext *pb = s->pb;
90     unsigned int fourcc_tag;
91     unsigned int size;
92     AVStream *st;
93     int ret = 0;
94     char *buffer;
95
96     /* default context members */
97     wc3->width = WC3_DEFAULT_WIDTH;
98     wc3->height = WC3_DEFAULT_HEIGHT;
99     wc3->pts = 0;
100     wc3->video_stream_index = wc3->audio_stream_index = 0;
101     av_init_packet(&wc3->vpkt);
102     wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
103
104     /* skip the first 3 32-bit numbers */
105     avio_skip(pb, 12);
106
107     /* traverse through the chunks and load the header information before
108      * the first BRCH tag */
109     fourcc_tag = avio_rl32(pb);
110     size = (avio_rb32(pb) + 1) & (~1);
111
112     do {
113         switch (fourcc_tag) {
114
115         case SOND_TAG:
116         case INDX_TAG:
117             /* SOND unknown, INDX unnecessary; ignore both */
118             avio_skip(pb, size);
119             break;
120
121         case PC__TAG:
122             /* number of palettes, unneeded */
123             avio_skip(pb, 12);
124             break;
125
126         case BNAM_TAG:
127             /* load up the name */
128             buffer = av_malloc(size+1);
129             if (!buffer)
130                 return AVERROR(ENOMEM);
131             if ((ret = avio_read(pb, buffer, size)) != size)
132                 return AVERROR(EIO);
133             buffer[size] = 0;
134             av_dict_set(&s->metadata, "title", buffer,
135                                    AV_DICT_DONT_STRDUP_VAL);
136             break;
137
138         case SIZE_TAG:
139             /* video resolution override */
140             wc3->width  = avio_rl32(pb);
141             wc3->height = avio_rl32(pb);
142             break;
143
144         case PALT_TAG:
145             /* one of several palettes */
146             avio_seek(pb, -8, SEEK_CUR);
147             av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
148             break;
149
150         default:
151             av_log(s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
152                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
153                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
154             return AVERROR_INVALIDDATA;
155             break;
156         }
157
158         fourcc_tag = avio_rl32(pb);
159         /* chunk sizes are 16-bit aligned */
160         size = (avio_rb32(pb) + 1) & (~1);
161         if (pb->eof_reached)
162             return AVERROR(EIO);
163
164     } while (fourcc_tag != BRCH_TAG);
165
166     /* initialize the decoder streams */
167     st = av_new_stream(s, 0);
168     if (!st)
169         return AVERROR(ENOMEM);
170     av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
171     wc3->video_stream_index = st->index;
172     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
173     st->codec->codec_id = CODEC_ID_XAN_WC3;
174     st->codec->codec_tag = 0;  /* no fourcc */
175     st->codec->width = wc3->width;
176     st->codec->height = wc3->height;
177
178     st = av_new_stream(s, 0);
179     if (!st)
180         return AVERROR(ENOMEM);
181     av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
182     wc3->audio_stream_index = st->index;
183     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
184     st->codec->codec_id = CODEC_ID_PCM_S16LE;
185     st->codec->codec_tag = 1;
186     st->codec->channels = WC3_AUDIO_CHANNELS;
187     st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
188     st->codec->sample_rate = WC3_SAMPLE_RATE;
189     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
190         st->codec->bits_per_coded_sample;
191     st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
192
193     return 0;
194 }
195
196 static int wc3_read_packet(AVFormatContext *s,
197                            AVPacket *pkt)
198 {
199     Wc3DemuxContext *wc3 = s->priv_data;
200     AVIOContext *pb = s->pb;
201     unsigned int fourcc_tag;
202     unsigned int size;
203     int packet_read = 0;
204     int ret = 0;
205     unsigned char text[1024];
206
207     while (!packet_read) {
208
209         fourcc_tag = avio_rl32(pb);
210         /* chunk sizes are 16-bit aligned */
211         size = (avio_rb32(pb) + 1) & (~1);
212         if (pb->eof_reached)
213             return AVERROR(EIO);
214
215         switch (fourcc_tag) {
216
217         case BRCH_TAG:
218             /* no-op */
219             break;
220
221         case SHOT_TAG:
222             /* load up new palette */
223             avio_seek(pb, -8, SEEK_CUR);
224             av_append_packet(pb, &wc3->vpkt, 8 + 4);
225             break;
226
227         case VGA__TAG:
228             /* send out video chunk */
229             avio_seek(pb, -8, SEEK_CUR);
230             ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
231             // ignore error if we have some data
232             if (wc3->vpkt.size > 0)
233                 ret = 0;
234             *pkt = wc3->vpkt;
235             wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
236             pkt->stream_index = wc3->video_stream_index;
237             pkt->pts = wc3->pts;
238             packet_read = 1;
239             break;
240
241         case TEXT_TAG:
242             /* subtitle chunk */
243 #if 0
244             avio_skip(pb, size);
245 #else
246             if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
247                 ret = AVERROR(EIO);
248             else {
249                 int i = 0;
250                 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
251                 av_log (s, AV_LOG_DEBUG, "  inglish: %s\n", &text[i + 1]);
252                 i += text[i] + 1;
253                 av_log (s, AV_LOG_DEBUG, "  doytsch: %s\n", &text[i + 1]);
254                 i += text[i] + 1;
255                 av_log (s, AV_LOG_DEBUG, "  fronsay: %s\n", &text[i + 1]);
256             }
257 #endif
258             break;
259
260         case AUDI_TAG:
261             /* send out audio chunk */
262             ret= av_get_packet(pb, pkt, size);
263             pkt->stream_index = wc3->audio_stream_index;
264             pkt->pts = wc3->pts;
265
266             /* time to advance pts */
267             wc3->pts++;
268
269             packet_read = 1;
270             break;
271
272         default:
273             av_log (s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
274                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
275                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
276             ret = AVERROR_INVALIDDATA;
277             packet_read = 1;
278             break;
279         }
280     }
281
282     return ret;
283 }
284
285 static int wc3_read_close(AVFormatContext *s)
286 {
287     Wc3DemuxContext *wc3 = s->priv_data;
288
289     if (wc3->vpkt.size > 0)
290         av_free_packet(&wc3->vpkt);
291
292     return 0;
293 }
294
295 AVInputFormat ff_wc3_demuxer = {
296     "wc3movie",
297     NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
298     sizeof(Wc3DemuxContext),
299     wc3_probe,
300     wc3_read_header,
301     wc3_read_packet,
302     wc3_read_close,
303 };