mirror of
https://mirror.skon.top/https://github.com/FFmpeg/FFmpeg
synced 2026-04-20 21:00:41 +08:00
avformat/wavenc: Keep fmt chunk first for -rf64 auto
When the WAV muxer's `-rf64 auto` option is used, the output is intended to be a normal WAV file if possible, only extended to RF64 format when the file size grows too large. This was accomplished by reserving space for the extra RF64-specific data using a standard JUNK chunk (ignored by readers), then overwriting the reserved space later with a ds64 chunk if needed. In the original rf64 auto implementation, the JUNK chunk was placed right after the RIFF/WAVE file header, before the fmt chunk; this is the design suggested by the "Achieving compatibility between BWF and RF64" section of the RF64 spec: RIFF 'WAVE' <JUNK chunk> <fmt-ck> ... However, this approach means that the fmt chunk is no longer in its conventional location at the beginning of the file, and some WAV-reading tools are confused by this layout. For example, the `file` tool is not able to show the format information for a file with the extra JUNK chunk before fmt. This change shuffles the order of the chunks for `-rf64 auto` mode so that the reserved space follows fmt instead of preceding it: RIFF 'WAVE' <fmt-ck> <JUNK chunk> ... With this small modification, tools expecting the fmt chunk to be the first chunk in the file work with files produced by `-rf64 auto`. This means the fmt chunk won't be in the location required by RF64, so if the automatic RF64 conversion is triggered, the fmt chunk needs to be relocated by rewriting it following the ds64 chunk during the conversion: RF64 'WAVE' <ds64 chunk> <fmt-ck> ...
This commit is contained in:
committed by
michaelni
parent
39f34ee019
commit
8eae5de5af
@@ -315,9 +315,9 @@ static int wav_write_header(AVFormatContext *s)
|
|||||||
|
|
||||||
ffio_wfourcc(pb, "WAVE");
|
ffio_wfourcc(pb, "WAVE");
|
||||||
|
|
||||||
if (wav->rf64 != RF64_NEVER) {
|
if (wav->rf64 == RF64_ALWAYS) {
|
||||||
/* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
|
/* write empty ds64 chunk */
|
||||||
ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
|
ffio_wfourcc(pb, "ds64");
|
||||||
avio_wl32(pb, 28); /* chunk size */
|
avio_wl32(pb, 28); /* chunk size */
|
||||||
wav->ds64 = avio_tell(pb);
|
wav->ds64 = avio_tell(pb);
|
||||||
ffio_fill(pb, 0, 28);
|
ffio_fill(pb, 0, 28);
|
||||||
@@ -332,6 +332,16 @@ static int wav_write_header(AVFormatContext *s)
|
|||||||
return AVERROR(ENOSYS);
|
return AVERROR(ENOSYS);
|
||||||
}
|
}
|
||||||
ff_end_tag(pb, fmt);
|
ff_end_tag(pb, fmt);
|
||||||
|
|
||||||
|
if (wav->rf64 == RF64_AUTO) {
|
||||||
|
/* reserve space for ds64 */
|
||||||
|
ffio_wfourcc(pb, "JUNK");
|
||||||
|
avio_wl32(pb, 28); /* chunk size */
|
||||||
|
ffio_fill(pb, 0, 28);
|
||||||
|
|
||||||
|
/* in RF64_AUTO mode, fmt + JUNK will be overwritten by ds64 + fmt */
|
||||||
|
wav->ds64 = fmt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
|
if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
|
||||||
@@ -411,6 +421,7 @@ static int wav_write_trailer(AVFormatContext *s)
|
|||||||
WAVMuxContext *wav = s->priv_data;
|
WAVMuxContext *wav = s->priv_data;
|
||||||
int64_t file_size, data_size;
|
int64_t file_size, data_size;
|
||||||
int64_t number_of_samples = 0;
|
int64_t number_of_samples = 0;
|
||||||
|
int64_t pos;
|
||||||
int rf64 = 0;
|
int rf64 = 0;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
@@ -459,7 +470,7 @@ static int wav_write_trailer(AVFormatContext *s)
|
|||||||
ffio_wfourcc(pb, "RF64");
|
ffio_wfourcc(pb, "RF64");
|
||||||
avio_wl32(pb, -1);
|
avio_wl32(pb, -1);
|
||||||
|
|
||||||
/* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
|
/* write ds64 chunk (overwrite fmt + JUNK if rf64 == RF64_AUTO) */
|
||||||
avio_seek(pb, wav->ds64 - 8, SEEK_SET);
|
avio_seek(pb, wav->ds64 - 8, SEEK_SET);
|
||||||
ffio_wfourcc(pb, "ds64");
|
ffio_wfourcc(pb, "ds64");
|
||||||
avio_wl32(pb, 28); /* ds64 chunk size */
|
avio_wl32(pb, 28); /* ds64 chunk size */
|
||||||
@@ -468,6 +479,11 @@ static int wav_write_trailer(AVFormatContext *s)
|
|||||||
avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
|
avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
|
||||||
avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
|
avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
|
||||||
|
|
||||||
|
/* rewrite fmt in its RF64 position after ds64 */
|
||||||
|
pos = ff_start_tag(pb, "fmt ");
|
||||||
|
ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0);
|
||||||
|
ff_end_tag(pb, pos);
|
||||||
|
|
||||||
/* write -1 in data chunk size */
|
/* write -1 in data chunk size */
|
||||||
avio_seek(pb, wav->data - 4, SEEK_SET);
|
avio_seek(pb, wav->data - 4, SEEK_SET);
|
||||||
avio_wl32(pb, -1);
|
avio_wl32(pb, -1);
|
||||||
|
|||||||
Reference in New Issue
Block a user