Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 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 | #ifndef __PJMEDIA_FRAME_H__ |
| 21 | #define __PJMEDIA_FRAME_H__ |
| 22 | |
| 23 | /** |
| 24 | * @file pjmedia/frame.h Media frame |
| 25 | * @brief Frame |
| 26 | */ |
| 27 | #include <pjmedia/types.h> |
| 28 | #include <pj/string.h> |
| 29 | |
| 30 | /** |
| 31 | * @defgroup PJMEDIA_FRAME Media frame |
| 32 | * @ingroup PJMEDIA_TYPES |
| 33 | * @brief Frame |
| 34 | * @{ |
| 35 | */ |
| 36 | |
| 37 | PJ_BEGIN_DECL |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Types of media frame. |
| 42 | */ |
| 43 | typedef enum pjmedia_frame_type |
| 44 | { |
| 45 | PJMEDIA_FRAME_TYPE_NONE, /**< No frame. */ |
| 46 | PJMEDIA_FRAME_TYPE_AUDIO, /**< Normal audio frame. */ |
| 47 | PJMEDIA_FRAME_TYPE_EXTENDED, /**< Extended audio frame. */ |
| 48 | PJMEDIA_FRAME_TYPE_VIDEO /**< Video frame. */ |
| 49 | |
| 50 | } pjmedia_frame_type; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * This structure describes a media frame. |
| 55 | */ |
| 56 | typedef struct pjmedia_frame |
| 57 | { |
| 58 | pjmedia_frame_type type; /**< Frame type. */ |
| 59 | void *buf; /**< Pointer to buffer. */ |
| 60 | pj_size_t size; /**< Frame size in bytes. */ |
| 61 | pj_timestamp timestamp; /**< Frame timestamp. */ |
| 62 | pj_uint32_t bit_info; /**< Bit info of the frame, sample case: |
| 63 | a frame may not exactly start and end |
| 64 | at the octet boundary, so this field |
| 65 | may be used for specifying start & |
| 66 | end bit offset. */ |
| 67 | } pjmedia_frame; |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * The pjmedia_frame_ext is used to carry a more complex audio frames than |
| 72 | * the typical PCM audio frames, and it is signaled by setting the "type" |
| 73 | * field of a pjmedia_frame to PJMEDIA_FRAME_TYPE_EXTENDED. With this set, |
| 74 | * application may typecast pjmedia_frame to pjmedia_frame_ext. |
| 75 | * |
| 76 | * This structure may contain more than one audio frames, which subsequently |
| 77 | * will be called subframes in this structure. The subframes section |
| 78 | * immediately follows the end of this structure, and each subframe is |
| 79 | * represented by pjmedia_frame_ext_subframe structure. Every next |
| 80 | * subframe immediately follows the previous subframe, and all subframes |
| 81 | * are byte-aligned although its payload may not be byte-aligned. |
| 82 | */ |
| 83 | |
| 84 | #pragma pack(1) |
| 85 | typedef struct pjmedia_frame_ext { |
| 86 | pjmedia_frame base; /**< Base frame info */ |
| 87 | pj_uint16_t samples_cnt; /**< Number of samples in this frame */ |
| 88 | pj_uint16_t subframe_cnt; /**< Number of (sub)frames in this frame */ |
| 89 | |
| 90 | /* Zero or more (sub)frames follows immediately after this, |
| 91 | * each will be represented by pjmedia_frame_ext_subframe |
| 92 | */ |
| 93 | } pjmedia_frame_ext; |
| 94 | #pragma pack() |
| 95 | |
| 96 | /** |
| 97 | * This structure represents the individual subframes in the |
| 98 | * pjmedia_frame_ext structure. |
| 99 | */ |
| 100 | #pragma pack(1) |
| 101 | typedef struct pjmedia_frame_ext_subframe { |
| 102 | pj_uint16_t bitlen; /**< Number of bits in the data */ |
| 103 | pj_uint8_t data[1]; /**< Start of encoded data */ |
| 104 | } pjmedia_frame_ext_subframe; |
| 105 | |
| 106 | #pragma pack() |
| 107 | |
| 108 | /** |
| 109 | * Copy one frame to another. If the destination frame's size is smaller than |
| 110 | * the source frame's, the destination buffer will be truncated. |
| 111 | * |
| 112 | * @param src Source frame. |
| 113 | * @param dst Destination frame. |
| 114 | */ |
| 115 | PJ_INLINE(void) pjmedia_frame_copy(pjmedia_frame *dst, |
| 116 | const pjmedia_frame *src) |
| 117 | { |
| 118 | dst->type = src->type; |
| 119 | dst->timestamp = src->timestamp; |
| 120 | dst->bit_info = src->bit_info; |
| 121 | dst->size = (dst->size < src->size? dst->size: src->size); |
| 122 | pj_memcpy(dst->buf, src->buf, dst->size); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Append one subframe to #pjmedia_frame_ext. |
| 127 | * |
| 128 | * @param frm The #pjmedia_frame_ext. |
| 129 | * @param src Subframe data. |
| 130 | * @param bitlen Length of subframe, in bits. |
| 131 | * @param samples_cnt Number of audio samples in subframe. |
| 132 | */ |
| 133 | PJ_INLINE(void) pjmedia_frame_ext_append_subframe(pjmedia_frame_ext *frm, |
| 134 | const void *src, |
| 135 | unsigned bitlen, |
| 136 | unsigned samples_cnt) |
| 137 | { |
| 138 | pjmedia_frame_ext_subframe *fsub; |
| 139 | pj_uint8_t *p; |
| 140 | unsigned i; |
| 141 | |
| 142 | p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext); |
| 143 | for (i = 0; i < frm->subframe_cnt; ++i) { |
| 144 | fsub = (pjmedia_frame_ext_subframe*) p; |
| 145 | p += sizeof(fsub->bitlen) + ((fsub->bitlen+7) >> 3); |
| 146 | } |
| 147 | |
| 148 | fsub = (pjmedia_frame_ext_subframe*) p; |
| 149 | fsub->bitlen = (pj_uint16_t)bitlen; |
| 150 | if (bitlen) |
| 151 | pj_memcpy(fsub->data, src, (bitlen+7) >> 3); |
| 152 | |
| 153 | frm->subframe_cnt++; |
| 154 | frm->samples_cnt = (pj_uint16_t)(frm->samples_cnt + samples_cnt); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get a subframe from #pjmedia_frame_ext. |
| 159 | * |
| 160 | * @param frm The #pjmedia_frame_ext. |
| 161 | * @param n Subframe index, zero based. |
| 162 | * |
| 163 | * @return The n-th subframe, or NULL if n is out-of-range. |
| 164 | */ |
| 165 | PJ_INLINE(pjmedia_frame_ext_subframe*) |
| 166 | pjmedia_frame_ext_get_subframe(const pjmedia_frame_ext *frm, unsigned n) |
| 167 | { |
| 168 | pjmedia_frame_ext_subframe *sf = NULL; |
| 169 | |
| 170 | if (n < frm->subframe_cnt) { |
| 171 | pj_uint8_t *p; |
| 172 | unsigned i; |
| 173 | |
| 174 | p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext); |
| 175 | for (i = 0; i < n; ++i) { |
| 176 | sf = (pjmedia_frame_ext_subframe*) p; |
| 177 | p += sizeof(sf->bitlen) + ((sf->bitlen+7) >> 3); |
| 178 | } |
| 179 | |
| 180 | sf = (pjmedia_frame_ext_subframe*) p; |
| 181 | } |
| 182 | |
| 183 | return sf; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Extract all frame payload to the specified buffer. |
| 188 | * |
| 189 | * @param frm The frame. |
| 190 | * @param dst Destination buffer. |
| 191 | * @param maxlen Maximum size to copy (i.e. the size of the |
| 192 | * destination buffer). |
| 193 | * |
| 194 | * @return Total size of payload copied. |
| 195 | */ |
| 196 | PJ_INLINE(unsigned) |
| 197 | pjmedia_frame_ext_copy_payload(const pjmedia_frame_ext *frm, |
| 198 | void *dst, |
| 199 | unsigned maxlen) |
| 200 | { |
| 201 | unsigned i, copied=0; |
| 202 | for (i=0; i<frm->subframe_cnt; ++i) { |
| 203 | pjmedia_frame_ext_subframe *sf; |
| 204 | unsigned sz; |
| 205 | |
| 206 | sf = pjmedia_frame_ext_get_subframe(frm, i); |
| 207 | if (!sf) |
| 208 | continue; |
| 209 | |
| 210 | sz = ((sf->bitlen + 7) >> 3); |
| 211 | if (sz + copied > maxlen) |
| 212 | break; |
| 213 | |
| 214 | pj_memcpy(((pj_uint8_t*)dst) + copied, sf->data, sz); |
| 215 | copied += sz; |
| 216 | } |
| 217 | return copied; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Pop out first n subframes from #pjmedia_frame_ext. |
| 223 | * |
| 224 | * @param frm The #pjmedia_frame_ext. |
| 225 | * @param n Number of first subframes to be popped out. |
| 226 | * |
| 227 | * @return PJ_SUCCESS when successful. |
| 228 | */ |
| 229 | PJ_INLINE(pj_status_t) |
| 230 | pjmedia_frame_ext_pop_subframes(pjmedia_frame_ext *frm, unsigned n) |
| 231 | { |
| 232 | pjmedia_frame_ext_subframe *sf; |
| 233 | pj_uint8_t *move_src; |
| 234 | pj_size_t move_len; |
| 235 | |
| 236 | if (frm->subframe_cnt <= n) { |
| 237 | frm->subframe_cnt = 0; |
| 238 | frm->samples_cnt = 0; |
| 239 | return PJ_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | move_src = (pj_uint8_t*)pjmedia_frame_ext_get_subframe(frm, n); |
| 243 | sf = pjmedia_frame_ext_get_subframe(frm, frm->subframe_cnt-1); |
| 244 | move_len = ((pj_uint8_t*)sf - move_src + sizeof(sf->bitlen) + |
| 245 | ((sf->bitlen+7) >> 3)); |
| 246 | pj_memmove((pj_uint8_t*)frm+sizeof(pjmedia_frame_ext), |
| 247 | move_src, move_len); |
| 248 | |
| 249 | frm->samples_cnt = (pj_uint16_t) |
| 250 | (frm->samples_cnt - n*frm->samples_cnt/frm->subframe_cnt); |
| 251 | frm->subframe_cnt = (pj_uint16_t) (frm->subframe_cnt - n); |
| 252 | |
| 253 | return PJ_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * This is a general purpose function set PCM samples to zero. |
| 259 | * Since this function is needed by many parts of the library, |
| 260 | * by putting this functionality in one place, it enables some. |
| 261 | * clever people to optimize this function. |
| 262 | * |
| 263 | * @param samples The 16bit PCM samples. |
| 264 | * @param count Number of samples. |
| 265 | */ |
| 266 | PJ_INLINE(void) pjmedia_zero_samples(pj_int16_t *samples, unsigned count) |
| 267 | { |
| 268 | #if 1 |
| 269 | pj_bzero(samples, (count<<1)); |
| 270 | #elif 0 |
| 271 | unsigned i; |
| 272 | for (i=0; i<count; ++i) samples[i] = 0; |
| 273 | #else |
| 274 | unsigned i; |
| 275 | count >>= 1; |
| 276 | for (i=0; i<count; ++i) ((pj_int32_t*)samples)[i] = (pj_int32_t)0; |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * This is a general purpose function to copy samples from/to buffers with |
| 283 | * equal size. Since this function is needed by many parts of the library, |
| 284 | * by putting this functionality in one place, it enables some. |
| 285 | * clever people to optimize this function. |
| 286 | */ |
| 287 | PJ_INLINE(void) pjmedia_copy_samples(pj_int16_t *dst, const pj_int16_t *src, |
| 288 | unsigned count) |
| 289 | { |
| 290 | #if 1 |
| 291 | pj_memcpy(dst, src, (count<<1)); |
| 292 | #elif 0 |
| 293 | unsigned i; |
| 294 | for (i=0; i<count; ++i) dst[i] = src[i]; |
| 295 | #else |
| 296 | unsigned i; |
| 297 | count >>= 1; |
| 298 | for (i=0; i<count; ++i) |
| 299 | ((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i]; |
| 300 | #endif |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /** |
| 305 | * This is a general purpose function to copy samples from/to buffers with |
| 306 | * equal size. Since this function is needed by many parts of the library, |
| 307 | * by putting this functionality in one place, it enables some. |
| 308 | * clever people to optimize this function. |
| 309 | */ |
| 310 | PJ_INLINE(void) pjmedia_move_samples(pj_int16_t *dst, const pj_int16_t *src, |
| 311 | unsigned count) |
| 312 | { |
| 313 | #if 1 |
| 314 | pj_memmove(dst, src, (count<<1)); |
| 315 | #elif 0 |
| 316 | unsigned i; |
| 317 | for (i=0; i<count; ++i) dst[i] = src[i]; |
| 318 | #else |
| 319 | unsigned i; |
| 320 | count >>= 1; |
| 321 | for (i=0; i<count; ++i) |
| 322 | ((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i]; |
| 323 | #endif |
| 324 | } |
| 325 | |
| 326 | PJ_END_DECL |
| 327 | |
| 328 | /** |
| 329 | * @} |
| 330 | */ |
| 331 | |
| 332 | #endif /* __PJMEDIA_FRAME_H__ */ |