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
30
31 /**< same with Div_Lut defined in spec 7.11.3.7 */
33 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
34 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
35 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
36 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
37 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
38 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
39 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
40 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
41 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
42 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
43 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
44 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
45 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
46 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
47 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
48 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
49 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
50 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
51 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
52 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
53 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
54 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
55 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
56 8240, 8224, 8208, 8192
57 };
58
60 {
62 return v;
63 else if (v & 1)
64 return r - ((v + 1) >> 1);
65 else
67 }
68
71 {
74 } else {
76 }
77 }
78
81 {
83 return x + low;
84 }
85
87 {
88 uint8_t primary_frame, prev_frame;
89 uint32_t abs_bits, prec_bits,
round, prec_diff,
sub, mx;
91
92 primary_frame =
s->raw_frame_header->primary_ref_frame;
93 prev_frame =
s->raw_frame_header->ref_frame_idx[primary_frame];
96
97 /* setup_past_independence() sets PrevGmParams to default values. We can
98 * simply point to the current's frame gm_params as they will be initialized
99 * with defaults at this point.
100 */
102 prev_gm_param =
s->cur_frame.gm_params[
ref][idx];
103 else
104 prev_gm_param =
s->ref[prev_frame].gm_params[
ref][idx];
105
106 if (idx < 2) {
109 !
s->raw_frame_header->allow_high_precision_mv;
111 !
s->raw_frame_header->allow_high_precision_mv;
112 } else {
115 }
116 }
119 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
120 mx = 1 << abs_bits;
121 r = (prev_gm_param >> prec_diff) -
sub;
122
123 s->cur_frame.gm_params[
ref][idx] =
125 -mx, mx + 1,
r) << prec_diff) +
round;
126 }
127
129 {
130 if (n == 0)
131 return x;
132 return ((x + ((uint64_t)1 << (n - 1))) >> n);
133 }
134
136 {
138 }
139
140 /**
141 * Resolve divisor process.
142 * see spec 7.11.3.7
143 */
145 {
147
149 e =
d - (1 << (*shift));
152 else
154
156
158 }
159
160 /**
161 * check if global motion params is valid.
162 * see spec 7.11.3.6
163 */
165 {
166 int16_t
alpha, beta, gamma,
delta, divf, divs;
168 int32_t *param = &
s->cur_frame.gm_params[idx][0];
169 if (param[2] < 0)
170 return 0;
171
176 w = (int64_t)param[3] * param[4];
179
184
187 return 0;
188
189 return 1;
190 }
191
192 /**
193 * update gm type/params, since cbs already implemented part of this funcation,
194 * so we don't need to full implement spec.
195 */
197 {
200
203 for (
int i = 0;
i < 6;
i++)
204 s->cur_frame.gm_params[
ref][
i] = (
i % 3 == 2) ?
206 }
209 return;
210
215 } else {
218 }
219 } else {
221 }
223
230 } else {
231 s->cur_frame.gm_params[
ref][4] = -
s->cur_frame.gm_params[
ref][3];
232 s->cur_frame.gm_params[
ref][5] =
s->cur_frame.gm_params[
ref][2];
233 }
234 }
238 }
241 }
242 }
243 }
244
246 unsigned int a,
unsigned int b)
247 {
248 unsigned int diff =
a -
b;
250 return (
diff & (m - 1)) - (
diff & m);
251 }
252
254 {
257
258 int forward_idx, backward_idx;
259 int forward_hint, backward_hint;
260 int second_forward_idx, second_forward_hint;
261 int ref_hint, dist,
i;
262
263 if (!
header->skip_mode_present)
264 return;
265
266 forward_idx = -1;
267 backward_idx = -1;
269 ref_hint =
s->ref[
header->ref_frame_idx[
i]].raw_frame_header->order_hint;
271 if (dist < 0) {
272 if (forward_idx < 0 ||
275 forward_hint = ref_hint;
276 }
277 } else if (dist > 0) {
278 if (backward_idx < 0 ||
281 backward_hint = ref_hint;
282 }
283 }
284 }
285
286 if (forward_idx < 0) {
287 return;
288 } else if (backward_idx >= 0) {
289 s->cur_frame.skip_mode_frame_idx[0] =
291 s->cur_frame.skip_mode_frame_idx[1] =
293 return;
294 }
295
296 second_forward_idx = -1;
298 ref_hint =
s->ref[
header->ref_frame_idx[
i]].raw_frame_header->order_hint;
300 if (second_forward_idx < 0 ||
302 second_forward_idx =
i;
303 second_forward_hint = ref_hint;
304 }
305 }
306 }
307
308 if (second_forward_idx < 0)
309 return;
310
311 s->cur_frame.skip_mode_frame_idx[0] =
313 s->cur_frame.skip_mode_frame_idx[1] =
315 }
316
318 {
321
325 s->cur_frame.coded_lossless = 0;
326 return;
327 }
328
329 s->cur_frame.coded_lossless = 1;
331 int qindex;
333 qindex = (
header->base_q_idx +
335 } else {
336 qindex =
header->base_q_idx;
337 }
339
340 if (qindex) {
341 s->cur_frame.coded_lossless = 0;
342 return;
343 }
344 }
345 }
346
348 {
352
354 return;
355
357 memcpy(dst, film_grain, sizeof(*dst));
358 return;
359 }
360
362
363 memcpy(dst,
src,
sizeof(*dst));
365 }
366
368
369 {
370 int cur_tile_num =
371 s->raw_frame_header->tile_cols *
s->raw_frame_header->tile_rows;
372 if (
s->tile_num < cur_tile_num) {
378 }
379 }
380 s->tile_num = cur_tile_num;
381
382 return 0;
383 }
384
386 {
389 uint16_t tile_num, tile_row, tile_col;
390 uint32_t
size = 0, size_bytes = 0;
391
395 s->tg_end = tile_group->
tg_end;
396
397 for (tile_num = tile_group->
tg_start; tile_num <= tile_group->tg_end; tile_num++) {
398 tile_row = tile_num /
s->raw_frame_header->tile_cols;
399 tile_col = tile_num %
s->raw_frame_header->tile_cols;
400
401 if (tile_num == tile_group->
tg_end) {
404 s->tile_group_info[tile_num].tile_row = tile_row;
405 s->tile_group_info[tile_num].tile_column = tile_col;
406 return 0;
407 }
408 size_bytes =
s->raw_frame_header->tile_size_bytes_minus1 + 1;
412 for (
int i = 0;
i < size_bytes;
i++)
413 size |= bytestream2_get_byteu(&gb) << 8 *
i;
417
418 s->tile_group_info[tile_num].tile_size =
size;
420 s->tile_group_info[tile_num].tile_row = tile_row;
421 s->tile_group_info[tile_num].tile_column = tile_col;
422
424 }
425
426 return 0;
427
428 }
429
431 {
437 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
438 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
439 CONFIG_AV1_NVDEC_HWACCEL + \
440 CONFIG_AV1_VAAPI_HWACCEL)
442
447 else {
450 return -1;
451 }
452
454 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
463 else
473 else
483 else
485 }
486 } else {
493 else
495 }
496
499
501 return -1;
502
505 #if CONFIG_AV1_DXVA2_HWACCEL
507 #endif
508 #if CONFIG_AV1_D3D11VA_HWACCEL
511 #endif
512 #if CONFIG_AV1_NVDEC_HWACCEL
514 #endif
515 #if CONFIG_AV1_VAAPI_HWACCEL
517 #endif
518 break;
520 #if CONFIG_AV1_DXVA2_HWACCEL
522 #endif
523 #if CONFIG_AV1_D3D11VA_HWACCEL
526 #endif
527 #if CONFIG_AV1_NVDEC_HWACCEL
529 #endif
530 #if CONFIG_AV1_VAAPI_HWACCEL
532 #endif
533 break;
535 #if CONFIG_AV1_NVDEC_HWACCEL
537 #endif
538 break;
540 #if CONFIG_AV1_NVDEC_HWACCEL
542 #endif
543 break;
544 }
545
548
552
553 /**
554 * check if the HW accel is inited correctly. If not, return un-implemented.
555 * Since now the av1 decoder doesn't support native decode, if it will be
556 * implemented in the future, need remove this check.
557 */
560 " hardware accelerated AV1 decoding.\n");
562 }
563
566
567 return 0;
568 }
569
571 {
574 f->hwaccel_picture_private =
NULL;
576 f->raw_frame_header =
NULL;
577 f->spatial_id =
f->temporal_id = 0;
578 memset(
f->skip_mode_frame_idx, 0,
579 2 * sizeof(uint8_t));
580 memset(&
f->film_grain, 0,
sizeof(
f->film_grain));
581 f->coded_lossless = 0;
582 }
583
585 {
587
591
595
597
598 if (
src->hwaccel_picture_private) {
603 }
604
617 src->skip_mode_frame_idx,
618 2 * sizeof(uint8_t));
623
624 return 0;
625
629 }
630
632 {
634
638 }
641
645
648
649 return 0;
650 }
651
654 {
657
660
666
670 break;
673 break;
674 }
675
678 else
680
685 }
687
693 INT_MAX);
696 }
697
698 return 0;
699 }
700
703 {
707 int r_width =
header->render_width_minus_1 + 1;
708 int r_height =
header->render_height_minus_1 + 1;
710
715 }
716
718 (int64_t)
height * r_width,
719 (int64_t)
width * r_height,
720 INT_MAX);
721
726 }
727
728 return 0;
729 }
730
732 {
736
739
742 if (!
s->ref[
i].tf.f) {
744 "Failed to allocate reference frame buffer %d.\n",
i);
746 }
747 }
748
750 if (!
s->cur_frame.tf.f) {
752 "Failed to allocate current frame buffer.\n");
754 }
755
759
760 av_opt_set_int(
s->cbc->priv_data,
"operating_point",
s->operating_point, 0);
761
765 avctx);
769 }
770
772 if (!seq) {
774 goto end;
775 }
776
780 goto end;
781 }
782
783 end:
785 }
786
788 }
789
791 {
796
801 }
802
806
807 f->raw_frame_header =
s->raw_frame_header;
808
811
814
815 switch (
header->frame_type) {
819 break;
822 break;
825 break;
826 }
827
831 f->hwaccel_priv_buf =
833 if (!
f->hwaccel_priv_buf) {
836 }
837 f->hwaccel_picture_private =
f->hwaccel_priv_buf->data;
838 }
839 }
840 return 0;
841
845 }
846
848 {
853
855 return 0;
856
858 if (!fgp)
860
863
872
877 }
882 }
887 }
888
889 for (
int i = 0;
i < 24;
i++) {
891 }
892 for (
int i = 0;
i < 25;
i++) {
895 }
896
903
904 return 0;
905 }
906
909 {
911 const AVFrame *srcframe =
s->cur_frame.tf.f;
913
914 // TODO: all layers
915 if (
s->operating_point_idc &&
916 av_log2(
s->operating_point_idc >> 8) >
s->cur_frame.spatial_id)
917 return 0;
918
922
928 }
929 }
930
934
935 *got_frame = 1;
936
937 return 0;
938 }
939
941 {
945
947 if (
header->refresh_frame_flags & (1 <<
i)) {
948 if (
s->ref[
i].tf.f->buf[0])
952 "Failed to update frame %d in reference list\n",
i);
954 }
955 }
956 }
957 return 0;
958 }
959
961 {
964
965 if (
s->cur_frame.tf.f->buf[0])
967
971 "Failed to allocate space for current frame.\n");
973 }
974
979 }
980
985
987 }
988
991 {
995
999 goto end;
1000 }
1002 s->current_obu.nb_units);
1003
1004 for (
int i = 0;
i <
s->current_obu.nb_units;
i++) {
1008
1009 if (!obu)
1010 continue;
1011
1014
1015 switch (unit->
type) {
1021 goto end;
1022 }
1023
1025
1030 goto end;
1031 }
1032
1033 s->operating_point_idc =
s->raw_seq->operating_point_idc[
s->operating_point];
1034
1039 "Failed to get pixel format.\n");
1041 goto end;
1042 }
1043 }
1044
1051 goto end;
1052 }
1053 }
1054 break;
1056 if (
s->raw_frame_header)
1057 break;
1058 // fall-through
1064 goto end;
1065 }
1066
1069 if (!
s->header_ref) {
1071 goto end;
1072 }
1073
1076 else
1078
1079 if (
s->raw_frame_header->show_existing_frame) {
1080 if (
s->cur_frame.tf.f->buf[0])
1082
1084 &
s->ref[
s->raw_frame_header->frame_to_show_map_idx]);
1087 goto end;
1088 }
1089
1093 goto end;
1094 }
1095
1099
1100 s->raw_frame_header =
NULL;
1101
1102 goto end;
1103 }
1104
1108 goto end;
1109 }
1110
1111 s->cur_frame.spatial_id =
header->spatial_id;
1112 s->cur_frame.temporal_id =
header->temporal_id;
1113
1119 goto end;
1120 }
1121 }
1123 break;
1124 // fall-through
1126 if (!
s->raw_frame_header) {
1129 goto end;
1130 }
1131
1134 else
1136
1139 goto end;
1140
1147 "HW accel decode slice fail.\n");
1148 goto end;
1149 }
1150 }
1151 break;
1156 break;
1157 default:
1161 }
1162
1163 if (raw_tile_group && (
s->tile_num == raw_tile_group->
tg_end + 1)) {
1168 goto end;
1169 }
1170 }
1171
1175 goto end;
1176 }
1177
1178 if (
s->raw_frame_header->show_frame) {
1182 goto end;
1183 }
1184 }
1185 raw_tile_group =
NULL;
1186 s->raw_frame_header =
NULL;
1187 }
1188 }
1189
1190 end:
1193 s->raw_frame_header =
NULL;
1195 }
1196
1198 {
1200
1203
1205 s->operating_point_idc = 0;
1206 s->raw_frame_header =
NULL;
1208
1210 }
1211
1212 #define OFFSET(x) offsetof(AV1DecContext, x)
1213 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1215 { "operating_point", "Select an operating point of the scalable bitstream",
1218 };
1219
1225 };
1226
1243 .bsfs = "av1_frame_split",
1245 #if CONFIG_AV1_DXVA2_HWACCEL
1247 #endif
1248 #if CONFIG_AV1_D3D11VA_HWACCEL
1250 #endif
1251 #if CONFIG_AV1_D3D11VA2_HWACCEL
1253 #endif
1254 #if CONFIG_AV1_NVDEC_HWACCEL
1256 #endif
1257 #if CONFIG_AV1_VAAPI_HWACCEL
1259 #endif
1261 },
1262 };