placeholder IDs for 4xm A/V codecs
[libav.git] / libavformat / 4xm.c
CommitLineData
cef4ba9e
MM
1/*
2 * 4X Technologies .4xm File Demuxer (no muxer)
3 * Copyright (c) 2003 The ffmpeg Project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/**
21 * @file 4xm.c
22 * 4X Technologies file demuxer
23 * by Mike Melanson (melanson@pcisys.net)
24 * for more information on the .4xm file format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
26 */
27
28#include "avformat.h"
29
30#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
31#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
32 (((uint8_t*)(x))[2] << 16) | \
33 (((uint8_t*)(x))[1] << 8) | \
34 ((uint8_t*)(x))[0])
35#define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
36#define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
37 (((uint8_t*)(x))[1] << 16) | \
38 (((uint8_t*)(x))[2] << 8) | \
39 ((uint8_t*)(x))[3])
40
41#define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
42 ( (long)(unsigned char)(ch3) | \
43 ( (long)(unsigned char)(ch2) << 8 ) | \
44 ( (long)(unsigned char)(ch1) << 16 ) | \
45 ( (long)(unsigned char)(ch0) << 24 ) )
46
47#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
48#define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
49#define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
50#define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
51#define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
52#define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
53#define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
54#define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
55#define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
56#define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
57#define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
58#define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
59#define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
60#define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
61#define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
62#define _TAG FOURCC_TAG('', '', '', '')
63
64#define vtrk_SIZE 0x44
65#define strk_SIZE 0x28
66
67#define GET_LIST_HEADER() \
68 fourcc_tag = get_be32(pb); \
69 size = get_le32(pb); \
70 if (fourcc_tag != LIST_TAG) \
71 return AVERROR_INVALIDDATA; \
72 fourcc_tag = get_be32(pb);
73
74typedef struct AudioTrack {
75 int sample_rate;
76 int bits;
77 int channels;
78} AudioTrack;
79
80typedef struct FourxmDemuxContext {
81 int width;
82 int height;
83 int track_count;
84 AudioTrack *tracks;
85 int selected_track;
86} FourxmDemuxContext;
87
88static int fourxm_probe(AVProbeData *p)
89{
90 if ((BE_32(&p->buf[0]) != RIFF_TAG) ||
91 (BE_32(&p->buf[8]) != _4XMV_TAG))
92 return 0;
93
94printf (" detected .4xm file\n");
95
96 return AVPROBE_SCORE_MAX;
97}
98
99static int fourxm_read_header(AVFormatContext *s,
100 AVFormatParameters *ap)
101{
102 ByteIOContext *pb = &s->pb;
103 unsigned int fourcc_tag;
104 unsigned int size;
105 int header_size;
106 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
107 unsigned char *header;
108 int i;
109 int current_track = -1;
110 AVStream *st;
111
112 fourxm->track_count = 0;
113 fourxm->tracks = NULL;
114 fourxm->selected_track = 0;
115
116 /* skip the first 3 32-bit numbers */
117 url_fseek(pb, 12, SEEK_CUR);
118
119 /* check for LIST-HEAD */
120 GET_LIST_HEADER();
121 header_size = size - 4;
122 if (fourcc_tag != HEAD_TAG)
123 return AVERROR_INVALIDDATA;
124
125 /* allocate space for the header and load the whole thing */
126 header = av_malloc(header_size);
127 if (!header)
128 return AVERROR_NOMEM;
129 if (get_buffer(pb, header, header_size) != header_size)
130 return AVERROR_IO;
131
132 /* take the lazy approach and search for any and all vtrk and strk chunks */
133 for (i = 0; i < header_size - 8; i++) {
134 fourcc_tag = BE_32(&header[i]);
135 size = LE_32(&header[i + 4]);
136
137 if (fourcc_tag == vtrk_TAG) {
138 /* check that there is enough data */
139 if (size != vtrk_SIZE) {
140 av_free(header);
141 return AVERROR_INVALIDDATA;
142 }
143 fourxm->width = LE_32(&header[i + 36]);
144 fourxm->height = LE_32(&header[i + 40]);
145 i += 8 + size;
146 } else if (fourcc_tag == strk_TAG) {
147 /* check that there is enough data */
148 if (size != strk_SIZE) {
149 av_free(header);
150 return AVERROR_INVALIDDATA;
151 }
152 current_track = LE_32(&header[i + 8]);
153 if (current_track + 1 > fourxm->track_count) {
5a634a69
MM
154 fourxm->track_count = current_track + 1;
155 fourxm->tracks = av_realloc(fourxm->tracks,
156 fourxm->track_count * sizeof(AudioTrack));
cef4ba9e
MM
157 if (!fourxm->tracks) {
158 av_free(header);
159 return AVERROR_NOMEM;
160 }
161 }
162 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
163 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
164 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
165 i += 8 + size;
166 }
167 }
168
169 av_free(header);
170
171 /* skip over the LIST-MOVI chunk (which is where the stream should be */
172 GET_LIST_HEADER();
173 if (fourcc_tag != MOVI_TAG)
174 return AVERROR_INVALIDDATA;
175
176 if (current_track > -1) {
177 st = av_new_stream(s, 0);
178 if (!st)
179 return AVERROR_NOMEM;
180
181 st->codec.codec_type = CODEC_TYPE_AUDIO;
182 st->codec.codec_tag = 1;
183 st->codec.channels = fourxm->tracks[current_track].channels;
184 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
185 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
186 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
187 st->codec.bits_per_sample;
188 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
189 if (st->codec.bits_per_sample == 8)
190 st->codec.codec_id = CODEC_ID_PCM_U8;
191 else
192 st->codec.codec_id = CODEC_ID_PCM_S16LE;
193 }
194
195 return 0;
196}
197
198static int fourxm_read_packet(AVFormatContext *s,
199 AVPacket *pkt)
200{
201 FourxmDemuxContext *fourxm = s->priv_data;
202 ByteIOContext *pb = &s->pb;
203 unsigned int fourcc_tag;
204 unsigned int size;
205 int ret = 0;
206 int track_number;
207 int packet_read = 0;
208
209 while (!packet_read) {
210
211 fourcc_tag = get_be32(pb);
212 size = get_le32(pb);
213 if (fourcc_tag == LIST_TAG) {
214 /* skip the LIST-FRAM tag and get the next fourcc */
215 get_be32(pb);
216 fourcc_tag = get_be32(pb);
217 size = get_le32(pb);
218 }
219
220 if (url_feof(pb))
221 return -EIO;
222
223 switch (fourcc_tag) {
224
225 case ifrm_TAG:
4a106616
MN
226printf (" %cfrm chunk\n", (char)(fourcc_tag >> 24) & 0xFF);
227url_fseek(pb, size, SEEK_CUR);
228 break;
cef4ba9e 229 case pfrm_TAG:
cef4ba9e
MM
230printf (" %cfrm chunk\n", (char)(fourcc_tag >> 24) & 0xFF);
231url_fseek(pb, size, SEEK_CUR);
232 break;
4a106616 233 case cfrm_TAG:{
948f01ff 234int unknown= get_le32(pb);
4a106616
MN
235int id= get_le32(pb);
236int whole= get_le32(pb);
237static int stats[1000];
238stats[id] += size - 12;
239printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
240url_fseek(pb, size-12, SEEK_CUR);
241 break;
242 }
cef4ba9e
MM
243 case snd__TAG:
244printf (" snd_ chunk, ");
245 track_number = get_le32(pb);
246 size = get_le32(pb);
247 if (track_number == fourxm->selected_track) {
248printf ("correct track, dispatching...\n");
249 if (av_new_packet(pkt, size))
250 return -EIO;
251 ret = get_buffer(&s->pb, pkt->data, size);
252 if (ret < 0)
253 av_free_packet(pkt);
254 packet_read = 1;
255 } else {
256printf ("wrong track, skipping...\n");
257 url_fseek(pb, size, SEEK_CUR);
258 }
259 break;
260
261 default:
262 url_fseek(pb, size, SEEK_CUR);
263 break;
264 }
265 }
266
267 return ret;
268}
269
270static int fourxm_read_close(AVFormatContext *s)
271{
272 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
273
274 av_free(fourxm->tracks);
275
276 return 0;
277}
278
279static AVInputFormat fourxm_iformat = {
280 "4xm",
281 "4X Technologies format",
282 sizeof(FourxmDemuxContext),
283 fourxm_probe,
284 fourxm_read_header,
285 fourxm_read_packet,
286 fourxm_read_close,
287};
288
289int fourxm_init(void)
290{
291 av_register_input_format(&fourxm_iformat);
292 return 0;
293}