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 "framequeue.h" |
24 |
|
|
|
25 |
|
2904343 |
static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx) |
26 |
|
|
{ |
27 |
|
2904343 |
return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)]; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
6011 |
void ff_framequeue_global_init(FFFrameQueueGlobal *fqg) |
31 |
|
|
{ |
32 |
|
6011 |
} |
33 |
|
|
|
34 |
|
5755952 |
static void check_consistency(FFFrameQueue *fq) |
35 |
|
|
{ |
36 |
|
|
#if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 |
37 |
|
|
uint64_t nb_samples = 0; |
38 |
|
|
size_t i; |
39 |
|
|
|
40 |
|
|
av_assert0(fq->queued == fq->total_frames_head - fq->total_frames_tail); |
41 |
|
|
for (i = 0; i < fq->queued; i++) |
42 |
|
|
nb_samples += bucket(fq, i)->frame->nb_samples; |
43 |
|
|
av_assert0(nb_samples == fq->total_samples_head - fq->total_samples_tail); |
44 |
|
|
#endif |
45 |
|
5755952 |
} |
46 |
|
|
|
47 |
|
23804 |
void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg) |
48 |
|
|
{ |
49 |
|
23804 |
fq->queue = &fq->first_bucket; |
50 |
|
23804 |
fq->allocated = 1; |
51 |
|
23804 |
} |
52 |
|
|
|
53 |
|
23936 |
void ff_framequeue_free(FFFrameQueue *fq) |
54 |
|
|
{ |
55 |
✓✓ |
23974 |
while (fq->queued) { |
56 |
|
38 |
AVFrame *frame = ff_framequeue_take(fq); |
57 |
|
38 |
av_frame_free(&frame); |
58 |
|
|
} |
59 |
✓✓ |
23936 |
if (fq->queue != &fq->first_bucket) |
60 |
|
333 |
av_freep(&fq->queue); |
61 |
|
23936 |
} |
62 |
|
|
|
63 |
|
1351253 |
int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame) |
64 |
|
|
{ |
65 |
|
|
FFFrameBucket *b; |
66 |
|
|
|
67 |
|
1351253 |
check_consistency(fq); |
68 |
✓✓ |
1351253 |
if (fq->queued == fq->allocated) { |
69 |
✓✓ |
319 |
if (fq->allocated == 1) { |
70 |
|
201 |
size_t na = 8; |
71 |
|
201 |
FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq)); |
72 |
✗✓ |
201 |
if (!nq) |
73 |
|
|
return AVERROR(ENOMEM); |
74 |
|
201 |
nq[0] = fq->queue[0]; |
75 |
|
201 |
fq->queue = nq; |
76 |
|
201 |
fq->allocated = na; |
77 |
|
|
} else { |
78 |
|
118 |
size_t na = fq->allocated << 1; |
79 |
|
118 |
FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq)); |
80 |
✗✓ |
118 |
if (!nq) |
81 |
|
|
return AVERROR(ENOMEM); |
82 |
✓✓ |
118 |
if (fq->tail + fq->queued > fq->allocated) |
83 |
|
10 |
memmove(nq + fq->allocated, nq, |
84 |
|
10 |
(fq->tail + fq->queued - fq->allocated) * sizeof(*nq)); |
85 |
|
118 |
fq->queue = nq; |
86 |
|
118 |
fq->allocated = na; |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
1351253 |
b = bucket(fq, fq->queued); |
90 |
|
1351253 |
b->frame = frame; |
91 |
|
1351253 |
fq->queued++; |
92 |
|
1351253 |
fq->total_frames_head++; |
93 |
|
1351253 |
fq->total_samples_head += frame->nb_samples; |
94 |
|
1351253 |
check_consistency(fq); |
95 |
|
1351253 |
return 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
1351253 |
AVFrame *ff_framequeue_take(FFFrameQueue *fq) |
99 |
|
|
{ |
100 |
|
|
FFFrameBucket *b; |
101 |
|
|
|
102 |
|
1351253 |
check_consistency(fq); |
103 |
|
|
av_assert1(fq->queued); |
104 |
|
1351253 |
b = bucket(fq, 0); |
105 |
|
1351253 |
fq->queued--; |
106 |
|
1351253 |
fq->tail++; |
107 |
|
1351253 |
fq->tail &= fq->allocated - 1; |
108 |
|
1351253 |
fq->total_frames_tail++; |
109 |
|
1351253 |
fq->total_samples_tail += b->frame->nb_samples; |
110 |
|
1351253 |
fq->samples_skipped = 0; |
111 |
|
1351253 |
check_consistency(fq); |
112 |
|
1351253 |
return b->frame; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
149103 |
AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx) |
116 |
|
|
{ |
117 |
|
|
FFFrameBucket *b; |
118 |
|
|
|
119 |
|
149103 |
check_consistency(fq); |
120 |
|
|
av_assert1(idx < fq->queued); |
121 |
|
149103 |
b = bucket(fq, idx); |
122 |
|
149103 |
check_consistency(fq); |
123 |
|
149103 |
return b->frame; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
52734 |
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 |
|
52734 |
check_consistency(fq); |
133 |
|
|
av_assert1(fq->queued); |
134 |
|
52734 |
b = bucket(fq, 0); |
135 |
|
|
av_assert1(samples < b->frame->nb_samples); |
136 |
|
52734 |
planar = av_sample_fmt_is_planar(b->frame->format); |
137 |
✓✓ |
52734 |
planes = planar ? b->frame->channels : 1; |
138 |
|
52734 |
bytes = samples * av_get_bytes_per_sample(b->frame->format); |
139 |
✓✓ |
52734 |
if (!planar) |
140 |
|
27534 |
bytes *= b->frame->channels; |
141 |
✓✗ |
52734 |
if (b->frame->pts != AV_NOPTS_VALUE) |
142 |
|
52734 |
b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base); |
143 |
|
52734 |
b->frame->nb_samples -= samples; |
144 |
|
52734 |
b->frame->linesize[0] -= bytes; |
145 |
✓✓ |
131005 |
for (i = 0; i < planes; i++) |
146 |
|
78271 |
b->frame->extended_data[i] += bytes; |
147 |
✓✓✓✗
|
131005 |
for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++) |
148 |
|
78271 |
b->frame->data[i] = b->frame->extended_data[i]; |
149 |
|
52734 |
fq->total_samples_tail += samples; |
150 |
|
52734 |
fq->samples_skipped = 1; |
151 |
|
52734 |
ff_framequeue_update_peeked(fq, 0); |
152 |
|
52734 |
} |