FFmpeg
aacdec_lpd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 Lynne <dev@lynne.ee>
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 "aacdec_lpd.h"
22 #include "aacdec_usac.h"
23 #include "libavcodec/unary.h"
24 
25 static void parse_qn(GetBitContext *gb, int *qn, int nk_mode, int no_qn)
26 {
27  if (nk_mode == 1) {
28  for (int k = 0; k < no_qn; k++) {
29  qn[k] = get_unary(gb, 0, 68); // TODO: find proper ranges
30  if (qn[k])
31  qn[k]++;
32  }
33  return;
34  }
35 
36  for (int k = 0; k < no_qn; k++)
37  qn[k] = get_bits(gb, 2) + 2;
38 
39  if (nk_mode == 2) {
40  for (int k = 0; k < no_qn; k++) {
41  if (qn[k] > 4) {
42  qn[k] = get_unary(gb, 0, 65);
43  if (qn[k])
44  qn[k] += 4;
45  }
46  }
47  return;
48  }
49 
50  for (int k = 0; k < no_qn; k++) {
51  if (qn[k] > 4) {
52  int qn_ext = get_unary(gb, 0, 65);
53  switch (qn_ext) {
54  case 0: qn[k] = 5; break;
55  case 1: qn[k] = 6; break;
56  case 2: qn[k] = 0; break;
57  default: qn[k] = qn_ext + 4; break;
58  }
59  }
60  }
61 }
62 
63 static int parse_codebook_idx(GetBitContext *gb, uint32_t *kv,
64  int nk_mode, int no_qn)
65 {
66  int n, nk;
67 
68  int qn[2];
69  parse_qn(gb, qn, nk_mode, no_qn);
70 
71  for (int k = 0; k < no_qn; k++) {
72  if (qn[k] > 4) {
73  nk = (qn[k] - 3) / 2;
74  n = qn[k] - nk*2;
75  } else {
76  nk = 0;
77  n = qn[k];
78  }
79  }
80 
81  if (nk > 25)
82  return AVERROR_PATCHWELCOME;
83 
84  skip_bits(gb, 4*n);
85 
86  if (nk > 0)
87  for (int i = 0; i < 8; i++)
88  kv[i] = get_bits(gb, nk);
89 
90  return 0;
91 }
92 
94  int use_gain, int len)
95 {
96  int ret;
97  if (use_gain)
98  ce->fac.gain = get_bits(gb, 7);
99 
100  if (len/8 > 8)
101  return AVERROR_PATCHWELCOME;
102 
103  for (int i = 0; i < len/8; i++) {
104  ret = parse_codebook_idx(gb, ce->fac.kv[i], 1, 1);
105  if (ret < 0)
106  return ret;
107  }
108 
109  return 0;
110 }
111 
114 {
115  int first_ldp_flag;
116 
117  ce->ldp.acelp_core_mode = get_bits(gb, 3);
118  ce->ldp.lpd_mode = get_bits(gb, 5);
119 
120  ce->ldp.bpf_control_info = get_bits1(gb);
121  ce->ldp.core_mode_last = get_bits1(gb);
122  ce->ldp.fac_data_present = get_bits1(gb);
123 
124  first_ldp_flag = !ce->ldp.core_mode_last;
125  if (first_ldp_flag)
126  ce->ldp.last_lpd_mode = -1; /* last_ldp_mode is a **STATEFUL** value */
127 
128  if (!ce->ldp.core_mode_last && ce->ldp.fac_data_present) {
129  uint16_t len_8 = usac->core_frame_len / 8;
130  uint16_t len_16 = usac->core_frame_len / 16;
131  uint16_t fac_len = get_bits1(gb) /* short_fac_flag */ ? len_8 : len_16;
132  int ret = ff_aac_parse_fac_data(ce, gb, 1, fac_len);
133  if (ret < 0)
134  return ret;
135  }
136 
137  return 0;
138 }
AACUSACConfig
Definition: aacdec.h:351
parse_qn
static void parse_qn(GetBitContext *gb, int *qn, int nk_mode, int no_qn)
Definition: aacdec_lpd.c:25
AACUsacElemData::acelp_core_mode
uint8_t acelp_core_mode
Definition: aacdec.h:135
aacdec_usac.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:364
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
AACUsacElemData::gain
uint8_t gain
Definition: aacdec.h:152
GetBitContext
Definition: get_bits.h:108
AACUsacElemData::kv
uint32_t kv[8][8]
Definition: aacdec.h:153
AACUsacElemData::last_lpd_mode
int last_lpd_mode
Definition: aacdec.h:142
AACUsacElemData::core_mode_last
uint8_t core_mode_last
Definition: aacdec.h:139
AACUsacElemData
Definition: aacdec.h:125
ff_aac_ldp_parse_channel_stream
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemData *ce, GetBitContext *gb)
Definition: aacdec_lpd.c:112
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AACUSACConfig::core_frame_len
uint16_t core_frame_len
Definition: aacdec.h:353
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
parse_codebook_idx
static int parse_codebook_idx(GetBitContext *gb, uint32_t *kv, int nk_mode, int no_qn)
Definition: aacdec_lpd.c:63
unary.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_aac_parse_fac_data
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb, int use_gain, int len)
Definition: aacdec_lpd.c:93
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
aacdec_lpd.h
AACDecContext
main AAC decoding context
Definition: aacdec.h:448
AACUsacElemData::fac_data_present
uint8_t fac_data_present
Definition: aacdec.h:140
AACUsacElemData::fac
struct AACUsacElemData::@16 fac
AACUsacElemData::ldp
struct AACUsacElemData::@14 ldp
AACUsacElemData::lpd_mode
uint8_t lpd_mode
Definition: aacdec.h:136
AACUsacElemData::bpf_control_info
uint8_t bpf_control_info
Definition: aacdec.h:138