FFmpeg
Loading...
Searching...
No Matches
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
25static 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
63static 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)
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)
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
121 ce->ldp.core_mode_last = 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}
static void parse_qn(GetBitContext *gb, int *qn, int nk_mode, int no_qn)
Definition aacdec_lpd.c:25
static int parse_codebook_idx(GetBitContext *gb, uint32_t *kv, int nk_mode, int no_qn)
Definition aacdec_lpd.c:63
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemData *ce, GetBitContext *gb)
Definition aacdec_lpd.c:112
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb, int use_gain, int len)
Definition aacdec_lpd.c:93
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
main AAC decoding context
Definition aacdec.h:500
uint16_t core_frame_len
Definition aacdec.h:398
struct AACUsacElemData::@170324333014322114255251102173201272223305376176 ldp
uint8_t gain
Definition aacdec.h:159
uint8_t acelp_core_mode
Definition aacdec.h:142
struct AACUsacElemData::@261150123320056304135021253254164365325070141151 fac
uint8_t core_mode_last
Definition aacdec.h:146
int last_lpd_mode
Definition aacdec.h:149
uint8_t fac_data_present
Definition aacdec.h:147
uint8_t lpd_mode
Definition aacdec.h:143
uint8_t bpf_control_info
Definition aacdec.h:145
uint32_t kv[8][8]
Definition aacdec.h:160
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition unary.h:46
int len