Python Forum
output mismatching when porting a python from python2 env to python3 env
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output mismatching when porting a python from python2 env to python3 env
#1
I am porting a python code which was written in python2 into the latest python3.8. I made some minor changes in syntax without changing the main logic such that it could run in the python3 environment without error. Both codes running correctly but the outputs are a little bit different from each other.

I thought I have made some mistakes that is why python3 code giving a different output. I have tried my best but I didn't find any mistake by myself.

I have posted below both python2 and python3 code. My humble request to please help me to find my error in python3 code.

Python2 code
    
import json

capture_settings= [{"framerate": 30, "width": 1280, "height": 720, "bitrate": 17000000, "overlay": False, "gop_size": 30}]

SEI_UUID_CAMERA_SETTINGS = bytearray(b'\x08\x94\xfc\xa2\x58\xce\x45\x02\x8f\x18\xc0\x8c\x68\xe5\x32\x35')
#print(len(SEI_UUID_CAMERA_SETTINGS))
#""" encode values >= 255 as 0xff0xff..0xresidual """
def encode_multibyte_value(value):
    """ encode values >= 255 as 0xff0xff..0xresidual """

    encoded = bytearray()

    while (value >= 255):
        encoded += bytearray(chr(255))
        value -= 255

    encoded += bytearray(chr(value))

    return encoded

def escape_bytearray(input):
    """ escape 000 to 0030, 001 to 0031, 002 to 0032 and 003 to 0033 """
    output = bytearray()

    history1 = None
    history2 = None

    for b in input:
        if (history1==0) and (history2==0) and (b <= 3):
            output += chr(3)
            history1 = 3
            history2 = b
        else:
            history1 = history2
            history2 = b

        output += chr(b)    

    return output


def create_sei_nal_unit(uuid, payload_string):
    """ create a 'user data unregistered' SEI nal unit in a bytearray """

    assert(bytearray == type(uuid))
    print(uuid)

    uuid_length = len(uuid)
    assert(16 == uuid_length)   

    nal_unit_prefix = bytearray(b'\x00\x00\x00\x01')
    nal_unit_type = bytearray(chr(6))                   # 6 = SEI

    encoded_payload_type = encode_multibyte_value(5)    # 5 = 'user data unregistered'

    payload = bytearray(payload_string)

    encoded_payload_size = encode_multibyte_value(uuid_length + len(payload))
    print(uuid + payload)
    escaped_payload = escape_bytearray(uuid + payload)


    trailing_bits = bytearray(b'\x80')

    sei_nal_unit = ( nal_unit_prefix
                   + nal_unit_type
                   + encoded_payload_type
                   + encoded_payload_size
                   + escaped_payload
                   + trailing_bits )

    return sei_nal_unit

sei_nal_unit = create_sei_nal_unit(SEI_UUID_CAMERA_SETTINGS, json.dumps(capture_settings))
print(sei_nal_unit)
Output:
Quote:x���X�E���h�25[{"overlay": false, "gop_size": 30, "framerate": 30, "height": 720, "width": 1280, "bitrate": 17000000}]�

Python3 Code:

 
import json


capture_settings= [{"framerate": 30, "width": 1280, "height": 720, "bitrate": 17000000, "overlay": False, "gop_size": 30}]

SEI_UUID_CAMERA_SETTINGS = b'\x08\x94\xfc\xa2\x58\xce\x45\x02\x8f\x18\xc0\x8c\x68\xe5\x32\x35'
#print(len(SEI_UUID_CAMERA_SETTINGS))
#""" encode values >= 255 as 0xff0xff..0xresidual """
def encode_multibyte_value(value):
    """ encode values >= 255 as 0xff0xff..0xresidual """

    encoded = bytes()

    while (value >= 255):
        encoded += bytes(chr(255).encode('utf-8'))
        value -= 255

    encoded += bytes(chr(value).encode('utf-8'))

    return encoded

def escape_bytearray(input):
    """ escape 000 to 0030, 001 to 0031, 002 to 0032 and 003 to 0033 """
    output = bytes()

    history1 = None
    history2 = None

    for b in input:
        if (history1==0) and (history2==0) and (b <= 3):
            output += b'\x03'
            history1 = 3
            history2 = b
        else:
            history1 = history2
            history2 = b

        output += chr(b).encode('utf-8')    

    return output


def create_sei_nal_unit(uuid, payload_string):
    """ create a 'user data unregistered' SEI nal unit in a bytearray """

    assert(bytes == type(uuid))

    uuid_length = len(uuid)
    assert(16 == uuid_length)   

    nal_unit_prefix = b'\x00\x00\x00\x01'
    nal_unit_type = b'\x06'                  # 6 = SEI
    encoded_payload_type = encode_multibyte_value(5)    # 5 = 'user data unregistered'

    payload = bytes(payload_string)

    encoded_payload_size = encode_multibyte_value(uuid_length + len(payload))
    escaped_payload = escape_bytearray(uuid + payload)


    trailing_bits = b'\x80'

    sei_nal_unit = ( nal_unit_prefix
                   + nal_unit_type
                   + encoded_payload_type
                   + encoded_payload_size
                   + escaped_payload
                   + trailing_bits )

    return sei_nal_unit

sei_nal_unit = create_sei_nal_unit(SEI_UUID_CAMERA_SETTINGS, json.dumps(capture_settings).encode('utf-8'))
print(sei_nal_unit)
Output:
Quote:b'\x00\x00\x00\x01\x06\x05x\x08\xc2\x94\xc3\xbc\xc2\xa2X\xc3\x8eE\x02\xc2\x8f\x18\xc3\x80\xc2\x8ch\xc3\xa525[{"framerate": 30, "width": 1280, "height": 720, "bitrate": 17000000, "overlay": false, "gop_size": 30}]\x80'
Reply
#2
It would be most helpful to explain the process, inputs, process, outputs
Reply
#3
(Jan-20-2020, 04:37 PM)Larz60+ Wrote: It would be most helpful to explain the process, inputs, process, outputs

The input of this code is "camera setting" and "capture option" which is mentioned in question this code is create a Nal_Unit for h264 codec in byte stream.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Migrating of python2 script to python3 zuri 7 957 Oct-05-2023, 02:40 PM
Last Post: snippsat
  Migration of Python2 and Python3 using Modernize and Future Rakshan 5 2,501 Oct-05-2023, 08:55 AM
Last Post: zuri
  Help with porting to python3 mason28 3 5,278 Jan-23-2021, 02:48 AM
Last Post: Larz60+
  python2 python3 messed up : How to fix ? hary 15 7,984 Dec-30-2020, 08:26 PM
Last Post: hary
  Getting a small Python2 prog to run in Python3 steve140 4 3,854 Apr-19-2020, 09:27 AM
Last Post: steve140
  Porting Applesoft BASIC "DECIDE" to Python RobinHood2020 0 1,599 Mar-29-2020, 06:55 PM
Last Post: RobinHood2020
  json.dumps output error in python3 prayuktibid 2 2,643 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. PythonNmap 21 10,851 Jan-19-2020, 07:54 PM
Last Post: PythonNmap
  python3 decoding problem but python2 OK mesbah 0 1,800 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,889 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020