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.

150 lines
4.9KB

  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 (st->codec->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 (!samples_per_frame)
  43. return AVERROR(EINVAL);
  44. if (!time_base.num) {
  45. av_log(s, AV_LOG_ERROR, "timebase not set for audio interleave\n");
  46. return AVERROR(EINVAL);
  47. }
  48. for (i = 0; i < s->nb_streams; i++) {
  49. AVStream *st = s->streams[i];
  50. AudioInterleaveContext *aic = st->priv_data;
  51. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  52. aic->sample_size = (st->codec->channels *
  53. av_get_bits_per_sample(st->codec->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->samples = aic->samples_per_frame;
  60. aic->time_base = time_base;
  61. aic->fifo_size = 100* *aic->samples;
  62. if (!(aic->fifo= av_fifo_alloc_array(100, *aic->samples)))
  63. return AVERROR(ENOMEM);
  64. }
  65. }
  66. return 0;
  67. }
  68. static int interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
  69. int stream_index, int flush)
  70. {
  71. AVStream *st = s->streams[stream_index];
  72. AudioInterleaveContext *aic = st->priv_data;
  73. int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size);
  74. if (!size || (!flush && size == av_fifo_size(aic->fifo)))
  75. return 0;
  76. if (av_new_packet(pkt, size) < 0)
  77. return AVERROR(ENOMEM);
  78. av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
  79. pkt->dts = pkt->pts = aic->dts;
  80. pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
  81. pkt->stream_index = stream_index;
  82. aic->dts += pkt->duration;
  83. aic->samples++;
  84. if (!*aic->samples)
  85. aic->samples = aic->samples_per_frame;
  86. return size;
  87. }
  88. int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
  89. int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
  90. int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
  91. {
  92. int i;
  93. if (pkt) {
  94. AVStream *st = s->streams[pkt->stream_index];
  95. AudioInterleaveContext *aic = st->priv_data;
  96. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  97. unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
  98. if (new_size > aic->fifo_size) {
  99. if (av_fifo_realloc2(aic->fifo, new_size) < 0)
  100. return AVERROR(ENOMEM);
  101. aic->fifo_size = new_size;
  102. }
  103. av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
  104. } else {
  105. int ret;
  106. // rewrite pts and dts to be decoded time line position
  107. pkt->pts = pkt->dts = aic->dts;
  108. aic->dts += pkt->duration;
  109. ret = ff_interleave_add_packet(s, pkt, compare_ts);
  110. if (ret < 0)
  111. return ret;
  112. }
  113. pkt = NULL;
  114. }
  115. for (i = 0; i < s->nb_streams; i++) {
  116. AVStream *st = s->streams[i];
  117. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  118. AVPacket new_pkt;
  119. int ret;
  120. while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) {
  121. ret = ff_interleave_add_packet(s, &new_pkt, compare_ts);
  122. if (ret < 0)
  123. return ret;
  124. }
  125. if (ret < 0)
  126. return ret;
  127. }
  128. }
  129. return get_packet(s, out, NULL, flush);
  130. }