FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/pcm_rechunk.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 87 104 83.7%
Functions: 7 8 87.5%
Branches: 42 58 72.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2020 Marton Balint
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "bsf.h"
22 #include "bsf_internal.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/opt.h"
25
26 typedef struct PCMContext {
27 const AVClass *class;
28
29 int nb_out_samples;
30 int pad;
31 AVRational frame_rate;
32
33 AVPacket *in_pkt;
34 AVPacket *out_pkt;
35 int sample_size;
36 int64_t n;
37 } PCMContext;
38
39 17 static int init(AVBSFContext *ctx)
40 {
41 17 PCMContext *s = ctx->priv_data;
42 17 AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
43 int64_t min_samples;
44
45
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (ctx->par_in->ch_layout.nb_channels <= 0 || ctx->par_in->sample_rate <= 0)
46 return AVERROR(EINVAL);
47
48 17 ctx->time_base_out = av_inv_q(sr);
49 34 s->sample_size = ctx->par_in->ch_layout.nb_channels *
50 17 av_get_bits_per_sample(ctx->par_in->codec_id) / 8;
51
52
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4 times.
17 if (s->frame_rate.num) {
53 13 min_samples = av_rescale_q_rnd(1, sr, s->frame_rate, AV_ROUND_DOWN);
54 } else {
55 4 min_samples = s->nb_out_samples;
56 }
57
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (min_samples <= 0 || min_samples > INT_MAX / s->sample_size - 1)
58 return AVERROR(EINVAL);
59
60 17 s->in_pkt = av_packet_alloc();
61 17 s->out_pkt = av_packet_alloc();
62
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (!s->in_pkt || !s->out_pkt)
63 return AVERROR(ENOMEM);
64
65 17 return 0;
66 }
67
68 17 static void uninit(AVBSFContext *ctx)
69 {
70 17 PCMContext *s = ctx->priv_data;
71 17 av_packet_free(&s->in_pkt);
72 17 av_packet_free(&s->out_pkt);
73 17 }
74
75 static void flush(AVBSFContext *ctx)
76 {
77 PCMContext *s = ctx->priv_data;
78 av_packet_unref(s->in_pkt);
79 av_packet_unref(s->out_pkt);
80 s->n = 0;
81 }
82
83 342 static int send_packet(PCMContext *s, int nb_samples, AVPacket *pkt)
84 {
85 342 pkt->duration = nb_samples;
86 342 s->n++;
87 342 return 0;
88 }
89
90 492 static void drain_packet(AVPacket *pkt, int drain_data, int drain_samples)
91 {
92 492 pkt->size -= drain_data;
93 492 pkt->data += drain_data;
94
1/2
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
492 if (pkt->dts != AV_NOPTS_VALUE)
95 492 pkt->dts += drain_samples;
96
1/2
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
492 if (pkt->pts != AV_NOPTS_VALUE)
97 492 pkt->pts += drain_samples;
98 492 }
99
100 566 static int get_next_nb_samples(AVBSFContext *ctx)
101 {
102 566 PCMContext *s = ctx->priv_data;
103
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 146 times.
566 if (s->frame_rate.num) {
104 420 AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
105 420 return av_rescale_q(s->n + 1, sr, s->frame_rate) - av_rescale_q(s->n, sr, s->frame_rate);
106 } else {
107 146 return s->nb_out_samples;
108 }
109 }
110
111 5 static void set_silence(AVCodecParameters *par, uint8_t *buf, size_t size)
112 {
113 5 int c = 0;
114
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 switch (par->codec_id) {
115 case AV_CODEC_ID_PCM_ALAW: c = 0xd5; break;
116 case AV_CODEC_ID_PCM_MULAW:
117 case AV_CODEC_ID_PCM_VIDC: c = 0xff; break;
118 case AV_CODEC_ID_PCM_U8: c = 0x80; break;
119 }
120 5 memset(buf, c, size);
121 5 }
122
123 566 static int rechunk_filter(AVBSFContext *ctx, AVPacket *pkt)
124 {
125 566 PCMContext *s = ctx->priv_data;
126 566 int nb_samples = get_next_nb_samples(ctx);
127 566 int data_size = nb_samples * s->sample_size;
128 int ret;
129
130 do {
131
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 256 times.
773 if (s->in_pkt->size) {
132
4/4
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 175 times.
✓ Branch 2 taken 143 times.
✓ Branch 3 taken 199 times.
698 if (s->out_pkt->size || s->in_pkt->size < data_size) {
133 318 int drain = FFMIN(s->in_pkt->size, data_size - s->out_pkt->size);
134
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 175 times.
318 if (!s->out_pkt->size) {
135 143 ret = av_new_packet(s->out_pkt, data_size);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 if (ret < 0)
137 return ret;
138 143 ret = av_packet_copy_props(s->out_pkt, s->in_pkt);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 if (ret < 0) {
140 av_packet_unref(s->out_pkt);
141 return ret;
142 }
143 143 s->out_pkt->size = 0;
144 }
145 318 memcpy(s->out_pkt->data + s->out_pkt->size, s->in_pkt->data, drain);
146 318 s->out_pkt->size += drain;
147 318 drain_packet(s->in_pkt, drain, drain / s->sample_size);
148
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 136 times.
318 if (!s->in_pkt->size)
149 182 av_packet_unref(s->in_pkt);
150
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 181 times.
318 if (s->out_pkt->size == data_size) {
151 137 av_packet_move_ref(pkt, s->out_pkt);
152 137 return send_packet(s, nb_samples, pkt);
153 }
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181 times.
181 av_assert0(!s->in_pkt->size);
155
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 25 times.
199 } else if (s->in_pkt->size > data_size) {
156 174 ret = av_packet_ref(pkt, s->in_pkt);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 if (ret < 0)
158 return ret;
159 174 pkt->size = data_size;
160 174 drain_packet(s->in_pkt, data_size, nb_samples);
161 174 return send_packet(s, nb_samples, pkt);
162 } else {
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(s->in_pkt->size == data_size);
164 25 av_packet_move_ref(pkt, s->in_pkt);
165 25 return send_packet(s, nb_samples, pkt);
166 }
167 } else
168 256 av_packet_unref(s->in_pkt);
169
170 437 ret = ff_bsf_get_packet_ref(ctx, s->in_pkt);
171
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 414 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 17 times.
437 if (ret == AVERROR_EOF && s->out_pkt->size) {
172
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (s->pad) {
173 5 set_silence(ctx->par_in, s->out_pkt->data + s->out_pkt->size, data_size - s->out_pkt->size);
174 5 s->out_pkt->size = data_size;
175 } else {
176 1 nb_samples = s->out_pkt->size / s->sample_size;
177 }
178 6 av_packet_move_ref(pkt, s->out_pkt);
179 6 return send_packet(s, nb_samples, pkt);
180 }
181
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 224 times.
431 if (ret >= 0)
182 207 av_packet_rescale_ts(s->in_pkt, ctx->time_base_in, ctx->time_base_out);
183
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 224 times.
431 } while (ret >= 0);
184
185 224 return ret;
186 }
187
188 #define OFFSET(x) offsetof(PCMContext, x)
189 #define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM)
190 static const AVOption options[] = {
191 { "nb_out_samples", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
192 { "n", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
193 { "pad", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS },
194 { "p", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS },
195 { "frame_rate", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
196 { "r", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
197 { NULL },
198 };
199
200 static const AVClass pcm_rechunk_class = {
201 .class_name = "pcm_rechunk_bsf",
202 .item_name = av_default_item_name,
203 .option = options,
204 .version = LIBAVUTIL_VERSION_INT,
205 };
206
207 static const enum AVCodecID codec_ids[] = {
208 AV_CODEC_ID_PCM_ALAW,
209 AV_CODEC_ID_PCM_F16LE,
210 AV_CODEC_ID_PCM_F24LE,
211 AV_CODEC_ID_PCM_F32BE,
212 AV_CODEC_ID_PCM_F32LE,
213 AV_CODEC_ID_PCM_F64BE,
214 AV_CODEC_ID_PCM_F64LE,
215 AV_CODEC_ID_PCM_MULAW,
216 AV_CODEC_ID_PCM_S16BE,
217 AV_CODEC_ID_PCM_S16LE,
218 AV_CODEC_ID_PCM_S24BE,
219 AV_CODEC_ID_PCM_S24DAUD,
220 AV_CODEC_ID_PCM_S24LE,
221 AV_CODEC_ID_PCM_S32BE,
222 AV_CODEC_ID_PCM_S32LE,
223 AV_CODEC_ID_PCM_S64BE,
224 AV_CODEC_ID_PCM_S64LE,
225 AV_CODEC_ID_PCM_S8,
226 AV_CODEC_ID_PCM_SGA,
227 AV_CODEC_ID_PCM_U8,
228 AV_CODEC_ID_PCM_VIDC,
229 AV_CODEC_ID_NONE,
230 };
231
232 const FFBitStreamFilter ff_pcm_rechunk_bsf = {
233 .p.name = "pcm_rechunk",
234 .p.codec_ids = codec_ids,
235 .p.priv_class = &pcm_rechunk_class,
236 .priv_data_size = sizeof(PCMContext),
237 .filter = rechunk_filter,
238 .init = init,
239 .flush = flush,
240 .close = uninit,
241 };
242