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