audio_frame_queue.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Audio Frame Queue
  3. * Copyright (c) 2012 Justin Ruggles
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "audio_frame_queue.h"
  24. #include "internal.h"
  25. #include "libavutil/avassert.h"
  26. av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
  27. {
  28. afq->avctx = avctx;
  29. afq->remaining_delay = avctx->initial_padding;
  30. afq->remaining_samples = avctx->initial_padding;
  31. afq->frame_count = 0;
  32. }
  33. void ff_af_queue_close(AudioFrameQueue *afq)
  34. {
  35. if(afq->frame_count)
  36. av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in the queue on closing\n", afq->frame_count);
  37. av_freep(&afq->frames);
  38. memset(afq, 0, sizeof(*afq));
  39. }
  40. int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
  41. {
  42. AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1));
  43. if(!new)
  44. return AVERROR(ENOMEM);
  45. afq->frames = new;
  46. new += afq->frame_count;
  47. /* get frame parameters */
  48. new->duration = f->nb_samples;
  49. new->duration += afq->remaining_delay;
  50. if (f->pts != AV_NOPTS_VALUE) {
  51. new->pts = av_rescale_q(f->pts,
  52. afq->avctx->time_base,
  53. (AVRational){ 1, afq->avctx->sample_rate });
  54. new->pts -= afq->remaining_delay;
  55. if(afq->frame_count && new[-1].pts >= new->pts)
  56. av_log(afq->avctx, AV_LOG_WARNING, "Queue input is backward in time\n");
  57. } else {
  58. new->pts = AV_NOPTS_VALUE;
  59. }
  60. afq->remaining_delay = 0;
  61. /* add frame sample count */
  62. afq->remaining_samples += f->nb_samples;
  63. afq->frame_count++;
  64. return 0;
  65. }
  66. void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
  67. int64_t *duration)
  68. {
  69. int64_t out_pts = AV_NOPTS_VALUE;
  70. int removed_samples = 0;
  71. int i;
  72. if (afq->frame_count || afq->frame_alloc) {
  73. if (afq->frames->pts != AV_NOPTS_VALUE)
  74. out_pts = afq->frames->pts;
  75. }
  76. if(!afq->frame_count)
  77. av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but the queue is empty\n", nb_samples);
  78. if (pts)
  79. *pts = ff_samples_to_time_base(afq->avctx, out_pts);
  80. for(i=0; nb_samples && i<afq->frame_count; i++){
  81. int n= FFMIN(afq->frames[i].duration, nb_samples);
  82. afq->frames[i].duration -= n;
  83. nb_samples -= n;
  84. removed_samples += n;
  85. if(afq->frames[i].pts != AV_NOPTS_VALUE)
  86. afq->frames[i].pts += n;
  87. }
  88. afq->remaining_samples -= removed_samples;
  89. i -= i && afq->frames[i-1].duration;
  90. memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i));
  91. afq->frame_count -= i;
  92. if(nb_samples){
  93. av_assert0(!afq->frame_count);
  94. av_assert0(afq->remaining_samples == afq->remaining_delay);
  95. if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE)
  96. afq->frames[0].pts += nb_samples;
  97. av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than there are in the queue\n", nb_samples);
  98. }
  99. if (duration)
  100. *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
  101. }