ISO Base Media Format (video mp4) Colour Description - tonykwok/made-mistakes-again GitHub Wiki

avcC box structure

-------
+ 4
10A0D38        AVC decode (49 bytes)
10A0D38         Header (8 bytes)
10A0D38          Size:                          49 (0x00000031)
-------
+ 4
10A0D3C          Name:                          avcC
-------
+ 1
10A0D40         Version:                        1 (0x01)
-------
+ 1
10A0D41         Specific (40 bytes)
10A0D41          AVCProfileIndication:          100 (0x64)
-------
+ 1
10A0D42          profile_compatibility:         0 (0x00)
-------
+ 1
10A0D43          AVCLevelIndication:            41 (0x29)
-------
+ 1
10A0D44          reserved:                      63 (0x3F) - (6 bits)
10A0D44          lengthSizeMinusOne:            3 (0x3) - (2 bits)
-------
+ 1
10A0D45          reserved:                      7 (0x7) - (3 bits)
10A0D45          numOfSequenceParameterSets:    1 (0x01) - (5 bits)
-------
+ 2
10A0D46          seq_parameter_set (23 bytes)
10A0D46           sequenceParameterSetLength:   21 (0x0015)
-------
+ 1
10A0D48           nal_ref_idc:                  3 (0x3) - (2 bits)
10A0D48           nal_unit_type:                7 (0x7) - (5 bits)
-------
+ 1
10A0D49           profile_idc:                  100 (0x64)
-------
+ 1
10A0D4A           constraints (1 bytes)
10A0D4A            constraint_set0_flag:        No
10A0D4A            constraint_set1_flag:        No
10A0D4A            constraint_set2_flag:        No
10A0D4A            constraint_set3_flag:        No
10A0D4A            constraint_set4_flag:        No
10A0D4A            constraint_set5_flag:        No
10A0D4A            reserved_zero_2bits:         0 (0x0) - (2 bits)
-------
+ 1
10A0D4B           level_idc:                    41 (0x29) - (8 bits)
-------
+ ?
10A0D4C           seq_parameter_set_id:         0 (0x0)
10A0D4C           high profile specific (1 bytes)
10A0D4C            chroma_format_idc:           1 (0x1) - 4:2:0
10A0D4C            bit_depth_luma_minus8:       0 (0x0)
10A0D4C            bit_depth_chroma_minus8:     0 (0x0)
10A0D4C            qpprime_y_zero_transform_bypass_flag: No
10A0D4C            seq_scaling_matrix_present_flag: No
10A0D4D           log2_max_frame_num_minus4:    4 (0x4)
10A0D4D           pic_order_cnt_type:           0 (0x0)
10A0D4D           log2_max_pic_order_cnt_lsb_minus4: 4 (0x4)
10A0D4E           max_num_ref_frames:           4 (0x4)
10A0D4F           gaps_in_frame_num_value_allowed_flag: Yes
10A0D4F           pic_width_in_mbs_minus1:      67 (0x043)
10A0D50           pic_height_in_map_units_minus1: 119 (0x077)
10A0D52           frame_mbs_only_flag:          Yes
10A0D52           direct_8x8_inference_flag:    Yes
10A0D52           frame_cropping_flag (1 bytes)
10A0D52            frame_cropping_flag:         Yes
10A0D52            frame_crop_left_offset:      0 (0x0)
10A0D53            frame_crop_right_offset:     4 (0x4)
10A0D53            frame_crop_top_offset:       0 (0x0)
10A0D53            frame_crop_bottom_offset:    0 (0x0)
10A0D53           vui_parameters_present_flag (9 bytes)
10A0D53            vui_parameters_present_flag: Yes
10A0D53            aspect_ratio_info_present_flag: No
-------
+ 4
10A0D54            overscan_info_present_flag:  No                                 // 01
10A0D54            video_signal_type_present_flag (3 bytes) 
10A0D54             video_signal_type_present_flag: Yes                            // 02
10A0D54             video_format:               5 (0x5) - (3 bits) -               // 03, 04, 05 
10A0D54             video_full_range_flag:      0 (0x0) - (1 bits) - Limited       // 06
10A0D54             colour_description_present_flag (3 bytes)
10A0D54              colour_description_present_flag: Yes                          // 07 
10A0D54              colour_primaries:          6 (0x06) - (8 bits) - BT.601 NTSC  // 08, 09, 10, 11, 12, 13, 14, 15
10A0D55              transfer_characteristics:  6 (0x06) - (8 bits) - BT.601       // 16, 17, 18, 19, 20, 21, 22, 23
10A0D56              matrix_coefficients:       6 (0x06) - (8 bits) - BT.601       // 24, 25, 26, 27, 28, 29, 30, 31
10A0D57            chroma_loc_info_present_flag: No                                // 32
-------

Example 1: BT.601 NTSC
----------------------

         video_full_range_flag   
                  |
10A0D54 6A  0110 101,0  // the last bit is 0
10A0D55 0C  0000 110,0  0000 0110 -> 6 BT.601 NTSC
10A0D56 0C  0000 110,0  0000 0110 -> 6 BT.601
10A0D57 0C  0000 110,0  0000 0110 -> 6 BT.601

Example 2: BT.601 PAL
----------------------

10A0D54 6A  0110 101,0  // the last bit is 0
10A0D55 0A  0000 101,0  0000 0101 -> 5 BT.601 PAL
10A0D56 0C  0000 110,0  0000 0110 -> 6 BT.601
10A0D57 0C  0000 110,0  0000 0110 -> 6 BT.601

Example 3: BT.709
----------------------

10A0D54 6A  0110 101,0  // the last bit is 0
10A0D55 02  0000 001,0  0000 0001 -> 1 BT.709
10A0D56 02  0000 001,0  0000 0001 -> 1 BT.709
10A0D57 02  0000 001,0  0000 0001 -> 1 BT.709

colr box structure

-------
+ 4
10A0D69         Header (8 bytes)
10A0D69          Size:                          19 (0x00000013)
-------
+ 4
10A0D6D          Name:                          colr
-------
+ 4
10A0D71         Color parameter type:           nclx
-------
+ 2
10A0D75         Primaries index:                0 (0x0000) - 
-------
+ 2
10A0D77         Transfer function index:        6 (0x0006) - BT.601
-------
+ 2
10A0D79         Matrix index:                   0 (0x0000) - Identity
-------
+ 1
10A0D7B         full_range_flag:                No
-------
10A0D7C        BitRate (20 bytes)
10A0D7C         Header (8 bytes)
10A0D7C          Size:                          20 (0x00000014)
10A0D80          Name:                          btrt
10A0D84         bufferSizeDB:                   0 (0x00000000)
10A0D88         maxBitrate:                     12977087 (0x00C603BF)
10A0D8C         avgBitrate:                     12977087 (0x00C603BF)

Use ffmpeg to modify colour_description in vui_parameters to BT.709

  • H.264

ffmpeg -y -i in.mp4 -map 0 -c copy -map_metadata 0 -bsf:v h264_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 out.mp4

  • H.265

ffmpeg -y -i in.mp4 -map 0 -c copy -map_metadata 0 -bsf:v hevc_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 out.mp4

Use mediainfo to find the position of colr box in the mp4 file

mediainfo out2.mp4 --Details=1 | grep -iE -15 "colr|vui"

Use ghex to modify colr box in the mp4 file

Note: every index occupied 2 bytes

ISO definitions

https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html#//apple_ref/doc/uid/TP40000939-CH205-125526

colour_description colour_primaries transfer_characteristics matrix_coefficients
BT.601 PAL (625-line) 5 6 6
BT.601 NTSC (525-line) 6 6 6
BT.709 1 1 1

  • NTSC = SMPTE 170M = BT 601 525

  • PAL = BT 470 BG = BT 601 625

See https://video.stackexchange.com/questions/16840/ffmpeg-explicitly-tag-h-264-as-bt-601-rather-than-leaving-unspecified

⚠️ **GitHub.com Fallback** ⚠️