swresample_frame.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2014 Luca Barbato <lu_zero@gentoo.org>
  3. * Copyright (c) 2014 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "swresample_internal.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/frame.h"
  24. #include "libavutil/opt.h"
  25. int swr_config_frame(SwrContext *s, const AVFrame *out, const AVFrame *in)
  26. {
  27. AVChannelLayout ch_layout = { 0 };
  28. int ret;
  29. swr_close(s);
  30. if (in) {
  31. #if FF_API_OLD_CHANNEL_LAYOUT
  32. FF_DISABLE_DEPRECATION_WARNINGS
  33. // if the old/new fields are set inconsistently, prefer the old ones
  34. if ((in->channel_layout && (in->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
  35. in->ch_layout.u.mask != in->channel_layout))) {
  36. av_channel_layout_from_mask(&ch_layout, in->channel_layout);
  37. FF_ENABLE_DEPRECATION_WARNINGS
  38. } else
  39. #endif
  40. if ((ret = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
  41. goto fail;
  42. if ((ret = av_opt_set_chlayout(s, "ichl", &ch_layout, 0)) < 0)
  43. goto fail;
  44. if ((ret = av_opt_set_int(s, "isf", in->format, 0)) < 0)
  45. goto fail;
  46. if ((ret = av_opt_set_int(s, "isr", in->sample_rate, 0)) < 0)
  47. goto fail;
  48. }
  49. if (out) {
  50. #if FF_API_OLD_CHANNEL_LAYOUT
  51. FF_DISABLE_DEPRECATION_WARNINGS
  52. // if the old/new fields are set inconsistently, prefer the old ones
  53. if ((out->channel_layout && (out->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
  54. out->ch_layout.u.mask != out->channel_layout))) {
  55. av_channel_layout_uninit(&ch_layout);
  56. av_channel_layout_from_mask(&ch_layout, out->channel_layout);
  57. FF_ENABLE_DEPRECATION_WARNINGS
  58. } else
  59. #endif
  60. if ((ret = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
  61. goto fail;
  62. if ((ret = av_opt_set_chlayout(s, "ochl", &ch_layout, 0)) < 0)
  63. goto fail;
  64. if ((ret = av_opt_set_int(s, "osf", out->format, 0)) < 0)
  65. goto fail;
  66. if ((ret = av_opt_set_int(s, "osr", out->sample_rate, 0)) < 0)
  67. goto fail;
  68. }
  69. ret = 0;
  70. fail:
  71. if (ret < 0)
  72. av_log(s, AV_LOG_ERROR, "Failed to set option\n");
  73. av_channel_layout_uninit(&ch_layout);
  74. return ret;
  75. }
  76. static int config_changed(SwrContext *s,
  77. const AVFrame *out, const AVFrame *in)
  78. {
  79. AVChannelLayout ch_layout = { 0 };
  80. int ret = 0, err;
  81. if (in) {
  82. #if FF_API_OLD_CHANNEL_LAYOUT
  83. FF_DISABLE_DEPRECATION_WARNINGS
  84. // if the old/new fields are set inconsistently, prefer the old ones
  85. if ((in->channel_layout && (in->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
  86. in->ch_layout.u.mask != in->channel_layout))) {
  87. av_channel_layout_from_mask(&ch_layout, in->channel_layout);
  88. FF_ENABLE_DEPRECATION_WARNINGS
  89. } else
  90. #endif
  91. if ((err = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
  92. return err;
  93. if (av_channel_layout_compare(&s->in_ch_layout, &ch_layout) ||
  94. s->in_sample_rate != in->sample_rate ||
  95. s->in_sample_fmt != in->format) {
  96. ret |= AVERROR_INPUT_CHANGED;
  97. }
  98. }
  99. if (out) {
  100. #if FF_API_OLD_CHANNEL_LAYOUT
  101. FF_DISABLE_DEPRECATION_WARNINGS
  102. // if the old/new fields are set inconsistently, prefer the old ones
  103. if ((out->channel_layout && (out->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
  104. out->ch_layout.u.mask != out->channel_layout))) {
  105. av_channel_layout_uninit(&ch_layout);
  106. av_channel_layout_from_mask(&ch_layout, out->channel_layout);
  107. FF_ENABLE_DEPRECATION_WARNINGS
  108. } else
  109. #endif
  110. if ((err = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
  111. return err;
  112. if (av_channel_layout_compare(&s->out_ch_layout, &ch_layout) ||
  113. s->out_sample_rate != out->sample_rate ||
  114. s->out_sample_fmt != out->format) {
  115. ret |= AVERROR_OUTPUT_CHANGED;
  116. }
  117. }
  118. av_channel_layout_uninit(&ch_layout);
  119. return ret;
  120. }
  121. static inline int convert_frame(SwrContext *s,
  122. AVFrame *out, const AVFrame *in)
  123. {
  124. int ret;
  125. uint8_t **out_data = NULL;
  126. const uint8_t **in_data = NULL;
  127. int out_nb_samples = 0, in_nb_samples = 0;
  128. if (out) {
  129. out_data = out->extended_data;
  130. out_nb_samples = out->nb_samples;
  131. }
  132. if (in) {
  133. in_data = (const uint8_t **)in->extended_data;
  134. in_nb_samples = in->nb_samples;
  135. }
  136. ret = swr_convert(s, out_data, out_nb_samples, in_data, in_nb_samples);
  137. if (ret < 0) {
  138. if (out)
  139. out->nb_samples = 0;
  140. return ret;
  141. }
  142. if (out)
  143. out->nb_samples = ret;
  144. return 0;
  145. }
  146. static inline int available_samples(AVFrame *out)
  147. {
  148. int bytes_per_sample = av_get_bytes_per_sample(out->format);
  149. int samples = out->linesize[0] / bytes_per_sample;
  150. if (av_sample_fmt_is_planar(out->format)) {
  151. return samples;
  152. } else {
  153. int channels;
  154. #if FF_API_OLD_CHANNEL_LAYOUT
  155. FF_DISABLE_DEPRECATION_WARNINGS
  156. channels = av_get_channel_layout_nb_channels(out->channel_layout);
  157. FF_ENABLE_DEPRECATION_WARNINGS
  158. if (!channels)
  159. #endif
  160. channels = out->ch_layout.nb_channels;
  161. return samples / channels;
  162. }
  163. }
  164. int swr_convert_frame(SwrContext *s,
  165. AVFrame *out, const AVFrame *in)
  166. {
  167. int ret, setup = 0;
  168. if (!swr_is_initialized(s)) {
  169. if ((ret = swr_config_frame(s, out, in)) < 0)
  170. return ret;
  171. if ((ret = swr_init(s)) < 0)
  172. return ret;
  173. setup = 1;
  174. } else {
  175. // return as is or reconfigure for input changes?
  176. if ((ret = config_changed(s, out, in)))
  177. return ret;
  178. }
  179. if (out) {
  180. if (!out->linesize[0]) {
  181. out->nb_samples = swr_get_delay(s, s->out_sample_rate) + 3;
  182. if (in) {
  183. out->nb_samples += in->nb_samples*(int64_t)s->out_sample_rate / s->in_sample_rate;
  184. }
  185. if ((ret = av_frame_get_buffer(out, 0)) < 0) {
  186. if (setup)
  187. swr_close(s);
  188. return ret;
  189. }
  190. } else {
  191. if (!out->nb_samples)
  192. out->nb_samples = available_samples(out);
  193. }
  194. }
  195. return convert_frame(s, out, in);
  196. }