开发者

C/C++实现H264文件解析

开发者 https://www.devze.com 2024-10-29 10:44 出处:网络 作者: 静止了 所有的花开
H264视频编码格式简介 H264是视频压缩编码标准。视频⽂件的传输是⼀个极⼤的问题:⼀段分辨率为19201080的视频,每个像素点为RGB占⽤3个字节,帧率是25的视频,对于传输带宽的要求是:192010803*25/1024/1024=148.31

H264视频编码格式简介

H264是视频压缩编码标准。视频⽂件的传输是⼀个极⼤的问题:⼀段分辨率为19201080的视频,每个像素点为RGB占⽤3个字节,帧率是25的视频,对于传输带宽的要求是:192010803*25/1024/1024=148.315MB/s,换成bps则意味着视频每秒带宽为1186.523Mbps,这样的速率对于⽹络存储是不可接受的。因此视频压缩和编码技术应运⽽⽣。

H264码流分析

通常我们使用H264码流分析工具时,我们会发现H264码流是由连续的帧组成的,一组连续的帧成为1个GOP即1组,对于1个GOP来说,通常包括SPS(序列参数集,编码视频序列的全局参数)PPS(图像参数集,⼀个序列中某⼀幅图像或者某⼏幅图像的参数),IDR帧(第一个出现的I帧成为IDR帧,1个GOP中通常只包含1个IDR帧),P帧(GOP中剩余帧基本都是P帧);GOP描述如下:

C/C++实现H264文件解析

H264码流结构如图:

C/C++实现H264文件解析

C/C++ H264文件解析

C++实现H264文件以及一段H264码流解析,源码如下:

h264Parse.h:

#ifndef _H264PARSE_H_
#define _H264PARSE_H_
 
#include <fstream>
 
class H264Parse
{
public:
    int open_file(const std::string &filename);
 
    /**
     * @brief 从文件中读取一个nalu,包含起始码
     * @param buf 存放nalu的缓冲区
     * @param size 缓冲区大小
     * @param len nalu的长度
     * @param n 每次读取多少个字节
     * @return  -1 失败    0 已到文件末尾     1 成功获取到一个nalu
     */
    int read_nalu(uint8_t *buf, uint32_t size, uint32_t &len, uint32_t n);
 
    void close_file();
 
    // 获取起始码长度
    static int get_startCode_len(const uint8_t *ptr);
 
    static const uint8_t *find_startCode_pos(const uint8_t *buf, uint32_t len);
 
    /**
     * @brief 从一段h264码流中分割nalu,包含起始码
     * @param stream h264码流
     * @param streamLen 码流大小
     * @param nalu Pointer to the extracted nalu
     * @param naluLen nalu的长度
     * @param record Pointer用于记录状态,第一次分割时把 *record 赋值为NULL
     * @return  -1 失败    0 已分割完     1 成功获取到一个nalu
     */
    static int nalu_tok(const uint8_t *stream, uint32_t streamLen, const uint8_t **nalu,
                        uint32_t &naluLen, const uint8_t **record);
 
private:
    std::fstream h264File;
 
    int read_start_code(uint8_t *buf);
    int adjust_filePointer_pos(uint32_t totalRead, uint32_t naluLen);
};
 
#endif // _H264PARSE_H_

h264Parse.cpp:

#include "h264Parse.h"
#include <IOStream>
#include <cstring>
 
int H264Parse::open_file(const std::string &filename)
{
    h264File.open(filename, std::ios::in | std::ios::binary);
    if (!h264File.is_open())
    {
        std::cout << "Failed to open the H.264 file." << std::endl;
        return -1;
    }
 
    return 0;
}
 
int H264Parse::get_startCode_len(const uint8_t *ptr)
{
    if (ptr[0] == 0x00 && ptr[1] == 0x00)
    {
        if (ptr[2] == 0x01)
            return 3;
        else if (ptr[2] == 0x00 && ptr[3] == 0x01)
            return 4;
    }
    return -1; // 无效的起始码
}
 
// 读取起始码,并返回其长度
int H264Parse::read_start_code(uint8_t *buf)
{
    // 读取前4个字节来判断起始码长度
    h264File.read(reinterpret_cast<char *>(buf), 4);
    if (h264File.gcount() < 4)
    {
        return -1;
    }
 
    return get_startCode_len(buf);
}
 
// 寻找NALU的起始码位置
const uint8_t *H264Parse::find_startCode_pos(const uint8_t *buf, uint32_t len)
{
    const uint8_t *p = buf;
 
    if (len < 3)
        return NULL;
 
    for (uint32_t i = 0; i < len - 3; ++i)
    {
        if ((p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01) ||
            (p[0] == 0x00 &phpamp;& p[1] == 0x00 && p[2] == 0x00 && p[3] == 0x01))
        {
            return p;
        }
        p++;
    }
 
    // 检查最后3字节是不是起始码
    if (p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01)
        return p;
 
    return NULL;
}
 
// 调整文件指针位置
int H264Parse::adjust_filePointer_pos(uint32_t totalRead, uint32_t naluLen)
{
    int offset = -(totalRead - naluLen);
 
    if (!h264File.eof())
    {
        h264File.seekg(offset, std::ios::cur);
    }
    else
    {
        h264File.clear(); // 达到文件末尾了要先清除 eof 标志
        h264File.seekg(offset, std::ios::end);
    }
 
    if (h264File.faycgokdtGVil())
    {
        std::cout << "seekg failed!" << std::endl;
        return -1;
    }
 
    return 0;
}
 
int H264Parse::read_nalu(uint8_t *buf, uint32_t size, uint32_t &len, uint32_t n)
{
    uint32_t totalRead = 0;
 
    int startCodeLength = read_start_code(buf);
    if (startCodeLength == -1)
    {
        printf("read_start_code failed.\n");
        return -1;
    }
 
    totalRead += 4; // 已经读取了4字节的长度
 
    while (true)
    {
        if (size < totalRead + n)
        {
            std::cout << "Buffer size is too small: size=" << size
                      << ", needed=" << totalRead + n << std::endl;
            return -1;
        }
 
        h264File.read(reinterpret_cast<char *>(buf + totalRead), n);
        std::streamsize bytesRead = h264File.gcount();
        if (bytesRead <= 0)
        {
            std::cout << "Failed to read from file!" << std::endl;
            return -1;
        }
 
        uint32_t searchStart = (totalRead > 4) ? totalRead - 3 : startCodeLength;
        const uint8_t *naluEnd = find_startCode_pos(buf + searchStart,
                                                    bytesRead + (totalRead > 4 ? 3 : 0));
        totalRead += bytesRead;
        if (naluEnd != nullptr)
        {
            len = naluEnd - buf;
            if (adjust_filePointer_pos(totalRead, len) < 0)
                return -1;
 
            break;
        }
 
        // 是否读取到文件末尾
        if (h264File.peek() == std::char_traits<char>::eof())
        {
            len = totalRead;
            return 0; // NALU完整读取
        }
    }
 
    memset(buf + len, 0, size - len); // 清空剩余部分
    return 1;                         // 成功读取
}
 
void H264Parse::close_file()
{
    h264File.close();
}
 
int H264Parse::nalu_tok(const uint8_t *stream, uint32_t streamLejsn, const uint8_t **nalu,
                        uint32_t &naluLen, const uint8_t **record)
{
    const uint8_t *current = (record && *record) ? *record : stream;
    uint32_t offset = static_cast<uint32_t>(current - stream);
 
    if (offset >= streamLen)
    {
        return -1; // 当前记录位置超出缓冲区
    }
 
    int scLen = get_startCode_len(current);
    if (scLen == -1 || (current + scLen) > (stream + streamLen))
    {
        return -1; // 无效的起始码或起始码长度超出缓冲区
    }
 
    // 查找下一个起始码的位置
    const uint8_t *next_start = find_startCode_pos(current + scLen, streamLen - offset - scLen);
 
    if (next_start)
    {
        *nalu = current;
        naluLen = static_cast<uint32_t>(next_start - current);
        *record = next_start;
        return 1; // 成功获取到一个 NALU
    }
    else
    {
        // 最后一个 NALU
        *nalu = current;
        naluLen = streamLen - offset;
        *record = NULL; // 重置记录指针
        return 0;       // 分割完毕
    }
}

测试:

#include <iostream>
#include <vector>
#include编程客栈 "h264Parse.h"
 
void test1()
{
    int ret;
    int number = 0;
    H264Parse h264;
    uint8_t buf[1024 * 1024];
    uint32_t len = 0;
 
    h264.open_file("/home/tl/work/app/res/output.h264");
    while ((ret = h264.read_nalu(buf, sizeof(buf), len, 1024 * 2)) != -1)
    {
        printf("number: %d nalu len: %u\n", number, len - h264.get_startCode_len(buf));
        number++;
        if (ret == 0)
            break;
    }
    if (ret == -1)
    {
        std::cout << "read_nalu failed." << std::endl;
    }
编程客栈    h264.close_file();
}
 
// 辅助函数:打印 NALU 信息
void print_nalu(const uint8_t *nalu, uint32_t len, int index)
{
    std::cout << "NALU " << index << ": Length = " << len << " bytes, Data = ";
    for (uint32_t i = 0; i < len; ++i)
    {
        printf("%02X ", nalu[i]);
    }
    std::cout << std::endl;
}
 
void test2()
{
    // 构造一个模拟的 H.264 码流缓冲区,包含多个 NALU
    // 起始码格式:0x000001 (3 字节) 和 0x00000001 (4 字节)
    // NALU 内容:随机填充的字节数据
    std::vector<uint8_t> buffer;
 
    // NALU 1: 3 字节起始码 + 5 字节数据
    std::vector<uint8_t> nalu1 = {0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x21, 0xA0};
    buffer.insert(buffer.end(), nalu1.begin(), nalu1.end());
 
    // NALU 2: 4 字节起始码 + 6 字节数据
    std::vector<uint8_t> nalu2 = {0x00, 0x00, 0x00, 0x01, 0x41, 0x9A, 0x5C, 0xD4, 0x00, 0x11};
    buffer.insert(buffer.end(), nalu2.begin(), nalu2.end());
 
    // NALU 3: 3 字节起始码 + 4 字节数据
    std::vector<uint8_t> nalu3 = {0x00, 0x00, 0x01, 0x06, 0x05, 0xFF, 0xEE};
    buffer.insert(buffer.end(), nalu3.begin(), nalu3.end());
 
    // NALU 4: 3 字节起始码 + 3 字节数据 (测试末尾)
    std::vector<uint8_t> nalu4 = {0x00, 0x00, 0x01, 0x07, 0xAD, 0xBE};
    buffer.insert(buffer.end(), nalu4.begin(), nalu4.end());
 
    // 输出构建的缓冲区(可选)
    std::cout << "Constructed H.264 Buffer: ";
    for (size_t i = 0; i < buffer.size(); ++i)
    {
        printf("%02X ", buffer[i]);
    }
    std::cout << "\n\n";
 
    const uint8_t *pnalu = nullptr;
    uint32_t nale_len = 0;
    const uint8_t *pRecord = NULL; // 初始时为 NULL
 
    int ret;
    int nalu_index = 1;
 
    // 循环分割并打印每个 NALU
    while ((ret = H264Parse::nalu_tok(buffer.data(), buffer.size(), &pnalu, nale_len, &pRecord)) != -1)
    {
        print_nalu(pnalu, nale_len, nalu_index);
        nalu_index++;
        if (ret == 0)
            break;
    }
 
    if (ret == -1)
    {
        std::cout << "Error occurred during NALU tokenization." << std::endl;
    }
}
 
// 主函数
int main()
{
    test1();
    // test2();
 
    return 0;
}

以上就是C/C++实现H264文件解析的详细内容,更多关于C++ H264文件解析的资料请关注编程客栈(www.devze.com)其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消