1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <malloc.h>
16 #include "sample_comm_ive.h"
17 #include "sample_ive_queue.h"
18
19 static HI_S32 s_s32MaxQueuelen = 0;
20 static HI_S32 s_s32CurQueueLen = 0;
21
SAMPLE_IVE_QueueCreate(HI_S32 s32Len)22 SAMPLE_IVE_QUEUE_S *SAMPLE_IVE_QueueCreate(HI_S32 s32Len)
23 {
24 SAMPLE_IVE_QUEUE_S *pstQueueHead = NULL;
25
26 if (s32Len < -1 || s32Len == 0) {
27 return NULL;
28 }
29 s_s32CurQueueLen = 0;
30 pstQueueHead = (SAMPLE_IVE_QUEUE_S *)malloc(sizeof(SAMPLE_IVE_QUEUE_S));
31 if (pstQueueHead == NULL) {
32 return NULL;
33 }
34 pstQueueHead->front = NULL;
35 pstQueueHead->rear = NULL;
36 s_s32MaxQueuelen = s32Len;
37
38 return pstQueueHead;
39 }
40
SAMPLE_IVE_QueueDestory(SAMPLE_IVE_QUEUE_S * pstQueueHead)41 HI_VOID SAMPLE_IVE_QueueDestory(SAMPLE_IVE_QUEUE_S *pstQueueHead)
42 {
43 SAMPLE_IVE_NODE_S *pstQueueTmp = NULL;
44
45 if (pstQueueHead == NULL) {
46 return;
47 }
48
49 pstQueueTmp = pstQueueHead->front;
50 while (pstQueueTmp != NULL) {
51 pstQueueHead->front = pstQueueTmp->next;
52 free(pstQueueTmp);
53 pstQueueTmp = pstQueueHead->front;
54 }
55 pstQueueHead->rear = pstQueueHead->front;
56 s_s32CurQueueLen = 0;
57 free(pstQueueHead);
58 pstQueueHead = NULL;
59
60 return;
61 }
62
SAMPLE_IVE_QueueClear(SAMPLE_IVE_QUEUE_S * pstQueueHead)63 HI_VOID SAMPLE_IVE_QueueClear(SAMPLE_IVE_QUEUE_S *pstQueueHead)
64 {
65 SAMPLE_IVE_NODE_S *pstQueueTmp = NULL;
66
67 if (pstQueueHead == NULL) {
68 return;
69 }
70
71 pstQueueTmp = pstQueueHead->front;
72 while (pstQueueTmp != NULL) {
73 pstQueueHead->front = pstQueueTmp->next;
74 free(pstQueueTmp);
75 pstQueueTmp = pstQueueHead->front;
76 }
77 pstQueueHead->rear = pstQueueHead->front;
78 s_s32CurQueueLen = 0;
79
80 return;
81 }
82
SAMPLE_IVE_QueueIsEmpty(SAMPLE_IVE_QUEUE_S * pstQueueHead)83 HI_BOOL SAMPLE_IVE_QueueIsEmpty(SAMPLE_IVE_QUEUE_S *pstQueueHead)
84 {
85 if (pstQueueHead == NULL) {
86 return HI_TRUE;
87 }
88
89 if (pstQueueHead->front != NULL) {
90 return HI_FALSE;
91 }
92
93 return HI_TRUE;
94 }
95
SAMPLE_IVE_QueueSize(SAMPLE_IVE_QUEUE_S * pstQueueHead)96 HI_S32 SAMPLE_IVE_QueueSize(SAMPLE_IVE_QUEUE_S *pstQueueHead)
97 {
98 if (pstQueueHead == NULL) {
99 return 0;
100 }
101
102 return s_s32CurQueueLen;
103 }
104
SAMPLE_IVE_QueueAddNode(SAMPLE_IVE_QUEUE_S * pstQueueHead,VIDEO_FRAME_INFO_S * pstFrameInfo)105 HI_S32 SAMPLE_IVE_QueueAddNode(SAMPLE_IVE_QUEUE_S *pstQueueHead, VIDEO_FRAME_INFO_S *pstFrameInfo)
106 {
107 SAMPLE_IVE_NODE_S *pstQueueNode = NULL;
108
109 if ((pstQueueHead == NULL) || (pstFrameInfo == NULL)) {
110 return QUEUE_NULL_POINTER;
111 }
112
113 if ((s_s32MaxQueuelen != -1) && (s_s32CurQueueLen >= s_s32MaxQueuelen)) {
114 return QUEUE_ILLEGAL_STATE;
115 }
116
117 pstQueueNode = (SAMPLE_IVE_NODE_S *)malloc(sizeof(SAMPLE_IVE_NODE_S));
118 if (pstQueueNode == NULL) {
119 return QUEUE_OUT_OF_MEMORY;
120 }
121
122 (HI_VOID)memcpy_s(&pstQueueNode->stFrameInfo, sizeof(VIDEO_FRAME_INFO_S), pstFrameInfo, sizeof(VIDEO_FRAME_INFO_S));
123 pstQueueNode->next = NULL;
124 if (SAMPLE_IVE_QueueIsEmpty(pstQueueHead)) {
125 pstQueueHead->front = pstQueueNode;
126 pstQueueHead->rear = pstQueueNode;
127 } else {
128 pstQueueHead->rear->next = pstQueueNode;
129 pstQueueHead->rear = pstQueueNode;
130 }
131
132 s_s32CurQueueLen++;
133
134 return HI_SUCCESS;
135 }
136
SAMPLE_IVE_QueueGetHeadNode(SAMPLE_IVE_QUEUE_S * pstQueueHead)137 SAMPLE_IVE_NODE_S *SAMPLE_IVE_QueueGetHeadNode(SAMPLE_IVE_QUEUE_S *pstQueueHead)
138 {
139 if ((pstQueueHead == NULL) || (pstQueueHead->front == NULL)) {
140 return NULL;
141 }
142
143 return pstQueueHead->front;
144 }
145
SAMPLE_IVE_QueueGetNode(SAMPLE_IVE_QUEUE_S * pstQueueHead)146 SAMPLE_IVE_NODE_S *SAMPLE_IVE_QueueGetNode(SAMPLE_IVE_QUEUE_S *pstQueueHead)
147 {
148 SAMPLE_IVE_NODE_S *pstQueueTmp = NULL;
149
150 if ((pstQueueHead == NULL) || (pstQueueHead->front == NULL)) {
151 return NULL;
152 }
153
154 pstQueueTmp = pstQueueHead->front;
155 pstQueueHead->front = pstQueueTmp->next;
156 if (pstQueueHead->front == NULL) {
157 pstQueueHead->rear = pstQueueHead->front;
158 }
159 s_s32CurQueueLen--;
160
161 return pstQueueTmp;
162 }
163
SAMPLE_IVE_QueueFreeNode(SAMPLE_IVE_NODE_S * pstNode)164 HI_VOID SAMPLE_IVE_QueueFreeNode(SAMPLE_IVE_NODE_S *pstNode)
165 {
166 if (pstNode != NULL) {
167 free(pstNode);
168 pstNode = NULL;
169 }
170
171 return;
172 }
173