-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg_wrapper.c
339 lines (293 loc) · 8.14 KB
/
ffmpeg_wrapper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#define PACKET_BUFFERED_NB 8
AVFormatContext* pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame* pFrameYUV[PACKET_BUFFERED_NB];
pthread_mutex_t lock[PACKET_BUFFERED_NB];
int freeSlot[PACKET_BUFFERED_NB];
unsigned int wArrayIdx = 0;
unsigned int rArrayIdx = 0;
int videoStreamIndex = -1;
pthread_t tId;
int srcX = 640;
int srcY = 360;
int dstX = 640;
int dstY = 360;
int cnt = 0;
void* taskBufferFrame(void *arg){
int frame_decoded = 0;
int fail = 0;
pthread_mutex_t * tmpLock;
AVPacket tmpPacket;
av_init_packet(&tmpPacket);
while(1){
tmpLock = &lock[wArrayIdx];
pthread_mutex_lock(tmpLock);
if(av_read_frame(pFormatCtx,&tmpPacket) >= 0 &&
tmpPacket.stream_index == videoStreamIndex){
avcodec_decode_video2(pCodecCtx, pFrameYUV[wArrayIdx], &frame_decoded, &tmpPacket);
av_free_packet(&tmpPacket);
av_init_packet(&tmpPacket);
if(frame_decoded){
freeSlot[wArrayIdx] = 0;
cnt++;
wArrayIdx = (wArrayIdx + 1) % PACKET_BUFFERED_NB;
}
else{
printf("buffer can't decode %u \n", wArrayIdx);
fail = 1;
}
}
else{
printf("buffer can't read %u \n", wArrayIdx);
av_free_packet(&tmpPacket);
av_init_packet(&tmpPacket);
fail = 1;
}
pthread_mutex_unlock(tmpLock);
if(fail){
usleep(5000);
fail = 0;
}
}
}
void reduce_c(unsigned char *bSrc, float *bDst, int wSrc, int hSrc, int wDst, int hDst, int sSrc)
{
int x, y, pX, pY;
int ycomp;
int wPixelStep = (wSrc << 16) / wDst;
int wFullPix = (((wDst << 16) / wSrc) + 128) >> 8;
int wRest = wPixelStep & 0xffff;
int wNbPix;
if (wRest == 0)
wNbPix = (wPixelStep >> 16);
else
wNbPix = (wPixelStep >> 16) + 2;
int hPixelStep = (hSrc << 16) / hDst;
int hFullPix = (((hDst << 16) / hSrc) + 128) >> 8;
int hNbPix = (hPixelStep >> 16) + 2;
int wStartPoint, hStartPoint;
int xLine, yLine;
int xCoef, yCoef, fullCoef, alphaCoef, sumCoef;
int xEndCoef, yEndCoef;
int tCoefX[10000];
int tCoefY[10000];
int ptX = 0, ptY = 0;
int useCorrection = 0;
wStartPoint = 0;
for (x = 0 ; x < wDst ; x++)
{
xLine = (wStartPoint >> 16);
xCoef = wFullPix - (((wStartPoint & 0xffff) * wFullPix) >> 16);
xEndCoef = 256 - xCoef;
tCoefX[ptX++] = xLine;
tCoefX[ptX++] = xCoef;
for (pX = 1 ; pX < wNbPix; pX++)
{
if (xEndCoef >= wFullPix)
{
xCoef = wFullPix;
xEndCoef -= wFullPix;
}
else if (xEndCoef > 0)
{
xCoef = xEndCoef;
xEndCoef -= wFullPix;
}
else
{
xCoef = 0;
}
tCoefX[ptX++] = xCoef;
}
wStartPoint += wPixelStep;
}
hStartPoint = 0;
for (y = 0 ; y < hDst ; y++)
{
yLine = (hStartPoint >> 16);
yCoef = hFullPix - (((hStartPoint & 0xffff) * hFullPix) >> 16);
yEndCoef = 256 - yCoef;
tCoefY[ptY++] = yLine;
tCoefY[ptY++] = yCoef;
for (pY = 1 ; pY < hNbPix; pY++)
{
if (yEndCoef >= hFullPix)
{
yCoef = hFullPix;
yEndCoef -= hFullPix;
}
else if (yEndCoef > 0)
{
yCoef = yEndCoef;
yEndCoef -= hFullPix;
}
else
{
yCoef = 0;
}
tCoefY[ptY++] = yCoef;
}
hStartPoint += hPixelStep;
}
ptY = 0;
for (y = 0 ; y < hDst ; y++)
{
yLine = tCoefY[ptY++];
ptX = 0;
for (x = 0 ; x < wDst ; x++)
{
xLine = tCoefX[ptX++];
ycomp = 0;
for (pY = 0 ; pY < hNbPix; pY++)
{
yCoef = tCoefY[ptY + pY];
for (pX = 0 ; pX < wNbPix; pX++)
{
xCoef = tCoefX[ptX + pX];
fullCoef = xCoef * yCoef;
ycomp += *(bSrc + (yLine + pY) * sSrc + (xLine + pX)) * fullCoef;
}
}
ptX += wNbPix;
*bDst = ycomp / 255.0;
bDst++;
}
ptY += hNbPix;
}
}
int decode_frame(float * output, AVFrame* src, int width, int height){
int i, j;
reduce_c(src->data[0], output, 640, 360, width, height, src->linesize[0]);
/* for(i = 0 ; i < height; i++){ */
/* for(j = 0 ; j < width; j++){ */
/* output[i*width + j] = *(src->data[0]+i * src->linesize[0] + j) / 255.0; */
/* } */
/* } */
return 1;
}
int get_frame(float * data, int width, int height){
int wait_frame = 1;
pthread_mutex_t * tmpLock;
while(wait_frame){
rArrayIdx = wArrayIdx;
if(rArrayIdx == 0)
rArrayIdx = PACKET_BUFFERED_NB - 1;
else
rArrayIdx = rArrayIdx - 1;
tmpLock = &lock[rArrayIdx];
//printf("reader wants lock %u \n", rArrayIdx);
pthread_mutex_lock(tmpLock);
//printf("reader gets lock %u \n", rArrayIdx);
if(freeSlot[rArrayIdx] == 1){
pthread_mutex_unlock(tmpLock);
//printf("reader has nothing to read %u \n", rArrayIdx);
usleep(1000);
}
else{
decode_frame(data, pFrameYUV[rArrayIdx], width, height);
freeSlot[rArrayIdx] = 1;
pthread_mutex_unlock(tmpLock);
return 1;
}
}
return 0;
}
int init_video(){
int i;
av_register_all();
avcodec_register_all();
avformat_network_init();
printf("\topenning stream %s ... ", "http://192.168.1.1:5555");
if(avformat_open_input(&pFormatCtx, /*"video_test.mp4"*/"http://192.168.1.1:5555",NULL,NULL) != 0){
fprintf(stderr, "could not open stream\n");
return EXIT_FAILURE;
}
if(pFormatCtx == NULL){
fprintf(stderr, "could not open stream\n");
return EXIT_FAILURE;
}
printf("done\n");
//search video stream
printf("\tsearching video stream among %d streams... \n", pFormatCtx->nb_streams);
for(i = 0;i<pFormatCtx->nb_streams;i++){
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
videoStreamIndex = i;
}
printf("done\n");
pCodecCtx=pFormatCtx->streams[videoStreamIndex]->codec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return EXIT_FAILURE; // Codec not found
}
// Open codec
printf("\topening codec ... ");
if(avcodec_open(pCodecCtx, pCodec)<0)
return EXIT_FAILURE; // Could not open codec
printf("done\n");
printf("\tDetected size : width %d, height %d \n", pCodecCtx->width, pCodecCtx->height);
printf("\tInitializing decoded frame ... ");
for(i = 0; i < PACKET_BUFFERED_NB; i++){
pFrameYUV[i]=avcodec_alloc_frame();
if(pFrameYUV[i]==NULL) {
fprintf(stderr, "Frame not initialized!\n");
return EXIT_FAILURE;
}
}
printf("done\n");
printf("\tInitializing Mutexes ... \n");
for(i = 0; i < PACKET_BUFFERED_NB; i++){
if (pthread_mutex_init(&lock[i], NULL) != 0){
fprintf(stderr, "mutex init failed\n");
return EXIT_FAILURE;
}
}
printf("done\n");
printf("\tInitializing buffer status flags ... \n");
for(i = 0; i < PACKET_BUFFERED_NB; i++){
freeSlot[i] = 1;
}
printf("done\n");
printf("\tLaunching task ... \n");
if(pthread_create(&(tId), NULL, &taskBufferFrame, NULL) != 0){
fprintf(stderr, "Can't create task\n");
return EXIT_FAILURE;
}
printf("done\n");
printf("\tPlaying stream ... ");
av_read_play(pFormatCtx);
printf("done\n");
printf("Init done\n");
return 0;
}
/* int main(int argc, char** argv) { */
/* int frameNb = 200; */
/* //float data[640*360]; */
/* float *data = NULL; */
/* int recorded_frame = 0; */
/* data = (float*)malloc(sizeof(float) * 640*360*frameNb); */
/* if(data == NULL){ */
/* printf("Can't malloc...\n"); */
/* return -1; */
/* } */
/* printf("Initialization...\n"); */
/* init(); */
/* sleep(1); */
/* while(recorded_frame < frameNb){ */
/* get_frame((data+640*360*recorded_frame), 640, 360, 640, 1); */
/* Mat_<float> display(360, 640, data); */
/* namedWindow("display"); */
/* imshow("display", display); */
/* cvWaitKey(1); */
/* } */
/* return (0); */
/* } */