2020-09-09 05:39:55 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2023-03-16 23:25:32 +02:00
|
|
|
#include "libavutil/hdr_dynamic_metadata.h"
|
2020-11-26 01:28:42 +02:00
|
|
|
#include "libavutil/film_grain_params.h"
|
2023-03-06 01:32:23 +02:00
|
|
|
#include "libavutil/mastering_display_metadata.h"
|
2020-09-09 05:39:55 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2020-11-15 23:55:41 +02:00
|
|
|
#include "libavutil/opt.h"
|
2020-09-09 05:39:55 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "av1dec.h"
|
2023-03-06 01:32:23 +02:00
|
|
|
#include "atsc_a53.h"
|
2020-09-17 05:09:19 +02:00
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:43:37 +02:00
|
|
|
#include "decode.h"
|
2020-09-09 05:39:55 +02:00
|
|
|
#include "hwconfig.h"
|
|
|
|
#include "profiles.h"
|
2022-02-06 15:49:23 +02:00
|
|
|
#include "thread.h"
|
2020-09-09 05:39:55 +02:00
|
|
|
|
2021-10-12 10:23:58 +02:00
|
|
|
/**< 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
|
|
|
|
};
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
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;
|
2020-10-28 16:23:44 +02:00
|
|
|
int32_t r, prev_gm_param;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-10-28 16:23:44 +02:00
|
|
|
/* 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];
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
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;
|
2020-10-28 16:23:44 +02:00
|
|
|
r = (prev_gm_param >> prec_diff) - sub;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-12 10:23:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
/**
|
2022-11-28 17:38:57 +02:00
|
|
|
* update gm type/params, since cbs already implemented part of this function,
|
2020-09-09 05:39:55 +02:00
|
|
|
* 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);
|
|
|
|
}
|
2021-10-12 10:23:58 +02:00
|
|
|
if (type <= AV1_WARP_MODEL_AFFINE) {
|
|
|
|
s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref);
|
|
|
|
}
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 16:34:28 +02:00
|
|
|
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->skip_mode_present)
|
|
|
|
return;
|
|
|
|
|
|
|
|
forward_idx = -1;
|
|
|
|
backward_idx = -1;
|
|
|
|
for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
|
2020-11-12 22:06:05 +02:00
|
|
|
ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
|
2020-11-03 16:34:28 +02:00
|
|
|
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++) {
|
2020-11-12 22:06:05 +02:00
|
|
|
ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
|
2020-11-03 16:34:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-03 16:54:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 22:02:56 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
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;
|
2020-09-17 05:09:19 +02:00
|
|
|
GetByteContext gb;
|
2020-09-09 05:39:55 +02:00
|
|
|
uint16_t tile_num, tile_row, tile_col;
|
2020-09-17 05:09:19 +02:00
|
|
|
uint32_t size = 0, size_bytes = 0;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
2020-09-17 05:09:19 +02:00
|
|
|
bytestream2_init(&gb, tile_group->tile_data.data,
|
|
|
|
tile_group->tile_data.data_size);
|
2020-09-09 05:39:55 +02:00
|
|
|
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) {
|
2020-09-17 05:09:19 +02:00
|
|
|
s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
|
|
|
|
s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
|
2020-09-09 05:39:55 +02:00
|
|
|
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;
|
2020-09-17 05:09:19 +02:00
|
|
|
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++;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
s->tile_group_info[tile_num].tile_size = size;
|
2020-09-17 05:09:19 +02:00
|
|
|
s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
|
2020-09-09 05:39:55 +02:00
|
|
|
s->tile_group_info[tile_num].tile_row = tile_row;
|
|
|
|
s->tile_group_info[tile_num].tile_column = tile_col;
|
|
|
|
|
2020-09-17 05:09:19 +02:00
|
|
|
bytestream2_skipu(&gb, size);
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_pixel_format(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
const AV1RawSequenceHeader *seq = s->raw_seq;
|
|
|
|
uint8_t bit_depth;
|
|
|
|
int ret;
|
|
|
|
enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
|
2020-11-05 15:13:14 +02:00
|
|
|
#define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
|
|
|
|
CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
|
|
|
|
CONFIG_AV1_NVDEC_HWACCEL + \
|
2022-05-16 15:15:58 +02:00
|
|
|
CONFIG_AV1_VAAPI_HWACCEL + \
|
|
|
|
CONFIG_AV1_VDPAU_HWACCEL)
|
2020-09-12 22:39:45 +02:00
|
|
|
enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
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(avctx, AV_LOG_ERROR,
|
|
|
|
"Unknown AV1 profile %d.\n", seq->seq_profile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
|
|
|
|
} else if (seq->color_config.subsampling_x == 1 &&
|
|
|
|
seq->color_config.subsampling_y == 0) {
|
|
|
|
if (bit_depth == 8)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P;
|
|
|
|
else if (bit_depth == 10)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P10;
|
|
|
|
else if (bit_depth == 12)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P12;
|
|
|
|
else
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
|
|
|
|
} else if (seq->color_config.subsampling_x == 1 &&
|
|
|
|
seq->color_config.subsampling_y == 1) {
|
|
|
|
if (bit_depth == 8)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
else if (bit_depth == 10)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P10;
|
|
|
|
else if (bit_depth == 12)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P12;
|
|
|
|
else
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-06 06:25:29 +02:00
|
|
|
if (bit_depth == 8)
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY8;
|
|
|
|
else if (bit_depth == 10)
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY10;
|
|
|
|
else if (bit_depth == 12)
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY12;
|
2020-09-09 05:39:55 +02:00
|
|
|
else
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
|
|
|
|
av_get_pix_fmt_name(pix_fmt));
|
|
|
|
|
|
|
|
if (pix_fmt == AV_PIX_FMT_NONE)
|
|
|
|
return -1;
|
|
|
|
|
2022-03-22 20:35:19 +02:00
|
|
|
switch (pix_fmt) {
|
2020-10-29 08:54:25 +02:00
|
|
|
case AV_PIX_FMT_YUV420P:
|
2020-11-05 15:13:14 +02:00
|
|
|
#if CONFIG_AV1_DXVA2_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_DXVA2_VLD;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_D3D11VA_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
|
|
|
|
*fmtp++ = AV_PIX_FMT_D3D11;
|
|
|
|
#endif
|
2020-10-17 18:16:54 +02:00
|
|
|
#if CONFIG_AV1_NVDEC_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_CUDA;
|
|
|
|
#endif
|
2020-10-29 08:54:25 +02:00
|
|
|
#if CONFIG_AV1_VAAPI_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_VAAPI;
|
2022-05-16 15:15:58 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_VDPAU_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_VDPAU;
|
2020-10-29 08:54:25 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case AV_PIX_FMT_YUV420P10:
|
2020-11-05 15:13:14 +02:00
|
|
|
#if CONFIG_AV1_DXVA2_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_DXVA2_VLD;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_D3D11VA_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
|
|
|
|
*fmtp++ = AV_PIX_FMT_D3D11;
|
|
|
|
#endif
|
2020-10-17 18:16:54 +02:00
|
|
|
#if CONFIG_AV1_NVDEC_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_CUDA;
|
|
|
|
#endif
|
2020-10-29 08:54:25 +02:00
|
|
|
#if CONFIG_AV1_VAAPI_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_VAAPI;
|
2022-05-16 15:15:58 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_VDPAU_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_VDPAU;
|
2020-12-06 06:25:29 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case AV_PIX_FMT_GRAY8:
|
|
|
|
#if CONFIG_AV1_NVDEC_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_CUDA;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case AV_PIX_FMT_GRAY10:
|
|
|
|
#if CONFIG_AV1_NVDEC_HWACCEL
|
|
|
|
*fmtp++ = AV_PIX_FMT_CUDA;
|
2020-10-29 08:54:25 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:35:19 +02:00
|
|
|
*fmtp++ = pix_fmt;
|
2020-09-09 05:39:55 +02:00
|
|
|
*fmtp = AV_PIX_FMT_NONE;
|
2020-09-12 22:39:45 +02:00
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = ff_thread_get_format(avctx, pix_fmts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-09-12 22:39:45 +02:00
|
|
|
/**
|
|
|
|
* check if the HW accel is inited correctly. If not, return un-implemented.
|
|
|
|
* Since now the av1 decoder doesn't support native decode, if it will be
|
|
|
|
* implemented in the future, need remove this check.
|
|
|
|
*/
|
|
|
|
if (!avctx->hwaccel) {
|
2022-11-28 17:38:58 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Your platform doesn't support"
|
2020-09-12 22:39:45 +02:00
|
|
|
" hardware accelerated AV1 decoding.\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:35:19 +02:00
|
|
|
s->pix_fmt = pix_fmt;
|
2020-09-09 05:39:55 +02:00
|
|
|
avctx->pix_fmt = ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
|
|
|
|
{
|
2022-02-06 15:49:23 +02:00
|
|
|
ff_thread_release_buffer(avctx, f->f);
|
2020-09-09 05:39:55 +02:00
|
|
|
av_buffer_unref(&f->hwaccel_priv_buf);
|
|
|
|
f->hwaccel_picture_private = NULL;
|
2020-11-12 21:59:37 +02:00
|
|
|
av_buffer_unref(&f->header_ref);
|
|
|
|
f->raw_frame_header = NULL;
|
2020-10-16 18:34:31 +02:00
|
|
|
f->spatial_id = f->temporal_id = 0;
|
2020-11-03 16:34:28 +02:00
|
|
|
memset(f->skip_mode_frame_idx, 0,
|
|
|
|
2 * sizeof(uint8_t));
|
2020-11-25 22:02:56 +02:00
|
|
|
memset(&f->film_grain, 0, sizeof(f->film_grain));
|
2020-11-03 16:54:44 +02:00
|
|
|
f->coded_lossless = 0;
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-01-07 21:11:12 +02:00
|
|
|
ret = av_buffer_replace(&dst->header_ref, src->header_ref);
|
2020-09-09 05:39:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-11-12 21:59:37 +02:00
|
|
|
dst->raw_frame_header = src->raw_frame_header;
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if (!src->f->buf[0])
|
2022-01-07 21:11:12 +02:00
|
|
|
return 0;
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
ret = av_frame_ref(dst->f, src->f);
|
2022-01-07 21:11:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
if (src->hwaccel_picture_private) {
|
|
|
|
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
|
|
|
|
if (!dst->hwaccel_priv_buf)
|
|
|
|
goto fail;
|
|
|
|
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:34:31 +02:00
|
|
|
dst->spatial_id = src->spatial_id;
|
|
|
|
dst->temporal_id = src->temporal_id;
|
2021-10-12 10:23:58 +02:00
|
|
|
memcpy(dst->gm_invalid,
|
|
|
|
src->gm_invalid,
|
|
|
|
AV1_NUM_REF_FRAMES * sizeof(uint8_t));
|
2020-09-09 05:39:55 +02:00
|
|
|
memcpy(dst->gm_type,
|
|
|
|
src->gm_type,
|
|
|
|
AV1_NUM_REF_FRAMES * sizeof(uint8_t));
|
|
|
|
memcpy(dst->gm_params,
|
|
|
|
src->gm_params,
|
|
|
|
AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
|
2020-11-03 16:34:28 +02:00
|
|
|
memcpy(dst->skip_mode_frame_idx,
|
|
|
|
src->skip_mode_frame_idx,
|
|
|
|
2 * sizeof(uint8_t));
|
2020-11-25 22:02:56 +02:00
|
|
|
memcpy(&dst->film_grain,
|
|
|
|
&src->film_grain,
|
|
|
|
sizeof(dst->film_grain));
|
2020-11-03 16:54:44 +02:00
|
|
|
dst->coded_lossless = src->coded_lossless;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av1_frame_unref(avctx, dst);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int av1_decode_free(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
2023-03-06 01:32:23 +02:00
|
|
|
AV1RawMetadataITUTT35 itut_t35;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
|
2020-09-14 06:37:24 +02:00
|
|
|
av1_frame_unref(avctx, &s->ref[i]);
|
2022-02-06 15:49:23 +02:00
|
|
|
av_frame_free(&s->ref[i].f);
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
2020-09-14 06:37:24 +02:00
|
|
|
av1_frame_unref(avctx, &s->cur_frame);
|
2022-02-06 15:49:23 +02:00
|
|
|
av_frame_free(&s->cur_frame.f);
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
av_buffer_unref(&s->seq_ref);
|
|
|
|
av_buffer_unref(&s->header_ref);
|
2023-03-06 01:32:23 +02:00
|
|
|
av_buffer_unref(&s->cll_ref);
|
|
|
|
av_buffer_unref(&s->mdcv_ref);
|
2020-09-09 05:39:55 +02:00
|
|
|
av_freep(&s->tile_group_info);
|
|
|
|
|
2023-03-06 01:32:23 +02:00
|
|
|
while (s->itut_t35_fifo && av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
|
|
|
|
av_buffer_unref(&itut_t35.payload_ref);
|
|
|
|
av_fifo_freep2(&s->itut_t35_fifo);
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
ff_cbs_fragment_free(&s->current_obu);
|
|
|
|
ff_cbs_close(&s->cbc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_context_with_sequence(AVCodecContext *avctx,
|
|
|
|
const AV1RawSequenceHeader *seq)
|
|
|
|
{
|
2020-09-25 16:20:41 +02:00
|
|
|
int width = seq->max_frame_width_minus_1 + 1;
|
|
|
|
int height = seq->max_frame_height_minus_1 + 1;
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
avctx->profile = seq->seq_profile;
|
|
|
|
avctx->level = seq->seq_level_idx[0];
|
|
|
|
|
|
|
|
avctx->color_range =
|
|
|
|
seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
|
|
|
|
avctx->color_primaries = seq->color_config.color_primaries;
|
|
|
|
avctx->colorspace = seq->color_config.color_primaries;
|
|
|
|
avctx->color_trc = seq->color_config.transfer_characteristics;
|
|
|
|
|
2020-09-09 23:42:00 +02:00
|
|
|
switch (seq->color_config.chroma_sample_position) {
|
|
|
|
case AV1_CSP_VERTICAL:
|
|
|
|
avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
|
|
|
|
break;
|
|
|
|
case AV1_CSP_COLOCATED:
|
|
|
|
avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-16 17:39:19 +02:00
|
|
|
if (seq->film_grain_params_present)
|
|
|
|
avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
|
|
|
|
else
|
|
|
|
avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
|
|
|
|
|
2020-09-25 16:20:41 +02:00
|
|
|
if (avctx->width != width || avctx->height != height) {
|
|
|
|
int ret = ff_set_dimensions(avctx, width, height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
if (seq->timing_info.num_units_in_display_tick &&
|
|
|
|
seq->timing_info.time_scale) {
|
|
|
|
av_reduce(&avctx->framerate.den, &avctx->framerate.num,
|
|
|
|
seq->timing_info.num_units_in_display_tick,
|
|
|
|
seq->timing_info.time_scale,
|
|
|
|
INT_MAX);
|
|
|
|
if (seq->timing_info.equal_picture_interval)
|
|
|
|
avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_context_with_frame_header(AVCodecContext *avctx,
|
|
|
|
const AV1RawFrameHeader *header)
|
|
|
|
{
|
|
|
|
AVRational aspect_ratio;
|
|
|
|
int width = header->frame_width_minus_1 + 1;
|
|
|
|
int height = header->frame_height_minus_1 + 1;
|
|
|
|
int r_width = header->render_width_minus_1 + 1;
|
|
|
|
int r_height = header->render_height_minus_1 + 1;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (avctx->width != width || avctx->height != height) {
|
|
|
|
ret = ff_set_dimensions(avctx, width, height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_reduce(&aspect_ratio.num, &aspect_ratio.den,
|
|
|
|
(int64_t)height * r_width,
|
|
|
|
(int64_t)width * r_height,
|
|
|
|
INT_MAX);
|
|
|
|
|
2020-09-09 21:57:50 +02:00
|
|
|
if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = ff_set_sar(avctx, aspect_ratio);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:03:26 +02:00
|
|
|
static const CodedBitstreamUnitType decompose_unit_types[] = {
|
|
|
|
AV1_OBU_FRAME,
|
|
|
|
AV1_OBU_FRAME_HEADER,
|
|
|
|
AV1_OBU_METADATA,
|
|
|
|
AV1_OBU_REDUNDANT_FRAME_HEADER,
|
|
|
|
AV1_OBU_SEQUENCE_HEADER,
|
|
|
|
AV1_OBU_TEMPORAL_DELIMITER,
|
|
|
|
AV1_OBU_TILE_GROUP,
|
|
|
|
};
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
static av_cold int av1_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
AV1RawSequenceHeader *seq;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->avctx = avctx;
|
|
|
|
s->pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
|
2022-02-06 15:49:23 +02:00
|
|
|
s->ref[i].f = av_frame_alloc();
|
|
|
|
if (!s->ref[i].f) {
|
2020-09-09 05:39:55 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to allocate reference frame buffer %d.\n", i);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
s->cur_frame.f = av_frame_alloc();
|
|
|
|
if (!s->cur_frame.f) {
|
2020-09-09 05:39:55 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to allocate current frame buffer.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-03-07 14:03:26 +02:00
|
|
|
s->cbc->decompose_unit_types = decompose_unit_types;
|
|
|
|
s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
|
|
|
|
|
2023-03-06 01:32:23 +02:00
|
|
|
s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35),
|
|
|
|
AV_FIFO_FLAG_AUTO_GROW);
|
|
|
|
if (!s->itut_t35_fifo)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2020-11-15 23:55:41 +02:00
|
|
|
av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
if (avctx->extradata && avctx->extradata_size) {
|
2020-11-23 17:36:20 +02:00
|
|
|
ret = ff_cbs_read_extradata_from_codec(s->cbc,
|
|
|
|
&s->current_obu,
|
|
|
|
avctx);
|
2020-09-09 05:39:55 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
|
2020-09-13 21:35:12 +02:00
|
|
|
return ret;
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
|
|
|
|
if (!seq) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = set_context_with_sequence(avctx, seq);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
ff_cbs_fragment_reset(&s->current_obu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
AV1RawFrameHeader *header= s->raw_frame_header;
|
|
|
|
AVFrame *frame;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = update_context_with_frame_header(avctx, header);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if ((ret = ff_thread_get_buffer(avctx, f->f, AV_GET_BUFFER_FLAG_REF)) < 0)
|
2020-12-04 19:09:48 +02:00
|
|
|
goto fail;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
frame = f->f;
|
2020-09-09 05:39:55 +02:00
|
|
|
frame->key_frame = header->frame_type == AV1_FRAME_KEY;
|
|
|
|
|
|
|
|
switch (header->frame_type) {
|
|
|
|
case AV1_FRAME_KEY:
|
|
|
|
case AV1_FRAME_INTRA_ONLY:
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
break;
|
|
|
|
case AV1_FRAME_INTER:
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
break;
|
|
|
|
case AV1_FRAME_SWITCH:
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_SP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->hwaccel) {
|
|
|
|
const AVHWAccel *hwaccel = avctx->hwaccel;
|
|
|
|
if (hwaccel->frame_priv_data_size) {
|
|
|
|
f->hwaccel_priv_buf =
|
|
|
|
av_buffer_allocz(hwaccel->frame_priv_data_size);
|
2020-12-04 19:09:48 +02:00
|
|
|
if (!f->hwaccel_priv_buf) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2020-09-09 05:39:55 +02:00
|
|
|
goto fail;
|
2020-12-04 19:09:48 +02:00
|
|
|
}
|
2020-09-09 05:39:55 +02:00
|
|
|
f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av1_frame_unref(avctx, f);
|
2020-12-04 19:09:48 +02:00
|
|
|
return ret;
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 01:32:23 +02:00
|
|
|
static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
const AV1RawMetadataITUTT35 *itut_t35)
|
|
|
|
{
|
|
|
|
GetByteContext gb;
|
|
|
|
int ret, provider_code;
|
|
|
|
|
|
|
|
bytestream2_init(&gb, itut_t35->payload, itut_t35->payload_size);
|
|
|
|
|
|
|
|
provider_code = bytestream2_get_be16(&gb);
|
|
|
|
switch (provider_code) {
|
|
|
|
case 0x31: { // atsc_provider_code
|
|
|
|
uint32_t user_identifier = bytestream2_get_be32(&gb);
|
|
|
|
switch (user_identifier) {
|
|
|
|
case MKBETAG('G', 'A', '9', '4'): { // closed captions
|
|
|
|
AVBufferRef *buf = NULL;
|
|
|
|
|
|
|
|
ret = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, buf))
|
|
|
|
av_buffer_unref(&buf);
|
|
|
|
|
|
|
|
avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: // ignore unsupported identifiers
|
|
|
|
break;
|
|
|
|
}
|
2023-03-15 16:00:33 +02:00
|
|
|
break;
|
2023-03-06 01:32:23 +02:00
|
|
|
}
|
|
|
|
case 0x3C: { // smpte_provider_code
|
|
|
|
AVDynamicHDRPlus *hdrplus;
|
|
|
|
int provider_oriented_code = bytestream2_get_be16(&gb);
|
|
|
|
int application_identifier = bytestream2_get_byte(&gb);
|
|
|
|
|
|
|
|
if (itut_t35->itu_t_t35_country_code != 0xB5 ||
|
|
|
|
provider_oriented_code != 1 || application_identifier != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hdrplus = av_dynamic_hdr_plus_create_side_data(frame);
|
|
|
|
if (!hdrplus)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2023-03-16 23:25:32 +02:00
|
|
|
ret = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer,
|
|
|
|
bytestream2_get_bytes_left(&gb));
|
2023-03-06 01:32:23 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: // ignore unsupported provider codes
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int export_metadata(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
AV1RawMetadataITUTT35 itut_t35;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (s->mdcv) {
|
|
|
|
AVMasteringDisplayMetadata *mastering = av_mastering_display_metadata_create_side_data(frame);
|
|
|
|
if (!mastering)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
mastering->display_primaries[i][0] = av_make_q(s->mdcv->primary_chromaticity_x[i], 1 << 16);
|
|
|
|
mastering->display_primaries[i][1] = av_make_q(s->mdcv->primary_chromaticity_y[i], 1 << 16);
|
|
|
|
}
|
|
|
|
mastering->white_point[0] = av_make_q(s->mdcv->white_point_chromaticity_x, 1 << 16);
|
|
|
|
mastering->white_point[1] = av_make_q(s->mdcv->white_point_chromaticity_y, 1 << 16);
|
|
|
|
|
|
|
|
mastering->max_luminance = av_make_q(s->mdcv->luminance_max, 1 << 8);
|
|
|
|
mastering->min_luminance = av_make_q(s->mdcv->luminance_min, 1 << 14);
|
|
|
|
|
|
|
|
mastering->has_primaries = 1;
|
|
|
|
mastering->has_luminance = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->cll) {
|
|
|
|
AVContentLightMetadata *light = av_content_light_metadata_create_side_data(frame);
|
|
|
|
if (!light)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
light->MaxCLL = s->cll->max_cll;
|
|
|
|
light->MaxFALL = s->cll->max_fall;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) {
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = export_itut_t35(avctx, frame, &itut_t35);
|
|
|
|
av_buffer_unref(&itut_t35.payload_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:28:42 +02:00
|
|
|
static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
|
|
|
|
AVFilmGrainParams *fgp;
|
|
|
|
AVFilmGrainAOMParams *aom;
|
|
|
|
|
|
|
|
if (!film_grain->apply_grain)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fgp = av_film_grain_params_create_side_data(frame);
|
|
|
|
if (!fgp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
|
|
|
|
fgp->seed = film_grain->grain_seed;
|
|
|
|
|
|
|
|
aom = &fgp->codec.aom;
|
|
|
|
aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
|
|
|
|
aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
|
|
|
|
aom->ar_coeff_lag = film_grain->ar_coeff_lag;
|
|
|
|
aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
|
|
|
|
aom->grain_scale_shift = film_grain->grain_scale_shift;
|
|
|
|
aom->overlap_flag = film_grain->overlap_flag;
|
|
|
|
aom->limit_output_range = film_grain->clip_to_restricted_range;
|
|
|
|
|
|
|
|
aom->num_y_points = film_grain->num_y_points;
|
|
|
|
for (int i = 0; i < film_grain->num_y_points; i++) {
|
|
|
|
aom->y_points[i][0] = film_grain->point_y_value[i];
|
|
|
|
aom->y_points[i][1] = film_grain->point_y_scaling[i];
|
|
|
|
}
|
|
|
|
aom->num_uv_points[0] = film_grain->num_cb_points;
|
|
|
|
for (int i = 0; i < film_grain->num_cb_points; i++) {
|
|
|
|
aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
|
|
|
|
aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
|
|
|
|
}
|
|
|
|
aom->num_uv_points[1] = film_grain->num_cr_points;
|
|
|
|
for (int i = 0; i < film_grain->num_cr_points; i++) {
|
|
|
|
aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
|
|
|
|
aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 25; i++) {
|
|
|
|
aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
|
|
|
|
aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
aom->uv_mult[0] = film_grain->cb_mult;
|
|
|
|
aom->uv_mult[1] = film_grain->cr_mult;
|
|
|
|
aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
|
|
|
|
aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
|
|
|
|
aom->uv_offset[0] = film_grain->cb_offset;
|
|
|
|
aom->uv_offset[1] = film_grain->cr_offset;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
const AVPacket *pkt, int *got_frame)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
2022-02-06 15:49:23 +02:00
|
|
|
const AVFrame *srcframe = s->cur_frame.f;
|
2020-09-09 05:39:55 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-11-15 23:55:41 +02:00
|
|
|
// TODO: all layers
|
|
|
|
if (s->operating_point_idc &&
|
|
|
|
av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
|
|
|
|
return 0;
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = av_frame_ref(frame, srcframe);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-03-06 01:32:23 +02:00
|
|
|
ret = export_metadata(avctx, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:28:42 +02:00
|
|
|
if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
|
|
|
|
ret = export_film_grain(avctx, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
frame->pts = pkt->pts;
|
|
|
|
frame->pkt_dts = pkt->dts;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2020-09-09 05:39:55 +02:00
|
|
|
frame->pkt_size = pkt->size;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_reference_list(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
const AV1RawFrameHeader *header = s->raw_frame_header;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
|
|
|
|
if (header->refresh_frame_flags & (1 << i)) {
|
2022-01-07 21:11:12 +02:00
|
|
|
av1_frame_unref(avctx, &s->ref[i]);
|
2020-09-09 05:39:55 +02:00
|
|
|
if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to update frame %d in reference list\n", i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_current_frame(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
2022-01-07 21:11:12 +02:00
|
|
|
av1_frame_unref(avctx, &s->cur_frame);
|
2020-09-09 05:39:55 +02:00
|
|
|
|
2022-01-07 21:11:12 +02:00
|
|
|
s->cur_frame.header_ref = av_buffer_ref(s->header_ref);
|
|
|
|
if (!s->cur_frame.header_ref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->cur_frame.raw_frame_header = s->raw_frame_header;
|
|
|
|
|
|
|
|
ret = init_tile_data(s);
|
2020-09-09 05:39:55 +02:00
|
|
|
if (ret < 0) {
|
2022-01-07 21:11:12 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
|
2020-09-09 05:39:55 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-07 21:11:12 +02:00
|
|
|
if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
|
|
|
|
(s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
|
|
|
|
s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
|
|
|
|
(avctx->skip_frame >= AVDISCARD_NONKEY &&
|
|
|
|
s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
|
|
|
|
avctx->skip_frame >= AVDISCARD_ALL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = av1_frame_alloc(avctx, &s->cur_frame);
|
2020-09-09 05:39:55 +02:00
|
|
|
if (ret < 0) {
|
2022-01-07 21:11:12 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to allocate space for current frame.\n");
|
2020-09-09 05:39:55 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
global_motion_params(s);
|
2020-11-03 16:34:28 +02:00
|
|
|
skip_mode_params(s);
|
2020-11-03 16:54:44 +02:00
|
|
|
coded_lossless_param(s);
|
2020-11-25 22:02:56 +02:00
|
|
|
load_grain_params(s);
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
2020-09-09 05:39:55 +02:00
|
|
|
int *got_frame, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
|
|
|
AV1RawTileGroup *raw_tile_group = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
|
|
|
|
s->current_obu.nb_units);
|
|
|
|
|
|
|
|
for (int i = 0; i < s->current_obu.nb_units; i++) {
|
|
|
|
CodedBitstreamUnit *unit = &s->current_obu.units[i];
|
|
|
|
AV1RawOBU *obu = unit->content;
|
2020-10-26 01:00:56 +02:00
|
|
|
const AV1RawOBUHeader *header;
|
|
|
|
|
|
|
|
if (!obu)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
header = &obu->header;
|
2020-09-09 05:39:55 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
|
|
|
|
|
|
|
|
switch (unit->type) {
|
|
|
|
case AV1_OBU_SEQUENCE_HEADER:
|
|
|
|
av_buffer_unref(&s->seq_ref);
|
|
|
|
s->seq_ref = av_buffer_ref(unit->content_ref);
|
|
|
|
if (!s->seq_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->raw_seq = &obu->obu.sequence_header;
|
|
|
|
|
|
|
|
ret = set_context_with_sequence(avctx, s->raw_seq);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
|
2020-10-04 15:21:59 +02:00
|
|
|
s->raw_seq = NULL;
|
2020-09-09 05:39:55 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:55:41 +02:00
|
|
|
s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
if (s->pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
ret = get_pixel_format(avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to get pixel format.\n");
|
2020-10-04 15:21:59 +02:00
|
|
|
s->raw_seq = NULL;
|
2020-09-09 05:39:55 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->hwaccel && avctx->hwaccel->decode_params) {
|
|
|
|
ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
|
|
|
|
unit->data_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
|
2020-10-04 15:21:59 +02:00
|
|
|
s->raw_seq = NULL;
|
2020-09-09 05:39:55 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV1_OBU_REDUNDANT_FRAME_HEADER:
|
|
|
|
if (s->raw_frame_header)
|
|
|
|
break;
|
|
|
|
// fall-through
|
|
|
|
case AV1_OBU_FRAME:
|
|
|
|
case AV1_OBU_FRAME_HEADER:
|
2020-09-25 16:05:30 +02:00
|
|
|
if (!s->raw_seq) {
|
2020-09-09 05:39:55 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_buffer_unref(&s->header_ref);
|
|
|
|
s->header_ref = av_buffer_ref(unit->content_ref);
|
|
|
|
if (!s->header_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unit->type == AV1_OBU_FRAME)
|
|
|
|
s->raw_frame_header = &obu->obu.frame.header;
|
|
|
|
else
|
|
|
|
s->raw_frame_header = &obu->obu.frame_header;
|
|
|
|
|
|
|
|
if (s->raw_frame_header->show_existing_frame) {
|
2022-01-07 21:11:12 +02:00
|
|
|
av1_frame_unref(avctx, &s->cur_frame);
|
2020-09-10 00:14:01 +02:00
|
|
|
|
|
|
|
ret = av1_frame_ref(avctx, &s->cur_frame,
|
|
|
|
&s->ref[s->raw_frame_header->frame_to_show_map_idx]);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = update_reference_list(avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if (s->cur_frame.f->buf[0]) {
|
2022-01-07 21:11:12 +02:00
|
|
|
ret = set_output_frame(avctx, frame, pkt, got_frame);
|
|
|
|
if (ret < 0)
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
|
|
|
|
}
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
s->raw_frame_header = NULL;
|
|
|
|
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = get_current_frame(avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:34:31 +02:00
|
|
|
s->cur_frame.spatial_id = header->spatial_id;
|
|
|
|
s->cur_frame.temporal_id = header->temporal_id;
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = avctx->hwaccel->start_frame(avctx, unit->data,
|
|
|
|
unit->data_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (unit->type != AV1_OBU_FRAME)
|
|
|
|
break;
|
|
|
|
// fall-through
|
|
|
|
case AV1_OBU_TILE_GROUP:
|
|
|
|
if (!s->raw_frame_header) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unit->type == AV1_OBU_FRAME)
|
|
|
|
raw_tile_group = &obu->obu.frame.tile_group;
|
|
|
|
else
|
|
|
|
raw_tile_group = &obu->obu.tile_group;
|
|
|
|
|
2020-09-17 05:09:19 +02:00
|
|
|
ret = get_tiles_info(avctx, raw_tile_group);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = avctx->hwaccel->decode_slice(avctx,
|
|
|
|
raw_tile_group->tile_data.data,
|
|
|
|
raw_tile_group->tile_data.data_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"HW accel decode slice fail.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV1_OBU_TILE_LIST:
|
|
|
|
case AV1_OBU_TEMPORAL_DELIMITER:
|
|
|
|
case AV1_OBU_PADDING:
|
2023-03-06 01:32:23 +02:00
|
|
|
break;
|
2020-09-09 05:39:55 +02:00
|
|
|
case AV1_OBU_METADATA:
|
2023-03-06 01:32:23 +02:00
|
|
|
switch (obu->obu.metadata.metadata_type) {
|
|
|
|
case AV1_METADATA_TYPE_HDR_CLL:
|
|
|
|
av_buffer_unref(&s->cll_ref);
|
|
|
|
s->cll_ref = av_buffer_ref(unit->content_ref);
|
|
|
|
if (!s->cll_ref) {
|
|
|
|
s->cll = NULL;
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
s->cll = &obu->obu.metadata.metadata.hdr_cll;
|
|
|
|
break;
|
|
|
|
case AV1_METADATA_TYPE_HDR_MDCV:
|
|
|
|
av_buffer_unref(&s->mdcv_ref);
|
|
|
|
s->mdcv_ref = av_buffer_ref(unit->content_ref);
|
|
|
|
if (!s->mdcv_ref) {
|
|
|
|
s->mdcv = NULL;
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
s->mdcv = &obu->obu.metadata.metadata.hdr_mdcv;
|
|
|
|
break;
|
|
|
|
case AV1_METADATA_TYPE_ITUT_T35: {
|
|
|
|
AV1RawMetadataITUTT35 itut_t35;
|
|
|
|
memcpy(&itut_t35, &obu->obu.metadata.metadata.itut_t35, sizeof(itut_t35));
|
|
|
|
itut_t35.payload_ref = av_buffer_ref(obu->obu.metadata.metadata.itut_t35.payload_ref);
|
|
|
|
if (!itut_t35.payload_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret = av_fifo_write(s->itut_t35_fifo, &itut_t35, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_buffer_unref(&itut_t35.payload_ref);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-09-09 05:39:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_DEBUG,
|
|
|
|
"Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
|
|
|
|
unit->type, unit->data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
|
2022-02-06 15:49:23 +02:00
|
|
|
if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = avctx->hwaccel->end_frame(avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = update_reference_list(avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:49:23 +02:00
|
|
|
if (s->raw_frame_header->show_frame && s->cur_frame.f->buf[0]) {
|
2020-09-09 05:39:55 +02:00
|
|
|
ret = set_output_frame(avctx, frame, pkt, got_frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
raw_tile_group = NULL;
|
|
|
|
s->raw_frame_header = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
ff_cbs_fragment_reset(&s->current_obu);
|
2020-10-04 15:21:59 +02:00
|
|
|
if (ret < 0)
|
|
|
|
s->raw_frame_header = NULL;
|
2020-09-09 05:39:55 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void av1_decode_flush(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
AV1DecContext *s = avctx->priv_data;
|
2023-03-06 01:32:23 +02:00
|
|
|
AV1RawMetadataITUTT35 itut_t35;
|
2020-09-09 05:39:55 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
|
|
|
|
av1_frame_unref(avctx, &s->ref[i]);
|
|
|
|
|
|
|
|
av1_frame_unref(avctx, &s->cur_frame);
|
2020-11-15 23:55:41 +02:00
|
|
|
s->operating_point_idc = 0;
|
2020-09-09 05:39:55 +02:00
|
|
|
s->raw_frame_header = NULL;
|
|
|
|
s->raw_seq = NULL;
|
2023-03-06 01:32:23 +02:00
|
|
|
s->cll = NULL;
|
|
|
|
s->mdcv = NULL;
|
|
|
|
while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
|
|
|
|
av_buffer_unref(&itut_t35.payload_ref);
|
2020-09-24 23:54:14 +02:00
|
|
|
|
|
|
|
ff_cbs_flush(s->cbc);
|
2020-09-09 05:39:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:55:41 +02:00
|
|
|
#define OFFSET(x) offsetof(AV1DecContext, x)
|
|
|
|
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption av1_options[] = {
|
|
|
|
{ "operating_point", "Select an operating point of the scalable bitstream",
|
|
|
|
OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass av1_class = {
|
|
|
|
.class_name = "AV1 decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = av1_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_av1_decoder = {
|
|
|
|
.p.name = "av1",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Alliance for Open Media AV1"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_AV1,
|
2020-09-09 05:39:55 +02:00
|
|
|
.priv_data_size = sizeof(AV1DecContext),
|
|
|
|
.init = av1_decode_init,
|
|
|
|
.close = av1_decode_free,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(av1_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
|
2020-09-09 05:39:55 +02:00
|
|
|
FF_CODEC_CAP_SETS_PKT_DTS,
|
|
|
|
.flush = av1_decode_flush,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
|
|
|
|
.p.priv_class = &av1_class,
|
2021-12-15 10:06:37 +02:00
|
|
|
.bsfs = "av1_frame_split",
|
2020-12-27 18:36:59 +02:00
|
|
|
.hw_configs = (const AVCodecHWConfigInternal *const []) {
|
2020-11-05 15:13:14 +02:00
|
|
|
#if CONFIG_AV1_DXVA2_HWACCEL
|
|
|
|
HWACCEL_DXVA2(av1),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_D3D11VA_HWACCEL
|
|
|
|
HWACCEL_D3D11VA(av1),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_AV1_D3D11VA2_HWACCEL
|
|
|
|
HWACCEL_D3D11VA2(av1),
|
|
|
|
#endif
|
2020-10-17 18:16:54 +02:00
|
|
|
#if CONFIG_AV1_NVDEC_HWACCEL
|
|
|
|
HWACCEL_NVDEC(av1),
|
|
|
|
#endif
|
2020-10-29 08:54:25 +02:00
|
|
|
#if CONFIG_AV1_VAAPI_HWACCEL
|
|
|
|
HWACCEL_VAAPI(av1),
|
|
|
|
#endif
|
2022-05-16 15:15:58 +02:00
|
|
|
#if CONFIG_AV1_VDPAU_HWACCEL
|
|
|
|
HWACCEL_VDPAU(av1),
|
|
|
|
#endif
|
|
|
|
|
2020-09-09 05:39:55 +02:00
|
|
|
NULL
|
|
|
|
},
|
|
|
|
};
|