audio_data.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/mem.h"
  22. #include "audio_data.h"
  23. static const AVClass audio_data_class = {
  24. .class_name = "AudioData",
  25. .item_name = av_default_item_name,
  26. .version = LIBAVUTIL_VERSION_INT,
  27. };
  28. /*
  29. * Calculate alignment for data pointers.
  30. */
  31. static void calc_ptr_alignment(AudioData *a)
  32. {
  33. int p;
  34. int min_align = 128;
  35. for (p = 0; p < a->planes; p++) {
  36. int cur_align = 128;
  37. while ((intptr_t)a->data[p] % cur_align)
  38. cur_align >>= 1;
  39. if (cur_align < min_align)
  40. min_align = cur_align;
  41. }
  42. a->ptr_align = min_align;
  43. }
  44. int ff_audio_data_set_channels(AudioData *a, int channels)
  45. {
  46. if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS ||
  47. channels > a->allocated_channels)
  48. return AVERROR(EINVAL);
  49. a->channels = channels;
  50. a->planes = a->is_planar ? channels : 1;
  51. calc_ptr_alignment(a);
  52. return 0;
  53. }
  54. int ff_audio_data_init(AudioData *a, void **src, int plane_size, int channels,
  55. int nb_samples, enum AVSampleFormat sample_fmt,
  56. int read_only, const char *name)
  57. {
  58. int p;
  59. memset(a, 0, sizeof(*a));
  60. a->class = &audio_data_class;
  61. if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {
  62. av_log(a, AV_LOG_ERROR, "invalid channel count: %d\n", channels);
  63. return AVERROR(EINVAL);
  64. }
  65. a->sample_size = av_get_bytes_per_sample(sample_fmt);
  66. if (!a->sample_size) {
  67. av_log(a, AV_LOG_ERROR, "invalid sample format\n");
  68. return AVERROR(EINVAL);
  69. }
  70. a->is_planar = av_sample_fmt_is_planar(sample_fmt);
  71. a->planes = a->is_planar ? channels : 1;
  72. a->stride = a->sample_size * (a->is_planar ? 1 : channels);
  73. for (p = 0; p < (a->is_planar ? channels : 1); p++) {
  74. if (!src[p]) {
  75. av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\n", p);
  76. return AVERROR(EINVAL);
  77. }
  78. a->data[p] = src[p];
  79. }
  80. a->allocated_samples = nb_samples * !read_only;
  81. a->nb_samples = nb_samples;
  82. a->sample_fmt = sample_fmt;
  83. a->channels = channels;
  84. a->allocated_channels = channels;
  85. a->read_only = read_only;
  86. a->allow_realloc = 0;
  87. a->name = name ? name : "{no name}";
  88. calc_ptr_alignment(a);
  89. a->samples_align = plane_size / a->stride;
  90. return 0;
  91. }
  92. AudioData *ff_audio_data_alloc(int channels, int nb_samples,
  93. enum AVSampleFormat sample_fmt, const char *name)
  94. {
  95. AudioData *a;
  96. int ret;
  97. if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS)
  98. return NULL;
  99. a = av_mallocz(sizeof(*a));
  100. if (!a)
  101. return NULL;
  102. a->sample_size = av_get_bytes_per_sample(sample_fmt);
  103. if (!a->sample_size) {
  104. av_free(a);
  105. return NULL;
  106. }
  107. a->is_planar = av_sample_fmt_is_planar(sample_fmt);
  108. a->planes = a->is_planar ? channels : 1;
  109. a->stride = a->sample_size * (a->is_planar ? 1 : channels);
  110. a->class = &audio_data_class;
  111. a->sample_fmt = sample_fmt;
  112. a->channels = channels;
  113. a->allocated_channels = channels;
  114. a->read_only = 0;
  115. a->allow_realloc = 1;
  116. a->name = name ? name : "{no name}";
  117. if (nb_samples > 0) {
  118. ret = ff_audio_data_realloc(a, nb_samples);
  119. if (ret < 0) {
  120. av_free(a);
  121. return NULL;
  122. }
  123. return a;
  124. } else {
  125. calc_ptr_alignment(a);
  126. return a;
  127. }
  128. }
  129. int ff_audio_data_realloc(AudioData *a, int nb_samples)
  130. {
  131. int ret, new_buf_size, plane_size, p;
  132. /* check if buffer is already large enough */
  133. if (a->allocated_samples >= nb_samples)
  134. return 0;
  135. /* validate that the output is not read-only and realloc is allowed */
  136. if (a->read_only || !a->allow_realloc)
  137. return AVERROR(EINVAL);
  138. new_buf_size = av_samples_get_buffer_size(&plane_size,
  139. a->allocated_channels, nb_samples,
  140. a->sample_fmt, 0);
  141. if (new_buf_size < 0)
  142. return new_buf_size;
  143. /* if there is already data in the buffer and the sample format is planar,
  144. allocate a new buffer and copy the data, otherwise just realloc the
  145. internal buffer and set new data pointers */
  146. if (a->nb_samples > 0 && a->is_planar) {
  147. uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };
  148. ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,
  149. nb_samples, a->sample_fmt, 0);
  150. if (ret < 0)
  151. return ret;
  152. for (p = 0; p < a->planes; p++)
  153. memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);
  154. av_freep(&a->buffer);
  155. memcpy(a->data, new_data, sizeof(new_data));
  156. a->buffer = a->data[0];
  157. } else {
  158. av_freep(&a->buffer);
  159. a->buffer = av_malloc(new_buf_size);
  160. if (!a->buffer)
  161. return AVERROR(ENOMEM);
  162. ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,
  163. a->allocated_channels, nb_samples,
  164. a->sample_fmt, 0);
  165. if (ret < 0)
  166. return ret;
  167. }
  168. a->buffer_size = new_buf_size;
  169. a->allocated_samples = nb_samples;
  170. calc_ptr_alignment(a);
  171. a->samples_align = plane_size / a->stride;
  172. return 0;
  173. }
  174. void ff_audio_data_free(AudioData **a)
  175. {
  176. if (!*a)
  177. return;
  178. av_free((*a)->buffer);
  179. av_freep(a);
  180. }
  181. int ff_audio_data_copy(AudioData *dst, AudioData *src)
  182. {
  183. int ret, p;
  184. /* validate input/output compatibility */
  185. if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
  186. return AVERROR(EINVAL);
  187. /* if the input is empty, just empty the output */
  188. if (!src->nb_samples) {
  189. dst->nb_samples = 0;
  190. return 0;
  191. }
  192. /* reallocate output if necessary */
  193. ret = ff_audio_data_realloc(dst, src->nb_samples);
  194. if (ret < 0)
  195. return ret;
  196. /* copy data */
  197. for (p = 0; p < src->planes; p++)
  198. memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
  199. dst->nb_samples = src->nb_samples;
  200. return 0;
  201. }
  202. int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
  203. int src_offset, int nb_samples)
  204. {
  205. int ret, p, dst_offset2, dst_move_size;
  206. /* validate input/output compatibility */
  207. if (dst->sample_fmt != src->sample_fmt || dst->channels != src->channels) {
  208. av_log(src, AV_LOG_ERROR, "sample format mismatch\n");
  209. return AVERROR(EINVAL);
  210. }
  211. /* validate offsets are within the buffer bounds */
  212. if (dst_offset < 0 || dst_offset > dst->nb_samples ||
  213. src_offset < 0 || src_offset > src->nb_samples) {
  214. av_log(src, AV_LOG_ERROR, "offset out-of-bounds: src=%d dst=%d\n",
  215. src_offset, dst_offset);
  216. return AVERROR(EINVAL);
  217. }
  218. /* check offsets and sizes to see if we can just do nothing and return */
  219. if (nb_samples > src->nb_samples - src_offset)
  220. nb_samples = src->nb_samples - src_offset;
  221. if (nb_samples <= 0)
  222. return 0;
  223. /* validate that the output is not read-only */
  224. if (dst->read_only) {
  225. av_log(dst, AV_LOG_ERROR, "dst is read-only\n");
  226. return AVERROR(EINVAL);
  227. }
  228. /* reallocate output if necessary */
  229. ret = ff_audio_data_realloc(dst, dst->nb_samples + nb_samples);
  230. if (ret < 0) {
  231. av_log(dst, AV_LOG_ERROR, "error reallocating dst\n");
  232. return ret;
  233. }
  234. dst_offset2 = dst_offset + nb_samples;
  235. dst_move_size = dst->nb_samples - dst_offset;
  236. for (p = 0; p < src->planes; p++) {
  237. if (dst_move_size > 0) {
  238. memmove(dst->data[p] + dst_offset2 * dst->stride,
  239. dst->data[p] + dst_offset * dst->stride,
  240. dst_move_size * dst->stride);
  241. }
  242. memcpy(dst->data[p] + dst_offset * dst->stride,
  243. src->data[p] + src_offset * src->stride,
  244. nb_samples * src->stride);
  245. }
  246. dst->nb_samples += nb_samples;
  247. return 0;
  248. }
  249. void ff_audio_data_drain(AudioData *a, int nb_samples)
  250. {
  251. if (a->nb_samples <= nb_samples) {
  252. /* drain the whole buffer */
  253. a->nb_samples = 0;
  254. } else {
  255. int p;
  256. int move_offset = a->stride * nb_samples;
  257. int move_size = a->stride * (a->nb_samples - nb_samples);
  258. for (p = 0; p < a->planes; p++)
  259. memmove(a->data[p], a->data[p] + move_offset, move_size);
  260. a->nb_samples -= nb_samples;
  261. }
  262. }
  263. int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
  264. int nb_samples)
  265. {
  266. uint8_t *offset_data[AVRESAMPLE_MAX_CHANNELS];
  267. int offset_size, p;
  268. if (offset >= a->nb_samples)
  269. return 0;
  270. offset_size = offset * a->stride;
  271. for (p = 0; p < a->planes; p++)
  272. offset_data[p] = a->data[p] + offset_size;
  273. return av_audio_fifo_write(af, (void **)offset_data, nb_samples);
  274. }
  275. int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
  276. {
  277. int ret;
  278. if (a->read_only)
  279. return AVERROR(EINVAL);
  280. ret = ff_audio_data_realloc(a, nb_samples);
  281. if (ret < 0)
  282. return ret;
  283. ret = av_audio_fifo_read(af, (void **)a->data, nb_samples);
  284. if (ret >= 0)
  285. a->nb_samples = ret;
  286. return ret;
  287. }