一、获取H264Nalu中内容

2024-02-18  本文已影响0人  一亩三分甜

H264H265视频编解码算法文章汇总
一、获取Nalu中的内容,不含起始码
输入文件中传入JM工程中编译输出的文件test.264

JM工程中生成的H264.png
NaluParse.png
// FindNALContent.cpp : 定义控制台应用程序的入口点。
//
#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,如果不是则第下一个位置也就是下标3的位置放在数组[0]位置,比较[1][2][0],以此类推
       [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控制台窗口会输出第一个,第二个,第三个Nalu单元,和用UltraEdit打开test.264文件对比


Nalu.png
NaluBinary.png
上一篇 下一篇

猜你喜欢

热点阅读