FFmpeg
opus.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Opus decoder/parser shared code
25  */
26 
27 #include <stdint.h>
28 
30 #include "libavutil/error.h"
31 #include "libavutil/ffmath.h"
32 
33 #include "opus_celt.h"
34 #include "opustab.h"
35 #include "internal.h"
36 #include "vorbis.h"
37 
38 static const uint16_t opus_frame_duration[32] = {
39  480, 960, 1920, 2880,
40  480, 960, 1920, 2880,
41  480, 960, 1920, 2880,
42  480, 960,
43  480, 960,
44  120, 240, 480, 960,
45  120, 240, 480, 960,
46  120, 240, 480, 960,
47  120, 240, 480, 960,
48 };
49 
50 /**
51  * Read a 1- or 2-byte frame length
52  */
53 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
54 {
55  int val;
56 
57  if (*ptr >= end)
58  return AVERROR_INVALIDDATA;
59  val = *(*ptr)++;
60  if (val >= 252) {
61  if (*ptr >= end)
62  return AVERROR_INVALIDDATA;
63  val += 4 * *(*ptr)++;
64  }
65  return val;
66 }
67 
68 /**
69  * Read a multi-byte length (used for code 3 packet padding size)
70  */
71 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
72 {
73  int val = 0;
74  int next;
75 
76  while (1) {
77  if (*ptr >= end || val > INT_MAX - 254)
78  return AVERROR_INVALIDDATA;
79  next = *(*ptr)++;
80  val += next;
81  if (next < 255)
82  break;
83  else
84  val--;
85  }
86  return val;
87 }
88 
89 /**
90  * Parse Opus packet info from raw packet data
91  */
92 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
93  int self_delimiting)
94 {
95  const uint8_t *ptr = buf;
96  const uint8_t *end = buf + buf_size;
97  int padding = 0;
98  int frame_bytes, i;
99 
100  if (buf_size < 1)
101  goto fail;
102 
103  /* TOC byte */
104  i = *ptr++;
105  pkt->code = (i ) & 0x3;
106  pkt->stereo = (i >> 2) & 0x1;
107  pkt->config = (i >> 3) & 0x1F;
108 
109  /* code 2 and code 3 packets have at least 1 byte after the TOC */
110  if (pkt->code >= 2 && buf_size < 2)
111  goto fail;
112 
113  switch (pkt->code) {
114  case 0:
115  /* 1 frame */
116  pkt->frame_count = 1;
117  pkt->vbr = 0;
118 
119  if (self_delimiting) {
120  int len = xiph_lacing_16bit(&ptr, end);
121  if (len < 0 || len > end - ptr)
122  goto fail;
123  end = ptr + len;
124  buf_size = end - buf;
125  }
126 
127  frame_bytes = end - ptr;
128  if (frame_bytes > MAX_FRAME_SIZE)
129  goto fail;
130  pkt->frame_offset[0] = ptr - buf;
131  pkt->frame_size[0] = frame_bytes;
132  break;
133  case 1:
134  /* 2 frames, equal size */
135  pkt->frame_count = 2;
136  pkt->vbr = 0;
137 
138  if (self_delimiting) {
139  int len = xiph_lacing_16bit(&ptr, end);
140  if (len < 0 || 2 * len > end - ptr)
141  goto fail;
142  end = ptr + 2 * len;
143  buf_size = end - buf;
144  }
145 
146  frame_bytes = end - ptr;
147  if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
148  goto fail;
149  pkt->frame_offset[0] = ptr - buf;
150  pkt->frame_size[0] = frame_bytes >> 1;
151  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
152  pkt->frame_size[1] = frame_bytes >> 1;
153  break;
154  case 2:
155  /* 2 frames, different sizes */
156  pkt->frame_count = 2;
157  pkt->vbr = 1;
158 
159  /* read 1st frame size */
160  frame_bytes = xiph_lacing_16bit(&ptr, end);
161  if (frame_bytes < 0)
162  goto fail;
163 
164  if (self_delimiting) {
165  int len = xiph_lacing_16bit(&ptr, end);
166  if (len < 0 || len + frame_bytes > end - ptr)
167  goto fail;
168  end = ptr + frame_bytes + len;
169  buf_size = end - buf;
170  }
171 
172  pkt->frame_offset[0] = ptr - buf;
173  pkt->frame_size[0] = frame_bytes;
174 
175  /* calculate 2nd frame size */
176  frame_bytes = end - ptr - pkt->frame_size[0];
177  if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
178  goto fail;
179  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
180  pkt->frame_size[1] = frame_bytes;
181  break;
182  case 3:
183  /* 1 to 48 frames, can be different sizes */
184  i = *ptr++;
185  pkt->frame_count = (i ) & 0x3F;
186  padding = (i >> 6) & 0x01;
187  pkt->vbr = (i >> 7) & 0x01;
188 
189  if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
190  goto fail;
191 
192  /* read padding size */
193  if (padding) {
194  padding = xiph_lacing_full(&ptr, end);
195  if (padding < 0)
196  goto fail;
197  }
198 
199  /* read frame sizes */
200  if (pkt->vbr) {
201  /* for VBR, all frames except the final one have their size coded
202  in the bitstream. the last frame size is implicit. */
203  int total_bytes = 0;
204  for (i = 0; i < pkt->frame_count - 1; i++) {
205  frame_bytes = xiph_lacing_16bit(&ptr, end);
206  if (frame_bytes < 0)
207  goto fail;
208  pkt->frame_size[i] = frame_bytes;
209  total_bytes += frame_bytes;
210  }
211 
212  if (self_delimiting) {
213  int len = xiph_lacing_16bit(&ptr, end);
214  if (len < 0 || len + total_bytes + padding > end - ptr)
215  goto fail;
216  end = ptr + total_bytes + len + padding;
217  buf_size = end - buf;
218  }
219 
220  frame_bytes = end - ptr - padding;
221  if (total_bytes > frame_bytes)
222  goto fail;
223  pkt->frame_offset[0] = ptr - buf;
224  for (i = 1; i < pkt->frame_count; i++)
225  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
226  pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
227  } else {
228  /* for CBR, the remaining packet bytes are divided evenly between
229  the frames */
230  if (self_delimiting) {
231  frame_bytes = xiph_lacing_16bit(&ptr, end);
232  if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
233  goto fail;
234  end = ptr + pkt->frame_count * frame_bytes + padding;
235  buf_size = end - buf;
236  } else {
237  frame_bytes = end - ptr - padding;
238  if (frame_bytes % pkt->frame_count ||
239  frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
240  goto fail;
241  frame_bytes /= pkt->frame_count;
242  }
243 
244  pkt->frame_offset[0] = ptr - buf;
245  pkt->frame_size[0] = frame_bytes;
246  for (i = 1; i < pkt->frame_count; i++) {
247  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
248  pkt->frame_size[i] = frame_bytes;
249  }
250  }
251  }
252 
253  pkt->packet_size = buf_size;
254  pkt->data_size = pkt->packet_size - padding;
255 
256  /* total packet duration cannot be larger than 120ms */
257  pkt->frame_duration = opus_frame_duration[pkt->config];
258  if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
259  goto fail;
260 
261  /* set mode and bandwidth */
262  if (pkt->config < 12) {
263  pkt->mode = OPUS_MODE_SILK;
264  pkt->bandwidth = pkt->config >> 2;
265  } else if (pkt->config < 16) {
266  pkt->mode = OPUS_MODE_HYBRID;
267  pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
268  } else {
269  pkt->mode = OPUS_MODE_CELT;
270  pkt->bandwidth = (pkt->config - 16) >> 2;
271  /* skip medium band */
272  if (pkt->bandwidth)
273  pkt->bandwidth++;
274  }
275 
276  return 0;
277 
278 fail:
279  memset(pkt, 0, sizeof(*pkt));
280  return AVERROR_INVALIDDATA;
281 }
282 
283 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
284 {
285  return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
286 }
287 
288 static int channel_reorder_unknown(int nb_channels, int channel_idx)
289 {
290  return channel_idx;
291 }
292 
294  OpusContext *s)
295 {
296  static const uint8_t default_channel_map[2] = { 0, 1 };
297 
298  int (*channel_reorder)(int, int) = channel_reorder_unknown;
299 
300  const uint8_t *extradata, *channel_map;
301  int extradata_size;
302  int version, channels, map_type, streams, stereo_streams, i, j;
303  uint64_t layout;
304 
305  if (!avctx->extradata) {
306  if (avctx->channels > 2) {
307  av_log(avctx, AV_LOG_ERROR,
308  "Multichannel configuration without extradata.\n");
309  return AVERROR(EINVAL);
310  }
311  extradata = opus_default_extradata;
312  extradata_size = sizeof(opus_default_extradata);
313  } else {
314  extradata = avctx->extradata;
315  extradata_size = avctx->extradata_size;
316  }
317 
318  if (extradata_size < 19) {
319  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
320  extradata_size);
321  return AVERROR_INVALIDDATA;
322  }
323 
324  version = extradata[8];
325  if (version > 15) {
326  avpriv_request_sample(avctx, "Extradata version %d", version);
327  return AVERROR_PATCHWELCOME;
328  }
329 
330  avctx->delay = AV_RL16(extradata + 10);
331  if (avctx->internal)
332  avctx->internal->skip_samples = avctx->delay;
333 
334  channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
335  if (!channels) {
336  av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
337  return AVERROR_INVALIDDATA;
338  }
339 
340  s->gain_i = AV_RL16(extradata + 16);
341  if (s->gain_i)
342  s->gain = ff_exp10(s->gain_i / (20.0 * 256));
343 
344  map_type = extradata[18];
345  if (!map_type) {
346  if (channels > 2) {
347  av_log(avctx, AV_LOG_ERROR,
348  "Channel mapping 0 is only specified for up to 2 channels\n");
349  return AVERROR_INVALIDDATA;
350  }
352  streams = 1;
353  stereo_streams = channels - 1;
354  channel_map = default_channel_map;
355  } else if (map_type == 1 || map_type == 2 || map_type == 255) {
356  if (extradata_size < 21 + channels) {
357  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
358  extradata_size);
359  return AVERROR_INVALIDDATA;
360  }
361 
362  streams = extradata[19];
363  stereo_streams = extradata[20];
364  if (!streams || stereo_streams > streams ||
365  streams + stereo_streams > 255) {
366  av_log(avctx, AV_LOG_ERROR,
367  "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
368  return AVERROR_INVALIDDATA;
369  }
370 
371  if (map_type == 1) {
372  if (channels > 8) {
373  av_log(avctx, AV_LOG_ERROR,
374  "Channel mapping 1 is only specified for up to 8 channels\n");
375  return AVERROR_INVALIDDATA;
376  }
378  channel_reorder = channel_reorder_vorbis;
379  } else if (map_type == 2) {
380  int ambisonic_order = ff_sqrt(channels) - 1;
381  if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
382  channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
383  av_log(avctx, AV_LOG_ERROR,
384  "Channel mapping 2 is only specified for channel counts"
385  " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
386  " for nonnegative integer n\n");
387  return AVERROR_INVALIDDATA;
388  }
389  if (channels > 227) {
390  av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
391  return AVERROR_INVALIDDATA;
392  }
393  layout = 0;
394  } else
395  layout = 0;
396 
397  channel_map = extradata + 21;
398  } else {
399  avpriv_request_sample(avctx, "Mapping type %d", map_type);
400  return AVERROR_PATCHWELCOME;
401  }
402 
403  s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
404  if (!s->channel_maps)
405  return AVERROR(ENOMEM);
406 
407  for (i = 0; i < channels; i++) {
408  ChannelMap *map = &s->channel_maps[i];
409  uint8_t idx = channel_map[channel_reorder(channels, i)];
410 
411  if (idx == 255) {
412  map->silence = 1;
413  continue;
414  } else if (idx >= streams + stereo_streams) {
415  av_log(avctx, AV_LOG_ERROR,
416  "Invalid channel map for output channel %d: %d\n", i, idx);
417  av_freep(&s->channel_maps);
418  return AVERROR_INVALIDDATA;
419  }
420 
421  /* check that we did not see this index yet */
422  map->copy = 0;
423  for (j = 0; j < i; j++)
424  if (channel_map[channel_reorder(channels, j)] == idx) {
425  map->copy = 1;
426  map->copy_idx = j;
427  break;
428  }
429 
430  if (idx < 2 * stereo_streams) {
431  map->stream_idx = idx / 2;
432  map->channel_idx = idx & 1;
433  } else {
434  map->stream_idx = idx - stereo_streams;
435  map->channel_idx = 0;
436  }
437  }
438 
439  avctx->channels = channels;
440  avctx->channel_layout = layout;
441  s->nb_streams = streams;
442  s->nb_stereo_streams = stereo_streams;
443 
444  return 0;
445 }
446 
448 {
449  float lowband_scratch[8 * 22];
450  float norm1[2 * 8 * 100];
451  float *norm2 = norm1 + 8 * 100;
452 
453  int totalbits = (f->framebits << 3) - f->anticollapse_needed;
454 
455  int update_lowband = 1;
456  int lowband_offset = 0;
457 
458  int i, j;
459 
460  for (i = f->start_band; i < f->end_band; i++) {
461  uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
462  int band_offset = ff_celt_freq_bands[i] << f->size;
463  int band_size = ff_celt_freq_range[i] << f->size;
464  float *X = f->block[0].coeffs + band_offset;
465  float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
466  float *norm_loc1, *norm_loc2;
467 
468  int consumed = opus_rc_tell_frac(rc);
469  int effective_lowband = -1;
470  int b = 0;
471 
472  /* Compute how many bits we want to allocate to this band */
473  if (i != f->start_band)
474  f->remaining -= consumed;
475  f->remaining2 = totalbits - consumed - 1;
476  if (i <= f->coded_bands - 1) {
477  int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
478  b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
479  }
480 
481  if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] ||
482  i == f->start_band + 1) && (update_lowband || lowband_offset == 0))
483  lowband_offset = i;
484 
485  if (i == f->start_band + 1) {
486  /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
487  the second to ensure the second band never has to use the LCG. */
488  int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
489 
490  memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
491 
492  if (f->channels == 2)
493  memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
494  }
495 
496  /* Get a conservative estimate of the collapse_mask's for the bands we're
497  going to be folding from. */
498  if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
499  f->blocks > 1 || f->tf_change[i] < 0)) {
500  int foldstart, foldend;
501 
502  /* This ensures we never repeat spectral content within one band */
503  effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
504  ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
505  foldstart = lowband_offset;
506  while (ff_celt_freq_bands[--foldstart] > effective_lowband);
507  foldend = lowband_offset - 1;
508  while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]);
509 
510  cm[0] = cm[1] = 0;
511  for (j = foldstart; j < foldend; j++) {
512  cm[0] |= f->block[0].collapse_masks[j];
513  cm[1] |= f->block[f->channels - 1].collapse_masks[j];
514  }
515  }
516 
517  if (f->dual_stereo && i == f->intensity_stereo) {
518  /* Switch off dual stereo to do intensity */
519  f->dual_stereo = 0;
520  for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
521  norm1[j] = (norm1[j] + norm2[j]) / 2;
522  }
523 
524  norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
525  norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
526 
527  if (f->dual_stereo) {
528  cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
529  f->blocks, norm_loc1, f->size,
530  norm1 + band_offset, 0, 1.0f,
531  lowband_scratch, cm[0]);
532 
533  cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
534  f->blocks, norm_loc2, f->size,
535  norm2 + band_offset, 0, 1.0f,
536  lowband_scratch, cm[1]);
537  } else {
538  cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0,
539  f->blocks, norm_loc1, f->size,
540  norm1 + band_offset, 0, 1.0f,
541  lowband_scratch, cm[0] | cm[1]);
542  cm[1] = cm[0];
543  }
544 
545  f->block[0].collapse_masks[i] = (uint8_t)cm[0];
546  f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
547  f->remaining += f->pulses[i] + consumed;
548 
549  /* Update the folding position only as long as we have 1 bit/sample depth */
550  update_lowband = (b > band_size << 3);
551  }
552 }
553 
554 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
555 
557 {
558  int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
559  int skip_startband = f->start_band;
560  int skip_bit = 0;
561  int intensitystereo_bit = 0;
562  int dualstereo_bit = 0;
563  int dynalloc = 6;
564  int extrabits = 0;
565 
566  int boost[CELT_MAX_BANDS] = { 0 };
567  int trim_offset[CELT_MAX_BANDS];
568  int threshold[CELT_MAX_BANDS];
569  int bits1[CELT_MAX_BANDS];
570  int bits2[CELT_MAX_BANDS];
571 
572  /* Spread */
573  if (opus_rc_tell(rc) + 4 <= f->framebits) {
574  if (encode)
576  else
578  } else {
579  f->spread = CELT_SPREAD_NORMAL;
580  }
581 
582  /* Initialize static allocation caps */
583  for (i = 0; i < CELT_MAX_BANDS; i++)
584  f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
585 
586  /* Band boosts */
587  tbits_8ths = f->framebits << 3;
588  for (i = f->start_band; i < f->end_band; i++) {
589  int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
590  int b_dynalloc = dynalloc;
591  int boost_amount = f->alloc_boost[i];
592  quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
593 
594  while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) {
595  int is_boost;
596  if (encode) {
597  is_boost = boost_amount--;
598  ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
599  } else {
600  is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
601  }
602 
603  if (!is_boost)
604  break;
605 
606  boost[i] += quanta;
607  tbits_8ths -= quanta;
608 
609  b_dynalloc = 1;
610  }
611 
612  if (boost[i])
613  dynalloc = FFMAX(dynalloc - 1, 2);
614  }
615 
616  /* Allocation trim */
617  if (!encode)
618  f->alloc_trim = 5;
619  if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
620  if (encode)
622  else
623  f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
624 
625  /* Anti-collapse bit reservation */
626  tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
627  f->anticollapse_needed = 0;
628  if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
629  f->anticollapse_needed = 1 << 3;
630  tbits_8ths -= f->anticollapse_needed;
631 
632  /* Band skip bit reservation */
633  if (tbits_8ths >= 1 << 3)
634  skip_bit = 1 << 3;
635  tbits_8ths -= skip_bit;
636 
637  /* Intensity/dual stereo bit reservation */
638  if (f->channels == 2) {
639  intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
640  if (intensitystereo_bit <= tbits_8ths) {
641  tbits_8ths -= intensitystereo_bit;
642  if (tbits_8ths >= 1 << 3) {
643  dualstereo_bit = 1 << 3;
644  tbits_8ths -= 1 << 3;
645  }
646  } else {
647  intensitystereo_bit = 0;
648  }
649  }
650 
651  /* Trim offsets */
652  for (i = f->start_band; i < f->end_band; i++) {
653  int trim = f->alloc_trim - 5 - f->size;
654  int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
655  int duration = f->size + 3;
656  int scale = duration + f->channels - 1;
657 
658  /* PVQ minimum allocation threshold, below this value the band is
659  * skipped */
660  threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
661  f->channels << 3);
662 
663  trim_offset[i] = trim * (band << scale) >> 6;
664 
665  if (ff_celt_freq_range[i] << f->size == 1)
666  trim_offset[i] -= f->channels << 3;
667  }
668 
669  /* Bisection */
670  low = 1;
671  high = CELT_VECTORS - 1;
672  while (low <= high) {
673  int center = (low + high) >> 1;
674  done = total = 0;
675 
676  for (i = f->end_band - 1; i >= f->start_band; i--) {
677  bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
678 
679  if (bandbits)
680  bandbits = FFMAX(bandbits + trim_offset[i], 0);
681  bandbits += boost[i];
682 
683  if (bandbits >= threshold[i] || done) {
684  done = 1;
685  total += FFMIN(bandbits, f->caps[i]);
686  } else if (bandbits >= f->channels << 3) {
687  total += f->channels << 3;
688  }
689  }
690 
691  if (total > tbits_8ths)
692  high = center - 1;
693  else
694  low = center + 1;
695  }
696  high = low--;
697 
698  /* Bisection */
699  for (i = f->start_band; i < f->end_band; i++) {
701  bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
703 
704  if (bits1[i])
705  bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
706  if (bits2[i])
707  bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
708 
709  if (low)
710  bits1[i] += boost[i];
711  bits2[i] += boost[i];
712 
713  if (boost[i])
714  skip_startband = i;
715  bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
716  }
717 
718  /* Bisection */
719  low = 0;
720  high = 1 << CELT_ALLOC_STEPS;
721  for (i = 0; i < CELT_ALLOC_STEPS; i++) {
722  int center = (low + high) >> 1;
723  done = total = 0;
724 
725  for (j = f->end_band - 1; j >= f->start_band; j--) {
726  bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
727 
728  if (bandbits >= threshold[j] || done) {
729  done = 1;
730  total += FFMIN(bandbits, f->caps[j]);
731  } else if (bandbits >= f->channels << 3)
732  total += f->channels << 3;
733  }
734  if (total > tbits_8ths)
735  high = center;
736  else
737  low = center;
738  }
739 
740  /* Bisection */
741  done = total = 0;
742  for (i = f->end_band - 1; i >= f->start_band; i--) {
743  bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
744 
745  if (bandbits >= threshold[i] || done)
746  done = 1;
747  else
748  bandbits = (bandbits >= f->channels << 3) ?
749  f->channels << 3 : 0;
750 
751  bandbits = FFMIN(bandbits, f->caps[i]);
752  f->pulses[i] = bandbits;
753  total += bandbits;
754  }
755 
756  /* Band skipping */
757  for (f->coded_bands = f->end_band; ; f->coded_bands--) {
758  int allocation;
759  j = f->coded_bands - 1;
760 
761  if (j == skip_startband) {
762  /* all remaining bands are not skipped */
763  tbits_8ths += skip_bit;
764  break;
765  }
766 
767  /* determine the number of bits available for coding "do not skip" markers */
768  remaining = tbits_8ths - total;
769  bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
770  remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
771  allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j];
772  allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
773 
774  /* a "do not skip" marker is only coded if the allocation is
775  * above the chosen threshold */
776  if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
777  int do_not_skip;
778  if (encode) {
779  do_not_skip = f->coded_bands <= f->skip_band_floor;
780  ff_opus_rc_enc_log(rc, do_not_skip, 1);
781  } else {
782  do_not_skip = ff_opus_rc_dec_log(rc, 1);
783  }
784 
785  if (do_not_skip)
786  break;
787 
788  total += 1 << 3;
789  allocation -= 1 << 3;
790  }
791 
792  /* the band is skipped, so reclaim its bits */
793  total -= f->pulses[j];
794  if (intensitystereo_bit) {
795  total -= intensitystereo_bit;
796  intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
797  total += intensitystereo_bit;
798  }
799 
800  total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0;
801  }
802 
803  /* IS start band */
804  if (encode) {
805  if (intensitystereo_bit) {
806  f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
807  ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
808  }
809  } else {
810  f->intensity_stereo = f->dual_stereo = 0;
811  if (intensitystereo_bit)
812  f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
813  }
814 
815  /* DS flag */
816  if (f->intensity_stereo <= f->start_band)
817  tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
818  else if (dualstereo_bit)
819  if (encode)
820  ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
821  else
822  f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
823 
824  /* Supply the remaining bits in this frame to lower bands */
825  remaining = tbits_8ths - total;
826  bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
827  remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
828  for (i = f->start_band; i < f->coded_bands; i++) {
829  const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
830  f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
831  remaining -= bits;
832  }
833 
834  /* Finally determine the allocation */
835  for (i = f->start_band; i < f->coded_bands; i++) {
836  int N = ff_celt_freq_range[i] << f->size;
837  int prev_extra = extrabits;
838  f->pulses[i] += extrabits;
839 
840  if (N > 1) {
841  int dof; /* degrees of freedom */
842  int temp; /* dof * channels * log(dof) */
843  int fine_bits;
844  int max_bits;
845  int offset; /* fine energy quantization offset, i.e.
846  * extra bits assigned over the standard
847  * totalbits/dof */
848 
849  extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
850  f->pulses[i] -= extrabits;
851 
852  /* intensity stereo makes use of an extra degree of freedom */
853  dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
854  temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
855  offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
856  if (N == 2) /* dof=2 is the only case that doesn't fit the model */
857  offset += dof << 1;
858 
859  /* grant an additional bias for the first and second pulses */
860  if (f->pulses[i] + offset < 2 * (dof << 3))
861  offset += temp >> 2;
862  else if (f->pulses[i] + offset < 3 * (dof << 3))
863  offset += temp >> 3;
864 
865  fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
866  max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
867  max_bits = FFMAX(max_bits, 0);
868  f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
869 
870  /* If fine_bits was rounded down or capped,
871  * give priority for the final fine energy pass */
872  f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
873 
874  /* the remaining bits are assigned to PVQ */
875  f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
876  } else {
877  /* all bits go to fine energy except for the sign bit */
878  extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
879  f->pulses[i] -= extrabits;
880  f->fine_bits[i] = 0;
881  f->fine_priority[i] = 1;
882  }
883 
884  /* hand back a limited number of extra fine energy bits to this band */
885  if (extrabits > 0) {
886  int fineextra = FFMIN(extrabits >> (f->channels + 2),
887  CELT_MAX_FINE_BITS - f->fine_bits[i]);
888  f->fine_bits[i] += fineextra;
889 
890  fineextra <<= f->channels + 2;
891  f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
892  extrabits -= fineextra;
893  }
894  }
895  f->remaining = extrabits;
896 
897  /* skipped bands dedicate all of their bits for fine energy */
898  for (; i < f->end_band; i++) {
899  f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
900  f->pulses[i] = 0;
901  f->fine_priority[i] = f->fine_bits[i] < 1;
902  }
903 }
ff_opus_rc_enc_cdf
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
Definition: opus_rc.c:109
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
av_clip
#define av_clip
Definition: common.h:96
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_celt_freq_bands
const uint8_t ff_celt_freq_bands[]
Definition: opustab.c:768
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1043
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:293
opus_default_extradata
static const uint8_t opus_default_extradata[30]
Definition: opus.h:57
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:120
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
ff_opus_rc_enc_uint
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
Definition: opus_rc.c:204
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:180
X
@ X
Definition: vf_addroi.c:26
internal.h
b
#define b
Definition: input.c:40
ff_celt_log_freq_range
const uint8_t ff_celt_log_freq_range[]
Definition: opustab.c:776
ff_opus_parse_packet
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition: opus.c:92
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:539
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: 8svx.c:60
opus_rc_tell
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame,...
Definition: opus_rc.h:61
opus_frame_duration
static const uint16_t opus_frame_duration[32]
Definition: opus.c:38
ff_celt_log2_frac
const uint8_t ff_celt_log2_frac[]
Definition: opustab.c:930
fail
#define fail()
Definition: checkasm.h:127
val
static double val(void *priv, double ch)
Definition: aeval.c:76
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
ff_sqrt
#define ff_sqrt
Definition: mathops.h:206
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
channel_reorder_vorbis
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
Definition: opus.c:283
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
CELT_VECTORS
#define CELT_VECTORS
Definition: opus_celt.h:38
duration
int64_t duration
Definition: movenc.c:64
xiph_lacing_16bit
static int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
Read a 1- or 2-byte frame length.
Definition: opus.c:53
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
ff_opus_rc_dec_uint
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition: opus_rc.c:182
s
#define s(width, name)
Definition: cbs_vp9.c:257
bits1
static const uint8_t bits1[81]
Definition: aactab.c:135
CELT_SPREAD_NORMAL
@ CELT_SPREAD_NORMAL
Definition: opus_celt.h:53
CELT_MAX_FINE_BITS
#define CELT_MAX_FINE_BITS
Definition: opus_celt.h:41
bits
uint8_t bits
Definition: vp3data.h:141
channels
channels
Definition: aptx.h:33
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
SwrContext::channel_map
const int * channel_map
channel index (or -1 if muted channel) map
Definition: swresample_internal.h:113
f
#define f(width, name)
Definition: cbs_vp9.c:255
NORMC
#define NORMC(bits)
Definition: opus.c:554
if
if(ret)
Definition: filter_design.txt:179
ff_celt_bitalloc
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition: opus.c:556
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:66
ff_celt_static_caps
const uint8_t ff_celt_static_caps[4][2][21]
Definition: opustab.c:866
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
channel_reorder_unknown
static int channel_reorder_unknown(int nb_channels, int channel_idx)
Definition: opus.c:288
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:418
CELT_MAX_BANDS
#define CELT_MAX_BANDS
Definition: opus.h:46
ff_celt_model_spread
const uint16_t ff_celt_model_spread[]
Definition: opustab.c:760
ff_vorbis_channel_layout_offsets
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:26
CELT_ALLOC_STEPS
#define CELT_ALLOC_STEPS
Definition: opus_celt.h:39
OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
Definition: opus.h:75
ff_celt_freq_range
const uint8_t ff_celt_freq_range[]
Definition: opustab.c:772
error.h
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
opustab.h
OpusRangeCoder
Definition: opus_rc.h:40
encode
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:95
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
N
#define N
Definition: af_mcompand.c:53
version
version
Definition: libkvazaar.c:313
ff_celt_static_alloc
const uint8_t ff_celt_static_alloc[11][21]
Definition: opustab.c:852
Y
#define Y
Definition: boxblur.h:37
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
ff_opus_rc_dec_cdf
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: opus_rc.c:90
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
vorbis.h
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
opus_celt.h
OPUS_MODE_HYBRID
@ OPUS_MODE_HYBRID
Definition: opus.h:65
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_celt_model_alloc_trim
const uint16_t ff_celt_model_alloc_trim[]
Definition: opustab.c:762
len
int len
Definition: vorbis_enc_data.h:426
opus_rc_tell_frac
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition: opus_rc.h:66
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
CELT_SPREAD_AGGRESSIVE
@ CELT_SPREAD_AGGRESSIVE
Definition: opus_celt.h:54
MAX_PACKET_DUR
#define MAX_PACKET_DUR
Definition: opus.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:383
channel_layout.h
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:64
ff_celt_quant_bands
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus.c:447
cm
#define cm
Definition: dvbsubdec.c:38
OpusPacket
Definition: opus.h:85
ff_opus_rc_enc_log
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
Definition: opus_rc.c:131
temp
else temp
Definition: vf_mcdeint.c:248
ffmath.h
ChannelMap
Definition: opus.h:147
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
CELT_FINE_OFFSET
#define CELT_FINE_OFFSET
Definition: opus_celt.h:40
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
xiph_lacing_full
static int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
Read a multi-byte length (used for code 3 packet padding size)
Definition: opus.c:71
ff_vorbis_channel_layouts
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_opus_rc_dec_log
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition: opus_rc.c:114
int
int
Definition: ffmpeg_filter.c:153
bits2
static const uint8_t bits2[81]
Definition: aactab.c:158
CeltFrame
Definition: opus_celt.h:93
MAX_FRAMES
#define MAX_FRAMES
Definition: diracdec.c:53
nb_channels
int nb_channels
Definition: channel_layout.c:81
OpusContext
Definition: opus.h:162