Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Audio Frame Queue | ||
3 | * Copyright (c) 2012 Justin Ruggles | ||
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 | ||
9 | * License 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 GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include "libavutil/attributes.h" | ||
23 | #include "libavutil/mem.h" | ||
24 | #include "audio_frame_queue.h" | ||
25 | #include "encode.h" | ||
26 | #include "libavutil/avassert.h" | ||
27 | |||
28 | 16 | av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq) | |
29 | { | ||
30 | 16 | afq->avctx = avctx; | |
31 | 16 | afq->remaining_delay = avctx->initial_padding; | |
32 | 16 | afq->remaining_samples = avctx->initial_padding; | |
33 | 16 | afq->frame_count = 0; | |
34 | 16 | } | |
35 | |||
36 | 16 | void ff_af_queue_close(AudioFrameQueue *afq) | |
37 | { | ||
38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if(afq->frame_count) |
39 | ✗ | av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in the queue on closing\n", afq->frame_count); | |
40 | 16 | av_freep(&afq->frames); | |
41 | 16 | memset(afq, 0, sizeof(*afq)); | |
42 | 16 | } | |
43 | |||
44 | 11836 | int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f) | |
45 | { | ||
46 | 11836 | AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1)); | |
47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11836 times.
|
11836 | if(!new) |
48 | ✗ | return AVERROR(ENOMEM); | |
49 | 11836 | afq->frames = new; | |
50 | 11836 | new += afq->frame_count; | |
51 | |||
52 | /* get frame parameters */ | ||
53 | 11836 | new->duration = f->nb_samples; | |
54 | 11836 | new->duration += afq->remaining_delay; | |
55 |
1/2✓ Branch 0 taken 11836 times.
✗ Branch 1 not taken.
|
11836 | if (f->pts != AV_NOPTS_VALUE) { |
56 | 11836 | new->pts = av_rescale_q(f->pts, | |
57 | 11836 | afq->avctx->time_base, | |
58 | 11836 | (AVRational){ 1, afq->avctx->sample_rate }); | |
59 | 11836 | new->pts -= afq->remaining_delay; | |
60 |
3/4✓ Branch 0 taken 11002 times.
✓ Branch 1 taken 834 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11002 times.
|
11836 | if(afq->frame_count && new[-1].pts >= new->pts) |
61 | ✗ | av_log(afq->avctx, AV_LOG_WARNING, "Queue input is backward in time\n"); | |
62 | } else { | ||
63 | ✗ | new->pts = AV_NOPTS_VALUE; | |
64 | } | ||
65 | 11836 | afq->remaining_delay = 0; | |
66 | |||
67 | /* add frame sample count */ | ||
68 | 11836 | afq->remaining_samples += f->nb_samples; | |
69 | |||
70 | 11836 | afq->frame_count++; | |
71 | |||
72 | 11836 | return 0; | |
73 | } | ||
74 | |||
75 | 5713 | void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, | |
76 | int64_t *duration) | ||
77 | { | ||
78 | 5713 | int64_t out_pts = AV_NOPTS_VALUE; | |
79 | 5713 | int removed_samples = 0; | |
80 | int i; | ||
81 | |||
82 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5713 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5713 | if (afq->frame_count || afq->frame_alloc) { |
83 |
1/2✓ Branch 0 taken 5713 times.
✗ Branch 1 not taken.
|
5713 | if (afq->frames->pts != AV_NOPTS_VALUE) |
84 | 5713 | out_pts = afq->frames->pts; | |
85 | } | ||
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5713 times.
|
5713 | if(!afq->frame_count) |
87 | ✗ | av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but the queue is empty\n", nb_samples); | |
88 |
1/2✓ Branch 0 taken 5713 times.
✗ Branch 1 not taken.
|
5713 | if (pts) |
89 | 5713 | *pts = ff_samples_to_time_base(afq->avctx, out_pts); | |
90 | |||
91 |
4/4✓ Branch 0 taken 12237 times.
✓ Branch 1 taken 5700 times.
✓ Branch 2 taken 12224 times.
✓ Branch 3 taken 13 times.
|
17937 | for(i=0; nb_samples && i<afq->frame_count; i++){ |
92 | 12224 | int n= FFMIN(afq->frames[i].duration, nb_samples); | |
93 | 12224 | afq->frames[i].duration -= n; | |
94 | 12224 | nb_samples -= n; | |
95 | 12224 | removed_samples += n; | |
96 |
1/2✓ Branch 0 taken 12224 times.
✗ Branch 1 not taken.
|
12224 | if(afq->frames[i].pts != AV_NOPTS_VALUE) |
97 | 12224 | afq->frames[i].pts += n; | |
98 | } | ||
99 | 5713 | afq->remaining_samples -= removed_samples; | |
100 |
3/4✓ Branch 0 taken 5713 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
✓ Branch 3 taken 5325 times.
|
5713 | i -= i && afq->frames[i-1].duration; |
101 | 5713 | memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i)); | |
102 | 5713 | afq->frame_count -= i; | |
103 | |||
104 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5700 times.
|
5713 | if(nb_samples){ |
105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | av_assert0(!afq->frame_count); |
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | av_assert0(afq->remaining_samples == afq->remaining_delay); |
107 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE) |
108 | 13 | afq->frames[0].pts += nb_samples; | |
109 | 13 | av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than there are in the queue\n", nb_samples); | |
110 | } | ||
111 |
1/2✓ Branch 0 taken 5713 times.
✗ Branch 1 not taken.
|
5713 | if (duration) |
112 | 5713 | *duration = ff_samples_to_time_base(afq->avctx, removed_samples); | |
113 | 5713 | } | |
114 |