@@ -45,6 +45,13 @@ static const uint8_t default_scaling4[2][16] = {
4545 20 , 24 , 27 , 30 , 24 , 27 , 30 , 34 }
4646};
4747
48+ static const uint8_t default_scaling4_mars [2 ][16 ] = {
49+ { 6 , 12 , 21 , 28 , 12 , 21 , 28 , 32 ,
50+ 21 , 28 , 32 , 36 , 28 , 32 , 36 , 42 },
51+ { 10 , 14 , 22 , 24 , 14 , 22 , 24 , 27 ,
52+ 22 , 24 , 27 , 31 , 24 , 27 , 31 , 34 }
53+ };
54+
4855static const uint8_t default_scaling8 [2 ][64 ] = {
4956 { 6 , 10 , 13 , 16 , 18 , 23 , 25 , 27 ,
5057 10 , 11 , 16 , 18 , 23 , 25 , 27 , 29 ,
@@ -64,6 +71,25 @@ static const uint8_t default_scaling8[2][64] = {
6471 24 , 25 , 27 , 28 , 30 , 32 , 33 , 35 }
6572};
6673
74+ static const uint8_t default_scaling8_mars [2 ][64 ] = {
75+ { 6 , 10 , 13 , 16 , 18 , 23 , 25 , 27 ,
76+ 10 , 11 , 16 , 18 , 23 , 25 , 27 , 29 ,
77+ 13 , 16 , 18 , 23 , 25 , 27 , 29 , 31 ,
78+ 16 , 18 , 23 , 25 , 27 , 29 , 31 , 34 ,
79+ 18 , 23 , 25 , 27 , 29 , 31 , 34 , 36 ,
80+ 23 , 25 , 27 , 29 , 31 , 34 , 36 , 37 ,
81+ 25 , 27 , 29 , 31 , 34 , 36 , 37 , 39 ,
82+ 27 , 29 , 31 , 34 , 36 , 37 , 39 , 42 }, // [PolyV] changed
83+ { 9 , 13 , 15 , 17 , 20 , 21 , 22 , 24 ,
84+ 13 , 13 , 17 , 20 , 21 , 22 , 24 , 25 ,
85+ 15 , 17 , 20 , 21 , 22 , 24 , 25 , 27 ,
86+ 17 , 20 , 21 , 22 , 24 , 25 , 27 , 28 ,
87+ 20 , 21 , 22 , 24 , 25 , 27 , 28 , 30 ,
88+ 21 , 22 , 24 , 25 , 27 , 28 , 30 , 33 ,
89+ 22 , 24 , 25 , 27 , 28 , 30 , 33 , 34 ,
90+ 24 , 25 , 27 , 28 , 30 , 33 , 34 , 35 } // [PolyV] changed
91+ };
92+
6793/* maximum number of MBs in the DPB for a given level */
6894static const int level_max_dpb_mbs [][2 ] = {
6995 { 10 , 396 },
@@ -142,8 +168,9 @@ static inline int decode_vui_parameters(GetBitContext *gb, void *logctx,
142168
143169 sps -> timing_info_present_flag = get_bits1 (gb );
144170 if (sps -> timing_info_present_flag ) {
171+ sps -> fixed_frame_rate_flag = get_bits1 (gb ); // [PolyV] moved
172+ unsigned time_scale = get_bits_long (gb , 32 ); // [PolyV] moved
145173 unsigned num_units_in_tick = get_bits_long (gb , 32 );
146- unsigned time_scale = get_bits_long (gb , 32 );
147174 if (!num_units_in_tick || !time_scale ) {
148175 av_log (logctx , AV_LOG_ERROR ,
149176 "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n" ,
@@ -153,14 +180,13 @@ static inline int decode_vui_parameters(GetBitContext *gb, void *logctx,
153180 sps -> num_units_in_tick = num_units_in_tick ;
154181 sps -> time_scale = time_scale ;
155182 }
156- sps -> fixed_frame_rate_flag = get_bits1 (gb );
157183 }
158184
159- sps -> nal_hrd_parameters_present_flag = get_bits1 (gb );
185+ sps -> nal_hrd_parameters_present_flag = get_bits1 (gb ); // [PolyV] may be flipped, can't prove it
160186 if (sps -> nal_hrd_parameters_present_flag )
161187 if (decode_hrd_parameters (gb , logctx , sps ) < 0 )
162188 return AVERROR_INVALIDDATA ;
163- sps -> vcl_hrd_parameters_present_flag = get_bits1 (gb );
189+ sps -> vcl_hrd_parameters_present_flag = get_bits1 (gb ); // [PolyV] may be flipped, can't prove it
164190 if (sps -> vcl_hrd_parameters_present_flag )
165191 if (decode_hrd_parameters (gb , logctx , sps ) < 0 )
166192 return AVERROR_INVALIDDATA ;
@@ -172,9 +198,9 @@ static inline int decode_vui_parameters(GetBitContext *gb, void *logctx,
172198 return 0 ;
173199 sps -> bitstream_restriction_flag = get_bits1 (gb );
174200 if (sps -> bitstream_restriction_flag ) {
175- get_bits1 (gb ); /* motion_vectors_over_pic_boundaries_flag */
176201 get_ue_golomb_31 (gb ); /* max_bytes_per_pic_denom */
177202 get_ue_golomb_31 (gb ); /* max_bits_per_mb_denom */
203+ get_bits1 (gb ); /* motion_vectors_over_pic_boundaries_flag */ // [PolyV] moved
178204 get_ue_golomb_31 (gb ); /* log2_max_mv_length_horizontal */
179205 get_ue_golomb_31 (gb ); /* log2_max_mv_length_vertical */
180206 sps -> num_reorder_frames = get_ue_golomb_31 (gb );
@@ -235,31 +261,72 @@ static int decode_scaling_matrices(GetBitContext *gb, const SPS *sps,
235261 uint8_t (* scaling_matrix8 )[64 ])
236262{
237263 int fallback_sps = !is_sps && sps -> scaling_matrix_present ;
238- const uint8_t * fallback [4 ] = {
239- fallback_sps ? sps -> scaling_matrix4 [0 ] : default_scaling4 [0 ],
240- fallback_sps ? sps -> scaling_matrix4 [3 ] : default_scaling4 [1 ],
241- fallback_sps ? sps -> scaling_matrix8 [0 ] : default_scaling8 [0 ],
242- fallback_sps ? sps -> scaling_matrix8 [3 ] : default_scaling8 [1 ]
243- };
264+ const uint8_t * fallback_0 ;
265+ const uint8_t * fallback_1 ;
266+ const uint8_t * fallback_2 ;
267+ const uint8_t * fallback_3 ;
268+
269+ int b_mars_flag ;
270+
271+ if (sps -> profile_idc == 77 || sps -> profile_idc == 66 ) {
272+ b_mars_flag = 0 ;
273+ if (fallback_sps ) {
274+ fallback_0 = sps -> scaling_matrix4 [0 ];
275+ fallback_1 = sps -> scaling_matrix4 [3 ];
276+ fallback_2 = sps -> scaling_matrix8 [0 ];
277+ fallback_3 = sps -> scaling_matrix8 [3 ];
278+ } else {
279+ fallback_0 = default_scaling4 [0 ];
280+ fallback_1 = default_scaling4 [1 ];
281+ fallback_2 = default_scaling8 [0 ];
282+ fallback_3 = default_scaling8 [1 ];
283+ }
284+ } else {
285+ b_mars_flag = 1 ;
286+ if (fallback_sps ) {
287+ fallback_0 = sps -> scaling_matrix4 [0 ];
288+ fallback_1 = sps -> scaling_matrix4 [3 ];
289+ fallback_2 = sps -> scaling_matrix8 [0 ];
290+ fallback_3 = sps -> scaling_matrix8 [3 ];
291+ } else {
292+ fallback_0 = default_scaling4_mars [0 ]; // [PolyV] edited scaling
293+ fallback_1 = default_scaling4_mars [1 ]; // [PolyV] edited scaling
294+ fallback_2 = default_scaling8_mars [1 ]; // [PolyV] edited scaling
295+ fallback_3 = default_scaling8_mars [0 ]; // [PolyV] edited scaling
296+ }
297+ }
298+
244299 int ret = 0 ;
245300 * mask = 0x0 ;
301+
246302 if (present_flag ) {
247- ret |= decode_scaling_list (gb , scaling_matrix4 [0 ], 16 , default_scaling4 [0 ], fallback [0 ], mask , 0 ); // Intra, Y
248- ret |= decode_scaling_list (gb , scaling_matrix4 [1 ], 16 , default_scaling4 [0 ], scaling_matrix4 [0 ], mask , 1 ); // Intra, Cr
249- ret |= decode_scaling_list (gb , scaling_matrix4 [2 ], 16 , default_scaling4 [0 ], scaling_matrix4 [1 ], mask , 2 ); // Intra, Cb
250- ret |= decode_scaling_list (gb , scaling_matrix4 [3 ], 16 , default_scaling4 [1 ], fallback [1 ], mask , 3 ); // Inter, Y
251- ret |= decode_scaling_list (gb , scaling_matrix4 [4 ], 16 , default_scaling4 [1 ], scaling_matrix4 [3 ], mask , 4 ); // Inter, Cr
252- ret |= decode_scaling_list (gb , scaling_matrix4 [5 ], 16 , default_scaling4 [1 ], scaling_matrix4 [4 ], mask , 5 ); // Inter, Cb
303+ const uint8_t * default4_0 = b_mars_flag ? default_scaling4_mars [0 ] : default_scaling4 [0 ];
304+ const uint8_t * default4_1 = b_mars_flag ? default_scaling4_mars [1 ] : default_scaling4 [1 ];
305+ const uint8_t * default8_0 = b_mars_flag ? default_scaling8_mars [0 ] : default_scaling8 [0 ];
306+ const uint8_t * default8_1 = b_mars_flag ? default_scaling8_mars [1 ] : default_scaling8 [1 ];
307+
308+ ret |= decode_scaling_list (gb , scaling_matrix4 [0 ], 16 , default4_0 , fallback_0 , mask , 0 );
309+ ret |= decode_scaling_list (gb , scaling_matrix4 [1 ], 16 , default4_0 , scaling_matrix4 [0 ], mask , 1 );
310+ ret |= decode_scaling_list (gb , scaling_matrix4 [2 ], 16 , default4_0 , scaling_matrix4 [1 ], mask , 2 );
311+ ret |= decode_scaling_list (gb , scaling_matrix4 [3 ], 16 , default4_1 , fallback_1 , mask , 3 );
312+ ret |= decode_scaling_list (gb , scaling_matrix4 [4 ], 16 , default4_1 , scaling_matrix4 [3 ], mask , 4 );
313+ ret |= decode_scaling_list (gb , scaling_matrix4 [5 ], 16 , default4_1 , scaling_matrix4 [4 ], mask , 5 );
314+
253315 if (is_sps || pps -> transform_8x8_mode ) {
254- ret |= decode_scaling_list (gb , scaling_matrix8 [0 ], 64 , default_scaling8 [0 ], fallback [2 ], mask , 6 ); // Intra, Y
255- ret |= decode_scaling_list (gb , scaling_matrix8 [3 ], 64 , default_scaling8 [1 ], fallback [3 ], mask , 7 ); // Inter, Y
316+ ret |= decode_scaling_list (gb , scaling_matrix8 [0 ], 64 , default8_0 , fallback_2 , mask , 6 );
317+ ret |= decode_scaling_list (gb , scaling_matrix8 [3 ], 64 , default8_1 , fallback_3 , mask , 7 );
318+
256319 if (sps -> chroma_format_idc == 3 ) {
257- ret |= decode_scaling_list (gb , scaling_matrix8 [1 ], 64 , default_scaling8 [0 ], scaling_matrix8 [0 ], mask , 8 ); // Intra, Cr
258- ret |= decode_scaling_list (gb , scaling_matrix8 [4 ], 64 , default_scaling8 [1 ], scaling_matrix8 [3 ], mask , 9 ); // Inter, Cr
259- ret |= decode_scaling_list (gb , scaling_matrix8 [2 ], 64 , default_scaling8 [0 ], scaling_matrix8 [1 ], mask , 10 ); // Intra, Cb
260- ret |= decode_scaling_list (gb , scaling_matrix8 [5 ], 64 , default_scaling8 [1 ], scaling_matrix8 [4 ], mask , 11 ); // Inter, Cb
320+ const uint8_t * default8_0_chroma = b_mars_flag ? default_scaling8_mars [1 ] : default_scaling8 [0 ]; // [PolyV] swapped
321+ const uint8_t * default8_1_chroma = b_mars_flag ? default_scaling8_mars [0 ] : default_scaling8 [1 ]; // [PolyV] swapped
322+
323+ ret |= decode_scaling_list (gb , scaling_matrix8 [1 ], 64 , default8_0_chroma , scaling_matrix8 [0 ], mask , 8 );
324+ ret |= decode_scaling_list (gb , scaling_matrix8 [4 ], 64 , default8_1_chroma , scaling_matrix8 [3 ], mask , 9 );
325+ ret |= decode_scaling_list (gb , scaling_matrix8 [2 ], 64 , default8_0_chroma , scaling_matrix8 [1 ], mask , 10 );
326+ ret |= decode_scaling_list (gb , scaling_matrix8 [5 ], 64 , default8_1_chroma , scaling_matrix8 [4 ], mask , 11 );
261327 }
262328 }
329+
263330 if (!ret )
264331 ret = is_sps ;
265332 }
@@ -301,8 +368,6 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
301368 }
302369 memcpy (sps -> data , gb -> buffer , sps -> data_size );
303370
304- sps -> b_mars_flag = 1 ; // [PolyV] always active
305-
306371 // Re-add the removed stop bit (may be used by hwaccels).
307372 if (!(gb -> size_in_bits & 7 ) && sps -> data_size < sizeof (sps -> data ))
308373 sps -> data [sps -> data_size ++ ] = 0x80 ;
@@ -441,7 +506,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
441506 goto fail ;
442507 }
443508
444- sps -> ref_frame_count = get_ue_golomb_31 (gb ) + 4 ; // [PolyV] Added + 4
509+ sps -> ref_frame_count = get_ue_golomb_31 (gb ) - 4 ; // [PolyV] Added - 4
445510 if (avctx -> codec_tag == MKTAG ('S' , 'M' , 'V' , '2' ))
446511 sps -> ref_frame_count = FFMAX (2 , sps -> ref_frame_count );
447512 if (sps -> ref_frame_count > H264_MAX_DPB_FRAMES ) {
@@ -802,10 +867,10 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct
802867 bits_left = bit_length - get_bits_count (gb );
803868 if (bits_left > 0 && more_rbsp_data_in_pps (sps , avctx )) {
804869 pps -> transform_8x8_mode = get_bits1 (gb );
805- //pps->pic_scaling_matrix_present_flag = get_bits1(gb); // [PolyV] ignored
806870
807- if (sps -> b_mars_flag == 1 ) // [PolyV] added
808- get_bits (gb , 2 );
871+ get_bits (gb , 2 ); // [PolyV] added
872+
873+ pps -> pic_scaling_matrix_present_flag = get_bits1 (gb ); // [PolyV] moved (read inside decode_scaling_matrices in the binary)
809874
810875 ret = decode_scaling_matrices (gb , sps , pps , 0 ,
811876 pps -> pic_scaling_matrix_present_flag ,
@@ -814,8 +879,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct
814879 if (ret < 0 )
815880 goto fail ;
816881
817- if (sps -> b_mars_flag == 1 ) // [PolyV] added
818- get_se_golomb (gb );
882+ get_se_golomb (gb ); // [PolyV] added
819883
820884 // second_chroma_qp_index_offset
821885 pps -> chroma_qp_index_offset [1 ] = get_se_golomb (gb );
0 commit comments