Skip to content

Instantly share code, notes, and snippets.

@kalidasya
Created June 25, 2019 07:54
Show Gist options
  • Save kalidasya/f7ded118a8145b6f47a441bfc780de50 to your computer and use it in GitHub Desktop.
Save kalidasya/f7ded118a8145b6f47a441bfc780de50 to your computer and use it in GitHub Desktop.
katai mpeg2 video prototype
meta:
id: mpeg2_video
file-extension: mpeg
endian: le
seq:
- id: sequences
type: sequence
repeat: until
repeat-until: _io.size - _io.pos < 4
types:
sequence:
seq:
- id: start_code
type: start_code
if: _io.size - _io.pos > 4
- id: data
type:
switch-on: start_code.start_code
cases:
0x00: picture_header
0xb2: user_data
0xb3: sequence_header
0xb5: extension_data
_: ignored_seq
if: _io.size - _io.pos > 4
ignored_seq:
seq:
- id: ignore
type: rest
rest:
seq:
- id: data
type: magic
repeat: until
# next line is ugly. next to the generic exit condition (prefix is 1 or eos reached)
# we have to make sure we call the next_start_code for each magic otherwise it will not be available later
# but we cannot do it when the eos was close so the data was not read
repeat-until: (_.prefix_code == 1 or _io.size - _io.pos < 4) and (_io.size - _io.pos < 4 or _.next_start_code > -1)
instances:
next_start_code:
value: data.last.next_start_code
start_code:
seq:
- id: sync
contents: [0x00, 0x00, 0x01]
- id: start_code
type: u1
magic:
seq:
- id: stuff
#contents: [0]
type: u1
instances:
prefix_code:
type: b24
pos: _io.pos
if: _io.size - _io.pos > 4
next_start_code:
type: u1
io: _root._io
pos: _io.pos + 3
if: _io.size - _io.pos > 4
sequence_header:
seq:
- id: horiz_size
type: b12
- id: vert_size
type: b12
- id: aspect_ratio_information
type: b4
- id: framerate_code
type: b4
- id: bitrate
type: b18
- id: marker_bit
# contents: [true]
type: b1
- id: vbv_buffer_size
type: b10
- id: constrained_param_flag
# contents: false
type: b1
- id: load_intra_quantiser_matrix
type: b1
- id: intra_quantiser_matrix
type: b8
repeat: expr
repeat-expr: 64
if: load_intra_quantiser_matrix
- id: load_non_intra_quantiser_matrix
type: b1
- id: non_intra_quantiser_matrix
type: b8
repeat: expr
repeat-expr: 64
if: load_non_intra_quantiser_matrix
## not sure if we can just ignore it is a b5 already
#- id: ignore
# type: rest
#- id: extension
# type: extension_and_user_data(ignore.next_start_code, 0)
## what we ignore here might be the next sequence?
#- id: ignore2
# type: rest
##- id: picture_or_group_start
## type: start_code
#- id: picture_or_group
# type:
# switch-on: ignore2.next_start_code
# cases:
# 0x00: picture
# 0xb8: group
instances:
framerate_fraction:
value: >-
framerate_code == 0 ? 0 / 1.0 :
framerate_code == 1 ? 24000 / 1001.0 :
framerate_code == 2 ? 24 / 1.0 :
framerate_code == 3 ? 25 / 1.0 :
framerate_code == 4 ? 30000 / 1001.0 :
framerate_code == 5 ? 30 / 1.0 :
framerate_code == 6 ? 50 / 1.0 :
framerate_code == 7 ? 60000 / 1001.0 :
framerate_code == 8 ? 60 / 1.0 : 0
picture_header:
seq:
- id: temporal_reference
type: b10
- id: picture_coding_type
type: b3
- id: vbv_delay
type: b16
# picture_coding_type cannot be 0
- id: full_pel_forward_vector
type: b1
if: picture_coding_type == 2 or picture_coding_type == 3
- id: forward_f_code
type: b3
if: picture_coding_type == 2 or picture_coding_type == 3
- id: full_pel_backward_vector
type: b1
if: picture_coding_type == 3
- id: backward_f_code
type: b3
if: picture_coding_type == 3
- id: byte_align
size: 0
- id: ignore
type: rest
#- id: ignore
# type: b3
# todo no easy way to read this structure
#- id: extra_information
# type: extra_information
# repeat: until
# repeat-until: not _.extra_bit
#- id: ignore
# type: rest
# lets ignore the rest for now
extra_information:
seq:
- id: extra_bit
type: b1
- id: extra_information_picture
type: b8
if: extra_bit
picture:
seq:
- id: start_code
type: start_code
- id: header
type: picture_header
#- id: extension_data
# type: extension_and_user_data(start_code.start_code, 2)
- id: ignore
type: rest
group:
seq:
- id: start_code
type: start_code
#- id: extension_data
# type: extension_and_user_data(start_code.start_code, 1)
- id: ignore
type: rest
#extension_and_user_data:
# params:
# - id: start_code
# type: u1
# - id: i
# type: u1
# seq:
# - id: sequence
# type: extension_n_user_data_seq(i)
# repeat: until
# repeat-until: _.rest.next_start_code != 0xb5 and _.rest.next_start_code != 0xb2
# if: start_code == 0xb5 or start_code == 0xb2
# #eos-error: false
#extension_n_user_data_seq:
# params:
# - id: i
# type: u1
# seq:
# - id: start_code
# type: start_code
# - id: extension_data
# type: extension_data(i)
# if: i != 1 and start_code.start_code == 0xb5
# - id: user_data
# type: user_data
# if: start_code.start_code == 0xb2
# # we might ignore a whole new seq here
# - id: rest
# type: rest
user_data:
seq:
- id: data
type: b48
repeat: until
repeat-until: _ == 0x000001
extension_data:
seq:
- id: start_code_id
type: b4
# byte aligned called if we do this we need to parse in this level
#- id: data
# type:
# switch-on: start_code_id
# cases:
# 0b0001: sequence_extension
# 0b0010: sequence_display_extension
# 0b1000: picture_coding_extension
- id: profile_and_level
type: b8
if: start_code_id == 0b0001
- id: progressive_sequence
type: b1
if: start_code_id == 0b0001
- id: chroma_format
type: b2
if: start_code_id == 0b0001
- id: horizontal_size_extension
type: b2
if: start_code_id == 0b0001
- id: vertical_size_extension
type: b2
if: start_code_id == 0b0001
- id: bitrate_extension
type: b12
if: start_code_id == 0b0001
- id: marker_bit
type: b1
if: start_code_id == 0b0001
- id: vbv_buffer_size_extension
type: b8
if: start_code_id == 0b0001
- id: low_delay
type: b1
if: start_code_id == 0b0001
- id: framerate_extension_n
type: b2
if: start_code_id == 0b0001
- id: framerate_extension_d
type: b5
if: start_code_id == 0b0001
- id: forward_horizontal
type: b4
if: start_code_id == 0b1000
- id: forward_vertical
type: b4
if: start_code_id == 0b1000
- id: backward_horizontal
type: b4
if: start_code_id == 0b1000
- id: backward_vertical
type: b4
if: start_code_id == 0b1000
- id: intra_dc_precision
type: b2
if: start_code_id == 0b1000
- id: picture_structure
type: b2
if: start_code_id == 0b1000
- id: things # todo later
type: b8
if: start_code_id == 0b1000
- id: progressive_frame
type: b1
if: start_code_id == 0b1000
- id: composite_display
type: b1
if: start_code_id == 0b1000
- id: stuffing
type: b6
if: start_code_id == 0b1000 and not composite_display
- id: things2 # todo later
type: b22
if: start_code_id == 0b1000 and composite_display
- id: video_format
type: b3
if: start_code_id == 0b0010
- id: colour_description
type: b1
if: start_code_id == 0b0010
- id: color_primaries
type: b8
if: start_code_id == 0b0010 and colour_description
- id: transfer_characteristics
type: b8
if: start_code_id == 0b0010 and colour_description
- id: matrix_coefficients
type: b8
if: start_code_id == 0b0010 and colour_description
- id: display_horizontal_size
type: b14
if: start_code_id == 0b0010
- id: marker_bit2
type: b1
if: start_code_id == 0b0010
- id: display_vertical_size
type: b14
if: start_code_id == 0b0010
- id: ignore
type: b3
if: start_code_id == 0b0010
# byte aligned called if we do this we need to parse in this level
#- id: data
# type:
# switch-on: start_code_id
# cases:
# 0b0001: sequence_extension
# 0b0010: sequence_display_extension
# 0b1000: picture_coding_extension
#sequence_extension:
# seq:
# - id: profile_and_level
# type: b8
# - id: progressive_sequence
# type: b1
# - id: chroma_format
# type: b2
# - id: horizontal_size_extension
# type: b2
# - id: vertical_size_extension
# type: b2
# - id: bitrate_extension
# type: b12
# - id: marker_bit
# type: b1
# - id: vbv_buffer_size_extension
# type: b8
# - id: low_delay
# type: b1
# - id: framerate_extension_n
# type: b2
# - id: framerate_extension_d
# type: b5
#picture_coding_extension:
# seq:
# - id: forward_horizontal
# type: b4
# - id: forward_vertical
# type: b4
# - id: backward_horizontal
# type: b4
# - id: backward_vertical
# type: b4
# - id: intra_dc_precision
# type: b2
# - id: picture_structure
# type: b2
# - id: things # todo later
# type: b8
# - id: progressive_frame
# type: b1
# - id: composite_display
# type: b1
# - id: stuffing
# type: b6
# if: not composite_display
# - id: things2 # todo later
# type: b22
# if: composite_display
#sequence_display_extension:
# seq:
# - id: video_format
# type: b3
# - id: colour_description
# type: b1
# - id: color_primaries
# type: u1
# if: colour_description
# - id: transfer_characteristics
# type: u1
# if: colour_description
# - id: matrix_coefficients
# type: u1
# if: colour_description
# - id: display_horizontal_size
# type: b14
# - id: marker_bit
# type: b1
# - id: display_vertical_size
# type: b14
# - id: ignore
# type: b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment