Unified · Split
| @@ -1,5 +1,7 @@ | |||
| 1 | OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o | 1 | OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o |
| 2 | OBJS-$(CONFIG_DNN) += dnn/dnn_io_proc.o | 2 | OBJS-$(CONFIG_DNN) += dnn/dnn_io_proc.o |
| 3 | OBJS-$(CONFIG_DNN) += dnn/queue.o | ||
| 4 | OBJS-$(CONFIG_DNN) += dnn/safe_queue.o | ||
| 3 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o | 5 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o |
| 4 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layers.o | 6 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layers.o |
| 5 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_avgpool.o | 7 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_avgpool.o |
| @@ -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 | } | ||
| @@ -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 | ||
| @@ -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 | } | ||
| @@ -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 | ||