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.

147 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 ret;
  74. int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size);
  75. if (!size || (!flush && size == av_fifo_size(aic->fifo)))
  76. return 0;
  77. ret = av_new_packet(pkt, size);
  78. if (ret < 0)
  79. return ret;
  80. av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
  81. pkt->dts = pkt->pts = aic->dts;
  82. pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
  83. pkt->stream_index = stream_index;
  84. aic->dts += pkt->duration;
  85. aic->samples++;
  86. if (!*aic->samples)
  87. aic->samples = aic->samples_per_frame;
  88. return size;
  89. }
  90. int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
  91. int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
  92. int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
  93. {
  94. int i, ret;
  95. if (pkt) {
  96. AVStream *st = s->streams[pkt->stream_index];
  97. AudioInterleaveContext *aic = st->priv_data;
  98. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  99. unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
  100. if (new_size > aic->fifo_size) {
  101. if (av_fifo_realloc2(aic->fifo, new_size) < 0)
  102. return AVERROR(ENOMEM);
  103. aic->fifo_size = new_size;
  104. }
  105. av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
  106. } else {
  107. // rewrite pts and dts to be decoded time line position
  108. pkt->pts = pkt->dts = aic->dts;
  109. aic->dts += pkt->duration;
  110. if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 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. while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) {
  120. if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0)
  121. return ret;
  122. }
  123. if (ret < 0)
  124. return ret;
  125. }
  126. }
  127. return get_packet(s, out, NULL, flush);
  128. }