@@ -385,8 +385,20 @@ remove_epb_from_sei_payload(bu_info_t *bu)
385385 bu -> with_epb = (bu -> reserved_byte & 0x80 ); // Hash with emulation prevention bytes
386386 bu -> is_golden_sei = (bu -> reserved_byte & 0x40 ); // The BU is a golden SEI.
387387
388- if (bu -> emulation_prevention_bytes <= 0 ) return ;
388+ // Check if the SEI is signed, i.e., a signature TLV tag exists. This is only possible
389+ // if TLV data size (computed from the payload size) fit within the total data size.
390+ const uint8_t * signature_ptr = NULL ;
391+ if (bu -> hashable_data_size > bu -> tlv_size ) {
392+ bool has_epb = bu -> emulation_prevention_bytes > 0 ;
393+ signature_ptr = sv_tlv_find_tag (bu -> tlv_data , bu -> tlv_size , SIGNATURE_TAG , has_epb );
394+ }
395+ bu -> is_signed = (signature_ptr != NULL );
396+ // If the SEI is signed the hashable part of the SEI ends at the signature tag.
397+ if (bu -> is_signed ) {
398+ bu -> hashable_data_size = signature_ptr - bu -> hashable_data ;
399+ }
389400
401+ if (bu -> emulation_prevention_bytes <= 0 ) return ;
390402 // We need to read byte by byte to a new memory and remove any emulation prevention bytes.
391403 uint16_t last_two_bytes = LAST_TWO_BYTES_INIT_VALUE ;
392404 // Complete data size including stop bit (byte). Note that |payload_size| excludes the final byte
@@ -407,10 +419,17 @@ remove_epb_from_sei_payload(bu_info_t *bu)
407419 // Point |tlv_data| to the first byte of the TLV part in |nalu_data_wo_epb|.
408420 bu -> tlv_data = & bu -> nalu_data_wo_epb [data_size - bu -> payload_size + UUID_LEN ];
409421 if (!bu -> with_epb ) {
422+ // Find the signature tag again, but now after emulation prevention has been
423+ // removed, to get the correct size of the hashable part.
424+ signature_ptr = sv_tlv_find_tag (bu -> tlv_data , bu -> tlv_size , SIGNATURE_TAG , false);
410425 // If the SEI was hashed before applying emulation prevention, update |hashable_data|.
411426 bu -> hashable_data = bu -> nalu_data_wo_epb ;
412- bu -> hashable_data_size = data_size ;
413427 bu -> tlv_start_in_bu_data = bu -> tlv_data ;
428+ if (bu -> is_signed ) {
429+ bu -> hashable_data_size = signature_ptr - bu -> hashable_data ;
430+ } else {
431+ bu -> hashable_data_size = data_size ;
432+ }
414433 }
415434 }
416435}
@@ -560,14 +579,6 @@ parse_bu_info(const uint8_t *bu_data,
560579 }
561580
562581 remove_epb_from_sei_payload (& bu );
563- if (bu .emulation_prevention_bytes >= 0 && (bu .hashable_data_size > bu .tlv_size )) {
564- // Check if a signature TLV tag exists. If number of computed emulation prevention
565- // bytes is negative, either the SEI is currupt or incomplete. Or if there is enough
566- // TLV data.
567- const uint8_t * signature_tag =
568- sv_tlv_find_tag (bu .tlv_data , bu .tlv_size , SIGNATURE_TAG , false);
569- bu .is_signed = (signature_tag != NULL );
570- }
571582 // Update |is_hashable| w.r.t. signed or not.
572583 bu .is_hashable |= bu .is_sv_sei && !bu .is_signed ;
573584 }
0 commit comments