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.

165 lines
5.2KB

  1. /*
  2. * Audio Frame Queue
  3. * Copyright (c) 2012 Justin Ruggles
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/mathematics.h"
  24. #include "internal.h"
  25. #include "audio_frame_queue.h"
  26. av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
  27. {
  28. afq->avctx = avctx;
  29. afq->next_pts = AV_NOPTS_VALUE;
  30. afq->remaining_delay = avctx->initial_padding;
  31. afq->remaining_samples = avctx->initial_padding;
  32. afq->frame_queue = NULL;
  33. }
  34. static void delete_next_frame(AudioFrameQueue *afq)
  35. {
  36. AudioFrame *f = afq->frame_queue;
  37. if (f) {
  38. afq->frame_queue = f->next;
  39. f->next = NULL;
  40. av_freep(&f);
  41. }
  42. }
  43. void ff_af_queue_close(AudioFrameQueue *afq)
  44. {
  45. /* remove/free any remaining frames */
  46. while (afq->frame_queue)
  47. delete_next_frame(afq);
  48. memset(afq, 0, sizeof(*afq));
  49. }
  50. #ifdef DEBUG
  51. static void af_queue_log_state(AudioFrameQueue *afq)
  52. {
  53. AudioFrame *f;
  54. ff_dlog(afq->avctx, "remaining delay = %d\n", afq->remaining_delay);
  55. ff_dlog(afq->avctx, "remaining samples = %d\n", afq->remaining_samples);
  56. ff_dlog(afq->avctx, "frames:\n");
  57. f = afq->frame_queue;
  58. while (f) {
  59. ff_dlog(afq->avctx, " [ pts=%9"PRId64" duration=%d ]\n",
  60. f->pts, f->duration);
  61. f = f->next;
  62. }
  63. }
  64. #endif /* DEBUG */
  65. int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
  66. {
  67. AudioFrame *new_frame;
  68. AudioFrame *queue_end = afq->frame_queue;
  69. /* find the end of the queue */
  70. while (queue_end && queue_end->next)
  71. queue_end = queue_end->next;
  72. /* allocate new frame queue entry */
  73. if (!(new_frame = av_malloc(sizeof(*new_frame))))
  74. return AVERROR(ENOMEM);
  75. /* get frame parameters */
  76. new_frame->next = NULL;
  77. new_frame->duration = f->nb_samples;
  78. if (f->pts != AV_NOPTS_VALUE) {
  79. new_frame->pts = av_rescale_q(f->pts,
  80. afq->avctx->time_base,
  81. (AVRational){ 1, afq->avctx->sample_rate });
  82. afq->next_pts = new_frame->pts + new_frame->duration;
  83. } else {
  84. new_frame->pts = AV_NOPTS_VALUE;
  85. afq->next_pts = AV_NOPTS_VALUE;
  86. }
  87. /* add new frame to the end of the queue */
  88. if (!queue_end)
  89. afq->frame_queue = new_frame;
  90. else
  91. queue_end->next = new_frame;
  92. /* add frame sample count */
  93. afq->remaining_samples += f->nb_samples;
  94. #ifdef DEBUG
  95. af_queue_log_state(afq);
  96. #endif
  97. return 0;
  98. }
  99. void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
  100. int64_t *duration)
  101. {
  102. int64_t out_pts = AV_NOPTS_VALUE;
  103. int removed_samples = 0;
  104. #ifdef DEBUG
  105. af_queue_log_state(afq);
  106. #endif
  107. /* get output pts from the next frame or generated pts */
  108. if (afq->frame_queue) {
  109. if (afq->frame_queue->pts != AV_NOPTS_VALUE)
  110. out_pts = afq->frame_queue->pts - afq->remaining_delay;
  111. } else {
  112. if (afq->next_pts != AV_NOPTS_VALUE)
  113. out_pts = afq->next_pts - afq->remaining_delay;
  114. }
  115. if (pts) {
  116. if (out_pts != AV_NOPTS_VALUE)
  117. *pts = ff_samples_to_time_base(afq->avctx, out_pts);
  118. else
  119. *pts = AV_NOPTS_VALUE;
  120. }
  121. /* if the delay is larger than the packet duration, we use up delay samples
  122. for the output packet and leave all frames in the queue */
  123. if (afq->remaining_delay >= nb_samples) {
  124. removed_samples += nb_samples;
  125. afq->remaining_delay -= nb_samples;
  126. }
  127. /* remove frames from the queue until we have enough to cover the
  128. requested number of samples or until the queue is empty */
  129. while (removed_samples < nb_samples && afq->frame_queue) {
  130. removed_samples += afq->frame_queue->duration;
  131. delete_next_frame(afq);
  132. }
  133. afq->remaining_samples -= removed_samples;
  134. /* if there are no frames left and we have room for more samples, use
  135. any remaining delay samples */
  136. if (removed_samples < nb_samples && afq->remaining_samples > 0) {
  137. int add_samples = FFMIN(afq->remaining_samples,
  138. nb_samples - removed_samples);
  139. removed_samples += add_samples;
  140. afq->remaining_samples -= add_samples;
  141. }
  142. if (removed_samples > nb_samples)
  143. av_log(afq->avctx, AV_LOG_WARNING, "frame_size is too large\n");
  144. if (duration)
  145. *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
  146. }