audio_data.c 11 KB

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