Revision 740e89cf libavformat/wav.c

View differences:

libavformat/wav.c
2 2
 * WAV muxer and demuxer
3 3
 * Copyright (c) 2001, 2002 Fabrice Bellard
4 4
 *
5
 * Sony Wave64 demuxer
6
 * Copyright (c) 2009 Daniel Verkamp
7
 *
5 8
 * This file is part of FFmpeg.
6 9
 *
7 10
 * FFmpeg is free software; you can redistribute it and/or
......
28 31
    int64_t minpts;
29 32
    int64_t maxpts;
30 33
    int last_duration;
34
    int w64;
31 35
} WAVContext;
32 36

  
33 37
#if CONFIG_WAV_MUXER
......
194 198
    return 0;
195 199
}
196 200

  
201
#if CONFIG_W64_DEMUXER
202

  
203
static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
204
    0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
205

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

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

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

  
215
static int w64_probe(AVProbeData *p)
216
{
217
    if (p->buf_size <= 40)
218
        return 0;
219
    if (!memcmp(p->buf,      guid_riff, 16) &&
220
        !memcmp(p->buf + 24, guid_wave, 16)) {
221
        return AVPROBE_SCORE_MAX;
222
    } else
223
        return 0;
224
}
225

  
226
/** Find chunk with w64 GUID by skipping over other chunks
227
 * @return the size of the found chunk
228
 */
229
static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
230
{
231
    uint8_t guid[16];
232
    int64_t size;
233

  
234
    while (!url_feof(pb)) {
235
        get_buffer(pb, guid, 16);
236
        size = get_le64(pb);
237
        if (size <= 24)
238
            return -1;
239
        if (!memcmp(guid, guid1, 16))
240
            return size;
241
        url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
242
    }
243
    return -1;
244
}
245

  
246
static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
247
{
248
    int64_t size;
249
    ByteIOContext *pb = s->pb;
250
    WAVContext *wav   = s->priv_data;
251
    AVStream *st;
252
    uint8_t guid[16];
253

  
254
    get_buffer(pb, guid, 16);
255
    if (memcmp(guid, guid_riff, 16))
256
        return -1;
257

  
258
    if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
259
        return -1;
260

  
261
    get_buffer(pb, guid, 16);
262
    if (memcmp(guid, guid_wave, 16)) {
263
        av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
264
        return -1;
265
    }
266

  
267
    size = find_guid(pb, guid_fmt);
268
    if (size < 0) {
269
        av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
270
        return -1;
271
    }
272

  
273
    st = av_new_stream(s, 0);
274
    if (!st)
275
        return AVERROR(ENOMEM);
276

  
277
    /* subtract chunk header size - normal wav file doesn't count it */
278
    ff_get_wav_header(pb, st->codec, size - 24);
279
    url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
280

  
281
    st->need_parsing = AVSTREAM_PARSE_FULL;
282

  
283
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
284

  
285
    size = find_guid(pb, guid_data);
286
    if (size < 0) {
287
        av_log(s, AV_LOG_ERROR, "could not find data guid\n");
288
        return -1;
289
    }
290
    wav->data_end = url_ftell(pb) + size - 24;
291
    wav->w64 = 1;
292

  
293
    return 0;
294
}
295
#endif /* CONFIG_W64_DEMUXER */
296

  
197 297
#define MAX_SIZE 4096
198 298

  
199 299
static int wav_read_packet(AVFormatContext *s,
200 300
                           AVPacket *pkt)
201 301
{
202
    int ret, size, left;
302
    int ret, size;
303
    int64_t left;
203 304
    AVStream *st;
204 305
    WAVContext *wav = s->priv_data;
205 306

  
......
209 310

  
210 311
    left= wav->data_end - url_ftell(s->pb);
211 312
    if(left <= 0){
313
        if (CONFIG_W64_DEMUXER && wav->w64) {
314
            left = find_guid(s->pb, guid_data) - 24;
315
        } else
212 316
        left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
213 317
        if (left < 0) {
214 318
            return AVERROR(EIO);
......
282 386
    .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
283 387
};
284 388
#endif
389
#if CONFIG_W64_DEMUXER
390
AVInputFormat w64_demuxer = {
391
    "w64",
392
    NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
393
    sizeof(WAVContext),
394
    w64_probe,
395
    w64_read_header,
396
    wav_read_packet,
397
    NULL,
398
    wav_read_seek,
399
    .flags = AVFMT_GENERIC_INDEX,
400
    .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
401
};
402
#endif

Also available in: Unified diff