framequeue.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Generic frame queue
  3. * Copyright (c) 2016 Nicolas George
  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 License
  9. * 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "framequeue.h"
  23. static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
  24. {
  25. return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)];
  26. }
  27. void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
  28. {
  29. }
  30. static void check_consistency(FFFrameQueue *fq)
  31. {
  32. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  33. uint64_t nb_samples = 0;
  34. size_t i;
  35. av_assert0(fq->queued == fq->total_frames_head - fq->total_frames_tail);
  36. for (i = 0; i < fq->queued; i++)
  37. nb_samples += bucket(fq, i)->frame->nb_samples;
  38. av_assert0(nb_samples == fq->total_samples_head - fq->total_samples_tail);
  39. #endif
  40. }
  41. void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
  42. {
  43. fq->queue = &fq->first_bucket;
  44. fq->allocated = 1;
  45. }
  46. void ff_framequeue_free(FFFrameQueue *fq)
  47. {
  48. while (fq->queued) {
  49. AVFrame *frame = ff_framequeue_take(fq);
  50. av_frame_free(&frame);
  51. }
  52. if (fq->queue != &fq->first_bucket)
  53. av_freep(&fq->queue);
  54. }
  55. int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
  56. {
  57. FFFrameBucket *b;
  58. check_consistency(fq);
  59. if (fq->queued == fq->allocated) {
  60. if (fq->allocated == 1) {
  61. size_t na = 8;
  62. FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq));
  63. if (!nq)
  64. return AVERROR(ENOMEM);
  65. nq[0] = fq->queue[0];
  66. fq->queue = nq;
  67. fq->allocated = na;
  68. } else {
  69. size_t na = fq->allocated << 1;
  70. FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq));
  71. if (!nq)
  72. return AVERROR(ENOMEM);
  73. if (fq->tail + fq->queued > fq->allocated)
  74. memmove(nq + fq->allocated, nq,
  75. (fq->tail + fq->queued - fq->allocated) * sizeof(*nq));
  76. fq->queue = nq;
  77. fq->allocated = na;
  78. }
  79. }
  80. b = bucket(fq, fq->queued);
  81. b->frame = frame;
  82. fq->queued++;
  83. fq->total_frames_head++;
  84. fq->total_samples_head += frame->nb_samples;
  85. check_consistency(fq);
  86. return 0;
  87. }
  88. AVFrame *ff_framequeue_take(FFFrameQueue *fq)
  89. {
  90. FFFrameBucket *b;
  91. check_consistency(fq);
  92. av_assert1(fq->queued);
  93. b = bucket(fq, 0);
  94. fq->queued--;
  95. fq->tail++;
  96. fq->tail &= fq->allocated - 1;
  97. fq->total_frames_tail++;
  98. fq->total_samples_tail += b->frame->nb_samples;
  99. fq->samples_skipped = 0;
  100. check_consistency(fq);
  101. return b->frame;
  102. }
  103. AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx)
  104. {
  105. FFFrameBucket *b;
  106. check_consistency(fq);
  107. av_assert1(idx < fq->queued);
  108. b = bucket(fq, idx);
  109. check_consistency(fq);
  110. return b->frame;
  111. }
  112. void ff_framequeue_skip_samples(FFFrameQueue *fq, size_t samples, AVRational time_base)
  113. {
  114. FFFrameBucket *b;
  115. size_t bytes;
  116. int planar, planes, i;
  117. check_consistency(fq);
  118. av_assert1(fq->queued);
  119. b = bucket(fq, 0);
  120. av_assert1(samples < b->frame->nb_samples);
  121. planar = av_sample_fmt_is_planar(b->frame->format);
  122. planes = planar ? b->frame->channels : 1;
  123. bytes = samples * av_get_bytes_per_sample(b->frame->format);
  124. if (!planar)
  125. bytes *= b->frame->channels;
  126. if (b->frame->pts != AV_NOPTS_VALUE)
  127. b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base);
  128. b->frame->nb_samples -= samples;
  129. b->frame->linesize[0] -= bytes;
  130. for (i = 0; i < planes; i++)
  131. b->frame->extended_data[i] += bytes;
  132. for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++)
  133. b->frame->data[i] = b->frame->extended_data[i];
  134. fq->total_samples_tail += samples;
  135. fq->samples_skipped = 1;
  136. ff_framequeue_update_peeked(fq, 0);
  137. }