-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh264_splitter.cpp
More file actions
75 lines (63 loc) · 2 KB
/
h264_splitter.cpp
File metadata and controls
75 lines (63 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by pengx on 2026/2/11.
//
#include "h264_splitter.hpp"
#include <iostream>
int H264Splitter::splitH264Frame(const uint8_t* frame, const size_t frame_size, std::vector<NALU>& nalu_vector) {
if (frame == nullptr || frame_size == 0) {
return 0;
}
nalu_vector.clear();
size_t i = 0;
// 查找所有起始码位置
std::vector<size_t> start_positions;
std::vector<size_t> start_code_lengths;
while (i < frame_size) {
// 检查 4 字节起始码
if (i + 3 < frame_size &&
frame[i] == 0x00 &&
frame[i + 1] == 0x00 &&
frame[i + 2] == 0x00 &&
frame[i + 3] == 0x01) {
start_positions.push_back(i);
start_code_lengths.push_back(4);
i += 4;
continue;
}
// 检查 3 字节起始码
if (i + 2 < frame_size &&
frame[i] == 0x00 &&
frame[i + 1] == 0x00 &&
frame[i + 2] == 0x01) {
start_positions.push_back(i);
start_code_lengths.push_back(3);
i += 3;
continue;
}
i++;
}
// 如果没找到起始码
if (start_positions.empty()) {
std::cerr << "未找到任何起始码" << std::endl;
return 0;
}
// 根据起始码位置提取 NALU
for (size_t idx = 0; idx < start_positions.size(); idx++) {
const size_t nalu_start = start_positions[idx] + start_code_lengths[idx];
size_t nalu_end;
if (idx + 1 < start_positions.size()) {
nalu_end = start_positions[idx + 1];
} else {
nalu_end = frame_size;
}
const size_t nalu_size = nalu_end - nalu_start;
if (nalu_size > 0 && nalu_start < frame_size) {
NALU nalu;
nalu.data = const_cast<uint8_t*>(frame + nalu_start);
nalu.size = nalu_size;
nalu.type = nalu.data[0] & 0x1F;
nalu_vector.push_back(nalu);
}
}
return nalu_vector.size();
}