annotate 4xm.c @ 155:78a13c7b7d49 libavformat

pal vs ntsc fix by stoyan k <stoyan@i-space.org>
author al3x
date Fri, 20 Jun 2003 15:30:14 +0000
parents 4f3960430e54
children bca5abd97e43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
1 /*
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
2 * 4X Technologies .4xm File Demuxer (no muxer)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
3 * Copyright (c) 2003 The ffmpeg Project
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
4 *
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
9 *
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
13 * Lesser General Public License for more details.
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
14 *
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
18 */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
19
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
20 /**
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
21 * @file 4xm.c
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
22 * 4X Technologies file demuxer
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
23 * by Mike Melanson (melanson@pcisys.net)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
24 * for more information on the .4xm file format, visit:
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
25 * http://www.pcisys.net/~melanson/codecs/
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
26 */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
27
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
28 #include "avformat.h"
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
29
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
30 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
31 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
32 (((uint8_t*)(x))[2] << 16) | \
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
33 (((uint8_t*)(x))[1] << 8) | \
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
34 ((uint8_t*)(x))[0])
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
35
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
36 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
37 ( (long)(unsigned char)(ch0) | \
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
38 ( (long)(unsigned char)(ch1) << 8 ) | \
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
39 ( (long)(unsigned char)(ch2) << 16 ) | \
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
40 ( (long)(unsigned char)(ch3) << 24 ) )
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
41
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
42 #define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
43 #define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
44 #define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
45 #define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
46 #define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
47 #define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
48 #define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
49 #define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
50 #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
51 #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
52 #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
53 #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
54 #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
55 #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
56 #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
57 #define _TAG FOURCC_TAG('', '', '', '')
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
58
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
59 #define vtrk_SIZE 0x44
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
60 #define strk_SIZE 0x28
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
61
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
62 #define GET_LIST_HEADER() \
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
63 fourcc_tag = get_le32(pb); \
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
64 size = get_le32(pb); \
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
65 if (fourcc_tag != LIST_TAG) \
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
66 return AVERROR_INVALIDDATA; \
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
67 fourcc_tag = get_le32(pb);
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
68
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
69 typedef struct AudioTrack {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
70 int sample_rate;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
71 int bits;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
72 int channels;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
73 int stream_index;
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
74 int adpcm;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
75 } AudioTrack;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
76
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
77 typedef struct FourxmDemuxContext {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
78 int width;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
79 int height;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
80 int video_stream_index;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
81 int track_count;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
82 AudioTrack *tracks;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
83 int selected_track;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
84
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
85 int64_t pts;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
86 int last_chunk_was_audio;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
87 int last_audio_frame_count;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
88 } FourxmDemuxContext;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
89
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
90 static int fourxm_probe(AVProbeData *p)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
91 {
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
92 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
93 (LE_32(&p->buf[8]) != _4XMV_TAG))
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
94 return 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
95
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
96 return AVPROBE_SCORE_MAX;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
97 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
98
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
99 static int fourxm_read_header(AVFormatContext *s,
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
100 AVFormatParameters *ap)
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
101 {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
102 ByteIOContext *pb = &s->pb;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
103 unsigned int fourcc_tag;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
104 unsigned int size;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
105 int header_size;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
106 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
107 unsigned char *header;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
108 int i;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
109 int current_track = -1;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
110 AVStream *st;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
111
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
112 fourxm->track_count = 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
113 fourxm->tracks = NULL;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
114 fourxm->selected_track = 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
115
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
116 /* skip the first 3 32-bit numbers */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
117 url_fseek(pb, 12, SEEK_CUR);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
118
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
119 /* check for LIST-HEAD */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
120 GET_LIST_HEADER();
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
121 header_size = size - 4;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
122 if (fourcc_tag != HEAD_TAG)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
123 return AVERROR_INVALIDDATA;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
124
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
125 /* allocate space for the header and load the whole thing */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
126 header = av_malloc(header_size);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
127 if (!header)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
128 return AVERROR_NOMEM;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
129 if (get_buffer(pb, header, header_size) != header_size)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
130 return AVERROR_IO;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
131
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
132 /* take the lazy approach and search for any and all vtrk and strk chunks */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
133 for (i = 0; i < header_size - 8; i++) {
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
134 fourcc_tag = LE_32(&header[i]);
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
135 size = LE_32(&header[i + 4]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
136
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
137 if (fourcc_tag == vtrk_TAG) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
138 /* check that there is enough data */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
139 if (size != vtrk_SIZE) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
140 av_free(header);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
141 return AVERROR_INVALIDDATA;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
142 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
143 fourxm->width = LE_32(&header[i + 36]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
144 fourxm->height = LE_32(&header[i + 40]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
145 i += 8 + size;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
146
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
147 /* allocate a new AVStream */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
148 st = av_new_stream(s, 0);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
149 if (!st)
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
150 return AVERROR_NOMEM;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
151
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
152 fourxm->video_stream_index = st->index;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
153
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
154 st->codec.codec_type = CODEC_TYPE_VIDEO;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
155 st->codec.codec_id = CODEC_ID_4XM;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
156 st->codec.codec_tag = 0; /* no fourcc */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
157 st->codec.width = fourxm->width;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
158 st->codec.height = fourxm->height;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
159
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
160 } else if (fourcc_tag == strk_TAG) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
161 /* check that there is enough data */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
162 if (size != strk_SIZE) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
163 av_free(header);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
164 return AVERROR_INVALIDDATA;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
165 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
166 current_track = LE_32(&header[i + 8]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
167 if (current_track + 1 > fourxm->track_count) {
141
d2d2ec541148 allocate enough bytes
tmmm
parents: 140
diff changeset
168 fourxm->track_count = current_track + 1;
d2d2ec541148 allocate enough bytes
tmmm
parents: 140
diff changeset
169 fourxm->tracks = av_realloc(fourxm->tracks,
d2d2ec541148 allocate enough bytes
tmmm
parents: 140
diff changeset
170 fourxm->track_count * sizeof(AudioTrack));
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
171 if (!fourxm->tracks) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
172 av_free(header);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
173 return AVERROR_NOMEM;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
174 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
175 }
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
176 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
177 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
178 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
179 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
180 printf("bps:%d\n", fourxm->tracks[current_track].bits);
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
181 i += 8 + size;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
182
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
183 /* allocate a new AVStream */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
184 st = av_new_stream(s, current_track);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
185 if (!st)
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
186 return AVERROR_NOMEM;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
187
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
188 fourxm->tracks[current_track].stream_index = st->index;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
189
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
190 st->codec.codec_type = CODEC_TYPE_AUDIO;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
191 st->codec.codec_tag = 1;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
192 st->codec.channels = fourxm->tracks[current_track].channels;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
193 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
194 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
195 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
196 st->codec.bits_per_sample;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
197 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
198 if (fourxm->tracks[current_track].adpcm)
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
199 st->codec.codec_id = CODEC_ID_ADPCM_4XM;
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
200 else if (st->codec.bits_per_sample == 8)
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
201 st->codec.codec_id = CODEC_ID_PCM_U8;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
202 else
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
203 st->codec.codec_id = CODEC_ID_PCM_S16LE;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
204 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
205 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
206
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
207 av_free(header);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
208
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
209 /* skip over the LIST-MOVI chunk (which is where the stream should be */
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
210 GET_LIST_HEADER();
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
211 if (fourcc_tag != MOVI_TAG)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
212 return AVERROR_INVALIDDATA;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
213
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
214 /* initialize context members */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
215 fourxm->pts = 0;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
216 fourxm->last_chunk_was_audio = 0;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
217 fourxm->last_audio_frame_count = 0;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
218
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
219 /* set the pts reference (1 pts = 1/90000) */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
220 s->pts_num = 1;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
221 s->pts_den = 90000;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
222
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
223 return 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
224 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
225
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
226 static int fourxm_read_packet(AVFormatContext *s,
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
227 AVPacket *pkt)
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
228 {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
229 FourxmDemuxContext *fourxm = s->priv_data;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
230 ByteIOContext *pb = &s->pb;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
231 unsigned int fourcc_tag;
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
232 unsigned int size, out_size;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
233 int ret = 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
234 int track_number;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
235 int packet_read = 0;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
236 unsigned char header[8];
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
237 int64_t pts_inc;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
238
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
239 while (!packet_read) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
240
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
241 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
242 return ret;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
243 fourcc_tag = LE_32(&header[0]);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
244 size = LE_32(&header[4]);
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
245 //printf(" %8X %c%c%c%c %d\n", fourcc_tag, fourcc_tag, fourcc_tag>>8, fourcc_tag>>16, fourcc_tag>>24, size);
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
246 if (url_feof(pb))
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
247 return -EIO;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
248 switch (fourcc_tag) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
249
144
9e73bf732800 get the video dispatch straight
tmmm
parents: 143
diff changeset
250 case LIST_TAG:
9e73bf732800 get the video dispatch straight
tmmm
parents: 143
diff changeset
251 /* skip the LIST-* tag and move on to the next fourcc */
9e73bf732800 get the video dispatch straight
tmmm
parents: 143
diff changeset
252 get_le32(pb);
9e73bf732800 get the video dispatch straight
tmmm
parents: 143
diff changeset
253 break;
9e73bf732800 get the video dispatch straight
tmmm
parents: 143
diff changeset
254
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
255 case ifrm_TAG:
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
256 case pfrm_TAG:
139
d5ac5007713c c frame size debug stuff
michaelni
parents: 137
diff changeset
257 case cfrm_TAG:{
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
258
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
259 int id, whole;
139
d5ac5007713c c frame size debug stuff
michaelni
parents: 137
diff changeset
260 static int stats[1000];
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
261
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
262 /* bump the pts if this last data sent out was audio */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
263 if (fourxm->last_chunk_was_audio) {
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
264 fourxm->last_chunk_was_audio = 0;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
265 pts_inc = fourxm->last_audio_frame_count;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
266 pts_inc *= 90000;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
267 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
268 fourxm->pts += pts_inc;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
269 }
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
270
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
271 /* allocate 8 more bytes than 'size' to account for fourcc
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
272 * and size */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
273 if (av_new_packet(pkt, size + 8))
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
274 return -EIO;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
275 pkt->stream_index = fourxm->video_stream_index;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
276 pkt->pts = fourxm->pts;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
277 memcpy(pkt->data, header, 8);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
278 ret = get_buffer(&s->pb, &pkt->data[8], size);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
279
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
280 if (fourcc_tag == cfrm_TAG) {
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
281 id = LE_32(&pkt->data[12]);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
282 whole = LE_32(&pkt->data[16]);
139
d5ac5007713c c frame size debug stuff
michaelni
parents: 137
diff changeset
283 stats[id] += size - 12;
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
284 //printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
285 }
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
286
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
287 if (ret < 0)
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
288 av_free_packet(pkt);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
289 else
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
290 packet_read = 1;
139
d5ac5007713c c frame size debug stuff
michaelni
parents: 137
diff changeset
291 break;
d5ac5007713c c frame size debug stuff
michaelni
parents: 137
diff changeset
292 }
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
293
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
294 case snd__TAG:
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
295 printf (" snd_ chunk, ");
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
296 track_number = get_le32(pb);
145
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
297 out_size= get_le32(pb);
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
298 size-=8;
4f3960430e54 4xm adpcm
michaelni
parents: 144
diff changeset
299
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
300 if (track_number == fourxm->selected_track) {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
301 printf ("correct track, dispatching...\n");
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
302 if (av_new_packet(pkt, size))
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
303 return -EIO;
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
304 pkt->stream_index =
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
305 fourxm->tracks[fourxm->selected_track].stream_index;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
306 pkt->pts = fourxm->pts;
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
307 ret = get_buffer(&s->pb, pkt->data, size);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
308 if (ret < 0)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
309 av_free_packet(pkt);
143
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
310 else
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
311 packet_read = 1;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
312
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
313 /* maintain pts info for the benefit of the video track */
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
314 fourxm->last_chunk_was_audio = 1;
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
315 fourxm->last_audio_frame_count = size /
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
316 ((fourxm->tracks[fourxm->selected_track].bits / 8) *
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
317 fourxm->tracks[fourxm->selected_track].channels);
76dd1e93b064 dispatch video as well as audio
tmmm
parents: 141
diff changeset
318
137
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
319 } else {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
320 printf ("wrong track, skipping...\n");
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
321 url_fseek(pb, size, SEEK_CUR);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
322 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
323 break;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
324
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
325 default:
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
326 url_fseek(pb, size, SEEK_CUR);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
327 break;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
328 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
329 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
330 return ret;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
331 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
332
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
333 static int fourxm_read_close(AVFormatContext *s)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
334 {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
335 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
336
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
337 av_free(fourxm->tracks);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
338
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
339 return 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
340 }
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
341
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
342 static AVInputFormat fourxm_iformat = {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
343 "4xm",
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
344 "4X Technologies format",
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
345 sizeof(FourxmDemuxContext),
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
346 fourxm_probe,
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
347 fourxm_read_header,
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
348 fourxm_read_packet,
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
349 fourxm_read_close,
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
350 };
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
351
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
352 int fourxm_init(void)
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
353 {
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
354 av_register_input_format(&fourxm_iformat);
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
355 return 0;
857a026b7e93 first pass at an experimental 4xm demuxer
tmmm
parents:
diff changeset
356 }