zin*_*non 6 bitstream h.265 hevc
有没有办法解析HEVC比特流文件?
我希望能够创建一个新的比特流文件,该文件具有从原始比特流文件中选择的所选nal单元分组.
编辑:我插入了我的代码.请在这里找到我的比特流文件.
#library for searching in a string
import re
#library to keep dictionary order
import collections
import bitstring
from bitstring import BitStream, BitArray, ConstBitStream, pack
from bitstring import ByteStore, offsetcopy
#read bitstream file
s = BitStream(filename='11LTCCA_560x416_50Hz_8b_P420_GOP8_IP48_200frms_QP28.HEVC.str')
#find no of packets
pcks = list(s.findall('0x000001', bytealigned=True))
print len(pcks)
#set the current position, in the beginning of the nal unit.
s.pos =pcks[0]-8
print s.pos
#find the number of bits of first nal packet
no_p = pcks[1]-pcks[0]
forbidden_zero_bit = s.read(1)
nal_unit_type = s.read('uint:6')
# go to the beginning of the second nal unit
s.read(no_p)
# print nal unit type of the 1st packet
print nal_unit_type
no_p = pcks[2]-pcks[1]
s.pos = pcks[1]-8
print s.pos
forbidden_zero_bit = s.read(1)
nal_unit_type = s.read('uint:6')
s.read(no_p)
print nal_unit_type
Run Code Online (Sandbox Code Playgroud)
如果你想做的只是采取一些nal单位数据包(例如,取决于图层ID和时间ID),你不需要修改VPS,SPS,PPS,切片标题等,那么你也可以轻松实现这个自己:
相应的语法在HEVC标准的附件B"字节流格式"中陈述.
简而言之:
在比特流文件中搜索模式0x000001,它将所有nal单元分开.另外,如果下一个nal单元是访问单元的第一个nal单元(访问单元=用于解码整个帧的所有nal单元),则在该模式之前可以有0x00字节.
根据HEVC标准的 7.3.1.2节读取最终单元标题,并根据您想要的标准保留/删除最终单位.确保保留参数集(根据HEVC标准的表7-1,nal单元类型32,33和34 ).
将所有nal单元组装到一个新文件中,并确保中间始终有0x000001序列.
我曾经使用Python做过类似的事情,效果非常好.如果您想更简单地阅读nal单元标题,请使用bitstring模块.如果你想这样做并有更详细的问题,如果你愿意,你可以向我寻求帮助.
编辑:
关于你发布的代码:为什么在BitStream对象(s.pos =pcks[0]-8和s.pos = pcks[1]-8)中分配位置时放"-8" ?这应该是+24(24位= 3字节=最小单位分隔符0x000001的长度),在分隔符后开始读取以获得nal单位.但是,在读取数据时必须考虑到这一点:no_p = pcks[1]-pcks[0]应该是no_p = pcks[1]-pcks[0]-24,因为你在nal单位分隔符之后开始阅读.
如果你感到困惑,第一个找到的位置(pcks[0])是8,而不是0:在每个nal单位分隔符之前,根据HEVC标准的附录B,可以有任意数量的零字节.通常,每个访问单元之前总是有一个零字节.