FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcm-blurayenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 168 0.0%
Functions: 0 2 0.0%
Branches: 0 62 0.0%

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 static av_cold int pcm_bluray_encode_init(AVCodecContext *avctx)
32 {
33 BlurayPCMEncContext *s = avctx->priv_data;
34 uint8_t ch_layout;
35 int quant, freq, frame_size;
36
37 switch (avctx->sample_fmt) {
38 case AV_SAMPLE_FMT_S16:
39 avctx->bits_per_coded_sample = 16;
40 frame_size = 240;
41 quant = 1;
42 break;
43 case AV_SAMPLE_FMT_S32:
44 frame_size = 360;
45 avctx->bits_per_coded_sample = 24;
46 quant = 3;
47 break;
48 default:
49 return AVERROR_BUG;
50 }
51
52 switch (avctx->sample_rate) {
53 case 48000:
54 freq = 1;
55 break;
56 case 96000:
57 freq = 4;
58 break;
59 case 192000:
60 freq = 5;
61 break;
62 default:
63 return AVERROR_BUG;
64 }
65
66 switch (av_channel_layout_subset(&avctx->ch_layout, ~(uint64_t)0)) {
67 case AV_CH_LAYOUT_MONO:
68 ch_layout = 1;
69 break;
70 case AV_CH_LAYOUT_STEREO:
71 ch_layout = 3;
72 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 case AV_CH_LAYOUT_5POINT1:
89 ch_layout = 9;
90 break;
91 case AV_CH_LAYOUT_7POINT0:
92 ch_layout = 10;
93 break;
94 case AV_CH_LAYOUT_7POINT1:
95 ch_layout = 11;
96 break;
97 default:
98 return AVERROR_BUG;
99 }
100
101 s->header = (((ch_layout << 4) | freq) << 8) | (quant << 6);
102 avctx->frame_size = frame_size;
103
104 return 0;
105 }
106
107 static int pcm_bluray_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
108 const AVFrame *frame, int *got_packet_ptr)
109 {
110 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 num_dest_channels = FFALIGN(avctx->ch_layout.nb_channels, 2);
119 sample_size = (num_dest_channels *
120 (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
121 samples = frame->nb_samples;
122
123 pkt_size = sample_size * samples + 4;
124
125 if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
126 return ret;
127
128 AV_WB16(avpkt->data, pkt_size - 4);
129 AV_WB16(avpkt->data + 2, s->header);
130
131 src16 = (const int16_t *)frame->data[0];
132 src32 = (const int32_t *)frame->data[0];
133
134 bytestream2_init_writer(&pb, avpkt->data + 4, avpkt->size - 4);
135
136 switch (avctx->ch_layout.u.mask) {
137 /* cases with same number of source and coded channels */
138 case AV_CH_LAYOUT_STEREO:
139 case AV_CH_LAYOUT_4POINT0:
140 case AV_CH_LAYOUT_2_2:
141 samples *= num_dest_channels;
142 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 bytestream2_put_be16u(&pb, *src16++);
148 } while (--samples);
149 #endif
150 } else {
151 do {
152 bytestream2_put_be24u(&pb, (*src32++) >> 8);
153 } while (--samples);
154 }
155 break;
156 /* cases where number of source channels = coded channels + 1 */
157 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 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 channel = avctx->ch_layout.nb_channels;
168 do {
169 bytestream2_put_be16u(&pb, *src16++);
170 } while (--channel);
171 #endif
172 bytestream2_put_ne16(&pb, 0);
173 } 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 break;
184 /* remapping: L, R, C, LBack, RBack, LF */
185 case AV_CH_LAYOUT_5POINT1:
186 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
187 do {
188 bytestream2_put_be16u(&pb, src16[0]);
189 bytestream2_put_be16u(&pb, src16[1]);
190 bytestream2_put_be16u(&pb, src16[2]);
191 bytestream2_put_be16u(&pb, src16[4]);
192 bytestream2_put_be16u(&pb, src16[5]);
193 bytestream2_put_be16u(&pb, src16[3]);
194 src16 += 6;
195 } 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 break;
208 /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
209 case AV_CH_LAYOUT_7POINT0:
210 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
211 do {
212 bytestream2_put_be16u(&pb, src16[0]);
213 bytestream2_put_be16u(&pb, src16[1]);
214 bytestream2_put_be16u(&pb, src16[2]);
215 bytestream2_put_be16u(&pb, src16[5]);
216 bytestream2_put_be16u(&pb, src16[3]);
217 bytestream2_put_be16u(&pb, src16[4]);
218 bytestream2_put_be16u(&pb, src16[6]);
219 src16 += 7;
220 bytestream2_put_ne16(&pb, 0);
221 } 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 break;
236 /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
237 case AV_CH_LAYOUT_7POINT1:
238 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
239 do {
240 bytestream2_put_be16u(&pb, src16[0]);
241 bytestream2_put_be16u(&pb, src16[1]);
242 bytestream2_put_be16u(&pb, src16[2]);
243 bytestream2_put_be16u(&pb, src16[6]);
244 bytestream2_put_be16u(&pb, src16[4]);
245 bytestream2_put_be16u(&pb, src16[5]);
246 bytestream2_put_be16u(&pb, src16[7]);
247 bytestream2_put_be16u(&pb, src16[3]);
248 src16 += 8;
249 } 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 break;
264 default:
265 return AVERROR_BUG;
266 }
267
268 *got_packet_ptr = 1;
269
270 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 .p.supported_samplerates = (const int[]) { 48000, 96000, 192000, 0 },
282 .p.ch_layouts = (const AVChannelLayout[]) {
283 AV_CHANNEL_LAYOUT_MONO,
284 AV_CHANNEL_LAYOUT_STEREO,
285 AV_CHANNEL_LAYOUT_SURROUND,
286 AV_CHANNEL_LAYOUT_2_1,
287 AV_CHANNEL_LAYOUT_4POINT0,
288 AV_CHANNEL_LAYOUT_2_2,
289 AV_CHANNEL_LAYOUT_5POINT0,
290 AV_CHANNEL_LAYOUT_5POINT1,
291 AV_CHANNEL_LAYOUT_7POINT0,
292 AV_CHANNEL_LAYOUT_7POINT1,
293 { 0 } },
294 .p.sample_fmts = (const enum AVSampleFormat[]) {
295 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE },
296 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
297 };
298