FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/audio_frame_queue.c
Date: 2026-08-27 03:59:31
Exec Total Coverage
Lines: 74 80 92.5%
Functions: 4 4 100.0%
Branches: 37 58 63.8%

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/intreadwrite.h"
24 #include "libavutil/mem.h"
25 #include "audio_frame_queue.h"
26 #include "encode.h"
27 #include "libavutil/avassert.h"
28
29 45 av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
30 {
31 45 afq->avctx = avctx;
32 45 afq->remaining_delay = avctx->initial_padding;
33 45 afq->remaining_samples = avctx->initial_padding;
34 45 afq->output_delay = avctx->initial_padding;
35 45 afq->frame_count = 0;
36 45 }
37
38 45 av_cold void ff_af_queue_close(AudioFrameQueue *afq)
39 {
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if(afq->frame_count)
41 av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in the queue on closing\n", afq->frame_count);
42 45 av_freep(&afq->frames);
43 45 memset(afq, 0, sizeof(*afq));
44 45 }
45
46 12337 int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
47 {
48 12337 AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1));
49 const AVFrameSideData *sd;
50 12337 int nb_samples = f->nb_samples;
51
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12337 times.
12337 if(!new)
53 return AVERROR(ENOMEM);
54 12337 afq->frames = new;
55 12337 new += afq->frame_count;
56
57 12337 sd = av_frame_side_data_get(f->side_data, f->nb_side_data, AV_FRAME_DATA_SKIP_SAMPLES);
58
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12333 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
12337 if (sd && sd->size >= 10) {
59 4 int discard_padding = AV_RL32(sd->data + 4);
60
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (discard_padding > 0 && discard_padding < nb_samples)
61 4 nb_samples -= discard_padding;
62 }
63
64 /* get frame parameters */
65 12337 new->duration = nb_samples;
66 12337 new->duration += afq->remaining_delay;
67
1/2
✓ Branch 0 taken 12337 times.
✗ Branch 1 not taken.
12337 if (f->pts != AV_NOPTS_VALUE) {
68 12337 new->pts = av_rescale_q(f->pts,
69 12337 afq->avctx->time_base,
70 12337 (AVRational){ 1, afq->avctx->sample_rate });
71 12337 new->pts -= afq->remaining_delay;
72
3/4
✓ Branch 0 taken 12088 times.
✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12088 times.
12337 if(afq->frame_count && new[-1].pts >= new->pts)
73 av_log(afq->avctx, AV_LOG_WARNING, "Queue input is backward in time\n");
74 } else {
75 new->pts = AV_NOPTS_VALUE;
76 }
77 12337 afq->remaining_delay = 0;
78
79 /* add frame sample count */
80 12337 afq->remaining_samples += nb_samples;
81
82 12337 afq->frame_count++;
83
84 12337 return 0;
85 }
86
87 6230 int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
88 {
89 6230 int64_t out_pts = AV_NOPTS_VALUE;
90 6230 int frame_size = nb_samples;
91 6230 int removed_samples = 0;
92 int i;
93
94
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6230 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6230 if (afq->frame_count || afq->frame_alloc) {
95
1/2
✓ Branch 0 taken 6230 times.
✗ Branch 1 not taken.
6230 if (afq->frames->pts != AV_NOPTS_VALUE)
96 6230 out_pts = afq->frames->pts;
97 }
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6230 times.
6230 if(!afq->frame_count)
99 av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but the queue is empty\n", nb_samples);
100
1/2
✓ Branch 0 taken 6230 times.
✗ Branch 1 not taken.
6230 if (pkt)
101 6230 pkt->pts = ff_samples_to_time_base(afq->avctx, out_pts);
102
103
4/4
✓ Branch 0 taken 14200 times.
✓ Branch 1 taken 6194 times.
✓ Branch 2 taken 14164 times.
✓ Branch 3 taken 36 times.
20394 for(i=0; nb_samples && i<afq->frame_count; i++){
104 14164 int n= FFMIN(afq->frames[i].duration, nb_samples);
105 14164 afq->frames[i].duration -= n;
106 14164 nb_samples -= n;
107 14164 removed_samples += n;
108
1/2
✓ Branch 0 taken 14164 times.
✗ Branch 1 not taken.
14164 if(afq->frames[i].pts != AV_NOPTS_VALUE)
109 14164 afq->frames[i].pts += n;
110 }
111 6230 afq->remaining_samples -= removed_samples;
112
3/4
✓ Branch 0 taken 6230 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1827 times.
✓ Branch 3 taken 4403 times.
6230 i -= i && afq->frames[i-1].duration;
113 6230 memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i));
114 6230 afq->frame_count -= i;
115
116
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6194 times.
6230 if(nb_samples){
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 av_assert0(!afq->frame_count);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 av_assert0(afq->remaining_samples == afq->remaining_delay);
119
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE)
120 36 afq->frames[0].pts += nb_samples;
121 36 av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than there are in the queue\n", nb_samples);
122 }
123
1/2
✓ Branch 0 taken 6230 times.
✗ Branch 1 not taken.
6230 if (pkt) {
124 6230 int discard_padding = frame_size - removed_samples;
125 6230 pkt->duration = ff_samples_to_time_base(afq->avctx, removed_samples);
126
4/4
✓ Branch 0 taken 6187 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 6151 times.
6230 if (afq->output_delay > 0 || discard_padding > 0) {
127 uint8_t *side_data =
128 79 av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!side_data)
130 return AVERROR(ENOMEM);
131
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 36 times.
79 if (afq->output_delay) {
132 43 AV_WL32(side_data, FFMIN(afq->output_delay, removed_samples));
133 43 afq->output_delay -= removed_samples;
134 43 afq->output_delay = FFMAX(afq->output_delay, 0);
135 }
136 79 AV_WL32(side_data + 4, discard_padding);
137 }
138 }
139
140 6230 return 0;
141 }
142