wav.c 7.56 KB
Newer Older
1
/*
2
 * WAV muxer and demuxer
3
 * Copyright (c) 2001, 2002 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
20 21
 */
#include "avformat.h"
22
#include "raw.h"
23
#include "riff.h"
24

Fabrice Bellard's avatar
Fabrice Bellard committed
25
typedef struct {
26 27
    int64_t data;
    int64_t data_end;
28 29 30
    int64_t minpts;
    int64_t maxpts;
    int last_duration;
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32
} WAVContext;

33
#if CONFIG_WAV_MUXER
Fabrice Bellard's avatar
Fabrice Bellard committed
34 35
static int wav_write_header(AVFormatContext *s)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
36
    WAVContext *wav = s->priv_data;
37
    ByteIOContext *pb = s->pb;
38
    int64_t fmt, fact;
Fabrice Bellard's avatar
Fabrice Bellard committed
39 40 41 42 43 44 45

    put_tag(pb, "RIFF");
    put_le32(pb, 0); /* file length */
    put_tag(pb, "WAVE");

    /* format header */
    fmt = start_tag(pb, "fmt ");
46
    if (put_wav_header(pb, s->streams[0]->codec) < 0) {
47
        av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
48
               s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
49
        av_free(wav);
50 51
        return -1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
52 53
    end_tag(pb, fmt);

54
    if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
55
       && !url_is_streamed(s->pb)) {
56 57 58 59 60
        fact = start_tag(pb, "fact");
        put_le32(pb, 0);
        end_tag(pb, fact);
    }

61
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
62 63
    wav->maxpts = wav->last_duration = 0;
    wav->minpts = INT64_MAX;
64

Fabrice Bellard's avatar
Fabrice Bellard committed
65 66
    /* data header */
    wav->data = start_tag(pb, "data");
67

Fabrice Bellard's avatar
Fabrice Bellard committed
68 69 70 71 72
    put_flush_packet(pb);

    return 0;
}

73
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
Fabrice Bellard's avatar
Fabrice Bellard committed
74
{
75
    ByteIOContext *pb = s->pb;
76
    WAVContext *wav = s->priv_data;
77
    put_buffer(pb, pkt->data, pkt->size);
78 79 80 81 82 83
    if(pkt->pts != AV_NOPTS_VALUE) {
        wav->minpts = FFMIN(wav->minpts, pkt->pts);
        wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
        wav->last_duration = pkt->duration;
    } else
        av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
84 85 86 87 88
    return 0;
}

static int wav_write_trailer(AVFormatContext *s)
{
89
    ByteIOContext *pb = s->pb;
Fabrice Bellard's avatar
Fabrice Bellard committed
90
    WAVContext *wav = s->priv_data;
91
    int64_t file_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
92

93
    if (!url_is_streamed(s->pb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
94 95 96 97 98
        end_tag(pb, wav->data);

        /* update file size */
        file_size = url_ftell(pb);
        url_fseek(pb, 4, SEEK_SET);
99
        put_le32(pb, (uint32_t)(file_size - 8));
Fabrice Bellard's avatar
Fabrice Bellard committed
100 101 102
        url_fseek(pb, file_size, SEEK_SET);

        put_flush_packet(pb);
103 104 105 106 107 108 109 110 111 112 113 114

        if(s->streams[0]->codec->codec_tag != 0x01) {
            /* Update num_samps in fact chunk */
            int number_of_samples;
            number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
                                           s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
                                           s->streams[0]->time_base.den);
            url_fseek(pb, wav->data-12, SEEK_SET);
            put_le32(pb, number_of_samples);
            url_fseek(pb, file_size, SEEK_SET);
            put_flush_packet(pb);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
115 116 117
    }
    return 0;
}
118
#endif /* CONFIG_WAV_MUXER */
Fabrice Bellard's avatar
Fabrice Bellard committed
119 120 121

/* return the size of the found tag */
/* XXX: > 2GB ? */
122
static int find_tag(ByteIOContext *pb, uint32_t tag1)
Fabrice Bellard's avatar
Fabrice Bellard committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
    unsigned int tag;
    int size;

    for(;;) {
        if (url_feof(pb))
            return -1;
        tag = get_le32(pb);
        size = get_le32(pb);
        if (tag == tag1)
            break;
        url_fseek(pb, size, SEEK_CUR);
    }
    if (size < 0)
        size = 0x7fffffff;
    return size;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
141 142 143 144 145 146 147 148 149
static int wav_probe(AVProbeData *p)
{
    /* check file header */
    if (p->buf_size <= 32)
        return 0;
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
        p->buf[8] == 'W' && p->buf[9] == 'A' &&
        p->buf[10] == 'V' && p->buf[11] == 'E')
150 151 152 153 154 155
        /*
          Since ACT demuxer has standard WAV header at top of it's own,
          returning score is decreased to avoid probe conflict
          between ACT and WAV.
        */
        return AVPROBE_SCORE_MAX - 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
156 157 158 159
    else
        return 0;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
160 161 162 163 164 165
/* wav input */
static int wav_read_header(AVFormatContext *s,
                           AVFormatParameters *ap)
{
    int size;
    unsigned int tag;
166
    ByteIOContext *pb = s->pb;
Fabrice Bellard's avatar
Fabrice Bellard committed
167
    AVStream *st;
168
    WAVContext *wav = s->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
169 170 171 172 173 174 175 176 177 178

    /* check RIFF header */
    tag = get_le32(pb);

    if (tag != MKTAG('R', 'I', 'F', 'F'))
        return -1;
    get_le32(pb); /* file size */
    tag = get_le32(pb);
    if (tag != MKTAG('W', 'A', 'V', 'E'))
        return -1;
179

Fabrice Bellard's avatar
Fabrice Bellard committed
180 181 182 183
    /* parse fmt header */
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
    if (size < 0)
        return -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
184
    st = av_new_stream(s, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
185
    if (!st)
186
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
187

188
    get_wav_header(pb, st->codec, size);
189
    st->need_parsing = AVSTREAM_PARSE_FULL;
190

191
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
192

193 194 195
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
    if (size < 0)
        return -1;
196
    wav->data_end= url_ftell(pb) + size;
Fabrice Bellard's avatar
Fabrice Bellard committed
197 198 199 200 201 202 203 204
    return 0;
}

#define MAX_SIZE 4096

static int wav_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
205
    int ret, size, left;
206
    AVStream *st;
207
    WAVContext *wav = s->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
208

209
    if (url_feof(s->pb))
210
        return AVERROR(EIO);
211 212
    st = s->streams[0];

213
    left= wav->data_end - url_ftell(s->pb);
214
    if(left <= 0){
215
        left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
216
        if (left < 0) {
217
            return AVERROR(EIO);
218
        }
219
        wav->data_end= url_ftell(s->pb) + left;
220 221
    }

222
    size = MAX_SIZE;
223 224 225 226
    if (st->codec->block_align > 1) {
        if (size < st->codec->block_align)
            size = st->codec->block_align;
        size = (size / st->codec->block_align) * st->codec->block_align;
227
    }
228
    size= FFMIN(size, left);
229
    ret= av_get_packet(s->pb, pkt, size);
230
    if (ret <= 0)
231
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
232 233
    pkt->stream_index = 0;

Fabrice Bellard's avatar
Fabrice Bellard committed
234 235 236
    /* note: we need to modify the packet size here to handle the last
       packet */
    pkt->size = ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
237 238 239
    return ret;
}

240
static int wav_read_seek(AVFormatContext *s,
241
                         int stream_index, int64_t timestamp, int flags)
242 243 244 245
{
    AVStream *st;

    st = s->streams[0];
246
    switch(st->codec->codec_id) {
247 248 249
    case CODEC_ID_MP2:
    case CODEC_ID_MP3:
    case CODEC_ID_AC3:
250
    case CODEC_ID_DTS:
251 252 253 254 255
        /* use generic seeking with dynamically generated indexes */
        return -1;
    default:
        break;
    }
256
    return pcm_read_seek(s, stream_index, timestamp, flags);
257 258
}

259
#if CONFIG_WAV_DEMUXER
260
AVInputFormat wav_demuxer = {
Fabrice Bellard's avatar
Fabrice Bellard committed
261
    "wav",
262
    NULL_IF_CONFIG_SMALL("WAV format"),
263
    sizeof(WAVContext),
Fabrice Bellard's avatar
Fabrice Bellard committed
264 265 266
    wav_probe,
    wav_read_header,
    wav_read_packet,
267
    NULL,
268
    wav_read_seek,
269
    .flags= AVFMT_GENERIC_INDEX,
270
    .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
Fabrice Bellard's avatar
Fabrice Bellard committed
271
};
272
#endif
273
#if CONFIG_WAV_MUXER
274
AVOutputFormat wav_muxer = {
Fabrice Bellard's avatar
Fabrice Bellard committed
275
    "wav",
276
    NULL_IF_CONFIG_SMALL("WAV format"),
Fabrice Bellard's avatar
Fabrice Bellard committed
277 278
    "audio/x-wav",
    "wav",
Fabrice Bellard's avatar
Fabrice Bellard committed
279
    sizeof(WAVContext),
Fabrice Bellard's avatar
Fabrice Bellard committed
280
    CODEC_ID_PCM_S16LE,
Fabrice Bellard's avatar
Fabrice Bellard committed
281 282 283 284
    CODEC_ID_NONE,
    wav_write_header,
    wav_write_packet,
    wav_write_trailer,
285
    .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
Fabrice Bellard's avatar
Fabrice Bellard committed
286
};
287
#endif