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