resample.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_RESAMPLE_H__
  20. #define __PJMEDIA_RESAMPLE_H__
  21. /**
  22. * @file resample.h
  23. * @brief Sample rate converter.
  24. */
  25. #include <pjmedia/types.h>
  26. #include <pjmedia/port.h>
  27. /**
  28. * @defgroup PJMEDIA_RESAMPLE Resampling Algorithm
  29. * @ingroup PJMEDIA_FRAME_OP
  30. * @brief Sample rate conversion algorithm
  31. * @{
  32. *
  33. * This section describes the base resampling functions. In addition to this,
  34. * application can use the @ref PJMEDIA_RESAMPLE_PORT which provides
  35. * media port abstraction for the base resampling algorithm.
  36. */
  37. PJ_BEGIN_DECL
  38. /*
  39. * This file declares two types of API:
  40. *
  41. * Application can use #pjmedia_resample_create() and #pjmedia_resample_run()
  42. * to convert a frame from source rate to destination rate. The inpuit frame
  43. * must have a constant length.
  44. *
  45. * Alternatively, application can create a resampling port with
  46. * #pjmedia_resample_port_create() and connect the port to other ports to
  47. * change the sampling rate of the samples.
  48. */
  49. /**
  50. * Opaque resample session.
  51. */
  52. typedef struct pjmedia_resample pjmedia_resample;
  53. /**
  54. * Create a frame based resample session.
  55. *
  56. * @param pool Pool to allocate the structure and buffers.
  57. * @param high_quality If true, then high quality conversion will be
  58. * used, at the expense of more CPU and memory,
  59. * because temporary buffer needs to be created.
  60. * @param large_filter If true, large filter size will be used.
  61. * @param channel_count Number of channels.
  62. * @param rate_in Clock rate of the input samples.
  63. * @param rate_out Clock rate of the output samples.
  64. * @param samples_per_frame Number of samples per frame in the input.
  65. * @param p_resample Pointer to receive the resample session.
  66. *
  67. * @return PJ_SUCCESS on success.
  68. */
  69. PJ_DECL(pj_status_t) pjmedia_resample_create(pj_pool_t *pool,
  70. pj_bool_t high_quality,
  71. pj_bool_t large_filter,
  72. unsigned channel_count,
  73. unsigned rate_in,
  74. unsigned rate_out,
  75. unsigned samples_per_frame,
  76. pjmedia_resample **p_resample);
  77. /**
  78. * Use the resample session to resample a frame. The frame must have the
  79. * same size and settings as the resample session, or otherwise the
  80. * behavior is undefined.
  81. *
  82. * @param resample The resample session.
  83. * @param input Buffer containing the input samples.
  84. * @param output Buffer to store the output samples.
  85. */
  86. PJ_DECL(void) pjmedia_resample_run( pjmedia_resample *resample,
  87. const pj_int16_t *input,
  88. pj_int16_t *output );
  89. /**
  90. * Get the input frame size of a resample session.
  91. *
  92. * @param resample The resample session.
  93. *
  94. * @return The frame size, in number of samples.
  95. */
  96. PJ_DECL(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample);
  97. /**
  98. * Destroy the resample.
  99. *
  100. * @param resample The resample session.
  101. */
  102. PJ_DECL(void) pjmedia_resample_destroy(pjmedia_resample *resample);
  103. /**
  104. * @}
  105. */
  106. /**
  107. * @defgroup PJMEDIA_RESAMPLE_PORT Resample Port
  108. * @ingroup PJMEDIA_PORT
  109. * @brief Audio sample rate conversion
  110. * @{
  111. *
  112. * This section describes media port abstraction for @ref PJMEDIA_RESAMPLE.
  113. */
  114. /**
  115. * Option flags that can be specified when creating resample port.
  116. */
  117. enum pjmedia_resample_port_options
  118. {
  119. /**
  120. * Do not use high quality resampling algorithm, but use linear
  121. * algorithm instead.
  122. */
  123. PJMEDIA_RESAMPLE_USE_LINEAR = 1,
  124. /**
  125. * Use small filter workspace when high quality resampling is
  126. * used.
  127. */
  128. PJMEDIA_RESAMPLE_USE_SMALL_FILTER = 2,
  129. /**
  130. * Do not destroy downstream port when resample port is destroyed.
  131. */
  132. PJMEDIA_RESAMPLE_DONT_DESTROY_DN = 4
  133. };
  134. /**
  135. * Create a resample port. This creates a bidirectional resample session,
  136. * which will resample frames when the port's get_frame() and put_frame()
  137. * is called.
  138. *
  139. * When the resample port's get_frame() is called, this port will get
  140. * a frame from the downstream port and resample the frame to the target
  141. * clock rate before returning it to the caller.
  142. *
  143. * When the resample port's put_frame() is called, this port will resample
  144. * the frame to the downstream port's clock rate before giving the frame
  145. * to the downstream port.
  146. *
  147. * @param pool Pool to allocate the structure and buffers.
  148. * @param dn_port The downstream port, which clock rate is to
  149. * be converted to the target clock rate.
  150. * @param clock_rate Target clock rate.
  151. * @param options Flags from #pjmedia_resample_port_options.
  152. * When this flag is zero, the default behavior
  153. * is to use high quality resampling with
  154. * large filter, and to destroy downstream port
  155. * when resample port is destroyed.
  156. * @param p_port Pointer to receive the resample port instance.
  157. *
  158. * @return PJ_SUCCESS on success.
  159. */
  160. PJ_DECL(pj_status_t) pjmedia_resample_port_create( pj_pool_t *pool,
  161. pjmedia_port *dn_port,
  162. unsigned clock_rate,
  163. unsigned options,
  164. pjmedia_port **p_port );
  165. PJ_END_DECL
  166. /**
  167. * @}
  168. */
  169. #endif /* __PJMEDIA_RESAMPLE_H__ */