-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathffplay-20110531.c
736 lines (691 loc) · 26.9 KB
/
ffplay-20110531.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
this is the wrapper of the native functions that provided for AndZop
it also glues the decoders
single-thread version for simplicity
1. parse the video to retrieve video packets
2. takes a packet decode the video and put them into a picture/frame queue
gcc -o andzop andzop-desktop.c -lavcodec -lavformat -lavutil -lswscale -lz
current implementation only works for video without sound
**/
/**
selective decoding strategy:
1. precompute the macroblock position and their bit position offset
2. based on roi, reformulate the data packet consists of macroblocks in the roi and reset the packet size
2.1 DC and AC values are differential encoded, we'll need to consider the dependency
3. decode the reformulated video packet
4. crop the video to roi (should be done in android java api)
*/
/*
for I frame: only one type of dependency -- the differential encoding dependency
from P frame: two types of dependency -- the motion vector differential encoding dependency
the motion compensation dependency
*/
/*standard library*/
#include <time.h>
#include <math.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
/*ffmpeg headers*/
#include "libavutil/avstring.h"
//#include <libavutil/colorspace.h>
#include "libavutil/pixdesc.h"
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/opt.h"
#include "libavcodec/avfft.h"
#include "queue.h"
const char program_name[] = "FFplay";
const int program_birth_year = 2003;
#define LOGI(...) printf(__VA_ARGS__); printf("\n");
#define LOGE(...) printf(__VA_ARGS__); printf("\n");
static int gsState; //gs=>global static
char *gFileName; //the file name of the video
#define DUMP_PACKET_TYPE
#ifdef DUMP_PACKET_TYPE
FILE *packetTypeFile;
#endif
int packetNum = 0;
AVFormatContext *gFormatCtx;
int gVideoStreamIndex;
AVPacket gVideoPacket;
AVPacket gVideoPacket2;
/*structure for decoded video frame*/
typedef struct VideoPicture {
double pts;
double delay;
int width, height;
AVPicture data;
} VideoPicture;
VideoPicture gVideoPicture;
AVCodecContext *gVideoCodecCtx; //can we adjust the roi through gVideoCodecCtx ???
struct SwsContext *gImgConvertCtx;
#define MAX_FRAME_NO 500
#define MAX_MB_H 50
#define MAX_MB_W 50
int startPos[MAX_FRAME_NO][MAX_MB_H][MAX_MB_W];
int endPos[MAX_FRAME_NO][MAX_MB_H][MAX_MB_W];
#define MAX_DEP_MB 4
struct MBIdx dep[MAX_FRAME_NO][MAX_MB_H][MAX_MB_W][MAX_DEP_MB];
struct MBIdx interDep[MAX_FRAME_NO][MAX_MB_H][MAX_MB_W][MAX_DEP_MB];
int interDepMask[MAX_FRAME_NO][MAX_MB_H][MAX_MB_W];
static void parse_thread_function(void *arg);
static void decode_a_video_packet();
static void dump_frame_to_file();
static void dump_video_frame(AVFrame *frame);
static void closeVideo();
static void render_a_frame();
static void compute_selected_mb_mask(int _frameNum, int _stH, int _stW, int _edH, int _edW, int _mbHeight, int _mbWidth);
void compute_selected_mb_mask_for_single_mb(int _frameNum, struct MBIdx _mb, int _mbHeight, int _mbWidth);
/*parsing the video file, done by parse thread*/
static void parse_thread_function(void *arg) {
AVCodec *lVideoCodec;
int lError;
/*some global variables initialization*/
LOGI("parse_thread_function starts!");
/*register the codec, demux, and protocol*/
//extern AVCodec ff_h263_decoder;
//avcodec_register(&ff_h263_decoder);
//extern AVInputFormat ff_mov_demuxer;
//av_register_input_format(&ff_mov_demuxer);
//extern URLProtocol ff_file_protocol;
//av_register_protocol2(&ff_file_protocol, sizeof(ff_file_protocol));
avcodec_register_all();
av_register_all();
/*open the video file*/
LOGI("open the video file.");
if ((lError = av_open_input_file(&gFormatCtx, gFileName, NULL, 0, NULL)) !=0 ) {
LOGI("Error open video file: %d", lError);
return; //open file failed
}
/*retrieve stream information*/
LOGI("find stream information.");
if ((lError = av_find_stream_info(gFormatCtx)) < 0) {
LOGI("Error find stream information: %d", lError);
return;
}
/*find the video stream and its decoder*/
LOGI("find best stream");
gVideoStreamIndex = av_find_best_stream(gFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &lVideoCodec, 0);
LOGI("video stream index check: %d", gVideoStreamIndex);
if (gVideoStreamIndex == AVERROR_STREAM_NOT_FOUND) {
LOGI("Error: cannot find a video stream");
return;
} else {
LOGI("video codec: %s", lVideoCodec->name);
}
if (gVideoStreamIndex == AVERROR_DECODER_NOT_FOUND) {
LOGI("Error: video stream found, but no decoder is found!");
return;
} else {
LOGI("found video stream: %d", gVideoStreamIndex);
}
/*open the codec*/
gVideoCodecCtx = gFormatCtx->streams[gVideoStreamIndex]->codec;
LOGI("open codec: (%d, %d)", gVideoCodecCtx->height, gVideoCodecCtx->width);
//gVideoCodecCtx->allow_selective_decoding = 1;
//gVideoCodecCtx->coded_height = gVideoCodecCtx->height/9*7;
//gVideoCodecCtx->coded_width = gVideoCodecCtx->width/11*7;
//gVideoCodecCtx->height = gVideoCodecCtx->height/9*6;
//gVideoCodecCtx->width = gVideoCodecCtx->width/11*6;
//LOGI("updated dimension: (%d, %d)", gVideoCodecCtx->coded_height, gVideoCodecCtx->coded_width);
if (avcodec_open(gVideoCodecCtx, lVideoCodec) < 0) {
LOGI("Error: cannot open the video codec!");
return;
}
}
//copy count bits starting from startPos from data to buf starting at bufPos
static int copy_bits(unsigned char *data, unsigned char *buf, int startPos, int count, int bufPos) {
unsigned char value;
int length;
int bitsCopied;
int i;
int numOfAlignedBytes;
bitsCopied = 0;
//LOGI("*****************startPos: %d; count: %d; bufPos: %d:\n", startPos, count, bufPos);
//1. get the starting bits that are not align with byte boundary
if (startPos % 8 != 0) {
value = (*(data + startPos / 8)) & (0xFF >> (startPos % 8));
length = 8 - startPos % 8;
//LOGI("**** value: %x\n", value);
if (8 - startPos % 8 <= 8 - bufPos % 8) {
//the current byte of buf can contain all bits from data
*(buf + bufPos / 8) |= (value << (startPos % 8 - bufPos % 8));
//LOGI("0!!!!%x\n", *(buf + bufPos / 8));
} else {
//the current byte of buf cannot contain all bits from data, split into two bytes
//((8 - startPos % 8) - (8 - bufPos % 8)): the bits cannot be contained in current buf byte
*(buf + bufPos / 8) |= (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8)));
*(buf + bufPos / 8 + 1) |= (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8))));
//LOGI("0!!!!%x\n", *(buf + bufPos / 8));
//LOGI("1!!!!%x\n", *(buf + bufPos / 8 + 1));
}
bufPos += length;
bitsCopied += length;
startPos += length;
}
//2. copy the bytes from data to buf
numOfAlignedBytes = (count - bitsCopied) / 8;
for (i = 0; i < numOfAlignedBytes; ++i) {
value = *(data + startPos / 8);
//LOGI("**** value: %x\n", value);
if (8 - startPos % 8 <= 8 - bufPos % 8) {
//the current byte of buf can contain all bits from data
*(buf + bufPos / 8) |= (value << (startPos % 8 - bufPos % 8));
//LOGI("1!!!!%x\n", *(buf + bufPos / 8));
} else {
//the current byte of buf cannot contain all bits from data, split into two bytes
//((8 - startPos % 8) - (8 - bufPos % 8)): the bits cannot be contained in current buf byte
//LOGI("%x & %x\n", *(buf + bufPos / 8), (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8))));
//LOGI("%x & %x\n", *(buf + bufPos / 8 + 1), (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8)))));
*(buf + bufPos / 8) |= (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8)));
*(buf + bufPos / 8 + 1) |= (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8))));
//LOGI("1!!!!%x, %d, %d, %x, %x\n", *(buf + bufPos / 8), bufPos, startPos, (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8))), (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8)))));
//LOGI("1!!!!%x\n", *(buf + bufPos / 8 + 1));
}
bufPos += 8;
bitsCopied += 8;
startPos += 8;
}
//3. copy the last few bites from data to buf
//LOGI("bitsCopied: %d, count: %d\n", bitsCopied, count);
if (bitsCopied < count) {
value = (*(data + startPos / 8)) & (0xFF << (8 - (count - bitsCopied)));
//LOGI("**** value: %x\n", value);
if (8 - startPos % 8 <= 8 - bufPos % 8) {
//the current byte of buf can contain all bits from data
*(buf + bufPos / 8) |= (value << (startPos % 8 - bufPos % 8));
//LOGI("2!!!!%x\n", *(buf + bufPos / 8));
} else {
//the current byte of buf cannot contain all bits from data, split into two bytes
//((8 - startPos % 8) - (8 - bufPos % 8)): the bits cannot be contained in current buf byte
*(buf + bufPos / 8) |= (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8)));
*(buf + bufPos / 8 + 1) |= (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8))));
//LOGI("2!!!!%x, %d, %d, %x, %x\n", *(buf + bufPos / 8), bufPos, startPos, (unsigned char)(value >> ((8 - startPos % 8) - (8 - bufPos % 8))), (unsigned char)(value << (8 - ((8 - startPos % 8) - (8 - bufPos % 8)))));
//LOGI("1!!!!%x\n", *(buf + bufPos / 8 + 1));
}
bufPos += (count - bitsCopied);
startPos += (count - bitsCopied);
}
return bufPos;
}
static void allocate_selected_mb_mask(int mbHeight, int mbWidth) {
int li;
gVideoCodecCtx->selected_mb_mask = (unsigned char **) malloc(mbHeight * sizeof(char *));
for (li = 0; li < mbHeight; ++li) {
gVideoCodecCtx->selected_mb_mask[li] = (unsigned char *) malloc(mbWidth * sizeof(char));
}
}
static void free_selected_mb_mask(int mbHeight) {
int li;
for (li = 0; li < mbHeight; ++li) {
free(gVideoCodecCtx->selected_mb_mask[li]);
}
free(gVideoCodecCtx->selected_mb_mask);
}
//#define DUMP_PACKET
#define COMPOSE_VIDEO
#define DUMP_DEP
#ifdef DUMP_DEP
FILE *logDep;
#endif
static void decode_a_video_packet(int _roiStH, int _roiStW, int _roiEdH, int _roiEdW) {
AVFrame *lVideoFrame = avcodec_alloc_frame();
int lRet;
int lNumOfDecodedFrames;
int li, lj;
FILE *packetF;
unsigned char type;
char dumpPacketFileName[30];
//unsigned char *composedData;
int composedDataSize;
int idxh, idxw, idx;
int bufPos;
int numOfStuffingBits;
int mbWidth, mbHeight;
int firstMb;
/*read the next video packet*/
LOGI("decode_a_video_packet");
while (av_read_frame(gFormatCtx, &gVideoPacket) >= 0) {
if (gVideoPacket.stream_index == gVideoStreamIndex) {
//it's a video packet
LOGI("got a video packet, decode it");
packetNum++;
LOGI("packet number: %d", packetNum);
#ifdef COMPOSE_VIDEO
/*here we compose the video: currently for I frame and P frame only*/
//based on the dependency, compute the selected mb mask to be decoded
mbHeight = (gVideoCodecCtx->height + 15) / 16;
mbWidth = (gVideoCodecCtx->width + 15) / 16;
LOGI("~~~~~~~~~~~~~~~~~~~~%d, %d, %d, %d~~~~~~~~~~~~~~~~~~~~~\n", gVideoCodecCtx->height, gVideoCodecCtx->width, mbHeight, mbWidth);
for (li = 0; li < mbHeight; ++li) {
for (lj = 0; lj < mbWidth; ++lj) {
gVideoCodecCtx->selected_mb_mask[li][lj] = 0;
}
}
gVideoCodecCtx->roi_start_mb_x = 0; gVideoCodecCtx->roi_start_mb_y = 0;
gVideoCodecCtx->roi_end_mb_x = _roiEdW; gVideoCodecCtx->roi_end_mb_y = _roiEdH;
LOGI("the outer bound for roi at frame %d: %d;%d;%d;%d", packetNum, 0, 0, _roiEdW, _roiEdH);
//compute the needed mb mask based on intra-dependency
compute_selected_mb_mask(packetNum, _roiStH, _roiStW, _roiEdH, _roiEdW, mbHeight, mbWidth);
//add the needed mb based on inter-dependency
for (li = 0; li < mbHeight; ++li) {
for (lj = 0; lj < mbWidth; ++lj) {
if (interDepMask[packetNum][li][lj] == 1)
gVideoCodecCtx->selected_mb_mask[li][lj] = 1;
}
}
#ifdef DUMP_DEP
//dump mb mask
if (packetNum == 1) {
logDep = fopen("./logdep.txt", "w");
} else {
logDep = fopen("./logdep.txt", "a+");
}
fprintf(logDep, "----- %d -----\n", packetNum);
for (li = 0; li < mbHeight; ++li) {
for (lj = 0; lj < mbWidth; ++lj) {
fprintf(logDep, "%d ", gVideoCodecCtx->selected_mb_mask[li][lj]);
}
fprintf(logDep, "\n");
}
fprintf(logDep, "\n");
fclose(logDep);
#endif
composedDataSize = 0;
if (0) {
//get the header bits length
composedDataSize += startPos[packetNum][0][0];
//get the size for each needed mb
for (idxh = 0; idxh < mbHeight; ++idxh) {
for (idxw = 0; idxw < mbWidth; ++idxw) {
if (gVideoCodecCtx->selected_mb_mask[idxh][idxw] == 1)
composedDataSize += (endPos[packetNum][idxh][idxw] - startPos[packetNum][idxh][idxw]);
}
}
}
LOGI("total number of bits: %d\n", composedDataSize);
numOfStuffingBits = (composedDataSize + 7)/8 * 8 - composedDataSize;
composedDataSize = (composedDataSize + 7)/8;
LOGI("total number of bytes: %d; number of stuffing bits: %d\n", composedDataSize, numOfStuffingBits);
//allocate an AVPacket for the composed video data
av_new_packet(&gVideoPacket2, composedDataSize);
av_dup_packet(&gVideoPacket2);
//composedData = (unsigned char*) malloc (composedDataSize);
LOGI("%d bytes allocated", gVideoPacket2.size);
//memset(composedData, 0x00, composedDataSize);
bufPos = 0;
//bufPos = copy_bits(gVideoPacket.data, composedData, 0, startPos[packetNum][0][0], bufPos);
//copy the header data first
if (0) {
bufPos = copy_bits(gVideoPacket.data, gVideoPacket2.data, 0, startPos[packetNum][0][0], bufPos);
for (idxh = 0; idxh < mbHeight; ++idxh) {
for (idxw = 0; idxw < mbWidth; ++idxw) {
//put the data into composed video packet
if (gVideoCodecCtx->selected_mb_mask[idxh][idxw] == 1) {
bufPos = copy_bits(gVideoPacket.data, gVideoPacket2.data, startPos[packetNum][idxh][idxw], (endPos[packetNum][idxh][idxw] - startPos[packetNum][idxh][idxw]), bufPos);
}
}
}
//stuffing the last byte
for (idx = 0; idx < numOfStuffingBits; ++idx) {
//composedData[composedDataSize - 1] |= (0x01 << idx);
gVideoPacket2.data[composedDataSize - 1] |= (0x01 << idx);
}
}
#endif
//LOGI("avcodec_decode_video2\n");
avcodec_decode_video2(gVideoCodecCtx, lVideoFrame, &lNumOfDecodedFrames, &gVideoPacket);
//avcodec_decode_video2(gVideoCodecCtx, lVideoFrame, &lNumOfDecodedFrames, &gVideoPacket2);
//dump_video_frame(lVideoFrame);
if (lNumOfDecodedFrames) {
LOGI("video packet decoded, start conversion. allocate a picture (%d;%d)", gVideoPicture.width, gVideoPicture.height);
//allocate the memory space for a new VideoPicture
avpicture_alloc(&gVideoPicture.data, PIX_FMT_RGBA, gVideoPicture.width, gVideoPicture.height);
//gVideoPicture.width = gVideoCodecCtx->width;
//gVideoPicture.height = gVideoCodecCtx->height;
//convert the frame to RGB formati
LOGI("video picture data allocated, try to get a sws context. %d;%d", gVideoCodecCtx->width, gVideoCodecCtx->height);
gImgConvertCtx = sws_getCachedContext(gImgConvertCtx, gVideoCodecCtx->width, gVideoCodecCtx->height, gVideoCodecCtx->pix_fmt, gVideoPicture.width, gVideoPicture.height, PIX_FMT_RGBA, SWS_BICUBIC, NULL, NULL, NULL);
if (gImgConvertCtx == NULL) {
LOGI("Error initialize the video frame conversion context");
}
LOGI("got sws context, try to scale the video frame: from (%d, %d) to (%d, %d)", gVideoCodecCtx->width, gVideoCodecCtx->height, gVideoPicture.width, gVideoPicture.height);
sws_scale(gImgConvertCtx, lVideoFrame->data, lVideoFrame->linesize, 0, gVideoCodecCtx->height, gVideoPicture.data.data, gVideoPicture.data.linesize);
LOGI("video packet conversion done, start free memory");
/*free the packet*/
av_free_packet(&gVideoPacket);
LOGI("free packet 2: %d; %d", composedDataSize, gVideoPacket2.size);
av_free_packet(&gVideoPacket2);
LOGI("free selected_mb_mask: %d", mbHeight);
}
av_free_packet(&gVideoPacket);
av_free_packet(&gVideoPacket2);
} else {
//it's not a video packet
LOGI("it's not a video packet, continue reading!");
/*for (li = 0; li < gVideoCodecCtx->width; ++li) {
free(gVideoCodecCtx->selected_mb_mask[li]);
}
free(gVideoCodecCtx->selected_mb_mask);*/
av_free_packet(&gVideoPacket);
//free(composedData);
//gVideoPacket2.data = NULL;
//gVideoPacket2.size = 0;
//av_free_packet(&gVideoPacket2);
}
}
av_free(lVideoFrame);
}
/*for debug*/
static void dump_frame_to_file() {
FILE *pFile;
char szFilename[32];
int y, k;
LOGI("dump frame to file");
// Open file
sprintf(szFilename, "frame_%d.ppm", packetNum);
pFile=fopen(szFilename, "wb");
if(pFile==NULL) {
return;
}
// Write header
fprintf(pFile, "P6\n%d %d\n255\n", gVideoPicture.width, gVideoPicture.height);
// Write pixel data
for(y=0; y<gVideoPicture.height; y++)
for (k=0; k < gVideoPicture.width; ++k) {
fwrite(gVideoPicture.data.data[0]+y*gVideoPicture.data.linesize[0] + k*4, 1, 3, pFile);
}
// Close file
fclose(pFile);
//avpicture_free(&gVideoPicture.data);
}
static void closeVideo() {
/*close the video codec*/
avcodec_close(gVideoCodecCtx);
/*close the video file*/
av_close_input_file(gFormatCtx);
}
/*fill in data for a bitmap*/
static void render_a_frame(int _roiStH, int _roiStW, int _roiEdH, int _roiEdW) {
//take a VideoPicture nd read the data into lPixels
decode_a_video_packet(_roiStH, _roiStW, _roiEdH, _roiEdW);
LOGI("start to fill in the bitmap pixels: h: %d, w: %d", gVideoPicture.height, gVideoPicture.width);
LOGI("line size: %d", gVideoPicture.data.linesize[0]);
if (gVideoPicture.data.linesize[0] != 0) {
dump_frame_to_file();
}
}
static void load_frame_mb_index() {
FILE *logP;
char aLine[30], *aToken;
int idxF, idxH, idxW, startP, endP;
memset(startPos, 0, sizeof(startPos));
memset(endPos, 0, sizeof(endPos));
logP = fopen("./mbPos.txt", "r");
idxF = 0; idxH = 0; idxW = 0;
while (fgets(aLine, 30, logP)!=NULL) {
//parse the line
if ((aToken = strtok(aLine, ":"))!=NULL)
idxF = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxW = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
startP = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
endP = atoi(aToken);
//printf("~~~~~~~~~~~~~~~~~~~~%d:%d:%d:%d:%d\n", idxF, idxH, idxW, startP, endP);
startPos[idxF][idxH][idxW] = startP;
endPos[idxF][idxH][idxW] = endP;
}
fclose(logP);
}
void compute_selected_mb_mask_for_single_mb(int _frameNum, struct MBIdx _mb, int _mbHeight, int _mbWidth) {
//here we use breadth-first traversal based on queue
struct Queue q;
struct MBIdx mb;
int li, lj;
FILE *logDep;
initQueue(&q);
enqueue(&q, _mb);
//printf("compute_selected_mb_mask_for_single_mb\n");
while (ifEmpty(&q) == 0) {
//get the front value
mb = front(&q);
//printf("%d;%d\n", mb.h, mb.w);
//mark the corresponding position in the mask
if (gVideoCodecCtx->selected_mb_mask[mb.h][mb.w] == 1) {
//printf("already selected\n");
dequeue(&q);
continue;
}
gVideoCodecCtx->selected_mb_mask[mb.h][mb.w] = 1;
for (li = 0; li < MAX_DEP_MB; ++li) {
if (dep[_frameNum][mb.h][mb.w][li].h == -1)
break;
//printf("enqueue %d; %d\n", dep[_frameNum][mb.h][mb.w][li].h, dep[_frameNum][mb.h][mb.w][li].w);
enqueue(&q, dep[_frameNum][mb.h][mb.w][li]);
}
dequeue(&q);
}
/*logDep = fopen("./logdep1.txt", "a+");
fprintf(logDep, "---%d---\n", _frameNum);
for (li = 0; li < _mbHeight; ++li) {
for (lj = 0; lj < _mbWidth; ++lj) {
fprintf(logDep, "%d ", gVideoCodecCtx->selected_mb_mask[li][lj]);
}
fprintf(logDep, "\n");
}
fprintf(logDep, "\n");
fclose(logDep);*/
}
//based on the start pos (_stH, _stW) and end pos (_edH, _edW), compute the mb needs to be decoded
static void compute_selected_mb_mask(int _frameNum, int _stH, int _stW, int _edH, int _edW, int _mbHeight, int _mbWidth) {
int li, lj;
struct MBIdx mb;
//memset(_selectedMbs, 0, sizeof(_selectedMbs));
for (li = 0; li < _mbHeight; ++li) {
for (lj = 0; lj < _mbWidth; ++lj) {
gVideoCodecCtx->selected_mb_mask[li][lj] = 0;
}
}
for (li = _stH; li <= _edH; ++li) {
for (lj = _stW; lj <= _edW; ++lj) {
//dependency list traversing for a block
//e.g. a has two depencies mbs, b and c, we track down to b and c, mark them as selected
//then do the same for b and c as we did for a. Basically a binary tree traversal problem.
mb.h = li;
mb.w = lj;
compute_selected_mb_mask_for_single_mb(_frameNum, mb, _mbHeight, _mbWidth);
}
}
}
//starting from the last frame of the GOP, calculate the inter-dependency backwards
//if the calculation is forward, then the case below might occur:
// mb 3 in frame 3 depends on mb 2 on frame 2, but mb 2 is not decoded
// if we know the roi for the entire GOP, we can pre-calculate the needed mbs at every frame
static void compute_inter_frame_dpendency(int _startFrame, int _endFrame, int _stH, int _stW, int _edH, int _edW) {
int li, lj, lk, lm;
for (li = 0; li < MAX_FRAME_NO; ++li) {
for (lj = 0; lj < MAX_MB_H; ++lj) {
for (lk = 0; lk < MAX_MB_W; ++lk) {
interDepMask[li][lj][lk] = 0;
}
}
}
//from last frame in the GOP, going backwards to the first frame of the GOP
//1. mark the roi as needed
for (li = _endFrame; li >= _startFrame; --li) {
for (lj = _stH; lj <= _edH; ++lj) {
for (lk = _stW; lk <= _edW; ++lk) {
interDepMask[li][lj][lk] = 1;
}
}
}
//2. based on the inter-dependency list, mark the needed mb
for (li = _endFrame; li >= _startFrame; --li) {
for (lj = 0; lj <= MAX_MB_H; ++lj) {
for (lk = 0; lk <= MAX_MB_W; ++lk) {
if (interDepMask[li][lj][lk] == 1) {
for (lm = 0; lm < MAX_DEP_MB; ++lm) {
//mark the needed mb in the previous frame
if (interDep[li][lj][lk][lm].h == -1)
break;
interDepMask[li-1][interDep[li][lj][lk][lm].h][interDep[li][lj][lk][lm].w] = 1;
}
}
}
}
}
}
//TODO: load depdencies can be simplied by passing file name as input parameter
static void load_frame_mb_inter_dependency() {
FILE *logP;
char aLine[40], *aToken;
int idxF, idxH, idxW, ldepH, ldepW, curDepIdx;
for (idxF = 0; idxF < MAX_FRAME_NO; ++idxF) {
for (idxH = 0; idxH < MAX_MB_H; ++idxH) {
for (idxW = 0; idxW < MAX_MB_W; ++idxW) {
for (ldepW = 0; ldepW < MAX_DEP_MB; ++ldepW) {
interDep[idxF][idxH][idxW][ldepW].h = -1;
interDep[idxF][idxH][idxW][ldepW].w = -1;
}
}
}
}
logP = fopen("./inter.txt", "r");
while (fgets(aLine, 48, logP)!=NULL) {
//get the frame number, mb position first
if ((aToken = strtok(aLine, ":"))!=NULL)
idxF = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxW = atoi(aToken);
//get the depency mb
curDepIdx = 0;
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) ldepH = atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) ldepW = atoi(aToken);
else break;
//put the dependencies into the array
interDep[idxF][idxH][idxW][curDepIdx].h = ldepH;
interDep[idxF][idxH][idxW][curDepIdx++].w = ldepW;
} while (aToken != NULL);
}
fclose(logP);
}
static void load_frame_mb_dependency() {
FILE *logP;
char aLine[40], *aToken;
int idxF, idxH, idxW, ldepH, ldepW, curDepIdx;
for (idxF = 0; idxF < MAX_FRAME_NO; ++idxF) {
for (idxH = 0; idxH < MAX_MB_H; ++idxH) {
for (idxW = 0; idxW < MAX_MB_W; ++idxW) {
for (ldepW = 0; ldepW < MAX_DEP_MB; ++ldepW) {
dep[idxF][idxH][idxW][ldepW].h = -1;
dep[idxF][idxH][idxW][ldepW].w = -1;
}
}
}
}
logP = fopen("./intra.txt", "r");
while (fgets(aLine, 40, logP)!=NULL) {
//parse the line
//get the frame number, mb position first
if ((aToken = strtok(aLine, ":"))!=NULL)
idxF = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
idxW = atoi(aToken);
//get the depency mb
curDepIdx = 0;
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) ldepH = atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) ldepW = atoi(aToken);
else break;
//put the dependencies into the array
dep[idxF][idxH][idxW][curDepIdx].h = ldepH;
dep[idxF][idxH][idxW][curDepIdx++].w = ldepW;
} while (aToken != NULL);
}
fclose(logP);
}
/*load both intra and inter dependencies*/
static void load_dependencies() {
load_frame_mb_index();
load_frame_mb_dependency();
load_frame_mb_inter_dependency();
}
/*decode the a video gop
constraints within a GOP: 1. no ROI change.
the needed mbs due to intra frame dependencies are computed when we decode each frame
the needed mbs due to inter frame dependencies are pre-computed for this GOP and then start decode it*/
static void decode_a_gop(int _stFrame, int _endFrame, int _roiSh, int _roiSw, int _roiEh, int _roiEw) {
int i;
compute_inter_frame_dpendency(_stFrame, _endFrame, _roiSh, _roiSw, _roiEh, _roiEw);
for (i = _stFrame; i <= _endFrame; ++i) {
render_a_frame(_roiSh, _roiSw, _roiEh, _roiEw);
}
}
int main(int argc, char **argv) {
/*get the video file name*/
int i = 0;
FILE *gopRecF;
char gopRecLine[50], *aToken;
int startFrame, endFrame;
int currentGop = 0;
int mbHeight, mbWidth;
packetTypeFile = fopen("packet_type.txt", "w");
gFileName = "atest1.3gp";
gVideoPicture.width = 176;
//gVideoPicture.width = 720;
//gVideoPicture.width = 800;
gVideoPicture.height = 144;
//gVideoPicture.height = 480;
//gVideoPicture.height = 480;
if (gFileName == NULL) {
LOGI("Error: cannot get the video file name!");
return 0;
}
LOGI("video file name is %s", gFileName);
parse_thread_function(NULL);
//*parse_thread_function(NULL);
LOGI("initialization done");
mbHeight = (gVideoCodecCtx->height + 15) / 16;
mbWidth = (gVideoCodecCtx->width + 15) / 16;
allocate_selected_mb_mask(mbHeight, mbWidth);
load_dependencies();
//read the gop information
gopRecF = fopen("./goprec.txt", "r");
while (fgets(gopRecLine, 51, gopRecF)!=NULL) {
//parse a line of gop
if ((aToken = strtok(gopRecLine, ":"))!=NULL)
startFrame = atoi(aToken);
if ((aToken = strtok(NULL, ":"))!=NULL)
endFrame = atoi(aToken);
LOGI("@@@@@@@@@@@@@@@@%d;%d\n", startFrame, endFrame);
decode_a_gop(startFrame, endFrame, 0, 0, 8, 8);
++currentGop;
//break;
}
fclose(gopRecF);
fclose(packetTypeFile);
free_selected_mb_mask(mbHeight);
return 0;
}