FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dolby_e.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 foo86
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/float_dsp.h"
22 #include "libavutil/thread.h"
23 #include "libavutil/mem.h"
24 
25 #include "internal.h"
26 #include "get_bits.h"
27 #include "put_bits.h"
28 #include "dolby_e.h"
29 #include "fft.h"
30 
31 static int skip_input(DBEContext *s, int nb_words)
32 {
33  if (nb_words > s->input_size) {
34  av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
35  return AVERROR_INVALIDDATA;
36  }
37 
38  s->input += nb_words * s->word_bytes;
39  s->input_size -= nb_words;
40  return 0;
41 }
42 
43 static int parse_key(DBEContext *s)
44 {
45  if (s->key_present) {
46  uint8_t *key = s->input;
47  int ret = skip_input(s, 1);
48  if (ret < 0)
49  return ret;
50  return AV_RB24(key) >> 24 - s->word_bits;
51  }
52  return 0;
53 }
54 
55 static int convert_input(DBEContext *s, int nb_words, int key)
56 {
57  uint8_t *src = s->input;
58  uint8_t *dst = s->buffer;
59  PutBitContext pb;
60  int i;
61 
62  av_assert0(nb_words <= 1024u);
63 
64  if (nb_words > s->input_size) {
65  av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
66  return AVERROR_INVALIDDATA;
67  }
68 
69  switch (s->word_bits) {
70  case 16:
71  for (i = 0; i < nb_words; i++, src += 2, dst += 2)
72  AV_WB16(dst, AV_RB16(src) ^ key);
73  break;
74  case 20:
75  init_put_bits(&pb, s->buffer, sizeof(s->buffer));
76  for (i = 0; i < nb_words; i++, src += 3)
77  put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
78  flush_put_bits(&pb);
79  break;
80  case 24:
81  for (i = 0; i < nb_words; i++, src += 3, dst += 3)
82  AV_WB24(dst, AV_RB24(src) ^ key);
83  break;
84  default:
85  av_assert0(0);
86  }
87 
88  return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
89 }
90 
92 {
93  int i, ret, key, mtd_size;
94 
95  if ((key = parse_key(s)) < 0)
96  return key;
97  if ((ret = convert_input(s, 1, key)) < 0)
98  return ret;
99 
100  skip_bits(&s->gb, 4);
101  mtd_size = get_bits(&s->gb, 10);
102  if (!mtd_size) {
103  av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
104  return AVERROR_INVALIDDATA;
105  }
106 
107  if ((ret = convert_input(s, mtd_size, key)) < 0)
108  return ret;
109 
110  skip_bits(&s->gb, 14);
111  s->prog_conf = get_bits(&s->gb, 6);
112  if (s->prog_conf > MAX_PROG_CONF) {
113  av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
114  return AVERROR_INVALIDDATA;
115  }
116 
119 
120  s->fr_code = get_bits(&s->gb, 4);
121  s->fr_code_orig = get_bits(&s->gb, 4);
122  if (!sample_rate_tab[s->fr_code] ||
124  av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
125  return AVERROR_INVALIDDATA;
126  }
127 
128  skip_bits_long(&s->gb, 88);
129  for (i = 0; i < s->nb_channels; i++)
130  s->ch_size[i] = get_bits(&s->gb, 10);
131  s->mtd_ext_size = get_bits(&s->gb, 8);
132  s->meter_size = get_bits(&s->gb, 8);
133 
134  skip_bits_long(&s->gb, 10 * s->nb_programs);
135  for (i = 0; i < s->nb_channels; i++) {
136  s->rev_id[i] = get_bits(&s->gb, 4);
137  skip_bits1(&s->gb);
138  s->begin_gain[i] = get_bits(&s->gb, 10);
139  s->end_gain[i] = get_bits(&s->gb, 10);
140  }
141 
142  if (get_bits_left(&s->gb) < 0) {
143  av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
144  return AVERROR_INVALIDDATA;
145  }
146 
147  return skip_input(s, mtd_size + 1);
148 }
149 
151 {
152  if (s->mtd_ext_size)
153  return skip_input(s, s->key_present + s->mtd_ext_size + 1);
154  return 0;
155 }
156 
158 {
159  int mstr_exp[MAX_MSTR_EXP];
160  int bias_exp[MAX_BIAS_EXP];
161  int i, j, k;
162 
163  for (i = 0; i < c->nb_mstr_exp; i++)
164  mstr_exp[i] = get_bits(&s->gb, 2) * 6;
165 
166  for (i = 0; i < g->nb_exponent; i++)
167  bias_exp[i] = get_bits(&s->gb, 5);
168 
169  for (i = k = 0; i < c->nb_mstr_exp; i++)
170  for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
171  c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
172 }
173 
175 {
176  DBEGroup *p, *g;
177  int i;
178 
179  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
180  c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
181  if (c->exp_strategy[i]) {
182  unbias_exponents(s, c, g);
183  } else {
184  memcpy(c->exponents + g->exp_ofs,
185  c->exponents + p->exp_ofs,
186  g->nb_exponent * sizeof(c->exponents[0]));
187  }
188  }
189 
190  return 0;
191 }
192 
193 static inline int log_add(int a, int b)
194 {
195  int c = FFABS(a - b) >> 1;
196  return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
197 }
198 
199 static void calc_lowcomp(int *msk_val)
200 {
201  int lwc_val[17] = { 0 };
202  int i, j, k;
203 
204  for (i = 0; i < 11; i++) {
205  int max_j = 0;
206  int max_v = INT_MIN;
207  int thr = 0;
208 
209  for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
210  int v = msk_val[j] + lwc_gain_tab[i][k];
211  if (v > max_v) {
212  max_j = j;
213  max_v = v;
214  }
215  thr = log_add(thr, v);
216  }
217 
218  if (msk_val[i] < thr) {
219  for (j = FFMAX(max_j - 3, 0),
220  k = FFMAX(3 - max_j, 0);
221  j <= max_j + 3; j++, k++)
222  lwc_val[j] += lwc_adj_tab[k];
223  }
224  }
225 
226  for (i = 0; i < 16; i++) {
227  int v = FFMAX(lwc_val[i], -512);
228  msk_val[i] = FFMAX(msk_val[i] + v, 0);
229  }
230 }
231 
232 static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
233  int *exp, int *bap,
234  int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
235 {
236  int msk_val[MAX_BIAS_EXP];
237  int psd_val[MAX_BIAS_EXP];
238  int fast_leak = 0;
239  int slow_leak = 0;
240  int dc_code = dc_code_tab[fr_code - 1];
241  int ht_code = ht_code_tab[fr_code - 1];
242  int fast_gain = fast_gain_tab[fg_ofs];
243  int slow_decay = slow_decay_tab[dc_code][msk_mod];
244  int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
245  const uint16_t *slow_gain = slow_gain_tab[nb_code][msk_mod];
246  const uint16_t *fast_decay = fast_decay_tab[nb_code][dc_code][msk_mod];
247  const uint16_t *fast_gain_adj = fast_gain_adj_tab[nb_code][dc_code];
248  const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
249  int i;
250 
251  for (i = 0; i < nb_exponent; i++)
252  psd_val[i] = (48 - exp[i]) * 64;
253 
254  fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
255  for (i = 0; i < nb_exponent; i++) {
256  fast_leak = log_add(fast_leak - fast_decay[i],
257  psd_val[i] - fast_gain + fast_gain_adj[i]);
258  slow_leak = log_add(slow_leak - slow_decay,
259  psd_val[i] - slow_gain[i]);
260  msk_val[i] = FFMAX(fast_leak, slow_leak);
261  }
262 
263  fast_leak = 0;
264  for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
265  fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
266  msk_val[i] = FFMAX(msk_val[i], fast_leak);
267  }
268 
269  for (i = 0; i < nb_exponent; i++)
270  msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
271 
272  if (!nb_code)
273  calc_lowcomp(msk_val);
274 
275  for (i = 0; i < nb_exponent; i++) {
276  int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
277  bap[i] = bap_tab[av_clip_uintp2(v, 6)];
278  }
279 }
280 
282 {
283  DBEGroup *p, *g;
284  int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
285  int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
286  int i, snr_ofs;
287 
288  for (i = 0; i < c->nb_groups; i++) {
289  bap_strategy[i] = !i || get_bits1(&s->gb);
290  if (bap_strategy[i]) {
291  fg_spc[i] = get_bits(&s->gb, 2);
292  fg_ofs[i] = get_bits(&s->gb, 3);
293  msk_mod[i] = get_bits1(&s->gb);
294  } else {
295  fg_spc[i] = fg_spc[i - 1];
296  fg_ofs[i] = fg_ofs[i - 1];
297  msk_mod[i] = msk_mod[i - 1];
298  }
299  }
300 
301  if (get_bits1(&s->gb)) {
302  avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
303  return AVERROR_PATCHWELCOME;
304  }
305 
306  snr_ofs = get_bits(&s->gb, 8);
307  if (!snr_ofs) {
308  memset(c->bap, 0, sizeof(c->bap));
309  return 0;
310  }
311 
312  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
313  if (c->exp_strategy[i] || bap_strategy[i]) {
315  c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
316  fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
317  } else {
318  memcpy(c->bap + g->exp_ofs,
319  c->bap + p->exp_ofs,
320  g->nb_exponent * sizeof(c->bap[0]));
321  }
322  }
323 
324  return 0;
325 }
326 
328 {
329  DBEGroup *p, *g;
330  int i, j;
331 
332  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
333  if (get_bits1(&s->gb)) {
334  int start = get_bits(&s->gb, 6);
335 
336  if (start > g->nb_exponent) {
337  av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
338  return AVERROR_INVALIDDATA;
339  }
340 
341  for (j = 0; j < start; j++)
342  c->idx[g->exp_ofs + j] = 0;
343 
344  for (; j < g->nb_exponent; j++)
345  c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
346  } else if (i && g->nb_exponent == p->nb_exponent) {
347  memcpy(c->idx + g->exp_ofs,
348  c->idx + p->exp_ofs,
349  g->nb_exponent * sizeof(c->idx[0]));
350  } else {
351  memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
352  }
353  }
354 
355  return 0;
356 }
357 
359 {
360  DBEGroup *g;
361  int i, j, k;
362 
363  for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
364  float *mnt = c->mantissas + g->mnt_ofs;
365 
366  for (j = 0; j < g->nb_exponent; j++) {
367  int bap = c->bap[g->exp_ofs + j];
368  int idx = c->idx[g->exp_ofs + j];
369  int size1 = mantissa_size1[bap][idx];
370  int count = g->nb_mantissa[j];
371  float exp = exponent_tab[c->exponents[g->exp_ofs + j]];
372  float scale = mantissa_tab1[size1][idx] * exp;
373 
374  if (!size1) {
375  memset(mnt, 0, count * sizeof(*mnt));
376  } else if (idx) {
377  int values[100];
378  int escape = -(1 << size1 - 1);
379 
380  for (k = 0; k < count; k++)
381  values[k] = get_sbits(&s->gb, size1);
382 
383  for (k = 0; k < count; k++) {
384  if (values[k] != escape) {
385  mnt[k] = values[k] * scale;
386  } else {
387  int size2 = mantissa_size2[bap][idx];
388  int value = get_sbits(&s->gb, size2);
389  float a = mantissa_tab2[size2][idx];
390  float b = mantissa_tab3[size2][idx];
391  if (value < 0)
392  mnt[k] = ((value + 1) * a - b) * exp;
393  else
394  mnt[k] = (value * a + b) * exp;
395  }
396  }
397  } else {
398  for (k = 0; k < count; k++)
399  mnt[k] = get_sbits(&s->gb, size1) * scale;
400  }
401 
402  mnt += count;
403  }
404 
405  for (; j < g->nb_exponent + c->bw_code; j++) {
406  memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
407  mnt += g->nb_mantissa[j];
408  }
409  }
410 
411  return 0;
412 }
413 
414 static int parse_channel(DBEContext *s, int ch, int seg_id)
415 {
416  DBEChannel *c = &s->channels[seg_id][ch];
417  int i, ret;
418 
419  if (s->rev_id[ch] > 1) {
420  avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s->rev_id[ch]);
421  return AVERROR_PATCHWELCOME;
422  }
423 
424  if (ch == lfe_channel_tab[s->prog_conf]) {
425  c->gr_code = 3;
426  c->bw_code = 29;
427  } else {
428  c->gr_code = get_bits(&s->gb, 2);
429  c->bw_code = get_bits(&s->gb, 3);
430  if (c->gr_code == 3) {
431  av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
432  return AVERROR_INVALIDDATA;
433  }
434  }
435 
438 
439  for (i = 0; i < c->nb_groups; i++) {
440  c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
441  if (c->nb_mstr_exp == 2) {
442  c->groups[i].nb_exponent -= c->bw_code;
443  c->groups[i].nb_bias_exp[1] -= c->bw_code;
444  }
445  }
446 
447  if ((ret = parse_exponents(s, c)) < 0)
448  return ret;
449  if ((ret = parse_bit_alloc(s, c)) < 0)
450  return ret;
451  if ((ret = parse_indices(s, c)) < 0)
452  return ret;
453  if ((ret = parse_mantissas(s, c)) < 0)
454  return ret;
455 
456  if (get_bits_left(&s->gb) < 0) {
457  av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
458  return AVERROR_INVALIDDATA;
459  }
460 
461  return 0;
462 }
463 
464 static int parse_audio(DBEContext *s, int start, int end, int seg_id)
465 {
466  int ch, ret, key;
467 
468  if ((key = parse_key(s)) < 0)
469  return key;
470 
471  for (ch = start; ch < end; ch++) {
472  if (!s->ch_size[ch]) {
473  s->channels[seg_id][ch].nb_groups = 0;
474  continue;
475  }
476  if ((ret = convert_input(s, s->ch_size[ch], key)) < 0)
477  return ret;
478  if ((ret = parse_channel(s, ch, seg_id)) < 0) {
480  return ret;
481  s->channels[seg_id][ch].nb_groups = 0;
482  }
483  if ((ret = skip_input(s, s->ch_size[ch])) < 0)
484  return ret;
485  }
486 
487  return skip_input(s, 1);
488 }
489 
491 {
492  if (s->meter_size)
493  return skip_input(s, s->key_present + s->meter_size + 1);
494  return 0;
495 }
496 
497 static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
498 {
499  FFTContext *imdct = &s->imdct[g->imdct_idx];
500  int n = 1 << imdct_bits_tab[g->imdct_idx];
501  int n2 = n >> 1;
502  int i;
503 
504  switch (g->imdct_phs) {
505  case 0:
506  imdct->imdct_half(imdct, result, values);
507  for (i = 0; i < n2; i++)
508  result[n2 + i] = result[n2 - i - 1];
509  break;
510  case 1:
511  imdct->imdct_calc(imdct, result, values);
512  break;
513  case 2:
514  imdct->imdct_half(imdct, result + n2, values);
515  for (i = 0; i < n2; i++)
516  result[i] = -result[n - i - 1];
517  break;
518  default:
519  av_assert0(0);
520  }
521 }
522 
523 static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
524 {
525  LOCAL_ALIGNED_32(float, buffer, [2048]);
526  LOCAL_ALIGNED_32(float, result, [1152]);
527  DBEGroup *g;
528  int i;
529 
530  memset(result, 0, 1152 * sizeof(float));
531  for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
532  float *src = buffer + g->src_ofs;
533  float *dst = result + g->dst_ofs;
534  float *win = window + g->win_ofs;
535 
536  imdct_calc(s, g, buffer, c->mantissas + g->mnt_ofs);
537  s->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
538  }
539 
540  for (i = 0; i < 256; i++)
541  output[i] = history[i] + result[i];
542  for (i = 256; i < 896; i++)
543  output[i] = result[i];
544  for (i = 0; i < 256; i++)
545  history[i] = result[896 + i];
546 }
547 
548 static void apply_gain(DBEContext *s, int begin, int end, float *output)
549 {
550  if (begin == 960 && end == 960)
551  return;
552 
553  if (begin == end) {
554  s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
555  } else {
556  float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
557  float b = gain_tab[end ] * (1.0f / (FRAME_SAMPLES - 1));
558  int i;
559 
560  for (i = 0; i < FRAME_SAMPLES; i++)
561  output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
562  }
563 }
564 
566 {
567  const uint8_t *reorder;
568  int ch, ret;
569 
570  if (s->nb_channels == 4)
571  reorder = ch_reorder_4;
572  else if (s->nb_channels == 6)
573  reorder = ch_reorder_6;
574  else if (s->nb_programs == 1 && !(s->avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE))
575  reorder = ch_reorder_8;
576  else
577  reorder = ch_reorder_n;
578 
579  frame->nb_samples = FRAME_SAMPLES;
580  if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
581  return ret;
582 
583  for (ch = 0; ch < s->nb_channels; ch++) {
584  float *output = (float *)frame->extended_data[reorder[ch]];
585  transform(s, &s->channels[0][ch], s->history[ch], output);
586  transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
587  apply_gain(s, s->begin_gain[ch], s->end_gain[ch], output);
588  }
589 
590  return 0;
591 }
592 
593 static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
594  int *got_frame_ptr, AVPacket *avpkt)
595 {
596  DBEContext *s = avctx->priv_data;
597  int i, j, hdr, ret;
598 
599  if (avpkt->size < 3)
600  return AVERROR_INVALIDDATA;
601 
602  hdr = AV_RB24(avpkt->data);
603  if ((hdr & 0xfffffe) == 0x7888e) {
604  s->word_bits = 24;
605  } else if ((hdr & 0xffffe0) == 0x788e0) {
606  s->word_bits = 20;
607  } else if ((hdr & 0xfffe00) == 0x78e00) {
608  s->word_bits = 16;
609  } else {
610  av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
611  return AVERROR_INVALIDDATA;
612  }
613 
614  s->word_bytes = s->word_bits + 7 >> 3;
615  s->input = avpkt->data + s->word_bytes;
616  s->input_size = avpkt->size / s->word_bytes - 1;
617  s->key_present = hdr >> 24 - s->word_bits & 1;
618 
619  if ((ret = parse_metadata(s)) < 0)
620  return ret;
621 
622  if (s->nb_programs > 1 && !s->multi_prog_warned) {
623  av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
624  "channels will be output in native order.\n", s->nb_programs, s->prog_conf);
625  s->multi_prog_warned = 1;
626  }
627 
628  switch (s->nb_channels) {
629  case 4:
631  break;
632  case 6:
634  break;
635  case 8:
637  break;
638  }
639 
640  avctx->channels = s->nb_channels;
641  avctx->sample_rate = sample_rate_tab[s->fr_code];
643 
644  i = s->nb_channels / 2;
645  j = s->nb_channels;
646  if ((ret = parse_audio(s, 0, i, 0)) < 0)
647  return ret;
648  if ((ret = parse_audio(s, i, j, 0)) < 0)
649  return ret;
650  if ((ret = parse_metadata_ext(s)) < 0)
651  return ret;
652  if ((ret = parse_audio(s, 0, i, 1)) < 0)
653  return ret;
654  if ((ret = parse_audio(s, i, j, 1)) < 0)
655  return ret;
656  if ((ret = parse_meter(s)) < 0)
657  return ret;
658  if ((ret = filter_frame(s, data)) < 0)
659  return ret;
660 
661  *got_frame_ptr = 1;
662  return avpkt->size;
663 }
664 
666 {
667  DBEContext *s = avctx->priv_data;
668 
669  memset(s->history, 0, sizeof(s->history));
670 }
671 
673 {
674  DBEContext *s = avctx->priv_data;
675  int i;
676 
677  for (i = 0; i < 3; i++)
678  ff_mdct_end(&s->imdct[i]);
679 
680  av_freep(&s->fdsp);
681  return 0;
682 }
683 
684 
685 static av_cold void init_tables(void)
686 {
687  int i, j;
688 
689  for (i = 1; i < 17; i++)
690  mantissa_tab1[i][0] = 1.0f / (1 << i - 1);
691 
692  for (i = 2; i < 16; i++) {
693  mantissa_tab1[i][1] = 1.0f / ((1 << i) - 1);
694  mantissa_tab1[i][2] = 0.5f / ((1 << i) - 1);
695  mantissa_tab1[i][3] = 0.25f / ((1 << i) - 1);
696  }
697 
698  mantissa_tab1[i][1] = 0.5f / (1 << 15);
699  mantissa_tab1[i][2] = 0.75f / (1 << 15);
700  mantissa_tab1[i][3] = 0.875f / (1 << 15);
701 
702  for (i = 1; i < 17; i++) {
703  mantissa_tab2[i][1] = mantissa_tab1[i][0] * 0.5f;
704  mantissa_tab2[i][2] = mantissa_tab1[i][0] * 0.75f;
705  mantissa_tab2[i][3] = mantissa_tab1[i][0] * 0.875f;
706  for (j = 1; j < 4; j++)
707  mantissa_tab3[i][j] = 1.0f / (1 << i) + 1.0f / (1 << j) - 1.0f / (1 << i + j);
708  }
709 
710  mantissa_tab3[1][3] = 0.6875f;
711 
712  for (i = 0; i < 25; i++) {
713  exponent_tab[i * 2 ] = 1.0f / (1 << i);
714  exponent_tab[i * 2 + 1] = M_SQRT1_2 / (1 << i);
715  }
716 
717  for (i = 1; i < 1024; i++)
718  gain_tab[i] = exp2f((i - 960) / 64.0f);
719 
720  // short 1
721  ff_kbd_window_init(window, 3.0f, 128);
722  for (i = 0; i < 128; i++)
723  window[128 + i] = window[127 - i];
724 
725  // start
726  for (i = 0; i < 192; i++)
727  window[256 + i] = start_window[i];
728 
729  // short 2
730  for (i = 0; i < 192; i++)
731  window[448 + i] = short_window2[i];
732  for (i = 0; i < 64; i++)
733  window[640 + i] = window[63 - i];
734 
735  // short 3
736  for (i = 0; i < 64; i++)
737  window[704 + i] = short_window3[i];
738  for (i = 0; i < 192; i++)
739  window[768 + i] = window[64 + i];
740 
741  // bridge
742  for (i = 0; i < 128; i++)
743  window[960 + i] = window[i];
744  for (i = 0; i < 64; i++)
745  window[1088 + i] = 1.0f;
746 
747  // long
748  ff_kbd_window_init(window + 1408, 3.0f, 256);
749  for (i = 0; i < 640; i++)
750  window[1664 + i] = 1.0f;
751  for (i = 0; i < 256; i++)
752  window[2304 + i] = window[1152 + i] = window[1663 - i];
753 
754  // reverse start
755  for (i = 0; i < 192; i++)
756  window[2560 + i] = window[447 - i];
757 
758  // reverse short 2
759  for (i = 0; i < 256; i++)
760  window[2752 + i] = window[703 - i];
761 
762  // reverse short 3
763  for (i = 0; i < 256; i++)
764  window[3008 + i] = window[959 - i];
765 
766  // reverse bridge
767  for (i = 0; i < 448; i++)
768  window[3264 + i] = window[1407 - i];
769 }
770 
772 {
773  static AVOnce init_once = AV_ONCE_INIT;
774  DBEContext *s = avctx->priv_data;
775  int i;
776 
777  if (ff_thread_once(&init_once, init_tables))
778  return AVERROR_UNKNOWN;
779 
780  for (i = 0; i < 3; i++)
781  if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
782  return AVERROR(ENOMEM);
783 
784  if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
785  return AVERROR(ENOMEM);
786 
788  s->avctx = avctx;
789  return 0;
790 }
791 
793  .name = "dolby_e",
794  .long_name = NULL_IF_CONFIG_SMALL("Dolby E"),
795  .type = AVMEDIA_TYPE_AUDIO,
796  .id = AV_CODEC_ID_DOLBY_E,
797  .priv_data_size = sizeof(DBEContext),
798  .init = dolby_e_init,
800  .close = dolby_e_close,
801  .flush = dolby_e_flush,
805 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static int parse_bit_alloc(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:281
uint16_t src_ofs
Definition: dolby_e.h:58
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
#define AV_CH_LAYOUT_7POINT1
int fr_code_orig
Definition: dolby_e.h:93
int exp_strategy[MAX_GROUPS]
Definition: dolby_e.h:69
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const uint8_t mantissa_size2[16][4]
Definition: dolby_e.h:232
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
static const uint8_t nb_groups_tab[4]
Definition: dolby_e.h:137
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
static const uint8_t mantissa_size1[16][4]
Definition: dolby_e.h:225
static float mantissa_tab2[17][4]
Definition: dolby_e.h:640
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:381
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
Generate a Kaiser-Bessel Derived Window.
Definition: kbdwin.c:26
uint8_t nb_bias_exp[MAX_MSTR_EXP]
Definition: dolby_e.h:49
Memory handling functions.
uint16_t win_len
Definition: dolby_e.h:55
static float win(SuperEqualizerContext *s, float n, int N)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:293
const char * g
Definition: vf_curves.c:115
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
DBEGroup groups[MAX_GROUPS]
Definition: dolby_e.h:67
static const uint16_t fast_gain_adj_tab[3][2][62]
Definition: dolby_e.h:458
static const uint8_t bap_tab[64]
Definition: dolby_e.h:632
#define M_SQRT1_2
Definition: mathematics.h:58
int size
Definition: avcodec.h:1446
const char * b
Definition: vf_curves.c:116
uint16_t win_ofs
Definition: dolby_e.h:57
int nb_mstr_exp
Definition: dolby_e.h:66
#define AV_CH_LAYOUT_4POINT0
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:1020
const char * key
static const uint8_t nb_mstr_exp_tab[4]
Definition: dolby_e.h:139
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define src
Definition: vp8dsp.c:254
static float mantissa_tab3[17][4]
Definition: dolby_e.h:641
AVCodec.
Definition: avcodec.h:3424
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:361
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define MAX_BIAS_EXP
Definition: dolby_e.h:45
static int parse_metadata(DBEContext *s)
Definition: dolby_e.c:91
uint8_t imdct_phs
Definition: dolby_e.h:54
static const uint8_t ch_reorder_4[4]
Definition: dolby_e.h:128
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int multi_prog_warned
Definition: dolby_e.h:103
#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
AVCodecContext * avctx
Definition: dolby_e.h:78
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2197
uint8_t
static const uint8_t log_add_tab[212]
Definition: dolby_e.h:615
#define av_cold
Definition: attributes.h:82
uint8_t buffer[1024 *3+AV_INPUT_BUFFER_PADDING_SIZE]
Definition: dolby_e.h:112
static int dolby_e_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: dolby_e.c:593
int idx[MAX_EXPONENTS]
Definition: dolby_e.h:72
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
GetBitContext gb
Definition: dolby_e.h:79
static const uint8_t dc_code_tab[5]
Definition: dolby_e.h:364
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static AVFrame * frame
static void apply_gain(DBEContext *s, int begin, int end, float *output)
Definition: dolby_e.c:548
uint8_t * data
Definition: avcodec.h:1445
static const int16_t lwc_gain_tab[11][7]
Definition: dolby_e.h:597
uint16_t mnt_ofs
Definition: dolby_e.h:51
static const uint8_t imdct_bits_tab[3]
Definition: dolby_e.h:160
bitstream reader API header.
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define AVOnce
Definition: thread.h:159
#define av_log(a,...)
int ch_size[MAX_CHANNELS]
Definition: dolby_e.h:95
int key_present
Definition: dolby_e.h:86
#define MAX_GROUPS
Definition: dolby_e.h:40
#define AV_CH_LAYOUT_5POINT1
int fr_code
Definition: dolby_e.h:92
static int parse_meter(DBEContext *s)
Definition: dolby_e.c:490
static int parse_key(DBEContext *s)
Definition: dolby_e.c:43
static const uint8_t ch_reorder_8[8]
Definition: dolby_e.h:130
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:814
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
AVFloatDSPContext * fdsp
Definition: dolby_e.h:110
int gr_code
Definition: dolby_e.h:62
#define AVERROR(e)
Definition: error.h:43
static int parse_audio(DBEContext *s, int start, int end, int seg_id)
Definition: dolby_e.c:464
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int nb_groups
Definition: dolby_e.h:65
#define MAX_PROG_CONF
Definition: dolby_e.h:35
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static const uint8_t band_low_tab[3]
Definition: dolby_e.h:372
int end_gain[MAX_CHANNELS]
Definition: dolby_e.h:101
#define ff_mdct_init
Definition: fft.h:169
#define MAX_MSTR_EXP
Definition: dolby_e.h:44
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
AVCodec ff_dolby_e_decoder
Definition: dolby_e.c:792
int8_t exp
Definition: eval.c:72
static int parse_indices(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:327
static av_cold int dolby_e_init(AVCodecContext *avctx)
Definition: dolby_e.c:771
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2240
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:107
#define FRAME_SAMPLES
Definition: dolby_e.h:33
static SDL_Window * window
Definition: ffplay.c:365
static const uint8_t ch_reorder_6[6]
Definition: dolby_e.h:129
uint8_t * input
Definition: dolby_e.h:81
int mtd_ext_size
Definition: dolby_e.h:96
uint8_t imdct_idx
Definition: dolby_e.h:53
static const uint16_t sample_rate_tab[16]
Definition: dolby_e.h:133
int prog_conf
Definition: dolby_e.h:88
Definition: fft.h:88
static const uint8_t nb_programs_tab[MAX_PROG_CONF+1]
Definition: dolby_e.h:115
int bap[MAX_EXPONENTS]
Definition: dolby_e.h:71
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2658
#define FFMIN(a, b)
Definition: common.h:96
int begin_gain[MAX_CHANNELS]
Definition: dolby_e.h:100
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static int parse_channel(DBEContext *s, int ch, int seg_id)
Definition: dolby_e.c:414
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
int meter_size
Definition: dolby_e.h:97
static const float short_window2[192]
Definition: dolby_e.h:292
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
int word_bytes
Definition: dolby_e.h:85
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2669
int n
Definition: avisynth_c.h:684
float history[MAX_CHANNELS][256]
Definition: dolby_e.h:107
static const uint16_t fast_gain_tab[8]
Definition: dolby_e.h:374
int nb_programs
Definition: dolby_e.h:90
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int input_size
Definition: dolby_e.h:82
static const float start_window[192]
Definition: dolby_e.h:239
static const uint8_t ch_reorder_n[8]
Definition: dolby_e.h:131
float mantissas[MAX_MANTISSAS]
Definition: dolby_e.h:74
static const uint16_t hearing_thresh_tab[3][3][50]
Definition: dolby_e.h:542
#define exp2f(x)
Definition: libm.h:293
int nb_channels
Definition: dolby_e.h:89
#define AV_ONCE_INIT
Definition: thread.h:160
static const int8_t lfe_channel_tab[MAX_PROG_CONF+1]
Definition: dolby_e.h:123
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
int sample_rate
samples per second
Definition: avcodec.h:2189
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
main external API structure.
Definition: avcodec.h:1533
static const uint16_t misc_decay_tab[3][2][2]
Definition: dolby_e.h:380
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
static void calc_lowcomp(int *msk_val)
Definition: dolby_e.c:199
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:108
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the entry wise product of two vectors of floats, add a third vector of floats and store the...
Definition: float_dsp.h:137
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:523
static const uint8_t nb_channels_tab[MAX_PROG_CONF+1]
Definition: dolby_e.h:119
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:460
static int parse_mantissas(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:358
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:615
static const uint8_t band_ofs_tab[3][4]
Definition: dolby_e.h:368
uint16_t dst_ofs
Definition: dolby_e.h:56
static float exponent_tab[50]
Definition: dolby_e.h:642
static int log_add(int a, int b)
Definition: dolby_e.c:193
static av_cold void dolby_e_flush(AVCodecContext *avctx)
Definition: dolby_e.c:665
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int filter_frame(DBEContext *s, AVFrame *frame)
Definition: dolby_e.c:565
static float mantissa_tab1[17][4]
Definition: dolby_e.h:639
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
FFTContext imdct[3]
Definition: dolby_e.h:109
static void bit_allocate(int nb_exponent, int nb_code, int fr_code, int *exp, int *bap, int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
Definition: dolby_e.c:232
const uint8_t * nb_mantissa
Definition: dolby_e.h:52
static const float short_window3[64]
Definition: dolby_e.h:345
common internal api header.
static const uint16_t slow_decay_tab[2][2]
Definition: dolby_e.h:378
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
DBEChannel channels[MAX_SEGMENTS][MAX_CHANNELS]
Definition: dolby_e.h:105
static const uint8_t ht_code_tab[5]
Definition: dolby_e.h:366
#define ff_mdct_end
Definition: fft.h:170
static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
Definition: dolby_e.c:523
static double c[64]
int rev_id[MAX_CHANNELS]
Definition: dolby_e.h:99
static float gain_tab[1024]
Definition: dolby_e.h:643
static const DBEGroup *const frm_ofs_tab[2][4]
Definition: dolby_e.h:220
static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
Definition: dolby_e.c:497
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * priv_data
Definition: avcodec.h:1560
static int parse_metadata_ext(DBEContext *s)
Definition: dolby_e.c:150
static const uint16_t fast_decay_tab[3][2][2][50]
Definition: dolby_e.h:386
int exponents[MAX_EXPONENTS]
Definition: dolby_e.h:70
int channels
number of audio channels
Definition: avcodec.h:2190
static int parse_exponents(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:174
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:162
uint16_t exp_ofs
Definition: dolby_e.h:50
static int skip_input(DBEContext *s, int nb_words)
Definition: dolby_e.c:31
static int convert_input(DBEContext *s, int nb_words, int key)
Definition: dolby_e.c:55
static av_cold void init_tables(void)
Definition: dolby_e.c:685
uint8_t nb_exponent
Definition: dolby_e.h:48
static const int16_t lwc_adj_tab[7]
Definition: dolby_e.h:611
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
int bw_code
Definition: dolby_e.h:63
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
static const uint16_t slow_gain_tab[3][2][50]
Definition: dolby_e.h:504
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:2247
This structure stores compressed data.
Definition: avcodec.h:1422
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
Definition: dolby_e.c:157
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
int word_bits
Definition: dolby_e.h:84
for(j=16;j >0;--j)
GLuint buffer
Definition: opengl_enc.c:102
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 av_cold int dolby_e_close(AVCodecContext *avctx)
Definition: dolby_e.c:672
bitstream writer API