|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2016-2023 Netflix, Inc. |
| 4 | + * |
| 5 | + * Licensed under the BSD+Patent License (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://opensource.org/licenses/BSDplusPatent |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include <errno.h> |
| 20 | +#include <string.h> |
| 21 | +#include <pthread.h> |
| 22 | +#include <stdbool.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include "framesync.h" |
| 25 | + |
| 26 | +enum { |
| 27 | + BUF_FREE = 0, |
| 28 | + BUF_ACQUIRED, |
| 29 | + BUF_FILLED, |
| 30 | + BUF_RETRIEVED, |
| 31 | +}; |
| 32 | + |
| 33 | +typedef struct VmafFrameSyncBuf { |
| 34 | + void *frame_data; |
| 35 | + int buf_status; |
| 36 | + signed long index; |
| 37 | + struct VmafFrameSyncBuf *next; |
| 38 | +} VmafFrameSyncBuf; |
| 39 | + |
| 40 | +typedef struct VmafFrameSyncContext { |
| 41 | + VmafFrameSyncBuf *buf_que; |
| 42 | + pthread_mutex_t acquire_lock; |
| 43 | + pthread_mutex_t retrieve_lock; |
| 44 | + pthread_cond_t retrieve; |
| 45 | + unsigned buf_cnt; |
| 46 | +} VmafFrameSyncContext; |
| 47 | + |
| 48 | +int vmaf_framesync_init(VmafFrameSyncContext **fs_ctx) |
| 49 | +{ |
| 50 | + VmafFrameSyncContext *const ctx = *fs_ctx = malloc(sizeof(VmafFrameSyncContext)); |
| 51 | + if (!ctx) return -ENOMEM; |
| 52 | + memset(ctx, 0, sizeof(VmafFrameSyncContext)); |
| 53 | + ctx->buf_cnt = 1; |
| 54 | + |
| 55 | + pthread_mutex_init(&(ctx->acquire_lock), NULL); |
| 56 | + pthread_mutex_init(&(ctx->retrieve_lock), NULL); |
| 57 | + pthread_cond_init(&(ctx->retrieve), NULL); |
| 58 | + |
| 59 | + VmafFrameSyncBuf *buf_que = ctx->buf_que = malloc(sizeof(VmafFrameSyncBuf)); |
| 60 | + |
| 61 | + buf_que->frame_data = NULL; |
| 62 | + buf_que->buf_status = BUF_FREE; |
| 63 | + buf_que->index = -1; |
| 64 | + buf_que->next = NULL; |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +int vmaf_framesync_acquire_new_buf(VmafFrameSyncContext *fs_ctx, void **data, |
| 70 | + unsigned data_sz, unsigned index) |
| 71 | +{ |
| 72 | + VmafFrameSyncBuf *buf_que = fs_ctx->buf_que; |
| 73 | + *data = NULL; |
| 74 | + |
| 75 | + pthread_mutex_lock(&(fs_ctx->acquire_lock)); |
| 76 | + |
| 77 | + // traverse until a free buffer is found |
| 78 | + for (unsigned i = 0; i < fs_ctx->buf_cnt; i++) { |
| 79 | + if (buf_que->buf_status == BUF_FREE) { |
| 80 | + buf_que->frame_data = *data = malloc(data_sz); |
| 81 | + if (!buf_que->frame_data) |
| 82 | + return -ENOMEM; |
| 83 | + buf_que->buf_status = BUF_ACQUIRED; |
| 84 | + buf_que->index = index; |
| 85 | + break; |
| 86 | + } |
| 87 | + // move to next node |
| 88 | + if (buf_que->next != NULL) |
| 89 | + buf_que = buf_que->next; |
| 90 | + } |
| 91 | + |
| 92 | + // create a new node if all nodes are occupied in the list and append to the tail |
| 93 | + if (*data == NULL) { |
| 94 | + VmafFrameSyncBuf *new_buf_node = malloc(sizeof(VmafFrameSyncBuf)); |
| 95 | + buf_que->next = new_buf_node; |
| 96 | + new_buf_node->buf_status = BUF_FREE; |
| 97 | + new_buf_node->index = -1; |
| 98 | + new_buf_node->next = NULL; |
| 99 | + fs_ctx->buf_cnt++; |
| 100 | + |
| 101 | + new_buf_node->frame_data = *data = malloc(data_sz); |
| 102 | + if (!new_buf_node->frame_data) |
| 103 | + return -ENOMEM; |
| 104 | + new_buf_node->buf_status = BUF_ACQUIRED; |
| 105 | + new_buf_node->index = index; |
| 106 | + } |
| 107 | + |
| 108 | + pthread_mutex_unlock(&(fs_ctx->acquire_lock)); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +int vmaf_framesync_submit_filled_data(VmafFrameSyncContext *fs_ctx, void *data, |
| 114 | + unsigned index) |
| 115 | +{ |
| 116 | + VmafFrameSyncBuf *buf_que = fs_ctx->buf_que; |
| 117 | + |
| 118 | + pthread_mutex_lock(&(fs_ctx->retrieve_lock)); |
| 119 | + |
| 120 | + // loop until a matchng buffer is found |
| 121 | + for (unsigned i = 0; i < fs_ctx->buf_cnt; i++) { |
| 122 | + if ((buf_que->index == index) && (buf_que->buf_status == BUF_ACQUIRED)) { |
| 123 | + buf_que->buf_status = BUF_FILLED; |
| 124 | + if (data != buf_que->frame_data) |
| 125 | + return -1; |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + // move to next node |
| 130 | + if (NULL != buf_que->next) |
| 131 | + buf_que = buf_que->next; |
| 132 | + } |
| 133 | + |
| 134 | + pthread_cond_broadcast(&(fs_ctx->retrieve)); |
| 135 | + pthread_mutex_unlock(&(fs_ctx->retrieve_lock)); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +int vmaf_framesync_retrieve_filled_data(VmafFrameSyncContext *fs_ctx, |
| 141 | + void **data, unsigned index) |
| 142 | +{ |
| 143 | + *data = NULL; |
| 144 | + |
| 145 | + while (*data == NULL) { |
| 146 | + VmafFrameSyncBuf *buf_que = fs_ctx->buf_que; |
| 147 | + pthread_mutex_lock(&(fs_ctx->retrieve_lock)); |
| 148 | + // loop until a free buffer is found |
| 149 | + for (unsigned i = 0; i < fs_ctx->buf_cnt; i++) { |
| 150 | + if ((buf_que->index == index) && (buf_que->buf_status == BUF_FILLED)) { |
| 151 | + buf_que->buf_status = BUF_RETRIEVED; |
| 152 | + *data = buf_que->frame_data; |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + // move to next node |
| 157 | + if (NULL != buf_que->next) |
| 158 | + buf_que = buf_que->next; |
| 159 | + } |
| 160 | + |
| 161 | + if (*data == NULL) |
| 162 | + pthread_cond_wait(&(fs_ctx->retrieve), &(fs_ctx->retrieve_lock)); |
| 163 | + |
| 164 | + pthread_mutex_unlock(&(fs_ctx->retrieve_lock)); |
| 165 | + } |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +int vmaf_framesync_release_buf(VmafFrameSyncContext *fs_ctx, void *data, |
| 171 | + unsigned index) |
| 172 | +{ |
| 173 | + VmafFrameSyncBuf *buf_que = fs_ctx->buf_que; |
| 174 | + |
| 175 | + pthread_mutex_lock(&(fs_ctx->acquire_lock)); |
| 176 | + // loop until a matching buffer is found |
| 177 | + for (unsigned i = 0; i < fs_ctx->buf_cnt; i++) { |
| 178 | + if ((buf_que->index == index) && (buf_que->buf_status == BUF_RETRIEVED)) { |
| 179 | + if (data != buf_que->frame_data) |
| 180 | + return -1; |
| 181 | + |
| 182 | + free(buf_que->frame_data); |
| 183 | + buf_que->frame_data = NULL; |
| 184 | + buf_que->buf_status = BUF_FREE; |
| 185 | + buf_que->index = -1; |
| 186 | + break; |
| 187 | + } |
| 188 | + |
| 189 | + // move to next node |
| 190 | + if (NULL != buf_que->next) |
| 191 | + buf_que = buf_que->next; |
| 192 | + } |
| 193 | + |
| 194 | + pthread_mutex_unlock(&(fs_ctx->acquire_lock)); |
| 195 | + return 0; |
| 196 | +} |
| 197 | + |
| 198 | +int vmaf_framesync_destroy(VmafFrameSyncContext *fs_ctx) |
| 199 | +{ |
| 200 | + VmafFrameSyncBuf *buf_que = fs_ctx->buf_que; |
| 201 | + VmafFrameSyncBuf *buf_que_tmp; |
| 202 | + |
| 203 | + pthread_mutex_destroy(&(fs_ctx->acquire_lock)); |
| 204 | + pthread_mutex_destroy(&(fs_ctx->retrieve_lock)); |
| 205 | + pthread_cond_destroy(&(fs_ctx->retrieve)); |
| 206 | + |
| 207 | + //check for any data buffers which are not freed |
| 208 | + for (unsigned i = 0; i < fs_ctx->buf_cnt; i++) { |
| 209 | + if (NULL != buf_que->frame_data) { |
| 210 | + free(buf_que->frame_data); |
| 211 | + buf_que->frame_data = NULL; |
| 212 | + } |
| 213 | + |
| 214 | + // move to next node |
| 215 | + if (NULL != buf_que->next) { |
| 216 | + buf_que_tmp = buf_que; |
| 217 | + buf_que = buf_que->next; |
| 218 | + free(buf_que_tmp); |
| 219 | + } else { |
| 220 | + free(buf_que); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + free(fs_ctx); |
| 225 | + |
| 226 | + return 0; |
| 227 | +} |
0 commit comments