风湿病是什么原因造成的| 甲醛对人体有什么危害| 硬膜囊前缘受压是什么意思| playboy什么意思| 世界上最深的湖泊是什么| 脱发是什么原因| 稀料对人体有什么危害| 乙肝表面抗体是什么意思| 煮沸除氯是什么意思| 松石绿是什么颜色| 前列腺钙化灶是什么意思| 病毒性肝炎有什么症状| 秋水长天什么意思| 土耳其是什么民族| 精神紊乱吃什么药| 腋臭是什么原因引起的| 2021年是什么年| 葵花宝典是什么意思| 睡着了流口水是什么原因| 葛粉吃了有什么好处| 特诊科是什么意思| 小孩脱水有什么症状| 喉咙痛吃什么水果| 乙型肝炎e抗体阳性是什么意思| 什么是晶体| 夏天适合种什么菜| 交可以组什么词| 夜尿频繁是什么原因| 白茶是什么茶| 头疼吃什么药效果好| 深棕色是什么颜色| 正常人为什么会得梅毒| otc是什么药| 山大王是什么意思| 白术有什么作用| 次氯酸钠是什么| 血竭是什么东西| 中国最厉害的武器是什么| 27属什么| 过氧化氢是什么意思| 鱼油功效和作用是什么| 不拘一格是什么意思| 见人说人话见鬼说鬼话是什么意思| 减肥适合吃什么水果| mi医学上是什么意思| 甲鱼喜欢吃什么| 好汉不吃眼前亏是什么意思| 慢性非萎缩性胃炎是什么意思| 眼睛发痒是什么原因| hbalc是什么意思| 正常人为什么会低血糖| 胖大海是什么东西| 黄鳝不能和什么一起吃| 胃酸反流是什么原因造成| 3月7日是什么星座| 鳞状上皮内低度病变是什么意思| 鲸鱼属于什么类动物| 今天开什么| 泄身是什么意思| 周围神经炎是什么症状| 蚂蝗怕什么| 榴莲吃多了有什么坏处| 吃什么助于睡眠| 日不落是什么意思| 新生儿拉肚子是什么原因引起的| 做健身教练有什么要求| 吃什么生发| 厚颜无耻是什么意思| 口唇发绀是什么意思| 葡萄上的白霜是什么| 和硕是什么意思| 细胞核由什么组成| 11月7日是什么星座| 梦到自己怀孕是什么意思| 1月22号是什么星座| 什么季节最短| 共济会是什么组织| 苦瓜不能跟什么一起吃| 盆腔镜检查是查什么的| 复活节是什么意思| 黄鳝吃什么食物| 上海市市长是什么级别| 巴图是什么意思| 城堡是什么意思| 送同学什么生日礼物好| 狗狗拉虫子又细又长吃什么药| 陆勤合格什么意思| DDP是什么| 病毒性感冒什么症状| 鸭胗是鸭的什么部位| 筋膜提升术是什么| 18岁是什么年华| 杏仁有什么作用| 为什么叫韩国人棒子| 粉尘作业时必须佩戴什么口罩| 火腿炒什么菜好吃| 女性血常规都检查什么| 甲沟炎应该挂什么科| 敞开心扉是什么意思| 大人发烧吃什么药| 阳光明媚是什么意思| 柔顺和拉直有什么区别| 建日是什么意思| 陈皮配什么喝去湿气| 孜字五行属什么| 6月13日什么星座| 深化是什么意思| 角膜炎是什么症状| 心灵鸡汤什么意思| 无名指长代表什么| hitachi是什么品牌| 揉肚子有什么好处| 乾是什么生肖| 口干嗓子干是什么原因| 枸杞有什么作用| 劝君更尽一杯酒的下一句是什么| 端着是什么意思| 为什么早上起来口苦| 鸡属于什么动物| 梦见吃肉是什么意思| 二月二十三日是什么星座| 天蝎女和什么星座最配| 胆囊壁增厚是什么意思| 鞋底md是什么材质| 酱瓜是什么瓜| 鬼长什么样子| 感冒拉肚子吃什么药| 带状疱疹可以吃什么水果| 青葱岁月是什么意思| 共襄盛举是什么意思| 照烧是什么意思| 粿条是什么| 身上发热是什么原因| 土字旁的有什么字| 子宫内膜薄有什么影响| 肤浅是什么意思| 狮子座是什么象星座| 薄荷脑是什么东西| 什么是价值| 什么野菜| 乜贴是什么意思| 香港的海是什么海| 爱趴着睡觉是什么原因| 支气管扩张吃什么药| 熬夜头疼是什么原因| 血小板低会引发什么病| 颈动脉挂什么科| 心率偏低会有什么危害| 黑龙江有什么特产| 9.23号是什么星座| 什么是太岁| 高考三百多分能上什么学校| 武警和特警有什么区别| 小便尿起泡是什么原因| 什么是紫外线| 风湿是什么原因造成的| 蟑螂讨厌什么味道| 蝉是什么| 甘油三酯高吃什么药能降下来| 能力是什么意思| 吃牛油果有什么好处和坏处| 排休是什么意思| 大便脂肪球是什么意思| 孕妇吃什么好对胎儿好三个月前期| 新生儿干呕是什么原因| 猎奇是什么意思| 青蒜是什么| 吃黄瓜有什么好处和坏处| qcy是什么牌子| 手上有红点是什么原因| 沐雨栉风是什么生肖| 痛风要吃什么药好得快| s.m是什么意思| 嘴巴周围长痘痘是什么原因引起的| ng是什么单位| 结婚12年是什么婚| 庶母是什么意思| 想的偏旁是什么| 为什么耳鸣| 支数是什么意思| 洗衣机单漂洗是什么意思| 前面有个豹子是什么车| 中指麻木是什么原因引起的| 中暑吃什么水果| 胃溃疡不能吃什么食物| 奶头疼是什么原因| 学业有成是什么意思| 老戏骨是什么意思| 热闹的什么| 美版苹果和国行有什么区别| 蜱虫用什么药消灭| 人突然消瘦是什么原因| apc是什么牌子| 什么的搏斗| 9月16号是什么星座| 脸上长痤疮用什么药| 栓是什么意思| 副鼻窦炎是什么意思| 为什么用英语怎么说| 就义是什么意思| 什么姿势最深| 眉毛有什么作用| 引火下行是什么意思| 皮牙子是什么意思| 梦见借给别人钱是什么意思| 小肚子大是什么原因| 北京豆汁什么味道| 身是什么结构的字| 男性雄激素低吃什么药| 孕妇什么东西不能吃| 吃什么补红细胞最快| 玉对人体有什么好处| 粘液阳性是什么意思| 骨质疏松吃什么| 尿肌酐低说明什么| tc是什么| 工勤人员是什么意思| 神疲乏力吃什么中成药| 补肾最好的药是什么药| 覆盆子有什么作用| 地黄长什么样| 牛虻是什么| 黄字五行属什么| 开方是什么意思| 乳化是什么意思| 杨梅不能与什么同吃| ct和拍片有什么区别| 什么食物对心脏好| 孕妇感冒了对胎儿有什么影响| 什么洗面奶好用| 铁公鸡是什么意思| 领衔是什么意思| 下嘴唇溃疡是什么原因| 瑶浴是什么意思| 中耳炎是什么| 鸡肉炒什么好吃| 游字五行属什么| 什么时间种白菜| dei是什么意思| 开天辟地是什么生肖| chemical是什么意思| 羽字属于五行属什么| 什么样的花朵| 神经损伤吃什么药最好| 杨颖是什么脸型| 出油多是什么原因| 樱花什么时候开| 淇字五行属什么| 大雄宝殿是什么意思| 家伙是什么意思| labs是什么意思| 宫腔粘连是什么意思| 一个草字头一个氏念什么| 检查乙肝五项挂什么科| 铲子是什么意思| 艾滋病的症状是什么样| 地下党是什么意思| 梦见动物是什么意思| 坐车晕车是什么原因| 铁剂不能与什么同服| 黄油是什么意思| 牛牛是什么| 三高不能吃什么食物| 灰配什么颜色好看| 百度

Guizhous Shuanghe Cave named longest cave in Asia


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2025-08-05 22:29:26
Exec Total Coverage
Lines: 274 886 30.9%
Functions: 12 34 35.3%
Branches: 107 490 21.8%

Line Branch Exec Source
1 /*
2 * AV1 video decoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config_components.h"
22
23 #include "libavutil/hdr_dynamic_metadata.h"
24 #include "libavutil/film_grain_params.h"
25 #include "libavutil/mastering_display_metadata.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/opt.h"
29 #include "avcodec.h"
30 #include "av1_parse.h"
31 #include "av1dec.h"
32 #include "atsc_a53.h"
33 #include "bytestream.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "hwaccel_internal.h"
37 #include "internal.h"
38 #include "itut35.h"
39 #include "hwconfig.h"
40 #include "profiles.h"
41 #include "progressframe.h"
42 #include "libavutil/refstruct.h"
43
44 /** same with Div_Lut defined in spec 7.11.3.7 */
45 static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {
46 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
47 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
48 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
49 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
50 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
51 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
52 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
53 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
54 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
55 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
56 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
57 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
58 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
59 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
60 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
61 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
62 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
63 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
64 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
65 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
66 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
67 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
68 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
69 8240, 8224, 8208, 8192
70 };
71
72 static uint32_t inverse_recenter(int r, uint32_t v)
73 {
74 if (v > 2 * r)
75 return v;
76 else if (v & 1)
77 return r - ((v + 1) >> 1);
78 else
79 return r + (v >> 1);
80 }
81
82 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
83 int mx, int r)
84 {
85 if ((r << 1) <= mx) {
86 return inverse_recenter(r, sub_exp);
87 } else {
88 return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
89 }
90 }
91
92 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
93 int high, int r)
94 {
95 int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
96 return x + low;
97 }
98
99 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
100 {
101 uint8_t primary_frame, prev_frame;
102 uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
103 int32_t r, prev_gm_param;
104
105 primary_frame = s->raw_frame_header->primary_ref_frame;
106 prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
107 abs_bits = AV1_GM_ABS_ALPHA_BITS;
108 prec_bits = AV1_GM_ALPHA_PREC_BITS;
109
110 /* setup_past_independence() sets PrevGmParams to default values. We can
111 * simply point to the current's frame gm_params as they will be initialized
112 * with defaults at this point.
113 */
114 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
115 prev_gm_param = s->cur_frame.gm_params[ref][idx];
116 else
117 prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
118
119 if (idx < 2) {
120 if (type == AV1_WARP_MODEL_TRANSLATION) {
121 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
122 !s->raw_frame_header->allow_high_precision_mv;
123 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
124 !s->raw_frame_header->allow_high_precision_mv;
125 } else {
126 abs_bits = AV1_GM_ABS_TRANS_BITS;
127 prec_bits = AV1_GM_TRANS_PREC_BITS;
128 }
129 }
130 round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
131 prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
132 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
133 mx = 1 << abs_bits;
134 r = (prev_gm_param >> prec_diff) - sub;
135
136 s->cur_frame.gm_params[ref][idx] =
137 (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
138 -mx, mx + 1, r) << prec_diff) + round;
139 }
140
141 static uint64_t round_two(uint64_t x, uint16_t n)
142 {
143 if (n == 0)
144 return x;
145 return ((x + ((uint64_t)1 << (n - 1))) >> n);
146 }
147
148 static int64_t round_two_signed(int64_t x, uint16_t n)
149 {
150 return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n));
151 }
152
153 /**
154 * Resolve divisor process.
155 * see spec 7.11.3.7
156 */
157 static int16_t resolve_divisor(uint32_t d, uint16_t *shift)
158 {
159 int32_t e, f;
160
161 *shift = av_log2(d);
162 e = d - (1 << (*shift));
163 if (*shift > AV1_DIV_LUT_BITS)
164 f = round_two(e, *shift - AV1_DIV_LUT_BITS);
165 else
166 f = e << (AV1_DIV_LUT_BITS - (*shift));
167
168 *shift += AV1_DIV_LUT_PREC_BITS;
169
170 return div_lut[f];
171 }
172
173 /**
174 * check if global motion params is valid.
175 * see spec 7.11.3.6
176 */
177 static uint8_t get_shear_params_valid(AV1DecContext *s, int idx)
178 {
179 int16_t alpha, beta, gamma, delta, divf, divs;
180 int64_t v, w;
181 int32_t *param = &s->cur_frame.gm_params[idx][0];
182 if (param[2] <= 0)
183 return 0;
184
185 alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS));
186 beta = av_clip_int16(param[3]);
187 divf = resolve_divisor(abs(param[2]), &divs);
188 v = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS);
189 w = (int64_t)param[3] * param[4];
190 gamma = av_clip_int16((int)round_two_signed((v * divf), divs));
191 delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS));
192
193 alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
194 beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
195 gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
196 delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
197
198 if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) ||
199 (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS))
200 return 0;
201
202 return 1;
203 }
204
205 /**
206 * update gm type/params, since cbs already implemented part of this function,
207 * so we don't need to full implement spec.
208 */
209 static void global_motion_params(AV1DecContext *s)
210 {
211 const AV1RawFrameHeader *header = s->raw_frame_header;
212 int type, ref;
213
214 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
215 s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
216 for (int i = 0; i < 6; i++)
217 s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
218 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
219 }
220 if (header->frame_type == AV1_FRAME_KEY ||
221 header->frame_type == AV1_FRAME_INTRA_ONLY)
222 return;
223
224 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
225 if (header->is_global[ref]) {
226 if (header->is_rot_zoom[ref]) {
227 type = AV1_WARP_MODEL_ROTZOOM;
228 } else {
229 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
230 : AV1_WARP_MODEL_AFFINE;
231 }
232 } else {
233 type = AV1_WARP_MODEL_IDENTITY;
234 }
235 s->cur_frame.gm_type[ref] = type;
236
237 if (type >= AV1_WARP_MODEL_ROTZOOM) {
238 read_global_param(s, type, ref, 2);
239 read_global_param(s, type, ref, 3);
240 if (type == AV1_WARP_MODEL_AFFINE) {
241 read_global_param(s, type, ref, 4);
242 read_global_param(s, type, ref, 5);
243 } else {
244 s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
245 s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
246 }
247 }
248 if (type >= AV1_WARP_MODEL_TRANSLATION) {
249 read_global_param(s, type, ref, 0);
250 read_global_param(s, type, ref, 1);
251 }
252 if (type <= AV1_WARP_MODEL_AFFINE) {
253 s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref);
254 }
255 }
256 }
257
258 static int get_relative_dist(const AV1RawSequenceHeader *seq,
259 unsigned int a, unsigned int b)
260 {
261 unsigned int diff = a - b;
262 unsigned int m = 1 << seq->order_hint_bits_minus_1;
263 return (diff & (m - 1)) - (diff & m);
264 }
265
266 static void skip_mode_params(AV1DecContext *s)
267 {
268 const AV1RawFrameHeader *header = s->raw_frame_header;
269 const AV1RawSequenceHeader *seq = s->raw_seq;
270
271 int forward_idx, backward_idx;
272 int forward_hint, backward_hint;
273 int second_forward_idx, second_forward_hint;
274 int ref_hint, dist, i;
275
276 if (header->frame_type == AV1_FRAME_KEY ||
277 header->frame_type == AV1_FRAME_INTRA_ONLY ||
278 !header->reference_select || !seq->enable_order_hint)
279 return;
280
281 forward_idx = -1;
282 backward_idx = -1;
283 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
284 if (!s->ref[header->ref_frame_idx[i]].raw_frame_header)
285 return;
286 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
287 dist = get_relative_dist(seq, ref_hint, header->order_hint);
288 if (dist < 0) {
289 if (forward_idx < 0 ||
290 get_relative_dist(seq, ref_hint, forward_hint) > 0) {
291 forward_idx = i;
292 forward_hint = ref_hint;
293 }
294 } else if (dist > 0) {
295 if (backward_idx < 0 ||
296 get_relative_dist(seq, ref_hint, backward_hint) < 0) {
297 backward_idx = i;
298 backward_hint = ref_hint;
299 }
300 }
301 }
302
303 if (forward_idx < 0) {
304 return;
305 } else if (backward_idx >= 0) {
306 s->cur_frame.skip_mode_frame_idx[0] =
307 AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
308 s->cur_frame.skip_mode_frame_idx[1] =
309 AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
310 return;
311 }
312
313 second_forward_idx = -1;
314 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
315 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
316 if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
317 if (second_forward_idx < 0 ||
318 get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
319 second_forward_idx = i;
320 second_forward_hint = ref_hint;
321 }
322 }
323 }
324
325 if (second_forward_idx < 0)
326 return;
327
328 s->cur_frame.skip_mode_frame_idx[0] =
329 AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
330 s->cur_frame.skip_mode_frame_idx[1] =
331 AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
332 }
333
334 static void coded_lossless_param(AV1DecContext *s)
335 {
336 const AV1RawFrameHeader *header = s->raw_frame_header;
337 int i;
338
339 if (header->delta_q_y_dc || header->delta_q_u_ac ||
340 header->delta_q_u_dc || header->delta_q_v_ac ||
341 header->delta_q_v_dc) {
342 s->cur_frame.coded_lossless = 0;
343 return;
344 }
345
346 s->cur_frame.coded_lossless = 1;
347 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
348 int qindex;
349 if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
350 qindex = (header->base_q_idx +
351 header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
352 } else {
353 qindex = header->base_q_idx;
354 }
355 qindex = av_clip_uintp2(qindex, 8);
356
357 if (qindex) {
358 s->cur_frame.coded_lossless = 0;
359 return;
360 }
361 }
362 }
363
364 static void order_hint_info(AV1DecContext *s)
365 {
366 const AV1RawFrameHeader *header = s->raw_frame_header;
367 const AV1RawSequenceHeader *seq = s->raw_seq;
368 AV1Frame *frame = &s->cur_frame;
369
370 frame->order_hint = header->order_hint;
371
372 for (int i = 0; i < AV1_REFS_PER_FRAME; i++) {
373 int ref_name = i + AV1_REF_FRAME_LAST;
374 int ref_slot = header->ref_frame_idx[i];
375 int ref_order_hint = s->ref[ref_slot].order_hint;
376
377 frame->order_hints[ref_name] = ref_order_hint;
378 if (!seq->enable_order_hint) {
379 frame->ref_frame_sign_bias[ref_name] = 0;
380 } else {
381 frame->ref_frame_sign_bias[ref_name] =
382 get_relative_dist(seq, ref_order_hint,
383 frame->order_hint) > 0;
384 }
385 }
386 }
387
388 static void load_grain_params(AV1DecContext *s)
389 {
390 const AV1RawFrameHeader *header = s->raw_frame_header;
391 const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
392 AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
393
394 if (!film_grain->apply_grain)
395 return;
396
397 if (film_grain->update_grain) {
398 memcpy(dst, film_grain, sizeof(*dst));
399 return;
400 }
401
402 src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
403
404 memcpy(dst, src, sizeof(*dst));
405 dst->grain_seed = film_grain->grain_seed;
406 }
407
408 24 static int init_tile_data(AV1DecContext *s)
409
410 {
411 24 int cur_tile_num =
412 24 s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
413
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s->tile_num < cur_tile_num) {
414 24 int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
415 sizeof(TileGroupInfo));
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
417 s->tile_num = 0;
418 return ret;
419 }
420 }
421 24 s->tile_num = cur_tile_num;
422
423 24 return 0;
424 }
425
426 32 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
427 {
428 32 AV1DecContext *s = avctx->priv_data;
429 GetByteContext gb;
430 uint16_t tile_num, tile_row, tile_col;
431 32 uint32_t size = 0, size_bytes = 0;
432
433 32 bytestream2_init(&gb, tile_group->tile_data.data,
434 32 tile_group->tile_data.data_size);
435 32 s->tg_start = tile_group->tg_start;
436 32 s->tg_end = tile_group->tg_end;
437
438
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
439 47 tile_row = tile_num / s->raw_frame_header->tile_cols;
440 47 tile_col = tile_num % s->raw_frame_header->tile_cols;
441
442
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 15 times.
47 if (tile_num == tile_group->tg_end) {
443 32 s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
444 32 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
445 32 s->tile_group_info[tile_num].tile_row = tile_row;
446 32 s->tile_group_info[tile_num].tile_column = tile_col;
447 32 return 0;
448 }
449 15 size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
450
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (bytestream2_get_bytes_left(&gb) < size_bytes)
451 return AVERROR_INVALIDDATA;
452 15 size = 0;
453
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 15 times.
53 for (int i = 0; i < size_bytes; i++)
454 38 size |= bytestream2_get_byteu(&gb) << 8 * i;
455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (bytestream2_get_bytes_left(&gb) <= size)
456 return AVERROR_INVALIDDATA;
457 15 size++;
458
459 15 s->tile_group_info[tile_num].tile_size = size;
460 15 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
461 15 s->tile_group_info[tile_num].tile_row = tile_row;
462 15 s->tile_group_info[tile_num].tile_column = tile_col;
463
464 15 bytestream2_skipu(&gb, size);
465 }
466
467 return 0;
468
469 }
470
471 28 static enum AVPixelFormat get_sw_pixel_format(void *logctx,
472 const AV1RawSequenceHeader *seq)
473 {
474 int bit_depth;
475 28 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
476
477
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
478 bit_depth = seq->color_config.twelve_bit ? 12 : 10;
479
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 else if (seq->seq_profile <= 2)
480
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27 times.
28 bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
481 else {
482 av_log(logctx, AV_LOG_ERROR,
483 "Unknown AV1 profile %d.\n", seq->seq_profile);
484 return AV_PIX_FMT_NONE;
485 }
486
487
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!seq->color_config.mono_chrome) {
488 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (seq->color_config.subsampling_x == 0 &&
490 seq->color_config.subsampling_y == 0) {
491 if (bit_depth == 8)
492 pix_fmt = AV_PIX_FMT_YUV444P;
493 else if (bit_depth == 10)
494 pix_fmt = AV_PIX_FMT_YUV444P10;
495 else if (bit_depth == 12)
496 pix_fmt = AV_PIX_FMT_YUV444P12;
497 else
498 av_assert0(0);
499
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 } else if (seq->color_config.subsampling_x == 1 &&
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 seq->color_config.subsampling_y == 0) {
501 if (bit_depth == 8)
502 pix_fmt = AV_PIX_FMT_YUV422P;
503 else if (bit_depth == 10)
504 pix_fmt = AV_PIX_FMT_YUV422P10;
505 else if (bit_depth == 12)
506 pix_fmt = AV_PIX_FMT_YUV422P12;
507 else
508 av_assert0(0);
509
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 } else if (seq->color_config.subsampling_x == 1 &&
510
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 seq->color_config.subsampling_y == 1) {
511
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 if (bit_depth == 8)
512 27 pix_fmt = AV_PIX_FMT_YUV420P;
513
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (bit_depth == 10)
514 1 pix_fmt = AV_PIX_FMT_YUV420P10;
515 else if (bit_depth == 12)
516 pix_fmt = AV_PIX_FMT_YUV420P12;
517 else
518 av_assert0(0);
519 }
520 } else {
521 if (bit_depth == 8)
522 pix_fmt = AV_PIX_FMT_GRAY8;
523 else if (bit_depth == 10)
524 pix_fmt = AV_PIX_FMT_GRAY10;
525 else if (bit_depth == 12)
526 pix_fmt = AV_PIX_FMT_GRAY12;
527 else
528 av_assert0(0);
529 }
530
531 28 return pix_fmt;
532 }
533
534 static int get_pixel_format(AVCodecContext *avctx)
535 {
536 AV1DecContext *s = avctx->priv_data;
537 const AV1RawSequenceHeader *seq = s->raw_seq;
538 int ret;
539 enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq);
540 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
541 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
542 CONFIG_AV1_D3D12VA_HWACCEL + \
543 CONFIG_AV1_NVDEC_HWACCEL + \
544 CONFIG_AV1_VAAPI_HWACCEL + \
545 CONFIG_AV1_VDPAU_HWACCEL + \
546 CONFIG_AV1_VIDEOTOOLBOX_HWACCEL + \
547 CONFIG_AV1_VULKAN_HWACCEL)
548 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
549
550 if (pix_fmt == AV_PIX_FMT_NONE)
551 return -1;
552
553 switch (pix_fmt) {
554 case AV_PIX_FMT_YUV420P:
555 #if CONFIG_AV1_DXVA2_HWACCEL
556 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
557 #endif
558 #if CONFIG_AV1_D3D11VA_HWACCEL
559 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
560 *fmtp++ = AV_PIX_FMT_D3D11;
561 #endif
562 #if CONFIG_AV1_D3D12VA_HWACCEL
563 *fmtp++ = AV_PIX_FMT_D3D12;
564 #endif
565 #if CONFIG_AV1_NVDEC_HWACCEL
566 *fmtp++ = AV_PIX_FMT_CUDA;
567 #endif
568 #if CONFIG_AV1_VAAPI_HWACCEL
569 *fmtp++ = AV_PIX_FMT_VAAPI;
570 #endif
571 #if CONFIG_AV1_VDPAU_HWACCEL
572 *fmtp++ = AV_PIX_FMT_VDPAU;
573 #endif
574 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
575 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
576 #endif
577 #if CONFIG_AV1_VULKAN_HWACCEL
578 *fmtp++ = AV_PIX_FMT_VULKAN;
579 #endif
580 break;
581 case AV_PIX_FMT_YUV420P10:
582 #if CONFIG_AV1_DXVA2_HWACCEL
583 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
584 #endif
585 #if CONFIG_AV1_D3D11VA_HWACCEL
586 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
587 *fmtp++ = AV_PIX_FMT_D3D11;
588 #endif
589 #if CONFIG_AV1_D3D12VA_HWACCEL
590 *fmtp++ = AV_PIX_FMT_D3D12;
591 #endif
592 #if CONFIG_AV1_NVDEC_HWACCEL
593 *fmtp++ = AV_PIX_FMT_CUDA;
594 #endif
595 #if CONFIG_AV1_VAAPI_HWACCEL
596 *fmtp++ = AV_PIX_FMT_VAAPI;
597 #endif
598 #if CONFIG_AV1_VDPAU_HWACCEL
599 *fmtp++ = AV_PIX_FMT_VDPAU;
600 #endif
601 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
602 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
603 #endif
604 #if CONFIG_AV1_VULKAN_HWACCEL
605 *fmtp++ = AV_PIX_FMT_VULKAN;
606 #endif
607 break;
608 case AV_PIX_FMT_YUV420P12:
609 #if CONFIG_AV1_VULKAN_HWACCEL
610 *fmtp++ = AV_PIX_FMT_VULKAN;
611 #endif
612 break;
613 case AV_PIX_FMT_YUV422P:
614 #if CONFIG_AV1_VULKAN_HWACCEL
615 *fmtp++ = AV_PIX_FMT_VULKAN;
616 #endif
617 break;
618 case AV_PIX_FMT_YUV422P10:
619 #if CONFIG_AV1_VULKAN_HWACCEL
620 *fmtp++ = AV_PIX_FMT_VULKAN;
621 #endif
622 break;
623 case AV_PIX_FMT_YUV422P12:
624 #if CONFIG_AV1_VULKAN_HWACCEL
625 *fmtp++ = AV_PIX_FMT_VULKAN;
626 #endif
627 break;
628 case AV_PIX_FMT_YUV444P:
629 #if CONFIG_AV1_VULKAN_HWACCEL
630 *fmtp++ = AV_PIX_FMT_VULKAN;
631 #endif
632 break;
633 case AV_PIX_FMT_YUV444P10:
634 #if CONFIG_AV1_VULKAN_HWACCEL
635 *fmtp++ = AV_PIX_FMT_VULKAN;
636 #endif
637 break;
638 case AV_PIX_FMT_YUV444P12:
639 #if CONFIG_AV1_VULKAN_HWACCEL
640 *fmtp++ = AV_PIX_FMT_VULKAN;
641 #endif
642 break;
643 case AV_PIX_FMT_GRAY8:
644 #if CONFIG_AV1_NVDEC_HWACCEL
645 *fmtp++ = AV_PIX_FMT_CUDA;
646 #endif
647 break;
648 case AV_PIX_FMT_GRAY10:
649 #if CONFIG_AV1_NVDEC_HWACCEL
650 *fmtp++ = AV_PIX_FMT_CUDA;
651 #endif
652 break;
653 }
654
655 *fmtp++ = pix_fmt;
656 *fmtp = AV_PIX_FMT_NONE;
657
658 for (int i = 0; pix_fmts[i] != pix_fmt; i++)
659 if (pix_fmts[i] == avctx->pix_fmt) {
660 s->pix_fmt = pix_fmt;
661 return 1;
662 }
663
664 ret = ff_get_format(avctx, pix_fmts);
665
666 /**
667 * check if the HW accel is inited correctly. If not, return un-implemented.
668 * Since now the av1 decoder doesn't support native decode, if it will be
669 * implemented in the future, need remove this check.
670 */
671 if (!avctx->hwaccel) {
672 av_log(avctx, AV_LOG_ERROR, "Your platform doesn't support"
673 " hardware accelerated AV1 decoding.\n");
674 avctx->pix_fmt = AV_PIX_FMT_NONE;
675 return AVERROR(ENOSYS);
676 }
677
678 s->pix_fmt = pix_fmt;
679 avctx->pix_fmt = ret;
680
681 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
682 av_get_pix_fmt_name(avctx->pix_fmt));
683
684 return 0;
685 }
686
687 285 static void av1_frame_unref(AV1Frame *f)
688 {
689 285 ff_progress_frame_unref(&f->pf);
690 285 av_refstruct_unref(&f->hwaccel_picture_private);
691 285 av_refstruct_unref(&f->header_ref);
692 285 f->raw_frame_header = NULL;
693 285 f->spatial_id = f->temporal_id = 0;
694 285 memset(f->skip_mode_frame_idx, 0,
695 2 * sizeof(uint8_t));
696 285 memset(&f->film_grain, 0, sizeof(f->film_grain));
697 285 f->coded_lossless = 0;
698 285 }
699
700 178 static void av1_frame_replace(AV1Frame *dst, const AV1Frame *src)
701 {
702 av_assert1(dst != src);
703
704 178 av_refstruct_replace(&dst->header_ref, src->header_ref);
705
706 178 dst->raw_frame_header = src->raw_frame_header;
707
708 178 ff_progress_frame_replace(&dst->pf, &src->pf);
709
710 178 av_refstruct_replace(&dst->hwaccel_picture_private,
711 178 src->hwaccel_picture_private);
712
713 178 dst->spatial_id = src->spatial_id;
714 178 dst->temporal_id = src->temporal_id;
715 178 memcpy(dst->gm_invalid,
716 178 src->gm_invalid,
717 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
718 178 memcpy(dst->gm_type,
719 178 src->gm_type,
720 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
721 178 memcpy(dst->gm_params,
722 178 src->gm_params,
723 AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
724 178 memcpy(dst->skip_mode_frame_idx,
725 178 src->skip_mode_frame_idx,
726 2 * sizeof(uint8_t));
727 178 memcpy(&dst->film_grain,
728 178 &src->film_grain,
729 sizeof(dst->film_grain));
730 178 dst->coded_lossless = src->coded_lossless;
731
732 178 dst->order_hint = src->order_hint;
733 178 memcpy(dst->ref_frame_sign_bias, src->ref_frame_sign_bias,
734 sizeof(dst->ref_frame_sign_bias));
735 178 memcpy(dst->order_hints, src->order_hints,
736 sizeof(dst->order_hints));
737
738 178 dst->force_integer_mv = src->force_integer_mv;
739 178 }
740
741 29 static av_cold int av1_decode_free(AVCodecContext *avctx)
742 {
743 29 AV1DecContext *s = avctx->priv_data;
744 AV1RawMetadataITUTT35 itut_t35;
745
746
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 29 times.
261 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
747 232 av1_frame_unref(&s->ref[i]);
748 29 av1_frame_unref(&s->cur_frame);
749 29 av_buffer_unref(&s->seq_data_ref);
750 29 av_refstruct_unref(&s->seq_ref);
751 29 av_refstruct_unref(&s->header_ref);
752 29 av_refstruct_unref(&s->cll_ref);
753 29 av_refstruct_unref(&s->mdcv_ref);
754 29 av_freep(&s->tile_group_info);
755
756
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
29 while (s->itut_t35_fifo && av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
757 av_buffer_unref(&itut_t35.payload_ref);
758 29 av_fifo_freep2(&s->itut_t35_fifo);
759
760 29 ff_cbs_fragment_free(&s->current_obu);
761 29 ff_cbs_close(&s->cbc);
762 29 ff_dovi_ctx_unref(&s->dovi);
763
764 29 return 0;
765 }
766
767 29 static int set_context_with_sequence(AVCodecContext *avctx,
768 const AV1RawSequenceHeader *seq)
769 {
770 29 int width = seq->max_frame_width_minus_1 + 1;
771 29 int height = seq->max_frame_height_minus_1 + 1;
772
773 29 avctx->profile = seq->seq_profile;
774 29 avctx->level = seq->seq_level_idx[0];
775
776 29 avctx->color_range =
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
778 29 avctx->color_primaries = seq->color_config.color_primaries;
779 29 avctx->colorspace = seq->color_config.matrix_coefficients;
780 29 avctx->color_trc = seq->color_config.transfer_characteristics;
781
782
2/3
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
29 switch (seq->color_config.chroma_sample_position) {
783 4 case AV1_CSP_VERTICAL:
784 4 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
785 4 break;
786 case AV1_CSP_COLOCATED:
787 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
788 break;
789 }
790
791 #if FF_API_CODEC_PROPS
792 FF_DISABLE_DEPRECATION_WARNINGS
793
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
29 if (seq->film_grain_params_present)
794 2 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
795 else
796 27 avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
797 FF_ENABLE_DEPRECATION_WARNINGS
798 #endif
799
800
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
29 if (avctx->width != width || avctx->height != height) {
801 4 int ret = ff_set_dimensions(avctx, width, height);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
803 return ret;
804 }
805
806
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 25 times.
29 if (seq->timing_info_present_flag)
807 4 avctx->framerate = ff_av1_framerate(1LL + seq->timing_info.num_ticks_per_picture_minus_1,
808 4 seq->timing_info.num_units_in_display_tick,
809 4 seq->timing_info.time_scale);
810
811
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
29 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
812 28 avctx->pix_fmt = get_sw_pixel_format(avctx, seq);
813
814 29 return 0;
815 }
816
817 static int update_context_with_frame_header(AVCodecContext *avctx,
818 const AV1RawFrameHeader *header)
819 {
820 AVRational aspect_ratio;
821 int width = header->frame_width_minus_1 + 1;
822 int height = header->frame_height_minus_1 + 1;
823 int r_width = header->render_width_minus_1 + 1;
824 int r_height = header->render_height_minus_1 + 1;
825 int ret;
826
827 if (avctx->width != width || avctx->height != height) {
828 ret = ff_set_dimensions(avctx, width, height);
829 if (ret < 0)
830 return ret;
831 }
832
833 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
834 (int64_t)height * r_width,
835 (int64_t)width * r_height,
836 INT_MAX);
837
838 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
839 ret = ff_set_sar(avctx, aspect_ratio);
840 if (ret < 0)
841 return ret;
842 }
843
844 return 0;
845 }
846
847 static const CodedBitstreamUnitType decompose_unit_types[] = {
848 AV1_OBU_FRAME,
849 AV1_OBU_FRAME_HEADER,
850 AV1_OBU_METADATA,
851 AV1_OBU_REDUNDANT_FRAME_HEADER,
852 AV1_OBU_SEQUENCE_HEADER,
853 AV1_OBU_TEMPORAL_DELIMITER,
854 AV1_OBU_TILE_GROUP,
855 };
856
857 29 static av_cold int av1_decode_init(AVCodecContext *avctx)
858 {
859 29 AV1DecContext *s = avctx->priv_data;
860 AV1RawSequenceHeader *seq;
861 const AVPacketSideData *sd;
862 int ret;
863
864 29 s->avctx = avctx;
865 29 s->pkt = avctx->internal->in_pkt;
866 29 s->pix_fmt = AV_PIX_FMT_NONE;
867
868 29 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
870 return ret;
871
872 29 s->cbc->decompose_unit_types = decompose_unit_types;
873 29 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
874
875 29 s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35),
876 AV_FIFO_FLAG_AUTO_GROW);
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!s->itut_t35_fifo)
878 return AVERROR(ENOMEM);
879
880 29 av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
881
882
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
29 if (avctx->extradata && avctx->extradata_size) {
883 9 ret = ff_cbs_read_extradata_from_codec(s->cbc,
884 &s->current_obu,
885 avctx);
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0) {
887 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
888 goto end;
889 }
890
891 9 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
892
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!seq) {
893 2 av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
894 2 goto end;
895 }
896
897 7 ret = set_context_with_sequence(avctx, seq);
898
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret < 0) {
899 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
900 goto end;
901 }
902
903 7 end:
904 9 ff_cbs_fragment_reset(&s->current_obu);
905 }
906
907 29 s->dovi.logctx = avctx;
908 29 s->dovi.cfg.dv_profile = 10; // default for AV1
909 29 sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
910
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29 if (sd && sd->size >= sizeof(s->dovi.cfg))
911 s->dovi.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
912
913 29 return ret;
914 }
915
916 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
917 {
918 AV1DecContext *s = avctx->priv_data;
919 AV1RawFrameHeader *header= s->raw_frame_header;
920 AVFrame *frame;
921 int ret;
922
923 ret = update_context_with_frame_header(avctx, header);
924 if (ret < 0) {
925 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
926 return ret;
927 }
928
929 ret = ff_progress_frame_get_buffer(avctx, &f->pf, AV_GET_BUFFER_FLAG_REF);
930 if (ret < 0)
931 goto fail;
932
933 frame = f->f;
934 if (header->frame_type == AV1_FRAME_KEY)
935 frame->flags |= AV_FRAME_FLAG_KEY;
936 else
937 frame->flags &= ~AV_FRAME_FLAG_KEY;
938
939 switch (header->frame_type) {
940 case AV1_FRAME_KEY:
941 case AV1_FRAME_INTRA_ONLY:
942 frame->pict_type = AV_PICTURE_TYPE_I;
943 break;
944 case AV1_FRAME_INTER:
945 frame->pict_type = AV_PICTURE_TYPE_P;
946 break;
947 case AV1_FRAME_SWITCH:
948 frame->pict_type = AV_PICTURE_TYPE_SP;
949 break;
950 }
951
952 ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
953 if (ret < 0)
954 goto fail;
955
956 return 0;
957
958 fail:
959 av1_frame_unref(f);
960 return ret;
961 }
962
963 static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame,
964 const AV1RawMetadataITUTT35 *itut_t35)
965 {
966 GetByteContext gb;
967 AV1DecContext *s = avctx->priv_data;
968 int ret, provider_code;
969
970 bytestream2_init(&gb, itut_t35->payload, itut_t35->payload_size);
971
972 provider_code = bytestream2_get_be16(&gb);
973 switch (provider_code) {
974 case ITU_T_T35_PROVIDER_CODE_ATSC: {
975 uint32_t user_identifier = bytestream2_get_be32(&gb);
976 switch (user_identifier) {
977 case MKBETAG('G', 'A', '9', '4'): { // closed captions
978 AVBufferRef *buf = NULL;
979
980 ret = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb));
981 if (ret < 0)
982 return ret;
983 if (!ret)
984 break;
985
986 ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_A53_CC, &buf);
987 if (ret < 0)
988 return ret;
989
990 #if FF_API_CODEC_PROPS
991 FF_DISABLE_DEPRECATION_WARNINGS
992 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
993 FF_ENABLE_DEPRECATION_WARNINGS
994 #endif
995 break;
996 }
997 default: // ignore unsupported identifiers
998 break;
999 }
1000 break;
1001 }
1002 case ITU_T_T35_PROVIDER_CODE_SMTPE: {
1003 AVDynamicHDRPlus *hdrplus;
1004 int provider_oriented_code = bytestream2_get_be16(&gb);
1005 int application_identifier = bytestream2_get_byte(&gb);
1006
1007 if (itut_t35->itu_t_t35_country_code != ITU_T_T35_COUNTRY_CODE_US ||
1008 provider_oriented_code != 1 || application_identifier != 4)
1009 break;
1010
1011 hdrplus = av_dynamic_hdr_plus_create_side_data(frame);
1012 if (!hdrplus)
1013 return AVERROR(ENOMEM);
1014
1015 ret = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer,
1016 bytestream2_get_bytes_left(&gb));
1017 if (ret < 0)
1018 return ret;
1019 break;
1020 }
1021 case ITU_T_T35_PROVIDER_CODE_DOLBY: {
1022 int provider_oriented_code = bytestream2_get_be32(&gb);
1023 if (itut_t35->itu_t_t35_country_code != ITU_T_T35_COUNTRY_CODE_US ||
1024 provider_oriented_code != 0x800)
1025 break;
1026
1027 ret = ff_dovi_rpu_parse(&s->dovi, gb.buffer, gb.buffer_end - gb.buffer,
1028 avctx->err_recognition);
1029 if (ret < 0) {
1030 av_log(avctx, AV_LOG_WARNING, "Error parsing DOVI OBU.\n");
1031 break; // ignore
1032 }
1033
1034 ret = ff_dovi_attach_side_data(&s->dovi, frame);
1035 if (ret < 0)
1036 return ret;
1037 break;
1038 }
1039 default: // ignore unsupported provider codes
1040 break;
1041 }
1042
1043 return 0;
1044 }
1045
1046 static int export_metadata(AVCodecContext *avctx, AVFrame *frame)
1047 {
1048 AV1DecContext *s = avctx->priv_data;
1049 AV1RawMetadataITUTT35 itut_t35;
1050 int ret = 0;
1051
1052 if (s->mdcv) {
1053 AVMasteringDisplayMetadata *mastering;
1054
1055 ret = ff_decode_mastering_display_new(avctx, frame, &mastering);
1056 if (ret < 0)
1057 return ret;
1058
1059 if (mastering) {
1060 for (int i = 0; i < 3; i++) {
1061 mastering->display_primaries[i][0] = av_make_q(s->mdcv->primary_chromaticity_x[i], 1 << 16);
1062 mastering->display_primaries[i][1] = av_make_q(s->mdcv->primary_chromaticity_y[i], 1 << 16);
1063 }
1064 mastering->white_point[0] = av_make_q(s->mdcv->white_point_chromaticity_x, 1 << 16);
1065 mastering->white_point[1] = av_make_q(s->mdcv->white_point_chromaticity_y, 1 << 16);
1066
1067 mastering->max_luminance = av_make_q(s->mdcv->luminance_max, 1 << 8);
1068 mastering->min_luminance = av_make_q(s->mdcv->luminance_min, 1 << 14);
1069
1070 mastering->has_primaries = 1;
1071 mastering->has_luminance = 1;
1072 }
1073 }
1074
1075 if (s->cll) {
1076 AVContentLightMetadata *light;
1077
1078 ret = ff_decode_content_light_new(avctx, frame, &light);
1079 if (ret < 0)
1080 return ret;
1081
1082 if (light) {
1083 light->MaxCLL = s->cll->max_cll;
1084 light->MaxFALL = s->cll->max_fall;
1085 }
1086 }
1087
1088 while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) {
1089 if (ret >= 0)
1090 ret = export_itut_t35(avctx, frame, &itut_t35);
1091 av_buffer_unref(&itut_t35.payload_ref);
1092 }
1093
1094 return ret;
1095 }
1096
1097 static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
1098 {
1099 AV1DecContext *s = avctx->priv_data;
1100 const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
1101 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(frame->format);
1102 AVFilmGrainParams *fgp;
1103 AVFilmGrainAOMParams *aom;
1104
1105 av_assert0(pixdesc);
1106 if (!film_grain->apply_grain)
1107 return 0;
1108
1109 fgp = av_film_grain_params_create_side_data(frame);
1110 if (!fgp)
1111 return AVERROR(ENOMEM);
1112
1113 fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
1114 fgp->seed = film_grain->grain_seed;
1115 fgp->width = frame->width;
1116 fgp->height = frame->height;
1117 fgp->color_range = frame->color_range;
1118 fgp->color_primaries = frame->color_primaries;
1119 fgp->color_trc = frame->color_trc;
1120 fgp->color_space = frame->colorspace;
1121 fgp->subsampling_x = pixdesc->log2_chroma_w;
1122 fgp->subsampling_y = pixdesc->log2_chroma_h;
1123
1124 aom = &fgp->codec.aom;
1125 aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
1126 aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
1127 aom->ar_coeff_lag = film_grain->ar_coeff_lag;
1128 aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
1129 aom->grain_scale_shift = film_grain->grain_scale_shift;
1130 aom->overlap_flag = film_grain->overlap_flag;
1131 aom->limit_output_range = film_grain->clip_to_restricted_range;
1132
1133 aom->num_y_points = film_grain->num_y_points;
1134 for (int i = 0; i < film_grain->num_y_points; i++) {
1135 aom->y_points[i][0] = film_grain->point_y_value[i];
1136 aom->y_points[i][1] = film_grain->point_y_scaling[i];
1137 }
1138 aom->num_uv_points[0] = film_grain->num_cb_points;
1139 for (int i = 0; i < film_grain->num_cb_points; i++) {
1140 aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
1141 aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
1142 }
1143 aom->num_uv_points[1] = film_grain->num_cr_points;
1144 for (int i = 0; i < film_grain->num_cr_points; i++) {
1145 aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
1146 aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
1147 }
1148
1149 for (int i = 0; i < 24; i++) {
1150 aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
1151 }
1152 for (int i = 0; i < 25; i++) {
1153 aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
1154 aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
1155 }
1156
1157 aom->uv_mult[0] = film_grain->cb_mult;
1158 aom->uv_mult[1] = film_grain->cr_mult;
1159 aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
1160 aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
1161 aom->uv_offset[0] = film_grain->cb_offset;
1162 aom->uv_offset[1] = film_grain->cr_offset;
1163
1164 return 0;
1165 }
1166
1167 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame)
1168 {
1169 AV1DecContext *s = avctx->priv_data;
1170 const AVFrame *srcframe = s->cur_frame.f;
1171 AVPacket *pkt = s->pkt;
1172 int ret;
1173
1174 // TODO: all layers
1175 if (s->operating_point_idc &&
1176 av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
1177 return 0;
1178
1179 ret = av_frame_ref(frame, srcframe);
1180 if (ret < 0)
1181 return ret;
1182
1183 ret = export_metadata(avctx, frame);
1184 if (ret < 0) {
1185 av_frame_unref(frame);
1186 return ret;
1187 }
1188
1189 if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
1190 ret = export_film_grain(avctx, frame);
1191 if (ret < 0) {
1192 av_frame_unref(frame);
1193 return ret;
1194 }
1195 }
1196
1197 frame->pts = pkt->pts;
1198 frame->pkt_dts = pkt->dts;
1199
1200 av_packet_unref(pkt);
1201
1202 return 0;
1203 }
1204
1205 24 static void update_reference_list(AVCodecContext *avctx)
1206 {
1207 24 AV1DecContext *s = avctx->priv_data;
1208 24 const AV1RawFrameHeader *header = s->raw_frame_header;
1209
1210
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 24 times.
216 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1211
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 14 times.
192 if (header->refresh_frame_flags & (1 << i))
1212 178 av1_frame_replace(&s->ref[i], &s->cur_frame);
1213 }
1214 24 }
1215
1216 24 static int get_current_frame(AVCodecContext *avctx)
1217 {
1218 24 AV1DecContext *s = avctx->priv_data;
1219 int ret;
1220
1221 24 av1_frame_unref(&s->cur_frame);
1222
1223 24 s->cur_frame.header_ref = av_refstruct_ref(s->header_ref);
1224
1225 24 s->cur_frame.raw_frame_header = s->raw_frame_header;
1226
1227 24 ret = init_tile_data(s);
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
1229 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
1230 return ret;
1231 }
1232
1233
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
1234
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 (s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
1236
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 (avctx->skip_frame >= AVDISCARD_NONKEY &&
1237
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
1238
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 avctx->skip_frame >= AVDISCARD_ALL)
1239 24 return 0;
1240
1241 if (s->pix_fmt == AV_PIX_FMT_NONE) {
1242 ret = get_pixel_format(avctx);
1243 if (ret < 0) {
1244 av_log(avctx, AV_LOG_ERROR, "Failed to get pixel format.\n");
1245 return ret;
1246 }
1247
1248 if (!ret && FF_HW_HAS_CB(avctx, decode_params)) {
1249 ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER,
1250 s->seq_data_ref->data, s->seq_data_ref->size);
1251 if (ret < 0) {
1252 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
1253 return ret;
1254 }
1255 }
1256 }
1257
1258 ret = av1_frame_alloc(avctx, &s->cur_frame);
1259 if (ret < 0) {
1260 av_log(avctx, AV_LOG_ERROR,
1261 "Failed to allocate space for current frame.\n");
1262 return ret;
1263 }
1264
1265 global_motion_params(s);
1266 skip_mode_params(s);
1267 coded_lossless_param(s);
1268 order_hint_info(s);
1269 load_grain_params(s);
1270
1271 s->cur_frame.force_integer_mv =
1272 s->raw_frame_header->force_integer_mv ||
1273 s->raw_frame_header->frame_type == AV1_FRAME_KEY ||
1274 s->raw_frame_header->frame_type == AV1_FRAME_INTRA_ONLY;
1275
1276 return ret;
1277 }
1278
1279 24 static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
1280 {
1281 24 AV1DecContext *s = avctx->priv_data;
1282 24 AV1RawTileGroup *raw_tile_group = NULL;
1283 24 int i = 0, ret;
1284
1285
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 for (i = s->nb_unit; i < s->current_obu.nb_units; i++) {
1286 83 CodedBitstreamUnit *unit = &s->current_obu.units[i];
1287 83 AV1RawOBU *obu = unit->content;
1288 const AV1RawOBUHeader *header;
1289
1290 83 av_log(avctx, AV_LOG_DEBUG, "OBU idx:%d, type:%d, content available:%d.\n", i, unit->type, !!obu);
1291
1292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (unit->type == AV1_OBU_TILE_LIST) {
1293 av_log(avctx, AV_LOG_ERROR, "Large scale tile decoding is unsupported.\n");
1294 ret = AVERROR_PATCHWELCOME;
1295 goto end;
1296 }
1297
1298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!obu)
1299 continue;
1300
1301 83 header = &obu->header;
1302
1303
5/7
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
83 switch (unit->type) {
1304 22 case AV1_OBU_SEQUENCE_HEADER:
1305 22 ret = av_buffer_replace(&s->seq_data_ref, unit->data_ref);
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1307 goto end;
1308
1309 22 s->seq_data_ref->data = unit->data;
1310 22 s->seq_data_ref->size = unit->data_size;
1311 22 av_refstruct_replace(&s->seq_ref, unit->content_ref);
1312
1313 22 s->raw_seq = &obu->obu.sequence_header;
1314
1315 22 ret = set_context_with_sequence(avctx, s->raw_seq);
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
1317 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
1318 s->raw_seq = NULL;
1319 goto end;
1320 }
1321
1322 22 s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
1323
1324 22 s->pix_fmt = AV_PIX_FMT_NONE;
1325
1326
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
22 if (FF_HW_HAS_CB(avctx, decode_params)) {
1327 ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER,
1328 s->seq_data_ref->data, s->seq_data_ref->size);
1329 if (ret < 0) {
1330 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
1331 return ret;
1332 }
1333 }
1334
1335 22 break;
1336 case AV1_OBU_REDUNDANT_FRAME_HEADER:
1337 if (s->raw_frame_header)
1338 break;
1339 // fall-through
1340 case AV1_OBU_FRAME:
1341 case AV1_OBU_FRAME_HEADER:
1342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->raw_seq) {
1343 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
1344 ret = AVERROR_INVALIDDATA;
1345 goto end;
1346 }
1347
1348 24 av_refstruct_replace(&s->header_ref, unit->content_ref);
1349
1350
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 5 times.
24 if (unit->type == AV1_OBU_FRAME)
1351 19 s->raw_frame_header = &obu->obu.frame.header;
1352 else
1353 5 s->raw_frame_header = &obu->obu.frame_header;
1354
1355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->raw_frame_header->show_existing_frame) {
1356 av1_frame_replace(&s->cur_frame,
1357 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
1358
1359 update_reference_list(avctx);
1360
1361 if (s->cur_frame.f) {
1362 ret = set_output_frame(avctx, frame);
1363 if (ret < 0) {
1364 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
1365 goto end;
1366 }
1367 }
1368
1369 s->raw_frame_header = NULL;
1370 i++;
1371 ret = 0;
1372
1373 goto end;
1374 }
1375
1376 24 ret = get_current_frame(avctx);
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
1378 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1379 goto end;
1380 }
1381
1382 24 s->cur_frame.spatial_id = header->spatial_id;
1383 24 s->cur_frame.temporal_id = header->temporal_id;
1384
1385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (avctx->hwaccel && s->cur_frame.f) {
1386 ret = FF_HW_CALL(avctx, start_frame, s->pkt->buf,
1387 unit->data, unit->data_size);
1388 if (ret < 0) {
1389 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1390 goto end;
1391 }
1392 }
1393
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 if (unit->type != AV1_OBU_FRAME)
1394 5 break;
1395 // fall-through
1396 case AV1_OBU_TILE_GROUP:
1397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s->raw_frame_header) {
1398 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
1399 ret = AVERROR_INVALIDDATA;
1400 goto end;
1401 }
1402
1403
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13 times.
32 if (unit->type == AV1_OBU_FRAME)
1404 19 raw_tile_group = &obu->obu.frame.tile_group;
1405 else
1406 13 raw_tile_group = &obu->obu.tile_group;
1407
1408 32 ret = get_tiles_info(avctx, raw_tile_group);
1409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
1410 goto end;
1411
1412
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
32 if (avctx->hwaccel && s->cur_frame.f) {
1413 ret = FF_HW_CALL(avctx, decode_slice, raw_tile_group->tile_data.data,
1414 raw_tile_group->tile_data.data_size);
1415 if (ret < 0) {
1416 av_log(avctx, AV_LOG_ERROR,
1417 "HW accel decode slice fail.\n");
1418 goto end;
1419 }
1420 }
1421 32 break;
1422 20 case AV1_OBU_TILE_LIST:
1423 case AV1_OBU_TEMPORAL_DELIMITER:
1424 case AV1_OBU_PADDING:
1425 20 break;
1426 4 case AV1_OBU_METADATA:
1427
2/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 switch (obu->obu.metadata.metadata_type) {
1428 2 case AV1_METADATA_TYPE_HDR_CLL:
1429 2 av_refstruct_replace(&s->cll_ref, unit->content_ref);
1430 2 s->cll = &obu->obu.metadata.metadata.hdr_cll;
1431 2 break;
1432 2 case AV1_METADATA_TYPE_HDR_MDCV:
1433 2 av_refstruct_replace(&s->mdcv_ref, unit->content_ref);
1434 2 s->mdcv = &obu->obu.metadata.metadata.hdr_mdcv;
1435 2 break;
1436 case AV1_METADATA_TYPE_ITUT_T35: {
1437 AV1RawMetadataITUTT35 itut_t35;
1438 memcpy(&itut_t35, &obu->obu.metadata.metadata.itut_t35, sizeof(itut_t35));
1439 itut_t35.payload_ref = av_buffer_ref(obu->obu.metadata.metadata.itut_t35.payload_ref);
1440 if (!itut_t35.payload_ref) {
1441 ret = AVERROR(ENOMEM);
1442 goto end;
1443 }
1444 ret = av_fifo_write(s->itut_t35_fifo, &itut_t35, 1);
1445 if (ret < 0) {
1446 av_buffer_unref(&itut_t35.payload_ref);
1447 goto end;
1448 }
1449 break;
1450 }
1451 default:
1452 break;
1453 }
1454 4 break;
1455 default:
1456 av_log(avctx, AV_LOG_DEBUG,
1457 "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
1458 unit->type, unit->data_size);
1459 }
1460
1461
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 8 times.
83 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
1462 24 int show_frame = s->raw_frame_header->show_frame;
1463 // Set nb_unit to point at the next OBU, to indicate which
1464 // OBUs have been processed for this current frame. (If this
1465 // frame gets output, we set nb_unit to this value later too.)
1466 24 s->nb_unit = i + 1;
1467
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (avctx->hwaccel && s->cur_frame.f) {
1468 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
1469 if (ret < 0) {
1470 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1471 goto end;
1472 }
1473 }
1474
1475 24 update_reference_list(avctx);
1476
1477 // Set start_unit to indicate the first OBU of the next frame.
1478 24 s->start_unit = s->nb_unit;
1479 24 raw_tile_group = NULL;
1480 24 s->raw_frame_header = NULL;
1481
1482
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (show_frame) {
1483 // cur_frame.f needn't exist due to skip_frame.
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->cur_frame.f) {
1485 ret = set_output_frame(avctx, frame);
1486 if (ret < 0) {
1487 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1488 goto end;
1489 }
1490 }
1491 24 i++;
1492 24 ret = 0;
1493 24 goto end;
1494 }
1495 }
1496 }
1497
1498 ret = AVERROR(EAGAIN);
1499 24 end:
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(i <= s->current_obu.nb_units);
1501 24 s->nb_unit = i;
1502
1503
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 2 times.
24 if ((ret < 0 && ret != AVERROR(EAGAIN)) || s->current_obu.nb_units == i) {
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1505 s->raw_frame_header = NULL;
1506 22 av_packet_unref(s->pkt);
1507 22 ff_cbs_fragment_reset(&s->current_obu);
1508 22 s->nb_unit = s->start_unit = 0;
1509 }
1510
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 if (!ret && !frame->buf[0])
1511 24 ret = AVERROR(EAGAIN);
1512
1513 24 return ret;
1514 }
1515
1516 44 static int av1_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1517 {
1518 44 AV1DecContext *s = avctx->priv_data;
1519 int ret;
1520
1521 do {
1522
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2 times.
68 if (!s->current_obu.nb_units) {
1523 66 ret = ff_decode_get_packet(avctx, s->pkt);
1524
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 if (ret < 0)
1525 44 return ret;
1526
1527 22 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, s->pkt);
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
1529 ff_cbs_fragment_reset(&s->current_obu);
1530 av_packet_unref(s->pkt);
1531 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
1532 return ret;
1533 }
1534
1535 22 s->nb_unit = s->start_unit = 0;
1536 22 av_log(avctx, AV_LOG_DEBUG, "Total OBUs on this packet: %d.\n",
1537 s->current_obu.nb_units);
1538 }
1539
1540 24 ret = av1_receive_frame_internal(avctx, frame);
1541
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } while (ret == AVERROR(EAGAIN));
1542
1543 return ret;
1544 }
1545
1546 static void av1_decode_flush(AVCodecContext *avctx)
1547 {
1548 AV1DecContext *s = avctx->priv_data;
1549 AV1RawMetadataITUTT35 itut_t35;
1550
1551 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1552 av1_frame_unref(&s->ref[i]);
1553
1554 av1_frame_unref(&s->cur_frame);
1555 s->operating_point_idc = 0;
1556 s->nb_unit = s->start_unit = 0;
1557 s->raw_frame_header = NULL;
1558 s->raw_seq = NULL;
1559 s->cll = NULL;
1560 s->mdcv = NULL;
1561 while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
1562 av_buffer_unref(&itut_t35.payload_ref);
1563
1564 ff_cbs_fragment_reset(&s->current_obu);
1565 ff_cbs_flush(s->cbc);
1566
1567 if (FF_HW_HAS_CB(avctx, flush))
1568 FF_HW_SIMPLE_CALL(avctx, flush);
1569 }
1570
1571 #define OFFSET(x) offsetof(AV1DecContext, x)
1572 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1573 static const AVOption av1_options[] = {
1574 { "operating_point", "Select an operating point of the scalable bitstream",
1575 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
1576 { NULL }
1577 };
1578
1579 static const AVClass av1_class = {
1580 .class_name = "AV1 decoder",
1581 .item_name = av_default_item_name,
1582 .option = av1_options,
1583 .version = LIBAVUTIL_VERSION_INT,
1584 };
1585
1586 const FFCodec ff_av1_decoder = {
1587 .p.name = "av1",
1588 CODEC_LONG_NAME("Alliance for Open Media AV1"),
1589 .p.type = AVMEDIA_TYPE_VIDEO,
1590 .p.id = AV_CODEC_ID_AV1,
1591 .priv_data_size = sizeof(AV1DecContext),
1592 .init = av1_decode_init,
1593 .close = av1_decode_free,
1594 FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame),
1595 .p.capabilities = AV_CODEC_CAP_DR1,
1596 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1597 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
1598 FF_CODEC_CAP_USES_PROGRESSFRAMES,
1599 .flush = av1_decode_flush,
1600 .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1601 .p.priv_class = &av1_class,
1602 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1603 #if CONFIG_AV1_DXVA2_HWACCEL
1604 HWACCEL_DXVA2(av1),
1605 #endif
1606 #if CONFIG_AV1_D3D11VA_HWACCEL
1607 HWACCEL_D3D11VA(av1),
1608 #endif
1609 #if CONFIG_AV1_D3D11VA2_HWACCEL
1610 HWACCEL_D3D11VA2(av1),
1611 #endif
1612 #if CONFIG_AV1_D3D12VA_HWACCEL
1613 HWACCEL_D3D12VA(av1),
1614 #endif
1615 #if CONFIG_AV1_NVDEC_HWACCEL
1616 HWACCEL_NVDEC(av1),
1617 #endif
1618 #if CONFIG_AV1_VAAPI_HWACCEL
1619 HWACCEL_VAAPI(av1),
1620 #endif
1621 #if CONFIG_AV1_VDPAU_HWACCEL
1622 HWACCEL_VDPAU(av1),
1623 #endif
1624 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
1625 HWACCEL_VIDEOTOOLBOX(av1),
1626 #endif
1627 #if CONFIG_AV1_VULKAN_HWACCEL
1628 HWACCEL_VULKAN(av1),
1629 #endif
1630
1631 NULL
1632 },
1633 };
1634

脸过敏发红痒擦什么药 白细胞计数偏低是什么原因 排尿困难吃什么药 为什么血脂会高 唇亡齿寒什么意思
踏空是什么意思 啤酒加生鸡蛋一起喝有什么效果 吃什么可以增加抵抗力和免疫力 开导是什么意思 春指什么生肖
孕妇脚肿是什么原因 传染病八项包括什么 胰岛素高是什么原因 嗓子有异物感吃什么药 宝妈男是什么意思
嘴巴里苦是什么原因 月经一个月来两次什么原因 tj什么意思 内风湿是什么原因引起的 世界上笔画最多的字是什么字
醋酸泼尼松片治什么病hcv7jop6ns1r.cn 丞字五行属什么hcv8jop7ns8r.cn dr检查是什么hcv9jop3ns0r.cn 尖锐湿疣是什么病hcv9jop1ns6r.cn 花是植物的什么器官hcv8jop2ns0r.cn
球拍状胎盘是什么意思0297y7.com 九寨沟属于什么市hcv8jop3ns1r.cn 冥冥之中是什么意思hcv8jop6ns9r.cn 岁月从不败美人什么意思hcv8jop1ns2r.cn 心脏由什么组织构成hcv8jop1ns0r.cn
咽喉干燥是什么原因hcv8jop1ns9r.cn 出生证明有什么用hcv7jop6ns5r.cn 卧推80公斤什么水平hcv9jop2ns2r.cn 属牛的跟什么属相最配hcv8jop4ns2r.cn 微创手术是什么意思kuyehao.com
大梁是什么朝代hcv9jop5ns5r.cn 三岁看大七岁看老什么意思hcv8jop7ns9r.cn 打饱嗝是什么原因造成的hcv9jop7ns0r.cn mt是什么单位hcv9jop2ns2r.cn 洋葱不能和什么一起吃hcv8jop9ns9r.cn
百度