chromium/third_party/libavif/src/src/obu.c

/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// OBU parsing and bit magic all originally from dav1d's obu.c and getbits.c,
// but heavily modified/reduced down to simply find the Sequence Header OBU
// and pull a few interesting pieces from it.
//
// Any other code in here is under this license:
//
// Copyright 2020 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause

#include "avif/internal.h"

#include <stdint.h>
#include <string.h>

#if defined(AVIF_CODEC_AVM)
#include "config/aom_config.h"
#endif

// ---------------------------------------------------------------------------
// avifBits - Originally dav1d's GetBits struct (see dav1d's getbits.c)

avifBits;

static inline uint32_t avifBitsReadPos(const avifBits * bits)
{}

static void avifBitsInit(avifBits * const bits, const uint8_t * const data, const size_t size)
{}

static void avifBitsRefill(avifBits * const bits, const uint32_t n)
{}

static uint32_t avifBitsRead(avifBits * const bits, const uint32_t n)
{}

static uint32_t avifBitsReadUleb128(avifBits * bits)
{}

static uint32_t avifBitsReadVLC(avifBits * const bits)
{}

// ---------------------------------------------------------------------------
// Variables in here use snake_case to self-document from the AV1 spec and the draft AV2 spec:
//
// https://aomediacodec.github.io/av1-spec/av1-spec.pdf
//
// Originally dav1d's parse_seq_hdr() function (heavily modified and split)

static avifBool parseSequenceHeaderProfile(avifBits * bits, avifSequenceHeader * header)
{}

static avifBool parseSequenceHeaderFrameMaxDimensions(avifBits * bits, avifSequenceHeader * header)
{}

static avifBool parseSequenceHeaderEnabledFeatures(avifBits * bits, avifSequenceHeader * header)
{}

static avifBool parseSequenceHeaderColorConfig(avifBits * bits, avifSequenceHeader * header)
{}

static avifBool parseAV1SequenceHeader(avifBits * bits, avifSequenceHeader * header)
{}

#if defined(AVIF_CODEC_AVM)
// See https://gitlab.com/AOMediaCodec/avm/-/blob/main/av1/decoder/decodeframe.c
static avifBool parseAV2SequenceHeader(avifBits * bits, avifSequenceHeader * header)
{
    // See read_sequence_header_obu() in avm.
    AVIF_CHECK(parseSequenceHeaderProfile(bits, header));

    // See av1_read_sequence_header() in avm.
    AVIF_CHECK(parseSequenceHeaderFrameMaxDimensions(bits, header));
#if CONFIG_BLOCK_256
    if (!avifBitsRead(bits, 1)) // BLOCK_256X256
#endif
        avifBitsRead(bits, 1); // BLOCK_128X128
    AVIF_CHECK(parseSequenceHeaderEnabledFeatures(bits, header));

    avifBitsRead(bits, 2);       // enable_superres, enable_cdef
    if (avifBitsRead(bits, 1)) { // enable_restoration
#if CONFIG_LR_IMPROVEMENTS
        const int lr_tools_disable_mask_length = /*RESTORE_SWITCHABLE_TYPES=*/5 - 1;
        avifBitsRead(bits, lr_tools_disable_mask_length); // lr_tools_disable_mask[0]
        if (avifBitsRead(bits, 1)) {
            avifBitsRead(bits, lr_tools_disable_mask_length - 1); // lr_tools_disable_mask[1]
        }
#endif
    }

    // See av1_read_color_config() in avm.
    AVIF_CHECK(parseSequenceHeaderColorConfig(bits, header));
    // Ignored fields.
    //   base_y_dc_delta_q
    //   base_uv_dc_delta_q

    // See read_sequence_header_obu() in avm.
    // Ignored field.
    //   film_grain_params_present

    // See av1_read_sequence_header_beyond_av1() in avm.
    // Other ignored fields.
    return !bits->error;
}
#endif

avifBool avifSequenceHeaderParse(avifSequenceHeader * header, const avifROData * sample, avifCodecType codecType)
{}