原理
H.264原始码流(又称为“裸流”)是由一个一个的NALU组成的。他们的结构如下图所示。
其中每个NALU之间通过startcode(起始码)进行分隔,起始码分成两种:0x000001(3Byte)或者0x00000001(4Byte)。如果NALU对应的Slice为一帧的开始就用0x00000001,否则就用0x000001。
H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,分离出NALU;然后再分析NALU的各个字段。本文的程序即实现了上述的两个步骤。
代码
整个程序位于simplest_h264_parser()函数中,如下所示。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- typedef enum {
- NALU_TYPE_SLICE = 1,
- NALU_TYPE_DPA = 2,
- NALU_TYPE_DPB = 3,
- NALU_TYPE_DPC = 4,
- NALU_TYPE_IDR = 5,
- NALU_TYPE_SEI = 6,
- NALU_TYPE_SPS = 7,
- NALU_TYPE_PPS = 8,
- NALU_TYPE_AUD = 9,
- NALU_TYPE_EOSEQ = 10,
- NALU_TYPE_EOSTREAM = 11,
- NALU_TYPE_FILL = 12,
- } NaluType;
-
- typedef enum {
- NALU_PRIORITY_DISPOSABLE = 0,
- NALU_PRIRITY_LOW = 1,
- NALU_PRIORITY_HIGH = 2,
- NALU_PRIORITY_HIGHEST = 3
- } NaluPriority;
-
-
- typedef struct
- {
- int startcodeprefix_len;
- unsigned len;
- unsigned max_size;
- int forbidden_bit;
- int nal_reference_idc;
- int nal_unit_type;
- char *buf;
- } NALU_t;
-
- FILE *h264bitstream = NULL;
-
- int info2=0, info3=0;
-