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.

112 lines
3.7KB

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