| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 93 | ✗ | int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb, | |
| 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 | |||
| 112 | ✗ | int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac, | |
| 113 | AACUsacElemData *ce, GetBitContext *gb) | ||
| 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 | } | ||
| 139 |