forked from helloworld-spec/qiwen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathak_venc.c
executable file
·3070 lines (2587 loc) · 86.5 KB
/
ak_venc.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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include "list.h"
#include "internal_error.h"
#include "akuio.h"
#include "ak_global.h"
#include "ak_common.h"
#include "ak_thread.h"
#include "ak_error.h"
#include "ak_vi.h"
#include "ak_venc.h"
/* H3 heads */
#include "ak_venc_cb.h"
#include "venc_ipcsrv.h"
#include "akv_interface.h"
#define MPI_VENC "<mpi_venc>"
#define VENC_STAT_RATE_TIME (2) //per 2 seconds
#define IP_PA_START (0x20020000) //IP start addr
#define IP_PA_LEN (0x10000) //64k
//#define VENC_CFG_PATH "/etc/jffs2/venc.cfg"
static char _cfg_path[64] = "/etc/jffs2/venc.cfg";
#define VENC_CFG_PATH _cfg_path
#define VENC_GET_STREAM_DEBUG (0)
#define VENC_STAT_INFO_DEBUG (0)
#define VENC_TYPE_MAX_USER (sizeof(int) * 8) //max user number in same group
#define ENC_OUT_MAX_SZ (512 * 1024) //encode output buf max lenght
#define ONE_STREAM_MAX (50) //max stream list node
#define CALC_VIDEO_CAPTURE (1) //debug capture
#define CALC_VIDEO_ENCODE (1) //debug encode
#define VENC_CALC_CAP_TIME (0) //debug capture use time
#define VENC_CALC_ENC_TIME (1) //debug encode use time
#define VENC_ORIGIN_TS (0) //use original timestamp
#define VENC_CALC_TS (0) //calculate timestamp
#define VENC_SMOOTH_TS (1) //smooth timestamp
#define SHOW_ORIGIN_TS (0) //show original timestamp
#define SMOOTH_TS_DEBUG (0) //smooth timestamp debug
#define USE_LIB_V1 (0) //encode lib v1 macro
/* stat venc info */
struct venc_stat {
int total; //stream total bytes
int bps;
int frame_cnt; //encode frame count
float fps;
int gop_factor;
int gop_len;
unsigned long long start_ts;
struct ak_timeval calc_time;
};
/* use for capture */
struct frame_node {
int bit_map; //which group will use it
struct video_input_frame *vframe; //video frame pointer
struct list_head list; //frames list
};
/* internal stream node */
struct video_streams_t {
int ref; //use count
int bit_map; //user map
struct video_stream stream; //data
struct list_head list; //stream list
};
/* two internal thread argument struct */
struct thread_arg {
int cap_run; //use for capture thread
int enc_run; //use for encode thread
int sensor_pre_fps; //record previous sensor capture frames per second
void *vi_handle; //record this thread group's vi handle
struct list_head head_frame;//store capture frame, get by encode thread
ak_pthread_t cap_tid; //thread need join
ak_pthread_t enc_tid; //thread need join
ak_sem_t cap_sem; //capture semaphore
ak_sem_t enc_sem; //encode semaphore
ak_mutex_t lock; //list operate lock
struct list_head list; //hang to video_ctrl
};
/* each encode group has one this structure to discribe its info */
struct encode_group {
int user_count; //how many user under this encode group
int req_ref; //request reference count
int user_map; //request user bit map
int capture_frames; //capture frame, syn with system's camera device
int encode_frames; //encode frames
int ts_offset; //enocde ts offset
unsigned long long frame_index; //use to control frames per second
unsigned long long smooth_index;//index of smooth encode ts
int is_stream_mode; //1 stream mode, 0 sigle mode
unsigned long long pre_ts; //timestamp(ms)
void *lib_handle; //encoder lib operate handle
void *output; //encode output buf
void *encbuf; //encode buf address
enum encode_group_type grp_type; //group flag
struct encode_param record; //record encode param
struct list_head stream_list; //store stream
struct list_head enc_list; //register encode handle list
ak_mutex_t lock; //mutex lock
ak_mutex_t close_mutex; //close mutex
int streams_count; //current queue has nodes number
struct ak_timeval env_time; //check work env time
enum video_work_scene pre_scene; //previous scene
void *mdinfo; //pointer to md info
struct venc_stat stat; //venc stat info
int stream_overflow_print_cnt; //to control print when stream overflow
};
/* global control handle */
struct video_ctrl_handle {
unsigned char module_init; //module init flag
ak_mutex_t lock; //mutex lock
int inited_enc_grp_num; //record number of current opened group
int thread_group_num; //number of thread group
struct encode_group *grp[ENCODE_GRP_NUM]; //use for stream encode ctrl
ak_mutex_t cancel_mutex; //cancel mutex
struct list_head venc_list; //video user list
struct list_head thread_list; //catpure and encode thread list
#if CALC_VIDEO_CAPTURE
int frame_num; //for debug, to calculate frame number
struct ak_timeval calc_time; //for debug
#endif
#if CALC_VIDEO_ENCODE
int stream_num; //for debug, record stream node number
struct ak_timeval enc_time; //for debug, record encode use time
#endif
#if 0
ak_pthread_t wait_irq_thid; //wait irq thread id
#endif
int wait_irq_runflg; //thread run flag
/* use for debug value */
//int save_streams_flg[ENCODE_GRP_NUM]; //save streams to file, 1 is save, 0 is not
};
/* stream handle use for get and release stream */
struct stream_handle {
void *vi_handle; //record current stream's vi handle
void *enc_handle; //record current stream's encode handle
int id; //user id
};
static const char venc_version[] = "libmpi_venc V1.0.11";
#ifdef AK_RTOS
static struct video_ctrl_handle video_ctrl = {0, 0, 0};
ak_mutex_t *g_venc_open_lock __attribute__((__section__(".lock_init"))) = &video_ctrl.lock;
#else
static struct video_ctrl_handle video_ctrl = {
0, PTHREAD_MUTEX_INITIALIZER, 0
};
static pthread_mutex_t close_lock=PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t cancel_lock=PTHREAD_MUTEX_INITIALIZER;
#endif
/* ------------ internal api start ---------------------*/
#if VENC_SMOOTH_TS
static inline int get_frame_interval(struct encode_group *handle)
{
if (!handle || !handle->encode_frames)
return 0;
return (1000 / handle->encode_frames);
}
static inline int get_smooth_index(struct encode_group *handle,
unsigned long long ts)
{
if (!handle || !handle->encode_frames)
return 0;
return (((ts * handle->encode_frames) / 1000) - 1);
}
static inline unsigned long long get_cur_ts(unsigned long long smooth_index,
int encode_frames)
{
if (0 == encode_frames)
return 0;
return ((smooth_index * 1000) / encode_frames);
}
#endif
/*
* alloc struct frame_node memory
*/
static void *alloc_video_frame(void)
{
struct frame_node *frame = (struct frame_node *)calloc(1,
sizeof(struct frame_node));
if (!frame)
return NULL;
frame->vframe = (struct video_input_frame *)calloc(1,
sizeof(struct video_input_frame));
if (!frame->vframe) {
free(frame);
frame = NULL;
}
return frame;
}
/*
* free struct frame_node memory
*/
static void free_frame(void *frame)
{
struct frame_node *pframe = (struct frame_node *)frame;
if (pframe) {
if (pframe->vframe) {
free(pframe->vframe);
pframe->vframe = NULL;
}
free(pframe);
pframe = NULL;
}
}
static void set_encode_fps(struct encode_group *handle, int fps)
{
T_AKVLIB_PAR encpar = {0};
AKV_Encoder_Get_Parameters(handle->lib_handle, &encpar);
encpar.EncFrameRate = fps;
ak_print_normal_ex(MPI_VENC "set %d chnannel fps: %d\n",
handle->grp_type, fps);
AKV_Encoder_Set_Parameters(handle->lib_handle, &encpar);
}
/********************************************************************************/
static T_AKV_DMA_BUFFER *venc_cb_dma_alloc(unsigned long size)
{
T_AKV_DMA_BUFFER *f = (T_AKV_DMA_BUFFER *)calloc(1, sizeof(T_AKV_DMA_BUFFER));
f->pData = akuio_alloc_pmem(size);
if (!f->pData) {
ak_print_error_ex("alloc dma for %lu failed\n", size);
free(f);
f = NULL;
goto exit;
}
f->pa = akuio_vaddr2paddr(f->pData);
f->size = size;
f->hHandle = f;
exit:
return f;
}
static int venc_cb_dma_free(T_AKV_DMA_BUFFER* buf)
{
if (buf) {
akuio_free_pmem(buf->pData);
free(buf);
return 0;
}
return -1;
}
static void venc_cb_release_frame_buf(T_AKV_DMA_BUFFER* buf)
{
return;
}
/**
* init video encoder, register callback
* return: 1 seccess, 0 failed
*/
static int venc_encoder_init(void)
{
T_AKVLIB_CB init_cb_fun = {0};
init_cb_fun.ak_FunPrintf = print_normal;
init_cb_fun.ak_FunMalloc = (AKV_CB_FUN_MALLOC)malloc;
init_cb_fun.ak_FunFree = (AKV_CB_FUN_FREE)free;
//read and write register function
init_cb_fun.ak_FunReadRegister = venc_cb_read_reg;
init_cb_fun.ak_FunWriteRegister = venc_cb_write_reg;
init_cb_fun.ak_FunDmaAlloc = (AKV_CB_FUN_DMA_ALLOC)venc_cb_dma_alloc;
init_cb_fun.ak_FunDmaFree = (AKV_CB_FUN_DMA_FREE)venc_cb_dma_free;
init_cb_fun.ak_FunFlushDcache = (AKV_CB_FUN_FLUSH_DCACHE)venc_cb_flush_dcache;
init_cb_fun.ak_FunReleaseFrameBuf =
(AKV_CB_FUN_RELEASE_FRAME_BUF)venc_cb_release_frame_buf;
init_cb_fun.ak_FunModuleClock = (AKV_CB_FUN_MODULE_CLOCK)venc_cb_module_clock;
init_cb_fun.ak_FunModuleReset = akuio_reset_video;
/* mutex */
init_cb_fun.ak_FunMutexCreate = (AKV_CB_FUN_MUTEX_CREATE)venc_cb_init_mutex;
init_cb_fun.ak_FunMutexDelete = (AKV_CB_FUN_MUTEX_DELETE)venc_cb_destroy_mutex;
init_cb_fun.ak_FunMutexGet = (AKV_CB_FUN_MUTEX_GET)venc_cb_lock_mutex;
init_cb_fun.ak_FunMutexRelease = (AKV_CB_FUN_MUTEX_RELEASE)venc_cb_unlock_mutex;
/* semaphore */
init_cb_fun.ak_FunSemaphoreCreate = (AKV_CB_FUN_SEMAPHORE_CREATE)venc_cb_sem_init;
init_cb_fun.ak_FunSemaphoreDelete = venc_cb_sem_destroy;
init_cb_fun.ak_FunSemaphoreGet = (AKV_CB_FUN_SEMAPHORE_GET)venc_cb_sem_wait;
init_cb_fun.ak_FunSemaphoreRelease =
(AKV_CB_FUN_SEMAPHORE_RELEASE)venc_cb_sem_post;
#if USE_LIB_V1
init_cb_fun.ak_FunEventCreate = (AKV_CB_FUN_EVENT_CREATE)venc_cb_create_event;
init_cb_fun.ak_FunEventDelete = (AKV_CB_FUN_EVENT_DELETE)venc_cb_delete_event;
init_cb_fun.ak_FunEventWait = (AKV_CB_FUN_EVENT_WAIT)venc_cb_wait_event;
init_cb_fun.ak_FunEventSet = (AKV_CB_FUN_EVENT_SET)venc_cb_set_event;
#endif
#if !(USE_LIB_V1)
init_cb_fun.ak_FunIrqWait = akuio_wait_irq;
init_cb_fun.ak_FunAtomicIncrement =
(AKV_CB_FUN_ATOMIC_INCREMENT)venc_cb_atomic_increment;
init_cb_fun.ak_FunAtomicDecrement =
(AKV_CB_FUN_ATOMIC_DECREMENT)venc_cb_atomic_decrement;
#endif
#if USE_LIB_V1
init_cb_fun.ak_FunFileOpen = (AKV_CB_FUN_FILE_OPEN)fopen;
init_cb_fun.ak_FunFileClose = (AKV_CB_FUN_FILE_CLOSE)fclose;
init_cb_fun.ak_FunFileRead = (AKV_CB_FUN_FILE_READ)fread;
init_cb_fun.ak_FunFileWrite = (AKV_CB_FUN_FILE_WRITE)fwrite;
#endif
/* init reg */
venc_cb_init_reg(IP_PA_START, IP_PA_LEN);
return AKV_Encoder_Init(&init_cb_fun);
}
/*
* venc_handle_init - init video encode user handle
* index[IN]: group index
* return: 0 on success, -1 failed
*/
static int venc_handle_init(int index)
{
/* already opened, just return success */
if (video_ctrl.grp[index]) {
ak_print_normal_ex(MPI_VENC "group %d's handle has already opened\n",
index);
return AK_SUCCESS;
}
/* allocate handle's memory */
video_ctrl.grp[index] = (struct encode_group *)calloc(1,
sizeof(struct encode_group));
if (!video_ctrl.grp[index]) {
set_error_no(ERROR_TYPE_MALLOC_FAILED);
ak_thread_mutex_unlock(&video_ctrl.lock);
return AK_FAILED;
}
/* handle's control resource init */
ak_thread_mutex_init(&video_ctrl.grp[index]->lock, NULL);
ak_thread_mutex_init(&video_ctrl.grp[index]->close_mutex, NULL);
INIT_LIST_HEAD(&video_ctrl.grp[index]->enc_list);
ak_get_ostime(&(video_ctrl.grp[index]->env_time));
/* indoor or outdoor mode */
video_ctrl.grp[index]->pre_scene = VIDEO_SCENE_UNKNOWN;
return AK_SUCCESS;
}
/*
* add_user - add user
* enc_handle[IN], encode handle
* return current handle's user counter number
*/
static int add_user(void *enc_handle)
{
struct encode_group *handle = (struct encode_group *)enc_handle;
handle->user_count++;
return handle->user_count;
}
/*
* del_user - delete user
* enc_handle[IN], encode handle
* return current handle's user counter number
*/
static int del_user(void *enc_handle)
{
struct encode_group *handle = (struct encode_group *)enc_handle;
ak_thread_mutex_lock(&handle->lock);
if (handle->user_count > 0)
handle->user_count--;
ak_thread_mutex_unlock(&handle->lock);
return handle->user_count;
}
/*
* release_group_streams - release group's buffered stream
* enc_handle[IN], encode handle
* return: 0 on suceess, -1 failed
* notes: "req_ref" is check by call function
*/
static int release_group_streams(void *enc_handle)
{
if (!enc_handle) {
return AK_FAILED;
}
struct encode_group *handle = (struct encode_group *)enc_handle;
struct video_streams_t *pos = NULL;
struct video_streams_t *n = NULL;
ak_print_info_ex(MPI_VENC "entry..., before: %d\n", handle->streams_count);
ak_thread_mutex_lock(&handle->lock);
list_for_each_entry_safe(pos, n, &handle->stream_list, list) {
if (pos->stream.data) {
free(pos->stream.data);
pos->stream.data = NULL;
}
list_del_init(&pos->list); //remove from stream list
free(pos); //free node's memory
pos = NULL;
handle->streams_count--; //decrese total stream number
}
/* when release all stream, reinit stream head */
INIT_LIST_HEAD(&handle->stream_list);
ak_thread_mutex_unlock(&handle->lock);
ak_print_info_ex(MPI_VENC "entry..., after: %d\n", handle->streams_count);
return AK_SUCCESS;
}
/*
* has_encode_running - check whether its still has encode running
* return: 1 on has user still running, 0 means all user exited
*/
static int has_encode_running(void)
{
int i;
for (i = 0; i < ENCODE_GRP_NUM; i++) {
if (video_ctrl.grp[i] && (video_ctrl.grp[i]->user_count > 0))
return 1;
}
return 0;
}
/*
* encode_group_exit - release group's resource, like encode handle
* enc_handle[IN], encode handle
*/
static void encode_group_exit(void *enc_handle)
{
struct encode_group *handle = (struct encode_group *)enc_handle;
ak_print_info_ex(MPI_VENC "enc_handle:%p, type:%d\n",
handle, handle->record.enc_grp);
ak_thread_mutex_lock(&handle->lock);
/* close encode handle */
if (handle->lib_handle) {
AKV_Encoder_Close(handle->lib_handle);
handle->lib_handle = NULL;
ak_print_normal_ex(MPI_VENC "Video Stream Encoder Close OK\n");
}
if (handle->encbuf)
free(handle->encbuf);
ak_thread_mutex_unlock(&handle->lock);
ak_thread_mutex_destroy(&handle->lock);
ak_thread_mutex_lock(&video_ctrl.lock);
/* no user anymore, destroy encode */
if (!has_encode_running()) {
#if 0
/* cancel wait_irq_thread */
video_ctrl.wait_irq_runflg = AK_FALSE;
if (video_ctrl.wait_irq_thid) {
ak_thread_cancel(video_ctrl.wait_irq_thid);
ak_thread_join(video_ctrl.wait_irq_thid);
video_ctrl.wait_irq_thid = 0;
}
#endif
AKV_Encoder_Destory();
ak_thread_mutex_destroy(&video_ctrl.cancel_mutex);
video_ctrl.module_init = AK_FALSE;
venc_sys_ipc_unregister();
}
ak_thread_mutex_unlock(&video_ctrl.lock);
}
/*
* venc_release_useless_frames - release all useless yuv frames
* thread_arg[IN]: thread group's argument
*/
static void venc_release_useless_frames(struct thread_arg *thread_arg)
{
/* one frame corresponding multiple encode group */
struct frame_node *pos = NULL;
struct frame_node *n = NULL; //frame loop value
/* traverse frame queue to release resourse */
ak_thread_mutex_lock(&thread_arg->lock);
list_for_each_entry_safe(pos, n, &thread_arg->head_frame, list) {
list_del(&pos->list);
ak_vi_release_frame(thread_arg->vi_handle, pos->vframe);
free_frame(pos);
}
ak_thread_mutex_unlock(&thread_arg->lock);
ak_print_normal_ex(MPI_VENC "all useless frames were released\n");
}
/*
* encode_thread_group_exit - release group thread's resources
*/
static void encode_thread_group_exit(void)
{
ak_print_info_ex(MPI_VENC "enter, inited_enc_grp_num=%d\n",
video_ctrl.inited_enc_grp_num);
/*
* All encode has close, so we close all thread group.
* Now it is cancel only one thread group, close specific
* thread group is not implement.
*/
if (!video_ctrl.inited_enc_grp_num) {
struct thread_arg *arg, *n;
list_for_each_entry_safe(arg, n, &video_ctrl.thread_list, list) {
/* exit capture */
arg->cap_run = 0;
ak_thread_sem_post(&arg->cap_sem);
ak_print_normal_ex(MPI_VENC "join capture thread, tid=%lu\n",
arg->cap_tid);
ak_thread_join(arg->cap_tid);
ak_print_notice_ex(MPI_VENC "join capture thread OK\n");
ak_vi_clear_buffer(arg->vi_handle);
/* exit encode */
arg->enc_run = 0;
ak_thread_sem_post(&arg->enc_sem);
ak_print_normal_ex(MPI_VENC "join encode thread, tid=%lu\n",
arg->enc_tid);
ak_thread_join(arg->enc_tid);
ak_print_notice_ex(MPI_VENC "join encode thread OK\n");
/// 释放初始化时创建的信号量。
ak_thread_sem_destroy(&arg->cap_sem);
ak_thread_sem_destroy(&arg->enc_sem);
ak_thread_mutex_destroy(&arg->lock);
/*
* release all frame(s) which haven't been encoded but has been
* capture to frame queue
*/
venc_release_useless_frames(arg);
/* delete current thread from this module */
list_del(&arg->list);
free(arg);
arg = NULL;
ak_print_info_ex(MPI_VENC "free thread arg OK\n");
ak_thread_mutex_lock(&video_ctrl.lock);
video_ctrl.thread_group_num--;
ak_thread_mutex_unlock(&video_ctrl.lock);
}
}
ak_print_info_ex(MPI_VENC "leave ...\n");
}
/**
* change_frame_index - change frame index when fps change
* group_type[IN]: encode group type number
* ts[IN]: capture frame ts
* return: none
*/
static void init_frames_ctrl(struct encode_group *grp)
{
grp->frame_index = 0;
}
/**
* frames_ctrl - decide whether current group need encode
* group_type[IN]: encode group type number
* max_frame[IN]: encode max frame
* ts[IN]: capture frame ts
* return: 0 unneed it, 1 need
*/
static int frames_ctrl(int group_type, const int max_frame,
unsigned long long ts)
{
int ret = 0;
struct encode_group *grp = video_ctrl.grp[group_type];
unsigned int enc_frame = grp->encode_frames;
/* get camera capture frames and video encode frames */
if ((max_frame <= enc_frame)
&& ((max_frame == enc_frame) && (max_frame != 12))) {
return 1;
}
if (enc_frame <= 0) {
return 0;
}
unsigned long long enc_ts = ((grp->frame_index * 1000) / enc_frame);
if (ts >= enc_ts) {
if ((enc_ts > 0) && (ts - enc_ts) > 200) {
ak_print_warning(MPI_VENC "group_type=%d, index=%llu, "
"ts=%llu, enc_ts=%llu\n",
group_type, grp->frame_index, ts, enc_ts);
}
ret = 1;
++grp->frame_index;
enc_ts = ((grp->frame_index * 1000) / enc_frame);
if (enc_ts < ts) {
grp->frame_index = (((ts + (1000/enc_frame)) * enc_frame) / 1000);
}
}
return ret;
}
/**
* check_sensor_fps_switch - according to current encode fps and capture fps
* to determine endode fps
* thread_arg[IN]: current thread group's argument, use it's vi handle
* sensor_fps[IN]: current sensor capture fps
*/
static void check_sensor_fps_switch(struct thread_arg *thread_arg,
const int sensor_fps)
{
int changed = 0;
if (sensor_fps != thread_arg->sensor_pre_fps) {
if (thread_arg->sensor_pre_fps) {
ak_print_warning_ex(MPI_VENC "\t*** sensor fps switch %d to %d ***\n\n",
thread_arg->sensor_pre_fps, sensor_fps);
changed = 1;
}
thread_arg->sensor_pre_fps = sensor_fps;
int i = 0;
struct encode_group *grp = NULL;
/* compare sensor's real fps and appointed record fps */
for (i = 0; i < ENCODE_GRP_NUM; i++) {
grp = video_ctrl.grp[i];
if (!grp)
continue;
if (sensor_fps < grp->record.fps) { /* use sensor fps */
grp->encode_frames = sensor_fps;
} else { /* use original encode fps */
grp->encode_frames = grp->record.fps;
}
/*
* if sensor frame rate change cause encode frame rate change too,
* we should notify encoder to adjust encode
*/
if (changed && (grp->encode_frames > 0)) {
set_encode_fps(grp, grp->encode_frames);
init_frames_ctrl(grp);
}
}
}
}
/**
* add_to_encode_list - add frame to current thread group
* thread_arg[IN]: current thread group's argument
* sensor_fps[IN]: pointer to frame which will be add
*/
static void add_to_encode_list(struct thread_arg *thread_arg,
struct frame_node *frame)
{
if (!thread_arg)
return ;
int i = 0;
int bit_map = 0;
const int sensor_fps = ak_vi_get_fps(thread_arg->vi_handle);
unsigned long long ts = frame->vframe->vi_frame[VIDEO_CHN_MAIN].ts;
check_sensor_fps_switch(thread_arg, sensor_fps);
/* uses comfirm */
for (i = 0; i < ENCODE_GRP_NUM; i++) {
if (video_ctrl.grp[i]) {
/* decide whether current frame should be encode */
if (frames_ctrl(i, sensor_fps, ts))
set_bit(&bit_map, i);
}
}
if (bit_map) {
frame->bit_map = bit_map;
ak_thread_mutex_lock(&thread_arg->lock);
list_add_tail(&frame->list, &thread_arg->head_frame);
ak_thread_mutex_unlock(&thread_arg->lock);
/* notify encode thread to work */
ak_thread_sem_post(&thread_arg->enc_sem);
} else {
/* no one need, release it at once */
ak_vi_release_frame(thread_arg->vi_handle, frame->vframe);
free_frame(frame);
}
}
static void get_venc_start_osd_stat(struct encode_group *venc_handle,
const struct video_stream *stream, unsigned long long origin_ts)
{
venc_handle->stat.start_ts = origin_ts;
venc_handle->stat.total = stream->len;
venc_handle->stat.frame_cnt = 1;
venc_handle->stat.gop_len = ak_venc_get_gop_len(venc_handle);
int encode_fps = ak_venc_get_fps(venc_handle);
if ((venc_handle->stat.gop_len > 0) && (encode_fps > 0)) {
venc_handle->stat.gop_factor = (venc_handle->stat.gop_len / encode_fps);
if (!venc_handle->stat.gop_factor)
venc_handle->stat.gop_factor = VENC_STAT_RATE_TIME;
}
ak_get_ostime(&(venc_handle->stat.calc_time));
#if VENC_STAT_INFO_DEBUG
ak_print_notice(MPI_VENC "grp_type=%d, start_ts=%llu, total=%d\n",
venc_handle->grp_type, venc_handle->stat.start_ts, venc_handle->stat.total);
ak_print_info(MPI_VENC "grp_type=%d, encode_fps=%d, gop_len=%d, gop_factor=%d\n",
venc_handle->grp_type, encode_fps,
venc_handle->stat.gop_len, venc_handle->stat.gop_factor);
ak_print_info(MPI_VENC "grp_type=%d, stream ts=%llu, len=%d, seq_no=%lu, type=%d\n",
venc_handle->grp_type,
stream->ts, stream->len, stream->seq_no, stream->frame_type);
#endif
}
static void calc_venc_osd_stat(struct encode_group *venc_handle,
const struct video_stream *stream, unsigned long long origin_ts)
{
struct ak_timeval cur_time;
/* real passed time */
ak_get_ostime(&cur_time);
long use_time = ak_diff_ms_time(&cur_time, &(venc_handle->stat.calc_time));
/* video timestamp time */
unsigned long long diff_ts = (origin_ts - venc_handle->stat.start_ts);
float factor = venc_handle->stat.gop_factor;
int stat_time = (factor * 1000);
if ((use_time >= stat_time)
|| ((diff_ts >= stat_time) && (FRAME_TYPE_I == stream->frame_type))) {
int interval = 40;
if (venc_handle->capture_frames > 0) {
interval = (1000 / venc_handle->capture_frames);
}
if ((diff_ts > stat_time) && ((diff_ts - stat_time) >= interval)) {
venc_handle->stat.frame_cnt -= ((diff_ts - stat_time) / interval);
}
venc_handle->stat.bps = (((venc_handle->stat.total / 1024) * 8) / factor);
venc_handle->stat.fps = (venc_handle->stat.frame_cnt / factor);
#if VENC_STAT_INFO_DEBUG
ak_print_info(MPI_VENC "grp_type=%d, start_ts=%llu, origin_ts=%llu\n",
venc_handle->grp_type, venc_handle->stat.start_ts, origin_ts);
ak_print_info(MPI_VENC "grp_type=%d, use_time=%ld, diff_ts=%llu, stat_time=%d\n",
venc_handle->grp_type, use_time, diff_ts, stat_time);
ak_print_info(MPI_VENC "grp_type=%d, factor=%.2f, total=%d, frame_cnt=%d\n",
venc_handle->grp_type, factor,
venc_handle->stat.total, venc_handle->stat.frame_cnt);
#endif
ak_print_info(MPI_VENC "grp_type=%d, rate=%d(kbps), gop=%d, fps=%.1f, fcnt=%d\n",
venc_handle->grp_type, venc_handle->stat.bps,
venc_handle->stat.gop_len, venc_handle->stat.fps,
venc_handle->stat.frame_cnt);
/* incase of gop changed, so we get gop again */
get_venc_start_osd_stat(venc_handle, stream, origin_ts);
} else {
venc_handle->stat.total += stream->len;
++venc_handle->stat.frame_cnt;
}
}
/**
* stat_venc_info - video stat info
* venc_handle[IN]: venc handle
* stream[IN]: current stream info
* return: none
*/
static void stat_venc_info(struct encode_group *venc_handle,
const struct video_stream *stream, unsigned long long origin_ts)
{
if (0 == venc_handle->stat.start_ts) {
if (FRAME_TYPE_I == stream->frame_type) {
get_venc_start_osd_stat(venc_handle, stream, origin_ts);
}
} else {
calc_venc_osd_stat(venc_handle, stream, origin_ts);
}
}
#if CALC_VIDEO_CAPTURE
static void calc_video_capture_frame(struct video_input_frame *vframe)
{
struct ak_timeval cur_time;
ak_get_ostime(&cur_time);
++(video_ctrl.frame_num);
long diff_time = ak_diff_ms_time(&cur_time, &(video_ctrl.calc_time));
/* calc frame number per ten seconds */
if(diff_time >= 10*1000){
int total = video_ctrl.frame_num;
int seconds = (diff_time / 1000);
if (seconds > 0) {
struct frame *main_frame = &(vframe->vi_frame[VIDEO_CHN_MAIN]);
ak_print_info(MPI_VENC "new capture ts=%llu, len=%u, seq_no=%lu\n",
main_frame->ts, main_frame->len, main_frame->seq_no);
ak_print_info(MPI_VENC "*** second=%d, total=%d, "
"average=%d ***\n\n", seconds, total, (total / seconds));
}
ak_get_ostime(&(video_ctrl.calc_time));
video_ctrl.frame_num = 0;
}
}
#endif
#if CALC_VIDEO_ENCODE
static void calc_venc_stream(void)
{
struct ak_timeval cur_time;
ak_get_ostime(&cur_time);
++(video_ctrl.stream_num);
long diff_time = ak_diff_ms_time(&cur_time, &(video_ctrl.enc_time));
/* calc frame number per ten seconds */
if(diff_time >= 10*1000){
int total = video_ctrl.stream_num;
int seconds = (diff_time / 1000);
if (seconds > 0) {
ak_print_info(MPI_VENC "*** encode second=%d, total=%d, "
"average=%d ***\n\n", seconds, total, (total / seconds));
}
ak_get_ostime(&(video_ctrl.enc_time));
video_ctrl.stream_num = 0;
}
}
#endif
#if VENC_CALC_TS
static unsigned long long calc_stream_ts(struct encode_group *enc_handle,
unsigned long long origin_ts)
{
/* origin ts should be capture ts */
unsigned long long calc_ts = 0;
int ts_interval = (1000 / enc_handle->encode_frames);
if (enc_handle->pre_ts > 0) {
calc_ts = (enc_handle->pre_ts + ts_interval);
if (abs(calc_ts - origin_ts) > 20) {
calc_ts = origin_ts;
}
} else {
calc_ts = origin_ts;
ak_print_notice_ex(MPI_VENC "we get first stream for group %d\n",
enc_handle->grp_type);
ak_print_normal_ex(MPI_VENC "encode_frames=%d, ts_interval=%d\n",
enc_handle->encode_frames, ts_interval);
ak_print_normal_ex(MPI_VENC "first stream ts=%llu\n", origin_ts);
}
enc_handle->pre_ts = calc_ts;
ak_print_info_ex(MPI_VENC "calc_ts=%llu, origin ts=%llu, diff=%lld\n",
calc_ts, origin_ts, (calc_ts - origin_ts));
return calc_ts;
}
#endif
#if VENC_SMOOTH_TS
static unsigned long long smooth_ts(struct encode_group *handle,
unsigned long long ts, unsigned long seq_no)
{
if (!handle || !handle->encode_frames)
return 0;
unsigned long long cur_ts = 0;
int actual_fitv = get_frame_interval(handle);
if (0 == handle->smooth_index) {
handle->smooth_index = get_smooth_index(handle, ts);
handle->ts_offset = 0;
ak_print_normal_ex(MPI_VENC "init smooth_index=%llu\n",
handle->smooth_index);
}
handle->smooth_index++;
cur_ts = get_cur_ts(handle->smooth_index, handle->encode_frames);
if ((cur_ts > (ts + (2 * actual_fitv)))
|| ((cur_ts + (2 * actual_fitv)) < ts)) {
#if SMOOTH_TS_DEBUG
ak_print_warning_ex(MPI_VENC "pre_ts=%llu, cur_ts=%llu, ts=%llu, "
"seq_no=%lu\n",
handle->pre_ts, cur_ts, ts, seq_no);
ak_print_normal_ex(MPI_VENC "acfitv=%d, smooth_index=%llu\n",
actual_fitv, handle->smooth_index);
#endif
handle->smooth_index = get_smooth_index(handle, ts);
handle->smooth_index++;
cur_ts = get_cur_ts(handle->smooth_index, handle->encode_frames);
handle->ts_offset = 0;
if ((cur_ts <= handle->pre_ts) && (cur_ts < ts)) {
handle->ts_offset = -(ts - cur_ts) / 2;
ak_print_normal_ex(MPI_VENC "cur_ts too small, cur_ts:%llu, "
"pre_ts:%llu, ts_offset:%d\n",
cur_ts, handle->pre_ts, handle->ts_offset);
}
#if SMOOTH_TS_DEBUG
ak_print_notice_ex(MPI_VENC "re-sync: ts=%llu, cur_ts=%llu, "
"smooth_index=%llu\n",
ts, cur_ts, handle->smooth_index);
#endif
}
cur_ts -= handle->ts_offset;
if (cur_ts > ts) {
#if SMOOTH_TS_DEBUG
ak_print_notice_ex(MPI_VENC "<--smooth--> ts: %llu, cur_ts: %llu, "
"offset: %d\n",
ts, cur_ts, handle->ts_offset);
#endif
int tmp_offset = handle->ts_offset;
int tmp = (cur_ts - ts);
handle->ts_offset += tmp;
cur_ts -= tmp;
#if SMOOTH_TS_DEBUG
ak_print_normal_ex(MPI_VENC "cur_ts=%llu, ts=%llu, ts_offset=%d, "
"smooth_index=%llu\n",
cur_ts, ts, handle->ts_offset, handle->smooth_index);
#endif
if (handle->smooth_index > 1) {
unsigned long long tmp_enc_ts = get_cur_ts((handle->smooth_index - 1),
handle->encode_frames);
int diff = cur_ts + tmp_offset - tmp_enc_ts;