Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 470bce2b

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