Skip to content
Mattias Wadman edited this page Jan 29, 2023 · 3 revisions

Function to create a ADTS frame, raw AAC frame as input, AAC configuration as $opt argument

# Usage:
# dump out samples for first track as aac lc, 44100 stereo:
# put this snippet in adts_frame.jq
# then ex:
# fq -L . 'include "adts_frame"; .tracks[0].samples[] | adts_frame({profile:2, sampling_frequency:4, channel_configuration:2})' file.mp4 > file.adts
#
# profile:
#   2: aac lc
# sampling_frequency: 
#   0x0: 96000,
#   0x1: 88200,
#   0x2: 64000,
#   0x3: 48000,
#   0x4: 44100,
#   0x5: 32000,
#   0x6: 24000,
#   0x7: 22050,
#   0x8: 16000,
#   0x9: 12000,
#   0xa: 11025,
#   0xb: 8000,
#   0xc: 7350,
# channel_configuration:
#   0: "defined in AOT Specifc Config",
#   1: "front-center",
#   2: "front-left, front-right",
#   3: "front-center, front-left, front-right",
#   4: "front-center, front-left, front-right, back-center",
#   5: "front-center, front-left, front-right, back-left, back-right",
#   6: "front-center, front-left, front-right, back-left, back-right, LFE-channel",
#   7: "front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel",
def adts_frame($opt):
  ( tobytes as $b
  | [ (0b111111111111 | tobits(12))            # syncword: 0b111111111111 (1.4)
    , (0 | tobits(1))                          # mpeg_version: "mpeg4 (0.1)
    , (0 | tobits(2))                          # layer: 0 (0.2)
    , (1 | tobits(1))                          # protection_absent: true (0.1)
    , ($opt.profile-1 | tobits(2))             # profile: "aac_lc (0.2)
    , ($opt.sampling_frequency | tobits(4))    # sampling_frequency: 44100 (0.4)
    , (0 | tobits(1))                          # private_bit: 0 (0.1)
    , ($opt.channel_configuration | tobits(3)) # channel_configuration: 2 (0.3)
    , (0 | tobits(1))                          # originality: 0 (0.1)
    , (0 | tobits(1))                          # home: 0 (0.1)
    , (0 | tobits(1))                          # copyrighted: 0 (0.1)
    , (0 | tobits(1))                          # copyright: 0 (0.1)
    , ($b.size+7 | tobits(13))                 # frame_length: 379 (1.5) +7 is header length
    , (2047 | tobits(11))                      # buffer_fullness: 2047 (1.3)
    , (0 | tobits(2))                          # number_of_rdbs: 1 (0.2)
    , $b
    ]
  | tobytes
  );
Clone this wiki locally