UniMRCP  1.7.0
mpf_codec.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_H
18 #define MPF_CODEC_H
19 
20 /**
21  * @file mpf_codec.h
22  * @brief MPF Codec
23  */
24 
25 #include "mpf_codec_descriptor.h"
26 
28 
29 /** Codec virtual table declaration */
31 /** Codec declaration*/
32 typedef struct mpf_codec_t mpf_codec_t;
33 
34 /** Codec */
35 struct mpf_codec_t {
36  /** Codec manipulators (encode, decode, dissect) */
38  /** Codec attributes (capabilities) */
40  /** Optional static codec descriptor (pt < 96) */
42 };
43 
44 /** Table of codec virtual methods */
46  /** Virtual open method */
47  apt_bool_t (*open)(mpf_codec_t *codec);
48  /** Virtual close method */
49  apt_bool_t (*close)(mpf_codec_t *codec);
50 
51  /** Virtual encode method */
52  apt_bool_t (*encode)(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out);
53  /** Virtual decode method */
54  apt_bool_t (*decode)(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out);
55 
56  /** Virtual dissect method */
57  apt_bool_t (*dissect)(mpf_codec_t *codec, void **buffer, apr_size_t *size, mpf_codec_frame_t *frame);
58 
59  /** Virtual initialize method */
60  apt_bool_t (*initialize)(mpf_codec_t *codec, mpf_codec_frame_t *frame_out);
61 };
62 
63 /**
64  * Create codec.
65  * @param vtable the table of virtual mthods
66  * @param attribs the codec attributes
67  * @param descriptor the codec descriptor
68  * @param pool the pool to allocate memory from
69  */
70 static APR_INLINE mpf_codec_t* mpf_codec_create(
71  const mpf_codec_vtable_t *vtable,
73  const mpf_codec_descriptor_t *descriptor,
74  apr_pool_t *pool)
75 {
76  mpf_codec_t *codec = (mpf_codec_t*)apr_palloc(pool,sizeof(mpf_codec_t));
77  codec->vtable = vtable;
78  codec->attribs = attribs;
79  codec->static_descriptor = descriptor;
80  return codec;
81 }
82 
83 /**
84  * Clone codec.
85  * @param src_codec the source (original) codec to clone
86  * @param pool the pool to allocate memory from
87  */
88 static APR_INLINE mpf_codec_t* mpf_codec_clone(mpf_codec_t *src_codec, apr_pool_t *pool)
89 {
90  mpf_codec_t *codec = (mpf_codec_t*)apr_palloc(pool,sizeof(mpf_codec_t));
91  codec->vtable = src_codec->vtable;
92  codec->attribs = src_codec->attribs;
93  codec->static_descriptor = src_codec->static_descriptor;
94  return codec;
95 }
96 
97 /** Open codec */
98 static APR_INLINE apt_bool_t mpf_codec_open(mpf_codec_t *codec)
99 {
100  apt_bool_t rv = TRUE;
101  if(codec->vtable->open) {
102  rv = codec->vtable->open(codec);
103  }
104  return rv;
105 }
106 
107 /** Close codec */
108 static APR_INLINE apt_bool_t mpf_codec_close(mpf_codec_t *codec)
109 {
110  apt_bool_t rv = TRUE;
111  if(codec->vtable->close) {
112  rv = codec->vtable->close(codec);
113  }
114  return rv;
115 }
116 
117 /** Encode codec frame */
118 static APR_INLINE apt_bool_t mpf_codec_encode(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out)
119 {
120  apt_bool_t rv = TRUE;
121  if(codec->vtable->encode) {
122  rv = codec->vtable->encode(codec,frame_in,frame_out);
123  }
124  return rv;
125 }
126 
127 /** Decode codec frame */
128 static APR_INLINE apt_bool_t mpf_codec_decode(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out)
129 {
130  apt_bool_t rv = TRUE;
131  if(codec->vtable->decode) {
132  rv = codec->vtable->decode(codec,frame_in,frame_out);
133  }
134  return rv;
135 }
136 
137 /** Dissect codec frame (navigate through codec frames in a buffer, which may contain multiple frames) */
138 static APR_INLINE apt_bool_t mpf_codec_dissect(mpf_codec_t *codec, void **buffer, apr_size_t *size, mpf_codec_frame_t *frame)
139 {
140  apt_bool_t rv = TRUE;
141  if(codec->vtable->dissect) {
142  /* custom dissector for codecs like G.729, G.723 */
143  rv = codec->vtable->dissect(codec,buffer,size,frame);
144  }
145  else {
146  /* default dissector */
147  if(*size >= frame->size && frame->size) {
148  memcpy(frame->buffer,*buffer,frame->size);
149 
150  *buffer = (apr_byte_t*)*buffer + frame->size;
151  *size = *size - frame->size;
152  }
153  else {
154  rv = FALSE;
155  }
156  }
157  return rv;
158 }
159 
160 /** Initialize (fill) codec frame with silence */
161 static APR_INLINE apt_bool_t mpf_codec_initialize(mpf_codec_t *codec, mpf_codec_frame_t *frame_out)
162 {
163  apt_bool_t rv = TRUE;
164  if(codec->vtable->initialize) {
165  rv = codec->vtable->initialize(codec,frame_out);
166  }
167  else {
168  memset(frame_out->buffer,0,frame_out->size);
169  }
170  return rv;
171 }
172 
174 
175 #endif /* MPF_CODEC_H */
Definition: mpf_codec_descriptor.h:89
#define APT_END_EXTERN_C
Definition: apt.h:38
apt_bool_t(* decode)(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out)
Definition: mpf_codec.h:54
int apt_bool_t
Definition: apt.h:57
Definition: mpf_codec.h:35
Definition: mpf_codec.h:45
#define APT_BEGIN_EXTERN_C
Definition: apt.h:36
Definition: mpf_codec_descriptor.h:107
apt_bool_t(* encode)(mpf_codec_t *codec, const mpf_codec_frame_t *frame_in, mpf_codec_frame_t *frame_out)
Definition: mpf_codec.h:52
apr_size_t size
Definition: mpf_codec_descriptor.h:111
apt_bool_t(* open)(mpf_codec_t *codec)
Definition: mpf_codec.h:47
apt_bool_t(* dissect)(mpf_codec_t *codec, void **buffer, apr_size_t *size, mpf_codec_frame_t *frame)
Definition: mpf_codec.h:57
void * buffer
Definition: mpf_codec_descriptor.h:109
apt_bool_t(* close)(mpf_codec_t *codec)
Definition: mpf_codec.h:49
MPF Codec Descriptor.
const mpf_codec_descriptor_t * static_descriptor
Definition: mpf_codec.h:41
apt_bool_t(* initialize)(mpf_codec_t *codec, mpf_codec_frame_t *frame_out)
Definition: mpf_codec.h:60
const mpf_codec_attribs_t * attribs
Definition: mpf_codec.h:39
const mpf_codec_vtable_t * vtable
Definition: mpf_codec.h:37
Definition: mpf_codec_descriptor.h:63