Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 462b2620

History | View | Annotate | Download (11.3 KB)

1
/*
2
 * WAV muxer and demuxer
3
 * Copyright (c) 2001, 2002 Fabrice Bellard
4
 *
5
 * Sony Wave64 demuxer
6
 * RF64 demuxer
7
 * Copyright (c) 2009 Daniel Verkamp
8
 *
9
 * This file is part of FFmpeg.
10
 *
11
 * FFmpeg is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * FFmpeg is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with FFmpeg; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 */
25
#include "avformat.h"
26
#include "pcm.h"
27
#include "riff.h"
28

    
29
typedef struct {
30
    int64_t data;
31
    int64_t data_end;
32
    int64_t minpts;
33
    int64_t maxpts;
34
    int last_duration;
35
    int w64;
36
} WAVContext;
37

    
38
#if CONFIG_WAV_MUXER
39
static int wav_write_header(AVFormatContext *s)
40
{
41
    WAVContext *wav = s->priv_data;
42
    ByteIOContext *pb = s->pb;
43
    int64_t fmt, fact;
44

    
45
    put_tag(pb, "RIFF");
46
    put_le32(pb, 0); /* file length */
47
    put_tag(pb, "WAVE");
48

    
49
    /* format header */
50
    fmt = ff_start_tag(pb, "fmt ");
51
    if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
52
        av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
53
               s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
54
        av_free(wav);
55
        return -1;
56
    }
57
    ff_end_tag(pb, fmt);
58

    
59
    if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
60
        && !url_is_streamed(s->pb)) {
61
        fact = ff_start_tag(pb, "fact");
62
        put_le32(pb, 0);
63
        ff_end_tag(pb, fact);
64
    }
65

    
66
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
67
    wav->maxpts = wav->last_duration = 0;
68
    wav->minpts = INT64_MAX;
69

    
70
    /* data header */
71
    wav->data = ff_start_tag(pb, "data");
72

    
73
    put_flush_packet(pb);
74

    
75
    return 0;
76
}
77

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

    
92
static int wav_write_trailer(AVFormatContext *s)
93
{
94
    ByteIOContext *pb  = s->pb;
95
    WAVContext    *wav = s->priv_data;
96
    int64_t file_size;
97

    
98
    put_flush_packet(pb);
99

    
100
    if (!url_is_streamed(s->pb)) {
101
        ff_end_tag(pb, wav->data);
102

    
103
        /* update file size */
104
        file_size = url_ftell(pb);
105
        url_fseek(pb, 4, SEEK_SET);
106
        put_le32(pb, (uint32_t)(file_size - 8));
107
        url_fseek(pb, file_size, SEEK_SET);
108

    
109
        put_flush_packet(pb);
110

    
111
        if(s->streams[0]->codec->codec_tag != 0x01) {
112
            /* Update num_samps in fact chunk */
113
            int number_of_samples;
114
            number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
115
                                           s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
116
                                           s->streams[0]->time_base.den);
117
            url_fseek(pb, wav->data-12, SEEK_SET);
118
            put_le32(pb, number_of_samples);
119
            url_fseek(pb, file_size, SEEK_SET);
120
            put_flush_packet(pb);
121
        }
122
    }
123
    return 0;
124
}
125

    
126
AVOutputFormat wav_muxer = {
127
    "wav",
128
    NULL_IF_CONFIG_SMALL("WAV format"),
129
    "audio/x-wav",
130
    "wav",
131
    sizeof(WAVContext),
132
    CODEC_ID_PCM_S16LE,
133
    CODEC_ID_NONE,
134
    wav_write_header,
135
    wav_write_packet,
136
    wav_write_trailer,
137
    .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
138
};
139
#endif /* CONFIG_WAV_MUXER */
140

    
141

    
142
#if CONFIG_WAV_DEMUXER
143
/* return the size of the found tag */
144
static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
145
{
146
    unsigned int tag;
147
    int64_t size;
148

    
149
    for (;;) {
150
        if (url_feof(pb))
151
            return -1;
152
        tag  = get_le32(pb);
153
        size = get_le32(pb);
154
        if (tag == tag1)
155
            break;
156
        url_fseek(pb, size, SEEK_CUR);
157
    }
158
    return size;
159
}
160

    
161
static int wav_probe(AVProbeData *p)
162
{
163
    /* check file header */
164
    if (p->buf_size <= 32)
165
        return 0;
166
    if (!memcmp(p->buf + 8, "WAVE", 4)) {
167
        if (!memcmp(p->buf, "RIFF", 4))
168
            /*
169
              Since ACT demuxer has standard WAV header at top of it's own,
170
              returning score is decreased to avoid probe conflict
171
              between ACT and WAV.
172
            */
173
            return AVPROBE_SCORE_MAX - 1;
174
        else if (!memcmp(p->buf,      "RF64", 4) &&
175
                 !memcmp(p->buf + 12, "ds64", 4))
176
            return AVPROBE_SCORE_MAX;
177
    }
178
    return 0;
179
}
180

    
181
/* wav input */
182
static int wav_read_header(AVFormatContext *s,
183
                           AVFormatParameters *ap)
184
{
185
    int64_t size, av_uninit(data_size);
186
    int64_t sample_count=0;
187
    int rf64;
188
    unsigned int tag;
189
    ByteIOContext *pb = s->pb;
190
    AVStream *st;
191
    WAVContext *wav = s->priv_data;
192

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

    
196
    rf64 = tag == MKTAG('R', 'F', '6', '4');
197
    if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
198
        return -1;
199
    get_le32(pb); /* file size */
200
    tag = get_le32(pb);
201
    if (tag != MKTAG('W', 'A', 'V', 'E'))
202
        return -1;
203

    
204
    if (rf64) {
205
        if (get_le32(pb) != MKTAG('d', 's', '6', '4'))
206
            return -1;
207
        size = get_le32(pb);
208
        if (size < 16)
209
            return -1;
210
        get_le64(pb); /* RIFF size */
211
        data_size = get_le64(pb);
212
        sample_count = get_le64(pb);
213
        url_fskip(pb, size - 16); /* skip rest of ds64 chunk */
214
    }
215

    
216
    /* parse fmt header */
217
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
218
    if (size < 0)
219
        return -1;
220
    st = av_new_stream(s, 0);
221
    if (!st)
222
        return AVERROR(ENOMEM);
223

    
224
    ff_get_wav_header(pb, st->codec, size);
225
    st->need_parsing = AVSTREAM_PARSE_FULL;
226

    
227
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
228

    
229
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
230
    if (rf64)
231
        size = data_size;
232
    if (size < 0)
233
        return -1;
234
    if (!size) {
235
        wav->data_end = INT64_MAX;
236
    } else
237
        wav->data_end= url_ftell(pb) + size;
238

    
239
    if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
240
        sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
241
    if (sample_count)
242
        st->duration = sample_count;
243
    return 0;
244
}
245

    
246
/** Find chunk with w64 GUID by skipping over other chunks
247
 * @return the size of the found chunk
248
 */
249
static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
250
{
251
    uint8_t guid[16];
252
    int64_t size;
253

    
254
    while (!url_feof(pb)) {
255
        get_buffer(pb, guid, 16);
256
        size = get_le64(pb);
257
        if (size <= 24)
258
            return -1;
259
        if (!memcmp(guid, guid1, 16))
260
            return size;
261
        url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
262
    }
263
    return -1;
264
}
265

    
266
static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
267
    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
268

    
269
#define MAX_SIZE 4096
270

    
271
static int wav_read_packet(AVFormatContext *s,
272
                           AVPacket *pkt)
273
{
274
    int ret, size;
275
    int64_t left;
276
    AVStream *st;
277
    WAVContext *wav = s->priv_data;
278

    
279
    st = s->streams[0];
280

    
281
    left = wav->data_end - url_ftell(s->pb);
282
    if (left <= 0){
283
        if (CONFIG_W64_DEMUXER && wav->w64)
284
            left = find_guid(s->pb, guid_data) - 24;
285
        else
286
            left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
287
        if (left < 0)
288
            return AVERROR_EOF;
289
        wav->data_end= url_ftell(s->pb) + left;
290
    }
291

    
292
    size = MAX_SIZE;
293
    if (st->codec->block_align > 1) {
294
        if (size < st->codec->block_align)
295
            size = st->codec->block_align;
296
        size = (size / st->codec->block_align) * st->codec->block_align;
297
    }
298
    size = FFMIN(size, left);
299
    ret  = av_get_packet(s->pb, pkt, size);
300
    if (ret < 0)
301
        return ret;
302
    pkt->stream_index = 0;
303

    
304
    return ret;
305
}
306

    
307
static int wav_read_seek(AVFormatContext *s,
308
                         int stream_index, int64_t timestamp, int flags)
309
{
310
    AVStream *st;
311

    
312
    st = s->streams[0];
313
    switch (st->codec->codec_id) {
314
    case CODEC_ID_MP2:
315
    case CODEC_ID_MP3:
316
    case CODEC_ID_AC3:
317
    case CODEC_ID_DTS:
318
        /* use generic seeking with dynamically generated indexes */
319
        return -1;
320
    default:
321
        break;
322
    }
323
    return pcm_read_seek(s, stream_index, timestamp, flags);
324
}
325

    
326
AVInputFormat wav_demuxer = {
327
    "wav",
328
    NULL_IF_CONFIG_SMALL("WAV format"),
329
    sizeof(WAVContext),
330
    wav_probe,
331
    wav_read_header,
332
    wav_read_packet,
333
    NULL,
334
    wav_read_seek,
335
    .flags= AVFMT_GENERIC_INDEX,
336
    .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
337
};
338
#endif /* CONFIG_WAV_DEMUXER */
339

    
340

    
341
#if CONFIG_W64_DEMUXER
342
static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
343
    0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
344

    
345
static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
346
    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
347

    
348
static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
349
    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
350

    
351
static int w64_probe(AVProbeData *p)
352
{
353
    if (p->buf_size <= 40)
354
        return 0;
355
    if (!memcmp(p->buf,      guid_riff, 16) &&
356
        !memcmp(p->buf + 24, guid_wave, 16))
357
        return AVPROBE_SCORE_MAX;
358
    else
359
        return 0;
360
}
361

    
362
static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
363
{
364
    int64_t size;
365
    ByteIOContext *pb  = s->pb;
366
    WAVContext    *wav = s->priv_data;
367
    AVStream *st;
368
    uint8_t guid[16];
369

    
370
    get_buffer(pb, guid, 16);
371
    if (memcmp(guid, guid_riff, 16))
372
        return -1;
373

    
374
    if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
375
        return -1;
376

    
377
    get_buffer(pb, guid, 16);
378
    if (memcmp(guid, guid_wave, 16)) {
379
        av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
380
        return -1;
381
    }
382

    
383
    size = find_guid(pb, guid_fmt);
384
    if (size < 0) {
385
        av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
386
        return -1;
387
    }
388

    
389
    st = av_new_stream(s, 0);
390
    if (!st)
391
        return AVERROR(ENOMEM);
392

    
393
    /* subtract chunk header size - normal wav file doesn't count it */
394
    ff_get_wav_header(pb, st->codec, size - 24);
395
    url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
396

    
397
    st->need_parsing = AVSTREAM_PARSE_FULL;
398

    
399
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
400

    
401
    size = find_guid(pb, guid_data);
402
    if (size < 0) {
403
        av_log(s, AV_LOG_ERROR, "could not find data guid\n");
404
        return -1;
405
    }
406
    wav->data_end = url_ftell(pb) + size - 24;
407
    wav->w64      = 1;
408

    
409
    return 0;
410
}
411

    
412
AVInputFormat w64_demuxer = {
413
    "w64",
414
    NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
415
    sizeof(WAVContext),
416
    w64_probe,
417
    w64_read_header,
418
    wav_read_packet,
419
    NULL,
420
    wav_read_seek,
421
    .flags = AVFMT_GENERIC_INDEX,
422
    .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
423
};
424
#endif /* CONFIG_W64_DEMUXER */