-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathffmpeg-HandBrake.patch
1937 lines (1817 loc) · 77.8 KB
/
ffmpeg-HandBrake.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -Naur ffmpeg-7.1.old/configure ffmpeg-7.1/configure
--- ffmpeg-7.1.old/configure 2024-12-03 14:07:23.586059283 +0100
+++ ffmpeg-7.1/configure 2024-12-03 14:07:23.774060411 +0100
@@ -3338,6 +3338,7 @@
av1_mediacodec_decoder_deps="mediacodec"
av1_mediacodec_encoder_deps="mediacodec"
av1_mediacodec_encoder_select="extract_extradata_bsf"
+av1_mf_encoder_deps="mediafoundation"
av1_nvenc_encoder_deps="nvenc NV_ENC_PIC_PARAMS_AV1"
av1_nvenc_encoder_select="atsc_a53"
av1_qsv_decoder_select="qsvdec"
diff -Naur ffmpeg-7.1.old/libavcodec/allcodecs.c ffmpeg-7.1/libavcodec/allcodecs.c
--- ffmpeg-7.1.old/libavcodec/allcodecs.c 2024-12-03 14:07:23.587059289 +0100
+++ ffmpeg-7.1/libavcodec/allcodecs.c 2024-12-03 14:07:23.774060411 +0100
@@ -839,6 +839,7 @@
extern const FFCodec ff_av1_qsv_decoder;
extern const FFCodec ff_av1_qsv_encoder;
extern const FFCodec ff_av1_amf_encoder;
+extern const FFCodec ff_av1_mf_encoder;
extern const FFCodec ff_av1_vaapi_encoder;
extern const FFCodec ff_libopenh264_encoder;
extern const FFCodec ff_libopenh264_decoder;
diff -Naur ffmpeg-7.1.old/libavcodec/amfenc_av1.c ffmpeg-7.1/libavcodec/amfenc_av1.c
--- ffmpeg-7.1.old/libavcodec/amfenc_av1.c 2024-12-03 14:07:22.576053223 +0100
+++ ffmpeg-7.1/libavcodec/amfenc_av1.c 2024-12-03 14:07:23.770060387 +0100
@@ -116,6 +116,7 @@
{ "none", "no adaptive quantization", 0, AV_OPT_TYPE_CONST, {.i64 = AMF_VIDEO_ENCODER_AV1_AQ_MODE_NONE }, 0, 0, VE, .unit = "adaptive_quantisation_mode" },
{ "caq", "context adaptive quantization", 0, AV_OPT_TYPE_CONST, {.i64 = AMF_VIDEO_ENCODER_AV1_AQ_MODE_CAQ }, 0, 0, VE, .unit = "adaptive_quantisation_mode" },
+ { "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "align", "alignment mode", OFFSET(align), AV_OPT_TYPE_INT, {.i64 = AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS }, AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_64X16_ONLY, AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS, VE, .unit = "align" },
{ "64x16", "", 0, AV_OPT_TYPE_CONST, {.i64 = AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_64X16_ONLY }, 0, 0, VE, .unit = "align" },
@@ -186,6 +187,8 @@
AMFRate framerate;
AMFSize framesize = AMFConstructSize(avctx->width, avctx->height);
amf_int64 color_depth;
+ amf_int64 color_primaries;
+ amf_int64 transfer_characteristic;
amf_int64 color_profile;
enum AVPixelFormat pix_fmt;
@@ -238,7 +241,11 @@
}
/// Color profile
+ color_primaries = ff_amf_get_color_primaries(avctx);
+ transfer_characteristic = ff_amf_get_transfer_characteristic(avctx);
color_profile = ff_amf_get_color_profile(avctx);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_COLOR_PRIMARIES, color_primaries);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_TRANSFER_CHARACTERISTIC, transfer_characteristic);
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_COLOR_PROFILE, color_profile);
/// Color Depth
@@ -250,16 +257,6 @@
}
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_COLOR_BIT_DEPTH, color_depth);
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_COLOR_PROFILE, color_profile);
- if (color_depth == AMF_COLOR_BIT_DEPTH_8) {
- /// Color Transfer Characteristics (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_TRANSFER_CHARACTERISTIC, AMF_COLOR_TRANSFER_CHARACTERISTIC_BT709);
- /// Color Primaries (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_COLOR_PRIMARIES, AMF_COLOR_PRIMARIES_BT709);
- } else {
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_TRANSFER_CHARACTERISTIC, AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE2084);
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_OUTPUT_COLOR_PRIMARIES, AMF_COLOR_PRIMARIES_BT2020);
- }
profile_level = avctx->level;
if (profile_level == AV_LEVEL_UNKNOWN) {
diff -Naur ffmpeg-7.1.old/libavcodec/amfenc.c ffmpeg-7.1/libavcodec/amfenc.c
--- ffmpeg-7.1.old/libavcodec/amfenc.c 2024-12-03 14:07:22.468052576 +0100
+++ ffmpeg-7.1/libavcodec/amfenc.c 2024-12-03 14:07:23.772060399 +0100
@@ -415,10 +415,6 @@
else
pix_fmt = avctx->pix_fmt;
- if (pix_fmt == AV_PIX_FMT_P010) {
- AMF_RETURN_IF_FALSE(ctx, ctx->version >= AMF_MAKE_FULL_VERSION(1, 4, 32, 0), AVERROR_UNKNOWN, "10-bit encoder is not supported by AMD GPU drivers versions lower than 23.30.\n");
- }
-
ctx->format = amf_av_to_amf_format(pix_fmt);
AMF_RETURN_IF_FALSE(ctx, ctx->format != AMF_SURFACE_UNKNOWN, AVERROR(EINVAL),
"Format %s is not supported\n", av_get_pix_fmt_name(pix_fmt));
@@ -766,11 +762,50 @@
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!ctx->aud);
+ switch (frame->pict_type) {
+ case AV_PICTURE_TYPE_I:
+ if (ctx->forced_idr) {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
+ } else {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
+ }
+ break;
+ case AV_PICTURE_TYPE_P:
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
+ break;
+ case AV_PICTURE_TYPE_B:
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
+ break;
+ }
break;
case AV_CODEC_ID_HEVC:
AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!ctx->aud);
+ switch (frame->pict_type) {
+ case AV_PICTURE_TYPE_I:
+ if (ctx->forced_idr) {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
+ } else {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
+ }
+ break;
+ case AV_PICTURE_TYPE_P:
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
+ break;
+ }
+ break;
+ case AV_CODEC_ID_AV1:
+ if (frame->pict_type == AV_PICTURE_TYPE_I) {
+ if (ctx->forced_idr) {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
+ } else {
+ AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
+ }
+ }
break;
- //case AV_CODEC_ID_AV1 not supported
default:
break;
}
@@ -878,6 +913,115 @@
return ret;
}
+int ff_amf_get_color_primaries(AVCodecContext *avctx)
+{
+ amf_int64 color_primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
+ switch (avctx->color_primaries) {
+ case AVCOL_PRI_BT709:
+ color_primaries = AMF_COLOR_PRIMARIES_BT709;
+ break;
+ case AVCOL_PRI_UNSPECIFIED:
+ color_primaries = AMF_COLOR_PRIMARIES_UNSPECIFIED;
+ break;
+ case AVCOL_PRI_RESERVED:
+ color_primaries = AMF_COLOR_PRIMARIES_RESERVED;
+ break;
+ case AVCOL_PRI_BT470M:
+ color_primaries = AMF_COLOR_PRIMARIES_BT470M;
+ break;
+ case AVCOL_PRI_BT470BG:
+ color_primaries = AMF_COLOR_PRIMARIES_BT470BG;
+ break;
+ case AVCOL_PRI_SMPTE170M:
+ color_primaries = AMF_COLOR_PRIMARIES_SMPTE170M;
+ break;
+ case AVCOL_PRI_SMPTE240M:
+ color_primaries = AMF_COLOR_PRIMARIES_SMPTE240M;
+ break;
+ case AVCOL_PRI_FILM:
+ color_primaries = AMF_COLOR_PRIMARIES_FILM;
+ break;
+ case AVCOL_PRI_BT2020:
+ color_primaries = AMF_COLOR_PRIMARIES_BT2020;
+ break;
+ case AVCOL_PRI_SMPTE428:
+ color_primaries = AMF_COLOR_PRIMARIES_SMPTE428;
+ break;
+ case AVCOL_PRI_SMPTE431:
+ color_primaries = AMF_COLOR_PRIMARIES_SMPTE431;
+ break;
+ case AVCOL_PRI_SMPTE432:
+ color_primaries = AMF_COLOR_PRIMARIES_SMPTE432;
+ break;
+ case AVCOL_PRI_EBU3213:
+ color_primaries = AMF_COLOR_PRIMARIES_JEDEC_P22;
+ break;
+ }
+ return color_primaries;
+}
+
+int ff_amf_get_transfer_characteristic(AVCodecContext *avctx)
+{
+ amf_int64 transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
+ switch (avctx->color_trc) {
+ case AVCOL_TRC_BT709:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_BT709;
+ break;
+ case AVCOL_TRC_UNSPECIFIED:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNSPECIFIED;
+ break;
+ case AVCOL_TRC_RESERVED:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_RESERVED;
+ break;
+ case AVCOL_TRC_GAMMA22:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_GAMMA22;
+ break;
+ case AVCOL_TRC_GAMMA28:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_GAMMA28;
+ break;
+ case AVCOL_TRC_SMPTE170M:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE170M;
+ break;
+ case AVCOL_TRC_SMPTE240M:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE240M;
+ break;
+ case AVCOL_TRC_LINEAR:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_LINEAR;
+ break;
+ case AVCOL_TRC_LOG:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_LOG;
+ break;
+ case AVCOL_TRC_LOG_SQRT:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_LOG_SQRT;
+ break;
+ case AVCOL_TRC_IEC61966_2_4:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_IEC61966_2_4;
+ break;
+ case AVCOL_TRC_BT1361_ECG:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_BT1361_ECG;
+ break;
+ case AVCOL_TRC_IEC61966_2_1:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_IEC61966_2_1;
+ break;
+ case AVCOL_TRC_BT2020_10:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_BT2020_10;
+ break;
+ case AVCOL_TRC_BT2020_12:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_BT2020_12;
+ break;
+ case AVCOL_TRC_SMPTE2084:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE2084;
+ break;
+ case AVCOL_TRC_SMPTE428:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE428;
+ break;
+ case AVCOL_TRC_ARIB_STD_B67:
+ transfer_characteristic = AMF_COLOR_TRANSFER_CHARACTERISTIC_ARIB_STD_B67;
+ break;
+ }
+ return transfer_characteristic;
+}
+
int ff_amf_get_color_profile(AVCodecContext *avctx)
{
amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
diff -Naur ffmpeg-7.1.old/libavcodec/amfenc.h ffmpeg-7.1/libavcodec/amfenc.h
--- ffmpeg-7.1.old/libavcodec/amfenc.h 2024-12-03 14:07:22.422052299 +0100
+++ ffmpeg-7.1/libavcodec/amfenc.h 2024-12-03 14:07:23.770060387 +0100
@@ -114,6 +114,7 @@
int max_b_frames;
int qvbr_quality_level;
int hw_high_motion_quality_boost;
+ int forced_idr;
// HEVC - specific options
@@ -173,6 +174,8 @@
*/
extern const enum AVPixelFormat ff_amf_pix_fmts[];
+int ff_amf_get_color_primaries(AVCodecContext *avctx);
+int ff_amf_get_transfer_characteristic(AVCodecContext *avctx);
int ff_amf_get_color_profile(AVCodecContext *avctx);
/**
diff -Naur ffmpeg-7.1.old/libavcodec/amfenc_h264.c ffmpeg-7.1/libavcodec/amfenc_h264.c
--- ffmpeg-7.1.old/libavcodec/amfenc_h264.c 2024-12-03 14:07:22.622053499 +0100
+++ ffmpeg-7.1/libavcodec/amfenc_h264.c 2024-12-03 14:07:23.770060387 +0100
@@ -133,6 +133,7 @@
{ "me_half_pel", "Enable ME Half Pixel", OFFSET(me_half_pel), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
{ "me_quarter_pel", "Enable ME Quarter Pixel", OFFSET(me_quarter_pel),AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+ { "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr) , AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "aud", "Inserts AU Delimiter NAL unit", OFFSET(aud) , AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
@@ -201,6 +202,8 @@
AMFRate framerate;
AMFSize framesize = AMFConstructSize(avctx->width, avctx->height);
int deblocking_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
+ amf_int64 color_primaries;
+ amf_int64 transfer_characteristic;
amf_int64 color_profile;
enum AVPixelFormat pix_fmt;
@@ -273,7 +276,11 @@
AMF_ASSIGN_PROPERTY_RATIO(res, ctx->encoder, AMF_VIDEO_ENCODER_ASPECT_RATIO, ratio);
}
+ color_primaries = ff_amf_get_color_primaries(avctx);
+ transfer_characteristic = ff_amf_get_transfer_characteristic(avctx);
color_profile = ff_amf_get_color_profile(avctx);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_COLOR_PRIMARIES, color_primaries);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_TRANSFER_CHARACTERISTIC, transfer_characteristic);
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_COLOR_PROFILE, color_profile);
/// Color Range (Support for older Drivers)
@@ -287,10 +294,6 @@
AMF_RETURN_IF_FALSE(ctx, pix_fmt != AV_PIX_FMT_P010, AVERROR_INVALIDDATA, "10-bit input video is not supported by AMF H264 encoder\n");
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_COLOR_BIT_DEPTH, AMF_COLOR_BIT_DEPTH_8);
- /// Color Transfer Characteristics (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->color_trc);
- /// Color Primaries (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_COLOR_PRIMARIES, (amf_int64)avctx->color_primaries);
// autodetect rate control method
if (ctx->rate_control_mode == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_UNKNOWN) {
diff -Naur ffmpeg-7.1.old/libavcodec/amfenc_hevc.c ffmpeg-7.1/libavcodec/amfenc_hevc.c
--- ffmpeg-7.1.old/libavcodec/amfenc_hevc.c 2024-12-03 14:07:22.423052305 +0100
+++ ffmpeg-7.1/libavcodec/amfenc_hevc.c 2024-12-03 14:07:23.770060387 +0100
@@ -100,6 +100,7 @@
{ "me_half_pel", "Enable ME Half Pixel", OFFSET(me_half_pel), AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE },
{ "me_quarter_pel", "Enable ME Quarter Pixel ", OFFSET(me_quarter_pel),AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE },
+ { "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr) ,AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE },
{ "aud", "Inserts AU Delimiter NAL unit", OFFSET(aud) ,AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE },
@@ -167,6 +168,8 @@
AMFSize framesize = AMFConstructSize(avctx->width, avctx->height);
int deblocking_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
amf_int64 color_depth;
+ amf_int64 color_primaries;
+ amf_int64 transfer_characteristic;
amf_int64 color_profile;
enum AVPixelFormat pix_fmt;
@@ -241,7 +244,11 @@
AMF_ASSIGN_PROPERTY_RATIO(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_ASPECT_RATIO, ratio);
}
+ color_primaries = ff_amf_get_color_primaries(avctx);
+ transfer_characteristic = ff_amf_get_transfer_characteristic(avctx);
color_profile = ff_amf_get_color_profile(avctx);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PRIMARIES, color_primaries);
+ AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_TRANSFER_CHARACTERISTIC, transfer_characteristic);
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PROFILE, color_profile);
/// Color Range (Support for older Drivers)
AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_NOMINAL_RANGE, !!(avctx->color_range == AVCOL_RANGE_JPEG));
@@ -253,15 +260,6 @@
color_depth = AMF_COLOR_BIT_DEPTH_10;
}
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_COLOR_BIT_DEPTH, color_depth);
- if (color_depth == AMF_COLOR_BIT_DEPTH_8) {
- /// Color Transfer Characteristics (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_TRANSFER_CHARACTERISTIC, AMF_COLOR_TRANSFER_CHARACTERISTIC_BT709);
- /// Color Primaries (AMF matches ISO/IEC)
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PRIMARIES, AMF_COLOR_PRIMARIES_BT709);
- } else {
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_TRANSFER_CHARACTERISTIC, AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE2084);
- AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PRIMARIES, AMF_COLOR_PRIMARIES_BT2020);
- }
// Picture control properties
AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_NUM_GOPS_PER_IDR, ctx->gops_per_idr);
diff -Naur ffmpeg-7.1.old/libavcodec/av1dec.c ffmpeg-7.1/libavcodec/av1dec.c
--- ffmpeg-7.1.old/libavcodec/av1dec.c 2024-12-03 14:07:22.534052971 +0100
+++ ffmpeg-7.1/libavcodec/av1dec.c 2024-12-03 14:07:23.766060363 +0100
@@ -1002,6 +1002,8 @@
break;
}
case ITU_T_T35_PROVIDER_CODE_DOLBY: {
+ AVBufferRef *rpu_buf;
+ AVFrameSideData *rpu;
int provider_oriented_code = bytestream2_get_be32(&gb);
if (itut_t35->itu_t_t35_country_code != ITU_T_T35_COUNTRY_CODE_US ||
provider_oriented_code != 0x800)
@@ -1014,6 +1016,18 @@
break; // ignore
}
+ rpu_buf = av_buffer_alloc(itut_t35->payload_size);
+ if (rpu_buf) {
+ memcpy(rpu_buf->data, itut_t35->payload, itut_t35->payload_size);
+ rpu = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_RPU_BUFFER_T35, rpu_buf);
+ if (!rpu) {
+ av_buffer_unref(&rpu_buf);
+ return AVERROR(ENOMEM);
+ }
+ } else {
+ return AVERROR(ENOMEM);
+ }
+
ret = ff_dovi_attach_side_data(&s->dovi, frame);
if (ret < 0)
return ret;
diff -Naur ffmpeg-7.1.old/libavcodec/avcodec.c ffmpeg-7.1/libavcodec/avcodec.c
--- ffmpeg-7.1.old/libavcodec/avcodec.c 2024-12-03 14:07:22.391052114 +0100
+++ ffmpeg-7.1/libavcodec/avcodec.c 2024-12-03 14:07:23.776060423 +0100
@@ -175,14 +175,6 @@
if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
return AVERROR(EINVAL);
- if ((ret = av_opt_set_dict(avctx, options)) < 0)
- return ret;
-
- if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
- av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
- return AVERROR(EINVAL);
- }
-
avci = av_codec_is_decoder(codec) ?
ff_decode_internal_alloc() :
ff_encode_internal_alloc();
@@ -216,6 +208,14 @@
} else {
avctx->priv_data = NULL;
}
+ if ((ret = av_opt_set_dict(avctx, options)) < 0)
+ goto free_and_end;
+
+ if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
+ ret = AVERROR(EINVAL);
+ goto free_and_end;
+ }
// only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
diff -Naur ffmpeg-7.1.old/libavcodec/bsf/hevc_mp4toannexb.c ffmpeg-7.1/libavcodec/bsf/hevc_mp4toannexb.c
--- ffmpeg-7.1.old/libavcodec/bsf/hevc_mp4toannexb.c 2024-12-03 14:07:22.514052852 +0100
+++ ffmpeg-7.1/libavcodec/bsf/hevc_mp4toannexb.c 2024-12-03 14:07:23.766060363 +0100
@@ -125,6 +125,7 @@
AVPacket *in;
GetByteContext gb;
+ int has_sps = 0, has_pps = 0;
int got_irap = 0;
int i, ret = 0;
@@ -158,11 +159,14 @@
}
nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
+ has_sps = (has_sps || nalu_type == HEVC_NAL_SPS);
+ has_pps = (has_pps || nalu_type == HEVC_NAL_PPS);
/* prepend extradata to IRAP frames */
is_irap = nalu_type >= HEVC_NAL_BLA_W_LP &&
nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
- add_extradata = is_irap && !got_irap;
+ /* ignore the extradata if IRAP frame has sps and pps */
+ add_extradata = is_irap && !got_irap && !(has_sps && has_pps);
extra_size = add_extradata * ctx->par_out->extradata_size;
got_irap |= is_irap;
diff -Naur ffmpeg-7.1.old/libavcodec/ccaption_dec.c ffmpeg-7.1/libavcodec/ccaption_dec.c
--- ffmpeg-7.1.old/libavcodec/ccaption_dec.c 2024-12-03 14:07:22.615053457 +0100
+++ ffmpeg-7.1/libavcodec/ccaption_dec.c 2024-12-03 14:07:23.761060333 +0100
@@ -889,12 +889,13 @@
if (ctx->buffer[bidx].str[0] || ctx->real_time) {
ff_dlog(ctx, "cdp writing data (%s)\n", ctx->buffer[bidx].str);
- start_time = ctx->buffer_time[0];
- sub->pts = start_time;
- end_time = ctx->buffer_time[1];
- if (!ctx->real_time)
+ if (!ctx->real_time) {
+ start_time = ctx->buffer_time[0];
+ sub->pts = start_time;
+ end_time = ctx->buffer_time[1];
sub->end_display_time = av_rescale_q(end_time - start_time,
AV_TIME_BASE_Q, ms_tb);
+ }
else
sub->end_display_time = -1;
ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated);
diff -Naur ffmpeg-7.1.old/libavcodec/dvdsubdec.c ffmpeg-7.1/libavcodec/dvdsubdec.c
--- ffmpeg-7.1.old/libavcodec/dvdsubdec.c 2024-12-03 14:07:22.407052209 +0100
+++ ffmpeg-7.1/libavcodec/dvdsubdec.c 2024-12-03 14:07:23.761060333 +0100
@@ -45,6 +45,8 @@
int buf_size;
int forced_subs_only;
uint8_t used_color[256];
+ int64_t pts;
+ int output_empty_rects;
} DVDSubContext;
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
@@ -230,7 +232,10 @@
uint32_t size;
int64_t offset1, offset2;
- if (buf_size < 10)
+ if (buf_size < 2)
+ return AVERROR(EAGAIN);
+
+ if (buf_size == 2 && AV_RB16(buf) == 0)
return -1;
if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
@@ -243,15 +248,22 @@
cmd_pos = 2;
}
+ if (big_offsets && buf_size < 6)
+ return AVERROR(EAGAIN);
+
size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
- cmd_pos = READ_OFFSET(buf + cmd_pos);
- if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
- if (cmd_pos > size) {
- av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
- return 0;
- }
+ if (size == 0)
+ return -1;
+
+ if (buf_size < size)
return AVERROR(EAGAIN);
+
+ cmd_pos = READ_OFFSET(buf + cmd_pos);
+
+ if (cmd_pos < 0 || cmd_pos > size) {
+ av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
+ return AVERROR_INVALIDDATA;
}
while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
@@ -524,10 +536,13 @@
int appended = 0;
int is_menu;
+ if (ctx->pts == AV_NOPTS_VALUE && sub->pts != AV_NOPTS_VALUE)
+ ctx->pts = sub->pts;
if (ctx->buf_size) {
int ret = append_to_cached_buf(avctx, buf, buf_size);
if (ret < 0) {
*data_size = 0;
+ ctx->pts = AV_NOPTS_VALUE;
return ret;
}
buf = ctx->buf;
@@ -538,7 +553,12 @@
is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
if (is_menu == AVERROR(EAGAIN)) {
*data_size = 0;
- return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
+ int ret = appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
+ if (ret < 0) {
+ ctx->pts = AV_NOPTS_VALUE;
+ return ret;
+ }
+ return buf_size;
}
if (is_menu < 0) {
@@ -547,9 +567,10 @@
reset_rects(sub);
*data_size = 0;
+ ctx->pts = AV_NOPTS_VALUE;
return buf_size;
}
- if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0)
+ if (!is_menu && !ctx->output_empty_rects && find_smallest_bounding_rectangle(ctx, sub) == 0)
goto no_subtitle;
if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
@@ -557,6 +578,8 @@
ctx->buf_size = 0;
*data_size = 1;
+ sub->pts = ctx->pts;
+ ctx->pts = AV_NOPTS_VALUE;
return buf_size;
}
@@ -682,6 +705,7 @@
av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32, ctx->palette[i]);
av_log(avctx, AV_LOG_DEBUG, "\n");
}
+ ctx->pts = AV_NOPTS_VALUE;
return 1;
}
@@ -698,6 +722,7 @@
{ "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
{ "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
{ "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
+ { "output_empty_rects", "Output subtitles with empty or fully transparent rects", OFFSET(output_empty_rects), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
{ NULL }
};
static const AVClass dvdsub_class = {
diff -Naur ffmpeg-7.1.old/libavcodec/libdav1d.c ffmpeg-7.1/libavcodec/libdav1d.c
--- ffmpeg-7.1.old/libavcodec/libdav1d.c 2024-12-03 14:07:22.531052953 +0100
+++ ffmpeg-7.1/libavcodec/libdav1d.c 2024-12-03 14:07:23.767060369 +0100
@@ -562,6 +562,8 @@
break;
}
case ITU_T_T35_PROVIDER_CODE_DOLBY: {
+ AVBufferRef *rpu_buf;
+ AVFrameSideData *rpu;
int provider_oriented_code = bytestream2_get_be32(&gb);
if (itut_t35->country_code != ITU_T_T35_COUNTRY_CODE_US ||
provider_oriented_code != 0x800)
@@ -574,6 +576,18 @@
break; // ignore
}
+ rpu_buf = av_buffer_alloc(itut_t35->payload_size);
+ if (rpu_buf) {
+ memcpy(rpu_buf->data, itut_t35->payload, itut_t35->payload_size);
+ rpu = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_RPU_BUFFER_T35, rpu_buf);
+ if (!rpu) {
+ av_buffer_unref(&rpu_buf);
+ goto fail;
+ }
+ } else {
+ goto fail;
+ }
+
res = ff_dovi_attach_side_data(&dav1d->dovi, frame);
if (res < 0)
goto fail;
diff -Naur ffmpeg-7.1.old/libavcodec/mfenc.c ffmpeg-7.1/libavcodec/mfenc.c
--- ffmpeg-7.1.old/libavcodec/mfenc.c 2024-12-03 14:07:22.568053176 +0100
+++ ffmpeg-7.1/libavcodec/mfenc.c 2024-12-03 14:07:23.775060417 +0100
@@ -1315,3 +1315,4 @@
MF_ENCODER(VIDEO, h264, H264, venc_opts, VFMTS, VCAPS, defaults);
MF_ENCODER(VIDEO, hevc, HEVC, venc_opts, VFMTS, VCAPS, defaults);
+MF_ENCODER(VIDEO, av1, AV1, venc_opts, VFMTS, VCAPS, defaults);
diff -Naur ffmpeg-7.1.old/libavcodec/mf_utils.c ffmpeg-7.1/libavcodec/mf_utils.c
--- ffmpeg-7.1.old/libavcodec/mf_utils.c 2024-12-03 14:07:22.526052923 +0100
+++ ffmpeg-7.1/libavcodec/mf_utils.c 2024-12-03 14:07:23.775060417 +0100
@@ -240,6 +240,7 @@
GUID_ENTRY(MFMediaType_Video),
GUID_ENTRY(MFAudioFormat_PCM),
GUID_ENTRY(MFAudioFormat_Float),
+ GUID_ENTRY(ff_MFVideoFormat_AV1),
GUID_ENTRY(MFVideoFormat_H264),
GUID_ENTRY(MFVideoFormat_H264_ES),
GUID_ENTRY(ff_MFVideoFormat_HEVC),
@@ -507,6 +508,7 @@
const CLSID *ff_codec_to_mf_subtype(enum AVCodecID codec)
{
switch (codec) {
+ case AV_CODEC_ID_AV1: return &ff_MFVideoFormat_AV1;
case AV_CODEC_ID_H264: return &MFVideoFormat_H264;
case AV_CODEC_ID_HEVC: return &ff_MFVideoFormat_HEVC;
case AV_CODEC_ID_AC3: return &MFAudioFormat_Dolby_AC3;
diff -Naur ffmpeg-7.1.old/libavcodec/mf_utils.h ffmpeg-7.1/libavcodec/mf_utils.h
--- ffmpeg-7.1.old/libavcodec/mf_utils.h 2024-12-03 14:07:22.499052761 +0100
+++ ffmpeg-7.1/libavcodec/mf_utils.h 2024-12-03 14:07:23.775060417 +0100
@@ -113,6 +113,7 @@
DEFINE_MEDIATYPE_GUID(ff_MFVideoFormat_HEVC, 0x43564548); // FCC('HEVC')
DEFINE_MEDIATYPE_GUID(ff_MFVideoFormat_HEVC_ES, 0x53564548); // FCC('HEVS')
+DEFINE_MEDIATYPE_GUID(ff_MFVideoFormat_AV1, 0x31305641); // FCC('AV01')
// This enum is missing from mingw-w64's codecapi.h by v7.0.0.
diff -Naur ffmpeg-7.1.old/libavcodec/pgssubdec.c ffmpeg-7.1/libavcodec/pgssubdec.c
--- ffmpeg-7.1.old/libavcodec/pgssubdec.c 2024-12-03 14:07:22.419052281 +0100
+++ ffmpeg-7.1/libavcodec/pgssubdec.c 2024-12-03 14:07:23.773060405 +0100
@@ -35,9 +35,11 @@
#include "libavutil/opt.h"
#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
-#define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
-#define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
-#define MAX_OBJECT_REFS 2 // Max objects per display set
+#define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
+#define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
+#define MAX_OBJECT_REFS 2 // Max objects per display set
+#define MAX_OBJECT_WH 4096 // Max object width/height
+
enum SegmentType {
PALETTE_SEGMENT = 0x14,
@@ -48,57 +50,80 @@
};
typedef struct PGSSubObjectRef {
- int id;
- int window_id;
- uint8_t composition_flag;
- int x;
- int y;
- int crop_x;
- int crop_y;
- int crop_w;
- int crop_h;
+ uint16_t id;
+ uint8_t window_id;
+ uint8_t composition_flag;
+ uint16_t x;
+ uint16_t y;
+ uint16_t crop_x;
+ uint16_t crop_y;
+ uint16_t crop_w;
+ uint16_t crop_h;
} PGSSubObjectRef;
typedef struct PGSSubPresentation {
- int id_number;
- int palette_id;
- int object_count;
+ uint8_t palette_flag;
+ uint8_t palette_id;
+ uint8_t object_count;
PGSSubObjectRef objects[MAX_OBJECT_REFS];
- int64_t pts;
+ int64_t pts;
} PGSSubPresentation;
typedef struct PGSSubObject {
- int id;
- int w;
- int h;
- uint8_t *rle;
- unsigned int rle_buffer_size, rle_data_len;
- unsigned int rle_remaining_len;
+ uint16_t id;
+ uint16_t w;
+ uint16_t h;
+ uint8_t *rle;
+ uint8_t *bitmap;
+ uint32_t rle_buffer_size;
+ uint32_t rle_data_len;
+ uint32_t rle_remaining_len;
+ uint32_t bitmap_buffer_size;
+ uint32_t bitmap_size;
} PGSSubObject;
typedef struct PGSSubObjects {
- int count;
+ uint8_t count;
PGSSubObject object[MAX_EPOCH_OBJECTS];
} PGSSubObjects;
typedef struct PGSSubPalette {
- int id;
- uint32_t clut[256];
+ uint8_t id;
+ uint32_t clut[AVPALETTE_COUNT];
} PGSSubPalette;
typedef struct PGSSubPalettes {
- int count;
+ uint8_t count;
PGSSubPalette palette[MAX_EPOCH_PALETTES];
} PGSSubPalettes;
+typedef struct PGSGraphicPlane {
+ uint8_t count;
+ uint8_t writable;
+ AVSubtitleRect visible_rect[MAX_OBJECT_REFS];
+} PGSGraphicPlane;
+
typedef struct PGSSubContext {
AVClass *class;
PGSSubPresentation presentation;
PGSSubPalettes palettes;
PGSSubObjects objects;
+ PGSGraphicPlane plane;
int forced_subs_only;
} PGSSubContext;
+static void clear_graphic_plane(PGSSubContext *ctx)
+{
+ int i;
+
+ for (i = 0; i < ctx->plane.count; i++) {
+ av_freep(&ctx->plane.visible_rect[i].data[0]);
+ memset(&ctx->plane.visible_rect[i], 0, sizeof(ctx->plane.visible_rect[i]));
+ }
+ ctx->plane.writable = 0;
+ ctx->plane.count = 0;
+}
+
static void flush_cache(AVCodecContext *avctx)
{
PGSSubContext *ctx = avctx->priv_data;
@@ -106,8 +131,11 @@
for (i = 0; i < ctx->objects.count; i++) {
av_freep(&ctx->objects.object[i].rle);
- ctx->objects.object[i].rle_buffer_size = 0;
+ ctx->objects.object[i].rle_buffer_size = 0;
ctx->objects.object[i].rle_remaining_len = 0;
+ av_freep(&ctx->objects.object[i].bitmap);
+ ctx->objects.object[i].bitmap_buffer_size = 0;
+ ctx->objects.object[i].bitmap_size = 0;
}
ctx->objects.count = 0;
ctx->palettes.count = 0;
@@ -144,6 +172,7 @@
static av_cold int close_decoder(AVCodecContext *avctx)
{
+ clear_graphic_plane((PGSSubContext *)avctx->priv_data);
flush_cache(avctx);
return 0;
@@ -159,48 +188,51 @@
* @param buf pointer to the RLE data to process
* @param buf_size size of the RLE data to process
*/
-static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect,
- const uint8_t *buf, unsigned int buf_size)
+static int decode_object_rle(AVCodecContext *avctx, PGSSubObject *object)
{
- const uint8_t *rle_bitmap_end;
+ const uint8_t *rle_buf;
+ const uint8_t *rle_end;
int pixel_count, line_count;
+ rle_buf = object->rle;
+ rle_end = object->rle + object->rle_data_len;
- rle_bitmap_end = buf + buf_size;
+ object->bitmap_size = object->w * object->h;
+ av_fast_padded_malloc(&object->bitmap, &object->bitmap_buffer_size,
+ object->bitmap_size);
- rect->data[0] = av_malloc_array(rect->w, rect->h);
-
- if (!rect->data[0])
+ if (!object->bitmap)
return AVERROR(ENOMEM);
pixel_count = 0;
line_count = 0;
- while (buf < rle_bitmap_end && line_count < rect->h) {
+ while (rle_buf < rle_end && line_count < object->h) {
uint8_t flags, color;
int run;
- color = bytestream_get_byte(&buf);
+ color = bytestream_get_byte(&rle_buf);
run = 1;
if (color == 0x00) {
- flags = bytestream_get_byte(&buf);
+ flags = bytestream_get_byte(&rle_buf);
run = flags & 0x3f;
if (flags & 0x40)
- run = (run << 8) + bytestream_get_byte(&buf);
- color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
+ run = (run << 8) + bytestream_get_byte(&rle_buf);
+ color = flags & 0x80 ? bytestream_get_byte(&rle_buf) : 0;
}
- if (run > 0 && pixel_count + run <= rect->w * rect->h) {
- memset(rect->data[0] + pixel_count, color, run);
+ if (run > 0 && pixel_count + run <= object->w * object->h) {
+ memset(object->bitmap + pixel_count, color, run);
pixel_count += run;
} else if (!run) {
/*
* New Line. Check if correct pixels decoded, if not display warning
* and adjust bitmap pointer to correct new line position.
*/
- if (pixel_count % rect->w > 0) {
- av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
- pixel_count % rect->w, rect->w);
+ if (pixel_count % object->w > 0) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Decoded %d pixels, when object line should be %d pixels\n",
+ pixel_count % object->w, object->w);
if (avctx->err_recognition & AV_EF_EXPLODE) {
return AVERROR_INVALIDDATA;
}
@@ -209,13 +241,11 @@
}
}
- if (pixel_count < rect->w * rect->h) {
- av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
+ if (pixel_count < object->w * object->h) {
+ av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for object\n");
return AVERROR_INVALIDDATA;
}
-
- ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h);
-
+ ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, object->w * object->h);
return 0;
}
@@ -237,7 +267,7 @@
uint8_t sequence_desc;
unsigned int rle_bitmap_len, width, height;
- int id;
+ int id, ret;
if (buf_size <= 4)
return AVERROR_INVALIDDATA;
@@ -260,57 +290,71 @@
/* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
sequence_desc = bytestream_get_byte(&buf);
- if (!(sequence_desc & 0x80)) {
- /* Additional RLE data */
- if (buf_size > object->rle_remaining_len)
+ /* First in sequence object definition segment */
+ if (sequence_desc & 0x80) {
+ if (buf_size <= 7)
return AVERROR_INVALIDDATA;
+ buf_size -= 7;
- memcpy(object->rle + object->rle_data_len, buf, buf_size);
- object->rle_data_len += buf_size;
- object->rle_remaining_len -= buf_size;
-
- return 0;
- }
-
- if (buf_size <= 7)
- return AVERROR_INVALIDDATA;
- buf_size -= 7;
+ /* Decode rle bitmap length, stored size includes width/height data */
+ rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
- /* Decode rle bitmap length, stored size includes width/height data */
- rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
+ if (buf_size > rle_bitmap_len) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Buffer dimension %d larger than the expected RLE data %d\n",
+ buf_size, rle_bitmap_len);
+ return AVERROR_INVALIDDATA;
+ }
- if (buf_size > rle_bitmap_len) {
- av_log(avctx, AV_LOG_ERROR,
- "Buffer dimension %d larger than the expected RLE data %d\n",
- buf_size, rle_bitmap_len);
- return AVERROR_INVALIDDATA;
- }
+ /* Get bitmap dimensions from data */
+ width = bytestream_get_be16(&buf);
+ height = bytestream_get_be16(&buf);
+
+ /* Make sure the bitmap is not too large */
+ if (MAX_OBJECT_WH < width || MAX_OBJECT_WH < height || !width || !height) {
+ av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height);
+ return AVERROR_INVALIDDATA;
+ }
- /* Get bitmap dimensions from data */
- width = bytestream_get_be16(&buf);
- height = bytestream_get_be16(&buf);
-
- /* Make sure the bitmap is not too large */
- if (avctx->width < width || avctx->height < height || !width || !height) {
- av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height);
- return AVERROR_INVALIDDATA;
- }
+ object->rle_data_len = 0;
+ object->w = width;
+ object->h = height;
+ /* Dimensions against video are checked at decode after cropping. */
+ av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
- object->w = width;
- object->h = height;
+ if (!object->rle) {
+ object->rle_remaining_len = 0;
+ return AVERROR(ENOMEM);
+ }
- av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
+ memcpy(object->rle, buf, buf_size);
+ object->rle_remaining_len = rle_bitmap_len;
+ } else {
+ /* Additional RLE data */
+ if (buf_size > object->rle_remaining_len)
+ return AVERROR_INVALIDDATA;
- if (!object->rle) {
- object->rle_data_len = 0;
- object->rle_remaining_len = 0;
- return AVERROR(ENOMEM);
+ memcpy(object->rle + object->rle_data_len, buf, buf_size);
}
+ object->rle_data_len += buf_size;
+ object->rle_remaining_len -= buf_size;
- memcpy(object->rle, buf, buf_size);
- object->rle_data_len = buf_size;
- object->rle_remaining_len = rle_bitmap_len - buf_size;
-
+ /* Last in sequence object definition (can be both first and last) */
+ if (sequence_desc & 0x40) {
+ /* Attempt decoding if data is valid */
+ if (0 == object->rle_remaining_len) {
+ ret = decode_object_rle(avctx, object);