Mercurial > libavformat.hg
annotate wav.c @ 1485:9000f9cdac49 libavformat
fix checksum check (was my bug not libnut)
author | michael |
---|---|
date | Sun, 12 Nov 2006 12:16:59 +0000 |
parents | 3b00fb8ef8e4 |
children | 2a85c82b8538 |
rev | line source |
---|---|
885 | 1 /* |
1415
3b00fb8ef8e4
replace coder/decoder file description in libavformat by muxer/demuxer
aurel
parents:
1358
diff
changeset
|
2 * WAV muxer and demuxer |
0 | 3 * Copyright (c) 2001, 2002 Fabrice Bellard. |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
893
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
1142
e3a585883bbd
Move initialisations and internal symbols in allformats.h,
gpoirier
parents:
1136
diff
changeset
|
22 #include "allformats.h" |
1172
6a5e58d2114b
move common stuff from avienc.c and wav.c to new file riff.c
mru
parents:
1169
diff
changeset
|
23 #include "riff.h" |
0 | 24 |
25 typedef struct { | |
26 offset_t data; | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
27 offset_t data_end; |
0 | 28 } WAVContext; |
29 | |
1149 | 30 #ifdef CONFIG_MUXERS |
0 | 31 static int wav_write_header(AVFormatContext *s) |
32 { | |
33 WAVContext *wav = s->priv_data; | |
34 ByteIOContext *pb = &s->pb; | |
35 offset_t fmt; | |
36 | |
37 put_tag(pb, "RIFF"); | |
38 put_le32(pb, 0); /* file length */ | |
39 put_tag(pb, "WAVE"); | |
40 | |
41 /* format header */ | |
42 fmt = start_tag(pb, "fmt "); | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
43 if (put_wav_header(pb, s->streams[0]->codec) < 0) { |
0 | 44 av_free(wav); |
45 return -1; | |
46 } | |
47 end_tag(pb, fmt); | |
48 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
49 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); |
645
9fc2d2cc4608
wav timestamp truncation fix by (Wolfram Gloger <wmglo dent.med.uni-muenchen de>)
michael
parents:
587
diff
changeset
|
50 |
0 | 51 /* data header */ |
52 wav->data = start_tag(pb, "data"); | |
885 | 53 |
0 | 54 put_flush_packet(pb); |
55 | |
56 return 0; | |
57 } | |
58 | |
468 | 59 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 60 { |
61 ByteIOContext *pb = &s->pb; | |
468 | 62 put_buffer(pb, pkt->data, pkt->size); |
0 | 63 return 0; |
64 } | |
65 | |
66 static int wav_write_trailer(AVFormatContext *s) | |
67 { | |
68 ByteIOContext *pb = &s->pb; | |
69 WAVContext *wav = s->priv_data; | |
70 offset_t file_size; | |
71 | |
72 if (!url_is_streamed(&s->pb)) { | |
73 end_tag(pb, wav->data); | |
74 | |
75 /* update file size */ | |
76 file_size = url_ftell(pb); | |
77 url_fseek(pb, 4, SEEK_SET); | |
65 | 78 put_le32(pb, (uint32_t)(file_size - 8)); |
0 | 79 url_fseek(pb, file_size, SEEK_SET); |
80 | |
81 put_flush_packet(pb); | |
82 } | |
83 return 0; | |
84 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
846
diff
changeset
|
85 #endif //CONFIG_MUXERS |
0 | 86 |
87 /* return the size of the found tag */ | |
88 /* XXX: > 2GB ? */ | |
65 | 89 static int find_tag(ByteIOContext *pb, uint32_t tag1) |
0 | 90 { |
91 unsigned int tag; | |
92 int size; | |
93 | |
94 for(;;) { | |
95 if (url_feof(pb)) | |
96 return -1; | |
97 tag = get_le32(pb); | |
98 size = get_le32(pb); | |
99 if (tag == tag1) | |
100 break; | |
101 url_fseek(pb, size, SEEK_CUR); | |
102 } | |
103 if (size < 0) | |
104 size = 0x7fffffff; | |
105 return size; | |
106 } | |
107 | |
108 static int wav_probe(AVProbeData *p) | |
109 { | |
110 /* check file header */ | |
111 if (p->buf_size <= 32) | |
112 return 0; | |
113 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
114 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
115 p->buf[8] == 'W' && p->buf[9] == 'A' && | |
116 p->buf[10] == 'V' && p->buf[11] == 'E') | |
117 return AVPROBE_SCORE_MAX; | |
118 else | |
119 return 0; | |
120 } | |
121 | |
122 /* wav input */ | |
123 static int wav_read_header(AVFormatContext *s, | |
124 AVFormatParameters *ap) | |
125 { | |
126 int size; | |
127 unsigned int tag; | |
128 ByteIOContext *pb = &s->pb; | |
129 AVStream *st; | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
130 WAVContext *wav = s->priv_data; |
0 | 131 |
132 /* check RIFF header */ | |
133 tag = get_le32(pb); | |
134 | |
135 if (tag != MKTAG('R', 'I', 'F', 'F')) | |
136 return -1; | |
137 get_le32(pb); /* file size */ | |
138 tag = get_le32(pb); | |
139 if (tag != MKTAG('W', 'A', 'V', 'E')) | |
140 return -1; | |
885 | 141 |
0 | 142 /* parse fmt header */ |
143 size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); | |
144 if (size < 0) | |
145 return -1; | |
146 st = av_new_stream(s, 0); | |
147 if (!st) | |
148 return AVERROR_NOMEM; | |
149 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
150 get_wav_header(pb, st->codec, size); |
309 | 151 st->need_parsing = 1; |
567 | 152 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
153 av_set_pts_info(st, 64, 1, st->codec->sample_rate); |
567 | 154 |
0 | 155 size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); |
156 if (size < 0) | |
157 return -1; | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
158 wav->data_end= url_ftell(pb) + size; |
0 | 159 return 0; |
160 } | |
161 | |
162 #define MAX_SIZE 4096 | |
163 | |
164 static int wav_read_packet(AVFormatContext *s, | |
165 AVPacket *pkt) | |
166 { | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
167 int ret, size, left; |
309 | 168 AVStream *st; |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
169 WAVContext *wav = s->priv_data; |
0 | 170 |
171 if (url_feof(&s->pb)) | |
482 | 172 return AVERROR_IO; |
309 | 173 st = s->streams[0]; |
174 | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
175 left= wav->data_end - url_ftell(&s->pb); |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
176 if(left <= 0){ |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
177 left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a')); |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
178 if (left < 0) { |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
179 return AVERROR_IO; |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
180 } |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
181 wav->data_end= url_ftell(&s->pb) + left; |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
182 } |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
183 |
309 | 184 size = MAX_SIZE; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
185 if (st->codec->block_align > 1) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
186 if (size < st->codec->block_align) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
187 size = st->codec->block_align; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
188 size = (size / st->codec->block_align) * st->codec->block_align; |
309 | 189 } |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
190 size= FFMIN(size, left); |
309 | 191 if (av_new_packet(pkt, size)) |
482 | 192 return AVERROR_IO; |
0 | 193 pkt->stream_index = 0; |
194 | |
195 ret = get_buffer(&s->pb, pkt->data, pkt->size); | |
196 if (ret < 0) | |
197 av_free_packet(pkt); | |
198 /* note: we need to modify the packet size here to handle the last | |
199 packet */ | |
200 pkt->size = ret; | |
201 return ret; | |
202 } | |
203 | |
204 static int wav_read_close(AVFormatContext *s) | |
205 { | |
206 return 0; | |
207 } | |
208 | |
885 | 209 static int wav_read_seek(AVFormatContext *s, |
558 | 210 int stream_index, int64_t timestamp, int flags) |
309 | 211 { |
212 AVStream *st; | |
213 | |
214 st = s->streams[0]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
215 switch(st->codec->codec_id) { |
309 | 216 case CODEC_ID_MP2: |
217 case CODEC_ID_MP3: | |
218 case CODEC_ID_AC3: | |
496
112057e05179
libdts support by (Benjamin Zores <ben at geexbox dot org>)
michael
parents:
482
diff
changeset
|
219 case CODEC_ID_DTS: |
309 | 220 /* use generic seeking with dynamically generated indexes */ |
221 return -1; | |
222 default: | |
223 break; | |
224 } | |
558 | 225 return pcm_read_seek(s, stream_index, timestamp, flags); |
309 | 226 } |
227 | |
1169 | 228 #ifdef CONFIG_WAV_DEMUXER |
229 AVInputFormat wav_demuxer = { | |
0 | 230 "wav", |
231 "wav format", | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
232 sizeof(WAVContext), |
0 | 233 wav_probe, |
234 wav_read_header, | |
235 wav_read_packet, | |
236 wav_read_close, | |
309 | 237 wav_read_seek, |
0 | 238 }; |
1169 | 239 #endif |
240 #ifdef CONFIG_WAV_MUXER | |
241 AVOutputFormat wav_muxer = { | |
0 | 242 "wav", |
243 "wav format", | |
244 "audio/x-wav", | |
245 "wav", | |
246 sizeof(WAVContext), | |
247 CODEC_ID_PCM_S16LE, | |
248 CODEC_ID_NONE, | |
249 wav_write_header, | |
250 wav_write_packet, | |
251 wav_write_trailer, | |
252 }; | |
1169 | 253 #endif |