Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjmedia-codec/ipp_codecs.h> |
| 21 | #include <pjmedia/codec.h> |
| 22 | #include <pjmedia/errno.h> |
| 23 | #include <pjmedia/endpoint.h> |
| 24 | #include <pjmedia/plc.h> |
| 25 | #include <pjmedia/port.h> |
| 26 | #include <pjmedia/silencedet.h> |
| 27 | #include <pj/assert.h> |
| 28 | #include <pj/log.h> |
| 29 | #include <pj/pool.h> |
| 30 | #include <pj/string.h> |
| 31 | #include <pj/os.h> |
| 32 | |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 33 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 34 | /* |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 35 | * Only build this file if PJMEDIA_HAS_INTEL_IPP != 0 |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 36 | */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 37 | #if defined(PJMEDIA_HAS_INTEL_IPP) && PJMEDIA_HAS_INTEL_IPP != 0 |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 38 | |
| 39 | #include <usc.h> |
Benny Prijono | e1e6d51 | 2009-01-02 15:17:47 +0000 | [diff] [blame] | 40 | #include <ippversion.h> |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 41 | |
| 42 | #define THIS_FILE "ipp_codecs.c" |
| 43 | |
| 44 | /* Prototypes for IPP codecs factory */ |
| 45 | static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory, |
| 46 | const pjmedia_codec_info *id ); |
| 47 | static pj_status_t ipp_default_attr( pjmedia_codec_factory *factory, |
| 48 | const pjmedia_codec_info *id, |
| 49 | pjmedia_codec_param *attr ); |
| 50 | static pj_status_t ipp_enum_codecs( pjmedia_codec_factory *factory, |
| 51 | unsigned *count, |
| 52 | pjmedia_codec_info codecs[]); |
| 53 | static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory, |
| 54 | const pjmedia_codec_info *id, |
| 55 | pjmedia_codec **p_codec); |
| 56 | static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory, |
| 57 | pjmedia_codec *codec ); |
| 58 | |
| 59 | /* Prototypes for IPP codecs implementation. */ |
| 60 | static pj_status_t ipp_codec_init( pjmedia_codec *codec, |
| 61 | pj_pool_t *pool ); |
| 62 | static pj_status_t ipp_codec_open( pjmedia_codec *codec, |
| 63 | pjmedia_codec_param *attr ); |
| 64 | static pj_status_t ipp_codec_close( pjmedia_codec *codec ); |
| 65 | static pj_status_t ipp_codec_modify(pjmedia_codec *codec, |
| 66 | const pjmedia_codec_param *attr ); |
| 67 | static pj_status_t ipp_codec_parse( pjmedia_codec *codec, |
| 68 | void *pkt, |
| 69 | pj_size_t pkt_size, |
| 70 | const pj_timestamp *ts, |
| 71 | unsigned *frame_cnt, |
| 72 | pjmedia_frame frames[]); |
| 73 | static pj_status_t ipp_codec_encode( pjmedia_codec *codec, |
| 74 | const struct pjmedia_frame *input, |
| 75 | unsigned output_buf_len, |
| 76 | struct pjmedia_frame *output); |
| 77 | static pj_status_t ipp_codec_decode( pjmedia_codec *codec, |
| 78 | const struct pjmedia_frame *input, |
| 79 | unsigned output_buf_len, |
| 80 | struct pjmedia_frame *output); |
| 81 | static pj_status_t ipp_codec_recover(pjmedia_codec *codec, |
| 82 | unsigned output_buf_len, |
| 83 | struct pjmedia_frame *output); |
| 84 | |
| 85 | /* Definition for IPP codecs operations. */ |
| 86 | static pjmedia_codec_op ipp_op = |
| 87 | { |
| 88 | &ipp_codec_init, |
| 89 | &ipp_codec_open, |
| 90 | &ipp_codec_close, |
| 91 | &ipp_codec_modify, |
| 92 | &ipp_codec_parse, |
| 93 | &ipp_codec_encode, |
| 94 | &ipp_codec_decode, |
| 95 | &ipp_codec_recover |
| 96 | }; |
| 97 | |
| 98 | /* Definition for IPP codecs factory operations. */ |
| 99 | static pjmedia_codec_factory_op ipp_factory_op = |
| 100 | { |
| 101 | &ipp_test_alloc, |
| 102 | &ipp_default_attr, |
| 103 | &ipp_enum_codecs, |
| 104 | &ipp_alloc_codec, |
| 105 | &ipp_dealloc_codec |
| 106 | }; |
| 107 | |
| 108 | /* IPP codecs factory */ |
| 109 | static struct ipp_factory { |
| 110 | pjmedia_codec_factory base; |
| 111 | pjmedia_endpt *endpt; |
| 112 | pj_pool_t *pool; |
| 113 | pj_mutex_t *mutex; |
| 114 | } ipp_factory; |
| 115 | |
| 116 | /* IPP codecs private data. */ |
| 117 | typedef struct ipp_private { |
| 118 | int codec_idx; /**< Codec index. */ |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 119 | void *codec_setting; /**< Specific codec setting. */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 120 | pj_pool_t *pool; /**< Pool for each instance. */ |
| 121 | |
| 122 | USC_Handle enc; /**< Encoder state. */ |
| 123 | USC_Handle dec; /**< Decoder state. */ |
| 124 | USC_CodecInfo *info; /**< Native codec info. */ |
| 125 | pj_uint16_t frame_size; /**< Bitstream frame size. */ |
| 126 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 127 | pj_bool_t plc_enabled; /**< PLC enabled flag. */ |
| 128 | pjmedia_plc *plc; /**< PJMEDIA PLC engine, NULL if |
| 129 | codec has internal PLC. */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 130 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 131 | pj_bool_t vad_enabled; /**< VAD enabled flag. */ |
| 132 | pjmedia_silence_det *vad; /**< PJMEDIA VAD engine, NULL if |
| 133 | codec has internal VAD. */ |
| 134 | pj_timestamp last_tx; /**< Timestamp of last transmit.*/ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 135 | } ipp_private_t; |
| 136 | |
| 137 | |
| 138 | /* USC codec implementations. */ |
| 139 | extern USC_Fxns USC_G729AFP_Fxns; |
Nanang Izzuddin | 762a5bd | 2008-09-16 14:11:09 +0000 | [diff] [blame] | 140 | extern USC_Fxns USC_G729I_Fxns; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 141 | extern USC_Fxns USC_G723_Fxns; |
| 142 | extern USC_Fxns USC_G726_Fxns; |
| 143 | extern USC_Fxns USC_G728_Fxns; |
| 144 | extern USC_Fxns USC_G722_Fxns; |
| 145 | extern USC_Fxns USC_GSMAMR_Fxns; |
| 146 | extern USC_Fxns USC_AMRWB_Fxns; |
| 147 | extern USC_Fxns USC_AMRWBE_Fxns; |
| 148 | |
Nanang Izzuddin | 762a5bd | 2008-09-16 14:11:09 +0000 | [diff] [blame] | 149 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 150 | /* CUSTOM CALLBACKS */ |
| 151 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 152 | /* This callback is useful for translating RTP frame into USC frame, e.g: |
| 153 | * reassigning frame attributes, reorder bitstream. Default behaviour of |
| 154 | * the translation is just setting the USC frame buffer & its size as |
| 155 | * specified in RTP frame, setting USC frame frametype to 0, setting bitrate |
| 156 | * of USC frame to bitrate info of codec_data. Implement this callback when |
| 157 | * the default behaviour is unapplicable. |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 158 | */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 159 | typedef void (*predecode_cb)(ipp_private_t *codec_data, |
| 160 | const pjmedia_frame *rtp_frame, |
| 161 | USC_Bitstream *usc_frame); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 162 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 163 | /* Parse frames from a packet. Default behaviour of frame parsing is |
| 164 | * just separating frames based on calculating frame length derived |
| 165 | * from bitrate. Implement this callback when the default behaviour is |
| 166 | * unapplicable. |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 167 | */ |
| 168 | typedef pj_status_t (*parse_cb)(ipp_private_t *codec_data, void *pkt, |
| 169 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 170 | unsigned *frame_cnt, pjmedia_frame frames[]); |
| 171 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 172 | /* Pack frames into a packet. Default behaviour of packing frames is |
| 173 | * just stacking the frames with octet aligned without adding any |
| 174 | * payload header. Implement this callback when the default behaviour is |
| 175 | * unapplicable. |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 176 | */ |
| 177 | typedef pj_status_t (*pack_cb)(ipp_private_t *codec_data, void *pkt, |
| 178 | pj_size_t *pkt_size, pj_size_t max_pkt_size); |
| 179 | |
| 180 | |
| 181 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 182 | /* Custom callback implementations. */ |
| 183 | static void predecode_g723( ipp_private_t *codec_data, |
| 184 | const pjmedia_frame *rtp_frame, |
| 185 | USC_Bitstream *usc_frame); |
| 186 | static pj_status_t parse_g723( ipp_private_t *codec_data, void *pkt, |
| 187 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 188 | unsigned *frame_cnt, pjmedia_frame frames[]); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 189 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 190 | static void predecode_g729( ipp_private_t *codec_data, |
| 191 | const pjmedia_frame *rtp_frame, |
| 192 | USC_Bitstream *usc_frame); |
| 193 | |
| 194 | static void predecode_amr( ipp_private_t *codec_data, |
| 195 | const pjmedia_frame *rtp_frame, |
| 196 | USC_Bitstream *usc_frame); |
| 197 | static pj_status_t parse_amr( ipp_private_t *codec_data, void *pkt, |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 198 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 199 | unsigned *frame_cnt, pjmedia_frame frames[]); |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 200 | static pj_status_t pack_amr( ipp_private_t *codec_data, void *pkt, |
| 201 | pj_size_t *pkt_size, pj_size_t max_pkt_size); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 202 | |
Nanang Izzuddin | 1b9f46b | 2009-04-06 13:52:01 +0000 | [diff] [blame] | 203 | static void predecode_g7221( ipp_private_t *codec_data, |
| 204 | const pjmedia_frame *rtp_frame, |
| 205 | USC_Bitstream *usc_frame); |
| 206 | static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt, |
| 207 | pj_size_t *pkt_size, pj_size_t max_pkt_size); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 208 | |
| 209 | /* IPP codec implementation descriptions. */ |
| 210 | static struct ipp_codec { |
| 211 | int enabled; /* Is this codec enabled? */ |
| 212 | const char *name; /* Codec name. */ |
| 213 | pj_uint8_t pt; /* Payload type. */ |
| 214 | USC_Fxns *fxns; /* USC callback functions. */ |
| 215 | unsigned clock_rate; /* Codec's clock rate. */ |
| 216 | unsigned channel_count; /* Codec's channel count. */ |
| 217 | unsigned samples_per_frame; /* Codec's samples count. */ |
| 218 | |
| 219 | unsigned def_bitrate; /* Default bitrate of this codec. */ |
| 220 | unsigned max_bitrate; /* Maximum bitrate of this codec. */ |
| 221 | pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/ |
| 222 | int has_native_vad; /* Codec has internal VAD? */ |
| 223 | int has_native_plc; /* Codec has internal PLC? */ |
| 224 | |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 225 | predecode_cb predecode; /* Callback to translate RTP frame |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 226 | into USC frame. */ |
| 227 | parse_cb parse; /* Callback to parse bitstream. */ |
| 228 | pack_cb pack; /* Callback to pack bitstream. */ |
| 229 | |
| 230 | pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */ |
| 231 | } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 232 | |
| 233 | ipp_codec[] = |
| 234 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 235 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 236 | /* AMR-NB SID seems to produce noise, so let's just disable its VAD. */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 237 | {1, "AMR", PJMEDIA_RTP_PT_AMR, &USC_GSMAMR_Fxns, 8000, 1, 160, |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 238 | 7400, 12200, 2, 0, 1, |
| 239 | &predecode_amr, &parse_amr, &pack_amr, |
| 240 | {1, {{{"octet-align", 11}, {"1", 1}}} } |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 241 | }, |
| 242 | # endif |
| 243 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 244 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB |
| 245 | {1, "AMR-WB", PJMEDIA_RTP_PT_AMRWB, &USC_AMRWB_Fxns, 16000, 1, 320, |
| 246 | 15850, 23850, 1, 1, 1, |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 247 | &predecode_amr, &parse_amr, &pack_amr, |
| 248 | {1, {{{"octet-align", 11}, {"1", 1}}} } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 249 | }, |
| 250 | # endif |
| 251 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 252 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_G729 |
Nanang Izzuddin | 762a5bd | 2008-09-16 14:11:09 +0000 | [diff] [blame] | 253 | # if defined(PJ_HAS_FLOATING_POINT) && (PJ_HAS_FLOATING_POINT != 0) |
| 254 | {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729AFP_Fxns, 8000, 1, 80, |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 255 | 8000, 11800, 2, 1, 1, |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 256 | &predecode_g729, NULL, NULL |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 257 | }, |
Nanang Izzuddin | 762a5bd | 2008-09-16 14:11:09 +0000 | [diff] [blame] | 258 | # else |
| 259 | {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729I_Fxns, 8000, 1, 80, |
| 260 | 8000, 11800, 2, 1, 1, |
| 261 | &predecode_g729, NULL, NULL |
| 262 | }, |
| 263 | # endif |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 264 | # endif |
| 265 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 266 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1 |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 267 | /* This is actually G.723.1 */ |
| 268 | {1, "G723", PJMEDIA_RTP_PT_G723, &USC_G723_Fxns, 8000, 1, 240, |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 269 | 6300, 6300, 1, 1, 1, |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 270 | &predecode_g723, &parse_g723, NULL |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 271 | }, |
| 272 | # endif |
| 273 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 274 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_G726 |
Nanang Izzuddin | f216f82 | 2008-08-15 18:35:50 +0000 | [diff] [blame] | 275 | {0, "G726-16", PJMEDIA_RTP_PT_G726_16, &USC_G726_Fxns, 8000, 1, 80, |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 276 | 16000, 16000, 2, 0, 0, |
| 277 | NULL, NULL, NULL |
| 278 | }, |
Nanang Izzuddin | f216f82 | 2008-08-15 18:35:50 +0000 | [diff] [blame] | 279 | {0, "G726-24", PJMEDIA_RTP_PT_G726_24, &USC_G726_Fxns, 8000, 1, 80, |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 280 | 24000, 24000, 2, 0, 0, |
| 281 | NULL, NULL, NULL |
| 282 | }, |
| 283 | {1, "G726-32", PJMEDIA_RTP_PT_G726_32, &USC_G726_Fxns, 8000, 1, 80, |
| 284 | 32000, 32000, 2, 0, 0, |
| 285 | NULL, NULL, NULL |
| 286 | }, |
Nanang Izzuddin | f216f82 | 2008-08-15 18:35:50 +0000 | [diff] [blame] | 287 | {0, "G726-40", PJMEDIA_RTP_PT_G726_40, &USC_G726_Fxns, 8000, 1, 80, |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 288 | 40000, 40000, 2, 0, 0, |
| 289 | NULL, NULL, NULL |
| 290 | }, |
| 291 | # endif |
| 292 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 293 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_G728 |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 294 | {1, "G728", PJMEDIA_RTP_PT_G728, &USC_G728_Fxns, 8000, 1, 80, |
| 295 | 16000, 16000, 2, 0, 1, |
| 296 | NULL, NULL, NULL |
| 297 | }, |
| 298 | # endif |
| 299 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 300 | # if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 301 | {0, "G7221", PJMEDIA_RTP_PT_G722_1_16, &USC_G722_Fxns, 16000, 1, 320, |
| 302 | 16000, 16000, 1, 0, 1, |
Nanang Izzuddin | 1b9f46b | 2009-04-06 13:52:01 +0000 | [diff] [blame] | 303 | predecode_g7221, NULL, pack_g7221, |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 304 | {1, {{{"bitrate", 7}, {"16000", 5}}} } |
| 305 | }, |
| 306 | {1, "G7221", PJMEDIA_RTP_PT_G722_1_24, &USC_G722_Fxns, 16000, 1, 320, |
| 307 | 24000, 24000, 1, 0, 1, |
Nanang Izzuddin | 1b9f46b | 2009-04-06 13:52:01 +0000 | [diff] [blame] | 308 | predecode_g7221, NULL, pack_g7221, |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 309 | {1, {{{"bitrate", 7}, {"24000", 5}}} } |
| 310 | }, |
| 311 | {1, "G7221", PJMEDIA_RTP_PT_G722_1_32, &USC_G722_Fxns, 16000, 1, 320, |
| 312 | 32000, 32000, 1, 0, 1, |
Nanang Izzuddin | 1b9f46b | 2009-04-06 13:52:01 +0000 | [diff] [blame] | 313 | predecode_g7221, NULL, pack_g7221, |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 314 | {1, {{{"bitrate", 7}, {"32000", 5}}} } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 315 | }, |
| 316 | # endif |
| 317 | }; |
| 318 | |
| 319 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 320 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729 |
| 321 | |
| 322 | static void predecode_g729( ipp_private_t *codec_data, |
| 323 | const pjmedia_frame *rtp_frame, |
| 324 | USC_Bitstream *usc_frame) |
| 325 | { |
| 326 | switch (rtp_frame->size) { |
| 327 | case 2: |
| 328 | /* SID */ |
| 329 | usc_frame->frametype = 1; |
| 330 | usc_frame->bitrate = codec_data->info->params.modes.bitrate; |
| 331 | break; |
| 332 | case 8: |
| 333 | /* G729D */ |
| 334 | usc_frame->frametype = 2; |
| 335 | usc_frame->bitrate = 6400; |
| 336 | break; |
| 337 | case 10: |
| 338 | /* G729 */ |
| 339 | usc_frame->frametype = 3; |
| 340 | usc_frame->bitrate = 8000; |
| 341 | break; |
| 342 | case 15: |
| 343 | /* G729E */ |
| 344 | usc_frame->frametype = 4; |
| 345 | usc_frame->bitrate = 11800; |
| 346 | break; |
| 347 | default: |
| 348 | usc_frame->frametype = 0; |
| 349 | usc_frame->bitrate = 0; |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | usc_frame->pBuffer = rtp_frame->buf; |
| 354 | usc_frame->nbytes = rtp_frame->size; |
| 355 | } |
| 356 | |
| 357 | #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G729 */ |
| 358 | |
| 359 | |
| 360 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1 |
| 361 | |
| 362 | static void predecode_g723( ipp_private_t *codec_data, |
| 363 | const pjmedia_frame *rtp_frame, |
| 364 | USC_Bitstream *usc_frame) |
| 365 | { |
| 366 | int i, HDR = 0; |
| 367 | pj_uint8_t *f = (pj_uint8_t*)rtp_frame->buf; |
| 368 | |
| 369 | PJ_UNUSED_ARG(codec_data); |
| 370 | |
| 371 | for (i = 0; i < 2; ++i){ |
| 372 | int tmp; |
| 373 | tmp = (f[0] >> (i & 0x7)) & 1; |
| 374 | HDR += tmp << i ; |
| 375 | } |
| 376 | |
| 377 | usc_frame->pBuffer = rtp_frame->buf; |
| 378 | usc_frame->nbytes = rtp_frame->size; |
| 379 | usc_frame->bitrate = HDR == 0? 6300 : 5300; |
| 380 | usc_frame->frametype = 0; |
| 381 | } |
| 382 | |
| 383 | static pj_status_t parse_g723(ipp_private_t *codec_data, void *pkt, |
| 384 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 385 | unsigned *frame_cnt, pjmedia_frame frames[]) |
| 386 | { |
| 387 | unsigned count = 0; |
| 388 | pj_uint8_t *f = (pj_uint8_t*)pkt; |
| 389 | |
| 390 | while (pkt_size && count < *frame_cnt) { |
| 391 | int framesize, i, j; |
| 392 | int HDR = 0; |
| 393 | |
| 394 | for (i = 0; i < 2; ++i){ |
| 395 | j = (f[0] >> (i & 0x7)) & 1; |
| 396 | HDR += j << i ; |
| 397 | } |
| 398 | |
| 399 | if (HDR == 0) |
| 400 | framesize = 24; |
| 401 | else if (HDR == 1) |
| 402 | framesize = 20; |
| 403 | else if (HDR == 2) |
| 404 | framesize = 4; |
| 405 | else if (HDR == 3) |
| 406 | framesize = 1; |
| 407 | else { |
| 408 | pj_assert(!"Unknown G723.1 frametype, packet may be corrupted!"); |
| 409 | return PJMEDIA_CODEC_EINMODE; |
| 410 | } |
| 411 | |
| 412 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 413 | frames[count].buf = f; |
| 414 | frames[count].size = framesize; |
| 415 | frames[count].timestamp.u64 = ts->u64 + count * |
| 416 | ipp_codec[codec_data->codec_idx].samples_per_frame; |
| 417 | |
| 418 | f += framesize; |
| 419 | pkt_size -= framesize; |
| 420 | |
| 421 | ++count; |
| 422 | } |
| 423 | |
| 424 | *frame_cnt = count; |
| 425 | return PJ_SUCCESS; |
| 426 | } |
| 427 | |
| 428 | #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1 */ |
| 429 | |
| 430 | |
| 431 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR |
| 432 | |
| 433 | #include <pjmedia-codec/amr_helper.h> |
| 434 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 435 | typedef struct amr_settings_t { |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 436 | pjmedia_codec_amr_pack_setting enc_setting; |
| 437 | pjmedia_codec_amr_pack_setting dec_setting; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 438 | pj_int8_t enc_mode; |
| 439 | } amr_settings_t; |
| 440 | |
| 441 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 442 | /* Rearrange AMR bitstream and convert RTP frame into USC frame: |
| 443 | * - make the start_bit to be 0 |
| 444 | * - if it is speech frame, reorder bitstream from sensitivity bits order |
| 445 | * to encoder bits order. |
| 446 | * - set the appropriate value of usc_frame. |
| 447 | */ |
| 448 | static void predecode_amr( ipp_private_t *codec_data, |
| 449 | const pjmedia_frame *rtp_frame, |
| 450 | USC_Bitstream *usc_frame) |
| 451 | { |
| 452 | pjmedia_frame frame; |
| 453 | pjmedia_codec_amr_bit_info *info; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 454 | pjmedia_codec_amr_pack_setting *setting; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 455 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 456 | setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting; |
| 457 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 458 | frame = *rtp_frame; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 459 | pjmedia_codec_amr_predecode(rtp_frame, setting, &frame); |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 460 | info = (pjmedia_codec_amr_bit_info*) &frame.bit_info; |
| 461 | |
| 462 | usc_frame->pBuffer = frame.buf; |
| 463 | usc_frame->nbytes = frame.size; |
| 464 | if (info->mode != -1) { |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 465 | usc_frame->bitrate = setting->amr_nb? |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 466 | pjmedia_codec_amrnb_bitrates[info->mode]: |
| 467 | pjmedia_codec_amrwb_bitrates[info->mode]; |
| 468 | } else { |
| 469 | usc_frame->bitrate = 0; |
| 470 | } |
| 471 | |
| 472 | if (frame.size > 5) { |
| 473 | /* Speech */ |
| 474 | if (info->good_quality) |
| 475 | usc_frame->frametype = 0; |
| 476 | else |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 477 | usc_frame->frametype = setting->amr_nb ? 5 : 6; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 478 | } else if (frame.size == 5) { |
| 479 | /* SID */ |
| 480 | if (info->good_quality) { |
| 481 | pj_bool_t STI; |
| 482 | STI = (((pj_uint8_t*)frame.buf)[35 >> 3] & 0x10) != 0; |
| 483 | usc_frame->frametype = STI? 2 : 1; |
| 484 | } else { |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 485 | usc_frame->frametype = setting->amr_nb ? 6 : 7; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 486 | } |
| 487 | } else { |
| 488 | /* no data */ |
| 489 | usc_frame->frametype = 3; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* Pack AMR payload */ |
| 494 | static pj_status_t pack_amr(ipp_private_t *codec_data, void *pkt, |
| 495 | pj_size_t *pkt_size, pj_size_t max_pkt_size) |
| 496 | { |
| 497 | enum {MAX_FRAMES_PER_PACKET = 16}; |
| 498 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 499 | pjmedia_frame frames[MAX_FRAMES_PER_PACKET]; |
| 500 | unsigned nframes = 0; |
| 501 | pjmedia_codec_amr_bit_info *info; |
| 502 | pj_uint8_t *r; /* Read cursor */ |
| 503 | pj_uint8_t SID_FT; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 504 | pjmedia_codec_amr_pack_setting *setting; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 505 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 506 | setting = &((amr_settings_t*)codec_data->codec_setting)->enc_setting; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 507 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 508 | SID_FT = (pj_uint8_t)(setting->amr_nb? 8 : 9); |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 509 | |
| 510 | /* Align pkt buf right */ |
| 511 | r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size; |
| 512 | pj_memmove(r, pkt, *pkt_size); |
| 513 | |
| 514 | /* Get frames */ |
| 515 | for (;;) { |
| 516 | pj_bool_t eof; |
| 517 | pj_uint16_t info_; |
| 518 | |
| 519 | info_ = *((pj_uint16_t*)r); |
| 520 | eof = ((info_ & 0x40) != 0); |
| 521 | |
| 522 | info = (pjmedia_codec_amr_bit_info*) &frames[nframes].bit_info; |
| 523 | pj_bzero(info, sizeof(*info)); |
| 524 | info->frame_type = (pj_uint8_t)(info_ & 0x0F); |
| 525 | info->good_quality = (pj_uint8_t)((info_ & 0x80) == 0); |
| 526 | info->mode = (pj_int8_t) ((info_ >> 8) & 0x0F); |
| 527 | |
| 528 | frames[nframes].buf = r + 2; |
| 529 | frames[nframes].size = info->frame_type <= SID_FT ? |
| 530 | pjmedia_codec_amrnb_framelen[info->frame_type] : |
| 531 | 0; |
| 532 | |
| 533 | r += frames[nframes].size + 2; |
| 534 | |
| 535 | /* Last frame */ |
| 536 | if (++nframes >= MAX_FRAMES_PER_PACKET || eof) |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | /* Pack */ |
| 541 | *pkt_size = max_pkt_size; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 542 | return pjmedia_codec_amr_pack(frames, nframes, setting, pkt, pkt_size); |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
| 546 | /* Parse AMR payload into frames. */ |
| 547 | static pj_status_t parse_amr(ipp_private_t *codec_data, void *pkt, |
| 548 | pj_size_t pkt_size, const pj_timestamp *ts, |
| 549 | unsigned *frame_cnt, pjmedia_frame frames[]) |
| 550 | { |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 551 | amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 552 | pjmedia_codec_amr_pack_setting *setting; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 553 | pj_status_t status; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 554 | pj_uint8_t cmr; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 555 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 556 | setting = &s->dec_setting; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 557 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 558 | status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames, |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 559 | frame_cnt, &cmr); |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 560 | if (status != PJ_SUCCESS) |
| 561 | return status; |
| 562 | |
| 563 | /* Check Change Mode Request. */ |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 564 | if ((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) { |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 565 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
| 566 | |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 567 | s->enc_mode = cmr; |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 568 | codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb? |
| 569 | pjmedia_codec_amrnb_bitrates[s->enc_mode] : |
| 570 | pjmedia_codec_amrwb_bitrates[s->enc_mode]; |
| 571 | ippc->fxns->std.Control(&codec_data->info->params.modes, |
| 572 | codec_data->enc); |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return PJ_SUCCESS; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_AMR */ |
| 579 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 580 | |
Nanang Izzuddin | 1b9f46b | 2009-04-06 13:52:01 +0000 | [diff] [blame] | 581 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 |
| 582 | |
| 583 | static void predecode_g7221( ipp_private_t *codec_data, |
| 584 | const pjmedia_frame *rtp_frame, |
| 585 | USC_Bitstream *usc_frame) |
| 586 | { |
| 587 | usc_frame->pBuffer = (char*)rtp_frame->buf; |
| 588 | usc_frame->nbytes = rtp_frame->size; |
| 589 | usc_frame->frametype = 0; |
| 590 | usc_frame->bitrate = codec_data->info->params.modes.bitrate; |
| 591 | |
| 592 | #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0 |
| 593 | { |
| 594 | pj_uint16_t *p, *p_end; |
| 595 | |
| 596 | p = (pj_uint16_t*)rtp_frame->buf; |
| 597 | p_end = p + rtp_frame->size/2; |
| 598 | while (p < p_end) { |
| 599 | *p = pj_ntohs(*p); |
| 600 | ++p; |
| 601 | } |
| 602 | } |
| 603 | #endif |
| 604 | } |
| 605 | |
| 606 | static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt, |
| 607 | pj_size_t *pkt_size, pj_size_t max_pkt_size) |
| 608 | { |
| 609 | PJ_UNUSED_ARG(codec_data); |
| 610 | PJ_UNUSED_ARG(max_pkt_size); |
| 611 | |
| 612 | #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0 |
| 613 | { |
| 614 | pj_uint16_t *p, *p_end; |
| 615 | |
| 616 | p = (pj_uint16_t*)pkt; |
| 617 | p_end = p + *pkt_size/2; |
| 618 | while (p < p_end) { |
| 619 | *p = pj_htons(*p); |
| 620 | ++p; |
| 621 | } |
| 622 | } |
| 623 | #else |
| 624 | PJ_UNUSED_ARG(pkt); |
| 625 | PJ_UNUSED_ARG(pkt_size); |
| 626 | #endif |
| 627 | |
| 628 | return PJ_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 */ |
| 632 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 633 | /* |
| 634 | * Initialize and register IPP codec factory to pjmedia endpoint. |
| 635 | */ |
| 636 | PJ_DEF(pj_status_t) pjmedia_codec_ipp_init( pjmedia_endpt *endpt ) |
| 637 | { |
| 638 | pjmedia_codec_mgr *codec_mgr; |
| 639 | pj_status_t status; |
| 640 | |
| 641 | if (ipp_factory.pool != NULL) { |
| 642 | /* Already initialized. */ |
| 643 | return PJ_SUCCESS; |
| 644 | } |
| 645 | |
| 646 | /* Create IPP codec factory. */ |
| 647 | ipp_factory.base.op = &ipp_factory_op; |
| 648 | ipp_factory.base.factory_data = NULL; |
| 649 | ipp_factory.endpt = endpt; |
| 650 | |
| 651 | ipp_factory.pool = pjmedia_endpt_create_pool(endpt, "IPP codecs", 4000, 4000); |
| 652 | if (!ipp_factory.pool) |
| 653 | return PJ_ENOMEM; |
| 654 | |
| 655 | /* Create mutex. */ |
| 656 | status = pj_mutex_create_simple(ipp_factory.pool, "IPP codecs", |
| 657 | &ipp_factory.mutex); |
| 658 | if (status != PJ_SUCCESS) |
| 659 | goto on_error; |
| 660 | |
| 661 | /* Get the codec manager. */ |
| 662 | codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); |
| 663 | if (!codec_mgr) { |
| 664 | status = PJ_EINVALIDOP; |
| 665 | goto on_error; |
| 666 | } |
| 667 | |
| 668 | /* Register codec factory to endpoint. */ |
| 669 | status = pjmedia_codec_mgr_register_factory(codec_mgr, |
| 670 | &ipp_factory.base); |
| 671 | if (status != PJ_SUCCESS) |
| 672 | goto on_error; |
| 673 | |
| 674 | /* Done. */ |
| 675 | return PJ_SUCCESS; |
| 676 | |
| 677 | on_error: |
| 678 | pj_pool_release(ipp_factory.pool); |
| 679 | ipp_factory.pool = NULL; |
| 680 | return status; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Unregister IPP codecs factory from pjmedia endpoint. |
| 685 | */ |
| 686 | PJ_DEF(pj_status_t) pjmedia_codec_ipp_deinit(void) |
| 687 | { |
| 688 | pjmedia_codec_mgr *codec_mgr; |
| 689 | pj_status_t status; |
| 690 | |
| 691 | if (ipp_factory.pool == NULL) { |
| 692 | /* Already deinitialized */ |
| 693 | return PJ_SUCCESS; |
| 694 | } |
| 695 | |
| 696 | pj_mutex_lock(ipp_factory.mutex); |
| 697 | |
| 698 | /* Get the codec manager. */ |
| 699 | codec_mgr = pjmedia_endpt_get_codec_mgr(ipp_factory.endpt); |
| 700 | if (!codec_mgr) { |
| 701 | pj_pool_release(ipp_factory.pool); |
| 702 | ipp_factory.pool = NULL; |
| 703 | return PJ_EINVALIDOP; |
| 704 | } |
| 705 | |
| 706 | /* Unregister IPP codecs factory. */ |
| 707 | status = pjmedia_codec_mgr_unregister_factory(codec_mgr, |
| 708 | &ipp_factory.base); |
| 709 | |
| 710 | /* Destroy mutex. */ |
| 711 | pj_mutex_destroy(ipp_factory.mutex); |
| 712 | |
| 713 | /* Destroy pool. */ |
| 714 | pj_pool_release(ipp_factory.pool); |
| 715 | ipp_factory.pool = NULL; |
| 716 | |
| 717 | return status; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Check if factory can allocate the specified codec. |
| 722 | */ |
| 723 | static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory, |
| 724 | const pjmedia_codec_info *info ) |
| 725 | { |
| 726 | unsigned i; |
| 727 | |
| 728 | PJ_UNUSED_ARG(factory); |
| 729 | |
| 730 | /* Type MUST be audio. */ |
| 731 | if (info->type != PJMEDIA_TYPE_AUDIO) |
| 732 | return PJMEDIA_CODEC_EUNSUP; |
| 733 | |
| 734 | for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) { |
| 735 | pj_str_t name = pj_str((char*)ipp_codec[i].name); |
| 736 | if ((pj_stricmp(&info->encoding_name, &name) == 0) && |
| 737 | (info->clock_rate == (unsigned)ipp_codec[i].clock_rate) && |
| 738 | (info->channel_cnt == (unsigned)ipp_codec[i].channel_count) && |
| 739 | (ipp_codec[i].enabled)) |
| 740 | { |
| 741 | return PJ_SUCCESS; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /* Unsupported, or mode is disabled. */ |
| 746 | return PJMEDIA_CODEC_EUNSUP; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Generate default attribute. |
| 751 | */ |
| 752 | static pj_status_t ipp_default_attr (pjmedia_codec_factory *factory, |
| 753 | const pjmedia_codec_info *id, |
| 754 | pjmedia_codec_param *attr ) |
| 755 | { |
| 756 | unsigned i; |
| 757 | |
| 758 | PJ_ASSERT_RETURN(factory==&ipp_factory.base, PJ_EINVAL); |
| 759 | |
| 760 | pj_bzero(attr, sizeof(pjmedia_codec_param)); |
| 761 | |
| 762 | for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) { |
| 763 | pj_str_t name = pj_str((char*)ipp_codec[i].name); |
| 764 | if ((pj_stricmp(&id->encoding_name, &name) == 0) && |
| 765 | (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) && |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 766 | (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) && |
| 767 | (id->pt == (unsigned)ipp_codec[i].pt)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 768 | { |
| 769 | attr->info.pt = (pj_uint8_t)id->pt; |
| 770 | attr->info.channel_cnt = ipp_codec[i].channel_count; |
| 771 | attr->info.clock_rate = ipp_codec[i].clock_rate; |
| 772 | attr->info.avg_bps = ipp_codec[i].def_bitrate; |
| 773 | attr->info.max_bps = ipp_codec[i].max_bitrate; |
| 774 | attr->info.pcm_bits_per_sample = 16; |
| 775 | attr->info.frm_ptime = (pj_uint16_t) |
| 776 | (ipp_codec[i].samples_per_frame * 1000 / |
| 777 | ipp_codec[i].channel_count / |
| 778 | ipp_codec[i].clock_rate); |
| 779 | attr->setting.frm_per_pkt = ipp_codec[i].frm_per_pkt; |
| 780 | |
| 781 | /* Default flags. */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 782 | attr->setting.plc = 1; |
| 783 | attr->setting.penh= 0; |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 784 | attr->setting.vad = 1; |
| 785 | attr->setting.cng = attr->setting.vad; |
| 786 | attr->setting.dec_fmtp = ipp_codec[i].dec_fmtp; |
| 787 | |
| 788 | if (attr->setting.vad == 0) { |
| 789 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729 |
| 790 | if (id->pt == PJMEDIA_RTP_PT_G729) { |
| 791 | /* Signal G729 Annex B is being disabled */ |
| 792 | attr->setting.dec_fmtp.cnt = 1; |
| 793 | pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb"); |
| 794 | pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no"); |
| 795 | } |
| 796 | #endif |
| 797 | } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 798 | |
| 799 | return PJ_SUCCESS; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | return PJMEDIA_CODEC_EUNSUP; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Enum codecs supported by this factory. |
| 808 | */ |
| 809 | static pj_status_t ipp_enum_codecs(pjmedia_codec_factory *factory, |
| 810 | unsigned *count, |
| 811 | pjmedia_codec_info codecs[]) |
| 812 | { |
| 813 | unsigned max; |
| 814 | unsigned i; |
| 815 | |
| 816 | PJ_UNUSED_ARG(factory); |
| 817 | PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); |
| 818 | |
| 819 | max = *count; |
| 820 | |
| 821 | for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(ipp_codec) && *count < max; ++i) |
| 822 | { |
| 823 | if (!ipp_codec[i].enabled) |
| 824 | continue; |
| 825 | |
| 826 | pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); |
| 827 | codecs[*count].encoding_name = pj_str((char*)ipp_codec[i].name); |
| 828 | codecs[*count].pt = ipp_codec[i].pt; |
| 829 | codecs[*count].type = PJMEDIA_TYPE_AUDIO; |
| 830 | codecs[*count].clock_rate = ipp_codec[i].clock_rate; |
| 831 | codecs[*count].channel_cnt = ipp_codec[i].channel_count; |
| 832 | |
| 833 | ++*count; |
| 834 | } |
| 835 | |
| 836 | return PJ_SUCCESS; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Allocate a new codec instance. |
| 841 | */ |
| 842 | static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory, |
| 843 | const pjmedia_codec_info *id, |
| 844 | pjmedia_codec **p_codec) |
| 845 | { |
| 846 | ipp_private_t *codec_data; |
| 847 | pjmedia_codec *codec; |
| 848 | int idx; |
| 849 | pj_pool_t *pool; |
| 850 | unsigned i; |
| 851 | |
| 852 | PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); |
| 853 | PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL); |
| 854 | |
| 855 | pj_mutex_lock(ipp_factory.mutex); |
| 856 | |
| 857 | /* Find codec's index */ |
| 858 | idx = -1; |
| 859 | for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) { |
| 860 | pj_str_t name = pj_str((char*)ipp_codec[i].name); |
| 861 | if ((pj_stricmp(&id->encoding_name, &name) == 0) && |
| 862 | (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) && |
| 863 | (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) && |
| 864 | (ipp_codec[i].enabled)) |
| 865 | { |
| 866 | idx = i; |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | if (idx == -1) { |
| 871 | *p_codec = NULL; |
| 872 | return PJMEDIA_CODEC_EFAILED; |
| 873 | } |
| 874 | |
| 875 | /* Create pool for codec instance */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 876 | pool = pjmedia_endpt_create_pool(ipp_factory.endpt, "IPPcodec", 512, 512); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 877 | codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec); |
| 878 | PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM); |
| 879 | codec->op = &ipp_op; |
| 880 | codec->factory = factory; |
| 881 | codec->codec_data = PJ_POOL_ZALLOC_T(pool, ipp_private_t); |
| 882 | codec_data = (ipp_private_t*) codec->codec_data; |
| 883 | |
| 884 | /* Create PLC if codec has no internal PLC */ |
| 885 | if (!ipp_codec[idx].has_native_plc) { |
| 886 | pj_status_t status; |
| 887 | status = pjmedia_plc_create(pool, ipp_codec[idx].clock_rate, |
| 888 | ipp_codec[idx].samples_per_frame, 0, |
| 889 | &codec_data->plc); |
| 890 | if (status != PJ_SUCCESS) { |
| 891 | pj_pool_release(pool); |
| 892 | pj_mutex_unlock(ipp_factory.mutex); |
| 893 | return status; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* Create silence detector if codec has no internal VAD */ |
| 898 | if (!ipp_codec[idx].has_native_vad) { |
| 899 | pj_status_t status; |
| 900 | status = pjmedia_silence_det_create(pool, |
| 901 | ipp_codec[idx].clock_rate, |
| 902 | ipp_codec[idx].samples_per_frame, |
| 903 | &codec_data->vad); |
| 904 | if (status != PJ_SUCCESS) { |
| 905 | pj_pool_release(pool); |
| 906 | pj_mutex_unlock(ipp_factory.mutex); |
| 907 | return status; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | codec_data->pool = pool; |
| 912 | codec_data->codec_idx = idx; |
| 913 | |
| 914 | pj_mutex_unlock(ipp_factory.mutex); |
| 915 | |
| 916 | *p_codec = codec; |
| 917 | return PJ_SUCCESS; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Free codec. |
| 922 | */ |
| 923 | static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory, |
| 924 | pjmedia_codec *codec ) |
| 925 | { |
| 926 | ipp_private_t *codec_data; |
| 927 | |
| 928 | PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); |
| 929 | PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL); |
| 930 | |
| 931 | /* Close codec, if it's not closed. */ |
| 932 | codec_data = (ipp_private_t*) codec->codec_data; |
| 933 | if (codec_data->enc != NULL || codec_data->dec != NULL) { |
| 934 | ipp_codec_close(codec); |
| 935 | } |
| 936 | |
| 937 | pj_pool_release(codec_data->pool); |
| 938 | |
| 939 | return PJ_SUCCESS; |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Init codec. |
| 944 | */ |
| 945 | static pj_status_t ipp_codec_init( pjmedia_codec *codec, |
| 946 | pj_pool_t *pool ) |
| 947 | { |
| 948 | PJ_UNUSED_ARG(codec); |
| 949 | PJ_UNUSED_ARG(pool); |
| 950 | return PJ_SUCCESS; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Open codec. |
| 955 | */ |
| 956 | static pj_status_t ipp_codec_open( pjmedia_codec *codec, |
| 957 | pjmedia_codec_param *attr ) |
| 958 | { |
| 959 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 960 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 961 | int info_size; |
| 962 | pj_pool_t *pool; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 963 | int i, j; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 964 | USC_MemBank *membanks; |
| 965 | int nb_membanks; |
| 966 | |
| 967 | pool = codec_data->pool; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 968 | |
| 969 | /* Get the codec info size */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 970 | if (USC_NoError != ippc->fxns->std.GetInfoSize(&info_size)) { |
| 971 | PJ_LOG(1,(THIS_FILE, "Error getting codec info size")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 972 | goto on_error; |
| 973 | } |
| 974 | /* Get the codec info */ |
| 975 | codec_data->info = pj_pool_zalloc(pool, info_size); |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 976 | if (USC_NoError != ippc->fxns->std.GetInfo((USC_Handle)NULL, |
| 977 | codec_data->info)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 978 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 979 | PJ_LOG(1,(THIS_FILE, "Error getting codec info")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 980 | goto on_error; |
| 981 | } |
| 982 | |
| 983 | /* PREPARING THE ENCODER */ |
| 984 | |
| 985 | /* Setting the encoder params */ |
| 986 | codec_data->info->params.direction = USC_ENCODE; |
Nanang Izzuddin | f216f82 | 2008-08-15 18:35:50 +0000 | [diff] [blame] | 987 | codec_data->info->params.modes.vad = attr->setting.vad && |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 988 | ippc->has_native_vad; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 989 | codec_data->info->params.modes.bitrate = attr->info.avg_bps; |
| 990 | codec_data->info->params.law = 0; /* Linear PCM input */ |
| 991 | |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 992 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729 |
| 993 | if (ippc->pt == PJMEDIA_RTP_PT_G729) { |
| 994 | /* Check if G729 Annex B is signaled to be disabled */ |
| 995 | for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) { |
| 996 | if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].name, "annexb")==0) |
| 997 | { |
| 998 | if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].val, "no")==0) |
| 999 | codec_data->info->params.modes.vad = 0; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | #endif |
| 1005 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1006 | /* Get number of memory blocks needed by the encoder */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1007 | if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params, |
| 1008 | &nb_membanks)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1009 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1010 | PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of encoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1011 | goto on_error; |
| 1012 | } |
| 1013 | |
| 1014 | /* Allocate memory blocks table */ |
| 1015 | membanks = (USC_MemBank*) pj_pool_zalloc(pool, |
| 1016 | sizeof(USC_MemBank) * nb_membanks); |
| 1017 | /* Get size of each memory block */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1018 | if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params, |
| 1019 | membanks)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1020 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1021 | PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of encoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1022 | goto on_error; |
| 1023 | } |
| 1024 | |
| 1025 | /* Allocate memory for each block */ |
| 1026 | for (i = 0; i < nb_membanks; i++) { |
| 1027 | membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes); |
| 1028 | } |
| 1029 | |
| 1030 | /* Create encoder instance */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1031 | if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params, |
| 1032 | membanks, |
| 1033 | &codec_data->enc)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1034 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1035 | PJ_LOG(1,(THIS_FILE, "Error initializing encoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1036 | goto on_error; |
| 1037 | } |
| 1038 | |
| 1039 | /* PREPARING THE DECODER */ |
| 1040 | |
| 1041 | /* Setting the decoder params */ |
| 1042 | codec_data->info->params.direction = USC_DECODE; |
| 1043 | |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 1044 | /* Not sure if VAD affects decoder, just try to be safe */ |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1045 | //codec_data->info->params.modes.vad = ippc->has_native_vad; |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 1046 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1047 | /* Get number of memory blocks needed by the decoder */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1048 | if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params, |
| 1049 | &nb_membanks)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1050 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1051 | PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of decoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1052 | goto on_error; |
| 1053 | } |
| 1054 | |
| 1055 | /* Allocate memory blocks table */ |
| 1056 | membanks = (USC_MemBank*) pj_pool_zalloc(pool, |
| 1057 | sizeof(USC_MemBank) * nb_membanks); |
| 1058 | /* Get size of each memory block */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1059 | if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params, |
| 1060 | membanks)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1061 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1062 | PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of decoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1063 | goto on_error; |
| 1064 | } |
| 1065 | |
| 1066 | /* Allocate memory for each block */ |
| 1067 | for (i = 0; i < nb_membanks; i++) { |
| 1068 | membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes); |
| 1069 | } |
| 1070 | |
| 1071 | /* Create decoder instance */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1072 | if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params, |
| 1073 | membanks, &codec_data->dec)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1074 | { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1075 | PJ_LOG(1,(THIS_FILE, "Error initializing decoder")); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1076 | goto on_error; |
| 1077 | } |
| 1078 | |
| 1079 | /* Update codec info */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1080 | ippc->fxns->std.GetInfo((USC_Handle)codec_data->enc, codec_data->info); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* Get bitstream size */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1083 | i = codec_data->info->params.modes.bitrate * ippc->samples_per_frame; |
| 1084 | j = ippc->clock_rate << 3; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1085 | codec_data->frame_size = (pj_uint16_t)(i / j); |
| 1086 | if (i % j) ++codec_data->frame_size; |
| 1087 | |
| 1088 | codec_data->vad_enabled = (attr->setting.vad != 0); |
| 1089 | codec_data->plc_enabled = (attr->setting.plc != 0); |
| 1090 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1091 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR |
| 1092 | /* Init AMR settings */ |
| 1093 | if (ippc->pt == PJMEDIA_RTP_PT_AMR || ippc->pt == PJMEDIA_RTP_PT_AMRWB) { |
| 1094 | amr_settings_t *s; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1095 | pj_uint8_t octet_align = 0; |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1096 | pj_int8_t enc_mode = -1; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1097 | |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1098 | /* Check AMR specific attributes */ |
| 1099 | |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1100 | for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) { |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1101 | /* octet-align, one of the parameters that must have same value |
| 1102 | * in offer & answer (RFC 4867 Section 8.3.1). Just check fmtp |
| 1103 | * in the decoder side, since it's value is guaranteed to fulfil |
| 1104 | * above requirement (by SDP negotiator). |
| 1105 | */ |
| 1106 | const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11}; |
| 1107 | |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1108 | if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, |
| 1109 | &STR_FMTP_OCTET_ALIGN) == 0) |
| 1110 | { |
| 1111 | octet_align=(pj_uint8_t) |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1112 | pj_strtoul(&attr->setting.dec_fmtp.param[i].val); |
| 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) { |
| 1117 | /* mode-set */ |
| 1118 | const pj_str_t STR_FMTP_MODE_SET = {"mode-set", 8}; |
| 1119 | |
| 1120 | if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name, |
| 1121 | &STR_FMTP_MODE_SET) == 0) |
| 1122 | { |
| 1123 | pj_int8_t tmp; |
| 1124 | |
| 1125 | /* Just get the first value. */ |
| 1126 | tmp = (pj_int8_t) |
| 1127 | pj_strtoul(&attr->setting.enc_fmtp.param[i].val); |
| 1128 | |
| 1129 | if ((ippc->pt == PJMEDIA_RTP_PT_AMR && tmp > 0 && tmp < 8) || |
| 1130 | (ippc->pt == PJMEDIA_RTP_PT_AMRWB && tmp > 0 && tmp < 9)) |
| 1131 | { |
| 1132 | enc_mode = tmp; |
| 1133 | PJ_LOG(4,(THIS_FILE, "Remote specifies AMR mode-set attr, " |
| 1134 | "selected: %d", enc_mode)); |
| 1135 | } |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | } |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1139 | |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1140 | /* Initialize AMR specific settings */ |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1141 | s = PJ_POOL_ZALLOC_T(pool, amr_settings_t); |
| 1142 | codec_data->codec_setting = s; |
| 1143 | |
Benny Prijono | 6b6fce1 | 2009-03-17 10:13:30 +0000 | [diff] [blame] | 1144 | s->enc_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR); |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1145 | s->enc_setting.octet_aligned = octet_align; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1146 | s->enc_setting.reorder = PJ_TRUE; |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1147 | s->enc_setting.cmr = 15; |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1148 | |
Benny Prijono | 6b6fce1 | 2009-03-17 10:13:30 +0000 | [diff] [blame] | 1149 | s->dec_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR); |
Nanang Izzuddin | 0b8f4ca | 2008-11-11 11:25:13 +0000 | [diff] [blame] | 1150 | s->dec_setting.octet_aligned = octet_align; |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1151 | s->dec_setting.reorder = PJ_TRUE; |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame^] | 1152 | |
| 1153 | s->enc_mode = pjmedia_codec_amr_get_mode( |
| 1154 | codec_data->info->params.modes.bitrate); |
| 1155 | if (s->enc_mode < 0) |
| 1156 | goto on_error; |
| 1157 | |
| 1158 | if (enc_mode != -1) { |
| 1159 | s->enc_mode = enc_mode; |
| 1160 | |
| 1161 | /* Apply requested encoder bitrate */ |
| 1162 | codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb? |
| 1163 | pjmedia_codec_amrnb_bitrates[s->enc_mode] : |
| 1164 | pjmedia_codec_amrwb_bitrates[s->enc_mode]; |
| 1165 | ippc->fxns->std.Control(&codec_data->info->params.modes, |
| 1166 | codec_data->enc); |
| 1167 | } |
| 1168 | |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1169 | } |
| 1170 | #endif |
| 1171 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1172 | return PJ_SUCCESS; |
| 1173 | |
| 1174 | on_error: |
| 1175 | return PJMEDIA_CODEC_EFAILED; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * Close codec. |
| 1180 | */ |
| 1181 | static pj_status_t ipp_codec_close( pjmedia_codec *codec ) |
| 1182 | { |
| 1183 | PJ_UNUSED_ARG(codec); |
| 1184 | |
| 1185 | return PJ_SUCCESS; |
| 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | /* |
| 1190 | * Modify codec settings. |
| 1191 | */ |
| 1192 | static pj_status_t ipp_codec_modify(pjmedia_codec *codec, |
| 1193 | const pjmedia_codec_param *attr ) |
| 1194 | { |
| 1195 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1196 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1197 | |
| 1198 | codec_data->vad_enabled = (attr->setting.vad != 0); |
| 1199 | codec_data->plc_enabled = (attr->setting.plc != 0); |
| 1200 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1201 | if (ippc->has_native_vad) { |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1202 | USC_Modes modes; |
| 1203 | |
| 1204 | modes = codec_data->info->params.modes; |
| 1205 | modes.vad = codec_data->vad_enabled; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1206 | ippc->fxns->std.Control(&modes, codec_data->enc); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | return PJ_SUCCESS; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Get frames in the packet. |
| 1214 | */ |
| 1215 | static pj_status_t ipp_codec_parse( pjmedia_codec *codec, |
| 1216 | void *pkt, |
| 1217 | pj_size_t pkt_size, |
| 1218 | const pj_timestamp *ts, |
| 1219 | unsigned *frame_cnt, |
| 1220 | pjmedia_frame frames[]) |
| 1221 | { |
| 1222 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1223 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1224 | unsigned count = 0; |
| 1225 | |
| 1226 | PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL); |
| 1227 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1228 | if (ippc->parse != NULL) { |
| 1229 | return ippc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | while (pkt_size >= codec_data->frame_size && count < *frame_cnt) { |
| 1233 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1234 | frames[count].buf = pkt; |
| 1235 | frames[count].size = codec_data->frame_size; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1236 | frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1237 | |
| 1238 | pkt = ((char*)pkt) + codec_data->frame_size; |
| 1239 | pkt_size -= codec_data->frame_size; |
| 1240 | |
| 1241 | ++count; |
| 1242 | } |
| 1243 | |
| 1244 | if (pkt_size && count < *frame_cnt) { |
| 1245 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1246 | frames[count].buf = pkt; |
| 1247 | frames[count].size = pkt_size; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1248 | frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1249 | ++count; |
| 1250 | } |
| 1251 | |
| 1252 | *frame_cnt = count; |
| 1253 | return PJ_SUCCESS; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * Encode frames. |
| 1258 | */ |
| 1259 | static pj_status_t ipp_codec_encode( pjmedia_codec *codec, |
| 1260 | const struct pjmedia_frame *input, |
| 1261 | unsigned output_buf_len, |
| 1262 | struct pjmedia_frame *output) |
| 1263 | { |
| 1264 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1265 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1266 | unsigned samples_per_frame; |
| 1267 | unsigned nsamples; |
| 1268 | pj_size_t tx = 0; |
| 1269 | pj_int16_t *pcm_in = (pj_int16_t*)input->buf; |
Benny Prijono | b133924 | 2008-08-21 20:58:55 +0000 | [diff] [blame] | 1270 | pj_uint8_t *bits_out = (pj_uint8_t*) output->buf; |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1271 | pj_uint8_t pt; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* Invoke external VAD if codec has no internal VAD */ |
| 1274 | if (codec_data->vad && codec_data->vad_enabled) { |
| 1275 | pj_bool_t is_silence; |
| 1276 | pj_int32_t silence_duration; |
| 1277 | |
| 1278 | silence_duration = pj_timestamp_diff32(&codec_data->last_tx, |
| 1279 | &input->timestamp); |
| 1280 | |
| 1281 | is_silence = pjmedia_silence_det_detect(codec_data->vad, |
| 1282 | (const pj_int16_t*) input->buf, |
| 1283 | (input->size >> 1), |
| 1284 | NULL); |
| 1285 | if (is_silence && |
| 1286 | PJMEDIA_CODEC_MAX_SILENCE_PERIOD != -1 && |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1287 | silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD * |
| 1288 | (int)ippc->clock_rate / 1000)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1289 | { |
| 1290 | output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1291 | output->buf = NULL; |
| 1292 | output->size = 0; |
| 1293 | output->timestamp = input->timestamp; |
| 1294 | return PJ_SUCCESS; |
| 1295 | } else { |
| 1296 | codec_data->last_tx = input->timestamp; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | nsamples = input->size >> 1; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1301 | samples_per_frame = ippc->samples_per_frame; |
| 1302 | pt = ippc->pt; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1303 | |
| 1304 | PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0, |
| 1305 | PJMEDIA_CODEC_EPCMFRMINLEN); |
| 1306 | |
| 1307 | /* Encode the frames */ |
| 1308 | while (nsamples >= samples_per_frame) { |
| 1309 | USC_PCMStream in; |
| 1310 | USC_Bitstream out; |
| 1311 | |
| 1312 | in.bitrate = codec_data->info->params.modes.bitrate; |
| 1313 | in.nbytes = samples_per_frame << 1; |
| 1314 | in.pBuffer = (char*)pcm_in; |
| 1315 | in.pcmType.bitPerSample = codec_data->info->params.pcmType.bitPerSample; |
| 1316 | in.pcmType.nChannels = codec_data->info->params.pcmType.nChannels; |
| 1317 | in.pcmType.sample_frequency = codec_data->info->params.pcmType.sample_frequency; |
| 1318 | |
Benny Prijono | b133924 | 2008-08-21 20:58:55 +0000 | [diff] [blame] | 1319 | out.pBuffer = (char*)bits_out; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1320 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1321 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1322 | /* For AMR: reserve two octets for AMR frame info */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1323 | if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) { |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1324 | out.pBuffer += 2; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1325 | } |
| 1326 | #endif |
| 1327 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1328 | if (USC_NoError != ippc->fxns->Encode(codec_data->enc, &in, &out)) { |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1332 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR |
Nanang Izzuddin | 35e01de | 2008-10-29 10:17:02 +0000 | [diff] [blame] | 1333 | /* For AMR: put info (frametype, degraded, last frame, mode) in the |
| 1334 | * first two octets for payload packing. |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1335 | */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1336 | if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) { |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1337 | pj_uint16_t *info = (pj_uint16_t*)bits_out; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1338 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1339 | /* Two octets for AMR frame info, 0=LSB: |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1340 | * bit 0-3 : frame type |
| 1341 | * bit 6 : last frame flag |
| 1342 | * bit 7 : quality flag |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1343 | * bit 8-11 : mode |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1344 | */ |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1345 | out.nbytes += 2; |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1346 | if (out.frametype == 0 || out.frametype == 4 || |
| 1347 | (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 5) || |
| 1348 | (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 6)) |
| 1349 | { |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1350 | /* Speech frame type */ |
| 1351 | *info = (char)pjmedia_codec_amr_get_mode(out.bitrate); |
| 1352 | /* Quality */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1353 | if (out.frametype == 5 || out.frametype == 6) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1354 | *info |= 0x80; |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1355 | } else if (out.frametype == 1 || out.frametype == 2 || |
| 1356 | (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 6) || |
| 1357 | (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 7)) |
| 1358 | { |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1359 | /* SID frame type */ |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1360 | *info = (pj_uint8_t)(pt == PJMEDIA_RTP_PT_AMRWB? 9 : 8); |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1361 | /* Quality */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1362 | if (out.frametype == 6 || out.frametype == 7) |
| 1363 | *info |= 0x80; |
| 1364 | } else { |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1365 | /* Untransmited */ |
| 1366 | *info = 15; |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1367 | out.nbytes = 2; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Nanang Izzuddin | d55553a | 2008-10-28 02:24:46 +0000 | [diff] [blame] | 1370 | /* Mode */ |
| 1371 | *info |= (char)pjmedia_codec_amr_get_mode(out.bitrate) << 8; |
| 1372 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1373 | /* Last frame flag */ |
| 1374 | if (nsamples == samples_per_frame) |
| 1375 | *info |= 0x40; |
| 1376 | } |
Nanang Izzuddin | 7dd3268 | 2008-08-19 11:23:33 +0000 | [diff] [blame] | 1377 | #endif |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1378 | |
| 1379 | pcm_in += samples_per_frame; |
| 1380 | nsamples -= samples_per_frame; |
| 1381 | tx += out.nbytes; |
| 1382 | bits_out += out.nbytes; |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 1383 | |
| 1384 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729 |
Nanang Izzuddin | e3a6fca | 2008-08-27 13:15:25 +0000 | [diff] [blame] | 1385 | if (pt == PJMEDIA_RTP_PT_G729) { |
| 1386 | if (out.frametype == 1) { |
| 1387 | /* SID */ |
| 1388 | break; |
| 1389 | } else if (out.frametype == 0) { |
| 1390 | /* Untransmitted */ |
| 1391 | tx -= out.nbytes; |
| 1392 | break; |
| 1393 | } |
Nanang Izzuddin | 23a00b7 | 2008-08-25 13:58:25 +0000 | [diff] [blame] | 1394 | } |
| 1395 | #endif |
| 1396 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1397 | } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1398 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1399 | if (ippc->pack != NULL) { |
| 1400 | ippc->pack(codec_data, output->buf, &tx, output_buf_len); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /* Check if we don't need to transmit the frame (DTX) */ |
| 1404 | if (tx == 0) { |
| 1405 | output->buf = NULL; |
| 1406 | output->size = 0; |
| 1407 | output->timestamp.u64 = input->timestamp.u64; |
| 1408 | output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1409 | return PJ_SUCCESS; |
| 1410 | } |
| 1411 | |
| 1412 | output->size = tx; |
| 1413 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1414 | output->timestamp = input->timestamp; |
| 1415 | |
| 1416 | return PJ_SUCCESS; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * Decode frame. |
| 1421 | */ |
| 1422 | static pj_status_t ipp_codec_decode( pjmedia_codec *codec, |
| 1423 | const struct pjmedia_frame *input, |
| 1424 | unsigned output_buf_len, |
| 1425 | struct pjmedia_frame *output) |
| 1426 | { |
| 1427 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1428 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1429 | unsigned samples_per_frame; |
| 1430 | USC_PCMStream out; |
| 1431 | USC_Bitstream in; |
| 1432 | pj_uint8_t pt; |
| 1433 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1434 | pt = ippc->pt; |
| 1435 | samples_per_frame = ippc->samples_per_frame; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1436 | |
| 1437 | PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1, |
| 1438 | PJMEDIA_CODEC_EPCMTOOSHORT); |
| 1439 | |
| 1440 | if (input->type == PJMEDIA_FRAME_TYPE_AUDIO) { |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1441 | if (ippc->predecode) { |
| 1442 | ippc->predecode(codec_data, input, &in); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1443 | } else { |
| 1444 | /* Most IPP codecs have frametype==0 for speech frame */ |
Nanang Izzuddin | df361e0 | 2008-08-16 06:46:08 +0000 | [diff] [blame] | 1445 | in.pBuffer = (char*)input->buf; |
| 1446 | in.nbytes = input->size; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1447 | in.frametype = 0; |
| 1448 | in.bitrate = codec_data->info->params.modes.bitrate; |
| 1449 | } |
| 1450 | |
| 1451 | out.pBuffer = output->buf; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1452 | } |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1453 | |
| 1454 | if (input->type != PJMEDIA_FRAME_TYPE_AUDIO || |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1455 | USC_NoError != ippc->fxns->Decode(codec_data->dec, &in, &out)) |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1456 | { |
| 1457 | pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame); |
| 1458 | output->size = samples_per_frame << 1; |
| 1459 | output->timestamp.u64 = input->timestamp.u64; |
| 1460 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1461 | return PJ_SUCCESS; |
| 1462 | } |
| 1463 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1464 | #if PJMEDIA_HAS_INTEL_IPP_CODEC_G726 |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1465 | /* For G.726: amplify decoding result (USC G.726 encoder deamplified it) */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1466 | if (pt == PJMEDIA_RTP_PT_G726_16 || pt == PJMEDIA_RTP_PT_G726_24 || |
| 1467 | pt == PJMEDIA_RTP_PT_G726_32 || pt == PJMEDIA_RTP_PT_G726_40) |
| 1468 | { |
| 1469 | unsigned i; |
| 1470 | pj_int16_t *s = (pj_int16_t*)output->buf; |
| 1471 | |
| 1472 | for (i = 0; i < samples_per_frame; ++i) |
| 1473 | s[i] <<= 2; |
| 1474 | } |
| 1475 | #endif |
| 1476 | |
| 1477 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1478 | output->size = samples_per_frame << 1; |
| 1479 | output->timestamp.u64 = input->timestamp.u64; |
| 1480 | |
| 1481 | /* Invoke external PLC if codec has no internal PLC */ |
| 1482 | if (codec_data->plc && codec_data->plc_enabled) |
| 1483 | pjmedia_plc_save(codec_data->plc, (pj_int16_t*)output->buf); |
| 1484 | |
| 1485 | return PJ_SUCCESS; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Recover lost frame. |
| 1490 | */ |
| 1491 | static pj_status_t ipp_codec_recover(pjmedia_codec *codec, |
| 1492 | unsigned output_buf_len, |
| 1493 | struct pjmedia_frame *output) |
| 1494 | { |
| 1495 | ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1496 | struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx]; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1497 | unsigned samples_per_frame; |
| 1498 | |
| 1499 | PJ_UNUSED_ARG(output_buf_len); |
| 1500 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1501 | samples_per_frame = ippc->samples_per_frame; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1502 | |
| 1503 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1504 | output->size = samples_per_frame << 1; |
| 1505 | |
| 1506 | if (codec_data->plc_enabled) { |
| 1507 | if (codec_data->plc) { |
| 1508 | pjmedia_plc_generate(codec_data->plc, (pj_int16_t*)output->buf); |
| 1509 | } else { |
| 1510 | USC_PCMStream out; |
| 1511 | out.pBuffer = output->buf; |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1512 | ippc->fxns->Decode(codec_data->dec, NULL, &out); |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } else { |
| 1515 | pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame); |
| 1516 | } |
| 1517 | |
| 1518 | return PJ_SUCCESS; |
| 1519 | } |
| 1520 | |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1521 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1522 | #if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_IPP_LIBS |
| 1523 | # pragma comment( lib, "ippcore.lib") |
| 1524 | # pragma comment( lib, "ipps.lib") |
| 1525 | # pragma comment( lib, "ippsc.lib") |
| 1526 | # pragma comment( lib, "ippsr.lib") |
Benny Prijono | c543e9e | 2009-01-03 12:19:53 +0000 | [diff] [blame] | 1527 | //# pragma comment( lib, "ippcorel.lib") |
| 1528 | //# pragma comment( lib, "ippsemerged.lib") |
| 1529 | //# pragma comment( lib, "ippsmerged.lib") |
| 1530 | //# pragma comment( lib, "ippscemerged.lib") |
| 1531 | //# pragma comment( lib, "ippscmerged.lib") |
| 1532 | //# pragma comment( lib, "ippsremerged.lib") |
| 1533 | //# pragma comment( lib, "ippsrmerged.lib") |
Benny Prijono | e1e6d51 | 2009-01-02 15:17:47 +0000 | [diff] [blame] | 1534 | # if defined(IPP_VERSION_MAJOR) && IPP_VERSION_MAJOR>=6 |
| 1535 | # pragma comment( lib, "speech.lib") |
| 1536 | # else |
| 1537 | # pragma comment( lib, "usc.lib") |
| 1538 | # endif |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1539 | #endif |
| 1540 | |
| 1541 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 1542 | #endif /* PJMEDIA_HAS_INTEL_IPP */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 1543 | |