1629 lines · 52.1 KB · C
RawDownload
/* * AV1 video decoder * * 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 "config_components.h" #include "libavutil/hdr_dynamic_metadata.h"#include "libavutil/film_grain_params.h"#include "libavutil/mastering_display_metadata.h"#include "libavutil/mem.h"#include "libavutil/pixdesc.h"#include "libavutil/opt.h"#include "avcodec.h"#include "av1_parse.h"#include "av1dec.h"#include "atsc_a53.h"#include "bytestream.h"#include "codec_internal.h"#include "decode.h"#include "hwaccel_internal.h"#include "internal.h"#include "itut35.h"#include "hwconfig.h"#include "profiles.h"#include "progressframe.h"#include "libavutil/refstruct.h" /**< same with Div_Lut defined in spec 7.11.3.7 */static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {  16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,  15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,  15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,  14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,  13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,  13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,  13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,  12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,  12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,  11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,  11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,  11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,  10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,  10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,  10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,  9963,  9939,  9916,  9892,  9869,  9846,  9823,  9800,  9777,  9754,  9732,  9709,  9687,  9664,  9642,  9620,  9598,  9576,  9554,  9533,  9511,  9489,  9468,  9447,  9425,  9404,  9383,  9362,  9341,  9321,  9300,  9279,  9259,  9239,  9218,  9198,  9178,  9158,  9138,  9118,  9098,  9079,  9059,  9039,  9020,  9001,  8981,  8962,  8943,  8924,  8905,  8886,  8867,  8849,  8830,  8812,  8793,  8775,  8756,  8738,  8720,  8702,  8684,  8666,  8648,  8630,  8613,  8595,  8577,  8560,  8542,  8525,  8508,  8490,  8473,  8456,  8439,  8422,  8405,  8389,  8372,  8355,  8339,  8322,  8306,  8289,  8273,  8257,  8240,  8224,  8208,  8192}; static uint32_t inverse_recenter(int r, uint32_t v){    if (v > 2 * r)        return v;    else if (v & 1)        return r - ((v + 1) >> 1);    else        return r + (v >> 1);} static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,                                                int mx, int r){    if ((r << 1) <= mx) {        return inverse_recenter(r, sub_exp);    } else {        return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);    }} static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,                                             int high, int r){    int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);    return x + low;} static void read_global_param(AV1DecContext *s, int type, int ref, int idx){    uint8_t primary_frame, prev_frame;    uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;    int32_t r, prev_gm_param;     primary_frame = s->raw_frame_header->primary_ref_frame;    prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];    abs_bits = AV1_GM_ABS_ALPHA_BITS;    prec_bits = AV1_GM_ALPHA_PREC_BITS;     /* setup_past_independence() sets PrevGmParams to default values. We can     * simply point to the current's frame gm_params as they will be initialized     * with defaults at this point.     */    if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)        prev_gm_param = s->cur_frame.gm_params[ref][idx];    else        prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];     if (idx < 2) {        if (type == AV1_WARP_MODEL_TRANSLATION) {            abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -                !s->raw_frame_header->allow_high_precision_mv;            prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -                !s->raw_frame_header->allow_high_precision_mv;        } else {            abs_bits = AV1_GM_ABS_TRANS_BITS;            prec_bits = AV1_GM_TRANS_PREC_BITS;        }    }    round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;    prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;    sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;    mx = 1 << abs_bits;    r = (prev_gm_param >> prec_diff) - sub;     s->cur_frame.gm_params[ref][idx] =        (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],                                       -mx, mx + 1, r) << prec_diff) + round;} static uint64_t round_two(uint64_t x, uint16_t n){    if (n == 0)        return x;    return ((x + ((uint64_t)1 << (n - 1))) >> n);} static int64_t round_two_signed(int64_t x, uint16_t n){    return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n));} /** * Resolve divisor process. * see spec 7.11.3.7 */static int16_t resolve_divisor(uint32_t d, uint16_t *shift){    int32_t e, f;     *shift = av_log2(d);    e = d - (1 << (*shift));    if (*shift > AV1_DIV_LUT_BITS)        f = round_two(e, *shift - AV1_DIV_LUT_BITS);    else        f = e << (AV1_DIV_LUT_BITS - (*shift));     *shift += AV1_DIV_LUT_PREC_BITS;     return div_lut[f];} /** * check if global motion params is valid. * see spec 7.11.3.6 */static uint8_t get_shear_params_valid(AV1DecContext *s, int idx){    int16_t alpha, beta, gamma, delta, divf, divs;    int64_t v, w;    int32_t *param = &s->cur_frame.gm_params[idx][0];    if (param[2] <= 0)        return 0;     alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS));    beta  = av_clip_int16(param[3]);    divf  = resolve_divisor(abs(param[2]), &divs);    v     = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS);    w     = (int64_t)param[3] * param[4];    gamma = av_clip_int16((int)round_two_signed((v * divf), divs));    delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS));     alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;    beta  = round_two_signed(beta,  AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;    gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;    delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;     if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) ||        (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS))        return 0;     return 1;} /*** update gm type/params, since cbs already implemented part of this function,* so we don't need to full implement spec.*/static void global_motion_params(AV1DecContext *s){    const AV1RawFrameHeader *header = s->raw_frame_header;    int type, ref;     for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {        s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;        for (int i = 0; i < 6; i++)            s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?                                             1 << AV1_WARPEDMODEL_PREC_BITS : 0;    }    if (header->frame_type == AV1_FRAME_KEY ||        header->frame_type == AV1_FRAME_INTRA_ONLY)        return;     for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {        if (header->is_global[ref]) {            if (header->is_rot_zoom[ref]) {                type = AV1_WARP_MODEL_ROTZOOM;            } else {                type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION                                                   : AV1_WARP_MODEL_AFFINE;            }        } else {            type = AV1_WARP_MODEL_IDENTITY;        }        s->cur_frame.gm_type[ref] = type;         if (type >= AV1_WARP_MODEL_ROTZOOM) {            read_global_param(s, type, ref, 2);            read_global_param(s, type, ref, 3);            if (type == AV1_WARP_MODEL_AFFINE) {                read_global_param(s, type, ref, 4);                read_global_param(s, type, ref, 5);            } else {                s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];                s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];            }        }        if (type >= AV1_WARP_MODEL_TRANSLATION) {            read_global_param(s, type, ref, 0);            read_global_param(s, type, ref, 1);        }        if (type <= AV1_WARP_MODEL_AFFINE) {            s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref);        }    }} static int get_relative_dist(const AV1RawSequenceHeader *seq,                             unsigned int a, unsigned int b){    unsigned int diff = a - b;    unsigned int m = 1 << seq->order_hint_bits_minus_1;    return (diff & (m - 1)) - (diff & m);} static void skip_mode_params(AV1DecContext *s){    const AV1RawFrameHeader *header = s->raw_frame_header;    const AV1RawSequenceHeader *seq = s->raw_seq;     int forward_idx,  backward_idx;    int forward_hint, backward_hint;    int second_forward_idx, second_forward_hint;    int ref_hint, dist, i;     if (header->frame_type == AV1_FRAME_KEY ||        header->frame_type == AV1_FRAME_INTRA_ONLY ||        !header->reference_select || !seq->enable_order_hint)        return;     forward_idx  = -1;    backward_idx = -1;    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {        if (!s->ref[header->ref_frame_idx[i]].raw_frame_header)            return;        ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;        dist = get_relative_dist(seq, ref_hint, header->order_hint);        if (dist < 0) {            if (forward_idx < 0 ||                get_relative_dist(seq, ref_hint, forward_hint) > 0) {                forward_idx  = i;                forward_hint = ref_hint;            }        } else if (dist > 0) {            if (backward_idx < 0 ||                get_relative_dist(seq, ref_hint, backward_hint) < 0) {                backward_idx  = i;                backward_hint = ref_hint;            }        }    }     if (forward_idx < 0) {        return;    } else if (backward_idx >= 0) {        s->cur_frame.skip_mode_frame_idx[0] =            AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);        s->cur_frame.skip_mode_frame_idx[1] =            AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);        return;    }     second_forward_idx = -1;    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {        ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;        if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {            if (second_forward_idx < 0 ||                get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {                second_forward_idx  = i;                second_forward_hint = ref_hint;            }        }    }     if (second_forward_idx < 0)        return;     s->cur_frame.skip_mode_frame_idx[0] =        AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);    s->cur_frame.skip_mode_frame_idx[1] =        AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);} static void coded_lossless_param(AV1DecContext *s){    const AV1RawFrameHeader *header = s->raw_frame_header;    int i;     if (header->delta_q_y_dc || header->delta_q_u_ac ||        header->delta_q_u_dc || header->delta_q_v_ac ||        header->delta_q_v_dc) {        s->cur_frame.coded_lossless = 0;        return;    }     s->cur_frame.coded_lossless = 1;    for (i = 0; i < AV1_MAX_SEGMENTS; i++) {        int qindex;        if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {            qindex = (header->base_q_idx +                      header->feature_value[i][AV1_SEG_LVL_ALT_Q]);        } else {            qindex = header->base_q_idx;        }        qindex = av_clip_uintp2(qindex, 8);         if (qindex) {            s->cur_frame.coded_lossless = 0;            return;        }    }} static void order_hint_info(AV1DecContext *s){    const AV1RawFrameHeader *header = s->raw_frame_header;    const AV1RawSequenceHeader *seq = s->raw_seq;    AV1Frame *frame = &s->cur_frame;     frame->order_hint = header->order_hint;     for (int i = 0; i < AV1_REFS_PER_FRAME; i++) {        int ref_name = i + AV1_REF_FRAME_LAST;        int ref_slot = header->ref_frame_idx[i];        int ref_order_hint = s->ref[ref_slot].order_hint;         frame->order_hints[ref_name] = ref_order_hint;        if (!seq->enable_order_hint) {            frame->ref_frame_sign_bias[ref_name] = 0;        } else {            frame->ref_frame_sign_bias[ref_name] =                get_relative_dist(seq, ref_order_hint,                                  frame->order_hint) > 0;        }    }} static void load_grain_params(AV1DecContext *s){    const AV1RawFrameHeader *header = s->raw_frame_header;    const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;    AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;     if (!film_grain->apply_grain)        return;     if (film_grain->update_grain) {        memcpy(dst, film_grain, sizeof(*dst));        return;    }     src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;     memcpy(dst, src, sizeof(*dst));    dst->grain_seed = film_grain->grain_seed;} static int init_tile_data(AV1DecContext *s) {    int cur_tile_num =        s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;    if (s->tile_num < cur_tile_num) {        int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,                                    sizeof(TileGroupInfo));        if (ret < 0) {            s->tile_num = 0;            return ret;        }    }    s->tile_num = cur_tile_num;     return 0;} static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group){    AV1DecContext *s = avctx->priv_data;    GetByteContext gb;    uint16_t tile_num, tile_row, tile_col;    uint32_t size = 0, size_bytes = 0;     bytestream2_init(&gb, tile_group->tile_data.data,                     tile_group->tile_data.data_size);    s->tg_start = tile_group->tg_start;    s->tg_end = tile_group->tg_end;     for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {        tile_row = tile_num / s->raw_frame_header->tile_cols;        tile_col = tile_num % s->raw_frame_header->tile_cols;         if (tile_num == tile_group->tg_end) {            s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);            s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);            s->tile_group_info[tile_num].tile_row = tile_row;            s->tile_group_info[tile_num].tile_column = tile_col;            return 0;        }        size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;        if (bytestream2_get_bytes_left(&gb) < size_bytes)            return AVERROR_INVALIDDATA;        size = 0;        for (int i = 0; i < size_bytes; i++)            size |= bytestream2_get_byteu(&gb) << 8 * i;        if (bytestream2_get_bytes_left(&gb) <= size)            return AVERROR_INVALIDDATA;        size++;         s->tile_group_info[tile_num].tile_size = size;        s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);        s->tile_group_info[tile_num].tile_row = tile_row;        s->tile_group_info[tile_num].tile_column = tile_col;         bytestream2_skipu(&gb, size);    }     return 0; } static enum AVPixelFormat get_sw_pixel_format(void *logctx,                                              const AV1RawSequenceHeader *seq){    int bit_depth;    enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;     if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)        bit_depth = seq->color_config.twelve_bit ? 12 : 10;    else if (seq->seq_profile <= 2)        bit_depth = seq->color_config.high_bitdepth ? 10 : 8;    else {        av_log(logctx, AV_LOG_ERROR,               "Unknown AV1 profile %d.\n", seq->seq_profile);        return AV_PIX_FMT_NONE;    }     if (!seq->color_config.mono_chrome) {        // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1        if (seq->color_config.subsampling_x == 0 &&            seq->color_config.subsampling_y == 0) {            if (bit_depth == 8)                pix_fmt = AV_PIX_FMT_YUV444P;            else if (bit_depth == 10)                pix_fmt = AV_PIX_FMT_YUV444P10;            else if (bit_depth == 12)                pix_fmt = AV_PIX_FMT_YUV444P12;            else                av_assert0(0);        } else if (seq->color_config.subsampling_x == 1 &&                   seq->color_config.subsampling_y == 0) {

Lines 1–500 of 1629.

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