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.

126 lines
4.0KB

  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 "avformat.h"
  24. #include "audiointerleave.h"
  25. void ff_audio_interleave_close(AVFormatContext *s)
  26. {
  27. int i;
  28. for (i = 0; i < s->nb_streams; i++) {
  29. AVStream *st = s->streams[i];
  30. AudioInterleaveContext *aic = st->priv_data;
  31. if (st->codec->codec_type == CODEC_TYPE_AUDIO)
  32. av_fifo_free(&aic->fifo);
  33. }
  34. }
  35. int ff_audio_interleave_init(AVFormatContext *s,
  36. const int *samples_per_frame,
  37. AVRational time_base)
  38. {
  39. int i;
  40. if (!samples_per_frame)
  41. return -1;
  42. for (i = 0; i < s->nb_streams; i++) {
  43. AVStream *st = s->streams[i];
  44. AudioInterleaveContext *aic = st->priv_data;
  45. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  46. aic->sample_size = (st->codec->channels *
  47. av_get_bits_per_sample(st->codec->codec_id)) / 8;
  48. if (!aic->sample_size) {
  49. av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
  50. return -1;
  51. }
  52. aic->samples_per_frame = samples_per_frame;
  53. aic->samples = aic->samples_per_frame;
  54. aic->time_base = time_base;
  55. av_fifo_init(&aic->fifo, 100 * *aic->samples);
  56. }
  57. }
  58. return 0;
  59. }
  60. int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
  61. int stream_index, int flush)
  62. {
  63. AVStream *st = s->streams[stream_index];
  64. AudioInterleaveContext *aic = st->priv_data;
  65. int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size);
  66. if (!size || (!flush && size == av_fifo_size(&aic->fifo)))
  67. return 0;
  68. av_new_packet(pkt, size);
  69. av_fifo_read(&aic->fifo, pkt->data, size);
  70. pkt->dts = pkt->pts = aic->dts;
  71. pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
  72. pkt->stream_index = stream_index;
  73. aic->dts += pkt->duration;
  74. aic->samples++;
  75. if (!*aic->samples)
  76. aic->samples = aic->samples_per_frame;
  77. return size;
  78. }
  79. int ff_audio_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
  80. int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
  81. int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
  82. {
  83. int i;
  84. if (pkt) {
  85. AVStream *st = s->streams[pkt->stream_index];
  86. AudioInterleaveContext *aic = st->priv_data;
  87. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  88. av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL);
  89. } else {
  90. // rewrite pts and dts to be decoded time line position
  91. pkt->pts = pkt->dts = aic->dts;
  92. aic->dts += pkt->duration;
  93. ff_interleave_add_packet(s, pkt, compare_ts);
  94. }
  95. pkt = NULL;
  96. }
  97. for (i = 0; i < s->nb_streams; i++) {
  98. AVStream *st = s->streams[i];
  99. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  100. AVPacket new_pkt;
  101. while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush))
  102. ff_interleave_add_packet(s, &new_pkt, compare_ts);
  103. }
  104. }
  105. return get_packet(s, out, pkt, flush);
  106. }