swresample_frame.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ((ret = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
  32. goto fail;
  33. if ((ret = av_opt_set_chlayout(s, "ichl", &ch_layout, 0)) < 0)
  34. goto fail;
  35. if ((ret = av_opt_set_int(s, "isf", in->format, 0)) < 0)
  36. goto fail;
  37. if ((ret = av_opt_set_int(s, "isr", in->sample_rate, 0)) < 0)
  38. goto fail;
  39. }
  40. if (out) {
  41. if ((ret = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
  42. goto fail;
  43. if ((ret = av_opt_set_chlayout(s, "ochl", &ch_layout, 0)) < 0)
  44. goto fail;
  45. if ((ret = av_opt_set_int(s, "osf", out->format, 0)) < 0)
  46. goto fail;
  47. if ((ret = av_opt_set_int(s, "osr", out->sample_rate, 0)) < 0)
  48. goto fail;
  49. }
  50. ret = 0;
  51. fail:
  52. if (ret < 0)
  53. av_log(s, AV_LOG_ERROR, "Failed to set option\n");
  54. av_channel_layout_uninit(&ch_layout);
  55. return ret;
  56. }
  57. static int config_changed(SwrContext *s,
  58. const AVFrame *out, const AVFrame *in)
  59. {
  60. AVChannelLayout ch_layout = { 0 };
  61. int ret = 0, err;
  62. if (in) {
  63. if ((err = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
  64. return err;
  65. if (av_channel_layout_compare(&s->in_ch_layout, &ch_layout) ||
  66. s->in_sample_rate != in->sample_rate ||
  67. s->in_sample_fmt != in->format) {
  68. ret |= AVERROR_INPUT_CHANGED;
  69. }
  70. }
  71. if (out) {
  72. if ((err = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
  73. return err;
  74. if (av_channel_layout_compare(&s->out_ch_layout, &ch_layout) ||
  75. s->out_sample_rate != out->sample_rate ||
  76. s->out_sample_fmt != out->format) {
  77. ret |= AVERROR_OUTPUT_CHANGED;
  78. }
  79. }
  80. av_channel_layout_uninit(&ch_layout);
  81. return ret;
  82. }
  83. static inline int convert_frame(SwrContext *s,
  84. AVFrame *out, const AVFrame *in)
  85. {
  86. int ret;
  87. uint8_t **out_data = NULL;
  88. const uint8_t **in_data = NULL;
  89. int out_nb_samples = 0, in_nb_samples = 0;
  90. if (out) {
  91. out_data = out->extended_data;
  92. out_nb_samples = out->nb_samples;
  93. }
  94. if (in) {
  95. in_data = (const uint8_t **)in->extended_data;
  96. in_nb_samples = in->nb_samples;
  97. }
  98. ret = swr_convert(s, out_data, out_nb_samples, in_data, in_nb_samples);
  99. if (ret < 0) {
  100. if (out)
  101. out->nb_samples = 0;
  102. return ret;
  103. }
  104. if (out)
  105. out->nb_samples = ret;
  106. return 0;
  107. }
  108. static inline int available_samples(AVFrame *out)
  109. {
  110. int bytes_per_sample = av_get_bytes_per_sample(out->format);
  111. int samples = out->linesize[0] / bytes_per_sample;
  112. if (av_sample_fmt_is_planar(out->format)) {
  113. return samples;
  114. } else {
  115. int channels = out->ch_layout.nb_channels;
  116. return samples / channels;
  117. }
  118. }
  119. int swr_convert_frame(SwrContext *s,
  120. AVFrame *out, const AVFrame *in)
  121. {
  122. int ret, setup = 0;
  123. if (!swr_is_initialized(s)) {
  124. if ((ret = swr_config_frame(s, out, in)) < 0)
  125. return ret;
  126. if ((ret = swr_init(s)) < 0)
  127. return ret;
  128. setup = 1;
  129. } else {
  130. // return as is or reconfigure for input changes?
  131. if ((ret = config_changed(s, out, in)))
  132. return ret;
  133. }
  134. if (out) {
  135. if (!out->linesize[0]) {
  136. out->nb_samples = swr_get_delay(s, s->out_sample_rate) + 3;
  137. if (in) {
  138. out->nb_samples += in->nb_samples*(int64_t)s->out_sample_rate / s->in_sample_rate;
  139. }
  140. if ((ret = av_frame_get_buffer(out, 0)) < 0) {
  141. if (setup)
  142. swr_close(s);
  143. return ret;
  144. }
  145. } else {
  146. if (!out->nb_samples)
  147. out->nb_samples = available_samples(out);
  148. }
  149. }
  150. return convert_frame(s, out, in);
  151. }