MediaProcessors
proc_if.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Rafael Antoniello
3  *
4  * This file is part of MediaProcessors.
5  *
6  * MediaProcessors is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * MediaProcessors 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MediaProcessors. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 #include "proc_if.h"
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 
31 #include <libmediaprocsutils/log.h>
32 #include <libmediaprocsutils/stat_codes.h>
33 #include <libmediaprocsutils/check_utils.h>
34 #include <libmediaprocsutils/mem_utils.h>
35 
36 /* **** Implementations **** */
37 
39 {
40  {PROC_IF_FMT_UNDEF, "Undefined format"},
41  {PROC_IF_FMT_YUV420P, "Planar YUV 4:2:0 with 12bpp"},
42  //{PROC_IF_FMT_RGB24, "Packed RGB 8:8:8 with 24bpp"}, // Reserved for future use
43  {PROC_IF_FMT_S16, "Interleaved signed 16 bits"},
44  {PROC_IF_FMT_S16P, "Planar signed 16 bits"}
45 };
46 
48 {
49  return (proc_frame_ctx_t*)calloc(1, sizeof(proc_frame_ctx_t));
50 }
51 
53  const proc_frame_ctx_t *proc_frame_ctx_arg)
54 {
55  register int i, data_size, end_code= STAT_ERROR;
56  proc_frame_ctx_t *proc_frame_ctx= NULL;
57  LOG_CTX_INIT(NULL);
58 
59  /* Check arguments */
60  CHECK_DO(proc_frame_ctx_arg!= NULL, return NULL);
61 
62  proc_frame_ctx= proc_frame_ctx_allocate();
63  CHECK_DO(proc_frame_ctx!= NULL, goto end);
64 
65  /* Compute data buffer size (note that data buffer can not be reallocated
66  * because of alignment, thus full-size is needed to allocate buffer).
67  * Copy line-size, width and height parameters for each data plane.
68  */
69  for(i= 0, data_size= 0; i< PROC_FRAME_NUM_DATA_POINTERS; i++) {
70  register int lsize_src, lsize_dst, width, height;
71  const uint8_t *p_data_src= proc_frame_ctx_arg->p_data[i];
72  if(p_data_src== NULL)
73  continue; // We assume no data available for this plane
74 
75  lsize_src= proc_frame_ctx_arg->linesize[i];
76  lsize_dst= EXTEND_SIZE_TO_MULTIPLE(lsize_src, CTX_S_BASE_ALIGN);
77  width= proc_frame_ctx_arg->width[i];
78  height= proc_frame_ctx_arg->height[i];
79  CHECK_DO(width> 0 && (width<= PROC_FRAME_MAX_WIDTH || height== 1),
80  goto end);
81  CHECK_DO(height> 0 && height<= PROC_FRAME_MAX_HEIGHT, goto end);
82  CHECK_DO(lsize_src>= width && lsize_dst>= lsize_src, goto end);
83  data_size+= lsize_dst* height;
84  proc_frame_ctx->linesize[i]= lsize_dst;
85  proc_frame_ctx->width[i]= width;
86  proc_frame_ctx->height[i]= height;
87  }
88 
89  /* Allocate data buffer, copy data and set pointers to planes */
90  proc_frame_ctx->data= (uint8_t*)aligned_alloc(CTX_S_BASE_ALIGN, data_size);
91  CHECK_DO(proc_frame_ctx->data!= NULL, goto end);
92  for(i= 0, data_size= 0; i< PROC_FRAME_NUM_DATA_POINTERS; i++) {
93  register int l, lsize_src, lsize, width, height;
94  const uint8_t *p_data_dst;
95  const uint8_t *p_data_src= proc_frame_ctx_arg->p_data[i];
96  if(p_data_src== NULL)
97  continue;
98 
99  lsize_src= proc_frame_ctx_arg->linesize[i];
100  lsize= proc_frame_ctx->linesize[i];
101  width= proc_frame_ctx->width[i];
102  height= proc_frame_ctx->height[i];
103  proc_frame_ctx->p_data[i]= p_data_dst= proc_frame_ctx->data+ data_size;
104  data_size+= lsize* height;
105 
106  for(l= 0; l< height; l++)
107  memcpy((void*)&p_data_dst[l* lsize], &p_data_src[l* lsize_src],
108  width);
109  }
110 
111  /* Copy rest of parameters */
112  proc_frame_ctx->proc_sample_fmt= proc_frame_ctx_arg->proc_sample_fmt;
113  proc_frame_ctx->proc_sampling_rate= proc_frame_ctx_arg->proc_sampling_rate;
114  proc_frame_ctx->pts= proc_frame_ctx_arg->pts;
115  proc_frame_ctx->dts= proc_frame_ctx_arg->dts;
116  proc_frame_ctx->es_id= proc_frame_ctx_arg->es_id;
117 
118  end_code= STAT_SUCCESS;
119 end:
120  if(end_code!= STAT_SUCCESS)
121  proc_frame_ctx_release(&proc_frame_ctx);
122  return proc_frame_ctx;
123 }
124 
125 void proc_frame_ctx_release(proc_frame_ctx_t **ref_proc_frame_ctx)
126 {
127  proc_frame_ctx_t *proc_frame_ctx= NULL;
128 
129  if(ref_proc_frame_ctx== NULL ||
130  (proc_frame_ctx= *ref_proc_frame_ctx)== NULL)
131  return;
132 
133  if(proc_frame_ctx->data!= NULL) {
134  free(proc_frame_ctx->data);
135  proc_frame_ctx->data= NULL;
136  }
137 
138  free(proc_frame_ctx);
139  *ref_proc_frame_ctx= NULL;
140 }
141 
143 {
144  return (proc_if_t*)calloc(1, sizeof(proc_if_t));
145 }
146 
147 proc_if_t* proc_if_dup(const proc_if_t *proc_if_arg)
148 {
149  int end_code= STAT_ERROR;
150  proc_if_t *proc_if= NULL;
151  LOG_CTX_INIT(NULL);
152 
153  /* Check arguments */
154  CHECK_DO(proc_if_arg!= NULL, return NULL);
155 
156  /* Allocate context structure */
157  proc_if= proc_if_allocate();
158  CHECK_DO(proc_if!= NULL, goto end);
159 
160  /* Copy simple-type members values.
161  * Note that pointers to callback are all supposed to be static values,
162  * for this reason we just copy (not duplicate) the pointer values.
163  */
164  memcpy(proc_if, proc_if_arg, sizeof(proc_if_t));
165 
166  /* **** Duplicate members that use dynamic memory allocations **** */
167 
168  CHECK_DO(proc_if_arg->proc_name!= NULL, goto end);
169  proc_if->proc_name= strdup(proc_if_arg->proc_name);
170  CHECK_DO(proc_if->proc_name!= NULL, goto end);
171 
172  CHECK_DO(proc_if_arg->proc_type!= NULL, goto end);
173  proc_if->proc_type= strdup(proc_if_arg->proc_type);
174  CHECK_DO(proc_if->proc_type!= NULL, goto end);
175 
176  CHECK_DO(proc_if_arg->proc_mime!= NULL, goto end);
177  proc_if->proc_mime= strdup(proc_if_arg->proc_mime);
178  CHECK_DO(proc_if->proc_mime!= NULL, goto end);
179 
180  end_code= STAT_SUCCESS;
181 end:
182  if(end_code!= STAT_SUCCESS)
183  proc_if_release(&proc_if);
184  return proc_if;
185 }
186 
187 int proc_if_cmp(const proc_if_t* proc_if1, const proc_if_t* proc_if2)
188 {
189  int ret_value= 1; // means "non-equal"
190  LOG_CTX_INIT(NULL);
191 
192  /* Check arguments */
193  CHECK_DO(proc_if1!= NULL, return 1);
194  CHECK_DO(proc_if2!= NULL, return 1);
195 
196  /* Compare contexts fields */
197  if(strcmp(proc_if1->proc_name, proc_if2->proc_name)!= 0)
198  goto end;
199  if(strcmp(proc_if1->proc_type, proc_if2->proc_type)!= 0)
200  goto end;
201  if(strcmp(proc_if1->proc_mime, proc_if2->proc_mime)!= 0)
202  goto end;
203  if(proc_if1->open!= proc_if2->open)
204  goto end;
205  if(proc_if1->close!= proc_if2->close)
206  goto end;
207  if(proc_if1->rest_put!= proc_if2->rest_put)
208  goto end;
209  if(proc_if1->rest_get!= proc_if2->rest_get)
210  goto end;
211  if(proc_if1->process_frame!= proc_if2->process_frame)
212  goto end;
213  if(proc_if1->opt!= proc_if2->opt)
214  goto end;
215  if(proc_if1->iput_fifo_elem_opaque_dup!=
216  proc_if2->iput_fifo_elem_opaque_dup)
217  goto end;
218  if(proc_if1->iput_fifo_elem_opaque_release!=
220  goto end;
221  if(proc_if1->oput_fifo_elem_opaque_dup!=
222  proc_if2->oput_fifo_elem_opaque_dup)
223  goto end;
224 
225  // Reserved for future use: compare new fields here...
226 
227  ret_value= 0; // contexts are equal
228 end:
229  return ret_value;
230 }
231 
232 void proc_if_release(proc_if_t **ref_proc_if)
233 {
234  proc_if_t *proc_if= NULL;
235 
236  if(ref_proc_if== NULL)
237  return;
238 
239  if((proc_if= *ref_proc_if)!= NULL) {
240 
241  if(proc_if->proc_name!= NULL) {
242  free((void*)proc_if->proc_name);
243  proc_if->proc_name= NULL;
244  }
245 
246  if(proc_if->proc_type!= NULL) {
247  free((void*)proc_if->proc_type);
248  proc_if->proc_type= NULL;
249  }
250 
251  if(proc_if->proc_mime!= NULL) {
252  free((void*)proc_if->proc_mime);
253  proc_if->proc_mime= NULL;
254  }
255 
256  free(proc_if);
257  *ref_proc_if= NULL;
258  }
259 }
size_t width[PROC_FRAME_NUM_DATA_POINTERS]
Definition: proc_if.h:113
void(* iput_fifo_elem_opaque_release)(void **ref_t)
Definition: proc_if.h:324
const char * proc_mime
Definition: proc_if.h:186
void proc_frame_ctx_release(proc_frame_ctx_t **ref_proc_frame_ctx)
Definition: proc_if.c:125
#define EXTEND_SIZE_TO_MULTIPLE(SIZE, MULTIPLE)
Definition: mem_utils.h:52
int proc_sampling_rate
Definition: proc_if.h:134
int linesize[PROC_FRAME_NUM_DATA_POINTERS]
Definition: proc_if.h:107
#define CTX_S_BASE_ALIGN
Definition: mem_utils.h:47
proc_ctx_t *(* open)(const proc_if_t *proc_if, const char *settings_str, log_ctx_t *log_ctx, va_list arg)
Definition: proc_if.h:207
int proc_sample_fmt
Definition: proc_if.h:127
int64_t dts
Definition: proc_if.h:144
const char * proc_type
Definition: proc_if.h:179
void proc_if_release(proc_if_t **ref_proc_if)
Definition: proc_if.c:232
int(* process_frame)(proc_ctx_t *proc_ctx, fifo_ctx_t *fifo_ctx_iput, fifo_ctx_t *fifo_ctx_oput)
Definition: proc_if.h:279
int(* opt)(proc_ctx_t *proc_ctx, const char *tag, va_list arg)
Definition: proc_if.h:295
proc_if_t * proc_if_dup(const proc_if_t *proc_if_arg)
Definition: proc_if.c:147
#define CHECK_DO(COND, ACTION)
Definition: check_utils.h:57
Planar YUV 4:2:0 with 12bpp (video)
Definition: proc_if.h:55
proc_frame_ctx_t * proc_frame_ctx_allocate()
Definition: proc_if.c:47
const proc_sample_fmt_lut_t proc_sample_fmt_lut[]
Definition: proc_if.c:38
void(* close)(proc_ctx_t **ref_proc_ctx)
Definition: proc_if.h:217
#define PROC_FRAME_MAX_HEIGHT
Definition: proc_if.h:48
#define PROC_FRAME_MAX_WIDTH
Definition: proc_if.h:44
int proc_if_cmp(const proc_if_t *proc_if1, const proc_if_t *proc_if2)
Definition: proc_if.c:187
proc_if_t * proc_if_allocate()
Definition: proc_if.c:142
Planar signed 16 bits (typically audio)
Definition: proc_if.h:58
Undefined format.
Definition: proc_if.h:54
proc_frame_ctx_t * proc_frame_ctx_dup(const proc_frame_ctx_t *proc_frame_ctx_arg)
Definition: proc_if.c:52
void *(* iput_fifo_elem_opaque_dup)(const proc_frame_ctx_t *proc_frame_ctx)
Definition: proc_if.h:311
size_t height[PROC_FRAME_NUM_DATA_POINTERS]
Definition: proc_if.h:119
Interleaved signed 16 bits (typically audio)
Definition: proc_if.h:57
const char * proc_name
Definition: proc_if.h:174
PROC interface prototype related definitions and functions.
const uint8_t * p_data[PROC_FRAME_NUM_DATA_POINTERS]
Definition: proc_if.h:94
int(* rest_get)(proc_ctx_t *proc_ctx, const proc_if_rest_fmt_t rest_fmt, void **ref_reponse)
Definition: proc_if.h:265
uint8_t * data
Definition: proc_if.h:84
proc_frame_ctx_t *(* oput_fifo_elem_opaque_dup)(const void *t)
Definition: proc_if.h:339
int64_t pts
Definition: proc_if.h:138
int(* rest_put)(proc_ctx_t *proc_ctx, const char *str)
Definition: proc_if.h:248