Mercurial > libavformat.hg
annotate 4xm.c @ 1346:3637b5c4e882 libavformat
add support for some MS DIB formats for avisynth
Patch by Steve Lhomme % slhomme AH divxcorp P com %
Original thread:
Date: Sep 27, 2006 7:20 PM
Subject: [Ffmpeg-devel] [PATCH] AVISynth support for some MS DIB formats
author | gpoirier |
---|---|
date | Sun, 01 Oct 2006 20:41:02 +0000 |
parents | d18cc9a1fd02 |
children | 0899bfe4105c |
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 | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
137 | 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 | |
386
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
30 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
31 #define _4XMV_TAG MKTAG('4', 'X', 'M', 'V') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
32 #define LIST_TAG MKTAG('L', 'I', 'S', 'T') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
33 #define HEAD_TAG MKTAG('H', 'E', 'A', 'D') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
34 #define TRK__TAG MKTAG('T', 'R', 'K', '_') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
35 #define MOVI_TAG MKTAG('M', 'O', 'V', 'I') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
36 #define VTRK_TAG MKTAG('V', 'T', 'R', 'K') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
37 #define STRK_TAG MKTAG('S', 'T', 'R', 'K') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
38 #define std__TAG MKTAG('s', 't', 'd', '_') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
39 #define name_TAG MKTAG('n', 'a', 'm', 'e') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
40 #define vtrk_TAG MKTAG('v', 't', 'r', 'k') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
41 #define strk_TAG MKTAG('s', 't', 'r', 'k') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
42 #define ifrm_TAG MKTAG('i', 'f', 'r', 'm') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
43 #define pfrm_TAG MKTAG('p', 'f', 'r', 'm') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
44 #define cfrm_TAG MKTAG('c', 'f', 'r', 'm') |
c152849ee643
remove numerous definitions of BE_*/LE_* macros; convert FOURCC_TAG ->
melanson
parents:
317
diff
changeset
|
45 #define snd__TAG MKTAG('s', 'n', 'd', '_') |
137 | 46 |
47 #define vtrk_SIZE 0x44 | |
48 #define strk_SIZE 0x28 | |
49 | |
50 #define GET_LIST_HEADER() \ | |
143 | 51 fourcc_tag = get_le32(pb); \ |
137 | 52 size = get_le32(pb); \ |
53 if (fourcc_tag != LIST_TAG) \ | |
54 return AVERROR_INVALIDDATA; \ | |
143 | 55 fourcc_tag = get_le32(pb); |
137 | 56 |
57 typedef struct AudioTrack { | |
58 int sample_rate; | |
59 int bits; | |
60 int channels; | |
143 | 61 int stream_index; |
145 | 62 int adpcm; |
137 | 63 } AudioTrack; |
64 | |
65 typedef struct FourxmDemuxContext { | |
66 int width; | |
67 int height; | |
143 | 68 int video_stream_index; |
137 | 69 int track_count; |
70 AudioTrack *tracks; | |
71 int selected_track; | |
143 | 72 |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
73 int64_t audio_pts; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
74 int64_t video_pts; |
317 | 75 float fps; |
137 | 76 } FourxmDemuxContext; |
77 | |
78 static int fourxm_probe(AVProbeData *p) | |
79 { | |
254
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
80 if (p->buf_size < 12) |
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
81 return 0; |
bca5abd97e43
clean up 4xm demuxer; make valgrind just a little happier
tmmm
parents:
145
diff
changeset
|
82 |
143 | 83 if ((LE_32(&p->buf[0]) != RIFF_TAG) || |
84 (LE_32(&p->buf[8]) != _4XMV_TAG)) | |
137 | 85 return 0; |
86 | |
87 return AVPROBE_SCORE_MAX; | |
88 } | |
89 | |
90 static int fourxm_read_header(AVFormatContext *s, | |
143 | 91 AVFormatParameters *ap) |
137 | 92 { |
93 ByteIOContext *pb = &s->pb; | |
94 unsigned int fourcc_tag; | |
95 unsigned int size; | |
96 int header_size; | |
97 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data; | |
98 unsigned char *header; | |
99 int i; | |
100 int current_track = -1; | |
101 AVStream *st; | |
102 | |
103 fourxm->track_count = 0; | |
104 fourxm->tracks = NULL; | |
105 fourxm->selected_track = 0; | |
317 | 106 fourxm->fps = 1.0; |
137 | 107 |
108 /* skip the first 3 32-bit numbers */ | |
109 url_fseek(pb, 12, SEEK_CUR); | |
110 | |
111 /* check for LIST-HEAD */ | |
112 GET_LIST_HEADER(); | |
113 header_size = size - 4; | |
114 if (fourcc_tag != HEAD_TAG) | |
115 return AVERROR_INVALIDDATA; | |
116 | |
117 /* allocate space for the header and load the whole thing */ | |
118 header = av_malloc(header_size); | |
119 if (!header) | |
120 return AVERROR_NOMEM; | |
121 if (get_buffer(pb, header, header_size) != header_size) | |
122 return AVERROR_IO; | |
123 | |
124 /* take the lazy approach and search for any and all vtrk and strk chunks */ | |
125 for (i = 0; i < header_size - 8; i++) { | |
143 | 126 fourcc_tag = LE_32(&header[i]); |
137 | 127 size = LE_32(&header[i + 4]); |
128 | |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
129 if (fourcc_tag == std__TAG) { |
824 | 130 fourxm->fps = av_int2flt(LE_32(&header[i + 12])); |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
131 } else if (fourcc_tag == vtrk_TAG) { |
137 | 132 /* check that there is enough data */ |
133 if (size != vtrk_SIZE) { | |
134 av_free(header); | |
135 return AVERROR_INVALIDDATA; | |
136 } | |
137 fourxm->width = LE_32(&header[i + 36]); | |
138 fourxm->height = LE_32(&header[i + 40]); | |
139 i += 8 + size; | |
143 | 140 |
141 /* allocate a new AVStream */ | |
142 st = av_new_stream(s, 0); | |
143 if (!st) | |
144 return AVERROR_NOMEM; | |
740 | 145 av_set_pts_info(st, 60, 1, fourxm->fps); |
143 | 146 |
147 fourxm->video_stream_index = st->index; | |
148 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
149 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
150 st->codec->codec_id = CODEC_ID_4XM; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
151 st->codec->codec_tag = 0; /* no fourcc */ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
152 st->codec->width = fourxm->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
153 st->codec->height = fourxm->height; |
143 | 154 |
137 | 155 } else if (fourcc_tag == strk_TAG) { |
156 /* check that there is enough data */ | |
157 if (size != strk_SIZE) { | |
158 av_free(header); | |
159 return AVERROR_INVALIDDATA; | |
160 } | |
161 current_track = LE_32(&header[i + 8]); | |
162 if (current_track + 1 > fourxm->track_count) { | |
141 | 163 fourxm->track_count = current_track + 1; |
639 | 164 if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack)) |
165 return -1; | |
885 | 166 fourxm->tracks = av_realloc(fourxm->tracks, |
141 | 167 fourxm->track_count * sizeof(AudioTrack)); |
137 | 168 if (!fourxm->tracks) { |
169 av_free(header); | |
170 return AVERROR_NOMEM; | |
171 } | |
172 } | |
145 | 173 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]); |
137 | 174 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]); |
175 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]); | |
176 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]); | |
177 i += 8 + size; | |
143 | 178 |
179 /* allocate a new AVStream */ | |
180 st = av_new_stream(s, current_track); | |
181 if (!st) | |
182 return AVERROR_NOMEM; | |
183 | |
740 | 184 av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate); |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
386
diff
changeset
|
185 |
143 | 186 fourxm->tracks[current_track].stream_index = st->index; |
187 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
188 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
189 st->codec->codec_tag = 1; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
190 st->codec->channels = fourxm->tracks[current_track].channels; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
191 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
192 st->codec->bits_per_sample = fourxm->tracks[current_track].bits; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
193 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
194 st->codec->bits_per_sample; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
195 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample; |
145 | 196 if (fourxm->tracks[current_track].adpcm) |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
197 st->codec->codec_id = CODEC_ID_ADPCM_4XM; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
198 else if (st->codec->bits_per_sample == 8) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
199 st->codec->codec_id = CODEC_ID_PCM_U8; |
143 | 200 else |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
201 st->codec->codec_id = CODEC_ID_PCM_S16LE; |
137 | 202 } |
203 } | |
204 | |
205 av_free(header); | |
206 | |
207 /* skip over the LIST-MOVI chunk (which is where the stream should be */ | |
208 GET_LIST_HEADER(); | |
209 if (fourcc_tag != MOVI_TAG) | |
210 return AVERROR_INVALIDDATA; | |
211 | |
143 | 212 /* initialize context members */ |
740 | 213 fourxm->video_pts = -1; /* first frame will push to 0 */ |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
214 fourxm->audio_pts = 0; |
137 | 215 |
216 return 0; | |
217 } | |
218 | |
219 static int fourxm_read_packet(AVFormatContext *s, | |
143 | 220 AVPacket *pkt) |
137 | 221 { |
222 FourxmDemuxContext *fourxm = s->priv_data; | |
223 ByteIOContext *pb = &s->pb; | |
224 unsigned int fourcc_tag; | |
145 | 225 unsigned int size, out_size; |
137 | 226 int ret = 0; |
227 int track_number; | |
228 int packet_read = 0; | |
143 | 229 unsigned char header[8]; |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
230 int audio_frame_count; |
137 | 231 |
232 while (!packet_read) { | |
233 | |
143 | 234 if ((ret = get_buffer(&s->pb, header, 8)) < 0) |
235 return ret; | |
236 fourcc_tag = LE_32(&header[0]); | |
237 size = LE_32(&header[4]); | |
137 | 238 if (url_feof(pb)) |
482 | 239 return AVERROR_IO; |
137 | 240 switch (fourcc_tag) { |
241 | |
144 | 242 case LIST_TAG: |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
243 /* this is a good time to bump the video pts */ |
740 | 244 fourxm->video_pts ++; |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
245 |
144 | 246 /* skip the LIST-* tag and move on to the next fourcc */ |
247 get_le32(pb); | |
248 break; | |
249 | |
137 | 250 case ifrm_TAG: |
251 case pfrm_TAG: | |
139 | 252 case cfrm_TAG:{ |
143 | 253 |
254 /* allocate 8 more bytes than 'size' to account for fourcc | |
255 * and size */ | |
643 | 256 if (size + 8 < size || av_new_packet(pkt, size + 8)) |
482 | 257 return AVERROR_IO; |
143 | 258 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
|
259 pkt->pts = fourxm->video_pts; |
775 | 260 pkt->pos = url_ftell(&s->pb); |
143 | 261 memcpy(pkt->data, header, 8); |
262 ret = get_buffer(&s->pb, &pkt->data[8], size); | |
263 | |
264 if (ret < 0) | |
265 av_free_packet(pkt); | |
266 else | |
267 packet_read = 1; | |
139 | 268 break; |
269 } | |
143 | 270 |
137 | 271 case snd__TAG: |
272 track_number = get_le32(pb); | |
145 | 273 out_size= get_le32(pb); |
274 size-=8; | |
275 | |
137 | 276 if (track_number == fourxm->selected_track) { |
775 | 277 ret= av_get_packet(&s->pb, pkt, size); |
278 if(ret<0) | |
482 | 279 return AVERROR_IO; |
885 | 280 pkt->stream_index = |
143 | 281 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
|
282 pkt->pts = fourxm->audio_pts; |
775 | 283 packet_read = 1; |
143 | 284 |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
285 /* pts accounting */ |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
286 audio_frame_count = size; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
287 if (fourxm->tracks[fourxm->selected_track].adpcm) |
885 | 288 audio_frame_count -= |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
289 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
|
290 audio_frame_count /= |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
291 fourxm->tracks[fourxm->selected_track].channels; |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
292 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
|
293 audio_frame_count *= 2; |
885 | 294 else |
316
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
295 audio_frame_count /= |
9aa23c6d396e
use the proper file framerate (specified by a float); account the pts
melanson
parents:
254
diff
changeset
|
296 (fourxm->tracks[fourxm->selected_track].bits / 8); |
740 | 297 fourxm->audio_pts += audio_frame_count; |
143 | 298 |
137 | 299 } else { |
300 url_fseek(pb, size, SEEK_CUR); | |
301 } | |
302 break; | |
303 | |
304 default: | |
305 url_fseek(pb, size, SEEK_CUR); | |
306 break; | |
307 } | |
308 } | |
309 return ret; | |
310 } | |
311 | |
312 static int fourxm_read_close(AVFormatContext *s) | |
313 { | |
314 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data; | |
315 | |
316 av_free(fourxm->tracks); | |
317 | |
318 return 0; | |
319 } | |
320 | |
1169 | 321 AVInputFormat fourxm_demuxer = { |
137 | 322 "4xm", |
323 "4X Technologies format", | |
324 sizeof(FourxmDemuxContext), | |
325 fourxm_probe, | |
326 fourxm_read_header, | |
327 fourxm_read_packet, | |
328 fourxm_read_close, | |
329 }; |