-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh264_decoder.cpp
More file actions
137 lines (110 loc) · 3.75 KB
/
h264_decoder.cpp
File metadata and controls
137 lines (110 loc) · 3.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Created by pengx on 2026/2/9.
//
#include "h264_decoder.hpp"
#include <QDebug>
#include <QThread>
#include <stdexcept>
H264Decoder::H264Decoder(QObject* parent) : QObject(parent) {
if (!initializeDecoder()) {
throw std::runtime_error("Failed to initialize H.264 decoder");
}
// 注册元类型以支持跨线程信号槽
qRegisterMetaType<cv::Mat>("cv::Mat");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<std::vector<uint8_t>>("std::vector<uint8_t>");
}
H264Decoder::~H264Decoder() {
cleanupDecoder();
}
bool H264Decoder::initializeDecoder() {
const AVCodec* codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
qCritical() << "H.264 decoder not found";
return false;
}
_codecContextPtr = avcodec_alloc_context3(codec);
if (!_codecContextPtr) {
qCritical() << "Failed to allocate codec context";
return false;
}
// 设置多线程解码
_codecContextPtr->thread_count = 4;
_codecContextPtr->thread_type = FF_THREAD_SLICE;
if (avcodec_open2(_codecContextPtr, codec, nullptr) < 0) {
qCritical() << "Failed to open codec";
return false;
}
_framePtr = av_frame_alloc();
_packetPtr = av_packet_alloc();
if (!_framePtr || !_packetPtr) {
qCritical() << "Failed to allocate frame or packet";
return false;
}
return true;
}
void H264Decoder::cleanupDecoder() {
if (_swsContextPtr) {
sws_freeContext(_swsContextPtr);
_swsContextPtr = nullptr;
}
if (_framePtr) {
av_frame_free(&_framePtr);
_framePtr = nullptr;
}
if (_packetPtr) {
av_packet_free(&_packetPtr);
_packetPtr = nullptr;
}
if (_codecContextPtr) {
avcodec_free_context(&_codecContextPtr);
_codecContextPtr = nullptr;
}
}
void H264Decoder::decodeH264Data(const std::vector<uint8_t>& h264Data, const uint32_t pts) {
if (h264Data.empty()) {
return;
}
_packetPtr->data = const_cast<uint8_t*>(h264Data.data());
_packetPtr->size = static_cast<int>(h264Data.size());
_packetPtr->pts = static_cast<int64_t>(pts);
int ret = avcodec_send_packet(_codecContextPtr, _packetPtr);
if (ret < 0) {
char err[256];
av_strerror(ret, err, sizeof(err));
qWarning() << "Error sending packet to decoder:" << err;
return;
}
while (ret >= 0) {
ret = avcodec_receive_frame(_codecContextPtr, _framePtr);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
if (ret < 0) {
char err[256];
av_strerror(ret, err, sizeof(err));
qWarning() << "Error receiving frame from decoder:" << err;
break;
}
// 转换YUV420P到BGR24
const int srcW = _framePtr->width;
const int srcH = _framePtr->height;
const auto srcFormat = static_cast<AVPixelFormat>(_framePtr->format);
_swsContextPtr = sws_getCachedContext(_swsContextPtr,
srcW, srcH, srcFormat,
srcW, srcH, AV_PIX_FMT_BGR24,
SWS_BILINEAR,
nullptr, nullptr, nullptr);
if (!_swsContextPtr) {
qWarning() << "Failed to get sws context";
continue;
}
cv::Mat frame(srcH, srcW, CV_8UC3);
uint8_t* dstData[1] = {frame.data};
const int dstStride[1] = {static_cast<int>(frame.step)};
sws_scale(_swsContextPtr, _framePtr->data, _framePtr->linesize, 0, srcH, dstData, dstStride);
emit frameDecoded(frame);
}
// 重置packet,不释放data
av_packet_unref(_packetPtr);
}