FFmpeg
Loading...
Searching...
No Matches
pcm-blurayenc.c
Go to the documentation of this file.
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
22#include "avcodec.h"
23#include "bytestream.h"
24#include "codec_internal.h"
25#include "encode.h"
26
27typedef struct BlurayPCMEncContext {
28 uint16_t header; // Header added to every frame
30
32{
34 uint8_t ch_layout;
35 int quant, freq, frame_size;
36
37 switch (avctx->sample_fmt) {
39 avctx->bits_per_coded_sample = 16;
40 frame_size = 240;
41 quant = 1;
42 break;
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)) {
68 ch_layout = 1;
69 break;
71 ch_layout = 3;
72 break;
74 ch_layout = 4;
75 break;
77 ch_layout = 5;
78 break;
80 ch_layout = 6;
81 break;
83 ch_layout = 7;
84 break;
86 ch_layout = 8;
87 break;
89 ch_layout = 9;
90 break;
92 ch_layout = 10;
93 break;
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
108 const AVFrame *frame, int *got_packet_ptr)
109{
111 int sample_size, samples, channel, num_dest_channels;
112 const int16_t *src16;
113 const int32_t *src32;
114 unsigned pkt_size;
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 */
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 */
159 case AV_CH_LAYOUT_2_1:
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
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 {
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 */
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> */
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 */
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
const FFCodec ff_pcm_bluray_encoder
int32_t
Libavcodec external API header.
#define bytestream2_put_ne16
Definition bytestream.h:127
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition bytestream.h:301
#define bytestream2_put_ne24
Definition bytestream.h:128
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_2_2
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_7POINT0
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_2_1
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_PCM_BLURAY
Definition codec_id.h:354
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_2_1
#define AV_CHANNEL_LAYOUT_7POINT1
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define AV_WB16(p, v)
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
static av_cold int pcm_bluray_encode_init(AVCodecContext *avctx)
static int pcm_bluray_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
static const uint8_t quant[64]
Definition vmixdec.c:71