FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sbcenc.c
Go to the documentation of this file.
1 /*
2  * Bluetooth low-complexity, subband codec (SBC)
3  *
4  * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
5  * Copyright (C) 2012-2013 Intel Corporation
6  * Copyright (C) 2008-2010 Nokia Corporation
7  * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8  * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
9  * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 /**
29  * @file
30  * SBC encoder implementation
31  */
32 
33 #include <stdbool.h>
34 #include "libavutil/opt.h"
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "profiles.h"
38 #include "put_bits.h"
39 #include "sbc.h"
40 #include "sbcdsp.h"
41 
42 typedef struct SBCEncContext {
43  AVClass *class;
44  int64_t max_delay;
45  int msbc;
47  DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp);
49 
50 static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
51 {
52  int ch, blk;
53  int16_t *x;
54 
55  switch (frame->subbands) {
56  case 4:
57  for (ch = 0; ch < frame->channels; ch++) {
58  x = &s->X[ch][s->position - 4 *
59  s->increment + frame->blocks * 4];
60  for (blk = 0; blk < frame->blocks;
61  blk += s->increment) {
62  s->sbc_analyze_4s(
63  s, x,
64  frame->sb_sample_f[blk][ch],
65  frame->sb_sample_f[blk + 1][ch] -
66  frame->sb_sample_f[blk][ch]);
67  x -= 4 * s->increment;
68  }
69  }
70  return frame->blocks * 4;
71 
72  case 8:
73  for (ch = 0; ch < frame->channels; ch++) {
74  x = &s->X[ch][s->position - 8 *
75  s->increment + frame->blocks * 8];
76  for (blk = 0; blk < frame->blocks;
77  blk += s->increment) {
78  s->sbc_analyze_8s(
79  s, x,
80  frame->sb_sample_f[blk][ch],
81  frame->sb_sample_f[blk + 1][ch] -
82  frame->sb_sample_f[blk][ch]);
83  x -= 8 * s->increment;
84  }
85  }
86  return frame->blocks * 8;
87 
88  default:
89  return AVERROR(EIO);
90  }
91 }
92 
93 /*
94  * Packs the SBC frame from frame into the memory in avpkt.
95  * Returns the length of the packed frame.
96  */
97 static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
98  int joint, bool msbc)
99 {
100  PutBitContext pb;
101 
102  /* Will copy the header parts for CRC-8 calculation here */
103  uint8_t crc_header[11] = { 0 };
104  int crc_pos;
105 
106  uint32_t audio_sample;
107 
108  int ch, sb, blk; /* channel, subband, block and bit counters */
109  int bits[2][8]; /* bits distribution */
110  uint32_t levels[2][8]; /* levels are derived from that */
111  uint32_t sb_sample_delta[2][8];
112 
113  if (msbc) {
114  avpkt->data[0] = MSBC_SYNCWORD;
115  avpkt->data[1] = 0;
116  avpkt->data[2] = 0;
117  } else {
118  avpkt->data[0] = SBC_SYNCWORD;
119 
120  avpkt->data[1] = (frame->frequency & 0x03) << 6;
121  avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
122  avpkt->data[1] |= (frame->mode & 0x03) << 2;
123  avpkt->data[1] |= (frame->allocation & 0x01) << 1;
124  avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0;
125 
126  avpkt->data[2] = frame->bitpool;
127 
128  if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
129  || frame->mode == JOINT_STEREO)))
130  return -5;
131  }
132 
133  /* Can't fill in crc yet */
134  crc_header[0] = avpkt->data[1];
135  crc_header[1] = avpkt->data[2];
136  crc_pos = 16;
137 
138  init_put_bits(&pb, avpkt->data + 4, avpkt->size);
139 
140  if (frame->mode == JOINT_STEREO) {
141  put_bits(&pb, frame->subbands, joint);
142  crc_header[crc_pos >> 3] = joint;
143  crc_pos += frame->subbands;
144  }
145 
146  for (ch = 0; ch < frame->channels; ch++) {
147  for (sb = 0; sb < frame->subbands; sb++) {
148  put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
149  crc_header[crc_pos >> 3] <<= 4;
150  crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
151  crc_pos += 4;
152  }
153  }
154 
155  /* align the last crc byte */
156  if (crc_pos % 8)
157  crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
158 
159  avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
160 
161  ff_sbc_calculate_bits(frame, bits);
162 
163  for (ch = 0; ch < frame->channels; ch++) {
164  for (sb = 0; sb < frame->subbands; sb++) {
165  levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
166  (32 - (frame->scale_factor[ch][sb] +
167  SCALE_OUT_BITS + 2));
168  sb_sample_delta[ch][sb] = (uint32_t) 1 <<
169  (frame->scale_factor[ch][sb] +
170  SCALE_OUT_BITS + 1);
171  }
172  }
173 
174  for (blk = 0; blk < frame->blocks; blk++) {
175  for (ch = 0; ch < frame->channels; ch++) {
176  for (sb = 0; sb < frame->subbands; sb++) {
177 
178  if (bits[ch][sb] == 0)
179  continue;
180 
181  audio_sample = ((uint64_t) levels[ch][sb] *
182  (sb_sample_delta[ch][sb] +
183  frame->sb_sample_f[blk][ch][sb])) >> 32;
184 
185  put_bits(&pb, bits[ch][sb], audio_sample);
186  }
187  }
188  }
189 
190  flush_put_bits(&pb);
191 
192  return (put_bits_count(&pb) + 7) / 8;
193 }
194 
195 static int sbc_encode_init(AVCodecContext *avctx)
196 {
197  SBCEncContext *sbc = avctx->priv_data;
198  struct sbc_frame *frame = &sbc->frame;
199 
200  if (avctx->profile == FF_PROFILE_SBC_MSBC)
201  sbc->msbc = 1;
202 
203  if (sbc->msbc) {
204  if (avctx->channels != 1) {
205  av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
206  return AVERROR(EINVAL);
207  }
208 
209  if (avctx->sample_rate != 16000) {
210  av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
211  return AVERROR(EINVAL);
212  }
213 
214  frame->mode = SBC_MODE_MONO;
215  frame->subbands = 8;
216  frame->blocks = MSBC_BLOCKS;
217  frame->allocation = SBC_AM_LOUDNESS;
218  frame->bitpool = 26;
219 
220  avctx->frame_size = 8 * MSBC_BLOCKS;
221  } else {
222  int d;
223 
224  if (avctx->global_quality > 255*FF_QP2LAMBDA) {
225  av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
226  return AVERROR(EINVAL);
227  }
228 
229  if (avctx->channels == 1) {
230  frame->mode = SBC_MODE_MONO;
231  if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
232  frame->subbands = 4;
233  else
234  frame->subbands = 8;
235  } else {
236  if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
237  frame->mode = SBC_MODE_JOINT_STEREO;
238  else
239  frame->mode = SBC_MODE_STEREO;
240  if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
241  frame->subbands = 4;
242  else
243  frame->subbands = 8;
244  }
245  /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
246  frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
247  / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
248 
249  frame->allocation = SBC_AM_LOUDNESS;
250 
251  d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
252  frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
253  - 4 * frame->subbands * avctx->channels
254  - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
255  if (avctx->global_quality > 0)
256  frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
257 
258  avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
259  }
260 
261  for (int i = 0; avctx->codec->supported_samplerates[i]; i++)
262  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
263  frame->frequency = i;
264 
265  frame->channels = avctx->channels;
266  frame->codesize = frame->subbands * frame->blocks * avctx->channels * 2;
268 
269  memset(&sbc->dsp.X, 0, sizeof(sbc->dsp.X));
270  sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
271  sbc->dsp.increment = sbc->msbc ? 1 : 4;
272  ff_sbcdsp_init(&sbc->dsp);
273 
274  return 0;
275 }
276 
277 static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
278  const AVFrame *av_frame, int *got_packet_ptr)
279 {
280  SBCEncContext *sbc = avctx->priv_data;
281  struct sbc_frame *frame = &sbc->frame;
283  uint8_t dual = frame->mode == SBC_MODE_DUAL_CHANNEL;
284  int ret, j = 0;
285 
286  int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
287  + ((frame->blocks * frame->bitpool * (1 + dual)
288  + joint * frame->subbands) + 7) / 8;
289 
290  /* input must be large enough to encode a complete frame */
291  if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
292  return 0;
293 
294  if ((ret = ff_alloc_packet2(avctx, avpkt, frame_length, 0)) < 0)
295  return ret;
296 
297  /* Select the needed input data processing function and call it */
298  if (frame->subbands == 8)
299  sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
300  sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
301  frame->subbands * frame->blocks, frame->channels);
302  else
303  sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
304  sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
305  frame->subbands * frame->blocks, frame->channels);
306 
307  sbc_analyze_audio(&sbc->dsp, &sbc->frame);
308 
309  if (frame->mode == JOINT_STEREO)
310  j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
311  frame->scale_factor,
312  frame->blocks,
313  frame->subbands);
314  else
315  sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
316  frame->scale_factor,
317  frame->blocks,
318  frame->channels,
319  frame->subbands);
320  emms_c();
321  sbc_pack_frame(avpkt, frame, j, sbc->msbc);
322 
323  *got_packet_ptr = 1;
324  return 0;
325 }
326 
327 #define OFFSET(x) offsetof(SBCEncContext, x)
328 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
329 static const AVOption options[] = {
330  { "sbc_delay", "set maximum algorithmic latency",
331  OFFSET(max_delay), AV_OPT_TYPE_DURATION, {.i64 = 13000}, 1000,13000, AE },
332  { "msbc", "use mSBC mode (wideband speech mono SBC)",
333  OFFSET(msbc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AE },
334  { NULL },
335 };
336 
337 static const AVClass sbc_class = {
338  .class_name = "sbc encoder",
339  .item_name = av_default_item_name,
340  .option = options,
341  .version = LIBAVUTIL_VERSION_INT,
342 };
343 
345  .name = "sbc",
346  .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
347  .type = AVMEDIA_TYPE_AUDIO,
348  .id = AV_CODEC_ID_SBC,
349  .priv_data_size = sizeof(SBCEncContext),
351  .encode2 = sbc_encode_frame,
352  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
353  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
354  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
356  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
358  .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
359  .priv_class = &sbc_class,
361 };
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1542
#define SBC_X_BUFFER_SIZE
Definition: sbcdsp.h:39
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
int msbc
Definition: sbcenc.c:45
enum sbc_frame::@140 mode
AVOption.
Definition: opt.h:246
uint16_t codesize
Definition: sbc.h:98
#define JOINT_STEREO
Definition: atrac3.c:55
static const AVOption options[]
Definition: sbcenc.c:329
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1583
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1446
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int32_t sb_sample_f[16][2][8]
Definition: sbc.h:107
#define SBC_AM_LOUDNESS
Definition: sbc.h:60
#define AV_CH_LAYOUT_STEREO
#define blk(i)
Definition: sha.c:185
int profile
profile
Definition: avcodec.h:2859
AVCodec.
Definition: avcodec.h:3424
SBC basic "building bricks".
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
#define MSBC_BLOCKS
Definition: sbc.h:39
uint8_t
AVOptions.
static AVFrame * frame
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
AVCodec ff_sbc_encoder
Definition: sbcenc.c:344
uint8_t * data
Definition: avcodec.h:1445
#define av_log(a,...)
#define SBC_MODE_MONO
Definition: sbc.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
SBCDSPContext dsp
Definition: sbcenc.c:47
uint32_t scale_factor[2][8]
Definition: sbc.h:104
#define AVERROR(e)
Definition: error.h:43
uint8_t bitpool
Definition: sbc.h:97
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
av_cold void ff_sbcdsp_init(SBCDSPContext *s)
Definition: sbcdsp.c:364
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:998
uint8_t channels
Definition: sbc.h:91
static const AVClass sbc_class
Definition: sbcenc.c:337
#define s(width, name)
Definition: cbs_vp9.c:257
#define SBC_MODE_JOINT_STEREO
Definition: sbc.h:57
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
Definition: sbc.c:55
enum sbc_frame::@141 allocation
static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame, int joint, bool msbc)
Definition: sbcenc.c:97
#define OFFSET(x)
Definition: sbcenc.c:327
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2209
#define MSBC_SYNCWORD
Definition: sbc.h:69
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
Definition: sbcenc.c:50
int sample_rate
samples per second
Definition: avcodec.h:2189
#define SBC_MODE_DUAL_CHANNEL
Definition: sbc.h:55
main external API structure.
Definition: avcodec.h:1533
#define FF_PROFILE_SBC_MSBC
Definition: avcodec.h:2962
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
Describe the class of an AVClass context structure.
Definition: log.h:67
int64_t max_delay
Definition: sbcenc.c:44
uint8_t frequency
Definition: sbc.h:83
#define AE
Definition: sbcenc.c:328
#define SBC_ALIGN
Definition: sbc.h:78
uint8_t joint
Definition: sbc.h:101
const AVProfile ff_sbc_profiles[]
Definition: profiles.c:149
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1599
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
common internal api header.
#define STEREO
Definition: cook.c:61
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
signed 16 bits
Definition: samplefmt.h:61
const AVCRC * crc_ctx
Definition: sbc.h:112
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
SBC common definitions for the encoder and decoder.
#define SBC_MODE_STEREO
Definition: sbc.h:56
void * priv_data
Definition: avcodec.h:1560
static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *av_frame, int *got_packet_ptr)
Definition: sbcenc.c:277
int channels
number of audio channels
Definition: avcodec.h:2190
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
Definition: avcodec.h:3446
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition: sbc.c:79
Definition: sbc.h:82
struct sbc_frame frame
Definition: sbcenc.c:46
#define AV_CH_LAYOUT_MONO
#define SCALE_OUT_BITS
Definition: sbcdsp.h:38
This structure stores compressed data.
Definition: avcodec.h:1422
uint8_t subbands
Definition: sbc.h:96
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
#define SBC_SYNCWORD
Definition: sbc.h:68
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static int sbc_encode_init(AVCodecContext *avctx)
Definition: sbcenc.c:195
uint8_t blocks
Definition: sbc.h:84
bitstream writer API