| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/mem.h" | ||
| 24 | #include "framequeue.h" | ||
| 25 | |||
| 26 | 3075410 | static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx) | |
| 27 | { | ||
| 28 | 3075410 | return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)]; | |
| 29 | } | ||
| 30 | |||
| 31 | 16159 | void ff_framequeue_global_init(FFFrameQueueGlobal *fqg) | |
| 32 | { | ||
| 33 | 16159 | fqg->max_queued = SIZE_MAX; | |
| 34 | 16159 | fqg->queued = 0; | |
| 35 | 16159 | } | |
| 36 | |||
| 37 | 6149305 | static void check_consistency(FFFrameQueue *fq) | |
| 38 | { | ||
| 39 | #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 | ||
| 40 | uint64_t nb_samples = 0; | ||
| 41 | size_t i; | ||
| 42 | |||
| 43 | av_assert0(fq->queued == fq->total_frames_head - fq->total_frames_tail); | ||
| 44 | for (i = 0; i < fq->queued; i++) | ||
| 45 | nb_samples += bucket(fq, i)->frame->nb_samples; | ||
| 46 | av_assert0(nb_samples == fq->total_samples_head - fq->total_samples_tail); | ||
| 47 | #endif | ||
| 48 | 6149305 | } | |
| 49 | |||
| 50 | 46649 | void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg) | |
| 51 | { | ||
| 52 | 46649 | fq->queue = &fq->first_bucket; | |
| 53 | 46649 | fq->allocated = 1; | |
| 54 | 46649 | fq->global = fqg; | |
| 55 | 46649 | } | |
| 56 | |||
| 57 | 46781 | void ff_framequeue_free(FFFrameQueue *fq) | |
| 58 | { | ||
| 59 |
2/2✓ Branch 0 taken 2960 times.
✓ Branch 1 taken 46781 times.
|
49741 | while (fq->queued) { |
| 60 | 2960 | AVFrame *frame = ff_framequeue_take(fq); | |
| 61 | 2960 | av_frame_free(&frame); | |
| 62 | } | ||
| 63 |
2/2✓ Branch 0 taken 230 times.
✓ Branch 1 taken 46551 times.
|
46781 | if (fq->queue != &fq->first_bucket) |
| 64 | 230 | av_freep(&fq->queue); | |
| 65 | 46781 | } | |
| 66 | |||
| 67 | 1534321 | int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame) | |
| 68 | { | ||
| 69 | FFFrameBucket *b; | ||
| 70 | |||
| 71 | 1534321 | check_consistency(fq); | |
| 72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534321 times.
|
1534321 | if (fq->global->queued >= fq->global->max_queued) |
| 73 | ✗ | return AVERROR(ENOMEM); | |
| 74 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 1534193 times.
|
1534321 | if (fq->queued == fq->allocated) { |
| 75 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 30 times.
|
128 | if (fq->allocated == 1) { |
| 76 | 98 | size_t na = 8; | |
| 77 | 98 | FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq)); | |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | if (!nq) |
| 79 | ✗ | return AVERROR(ENOMEM); | |
| 80 | 98 | nq[0] = fq->queue[0]; | |
| 81 | 98 | fq->queue = nq; | |
| 82 | 98 | fq->allocated = na; | |
| 83 | } else { | ||
| 84 | 30 | size_t na = fq->allocated << 1; | |
| 85 | 30 | FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq)); | |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!nq) |
| 87 | ✗ | return AVERROR(ENOMEM); | |
| 88 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29 times.
|
30 | if (fq->tail) |
| 89 | 1 | memmove(nq + fq->allocated, nq, fq->tail * sizeof(*nq)); | |
| 90 | 30 | fq->queue = nq; | |
| 91 | 30 | fq->allocated = na; | |
| 92 | } | ||
| 93 | } | ||
| 94 | 1534321 | b = bucket(fq, fq->queued); | |
| 95 | 1534321 | b->frame = frame; | |
| 96 | 1534321 | fq->queued++; | |
| 97 | 1534321 | fq->global->queued++; | |
| 98 | 1534321 | fq->total_frames_head++; | |
| 99 | 1534321 | fq->total_samples_head += frame->nb_samples; | |
| 100 | 1534321 | check_consistency(fq); | |
| 101 | 1534321 | return 0; | |
| 102 | } | ||
| 103 | |||
| 104 | 1534321 | AVFrame *ff_framequeue_take(FFFrameQueue *fq) | |
| 105 | { | ||
| 106 | FFFrameBucket *b; | ||
| 107 | |||
| 108 | 1534321 | check_consistency(fq); | |
| 109 | av_assert1(fq->queued); | ||
| 110 | 1534321 | b = bucket(fq, 0); | |
| 111 | 1534321 | fq->queued--; | |
| 112 | 1534321 | fq->global->queued--; | |
| 113 | 1534321 | fq->tail++; | |
| 114 | 1534321 | fq->tail &= fq->allocated - 1; | |
| 115 | 1534321 | fq->total_frames_tail++; | |
| 116 | 1534321 | fq->total_samples_tail += b->frame->nb_samples; | |
| 117 | 1534321 | fq->samples_skipped = 0; | |
| 118 | 1534321 | check_consistency(fq); | |
| 119 | 1534321 | return b->frame; | |
| 120 | } | ||
| 121 | |||
| 122 | 5253 | AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx) | |
| 123 | { | ||
| 124 | FFFrameBucket *b; | ||
| 125 | |||
| 126 | 5253 | check_consistency(fq); | |
| 127 | av_assert1(idx < fq->queued); | ||
| 128 | 5253 | b = bucket(fq, idx); | |
| 129 | 5253 | check_consistency(fq); | |
| 130 | 5253 | return b->frame; | |
| 131 | } | ||
| 132 | |||
| 133 | 1515 | void ff_framequeue_skip_samples(FFFrameQueue *fq, size_t samples, AVRational time_base) | |
| 134 | { | ||
| 135 | FFFrameBucket *b; | ||
| 136 | size_t bytes; | ||
| 137 | int planar, planes, i; | ||
| 138 | |||
| 139 | 1515 | check_consistency(fq); | |
| 140 | av_assert1(fq->queued); | ||
| 141 | 1515 | b = bucket(fq, 0); | |
| 142 | av_assert1(samples < b->frame->nb_samples); | ||
| 143 | 1515 | planar = av_sample_fmt_is_planar(b->frame->format); | |
| 144 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1478 times.
|
1515 | planes = planar ? b->frame->ch_layout.nb_channels : 1; |
| 145 | 1515 | bytes = samples * av_get_bytes_per_sample(b->frame->format); | |
| 146 |
2/2✓ Branch 0 taken 1478 times.
✓ Branch 1 taken 37 times.
|
1515 | if (!planar) |
| 147 | 1478 | bytes *= b->frame->ch_layout.nb_channels; | |
| 148 |
1/2✓ Branch 0 taken 1515 times.
✗ Branch 1 not taken.
|
1515 | if (b->frame->pts != AV_NOPTS_VALUE) |
| 149 | 1515 | b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base); | |
| 150 | 1515 | b->frame->nb_samples -= samples; | |
| 151 | 1515 | b->frame->linesize[0] -= bytes; | |
| 152 |
2/2✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 1515 times.
|
3065 | for (i = 0; i < planes; i++) |
| 153 | 1550 | b->frame->extended_data[i] += bytes; | |
| 154 |
3/4✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 1515 times.
✓ Branch 2 taken 1550 times.
✗ Branch 3 not taken.
|
3065 | for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++) |
| 155 | 1550 | b->frame->data[i] = b->frame->extended_data[i]; | |
| 156 | 1515 | fq->total_samples_tail += samples; | |
| 157 | 1515 | fq->samples_skipped = 1; | |
| 158 | 1515 | ff_framequeue_update_peeked(fq, 0); | |
| 159 | 1515 | } | |
| 160 |