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 | 2998384 | static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx) | |
27 | { | ||
28 | 2998384 | return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)]; | |
29 | } | ||
30 | |||
31 | 16141 | void ff_framequeue_global_init(FFFrameQueueGlobal *fqg) | |
32 | { | ||
33 | 16141 | fqg->max_queued = SIZE_MAX; | |
34 | 16141 | fqg->queued = 0; | |
35 | 16141 | } | |
36 | |||
37 | 5995255 | 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 | 5995255 | } | |
49 | |||
50 | 46622 | void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg) | |
51 | { | ||
52 | 46622 | fq->queue = &fq->first_bucket; | |
53 | 46622 | fq->allocated = 1; | |
54 | 46622 | fq->global = fqg; | |
55 | 46622 | } | |
56 | |||
57 | 46754 | void ff_framequeue_free(FFFrameQueue *fq) | |
58 | { | ||
59 |
2/2✓ Branch 0 taken 2965 times.
✓ Branch 1 taken 46754 times.
|
49719 | while (fq->queued) { |
60 | 2965 | AVFrame *frame = ff_framequeue_take(fq); | |
61 | 2965 | av_frame_free(&frame); | |
62 | } | ||
63 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 46518 times.
|
46754 | if (fq->queue != &fq->first_bucket) |
64 | 236 | av_freep(&fq->queue); | |
65 | 46754 | } | |
66 | |||
67 | 1495895 | int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame) | |
68 | { | ||
69 | FFFrameBucket *b; | ||
70 | |||
71 | 1495895 | check_consistency(fq); | |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1495895 times.
|
1495895 | if (fq->global->queued >= fq->global->max_queued) |
73 | ✗ | return AVERROR(ENOMEM); | |
74 |
2/2✓ Branch 0 taken 155 times.
✓ Branch 1 taken 1495740 times.
|
1495895 | if (fq->queued == fq->allocated) { |
75 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 51 times.
|
155 | if (fq->allocated == 1) { |
76 | 104 | size_t na = 8; | |
77 | 104 | FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq)); | |
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (!nq) |
79 | ✗ | return AVERROR(ENOMEM); | |
80 | 104 | nq[0] = fq->queue[0]; | |
81 | 104 | fq->queue = nq; | |
82 | 104 | fq->allocated = na; | |
83 | } else { | ||
84 | 51 | size_t na = fq->allocated << 1; | |
85 | 51 | FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq)); | |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (!nq) |
87 | ✗ | return AVERROR(ENOMEM); | |
88 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 44 times.
|
51 | if (fq->tail) |
89 | 7 | memmove(nq + fq->allocated, nq, fq->tail * sizeof(*nq)); | |
90 | 51 | fq->queue = nq; | |
91 | 51 | fq->allocated = na; | |
92 | } | ||
93 | } | ||
94 | 1495895 | b = bucket(fq, fq->queued); | |
95 | 1495895 | b->frame = frame; | |
96 | 1495895 | fq->queued++; | |
97 | 1495895 | fq->global->queued++; | |
98 | 1495895 | fq->total_frames_head++; | |
99 | 1495895 | fq->total_samples_head += frame->nb_samples; | |
100 | 1495895 | check_consistency(fq); | |
101 | 1495895 | return 0; | |
102 | } | ||
103 | |||
104 | 1495895 | AVFrame *ff_framequeue_take(FFFrameQueue *fq) | |
105 | { | ||
106 | FFFrameBucket *b; | ||
107 | |||
108 | 1495895 | check_consistency(fq); | |
109 | av_assert1(fq->queued); | ||
110 | 1495895 | b = bucket(fq, 0); | |
111 | 1495895 | fq->queued--; | |
112 | 1495895 | fq->global->queued--; | |
113 | 1495895 | fq->tail++; | |
114 | 1495895 | fq->tail &= fq->allocated - 1; | |
115 | 1495895 | fq->total_frames_tail++; | |
116 | 1495895 | fq->total_samples_tail += b->frame->nb_samples; | |
117 | 1495895 | fq->samples_skipped = 0; | |
118 | 1495895 | check_consistency(fq); | |
119 | 1495895 | return b->frame; | |
120 | } | ||
121 | |||
122 | 5081 | AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx) | |
123 | { | ||
124 | FFFrameBucket *b; | ||
125 | |||
126 | 5081 | check_consistency(fq); | |
127 | av_assert1(idx < fq->queued); | ||
128 | 5081 | b = bucket(fq, idx); | |
129 | 5081 | check_consistency(fq); | |
130 | 5081 | return b->frame; | |
131 | } | ||
132 | |||
133 | 1513 | 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 | 1513 | check_consistency(fq); | |
140 | av_assert1(fq->queued); | ||
141 | 1513 | b = bucket(fq, 0); | |
142 | av_assert1(samples < b->frame->nb_samples); | ||
143 | 1513 | planar = av_sample_fmt_is_planar(b->frame->format); | |
144 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1476 times.
|
1513 | planes = planar ? b->frame->ch_layout.nb_channels : 1; |
145 | 1513 | bytes = samples * av_get_bytes_per_sample(b->frame->format); | |
146 |
2/2✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 37 times.
|
1513 | if (!planar) |
147 | 1476 | bytes *= b->frame->ch_layout.nb_channels; | |
148 |
1/2✓ Branch 0 taken 1513 times.
✗ Branch 1 not taken.
|
1513 | if (b->frame->pts != AV_NOPTS_VALUE) |
149 | 1513 | b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base); | |
150 | 1513 | b->frame->nb_samples -= samples; | |
151 | 1513 | b->frame->linesize[0] -= bytes; | |
152 |
2/2✓ Branch 0 taken 1548 times.
✓ Branch 1 taken 1513 times.
|
3061 | for (i = 0; i < planes; i++) |
153 | 1548 | b->frame->extended_data[i] += bytes; | |
154 |
3/4✓ Branch 0 taken 1548 times.
✓ Branch 1 taken 1513 times.
✓ Branch 2 taken 1548 times.
✗ Branch 3 not taken.
|
3061 | for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++) |
155 | 1548 | b->frame->data[i] = b->frame->extended_data[i]; | |
156 | 1513 | fq->total_samples_tail += samples; | |
157 | 1513 | fq->samples_skipped = 1; | |
158 | 1513 | ff_framequeue_update_peeked(fq, 0); | |
159 | 1513 | } | |
160 |