FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcm-blurayenc.c
Date: 2026-04-11 18:33:02
Exec Total Coverage
Lines: 108 168 64.3%
Functions: 2 2 100.0%
Branches: 36 62 58.1%

Line Branch Exec Source
1 /*
2 * LPCM codecs for PCM formats found in Blu-ray m2ts streams
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 "libavutil/channel_layout.h"
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26
27 typedef struct BlurayPCMEncContext {
28 uint16_t header; // Header added to every frame
29 } BlurayPCMEncContext;
30
31 6 static av_cold int pcm_bluray_encode_init(AVCodecContext *avctx)
32 {
33 6 BlurayPCMEncContext *s = avctx->priv_data;
34 uint8_t ch_layout;
35 int quant, freq, frame_size;
36
37
2/3
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
6 switch (avctx->sample_fmt) {
38 5 case AV_SAMPLE_FMT_S16:
39 5 avctx->bits_per_coded_sample = 16;
40 5 frame_size = 240;
41 5 quant = 1;
42 5 break;
43 1 case AV_SAMPLE_FMT_S32:
44 1 frame_size = 360;
45 1 avctx->bits_per_coded_sample = 24;
46 1 quant = 3;
47 1 break;
48 default:
49 return AVERROR_BUG;
50 }
51
52
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
6 switch (avctx->sample_rate) {
53 5 case 48000:
54 5 freq = 1;
55 5 break;
56 case 96000:
57 freq = 4;
58 break;
59 1 case 192000:
60 1 freq = 5;
61 1 break;
62 default:
63 return AVERROR_BUG;
64 }
65
66
5/11
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
6 switch (av_channel_layout_subset(&avctx->ch_layout, ~(uint64_t)0)) {
67 1 case AV_CH_LAYOUT_MONO:
68 1 ch_layout = 1;
69 1 break;
70 2 case AV_CH_LAYOUT_STEREO:
71 2 ch_layout = 3;
72 2 break;
73 case AV_CH_LAYOUT_SURROUND:
74 ch_layout = 4;
75 break;
76 case AV_CH_LAYOUT_2_1:
77 ch_layout = 5;
78 break;
79 case AV_CH_LAYOUT_4POINT0:
80 ch_layout = 6;
81 break;
82 case AV_CH_LAYOUT_2_2:
83 ch_layout = 7;
84 break;
85 case AV_CH_LAYOUT_5POINT0:
86 ch_layout = 8;
87 break;
88 1 case AV_CH_LAYOUT_5POINT1:
89 1 ch_layout = 9;
90 1 break;
91 1 case AV_CH_LAYOUT_7POINT0:
92 1 ch_layout = 10;
93 1 break;
94 1 case AV_CH_LAYOUT_7POINT1:
95 1 ch_layout = 11;
96 1 break;
97 default:
98 return AVERROR_BUG;
99 }
100
101 6 s->header = (((ch_layout << 4) | freq) << 8) | (quant << 6);
102 6 avctx->frame_size = frame_size;
103
104 6 return 0;
105 }
106
107 767 static int pcm_bluray_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
108 const AVFrame *frame, int *got_packet_ptr)
109 {
110 767 BlurayPCMEncContext *s = avctx->priv_data;
111 int sample_size, samples, channel, num_dest_channels;
112 const int16_t *src16;
113 const int32_t *src32;
114 unsigned pkt_size;
115 PutByteContext pb;
116 int ret;
117
118 767 num_dest_channels = FFALIGN(avctx->ch_layout.nb_channels, 2);
119 767 sample_size = (num_dest_channels *
120
2/2
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 267 times.
767 (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
121 767 samples = frame->nb_samples;
122
123 767 pkt_size = sample_size * samples + 4;
124
125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 767 times.
767 if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
126 return ret;
127
128 767 AV_WB16(avpkt->data, pkt_size - 4);
129 767 AV_WB16(avpkt->data + 2, s->header);
130
131 767 src16 = (const int16_t *)frame->data[0];
132 767 src32 = (const int32_t *)frame->data[0];
133
134 767 bytestream2_init_writer(&pb, avpkt->data + 4, avpkt->size - 4);
135
136
5/6
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 100 times.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
767 switch (avctx->ch_layout.u.mask) {
137 /* cases with same number of source and coded channels */
138 367 case AV_CH_LAYOUT_STEREO:
139 case AV_CH_LAYOUT_4POINT0:
140 case AV_CH_LAYOUT_2_2:
141 367 samples *= num_dest_channels;
142
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 267 times.
367 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
143 #if HAVE_BIGENDIAN
144 bytestream2_put_bufferu(&pb, frame->data[0], samples * 2);
145 #else
146 do {
147 48000 bytestream2_put_be16u(&pb, *src16++);
148
2/2
✓ Branch 0 taken 47900 times.
✓ Branch 1 taken 100 times.
48000 } while (--samples);
149 #endif
150 } else {
151 do {
152 192240 bytestream2_put_be24u(&pb, (*src32++) >> 8);
153
2/2
✓ Branch 0 taken 191973 times.
✓ Branch 1 taken 267 times.
192240 } while (--samples);
154 }
155 367 break;
156 /* cases where number of source channels = coded channels + 1 */
157 100 case AV_CH_LAYOUT_MONO:
158 case AV_CH_LAYOUT_SURROUND:
159 case AV_CH_LAYOUT_2_1:
160 case AV_CH_LAYOUT_5POINT0:
161
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
162 do {
163 #if HAVE_BIGENDIAN
164 bytestream2_put_bufferu(&pb, (const uint8_t *)src16, avctx->ch_layout.nb_channels * 2);
165 src16 += avctx->ch_layout.nb_channels;
166 #else
167 24000 channel = avctx->ch_layout.nb_channels;
168 do {
169 24000 bytestream2_put_be16u(&pb, *src16++);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24000 times.
24000 } while (--channel);
171 #endif
172 24000 bytestream2_put_ne16(&pb, 0);
173
2/2
✓ Branch 0 taken 23900 times.
✓ Branch 1 taken 100 times.
24000 } while (--samples);
174 } else {
175 do {
176 channel = avctx->ch_layout.nb_channels;
177 do {
178 bytestream2_put_be24u(&pb, (*src32++) >> 8);
179 } while (--channel);
180 bytestream2_put_ne24(&pb, 0);
181 } while (--samples);
182 }
183 100 break;
184 /* remapping: L, R, C, LBack, RBack, LF */
185 100 case AV_CH_LAYOUT_5POINT1:
186
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
187 do {
188 24000 bytestream2_put_be16u(&pb, src16[0]);
189 24000 bytestream2_put_be16u(&pb, src16[1]);
190 24000 bytestream2_put_be16u(&pb, src16[2]);
191 24000 bytestream2_put_be16u(&pb, src16[4]);
192 24000 bytestream2_put_be16u(&pb, src16[5]);
193 24000 bytestream2_put_be16u(&pb, src16[3]);
194 24000 src16 += 6;
195
2/2
✓ Branch 0 taken 23900 times.
✓ Branch 1 taken 100 times.
24000 } while (--samples);
196 } else {
197 do {
198 bytestream2_put_be24u(&pb, src32[0] >> 8);
199 bytestream2_put_be24u(&pb, src32[1] >> 8);
200 bytestream2_put_be24u(&pb, src32[2] >> 8);
201 bytestream2_put_be24u(&pb, src32[4] >> 8);
202 bytestream2_put_be24u(&pb, src32[5] >> 8);
203 bytestream2_put_be24u(&pb, src32[3] >> 8);
204 src32 += 6;
205 } while (--samples);
206 }
207 100 break;
208 /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
209 100 case AV_CH_LAYOUT_7POINT0:
210
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
211 do {
212 24000 bytestream2_put_be16u(&pb, src16[0]);
213 24000 bytestream2_put_be16u(&pb, src16[1]);
214 24000 bytestream2_put_be16u(&pb, src16[2]);
215 24000 bytestream2_put_be16u(&pb, src16[5]);
216 24000 bytestream2_put_be16u(&pb, src16[3]);
217 24000 bytestream2_put_be16u(&pb, src16[4]);
218 24000 bytestream2_put_be16u(&pb, src16[6]);
219 24000 src16 += 7;
220 24000 bytestream2_put_ne16(&pb, 0);
221
2/2
✓ Branch 0 taken 23900 times.
✓ Branch 1 taken 100 times.
24000 } while (--samples);
222 } else {
223 do {
224 bytestream2_put_be24u(&pb, src32[0] >> 8);
225 bytestream2_put_be24u(&pb, src32[1] >> 8);
226 bytestream2_put_be24u(&pb, src32[2] >> 8);
227 bytestream2_put_be24u(&pb, src32[5] >> 8);
228 bytestream2_put_be24u(&pb, src32[3] >> 8);
229 bytestream2_put_be24u(&pb, src32[4] >> 8);
230 bytestream2_put_be24u(&pb, src32[6] >> 8);
231 src32 += 7;
232 bytestream2_put_ne24(&pb, 0);
233 } while (--samples);
234 }
235 100 break;
236 /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
237 100 case AV_CH_LAYOUT_7POINT1:
238
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
239 do {
240 24000 bytestream2_put_be16u(&pb, src16[0]);
241 24000 bytestream2_put_be16u(&pb, src16[1]);
242 24000 bytestream2_put_be16u(&pb, src16[2]);
243 24000 bytestream2_put_be16u(&pb, src16[6]);
244 24000 bytestream2_put_be16u(&pb, src16[4]);
245 24000 bytestream2_put_be16u(&pb, src16[5]);
246 24000 bytestream2_put_be16u(&pb, src16[7]);
247 24000 bytestream2_put_be16u(&pb, src16[3]);
248 24000 src16 += 8;
249
2/2
✓ Branch 0 taken 23900 times.
✓ Branch 1 taken 100 times.
24000 } while (--samples);
250 } else {
251 do {
252 bytestream2_put_be24u(&pb, src32[0]);
253 bytestream2_put_be24u(&pb, src32[1]);
254 bytestream2_put_be24u(&pb, src32[2]);
255 bytestream2_put_be24u(&pb, src32[6]);
256 bytestream2_put_be24u(&pb, src32[4]);
257 bytestream2_put_be24u(&pb, src32[5]);
258 bytestream2_put_be24u(&pb, src32[7]);
259 bytestream2_put_be24u(&pb, src32[3]);
260 src32 += 8;
261 } while (--samples);
262 }
263 100 break;
264 default:
265 return AVERROR_BUG;
266 }
267
268 767 *got_packet_ptr = 1;
269
270 767 return 0;
271 }
272
273 const FFCodec ff_pcm_bluray_encoder = {
274 .p.name = "pcm_bluray",
275 CODEC_LONG_NAME("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
276 .p.type = AVMEDIA_TYPE_AUDIO,
277 .p.id = AV_CODEC_ID_PCM_BLURAY,
278 .priv_data_size = sizeof(BlurayPCMEncContext),
279 .init = pcm_bluray_encode_init,
280 FF_CODEC_ENCODE_CB(pcm_bluray_encode_frame),
281 CODEC_SAMPLERATES(48000, 96000, 192000),
282 CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO,
283 AV_CHANNEL_LAYOUT_SURROUND, AV_CHANNEL_LAYOUT_2_1,
284 AV_CHANNEL_LAYOUT_4POINT0, AV_CHANNEL_LAYOUT_2_2,
285 AV_CHANNEL_LAYOUT_5POINT0, AV_CHANNEL_LAYOUT_5POINT1,
286 AV_CHANNEL_LAYOUT_7POINT0, AV_CHANNEL_LAYOUT_7POINT1),
287 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32),
288 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
289 };
290