Message ID | b8e9700eb46a47361586caea2c04b74da3461c37.camel@haerdin.se |
---|---|
State | New |
Headers | show |
Series | [FFmpeg-devel,1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry | expand |
Context | Check | Description |
---|---|---|
yinshiyou/configure_loongarch64 | warning | Failed to apply patch |
andriy/configure_x86 | warning | Failed to apply patch |
Hi On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote: > [...] > + > +static int apc_write_header(AVFormatContext *s) > +{ > + AVIOContext *pb = s->pb; > + AVCodecParameters *par; > + AVStream *st; > + > + if (s->nb_streams != 1) { > + av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n"); > + return AVERROR(EINVAL); > + } > + > + st = s->streams[0]; > + par = st->codecpar; > + > + if (par->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels > 2) { > + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); > + return AVERROR(EINVAL); > + } > + > + if (par->extradata_size != 0 && par->extradata_size != 8) { > + av_log(s, AV_LOG_ERROR, > + "Must have exactly 0 or 8 bytes of extradata, got %i\n", > + par->extradata_size); > + return AVERROR(EINVAL); > + } > + > + ffio_wfourcc(pb, "CRYO"); > + ffio_wfourcc(pb, "_APC"); > + ffio_wfourcc(pb, "1.20"); > + avio_wl32(pb, 0); // number or samples please add to the comment "updated in apc_write_trailer()" > + avio_wl32(pb, par->sample_rate); > + > + // write extradata if we have it (remuxing) > + // else write dummy values and wait for AV_PKT_DATA_NEW_EXTRADATA (encoding) > + if (par->extradata_size) { > + avio_write(pb, par->extradata, par->extradata_size); > + } else { > + avio_wl64(pb, 0); > + } > + > + avio_wl32(pb, par->ch_layout.nb_channels - 1); > + avpriv_set_pts_info(st, 64, 1, par->sample_rate); > + return 0; > +} > + > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) > +{ > + size_t extradata_size = 0; > + const uint8_t *extradata = av_packet_get_side_data( > + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); > + > + if (extradata_size == 8) { > + // we got predictors from encoder > + // try to seek back end write them > + int64_t pos = avio_tell(s->pb), err; > + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { > + avio_write(s->pb, extradata, extradata_size); > + avio_seek(s->pb, pos, SEEK_SET); > + } else { > + av_log(s, AV_LOG_ERROR, "Got predictors from encoder but couldn't seek back to write them\n"); > + // fail since we should always be able to do this within the avio cache > + // unless the encoder gave us predictors way too late for some reason > + return err; > + } > + } I think the encoder should buffer data or use 2 passes if it needs future data. seeking back in the muxer is a bit odd. if it becomes available always in teh first packet then maybe the whole header could be written in the first packet I would suggest, you add APC support to nut. Thats a good test to ensure the packet and extradata is set at the right time for a generic muxer > + > + avio_write(s->pb, pkt->data, pkt->size); > + return 0; > +} > + > +static int apc_write_trailer(AVFormatContext *s) > +{ > + int64_t file_size = avio_tell(s->pb); > + > + // write length, if we're able to seek back > + if (avio_seek(s->pb, 12, SEEK_SET) >= 0) { > + if (file_size - APC_HEADER_SIZE > > + UINT32_MAX * s->streams[0]->codecpar->ch_layout.nb_channels / 2) { > + av_log(s, AV_LOG_ERROR, "File too large\n"); > + return AVERROR(EINVAL); > + } > + > + avio_wl32(s->pb, (file_size - APC_HEADER_SIZE) * > + 2 / s->streams[0]->codecpar->ch_layout.nb_channels); I think something like this is slightly cleaner: int64_t number_of_samples = (file_size - APC_HEADER_SIZE) * 2 / s->streams[0]->codecpar->ch_layout.nb_channels; if (number_of_samples > UINT32_MAX) ... [...] thx -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Avoid a single point of failure, be that a person or equipment.
tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer: > Hi > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote: > > > > [...] > > + > > +static int apc_write_header(AVFormatContext *s) > > +{ > > + AVIOContext *pb = s->pb; > > + AVCodecParameters *par; > > + AVStream *st; > > + > > + if (s->nb_streams != 1) { > > + av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n"); > > + return AVERROR(EINVAL); > > + } > > + > > + st = s->streams[0]; > > + par = st->codecpar; > > + > > + if (par->ch_layout.nb_channels <= 0 || par- > > >ch_layout.nb_channels > 2) { > > + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); > > + return AVERROR(EINVAL); > > + } > > + > > + if (par->extradata_size != 0 && par->extradata_size != 8) { > > + av_log(s, AV_LOG_ERROR, > > + "Must have exactly 0 or 8 bytes of extradata, got > > %i\n", > > + par->extradata_size); > > + return AVERROR(EINVAL); > > + } > > + > > + ffio_wfourcc(pb, "CRYO"); > > + ffio_wfourcc(pb, "_APC"); > > + ffio_wfourcc(pb, "1.20"); > > > + avio_wl32(pb, 0); // number or samples > > please add to the comment "updated in apc_write_trailer()" Sure > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) > > +{ > > + size_t extradata_size = 0; > > > + const uint8_t *extradata = av_packet_get_side_data( > > + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); > > + > > + if (extradata_size == 8) { > > + // we got predictors from encoder > > + // try to seek back end write them > > + int64_t pos = avio_tell(s->pb), err; > > + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { > > + avio_write(s->pb, extradata, extradata_size); > > + avio_seek(s->pb, pos, SEEK_SET); > > + } else { > > + av_log(s, AV_LOG_ERROR, "Got predictors from encoder > > but couldn't seek back to write them\n"); > > + // fail since we should always be able to do this > > within the avio cache > > + // unless the encoder gave us predictors way too late > > for some reason > > + return err; > > + } > > + } > > I think the encoder should buffer data or use 2 passes > if it needs future data. seeking back in the muxer is a bit odd. > > if it becomes available always in teh first packet then maybe the > whole header could be written in the first packet Good idea. IIRC the issue was that extradata is either given immediately (remuxing) or after a single-packet delay (encoding), hence the check for *new* metadata > I would suggest, you add APC support to nut. > Thats a good test to ensure the packet and extradata is set at the > right time > for a generic muxer How would I go about doing that? > > +static int apc_write_trailer(AVFormatContext *s) > > +{ > > + int64_t file_size = avio_tell(s->pb); > > + > > + // write length, if we're able to seek back > > + if (avio_seek(s->pb, 12, SEEK_SET) >= 0) { > > > + if (file_size - APC_HEADER_SIZE > > > + UINT32_MAX * s->streams[0]->codecpar- > > >ch_layout.nb_channels / 2) { > > + av_log(s, AV_LOG_ERROR, "File too large\n"); > > + return AVERROR(EINVAL); > > + } > > + > > + avio_wl32(s->pb, (file_size - APC_HEADER_SIZE) * > > + 2 / s->streams[0]->codecpar- > > >ch_layout.nb_channels); > > I think something like this is slightly cleaner: > > > int64_t number_of_samples = (file_size - APC_HEADER_SIZE) * > 2 / s->streams[0]->codecpar- > >ch_layout.nb_channels; This will UB when file_size is a bit above INT64_MAX / 2. But yeah it can probably be made prettier. I'll have a think about it /Tomas
Updated patch attached. I reordered the patch series a bit also, will post the entire series once this patch looks good enough /Tomas
tor 2023-12-28 klockan 18:23 +0100 skrev Tomas Härdin: > +static int apc_write_trailer(AVFormatContext *s) > +{ > + int64_t file_size = avio_tell(s->pb); > + > + // write length, if we're able to seek back > + if (avio_seek(s->pb, 12, SEEK_SET) >= 0) { > + int64_t data_size = file_size -= APC_HEADER_SIZE; I realized a small bug right after posting: data_size can become negative if the file is closed immediately after opening. Will fix this when reposting the series /Tomas
On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote: > tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer: > > Hi > > > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote: > > > > > > > [...] > > > + > > > +static int apc_write_header(AVFormatContext *s) > > > +{ > > > + AVIOContext *pb = s->pb; > > > + AVCodecParameters *par; > > > + AVStream *st; > > > + > > > + if (s->nb_streams != 1) { > > > + av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n"); > > > + return AVERROR(EINVAL); > > > + } > > > + > > > + st = s->streams[0]; > > > + par = st->codecpar; > > > + > > > + if (par->ch_layout.nb_channels <= 0 || par- > > > >ch_layout.nb_channels > 2) { > > > + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); > > > + return AVERROR(EINVAL); > > > + } > > > + > > > + if (par->extradata_size != 0 && par->extradata_size != 8) { > > > + av_log(s, AV_LOG_ERROR, > > > + "Must have exactly 0 or 8 bytes of extradata, got > > > %i\n", > > > + par->extradata_size); > > > + return AVERROR(EINVAL); > > > + } > > > + > > > + ffio_wfourcc(pb, "CRYO"); > > > + ffio_wfourcc(pb, "_APC"); > > > + ffio_wfourcc(pb, "1.20"); > > > > > + avio_wl32(pb, 0); // number or samples > > > > please add to the comment "updated in apc_write_trailer()" > > Sure > > > > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) > > > +{ > > > + size_t extradata_size = 0; > > > > > + const uint8_t *extradata = av_packet_get_side_data( > > > + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); > > > + > > > + if (extradata_size == 8) { > > > + // we got predictors from encoder > > > + // try to seek back end write them > > > + int64_t pos = avio_tell(s->pb), err; > > > + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { > > > + avio_write(s->pb, extradata, extradata_size); > > > + avio_seek(s->pb, pos, SEEK_SET); > > > + } else { > > > + av_log(s, AV_LOG_ERROR, "Got predictors from encoder > > > but couldn't seek back to write them\n"); > > > + // fail since we should always be able to do this > > > within the avio cache > > > + // unless the encoder gave us predictors way too late > > > for some reason > > > + return err; > > > + } > > > + } > > > > I think the encoder should buffer data or use 2 passes > > if it needs future data. seeking back in the muxer is a bit odd. > > > > if it becomes available always in teh first packet then maybe the > > whole header could be written in the first packet > > Good idea. IIRC the issue was that extradata is either given > immediately (remuxing) or after a single-packet delay (encoding), hence > the check for *new* metadata > > > I would suggest, you add APC support to nut. > > Thats a good test to ensure the packet and extradata is set at the > > right time > > for a generic muxer > > How would I go about doing that? nut mainly needs to be able to map the ffmpeg API CODEC_ID to some external id so the codec id needs to be in one of the table nut scans for it thx [...]
fre 2023-12-29 klockan 18:33 +0100 skrev Michael Niedermayer: > On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote: > > tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer: > > > Hi > > > > > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote: > > > > > > > > > > [...] > > > > + > > > > +static int apc_write_header(AVFormatContext *s) > > > > +{ > > > > + AVIOContext *pb = s->pb; > > > > + AVCodecParameters *par; > > > > + AVStream *st; > > > > + > > > > + if (s->nb_streams != 1) { > > > > + av_log(s, AV_LOG_ERROR, "Must have exactly one > > > > stream\n"); > > > > + return AVERROR(EINVAL); > > > > + } > > > > + > > > > + st = s->streams[0]; > > > > + par = st->codecpar; > > > > + > > > > + if (par->ch_layout.nb_channels <= 0 || par- > > > > > ch_layout.nb_channels > 2) { > > > > + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); > > > > + return AVERROR(EINVAL); > > > > + } > > > > + > > > > + if (par->extradata_size != 0 && par->extradata_size != 8) > > > > { > > > > + av_log(s, AV_LOG_ERROR, > > > > + "Must have exactly 0 or 8 bytes of extradata, got > > > > %i\n", > > > > + par->extradata_size); > > > > + return AVERROR(EINVAL); > > > > + } > > > > + > > > > + ffio_wfourcc(pb, "CRYO"); > > > > + ffio_wfourcc(pb, "_APC"); > > > > + ffio_wfourcc(pb, "1.20"); > > > > > > > + avio_wl32(pb, 0); // number or samples > > > > > > please add to the comment "updated in apc_write_trailer()" > > > > Sure > > > > > > > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) > > > > +{ > > > > + size_t extradata_size = 0; > > > > > > > + const uint8_t *extradata = av_packet_get_side_data( > > > > + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); > > > > + > > > > + if (extradata_size == 8) { > > > > + // we got predictors from encoder > > > > + // try to seek back end write them > > > > + int64_t pos = avio_tell(s->pb), err; > > > > + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { > > > > + avio_write(s->pb, extradata, extradata_size); > > > > + avio_seek(s->pb, pos, SEEK_SET); > > > > + } else { > > > > + av_log(s, AV_LOG_ERROR, "Got predictors from > > > > encoder > > > > but couldn't seek back to write them\n"); > > > > + // fail since we should always be able to do this > > > > within the avio cache > > > > + // unless the encoder gave us predictors way too > > > > late > > > > for some reason > > > > + return err; > > > > + } > > > > + } > > > > > > I think the encoder should buffer data or use 2 passes > > > if it needs future data. seeking back in the muxer is a bit odd. > > > > > > if it becomes available always in teh first packet then maybe the > > > whole header could be written in the first packet > > > > Good idea. IIRC the issue was that extradata is either given > > immediately (remuxing) or after a single-packet delay (encoding), > > hence > > the check for *new* metadata > > > > > I would suggest, you add APC support to nut. > > > Thats a good test to ensure the packet and extradata is set at > > > the > > > right time > > > for a generic muxer > > > > How would I go about doing that? > > nut mainly needs to be able to map the ffmpeg API CODEC_ID to some > external id > so the codec id needs to be in one of the table nut scans for it I doubt this codec has an official RIFF TwoCC. I see some other IMA codecs in riff.c though. I suspect this would require coordinating with Microsoft, which I feel shouldn't hold up this patchset /Tomas
On Sat, Dec 30, 2023 at 06:01:25PM +0100, Tomas Härdin wrote: > fre 2023-12-29 klockan 18:33 +0100 skrev Michael Niedermayer: > > On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote: > > > tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer: > > > > Hi > > > > > > > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote: > > > > > > > > > > > > > [...] > > > > > + > > > > > +static int apc_write_header(AVFormatContext *s) > > > > > +{ > > > > > + AVIOContext *pb = s->pb; > > > > > + AVCodecParameters *par; > > > > > + AVStream *st; > > > > > + > > > > > + if (s->nb_streams != 1) { > > > > > + av_log(s, AV_LOG_ERROR, "Must have exactly one > > > > > stream\n"); > > > > > + return AVERROR(EINVAL); > > > > > + } > > > > > + > > > > > + st = s->streams[0]; > > > > > + par = st->codecpar; > > > > > + > > > > > + if (par->ch_layout.nb_channels <= 0 || par- > > > > > > ch_layout.nb_channels > 2) { > > > > > + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); > > > > > + return AVERROR(EINVAL); > > > > > + } > > > > > + > > > > > + if (par->extradata_size != 0 && par->extradata_size != 8) > > > > > { > > > > > + av_log(s, AV_LOG_ERROR, > > > > > + "Must have exactly 0 or 8 bytes of extradata, got > > > > > %i\n", > > > > > + par->extradata_size); > > > > > + return AVERROR(EINVAL); > > > > > + } > > > > > + > > > > > + ffio_wfourcc(pb, "CRYO"); > > > > > + ffio_wfourcc(pb, "_APC"); > > > > > + ffio_wfourcc(pb, "1.20"); > > > > > > > > > + avio_wl32(pb, 0); // number or samples > > > > > > > > please add to the comment "updated in apc_write_trailer()" > > > > > > Sure > > > > > > > > > > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) > > > > > +{ > > > > > + size_t extradata_size = 0; > > > > > > > > > + const uint8_t *extradata = av_packet_get_side_data( > > > > > + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); > > > > > + > > > > > + if (extradata_size == 8) { > > > > > + // we got predictors from encoder > > > > > + // try to seek back end write them > > > > > + int64_t pos = avio_tell(s->pb), err; > > > > > + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { > > > > > + avio_write(s->pb, extradata, extradata_size); > > > > > + avio_seek(s->pb, pos, SEEK_SET); > > > > > + } else { > > > > > + av_log(s, AV_LOG_ERROR, "Got predictors from > > > > > encoder > > > > > but couldn't seek back to write them\n"); > > > > > + // fail since we should always be able to do this > > > > > within the avio cache > > > > > + // unless the encoder gave us predictors way too > > > > > late > > > > > for some reason > > > > > + return err; > > > > > + } > > > > > + } > > > > > > > > I think the encoder should buffer data or use 2 passes > > > > if it needs future data. seeking back in the muxer is a bit odd. > > > > > > > > if it becomes available always in teh first packet then maybe the > > > > whole header could be written in the first packet > > > > > > Good idea. IIRC the issue was that extradata is either given > > > immediately (remuxing) or after a single-packet delay (encoding), > > > hence > > > the check for *new* metadata > > > > > > > I would suggest, you add APC support to nut. > > > > Thats a good test to ensure the packet and extradata is set at > > > > the > > > > right time > > > > for a generic muxer > > > > > > How would I go about doing that? > > > > nut mainly needs to be able to map the ffmpeg API CODEC_ID to some > > external id > > so the codec id needs to be in one of the table nut scans for it > > I doubt this codec has an official RIFF TwoCC. I see some other IMA > codecs in riff.c though. I suspect this would require coordinating > with Microsoft, which I feel shouldn't hold up this patchset you only have to add it to: https://git.ffmpeg.org/gitweb/nut.git/blob/HEAD:/docs/nut4cc.txt https://lists.mplayerhq.hu/mailman/listinfo/nut-devel and please CC me in case this ML doesnt work (it seems a little silent) thx [...]
From 14f3dd40a49ebf5ea020465732511e9d79a2e14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se> Date: Tue, 26 Dec 2023 14:32:10 +0100 Subject: [PATCH 3/6] Add CRYO APC muxer --- Changelog | 1 + doc/general_contents.texi | 2 +- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/apcenc.c | 125 ++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 6 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 libavformat/apcenc.c diff --git a/Changelog b/Changelog index ca38546262..344bf4d1cf 100644 --- a/Changelog +++ b/Changelog @@ -3,6 +3,7 @@ releases are sorted from youngest to oldest. version <next>: - LEAD MCMP decoder +- CRYO APC muxer version 6.1: - libaribcaption decoder diff --git a/doc/general_contents.texi b/doc/general_contents.texi index 3328753acb..a43736e3a8 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -448,7 +448,7 @@ library: @item CRC testing format @tab X @tab @item Creative Voice @tab X @tab X @tab Created for the Sound Blaster Pro. -@item CRYO APC @tab @tab X +@item CRYO APC @tab X @tab X @tab Audio format used in some games by CRYO Interactive Entertainment. @item D-Cinema audio @tab X @tab X @item Deluxe Paint Animation @tab @tab X diff --git a/libavformat/Makefile b/libavformat/Makefile index 329055ccfd..d88a65d713 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -105,6 +105,7 @@ OBJS-$(CONFIG_AMV_MUXER) += amvenc.o OBJS-$(CONFIG_ANM_DEMUXER) += anm.o OBJS-$(CONFIG_APAC_DEMUXER) += apac.o rawdec.o OBJS-$(CONFIG_APC_DEMUXER) += apc.o +OBJS-$(CONFIG_APC_MUXER) += apcenc.o OBJS-$(CONFIG_APE_DEMUXER) += ape.o apetag.o img2.o OBJS-$(CONFIG_APM_DEMUXER) += apm.o OBJS-$(CONFIG_APM_MUXER) += apm.o rawenc.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index d4b505a5a3..ab74c2028f 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -60,6 +60,7 @@ extern const FFOutputFormat ff_amv_muxer; extern const AVInputFormat ff_anm_demuxer; extern const AVInputFormat ff_apac_demuxer; extern const AVInputFormat ff_apc_demuxer; +extern const FFOutputFormat ff_apc_muxer; extern const AVInputFormat ff_ape_demuxer; extern const AVInputFormat ff_apm_demuxer; extern const FFOutputFormat ff_apm_muxer; diff --git a/libavformat/apcenc.c b/libavformat/apcenc.c new file mode 100644 index 0000000000..ba024ead50 --- /dev/null +++ b/libavformat/apcenc.c @@ -0,0 +1,125 @@ +/* + * CRYO APC audio format muxer + * Copyright (c) 2023 Tomas Härdin <git@haerdin.se> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avio_internal.h" +#include "internal.h" +#include "mux.h" + +#define APC_HEADER_SIZE (8*4) + +static int apc_write_header(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AVCodecParameters *par; + AVStream *st; + + if (s->nb_streams != 1) { + av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n"); + return AVERROR(EINVAL); + } + + st = s->streams[0]; + par = st->codecpar; + + if (par->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels > 2) { + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); + return AVERROR(EINVAL); + } + + if (par->extradata_size != 0 && par->extradata_size != 8) { + av_log(s, AV_LOG_ERROR, + "Must have exactly 0 or 8 bytes of extradata, got %i\n", + par->extradata_size); + return AVERROR(EINVAL); + } + + ffio_wfourcc(pb, "CRYO"); + ffio_wfourcc(pb, "_APC"); + ffio_wfourcc(pb, "1.20"); + avio_wl32(pb, 0); // number or samples + avio_wl32(pb, par->sample_rate); + + // write extradata if we have it (remuxing) + // else write dummy values and wait for AV_PKT_DATA_NEW_EXTRADATA (encoding) + if (par->extradata_size) { + avio_write(pb, par->extradata, par->extradata_size); + } else { + avio_wl64(pb, 0); + } + + avio_wl32(pb, par->ch_layout.nb_channels - 1); + avpriv_set_pts_info(st, 64, 1, par->sample_rate); + return 0; +} + +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + size_t extradata_size = 0; + const uint8_t *extradata = av_packet_get_side_data( + pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size); + + if (extradata_size == 8) { + // we got predictors from encoder + // try to seek back end write them + int64_t pos = avio_tell(s->pb), err; + if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) { + avio_write(s->pb, extradata, extradata_size); + avio_seek(s->pb, pos, SEEK_SET); + } else { + av_log(s, AV_LOG_ERROR, "Got predictors from encoder but couldn't seek back to write them\n"); + // fail since we should always be able to do this within the avio cache + // unless the encoder gave us predictors way too late for some reason + return err; + } + } + + avio_write(s->pb, pkt->data, pkt->size); + return 0; +} + +static int apc_write_trailer(AVFormatContext *s) +{ + int64_t file_size = avio_tell(s->pb); + + // write length, if we're able to seek back + if (avio_seek(s->pb, 12, SEEK_SET) >= 0) { + if (file_size - APC_HEADER_SIZE > + UINT32_MAX * s->streams[0]->codecpar->ch_layout.nb_channels / 2) { + av_log(s, AV_LOG_ERROR, "File too large\n"); + return AVERROR(EINVAL); + } + + avio_wl32(s->pb, (file_size - APC_HEADER_SIZE) * + 2 / s->streams[0]->codecpar->ch_layout.nb_channels); + } + return 0; +} + +const FFOutputFormat ff_apc_muxer = { + .p.name = "apc", + .p.long_name = NULL_IF_CONFIG_SMALL("CRYO APC"), + .p.extensions = "apc", + .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_APC, + .write_header = apc_write_header, + .write_packet = apc_write_packet, + .write_trailer = apc_write_trailer, +}; + diff --git a/libavformat/version.h b/libavformat/version.h index 2a28a3bf40..6a80f3ac4e 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 17 +#define LIBAVFORMAT_VERSION_MINOR 18 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- 2.39.2