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