2 * LPCM codecs for PCM formats found in Blu-ray m2ts streams
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/channel_layout.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
27 typedef struct BlurayPCMEncContext
{
28 uint16_t header
; // Header added to every frame
29 } BlurayPCMEncContext
;
31 static av_cold
int pcm_bluray_encode_init(AVCodecContext
*avctx
)
33 BlurayPCMEncContext
*s
= avctx
->priv_data
;
35 int quant
, freq
, frame_size
;
37 switch (avctx
->sample_fmt
) {
38 case AV_SAMPLE_FMT_S16
:
39 avctx
->bits_per_coded_sample
= 16;
43 case AV_SAMPLE_FMT_S32
:
45 avctx
->bits_per_coded_sample
= 24;
52 switch (avctx
->sample_rate
) {
66 switch (av_channel_layout_subset(&avctx
->ch_layout
, ~(uint64_t)0)) {
67 case AV_CH_LAYOUT_MONO
:
70 case AV_CH_LAYOUT_STEREO
:
73 case AV_CH_LAYOUT_SURROUND
:
76 case AV_CH_LAYOUT_2_1
:
79 case AV_CH_LAYOUT_4POINT0
:
82 case AV_CH_LAYOUT_2_2
:
85 case AV_CH_LAYOUT_5POINT0
:
88 case AV_CH_LAYOUT_5POINT1
:
91 case AV_CH_LAYOUT_7POINT0
:
94 case AV_CH_LAYOUT_7POINT1
:
101 s
->header
= (((ch_layout
<< 4) | freq
) << 8) | (quant
<< 6);
102 avctx
->frame_size
= frame_size
;
107 static int pcm_bluray_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
108 const AVFrame
*frame
, int *got_packet_ptr
)
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
;
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
;
123 pkt_size
= sample_size
* samples
+ 4;
125 if ((ret
= ff_get_encode_buffer(avctx
, avpkt
, pkt_size
, 0)) < 0)
128 AV_WB16(avpkt
->data
, pkt_size
- 4);
129 AV_WB16(avpkt
->data
+ 2, s
->header
);
131 src16
= (const int16_t *)frame
->data
[0];
132 src32
= (const int32_t *)frame
->data
[0];
134 bytestream2_init_writer(&pb
, avpkt
->data
+ 4, avpkt
->size
- 4);
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
) {
144 bytestream2_put_bufferu(&pb
, frame
->data
[0], samples
* 2);
147 bytestream2_put_be16u(&pb
, *src16
++);
152 bytestream2_put_be24u(&pb
, (*src32
++) >> 8);
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
) {
164 bytestream2_put_bufferu(&pb
, (const uint8_t *)src16
, avctx
->ch_layout
.nb_channels
* 2);
165 src16
+= avctx
->ch_layout
.nb_channels
;
167 channel
= avctx
->ch_layout
.nb_channels
;
169 bytestream2_put_be16u(&pb
, *src16
++);
172 bytestream2_put_ne16(&pb
, 0);
176 channel
= avctx
->ch_layout
.nb_channels
;
178 bytestream2_put_be24u(&pb
, (*src32
++) >> 8);
180 bytestream2_put_ne24(&pb
, 0);
184 /* remapping: L, R, C, LBack, RBack, LF */
185 case AV_CH_LAYOUT_5POINT1
:
186 if (AV_SAMPLE_FMT_S16
== avctx
->sample_fmt
) {
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]);
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);
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
) {
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]);
220 bytestream2_put_ne16(&pb
, 0);
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);
232 bytestream2_put_ne24(&pb
, 0);
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
) {
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]);
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]);
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
,
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
,