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.

2

2 Answers 2

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')
        ...
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like an interesting library. I'm going to have a more extensive play with it when I find some time.
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 with your library?
@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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.