-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdependency.c
1425 lines (1388 loc) · 67.6 KB
/
dependency.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "dependency.h"
static int videoIndexCmp(const int *a, const int *b) {
LOGI(10, "videoIndexCmp: %d, %d, %d, %d", *a, *b, gVideoCodecCtxList[*a]->width * gVideoCodecCtxList[*a]->height, gVideoCodecCtxList[*b]->width * gVideoCodecCtxList[*b]->height);
if ((gVideoCodecCtxList[*a]->width * gVideoCodecCtxList[*a]->height) > (gVideoCodecCtxList[*b]->width * gVideoCodecCtxList[*b]->height)) {
return 1;
} else if ((gVideoCodecCtxList[*a]->width * gVideoCodecCtxList[*a]->height) < (gVideoCodecCtxList[*b]->width * gVideoCodecCtxList[*b]->height)) {
return -1;
} else {
return 0;
}
}
/*parsing the video file, done by parse thread*/
void get_video_info(int p_debug) {
AVCodec *lVideoCodec;
int lError;
int l_dumpDep;
int l_i;
/*some global variables initialization*/
extern AVCodec ff_h263_decoder;
extern AVCodec ff_h264_decoder;
extern AVCodec ff_mpeg4_decoder;
extern AVCodec ff_mjpeg_decoder;
extern AVInputFormat ff_mov_demuxer;
extern URLProtocol ff_file_protocol;
LOGI(10, "get video info starts!");
/*register the codec*/
avcodec_register(&ff_h263_decoder);
avcodec_register(&ff_h264_decoder);
avcodec_register(&ff_mpeg4_decoder);
avcodec_register(&ff_mjpeg_decoder);
/*register parsers*/
//extern AVCodecParser ff_h264_parser;
//av_register_codec_parser(&ff_h264_parser);
//extern AVCodecParser ff_mpeg4video_parser;
//av_register_codec_parser(&ff_mpeg4video_parser);
/*register demux*/
av_register_input_format(&ff_mov_demuxer);
//extern AVInputFormat ff_h264_demuxer;
//av_register_input_format(&ff_h264_demuxer);
/*register the protocol*/
av_register_protocol2(&ff_file_protocol, sizeof(ff_file_protocol));
/*initialize the lists*/
gFormatCtxList = (AVFormatContext*)malloc(gNumOfVideoFiles*sizeof(AVFormatContext));
gVideoStreamIndexList = (int*)malloc(gNumOfVideoFiles*sizeof(int));
gVideoCodecCtxList = (AVCodecContext*)malloc(gNumOfVideoFiles*sizeof(AVCodecContext));
gVideoPacketQueueList = (PacketQueue*)malloc(gNumOfVideoFiles*sizeof(PacketQueue));
gFormatCtxDepList = (AVFormatContext*)malloc(gNumOfVideoFiles*sizeof(AVFormatContext));
gVideoCodecCtxDepList = (AVCodecContext*)malloc(gNumOfVideoFiles*sizeof(AVCodecContext));
gVideoPacketDepList = (AVPacket*)malloc(gNumOfVideoFiles*sizeof(AVPacket));
for (l_i = 0; l_i < gNumOfVideoFiles; ++l_i) {
packet_queue_init(&gVideoPacketQueueList[l_i]); //initialize the packet queue
gVideoPacketQueueList[l_i].dep_gop_num = 1;
g_decode_gop_num = 1;
gVideoPacketQueueList[l_i].nb_packets = 0;
}
for (l_i = 0; l_i < gNumOfVideoFiles; ++l_i) {
#if (defined SELECTIVE_DECODING) || (defined NORM_DECODE_DEBUG)
/***
The following section are initialization for dumping dependencies
**/
/*open the video file*/
if ((lError = av_open_input_file(&gFormatCtxDepList[l_i], gVideoFileNameList[l_i], NULL, 0, NULL)) !=0 ) {
LOGE(1, "Error open video file: %d", lError);
return; //open file failed
}
/*retrieve stream information*/
if ((lError = av_find_stream_info(gFormatCtxDepList[l_i])) < 0) {
LOGE(1, "Error find stream information: %d", lError);
return;
}
/*find the video stream and its decoder*/
gVideoStreamIndexList[l_i] = av_find_best_stream(gFormatCtxDepList[l_i], AVMEDIA_TYPE_VIDEO, -1, -1, &lVideoCodec, 0);
if (gVideoStreamIndexList[l_i] == AVERROR_STREAM_NOT_FOUND) {
LOGE(1, "Error: cannot find a video stream");
return;
} else {
LOGI(10, "video codec: %s; stream index: %d", lVideoCodec->name, gVideoStreamIndexList[l_i]);
}
if (gVideoStreamIndexList[l_i] == AVERROR_DECODER_NOT_FOUND) {
LOGE(1, "Error: video stream found, but no decoder is found!");
return;
}
/*open the codec*/
gVideoCodecCtxDepList[l_i] = gFormatCtxDepList[l_i]->streams[gVideoStreamIndexList[l_i]]->codec;
LOGI(10, "open codec for dumping dep: (%d, %d)", gVideoCodecCtxDepList[l_i]->height, gVideoCodecCtxDepList[l_i]->width);
if (avcodec_open(gVideoCodecCtxDepList[l_i], lVideoCodec) < 0) {
LOGE(1, "Error: cannot open the video codec!");
return;
}
#endif
/***
The following section are initialization for decoding
**/
if ((lError = av_open_input_file(&gFormatCtxList[l_i], gVideoFileNameList[l_i], NULL, 0, NULL)) !=0 ) {
LOGE(1, "Error open video file: %d", lError);
return; //open file failed
}
/*retrieve stream information*/
LOGI(1, "try find stream info");
if ((lError = av_find_stream_info(gFormatCtxList[l_i])) < 0) {
LOGE(1, "Error find stream information: %d", lError);
return;
}
LOGI(1, "stream info retrieved");
/*find the video stream and its decoder*/
gVideoStreamIndexList[l_i] = av_find_best_stream(gFormatCtxList[l_i], AVMEDIA_TYPE_VIDEO, -1, -1, &lVideoCodec, 0);
if (gVideoStreamIndexList[l_i] == AVERROR_STREAM_NOT_FOUND) {
LOGE(1, "Error: cannot find a video stream");
return;
} else {
LOGI(10, "video codec: %s; stream index: %d", lVideoCodec->name, gVideoStreamIndexList[l_i]);
}
if (gVideoStreamIndexList[l_i] == AVERROR_DECODER_NOT_FOUND) {
LOGE(1, "Error: video stream found, but no decoder is found!");
return;
}
/*open the codec*/
gVideoCodecCtxList[l_i] = gFormatCtxList[l_i]->streams[gVideoStreamIndexList[l_i]]->codec;
LOGI(10, "open codec: (%d, %d)", gVideoCodecCtxList[l_i]->height, gVideoCodecCtxList[l_i]->width);
if (avcodec_open(gVideoCodecCtxList[l_i], lVideoCodec) < 0) {
LOGE(1, "Error: cannot open the video codec!");
return;
}
#ifdef SELECTIVE_DECODING
LOGI(10, "SELECTIVE_DECODING is enabled");
gVideoCodecCtxList[l_i]->allow_selective_decoding = 1;
#else
LOGI(10, "SELECTIVE_DECODING is disabled");
gVideoCodecCtxList[l_i]->allow_selective_decoding = 0;
#endif
/*set the debug option*/
gVideoCodecCtxList[l_i]->debug_selective = p_debug;
if (gVideoCodecCtxList[l_i]->debug_selective == 1) {
//clear the old dump file
#ifdef ANDROID_BUILD
FILE *dctF = fopen("/sdcard/r10videocam/debug_dct.txt", "w");
#else
FILE *dctF = fopen("./debug_dct.txt", "w");
#endif
fclose(dctF);
}
}
//if there's multiple inputs, we'll need to come up with a zoom level index to video file index map
gZoomLevelToVideoIndex = (int*) malloc(gNumOfVideoFiles*sizeof(int));
for (l_i = 0; l_i < gNumOfVideoFiles; ++l_i) {
gZoomLevelToVideoIndex[l_i] = l_i;
}
qsort(gZoomLevelToVideoIndex, gNumOfVideoFiles, sizeof(int), videoIndexCmp);
gCurrentDecodingVideoFileIndex = gZoomLevelToVideoIndex[0];
for (l_i = 0; l_i < gNumOfVideoFiles; ++l_i) {
LOGI(10, "zoom level to video index map %d: %d", l_i, gZoomLevelToVideoIndex[l_i]);
}
LOGI(10, "get video info ends");
}
//TODO: more fields to clear
void allocate_selected_decoding_fields(int p_videoFileIndex, int _mbHeight, int _mbWidth) {
int l_i;
LOGI(10, "allocate %d video selected decoding fields: %d, %d", p_videoFileIndex, _mbHeight, _mbWidth);
gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask = (unsigned char **) malloc(_mbHeight * sizeof(unsigned char *));
for (l_i = 0; l_i < _mbHeight; ++l_i) {
gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask[l_i] = (unsigned char *) malloc(_mbWidth * sizeof(unsigned char));
}
/*gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir = (unsigned char **) malloc(_mbHeight * sizeof(unsigned char *));
for (l_i = 0; l_i < _mbHeight; ++l_i) {
gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[l_i] = (unsigned char *) malloc(_mbWidth * sizeof(unsigned char));
}*/
LOGI(10, "allocate %d video selected decoding fields: %d, %d is done", p_videoFileIndex, _mbHeight, _mbWidth);
}
//TODO: more fields to clear
void free_selected_decoding_fields(int p_videoFileIndex, int _mbHeight) {
int l_i;
for (l_i = 0; l_i < _mbHeight; ++l_i) {
free(gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask[l_i]);
}
free(gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask);
/*for (l_i = 0; l_i < _mbHeight; ++l_i) {
free(gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[l_i]);
}
free(gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir);*/
}
int *mbStartPos;
int mapStLen;
int mbStartFd;
int *mbEndPos;
int mapEdLen;
int mbEndFd;
//struct MBIdx intraDep[MAX_FRAME_NUM_IN_GOP][MAX_MB_H][MAX_MB_W][MAX_DEP_MB];
//struct MBIdx interDep[MAX_FRAME_NUM_IN_GOP][MAX_MB_H][MAX_MB_W][MAX_DEP_MB];
int interDepMask[MAX_FRAME_NUM_IN_GOP][MAX_MB_H][MAX_MB_W];
/*load frame mb index from frame _stFrame to frame _edFrame*/
/*static void load_frame_mb_index(int p_videoFileIndex, int _stFrame, int _edFrame) {
char aLine[30];
//char temp[30];
char *aToken;
int idxF, idxH, idxW, stP, edP;
LOGI(10, "+++++load_frame_mb_index: %d to %d\n", _stFrame, _edFrame);
if (gVideoCodecCtxList[p_videoFileIndex]->g_mbPosF == NULL) {
LOGE(1, "Error: no valid mb index records!!!");
}
memset(mbStartPos, 0, MAX_FRAME_NUM_IN_GOP*MAX_MB_H*MAX_MB_W);
memset(mbEndPos, 0, MAX_FRAME_NUM_IN_GOP*MAX_MB_H*MAX_MB_W);
idxF = 0; idxH = 0; idxW = 0;
while (fgets(aLine, 30, gVideoCodecCtxList[p_videoFileIndex]->g_mbPosF) != NULL) {
//strcpy(temp, aLine);
//parse the line
if ((idxF == _edFrame) && (strcmp(aLine, "\n") == 0)) {
//we must continue to read to the last line of this frame, otherwise, only the first line of
//this frame is read, the empty space indicates the end of a frame
LOGI(10, "+++++++load_frame_mb_index finished");
break;
}
if (idxF > _edFrame) {
LOGI(10, "+++++++load_frame_mb_index finished, overread");
break;
}
//LOGI(10, "line in mb pos file: %s", aLine);
if ((aToken = strtok(aLine, ":")) != NULL)
idxF = atoi(aToken);
if (idxF < _stFrame) {
//not the start frame yet, continue reading
//LOGI(10, "not the start frame yet, continue reading, %d:%d-%s", idxF, _stFrame, temp);
continue;
}
if ((aToken = strtok(NULL, ":")) != NULL)
idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
idxW = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
stP = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
edP = atoi(aToken);
mbStartPos[idxF - _stFrame][idxH][idxW] = stP;
mbEndPos[idxF - _stFrame][idxH][idxW] = edP;
}
LOGI(10, "+++++load_frame_mb_index finished, exit the function");
}*/
void unload_frame_mb_stindex(void) {
close(mbStartFd);
munmap(mbStartPos, mapStLen);
}
void unload_frame_mb_edindex(void) {
close(mbEndFd);
munmap(mbEndPos, mapEdLen);
}
void load_frame_mb_stindex(int p_videoFileIndex) {
struct stat sbuf;
char l_mbStPosFileName[100];
sprintf(l_mbStPosFileName, "./%s_mbstpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], g_decode_gop_num);
LOGI(10, "+++++load_frame_mb_stindex: %s", l_mbStPosFileName);
//if (fd = open(gVideoCodecCtxList[p_videoFileIndex]->g_mbStPosFileName, O_RDONLY) == -1) {
if ((mbStartFd = open(l_mbStPosFileName, O_RDONLY)) == -1) {
LOGE(1, "file open error: %s", gVideoCodecCtxList[p_videoFileIndex]->g_mbStPosFileName);
perror("file open error: ");
exit(1);
}
//if (stat(gVideoCodecCtxList[p_videoFileIndex]->g_mbStPosFileName, &sbuf) == -1) {
if (stat(l_mbStPosFileName, &sbuf) == -1) {
LOGE(1, "stat error");
exit(1);
}
LOGI(10, "file size: %ld", sbuf.st_size);
//mbStartPos = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
//mbStartPos = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
//mbStartPos = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
mapStLen = sbuf.st_size;
mbStartPos = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, mbStartFd, 0) ;
//mbStartPos = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mbStartPos == MAP_FAILED) {
LOGE(1, "mmap error");
perror("mmap error: ");
exit(1);
}
//TODO: preload the data
LOGI(10, "+++++load_frame_mb_stindex finished, exit the function");
}
void load_frame_mb_edindex(int p_videoFileIndex) {
struct stat sbuf;
char l_mbEdPosFileName[100];
sprintf(l_mbEdPosFileName, "./%s_mbedpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], g_decode_gop_num);
LOGI(10, "+++++load_frame_mb_edindex, file: %s", l_mbEdPosFileName);
if ((mbEndFd = open(l_mbEdPosFileName, O_RDONLY)) == -1) {
LOGE(1, "file open error");
exit(1);
}
if (stat(l_mbEdPosFileName, &sbuf) == -1) {
LOGE(1, "stat error");
exit(1);
}
LOGI(10, "file size: %ld", sbuf.st_size);
//mbEndPos = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
//mbEndPos = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
mapEdLen = sbuf.st_size;
mbEndPos = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, mbEndFd, 0);
if (mbEndPos == MAP_FAILED) {
LOGE(1, "mmap error");
perror("mmap error: ");
exit(1);
}
//TODO: preload the data
LOGI(10, "+++++load_frame_mb_edindex finished, exit the function");
}
unsigned char *intraDepMap, *intraDepMapMove;
long intraDepMapLen;
int intraDepFd;
void unload_intra_frame_mb_dependency(void) {
LOGI(10, "unload_intra_frame_mb_dependency: %d", intraDepMapLen);
close(intraDepFd);
if (munmap(intraDepMap, intraDepMapLen)!=0) {
LOGE(1, "munmap error!");
exit(0);
}
}
static void load_intra_frame_mb_dependency(int p_videoFileIndex, int p_gopNumber) {
char l_depIntraFileName[100];
struct stat sbuf;
sprintf(l_depIntraFileName, "./%s_intra_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNumber);
LOGI(10, "+++++load_intra_frame_mb_dependency, file: %s", l_depIntraFileName);
if ((intraDepFd = open(l_depIntraFileName, O_RDONLY)) == -1) {
LOGE(1, "file open error");
exit(1);
}
if (stat(l_depIntraFileName, &sbuf) == -1) {
LOGE(1, "stat error");
exit(1);
}
LOGI(10, "file size: %ld", sbuf.st_size);
//mbEndPos = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
//mbEndPos = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
intraDepMapLen = sbuf.st_size;
intraDepMap = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, intraDepFd, 0);
intraDepMapMove = intraDepMap;
if (intraDepMap == MAP_FAILED) {
LOGE(1, "mmap error");
perror("mmap error: ");
exit(1);
}
LOGI(10, "+++++load_intra_frame_mb_dependency finished, exit the function");
}
/*static void load_intra_frame_mb_dependency(int p_videoFileIndex, int _stFrame, int _edFrame) {
char aLine[40], *aToken;
int l_idxF, l_idxH, l_idxW, l_depH, l_depW, l_curDepIdx;
LOGI(10, "load_intra_frame_mb_dependency\n");
if (gVideoCodecCtxList[p_videoFileIndex]->g_intraDepF == NULL) {
LOGE(1, "no valid intra frame mb dependency!!!");
}
for (l_idxF = 0; l_idxF < MAX_FRAME_NUM_IN_GOP; ++l_idxF) {
for (l_idxH = 0; l_idxH < MAX_MB_H; ++l_idxH) {
for (l_idxW = 0; l_idxW < MAX_MB_W; ++l_idxW) {
for (l_curDepIdx = 0; l_curDepIdx < MAX_DEP_MB; ++l_curDepIdx) {
intraDep[l_idxF][l_idxH][l_idxW][l_curDepIdx].h = -1;
intraDep[l_idxF][l_idxH][l_idxW][l_curDepIdx].w = -1;
}
}
}
}
LOGI(10, "load_intra_frame_mb_dependency init done");
//memset(intraDep, 0, sizeof(interDep[0][0][0][0])*MAX_FRAME_NUM_IN_GOP*MAX_MB_H*MAX_MB_W*MAX_DEP_MB);
while (fgets(aLine, 40, gVideoCodecCtxList[p_videoFileIndex]->g_intraDepF) != NULL) {
//parse the line
//get the frame number, mb position first
if ((aToken = strtok(aLine, ":")) != NULL)
l_idxF = atoi(aToken);
if (l_idxF < _stFrame) {
continue;
}
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxW = atoi(aToken);
l_curDepIdx = 0;
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depH = atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depW = atoi(aToken);
else break;
//put the dependencies into the array
intraDep[l_idxF - _stFrame][l_idxH][l_idxW][l_curDepIdx].h = l_depH;
intraDep[l_idxF - _stFrame][l_idxH][l_idxW][l_curDepIdx++].w = l_depW;
} while (aToken != NULL);
if (l_idxF == _edFrame) {
break;
}
}
LOGI(10, "load_intra_frame_mb_dependency finished\n");
}*/
unsigned char *interDepMap, *interDepMapMove;
long interDepMapLen;
int interDepFd;
void unload_inter_frame_mb_dependency(void) {
close(interDepFd);
munmap(interDepMap, interDepMapLen);
}
static void load_inter_frame_mb_dependency(int p_videoFileIndex) {
char l_depInterFileName[100];
struct stat sbuf;
sprintf(l_depInterFileName, "./%s_inter_gop%d.txt", gVideoFileNameList[p_videoFileIndex], g_decode_gop_num);
LOGI(10, "+++++load_inter_frame_mb_dependency, file: %s", l_depInterFileName);
if ((interDepFd = open(l_depInterFileName, O_RDONLY)) == -1) {
LOGE(1, "file open error");
exit(1);
}
if (stat(l_depInterFileName, &sbuf) == -1) {
LOGE(1, "stat error");
exit(1);
}
LOGI(10, "file size: %ld", sbuf.st_size);
//mbEndPos = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
//mbEndPos = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
interDepMapLen = sbuf.st_size;
interDepMap = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, interDepFd, 0);
interDepMapMove = interDepMap;
if (interDepMap == MAP_FAILED) {
LOGE(1, "mmap error");
perror("mmap error: ");
exit(1);
}
LOGI(10, "+++++load_inter_frame_mb_dependency finished, exit the function");
}
/*static void load_inter_frame_mb_dependency(int p_videoFileIndex, int _stFrame, int _edFrame) {
char aLine[40], *aToken;
int l_idxF, l_idxH, l_idxW, l_depH, l_depW, l_curDepIdx;
LOGI(10, "load_inter_frame_mb_dependency for video %d: %d: %d\n", p_videoFileIndex, _stFrame, _edFrame);
if (gVideoCodecCtxList[p_videoFileIndex]->g_interDepF == NULL) {
LOGE(1, "no valid inter frame mb dependency!!!");
}
memset(interDep, 0, sizeof(interDep[0][0][0][0])*MAX_FRAME_NUM_IN_GOP*MAX_MB_H*MAX_MB_W*MAX_DEP_MB);
while (fgets(aLine, 40, gVideoCodecCtxList[p_videoFileIndex]->g_interDepF) != NULL) {
//get the frame number, mb position first
if ((aToken = strtok(aLine, ":")) != NULL)
l_idxF = atoi(aToken);
if (l_idxF < _stFrame) {
continue;
}
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxW = atoi(aToken);
//get the dependency mb
l_curDepIdx = 0;
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depH = atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depW = atoi(aToken);
else break;
//put the dependencies into the array
if ((l_idxH < MAX_MB_H) && (l_idxW < MAX_MB_W)) {
interDep[l_idxF - _stFrame][l_idxH][l_idxW][l_curDepIdx].h = l_depH;
interDep[l_idxF - _stFrame][l_idxH][l_idxW][l_curDepIdx++].w = l_depW;
} else {
LOGI(1, "*******Error***********************************************");
}
} while (aToken != NULL);
if (l_idxF == _edFrame) {
break;
}
}
}*/
long dcpMapLen;
unsigned char *dcpPos, *dcpPosMove;
int dcpFd;
void unload_frame_dc_pred_direction(void) {
close(dcpFd);
munmap(dcpPos, dcpMapLen);
}
static void load_gop_dc_pred_direction(int p_videoFileIndex) {
char l_dcPredFileName[100];
struct stat sbuf;
sprintf(l_dcPredFileName, "./%s_dcp_gop%d.txt", gVideoFileNameList[p_videoFileIndex], g_decode_gop_num);
LOGI(10, "load_gop_dc_pred_direction: %s\n", l_dcPredFileName);
if ((dcpFd = open(l_dcPredFileName, O_RDONLY)) == -1) {
LOGE(1, "file open error: %s", l_dcPredFileName);
perror("file open error: ");
exit(1);
}
if (stat(l_dcPredFileName, &sbuf) == -1) {
LOGE(1, "stat error");
exit(1);
}
dcpMapLen = sbuf.st_size;
LOGI(9, "file size: %ld", dcpMapLen);
dcpPos = mmap(0, dcpMapLen, PROT_READ, MAP_PRIVATE, dcpFd, 0);
dcpPosMove = dcpPos;
if (dcpPos == MAP_FAILED) {
LOGE(1, "map error");
perror("mmap error:");
exit(1);
}
LOGI(10, "load_gop_dc_pred_direction done\n");
}
static void load_frame_dc_pred_direction(int p_videoFileIndex, int _height, int _width) {
// FILE *testF;
int i, j;
LOGI(10, "load_frame_dc_pred_direction\n");
//memset(gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir, 0, _height*_width*sizeof(gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[0][0]));
gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir = dcpPosMove;
/*testF = fopen("test.txt", "a+");
for (i = 0; i < _height; ++i) {
for (j = 0; j < _width; ++j) {
//fprintf("%d:%d:%d\n", i, j, *(gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir + (i*_width) + j));
fprintf(testF, "%d:%d:%d\n", i, j, gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[(i*_width) + j]);
}
}
fclose(testF);*/
dcpPosMove += _height*_width;
LOGI(10, "load_frame_dc_pred_direction done\n");
}
/*static void load_frame_dc_pred_direction(int p_videoFileIndex, int _frameNum, int _height, int _width) {
int l_i, l_j, l_idxF, l_idxH, l_idxW, l_idxDir;
char aLine[40], *aToken;
LOGI(10, "load_frame_dc_pred_direction\n");
if (gVideoCodecCtxList[p_videoFileIndex]->g_dcPredF==NULL) {
LOGI(1, "no valid dc pred!!!");
}
for (l_i = 0; l_i < _height; ++l_i) {
for (l_j = 0; l_j < _width; ++l_j) {
gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[l_i][l_j] = 0;
}
}
while (fgets(aLine, 40, gVideoCodecCtxList[p_videoFileIndex]->g_dcPredF) != NULL) {
//get the frame number, mb position first
if ((aToken = strtok(aLine, ":")) != NULL)
l_idxF = atoi(aToken);
if (l_idxF < _frameNum) {
continue;
} else if (l_idxF > _frameNum) {
//continue to parse the string to get length
l_i = strlen(aToken) + 1;
aToken = strtok(NULL, "\n");
l_i += strlen(aToken) + 1;
//go back to the previous line
fseek(gVideoCodecCtxList[p_videoFileIndex]->g_dcPredF, -l_i, SEEK_CUR);
//LOGI(10, "go back: %d", l_i);
break;
}
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxW = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxDir = atoi(aToken);
//get the dependency mb
gVideoCodecCtxList[p_videoFileIndex]->pred_dc_dir[l_idxH][l_idxW] = l_idxDir;
}
LOGI(10, "load_frame_dc_pred_direction done\n");
}*/
/*done on a GOP basis*/
static void load_pre_computation_result(int p_videoFileIndex, int _stFrame, int _edFrame) {
//load_frame_mb_index(p_videoFileIndex, _stFrame, _edFrame); //the mb index position
load_frame_mb_stindex(p_videoFileIndex); //the mb index position
load_frame_mb_edindex(p_videoFileIndex); //the mb index position
load_intra_frame_mb_dependency(p_videoFileIndex, g_decode_gop_num); //the intra-frame dependency
load_inter_frame_mb_dependency(p_videoFileIndex); //the inter-frame dependency
load_gop_dc_pred_direction(p_videoFileIndex); //the dc prediction direction
}
void dump_frame_to_file(int _frameNum) {
FILE *l_pFile;
char l_filename[32];
int y, k;
LOGI(10, "dump frame to file");
//open file
#ifdef ANDROID_BUILD
sprintf(l_filename, "/sdcard/r10videocam/frame_%d.ppm", _frameNum);
#else
sprintf(l_filename, "frame_%d.ppm", _frameNum);
#endif
l_pFile = fopen(l_filename, "wb");
if (l_pFile == NULL)
return;
//write header
fprintf(l_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, l_pFile);
}
}
fclose(l_pFile);
LOGI(10, "frame dumped to file");
}
//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;
//1. get the starting bits that are not align with byte boundary
//[TODO] this part is a bit tricky to consider all situations, better to build a unit test for it
if (startPos % 8 != 0) {
length = (8 - startPos % 8) < count ? (8 - startPos % 8):count; //use count if count cannot fill to the byte boundary
value = (*(data + startPos / 8)) & (0xFF >> (startPos % 8)) & (0xFF << (8 - startPos % 8 - length));
//LOGI("**** value: %x, %d\n", value, length);
if (8 - startPos % 8 <= 8 - bufPos % 8) {
//LOGI("0!!!!%d, %d", bufPos / 8, startPos % 8 - bufPos % 8);
//LOGI("0!!!!%x", *(buf + bufPos / 8));
//LOGI("0!!!!%x", *(buf + bufPos / 8));
//the current byte of buf can contain all bits from data
*(buf + bufPos / 8) |= (value << (startPos % 8 - bufPos % 8));
//LOGI("0!!!!%x", *(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("0!!!!%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("2!!!!%x\n", *(buf + bufPos / 8 + 1));
}
bufPos += (count - bitsCopied);
startPos += (count - bitsCopied);
}
return bufPos;
}
static void compute_mb_mask_from_intra_frame_dependency_for_single_mb(int p_videoFileIndex, int _stFrame, int _frameNum, struct MBIdx _Pmb, int _height, int _width) {
struct Queue l_q;
struct MBIdx l_mb, l_mb2;
int l_i;
unsigned char *p, *pframe;
//FILE *testF;
//char testFileName[50];
//sprintf(testFileName, "testi_%d_%d", _frameNum, _stFrame);
//testF = fopen(testFileName, "a+");
initQueue(&l_q);
enqueue(&l_q, _Pmb);
pframe = intraDepMap + (_frameNum - _stFrame)*_height*_width*6;
//fprintf(testF, "location: %d\n", intraDepMap);
while (ifEmpty(&l_q) == 0) {
//get the front value
l_mb = front(&l_q);
//mark the corresponding position in the mask
if (gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask[l_mb.h][l_mb.w] > 1) {
//it has been tracked before
dequeue(&l_q);
continue;
}
//fprintf(testF, "dequeue: %d:%d: ", l_mb.h, l_mb.w);
gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask[l_mb.h][l_mb.w]++;
for (l_i = 0; l_i < 3; ++l_i) {
p = pframe + (l_mb.h*_width + l_mb.w)*6 + l_i*2;
if ((*p !=0) || (*(p+1) != 0)) {
l_mb2.h = *p;
l_mb2.w = *(p+1);
enqueue(&l_q, l_mb2);
//fprintf(testF, "enqueue: %d:%d: ", l_mb2.h, l_mb2.w);
}
}
dequeue(&l_q);
}
//fprintf(testF, "\n");
//fclose(testF);
}
/*based on the start pos (_stH, _stW) and end pos (_edH, _edW), compute the mb needed to decode the roi due to inter-frame dependency
for I-frame: intra-frame dependency are due to differential encoding of DC and AC coefficients
for P-frame: intra-frame dependency are due to differential encoding of motion vectors*/
static void compute_mb_mask_from_intra_frame_dependency(int p_videoFileIndex, int _stFrame, int _frameNum, int _height, int _width) {
int l_i, l_j;
struct MBIdx l_mb;
for (l_i = 0; l_i < _height; ++l_i) {
for (l_j = 0; l_j < _width; ++l_j) {
//dependency list traversing for a block
//e.g. mb A has two dependencies mb B and C. We track down to B and C, mark them as needed, then do the same for B and C as we did for A.
//basically a tree traversal problem.
if (gVideoCodecCtxList[p_videoFileIndex]->selected_mb_mask[l_i][l_j] == 1) {
l_mb.h = l_i;
l_mb.w = l_j;
compute_mb_mask_from_intra_frame_dependency_for_single_mb(p_videoFileIndex, _stFrame, _frameNum, l_mb, _height, _width);
}
}
}
}
/*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_mb_mask_from_inter_frame_dependency(int p_videoFileIndex, int _stFrame, int _edFrame, int _stH, int _stW, int _edH, int _edW) {
int l_i, l_j, l_k, l_m;
int l_mbHeight, l_mbWidth;
//FILE *tf, *tf1;
char logFileName[100];
//sprintf(logFileName, "test1_%d.txt", _stFrame);
//tf = fopen(logFileName, "w");
//tf1 = fopen("test2.txt", "w");
l_mbHeight = (gVideoCodecCtxList[p_videoFileIndex]->height + 15) / 16;
l_mbWidth = (gVideoCodecCtxList[p_videoFileIndex]->width + 15) / 16;
LOGI(10, "start of compute_mb_mask_from_inter_frame_dependency: %d, %d, [%d:%d] (%d, %d) (%d, %d)", _stFrame, _edFrame, l_mbHeight, l_mbWidth, _stH, _stW, _edH, _edW);
//fprintf(tf1, "%d, %d, [%d:%d] (%d, %d) (%d, %d)", _stFrame, _edFrame, l_mbHeight, l_mbWidth, _stH, _stW, _edH, _edW);
memset(interDepMask, 0, sizeof(interDepMask[0][0][0])*MAX_FRAME_NUM_IN_GOP*MAX_MB_H*MAX_MB_W);
//from last frame in the GOP, going backwards to the first frame of the GOP
//1. mark the roi as needed
for (l_i = _edFrame; l_i >= _stFrame; --l_i) {
for (l_j = _stH; l_j <= _edH; ++l_j) {
for (l_k = _stW; l_k <= _edW; ++l_k) {
interDepMask[l_i - _stFrame][l_j][l_k] = 1;
}
}
}
//2. based on inter-dependency list, mark the needed mb
//it's not necessary to process _stFrame, as there's no inter-dependency for it
// for (l_i = _edFrame; l_i >= _stFrame; --l_i) {
for (l_i = _edFrame; l_i > _stFrame; --l_i) {
interDepMapMove = interDepMap + (l_i - _stFrame)*l_mbHeight*l_mbWidth*8;
//as we initialize the interDepMask to zero, we don't have a way to tell whether the upper left mb should be decoded, we always mark it as needed
interDepMask[l_i - 1 - _stFrame][0][0] = 1;
for (l_j = 0; l_j < l_mbHeight; ++l_j) {
for (l_k = 0; l_k < l_mbWidth; ++l_k) {
//fprintf(tf, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:\n", l_i, l_j, l_k, *interDepMapMove, *(interDepMapMove+1), *(interDepMapMove+2), *(interDepMapMove+3), *(interDepMapMove+4), *(interDepMapMove+5), *(interDepMapMove+6), *(interDepMapMove+7));
if (interDepMask[l_i - _stFrame][l_j][l_k] == 1) {
// fprintf(tf1, "%d:%d:%d:\n", l_i, l_j, l_k);
for (l_m = 0; l_m < MAX_INTER_DEP_MB; ++l_m) {
//LOGI(11, "%d,%d,%d,%d,%d,%d", l_i, l_j, l_k, l_m, *interDepMapMove, *(interDepMapMove+1));
//mark the needed mb in the previous frame
if (((*interDepMapMove) < 0) || (*(interDepMapMove+1) < 0)) {
} else
if (((*interDepMapMove) == 0) && (*(interDepMapMove+1) == 0)) {
} else {
//fprintf(tf, "%d,%d,%d,%d,%d,%d\n", l_i, l_j, l_k, l_m, *interDepMapMove, *(interDepMapMove+1));
//fprintf(tf, "%d,%d,%d,%d,%d\n", l_i, l_j, l_k, *interDepMapMove, *(interDepMapMove+1));
interDepMask[l_i - 1 - _stFrame][*interDepMapMove][*(interDepMapMove+1)] = 1;
}
interDepMapMove += 2;
}
} else {
interDepMapMove += 8;
}
}
}
}
//fclose(tf);
//fclose(tf1);
//we can unload the inter frame dependency file here
unload_inter_frame_mb_dependency();
LOGI(10, "end of compute_mb_mask_from_inter_frame_dependency");
}
/*called by decoding thread, to check if needs to wait for dumping thread to dump dependency*/
/*or called by dumping thread, see if it needs to generate dependency files for this gop*/
/*return 1 if it's complete; otherwise, return 0*/
int if_dependency_complete(int p_videoFileIndex, int p_gopNum) {
char l_depGopRecFileName[100], l_depIntraFileName[100], l_depInterFileName[100], l_depMbStPosFileName[100], l_depMbEdPosFileName[100], l_depDcpFileName[100];
FILE* lGopF;
int ret, ti, tj;
/*check if the dependency files exist, if not, we'll need to dump the dependencies*/
#ifdef ANDROID_BUILD
sprintf(l_depGopRecFileName, "%s_goprec_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depIntraFileName, "%s_intra_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depInterFileName, "%s_inter_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depMbStPosFileName, "%s_mbstpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depMbEdPosFileName, "%s_mbedpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depDcpFileName, "%s_dcp_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
#else
sprintf(l_depGopRecFileName, "%s_goprec_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depIntraFileName, "%s_intra_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depInterFileName, "%s_inter_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depMbStPosFileName, "%s_mbstpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depMbEdPosFileName, "%s_mbedpos_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
sprintf(l_depDcpFileName, "%s_dcp_gop%d.txt", gVideoFileNameList[p_videoFileIndex], p_gopNum);
#endif
if ((!if_file_exists(l_depGopRecFileName)) || (!if_file_exists(l_depIntraFileName)) || (!if_file_exists(l_depInterFileName)) || (!if_file_exists(l_depMbStPosFileName)) || (!if_file_exists(l_depMbEdPosFileName)) || (!if_file_exists(l_depDcpFileName))) {
return 0;
} else {
//further check if the gop file contains both start frame and end frame
lGopF = fopen(l_depGopRecFileName, "r");
ret = load_gop_info(lGopF, &ti, &tj);
fclose(lGopF);
return ((ret == 0)?1:0);
}
}
void dep_decode_a_video_packet(int p_videoFileIndex) {
char l_depGopRecFileName[100], l_depIntraFileName[100], l_depInterFileName[100];
AVFrame *l_videoFrame = avcodec_alloc_frame();
int l_numOfDecodedFrames, l_frameType;
int ti, tj;
FILE *tmpF, *postF;
unsigned char interDep[8];
unsigned char intraDep[6];
char aLine[80], *aToken, testLine[80];
unsigned char l_depH, l_depW, l_curDepIdx;
int l_idxF, l_idxH, l_idxW, lidxF = -1, lidxH = -1, lidxW = -1;
int i, j, k, m;
LOGI(10, "dep_decode_a_video_packet for video: %d", p_videoFileIndex);
while (av_read_frame(gFormatCtxDepList[p_videoFileIndex], &gVideoPacketDepList[p_videoFileIndex]) >= 0) {
if (gVideoPacketDepList[p_videoFileIndex].stream_index == gVideoStreamIndexList[p_videoFileIndex]) {
LOGI(10, "got a video packet, dump dependency: %d", p_videoFileIndex);
++gVideoCodecCtxDepList[p_videoFileIndex]->dep_video_packet_num;
/*put the video packet into the packet queue, for the decoding thread to access*/
//to save memory, don't do it
//LOGI(10, "put a video packet into queue: %d", p_videoFileIndex);
//packet_queue_put(&gVideoPacketQueueList[p_videoFileIndex], &gVideoPacketDepList[p_videoFileIndex]);
/*update the gop information if it's an I-frame
update: for every GOP, we generate a set of dependency files. It has the following advantages:
0. It avoids the overhead, and memory issue caused by opearting on big files
1. It makes it easy to delete some of the files that is already used if we're having a space constraints
or we never use the dependency files again (in live streaming)
*/
l_frameType = (gVideoPacketDepList[p_videoFileIndex].data[4] & 0xC0);
if (l_frameType == 0x00) { //an I frame packet
LOGI(10, "dump dependency: got I frame");
if (gVideoCodecCtxDepList[p_videoFileIndex]->dep_video_packet_num == 1) {
//if it's first frame, no action is needed
} else {
//print the end frame number of previoius gop
if (gVideoCodecCtxDepList[p_videoFileIndex]->dump_dependency) {
fprintf(gVideoCodecCtxDepList[p_videoFileIndex]->g_gopF, "%d:\n", gVideoCodecCtxDepList[p_videoFileIndex]->dep_video_packet_num - 1);
//TODO: fflush all the dependency files for previous gop, may not be necessary since we're closing these files
/*fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_gopF);
fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_mbStPosF);
fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_mbEdPosF);
fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_dcPredF);
fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_intraDepF);
fflush(gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepF);*/
//close all dependency files for this GOP
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_gopF);
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_mbStPosF);
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_mbEdPosF);
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_dcPredF);
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_intraDepF);
fclose(gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepF);
//post processing for inter frame dependency files to make them mmap compatible
sprintf(l_depInterFileName, "%s_inter_gop%d.txt.tmp", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
sprintf(gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepFileName, "%s_inter_gop%d.txt", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
tmpF = fopen(l_depInterFileName, "r");
postF = fopen(gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepFileName, "w");
LOGI(10, "...........processing %s to %s", l_depInterFileName, gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepFileName);
while (fgets(aLine, 80, tmpF) != NULL) {
memset(interDep, 0, 8);
if ((aToken = strtok(aLine, ":")) != NULL) //get the frame number, mb position first
l_idxF = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxW = atoi(aToken);
//get the dependency mb
l_curDepIdx = 0;
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depH = (unsigned char) atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depW = (unsigned char) atoi(aToken);
else break;
//put the dependencies into the array
interDep[l_curDepIdx++] = l_depH;
interDep[l_curDepIdx++] = l_depW;
} while (aToken != NULL);
fwrite(interDep, 1, 8, postF);
}
fclose(tmpF);
fclose(postF);
//[REMOVE]for verification of file: verified the content written to binary file is correct
/*load_inter_frame_mb_dependency(p_videoFileIndex);
interDepMapMove = interDepMap;
sprintf(l_depInterFileName, "%s_inter_gop%d.txt.ver", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
tmpF = fopen(l_depInterFileName, "w");
postF = fopen(gVideoCodecCtxDepList[p_videoFileIndex]->g_interDepFileName, "r");
for (i = 1; i <= 12; ++i) {
for (j =0; j < (gVideoCodecCtxList[p_videoFileIndex]->height + 15) / 16; ++j) {
for (k = 0; k < (gVideoCodecCtxList[p_videoFileIndex]->width + 15) / 16; ++k) {
fprintf(tmpF, "%d:%d:%d:", i, j, k);
for (m = 0; m < 4; ++m) {
//if ((*interDepMapMove == 0) && (*(interDepMapMove+1) == 0)) {
//} else {
fprintf(tmpF, "%d:%d:", *interDepMapMove, *(interDepMapMove+1));
//}
interDepMapMove += 2;
}
fprintf(tmpF, "\n");
}
}
}
fclose(tmpF);
fclose(postF);*/
//post processing for intra-frame dependency
sprintf(l_depIntraFileName, "%s_intra_gop%d.txt.tmp", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
sprintf(gVideoCodecCtxDepList[p_videoFileIndex]->g_intraDepFileName, "%s_intra_gop%d.txt", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
tmpF = fopen(l_depIntraFileName, "r");
postF = fopen(gVideoCodecCtxDepList[p_videoFileIndex]->g_intraDepFileName, "w");
LOGI(10, "...........processing %s to %s", l_depIntraFileName, gVideoCodecCtxDepList[p_videoFileIndex]->g_intraDepFileName);
while (fgets(aLine, 80, tmpF) != NULL) {
//memcpy(testLine, aLine, 80);
memset(intraDep, 0, 6);
if ((aToken = strtok(aLine, ":")) != NULL) //get the frame number, mb position first
l_idxF = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxH = atoi(aToken);
if ((aToken = strtok(NULL, ":")) != NULL)
l_idxW = atoi(aToken);
//LOGE(1, "testLine: %s", testLine);
/*if ((l_idxF < lidxF) || ((l_idxF == lidxF) && (l_idxH <= lidxH) && (l_idxW <= lidxW)) ) {
LOGE(1, "error:%d:%d:%d:%d:%d:%d", l_idxF, l_idxH, l_idxW, lidxF, lidxH, lidxW);
exit(1);
}
lidxF = l_idxF; lidxH = l_idxH; lidxW = l_idxW;*/
do {
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depH = (unsigned char) atoi(aToken);
else break;
aToken = strtok(NULL, ":");
if (aToken != NULL) l_depW = (unsigned char) atoi(aToken);
else break;
//put the dependencies into the array
if ((l_depH == l_idxH - 1) && (l_depW == l_idxW)) {
intraDep[0] = l_depH;
intraDep[1] = l_depW;
} else if ((l_depH == l_idxH - 1) && (l_depW == l_idxW + 1)) {
intraDep[2] = l_depH;
intraDep[3] = l_depW;
} else if ((l_depH == l_idxH) && (l_depW == l_idxW - 1)) {
intraDep[4] = l_depH;
intraDep[5] = l_depW;
} else {
LOGE(1, "EEEEEEEEEerror: intra dependency unexpected dependency");
}
} while (aToken != NULL);
fwrite(intraDep, 1, 6, postF);
}
fclose(tmpF);
fclose(postF);
//[REMOVE]for verification of file: verified the content written to binary file is correct
//if we want to debug for 2nd gop onwards, we'll need to change the interface to include gop number
/*load_intra_frame_mb_dependency(p_videoFileIndex, gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
intraDepMapMove = intraDepMap;
sprintf(l_depIntraFileName, "%s_intra_gop%d.txt.ver", gVideoFileNameList[p_videoFileIndex], gVideoPacketQueueList[p_videoFileIndex].dep_gop_num);
tmpF = fopen(l_depIntraFileName, "w");