Skip to main content

Extract NAL units from H.264 bitstreams

Project description

h26x-extractor

PyPI version

Author: Werner Robitza, with contributions from @chemag, Paulo Sherring.

Extracts NAL units from H.264 bitstreams and decodes their type and content, if supported.

⚠️ h26x-extractor is neither fast nor robust to bitstream errors. It's rather a playground for parsing bitstreams. Use with caution!

Installation

Requirements: Python 3.5 or higher

Via pip:

pip3 install h26x-extractor

Status

Currently supported:

  • Parsing of H.264 bitstreams
  • Parsing of NALU
  • Parsing of AUD
  • Parsing of CodedSliceIDR
  • Parsing of CodedSliceNonIDR
  • Parsing of SPS
  • Parsing of PPS

Currently planned:

  • Parsing of SEI
  • Parsing of VUI
  • Parsing of H.265 bitstreams

Usage

If you installed the program via pip, you can run it directly:

h26x-extractor [options] <input-file>...

Otherwise you can clone this repo and run it via:

python3 -m h264_extractor [options] <input-file>...

You can pass the -v flag to enable verbose output, e.g. the following. You will get, for each NAL unit:

  • The byte position range
  • The offset from the start of the stream
  • The overall length including start code
  • The type (also translated in plaintext)
  • Its content in raw bytes, encoded as hex
  • Its RBSP content
  • A table with its content decoded, if supported

Example:

NALU bytepos:   [0, 28]
NALU offset:    0 Bytes
NALU length:    29 Bytes (including start code)
NALU type:      7 (Sequence parameter set)
NALU bytes:     0x0000000167f4000d919b28283f6022000003000200000300641e28532c
NALU RBSP:      0xf4000d919b28283f602200000002000000641e28532c

SPS (payload size: 22.0 Bytes)
+--------------------------------------+---------+
| field                                | value   |
+======================================+=========+
| constraint_set0_flag                 | 0       |
+--------------------------------------+---------+
| constraint_set1_flag                 | 0       |
+--------------------------------------+---------+
....

Programmatic usage

You can also use this library in your code, e.g.:

from h26x_extractor.h26x_parser import H26xParser

def do_something(bytes):
    pass
    # do something with the NALU bytes

H26xParser.set_callback("nalu", do_something)
H26xParser.parse()

The callback is called for each type of info found. Valid callbacks are:

  • sps
  • pps
  • slice
  • aud
  • nalu

The raw data for all callbacks includes the RBSP.

You can also call the nalutypes classes to decode the individual fields, e.g. nalutypes.SPS:

from h26x_extractor.h26x_parser import H26xParser
from h26x_extractor.nalutypes import SPS

def parse_sps(bytes):
    sps = SPS(bytes)
    sps.print_verbose()

H26xParser.set_callback("sps", parse_sps)
H26xParser.parse()

Alternatives

h264bitstream is a proper H.264 parser.

FFmpeg can also parse bitstream data:

ffmpeg -i video.h264 -c copy -bsf:v trace_headers -f null - 2> output.txt

License

The MIT License (MIT)

Copyright (c) 2017-2021 Werner Robitza

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Changelog

v0.8.0 (2021-11-12)

  • Bump version to 0.8.0.

  • Remove unneeded imports.

  • Rename test dir.

  • Apply formatting with black.

  • Parse as bytearray instead of BitStream.

  • Update copyright year.

  • Add requirements.txt.

  • Update badge link.

  • Update README.

v0.7.0 (2021-03-10)

  • Bump version to 0.7.0.

  • Add python_requires to setup.py.

  • Remove release script.

v0.6 (2021-03-06)

  • Bump version to 0.6.

  • Switch version to string.

  • Format setup.py and switch to markdown.

  • Update badge URL.

v0.5 (2020-12-30)

  • Bump version to 0.5.

  • Apply formatting.

  • Rm python 2.x support, README fixes.

  • Fix frame_crop_left_offset typo (#5)

  • Formatting.

  • Import local package for tests.

  • Add pypi badge.

  • Add missing gitchangelog template.

v0.4 (2020-04-12)

  • Bump version to 0.4.

  • Update release script.

  • Update readme.

  • H26x_extractor: add deeper slice_header parsing (#4)

    Includes:

    • passing the SPS and PPS NALUs to CodedSliceIDR and CodedSliceNonIDR (which need them to go further)
    • adding an optional concept of order in the verbose printing, so that NALU parameters are printed in the order they are parsed
    • implementing ordering in SPS, PPS, and CodedSliceIDR NALUs
    • fix on the SPS parser based on profile_idc values, per 2016-02 standard (Section 7.3.2.1.1, page 44). Note in particular that the if loop does not include value 144
    • fix minor type (s/slice_gropu_change_rate_minus1/slice_group_change_rate_minus1/)

    Tested:

    $ h26x-extractor -v file.264
    ...
    SPS (payload size: 21.0 Bytes)
    +--------------------------------------+---------+
    | field                                | value   |
    +======================================+=========+
    | profile_idc                          | 66      |
    +--------------------------------------+---------+
    | constraint_set0_flag                 | 1       |
    +--------------------------------------+---------+
    | constraint_set1_flag                 | 1       |
    +--------------------------------------+---------+
    | constraint_set2_flag                 | 0       |
    +--------------------------------------+---------+
    | constraint_set3_flag                 | 0       |
    +--------------------------------------+---------+
    ...
    NALU type:  5 (Coded slice of an IDR picture)
    NALU bytes: 0x000000016588841afffffc2f14000416fd78e06380de6a1306bb224a722233e54ffa7cb9526c188ed1189699e7c6fd1a2307f757de5c3fca2f3d22b7fc667ccb6ff6b6a8dabb515b59fe53f8a7d83a8beb6ff17988adbfde818bdf2dafc5e7a2b5fde4a0bca4156137f8ceadff19bd5bfe233e152fbefbefbe2fab6dfe...
    NALU RBSP:  0x88841afffffc2f14000416fd78e06380de6a1306bb224a722233e54ffa7cb9526c188ed1189699e7c6fd1a2307f757de5c3fca2f3d22b7fc667ccb6ff6b6a8dabb515b59fe53f8a7d83a8beb6ff17988adbfde818bdf2dafc5e7a2b5fde4a0bca4156137f8ceadff19bd5bfe233e152fbefbefbe2fab6dfed6d62d6ad7...
    
    CodedSliceIDR (payload size: 4538.0 Bytes)
    +----------------------+---------+
    | field                | value   |
    +======================+=========+
    | first_mb_in_slice    | 0       |
    +----------------------+---------+
    | slice_type           | 7       |
    +----------------------+---------+
    | slice_type_clear     | I       |
    +----------------------+---------+
    | pic_parameter_set_id | 0       |
    +----------------------+---------+
    | frame_num            | 0       |
    +----------------------+---------+
    | idr_pic_id           | 0       |
    ...
    
  • Fix offset-by-one (#3)

    • h26x-extractor: add a simple parsing test

    Needed to refactor the parser constructor in order to allow testing binary blobs.

    Tested:

    Before the AUD fix patch:

    $ ./tests/simple_parsing.py
    
    ========================================================================================================
    
    NALU bytepos: [0, 5]
    NALU offset:  0 Bytes
    NALU length:  6 Bytes (including start code)
    NALU type:  9 (Access unit delimiter)
    NALU bytes: 0x000000010910
    NALU RBSP:
    
    E
    ======================================================================
    ERROR: testAUDParser (__main__.ParsingTest)
    Simple AUD parsing.
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "./tests/simple_parsing.py", line 17, in testAUDParser
        ex.parse()
      File "/usr/local/lib/python3.7/site-packages/h26x_extractor-0.3-py3.7.egg/h26x_extractor/h26x_parser.py", line 209, in parse
        aud = nalutypes.AUD(rbsp_payload, self.verbose)
      File "/usr/local/lib/python3.7/site-packages/h26x_extractor-0.3-py3.7.egg/h26x_extractor/nalutypes.py", line 120, in __init__
        self.primary_pic_type = self.s.read('uint:3')
      File "/usr/lib/python3.7/site-packages/bitstring.py", line 3902, in read
        value, self._pos = self._readtoken(name, self._pos, length)
      File "/usr/lib/python3.7/site-packages/bitstring.py", line 2016, in _readtoken
        "Tried to read {0} bits when only {1} available.".format(int(length), self.length - pos))
    bitstring.ReadError: Reading off the end of the data. Tried to read 3 bits when only 0 available.
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.003s
    
    FAILED (errors=1)
    

    After the AUD fix patch:

    $ ./tests/simple_parsing.py
    
    ========================================================================================================
    
    NALU bytepos: [0, 5]
    NALU offset:  0 Bytes
    NALU length:  6 Bytes (including start code)
    NALU type:  9 (Access unit delimiter)
    NALU bytes: 0x000000010910
    NALU RBSP:  0x10
    
    AUD (payload size: 1.0 Bytes)
    +------------------+---------+
    | field            |   value |
    +==================+=========+
    | primary_pic_type |       0 |
    +------------------+---------+
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    OK
    
    • h26x_extractor: fix offset-by-1 in NALU parser

    Tested:

    Before the AUD fix patch:

    $ ./tests/simple_parsing.py
    
    ========================================================================================================
    
    NALU bytepos: [0, 5]
    NALU offset:  0 Bytes
    NALU length:  6 Bytes (including start code)
    NALU type:  9 (Access unit delimiter)
    NALU bytes: 0x000000010910
    NALU RBSP:
    
    E
    ======================================================================
    ERROR: testAUDParser (__main__.ParsingTest)
    Simple AUD parsing.
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "./tests/simple_parsing.py", line 17, in testAUDParser
        ex.parse()
      File "/usr/local/lib/python3.7/site-packages/h26x_extractor-0.3-py3.7.egg/h26x_extractor/h26x_parser.py", line 209, in parse
        aud = nalutypes.AUD(rbsp_payload, self.verbose)
      File "/usr/local/lib/python3.7/site-packages/h26x_extractor-0.3-py3.7.egg/h26x_extractor/nalutypes.py", line 120, in __init__
        self.primary_pic_type = self.s.read('uint:3')
      File "/usr/lib/python3.7/site-packages/bitstring.py", line 3902, in read
        value, self._pos = self._readtoken(name, self._pos, length)
      File "/usr/lib/python3.7/site-packages/bitstring.py", line 2016, in _readtoken
        "Tried to read {0} bits when only {1} available.".format(int(length), self.length - pos))
    bitstring.ReadError: Reading off the end of the data. Tried to read 3 bits when only 0 available.
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.003s
    
    FAILED (errors=1)
    

    After the AUD fix patch:

    $ ./tests/simple_parsing.py
    
    ========================================================================================================
    
    NALU bytepos: [0, 5]
    NALU offset:  0 Bytes
    NALU length:  6 Bytes (including start code)
    NALU type:  9 (Access unit delimiter)
    NALU bytes: 0x000000010910
    NALU RBSP:  0x10
    
    AUD (payload size: 1.0 Bytes)
    +------------------+---------+
    | field            |   value |
    +==================+=========+
    | primary_pic_type |       0 |
    +------------------+---------+
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    OK
    

v0.3 (2020-03-15)

  • Bump version to 0.3.

  • Add release script.

  • Python 3.7 and 3.8.

  • Rename changelog.

v0.2 (2017-08-02)

  • Many updates.

v0.1 (2017-07-17)

  • Initial commit.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

h26x-extractor-0.8.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

h26x_extractor-0.8.0-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file h26x-extractor-0.8.0.tar.gz.

File metadata

  • Download URL: h26x-extractor-0.8.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for h26x-extractor-0.8.0.tar.gz
Algorithm Hash digest
SHA256 1c0ba3a5a4e3ad9e9cad0762ac176f3f0eb0d8778292afbf20e821f1b8ff2b92
MD5 dcd07340867bfbb4463405afc3a9a9a6
BLAKE2b-256 532a3b800dbd2d4e4f3e3bcb42b4f4eb4463abe5b437295480958e42e11dc004

See more details on using hashes here.

File details

Details for the file h26x_extractor-0.8.0-py2.py3-none-any.whl.

File metadata

  • Download URL: h26x_extractor-0.8.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for h26x_extractor-0.8.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 713eda9080d3268d1591f409efb0ce1633f9ec9171570b1da455dfbc120cc5e0
MD5 6a2344f68031d07b70e8554654f948eb
BLAKE2b-256 914d6ce4a5f879521db75335b5cfebc20c4f8cf5948e85d584099bc054cecf36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page