audio_data.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef AVRESAMPLE_AUDIO_DATA_H
  21. #define AVRESAMPLE_AUDIO_DATA_H
  22. #include <stdint.h>
  23. #include "libavutil/audio_fifo.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/samplefmt.h"
  26. #include "avresample.h"
  27. #include "internal.h"
  28. int ff_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels);
  29. /**
  30. * Audio buffer used for intermediate storage between conversion phases.
  31. */
  32. struct AudioData {
  33. const AVClass *class; /**< AVClass for logging */
  34. uint8_t *data[AVRESAMPLE_MAX_CHANNELS]; /**< data plane pointers */
  35. uint8_t *buffer; /**< data buffer */
  36. unsigned int buffer_size; /**< allocated buffer size */
  37. int allocated_samples; /**< number of samples the buffer can hold */
  38. int nb_samples; /**< current number of samples */
  39. enum AVSampleFormat sample_fmt; /**< sample format */
  40. int channels; /**< channel count */
  41. int allocated_channels; /**< allocated channel count */
  42. int is_planar; /**< sample format is planar */
  43. int planes; /**< number of data planes */
  44. int sample_size; /**< bytes per sample */
  45. int stride; /**< sample byte offset within a plane */
  46. int read_only; /**< data is read-only */
  47. int allow_realloc; /**< realloc is allowed */
  48. int ptr_align; /**< minimum data pointer alignment */
  49. int samples_align; /**< allocated samples alignment */
  50. const char *name; /**< name for debug logging */
  51. };
  52. int ff_audio_data_set_channels(AudioData *a, int channels);
  53. /**
  54. * Initialize AudioData using a given source.
  55. *
  56. * This does not allocate an internal buffer. It only sets the data pointers
  57. * and audio parameters.
  58. *
  59. * @param a AudioData struct
  60. * @param src source data pointers
  61. * @param plane_size plane size, in bytes.
  62. * This can be 0 if unknown, but that will lead to
  63. * optimized functions not being used in many cases,
  64. * which could slow down some conversions.
  65. * @param channels channel count
  66. * @param nb_samples number of samples in the source data
  67. * @param sample_fmt sample format
  68. * @param read_only indicates if buffer is read only or read/write
  69. * @param name name for debug logging (can be NULL)
  70. * @return 0 on success, negative AVERROR value on error
  71. */
  72. int ff_audio_data_init(AudioData *a, uint8_t * const *src, int plane_size,
  73. int channels, int nb_samples,
  74. enum AVSampleFormat sample_fmt, int read_only,
  75. const char *name);
  76. /**
  77. * Allocate AudioData.
  78. *
  79. * This allocates an internal buffer and sets audio parameters.
  80. *
  81. * @param channels channel count
  82. * @param nb_samples number of samples to allocate space for
  83. * @param sample_fmt sample format
  84. * @param name name for debug logging (can be NULL)
  85. * @return newly allocated AudioData struct, or NULL on error
  86. */
  87. AudioData *ff_audio_data_alloc(int channels, int nb_samples,
  88. enum AVSampleFormat sample_fmt,
  89. const char *name);
  90. /**
  91. * Reallocate AudioData.
  92. *
  93. * The AudioData must have been previously allocated with ff_audio_data_alloc().
  94. *
  95. * @param a AudioData struct
  96. * @param nb_samples number of samples to allocate space for
  97. * @return 0 on success, negative AVERROR value on error
  98. */
  99. int ff_audio_data_realloc(AudioData *a, int nb_samples);
  100. /**
  101. * Free AudioData.
  102. *
  103. * The AudioData must have been previously allocated with ff_audio_data_alloc().
  104. *
  105. * @param a AudioData struct
  106. */
  107. void ff_audio_data_free(AudioData **a);
  108. /**
  109. * Copy data from one AudioData to another.
  110. *
  111. * @param out output AudioData
  112. * @param in input AudioData
  113. * @param map channel map, NULL if not remapping
  114. * @return 0 on success, negative AVERROR value on error
  115. */
  116. int ff_audio_data_copy(AudioData *out, AudioData *in, ChannelMapInfo *map);
  117. /**
  118. * Append data from one AudioData to the end of another.
  119. *
  120. * @param dst destination AudioData
  121. * @param dst_offset offset, in samples, to start writing, relative to the
  122. * start of dst
  123. * @param src source AudioData
  124. * @param src_offset offset, in samples, to start copying, relative to the
  125. * start of the src
  126. * @param nb_samples number of samples to copy
  127. * @return 0 on success, negative AVERROR value on error
  128. */
  129. int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
  130. int src_offset, int nb_samples);
  131. /**
  132. * Drain samples from the start of the AudioData.
  133. *
  134. * Remaining samples are shifted to the start of the AudioData.
  135. *
  136. * @param a AudioData struct
  137. * @param nb_samples number of samples to drain
  138. */
  139. void ff_audio_data_drain(AudioData *a, int nb_samples);
  140. /**
  141. * Add samples in AudioData to an AVAudioFifo.
  142. *
  143. * @param af Audio FIFO Buffer
  144. * @param a AudioData struct
  145. * @param offset number of samples to skip from the start of the data
  146. * @param nb_samples number of samples to add to the FIFO
  147. * @return number of samples actually added to the FIFO, or
  148. * negative AVERROR code on error
  149. */
  150. int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
  151. int nb_samples);
  152. /**
  153. * Read samples from an AVAudioFifo to AudioData.
  154. *
  155. * @param af Audio FIFO Buffer
  156. * @param a AudioData struct
  157. * @param nb_samples number of samples to read from the FIFO
  158. * @return number of samples actually read from the FIFO, or
  159. * negative AVERROR code on error
  160. */
  161. int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples);
  162. #endif /* AVRESAMPLE_AUDIO_DATA_H */