3

There is nice one for java - MINA.

Once I've heard that there is something similar for python. But can't remind.

EDIT: to be more specific, I would like to have a tool which would help me to create a coded for some binary stream.

EDIT2: I'd like to list solutions here (thanks Scott for related topics) Listed in order i'd use it.

bua
  • 4,761
  • 1
  • 26
  • 32
  • Some possibly related questions: http://stackoverflow.com/questions/39663/ http://stackoverflow.com/questions/967652 – Scott Griffiths Aug 18 '10 at 11:19
  • Another related question: https://stackoverflow.com/questions/3753589/packing-and-unpacking-variable-length-array-string-using-the-struct-module-in-py/38762935#38762935 – Victor Sergienko Dec 08 '20 at 23:59

2 Answers2

5

python has pack/unpack in the standard lib that can be used to interpret binary data and map them to structs

see "11.3. Working with Binary Data Record Layouts" here http://docs.python.org/tutorial/stdlib2.html

or here http://docs.python.org/library/struct.html

Nikolaus Gradwohl
  • 19,708
  • 3
  • 45
  • 61
5

Have you tried the bitstring module? (Full disclosure: I wrote it).

It's designed to make constructing and parsing binary data as simple as possible. Take a look at a few examples to see if it's anything like you need.

This snippet does some parsing of a H.264 video file:

    from bitstring import ConstBitStream
    s = ConstBitStream(filename='somefile.h264')
    profile_idc = s.read('uint:8')
    # Multiple reads in one go returns a list:
    constraint_flags = s.readlist('4*uint:1')
    reserved_zero_4bits = s.read('bin:4')
    level_idc = s.read('uint:8')
    seq_parameter_set_id = s.read('ue')
    if profile_idc in [100, 110, 122, 244, 44, 83, 86]:
        chroma_format_idc = s.read('ue')
        if chroma_format_idc == 3:
            separate_colour_plane_flag = s.read('uint:1')
        bit_depth_luma_minus8 = s.read('ue')
        bit_depth_chroma_minus8 = s.read('ue')
        ...
Scott Griffiths
  • 21,438
  • 8
  • 55
  • 85
  • Looks like an interesting library. I'm going to have a more extensive play with it when I find some time. – Andrew Aug 19 '10 at 07:23
  • Actually, the bitstream should be split into NAL units prefixed with with `0x000001` or `0x00000001` start codes. Your code shows how to parse an SPS directly. How would I translate [this](http://stackoverflow.com/questions/10946496/h264-reference-frames) with your library? – slhck Mar 27 '15 at 14:45
  • @slhck: Well start by searching for a byte aligned 0x000001. `s.find('0x000001', bytealigned=True)`. But I couldn't really write a full H.264 decoder in a S.O. answer so it was only meant to be illustrative. – Scott Griffiths Mar 27 '15 at 16:10
  • Sure :) thanks, I found what I needed. What I meant to say is that I think the code as presented will probably not work because there should be at least a 0x00 00 00 prefix in the file at the very beginning. – slhck Mar 27 '15 at 17:38