Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 71e445fc

History | View | Annotate | Download (5.99 KB)

1 115329f1 Diego Biurrun
/*
2 7fbde343 Aurelien Jacobs
 * WAV muxer and demuxer
3 e095026a Fabrice Bellard
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21
#include "avformat.h"
22 e006c307 Diego Pettenò
#include "allformats.h"
23 9d9f4119 Måns Rullgård
#include "riff.h"
24 46a3d068 Fabrice Bellard
25 de6d9b64 Fabrice Bellard
typedef struct {
26
    offset_t data;
27 b582d7ba Michael Niedermayer
    offset_t data_end;
28 de6d9b64 Fabrice Bellard
} WAVContext;
29
30 277e84f5 Gianluigi Tiesi
#ifdef CONFIG_MUXERS
31 de6d9b64 Fabrice Bellard
static int wav_write_header(AVFormatContext *s)
32
{
33 c9a65ca8 Fabrice Bellard
    WAVContext *wav = s->priv_data;
34 de6d9b64 Fabrice Bellard
    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 01f4895c Michael Niedermayer
    if (put_wav_header(pb, s->streams[0]->codec) < 0) {
44 1ea4f593 Fabrice Bellard
        av_free(wav);
45 46a3d068 Fabrice Bellard
        return -1;
46
    }
47 de6d9b64 Fabrice Bellard
    end_tag(pb, fmt);
48
49 01f4895c Michael Niedermayer
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
50 a1e568bd Michael Niedermayer
51 de6d9b64 Fabrice Bellard
    /* data header */
52
    wav->data = start_tag(pb, "data");
53 115329f1 Diego Biurrun
54 de6d9b64 Fabrice Bellard
    put_flush_packet(pb);
55
56
    return 0;
57
}
58
59 e928649b Michael Niedermayer
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
60 de6d9b64 Fabrice Bellard
{
61
    ByteIOContext *pb = &s->pb;
62 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, pkt->size);
63 de6d9b64 Fabrice Bellard
    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 0c1a9eda Zdenek Kabelac
        put_le32(pb, (uint32_t)(file_size - 8));
79 de6d9b64 Fabrice Bellard
        url_fseek(pb, file_size, SEEK_SET);
80
81
        put_flush_packet(pb);
82
    }
83
    return 0;
84
}
85 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
86 de6d9b64 Fabrice Bellard
87
/* return the size of the found tag */
88
/* XXX: > 2GB ? */
89 0c1a9eda Zdenek Kabelac
static int find_tag(ByteIOContext *pb, uint32_t tag1)
90 de6d9b64 Fabrice Bellard
{
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 c9a65ca8 Fabrice Bellard
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 de6d9b64 Fabrice Bellard
/* 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 b582d7ba Michael Niedermayer
    WAVContext *wav = s->priv_data;
131 de6d9b64 Fabrice Bellard
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 115329f1 Diego Biurrun
142 de6d9b64 Fabrice Bellard
    /* parse fmt header */
143
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
144
    if (size < 0)
145
        return -1;
146 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
147 de6d9b64 Fabrice Bellard
    if (!st)
148 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
149 de6d9b64 Fabrice Bellard
150 01f4895c Michael Niedermayer
    get_wav_header(pb, st->codec, size);
151 f9b5459e Fabrice Bellard
    st->need_parsing = 1;
152 595bf4ef Michael Niedermayer
153 01f4895c Michael Niedermayer
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
154 595bf4ef Michael Niedermayer
155 e095026a Fabrice Bellard
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
156
    if (size < 0)
157
        return -1;
158 b582d7ba Michael Niedermayer
    wav->data_end= url_ftell(pb) + size;
159 de6d9b64 Fabrice Bellard
    return 0;
160
}
161
162
#define MAX_SIZE 4096
163
164
static int wav_read_packet(AVFormatContext *s,
165
                           AVPacket *pkt)
166
{
167 b582d7ba Michael Niedermayer
    int ret, size, left;
168 f9b5459e Fabrice Bellard
    AVStream *st;
169 b582d7ba Michael Niedermayer
    WAVContext *wav = s->priv_data;
170 de6d9b64 Fabrice Bellard
171
    if (url_feof(&s->pb))
172 0bd586c5 Mike Melanson
        return AVERROR_IO;
173 f9b5459e Fabrice Bellard
    st = s->streams[0];
174
175 b582d7ba Michael Niedermayer
    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 f9b5459e Fabrice Bellard
    size = MAX_SIZE;
185 01f4895c Michael Niedermayer
    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 f9b5459e Fabrice Bellard
    }
190 b582d7ba Michael Niedermayer
    size= FFMIN(size, left);
191 e9b78eeb Michael Niedermayer
    ret= av_get_packet(&s->pb, pkt, size);
192
    if (ret <= 0)
193 0bd586c5 Mike Melanson
        return AVERROR_IO;
194 de6d9b64 Fabrice Bellard
    pkt->stream_index = 0;
195
196 5ed8fafc Fabrice Bellard
    /* note: we need to modify the packet size here to handle the last
197
       packet */
198
    pkt->size = ret;
199 de6d9b64 Fabrice Bellard
    return ret;
200
}
201
202
static int wav_read_close(AVFormatContext *s)
203
{
204
    return 0;
205
}
206
207 115329f1 Diego Biurrun
static int wav_read_seek(AVFormatContext *s,
208 7b3c1382 Michael Niedermayer
                         int stream_index, int64_t timestamp, int flags)
209 f9b5459e Fabrice Bellard
{
210
    AVStream *st;
211
212
    st = s->streams[0];
213 01f4895c Michael Niedermayer
    switch(st->codec->codec_id) {
214 f9b5459e Fabrice Bellard
    case CODEC_ID_MP2:
215
    case CODEC_ID_MP3:
216
    case CODEC_ID_AC3:
217 23c99253 Michael Niedermayer
    case CODEC_ID_DTS:
218 f9b5459e Fabrice Bellard
        /* use generic seeking with dynamically generated indexes */
219
        return -1;
220
    default:
221
        break;
222
    }
223 7b3c1382 Michael Niedermayer
    return pcm_read_seek(s, stream_index, timestamp, flags);
224 f9b5459e Fabrice Bellard
}
225
226 ff70e601 Måns Rullgård
#ifdef CONFIG_WAV_DEMUXER
227
AVInputFormat wav_demuxer = {
228 c9a65ca8 Fabrice Bellard
    "wav",
229
    "wav format",
230 b582d7ba Michael Niedermayer
    sizeof(WAVContext),
231 c9a65ca8 Fabrice Bellard
    wav_probe,
232
    wav_read_header,
233
    wav_read_packet,
234
    wav_read_close,
235 f9b5459e Fabrice Bellard
    wav_read_seek,
236 e9b78eeb Michael Niedermayer
    .flags= AVFMT_GENERIC_INDEX,
237 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
238 c9a65ca8 Fabrice Bellard
};
239 ff70e601 Måns Rullgård
#endif
240
#ifdef CONFIG_WAV_MUXER
241
AVOutputFormat wav_muxer = {
242 de6d9b64 Fabrice Bellard
    "wav",
243
    "wav format",
244
    "audio/x-wav",
245
    "wav",
246 c9a65ca8 Fabrice Bellard
    sizeof(WAVContext),
247 5ed8fafc Fabrice Bellard
    CODEC_ID_PCM_S16LE,
248 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
249
    wav_write_header,
250
    wav_write_packet,
251
    wav_write_trailer,
252 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
253 de6d9b64 Fabrice Bellard
};
254 ff70e601 Måns Rullgård
#endif