History

dnn/queue: add queue and safe_queue support

Xie, Lin committed 6 years ago

Commit 6506ab81 parent 1b649545 files · +347 −0Browse files

Unified · Split

5 files
  1. libavfilter/dnn/Makefile +2 −0
  2. libavfilter/dnn/queue.c +176 −0
  3. libavfilter/dnn/queue.h +41 −0
  4. libavfilter/dnn/safe_queue.c +92 −0
  5. libavfilter/dnn/safe_queue.h +36 −0
libavfilter/dnn/Makefile+2−0
@@ -1,5 +1,7 @@
11 OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o
22 OBJS-$(CONFIG_DNN) += dnn/dnn_io_proc.o
3+OBJS-$(CONFIG_DNN) += dnn/queue.o
4+OBJS-$(CONFIG_DNN) += dnn/safe_queue.o
35 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o
46 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layers.o
57 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_avgpool.o
libavfilter/dnn/queue.c+176−0
@@ -0,0 +1,176 @@
1+/*
2+ * Copyright (c) 2020
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+#include <stdio.h>
22+#include "queue.h"
23+#include "libavutil/mem.h"
24+#include "libavutil/avassert.h"
25+
26+typedef struct _queue_entry queue_entry;
27+
28+typedef struct _queue {
29+ queue_entry *head;
30+ queue_entry *tail;
31+ size_t length;
32+}queue;
33+
34+typedef struct _queue_entry {
35+ void *value;
36+ queue_entry *prev;
37+ queue_entry *next;
38+} queue_entry;
39+
40+static inline queue_entry *create_entry(void *val)
41+{
42+ queue_entry *entry = av_malloc(sizeof(*entry));
43+ av_assert0(entry != NULL);
44+ entry->value = val;
45+ return entry;
46+}
47+
48+queue* queue_create(void)
49+{
50+ queue *q = av_malloc(sizeof(*q));
51+ if (!q)
52+ return NULL;
53+
54+ q->head = create_entry(q);
55+ q->tail = create_entry(q);
56+ q->head->next = q->tail;
57+ q->tail->prev = q->head;
58+ q->head->prev = NULL;
59+ q->tail->next = NULL;
60+ q->length = 0;
61+
62+ return q;
63+}
64+
65+void queue_destroy(queue *q)
66+{
67+ queue_entry *entry;
68+ if (!q)
69+ return;
70+
71+ entry = q->head;
72+ while (entry != NULL) {
73+ queue_entry *temp = entry;
74+ entry = entry->next;
75+ av_freep(&temp);
76+ }
77+
78+ av_freep(&q);
79+}
80+
81+size_t queue_size(queue *q)
82+{
83+ return q ? q->length : 0;
84+}
85+
86+void *queue_peek_front(queue *q)
87+{
88+ if (!q || q->length == 0)
89+ return NULL;
90+
91+ return q->head->next->value;
92+}
93+
94+void *queue_peek_back(queue *q)
95+{
96+ if (!q || q->length == 0)
97+ return NULL;
98+
99+ return q->tail->prev->value;
100+}
101+
102+void queue_push_front(queue *q, void *v)
103+{
104+ queue_entry *new_entry;
105+ queue_entry *original_next;
106+ if (!q)
107+ return;
108+
109+ new_entry = create_entry(v);
110+ original_next = q->head->next;
111+
112+ q->head->next = new_entry;
113+ original_next->prev = new_entry;
114+ new_entry->prev = q->head;
115+ new_entry->next = original_next;
116+ q->length++;
117+}
118+
119+void queue_push_back(queue *q, void *v)
120+{
121+ queue_entry *new_entry;
122+ queue_entry *original_prev;
123+ if (!q)
124+ return;
125+
126+ new_entry = create_entry(v);
127+ original_prev = q->tail->prev;
128+
129+ q->tail->prev = new_entry;
130+ original_prev->next = new_entry;
131+ new_entry->next = q->tail;
132+ new_entry->prev = original_prev;
133+ q->length++;
134+}
135+
136+void *queue_pop_front(queue *q)
137+{
138+ queue_entry *front;
139+ queue_entry *new_head_next;
140+ void *ret;
141+
142+ if (!q || q->length == 0)
143+ return NULL;
144+
145+ front = q->head->next;
146+ new_head_next = front->next;
147+ ret = front->value;
148+
149+ q->head->next = new_head_next;
150+ new_head_next->prev = q->head;
151+
152+ av_freep(&front);
153+ q->length--;
154+ return ret;
155+}
156+
157+void *queue_pop_back(queue *q)
158+{
159+ queue_entry *back;
160+ queue_entry *new_tail_prev;
161+ void *ret;
162+
163+ if (!q || q->length == 0)
164+ return NULL;
165+
166+ back = q->tail->prev;
167+ new_tail_prev = back->prev;
168+ ret = back->value;
169+
170+ q->tail->prev = new_tail_prev;
171+ new_tail_prev->next = q->tail;
172+
173+ av_freep(&back);
174+ q->length--;
175+ return ret;
176+}
libavfilter/dnn/queue.h+41−0
@@ -0,0 +1,41 @@
1+/*
2+ * Copyright (c) 2020
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+
22+#ifndef AVFILTER_DNN_QUEUE_H
23+#define AVFILTER_DNN_QUEUE_H
24+
25+typedef struct _queue queue;
26+
27+queue *queue_create(void);
28+void queue_destroy(queue *q);
29+
30+size_t queue_size(queue *q);
31+
32+void *queue_peek_front(queue *q);
33+void *queue_peek_back(queue *q);
34+
35+void queue_push_front(queue *q, void *v);
36+void queue_push_back(queue *q, void *v);
37+
38+void *queue_pop_front(queue *q);
39+void *queue_pop_back(queue *q);
40+
41+#endif
libavfilter/dnn/safe_queue.c+92−0
@@ -0,0 +1,92 @@
1+/*
2+ * Copyright (c) 2020
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+#include <stdio.h>
22+#include "queue.h"
23+#include "safe_queue.h"
24+#include "libavutil/mem.h"
25+#include "libavutil/avassert.h"
26+#include "libavutil/thread.h"
27+
28+typedef struct _safe_queue {
29+ queue *q;
30+ pthread_mutex_t mutex;
31+ pthread_cond_t cond;
32+}safe_queue;
33+
34+safe_queue *safe_queue_create(void)
35+{
36+ safe_queue *sq = av_malloc(sizeof(*sq));
37+ if (!sq)
38+ return NULL;
39+
40+ sq->q = queue_create();
41+ if (!sq->q)
42+ return NULL;
43+
44+ pthread_mutex_init(&sq->mutex, NULL);
45+ pthread_cond_init(&sq->cond, NULL);
46+ return sq;
47+}
48+
49+void safe_queue_destroy(safe_queue *sq)
50+{
51+ if (!sq)
52+ return;
53+
54+ queue_destroy(sq->q);
55+ pthread_mutex_destroy(&sq->mutex);
56+ pthread_cond_destroy(&sq->cond);
57+ av_freep(&sq);
58+}
59+
60+size_t safe_queue_size(safe_queue *sq)
61+{
62+ return sq ? queue_size(sq->q) : 0;
63+}
64+
65+void safe_queue_push_front(safe_queue *sq, void *v)
66+{
67+ pthread_mutex_lock(&sq->mutex);
68+ queue_push_front(sq->q, v);
69+ pthread_cond_signal(&sq->cond);
70+ pthread_mutex_unlock(&sq->mutex);
71+}
72+
73+void safe_queue_push_back(safe_queue *sq, void *v)
74+{
75+ pthread_mutex_lock(&sq->mutex);
76+ queue_push_back(sq->q, v);
77+ pthread_cond_signal(&sq->cond);
78+ pthread_mutex_unlock(&sq->mutex);
79+}
80+
81+void *safe_queue_pop_front(safe_queue *sq)
82+{
83+ void *value;
84+ pthread_mutex_lock(&sq->mutex);
85+ while (queue_size(sq->q) == 0) {
86+ pthread_cond_wait(&sq->cond, &sq->mutex);
87+ }
88+ value = queue_pop_front(sq->q);
89+ pthread_cond_signal(&sq->cond);
90+ pthread_mutex_unlock(&sq->mutex);
91+ return value;
92+}
libavfilter/dnn/safe_queue.h+36−0
@@ -0,0 +1,36 @@
1+/*
2+ * Copyright (c) 2020
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+#ifndef AVFILTER_DNN_SAFE_QUEUE_H
22+#define AVFILTER_DNN_SAFE_QUEUE_H
23+
24+typedef struct _safe_queue safe_queue;
25+
26+safe_queue *safe_queue_create(void);
27+void safe_queue_destroy(safe_queue *sq);
28+
29+size_t safe_queue_size(safe_queue *sq);
30+
31+void safe_queue_push_front(safe_queue *sq, void *v);
32+void safe_queue_push_back(safe_queue *sq, void *v);
33+
34+void *safe_queue_pop_front(safe_queue *sq);
35+
36+#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t