Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ d2062ff6

History | View | Annotate | Download (7.25 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 d794d388 Michel Bardiaux
    int64_t minpts;
29
    int64_t maxpts;
30
    int last_duration;
31 de6d9b64 Fabrice Bellard
} WAVContext;
32
33 277e84f5 Gianluigi Tiesi
#ifdef CONFIG_MUXERS
34 de6d9b64 Fabrice Bellard
static int wav_write_header(AVFormatContext *s)
35
{
36 c9a65ca8 Fabrice Bellard
    WAVContext *wav = s->priv_data;
37 de6d9b64 Fabrice Bellard
    ByteIOContext *pb = &s->pb;
38 d794d388 Michel Bardiaux
    offset_t fmt, fact;
39 de6d9b64 Fabrice Bellard
40
    put_tag(pb, "RIFF");
41
    put_le32(pb, 0); /* file length */
42
    put_tag(pb, "WAVE");
43
44
    /* format header */
45
    fmt = start_tag(pb, "fmt ");
46 01f4895c Michael Niedermayer
    if (put_wav_header(pb, s->streams[0]->codec) < 0) {
47 1ea4f593 Fabrice Bellard
        av_free(wav);
48 46a3d068 Fabrice Bellard
        return -1;
49
    }
50 de6d9b64 Fabrice Bellard
    end_tag(pb, fmt);
51
52 d794d388 Michel Bardiaux
    if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
53
       && !url_is_streamed(&s->pb)) {
54
        fact = start_tag(pb, "fact");
55
        put_le32(pb, 0);
56
        end_tag(pb, fact);
57
    }
58
59 01f4895c Michael Niedermayer
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
60 d794d388 Michel Bardiaux
    wav->maxpts = wav->last_duration = 0;
61
    wav->minpts = INT64_MAX;
62 a1e568bd Michael Niedermayer
63 de6d9b64 Fabrice Bellard
    /* data header */
64
    wav->data = start_tag(pb, "data");
65 115329f1 Diego Biurrun
66 de6d9b64 Fabrice Bellard
    put_flush_packet(pb);
67
68
    return 0;
69
}
70
71 e928649b Michael Niedermayer
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
72 de6d9b64 Fabrice Bellard
{
73
    ByteIOContext *pb = &s->pb;
74 d794d388 Michel Bardiaux
    WAVContext *wav = s->priv_data;
75 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, pkt->size);
76 d794d388 Michel Bardiaux
    if(pkt->pts != AV_NOPTS_VALUE) {
77
        wav->minpts = FFMIN(wav->minpts, pkt->pts);
78
        wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
79
        wav->last_duration = pkt->duration;
80
    } else
81
        av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
82 de6d9b64 Fabrice Bellard
    return 0;
83
}
84
85
static int wav_write_trailer(AVFormatContext *s)
86
{
87
    ByteIOContext *pb = &s->pb;
88
    WAVContext *wav = s->priv_data;
89
    offset_t file_size;
90
91
    if (!url_is_streamed(&s->pb)) {
92
        end_tag(pb, wav->data);
93
94
        /* update file size */
95
        file_size = url_ftell(pb);
96
        url_fseek(pb, 4, SEEK_SET);
97 0c1a9eda Zdenek Kabelac
        put_le32(pb, (uint32_t)(file_size - 8));
98 de6d9b64 Fabrice Bellard
        url_fseek(pb, file_size, SEEK_SET);
99
100
        put_flush_packet(pb);
101 d794d388 Michel Bardiaux
102
        if(s->streams[0]->codec->codec_tag != 0x01) {
103
            /* Update num_samps in fact chunk */
104
            int number_of_samples;
105
            number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
106
                                           s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
107
                                           s->streams[0]->time_base.den);
108
            url_fseek(pb, wav->data-12, SEEK_SET);
109
            put_le32(pb, number_of_samples);
110
            url_fseek(pb, file_size, SEEK_SET);
111
            put_flush_packet(pb);
112
        }
113 de6d9b64 Fabrice Bellard
    }
114
    return 0;
115
}
116 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
117 de6d9b64 Fabrice Bellard
118
/* return the size of the found tag */
119
/* XXX: > 2GB ? */
120 0c1a9eda Zdenek Kabelac
static int find_tag(ByteIOContext *pb, uint32_t tag1)
121 de6d9b64 Fabrice Bellard
{
122
    unsigned int tag;
123
    int size;
124
125
    for(;;) {
126
        if (url_feof(pb))
127
            return -1;
128
        tag = get_le32(pb);
129
        size = get_le32(pb);
130
        if (tag == tag1)
131
            break;
132
        url_fseek(pb, size, SEEK_CUR);
133
    }
134
    if (size < 0)
135
        size = 0x7fffffff;
136
    return size;
137
}
138
139 c9a65ca8 Fabrice Bellard
static int wav_probe(AVProbeData *p)
140
{
141
    /* check file header */
142
    if (p->buf_size <= 32)
143
        return 0;
144
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
145
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
146
        p->buf[8] == 'W' && p->buf[9] == 'A' &&
147
        p->buf[10] == 'V' && p->buf[11] == 'E')
148
        return AVPROBE_SCORE_MAX;
149
    else
150
        return 0;
151
}
152
153 de6d9b64 Fabrice Bellard
/* wav input */
154
static int wav_read_header(AVFormatContext *s,
155
                           AVFormatParameters *ap)
156
{
157
    int size;
158
    unsigned int tag;
159
    ByteIOContext *pb = &s->pb;
160
    AVStream *st;
161 b582d7ba Michael Niedermayer
    WAVContext *wav = s->priv_data;
162 de6d9b64 Fabrice Bellard
163
    /* check RIFF header */
164
    tag = get_le32(pb);
165
166
    if (tag != MKTAG('R', 'I', 'F', 'F'))
167
        return -1;
168
    get_le32(pb); /* file size */
169
    tag = get_le32(pb);
170
    if (tag != MKTAG('W', 'A', 'V', 'E'))
171
        return -1;
172 115329f1 Diego Biurrun
173 de6d9b64 Fabrice Bellard
    /* parse fmt header */
174
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
175
    if (size < 0)
176
        return -1;
177 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
178 de6d9b64 Fabrice Bellard
    if (!st)
179 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
180 de6d9b64 Fabrice Bellard
181 01f4895c Michael Niedermayer
    get_wav_header(pb, st->codec, size);
182 57004ff1 Aurelien Jacobs
    st->need_parsing = AVSTREAM_PARSE_FULL;
183 595bf4ef Michael Niedermayer
184 01f4895c Michael Niedermayer
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
185 595bf4ef Michael Niedermayer
186 e095026a Fabrice Bellard
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
187
    if (size < 0)
188
        return -1;
189 b582d7ba Michael Niedermayer
    wav->data_end= url_ftell(pb) + size;
190 de6d9b64 Fabrice Bellard
    return 0;
191
}
192
193
#define MAX_SIZE 4096
194
195
static int wav_read_packet(AVFormatContext *s,
196
                           AVPacket *pkt)
197
{
198 b582d7ba Michael Niedermayer
    int ret, size, left;
199 f9b5459e Fabrice Bellard
    AVStream *st;
200 b582d7ba Michael Niedermayer
    WAVContext *wav = s->priv_data;
201 de6d9b64 Fabrice Bellard
202
    if (url_feof(&s->pb))
203 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
204 f9b5459e Fabrice Bellard
    st = s->streams[0];
205
206 b582d7ba Michael Niedermayer
    left= wav->data_end - url_ftell(&s->pb);
207
    if(left <= 0){
208
        left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
209
        if (left < 0) {
210 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
211 b582d7ba Michael Niedermayer
        }
212
        wav->data_end= url_ftell(&s->pb) + left;
213
    }
214
215 f9b5459e Fabrice Bellard
    size = MAX_SIZE;
216 01f4895c Michael Niedermayer
    if (st->codec->block_align > 1) {
217
        if (size < st->codec->block_align)
218
            size = st->codec->block_align;
219
        size = (size / st->codec->block_align) * st->codec->block_align;
220 f9b5459e Fabrice Bellard
    }
221 b582d7ba Michael Niedermayer
    size= FFMIN(size, left);
222 e9b78eeb Michael Niedermayer
    ret= av_get_packet(&s->pb, pkt, size);
223
    if (ret <= 0)
224 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
225 de6d9b64 Fabrice Bellard
    pkt->stream_index = 0;
226
227 5ed8fafc Fabrice Bellard
    /* note: we need to modify the packet size here to handle the last
228
       packet */
229
    pkt->size = ret;
230 de6d9b64 Fabrice Bellard
    return ret;
231
}
232
233
static int wav_read_close(AVFormatContext *s)
234
{
235
    return 0;
236
}
237
238 115329f1 Diego Biurrun
static int wav_read_seek(AVFormatContext *s,
239 7b3c1382 Michael Niedermayer
                         int stream_index, int64_t timestamp, int flags)
240 f9b5459e Fabrice Bellard
{
241
    AVStream *st;
242
243
    st = s->streams[0];
244 01f4895c Michael Niedermayer
    switch(st->codec->codec_id) {
245 f9b5459e Fabrice Bellard
    case CODEC_ID_MP2:
246
    case CODEC_ID_MP3:
247
    case CODEC_ID_AC3:
248 23c99253 Michael Niedermayer
    case CODEC_ID_DTS:
249 f9b5459e Fabrice Bellard
        /* use generic seeking with dynamically generated indexes */
250
        return -1;
251
    default:
252
        break;
253
    }
254 7b3c1382 Michael Niedermayer
    return pcm_read_seek(s, stream_index, timestamp, flags);
255 f9b5459e Fabrice Bellard
}
256
257 ff70e601 Måns Rullgård
#ifdef CONFIG_WAV_DEMUXER
258
AVInputFormat wav_demuxer = {
259 c9a65ca8 Fabrice Bellard
    "wav",
260
    "wav format",
261 b582d7ba Michael Niedermayer
    sizeof(WAVContext),
262 c9a65ca8 Fabrice Bellard
    wav_probe,
263
    wav_read_header,
264
    wav_read_packet,
265
    wav_read_close,
266 f9b5459e Fabrice Bellard
    wav_read_seek,
267 e9b78eeb Michael Niedermayer
    .flags= AVFMT_GENERIC_INDEX,
268 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
269 c9a65ca8 Fabrice Bellard
};
270 ff70e601 Måns Rullgård
#endif
271
#ifdef CONFIG_WAV_MUXER
272
AVOutputFormat wav_muxer = {
273 de6d9b64 Fabrice Bellard
    "wav",
274
    "wav format",
275
    "audio/x-wav",
276
    "wav",
277 c9a65ca8 Fabrice Bellard
    sizeof(WAVContext),
278 5ed8fafc Fabrice Bellard
    CODEC_ID_PCM_S16LE,
279 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
280
    wav_write_header,
281
    wav_write_packet,
282
    wav_write_trailer,
283 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
284 de6d9b64 Fabrice Bellard
};
285 ff70e601 Måns Rullgård
#endif