H264H265視頻編解碼算法文章匯總
一惜索、獲取Nalu中的內(nèi)容氛悬,不含起始碼
輸入文件中傳入JM工程中編譯輸出的文件test.264
JM工程中生成的H264.png
NaluParse.png
// FindNALContent.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
using namespace std;
typedef unsigned char uint8;
static int find_nal_prefix(FILE **pFilein,vector<uint8> &nalBytes) {
FILE *pFile = *pFilein;
/*
00 00 00 01 x x x x x x x x 00 00 00 01
*/
uint8 prefix[3] = { 0 };
uint8 fileByte;
nalBytes.clear();
/*
00 00 00 01
先比較0 1 2的位置是否均為0粗悯,如果不是則第下一個(gè)位置也就是下標(biāo)3的位置放在數(shù)組[0]位置虚循,比較[1][2][0],以此類(lèi)推
[0][1][2] == [0][0][0] -> [1][2][0] == [0][0][0] -> [2][0][1] = [0][0][0]
getchar() = 1 =====> 0 0 0 1
00 00 01
[0][1][2] == [0][0][1] -> [1][2][0] == [0][0][1] -> [2][0][1] = [0][0][1]
*/
int pos = 0, getPrefix = 0;
for (int idx = 0; idx < 3; idx++)
{
prefix[idx] = getc(pFile);
nalBytes.push_back(prefix[idx]);
}
while (!feof(pFile)) {
if (prefix[pos%3] == 0 && prefix[(pos+1)%3] == 0 && prefix[(pos + 2)%3] == 1)
{
/*
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) {
if (1 == getc(pFile))
{
/*
00 00 00 01
*/
getPrefix = 2;
nalBytes.pop_back();
nalBytes.pop_back();
nalBytes.pop_back();
break;
}
}
else
{
fileByte = getc(pFile);
prefix[(pos++) % 3] = fileByte;
nalBytes.push_back(fileByte);
}
}
return getPrefix;
}
int _tmain(int argc, _TCHAR *argv[])
{
printf("argv[0]:%ls\n", argv[0]);
printf("argv[1]:%ls\n", argv[1]);
printf("argv[2]:%ls\n", argv[2]);
FILE *pFile_in = NULL;
_tfopen_s(&pFile_in,argv[1],_T("rb"));
if (!pFile_in) {
printf("Error:open file failure!\n");
}
vector<uint8> nalBytes;
find_nal_prefix(&pFile_in, nalBytes);
find_nal_prefix(&pFile_in, nalBytes);
for (int idx = 0; idx < nalBytes.size(); idx++)
{
printf(" %x ", nalBytes.at(idx));
}
printf("\n");
find_nal_prefix(&pFile_in, nalBytes);
for (int idx = 0; idx < nalBytes.size(); idx++)
{
printf(" %x ", nalBytes.at(idx));
}
printf("\n");
find_nal_prefix(&pFile_in, nalBytes);
for (int idx = 0; idx < nalBytes.size(); idx++)
{
printf(" %x ", nalBytes.at(idx));
}
printf("\n");
return 0;
}
visual studio 2015控制臺(tái)窗口會(huì)輸出第一個(gè),第二個(gè)样傍,第三個(gè)Nalu單元,和用UltraEdit打開(kāi)test.264文件對(duì)比
Nalu.png
NaluBinary.png