UniMRCP  1.7.0
mpf_codec_descriptor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2015 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MPF_CODEC_DESCRIPTOR_H
18 #define MPF_CODEC_DESCRIPTOR_H
19 
20 /**
21  * @file mpf_codec_descriptor.h
22  * @brief MPF Codec Descriptor
23  */
24 
25 #include <apr_tables.h>
26 #include "apt_string.h"
27 #include "mpf.h"
28 
30 
31 /** Codec frame time base in msec */
32 #define CODEC_FRAME_TIME_BASE 10
33 /** Bytes per sample for linear pcm */
34 #define BYTES_PER_SAMPLE 2
35 /** Bits per sample for linear pcm */
36 #define BITS_PER_SAMPLE 16
37 
38 /** Supported sampling rates */
39 typedef enum {
40  MPF_SAMPLE_RATE_NONE = 0x00,
41  MPF_SAMPLE_RATE_8000 = 0x01,
42  MPF_SAMPLE_RATE_16000 = 0x02,
43  MPF_SAMPLE_RATE_32000 = 0x04,
44  MPF_SAMPLE_RATE_48000 = 0x08,
45 
46  MPF_SAMPLE_RATE_SUPPORTED = MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000 |
47  MPF_SAMPLE_RATE_32000 | MPF_SAMPLE_RATE_48000
49 
50 /** Codec descriptor declaration */
52 /** Codec attributes declaration */
54 /** Codec list declaration */
56 /** Codec capabilities declaration */
58 /** Codec frame declaration */
60 
61 
62 /** Codec descriptor */
64  /** Payload type used in RTP packet */
65  apr_byte_t payload_type;
66  /** Codec name */
68  /** Sampling rate */
69  apr_uint16_t sampling_rate;
70  /** Channel count */
71  apr_byte_t channel_count;
72  /** Codec dependent additional format */
74  /** Enabled/disabled state */
76 };
77 
78 /** List of codec descriptors */
80  /** Dynamic array of codec descriptors (mpf_codec_descriptor_t) */
81  apr_array_header_t *descriptor_arr;
82  /** Preffered primary (audio/video codec) descriptor from descriptor_arr */
84  /** Preffered named event (telephone-event) descriptor from descriptor_arr */
86 };
87 
88 /** Codec attributes */
90  /** Codec name */
92  /** Bits per sample */
93  apr_byte_t bits_per_sample;
94  /** Supported sampling rates (mpf_sample_rates_e) */
96 };
97 
98 /** List of codec attributes (capabilities) */
100  /** Dynamic array of codec attributes (mpf_codec_attrribs_t) */
101  apr_array_header_t *attrib_arr;
102  /** Allow/support named events */
104 };
105 
106 /** Codec frame */
108  /** Raw buffer, which may contain encoded or decoded data */
109  void *buffer;
110  /** Buffer size */
111  apr_size_t size;
112 };
113 
114 
115 /** Initialize codec descriptor */
116 static APR_INLINE void mpf_codec_descriptor_init(mpf_codec_descriptor_t *descriptor)
117 {
118  descriptor->payload_type = 0;
119  apt_string_reset(&descriptor->name);
120  descriptor->sampling_rate = 0;
121  descriptor->channel_count = 0;
122  apt_string_reset(&descriptor->format);
123  descriptor->enabled = TRUE;
124 }
125 
126 /** Initialize codec descriptor */
127 static APR_INLINE mpf_codec_descriptor_t* mpf_codec_descriptor_create(apr_pool_t *pool)
128 {
129  mpf_codec_descriptor_t *descriptor = (mpf_codec_descriptor_t*) apr_palloc(pool,sizeof(mpf_codec_descriptor_t));
130  mpf_codec_descriptor_init(descriptor);
131  return descriptor;
132 }
133 
134 /** Calculate encoded frame size in bytes */
135 static APR_INLINE apr_size_t mpf_codec_frame_size_calculate(const mpf_codec_descriptor_t *descriptor, const mpf_codec_attribs_t *attribs)
136 {
137  return (apr_size_t) descriptor->channel_count * attribs->bits_per_sample * CODEC_FRAME_TIME_BASE *
138  descriptor->sampling_rate / 1000 / 8; /* 1000 - msec per sec, 8 - bits per byte */
139 }
140 
141 /** Calculate samples of the frame (ts) */
142 static APR_INLINE apr_size_t mpf_codec_frame_samples_calculate(const mpf_codec_descriptor_t *descriptor)
143 {
144  return (apr_size_t) descriptor->channel_count * CODEC_FRAME_TIME_BASE * descriptor->sampling_rate / 1000;
145 }
146 
147 /** Calculate linear frame size in bytes */
148 static APR_INLINE apr_size_t mpf_codec_linear_frame_size_calculate(apr_uint16_t sampling_rate, apr_byte_t channel_count)
149 {
150  return (apr_size_t) channel_count * BYTES_PER_SAMPLE * CODEC_FRAME_TIME_BASE * sampling_rate / 1000;
151 }
152 
153 
154 
155 /** Reset list of codec descriptors */
156 static APR_INLINE void mpf_codec_list_reset(mpf_codec_list_t *codec_list)
157 {
158  codec_list->descriptor_arr = NULL;
159  codec_list->primary_descriptor = NULL;
160  codec_list->event_descriptor = NULL;
161 }
162 
163 /** Initialize list of codec descriptors */
164 static APR_INLINE void mpf_codec_list_init(mpf_codec_list_t *codec_list, apr_size_t initial_count, apr_pool_t *pool)
165 {
166  codec_list->descriptor_arr = apr_array_make(pool,(int)initial_count, sizeof(mpf_codec_descriptor_t));
167  codec_list->primary_descriptor = NULL;
168  codec_list->event_descriptor = NULL;
169 }
170 
171 /** Copy list of codec descriptors */
172 static APR_INLINE void mpf_codec_list_copy(mpf_codec_list_t *codec_list, const mpf_codec_list_t *src_codec_list, apr_pool_t *pool)
173 {
174  codec_list->descriptor_arr = apr_array_copy(pool,src_codec_list->descriptor_arr);
175 }
176 
177 /** Increment number of codec descriptors in the list and return the descriptor to fill */
178 static APR_INLINE mpf_codec_descriptor_t* mpf_codec_list_add(mpf_codec_list_t *codec_list)
179 {
180  mpf_codec_descriptor_t *descriptor = (mpf_codec_descriptor_t*)apr_array_push(codec_list->descriptor_arr);
181  mpf_codec_descriptor_init(descriptor);
182  return descriptor;
183 }
184 
185 /** Determine if codec list is empty */
186 static APR_INLINE apt_bool_t mpf_codec_list_is_empty(const mpf_codec_list_t *codec_list)
187 {
188  return apr_is_empty_array(codec_list->descriptor_arr);
189 }
190 
191 /** Get codec descriptor by index */
192 static APR_INLINE mpf_codec_descriptor_t* mpf_codec_list_descriptor_get(const mpf_codec_list_t *codec_list, apr_size_t id)
193 {
194  if(id >= (apr_size_t)codec_list->descriptor_arr->nelts) {
195  return NULL;
196  }
197  return &APR_ARRAY_IDX(codec_list->descriptor_arr,id,mpf_codec_descriptor_t);
198 }
199 
200 /** Create linear PCM descriptor */
202 
203 /** Create codec descriptor by capabilities */
205 
206 /** Match two codec descriptors */
208 
209 /** Match specified codec descriptor and the default lpcm one */
211 
212 /** Match codec descriptor by attribs specified */
214 
215 
216 
217 /** Initialize codec capabilities */
218 static APR_INLINE void mpf_codec_capabilities_init(mpf_codec_capabilities_t *capabilities, apr_size_t initial_count, apr_pool_t *pool)
219 {
220  capabilities->attrib_arr = apr_array_make(pool,(int)initial_count, sizeof(mpf_codec_attribs_t));
221  capabilities->allow_named_events = TRUE;
222 }
223 
224 /** Clone codec capabilities */
225 static APR_INLINE void mpf_codec_capabilities_clone(mpf_codec_capabilities_t *capabilities, const mpf_codec_capabilities_t *src_capabilities, apr_pool_t *pool)
226 {
227  capabilities->attrib_arr = apr_array_copy(pool,src_capabilities->attrib_arr);
228  capabilities->allow_named_events = src_capabilities->allow_named_events;
229 }
230 
231 /** Merge codec capabilities */
232 static APR_INLINE apt_bool_t mpf_codec_capabilities_merge(mpf_codec_capabilities_t *capabilities, const mpf_codec_capabilities_t *src_capabilities, apr_pool_t *pool)
233 {
234  if(capabilities->allow_named_events == FALSE && src_capabilities->allow_named_events == TRUE) {
235  capabilities->allow_named_events = src_capabilities->allow_named_events;
236  }
237  capabilities->attrib_arr = apr_array_append(pool,capabilities->attrib_arr,src_capabilities->attrib_arr);
238  return TRUE;
239 }
240 
241 /** Add codec capabilities */
242 static APR_INLINE apt_bool_t mpf_codec_capabilities_add(mpf_codec_capabilities_t *capabilities, int sample_rates, const char *codec_name)
243 {
244  mpf_codec_attribs_t *attribs = (mpf_codec_attribs_t*)apr_array_push(capabilities->attrib_arr);
245  apt_string_assign(&attribs->name,codec_name,capabilities->attrib_arr->pool);
246  attribs->sample_rates = sample_rates;
247  attribs->bits_per_sample = 0;
248  return TRUE;
249 }
250 
251 /** Add default (linear PCM) capabilities */
253 
254 /** Validate codec capabilities */
255 static APR_INLINE apt_bool_t mpf_codec_capabilities_validate(mpf_codec_capabilities_t *capabilities)
256 {
257  if(apr_is_empty_array(capabilities->attrib_arr) == TRUE) {
259  }
260  return TRUE;
261 }
262 
263 
264 
265 /** Find matched descriptor in codec list */
267 
268 /** Match codec list with specified capabilities */
270 
271 /** Intersect two codec lists */
273 
274 /** Compare two codec lists */
275 MPF_DECLARE(apt_bool_t) mpf_codec_lists_compare(const mpf_codec_list_t *codec_list1, const mpf_codec_list_t *codec_list2);
276 
277 /** Get sampling rate mask (mpf_sample_rate_e) by integer value */
279 
280 
282 
283 #endif /* MPF_CODEC_DESCRIPTOR_H */
apt_bool_t mpf_codec_descriptors_match(const mpf_codec_descriptor_t *descriptor1, const mpf_codec_descriptor_t *descriptor2)
Definition: mpf_codec_descriptor.h:79
apt_str_t format
Definition: mpf_codec_descriptor.h:73
apt_bool_t mpf_codec_lists_intersect(mpf_codec_list_t *codec_list1, mpf_codec_list_t *codec_list2)
Definition: mpf_codec_descriptor.h:89
mpf_codec_descriptor_t * mpf_codec_list_descriptor_find(const mpf_codec_list_t *codec_list, const mpf_codec_descriptor_t *descriptor)
mpf_sample_rates_e
Definition: mpf_codec_descriptor.h:39
#define APT_END_EXTERN_C
Definition: apt.h:38
apr_array_header_t * attrib_arr
Definition: mpf_codec_descriptor.h:101
apt_bool_t mpf_codec_descriptor_match_by_attribs(mpf_codec_descriptor_t *descriptor, const mpf_codec_descriptor_t *static_descriptor, const mpf_codec_attribs_t *attribs)
apt_bool_t mpf_codec_lists_compare(const mpf_codec_list_t *codec_list1, const mpf_codec_list_t *codec_list2)
int apt_bool_t
Definition: apt.h:57
mpf_codec_descriptor_t * mpf_codec_descriptor_create_by_capabilities(const mpf_codec_capabilities_t *capabilities, const mpf_codec_descriptor_t *peer, apr_pool_t *pool)
apr_array_header_t * descriptor_arr
Definition: mpf_codec_descriptor.h:81
int mpf_sample_rate_mask_get(apr_uint16_t sampling_rate)
apt_bool_t mpf_codec_list_match(mpf_codec_list_t *codec_list, const mpf_codec_capabilities_t *capabilities)
#define BYTES_PER_SAMPLE
Definition: mpf_codec_descriptor.h:34
#define MPF_DECLARE(type)
Definition: mpf.h:40
#define APT_BEGIN_EXTERN_C
Definition: apt.h:36
apt_str_t name
Definition: mpf_codec_descriptor.h:91
Definition: mpf_codec_descriptor.h:107
#define CODEC_FRAME_TIME_BASE
Definition: mpf_codec_descriptor.h:32
apr_size_t size
Definition: mpf_codec_descriptor.h:111
apr_byte_t payload_type
Definition: mpf_codec_descriptor.h:65
apt_bool_t allow_named_events
Definition: mpf_codec_descriptor.h:103
apr_byte_t bits_per_sample
Definition: mpf_codec_descriptor.h:93
void * buffer
Definition: mpf_codec_descriptor.h:109
apr_uint16_t sampling_rate
Definition: mpf_codec_descriptor.h:69
Media Processing Framework Definitions.
apt_bool_t mpf_codec_lpcm_descriptor_match(const mpf_codec_descriptor_t *descriptor)
Definition: apt_string.h:36
mpf_codec_descriptor_t * mpf_codec_lpcm_descriptor_create(apr_uint16_t sampling_rate, apr_byte_t channel_count, apr_pool_t *pool)
Definition: mpf_codec_descriptor.h:99
int sample_rates
Definition: mpf_codec_descriptor.h:95
mpf_codec_descriptor_t * primary_descriptor
Definition: mpf_codec_descriptor.h:83
String Representation.
apt_str_t name
Definition: mpf_codec_descriptor.h:67
mpf_codec_descriptor_t * event_descriptor
Definition: mpf_codec_descriptor.h:85
apr_byte_t channel_count
Definition: mpf_codec_descriptor.h:71
apt_bool_t mpf_codec_default_capabilities_add(mpf_codec_capabilities_t *capabilities)
Definition: mpf_codec_descriptor.h:63
apt_bool_t enabled
Definition: mpf_codec_descriptor.h:75