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.

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