其他分享
首页 > 其他分享> > H.264码流结构

H.264码流结构

作者:互联网

H.264的码流封装格式

在H.264进行编码之后,生成的数据都封装为NAL Unit,多个NAL Unit串在一起形成总的输出码流。

NAL Unit字节流格式

标准协议文档:“Annex B - Byte stream format”
在这里插入图片描述
在这里插入图片描述
NAL Unit提取方法

实践:从码流中读取NAL Unit

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
typedef unsigned char uint8;

static int find_nal_prefix(ifstream& pfile,vector<uint8> &nalBytes) {
	/*
		00 00 00 01 x x x x x 00 00 00 01
	*/
	uint8 prefix[3] = { 0 };
	/*
		[0][1][2] = {0 0 0}-> [1][2][0]={0 0 0} -> [2][0][1]={0 0 0}->...
		getc()=1
	*/
	nalBytes.clear();
	uint8 x,pos=0;
	int getprefix = 0;
	for (int idx = 0; idx < 3; idx++) {
		x = pfile.get();
		prefix[idx] = x;
		nalBytes.push_back(x);
	}
	while ((x = pfile.get()) != EOF) {
		//cout << x << endl;
		if (prefix[pos %3]==0&& prefix[(pos+1) % 3] == 0&& prefix[(pos + 2) % 3] == 1) {
		 //0x 00 00 01 found
			getprefix = 1;
			nalBytes.pop_back();
			nalBytes.pop_back();
			nalBytes.pop_back();
			break;
		}
		else if (prefix[pos % 3] == 0 && prefix[(pos + 1) % 3] == 0 && prefix[(pos + 2) % 3] == 0) {
			//0x 00 00 00 found
			if (x == 1) {
				//0x 00 00 00 01 found
				getprefix = 2;
				nalBytes.pop_back();
				nalBytes.pop_back();
				nalBytes.pop_back();
				//nalBytes.pop_back();  还没push
				break;
			}
		}
		else {
			prefix[(pos++) % 3] = x;
			nalBytes.push_back(x);
		}
	}
	return getprefix;
}

//void Oc2Hex(int a){
//	char buffer[33];
//	sprintf_s(buffer, "%x", a);
//	cout << buffer << endl;
//}

int main(int argc,char* argv[])
{
	int *p1 = new int[10];
	int *p2 = new int[10]();

	ifstream inFile;
	inFile.open(argv[1], ios::in | ios::binary);
	if (!inFile) {  //条件成立,则说明文件打开成功
		cout << "Error:opening input file failed!" << endl;
		return 0;
	}
	else
		cout << "opening input file success!" << endl;
	
	int x;
	vector<uint8> nalBaytes;
	auto ou = find_nal_prefix(inFile,nalBaytes);
	ou = find_nal_prefix(inFile, nalBaytes);
	for (auto i : nalBaytes)
		printf("%x\t", i);
	printf("\n");
	ou = find_nal_prefix(inFile, nalBaytes);
	for (auto i : nalBaytes)
		printf("%x\t", i);
	printf("\n");
	ou = find_nal_prefix(inFile, nalBaytes);
	for (auto i : nalBaytes)
		printf("%x\t", i);
	printf("\n");
	inFile.close();
    return 0;
}

标签:码流,00,H.264,NAL,prefix,01,nalBaytes,Unit,结构
来源: https://blog.csdn.net/weixin_36049506/article/details/95928742