diff mbox series

[FFmpeg-devel,v10,1/5] avformat/dovi_isom: Implement Dolby Vision configuration parsing/writing

Message ID 20211214153001.2610297-2-tcChlisop0@gmail.com
State New
Headers show
Series Add support for Matroska BlockAdditionMapping elements | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

quietvoid Dec. 14, 2021, 3:29 p.m. UTC
Both parse/write implementations are based on mov/movenc.

This only adds support for the "Dolby Vision configuration box".
Other configuration boxes, such as
"Dolby Vision enhancement layer configuration box" are not supported.

The new functions will be used to implement parsing/writing the DOVI config
for Matroska, as well as to refactor both mov/movenc to use dovi_isom functions.

Signed-off-by: quietvoid <tcChlisop0@gmail.com>
---
 libavformat/dovi_isom.c | 118 ++++++++++++++++++++++++++++++++++++++++
 libavformat/dovi_isom.h |  35 ++++++++++++
 2 files changed, 153 insertions(+)
 create mode 100644 libavformat/dovi_isom.c
 create mode 100644 libavformat/dovi_isom.h

Comments

Andreas Rheinhardt Jan. 1, 2022, 2:16 a.m. UTC | #1
quietvoid:
> Both parse/write implementations are based on mov/movenc.
> 
> This only adds support for the "Dolby Vision configuration box".
> Other configuration boxes, such as
> "Dolby Vision enhancement layer configuration box" are not supported.
> 
> The new functions will be used to implement parsing/writing the DOVI config
> for Matroska, as well as to refactor both mov/movenc to use dovi_isom functions.
> 
> Signed-off-by: quietvoid <tcChlisop0@gmail.com>
> ---
>  libavformat/dovi_isom.c | 118 ++++++++++++++++++++++++++++++++++++++++
>  libavformat/dovi_isom.h |  35 ++++++++++++
>  2 files changed, 153 insertions(+)
>  create mode 100644 libavformat/dovi_isom.c
>  create mode 100644 libavformat/dovi_isom.h
> 
> diff --git a/libavformat/dovi_isom.c b/libavformat/dovi_isom.c
> new file mode 100644
> index 0000000000..262d7e37ba
> --- /dev/null
> +++ b/libavformat/dovi_isom.c
> @@ -0,0 +1,118 @@
> +/*
> + * DOVI ISO Media common code
> + *
> + * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.com>
> + * Copyright (c) 2021 quietvoid
> + *
> + * 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 "libavutil/dovi_meta.h"
> +
> +#include "libavcodec/put_bits.h"
> +
> +#include "avformat.h"
> +#include "dovi_isom.h"
> +
> +int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size)
> +{
> +    uint32_t buf;
> +    AVDOVIDecoderConfigurationRecord *dovi;
> +    size_t dovi_size;
> +    int ret;
> +
> +    if (size > (1 << 30) || size < 4)
> +        return AVERROR_INVALIDDATA;
> +
> +    dovi = av_dovi_alloc(&dovi_size);
> +    if (!dovi)
> +        return AVERROR(ENOMEM);
> +
> +    dovi->dv_version_major = *buf_ptr++;    // 8 bits
> +    dovi->dv_version_minor = *buf_ptr++;    // 8 bits
> +
> +    buf = *buf_ptr++ << 8;
> +    buf |= *buf_ptr++;
> +
> +    dovi->dv_profile        = (buf >> 9) & 0x7f;    // 7 bits
> +    dovi->dv_level          = (buf >> 3) & 0x3f;    // 6 bits
> +    dovi->rpu_present_flag  = (buf >> 2) & 0x01;    // 1 bit
> +    dovi->el_present_flag   = (buf >> 1) & 0x01;    // 1 bit
> +    dovi->bl_present_flag   =  buf       & 0x01;    // 1 bit
> +
> +    // Has enough remaining data
> +    if (size >= 5) {
> +        dovi->dv_bl_signal_compatibility_id = ((*buf_ptr++) >> 4) & 0x0f; // 4 bits
> +    } else {
> +        // 0 stands for None
> +        // Dolby Vision V1.2.93 profiles and levels
> +        dovi->dv_bl_signal_compatibility_id = 0;
> +    }
> +
> +    ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
> +                                  (uint8_t *)dovi, dovi_size);
> +    if (ret < 0) {
> +        av_free(dovi);
> +        return ret;
> +    }
> +
> +    av_log(s, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "
> +           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
> +           dovi->dv_version_major, dovi->dv_version_minor,
> +           dovi->dv_profile, dovi->dv_level,
> +           dovi->rpu_present_flag,
> +           dovi->el_present_flag,
> +           dovi->bl_present_flag,
> +           dovi->dv_bl_signal_compatibility_id);
> +
> +    return 0;
> +}
> +
> +void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
> +                           AVDOVIDecoderConfigurationRecord *dovi)
> +{
> +    PutBitContext pb;
> +
> +    init_put_bits(&pb, out, ISOM_DVCC_DVVC_SIZE);
> +
> +    put_bits(&pb, 8, dovi->dv_version_major);
> +    put_bits(&pb, 8, dovi->dv_version_minor);
> +    put_bits(&pb, 7, dovi->dv_profile);
> +    put_bits(&pb, 6, dovi->dv_level);
> +    put_bits(&pb, 1, dovi->rpu_present_flag);
> +    put_bits(&pb, 1, dovi->el_present_flag);
> +    put_bits(&pb, 1, dovi->bl_present_flag);
> +    put_bits(&pb, 4, dovi->dv_bl_signal_compatibility_id);

There is a slight problem with the last six values: They come from the
user and therefore must not be trusted to be within the range they are
supposed to be. (The put_bits API uses an av_assert2 to check that
the values to be written actually fit into the amount of bits that is
used to write them.) So I suggest you use !! for the flags and
appropriate masks for dv_profile, dv_level and
dv_bl_signal_compatibility_id.

> +
> +    put_bits(&pb, 28, 0); /* reserved */
> +    put_bits32(&pb, 0); /* reserved */
> +    put_bits32(&pb, 0); /* reserved */
> +    put_bits32(&pb, 0); /* reserved */
> +    put_bits32(&pb, 0); /* reserved */
> +
> +    flush_put_bits(&pb);
> +
> +    av_log(s, AV_LOG_DEBUG, "DOVI in %s box, version: %d.%d, profile: %d, level: %d, "
> +           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
> +           dovi->dv_profile > 10 ? "dvwC" : (dovi->dv_profile > 7 ? "dvvC" : "dvcC"),
> +           dovi->dv_version_major, dovi->dv_version_minor,
> +           dovi->dv_profile, dovi->dv_level,
> +           dovi->rpu_present_flag,
> +           dovi->el_present_flag,
> +           dovi->bl_present_flag,
> +           dovi->dv_bl_signal_compatibility_id);
> +}
> diff --git a/libavformat/dovi_isom.h b/libavformat/dovi_isom.h
> new file mode 100644
> index 0000000000..1526164319
> --- /dev/null
> +++ b/libavformat/dovi_isom.h
> @@ -0,0 +1,35 @@
> +/*
> + * DOVI ISO Media common code
> + * Copyright (c) 2021 quietvoid
> + *
> + * 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
> + */
> +
> +#ifndef AVFORMAT_DOVI_ISOM_H
> +#define AVFORMAT_DOVI_ISOM_H
> +
> +#include "libavutil/dovi_meta.h"
> +
> +#include "avformat.h"
> +
> +#define ISOM_DVCC_DVVC_SIZE 24
> +
> +int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size);
> +void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
> +                           AVDOVIDecoderConfigurationRecord *dovi);
> +
> +#endif /* AVFORMAT_DOVI_ISOM_H */
>
quietvoid Jan. 1, 2022, 4:17 a.m. UTC | #2
On Fri, Dec 31, 2021 at 9:16 PM Andreas Rheinhardt
<andreas.rheinhardt@outlook.com> wrote:
>
> quietvoid:
> > Both parse/write implementations are based on mov/movenc.
> >
> > This only adds support for the "Dolby Vision configuration box".
> > Other configuration boxes, such as
> > "Dolby Vision enhancement layer configuration box" are not supported.
> >
> > The new functions will be used to implement parsing/writing the DOVI config
> > for Matroska, as well as to refactor both mov/movenc to use dovi_isom functions.
> >
> > Signed-off-by: quietvoid <tcChlisop0@gmail.com>
> > ---
> >  libavformat/dovi_isom.c | 118 ++++++++++++++++++++++++++++++++++++++++
> >  libavformat/dovi_isom.h |  35 ++++++++++++
> >  2 files changed, 153 insertions(+)
> >  create mode 100644 libavformat/dovi_isom.c
> >  create mode 100644 libavformat/dovi_isom.h
> >
> > diff --git a/libavformat/dovi_isom.c b/libavformat/dovi_isom.c
> > new file mode 100644
> > index 0000000000..262d7e37ba
> > --- /dev/null
> > +++ b/libavformat/dovi_isom.c
> > @@ -0,0 +1,118 @@
> > +/*
> > + * DOVI ISO Media common code
> > + *
> > + * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.com>
> > + * Copyright (c) 2021 quietvoid
> > + *
> > + * 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 "libavutil/dovi_meta.h"
> > +
> > +#include "libavcodec/put_bits.h"
> > +
> > +#include "avformat.h"
> > +#include "dovi_isom.h"
> > +
> > +int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size)
> > +{
> > +    uint32_t buf;
> > +    AVDOVIDecoderConfigurationRecord *dovi;
> > +    size_t dovi_size;
> > +    int ret;
> > +
> > +    if (size > (1 << 30) || size < 4)
> > +        return AVERROR_INVALIDDATA;
> > +
> > +    dovi = av_dovi_alloc(&dovi_size);
> > +    if (!dovi)
> > +        return AVERROR(ENOMEM);
> > +
> > +    dovi->dv_version_major = *buf_ptr++;    // 8 bits
> > +    dovi->dv_version_minor = *buf_ptr++;    // 8 bits
> > +
> > +    buf = *buf_ptr++ << 8;
> > +    buf |= *buf_ptr++;
> > +
> > +    dovi->dv_profile        = (buf >> 9) & 0x7f;    // 7 bits
> > +    dovi->dv_level          = (buf >> 3) & 0x3f;    // 6 bits
> > +    dovi->rpu_present_flag  = (buf >> 2) & 0x01;    // 1 bit
> > +    dovi->el_present_flag   = (buf >> 1) & 0x01;    // 1 bit
> > +    dovi->bl_present_flag   =  buf       & 0x01;    // 1 bit
> > +
> > +    // Has enough remaining data
> > +    if (size >= 5) {
> > +        dovi->dv_bl_signal_compatibility_id = ((*buf_ptr++) >> 4) & 0x0f; // 4 bits
> > +    } else {
> > +        // 0 stands for None
> > +        // Dolby Vision V1.2.93 profiles and levels
> > +        dovi->dv_bl_signal_compatibility_id = 0;
> > +    }
> > +
> > +    ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
> > +                                  (uint8_t *)dovi, dovi_size);
> > +    if (ret < 0) {
> > +        av_free(dovi);
> > +        return ret;
> > +    }
> > +
> > +    av_log(s, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "
> > +           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
> > +           dovi->dv_version_major, dovi->dv_version_minor,
> > +           dovi->dv_profile, dovi->dv_level,
> > +           dovi->rpu_present_flag,
> > +           dovi->el_present_flag,
> > +           dovi->bl_present_flag,
> > +           dovi->dv_bl_signal_compatibility_id);
> > +
> > +    return 0;
> > +}
> > +
> > +void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
> > +                           AVDOVIDecoderConfigurationRecord *dovi)
> > +{
> > +    PutBitContext pb;
> > +
> > +    init_put_bits(&pb, out, ISOM_DVCC_DVVC_SIZE);
> > +
> > +    put_bits(&pb, 8, dovi->dv_version_major);
> > +    put_bits(&pb, 8, dovi->dv_version_minor);
> > +    put_bits(&pb, 7, dovi->dv_profile);
> > +    put_bits(&pb, 6, dovi->dv_level);
> > +    put_bits(&pb, 1, dovi->rpu_present_flag);
> > +    put_bits(&pb, 1, dovi->el_present_flag);
> > +    put_bits(&pb, 1, dovi->bl_present_flag);
> > +    put_bits(&pb, 4, dovi->dv_bl_signal_compatibility_id);
>
> There is a slight problem with the last six values: They come from the
> user and therefore must not be trusted to be within the range they are
> supposed to be. (The put_bits API uses an av_assert2 to check that
> the values to be written actually fit into the amount of bits that is
> used to write them.) So I suggest you use !! for the flags and
> appropriate masks for dv_profile, dv_level and
> dv_bl_signal_compatibility_id.

Thanks. I used the same masks as in the read function.

Updated along with the fixes for the other patches in v11:
https://ffmpeg.org/pipermail/ffmpeg-devel/2022-January/290611.html

> > +
> > +    put_bits(&pb, 28, 0); /* reserved */
> > +    put_bits32(&pb, 0); /* reserved */
> > +    put_bits32(&pb, 0); /* reserved */
> > +    put_bits32(&pb, 0); /* reserved */
> > +    put_bits32(&pb, 0); /* reserved */
> > +
> > +    flush_put_bits(&pb);
> > +
> > +    av_log(s, AV_LOG_DEBUG, "DOVI in %s box, version: %d.%d, profile: %d, level: %d, "
> > +           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
> > +           dovi->dv_profile > 10 ? "dvwC" : (dovi->dv_profile > 7 ? "dvvC" : "dvcC"),
> > +           dovi->dv_version_major, dovi->dv_version_minor,
> > +           dovi->dv_profile, dovi->dv_level,
> > +           dovi->rpu_present_flag,
> > +           dovi->el_present_flag,
> > +           dovi->bl_present_flag,
> > +           dovi->dv_bl_signal_compatibility_id);
> > +}
> > diff --git a/libavformat/dovi_isom.h b/libavformat/dovi_isom.h
> > new file mode 100644
> > index 0000000000..1526164319
> > --- /dev/null
> > +++ b/libavformat/dovi_isom.h
> > @@ -0,0 +1,35 @@
> > +/*
> > + * DOVI ISO Media common code
> > + * Copyright (c) 2021 quietvoid
> > + *
> > + * 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
> > + */
> > +
> > +#ifndef AVFORMAT_DOVI_ISOM_H
> > +#define AVFORMAT_DOVI_ISOM_H
> > +
> > +#include "libavutil/dovi_meta.h"
> > +
> > +#include "avformat.h"
> > +
> > +#define ISOM_DVCC_DVVC_SIZE 24
> > +
> > +int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size);
> > +void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
> > +                           AVDOVIDecoderConfigurationRecord *dovi);
> > +
> > +#endif /* AVFORMAT_DOVI_ISOM_H */
> >
>
> _______________________________________________
> 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".
Andreas Rheinhardt Jan. 1, 2022, 7:24 a.m. UTC | #3
quietvoid:
> On Fri, Dec 31, 2021 at 9:16 PM Andreas Rheinhardt
> <andreas.rheinhardt@outlook.com> wrote:
>>
>> quietvoid:
>>> Both parse/write implementations are based on mov/movenc.
>>>
>>> This only adds support for the "Dolby Vision configuration box".
>>> Other configuration boxes, such as
>>> "Dolby Vision enhancement layer configuration box" are not supported.
>>>
>>> The new functions will be used to implement parsing/writing the DOVI config
>>> for Matroska, as well as to refactor both mov/movenc to use dovi_isom functions.
>>>
>>> Signed-off-by: quietvoid <tcChlisop0@gmail.com>
>>> ---
>>>  libavformat/dovi_isom.c | 118 ++++++++++++++++++++++++++++++++++++++++
>>>  libavformat/dovi_isom.h |  35 ++++++++++++
>>>  2 files changed, 153 insertions(+)
>>>  create mode 100644 libavformat/dovi_isom.c
>>>  create mode 100644 libavformat/dovi_isom.h
>>>
>>> diff --git a/libavformat/dovi_isom.c b/libavformat/dovi_isom.c
>>> new file mode 100644
>>> index 0000000000..262d7e37ba
>>> --- /dev/null
>>> +++ b/libavformat/dovi_isom.c
>>> @@ -0,0 +1,118 @@
>>> +/*
>>> + * DOVI ISO Media common code
>>> + *
>>> + * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.com>
>>> + * Copyright (c) 2021 quietvoid
>>> + *
>>> + * 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 "libavutil/dovi_meta.h"
>>> +
>>> +#include "libavcodec/put_bits.h"
>>> +
>>> +#include "avformat.h"
>>> +#include "dovi_isom.h"
>>> +
>>> +int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size)
>>> +{
>>> +    uint32_t buf;
>>> +    AVDOVIDecoderConfigurationRecord *dovi;
>>> +    size_t dovi_size;
>>> +    int ret;
>>> +
>>> +    if (size > (1 << 30) || size < 4)
>>> +        return AVERROR_INVALIDDATA;
>>> +
>>> +    dovi = av_dovi_alloc(&dovi_size);
>>> +    if (!dovi)
>>> +        return AVERROR(ENOMEM);
>>> +
>>> +    dovi->dv_version_major = *buf_ptr++;    // 8 bits
>>> +    dovi->dv_version_minor = *buf_ptr++;    // 8 bits
>>> +
>>> +    buf = *buf_ptr++ << 8;
>>> +    buf |= *buf_ptr++;
>>> +
>>> +    dovi->dv_profile        = (buf >> 9) & 0x7f;    // 7 bits
>>> +    dovi->dv_level          = (buf >> 3) & 0x3f;    // 6 bits
>>> +    dovi->rpu_present_flag  = (buf >> 2) & 0x01;    // 1 bit
>>> +    dovi->el_present_flag   = (buf >> 1) & 0x01;    // 1 bit
>>> +    dovi->bl_present_flag   =  buf       & 0x01;    // 1 bit
>>> +
>>> +    // Has enough remaining data
>>> +    if (size >= 5) {
>>> +        dovi->dv_bl_signal_compatibility_id = ((*buf_ptr++) >> 4) & 0x0f; // 4 bits
>>> +    } else {
>>> +        // 0 stands for None
>>> +        // Dolby Vision V1.2.93 profiles and levels
>>> +        dovi->dv_bl_signal_compatibility_id = 0;
>>> +    }
>>> +
>>> +    ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
>>> +                                  (uint8_t *)dovi, dovi_size);
>>> +    if (ret < 0) {
>>> +        av_free(dovi);
>>> +        return ret;
>>> +    }
>>> +
>>> +    av_log(s, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "
>>> +           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
>>> +           dovi->dv_version_major, dovi->dv_version_minor,
>>> +           dovi->dv_profile, dovi->dv_level,
>>> +           dovi->rpu_present_flag,
>>> +           dovi->el_present_flag,
>>> +           dovi->bl_present_flag,
>>> +           dovi->dv_bl_signal_compatibility_id);
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
>>> +                           AVDOVIDecoderConfigurationRecord *dovi)
>>> +{
>>> +    PutBitContext pb;
>>> +
>>> +    init_put_bits(&pb, out, ISOM_DVCC_DVVC_SIZE);
>>> +
>>> +    put_bits(&pb, 8, dovi->dv_version_major);
>>> +    put_bits(&pb, 8, dovi->dv_version_minor);
>>> +    put_bits(&pb, 7, dovi->dv_profile);
>>> +    put_bits(&pb, 6, dovi->dv_level);
>>> +    put_bits(&pb, 1, dovi->rpu_present_flag);
>>> +    put_bits(&pb, 1, dovi->el_present_flag);
>>> +    put_bits(&pb, 1, dovi->bl_present_flag);
>>> +    put_bits(&pb, 4, dovi->dv_bl_signal_compatibility_id);
>>
>> There is a slight problem with the last six values: They come from the
>> user and therefore must not be trusted to be within the range they are
>> supposed to be. (The put_bits API uses an av_assert2 to check that
>> the values to be written actually fit into the amount of bits that is
>> used to write them.) So I suggest you use !! for the flags and
>> appropriate masks for dv_profile, dv_level and
>> dv_bl_signal_compatibility_id.
> 
> Thanks. I used the same masks as in the read function.
> 

The current code is also dangerous (just a bit less than this version of
your patchset): If dv_profile & 128 is set (which it mustn't), then the
avio_wb16() will run into the av_assert2 in avio_w8() (if assert-level
is >=2).
(And dovi->dv_bl_signal_compatibility_id << 28 would be UB in case
dv_bl_signal_compatibility_id & 0xF8 is != 0.)

- Andreas
diff mbox series

Patch

diff --git a/libavformat/dovi_isom.c b/libavformat/dovi_isom.c
new file mode 100644
index 0000000000..262d7e37ba
--- /dev/null
+++ b/libavformat/dovi_isom.c
@@ -0,0 +1,118 @@ 
+/*
+ * DOVI ISO Media common code
+ *
+ * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.com>
+ * Copyright (c) 2021 quietvoid
+ *
+ * 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 "libavutil/dovi_meta.h"
+
+#include "libavcodec/put_bits.h"
+
+#include "avformat.h"
+#include "dovi_isom.h"
+
+int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size)
+{
+    uint32_t buf;
+    AVDOVIDecoderConfigurationRecord *dovi;
+    size_t dovi_size;
+    int ret;
+
+    if (size > (1 << 30) || size < 4)
+        return AVERROR_INVALIDDATA;
+
+    dovi = av_dovi_alloc(&dovi_size);
+    if (!dovi)
+        return AVERROR(ENOMEM);
+
+    dovi->dv_version_major = *buf_ptr++;    // 8 bits
+    dovi->dv_version_minor = *buf_ptr++;    // 8 bits
+
+    buf = *buf_ptr++ << 8;
+    buf |= *buf_ptr++;
+
+    dovi->dv_profile        = (buf >> 9) & 0x7f;    // 7 bits
+    dovi->dv_level          = (buf >> 3) & 0x3f;    // 6 bits
+    dovi->rpu_present_flag  = (buf >> 2) & 0x01;    // 1 bit
+    dovi->el_present_flag   = (buf >> 1) & 0x01;    // 1 bit
+    dovi->bl_present_flag   =  buf       & 0x01;    // 1 bit
+
+    // Has enough remaining data
+    if (size >= 5) {
+        dovi->dv_bl_signal_compatibility_id = ((*buf_ptr++) >> 4) & 0x0f; // 4 bits
+    } else {
+        // 0 stands for None
+        // Dolby Vision V1.2.93 profiles and levels
+        dovi->dv_bl_signal_compatibility_id = 0;
+    }
+
+    ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
+                                  (uint8_t *)dovi, dovi_size);
+    if (ret < 0) {
+        av_free(dovi);
+        return ret;
+    }
+
+    av_log(s, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "
+           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
+           dovi->dv_version_major, dovi->dv_version_minor,
+           dovi->dv_profile, dovi->dv_level,
+           dovi->rpu_present_flag,
+           dovi->el_present_flag,
+           dovi->bl_present_flag,
+           dovi->dv_bl_signal_compatibility_id);
+
+    return 0;
+}
+
+void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
+                           AVDOVIDecoderConfigurationRecord *dovi)
+{
+    PutBitContext pb;
+
+    init_put_bits(&pb, out, ISOM_DVCC_DVVC_SIZE);
+
+    put_bits(&pb, 8, dovi->dv_version_major);
+    put_bits(&pb, 8, dovi->dv_version_minor);
+    put_bits(&pb, 7, dovi->dv_profile);
+    put_bits(&pb, 6, dovi->dv_level);
+    put_bits(&pb, 1, dovi->rpu_present_flag);
+    put_bits(&pb, 1, dovi->el_present_flag);
+    put_bits(&pb, 1, dovi->bl_present_flag);
+    put_bits(&pb, 4, dovi->dv_bl_signal_compatibility_id);
+
+    put_bits(&pb, 28, 0); /* reserved */
+    put_bits32(&pb, 0); /* reserved */
+    put_bits32(&pb, 0); /* reserved */
+    put_bits32(&pb, 0); /* reserved */
+    put_bits32(&pb, 0); /* reserved */
+
+    flush_put_bits(&pb);
+
+    av_log(s, AV_LOG_DEBUG, "DOVI in %s box, version: %d.%d, profile: %d, level: %d, "
+           "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
+           dovi->dv_profile > 10 ? "dvwC" : (dovi->dv_profile > 7 ? "dvvC" : "dvcC"),
+           dovi->dv_version_major, dovi->dv_version_minor,
+           dovi->dv_profile, dovi->dv_level,
+           dovi->rpu_present_flag,
+           dovi->el_present_flag,
+           dovi->bl_present_flag,
+           dovi->dv_bl_signal_compatibility_id);
+}
diff --git a/libavformat/dovi_isom.h b/libavformat/dovi_isom.h
new file mode 100644
index 0000000000..1526164319
--- /dev/null
+++ b/libavformat/dovi_isom.h
@@ -0,0 +1,35 @@ 
+/*
+ * DOVI ISO Media common code
+ * Copyright (c) 2021 quietvoid
+ *
+ * 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
+ */
+
+#ifndef AVFORMAT_DOVI_ISOM_H
+#define AVFORMAT_DOVI_ISOM_H
+
+#include "libavutil/dovi_meta.h"
+
+#include "avformat.h"
+
+#define ISOM_DVCC_DVVC_SIZE 24
+
+int ff_isom_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const uint8_t *buf_ptr, uint64_t size);
+void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE],
+                           AVDOVIDecoderConfigurationRecord *dovi);
+
+#endif /* AVFORMAT_DOVI_ISOM_H */