-
Notifications
You must be signed in to change notification settings - Fork 252
/
header.rs
1238 lines (1097 loc) · 38.1 KB
/
header.rs
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
// Copyright (c) 2018-2022, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
use crate::api::*;
use crate::context::*;
use crate::ec::*;
use crate::lrf::*;
use crate::partition::*;
use crate::tiling::MAX_TILE_WIDTH;
use crate::util::Fixed;
use crate::util::Pixel;
use crate::DeblockState;
use crate::FrameInvariants;
use crate::FrameState;
use crate::SegmentationState;
use crate::Sequence;
use arrayvec::ArrayVec;
use bitstream_io::{BigEndian, BitWrite, BitWriter, LittleEndian};
use std::io;
pub const PRIMARY_REF_NONE: u32 = 7;
pub const ALL_REF_FRAMES_MASK: u32 = (1 << REF_FRAMES) - 1;
const PRIMARY_REF_BITS: u32 = 3;
#[allow(unused)]
const OP_POINTS_IDC_BITS: usize = 12;
#[allow(unused)]
const LEVEL_MAJOR_MIN: usize = 2;
#[allow(unused)]
const LEVEL_MAJOR_BITS: usize = 3;
#[allow(unused)]
const LEVEL_MINOR_BITS: usize = 2;
#[allow(unused)]
const LEVEL_BITS: usize = LEVEL_MAJOR_BITS + LEVEL_MINOR_BITS;
#[allow(dead_code, non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReferenceMode {
SINGLE = 0,
COMPOUND = 1,
SELECT = 2,
}
#[allow(non_camel_case_types)]
#[allow(unused)]
pub enum ObuType {
OBU_SEQUENCE_HEADER = 1,
OBU_TEMPORAL_DELIMITER = 2,
OBU_FRAME_HEADER = 3,
OBU_TILE_GROUP = 4,
OBU_METADATA = 5,
OBU_FRAME = 6,
OBU_REDUNDANT_FRAME_HEADER = 7,
OBU_TILE_LIST = 8,
OBU_PADDING = 15,
}
#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
#[allow(unused)]
pub enum ObuMetaType {
OBU_META_HDR_CLL = 1,
OBU_META_HDR_MDCV = 2,
OBU_META_SCALABILITY = 3,
OBU_META_ITUT_T35 = 4,
OBU_META_TIMECODE = 5,
}
impl ObuMetaType {
const fn size(self) -> u64 {
use self::ObuMetaType::*;
match self {
OBU_META_HDR_CLL => 4,
OBU_META_HDR_MDCV => 24,
_ => 0,
}
}
}
pub trait ULEB128Writer {
fn write_uleb128(&mut self, payload: u64) -> io::Result<()>;
}
impl<W: io::Write> ULEB128Writer for BitWriter<W, BigEndian> {
fn write_uleb128(&mut self, payload: u64) -> io::Result<()> {
// NOTE from libaom:
// Disallow values larger than 32-bits to ensure consistent behavior on 32 and
// 64 bit targets: value is typically used to determine buffer allocation size
// when decoded.
let mut coded_value: ArrayVec<u8, 8> = ArrayVec::new();
let mut value = payload as u32;
loop {
let mut byte = (value & 0x7f) as u8;
value >>= 7u8;
if value != 0 {
// Signal that more bytes follow.
byte |= 0x80;
}
coded_value.push(byte);
if value == 0 {
// We have to break at the end of the loop
// because there must be at least one byte written.
break;
}
}
for byte in coded_value {
self.write(8, byte)?;
}
Ok(())
}
}
pub trait LEWriter {
fn write_le(&mut self, bytes: u32, payload: u64) -> io::Result<()>;
}
// to write little endian values in a globally big-endian BitWriter
impl<W: io::Write> LEWriter for BitWriter<W, BigEndian> {
fn write_le(&mut self, bytes: u32, value: u64) -> io::Result<()> {
let mut data = Vec::new();
let mut bwle = BitWriter::endian(&mut data, LittleEndian);
bwle.write(bytes * 8, value)?;
self.write_bytes(&data)
}
}
pub trait UncompressedHeader {
// Start of OBU Headers
fn write_obu_header(
&mut self, obu_type: ObuType, obu_extension: u32,
) -> io::Result<()>;
fn write_sequence_metadata_obu(
&mut self, obu_meta_type: ObuMetaType, seq: &Sequence,
) -> io::Result<()>;
fn write_sequence_header_obu<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_frame_header_obu<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, fs: &FrameState<T>,
inter_cfg: &InterConfig,
) -> io::Result<()>;
fn write_sequence_header<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_color_config(&mut self, seq: &Sequence) -> io::Result<()>;
fn write_t35_metadata_obu(&mut self, t35: &T35) -> io::Result<()>;
// End of OBU Headers
fn write_max_frame_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_frame_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_render_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_frame_size_with_refs<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_deblock_filter_a<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, deblock: &DeblockState,
) -> io::Result<()>;
fn write_deblock_filter_b<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, deblock: &DeblockState,
) -> io::Result<()>;
fn write_frame_cdef<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()>;
fn write_frame_lrf<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, rs: &RestorationState,
) -> io::Result<()>;
fn write_segment_data<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, segmentation: &SegmentationState,
) -> io::Result<()>;
fn write_delta_q(&mut self, delta_q: i8) -> io::Result<()>;
}
impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
// Start of OBU Headers
// Write OBU Header syntax
fn write_obu_header(
&mut self, obu_type: ObuType, obu_extension: u32,
) -> io::Result<()> {
self.write_bit(false)?; // forbidden bit.
self.write(4, obu_type as u32)?;
self.write_bit(obu_extension != 0)?;
self.write_bit(true)?; // obu_has_payload_length_field
self.write_bit(false)?; // reserved
if obu_extension != 0 {
unimplemented!();
//self.write(8, obu_extension & 0xFF)?; size += 8;
}
Ok(())
}
fn write_sequence_metadata_obu(
&mut self, obu_meta_type: ObuMetaType, seq: &Sequence,
) -> io::Result<()> {
// header
self.write_obu_header(ObuType::OBU_METADATA, 0)?;
// uleb128() - length
// we use a constant value to avoid computing the OBU size every time
// since it is fixed (depending on the metadata)
// +2 is for the metadata_type field and the trailing bits byte
self.write_uleb128(obu_meta_type.size() + 2)?;
// uleb128() - metadata_type (1 byte)
self.write_uleb128(obu_meta_type as u64)?;
match obu_meta_type {
ObuMetaType::OBU_META_HDR_CLL => {
let cll = seq.content_light.unwrap();
self.write(16, cll.max_content_light_level)?;
self.write(16, cll.max_frame_average_light_level)?;
}
ObuMetaType::OBU_META_HDR_MDCV => {
let mdcv = seq.mastering_display.unwrap();
for i in 0..3 {
self.write(16, mdcv.primaries[i].x)?;
self.write(16, mdcv.primaries[i].y)?;
}
self.write(16, mdcv.white_point.x)?;
self.write(16, mdcv.white_point.y)?;
self.write(32, mdcv.max_luminance)?;
self.write(32, mdcv.min_luminance)?;
}
_ => {}
}
// trailing bits (1 byte)
self.write_bit(true)?;
self.byte_align()?;
Ok(())
}
fn write_t35_metadata_obu(&mut self, t35: &T35) -> io::Result<()> {
self.write_obu_header(ObuType::OBU_METADATA, 0)?;
// metadata type + country code + optional extension + trailing bits
self.write_uleb128(
t35.data.len() as u64 + if t35.country_code == 0xFF { 4 } else { 3 },
)?;
self.write_uleb128(ObuMetaType::OBU_META_ITUT_T35 as u64)?;
self.write(8, t35.country_code)?;
if t35.country_code == 0xFF {
self.write(8, t35.country_code_extension_byte)?;
}
self.write_bytes(&t35.data)?;
// trailing bits (1 byte)
self.write_bit(true)?;
self.byte_align()?;
Ok(())
}
fn write_sequence_header_obu<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
assert!(
!fi.sequence.reduced_still_picture_hdr || fi.sequence.still_picture
);
self.write(3, fi.sequence.profile)?; // profile
self.write_bit(fi.sequence.still_picture)?; // still_picture
self.write_bit(fi.sequence.reduced_still_picture_hdr)?; // reduced_still_picture_header
assert!(fi.sequence.level_idx[0] <= 31);
if fi.sequence.reduced_still_picture_hdr {
assert!(!fi.sequence.timing_info_present);
assert!(!fi.sequence.decoder_model_info_present_flag);
assert_eq!(fi.sequence.operating_points_cnt_minus_1, 0);
assert_eq!(fi.sequence.operating_point_idc[0], 0);
self.write(5, fi.sequence.level_idx[0])?; // level
assert_eq!(fi.sequence.tier[0], 0);
} else {
self.write_bit(fi.sequence.timing_info_present)?; // timing info present
if fi.sequence.timing_info_present {
self.write(32, fi.sequence.time_base.num)?;
self.write(32, fi.sequence.time_base.den)?;
self.write_bit(true)?; // equal picture interval
self.write_bit(true)?; // zero interval
self.write_bit(false)?; // decoder model info present flag
}
self.write_bit(false)?; // initial display delay present flag
self.write(5, 0)?; // one operating point
self.write(12, 0)?; // idc
self.write(5, fi.sequence.level_idx[0])?; // level
if fi.sequence.level_idx[0] > 7 {
self.write(1, 0)?; // tier
}
}
self.write_sequence_header(fi)?;
self.write_color_config(&fi.sequence)?;
self.write_bit(fi.sequence.film_grain_params_present)?;
Ok(())
}
fn write_sequence_header<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
self.write_max_frame_size(fi)?;
let seq = &fi.sequence;
if seq.reduced_still_picture_hdr {
assert!(!seq.frame_id_numbers_present_flag);
} else {
self.write_bit(seq.frame_id_numbers_present_flag)?;
}
if seq.frame_id_numbers_present_flag {
// We must always have delta_frame_id_length < frame_id_length,
// in order for a frame to be referenced with a unique delta.
// Avoid wasting bits by using a coding that enforces this restriction.
self.write(4, seq.delta_frame_id_length - 2)?;
self.write(3, seq.frame_id_length - seq.delta_frame_id_length - 1)?;
}
self.write_bit(seq.use_128x128_superblock)?;
self.write_bit(seq.enable_filter_intra)?;
self.write_bit(seq.enable_intra_edge_filter)?;
if seq.reduced_still_picture_hdr {
assert!(!seq.enable_interintra_compound);
assert!(!seq.enable_masked_compound);
assert!(!seq.enable_warped_motion);
assert!(!seq.enable_dual_filter);
assert!(!seq.enable_order_hint);
assert!(!seq.enable_jnt_comp);
assert!(!seq.enable_ref_frame_mvs);
assert!(seq.force_screen_content_tools == 2);
assert!(seq.force_integer_mv == 2);
} else {
self.write_bit(seq.enable_interintra_compound)?;
self.write_bit(seq.enable_masked_compound)?;
self.write_bit(seq.enable_warped_motion)?;
self.write_bit(seq.enable_dual_filter)?;
self.write_bit(seq.enable_order_hint)?;
if seq.enable_order_hint {
self.write_bit(seq.enable_jnt_comp)?;
self.write_bit(seq.enable_ref_frame_mvs)?;
}
if seq.force_screen_content_tools == 2 {
self.write_bit(true)?;
} else {
self.write_bit(false)?;
self.write_bit(seq.force_screen_content_tools != 0)?;
}
if seq.force_screen_content_tools > 0 {
if seq.force_integer_mv == 2 {
self.write_bit(true)?;
} else {
self.write_bit(false)?;
self.write_bit(seq.force_integer_mv != 0)?;
}
} else {
assert!(seq.force_integer_mv == 2);
}
if seq.enable_order_hint {
self.write(3, seq.order_hint_bits_minus_1)?;
}
}
self.write_bit(seq.enable_superres)?;
self.write_bit(seq.enable_cdef)?;
self.write_bit(seq.enable_restoration)?;
Ok(())
}
// <https://aomediacodec.github.io/av1-spec/#color-config-syntax>
fn write_color_config(&mut self, seq: &Sequence) -> io::Result<()> {
let high_bitdepth = seq.bit_depth > 8;
self.write_bit(high_bitdepth)?;
if seq.profile == 2 && high_bitdepth {
self.write_bit(seq.bit_depth == 12)?; // twelve_bit
}
let monochrome = seq.chroma_sampling == ChromaSampling::Cs400;
if seq.profile == 1 {
assert!(!monochrome);
} else {
self.write_bit(monochrome)?; // mono_chrome
}
// color_description_present_flag
self.write_bit(seq.color_description.is_some())?;
let mut srgb_triple = false;
if let Some(color_description) = seq.color_description {
self.write(8, color_description.color_primaries as u8)?;
self.write(8, color_description.transfer_characteristics as u8)?;
self.write(8, color_description.matrix_coefficients as u8)?;
srgb_triple = color_description.is_srgb_triple();
}
if monochrome || !srgb_triple {
self.write_bit(seq.pixel_range == PixelRange::Full)?; // color_range
}
if monochrome {
return Ok(());
} else if srgb_triple {
assert!(seq.pixel_range == PixelRange::Full);
assert!(seq.chroma_sampling == ChromaSampling::Cs444);
} else {
if seq.profile == 0 {
assert!(seq.chroma_sampling == ChromaSampling::Cs420);
} else if seq.profile == 1 {
assert!(seq.chroma_sampling == ChromaSampling::Cs444);
} else if seq.bit_depth == 12 {
let subsampling_x = seq.chroma_sampling != ChromaSampling::Cs444;
let subsampling_y = seq.chroma_sampling == ChromaSampling::Cs420;
self.write_bit(subsampling_x)?;
if subsampling_x {
self.write_bit(subsampling_y)?;
}
} else {
assert!(seq.chroma_sampling == ChromaSampling::Cs422);
}
if seq.chroma_sampling == ChromaSampling::Cs420 {
self.write(2, seq.chroma_sample_position as u32)?;
}
}
self.write_bit(true)?; // separate_uv_delta_q
Ok(())
}
#[allow(unused)]
fn write_frame_header_obu<T: Pixel>(
&mut self, fi: &FrameInvariants<T>, fs: &FrameState<T>,
inter_cfg: &InterConfig,
) -> io::Result<()> {
if fi.sequence.reduced_still_picture_hdr {
assert!(!fi.is_show_existing_frame());
assert!(fi.frame_type == FrameType::KEY);
assert!(fi.show_frame);
assert!(!fi.showable_frame);
} else {
self.write_bit(fi.is_show_existing_frame())?;
if fi.is_show_existing_frame() {
self.write(3, fi.frame_to_show_map_idx)?;
//TODO:
/* temporal_point_info();
if fi.sequence.decoder_model_info_present_flag &&
timing_info.equal_picture_interval == 0 {
// write frame_presentation_delay;
}
if fi.sequence.frame_id_numbers_present_flag {
// write display_frame_id;
}*/
self.write_bit(true)?; // trailing bit
self.byte_align()?;
return Ok(());
}
self.write(2, fi.frame_type as u32)?;
self.write_bit(fi.show_frame)?; // show frame
if fi.show_frame {
//TODO:
/* temporal_point_info();
if fi.sequence.decoder_model_info_present_flag &&
timing_info.equal_picture_interval == 0 {
// write frame_presentation_delay;
}*/
} else {
self.write_bit(fi.showable_frame)?;
}
if fi.error_resilient {
assert!(fi.primary_ref_frame == PRIMARY_REF_NONE);
}
if fi.frame_type == FrameType::SWITCH {
assert!(fi.error_resilient);
} else if !(fi.frame_type == FrameType::KEY && fi.show_frame) {
self.write_bit(fi.error_resilient)?; // error resilient
}
}
self.write_bit(fi.disable_cdf_update)?;
if fi.sequence.force_screen_content_tools == 2 {
self.write_bit(fi.allow_screen_content_tools != 0)?;
} else {
assert!(
fi.allow_screen_content_tools
== fi.sequence.force_screen_content_tools
);
}
if fi.allow_screen_content_tools > 0 {
if fi.sequence.force_integer_mv == 2 {
self.write_bit(fi.force_integer_mv != 0)?;
} else {
assert!(fi.force_integer_mv == fi.sequence.force_integer_mv);
}
}
assert!(
fi.force_integer_mv
== u32::from(fi.frame_type == FrameType::KEY || fi.intra_only)
);
if fi.sequence.frame_id_numbers_present_flag {
unimplemented!();
//TODO:
//let frame_id_len = fi.sequence.frame_id_length;
//self.write(frame_id_len, fi.current_frame_id);
}
if fi.frame_type != FrameType::SWITCH
&& !fi.sequence.reduced_still_picture_hdr
{
self.write_bit(fi.frame_size_override_flag)?; // frame size overhead flag
}
if fi.sequence.enable_order_hint {
let n = fi.sequence.order_hint_bits_minus_1 + 1;
let mask = (1 << n) - 1;
self.write(n, fi.order_hint & mask)?;
}
if !fi.error_resilient && !fi.intra_only {
self.write(PRIMARY_REF_BITS, fi.primary_ref_frame)?;
}
if fi.sequence.decoder_model_info_present_flag {
unimplemented!();
}
if fi.frame_type == FrameType::KEY {
if !fi.show_frame {
// unshown keyframe (forward keyframe)
unimplemented!();
self.write(REF_FRAMES as u32, fi.refresh_frame_flags)?;
} else {
assert!(fi.refresh_frame_flags == ALL_REF_FRAMES_MASK);
}
} else if fi.frame_type == FrameType::SWITCH {
assert!(fi.refresh_frame_flags == ALL_REF_FRAMES_MASK);
} else {
// Inter frame info goes here
if fi.intra_only {
assert!(fi.refresh_frame_flags != ALL_REF_FRAMES_MASK);
} else {
// TODO: This should be set once inter mode is used
}
self.write(REF_FRAMES as u32, fi.refresh_frame_flags)?;
};
if (!fi.intra_only || fi.refresh_frame_flags != ALL_REF_FRAMES_MASK) {
// Write all ref frame order hints if error_resilient_mode == 1
if (fi.error_resilient && fi.sequence.enable_order_hint) {
for i in 0..REF_FRAMES {
let n = fi.sequence.order_hint_bits_minus_1 + 1;
let mask = (1 << n) - 1;
if let Some(ref rec) = fi.rec_buffer.frames[i] {
let ref_hint = rec.order_hint;
self.write(n, ref_hint & mask)?;
} else {
self.write(n, 0)?;
}
}
}
}
// if KEY or INTRA_ONLY frame
if fi.intra_only {
self.write_frame_size(fi)?;
self.write_render_size(fi)?;
if fi.allow_screen_content_tools != 0 {
// TODO: && UpscaledWidth == FrameWidth.
self.write_bit(fi.allow_intrabc)?;
}
}
let frame_refs_short_signaling = false;
if fi.frame_type == FrameType::KEY || fi.intra_only {
// Done by above
} else {
if fi.sequence.enable_order_hint {
self.write_bit(frame_refs_short_signaling)?;
if frame_refs_short_signaling {
unimplemented!();
}
}
for i in 0..INTER_REFS_PER_FRAME {
if !frame_refs_short_signaling {
self.write(REF_FRAMES_LOG2 as u32, fi.ref_frames[i])?;
}
if fi.sequence.frame_id_numbers_present_flag {
unimplemented!();
}
}
if !fi.error_resilient && fi.frame_size_override_flag {
self.write_frame_size_with_refs(fi)?;
} else {
self.write_frame_size(fi)?;
self.write_render_size(fi)?;
}
if fi.force_integer_mv == 0 {
self.write_bit(fi.allow_high_precision_mv);
}
self.write_bit(fi.is_filter_switchable)?;
if !fi.is_filter_switchable {
self.write(2, fi.default_filter as u8)?;
}
self.write_bit(fi.is_motion_mode_switchable)?;
if (!fi.error_resilient && fi.sequence.enable_ref_frame_mvs) {
self.write_bit(fi.use_ref_frame_mvs)?;
}
}
if fi.sequence.reduced_still_picture_hdr || fi.disable_cdf_update {
assert!(fi.disable_frame_end_update_cdf);
} else {
self.write_bit(fi.disable_frame_end_update_cdf)?;
}
// tile
// <https://aomediacodec.github.io/av1-spec/#tile-info-syntax>
// Can we use the uniform spacing tile syntax? 'Uniform spacing'
// is a slight misnomer; it's more constrained than just a uniform
// spacing.
let ti = &fi.sequence.tiling;
if fi.sb_width.align_power_of_two_and_shift(ti.tile_cols_log2)
== ti.tile_width_sb
&& fi.sb_height.align_power_of_two_and_shift(ti.tile_rows_log2)
== ti.tile_height_sb
{
// yes; our actual tile width/height setting (which is always
// currently uniform) also matches the constrained width/height
// calculation implicit in the uniform spacing flag.
self.write_bit(true)?; // uniform_tile_spacing_flag
let cols_ones = ti.tile_cols_log2 - ti.min_tile_cols_log2;
for _ in 0..cols_ones {
self.write_bit(true);
}
if ti.tile_cols_log2 < ti.max_tile_cols_log2 {
self.write_bit(false);
}
let rows_ones = ti.tile_rows_log2 - ti.min_tile_rows_log2;
for _ in 0..rows_ones {
self.write_bit(true);
}
if ti.tile_rows_log2 < ti.max_tile_rows_log2 {
self.write_bit(false);
}
} else {
self.write_bit(false)?; // uniform_tile_spacing_flag
let mut sofar = 0;
let mut widest_tile_sb = 0;
for _ in 0..ti.cols {
let max = (MAX_TILE_WIDTH
>> if fi.sequence.use_128x128_superblock { 7 } else { 6 })
.min(fi.sb_width - sofar) as u16;
let this_sb_width = ti.tile_width_sb.min(fi.sb_width - sofar);
self.write_quniform(max, (this_sb_width - 1) as u16);
sofar += this_sb_width;
widest_tile_sb = widest_tile_sb.max(this_sb_width);
}
let max_tile_area_sb = if ti.min_tiles_log2 > 0 {
(fi.sb_height * fi.sb_width) >> (ti.min_tiles_log2 + 1)
} else {
fi.sb_height * fi.sb_width
};
let max_tile_height_sb = (max_tile_area_sb / widest_tile_sb).max(1);
sofar = 0;
for i in 0..ti.rows {
let max = max_tile_height_sb.min(fi.sb_height - sofar) as u16;
let this_sb_height = ti.tile_height_sb.min(fi.sb_height - sofar);
self.write_quniform(max, (this_sb_height - 1) as u16);
sofar += this_sb_height;
}
}
let tiles_log2 = ti.tile_cols_log2 + ti.tile_rows_log2;
if tiles_log2 > 0 {
// context_update_tile_id
// for now, always use the first tile CDF
self.write(tiles_log2 as u32, fs.context_update_tile_id as u32)?;
// tile_size_bytes_minus_1
self.write(2, fs.max_tile_size_bytes - 1);
}
// quantization
assert!(fi.base_q_idx > 0);
self.write(8, fi.base_q_idx)?; // base_q_idx
self.write_delta_q(fi.dc_delta_q[0])?;
if fi.sequence.chroma_sampling != ChromaSampling::Cs400 {
assert!(fi.ac_delta_q[0] == 0);
let diff_uv_delta = fi.dc_delta_q[1] != fi.dc_delta_q[2]
|| fi.ac_delta_q[1] != fi.ac_delta_q[2];
self.write_bit(diff_uv_delta)?;
self.write_delta_q(fi.dc_delta_q[1])?;
self.write_delta_q(fi.ac_delta_q[1])?;
if diff_uv_delta {
self.write_delta_q(fi.dc_delta_q[2])?;
self.write_delta_q(fi.ac_delta_q[2])?;
}
}
self.write_bit(false)?; // no qm
// segmentation
self.write_segment_data(fi, &fs.segmentation)?;
// delta_q
self.write_bit(false)?; // delta_q_present_flag: no delta q
// delta_lf_params in the spec
self.write_deblock_filter_a(fi, &fs.deblock)?;
// code for features not yet implemented....
// loop_filter_params in the spec
self.write_deblock_filter_b(fi, &fs.deblock)?;
// cdef
self.write_frame_cdef(fi)?;
// loop restoration
self.write_frame_lrf(fi, &fs.restoration)?;
self.write_bit(fi.tx_mode_select)?; // tx mode
let mut reference_select = false;
if !fi.intra_only {
reference_select = fi.reference_mode != ReferenceMode::SINGLE;
self.write_bit(reference_select)?;
}
let skip_mode_allowed =
fi.sequence.get_skip_mode_allowed(fi, inter_cfg, reference_select);
if skip_mode_allowed {
self.write_bit(false)?; // skip_mode_present
}
if fi.intra_only || fi.error_resilient || !fi.sequence.enable_warped_motion
{
} else {
self.write_bit(fi.allow_warped_motion)?; // allow_warped_motion
}
self.write_bit(fi.use_reduced_tx_set)?; // reduced tx
// global motion
if !fi.intra_only {
for i in 0..7 {
let mode = fi.globalmv_transformation_type[i];
self.write_bit(mode != GlobalMVMode::IDENTITY)?;
if mode != GlobalMVMode::IDENTITY {
self.write_bit(mode == GlobalMVMode::ROTZOOM)?;
if mode != GlobalMVMode::ROTZOOM {
self.write_bit(mode == GlobalMVMode::TRANSLATION)?;
}
}
match mode {
GlobalMVMode::IDENTITY => { /* Nothing to do */ }
GlobalMVMode::TRANSLATION => {
let mv_x = 0;
let mv_x_ref = 0;
let mv_y = 0;
let mv_y_ref = 0;
let bits = 12 - 6 + 3 - !fi.allow_high_precision_mv as u8;
let bits_diff = 12 - 3 + fi.allow_high_precision_mv as u8;
BCodeWriter::write_s_refsubexpfin(
self,
(1 << bits) + 1,
3,
mv_x_ref >> bits_diff,
mv_x >> bits_diff,
)?;
BCodeWriter::write_s_refsubexpfin(
self,
(1 << bits) + 1,
3,
mv_y_ref >> bits_diff,
mv_y >> bits_diff,
)?;
}
GlobalMVMode::ROTZOOM => unimplemented!(),
GlobalMVMode::AFFINE => unimplemented!(),
};
}
}
if fi.sequence.film_grain_params_present {
if let Some(grain_params) = fi.film_grain_params() {
// Apply grain
self.write_bit(true)?;
self.write(16, grain_params.random_seed)?;
if fi.frame_type == FrameType::INTER {
// For the purposes of photon noise,
// it's simpler to always update the params,
// and the output will be the same.
self.write_bit(true)?;
}
self.write(4, grain_params.scaling_points_y.len() as u8)?;
for point in &grain_params.scaling_points_y {
self.write(8, point[0])?;
self.write(8, point[1])?;
}
let chroma_scaling_from_luma =
if fi.sequence.chroma_sampling != ChromaSampling::Cs400 {
self.write_bit(grain_params.chroma_scaling_from_luma)?;
grain_params.chroma_scaling_from_luma
} else {
false
};
if !(fi.sequence.chroma_sampling == ChromaSampling::Cs400
|| chroma_scaling_from_luma
|| (fi.sequence.chroma_sampling == ChromaSampling::Cs420
&& grain_params.scaling_points_y.is_empty()))
{
self.write(4, grain_params.scaling_points_cb.len() as u8)?;
for point in &grain_params.scaling_points_cb {
self.write(8, point[0])?;
self.write(8, point[1])?;
}
self.write(4, grain_params.scaling_points_cr.len() as u8)?;
for point in &grain_params.scaling_points_cr {
self.write(8, point[0])?;
self.write(8, point[1])?;
}
}
self.write(2, grain_params.scaling_shift - 8)?;
self.write(2, grain_params.ar_coeff_lag)?;
let mut num_pos_luma =
(2 * grain_params.ar_coeff_lag * (grain_params.ar_coeff_lag + 1))
as usize;
let mut num_pos_chroma;
if !grain_params.scaling_points_y.is_empty() {
num_pos_chroma = num_pos_luma + 1;
for i in 0..num_pos_luma {
self.write(8, grain_params.ar_coeffs_y[i] as i16 + 128)?;
}
} else {
num_pos_chroma = num_pos_luma;
}
if chroma_scaling_from_luma
|| !grain_params.scaling_points_cb.is_empty()
{
for i in 0..num_pos_chroma {
self.write(8, grain_params.ar_coeffs_cb[i] as i16 + 128)?;
}
}
if chroma_scaling_from_luma
|| !grain_params.scaling_points_cr.is_empty()
{
for i in 0..num_pos_chroma {
self.write(8, grain_params.ar_coeffs_cr[i] as i16 + 128)?;
}
}
self.write(2, grain_params.ar_coeff_shift - 6)?;
self.write(2, grain_params.grain_scale_shift)?;
if !grain_params.scaling_points_cb.is_empty() {
self.write(8, grain_params.cb_mult)?;
self.write(8, grain_params.cb_luma_mult)?;
self.write(9, grain_params.cb_offset)?;
}
if !grain_params.scaling_points_cr.is_empty() {
self.write(8, grain_params.cr_mult)?;
self.write(8, grain_params.cr_luma_mult)?;
self.write(9, grain_params.cr_offset)?;
}
self.write_bit(grain_params.overlap_flag)?;
self.write_bit(fi.sequence.pixel_range == PixelRange::Limited)?;
} else {
// No film grain for this frame
self.write_bit(false)?;
}
}
if fi.large_scale_tile {
unimplemented!();
}
self.byte_align()?;
Ok(())
}
// End of OBU Headers
fn write_max_frame_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
// width_bits and height_bits will have to be moved to the sequence header OBU
// when we add support for it.
let width = fi.width - 1;
let height = fi.height - 1;
let width_bits = log_in_base_2(width as u32) as u32 + 1;
let height_bits = log_in_base_2(height as u32) as u32 + 1;
assert!(width_bits <= 16);
assert!(height_bits <= 16);
self.write(4, width_bits - 1)?;
self.write(4, height_bits - 1)?;
self.write(width_bits, width as u16)?;
self.write(height_bits, height as u16)?;
Ok(())
}
fn write_frame_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
// width_bits and height_bits will have to be moved to the sequence header OBU
// when we add support for it.
if fi.frame_size_override_flag {
let width = fi.width - 1;
let height = fi.height - 1;
let width_bits = log_in_base_2(width as u32) as u32 + 1;
let height_bits = log_in_base_2(height as u32) as u32 + 1;
assert!(width_bits <= 16);
assert!(height_bits <= 16);
self.write(width_bits, width as u16)?;
self.write(height_bits, height as u16)?;
}
if fi.sequence.enable_superres {
unimplemented!();
}
Ok(())
}
fn write_render_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
self.write_bit(fi.render_and_frame_size_different)?;
if fi.render_and_frame_size_different {
self.write(16, fi.render_width - 1)?;
self.write(16, fi.render_height - 1)?;
}
Ok(())
}
fn write_frame_size_with_refs<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
let mut found_ref = false;
for i in 0..INTER_REFS_PER_FRAME {
if let Some(ref rec) = fi.rec_buffer.frames[fi.ref_frames[i] as usize] {
if rec.width == fi.width as u32
&& rec.height == fi.height as u32
&& rec.render_width == fi.render_width
&& rec.render_height == fi.render_height
{