-
Notifications
You must be signed in to change notification settings - Fork 6
/
videodecoder.c
166 lines (137 loc) · 5.53 KB
/
videodecoder.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
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include "videodecoder.h"
#define CPrintf printf
struct AVCodecContext *pAVCodecCtxDecoder = NULL;
struct AVCodec *pAVCodecDecoder;
struct AVPacket mAVPacketDecoder;
struct AVFrame *pAVFrameDecoder = NULL;
struct SwsContext* pImageConvertCtxDecoder = NULL;
struct AVFrame *pFrameYUVDecoder = NULL;
int ffmpeg_init_video_decoder(AVCodecParameters *codecParameters)
{
if (!codecParameters) {
CPrintf("Source codec context is NULL."); //CPrintf需替换为printf
return -1;
}
ffmpeg_release_video_decoder();
avcodec_register_all();
pAVCodecDecoder = avcodec_find_decoder(codecParameters->codec_id);
if (!pAVCodecDecoder) {
CPrintf("Can not find codec:%d\n", codecParameters->codec_id);
return -2;
}
pAVCodecCtxDecoder = avcodec_alloc_context3(pAVCodecDecoder);
if (!pAVCodecCtxDecoder) {
CPrintf("Failed to alloc codec context.");
ffmpeg_release_video_decoder();
return -3;
}
if (avcodec_parameters_to_context(pAVCodecCtxDecoder, codecParameters) < 0) {
CPrintf("Failed to copy avcodec parameters to codec context.");
ffmpeg_release_video_decoder();
return -3;
}
if (avcodec_open2(pAVCodecCtxDecoder, pAVCodecDecoder, NULL) < 0){
CPrintf("Failed to open h264 decoder");
ffmpeg_release_video_decoder();
return -4;
}
av_init_packet(&mAVPacketDecoder);
pAVFrameDecoder = av_frame_alloc();
pFrameYUVDecoder = av_frame_alloc();
return 0;
}
int ffmpeg_init_h264_decoder()
{
avcodec_register_all();
AVCodec *pAVCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!pAVCodec){
CPrintf("can not find H264 codec\n");
return -1;
}
AVCodecContext *pAVCodecCtx = avcodec_alloc_context3(pAVCodec);
if (pAVCodecCtx == NULL) {
CPrintf("Could not alloc video context!\n");
return -2;
}
AVCodecParameters *codecParameters = avcodec_parameters_alloc();
if (avcodec_parameters_from_context(codecParameters, pAVCodecCtx) < 0) {
CPrintf("Failed to copy avcodec parameters from codec context.");
avcodec_parameters_free(&codecParameters);
avcodec_free_context(&pAVCodecCtx);
return -3;
}
int ret = ffmpeg_init_video_decoder(codecParameters);
avcodec_parameters_free(&codecParameters);
avcodec_free_context(&pAVCodecCtx);
return ret;
}
int ffmpeg_release_video_decoder() {
if (pAVCodecCtxDecoder != NULL) {
avcodec_free_context(&pAVCodecCtxDecoder);
pAVCodecCtxDecoder = NULL;
}
if (pAVFrameDecoder != NULL) {
av_packet_unref(&mAVPacketDecoder);
av_free(pAVFrameDecoder);
pAVFrameDecoder = NULL;
}
if (pFrameYUVDecoder) {
av_frame_unref(pFrameYUVDecoder);
av_free(pFrameYUVDecoder);
pFrameYUVDecoder = NULL;
}
if (pImageConvertCtxDecoder) {
sws_freeContext(pImageConvertCtxDecoder);
}
av_packet_unref(&mAVPacketDecoder);
return 0;
}
int ffmpeg_decode_h264(unsigned char *inbuf, int inbufSize, int *framePara, unsigned char **outRGBBuf, unsigned char *outYUVBuf)
{
if (!pAVCodecCtxDecoder || !pAVFrameDecoder || !inbuf || inbufSize<=0 || !framePara || (!outRGBBuf && !outYUVBuf)) {
return -1;
}
av_frame_unref(pAVFrameDecoder);
av_frame_unref(pFrameYUVDecoder);
framePara[0] = framePara[1] = 0;
mAVPacketDecoder.data = inbuf;
mAVPacketDecoder.size = inbufSize;
int ret = avcodec_send_packet(pAVCodecCtxDecoder, &mAVPacketDecoder);
if (ret == 0) {
ret = avcodec_receive_frame(pAVCodecCtxDecoder, pAVFrameDecoder);
if (ret == 0) {
framePara[0] = pAVFrameDecoder->width;
framePara[1] = pAVFrameDecoder->height;
if (outYUVBuf) {
//*outYUVBuf = (unsigned char *)pAVFrameDecoder->data;
memcpy(outYUVBuf, pAVFrameDecoder->data, sizeof(pAVFrameDecoder->data));
framePara[2] = pAVFrameDecoder->linesize[0];
framePara[3] = pAVFrameDecoder->linesize[1];
framePara[4] = pAVFrameDecoder->linesize[2];
} else if (outRGBBuf) {
pFrameYUVDecoder->data[0] = outRGBBuf;
pFrameYUVDecoder->data[1] = NULL;
pFrameYUVDecoder->data[2] = NULL;
pFrameYUVDecoder->data[3] = NULL;
int linesize[4] = { pAVCodecCtxDecoder->width * 3, pAVCodecCtxDecoder->height * 3, 0, 0 };
pImageConvertCtxDecoder = sws_getContext(pAVCodecCtxDecoder->width, pAVCodecCtxDecoder->height,
AV_PIX_FMT_YUV420P,
pAVCodecCtxDecoder->width,
pAVCodecCtxDecoder->height,
AV_PIX_FMT_RGB24, SWS_FAST_BILINEAR,
NULL, NULL, NULL);
sws_scale(pImageConvertCtxDecoder, (const uint8_t* const *) pAVFrameDecoder->data, pAVFrameDecoder->linesize, 0, pAVCodecCtxDecoder->height, pFrameYUVDecoder->data, linesize);
sws_freeContext(pImageConvertCtxDecoder);
}
return 1;
} else if (ret == AVERROR(EAGAIN)) {
return 0;
} else {
return -1;
}
}
return 0;
}