-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoDecodeW.cpp
More file actions
180 lines (165 loc) · 4.77 KB
/
VideoDecodeW.cpp
File metadata and controls
180 lines (165 loc) · 4.77 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
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
175
176
177
178
179
180
/*
* Copyright 2022 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Written by: Evgeny Stupachenko
* e-mail: evgeny.v.stupachenko@intel.com
*/
#include <windows.h>
#include <sysinfoapi.h>
#include "VideoDecodeW.h"
#include "Config.h"
VideoDecode::VideoDecode() {
CodecCtx = NULL;
Options = NULL;
Frame = NULL;
DecodeTime = 0;
DecodeFrame = 0;
}
VideoDecode::~VideoDecode() {
#ifdef PRINT_STAT
printf("Average Decode Time %g\n", 1.0 * DecodeTime / DecodeFrame);
#endif
if (CodecCtx) {
avcodec_close(CodecCtx);
av_free(CodecCtx);
}
if (Frame) {
av_frame_free(&Frame);
}
}
int32_t VideoDecode::init(int32_t SliceNum) {
Codec = avcodec_find_decoder_by_name("h264_qsv");
if (!Codec) {
#ifdef PRINT_WARN
printf("Hardware accel not found! Decode could be slow!\n");
#endif
Codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!Codec) {
printf("Screen codec not found!\n");
return -1; // Screen Codec not found
}
}
CodecCtx = avcodec_alloc_context3(Codec);
if (!CodecCtx) {
return -2; // Could not allocate video codec context
}
CodecCtx->thread_count = 1;
// CodecCtx->delay = 0;
// CodecCtx->flags |= AV_CODEC_FLAG_LOW_DELAY;
if (avcodec_open2(CodecCtx, Codec, NULL) < 0) {
return -3; // Could not open screen codec
}
av_log_set_level(0);
Frame = av_frame_alloc();
if (!Frame) {
return -4; // Could not allocate video frame
}
return 0;
}
int32_t VideoDecode::getCtxWidth() { return CodecCtx->width; }
int32_t VideoDecode::getCtxHeight() { return CodecCtx->height; }
void YUV444toBGRA (uint8_t *InY, uint8_t *InU, uint8_t *InV, uint8_t *Out,
int width, int height, int32_t Num) {
int i, j;
for (i = 0; i < height; i++) {
int baseI = i * width;
int baseIO = i * width * SLICE_NUM_X;
for (j = 0; j < width; j++) {
int baseJO = j * 4 + (Num % SLICE_NUM_X) * 4 * width;
#ifdef NO_CONVERT
Out[4 * baseIO + baseJO] = InY[baseI + j];
Out[4 * baseIO + baseJO + 1] = InU[baseI + j];
Out[4 * baseIO + baseJO + 2] = InV[baseI + j];
Out[4 * baseIO + baseJO + 3] = 255;
#else
int c = InY[baseI + j] - 16;
int d = InU[baseI + j] - 128;
int e = InV[baseI + j] - 128;
int B = (298 * c + 516 * d + 128) >> 8;
int G = (298 * c - 100 * d - 208 * e + 128) >> 8;
int R = (298 * c + 409 * e + 128) >> 8;
B = B < 0 ? 0 : B;
B = B > 255 ? 255 : B;
G = G < 0 ? 0 : G;
G = G > 255 ? 255 : G;
R = R < 0 ? 0 : R;
R = R > 255 ? 255 : R;
Out[4 * baseIO + baseJO] = B;
Out[4 * baseIO + baseJO + 1] = G;
Out[4 * baseIO + baseJO + 2] = R;
Out[4 * baseIO + baseJO + 3] = 255;
#endif
}
}
}
static uint64_t getTime() {
#ifdef _WIN32
uint64_t Time = 0;
QueryInterruptTimePrecise(&Time);
return Time;
//
// return GetTickCount64() * 1000;
#else
struct timeval t0;
gettimeofday(&t0, NULL);
return t0.tv_sec * 1000000 + t0.tv_usec;
#endif
}
// Decode one frame from Screen Stream Write result to OutputFrame.
int32_t VideoDecode::decodeFrame(uint8_t *OutputData, int32_t PktSize,
uint8_t *PktData, int32_t Num, bool WriteData) {
uint64_t Start;
uint64_t Stop;
Start = getTime();
AVPacket Pkt;
int32_t RetDec, HasFrame = 0;
av_init_packet(&Pkt);
Pkt.size = PktSize;
Pkt.data = PktData;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
RetDec = avcodec_send_packet(CodecCtx, &Pkt);
HasFrame = avcodec_receive_frame(CodecCtx, Frame);
if (RetDec < 0) {
av_packet_unref(&Pkt);
return -2; // Decode error
}
if (HasFrame < 0) {
av_packet_unref(&Pkt);
return -3; // Cannot decode frame
}
#else
RetDec = avcodec_decode_video2(CodecCtx, Frame, &HasFrame, &Pkt);
if (RetDec < 0) {
av_packet_unref(&Pkt);
return -2; // Decode error
}
if (!HasFrame) {
av_packet_unref(&Pkt);
return -3; // Cannot decode frame
}
#endif
CodecCtx->width = Frame->width;
CodecCtx->height = Frame->height;
CodecCtx->pix_fmt = (AVPixelFormat)Frame->format;
if (WriteData) {
YUV444toBGRA(Frame->data[0], Frame->data[1], Frame->data[2], OutputData, Frame->width, Frame->height, Num);
}
av_packet_unref(&Pkt);
Stop = getTime();
DecodeTime += (Stop - Start);
DecodeFrame++;
return 0;
}