/src/ffmpeg/libavcodec/av1dec.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
74 | 0 | if (v > 2 * r) |
75 | 0 | return v; |
76 | 0 | else if (v & 1) |
77 | 0 | return r - ((v + 1) >> 1); |
78 | 0 | else |
79 | 0 | return r + (v >> 1); |
80 | 0 | } |
81 | | |
82 | | static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp, |
83 | | int mx, int r) |
84 | 0 | { |
85 | 0 | if ((r << 1) <= mx) { |
86 | 0 | return inverse_recenter(r, sub_exp); |
87 | 0 | } else { |
88 | 0 | return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low, |
93 | | int high, int r) |
94 | 0 | { |
95 | 0 | int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low); |
96 | 0 | return x + low; |
97 | 0 | } |
98 | | |
99 | | static void read_global_param(AV1DecContext *s, int type, int ref, int idx) |
100 | 0 | { |
101 | 0 | uint8_t primary_frame, prev_frame; |
102 | 0 | uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx; |
103 | 0 | int32_t r, prev_gm_param; |
104 | |
|
105 | 0 | primary_frame = s->raw_frame_header->primary_ref_frame; |
106 | 0 | prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame]; |
107 | 0 | abs_bits = AV1_GM_ABS_ALPHA_BITS; |
108 | 0 | 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 | 0 | if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE) |
115 | 0 | prev_gm_param = s->cur_frame.gm_params[ref][idx]; |
116 | 0 | else |
117 | 0 | prev_gm_param = s->ref[prev_frame].gm_params[ref][idx]; |
118 | |
|
119 | 0 | if (idx < 2) { |
120 | 0 | if (type == AV1_WARP_MODEL_TRANSLATION) { |
121 | 0 | abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS - |
122 | 0 | !s->raw_frame_header->allow_high_precision_mv; |
123 | 0 | prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - |
124 | 0 | !s->raw_frame_header->allow_high_precision_mv; |
125 | 0 | } else { |
126 | 0 | abs_bits = AV1_GM_ABS_TRANS_BITS; |
127 | 0 | prec_bits = AV1_GM_TRANS_PREC_BITS; |
128 | 0 | } |
129 | 0 | } |
130 | 0 | round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0; |
131 | 0 | prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits; |
132 | 0 | sub = (idx % 3) == 2 ? (1 << prec_bits) : 0; |
133 | 0 | mx = 1 << abs_bits; |
134 | 0 | r = (prev_gm_param >> prec_diff) - sub; |
135 | |
|
136 | 0 | s->cur_frame.gm_params[ref][idx] = |
137 | 0 | (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx], |
138 | 0 | -mx, mx + 1, r) << prec_diff) + round; |
139 | 0 | } |
140 | | |
141 | | static uint64_t round_two(uint64_t x, uint16_t n) |
142 | 0 | { |
143 | 0 | if (n == 0) |
144 | 0 | return x; |
145 | 0 | return ((x + ((uint64_t)1 << (n - 1))) >> n); |
146 | 0 | } |
147 | | |
148 | | static int64_t round_two_signed(int64_t x, uint16_t n) |
149 | 0 | { |
150 | 0 | return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n)); |
151 | 0 | } |
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 | 0 | { |
159 | 0 | int32_t e, f; |
160 | |
|
161 | 0 | *shift = av_log2(d); |
162 | 0 | e = d - (1 << (*shift)); |
163 | 0 | if (*shift > AV1_DIV_LUT_BITS) |
164 | 0 | f = round_two(e, *shift - AV1_DIV_LUT_BITS); |
165 | 0 | else |
166 | 0 | f = e << (AV1_DIV_LUT_BITS - (*shift)); |
167 | |
|
168 | 0 | *shift += AV1_DIV_LUT_PREC_BITS; |
169 | |
|
170 | 0 | return div_lut[f]; |
171 | 0 | } |
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 | 0 | { |
179 | 0 | int16_t alpha, beta, gamma, delta, divf, divs; |
180 | 0 | int64_t v, w; |
181 | 0 | int32_t *param = &s->cur_frame.gm_params[idx][0]; |
182 | 0 | if (param[2] <= 0) |
183 | 0 | return 0; |
184 | | |
185 | 0 | alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS)); |
186 | 0 | beta = av_clip_int16(param[3]); |
187 | 0 | divf = resolve_divisor(abs(param[2]), &divs); |
188 | 0 | v = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS); |
189 | 0 | w = (int64_t)param[3] * param[4]; |
190 | 0 | gamma = av_clip_int16((int)round_two_signed((v * divf), divs)); |
191 | 0 | delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS)); |
192 | |
|
193 | 0 | alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; |
194 | 0 | beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; |
195 | 0 | gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; |
196 | 0 | delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; |
197 | |
|
198 | 0 | if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) || |
199 | 0 | (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS)) |
200 | 0 | return 0; |
201 | | |
202 | 0 | return 1; |
203 | 0 | } |
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 | 0 | { |
211 | 0 | const AV1RawFrameHeader *header = s->raw_frame_header; |
212 | 0 | int type, ref; |
213 | |
|
214 | 0 | for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) { |
215 | 0 | s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY; |
216 | 0 | for (int i = 0; i < 6; i++) |
217 | 0 | s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ? |
218 | 0 | 1 << AV1_WARPEDMODEL_PREC_BITS : 0; |
219 | 0 | } |
220 | 0 | if (header->frame_type == AV1_FRAME_KEY || |
221 | 0 | header->frame_type == AV1_FRAME_INTRA_ONLY) |
222 | 0 | return; |
223 | | |
224 | 0 | for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) { |
225 | 0 | if (header->is_global[ref]) { |
226 | 0 | if (header->is_rot_zoom[ref]) { |
227 | 0 | type = AV1_WARP_MODEL_ROTZOOM; |
228 | 0 | } else { |
229 | 0 | type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION |
230 | 0 | : AV1_WARP_MODEL_AFFINE; |
231 | 0 | } |
232 | 0 | } else { |
233 | 0 | type = AV1_WARP_MODEL_IDENTITY; |
234 | 0 | } |
235 | 0 | s->cur_frame.gm_type[ref] = type; |
236 | |
|
237 | 0 | if (type >= AV1_WARP_MODEL_ROTZOOM) { |
238 | 0 | read_global_param(s, type, ref, 2); |
239 | 0 | read_global_param(s, type, ref, 3); |
240 | 0 | if (type == AV1_WARP_MODEL_AFFINE) { |
241 | 0 | read_global_param(s, type, ref, 4); |
242 | 0 | read_global_param(s, type, ref, 5); |
243 | 0 | } else { |
244 | 0 | s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3]; |
245 | 0 | s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2]; |
246 | 0 | } |
247 | 0 | } |
248 | 0 | if (type >= AV1_WARP_MODEL_TRANSLATION) { |
249 | 0 | read_global_param(s, type, ref, 0); |
250 | 0 | read_global_param(s, type, ref, 1); |
251 | 0 | } |
252 | 0 | if (type <= AV1_WARP_MODEL_AFFINE) { |
253 | 0 | s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref); |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | static int get_relative_dist(const AV1RawSequenceHeader *seq, |
259 | | unsigned int a, unsigned int b) |
260 | 0 | { |
261 | 0 | unsigned int diff = a - b; |
262 | 0 | unsigned int m = 1 << seq->order_hint_bits_minus_1; |
263 | 0 | return (diff & (m - 1)) - (diff & m); |
264 | 0 | } |
265 | | |
266 | | static void skip_mode_params(AV1DecContext *s) |
267 | 0 | { |
268 | 0 | const AV1RawFrameHeader *header = s->raw_frame_header; |
269 | 0 | const AV1RawSequenceHeader *seq = s->raw_seq; |
270 | |
|
271 | 0 | int forward_idx, backward_idx; |
272 | 0 | int forward_hint, backward_hint; |
273 | 0 | int second_forward_idx, second_forward_hint; |
274 | 0 | int ref_hint, dist, i; |
275 | |
|
276 | 0 | if (header->frame_type == AV1_FRAME_KEY || |
277 | 0 | header->frame_type == AV1_FRAME_INTRA_ONLY || |
278 | 0 | !header->reference_select || !seq->enable_order_hint) |
279 | 0 | return; |
280 | | |
281 | 0 | forward_idx = -1; |
282 | 0 | backward_idx = -1; |
283 | 0 | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
284 | 0 | if (!s->ref[header->ref_frame_idx[i]].raw_frame_header) |
285 | 0 | return; |
286 | 0 | ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint; |
287 | 0 | dist = get_relative_dist(seq, ref_hint, header->order_hint); |
288 | 0 | if (dist < 0) { |
289 | 0 | if (forward_idx < 0 || |
290 | 0 | get_relative_dist(seq, ref_hint, forward_hint) > 0) { |
291 | 0 | forward_idx = i; |
292 | 0 | forward_hint = ref_hint; |
293 | 0 | } |
294 | 0 | } else if (dist > 0) { |
295 | 0 | if (backward_idx < 0 || |
296 | 0 | get_relative_dist(seq, ref_hint, backward_hint) < 0) { |
297 | 0 | backward_idx = i; |
298 | 0 | backward_hint = ref_hint; |
299 | 0 | } |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | 0 | if (forward_idx < 0) { |
304 | 0 | return; |
305 | 0 | } else if (backward_idx >= 0) { |
306 | 0 | s->cur_frame.skip_mode_frame_idx[0] = |
307 | 0 | AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx); |
308 | 0 | s->cur_frame.skip_mode_frame_idx[1] = |
309 | 0 | AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx); |
310 | 0 | return; |
311 | 0 | } |
312 | | |
313 | 0 | second_forward_idx = -1; |
314 | 0 | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
315 | 0 | ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint; |
316 | 0 | if (get_relative_dist(seq, ref_hint, forward_hint) < 0) { |
317 | 0 | if (second_forward_idx < 0 || |
318 | 0 | get_relative_dist(seq, ref_hint, second_forward_hint) > 0) { |
319 | 0 | second_forward_idx = i; |
320 | 0 | second_forward_hint = ref_hint; |
321 | 0 | } |
322 | 0 | } |
323 | 0 | } |
324 | |
|
325 | 0 | if (second_forward_idx < 0) |
326 | 0 | return; |
327 | | |
328 | 0 | s->cur_frame.skip_mode_frame_idx[0] = |
329 | 0 | AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx); |
330 | 0 | s->cur_frame.skip_mode_frame_idx[1] = |
331 | 0 | AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx); |
332 | 0 | } |
333 | | |
334 | | static void coded_lossless_param(AV1DecContext *s) |
335 | 0 | { |
336 | 0 | const AV1RawFrameHeader *header = s->raw_frame_header; |
337 | 0 | int i; |
338 | |
|
339 | 0 | if (header->delta_q_y_dc || header->delta_q_u_ac || |
340 | 0 | header->delta_q_u_dc || header->delta_q_v_ac || |
341 | 0 | header->delta_q_v_dc) { |
342 | 0 | s->cur_frame.coded_lossless = 0; |
343 | 0 | return; |
344 | 0 | } |
345 | | |
346 | 0 | s->cur_frame.coded_lossless = 1; |
347 | 0 | for (i = 0; i < AV1_MAX_SEGMENTS; i++) { |
348 | 0 | int qindex; |
349 | 0 | if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) { |
350 | 0 | qindex = (header->base_q_idx + |
351 | 0 | header->feature_value[i][AV1_SEG_LVL_ALT_Q]); |
352 | 0 | } else { |
353 | 0 | qindex = header->base_q_idx; |
354 | 0 | } |
355 | 0 | qindex = av_clip_uintp2(qindex, 8); |
356 | |
|
357 | 0 | if (qindex) { |
358 | 0 | s->cur_frame.coded_lossless = 0; |
359 | 0 | return; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | | static void order_hint_info(AV1DecContext *s) |
365 | 0 | { |
366 | 0 | const AV1RawFrameHeader *header = s->raw_frame_header; |
367 | 0 | const AV1RawSequenceHeader *seq = s->raw_seq; |
368 | 0 | AV1Frame *frame = &s->cur_frame; |
369 | |
|
370 | 0 | frame->order_hint = header->order_hint; |
371 | |
|
372 | 0 | for (int i = 0; i < AV1_REFS_PER_FRAME; i++) { |
373 | 0 | int ref_name = i + AV1_REF_FRAME_LAST; |
374 | 0 | int ref_slot = header->ref_frame_idx[i]; |
375 | 0 | int ref_order_hint = s->ref[ref_slot].order_hint; |
376 | |
|
377 | 0 | frame->order_hints[ref_name] = ref_order_hint; |
378 | 0 | if (!seq->enable_order_hint) { |
379 | 0 | frame->ref_frame_sign_bias[ref_name] = 0; |
380 | 0 | } else { |
381 | 0 | frame->ref_frame_sign_bias[ref_name] = |
382 | 0 | get_relative_dist(seq, ref_order_hint, |
383 | 0 | frame->order_hint) > 0; |
384 | 0 | } |
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | | static void load_grain_params(AV1DecContext *s) |
389 | 0 | { |
390 | 0 | const AV1RawFrameHeader *header = s->raw_frame_header; |
391 | 0 | const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src; |
392 | 0 | AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain; |
393 | |
|
394 | 0 | if (!film_grain->apply_grain) |
395 | 0 | return; |
396 | | |
397 | 0 | if (film_grain->update_grain) { |
398 | 0 | memcpy(dst, film_grain, sizeof(*dst)); |
399 | 0 | return; |
400 | 0 | } |
401 | | |
402 | 0 | src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain; |
403 | |
|
404 | 0 | memcpy(dst, src, sizeof(*dst)); |
405 | 0 | dst->grain_seed = film_grain->grain_seed; |
406 | 0 | } |
407 | | |
408 | | static int init_tile_data(AV1DecContext *s) |
409 | | |
410 | 6.65k | { |
411 | 6.65k | int cur_tile_num = |
412 | 6.65k | s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows; |
413 | 6.65k | if (s->tile_num < cur_tile_num) { |
414 | 869 | int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num, |
415 | 869 | sizeof(TileGroupInfo)); |
416 | 869 | if (ret < 0) { |
417 | 0 | s->tile_num = 0; |
418 | 0 | return ret; |
419 | 0 | } |
420 | 869 | } |
421 | 6.65k | s->tile_num = cur_tile_num; |
422 | | |
423 | 6.65k | return 0; |
424 | 6.65k | } |
425 | | |
426 | | static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group) |
427 | 2.16k | { |
428 | 2.16k | AV1DecContext *s = avctx->priv_data; |
429 | 2.16k | GetByteContext gb; |
430 | 2.16k | uint16_t tile_num, tile_row, tile_col; |
431 | 2.16k | uint32_t size = 0, size_bytes = 0; |
432 | | |
433 | 2.16k | bytestream2_init(&gb, tile_group->tile_data.data, |
434 | 2.16k | tile_group->tile_data.data_size); |
435 | 2.16k | s->tg_start = tile_group->tg_start; |
436 | 2.16k | s->tg_end = tile_group->tg_end; |
437 | | |
438 | 4.02k | for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) { |
439 | 4.02k | tile_row = tile_num / s->raw_frame_header->tile_cols; |
440 | 4.02k | tile_col = tile_num % s->raw_frame_header->tile_cols; |
441 | | |
442 | 4.02k | if (tile_num == tile_group->tg_end) { |
443 | 1.40k | s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb); |
444 | 1.40k | s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb); |
445 | 1.40k | s->tile_group_info[tile_num].tile_row = tile_row; |
446 | 1.40k | s->tile_group_info[tile_num].tile_column = tile_col; |
447 | 1.40k | return 0; |
448 | 1.40k | } |
449 | 2.61k | size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1; |
450 | 2.61k | if (bytestream2_get_bytes_left(&gb) < size_bytes) |
451 | 283 | return AVERROR_INVALIDDATA; |
452 | 2.33k | size = 0; |
453 | 5.81k | for (int i = 0; i < size_bytes; i++) |
454 | 3.48k | size |= bytestream2_get_byteu(&gb) << 8 * i; |
455 | 2.33k | if (bytestream2_get_bytes_left(&gb) <= size) |
456 | 480 | return AVERROR_INVALIDDATA; |
457 | 1.85k | size++; |
458 | | |
459 | 1.85k | s->tile_group_info[tile_num].tile_size = size; |
460 | 1.85k | s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb); |
461 | 1.85k | s->tile_group_info[tile_num].tile_row = tile_row; |
462 | 1.85k | s->tile_group_info[tile_num].tile_column = tile_col; |
463 | | |
464 | 1.85k | bytestream2_skipu(&gb, size); |
465 | 1.85k | } |
466 | | |
467 | 0 | return 0; |
468 | | |
469 | 2.16k | } |
470 | | |
471 | | static enum AVPixelFormat get_sw_pixel_format(void *logctx, |
472 | | const AV1RawSequenceHeader *seq) |
473 | 7.00k | { |
474 | 7.00k | int bit_depth; |
475 | 7.00k | enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE; |
476 | | |
477 | 7.00k | if (seq->seq_profile == 2 && seq->color_config.high_bitdepth) |
478 | 1.75k | bit_depth = seq->color_config.twelve_bit ? 12 : 10; |
479 | 5.25k | else if (seq->seq_profile <= 2) |
480 | 5.25k | bit_depth = seq->color_config.high_bitdepth ? 10 : 8; |
481 | 0 | else { |
482 | 0 | av_log(logctx, AV_LOG_ERROR, |
483 | 0 | "Unknown AV1 profile %d.\n", seq->seq_profile); |
484 | 0 | return AV_PIX_FMT_NONE; |
485 | 0 | } |
486 | | |
487 | 7.00k | 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 | 4.44k | if (seq->color_config.subsampling_x == 0 && |
490 | 4.44k | seq->color_config.subsampling_y == 0) { |
491 | 2.10k | if (bit_depth == 8) |
492 | 618 | pix_fmt = AV_PIX_FMT_YUV444P; |
493 | 1.48k | else if (bit_depth == 10) |
494 | 864 | pix_fmt = AV_PIX_FMT_YUV444P10; |
495 | 619 | else if (bit_depth == 12) |
496 | 619 | pix_fmt = AV_PIX_FMT_YUV444P12; |
497 | 0 | else |
498 | 0 | av_assert0(0); |
499 | 2.33k | } else if (seq->color_config.subsampling_x == 1 && |
500 | 2.33k | seq->color_config.subsampling_y == 0) { |
501 | 1.29k | if (bit_depth == 8) |
502 | 642 | pix_fmt = AV_PIX_FMT_YUV422P; |
503 | 648 | else if (bit_depth == 10) |
504 | 265 | pix_fmt = AV_PIX_FMT_YUV422P10; |
505 | 383 | else if (bit_depth == 12) |
506 | 383 | pix_fmt = AV_PIX_FMT_YUV422P12; |
507 | 0 | else |
508 | 0 | av_assert0(0); |
509 | 1.29k | } else if (seq->color_config.subsampling_x == 1 && |
510 | 1.04k | seq->color_config.subsampling_y == 1) { |
511 | 1.04k | if (bit_depth == 8) |
512 | 464 | pix_fmt = AV_PIX_FMT_YUV420P; |
513 | 585 | else if (bit_depth == 10) |
514 | 369 | pix_fmt = AV_PIX_FMT_YUV420P10; |
515 | 216 | else if (bit_depth == 12) |
516 | 216 | pix_fmt = AV_PIX_FMT_YUV420P12; |
517 | 0 | else |
518 | 0 | av_assert0(0); |
519 | 1.04k | } |
520 | 4.44k | } else { |
521 | 2.56k | if (bit_depth == 8) |
522 | 1.34k | pix_fmt = AV_PIX_FMT_GRAY8; |
523 | 1.22k | else if (bit_depth == 10) |
524 | 977 | pix_fmt = AV_PIX_FMT_GRAY10; |
525 | 249 | else if (bit_depth == 12) |
526 | 249 | pix_fmt = AV_PIX_FMT_GRAY12; |
527 | 0 | else |
528 | 0 | av_assert0(0); |
529 | 2.56k | } |
530 | | |
531 | 7.00k | return pix_fmt; |
532 | 7.00k | } |
533 | | |
534 | | static int get_pixel_format(AVCodecContext *avctx) |
535 | 4.28k | { |
536 | 4.28k | AV1DecContext *s = avctx->priv_data; |
537 | 4.28k | const AV1RawSequenceHeader *seq = s->raw_seq; |
538 | 4.28k | int ret; |
539 | 4.28k | enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq); |
540 | 4.28k | #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \ |
541 | 4.28k | CONFIG_AV1_D3D11VA_HWACCEL * 2 + \ |
542 | 4.28k | CONFIG_AV1_D3D12VA_HWACCEL + \ |
543 | 4.28k | CONFIG_AV1_NVDEC_HWACCEL + \ |
544 | 4.28k | CONFIG_AV1_VAAPI_HWACCEL + \ |
545 | 4.28k | CONFIG_AV1_VDPAU_HWACCEL + \ |
546 | 4.28k | CONFIG_AV1_VIDEOTOOLBOX_HWACCEL + \ |
547 | 4.28k | CONFIG_AV1_VULKAN_HWACCEL) |
548 | 4.28k | enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts; |
549 | | |
550 | 4.28k | if (pix_fmt == AV_PIX_FMT_NONE) |
551 | 0 | return -1; |
552 | | |
553 | 4.28k | switch (pix_fmt) { |
554 | 223 | 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 | 223 | break; |
581 | 236 | 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 | 236 | break; |
608 | 199 | case AV_PIX_FMT_YUV420P12: |
609 | | #if CONFIG_AV1_VULKAN_HWACCEL |
610 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
611 | | #endif |
612 | 199 | break; |
613 | 532 | case AV_PIX_FMT_YUV422P: |
614 | | #if CONFIG_AV1_VULKAN_HWACCEL |
615 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
616 | | #endif |
617 | 532 | break; |
618 | 219 | case AV_PIX_FMT_YUV422P10: |
619 | | #if CONFIG_AV1_VULKAN_HWACCEL |
620 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
621 | | #endif |
622 | 219 | break; |
623 | 351 | case AV_PIX_FMT_YUV422P12: |
624 | | #if CONFIG_AV1_VULKAN_HWACCEL |
625 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
626 | | #endif |
627 | 351 | break; |
628 | 221 | case AV_PIX_FMT_YUV444P: |
629 | | #if CONFIG_AV1_VULKAN_HWACCEL |
630 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
631 | | #endif |
632 | 221 | break; |
633 | 414 | case AV_PIX_FMT_YUV444P10: |
634 | | #if CONFIG_AV1_VULKAN_HWACCEL |
635 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
636 | | #endif |
637 | 414 | break; |
638 | 304 | case AV_PIX_FMT_YUV444P12: |
639 | | #if CONFIG_AV1_VULKAN_HWACCEL |
640 | | *fmtp++ = AV_PIX_FMT_VULKAN; |
641 | | #endif |
642 | 304 | break; |
643 | 794 | case AV_PIX_FMT_GRAY8: |
644 | | #if CONFIG_AV1_NVDEC_HWACCEL |
645 | | *fmtp++ = AV_PIX_FMT_CUDA; |
646 | | #endif |
647 | 794 | break; |
648 | 567 | case AV_PIX_FMT_GRAY10: |
649 | | #if CONFIG_AV1_NVDEC_HWACCEL |
650 | | *fmtp++ = AV_PIX_FMT_CUDA; |
651 | | #endif |
652 | 567 | break; |
653 | 4.28k | } |
654 | | |
655 | 4.28k | *fmtp++ = pix_fmt; |
656 | 4.28k | *fmtp = AV_PIX_FMT_NONE; |
657 | | |
658 | 4.28k | for (int i = 0; pix_fmts[i] != pix_fmt; i++) |
659 | 0 | if (pix_fmts[i] == avctx->pix_fmt) { |
660 | 0 | s->pix_fmt = pix_fmt; |
661 | 0 | return 1; |
662 | 0 | } |
663 | | |
664 | 4.28k | 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 | 4.28k | if (!avctx->hwaccel) { |
672 | 4.28k | av_log(avctx, AV_LOG_ERROR, "Your platform doesn't support" |
673 | 4.28k | " hardware accelerated AV1 decoding.\n"); |
674 | 4.28k | avctx->pix_fmt = AV_PIX_FMT_NONE; |
675 | 4.28k | return AVERROR(ENOSYS); |
676 | 4.28k | } |
677 | | |
678 | 0 | s->pix_fmt = pix_fmt; |
679 | 0 | avctx->pix_fmt = ret; |
680 | |
|
681 | 0 | av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n", |
682 | 0 | av_get_pix_fmt_name(avctx->pix_fmt)); |
683 | |
|
684 | 0 | return 0; |
685 | 4.28k | } |
686 | | |
687 | | static void av1_frame_unref(AV1Frame *f) |
688 | 591k | { |
689 | 591k | ff_progress_frame_unref(&f->pf); |
690 | 591k | av_refstruct_unref(&f->hwaccel_picture_private); |
691 | 591k | av_refstruct_unref(&f->header_ref); |
692 | 591k | f->raw_frame_header = NULL; |
693 | 591k | f->spatial_id = f->temporal_id = 0; |
694 | 591k | memset(f->skip_mode_frame_idx, 0, |
695 | 591k | 2 * sizeof(uint8_t)); |
696 | 591k | memset(&f->film_grain, 0, sizeof(f->film_grain)); |
697 | 591k | f->coded_lossless = 0; |
698 | 591k | } |
699 | | |
700 | | static void av1_frame_replace(AV1Frame *dst, const AV1Frame *src) |
701 | 9.53k | { |
702 | 9.53k | av_assert1(dst != src); |
703 | | |
704 | 9.53k | av_refstruct_replace(&dst->header_ref, src->header_ref); |
705 | | |
706 | 9.53k | dst->raw_frame_header = src->raw_frame_header; |
707 | | |
708 | 9.53k | ff_progress_frame_replace(&dst->pf, &src->pf); |
709 | | |
710 | 9.53k | av_refstruct_replace(&dst->hwaccel_picture_private, |
711 | 9.53k | src->hwaccel_picture_private); |
712 | | |
713 | 9.53k | dst->spatial_id = src->spatial_id; |
714 | 9.53k | dst->temporal_id = src->temporal_id; |
715 | 9.53k | memcpy(dst->gm_invalid, |
716 | 9.53k | src->gm_invalid, |
717 | 9.53k | AV1_NUM_REF_FRAMES * sizeof(uint8_t)); |
718 | 9.53k | memcpy(dst->gm_type, |
719 | 9.53k | src->gm_type, |
720 | 9.53k | AV1_NUM_REF_FRAMES * sizeof(uint8_t)); |
721 | 9.53k | memcpy(dst->gm_params, |
722 | 9.53k | src->gm_params, |
723 | 9.53k | AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t)); |
724 | 9.53k | memcpy(dst->skip_mode_frame_idx, |
725 | 9.53k | src->skip_mode_frame_idx, |
726 | 9.53k | 2 * sizeof(uint8_t)); |
727 | 9.53k | memcpy(&dst->film_grain, |
728 | 9.53k | &src->film_grain, |
729 | 9.53k | sizeof(dst->film_grain)); |
730 | 9.53k | dst->coded_lossless = src->coded_lossless; |
731 | | |
732 | 9.53k | dst->order_hint = src->order_hint; |
733 | 9.53k | memcpy(dst->ref_frame_sign_bias, src->ref_frame_sign_bias, |
734 | 9.53k | sizeof(dst->ref_frame_sign_bias)); |
735 | 9.53k | memcpy(dst->order_hints, src->order_hints, |
736 | 9.53k | sizeof(dst->order_hints)); |
737 | | |
738 | 9.53k | dst->force_integer_mv = src->force_integer_mv; |
739 | 9.53k | } |
740 | | |
741 | | static av_cold int av1_decode_free(AVCodecContext *avctx) |
742 | 6.70k | { |
743 | 6.70k | AV1DecContext *s = avctx->priv_data; |
744 | 6.70k | AV1RawMetadataITUTT35 itut_t35; |
745 | | |
746 | 60.3k | for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) |
747 | 53.6k | av1_frame_unref(&s->ref[i]); |
748 | 6.70k | av1_frame_unref(&s->cur_frame); |
749 | 6.70k | av_buffer_unref(&s->seq_data_ref); |
750 | 6.70k | av_refstruct_unref(&s->seq_ref); |
751 | 6.70k | av_refstruct_unref(&s->header_ref); |
752 | 6.70k | av_refstruct_unref(&s->cll_ref); |
753 | 6.70k | av_refstruct_unref(&s->mdcv_ref); |
754 | 6.70k | av_freep(&s->tile_group_info); |
755 | | |
756 | 23.4k | while (s->itut_t35_fifo && av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) |
757 | 16.7k | av_buffer_unref(&itut_t35.payload_ref); |
758 | 6.70k | av_fifo_freep2(&s->itut_t35_fifo); |
759 | | |
760 | 6.70k | ff_cbs_fragment_free(&s->current_obu); |
761 | 6.70k | ff_cbs_close(&s->cbc); |
762 | 6.70k | ff_dovi_ctx_unref(&s->dovi); |
763 | | |
764 | 6.70k | return 0; |
765 | 6.70k | } |
766 | | |
767 | | static int set_context_with_sequence(AVCodecContext *avctx, |
768 | | const AV1RawSequenceHeader *seq) |
769 | 6.90k | { |
770 | 6.90k | int width = seq->max_frame_width_minus_1 + 1; |
771 | 6.90k | int height = seq->max_frame_height_minus_1 + 1; |
772 | | |
773 | 6.90k | avctx->profile = seq->seq_profile; |
774 | 6.90k | avctx->level = seq->seq_level_idx[0]; |
775 | | |
776 | 6.90k | avctx->color_range = |
777 | 6.90k | seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
778 | 6.90k | avctx->color_primaries = seq->color_config.color_primaries; |
779 | 6.90k | avctx->colorspace = seq->color_config.matrix_coefficients; |
780 | 6.90k | avctx->color_trc = seq->color_config.transfer_characteristics; |
781 | | |
782 | 6.90k | switch (seq->color_config.chroma_sample_position) { |
783 | 375 | case AV1_CSP_VERTICAL: |
784 | 375 | avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; |
785 | 375 | break; |
786 | 452 | case AV1_CSP_COLOCATED: |
787 | 452 | avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; |
788 | 452 | break; |
789 | 6.90k | } |
790 | | |
791 | 6.90k | #if FF_API_CODEC_PROPS |
792 | 6.90k | FF_DISABLE_DEPRECATION_WARNINGS |
793 | 6.90k | if (seq->film_grain_params_present) |
794 | 2.92k | avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN; |
795 | 3.98k | else |
796 | 3.98k | avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN; |
797 | 6.90k | FF_ENABLE_DEPRECATION_WARNINGS |
798 | 6.90k | #endif |
799 | | |
800 | 6.90k | if (avctx->width != width || avctx->height != height) { |
801 | 3.66k | int ret = ff_set_dimensions(avctx, width, height); |
802 | 3.66k | if (ret < 0) |
803 | 443 | return ret; |
804 | 3.66k | } |
805 | | |
806 | 6.46k | if (seq->timing_info_present_flag) |
807 | 1.46k | avctx->framerate = ff_av1_framerate(1LL + seq->timing_info.num_ticks_per_picture_minus_1, |
808 | 1.46k | seq->timing_info.num_units_in_display_tick, |
809 | 1.46k | seq->timing_info.time_scale); |
810 | | |
811 | 6.46k | if (avctx->pix_fmt == AV_PIX_FMT_NONE) |
812 | 2.72k | avctx->pix_fmt = get_sw_pixel_format(avctx, seq); |
813 | | |
814 | 6.46k | return 0; |
815 | 6.90k | } |
816 | | |
817 | | static int update_context_with_frame_header(AVCodecContext *avctx, |
818 | | const AV1RawFrameHeader *header) |
819 | 0 | { |
820 | 0 | AVRational aspect_ratio; |
821 | 0 | int width = header->frame_width_minus_1 + 1; |
822 | 0 | int height = header->frame_height_minus_1 + 1; |
823 | 0 | int r_width = header->render_width_minus_1 + 1; |
824 | 0 | int r_height = header->render_height_minus_1 + 1; |
825 | 0 | int ret; |
826 | |
|
827 | 0 | if (avctx->width != width || avctx->height != height) { |
828 | 0 | ret = ff_set_dimensions(avctx, width, height); |
829 | 0 | if (ret < 0) |
830 | 0 | return ret; |
831 | 0 | } |
832 | | |
833 | 0 | av_reduce(&aspect_ratio.num, &aspect_ratio.den, |
834 | 0 | (int64_t)height * r_width, |
835 | 0 | (int64_t)width * r_height, |
836 | 0 | INT_MAX); |
837 | |
|
838 | 0 | if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) { |
839 | 0 | ret = ff_set_sar(avctx, aspect_ratio); |
840 | 0 | if (ret < 0) |
841 | 0 | return ret; |
842 | 0 | } |
843 | | |
844 | 0 | return 0; |
845 | 0 | } |
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 | | static av_cold int av1_decode_init(AVCodecContext *avctx) |
858 | 6.70k | { |
859 | 6.70k | AV1DecContext *s = avctx->priv_data; |
860 | 6.70k | AV1RawSequenceHeader *seq; |
861 | 6.70k | const AVPacketSideData *sd; |
862 | 6.70k | int ret; |
863 | | |
864 | 6.70k | s->avctx = avctx; |
865 | 6.70k | s->pkt = avctx->internal->in_pkt; |
866 | 6.70k | s->pix_fmt = AV_PIX_FMT_NONE; |
867 | | |
868 | 6.70k | ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx); |
869 | 6.70k | if (ret < 0) |
870 | 0 | return ret; |
871 | | |
872 | 6.70k | s->cbc->decompose_unit_types = decompose_unit_types; |
873 | 6.70k | s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types); |
874 | | |
875 | 6.70k | s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35), |
876 | 6.70k | AV_FIFO_FLAG_AUTO_GROW); |
877 | 6.70k | if (!s->itut_t35_fifo) |
878 | 0 | return AVERROR(ENOMEM); |
879 | | |
880 | 6.70k | av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0); |
881 | | |
882 | 6.70k | if (avctx->extradata && avctx->extradata_size) { |
883 | 77 | ret = ff_cbs_read_extradata_from_codec(s->cbc, |
884 | 77 | &s->current_obu, |
885 | 77 | avctx); |
886 | 77 | if (ret < 0) { |
887 | 27 | av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n"); |
888 | 27 | goto end; |
889 | 27 | } |
890 | | |
891 | 50 | seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header; |
892 | 50 | if (!seq) { |
893 | 48 | av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n"); |
894 | 48 | goto end; |
895 | 48 | } |
896 | | |
897 | 2 | ret = set_context_with_sequence(avctx, seq); |
898 | 2 | if (ret < 0) { |
899 | 1 | av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n"); |
900 | 1 | goto end; |
901 | 1 | } |
902 | | |
903 | 77 | end: |
904 | 77 | ff_cbs_fragment_reset(&s->current_obu); |
905 | 77 | } |
906 | | |
907 | 6.70k | s->dovi.logctx = avctx; |
908 | 6.70k | s->dovi.cfg.dv_profile = 10; // default for AV1 |
909 | 6.70k | sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF); |
910 | 6.70k | if (sd && sd->size >= sizeof(s->dovi.cfg)) |
911 | 0 | s->dovi.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data; |
912 | | |
913 | 6.70k | return ret; |
914 | 6.70k | } |
915 | | |
916 | | static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f) |
917 | 0 | { |
918 | 0 | AV1DecContext *s = avctx->priv_data; |
919 | 0 | AV1RawFrameHeader *header= s->raw_frame_header; |
920 | 0 | AVFrame *frame; |
921 | 0 | int ret; |
922 | |
|
923 | 0 | ret = update_context_with_frame_header(avctx, header); |
924 | 0 | if (ret < 0) { |
925 | 0 | av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n"); |
926 | 0 | return ret; |
927 | 0 | } |
928 | | |
929 | 0 | ret = ff_progress_frame_get_buffer(avctx, &f->pf, AV_GET_BUFFER_FLAG_REF); |
930 | 0 | if (ret < 0) |
931 | 0 | goto fail; |
932 | | |
933 | 0 | frame = f->f; |
934 | 0 | if (header->frame_type == AV1_FRAME_KEY) |
935 | 0 | frame->flags |= AV_FRAME_FLAG_KEY; |
936 | 0 | else |
937 | 0 | frame->flags &= ~AV_FRAME_FLAG_KEY; |
938 | |
|
939 | 0 | switch (header->frame_type) { |
940 | 0 | case AV1_FRAME_KEY: |
941 | 0 | case AV1_FRAME_INTRA_ONLY: |
942 | 0 | frame->pict_type = AV_PICTURE_TYPE_I; |
943 | 0 | break; |
944 | 0 | case AV1_FRAME_INTER: |
945 | 0 | frame->pict_type = AV_PICTURE_TYPE_P; |
946 | 0 | break; |
947 | 0 | case AV1_FRAME_SWITCH: |
948 | 0 | frame->pict_type = AV_PICTURE_TYPE_SP; |
949 | 0 | break; |
950 | 0 | } |
951 | | |
952 | 0 | ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private); |
953 | 0 | if (ret < 0) |
954 | 0 | goto fail; |
955 | | |
956 | 0 | return 0; |
957 | | |
958 | 0 | fail: |
959 | 0 | av1_frame_unref(f); |
960 | 0 | return ret; |
961 | 0 | } |
962 | | |
963 | | static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame, |
964 | | const AV1RawMetadataITUTT35 *itut_t35) |
965 | 0 | { |
966 | 0 | GetByteContext gb; |
967 | 0 | AV1DecContext *s = avctx->priv_data; |
968 | 0 | int ret, provider_code, country_code; |
969 | |
|
970 | 0 | bytestream2_init(&gb, itut_t35->payload, itut_t35->payload_size); |
971 | |
|
972 | 0 | provider_code = bytestream2_get_be16(&gb); |
973 | 0 | country_code = itut_t35->itu_t_t35_country_code ; |
974 | 0 | if (country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_ATSC) { |
975 | 0 | uint32_t user_identifier = bytestream2_get_be32(&gb); |
976 | 0 | switch (user_identifier) { |
977 | 0 | case MKBETAG('G', 'A', '9', '4'): { // closed captions |
978 | 0 | AVBufferRef *buf = NULL; |
979 | |
|
980 | 0 | ret = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb)); |
981 | 0 | if (ret < 0) |
982 | 0 | return ret; |
983 | 0 | if (!ret) |
984 | 0 | break; |
985 | | |
986 | 0 | ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_A53_CC, &buf); |
987 | 0 | if (ret < 0) |
988 | 0 | return ret; |
989 | | |
990 | 0 | #if FF_API_CODEC_PROPS |
991 | 0 | FF_DISABLE_DEPRECATION_WARNINGS |
992 | 0 | avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; |
993 | 0 | FF_ENABLE_DEPRECATION_WARNINGS |
994 | 0 | #endif |
995 | 0 | break; |
996 | 0 | } |
997 | 0 | default: // ignore unsupported identifiers |
998 | 0 | break; |
999 | 0 | } |
1000 | 0 | } else if (country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_SAMSUNG) { |
1001 | 0 | AVDynamicHDRPlus *hdrplus; |
1002 | 0 | int provider_oriented_code = bytestream2_get_be16(&gb); |
1003 | 0 | int application_identifier = bytestream2_get_byte(&gb); |
1004 | |
|
1005 | 0 | if (provider_oriented_code != 1 || application_identifier != 4) |
1006 | 0 | return 0; // ignore |
1007 | | |
1008 | 0 | hdrplus = av_dynamic_hdr_plus_create_side_data(frame); |
1009 | 0 | if (!hdrplus) |
1010 | 0 | return AVERROR(ENOMEM); |
1011 | | |
1012 | 0 | ret = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer, |
1013 | 0 | bytestream2_get_bytes_left(&gb)); |
1014 | 0 | if (ret < 0) |
1015 | 0 | return ret; |
1016 | 0 | } else if (country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_DOLBY) { |
1017 | 0 | int provider_oriented_code = bytestream2_get_be32(&gb); |
1018 | 0 | if (provider_oriented_code != 0x800) |
1019 | 0 | return 0; // ignore |
1020 | | |
1021 | 0 | ret = ff_dovi_rpu_parse(&s->dovi, gb.buffer, gb.buffer_end - gb.buffer, |
1022 | 0 | avctx->err_recognition); |
1023 | 0 | if (ret < 0) { |
1024 | 0 | av_log(avctx, AV_LOG_WARNING, "Error parsing DOVI OBU.\n"); |
1025 | 0 | return 0; // ignore |
1026 | 0 | } |
1027 | | |
1028 | 0 | ret = ff_dovi_attach_side_data(&s->dovi, frame); |
1029 | 0 | if (ret < 0) |
1030 | 0 | return ret; |
1031 | 0 | } else { |
1032 | | // ignore unsupported provider codes |
1033 | 0 | } |
1034 | | |
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | | |
1038 | | static int export_metadata(AVCodecContext *avctx, AVFrame *frame) |
1039 | 0 | { |
1040 | 0 | AV1DecContext *s = avctx->priv_data; |
1041 | 0 | AV1RawMetadataITUTT35 itut_t35; |
1042 | 0 | int ret = 0; |
1043 | |
|
1044 | 0 | if (s->mdcv) { |
1045 | 0 | AVMasteringDisplayMetadata *mastering; |
1046 | |
|
1047 | 0 | ret = ff_decode_mastering_display_new(avctx, frame, &mastering); |
1048 | 0 | if (ret < 0) |
1049 | 0 | return ret; |
1050 | | |
1051 | 0 | if (mastering) { |
1052 | 0 | for (int i = 0; i < 3; i++) { |
1053 | 0 | mastering->display_primaries[i][0] = av_make_q(s->mdcv->primary_chromaticity_x[i], 1 << 16); |
1054 | 0 | mastering->display_primaries[i][1] = av_make_q(s->mdcv->primary_chromaticity_y[i], 1 << 16); |
1055 | 0 | } |
1056 | 0 | mastering->white_point[0] = av_make_q(s->mdcv->white_point_chromaticity_x, 1 << 16); |
1057 | 0 | mastering->white_point[1] = av_make_q(s->mdcv->white_point_chromaticity_y, 1 << 16); |
1058 | |
|
1059 | 0 | mastering->max_luminance = av_make_q(s->mdcv->luminance_max, 1 << 8); |
1060 | 0 | mastering->min_luminance = av_make_q(s->mdcv->luminance_min, 1 << 14); |
1061 | |
|
1062 | 0 | mastering->has_primaries = 1; |
1063 | 0 | mastering->has_luminance = 1; |
1064 | 0 | } |
1065 | 0 | } |
1066 | | |
1067 | 0 | if (s->cll) { |
1068 | 0 | AVContentLightMetadata *light; |
1069 | |
|
1070 | 0 | ret = ff_decode_content_light_new(avctx, frame, &light); |
1071 | 0 | if (ret < 0) |
1072 | 0 | return ret; |
1073 | | |
1074 | 0 | if (light) { |
1075 | 0 | light->MaxCLL = s->cll->max_cll; |
1076 | 0 | light->MaxFALL = s->cll->max_fall; |
1077 | 0 | } |
1078 | 0 | } |
1079 | | |
1080 | 0 | while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) { |
1081 | 0 | if (ret >= 0) |
1082 | 0 | ret = export_itut_t35(avctx, frame, &itut_t35); |
1083 | 0 | av_buffer_unref(&itut_t35.payload_ref); |
1084 | 0 | } |
1085 | |
|
1086 | 0 | return ret; |
1087 | 0 | } |
1088 | | |
1089 | | static int export_film_grain(AVCodecContext *avctx, AVFrame *frame) |
1090 | 0 | { |
1091 | 0 | AV1DecContext *s = avctx->priv_data; |
1092 | 0 | const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain; |
1093 | 0 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(frame->format); |
1094 | 0 | AVFilmGrainParams *fgp; |
1095 | 0 | AVFilmGrainAOMParams *aom; |
1096 | |
|
1097 | 0 | av_assert0(pixdesc); |
1098 | 0 | if (!film_grain->apply_grain) |
1099 | 0 | return 0; |
1100 | | |
1101 | 0 | fgp = av_film_grain_params_create_side_data(frame); |
1102 | 0 | if (!fgp) |
1103 | 0 | return AVERROR(ENOMEM); |
1104 | | |
1105 | 0 | fgp->type = AV_FILM_GRAIN_PARAMS_AV1; |
1106 | 0 | fgp->seed = film_grain->grain_seed; |
1107 | 0 | fgp->width = frame->width; |
1108 | 0 | fgp->height = frame->height; |
1109 | 0 | fgp->color_range = frame->color_range; |
1110 | 0 | fgp->color_primaries = frame->color_primaries; |
1111 | 0 | fgp->color_trc = frame->color_trc; |
1112 | 0 | fgp->color_space = frame->colorspace; |
1113 | 0 | fgp->subsampling_x = pixdesc->log2_chroma_w; |
1114 | 0 | fgp->subsampling_y = pixdesc->log2_chroma_h; |
1115 | |
|
1116 | 0 | aom = &fgp->codec.aom; |
1117 | 0 | aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma; |
1118 | 0 | aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8; |
1119 | 0 | aom->ar_coeff_lag = film_grain->ar_coeff_lag; |
1120 | 0 | aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6; |
1121 | 0 | aom->grain_scale_shift = film_grain->grain_scale_shift; |
1122 | 0 | aom->overlap_flag = film_grain->overlap_flag; |
1123 | 0 | aom->limit_output_range = film_grain->clip_to_restricted_range; |
1124 | |
|
1125 | 0 | aom->num_y_points = film_grain->num_y_points; |
1126 | 0 | for (int i = 0; i < film_grain->num_y_points; i++) { |
1127 | 0 | aom->y_points[i][0] = film_grain->point_y_value[i]; |
1128 | 0 | aom->y_points[i][1] = film_grain->point_y_scaling[i]; |
1129 | 0 | } |
1130 | 0 | aom->num_uv_points[0] = film_grain->num_cb_points; |
1131 | 0 | for (int i = 0; i < film_grain->num_cb_points; i++) { |
1132 | 0 | aom->uv_points[0][i][0] = film_grain->point_cb_value[i]; |
1133 | 0 | aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i]; |
1134 | 0 | } |
1135 | 0 | aom->num_uv_points[1] = film_grain->num_cr_points; |
1136 | 0 | for (int i = 0; i < film_grain->num_cr_points; i++) { |
1137 | 0 | aom->uv_points[1][i][0] = film_grain->point_cr_value[i]; |
1138 | 0 | aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i]; |
1139 | 0 | } |
1140 | |
|
1141 | 0 | for (int i = 0; i < 24; i++) { |
1142 | 0 | aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128; |
1143 | 0 | } |
1144 | 0 | for (int i = 0; i < 25; i++) { |
1145 | 0 | aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128; |
1146 | 0 | aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128; |
1147 | 0 | } |
1148 | |
|
1149 | 0 | aom->uv_mult[0] = film_grain->cb_mult; |
1150 | 0 | aom->uv_mult[1] = film_grain->cr_mult; |
1151 | 0 | aom->uv_mult_luma[0] = film_grain->cb_luma_mult; |
1152 | 0 | aom->uv_mult_luma[1] = film_grain->cr_luma_mult; |
1153 | 0 | aom->uv_offset[0] = film_grain->cb_offset; |
1154 | 0 | aom->uv_offset[1] = film_grain->cr_offset; |
1155 | |
|
1156 | 0 | return 0; |
1157 | 0 | } |
1158 | | |
1159 | | static int set_output_frame(AVCodecContext *avctx, AVFrame *frame) |
1160 | 0 | { |
1161 | 0 | AV1DecContext *s = avctx->priv_data; |
1162 | 0 | const AVFrame *srcframe = s->cur_frame.f; |
1163 | 0 | AVPacket *pkt = s->pkt; |
1164 | 0 | int ret; |
1165 | | |
1166 | | // TODO: all layers |
1167 | 0 | if (s->operating_point_idc && |
1168 | 0 | av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id) |
1169 | 0 | return 0; |
1170 | | |
1171 | 0 | ret = av_frame_ref(frame, srcframe); |
1172 | 0 | if (ret < 0) |
1173 | 0 | return ret; |
1174 | | |
1175 | 0 | ret = export_metadata(avctx, frame); |
1176 | 0 | if (ret < 0) { |
1177 | 0 | av_frame_unref(frame); |
1178 | 0 | return ret; |
1179 | 0 | } |
1180 | | |
1181 | 0 | if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) { |
1182 | 0 | ret = export_film_grain(avctx, frame); |
1183 | 0 | if (ret < 0) { |
1184 | 0 | av_frame_unref(frame); |
1185 | 0 | return ret; |
1186 | 0 | } |
1187 | 0 | } |
1188 | | |
1189 | 0 | frame->pts = pkt->pts; |
1190 | 0 | frame->pkt_dts = pkt->dts; |
1191 | |
|
1192 | 0 | av_packet_unref(pkt); |
1193 | |
|
1194 | 0 | return 0; |
1195 | 0 | } |
1196 | | |
1197 | | static void update_reference_list(AVCodecContext *avctx) |
1198 | 2.17k | { |
1199 | 2.17k | AV1DecContext *s = avctx->priv_data; |
1200 | 2.17k | const AV1RawFrameHeader *header = s->raw_frame_header; |
1201 | | |
1202 | 19.5k | for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
1203 | 17.3k | if (header->refresh_frame_flags & (1 << i)) |
1204 | 8.52k | av1_frame_replace(&s->ref[i], &s->cur_frame); |
1205 | 17.3k | } |
1206 | 2.17k | } |
1207 | | |
1208 | | static int get_current_frame(AVCodecContext *avctx) |
1209 | 6.65k | { |
1210 | 6.65k | AV1DecContext *s = avctx->priv_data; |
1211 | 6.65k | int ret; |
1212 | | |
1213 | 6.65k | av1_frame_unref(&s->cur_frame); |
1214 | | |
1215 | 6.65k | s->cur_frame.header_ref = av_refstruct_ref(s->header_ref); |
1216 | | |
1217 | 6.65k | s->cur_frame.raw_frame_header = s->raw_frame_header; |
1218 | | |
1219 | 6.65k | ret = init_tile_data(s); |
1220 | 6.65k | if (ret < 0) { |
1221 | 0 | av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n"); |
1222 | 0 | return ret; |
1223 | 0 | } |
1224 | | |
1225 | 6.65k | if ((avctx->skip_frame >= AVDISCARD_NONINTRA && |
1226 | 6.65k | (s->raw_frame_header->frame_type != AV1_FRAME_KEY && |
1227 | 2.77k | s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) || |
1228 | 6.65k | (avctx->skip_frame >= AVDISCARD_NONKEY && |
1229 | 6.11k | s->raw_frame_header->frame_type != AV1_FRAME_KEY) || |
1230 | 6.65k | avctx->skip_frame >= AVDISCARD_ALL) |
1231 | 2.37k | return 0; |
1232 | | |
1233 | 4.28k | if (s->pix_fmt == AV_PIX_FMT_NONE) { |
1234 | 4.28k | ret = get_pixel_format(avctx); |
1235 | 4.28k | if (ret < 0) { |
1236 | 4.28k | av_log(avctx, AV_LOG_ERROR, "Failed to get pixel format.\n"); |
1237 | 4.28k | return ret; |
1238 | 4.28k | } |
1239 | | |
1240 | 0 | if (!ret && FF_HW_HAS_CB(avctx, decode_params)) { |
1241 | 0 | ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER, |
1242 | 0 | s->seq_data_ref->data, s->seq_data_ref->size); |
1243 | 0 | if (ret < 0) { |
1244 | 0 | av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n"); |
1245 | 0 | return ret; |
1246 | 0 | } |
1247 | 0 | } |
1248 | 0 | } |
1249 | | |
1250 | 0 | ret = av1_frame_alloc(avctx, &s->cur_frame); |
1251 | 0 | if (ret < 0) { |
1252 | 0 | av_log(avctx, AV_LOG_ERROR, |
1253 | 0 | "Failed to allocate space for current frame.\n"); |
1254 | 0 | return ret; |
1255 | 0 | } |
1256 | | |
1257 | 0 | global_motion_params(s); |
1258 | 0 | skip_mode_params(s); |
1259 | 0 | coded_lossless_param(s); |
1260 | 0 | order_hint_info(s); |
1261 | 0 | load_grain_params(s); |
1262 | |
|
1263 | 0 | s->cur_frame.force_integer_mv = |
1264 | 0 | s->raw_frame_header->force_integer_mv || |
1265 | 0 | s->raw_frame_header->frame_type == AV1_FRAME_KEY || |
1266 | 0 | s->raw_frame_header->frame_type == AV1_FRAME_INTRA_ONLY; |
1267 | |
|
1268 | 0 | return ret; |
1269 | 0 | } |
1270 | | |
1271 | | static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) |
1272 | 127k | { |
1273 | 127k | AV1DecContext *s = avctx->priv_data; |
1274 | 127k | AV1RawTileGroup *raw_tile_group = NULL; |
1275 | 127k | int i = 0, ret; |
1276 | | |
1277 | 701k | for (i = s->nb_unit; i < s->current_obu.nb_units; i++) { |
1278 | 587k | CodedBitstreamUnit *unit = &s->current_obu.units[i]; |
1279 | 587k | AV1RawOBU *obu = unit->content; |
1280 | 587k | const AV1RawOBUHeader *header; |
1281 | | |
1282 | 587k | av_log(avctx, AV_LOG_DEBUG, "OBU idx:%d, type:%d, content available:%d.\n", i, unit->type, !!obu); |
1283 | | |
1284 | 587k | if (unit->type == AV1_OBU_TILE_LIST) { |
1285 | 1.08k | av_log(avctx, AV_LOG_ERROR, "Large scale tile decoding is unsupported.\n"); |
1286 | 1.08k | ret = AVERROR_PATCHWELCOME; |
1287 | 1.08k | goto end; |
1288 | 1.08k | } |
1289 | | |
1290 | 586k | if (!obu) |
1291 | 511k | continue; |
1292 | | |
1293 | 74.7k | header = &obu->header; |
1294 | | |
1295 | 74.7k | switch (unit->type) { |
1296 | 6.90k | case AV1_OBU_SEQUENCE_HEADER: |
1297 | 6.90k | ret = av_buffer_replace(&s->seq_data_ref, unit->data_ref); |
1298 | 6.90k | if (ret < 0) |
1299 | 0 | goto end; |
1300 | | |
1301 | 6.90k | s->seq_data_ref->data = unit->data; |
1302 | 6.90k | s->seq_data_ref->size = unit->data_size; |
1303 | 6.90k | av_refstruct_replace(&s->seq_ref, unit->content_ref); |
1304 | | |
1305 | 6.90k | s->raw_seq = &obu->obu.sequence_header; |
1306 | | |
1307 | 6.90k | ret = set_context_with_sequence(avctx, s->raw_seq); |
1308 | 6.90k | if (ret < 0) { |
1309 | 442 | av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n"); |
1310 | 442 | s->raw_seq = NULL; |
1311 | 442 | goto end; |
1312 | 442 | } |
1313 | | |
1314 | 6.45k | s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point]; |
1315 | | |
1316 | 6.45k | s->pix_fmt = AV_PIX_FMT_NONE; |
1317 | | |
1318 | 6.45k | if (FF_HW_HAS_CB(avctx, decode_params)) { |
1319 | 0 | ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER, |
1320 | 0 | s->seq_data_ref->data, s->seq_data_ref->size); |
1321 | 0 | if (ret < 0) { |
1322 | 0 | av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n"); |
1323 | 0 | return ret; |
1324 | 0 | } |
1325 | 0 | } |
1326 | | |
1327 | 6.45k | break; |
1328 | 6.45k | case AV1_OBU_REDUNDANT_FRAME_HEADER: |
1329 | 627 | if (s->raw_frame_header) |
1330 | 0 | break; |
1331 | | // fall-through |
1332 | 9.93k | case AV1_OBU_FRAME: |
1333 | 10.4k | case AV1_OBU_FRAME_HEADER: |
1334 | 10.4k | if (!s->raw_seq) { |
1335 | 2.81k | av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n"); |
1336 | 2.81k | ret = AVERROR_INVALIDDATA; |
1337 | 2.81k | goto end; |
1338 | 2.81k | } |
1339 | | |
1340 | 7.65k | av_refstruct_replace(&s->header_ref, unit->content_ref); |
1341 | | |
1342 | 7.65k | if (unit->type == AV1_OBU_FRAME) |
1343 | 7.12k | s->raw_frame_header = &obu->obu.frame.header; |
1344 | 529 | else |
1345 | 529 | s->raw_frame_header = &obu->obu.frame_header; |
1346 | | |
1347 | 7.65k | if (s->raw_frame_header->show_existing_frame) { |
1348 | 1.00k | av1_frame_replace(&s->cur_frame, |
1349 | 1.00k | &s->ref[s->raw_frame_header->frame_to_show_map_idx]); |
1350 | | |
1351 | 1.00k | update_reference_list(avctx); |
1352 | | |
1353 | 1.00k | if (s->cur_frame.f) { |
1354 | 0 | ret = set_output_frame(avctx, frame); |
1355 | 0 | if (ret < 0) { |
1356 | 0 | av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n"); |
1357 | 0 | goto end; |
1358 | 0 | } |
1359 | 0 | } |
1360 | | |
1361 | 1.00k | s->raw_frame_header = NULL; |
1362 | 1.00k | i++; |
1363 | 1.00k | ret = 0; |
1364 | | |
1365 | 1.00k | goto end; |
1366 | 1.00k | } |
1367 | | |
1368 | 6.65k | ret = get_current_frame(avctx); |
1369 | 6.65k | if (ret < 0) { |
1370 | 4.28k | av_log(avctx, AV_LOG_ERROR, "Get current frame error\n"); |
1371 | 4.28k | goto end; |
1372 | 4.28k | } |
1373 | | |
1374 | 2.37k | s->cur_frame.spatial_id = header->spatial_id; |
1375 | 2.37k | s->cur_frame.temporal_id = header->temporal_id; |
1376 | | |
1377 | 2.37k | if (avctx->hwaccel && s->cur_frame.f) { |
1378 | 0 | ret = FF_HW_CALL(avctx, start_frame, s->pkt->buf, |
1379 | 0 | unit->data, unit->data_size); |
1380 | 0 | if (ret < 0) { |
1381 | 0 | av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n"); |
1382 | 0 | goto end; |
1383 | 0 | } |
1384 | 0 | } |
1385 | 2.37k | if (unit->type != AV1_OBU_FRAME) |
1386 | 202 | break; |
1387 | | // fall-through |
1388 | 4.68k | case AV1_OBU_TILE_GROUP: |
1389 | 4.68k | if (!s->raw_frame_header) { |
1390 | 2.51k | av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n"); |
1391 | 2.51k | ret = AVERROR_INVALIDDATA; |
1392 | 2.51k | goto end; |
1393 | 2.51k | } |
1394 | | |
1395 | 2.16k | if (unit->type == AV1_OBU_FRAME) |
1396 | 2.16k | raw_tile_group = &obu->obu.frame.tile_group; |
1397 | 0 | else |
1398 | 0 | raw_tile_group = &obu->obu.tile_group; |
1399 | | |
1400 | 2.16k | ret = get_tiles_info(avctx, raw_tile_group); |
1401 | 2.16k | if (ret < 0) |
1402 | 763 | goto end; |
1403 | | |
1404 | 1.40k | if (avctx->hwaccel && s->cur_frame.f) { |
1405 | 0 | ret = FF_HW_CALL(avctx, decode_slice, raw_tile_group->tile_data.data, |
1406 | 0 | raw_tile_group->tile_data.data_size); |
1407 | 0 | if (ret < 0) { |
1408 | 0 | av_log(avctx, AV_LOG_ERROR, |
1409 | 0 | "HW accel decode slice fail.\n"); |
1410 | 0 | goto end; |
1411 | 0 | } |
1412 | 0 | } |
1413 | 1.40k | break; |
1414 | 1.40k | case AV1_OBU_TILE_LIST: |
1415 | 34.3k | case AV1_OBU_TEMPORAL_DELIMITER: |
1416 | 34.3k | case AV1_OBU_PADDING: |
1417 | 34.3k | break; |
1418 | 20.4k | case AV1_OBU_METADATA: |
1419 | 20.4k | switch (obu->obu.metadata.metadata_type) { |
1420 | 218 | case AV1_METADATA_TYPE_HDR_CLL: |
1421 | 218 | av_refstruct_replace(&s->cll_ref, unit->content_ref); |
1422 | 218 | s->cll = &obu->obu.metadata.metadata.hdr_cll; |
1423 | 218 | break; |
1424 | 194 | case AV1_METADATA_TYPE_HDR_MDCV: |
1425 | 194 | av_refstruct_replace(&s->mdcv_ref, unit->content_ref); |
1426 | 194 | s->mdcv = &obu->obu.metadata.metadata.hdr_mdcv; |
1427 | 194 | break; |
1428 | 19.5k | case AV1_METADATA_TYPE_ITUT_T35: { |
1429 | 19.5k | AV1RawMetadataITUTT35 itut_t35; |
1430 | 19.5k | memcpy(&itut_t35, &obu->obu.metadata.metadata.itut_t35, sizeof(itut_t35)); |
1431 | 19.5k | itut_t35.payload_ref = av_buffer_ref(obu->obu.metadata.metadata.itut_t35.payload_ref); |
1432 | 19.5k | if (!itut_t35.payload_ref) { |
1433 | 0 | ret = AVERROR(ENOMEM); |
1434 | 0 | goto end; |
1435 | 0 | } |
1436 | 19.5k | ret = av_fifo_write(s->itut_t35_fifo, &itut_t35, 1); |
1437 | 19.5k | if (ret < 0) { |
1438 | 0 | av_buffer_unref(&itut_t35.payload_ref); |
1439 | 0 | goto end; |
1440 | 0 | } |
1441 | 19.5k | break; |
1442 | 19.5k | } |
1443 | 19.5k | default: |
1444 | 496 | break; |
1445 | 20.4k | } |
1446 | 20.4k | break; |
1447 | 20.4k | default: |
1448 | 0 | av_log(avctx, AV_LOG_DEBUG, |
1449 | 0 | "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n", |
1450 | 0 | unit->type, unit->data_size); |
1451 | 74.7k | } |
1452 | | |
1453 | 62.8k | if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) { |
1454 | 1.16k | int show_frame = s->raw_frame_header->show_frame; |
1455 | | // Set nb_unit to point at the next OBU, to indicate which |
1456 | | // OBUs have been processed for this current frame. (If this |
1457 | | // frame gets output, we set nb_unit to this value later too.) |
1458 | 1.16k | s->nb_unit = i + 1; |
1459 | 1.16k | if (avctx->hwaccel && s->cur_frame.f) { |
1460 | 0 | ret = FF_HW_SIMPLE_CALL(avctx, end_frame); |
1461 | 0 | if (ret < 0) { |
1462 | 0 | av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n"); |
1463 | 0 | goto end; |
1464 | 0 | } |
1465 | 0 | } |
1466 | | |
1467 | 1.16k | update_reference_list(avctx); |
1468 | | |
1469 | | // Set start_unit to indicate the first OBU of the next frame. |
1470 | 1.16k | s->start_unit = s->nb_unit; |
1471 | 1.16k | raw_tile_group = NULL; |
1472 | 1.16k | s->raw_frame_header = NULL; |
1473 | | |
1474 | 1.16k | if (show_frame) { |
1475 | | // cur_frame.f needn't exist due to skip_frame. |
1476 | 586 | if (s->cur_frame.f) { |
1477 | 0 | ret = set_output_frame(avctx, frame); |
1478 | 0 | if (ret < 0) { |
1479 | 0 | av_log(avctx, AV_LOG_ERROR, "Set output frame error\n"); |
1480 | 0 | goto end; |
1481 | 0 | } |
1482 | 0 | } |
1483 | 586 | i++; |
1484 | 586 | ret = 0; |
1485 | 586 | goto end; |
1486 | 586 | } |
1487 | 1.16k | } |
1488 | 62.8k | } |
1489 | | |
1490 | 113k | ret = AVERROR(EAGAIN); |
1491 | 127k | end: |
1492 | 127k | av_assert0(i <= s->current_obu.nb_units); |
1493 | 127k | s->nb_unit = i; |
1494 | | |
1495 | 127k | if ((ret < 0 && ret != AVERROR(EAGAIN)) || s->current_obu.nb_units == i) { |
1496 | 127k | if (ret < 0) |
1497 | 125k | s->raw_frame_header = NULL; |
1498 | 127k | av_packet_unref(s->pkt); |
1499 | 127k | ff_cbs_fragment_reset(&s->current_obu); |
1500 | 127k | s->nb_unit = s->start_unit = 0; |
1501 | 127k | } |
1502 | 127k | if (!ret && !frame->buf[0]) |
1503 | 1.59k | ret = AVERROR(EAGAIN); |
1504 | | |
1505 | 127k | return ret; |
1506 | 127k | } |
1507 | | |
1508 | | static int av1_receive_frame(AVCodecContext *avctx, AVFrame *frame) |
1509 | 438k | { |
1510 | 438k | AV1DecContext *s = avctx->priv_data; |
1511 | 438k | int ret; |
1512 | | |
1513 | 554k | do { |
1514 | 554k | if (!s->current_obu.nb_units) { |
1515 | 553k | ret = ff_decode_get_packet(avctx, s->pkt); |
1516 | 553k | if (ret < 0) |
1517 | 236k | return ret; |
1518 | | |
1519 | 317k | ret = ff_cbs_read_packet(s->cbc, &s->current_obu, s->pkt); |
1520 | 317k | if (ret < 0) { |
1521 | 190k | ff_cbs_fragment_reset(&s->current_obu); |
1522 | 190k | av_packet_unref(s->pkt); |
1523 | 190k | av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n"); |
1524 | 190k | return ret; |
1525 | 190k | } |
1526 | | |
1527 | 127k | s->nb_unit = s->start_unit = 0; |
1528 | 127k | av_log(avctx, AV_LOG_DEBUG, "Total OBUs on this packet: %d.\n", |
1529 | 127k | s->current_obu.nb_units); |
1530 | 127k | } |
1531 | | |
1532 | 127k | ret = av1_receive_frame_internal(avctx, frame); |
1533 | 127k | } while (ret == AVERROR(EAGAIN)); |
1534 | | |
1535 | 11.8k | return ret; |
1536 | 438k | } |
1537 | | |
1538 | | static void av1_decode_flush(AVCodecContext *avctx) |
1539 | 58.3k | { |
1540 | 58.3k | AV1DecContext *s = avctx->priv_data; |
1541 | 58.3k | AV1RawMetadataITUTT35 itut_t35; |
1542 | | |
1543 | 524k | for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) |
1544 | 466k | av1_frame_unref(&s->ref[i]); |
1545 | | |
1546 | 58.3k | av1_frame_unref(&s->cur_frame); |
1547 | 58.3k | s->operating_point_idc = 0; |
1548 | 58.3k | s->nb_unit = s->start_unit = 0; |
1549 | 58.3k | s->raw_frame_header = NULL; |
1550 | 58.3k | s->raw_seq = NULL; |
1551 | 58.3k | s->cll = NULL; |
1552 | 58.3k | s->mdcv = NULL; |
1553 | 61.1k | while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) |
1554 | 2.80k | av_buffer_unref(&itut_t35.payload_ref); |
1555 | | |
1556 | 58.3k | ff_cbs_fragment_reset(&s->current_obu); |
1557 | 58.3k | ff_cbs_flush(s->cbc); |
1558 | | |
1559 | 58.3k | if (FF_HW_HAS_CB(avctx, flush)) |
1560 | 0 | FF_HW_SIMPLE_CALL(avctx, flush); |
1561 | 58.3k | } |
1562 | | |
1563 | | #define OFFSET(x) offsetof(AV1DecContext, x) |
1564 | | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
1565 | | static const AVOption av1_options[] = { |
1566 | | { "operating_point", "Select an operating point of the scalable bitstream", |
1567 | | OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD }, |
1568 | | { NULL } |
1569 | | }; |
1570 | | |
1571 | | static const AVClass av1_class = { |
1572 | | .class_name = "AV1 decoder", |
1573 | | .item_name = av_default_item_name, |
1574 | | .option = av1_options, |
1575 | | .version = LIBAVUTIL_VERSION_INT, |
1576 | | }; |
1577 | | |
1578 | | const FFCodec ff_av1_decoder = { |
1579 | | .p.name = "av1", |
1580 | | CODEC_LONG_NAME("Alliance for Open Media AV1"), |
1581 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1582 | | .p.id = AV_CODEC_ID_AV1, |
1583 | | .priv_data_size = sizeof(AV1DecContext), |
1584 | | .init = av1_decode_init, |
1585 | | .close = av1_decode_free, |
1586 | | FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame), |
1587 | | .p.capabilities = AV_CODEC_CAP_DR1, |
1588 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
1589 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | |
1590 | | FF_CODEC_CAP_USES_PROGRESSFRAMES, |
1591 | | .flush = av1_decode_flush, |
1592 | | .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles), |
1593 | | .p.priv_class = &av1_class, |
1594 | | .hw_configs = (const AVCodecHWConfigInternal *const []) { |
1595 | | #if CONFIG_AV1_DXVA2_HWACCEL |
1596 | | HWACCEL_DXVA2(av1), |
1597 | | #endif |
1598 | | #if CONFIG_AV1_D3D11VA_HWACCEL |
1599 | | HWACCEL_D3D11VA(av1), |
1600 | | #endif |
1601 | | #if CONFIG_AV1_D3D11VA2_HWACCEL |
1602 | | HWACCEL_D3D11VA2(av1), |
1603 | | #endif |
1604 | | #if CONFIG_AV1_D3D12VA_HWACCEL |
1605 | | HWACCEL_D3D12VA(av1), |
1606 | | #endif |
1607 | | #if CONFIG_AV1_NVDEC_HWACCEL |
1608 | | HWACCEL_NVDEC(av1), |
1609 | | #endif |
1610 | | #if CONFIG_AV1_VAAPI_HWACCEL |
1611 | | HWACCEL_VAAPI(av1), |
1612 | | #endif |
1613 | | #if CONFIG_AV1_VDPAU_HWACCEL |
1614 | | HWACCEL_VDPAU(av1), |
1615 | | #endif |
1616 | | #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL |
1617 | | HWACCEL_VIDEOTOOLBOX(av1), |
1618 | | #endif |
1619 | | #if CONFIG_AV1_VULKAN_HWACCEL |
1620 | | HWACCEL_VULKAN(av1), |
1621 | | #endif |
1622 | | |
1623 | | NULL |
1624 | | }, |
1625 | | }; |