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