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.

164 lines
5.1KB

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