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