Mercurial > libavformat.hg
annotate 4xm.c @ 362:07bf7ff87eb3 libavformat
* Initial implementation of the G.726 ADPCM audio codec.
author | romansh |
---|---|
date | Sat, 07 Feb 2004 08:20:00 +0000 |
parents | 5cd41ae1debf |
children | c152849ee643 |
rev | line source |
---|---|
137 | 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 | |
36 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \ | |
143 | 37 ( (long)(unsigned char)(ch0) | \ |
38 ( (long)(unsigned char)(ch1) << 8 ) | \ | |
39 ( (long)(unsigned char)(ch2) << 16 ) | \ | |
40 ( (long)(unsigned char)(ch3) << 24 ) ) | |
137 | 41 |
42 #define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F') | |
43 #define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V') | |
44 #define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T') | |
45 #define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D') | |
46 #define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_') | |
47 #define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I') | |
48 #define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K') | |
49 #define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K') | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
50 #define std__TAG FOURCC_TAG('s', 't', 'd', '_') |
137 | 51 #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e') |
52 #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k') | |
53 #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k') | |
54 #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm') | |
55 #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm') | |
56 #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm') | |
57 #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_') | |
58 #define _TAG FOURCC_TAG('', '', '', '') | |
59 | |
60 #define vtrk_SIZE 0x44 | |
61 #define strk_SIZE 0x28 | |
62 | |
63 #define GET_LIST_HEADER() \ | |
143 | 64 fourcc_tag = get_le32(pb); \ |
137 | 65 size = get_le32(pb); \ |
66 if (fourcc_tag != LIST_TAG) \ | |
67 return AVERROR_INVALIDDATA; \ | |
143 | 68 fourcc_tag = get_le32(pb); |
137 | 69 |
70 typedef struct AudioTrack { | |
71 int sample_rate; | |
72 int bits; | |
73 int channels; | |
143 | 74 int stream_index; |
145 | 75 int adpcm; |
137 | 76 } AudioTrack; |
77 | |
78 typedef struct FourxmDemuxContext { | |
79 int width; | |
80 int height; | |
143 | 81 int video_stream_index; |
137 | 82 int track_count; |
83 AudioTrack *tracks; | |
84 int selected_track; | |
143 | 85 |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
86 int64_t audio_pts; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
87 int64_t video_pts; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
88 int video_pts_inc; |
317 | 89 float fps; |
137 | 90 } FourxmDemuxContext; |
91 | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
92 static float get_le_float(unsigned char *buffer) |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
93 { |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
94 float f; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
95 unsigned char *float_buffer = (unsigned char *)&f; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
96 |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
97 #ifdef WORDS_BIGENDIAN |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
98 float_buffer[0] = buffer[3]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
99 float_buffer[1] = buffer[2]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
100 float_buffer[2] = buffer[1]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
101 float_buffer[3] = buffer[0]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
102 #else |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
103 float_buffer[0] = buffer[0]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
104 float_buffer[1] = buffer[1]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
105 float_buffer[2] = buffer[2]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
106 float_buffer[3] = buffer[3]; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
107 #endif |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
108 |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
109 return f; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
110 } |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
111 |
137 | 112 static int fourxm_probe(AVProbeData *p) |
113 { | |
254
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
114 if (p->buf_size < 12) |
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
115 return 0; |
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
116 |
143 | 117 if ((LE_32(&p->buf[0]) != RIFF_TAG) || |
118 (LE_32(&p->buf[8]) != _4XMV_TAG)) | |
137 | 119 return 0; |
120 | |
121 return AVPROBE_SCORE_MAX; | |
122 } | |
123 | |
124 static int fourxm_read_header(AVFormatContext *s, | |
143 | 125 AVFormatParameters *ap) |
137 | 126 { |
127 ByteIOContext *pb = &s->pb; | |
128 unsigned int fourcc_tag; | |
129 unsigned int size; | |
130 int header_size; | |
131 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data; | |
132 unsigned char *header; | |
133 int i; | |
134 int current_track = -1; | |
135 AVStream *st; | |
136 | |
137 fourxm->track_count = 0; | |
138 fourxm->tracks = NULL; | |
139 fourxm->selected_track = 0; | |
317 | 140 fourxm->fps = 1.0; |
137 | 141 |
142 /* skip the first 3 32-bit numbers */ | |
143 url_fseek(pb, 12, SEEK_CUR); | |
144 | |
145 /* check for LIST-HEAD */ | |
146 GET_LIST_HEADER(); | |
147 header_size = size - 4; | |
148 if (fourcc_tag != HEAD_TAG) | |
149 return AVERROR_INVALIDDATA; | |
150 | |
151 /* allocate space for the header and load the whole thing */ | |
152 header = av_malloc(header_size); | |
153 if (!header) | |
154 return AVERROR_NOMEM; | |
155 if (get_buffer(pb, header, header_size) != header_size) | |
156 return AVERROR_IO; | |
157 | |
158 /* take the lazy approach and search for any and all vtrk and strk chunks */ | |
159 for (i = 0; i < header_size - 8; i++) { | |
143 | 160 fourcc_tag = LE_32(&header[i]); |
137 | 161 size = LE_32(&header[i + 4]); |
162 | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
163 if (fourcc_tag == std__TAG) { |
317 | 164 fourxm->fps = get_le_float(&header[i + 12]); |
165 fourxm->video_pts_inc = (int)(90000.0 / fourxm->fps); | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
166 } else if (fourcc_tag == vtrk_TAG) { |
137 | 167 /* check that there is enough data */ |
168 if (size != vtrk_SIZE) { | |
169 av_free(header); | |
170 return AVERROR_INVALIDDATA; | |
171 } | |
172 fourxm->width = LE_32(&header[i + 36]); | |
173 fourxm->height = LE_32(&header[i + 40]); | |
174 i += 8 + size; | |
143 | 175 |
176 /* allocate a new AVStream */ | |
177 st = av_new_stream(s, 0); | |
178 if (!st) | |
179 return AVERROR_NOMEM; | |
180 | |
181 fourxm->video_stream_index = st->index; | |
182 | |
317 | 183 st->codec.frame_rate = fourxm->fps; |
184 st->codec.frame_rate_base = 1.0; | |
143 | 185 st->codec.codec_type = CODEC_TYPE_VIDEO; |
186 st->codec.codec_id = CODEC_ID_4XM; | |
187 st->codec.codec_tag = 0; /* no fourcc */ | |
188 st->codec.width = fourxm->width; | |
189 st->codec.height = fourxm->height; | |
190 | |
137 | 191 } else if (fourcc_tag == strk_TAG) { |
192 /* check that there is enough data */ | |
193 if (size != strk_SIZE) { | |
194 av_free(header); | |
195 return AVERROR_INVALIDDATA; | |
196 } | |
197 current_track = LE_32(&header[i + 8]); | |
198 if (current_track + 1 > fourxm->track_count) { | |
141 | 199 fourxm->track_count = current_track + 1; |
200 fourxm->tracks = av_realloc(fourxm->tracks, | |
201 fourxm->track_count * sizeof(AudioTrack)); | |
137 | 202 if (!fourxm->tracks) { |
203 av_free(header); | |
204 return AVERROR_NOMEM; | |
205 } | |
206 } | |
145 | 207 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]); |
137 | 208 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]); |
209 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]); | |
210 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]); | |
211 i += 8 + size; | |
143 | 212 |
213 /* allocate a new AVStream */ | |
214 st = av_new_stream(s, current_track); | |
215 if (!st) | |
216 return AVERROR_NOMEM; | |
217 | |
218 fourxm->tracks[current_track].stream_index = st->index; | |
219 | |
220 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
221 st->codec.codec_tag = 1; | |
222 st->codec.channels = fourxm->tracks[current_track].channels; | |
223 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate; | |
224 st->codec.bits_per_sample = fourxm->tracks[current_track].bits; | |
225 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * | |
226 st->codec.bits_per_sample; | |
227 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample; | |
145 | 228 if (fourxm->tracks[current_track].adpcm) |
229 st->codec.codec_id = CODEC_ID_ADPCM_4XM; | |
230 else if (st->codec.bits_per_sample == 8) | |
143 | 231 st->codec.codec_id = CODEC_ID_PCM_U8; |
232 else | |
233 st->codec.codec_id = CODEC_ID_PCM_S16LE; | |
137 | 234 } |
235 } | |
236 | |
237 av_free(header); | |
238 | |
239 /* skip over the LIST-MOVI chunk (which is where the stream should be */ | |
240 GET_LIST_HEADER(); | |
241 if (fourcc_tag != MOVI_TAG) | |
242 return AVERROR_INVALIDDATA; | |
243 | |
143 | 244 /* initialize context members */ |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
245 fourxm->video_pts = -fourxm->video_pts_inc; /* first frame will push to 0 */ |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
246 fourxm->audio_pts = 0; |
137 | 247 |
143 | 248 /* set the pts reference (1 pts = 1/90000) */ |
249 s->pts_num = 1; | |
250 s->pts_den = 90000; | |
137 | 251 |
252 return 0; | |
253 } | |
254 | |
255 static int fourxm_read_packet(AVFormatContext *s, | |
143 | 256 AVPacket *pkt) |
137 | 257 { |
258 FourxmDemuxContext *fourxm = s->priv_data; | |
259 ByteIOContext *pb = &s->pb; | |
260 unsigned int fourcc_tag; | |
145 | 261 unsigned int size, out_size; |
137 | 262 int ret = 0; |
263 int track_number; | |
264 int packet_read = 0; | |
143 | 265 unsigned char header[8]; |
266 int64_t pts_inc; | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
267 int audio_frame_count; |
137 | 268 |
269 while (!packet_read) { | |
270 | |
143 | 271 if ((ret = get_buffer(&s->pb, header, 8)) < 0) |
272 return ret; | |
273 fourcc_tag = LE_32(&header[0]); | |
274 size = LE_32(&header[4]); | |
137 | 275 if (url_feof(pb)) |
276 return -EIO; | |
277 switch (fourcc_tag) { | |
278 | |
144 | 279 case LIST_TAG: |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
280 /* this is a good time to bump the video pts */ |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
281 fourxm->video_pts += fourxm->video_pts_inc; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
282 |
144 | 283 /* skip the LIST-* tag and move on to the next fourcc */ |
284 get_le32(pb); | |
285 break; | |
286 | |
137 | 287 case ifrm_TAG: |
288 case pfrm_TAG: | |
139 | 289 case cfrm_TAG:{ |
143 | 290 |
291 /* allocate 8 more bytes than 'size' to account for fourcc | |
292 * and size */ | |
293 if (av_new_packet(pkt, size + 8)) | |
294 return -EIO; | |
295 pkt->stream_index = fourxm->video_stream_index; | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
296 pkt->pts = fourxm->video_pts; |
143 | 297 memcpy(pkt->data, header, 8); |
298 ret = get_buffer(&s->pb, &pkt->data[8], size); | |
299 | |
300 if (ret < 0) | |
301 av_free_packet(pkt); | |
302 else | |
303 packet_read = 1; | |
139 | 304 break; |
305 } | |
143 | 306 |
137 | 307 case snd__TAG: |
308 track_number = get_le32(pb); | |
145 | 309 out_size= get_le32(pb); |
310 size-=8; | |
311 | |
137 | 312 if (track_number == fourxm->selected_track) { |
313 if (av_new_packet(pkt, size)) | |
314 return -EIO; | |
143 | 315 pkt->stream_index = |
316 fourxm->tracks[fourxm->selected_track].stream_index; | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
317 pkt->pts = fourxm->audio_pts; |
137 | 318 ret = get_buffer(&s->pb, pkt->data, size); |
319 if (ret < 0) | |
320 av_free_packet(pkt); | |
143 | 321 else |
322 packet_read = 1; | |
323 | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
324 /* pts accounting */ |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
325 audio_frame_count = size; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
326 if (fourxm->tracks[fourxm->selected_track].adpcm) |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
327 audio_frame_count -= |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
328 2 * (fourxm->tracks[fourxm->selected_track].channels); |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
329 audio_frame_count /= |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
330 fourxm->tracks[fourxm->selected_track].channels; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
331 if (fourxm->tracks[fourxm->selected_track].adpcm) |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
332 audio_frame_count *= 2; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
333 else |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
334 audio_frame_count /= |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
335 (fourxm->tracks[fourxm->selected_track].bits / 8); |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
336 pts_inc = audio_frame_count; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
337 pts_inc *= 90000; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
338 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
339 fourxm->audio_pts += pts_inc; |
143 | 340 |
137 | 341 } else { |
342 url_fseek(pb, size, SEEK_CUR); | |
343 } | |
344 break; | |
345 | |
346 default: | |
347 url_fseek(pb, size, SEEK_CUR); | |
348 break; | |
349 } | |
350 } | |
351 return ret; | |
352 } | |
353 | |
354 static int fourxm_read_close(AVFormatContext *s) | |
355 { | |
356 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data; | |
357 | |
358 av_free(fourxm->tracks); | |
359 | |
360 return 0; | |
361 } | |
362 | |
363 static AVInputFormat fourxm_iformat = { | |
364 "4xm", | |
365 "4X Technologies format", | |
366 sizeof(FourxmDemuxContext), | |
367 fourxm_probe, | |
368 fourxm_read_header, | |
369 fourxm_read_packet, | |
370 fourxm_read_close, | |
371 }; | |
372 | |
373 int fourxm_init(void) | |
374 { | |
375 av_register_input_format(&fourxm_iformat); | |
376 return 0; | |
377 } |