You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.3KB

  1. /*
  2. * Audio Interleaving functions
  3. *
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/mathematics.h"
  24. #include "avformat.h"
  25. #include "audiointerleave.h"
  26. #include "internal.h"
  27. void ff_audio_interleave_close(AVFormatContext *s)
  28. {
  29. int i;
  30. for (i = 0; i < s->nb_streams; i++) {
  31. AVStream *st = s->streams[i];
  32. AudioInterleaveContext *aic = st->priv_data;
  33. if (aic && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  34. av_fifo_freep(&aic->fifo);
  35. }
  36. }
  37. int ff_audio_interleave_init(AVFormatContext *s,
  38. const int samples_per_frame,
  39. AVRational time_base)
  40. {
  41. int i;
  42. if (!time_base.num) {
  43. av_log(s, AV_LOG_ERROR, "timebase not set for audio interleave\n");
  44. return AVERROR(EINVAL);
  45. }
  46. for (i = 0; i < s->nb_streams; i++) {
  47. AVStream *st = s->streams[i];
  48. AudioInterleaveContext *aic = st->priv_data;
  49. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  50. int max_samples = samples_per_frame ? samples_per_frame :
  51. av_rescale_rnd(st->codecpar->sample_rate, time_base.num, time_base.den, AV_ROUND_UP);
  52. aic->sample_size = (st->codecpar->channels *
  53. av_get_bits_per_sample(st->codecpar->codec_id)) / 8;
  54. if (!aic->sample_size) {
  55. av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
  56. return AVERROR(EINVAL);
  57. }
  58. aic->samples_per_frame = samples_per_frame;
  59. aic->time_base = time_base;
  60. if (!(aic->fifo = av_fifo_alloc_array(100, max_samples)))
  61. return AVERROR(ENOMEM);
  62. aic->fifo_size = 100 * max_samples;
  63. }
  64. }
  65. return 0;
  66. }
  67. static int interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
  68. int stream_index, int flush)
  69. {
  70. AVStream *st = s->streams[stream_index];
  71. AudioInterleaveContext *aic = st->priv_data;
  72. int ret;
  73. int nb_samples = aic->samples_per_frame ? aic->samples_per_frame :
  74. (av_rescale_q(aic->n + 1, av_make_q(st->codecpar->sample_rate, 1), av_inv_q(aic->time_base)) - aic->nb_samples);
  75. int frame_size = nb_samples * aic->sample_size;
  76. int size = FFMIN(av_fifo_size(aic->fifo), frame_size);
  77. if (!size || (!flush && size == av_fifo_size(aic->fifo)))
  78. return 0;
  79. ret = av_new_packet(pkt, frame_size);
  80. if (ret < 0)
  81. return ret;
  82. av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
  83. if (size < pkt->size)
  84. memset(pkt->data + size, 0, pkt->size - size);
  85. pkt->dts = pkt->pts = aic->dts;
  86. pkt->duration = av_rescale_q(nb_samples, st->time_base, aic->time_base);
  87. pkt->stream_index = stream_index;
  88. aic->dts += pkt->duration;
  89. aic->nb_samples += nb_samples;
  90. aic->n++;
  91. return pkt->size;
  92. }
  93. int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
  94. int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
  95. int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *))
  96. {
  97. int i, ret;
  98. if (pkt) {
  99. AVStream *st = s->streams[pkt->stream_index];
  100. AudioInterleaveContext *aic = st->priv_data;
  101. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  102. unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
  103. if (new_size > aic->fifo_size) {
  104. if (av_fifo_realloc2(aic->fifo, new_size) < 0)
  105. return AVERROR(ENOMEM);
  106. aic->fifo_size = new_size;
  107. }
  108. av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
  109. } else {
  110. // rewrite pts and dts to be decoded time line position
  111. pkt->pts = pkt->dts = aic->dts;
  112. aic->dts += pkt->duration;
  113. if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0)
  114. return ret;
  115. }
  116. pkt = NULL;
  117. }
  118. for (i = 0; i < s->nb_streams; i++) {
  119. AVStream *st = s->streams[i];
  120. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  121. AVPacket new_pkt = { 0 };
  122. while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) {
  123. if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0)
  124. return ret;
  125. }
  126. if (ret < 0)
  127. return ret;
  128. }
  129. }
  130. return get_packet(s, out, NULL, flush);
  131. }