Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 1ae7518d

History | View | Annotate | Download (6.03 KB)

1
/*
2
 * WAV muxer and demuxer
3
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
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
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include "allformats.h"
23
#include "riff.h"
24

    
25
typedef struct {
26
    offset_t data;
27
    offset_t data_end;
28
} WAVContext;
29

    
30
#ifdef CONFIG_MUXERS
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 ");
43
    if (put_wav_header(pb, s->streams[0]->codec) < 0) {
44
        av_free(wav);
45
        return -1;
46
    }
47
    end_tag(pb, fmt);
48

    
49
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
50

    
51
    /* data header */
52
    wav->data = start_tag(pb, "data");
53

    
54
    put_flush_packet(pb);
55

    
56
    return 0;
57
}
58

    
59
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
60
{
61
    ByteIOContext *pb = &s->pb;
62
    put_buffer(pb, pkt->data, pkt->size);
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);
78
        put_le32(pb, (uint32_t)(file_size - 8));
79
        url_fseek(pb, file_size, SEEK_SET);
80

    
81
        put_flush_packet(pb);
82
    }
83
    return 0;
84
}
85
#endif //CONFIG_MUXERS
86

    
87
/* return the size of the found tag */
88
/* XXX: > 2GB ? */
89
static int find_tag(ByteIOContext *pb, uint32_t tag1)
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;
130
    WAVContext *wav = s->priv_data;
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;
141

    
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

    
150
    get_wav_header(pb, st->codec, size);
151
    st->need_parsing = 1;
152

    
153
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
154

    
155
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
156
    if (size < 0)
157
        return -1;
158
    wav->data_end= url_ftell(pb) + size;
159
    return 0;
160
}
161

    
162
#define MAX_SIZE 4096
163

    
164
static int wav_read_packet(AVFormatContext *s,
165
                           AVPacket *pkt)
166
{
167
    int ret, size, left;
168
    AVStream *st;
169
    WAVContext *wav = s->priv_data;
170

    
171
    if (url_feof(&s->pb))
172
        return AVERROR_IO;
173
    st = s->streams[0];
174

    
175
    left= wav->data_end - url_ftell(&s->pb);
176
    if(left <= 0){
177
        left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
178
        if (left < 0) {
179
            return AVERROR_IO;
180
        }
181
        wav->data_end= url_ftell(&s->pb) + left;
182
    }
183

    
184
    size = MAX_SIZE;
185
    if (st->codec->block_align > 1) {
186
        if (size < st->codec->block_align)
187
            size = st->codec->block_align;
188
        size = (size / st->codec->block_align) * st->codec->block_align;
189
    }
190
    size= FFMIN(size, left);
191
    if (av_new_packet(pkt, size))
192
        return AVERROR_IO;
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

    
209
static int wav_read_seek(AVFormatContext *s,
210
                         int stream_index, int64_t timestamp, int flags)
211
{
212
    AVStream *st;
213

    
214
    st = s->streams[0];
215
    switch(st->codec->codec_id) {
216
    case CODEC_ID_MP2:
217
    case CODEC_ID_MP3:
218
    case CODEC_ID_AC3:
219
    case CODEC_ID_DTS:
220
        /* use generic seeking with dynamically generated indexes */
221
        return -1;
222
    default:
223
        break;
224
    }
225
    return pcm_read_seek(s, stream_index, timestamp, flags);
226
}
227

    
228
#ifdef CONFIG_WAV_DEMUXER
229
AVInputFormat wav_demuxer = {
230
    "wav",
231
    "wav format",
232
    sizeof(WAVContext),
233
    wav_probe,
234
    wav_read_header,
235
    wav_read_packet,
236
    wav_read_close,
237
    wav_read_seek,
238
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
239
};
240
#endif
241
#ifdef CONFIG_WAV_MUXER
242
AVOutputFormat wav_muxer = {
243
    "wav",
244
    "wav format",
245
    "audio/x-wav",
246
    "wav",
247
    sizeof(WAVContext),
248
    CODEC_ID_PCM_S16LE,
249
    CODEC_ID_NONE,
250
    wav_write_header,
251
    wav_write_packet,
252
    wav_write_trailer,
253
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
254
};
255
#endif