format.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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_FORMAT_H__
  20. #define __PJMEDIA_FORMAT_H__
  21. /**
  22. * @file pjmedia/format.h Media format
  23. * @brief Media format
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMEDIA_FORMAT Media format
  28. * @ingroup PJMEDIA_TYPES
  29. * @brief Media format
  30. * @{
  31. */
  32. PJ_BEGIN_DECL
  33. /**
  34. * Macro for packing format from a four character code, similar to FOURCC.
  35. * This macro is used for building the constants in pjmedia_format_id
  36. * enumeration.
  37. */
  38. #define PJMEDIA_FORMAT_PACK(C1, C2, C3, C4) PJMEDIA_FOURCC(C1, C2, C3, C4)
  39. /**
  40. * This enumeration uniquely identify audio sample and/or video pixel formats.
  41. * Some well known formats are listed here. The format ids are built by
  42. * combining four character codes, similar to FOURCC. The format id is
  43. * extensible, as application may define and use format ids not declared
  44. * on this enumeration.
  45. *
  46. * This format id along with other information will fully describe the media
  47. * in #pjmedia_format structure.
  48. */
  49. typedef enum pjmedia_format_id
  50. {
  51. /*
  52. * Audio formats
  53. */
  54. /** 16bit signed integer linear PCM audio */
  55. PJMEDIA_FORMAT_L16 = 0,
  56. /** Alias for PJMEDIA_FORMAT_L16 */
  57. PJMEDIA_FORMAT_PCM = PJMEDIA_FORMAT_L16,
  58. /** G.711 ALAW */
  59. PJMEDIA_FORMAT_PCMA = PJMEDIA_FORMAT_PACK('A', 'L', 'A', 'W'),
  60. /** Alias for PJMEDIA_FORMAT_PCMA */
  61. PJMEDIA_FORMAT_ALAW = PJMEDIA_FORMAT_PCMA,
  62. /** G.711 ULAW */
  63. PJMEDIA_FORMAT_PCMU = PJMEDIA_FORMAT_PACK('u', 'L', 'A', 'W'),
  64. /** Aliaw for PJMEDIA_FORMAT_PCMU */
  65. PJMEDIA_FORMAT_ULAW = PJMEDIA_FORMAT_PCMU,
  66. /** AMR narrowband */
  67. PJMEDIA_FORMAT_AMR = PJMEDIA_FORMAT_PACK(' ', 'A', 'M', 'R'),
  68. /** ITU G.729 */
  69. PJMEDIA_FORMAT_G729 = PJMEDIA_FORMAT_PACK('G', '7', '2', '9'),
  70. /** Internet Low Bit-Rate Codec (ILBC) */
  71. PJMEDIA_FORMAT_ILBC = PJMEDIA_FORMAT_PACK('I', 'L', 'B', 'C'),
  72. /*
  73. * Video formats.
  74. */
  75. /**
  76. * 24bit RGB
  77. */
  78. PJMEDIA_FORMAT_RGB24 = PJMEDIA_FORMAT_PACK('R', 'G', 'B', '3'),
  79. /**
  80. * 32bit RGB with alpha channel
  81. */
  82. PJMEDIA_FORMAT_RGBA = PJMEDIA_FORMAT_PACK('R', 'G', 'B', 'A'),
  83. PJMEDIA_FORMAT_BGRA = PJMEDIA_FORMAT_PACK('B', 'G', 'R', 'A'),
  84. /**
  85. * Alias for PJMEDIA_FORMAT_RGBA
  86. */
  87. PJMEDIA_FORMAT_RGB32 = PJMEDIA_FORMAT_RGBA,
  88. /**
  89. * Device Independent Bitmap, alias for 24 bit RGB
  90. */
  91. PJMEDIA_FORMAT_DIB = PJMEDIA_FORMAT_PACK('D', 'I', 'B', ' '),
  92. /**
  93. * This is planar 4:4:4/24bpp RGB format, the data can be treated as
  94. * three planes of color components, where the first plane contains
  95. * only the G samples, the second plane contains only the B samples,
  96. * and the third plane contains only the R samples.
  97. */
  98. PJMEDIA_FORMAT_GBRP = PJMEDIA_FORMAT_PACK('G', 'B', 'R', 'P'),
  99. /**
  100. * This is a packed 4:4:4/32bpp format, where each pixel is encoded as
  101. * four consecutive bytes, arranged in the following sequence: V0, U0,
  102. * Y0, A0. Source:
  103. * http://msdn.microsoft.com/en-us/library/dd206750%28v=VS.85%29.aspx#ayuv
  104. */
  105. PJMEDIA_FORMAT_AYUV = PJMEDIA_FORMAT_PACK('A', 'Y', 'U', 'V'),
  106. /**
  107. * This is packed 4:2:2/16bpp YUV format, the data can be treated as
  108. * an array of unsigned char values, where the first byte contains
  109. * the first Y sample, the second byte contains the first U (Cb) sample,
  110. * the third byte contains the second Y sample, and the fourth byte
  111. * contains the first V (Cr) sample, and so forth. Source:
  112. * http://msdn.microsoft.com/en-us/library/dd206750%28v=VS.85%29.aspx#yuy2
  113. */
  114. PJMEDIA_FORMAT_YUY2 = PJMEDIA_FORMAT_PACK('Y', 'U', 'Y', '2'),
  115. /**
  116. * This format is the same as the YUY2 format except the byte order is
  117. * reversed -- that is, the chroma and luma bytes are flipped. If the
  118. * image is addressed as an array of two little-endian WORD values, the
  119. * first WORD contains U in the LSBs and Y0 in the MSBs, and the second
  120. * WORD contains V in the LSBs and Y1 in the MSBs. Source:
  121. * http://msdn.microsoft.com/en-us/library/dd206750%28v=VS.85%29.aspx#uyvy
  122. */
  123. PJMEDIA_FORMAT_UYVY = PJMEDIA_FORMAT_PACK('U', 'Y', 'V', 'Y'),
  124. /**
  125. * This format is the same as the YUY2 and UYVY format except the byte
  126. * order is reversed -- that is, the chroma and luma bytes are flipped.
  127. * If the image is addressed as an array of two little-endian WORD values,
  128. * the first WORD contains Y0 in the LSBs and V in the MSBs, and the second
  129. * WORD contains Y1 in the LSBs and U in the MSBs.
  130. */
  131. PJMEDIA_FORMAT_YVYU = PJMEDIA_FORMAT_PACK('Y', 'V', 'Y', 'U'),
  132. /**
  133. * This is planar 4:2:0/12bpp YUV format, the data can be treated as
  134. * three planes of color components, where the first plane contains
  135. * only the Y samples, the second plane contains only the U (Cb) samples,
  136. * and the third plane contains only the V (Cr) sample.
  137. */
  138. PJMEDIA_FORMAT_I420 = PJMEDIA_FORMAT_PACK('I', '4', '2', '0'),
  139. /**
  140. * IYUV is alias for I420.
  141. */
  142. PJMEDIA_FORMAT_IYUV = PJMEDIA_FORMAT_I420,
  143. /**
  144. * This is planar 4:2:0/12bpp YUV format, similar to I420 or IYUV but
  145. * the U (Cb) and V (Cr) planes order is switched, i.e: the second plane
  146. * contains the V (Cb) samples and the third plane contains the V (Cr)
  147. * samples.
  148. */
  149. PJMEDIA_FORMAT_YV12 = PJMEDIA_FORMAT_PACK('Y', 'V', '1', '2'),
  150. /**
  151. * This is planar 4:2:0/12bpp YUV format, the data can be treated as
  152. * two planes of color components, where the first plane contains
  153. * only the Y samples, the second plane contains interleaved
  154. * U (Cb) - V (Cr) samples.
  155. */
  156. PJMEDIA_FORMAT_NV12 = PJMEDIA_FORMAT_PACK('N', 'V', '1', '2'),
  157. /**
  158. * This is planar 4:2:0/12bpp YUV format, the data can be treated as
  159. * two planes of color components, where the first plane contains
  160. * only the Y samples, the second plane contains interleaved
  161. * V (Cr) - U (Cb) samples.
  162. */
  163. PJMEDIA_FORMAT_NV21 = PJMEDIA_FORMAT_PACK('N', 'V', '2', '1'),
  164. /**
  165. * This is planar 4:2:2/16bpp YUV format, the data can be treated as
  166. * three planes of color components, where the first plane contains
  167. * only the Y samples, the second plane contains only the U (Cb) samples,
  168. * and the third plane contains only the V (Cr) sample.
  169. */
  170. PJMEDIA_FORMAT_I422 = PJMEDIA_FORMAT_PACK('I', '4', '2', '2'),
  171. /**
  172. * The JPEG version of planar 4:2:0/12bpp YUV format.
  173. */
  174. PJMEDIA_FORMAT_I420JPEG = PJMEDIA_FORMAT_PACK('J', '4', '2', '0'),
  175. /**
  176. * The JPEG version of planar 4:2:2/16bpp YUV format.
  177. */
  178. PJMEDIA_FORMAT_I422JPEG = PJMEDIA_FORMAT_PACK('J', '4', '2', '2'),
  179. /**
  180. * Encoded video formats
  181. */
  182. PJMEDIA_FORMAT_H261 = PJMEDIA_FORMAT_PACK('H', '2', '6', '1'),
  183. PJMEDIA_FORMAT_H263 = PJMEDIA_FORMAT_PACK('H', '2', '6', '3'),
  184. PJMEDIA_FORMAT_H263P = PJMEDIA_FORMAT_PACK('P', '2', '6', '3'),
  185. PJMEDIA_FORMAT_H264 = PJMEDIA_FORMAT_PACK('H', '2', '6', '4'),
  186. PJMEDIA_FORMAT_VP8 = PJMEDIA_FORMAT_PACK('V', 'P', '8', '0'),
  187. PJMEDIA_FORMAT_VP9 = PJMEDIA_FORMAT_PACK('V', 'P', '9', '0'),
  188. PJMEDIA_FORMAT_MJPEG = PJMEDIA_FORMAT_PACK('M', 'J', 'P', 'G'),
  189. PJMEDIA_FORMAT_MPEG1VIDEO = PJMEDIA_FORMAT_PACK('M', 'P', '1', 'V'),
  190. PJMEDIA_FORMAT_MPEG2VIDEO = PJMEDIA_FORMAT_PACK('M', 'P', '2', 'V'),
  191. PJMEDIA_FORMAT_MPEG4 = PJMEDIA_FORMAT_PACK('M', 'P', 'G', '4'),
  192. PJMEDIA_FORMAT_INVALID = 0xFFFFFFF
  193. } pjmedia_format_id;
  194. /**
  195. * This enumeration specifies what type of detail is included in a
  196. * #pjmedia_format structure.
  197. */
  198. typedef enum pjmedia_format_detail_type
  199. {
  200. /** Format detail is not specified. */
  201. PJMEDIA_FORMAT_DETAIL_NONE,
  202. /** Audio format detail. */
  203. PJMEDIA_FORMAT_DETAIL_AUDIO,
  204. /** Video format detail. */
  205. PJMEDIA_FORMAT_DETAIL_VIDEO,
  206. /** Number of format detail type that has been defined. */
  207. PJMEDIA_FORMAT_DETAIL_MAX
  208. } pjmedia_format_detail_type;
  209. /**
  210. * This structure is put in \a detail field of #pjmedia_format to describe
  211. * detail information about an audio media.
  212. */
  213. typedef struct pjmedia_audio_format_detail
  214. {
  215. unsigned clock_rate; /**< Audio clock rate in samples or Hz. */
  216. unsigned channel_count; /**< Number of channels. */
  217. unsigned frame_time_usec;/**< Frame interval, in microseconds. */
  218. unsigned bits_per_sample;/**< Number of bits per sample. */
  219. pj_uint32_t avg_bps; /**< Average bitrate */
  220. pj_uint32_t max_bps; /**< Maximum bitrate */
  221. } pjmedia_audio_format_detail;
  222. /**
  223. * This structure is put in \a detail field of #pjmedia_format to describe
  224. * detail information about a video media.
  225. *
  226. * Additional information about a video format can also be retrieved by
  227. * calling #pjmedia_get_video_format_info().
  228. */
  229. typedef struct pjmedia_video_format_detail
  230. {
  231. pjmedia_rect_size size; /**< Video size (width, height) */
  232. pjmedia_ratio fps; /**< Number of frames per second. */
  233. pj_uint32_t avg_bps;/**< Average bitrate. */
  234. pj_uint32_t max_bps;/**< Maximum bitrate. */
  235. } pjmedia_video_format_detail;
  236. /**
  237. * This macro declares the size of the detail section in #pjmedia_format
  238. * to be reserved for user defined detail.
  239. */
  240. #ifndef PJMEDIA_FORMAT_DETAIL_USER_SIZE
  241. # define PJMEDIA_FORMAT_DETAIL_USER_SIZE 1
  242. #endif
  243. /**
  244. * This structure contains all the information needed to completely describe
  245. * a media.
  246. */
  247. typedef struct pjmedia_format
  248. {
  249. /**
  250. * The format id that specifies the audio sample or video pixel format.
  251. * Some well known formats ids are declared in pjmedia_format_id
  252. * enumeration.
  253. *
  254. * @see pjmedia_format_id
  255. */
  256. pj_uint32_t id;
  257. /**
  258. * The top-most type of the media, as an information.
  259. */
  260. pjmedia_type type;
  261. /**
  262. * The type of detail structure in the \a detail pointer.
  263. */
  264. pjmedia_format_detail_type detail_type;
  265. /**
  266. * Detail section to describe the media.
  267. */
  268. union
  269. {
  270. /**
  271. * Detail section for audio format.
  272. */
  273. pjmedia_audio_format_detail aud;
  274. /**
  275. * Detail section for video format.
  276. */
  277. pjmedia_video_format_detail vid;
  278. /**
  279. * Reserved area for user-defined format detail.
  280. */
  281. char user[PJMEDIA_FORMAT_DETAIL_USER_SIZE];
  282. } det;
  283. } pjmedia_format;
  284. /**
  285. * This enumeration describes video color model. It mostly serves as
  286. * information only.
  287. */
  288. typedef enum pjmedia_color_model
  289. {
  290. /** The color model is unknown or unspecified. */
  291. PJMEDIA_COLOR_MODEL_NONE,
  292. /** RGB color model. */
  293. PJMEDIA_COLOR_MODEL_RGB,
  294. /** YUV color model. */
  295. PJMEDIA_COLOR_MODEL_YUV
  296. } pjmedia_color_model;
  297. /**
  298. * This structure holds information to apply a specific video format
  299. * against size and buffer information, and get additional information
  300. * from it. To do that, application fills up the input fields of this
  301. * structure, and give this structure to \a apply_fmt() function
  302. * of #pjmedia_video_format_info structure.
  303. */
  304. typedef struct pjmedia_video_apply_fmt_param
  305. {
  306. /* input fields: */
  307. /**
  308. * [IN] The image size. This field is mandatory, and has to be set
  309. * correctly prior to calling \a apply_fmt() function.
  310. */
  311. pjmedia_rect_size size;
  312. /**
  313. * [IN] Pointer to the buffer that holds the frame. The \a apply_fmt()
  314. * function uses this pointer to calculate the pointer for each video
  315. * planes of the media. This field is optional -- however, the
  316. * \a apply_fmt() would still fill up the \a planes[] array with the
  317. * correct pointer even though the buffer is set to NULL. This could be
  318. * useful to calculate the size (in bytes) of each plane.
  319. */
  320. pj_uint8_t *buffer;
  321. /* output fields: */
  322. /**
  323. * [OUT] The size (in bytes) required of the buffer to hold the video
  324. * frame of the particular frame size (width, height).
  325. */
  326. pj_size_t framebytes;
  327. /**
  328. * [OUT] Array of strides value (in bytes) for each video plane.
  329. */
  330. int strides[PJMEDIA_MAX_VIDEO_PLANES];
  331. /**
  332. * [OUT] Array of pointers to each of the video planes. The values are
  333. * calculated from the \a buffer field.
  334. */
  335. pj_uint8_t *planes[PJMEDIA_MAX_VIDEO_PLANES];
  336. /**
  337. * [OUT] Array of video plane sizes.
  338. */
  339. pj_size_t plane_bytes[PJMEDIA_MAX_VIDEO_PLANES];
  340. } pjmedia_video_apply_fmt_param;
  341. /**
  342. * This structure holds information to describe a video format. Application
  343. * can retrieve this structure by calling #pjmedia_get_video_format_info()
  344. * funcion.
  345. */
  346. typedef struct pjmedia_video_format_info
  347. {
  348. /**
  349. * The unique format ID of the media. Well known format ids are declared
  350. * in pjmedia_format_id enumeration.
  351. */
  352. pj_uint32_t id;
  353. /**
  354. * Null terminated string containing short identification about the
  355. * format.
  356. */
  357. char name[8];
  358. /**
  359. * Information about the color model of this video format.
  360. */
  361. pjmedia_color_model color_model;
  362. /**
  363. * Number of bits needed to store one pixel of this video format.
  364. */
  365. pj_uint8_t bpp;
  366. /**
  367. * Number of video planes that this format uses. Value 1 indicates
  368. * packed format, while value greater than 1 indicates planar format.
  369. */
  370. pj_uint8_t plane_cnt;
  371. /**
  372. * Pointer to function to apply this format against size and buffer
  373. * information in pjmedia_video_apply_fmt_param argument. Application
  374. * uses this function to obtain various information such as the
  375. * memory size of a frame buffer, strides value of the image, the
  376. * location of the planes, and so on. See pjmedia_video_apply_fmt_param
  377. * for additional information.
  378. *
  379. * @param vfi The video format info.
  380. * @param vafp The parameters to investigate.
  381. *
  382. * @return PJ_SUCCESS if the function has calculated the
  383. * information in \a vafp successfully.
  384. */
  385. pj_status_t (*apply_fmt)(const struct pjmedia_video_format_info *vfi,
  386. pjmedia_video_apply_fmt_param *vafp);
  387. } pjmedia_video_format_info;
  388. /*****************************************************************************
  389. * UTILITIES:
  390. */
  391. /**
  392. * General utility routine to calculate samples per frame value from clock
  393. * rate, ptime (in usec), and channel count. Application should use this
  394. * macro whenever possible due to possible overflow in the math calculation.
  395. *
  396. * @param clock_rate Clock rate.
  397. * @param usec_ptime Frame interval, in microsecond.
  398. * @param channel_count Number of channels.
  399. *
  400. * @return The samples per frame value.
  401. */
  402. PJ_INLINE(unsigned) PJMEDIA_SPF(unsigned clock_rate, unsigned usec_ptime,
  403. unsigned channel_count)
  404. {
  405. #if PJ_HAS_INT64
  406. return ((unsigned)((pj_uint64_t)usec_ptime * \
  407. clock_rate * channel_count / 1000000));
  408. #elif PJ_HAS_FLOATING_POINT
  409. return ((unsigned)(1.0*usec_ptime * clock_rate * channel_count / 1000000));
  410. #else
  411. return ((unsigned)(usec_ptime / 1000L * clock_rate * \
  412. channel_count / 1000));
  413. #endif
  414. }
  415. /**
  416. * Variant of #PJMEDIA_SPF() which takes frame rate instead of ptime.
  417. */
  418. PJ_INLINE(unsigned) PJMEDIA_SPF2(unsigned clock_rate, const pjmedia_ratio *fr,
  419. unsigned channel_count)
  420. {
  421. #if PJ_HAS_INT64
  422. return ((unsigned)((pj_uint64_t)clock_rate * fr->denum \
  423. / fr->num / channel_count));
  424. #elif PJ_HAS_FLOATING_POINT
  425. return ((unsigned)(1.0* clock_rate * fr->denum / fr->num /channel_count));
  426. #else
  427. return ((unsigned)(1L * clock_rate * fr->denum / fr->num / channel_count));
  428. #endif
  429. }
  430. /**
  431. * Utility routine to calculate frame size (in bytes) from bitrate and frame
  432. * interval values. Application should use this macro whenever possible due
  433. * to possible overflow in the math calculation.
  434. *
  435. * @param bps The bitrate of the stream.
  436. * @param usec_ptime Frame interval, in microsecond.
  437. *
  438. * @return Frame size in bytes.
  439. */
  440. PJ_INLINE(unsigned) PJMEDIA_FSZ(unsigned bps, unsigned usec_ptime)
  441. {
  442. #if PJ_HAS_INT64
  443. return ((unsigned)((pj_uint64_t)bps * usec_ptime / PJ_UINT64(8000000)));
  444. #elif PJ_HAS_FLOATING_POINT
  445. return ((unsigned)(1.0 * bps * usec_ptime / 8000000.0));
  446. #else
  447. return ((unsigned)(bps / 8L * usec_ptime / 1000000));
  448. #endif
  449. }
  450. /**
  451. * General utility routine to calculate ptime value from frame rate.
  452. * Application should use this macro whenever possible due to possible
  453. * overflow in the math calculation.
  454. *
  455. * @param frame_rate Frame rate
  456. *
  457. * @return The ptime value (in usec).
  458. */
  459. PJ_INLINE(unsigned) PJMEDIA_PTIME(const pjmedia_ratio *frame_rate)
  460. {
  461. #if PJ_HAS_INT64
  462. return ((unsigned)((pj_uint64_t)1000000 * \
  463. frame_rate->denum / frame_rate->num));
  464. #elif PJ_HAS_FLOATING_POINT
  465. return ((unsigned)(1000000.0 * frame_rate->denum /
  466. frame_rate->num));
  467. #else
  468. return ((unsigned)((1000L * frame_rate->denum /
  469. frame_rate->num) * 1000));
  470. #endif
  471. }
  472. /**
  473. * Utility to retrieve samples_per_frame value from
  474. * pjmedia_audio_format_detail.
  475. *
  476. * @param pafd Pointer to pjmedia_audio_format_detail
  477. * @return Samples per frame
  478. */
  479. PJ_INLINE(unsigned) PJMEDIA_AFD_SPF(const pjmedia_audio_format_detail *pafd)
  480. {
  481. return PJMEDIA_SPF(pafd->clock_rate, pafd->frame_time_usec,
  482. pafd->channel_count);
  483. }
  484. /**
  485. * Utility to retrieve average frame size from pjmedia_audio_format_detail.
  486. * The average frame size is derived from the average bitrate of the audio
  487. * stream.
  488. *
  489. * @param afd Pointer to pjmedia_audio_format_detail
  490. * @return Average frame size.
  491. */
  492. PJ_INLINE(unsigned) PJMEDIA_AFD_AVG_FSZ(const pjmedia_audio_format_detail *afd)
  493. {
  494. return PJMEDIA_FSZ(afd->avg_bps, afd->frame_time_usec);
  495. }
  496. /**
  497. * Utility to retrieve maximum frame size from pjmedia_audio_format_detail.
  498. * The maximum frame size is derived from the maximum bitrate of the audio
  499. * stream.
  500. *
  501. * @param afd Pointer to pjmedia_audio_format_detail
  502. * @return Average frame size.
  503. */
  504. PJ_INLINE(unsigned) PJMEDIA_AFD_MAX_FSZ(const pjmedia_audio_format_detail *afd)
  505. {
  506. return PJMEDIA_FSZ(afd->max_bps, afd->frame_time_usec);
  507. }
  508. /**
  509. * Initialize the format as audio format with the specified parameters.
  510. *
  511. * @param fmt The format to be initialized.
  512. * @param fmt_id Format ID. See #pjmedia_format_id
  513. * @param clock_rate Audio clock rate.
  514. * @param channel_count Number of channels.
  515. * @param bits_per_sample Number of bits per sample.
  516. * @param frame_time_usec Frame interval, in microsecond.
  517. * @param avg_bps Average bitrate.
  518. * @param max_bps Maximum bitrate.
  519. */
  520. PJ_INLINE(void) pjmedia_format_init_audio(pjmedia_format *fmt,
  521. pj_uint32_t fmt_id,
  522. unsigned clock_rate,
  523. unsigned channel_count,
  524. unsigned bits_per_sample,
  525. unsigned frame_time_usec,
  526. pj_uint32_t avg_bps,
  527. pj_uint32_t max_bps)
  528. {
  529. /* This function is inlined to avoid build problem due to circular
  530. * dependency, i.e: this function is part of pjmedia and is needed
  531. * by pjmedia-audiodev, while pjmedia depends on pjmedia-audiodev.
  532. */
  533. fmt->id = fmt_id;
  534. fmt->type = PJMEDIA_TYPE_AUDIO;
  535. fmt->detail_type = PJMEDIA_FORMAT_DETAIL_AUDIO;
  536. fmt->det.aud.clock_rate = clock_rate;
  537. fmt->det.aud.channel_count = channel_count;
  538. fmt->det.aud.bits_per_sample = bits_per_sample;
  539. fmt->det.aud.frame_time_usec = frame_time_usec;
  540. fmt->det.aud.avg_bps = avg_bps;
  541. fmt->det.aud.max_bps = max_bps;
  542. }
  543. /**
  544. * Initialize the format as video format with the specified parameters.
  545. * A format manager should have been created, as this function will need
  546. * to consult to a format manager in order to fill in detailed
  547. * information about the format.
  548. *
  549. * @param fmt The format to be initialised.
  550. * @param fmt_id Format ID. See #pjmedia_format_id
  551. * @param width Image width.
  552. * @param height Image heigth.
  553. * @param fps_num FPS numerator.
  554. * @param fps_denum FPS denumerator.
  555. */
  556. PJ_DECL(void) pjmedia_format_init_video(pjmedia_format *fmt,
  557. pj_uint32_t fmt_id,
  558. unsigned width,
  559. unsigned height,
  560. unsigned fps_num,
  561. unsigned fps_denum);
  562. /**
  563. * Copy format to another.
  564. *
  565. * @param dst The destination format.
  566. * @param src The source format.
  567. *
  568. * @return Pointer to destination format.
  569. */
  570. PJ_DECL(pjmedia_format*) pjmedia_format_copy(pjmedia_format *dst,
  571. const pjmedia_format *src);
  572. /**
  573. * Check if the format contains audio format, and retrieve the audio format
  574. * detail in the format.
  575. *
  576. * @param fmt The format structure.
  577. * @param assert_valid If this is set to non-zero, an assertion will be
  578. * raised if the detail type is not audio or if the
  579. * the detail is NULL.
  580. *
  581. * @return The instance of audio format detail in the format
  582. * structure, or NULL if the format doesn't contain
  583. * audio detail.
  584. */
  585. PJ_DECL(pjmedia_audio_format_detail*)
  586. pjmedia_format_get_audio_format_detail(const pjmedia_format *fmt,
  587. pj_bool_t assert_valid);
  588. /**
  589. * Check if the format contains video format, and retrieve the video format
  590. * detail in the format.
  591. *
  592. * @param fmt The format structure.
  593. * @param assert_valid If this is set to non-zero, an assertion will be
  594. * raised if the detail type is not video or if the
  595. * the detail is NULL.
  596. *
  597. * @return The instance of video format detail in the format
  598. * structure, or NULL if the format doesn't contain
  599. * video detail.
  600. */
  601. PJ_DECL(pjmedia_video_format_detail*)
  602. pjmedia_format_get_video_format_detail(const pjmedia_format *fmt,
  603. pj_bool_t assert_valid);
  604. /*****************************************************************************
  605. * FORMAT MANAGEMENT:
  606. */
  607. /**
  608. * Opaque data type for video format manager. The video format manager manages
  609. * the repository of video formats that the framework recognises. Typically it
  610. * is a singleton instance, although application may instantiate more than one
  611. * instances of this if required.
  612. */
  613. typedef struct pjmedia_video_format_mgr pjmedia_video_format_mgr;
  614. /**
  615. * Create a new video format manager instance. This will also set the pointer
  616. * to the singleton instance if the value is still NULL.
  617. *
  618. * @param pool The pool to allocate memory.
  619. * @param max_fmt Maximum number of formats to accommodate.
  620. * @param options Option flags. Must be zero for now.
  621. * @param p_mgr Pointer to hold the created instance.
  622. *
  623. * @return PJ_SUCCESS on success, or the appripriate error value.
  624. */
  625. PJ_DECL(pj_status_t)
  626. pjmedia_video_format_mgr_create(pj_pool_t *pool,
  627. unsigned max_fmt,
  628. unsigned options,
  629. pjmedia_video_format_mgr **p_mgr);
  630. /**
  631. * Get the singleton instance of the video format manager.
  632. *
  633. * @return The instance.
  634. */
  635. PJ_DECL(pjmedia_video_format_mgr*) pjmedia_video_format_mgr_instance(void);
  636. /**
  637. * Manually assign a specific video manager instance as the singleton
  638. * instance. Normally this is not needed if only one instance is ever
  639. * going to be created, as the library automatically assign the singleton
  640. * instance.
  641. *
  642. * @param mgr The instance to be used as the singleton instance.
  643. * Application may specify NULL to clear the singleton
  644. * singleton instance.
  645. */
  646. PJ_DECL(void)
  647. pjmedia_video_format_mgr_set_instance(pjmedia_video_format_mgr *mgr);
  648. /**
  649. * Retrieve a video format info for the specified format id.
  650. *
  651. * @param mgr The video format manager. Specify NULL to use
  652. * the singleton instance (however, a video format
  653. * manager still must have been created prior to
  654. * calling this function).
  655. * @param id The format id which format info is to be
  656. * retrieved.
  657. *
  658. * @return The video format info.
  659. */
  660. PJ_DECL(const pjmedia_video_format_info*)
  661. pjmedia_get_video_format_info(pjmedia_video_format_mgr *mgr,
  662. pj_uint32_t id);
  663. /**
  664. * Register a new video format to the framework. By default, built-in
  665. * formats will be registered automatically to the format manager when
  666. * it is created (note: built-in formats are ones which format id is
  667. * listed in pjmedia_format_id enumeration). This function allows
  668. * application to use user defined format id by registering that format
  669. * into the framework.
  670. *
  671. * @param mgr The video format manager. Specify NULL to use
  672. * the singleton instance (however, a video format
  673. * manager still must have been created prior to
  674. * calling this function).
  675. * @param vfi The video format info to be registered. This
  676. * structure must remain valid until the format
  677. * manager is destroyed.
  678. *
  679. * @return PJ_SUCCESS on success, or the appripriate error value.
  680. */
  681. PJ_DECL(pj_status_t)
  682. pjmedia_register_video_format_info(pjmedia_video_format_mgr *mgr,
  683. pjmedia_video_format_info *vfi);
  684. /**
  685. * Destroy a video format manager. If the manager happens to be the singleton
  686. * instance, the singleton instance will be set to NULL.
  687. *
  688. * @param mgr The video format manager. Specify NULL to use
  689. * the singleton instance (however, a video format
  690. * manager still must have been created prior to
  691. * calling this function).
  692. */
  693. PJ_DECL(void) pjmedia_video_format_mgr_destroy(pjmedia_video_format_mgr *mgr);
  694. /*****************************************************************************
  695. * FORMAT FUNCTION HELPER:
  696. */
  697. /**
  698. * Fill video frame buffer with black color.
  699. *
  700. * @param fmt The video format.
  701. * @param buf The frame buffer.
  702. * @param buf_size The frame buffer size.
  703. *
  704. * @return PJ_SUCCESS if successfull.
  705. */
  706. PJ_DECL(pj_status_t) pjmedia_video_format_fill_black(const pjmedia_format *fmt,
  707. void *buf,
  708. pj_size_t buf_size);
  709. PJ_END_DECL
  710. /**
  711. * @}
  712. */
  713. #endif /* __PJMEDIA_FORMAT_H__ */