Commit 1ecce266 authored by Daniel Verkamp's avatar Daniel Verkamp Committed by Paul B Mahol

WAV muxer: add RF64 support

parent 09570412
......@@ -17,6 +17,7 @@ version <next>:
- tee muxer
- il filter ported from libmpcodecs
- support ID3v2 tags in ASF files
- RF64 support in WAV muxer
version 1.1:
......
......@@ -30,7 +30,7 @@
#include "libavutil/avutil.h"
#define LIBAVFORMAT_VERSION_MAJOR 54
#define LIBAVFORMAT_VERSION_MINOR 62
#define LIBAVFORMAT_VERSION_MINOR 63
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
......
......@@ -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,33 +186,67 @@ 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 },
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment