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
43
44 /**< same with Div_Lut defined in spec 7.11.3.7 */
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
73 {
75 return v;
76 else if (v & 1)
77 return r - ((v + 1) >> 1);
78 else
80 }
81
84 {
87 } else {
89 }
90 }
91
94 {
96 return x + low;
97 }
98
100 {
101 uint8_t primary_frame, prev_frame;
102 uint32_t abs_bits, prec_bits,
round, prec_diff, sub,
mx;
104
105 primary_frame =
s->raw_frame_header->primary_ref_frame;
106 prev_frame =
s->raw_frame_header->ref_frame_idx[primary_frame];
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 */
115 prev_gm_param =
s->cur_frame.gm_params[
ref][idx];
116 else
117 prev_gm_param =
s->ref[prev_frame].gm_params[
ref][idx];
118
119 if (idx < 2) {
122 !
s->raw_frame_header->allow_high_precision_mv;
124 !
s->raw_frame_header->allow_high_precision_mv;
125 } else {
128 }
129 }
132 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
134 r = (prev_gm_param >> prec_diff) - sub;
135
136 s->cur_frame.gm_params[
ref][idx] =
139 }
140
142 {
143 if (n == 0)
144 return x;
145 return ((x + ((uint64_t)1 << (n - 1))) >> n);
146 }
147
149 {
151 }
152
153 /**
154 * Resolve divisor process.
155 * see spec 7.11.3.7
156 */
158 {
160
162 e = d - (1 << (*shift));
165 else
167
169
171 }
172
173 /**
174 * check if global motion params is valid.
175 * see spec 7.11.3.6
176 */
178 {
179 int16_t
alpha, beta, gamma,
delta, divf, divs;
181 int32_t *param = &
s->cur_frame.gm_params[idx][0];
182 if (param[2] <= 0)
183 return 0;
184
192
197
200 return 0;
201
202 return 1;
203 }
204
205 /**
206 * update gm type/params, since cbs already implemented part of this function,
207 * so we don't need to full implement spec.
208 */
210 {
213
216 for (
int i = 0;
i < 6;
i++)
217 s->cur_frame.gm_params[
ref][
i] = (
i % 3 == 2) ?
219 }
222 return;
223
228 } else {
231 }
232 } else {
234 }
236
243 } else {
244 s->cur_frame.gm_params[
ref][4] = -
s->cur_frame.gm_params[
ref][3];
245 s->cur_frame.gm_params[
ref][5] =
s->cur_frame.gm_params[
ref][2];
246 }
247 }
251 }
254 }
255 }
256 }
257
259 unsigned int a,
unsigned int b)
260 {
261 unsigned int diff =
a -
b;
263 return (
diff & (m - 1)) - (
diff & m);
264 }
265
267 {
270
271 int forward_idx, backward_idx;
272 int forward_hint, backward_hint;
273 int second_forward_idx, second_forward_hint;
274 int ref_hint, dist,
i;
275
279 return;
280
281 forward_idx = -1;
282 backward_idx = -1;
284 ref_hint =
s->ref[
header->ref_frame_idx[
i]].raw_frame_header->order_hint;
286 if (dist < 0) {
287 if (forward_idx < 0 ||
290 forward_hint = ref_hint;
291 }
292 } else if (dist > 0) {
293 if (backward_idx < 0 ||
296 backward_hint = ref_hint;
297 }
298 }
299 }
300
301 if (forward_idx < 0) {
302 return;
303 } else if (backward_idx >= 0) {
304 s->cur_frame.skip_mode_frame_idx[0] =
306 s->cur_frame.skip_mode_frame_idx[1] =
308 return;
309 }
310
311 second_forward_idx = -1;
313 ref_hint =
s->ref[
header->ref_frame_idx[
i]].raw_frame_header->order_hint;
315 if (second_forward_idx < 0 ||
317 second_forward_idx =
i;
318 second_forward_hint = ref_hint;
319 }
320 }
321 }
322
323 if (second_forward_idx < 0)
324 return;
325
326 s->cur_frame.skip_mode_frame_idx[0] =
328 s->cur_frame.skip_mode_frame_idx[1] =
330 }
331
333 {
336
340 s->cur_frame.coded_lossless = 0;
341 return;
342 }
343
344 s->cur_frame.coded_lossless = 1;
346 int qindex;
348 qindex = (
header->base_q_idx +
350 } else {
351 qindex =
header->base_q_idx;
352 }
354
355 if (qindex) {
356 s->cur_frame.coded_lossless = 0;
357 return;
358 }
359 }
360 }
361
363 {
367
369
372 int ref_slot =
header->ref_frame_idx[
i];
373 int ref_order_hint =
s->ref[ref_slot].order_hint;
374
375 frame->order_hints[ref_name] = ref_order_hint;
377 frame->ref_frame_sign_bias[ref_name] = 0;
378 } else {
379 frame->ref_frame_sign_bias[ref_name] =
381 frame->order_hint) > 0;
382 }
383 }
384 }
385
387 {
391
393 return;
394
396 memcpy(
dst, film_grain,
sizeof(*
dst));
397 return;
398 }
399
401
404 }
405
407
408 {
409 int cur_tile_num =
410 s->raw_frame_header->tile_cols *
s->raw_frame_header->tile_rows;
411 if (
s->tile_num < cur_tile_num) {
417 }
418 }
419 s->tile_num = cur_tile_num;
420
421 return 0;
422 }
423
425 {
428 uint16_t tile_num, tile_row, tile_col;
429 uint32_t
size = 0, size_bytes = 0;
430
434 s->tg_end = tile_group->
tg_end;
435
436 for (tile_num = tile_group->
tg_start; tile_num <= tile_group->tg_end; tile_num++) {
437 tile_row = tile_num /
s->raw_frame_header->tile_cols;
438 tile_col = tile_num %
s->raw_frame_header->tile_cols;
439
440 if (tile_num == tile_group->
tg_end) {
443 s->tile_group_info[tile_num].tile_row = tile_row;
444 s->tile_group_info[tile_num].tile_column = tile_col;
445 return 0;
446 }
447 size_bytes =
s->raw_frame_header->tile_size_bytes_minus1 + 1;
451 for (
int i = 0;
i < size_bytes;
i++)
452 size |= bytestream2_get_byteu(&gb) << 8 *
i;
456
457 s->tile_group_info[tile_num].tile_size =
size;
459 s->tile_group_info[tile_num].tile_row = tile_row;
460 s->tile_group_info[tile_num].tile_column = tile_col;
461
463 }
464
465 return 0;
466
467 }
468
471 {
474
479 else {
483 }
484
486 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
495 else
505 else
515 else
517 }
518 } else {
525 else
527 }
528
530 }
531
533 {
538 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
539 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
540 CONFIG_AV1_D3D12VA_HWACCEL + \
541 CONFIG_AV1_NVDEC_HWACCEL + \
542 CONFIG_AV1_VAAPI_HWACCEL + \
543 CONFIG_AV1_VDPAU_HWACCEL + \
544 CONFIG_AV1_VULKAN_HWACCEL)
546
548 return -1;
549
552 #if CONFIG_AV1_DXVA2_HWACCEL
554 #endif
555 #if CONFIG_AV1_D3D11VA_HWACCEL
558 #endif
559 #if CONFIG_AV1_D3D12VA_HWACCEL
561 #endif
562 #if CONFIG_AV1_NVDEC_HWACCEL
564 #endif
565 #if CONFIG_AV1_VAAPI_HWACCEL
567 #endif
568 #if CONFIG_AV1_VDPAU_HWACCEL
570 #endif
571 #if CONFIG_AV1_VULKAN_HWACCEL
573 #endif
574 break;
576 #if CONFIG_AV1_DXVA2_HWACCEL
578 #endif
579 #if CONFIG_AV1_D3D11VA_HWACCEL
582 #endif
583 #if CONFIG_AV1_D3D12VA_HWACCEL
585 #endif
586 #if CONFIG_AV1_NVDEC_HWACCEL
588 #endif
589 #if CONFIG_AV1_VAAPI_HWACCEL
591 #endif
592 #if CONFIG_AV1_VDPAU_HWACCEL
594 #endif
595 #if CONFIG_AV1_VULKAN_HWACCEL
597 #endif
598 break;
600 #if CONFIG_AV1_VULKAN_HWACCEL
602 #endif
603 break;
605 #if CONFIG_AV1_VULKAN_HWACCEL
607 #endif
608 break;
610 #if CONFIG_AV1_VULKAN_HWACCEL
612 #endif
613 break;
615 #if CONFIG_AV1_VULKAN_HWACCEL
617 #endif
618 break;
620 #if CONFIG_AV1_VULKAN_HWACCEL
622 #endif
623 break;
625 #if CONFIG_AV1_VULKAN_HWACCEL
627 #endif
628 break;
630 #if CONFIG_AV1_VULKAN_HWACCEL
632 #endif
633 break;
635 #if CONFIG_AV1_NVDEC_HWACCEL
637 #endif
638 break;
640 #if CONFIG_AV1_NVDEC_HWACCEL
642 #endif
643 break;
644 }
645
648
652 return 1;
653 }
654
656
657 /**
658 * check if the HW accel is inited correctly. If not, return un-implemented.
659 * Since now the av1 decoder doesn't support native decode, if it will be
660 * implemented in the future, need remove this check.
661 */
664 " hardware accelerated AV1 decoding.\n");
667 }
668
671
674
675 return 0;
676 }
677
679 {
683 f->raw_frame_header =
NULL;
684 f->spatial_id =
f->temporal_id = 0;
685 memset(
f->skip_mode_frame_idx, 0,
686 2 * sizeof(uint8_t));
687 memset(&
f->film_grain, 0,
sizeof(
f->film_grain));
688 f->coded_lossless = 0;
689 }
690
692 {
694
696
697 dst->raw_frame_header =
src->raw_frame_header;
698
700
702 src->hwaccel_picture_private);
703
704 dst->spatial_id =
src->spatial_id;
705 dst->temporal_id =
src->temporal_id;
706 memcpy(
dst->gm_invalid,
712 memcpy(
dst->gm_params,
715 memcpy(
dst->skip_mode_frame_idx,
716 src->skip_mode_frame_idx,
717 2 * sizeof(uint8_t));
718 memcpy(&
dst->film_grain,
720 sizeof(
dst->film_grain));
721 dst->coded_lossless =
src->coded_lossless;
722
723 dst->order_hint =
src->order_hint;
724 memcpy(
dst->ref_frame_sign_bias,
src->ref_frame_sign_bias,
725 sizeof(
dst->ref_frame_sign_bias));
726 memcpy(
dst->order_hints,
src->order_hints,
727 sizeof(
dst->order_hints));
728
729 dst->force_integer_mv =
src->force_integer_mv;
730 }
731
733 {
736
746
747 while (
s->itut_t35_fifo &&
av_fifo_read(
s->itut_t35_fifo, &itut_t35, 1) >= 0)
750
754
755 return 0;
756 }
757
760 {
763
766
772
776 break;
779 break;
780 }
781
784 else
786
791 }
792
797
800
801 return 0;
802 }
803
806 {
810 int r_width =
header->render_width_minus_1 + 1;
811 int r_height =
header->render_height_minus_1 + 1;
813
818 }
819
823 INT_MAX);
824
829 }
830
831 return 0;
832 }
833
842 };
843
845 {
850
854
858
861
864 if (!
s->itut_t35_fifo)
866
867 av_opt_set_int(
s->cbc->priv_data,
"operating_point",
s->operating_point, 0);
868
872 avctx);
875 goto end;
876 }
877
879 if (!seq) {
881 goto end;
882 }
883
887 goto end;
888 }
889
890 end:
892 }
893
894 s->dovi.logctx = avctx;
895 s->dovi.cfg.dv_profile = 10;
// default for AV1
897 if (sd && sd->
size >=
sizeof(
s->dovi.cfg))
899
901 }
902
904 {
909
914 }
915
919
923 else
925
926 switch (
header->frame_type) {
930 break;
933 break;
936 break;
937 }
938
942
943 return 0;
944
948 }
949
952 {
955 int ret, provider_code;
956
958
959 provider_code = bytestream2_get_be16(&gb);
960 switch (provider_code) {
962 uint32_t user_identifier = bytestream2_get_be32(&gb);
963 switch (user_identifier) {
964 case MKBETAG(
'G',
'A',
'9',
'4'): {
// closed captions
966
971 break;
972
976
978 break;
979 }
980 default: // ignore unsupported identifiers
981 break;
982 }
983 break;
984 }
987 int provider_oriented_code = bytestream2_get_be16(&gb);
988 int application_identifier = bytestream2_get_byte(&gb);
989
991 provider_oriented_code != 1 || application_identifier != 4)
992 break;
993
995 if (!hdrplus)
997
1002 break;
1003 }
1005 int provider_oriented_code = bytestream2_get_be32(&gb);
1007 provider_oriented_code != 0x800)
1008 break;
1009
1014 break; // ignore
1015 }
1016
1020 break;
1021 }
1022 default: // ignore unsupported provider codes
1023 break;
1024 }
1025
1026 return 0;
1027 }
1028
1030 {
1034
1037
1041
1042 if (mastering) {
1043 for (
int i = 0;
i < 3;
i++) {
1046 }
1049
1052
1055 }
1056 }
1057
1060
1064
1065 if (light) {
1066 light->
MaxCLL =
s->cll->max_cll;
1068 }
1069 }
1070
1075 }
1076
1078 }
1079
1081 {
1087
1090 return 0;
1091
1093 if (!fgp)
1095
1106
1115
1120 }
1125 }
1130 }
1131
1132 for (
int i = 0;
i < 24;
i++) {
1134 }
1135 for (
int i = 0;
i < 25;
i++) {
1138 }
1139
1146
1147 return 0;
1148 }
1149
1151 {
1153 const AVFrame *srcframe =
s->cur_frame.f;
1156
1157 // TODO: all layers
1158 if (
s->operating_point_idc &&
1159 av_log2(
s->operating_point_idc >> 8) >
s->cur_frame.spatial_id)
1160 return 0;
1161
1165
1170 }
1171
1177 }
1178 }
1179
1182 #if FF_API_FRAME_PKT
1187 #endif
1188
1190
1191 return 0;
1192 }
1193
1195 {
1198
1200 if (
header->refresh_frame_flags & (1 <<
i))
1202 }
1203 }
1204
1206 {
1209
1211
1213
1214 s->cur_frame.raw_frame_header =
s->raw_frame_header;
1215
1220 }
1221
1228 return 0;
1229
1235 }
1236
1239 s->seq_data_ref->data,
s->seq_data_ref->size);
1243 }
1244 }
1245 }
1246
1250 "Failed to allocate space for current frame.\n");
1252 }
1253
1259
1260 s->cur_frame.force_integer_mv =
1261 s->raw_frame_header->force_integer_mv ||
1264
1266 }
1267
1269 {
1273
1274 for (
i =
s->nb_unit; i < s->current_obu.nb_units;
i++) {
1278
1280
1284 goto end;
1285 }
1286
1287 if (!obu)
1288 continue;
1289
1291
1292 switch (unit->
type) {
1296 goto end;
1297
1298 s->seq_data_ref->data = unit->
data;
1301
1303
1308 goto end;
1309 }
1310
1311 s->operating_point_idc =
s->raw_seq->operating_point_idc[
s->operating_point];
1312
1314
1315 break;
1317 if (
s->raw_frame_header)
1318 break;
1319 // fall-through
1325 goto end;
1326 }
1327
1329
1332 else
1334
1335 if (
s->raw_frame_header->show_existing_frame) {
1337 &
s->ref[
s->raw_frame_header->frame_to_show_map_idx]);
1338
1340
1341 if (
s->cur_frame.f) {
1345 goto end;
1346 }
1347 }
1348
1349 s->raw_frame_header =
NULL;
1352
1353 goto end;
1354 }
1355
1359 goto end;
1360 }
1361
1362 s->cur_frame.spatial_id =
header->spatial_id;
1363 s->cur_frame.temporal_id =
header->temporal_id;
1364
1365 if (avctx->
hwaccel &&
s->cur_frame.f) {
1369 goto end;
1370 }
1371 }
1373 break;
1374 // fall-through
1376 if (!
s->raw_frame_header) {
1379 goto end;
1380 }
1381
1384 else
1386
1389 goto end;
1390
1391 if (avctx->
hwaccel &&
s->cur_frame.f) {
1396 "HW accel decode slice fail.\n");
1397 goto end;
1398 }
1399 }
1400 break;
1404 break;
1410 break;
1414 break;
1421 goto end;
1422 }
1426 goto end;
1427 }
1428 break;
1429 }
1430 default:
1431 break;
1432 }
1433 break;
1434 default:
1438 }
1439
1440 if (raw_tile_group && (
s->tile_num == raw_tile_group->
tg_end + 1)) {
1442 if (avctx->
hwaccel &&
s->cur_frame.f) {
1446 goto end;
1447 }
1448 }
1449
1451
1452 raw_tile_group =
NULL;
1453 s->raw_frame_header =
NULL;
1454
1456 // cur_frame.f needn't exist due to skip_frame.
1457 if (
s->cur_frame.f) {
1461 goto end;
1462 }
1463 }
1466 goto end;
1467 }
1468 }
1469 }
1470
1472 end:
1475
1476 if ((
ret < 0 &&
ret !=
AVERROR(EAGAIN)) ||
s->current_obu.nb_units ==
i) {
1478 s->raw_frame_header =
NULL;
1482 }
1485
1487 }
1488
1490 {
1493
1494 do {
1495 if (!
s->current_obu.nb_units) {
1499
1506 }
1507
1510 s->current_obu.nb_units);
1511 }
1512
1515
1517 }
1518
1520 {
1523
1526
1528 s->operating_point_idc = 0;
1530 s->raw_frame_header =
NULL;
1536
1539
1542 }
1543
1544 #define OFFSET(x) offsetof(AV1DecContext, x)
1545 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1547 { "operating_point", "Select an operating point of the scalable bitstream",
1550 };
1551
1557 };
1558
1576 #if CONFIG_AV1_DXVA2_HWACCEL
1578 #endif
1579 #if CONFIG_AV1_D3D11VA_HWACCEL
1581 #endif
1582 #if CONFIG_AV1_D3D11VA2_HWACCEL
1584 #endif
1585 #if CONFIG_AV1_D3D12VA_HWACCEL
1587 #endif
1588 #if CONFIG_AV1_NVDEC_HWACCEL
1590 #endif
1591 #if CONFIG_AV1_VAAPI_HWACCEL
1593 #endif
1594 #if CONFIG_AV1_VDPAU_HWACCEL
1596 #endif
1597 #if CONFIG_AV1_VULKAN_HWACCEL
1599 #endif
1600
1602 },
1603 };