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