diff mbox series

[FFmpeg-devel,1/3] avcodec/dovi_rpu: implement support for profile 10

Message ID 20240225114451.27645-1-ffmpeg@haasn.xyz
State New
Headers show
Series [FFmpeg-devel,1/3] avcodec/dovi_rpu: implement support for profile 10 | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Niklas Haas Feb. 25, 2024, 11:44 a.m. UTC
From: Niklas Haas <git@haasn.dev>

Instead of the nal_prefix, this profile inside wraps the RPU inside an
EMDF header, as specified in ETSI TS 102 366. This particular usage is
supposedly specified in ETSI TS 103 572, at least according to European
Patent EP 3 588 964 A1, but I could not find any references to DV RPUs
in the former.

It's worth pointing out that the EMDF container is not byte-aligned,
meaning that payloads are delivered at arbitrary byte boundaries. Hence
the reason for doing it inside ff_dovi_rpu_parse, which already uses
a bitstream reader, rather than splitting off the container in
a separate stage. (Plus, we hard-code the DV-specific payload ID)

Magic values were taken from a combination of the sources below, all of
which agree about what the specific EMDF header should look like. In
fact, they all hard-code a very *specific* header sequence, but I wanted
to go the extra mile and at least properly skip the variable fields
- even though the non-existent Dolby Vision specification probably
specifies that they all must be 0. This is probably overkill.

Validated and tested using sample files from the merge request linked
below.

Relevant links:
- https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
- https://patentimages.storage.googleapis.com/8a/0b/da/28294acaed2182/EP3588964A1.pdf
- https://www.etsi.org/deliver/etsi_ts/103500_103599/103572/01.03.01_60/ts_103572v010301p.pdf
- https://github.com/rockchip-linux/mpp/blob/fdeb8c378b79d4b4ef80457e4431815de89dc417/mpp/codec/dec/av1/av1d_cbs.c#L2378
- https://github.com/elginsk8r/android_kernel_amlogic_linux-4.9/blob/23a4c38bf06ef34821e476a8edddbf9213712c8a/drivers/amlogic/media/enhancement/amdolby_vision/amdolby_vision.c#L5638
- https://gitlab.com/mbunkus/mkvtoolnix/-/merge_requests/2254
---
 libavcodec/dovi_rpu.c | 68 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 65 insertions(+), 3 deletions(-)

Comments

Niklas Haas March 4, 2024, 11:42 a.m. UTC | #1
On Sun, 25 Feb 2024 12:44:49 +0100 Niklas Haas <ffmpeg@haasn.xyz> wrote:
> From: Niklas Haas <git@haasn.dev>
> 
> Instead of the nal_prefix, this profile inside wraps the RPU inside an
> EMDF header, as specified in ETSI TS 102 366. This particular usage is
> supposedly specified in ETSI TS 103 572, at least according to European
> Patent EP 3 588 964 A1, but I could not find any references to DV RPUs
> in the former.
> 
> It's worth pointing out that the EMDF container is not byte-aligned,
> meaning that payloads are delivered at arbitrary byte boundaries. Hence
> the reason for doing it inside ff_dovi_rpu_parse, which already uses
> a bitstream reader, rather than splitting off the container in
> a separate stage. (Plus, we hard-code the DV-specific payload ID)
> 
> Magic values were taken from a combination of the sources below, all of
> which agree about what the specific EMDF header should look like. In
> fact, they all hard-code a very *specific* header sequence, but I wanted
> to go the extra mile and at least properly skip the variable fields
> - even though the non-existent Dolby Vision specification probably
> specifies that they all must be 0. This is probably overkill.
> 
> Validated and tested using sample files from the merge request linked
> below.
> 
> Relevant links:
> - https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
> - https://patentimages.storage.googleapis.com/8a/0b/da/28294acaed2182/EP3588964A1.pdf
> - https://www.etsi.org/deliver/etsi_ts/103500_103599/103572/01.03.01_60/ts_103572v010301p.pdf
> - https://github.com/rockchip-linux/mpp/blob/fdeb8c378b79d4b4ef80457e4431815de89dc417/mpp/codec/dec/av1/av1d_cbs.c#L2378
> - https://github.com/elginsk8r/android_kernel_amlogic_linux-4.9/blob/23a4c38bf06ef34821e476a8edddbf9213712c8a/drivers/amlogic/media/enhancement/amdolby_vision/amdolby_vision.c#L5638
> - https://gitlab.com/mbunkus/mkvtoolnix/-/merge_requests/2254

Ping. Is there anybody who could kindly review this?
Andreas Rheinhardt March 4, 2024, 1:06 p.m. UTC | #2
Niklas Haas:
> From: Niklas Haas <git@haasn.dev>
> 
> Instead of the nal_prefix, this profile inside wraps the RPU inside an
> EMDF header, as specified in ETSI TS 102 366. This particular usage is
> supposedly specified in ETSI TS 103 572, at least according to European
> Patent EP 3 588 964 A1, but I could not find any references to DV RPUs
> in the former.
> 
> It's worth pointing out that the EMDF container is not byte-aligned,
> meaning that payloads are delivered at arbitrary byte boundaries. Hence
> the reason for doing it inside ff_dovi_rpu_parse, which already uses
> a bitstream reader, rather than splitting off the container in
> a separate stage. (Plus, we hard-code the DV-specific payload ID)
> 
> Magic values were taken from a combination of the sources below, all of
> which agree about what the specific EMDF header should look like. In
> fact, they all hard-code a very *specific* header sequence, but I wanted
> to go the extra mile and at least properly skip the variable fields
> - even though the non-existent Dolby Vision specification probably
> specifies that they all must be 0. This is probably overkill.
> 
> Validated and tested using sample files from the merge request linked
> below.
> 
> Relevant links:
> - https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
> - https://patentimages.storage.googleapis.com/8a/0b/da/28294acaed2182/EP3588964A1.pdf
> - https://www.etsi.org/deliver/etsi_ts/103500_103599/103572/01.03.01_60/ts_103572v010301p.pdf
> - https://github.com/rockchip-linux/mpp/blob/fdeb8c378b79d4b4ef80457e4431815de89dc417/mpp/codec/dec/av1/av1d_cbs.c#L2378
> - https://github.com/elginsk8r/android_kernel_amlogic_linux-4.9/blob/23a4c38bf06ef34821e476a8edddbf9213712c8a/drivers/amlogic/media/enhancement/amdolby_vision/amdolby_vision.c#L5638
> - https://gitlab.com/mbunkus/mkvtoolnix/-/merge_requests/2254
> ---
>  libavcodec/dovi_rpu.c | 68 +++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 65 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
> index a6b23f4dd11..c7cdd65a2f2 100644
> --- a/libavcodec/dovi_rpu.c
> +++ b/libavcodec/dovi_rpu.c
> @@ -174,6 +174,18 @@ static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *
>      return 0; /* unreachable */
>  }
>  
> +static inline unsigned get_variable_bits(GetBitContext *gb, int n)
> +{
> +    unsigned int value = get_bits(gb, n);
> +    int read_more = get_bits1(gb);
> +    while (read_more) {
> +        value = (value + 1) << n;
> +        value += get_bits(gb, n);

Using |= is more appropriate.

> +        read_more = get_bits1(gb);
> +    };

The ';' is a null statement.

> +    return value;
> +}
> +
>  #define VALIDATE(VAR, MIN, MAX)                                                 \
>      do {                                                                        \
>          if (VAR < MIN || VAR > MAX) {                                           \
> @@ -200,9 +212,59 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
>      if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
>          return ret;
>  
> -    /* RPU header, common values */
> -    nal_prefix = get_bits(gb, 8);
> -    VALIDATE(nal_prefix, 25, 25);
> +    /* Container header */
> +    if (s->dv_profile == 10 /* dav1.10 */) {
> +        /* DV inside AV1 re-uses EMDF container, parse header */
> +        int emdf_version, key_id, emdf_payload_id, emdf_payload_size, smploffste;
> +        emdf_version = get_bits(gb, 2);
> +        if (emdf_version == 3)
> +            emdf_version += get_variable_bits(gb, 2);
> +        key_id = get_bits(gb, 3);
> +        if (key_id == 7)
> +            key_id += get_variable_bits(gb, 3);
> +        emdf_payload_id = get_bits(gb, 5);
> +        if (emdf_payload_id == 0x1F)
> +            emdf_payload_id += get_variable_bits(gb, 5);
> +
> +        /* Magic values taken from amlogic and rockchip drivers, and also
> +         * match values found in practice (and validated in hardware) */
> +        VALIDATE(key_id, 6, 6);
> +        VALIDATE(emdf_payload_id, 256, 256);
> +
> +        /* Note: In theory, there could be multiple payloads, and we should
> +         * skip over the unused payloads until we find the one we're interested
> +         * in. But this capability does not seem to be used in practice, so
> +         * the DV-specific usage of EMDF probably requires only one payload
> +         * per T.35 OBU */
> +
> +        /* Skip emdf_payload_config() */
> +        if ((smploffste = get_bits1(gb)))
> +            skip_bits(gb, 11 + 1);
> +        if (get_bits1(gb)) /* duratione */
> +            get_variable_bits(gb, 11);
> +        if (get_bits1(gb)) /* groupide */
> +            get_variable_bits(gb, 2);
> +        if (get_bits1(gb)) /* codecdatae */
> +            skip_bits(gb, 8);
> +        if (!get_bits1(gb)) { /* discard_unknown_payload */
> +            int payload_frame_aligned;
> +            if (!smploffste) {
> +                if ((payload_frame_aligned = get_bits1(gb)))
> +                    skip_bits(gb, 1 + 1);
> +            }
> +            if (smploffste || payload_frame_aligned)
> +                skip_bits(gb, 5 + 2);
> +        }
> +
> +        emdf_payload_size = get_variable_bits(gb, 8);
> +        if (emdf_payload_size * 8 > get_bits_left(gb))
> +            return AVERROR_INVALIDDATA;
> +    } else {
> +        nal_prefix = get_bits(gb, 8);
> +        VALIDATE(nal_prefix, 25, 25);
> +    }
> +
> +    /* RPU header */
>      rpu_type = get_bits(gb, 6);
>      if (rpu_type != 2) {
>          av_log(s->logctx, AV_LOG_WARNING, "Unrecognized RPU type "
Niklas Haas March 4, 2024, 1:23 p.m. UTC | #3
On Mon, 04 Mar 2024 14:06:51 +0100 Andreas Rheinhardt <andreas.rheinhardt@outlook.com> wrote:
> Niklas Haas:
> > From: Niklas Haas <git@haasn.dev>
> > 
> > Instead of the nal_prefix, this profile inside wraps the RPU inside an
> > EMDF header, as specified in ETSI TS 102 366. This particular usage is
> > supposedly specified in ETSI TS 103 572, at least according to European
> > Patent EP 3 588 964 A1, but I could not find any references to DV RPUs
> > in the former.
> > 
> > It's worth pointing out that the EMDF container is not byte-aligned,
> > meaning that payloads are delivered at arbitrary byte boundaries. Hence
> > the reason for doing it inside ff_dovi_rpu_parse, which already uses
> > a bitstream reader, rather than splitting off the container in
> > a separate stage. (Plus, we hard-code the DV-specific payload ID)
> > 
> > Magic values were taken from a combination of the sources below, all of
> > which agree about what the specific EMDF header should look like. In
> > fact, they all hard-code a very *specific* header sequence, but I wanted
> > to go the extra mile and at least properly skip the variable fields
> > - even though the non-existent Dolby Vision specification probably
> > specifies that they all must be 0. This is probably overkill.
> > 
> > Validated and tested using sample files from the merge request linked
> > below.
> > 
> > Relevant links:
> > - https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
> > - https://patentimages.storage.googleapis.com/8a/0b/da/28294acaed2182/EP3588964A1.pdf
> > - https://www.etsi.org/deliver/etsi_ts/103500_103599/103572/01.03.01_60/ts_103572v010301p.pdf
> > - https://github.com/rockchip-linux/mpp/blob/fdeb8c378b79d4b4ef80457e4431815de89dc417/mpp/codec/dec/av1/av1d_cbs.c#L2378
> > - https://github.com/elginsk8r/android_kernel_amlogic_linux-4.9/blob/23a4c38bf06ef34821e476a8edddbf9213712c8a/drivers/amlogic/media/enhancement/amdolby_vision/amdolby_vision.c#L5638
> > - https://gitlab.com/mbunkus/mkvtoolnix/-/merge_requests/2254
> > ---
> >  libavcodec/dovi_rpu.c | 68 +++++++++++++++++++++++++++++++++++++++++--
> >  1 file changed, 65 insertions(+), 3 deletions(-)
> > 
> > diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
> > index a6b23f4dd11..c7cdd65a2f2 100644
> > --- a/libavcodec/dovi_rpu.c
> > +++ b/libavcodec/dovi_rpu.c
> > @@ -174,6 +174,18 @@ static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *
> >      return 0; /* unreachable */
> >  }
> >  
> > +static inline unsigned get_variable_bits(GetBitContext *gb, int n)
> > +{
> > +    unsigned int value = get_bits(gb, n);
> > +    int read_more = get_bits1(gb);
> > +    while (read_more) {
> > +        value = (value + 1) << n;
> > +        value += get_bits(gb, n);
> 
> Using |= is more appropriate.
> 
> > +        read_more = get_bits1(gb);
> > +    };
> 
> The ';' is a null statement.

Fixed both, thanks.
Niklas Haas March 7, 2024, 8:10 p.m. UTC | #4
Will merge tomorrow without further comments.
diff mbox series

Patch

diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
index a6b23f4dd11..c7cdd65a2f2 100644
--- a/libavcodec/dovi_rpu.c
+++ b/libavcodec/dovi_rpu.c
@@ -174,6 +174,18 @@  static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *
     return 0; /* unreachable */
 }
 
+static inline unsigned get_variable_bits(GetBitContext *gb, int n)
+{
+    unsigned int value = get_bits(gb, n);
+    int read_more = get_bits1(gb);
+    while (read_more) {
+        value = (value + 1) << n;
+        value += get_bits(gb, n);
+        read_more = get_bits1(gb);
+    };
+    return value;
+}
+
 #define VALIDATE(VAR, MIN, MAX)                                                 \
     do {                                                                        \
         if (VAR < MIN || VAR > MAX) {                                           \
@@ -200,9 +212,59 @@  int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
     if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
         return ret;
 
-    /* RPU header, common values */
-    nal_prefix = get_bits(gb, 8);
-    VALIDATE(nal_prefix, 25, 25);
+    /* Container header */
+    if (s->dv_profile == 10 /* dav1.10 */) {
+        /* DV inside AV1 re-uses EMDF container, parse header */
+        int emdf_version, key_id, emdf_payload_id, emdf_payload_size, smploffste;
+        emdf_version = get_bits(gb, 2);
+        if (emdf_version == 3)
+            emdf_version += get_variable_bits(gb, 2);
+        key_id = get_bits(gb, 3);
+        if (key_id == 7)
+            key_id += get_variable_bits(gb, 3);
+        emdf_payload_id = get_bits(gb, 5);
+        if (emdf_payload_id == 0x1F)
+            emdf_payload_id += get_variable_bits(gb, 5);
+
+        /* Magic values taken from amlogic and rockchip drivers, and also
+         * match values found in practice (and validated in hardware) */
+        VALIDATE(key_id, 6, 6);
+        VALIDATE(emdf_payload_id, 256, 256);
+
+        /* Note: In theory, there could be multiple payloads, and we should
+         * skip over the unused payloads until we find the one we're interested
+         * in. But this capability does not seem to be used in practice, so
+         * the DV-specific usage of EMDF probably requires only one payload
+         * per T.35 OBU */
+
+        /* Skip emdf_payload_config() */
+        if ((smploffste = get_bits1(gb)))
+            skip_bits(gb, 11 + 1);
+        if (get_bits1(gb)) /* duratione */
+            get_variable_bits(gb, 11);
+        if (get_bits1(gb)) /* groupide */
+            get_variable_bits(gb, 2);
+        if (get_bits1(gb)) /* codecdatae */
+            skip_bits(gb, 8);
+        if (!get_bits1(gb)) { /* discard_unknown_payload */
+            int payload_frame_aligned;
+            if (!smploffste) {
+                if ((payload_frame_aligned = get_bits1(gb)))
+                    skip_bits(gb, 1 + 1);
+            }
+            if (smploffste || payload_frame_aligned)
+                skip_bits(gb, 5 + 2);
+        }
+
+        emdf_payload_size = get_variable_bits(gb, 8);
+        if (emdf_payload_size * 8 > get_bits_left(gb))
+            return AVERROR_INVALIDDATA;
+    } else {
+        nal_prefix = get_bits(gb, 8);
+        VALIDATE(nal_prefix, 25, 25);
+    }
+
+    /* RPU header */
     rpu_type = get_bits(gb, 6);
     if (rpu_type != 2) {
         av_log(s->logctx, AV_LOG_WARNING, "Unrecognized RPU type "