-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1501 lines (1234 loc) · 37 KB
/
main.cpp
File metadata and controls
1501 lines (1234 loc) · 37 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
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
/*========================================================================
# FileName: main.c
# Author: Ruchee
# Email: my@ruchee.com
# HomePage: http://www.ruchee.com
# LastChange: 2013-03-13 14:23:34
========================================================================*/
/*Time: 2012/04/10*/
/*Version: 1.7*/
/*Author: Tae*/
/*Description: 1)I-Frame进行LT编码,P-Frame仍只切片发送*/
#include "main.h"
//#include "raptorcode.h"
#include <time.h>
#include <limits.h>
#include <sys/time.h>
#include <semaphore.h>
#include <unistd.h>
#define K1_MAX 5000
#define T_MAX 2048
#define INDEX_SIZE 40
#define PORT_FB 3333
#define FB_DATA_SIZE 5000
//#define YUV_NO_MAX 50000
/*global variable*/
static int cam_p_fp = -1;
static int cam_c_fp = -1;
static char * win0_fb_addr = NULL;
//static int pre_fb_fd = -1;
//static unsigned char delimiter_h264[4] = {0x00, 0x00, 0x00, 0x01};
//static void * dec_handle;
static char ip[80];
static int gop, qp, res, lcd_width=400, lcd_height=272, with_audio, with_video, with_preview, with_multi_description,
with_local, with_fb, with_fec, t_init , k1_max_init=3000;
static int camera_no = 1;
static void *video_handle, *video_handle1;
//static snd_pcm_t *audio_handle;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//结构常量,静态初始化互斥锁
//static int finished = 0;
//int lt_frame = 1; // 用于计算凑齐可以进行lt的帧个数
int LT_R = 20;
int NEW_D = 1;
int M_SLICE = 1;
int time_dely = 30;
int time_dely_save = 30;
int locally_store_264 = 0;
float lose_q = 0.3;
sem_t sem_id,sem_quene,sem_room;//semaphore
typedef struct{
int frame_no;
long slice_no;
int frame_type;
long F;
int T;
int K;
int R;
int esi;
int camera_no;
} Frame_header;
struct frame_info{
int type;
int frameNo;
int count;
int fragmentLen;
int fragmentNo;
int offset;
int iCnt;
int pCnt;
};
/***************************************g_yuv处理**************************************/
enum
{
WIDTH = 480,
HEIGHT= 272,
};
enum
{
YUV420Size = WIDTH * HEIGHT * 3 >> 1,
};
BYTE byBuffer[YUV420Size] = { 0 };
enum
{
STEP = 12,
};
/***************************************g_yuv处理**************************************/
struct quene_node{
int size;
struct quene_node* next;
};
struct quene{
int size;
quene_node* first;
quene_node* last;
};
struct yuv_info{
int length;
int frameNo;
};
typedef struct Feedback
{
int type;
float value;
}feedback;
struct linkmsg
{
int flag;
int type;
double x;
double y;
double speed;
struct timeval collect_time;
};
//udp connection with video_dispaly end
int sockfd;
struct sockaddr_in saddr;
int fds[1], maxfd;
fd_set inset;
//void add_to_quene ( quene *, void*, size_t);
//void get_data(quene * ,char *);
char * sendq;
int savedata, getdata;
FILE* file_v;
/* Main Process */
int main(int argc, char **argv){
pthread_t /*a_pth, */v_pth, v_s_pth;//, m_c_pth;
pthread_t k_l_pth;
int ret, start, found = 0;
int /*a_id,*/ v_id, v_s_id;//, m_c_msg;
int k_l_id;
unsigned int addr = 0;
char * rgb_for_preview = (char *)malloc(lcd_width*lcd_height*4*sizeof(char));
struct v4l2_capability cap;
struct v4l2_input chan;
struct v4l2_framebuffer preview;
struct v4l2_pix_format preview_fmt;
struct v4l2_format codec_fmt;
int i;
if(argc%2 == 0){
printf(">>>>> Wrong number of arguments!\n");
return 1;
}
else{
strcpy(ip, IP_DEFAULT);
gop = GOP_DEFAULT;
qp = QP_DEFAULT;
res = RES_DEFAULT;
with_video = WITH_VEDIO_DEFAULT;
with_audio = WITH_AUDIO_DEFAULT;
with_preview = WITH_PREVIEW_DEFAULT;
with_multi_description = WITH_MULTI_DESCRIPTION;
with_local = WITH_LOCAL_DEFAULT;
with_fb = WITH_FB_DEFAULT;
with_fec = WITH_FEC_DEFAULT;
t_init = T_INIT_DEFAULT;
k1_max_init = K1_MAX;
for(i = 1; i < argc; i += 2){
switch(argv[i][1]){
case 'i':
strcpy(ip, argv[i+1]);
break;
case 'g':
gop = atoi(argv[i+1]);
break;
case 'q':
qp = atoi(argv[i+1]);
break;
case 'r':
res = atoi(argv[i+1]);
break;
case 'v':
with_video = atoi(argv[i+1]);
break;
case 'a':
with_audio = atoi(argv[i+1]);
break;
case 'p':
with_preview = atoi(argv[i+1]);
break;
//case 'm':
//with_multi_description = atoi(argv[i+1]);
// break;
case 'l':
with_local = atoi(argv[i+1]);
break;
case 'f':
with_fb = atoi(argv[i+1]);
break;
case 'z':
with_fec = atoi(argv[i+1]);
break;
case 't':
t_init = atoi(argv[i+1]);
break;
case 'k':
k1_max_init = atoi(argv[i+1]);
break;
// case 'b':
// lt_frame = atoi(argv[i+1]);
// break;
case 'R':
LT_R = atoi(argv[i+1]);
break;
case 'D':
NEW_D = atoi(argv[i+1]);
break;
case 's':
M_SLICE = atoi(argv[i+1]);
break;
case 'S':
locally_store_264 = atoi(argv[i+1]);
break;
case 'd':
time_dely = atoi(argv[i+1]);
break;
case 'Q':
time_dely_save = atoi(argv[i+1]);
break;
case 'c':
camera_no = atoi(argv[i+1]);
break;
default:
printf(">>>>> Wrong arguments!\n");
return 1;
}
}
switch(res){
case 4:
lcd_width = 480;
lcd_height = 272;
break;
case 6:
lcd_width = 640;
lcd_height = 480;
break;
case 8:
lcd_width = 800;
lcd_height = 600;
break;
default:
printf(">>>>> Wrong resolution!\n");
return 1;
}
}//else
printf("\n");
printf("\t***********************************\n");
printf("\t* *\n");
printf("\t* IP %15s -i *\n", ip);
printf("\t* *\n");
printf("\t* GOP %14d -g *\n", gop);
printf("\t* *\n");
printf("\t* QP %15d -q *\n", qp);
printf("\t* *\n");
printf("\t* RES %10d*%3d -r *\n", lcd_width, lcd_height);
printf("\t* *\n");
printf("\t* VEDIO %12d -v *\n", with_video);
printf("\t* *\n");
printf("\t* AUDIO %12d -a *\n", with_audio);
printf("\t* *\n");
printf("\t* PREVIEW %10d -p *\n", with_preview);
printf("\t* *\n");
printf("\t* LOCAL %12d -l *\n", with_local);
printf("\t* *\n");
printf("\t* FEED BACK %8d -f *\n", with_fb);
printf("\t* *\n");
printf("\t* TIME DELY %8d -d *\n", time_dely);
printf("\t* *\n");
printf("\t* CAMERA NO %8d -c *\n", camera_no);
printf("\t* *\n");
printf("\t* FEC %14d -z *\n", with_fec);
printf("\t* *\n");
printf("\t* T_INIT %11d -t *\n", t_init);
printf("\t* *\n");
printf("\t* K1_MAX %11d -k *\n", k1_max_init);
printf("\t* *\n");
printf("\t* LT_R %13d -R *\n", LT_R);
printf("\t* *\n");
// printf("\t* LT_FRAME %9d -b *\n", lt_frame);
// printf("\t* *\n");
printf("\t* NEW_D %12d -D *\n", NEW_D);
printf("\t* *\n");
printf("\t* M_SLICE %10d -s *\n", M_SLICE);
printf("\t* *\n");
printf("\t* L_STORE %10d -S *\n", locally_store_264);
printf("\t* *\n");
printf("\t* TIME_DELY %8d -d *\n",time_dely);
printf("\t* *\n");
printf("\t***********************************\n");
printf("\n");
sleep(3);
sendq = (char*)malloc((T_MAX+sizeof(Frame_header))*20);
savedata = getdata = 0;
bzero(&saddr,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORT_V);
if(inet_pton(AF_INET, ip, &saddr.sin_addr) <= 0){
printf("[%s] is not a valid IP address\n", ip);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
connect(sockfd,(struct sockaddr *)&saddr, sizeof(saddr));
fds[0] = sockfd;
maxfd = sockfd;
sem_init(&sem_id,0,1);
sem_init(&sem_quene,0,0);
sem_init(&sem_room,0,20);
//signal(SIGINT, signal_ctrl_c);
//signal(SIGINT, exit_from_app);
if(with_preview){
// Camera preview initialization
if((cam_p_fp = cam_p_init()) < 0)
exit_from_app();
win0_fb_addr = (char *)addr;
// Get capability
if((ret = ioctl(cam_p_fp , VIDIOC_QUERYCAP, &cap)) < 0){
printf("V4L2 : ioctl on VIDIOC_QUERYCAP failled\n");
exit(1);
}
// Check the type - preview(OVERLAY)
if(!(cap.capabilities & V4L2_CAP_VIDEO_OVERLAY)){
printf("V4L2 : Can not capture(V4L2_CAP_VIDEO_OVERLAY is false)\n");
exit(1);
}
chan.index = 0;
found = 0;
while(1){
if((ret = ioctl(cam_p_fp, VIDIOC_ENUMINPUT, &chan)) < 0){//视频捕获的应用首先要通过VIDIOC_ENUMINPUT
//命令来枚举所有可用的输入
printf("V4L2 : ioctl on VIDIOC_ENUMINPUT failled !!!!\n");
fflush(stdout);
break;
}
// Test channel.type
if(chan.type & V4L2_INPUT_TYPE_CAMERA){
found = 1;
break;
}
chan.index++;
}
if(!found)
exit_from_app();
// Settings for input channel 0 which is channel of webcam
chan.type = V4L2_INPUT_TYPE_CAMERA;
//一个video设备节点可能对应多个视频源,上层调用S_INPUT ioctl在多个cvbs视频输入间切换
if((ret = ioctl(cam_p_fp, VIDIOC_S_INPUT, &chan)) < 0){
printf("V4L2 : ioctl on VIDIOC_S_INPUT failed\n");
fflush(stdout);
exit(1);
}
preview_fmt.width = lcd_width;
preview_fmt.height = lcd_height;
preview_fmt.pixelformat = LCD_BPP_V4L2;
preview.capability = 0;
preview.flags = 0;
preview.fmt = preview_fmt;
// Set up for preview
if((ret = ioctl(cam_p_fp, VIDIOC_S_FBUF, &preview)) < 0){
printf("V4L2 : ioctl on VIDIOC_S_BUF failed\n");
exit(1);
}
// Preview start
start = 1;
if((ret = ioctl(cam_p_fp, VIDIOC_OVERLAY, &start)) < 0){
printf("V4L2 : ioctl on VIDIOC_OVERLAY failed\n");
exit(1);
}
}
if(with_local == 0){
// Camera codec initialization
if((cam_c_fp = cam_c_init()) < 0)
exit_from_app();
// Get capability
if((ret = ioctl(cam_c_fp , VIDIOC_QUERYCAP, &cap)) < 0){
printf("V4L2 : ioctl on VIDIOC_QUERYCAP failled\n");
exit(1);
}
// Check the type - preview(OVERLAY)
if(!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
printf("V4L2 : Can not capture(V4L2_CAP_VIDEO_CAPTURE is false)\n");
exit(1);
}
// Set format
codec_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
codec_fmt.fmt.pix.width = lcd_width;
codec_fmt.fmt.pix.height = lcd_height;
codec_fmt.fmt.pix.pixelformat= V4L2_PIX_FMT_YUV420;
if((ret = ioctl(cam_c_fp , VIDIOC_S_FMT, &codec_fmt)) < 0){
printf("V4L2 : ioctl on VIDIOC_S_FMT failled\n");
exit(1);
}
}
// Encoding threads creation
if(with_video){
v_id = pthread_create(&v_pth, 0, video_thread, 0);
if(with_local == 0 && locally_store_264 == 1){
if(!(file_v = fopen("origin_video", "wb+"))){
perror("origin_video file open error");
exit(1);
}
}
}
v_s_id = pthread_create(&v_s_pth, 0, video_send_thread, 0);
k_l_id = pthread_create(&k_l_pth, 0, keep_linked_thread, 0);
// if(with_audio)
// a_id = pthread_create(&a_pth, 0, audio_thread, 0);
while(with_preview){ // preview
// Get RGB frame from camera preview
if(!read_data(cam_p_fp, &rgb_for_preview[0], lcd_width, lcd_height, LCD_BPP)){
printf("V4L2 : read_data() failed\n");
break;
}
// Write RGB frame to LCD frame buf
draw(win0_fb_addr, &rgb_for_preview[0], lcd_width, lcd_height, LCD_BPP);
}
// Start encoding thread
if(with_video){
pthread_join(v_pth, NULL);//使一个线程等待另一个线程结束,linux使用此函数对创建的线程进行资源回收
}
pthread_join(v_s_pth, NULL);
pthread_join(k_l_pth, NULL);
// if(with_audio)
// pthread_join(a_pth, NULL);
exit_from_app();
return 0;
}
/***************** Vedio Thread *****************/
static void* video_thread(void*){
/************************g_yuv处理**************************/
YUVImage yuvImage = { 0 };
MixerConfig mixerConfig = { 0 };
yuvImage.dwPitch = WIDTH;
yuvImage.dwHeight= HEIGHT;
yuvImage.lpYUVImage= byBuffer;
//CHAR szDate[] = " ";
//CHAR szTime[] = " ";
USHORT dayForamt[] = { TIME_FMT_YEAR4, '-', TIME_FMT_MONTH2, '-', TIME_FMT_DAY, ' ',TIME_FMT_CWEEK1,' '};
USHORT timeForamt[] = { TIME_FMT_HOUR24, ':', TIME_FMT_MINUTE, ':', TIME_FMT_SECOND, };
//mixerConfig.timeConfig.bEnable = true;
mixerConfig.timeConfig.bEnable = 1;
mixerConfig.timeConfig.dwFontSize= FONT_SIZE_16;
mixerConfig.timeConfig.x = 90;
mixerConfig.timeConfig.y = 250;
//mixerConfig.timeConfig.bAdjustFontLuma = false;
mixerConfig.timeConfig.bAdjustFontLuma = 0;
mixerConfig.timeConfig.byFontLuma = 0xFF;
UINT nLength = 0;
// CopyMemory(&mixerConfig.timeConfig.tFormat[nLength], &szDate, sizeof(szDate));
// nLength += sizeof(szDate) ;
CopyMemory(&mixerConfig.timeConfig.tFormat[nLength], dayForamt, sizeof(dayForamt));
nLength += sizeof(dayForamt);
// CopyMemory(&mixerConfig.timeConfig.tFormat[nLength], &szTime, sizeof(szTime));
// nLength += sizeof(szTime) ;
CopyMemory(&mixerConfig.timeConfig.tFormat[nLength], timeForamt, sizeof(timeForamt));
nLength += sizeof(timeForamt);
yuvImage.dwYUVFmt= YUV_FMT_YV12;
/*******************************************************************************/
char file_name[100];
struct timeval t_start,t_end;
long cost_time_sec,cost_time_usec;
long yuv_no = 1;
int start, ret;
int yuv_frame_buf_size = (lcd_width*lcd_height)+(lcd_width*lcd_height)/2;
unsigned char g_yuv[yuv_frame_buf_size];
unsigned char * encoded_buf;
long encoded_buf_size;
uint32 T = t_init; //default: 128
long F = 0;
uint32 K = 0, R = 0;
unsigned int i;
// int i, j, k;
unsigned int symbol_no;
uint8* input_buf = (uint8*)malloc(K1_MAX*T_MAX);
uint8* output = (uint8*)malloc(K1_MAX*T_MAX);
char * output_buf = (char *)malloc(sizeof(Frame_header)+T_MAX); //output_buf存储生成的每一个encoding symbol
char * sps_pps = (char *)malloc(30);
Frame_header * frame_header = (Frame_header *)malloc(sizeof(Frame_header));
uint8* intermediate = (uint8*)malloc(K1_MAX*T_MAX+100);
long long loop_times;
long slice_no = 1;
RParam para;
para = (RParam)malloc(sizeof(RaptorParam));
raptor_init(k1_max_init, para);
//float recieve;
//char * fb_data = (char *)malloc(FB_DATA_SIZE*sizeof(char));
//struct timeval tv;
FILE * local_video_source_fp = NULL;
if(with_local){
if((local_video_source_fp = fopen("origin_video", "rb")) == 0){
perror("origin_video");
exit(1);
}
loop_times = 3076; //特定大小
}
else loop_times = LONG_MAX;
// pthread_mutex_lock(&mutex);
video_handle = mfc_encoder_init(lcd_width, lcd_height, 30, 1000, 30);
printf(">>>>>> video_handle init\n");
sprintf(&file_name[0], "Cam_encoding_%dx%d.264", lcd_width, lcd_height);
fflush(stdout);
if(with_local == 0){
// Codec start
start = 1;
if((ret = ioctl(cam_c_fp, VIDIOC_STREAMON, &start)) < 0){
printf("V4L2 : ioctl on VIDIOC_STREAMON failed(start)\n");
exit(1);
}
}
//*** set GOP & QP & slice_num***
H264_ENC_CONF conf_type = H264_ENC_SETCONF_PARAM_CHANGE;
int value[2];
value[0] = H264_ENC_PARAM_GOP_NUM;
value[1] = gop;
SsbSipH264EncodeSetConfig(video_handle, conf_type, value);
value[0] = H264_ENC_PARAM_INTRA_QP;
value[1] = qp;
SsbSipH264EncodeSetConfig(video_handle, conf_type, value);
value[0] = H264_ENC_PARAM_SLICE_MODE;
value[1] = 4;
SsbSipH264EncodeSetConfig(video_handle, conf_type, value);
while(yuv_no < loop_times || with_local == 0){
signal(SIGINT, (void (*)(int))ctrl_c);
//read from camera device
if(with_local == 1){
if(feof(local_video_source_fp) == 0){
if(fread(g_yuv, 1, yuv_frame_buf_size, local_video_source_fp) < 0){
perror("read()");
}
}
else{
printf("The end!!!!!!!!\n");
SsbSipH264EncodeDeInit(video_handle);
exit(1);
}
//printf("origin_video reading...\n");
}
else{
if((read(cam_c_fp, g_yuv, yuv_frame_buf_size)) < 0)
perror("read()");
else if(locally_store_264 == 1){
//locally restore
fwrite((const unsigned char*)g_yuv, 1, yuv_frame_buf_size, file_v);
fflush(file_v);
}
}
/*****************************g_yuv处理*********************************/
memcpy(yuvImage.lpYUVImage,g_yuv,YUV420Size);
HANDLE hMixer = YOM_Initialize();
YOM_MixOSD(hMixer, &mixerConfig, &yuvImage);
YOM_Uninitialize(hMixer);
memcpy(g_yuv,yuvImage.lpYUVImage,YUV420Size);
//存在big.yuv里面,测试到底是不是编码所致的乱码
//iOffset = count_F * YUV420Size;
//fseek(fp,iOffset,SEEK_SET);
//fwrite(g_yuv,1,YUV420Size,fp);
//count_F++;
/*****************************g_yuv处理*********************************/
// gettimeofday(&t_start, NULL);
if(yuv_no % gop == 1){
printf("\n264 i\n");
conf_type = H264_ENC_SETCONF_CUR_PIC_OPT;
value[0] = H264_ENC_PIC_OPT_IDR;
value[1] = 1;
SsbSipH264EncodeSetConfig(video_handle, conf_type, value);
encoded_buf = (unsigned char*)mfc_encoder_exe(video_handle, g_yuv, yuv_frame_buf_size, 1, &encoded_buf_size);
if(yuv_no == 1){
memcpy(sps_pps,encoded_buf,21);
}
F = encoded_buf_size+21;
K = (uint32)ceil((double)F/T);
if(with_fec){
R = (uint32)ceil(K/2);
printf("K = %d R = %d LT_R = 50%%\n",(int)K,(int)R);
}
else{
memset(input_buf, 0, K*T);
memcpy(input_buf,sps_pps,21);
memcpy(input_buf+21,encoded_buf,encoded_buf_size);
}
}
else{
printf("\n264 p\n");
encoded_buf = (unsigned char*)mfc_encoder_exe(video_handle, g_yuv, yuv_frame_buf_size, 0, &encoded_buf_size);
F = encoded_buf_size;
K = (uint32)ceil((double)F/T);
if(K<5){
K = 5;
}
if(with_fec){
R = (uint32)ceil((LT_R*K)/(100-LT_R));
printf("K = %d, R = %d LT_R = %d%%\n",(int)K,(int)R, LT_R);
}
else{
memset(input_buf, 0, K*T);
memcpy(input_buf, encoded_buf, encoded_buf_size);
}
}
// tv.tv_sec = 0;
// tv.tv_usec = 0;
// FD_ZERO(&inset);
// FD_SET(sockfd, &inset);
// maxfd = sockfd;
// if( select(maxfd+1, &inset, NULL, NULL, &tv) > 0){
// if(FD_ISSET(sockfd, &inset))
// {
// read(sockfd, fb_data, sizeof(float));
// memcpy(&recieve, fb_data, sizeof(float));
// lose_q = recieve;
// }
// }
memset(output_buf,0,sizeof(Frame_header)+T_MAX);//2012
if(with_fec){
gettimeofday(&t_start, NULL);
// raptor_init(K,para);
// R = (uint32)ceil((K+3)/(1-lose_q))-K;
// R = 20;
// R = (uint32)ceil((LT_R*K)/(100-LT_R));
// printf("\nK = %d,cauculate R = %d lose_q = %2f\n",(int)K,(int)R,lose_q);
// printf("\nK = %d, R = %d LT_R = %d%\n",(int)K,(int)R,LT_R);
raptor_reset(K,para);
memset(input_buf, 0, para->L*T);
if(yuv_no%gop == 1){
memcpy(input_buf+(para->S+para->H)*T,sps_pps,21);
memcpy(input_buf+(para->S+para->H)*T+21, encoded_buf, encoded_buf_size);
}else{
memcpy(input_buf+(para->S+para->H)*T, encoded_buf, encoded_buf_size);
}
int result = raptor_encode(para,R,input_buf,intermediate,output,T);
if(result == 0){
printf("encode error!\n");
//raptor_parameterfree(para);
if(yuv_no == INT_MAX)
yuv_no = 0;
else
yuv_no++;
continue;
}
gettimeofday(&t_end, NULL);
cost_time_sec=t_end.tv_sec-t_start.tv_sec;
cost_time_usec=t_end.tv_usec-t_start.tv_usec;
if(cost_time_usec<0){
cost_time_usec+=1000000;
cost_time_sec--;
}
printf("\n frame :%ld raptor encoder cost time %ld.%06ld s\n",yuv_no, cost_time_sec, cost_time_usec);
for(symbol_no = 0; symbol_no < K+R; symbol_no++){
frame_header->frame_no = htonl(yuv_no);
frame_header->slice_no = htonl(slice_no);
if(slice_no == LONG_MAX)
slice_no = 0;
else
slice_no++;
frame_header->frame_type = htonl(1);
frame_header->F = htonl(F);
frame_header->T = htonl(T);
frame_header->K = htonl(K);
frame_header->R = htonl(R);
frame_header->esi = htonl(symbol_no);
frame_header->camera_no = htonl(camera_no);
memcpy(output_buf, frame_header, sizeof(Frame_header));
memcpy(output_buf+sizeof(Frame_header), output+symbol_no*T, T);
sem_wait(&sem_room);
sem_wait(&sem_id);
memcpy(sendq+(T+sizeof(Frame_header))*savedata, output_buf, sizeof(Frame_header)+T);
savedata = (savedata+1)%20;
sem_post(&sem_quene);
sem_post(&sem_id);
}
//raptor_parameterfree(para);
if(yuv_no == INT_MAX)
yuv_no = 0;
else
yuv_no++;
}
else{
for(i = 0; i < K; i++){
frame_header->frame_no = htonl(yuv_no);
frame_header->slice_no = htonl(slice_no);
if(slice_no == LONG_MAX)
slice_no = 0;
else
slice_no++;
frame_header->frame_type = htonl(2);
frame_header->F = htonl(F);
frame_header->T = htonl(T);
frame_header->K = htonl(K);
frame_header->R = htonl(0);
frame_header->esi = htonl(i);
frame_header->camera_no = htonl(camera_no);
memcpy(output_buf, frame_header, sizeof(Frame_header));
memcpy(output_buf+sizeof(Frame_header), input_buf+i*T, T);
sem_wait(&sem_room);
sem_wait(&sem_id);
memcpy(sendq+(T+sizeof(Frame_header))*savedata, output_buf, sizeof(Frame_header)+T);
savedata = (savedata+1)%20;
sem_post(&sem_quene);
sem_post(&sem_id);
}
if(yuv_no == INT_MAX)
yuv_no = 0;
else
yuv_no++;
}
}
if(with_local == 0){
// Codec stop
start = 0;
ioctl(cam_c_fp, VIDIOC_STREAMOFF, &start);
}
// pthread_mutex_unlock(&mutex);
return 0;
}
// static long total_data = 0;
static void* video_send_thread(void*){
int T_INIT = t_init;
int pLen;
char output_buf_s[1024];
char temp_frame[1024];
int w_n;
int i;
int header_size = sizeof(Frame_header);
unsigned int esi_temp = 0;
unsigned int frame_no_temp = 0;
int temp_frame_flag = 0;
int data_len = sizeof(int);
double total_dt;
Frame_header *frame_header = (Frame_header *)malloc(sizeof(Frame_header));
while(1){
i = 0;
pLen = 0;
memset(output_buf_s, '\0', sizeof(output_buf_s));
if(temp_frame_flag == 1){
memcpy(frame_header, temp_frame, header_size);
frame_no_temp = ntohl(frame_header->frame_no);
esi_temp = ntohl(frame_header->esi);
// printf("frame_no : %d esi : %d size : %d\n", frame_no_temp, esi_temp, header_size+T_INIT);
memcpy(output_buf_s, temp_frame, header_size+T_INIT);
i++;
getdata = (getdata+1)%20;
pLen = T_INIT + header_size;
pLen += data_len;
temp_frame_flag = 0;
}
while(pLen+T_INIT <= 1024){
sem_wait(&sem_quene);
sem_wait(&sem_id);
memcpy(frame_header,sendq+getdata*(T_INIT+header_size),header_size);
if(pLen == 0){
frame_no_temp = ntohl(frame_header->frame_no);
esi_temp = ntohl(frame_header->esi);
// printf("frame_no : %d esi : %d size : %d\n", frame_no_temp, esi_temp, header_size+T_INIT);
memcpy(output_buf_s, sendq+getdata*(T_INIT+header_size), header_size+T_INIT);
pLen = T_INIT + header_size;
pLen += data_len;
}
else{
if(frame_no_temp == ntohl(frame_header->frame_no)){
esi_temp ++;
// printf("frame_no : %d esi : %d size : %d\n", frame_no_temp, esi_temp, header_size+T_INIT);
// memcpy(output_buf_s+header_size+i*T_INIT+data_len, sendq+getdata*(T_INIT+header_size)+header_size, T_INIT);
memcpy(output_buf_s+pLen, sendq+getdata*(T_INIT+header_size)+header_size, T_INIT);
pLen += T_INIT;
}
else{
temp_frame_flag = 1;
memcpy(temp_frame, sendq+getdata*(T_INIT+header_size), header_size+T_INIT);
sem_post(&sem_room);
sem_post(&sem_id);
break;
}
}
sem_post(&sem_room);
sem_post(&sem_id);
i++;
getdata = (getdata+1)%20;
}
i--;
i = htonl(i);
memcpy(output_buf_s+T_INIT+header_size, &i, sizeof(i));
w_n = write(sockfd, output_buf_s, pLen);
// total_data += w_n;
// if(total_data > 1048576)
// {
// total_dt = (double)total_data/1048576;
// fprintf(stderr, "total send: %ldB = %.3fMB\n", total_data, total_dt);
// }
// else if(total_data > 1024)
// {
// total_dt = (double)total_data/1024;
// fprintf(stderr, "total send: %ldB = %.3fKB\n", total_data, total_dt);
// }
// else
// {
// fprintf(stderr, "total send: %ldB\n", total_data);
// }
// printf("this send: %dB, plen = %d i = %d\n", w_n, pLen, ntohl(i));
usleep(time_dely*1000);
}
return 0;
}
static void* keep_linked_thread(void*){
char msg[] = "###";
while(1){
write(sockfd, msg, sizeof(msg));