frame.h 10 KB

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