samplefmt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "common.h"
  19. #include "samplefmt.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. typedef struct SampleFmtInfo {
  24. char name[8];
  25. int bits;
  26. int planar;
  27. enum AVSampleFormat altform; ///< planar<->packed alternative form
  28. } SampleFmtInfo;
  29. /** this table gives more information about formats */
  30. static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
  31. [AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0, .altform = AV_SAMPLE_FMT_U8P },
  32. [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0, .altform = AV_SAMPLE_FMT_S16P },
  33. [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_S32P },
  34. [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_FLTP },
  35. [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_DBLP },
  36. [AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1, .altform = AV_SAMPLE_FMT_U8 },
  37. [AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1, .altform = AV_SAMPLE_FMT_S16 },
  38. [AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_S32 },
  39. [AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_FLT },
  40. [AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_DBL },
  41. };
  42. const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
  43. {
  44. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  45. return NULL;
  46. return sample_fmt_info[sample_fmt].name;
  47. }
  48. enum AVSampleFormat av_get_sample_fmt(const char *name)
  49. {
  50. int i;
  51. for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
  52. if (!strcmp(sample_fmt_info[i].name, name))
  53. return i;
  54. return AV_SAMPLE_FMT_NONE;
  55. }
  56. enum AVSampleFormat av_get_alt_sample_fmt(enum AVSampleFormat sample_fmt, int planar)
  57. {
  58. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  59. return AV_SAMPLE_FMT_NONE;
  60. if (sample_fmt_info[sample_fmt].planar == planar)
  61. return sample_fmt;
  62. return sample_fmt_info[sample_fmt].altform;
  63. }
  64. enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
  65. {
  66. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  67. return AV_SAMPLE_FMT_NONE;
  68. if (sample_fmt_info[sample_fmt].planar)
  69. return sample_fmt_info[sample_fmt].altform;
  70. return sample_fmt;
  71. }
  72. enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
  73. {
  74. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  75. return AV_SAMPLE_FMT_NONE;
  76. if (sample_fmt_info[sample_fmt].planar)
  77. return sample_fmt;
  78. return sample_fmt_info[sample_fmt].altform;
  79. }
  80. char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
  81. {
  82. /* print header */
  83. if (sample_fmt < 0)
  84. snprintf(buf, buf_size, "name " " depth");
  85. else if (sample_fmt < AV_SAMPLE_FMT_NB) {
  86. SampleFmtInfo info = sample_fmt_info[sample_fmt];
  87. snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
  88. }
  89. return buf;
  90. }
  91. int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
  92. {
  93. return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
  94. 0 : sample_fmt_info[sample_fmt].bits >> 3;
  95. }
  96. #if FF_API_GET_BITS_PER_SAMPLE_FMT
  97. int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
  98. {
  99. return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
  100. 0 : sample_fmt_info[sample_fmt].bits;
  101. }
  102. #endif
  103. int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
  104. {
  105. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  106. return 0;
  107. return sample_fmt_info[sample_fmt].planar;
  108. }
  109. int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
  110. enum AVSampleFormat sample_fmt, int align)
  111. {
  112. int line_size;
  113. int sample_size = av_get_bytes_per_sample(sample_fmt);
  114. int planar = av_sample_fmt_is_planar(sample_fmt);
  115. /* validate parameter ranges */
  116. if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
  117. return AVERROR(EINVAL);
  118. /* auto-select alignment if not specified */
  119. if (!align) {
  120. if (nb_samples > INT_MAX - 31)
  121. return AVERROR(EINVAL);
  122. align = 1;
  123. nb_samples = FFALIGN(nb_samples, 32);
  124. }
  125. /* check for integer overflow */
  126. if (nb_channels > INT_MAX / align ||
  127. (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
  128. return AVERROR(EINVAL);
  129. line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
  130. FFALIGN(nb_samples * sample_size * nb_channels, align);
  131. if (linesize)
  132. *linesize = line_size;
  133. return planar ? line_size * nb_channels : line_size;
  134. }
  135. int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
  136. const uint8_t *buf, int nb_channels, int nb_samples,
  137. enum AVSampleFormat sample_fmt, int align)
  138. {
  139. int ch, planar, buf_size, line_size;
  140. planar = av_sample_fmt_is_planar(sample_fmt);
  141. buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,
  142. sample_fmt, align);
  143. if (buf_size < 0)
  144. return buf_size;
  145. audio_data[0] = (uint8_t *)buf;
  146. for (ch = 1; planar && ch < nb_channels; ch++)
  147. audio_data[ch] = audio_data[ch-1] + line_size;
  148. if (linesize)
  149. *linesize = line_size;
  150. #if FF_API_SAMPLES_UTILS_RETURN_ZERO
  151. return 0;
  152. #else
  153. return buf_size;
  154. #endif
  155. }
  156. int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
  157. int nb_samples, enum AVSampleFormat sample_fmt, int align)
  158. {
  159. uint8_t *buf;
  160. int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
  161. sample_fmt, align);
  162. if (size < 0)
  163. return size;
  164. buf = av_malloc(size);
  165. if (!buf)
  166. return AVERROR(ENOMEM);
  167. size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
  168. nb_samples, sample_fmt, align);
  169. if (size < 0) {
  170. av_free(buf);
  171. return size;
  172. }
  173. av_samples_set_silence(audio_data, 0, nb_samples, nb_channels, sample_fmt);
  174. #if FF_API_SAMPLES_UTILS_RETURN_ZERO
  175. return 0;
  176. #else
  177. return size;
  178. #endif
  179. }
  180. int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
  181. int src_offset, int nb_samples, int nb_channels,
  182. enum AVSampleFormat sample_fmt)
  183. {
  184. int planar = av_sample_fmt_is_planar(sample_fmt);
  185. int planes = planar ? nb_channels : 1;
  186. int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
  187. int data_size = nb_samples * block_align;
  188. int i;
  189. dst_offset *= block_align;
  190. src_offset *= block_align;
  191. if((dst[0] < src[0] ? src[0] - dst[0] : dst[0] - src[0]) >= data_size) {
  192. for (i = 0; i < planes; i++)
  193. memcpy(dst[i] + dst_offset, src[i] + src_offset, data_size);
  194. } else {
  195. for (i = 0; i < planes; i++)
  196. memmove(dst[i] + dst_offset, src[i] + src_offset, data_size);
  197. }
  198. return 0;
  199. }
  200. int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
  201. int nb_channels, enum AVSampleFormat sample_fmt)
  202. {
  203. int planar = av_sample_fmt_is_planar(sample_fmt);
  204. int planes = planar ? nb_channels : 1;
  205. int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
  206. int data_size = nb_samples * block_align;
  207. int fill_char = (sample_fmt == AV_SAMPLE_FMT_U8 ||
  208. sample_fmt == AV_SAMPLE_FMT_U8P) ? 0x80 : 0x00;
  209. int i;
  210. offset *= block_align;
  211. for (i = 0; i < planes; i++)
  212. memset(audio_data[i] + offset, fill_char, data_size);
  213. return 0;
  214. }