-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_hevc.c
174 lines (138 loc) · 3.85 KB
/
decode_hevc.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void (*YUV420PBuffer)(uint8_t* yuv_buffer, int size, int width,
int height);
typedef enum ErrorCode {
kErrorCode_Success = 0,
kErrorCode_FFmpeg_Error = 1
} ErrorCode;
YUV420PBuffer YUV420P_buffer_callback = NULL;
const AVCodec* codec;
AVCodecParserContext* parser_context;
AVCodecContext* codec_context = NULL;
AVPacket* pkt;
AVFrame* frame;
ErrorCode init_decoder(YUV420PBuffer callback) {
ErrorCode ret = kErrorCode_Success;
do {
codec = avcodec_find_decoder(AV_CODEC_ID_H265);
if (!codec) {
ret = kErrorCode_FFmpeg_Error;
break;
}
parser_context = av_parser_init(codec->id);
if (!parser_context) {
ret = kErrorCode_FFmpeg_Error;
break;
}
codec_context = avcodec_alloc_context3(codec);
if (!codec_context) {
ret = kErrorCode_FFmpeg_Error;
break;
}
/* 多线程开启配置: 配置 4 个线程进行解码 */
// codec_context->thread_count = 4;
if (avcodec_open2(codec_context, codec, NULL) < 0) {
ret = kErrorCode_FFmpeg_Error;
break;
}
frame = av_frame_alloc();
/* 指定格式为 YUV420 */
frame->format = AV_PIX_FMT_YUV420P;
if (!frame) {
ret = kErrorCode_FFmpeg_Error;
break;
}
pkt = av_packet_alloc();
if (!pkt) {
ret = kErrorCode_FFmpeg_Error;
break;
}
YUV420P_buffer_callback = callback;
} while (0);
return ret;
}
void AVFrame_to_yuv_buffer(AVFrame* frame) {
int frame_size =
av_image_get_buffer_size(frame->format, frame->width, frame->height, 1);
if (frame_size < 0) {
return;
}
uint8_t* yuv_buffer = (uint8_t*)av_malloc(frame_size);
if (!yuv_buffer) {
return;
}
/* frame 数据按照 yuv420 格式填充到 yuv_buffer */
int ret = av_image_copy_to_buffer(
yuv_buffer, frame_size, (const uint8_t**)frame->data, frame->linesize,
frame->format, frame->width, frame->height, 1);
if (ret < 0) {
av_free(yuv_buffer);
return;
}
YUV420P_buffer_callback(yuv_buffer, frame_size, frame->width, frame->height);
av_free(yuv_buffer);
}
static ErrorCode decode_AVPacket(AVCodecContext* dec_ctx, AVFrame* frame,
AVPacket* pkt) {
ErrorCode res = kErrorCode_Success;
int ret;
ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
res = kErrorCode_FFmpeg_Error;
} else {
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
res = kErrorCode_FFmpeg_Error;
break;
}
AVFrame_to_yuv_buffer(frame);
}
}
return res;
}
ErrorCode decode_AnnexB_buffer(const uint8_t* buffer, size_t buffer_size) {
ErrorCode ret = kErrorCode_Success;
while (buffer_size > 0) {
int size = av_parser_parse2(parser_context, codec_context, &pkt->data,
&pkt->size, buffer, buffer_size, AV_NOPTS_VALUE,
AV_NOPTS_VALUE, 0);
if (size < 0) {
ret = kErrorCode_FFmpeg_Error;
break;
}
buffer += size;
buffer_size -= size;
if (pkt->size) {
ret = decode_AVPacket(codec_context, frame, pkt);
if (ret != kErrorCode_Success) {
break;
}
}
}
return ret;
}
ErrorCode flush_decoder() { return decode_AVPacket(codec_context, frame, NULL); }
ErrorCode close_decoder() {
ErrorCode ret = kErrorCode_Success;
if (parser_context != NULL) {
av_parser_close(parser_context);
}
if (codec_context != NULL) {
avcodec_free_context(&codec_context);
}
if (frame != NULL) {
av_frame_free(&frame);
}
if (pkt != NULL) {
av_packet_free(&pkt);
}
return ret;
}
int main(int argc, char** argv) { return 0; }