]> git.sesse.net Git - ffmpeg/blob - libavformat/wav.c
use parenthesis around value
[ffmpeg] / libavformat / wav.c
1 /*
2  * WAV encoder and decoder
3  * Copyright (c) 2001, 2002 Fabrice Bellard.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include "avformat.h"
20 #include "allformats.h"
21 #include "riff.h"
22
23 typedef struct {
24     offset_t data;
25     offset_t data_end;
26 } WAVContext;
27
28 #ifdef CONFIG_MUXERS
29 static int wav_write_header(AVFormatContext *s)
30 {
31     WAVContext *wav = s->priv_data;
32     ByteIOContext *pb = &s->pb;
33     offset_t fmt;
34
35     put_tag(pb, "RIFF");
36     put_le32(pb, 0); /* file length */
37     put_tag(pb, "WAVE");
38
39     /* format header */
40     fmt = start_tag(pb, "fmt ");
41     if (put_wav_header(pb, s->streams[0]->codec) < 0) {
42         av_free(wav);
43         return -1;
44     }
45     end_tag(pb, fmt);
46
47     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
48
49     /* data header */
50     wav->data = start_tag(pb, "data");
51
52     put_flush_packet(pb);
53
54     return 0;
55 }
56
57 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
58 {
59     ByteIOContext *pb = &s->pb;
60     put_buffer(pb, pkt->data, pkt->size);
61     return 0;
62 }
63
64 static int wav_write_trailer(AVFormatContext *s)
65 {
66     ByteIOContext *pb = &s->pb;
67     WAVContext *wav = s->priv_data;
68     offset_t file_size;
69
70     if (!url_is_streamed(&s->pb)) {
71         end_tag(pb, wav->data);
72
73         /* update file size */
74         file_size = url_ftell(pb);
75         url_fseek(pb, 4, SEEK_SET);
76         put_le32(pb, (uint32_t)(file_size - 8));
77         url_fseek(pb, file_size, SEEK_SET);
78
79         put_flush_packet(pb);
80     }
81     return 0;
82 }
83 #endif //CONFIG_MUXERS
84
85 /* return the size of the found tag */
86 /* XXX: > 2GB ? */
87 static int find_tag(ByteIOContext *pb, uint32_t tag1)
88 {
89     unsigned int tag;
90     int size;
91
92     for(;;) {
93         if (url_feof(pb))
94             return -1;
95         tag = get_le32(pb);
96         size = get_le32(pb);
97         if (tag == tag1)
98             break;
99         url_fseek(pb, size, SEEK_CUR);
100     }
101     if (size < 0)
102         size = 0x7fffffff;
103     return size;
104 }
105
106 static int wav_probe(AVProbeData *p)
107 {
108     /* check file header */
109     if (p->buf_size <= 32)
110         return 0;
111     if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
112         p->buf[2] == 'F' && p->buf[3] == 'F' &&
113         p->buf[8] == 'W' && p->buf[9] == 'A' &&
114         p->buf[10] == 'V' && p->buf[11] == 'E')
115         return AVPROBE_SCORE_MAX;
116     else
117         return 0;
118 }
119
120 /* wav input */
121 static int wav_read_header(AVFormatContext *s,
122                            AVFormatParameters *ap)
123 {
124     int size;
125     unsigned int tag;
126     ByteIOContext *pb = &s->pb;
127     AVStream *st;
128     WAVContext *wav = s->priv_data;
129
130     /* check RIFF header */
131     tag = get_le32(pb);
132
133     if (tag != MKTAG('R', 'I', 'F', 'F'))
134         return -1;
135     get_le32(pb); /* file size */
136     tag = get_le32(pb);
137     if (tag != MKTAG('W', 'A', 'V', 'E'))
138         return -1;
139
140     /* parse fmt header */
141     size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
142     if (size < 0)
143         return -1;
144     st = av_new_stream(s, 0);
145     if (!st)
146         return AVERROR_NOMEM;
147
148     get_wav_header(pb, st->codec, size);
149     st->need_parsing = 1;
150
151     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
152
153     size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
154     if (size < 0)
155         return -1;
156     wav->data_end= url_ftell(pb) + size;
157     return 0;
158 }
159
160 #define MAX_SIZE 4096
161
162 static int wav_read_packet(AVFormatContext *s,
163                            AVPacket *pkt)
164 {
165     int ret, size, left;
166     AVStream *st;
167     WAVContext *wav = s->priv_data;
168
169     if (url_feof(&s->pb))
170         return AVERROR_IO;
171     st = s->streams[0];
172
173     left= wav->data_end - url_ftell(&s->pb);
174     if(left <= 0){
175         left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
176         if (left < 0) {
177             return AVERROR_IO;
178         }
179         wav->data_end= url_ftell(&s->pb) + left;
180     }
181
182     size = MAX_SIZE;
183     if (st->codec->block_align > 1) {
184         if (size < st->codec->block_align)
185             size = st->codec->block_align;
186         size = (size / st->codec->block_align) * st->codec->block_align;
187     }
188     size= FFMIN(size, left);
189     if (av_new_packet(pkt, size))
190         return AVERROR_IO;
191     pkt->stream_index = 0;
192
193     ret = get_buffer(&s->pb, pkt->data, pkt->size);
194     if (ret < 0)
195         av_free_packet(pkt);
196     /* note: we need to modify the packet size here to handle the last
197        packet */
198     pkt->size = ret;
199     return ret;
200 }
201
202 static int wav_read_close(AVFormatContext *s)
203 {
204     return 0;
205 }
206
207 static int wav_read_seek(AVFormatContext *s,
208                          int stream_index, int64_t timestamp, int flags)
209 {
210     AVStream *st;
211
212     st = s->streams[0];
213     switch(st->codec->codec_id) {
214     case CODEC_ID_MP2:
215     case CODEC_ID_MP3:
216     case CODEC_ID_AC3:
217     case CODEC_ID_DTS:
218         /* use generic seeking with dynamically generated indexes */
219         return -1;
220     default:
221         break;
222     }
223     return pcm_read_seek(s, stream_index, timestamp, flags);
224 }
225
226 #ifdef CONFIG_WAV_DEMUXER
227 AVInputFormat wav_demuxer = {
228     "wav",
229     "wav format",
230     sizeof(WAVContext),
231     wav_probe,
232     wav_read_header,
233     wav_read_packet,
234     wav_read_close,
235     wav_read_seek,
236 };
237 #endif
238 #ifdef CONFIG_WAV_MUXER
239 AVOutputFormat wav_muxer = {
240     "wav",
241     "wav format",
242     "audio/x-wav",
243     "wav",
244     sizeof(WAVContext),
245     CODEC_ID_PCM_S16LE,
246     CODEC_ID_NONE,
247     wav_write_header,
248     wav_write_packet,
249     wav_write_trailer,
250 };
251 #endif