From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTP id AD58047900
	for <ffmpegdev@gitmailbox.com>; Tue, 26 Dec 2023 22:30:59 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 035EF68CD0C;
	Wed, 27 Dec 2023 00:30:57 +0200 (EET)
Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net
 [217.70.183.201])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 14AA168CA49
 for <ffmpeg-devel@ffmpeg.org>; Wed, 27 Dec 2023 00:30:51 +0200 (EET)
Received: by mail.gandi.net (Postfix) with ESMTPSA id 59F311BF206
 for <ffmpeg-devel@ffmpeg.org>; Tue, 26 Dec 2023 22:30:50 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1703629850;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 in-reply-to:in-reply-to:references:references;
 bh=jqdlwn0ghgxj5hfHysejOso/qo1OBiNoBoKdJ+1w6Rk=;
 b=ScLO3t7exxsCfZP+Sk/aXR30mcqgnhchzWGdB+ngr3KP9Ak4PrreXg2X4uLDHqfvITtbdk
 gYfkkiXNaMKD68j88+LDnKK7TgjV7tm2taF2O+GAGwxjYNjF+KuYViZ+R1RGUx6U6a5a0J
 lu/cgY+mrSuDrYE+tjL4sOO5XW8iyxZ1kT40bIGh13bpBLxLs6jv7nFGhQ/gHsTI9zLE6M
 c5LPiKQVpBcvg8Hqd2uRMQoEXrhlU9ouLNEMW5PoB61cLIdKfmcVtJv2wpPY1B531cpQTb
 g9DXmQDJvFKco1QekqP03qK+Nu44/G/E6Xdo1Gm9ohLBIBnD90ugAZ7nDSSxRQ==
Date: Tue, 26 Dec 2023 23:30:49 +0100
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Message-ID: <20231226223049.GU6420@pb2>
References: <e8ff0c6cfe26a6559a3e1d987bde70072b7ddc4d.camel@haerdin.se>
 <b8e9700eb46a47361586caea2c04b74da3461c37.camel@haerdin.se>
MIME-Version: 1.0
In-Reply-To: <b8e9700eb46a47361586caea2c04b74da3461c37.camel@haerdin.se>
X-GND-Sasl: michael@niedermayer.cc
Subject: Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: multipart/mixed; boundary="===============1940004431572163355=="
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20231226223049.GU6420@pb2/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>


--===============1940004431572163355==
Content-Type: multipart/signed; micalg=pgp-sha256;
	protocol="application/pgp-signature"; boundary="95xjieIIiUQImiTX"
Content-Disposition: inline


--95xjieIIiUQImiTX
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi

On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas H=E4rdin wrote:
>=20

[...]
> +
> +static int apc_write_header(AVFormatContext *s)
> +{
> +    AVIOContext *pb =3D s->pb;
> +    AVCodecParameters *par;
> +    AVStream *st;
> +
> +    if (s->nb_streams !=3D 1) {
> +        av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    st =3D s->streams[0];
> +    par =3D st->codecpar;
> +
> +    if (par->ch_layout.nb_channels <=3D 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 !=3D 0 && par->extradata_size !=3D 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 (e=
ncoding)
> +    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 =3D 0;

> +    const uint8_t *extradata =3D av_packet_get_side_data(
> +            pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
> +
> +    if (extradata_size =3D=3D 8) {
> +        // we got predictors from encoder
> +        // try to seek back end write them
> +        int64_t pos =3D avio_tell(s->pb), err;
> +        if ((err =3D avio_seek(s->pb, 20, SEEK_SET)) >=3D 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 cou=
ldn'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 so=
me 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 ti=
me
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 =3D avio_tell(s->pb);
> +
> +    // write length, if we're able to seek back
> +    if (avio_seek(s->pb, 12, SEEK_SET) >=3D 0) {

> +        if (file_size - APC_HEADER_SIZE >
> +                UINT32_MAX * s->streams[0]->codecpar->ch_layout.nb_chann=
els / 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_ch=
annels);

I think something like this is slightly cleaner:


int64_t number_of_samples =3D (file_size - APC_HEADER_SIZE) *
                            2 / s->streams[0]->codecpar->ch_layout.nb_chann=
els;

if (number_of_samples > UINT32_MAX)
    ...
[...]

thx

--
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Avoid a single point of failure, be that a person or equipment.

--95xjieIIiUQImiTX
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iF0EABEIAB0WIQSf8hKLFH72cwut8TNhHseHBAsPqwUCZYtUEgAKCRBhHseHBAsP
q44/AKCVZQ4yWuk/8q9Al109ndDFtGdk0ACfUEP2XHwfKuvNyRsPoOgk/APMB6M=
=jnY3
-----END PGP SIGNATURE-----

--95xjieIIiUQImiTX--

--===============1940004431572163355==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

--===============1940004431572163355==--