diff options
author | Daniel Verkamp <daniel@drv.nu> | 2013-01-23 01:30:23 -0700 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2013-02-12 14:50:27 +0000 |
commit | 1ecce266fa5b6e7b8006744ee73ca38d2f594726 (patch) | |
tree | f278bac59d824284b1f7cc7f2aa5393ed226880b /libavformat/wavenc.c | |
parent | 0957041221fa6d883c4ff245f2f3a452cca55b3f (diff) | |
download | ffmpeg-1ecce266fa5b6e7b8006744ee73ca38d2f594726.tar.gz |
WAV muxer: add RF64 support
Diffstat (limited to 'libavformat/wavenc.c')
-rw-r--r-- | libavformat/wavenc.c | 73 |
1 files changed, 67 insertions, 6 deletions
diff --git a/libavformat/wavenc.c b/libavformat/wavenc.c index 2e80a75b6e..ba902c9024 100644 --- a/libavformat/wavenc.c +++ b/libavformat/wavenc.c @@ -5,6 +5,9 @@ * Sony Wave64 muxer * Copyright (c) 2012 Paul B Mahol * + * WAV muxer RF64 support + * Copyright (c) 2013 Daniel Verkamp <daniel@drv.nu> + * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or @@ -36,14 +39,20 @@ #include "internal.h" #include "riff.h" +#define RF64_AUTO (-1) +#define RF64_NEVER 0 +#define RF64_ALWAYS 1 + typedef struct WAVMuxContext { const AVClass *class; int64_t data; int64_t fact_pos; + int64_t ds64; int64_t minpts; int64_t maxpts; int last_duration; int write_bext; + int rf64; } WAVMuxContext; #if CONFIG_WAV_MUXER @@ -107,10 +116,24 @@ static int wav_write_header(AVFormatContext *s) AVIOContext *pb = s->pb; int64_t fmt; + if (wav->rf64 == RF64_ALWAYS) { + ffio_wfourcc(pb, "RF64"); + avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */ + } else { ffio_wfourcc(pb, "RIFF"); avio_wl32(pb, 0); /* file length */ + } + ffio_wfourcc(pb, "WAVE"); + if (wav->rf64 != RF64_NEVER) { + /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */ + ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK"); + avio_wl32(pb, 28); /* chunk size */ + wav->ds64 = avio_tell(pb); + ffio_fill(pb, 0, 28); + } + /* format header */ fmt = ff_start_tag(pb, "fmt "); if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) { @@ -163,31 +186,65 @@ static int wav_write_trailer(AVFormatContext *s) { AVIOContext *pb = s->pb; WAVMuxContext *wav = s->priv_data; - int64_t file_size; + int64_t file_size, data_size; + int64_t number_of_samples = 0; + int rf64 = 0; avio_flush(pb); if (s->pb->seekable) { - ff_end_tag(pb, wav->data); - /* update file size */ file_size = avio_tell(pb); + data_size = file_size - wav->data; + if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) { + rf64 = 1; + } else { avio_seek(pb, 4, SEEK_SET); avio_wl32(pb, (uint32_t)(file_size - 8)); avio_seek(pb, file_size, SEEK_SET); + ff_end_tag(pb, wav->data); avio_flush(pb); + } - 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); + + if(s->streams[0]->codec->codec_tag != 0x01) { + /* Update num_samps in fact chunk */ avio_seek(pb, wav->fact_pos, SEEK_SET); + if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) { + rf64 = 1; + avio_wl32(pb, -1); + } else { avio_wl32(pb, number_of_samples); avio_seek(pb, file_size, SEEK_SET); avio_flush(pb); + } + } + + if (rf64) { + /* overwrite RIFF with RF64 */ + avio_seek(pb, 0, SEEK_SET); + ffio_wfourcc(pb, "RF64"); + avio_wl32(pb, -1); + + /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */ + avio_seek(pb, wav->ds64 - 8, SEEK_SET); + ffio_wfourcc(pb, "ds64"); + avio_wl32(pb, 28); /* ds64 chunk size */ + avio_wl64(pb, file_size - 8); /* RF64 chunk size */ + avio_wl64(pb, data_size); /* data chunk size */ + avio_wl64(pb, number_of_samples); /* fact chunk number of samples */ + avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */ + + /* write -1 in data chunk size */ + avio_seek(pb, wav->data - 4, SEEK_SET); + avio_wl32(pb, -1); + + avio_seek(pb, file_size, SEEK_SET); + avio_flush(pb); } } return 0; @@ -197,6 +254,10 @@ static int wav_write_trailer(AVFormatContext *s) #define ENC AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC }, + { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" }, + { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" }, + { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" }, + { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" }, { NULL }, }; |