我想知道如何使用C编程来识别HEVC流中的NAL单元

I want to know how to recognize the NAL unit in a HEVC stream using C programing

本文关键字:HEVC 单元 NAL 识别 想知道 何使用 编程      更新时间:2023-10-16

我想知道如何使用C或C++编程来识别HEVC流中的NAL单元。该代码应当能够将HEVC流作为输入,并且应当产生关于NAL单元的信息,例如IDR帧。

#include <stdio.h>
int main(int argc, char *argv[])
{
    FILE *inp = fopen("c:\test.hevc", "rb");
    // parser state
    bool start = true;
    // current code word
    unsigned long int code_word = 0;
    // iterate through byte stream
    while(true) {
        // read next byte
        int b = fgetc(inp);
        // quit at end of byte stream
        if (b == EOF) { 
            break;
        }
        // track 32-bit code word
        code_word = (code_word | b) << 8;
        // NALU start
        if (code_word == 0x00000100) {
            if (start) {
                // read nalu type
                int type = fgetc(inp);
                // quit at end of byte stream
                if (type == EOF) {
                    break;
                }
                // ignore reserved bit
                type >>= 1;
                if (type == 19 /* IDR */) {
                    printf("NALU type: IDRn");
                }
                else {
                    printf("NALU type: %dn", type);
                }
                code_word = 0;
            }
            start = !start;
        }
    }
    fclose(inp);
    return 0;
}