1605 lines · 67.0 KB · C
RawDownload
/* * 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. */ #include "config.h" #include <errno.h>#include <limits.h>#ifndef __plan9__#include <stdio.h>#endif #include "dav1d/data.h" #include "common/intops.h" #include "src/decode.h"#include "src/getbits.h"#include "src/levels.h"#include "src/log.h"#include "src/obu.h"#include "src/ref.h"#include "src/thread_task.h" static int parse_seq_hdr(Dav1dContext *const c, GetBits *const gb,                         Dav1dSequenceHeader *const hdr){#define DEBUG_SEQ_HDR 0 #if DEBUG_SEQ_HDR    const unsigned init_bit_pos = dav1d_get_bits_pos(gb);#endif     hdr->profile = dav1d_get_bits(gb, 3);    if (hdr->profile > 2) goto error;#if DEBUG_SEQ_HDR    printf("SEQHDR: post-profile: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     hdr->still_picture = dav1d_get_bits(gb, 1);    hdr->reduced_still_picture_header = dav1d_get_bits(gb, 1);    if (hdr->reduced_still_picture_header && !hdr->still_picture) goto error;#if DEBUG_SEQ_HDR    printf("SEQHDR: post-stillpicture_flags: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     if (hdr->reduced_still_picture_header) {        hdr->timing_info_present = 0;        hdr->decoder_model_info_present = 0;        hdr->display_model_info_present = 0;        hdr->num_operating_points = 1;        hdr->operating_points[0].idc = 0;        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);        hdr->operating_points[0].tier = 0;        hdr->operating_points[0].decoder_model_param_present = 0;        hdr->operating_points[0].display_model_param_present = 0;    } else {        hdr->timing_info_present = dav1d_get_bits(gb, 1);        if (hdr->timing_info_present) {            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);            hdr->time_scale = dav1d_get_bits(gb, 32);            hdr->equal_picture_interval = dav1d_get_bits(gb, 1);            if (hdr->equal_picture_interval) {                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);                if (num_ticks_per_picture == 0xFFFFFFFFU)                    goto error;                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;            }             hdr->decoder_model_info_present = dav1d_get_bits(gb, 1);            if (hdr->decoder_model_info_present) {                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;            }        } else {            hdr->decoder_model_info_present = 0;        }#if DEBUG_SEQ_HDR        printf("SEQHDR: post-timinginfo: off=%u\n",               dav1d_get_bits_pos(gb) - init_bit_pos);#endif         hdr->display_model_info_present = dav1d_get_bits(gb, 1);        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;        for (int i = 0; i < hdr->num_operating_points; i++) {            struct Dav1dSequenceHeaderOperatingPoint *const op =                &hdr->operating_points[i];            op->idc = dav1d_get_bits(gb, 12);            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))                goto error;            op->major_level = 2 + dav1d_get_bits(gb, 3);            op->minor_level = dav1d_get_bits(gb, 2);            op->tier = op->major_level > 3 ? dav1d_get_bits(gb, 1) : 0;            op->decoder_model_param_present =                hdr->decoder_model_info_present && dav1d_get_bits(gb, 1);            if (op->decoder_model_param_present) {                struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =                    &hdr->operating_parameter_info[i];                opi->decoder_buffer_delay =                    dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);                opi->encoder_buffer_delay =                    dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);                opi->low_delay_mode = dav1d_get_bits(gb, 1);            }            op->display_model_param_present =                hdr->display_model_info_present && dav1d_get_bits(gb, 1);            if (op->display_model_param_present) {                op->initial_display_delay = dav1d_get_bits(gb, 4) + 1;            }        }        const int op_idx =            c->operating_point < hdr->num_operating_points ? c->operating_point : 0;        c->operating_point_idc = hdr->operating_points[op_idx].idc;#if DEBUG_SEQ_HDR        printf("SEQHDR: post-operating-points: off=%u\n",               dav1d_get_bits_pos(gb) - init_bit_pos);#endif    }     hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;    hdr->max_height = dav1d_get_bits(gb, hdr->height_n_bits) + 1;#if DEBUG_SEQ_HDR    printf("SEQHDR: post-size: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif    hdr->frame_id_numbers_present =        hdr->reduced_still_picture_header ? 0 : dav1d_get_bits(gb, 1);    if (hdr->frame_id_numbers_present) {        hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;        hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;    }#if DEBUG_SEQ_HDR    printf("SEQHDR: post-frame-id-numbers-present: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     hdr->sb128 = dav1d_get_bits(gb, 1);    hdr->filter_intra = dav1d_get_bits(gb, 1);    hdr->intra_edge_filter = dav1d_get_bits(gb, 1);    if (hdr->reduced_still_picture_header) {        hdr->inter_intra = 0;        hdr->masked_compound = 0;        hdr->warped_motion = 0;        hdr->dual_filter = 0;        hdr->order_hint = 0;        hdr->jnt_comp = 0;        hdr->ref_frame_mvs = 0;        hdr->order_hint_n_bits = 0;        hdr->screen_content_tools = DAV1D_ADAPTIVE;        hdr->force_integer_mv = DAV1D_ADAPTIVE;    } else {        hdr->inter_intra = dav1d_get_bits(gb, 1);        hdr->masked_compound = dav1d_get_bits(gb, 1);        hdr->warped_motion = dav1d_get_bits(gb, 1);        hdr->dual_filter = dav1d_get_bits(gb, 1);        hdr->order_hint = dav1d_get_bits(gb, 1);        if (hdr->order_hint) {            hdr->jnt_comp = dav1d_get_bits(gb, 1);            hdr->ref_frame_mvs = dav1d_get_bits(gb, 1);        } else {            hdr->jnt_comp = 0;            hdr->ref_frame_mvs = 0;            hdr->order_hint_n_bits = 0;        }        hdr->screen_content_tools = dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1);    #if DEBUG_SEQ_HDR        printf("SEQHDR: post-screentools: off=%u\n",               dav1d_get_bits_pos(gb) - init_bit_pos);    #endif        hdr->force_integer_mv = hdr->screen_content_tools ?                                dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1) : 2;        if (hdr->order_hint)            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;    }    hdr->super_res = dav1d_get_bits(gb, 1);    hdr->cdef = dav1d_get_bits(gb, 1);    hdr->restoration = dav1d_get_bits(gb, 1);#if DEBUG_SEQ_HDR    printf("SEQHDR: post-featurebits: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     hdr->hbd = dav1d_get_bits(gb, 1);    if (hdr->profile == 2 && hdr->hbd) hdr->hbd += dav1d_get_bits(gb, 1);    hdr->monochrome = hdr->profile != 1 ? dav1d_get_bits(gb, 1) : 0;    hdr->color_description_present = dav1d_get_bits(gb, 1);    if (hdr->color_description_present) {        hdr->pri = dav1d_get_bits(gb, 8);        hdr->trc = dav1d_get_bits(gb, 8);        hdr->mtrx = dav1d_get_bits(gb, 8);    } else {        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;        hdr->trc = DAV1D_TRC_UNKNOWN;        hdr->mtrx = DAV1D_MC_UNKNOWN;    }    if (hdr->monochrome) {        hdr->color_range = dav1d_get_bits(gb, 1);        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;        hdr->ss_hor = hdr->ss_ver = 1;        hdr->chr = DAV1D_CHR_UNKNOWN;        hdr->separate_uv_delta_q = 0;    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&               hdr->trc == DAV1D_TRC_SRGB &&               hdr->mtrx == DAV1D_MC_IDENTITY)    {        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;        hdr->ss_hor = hdr->ss_ver = 0;        hdr->color_range = 1;        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))            goto error;    } else {        hdr->color_range = dav1d_get_bits(gb, 1);        switch (hdr->profile) {        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;                hdr->ss_hor = hdr->ss_ver = 1;                break;        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;                hdr->ss_hor = hdr->ss_ver = 0;                break;        case 2:            if (hdr->hbd == 2) {                hdr->ss_hor = dav1d_get_bits(gb, 1);                hdr->ss_ver = hdr->ss_hor && dav1d_get_bits(gb, 1);            } else {                hdr->ss_hor = 1;                hdr->ss_ver = 0;            }            hdr->layout = hdr->ss_hor ?                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :                                        DAV1D_PIXEL_LAYOUT_I422 :                                        DAV1D_PIXEL_LAYOUT_I444;            break;        }        hdr->chr = hdr->ss_hor == 1 && hdr->ss_ver == 1 ?                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;    }    hdr->separate_uv_delta_q = !hdr->monochrome && dav1d_get_bits(gb, 1);#if DEBUG_SEQ_HDR    printf("SEQHDR: post-colorinfo: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     hdr->film_grain_present = dav1d_get_bits(gb, 1);#if DEBUG_SEQ_HDR    printf("SEQHDR: post-filmgrain: off=%u\n",           dav1d_get_bits_pos(gb) - init_bit_pos);#endif     dav1d_get_bits(gb, 1); // dummy bit     // We needn't bother flushing the OBU here: we'll check we didn't    // overrun in the caller and will then discard gb, so there's no    // point in setting its position properly.     return 0; error:    dav1d_log(c, "Error parsing sequence header\n");    return DAV1D_ERR(EINVAL);} static int read_frame_size(Dav1dContext *const c, GetBits *const gb,                           const int use_ref){    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;    Dav1dFrameHeader *const hdr = c->frame_hdr;     if (use_ref) {        for (int i = 0; i < 7; i++) {            if (dav1d_get_bits(gb, 1)) {                const Dav1dThreadPicture *const ref =                    &c->refs[c->frame_hdr->refidx[i]].p;                if (!ref->p.data[0]) return -1;                hdr->width[1] = ref->p.p.w;                hdr->height = ref->p.p.h;                hdr->render_width = ref->p.frame_hdr->render_width;                hdr->render_height = ref->p.frame_hdr->render_height;                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bits(gb, 1);                if (hdr->super_res.enabled) {                    const int d = hdr->super_res.width_scale_denominator =                        9 + dav1d_get_bits(gb, 3);                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,                                         imin(16, hdr->width[1]));                } else {                    hdr->super_res.width_scale_denominator = 8;                    hdr->width[0] = hdr->width[1];                }                return 0;            }        }    }     if (hdr->frame_size_override) {        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;    } else {        hdr->width[1] = seqhdr->max_width;        hdr->height = seqhdr->max_height;    }    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bits(gb, 1);    if (hdr->super_res.enabled) {        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));    } else {        hdr->super_res.width_scale_denominator = 8;        hdr->width[0] = hdr->width[1];    }    hdr->have_render_size = dav1d_get_bits(gb, 1);    if (hdr->have_render_size) {        hdr->render_width = dav1d_get_bits(gb, 16) + 1;        hdr->render_height = dav1d_get_bits(gb, 16) + 1;    } else {        hdr->render_width = hdr->width[1];        hdr->render_height = hdr->height;    }    return 0;} static inline int tile_log2(const int sz, const int tgt) {    int k;    for (k = 0; (sz << k) < tgt; k++) ;    return k;} static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas = {    .mode_delta = { 0, 0 },    .ref_delta = { 1, 0, 0, 0, -1, 0, -1, -1 },}; static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {#define DEBUG_FRAME_HDR 0 #if DEBUG_FRAME_HDR    const uint8_t *const init_ptr = gb->ptr;#endif    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;    Dav1dFrameHeader *const hdr = c->frame_hdr;     hdr->show_existing_frame =        !seqhdr->reduced_still_picture_header && dav1d_get_bits(gb, 1);#if DEBUG_FRAME_HDR    printf("HDR: post-show_existing_frame: off=%td\n",           (gb->ptr - init_ptr) * 8 - gb->bits_left);#endif    if (hdr->show_existing_frame) {        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);        if (seqhdr->frame_id_numbers_present) {            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) return DAV1D_ERR(EINVAL);        }        return 0;    }     hdr->frame_type = seqhdr->reduced_still_picture_header ? DAV1D_FRAME_TYPE_KEY : dav1d_get_bits(gb, 2);    hdr->show_frame = seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1);    if (hdr->show_frame) {        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);    } else        hdr->showable_frame = dav1d_get_bits(gb, 1);    hdr->error_resilient_mode =        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||        seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1);#if DEBUG_FRAME_HDR    printf("HDR: post-frametype_bits: off=%td\n",           (gb->ptr - init_ptr) * 8 - gb->bits_left);#endif    hdr->disable_cdf_update = dav1d_get_bits(gb, 1);    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?                                 dav1d_get_bits(gb, 1) : seqhdr->screen_content_tools;    if (hdr->allow_screen_content_tools)        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?                                dav1d_get_bits(gb, 1) : seqhdr->force_integer_mv;    else        hdr->force_integer_mv = 0;     if (!(hdr->frame_type & 1))        hdr->force_integer_mv = 1;     if (seqhdr->frame_id_numbers_present)        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);     hdr->frame_size_override = seqhdr->reduced_still_picture_header ? 0 :                               hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bits(gb, 1);#if DEBUG_FRAME_HDR    printf("HDR: post-frame_size_override_flag: off=%td\n",           (gb->ptr - init_ptr) * 8 - gb->bits_left);#endif    hdr->frame_offset = seqhdr->order_hint ?                        dav1d_get_bits(gb, seqhdr->order_hint_n_bits) : 0;    hdr->primary_ref_frame = !hdr->error_resilient_mode && hdr->frame_type & 1 ?                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;     if (seqhdr->decoder_model_info_present) {        hdr->buffer_removal_time_present = dav1d_get_bits(gb, 1);        if (hdr->buffer_removal_time_present) {            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];                if (seqop->decoder_model_param_present) {                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);                }            }        }    }     if (hdr->frame_type == DAV1D_FRAME_TYPE_KEY ||        hdr->frame_type == DAV1D_FRAME_TYPE_INTRA)    {        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)            for (int i = 0; i < 8; i++)                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);        if (read_frame_size(c, gb, 0) < 0) goto error;        hdr->allow_intrabc = hdr->allow_screen_content_tools &&                             !hdr->super_res.enabled && dav1d_get_bits(gb, 1);        hdr->use_ref_frame_mvs = 0;    } else {        hdr->allow_intrabc = 0;        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :                                   dav1d_get_bits(gb, 8);        if (hdr->error_resilient_mode && seqhdr->order_hint)            for (int i = 0; i < 8; i++)                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);        hdr->frame_ref_short_signaling =            seqhdr->order_hint && dav1d_get_bits(gb, 1);        if (hdr->frame_ref_short_signaling) { // FIXME: Nearly verbatim copy from section 7.8            hdr->refidx[0] = dav1d_get_bits(gb, 3);            hdr->refidx[1] = hdr->refidx[2] = -1;            hdr->refidx[3] = dav1d_get_bits(gb, 3);            hdr->refidx[4] = hdr->refidx[5] = hdr->refidx[6] = -1;             int shifted_frame_offset[8];            const int current_frame_offset = 1 << (seqhdr->order_hint_n_bits - 1);            for (int i = 0; i < 8; i++) {                if (!c->refs[i].p.p.frame_hdr) goto error;                shifted_frame_offset[i] = current_frame_offset +                    get_poc_diff(seqhdr->order_hint_n_bits,                                 c->refs[i].p.p.frame_hdr->frame_offset,                                 hdr->frame_offset);            }             int used_frame[8] = { 0 };            used_frame[hdr->refidx[0]] = 1;            used_frame[hdr->refidx[3]] = 1;             int latest_frame_offset = -1;            for (int i = 0; i < 8; i++) {                const int hint = shifted_frame_offset[i];                if (!used_frame[i] && hint >= current_frame_offset &&                    hint >= latest_frame_offset)                {                    hdr->refidx[6] = i;                    latest_frame_offset = hint;                }            }            if (latest_frame_offset != -1)                used_frame[hdr->refidx[6]] = 1;             int earliest_frame_offset = INT_MAX;            for (int i = 0; i < 8; i++) {                const int hint = shifted_frame_offset[i];

Lines 1–500 of 1605.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t