| 1 | /*
|
|---|
| 2 | * DCA compatible decoder
|
|---|
| 3 | * Copyright (C) 2004 Gildas Bazin
|
|---|
| 4 | * Copyright (C) 2004 Benjamin Zores
|
|---|
| 5 | * Copyright (C) 2006 Benjamin Larsson
|
|---|
| 6 | * Copyright (C) 2007 Konstantin Shishkov
|
|---|
| 7 | *
|
|---|
| 8 | * This file is part of FFmpeg.
|
|---|
| 9 | *
|
|---|
| 10 | * FFmpeg is free software; you can redistribute it and/or
|
|---|
| 11 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 12 | * License as published by the Free Software Foundation; either
|
|---|
| 13 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 14 | *
|
|---|
| 15 | * FFmpeg is distributed in the hope that it will be useful,
|
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 18 | * Lesser General Public License for more details.
|
|---|
| 19 | *
|
|---|
| 20 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 21 | * License along with FFmpeg; if not, write to the Free Software
|
|---|
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include <math.h>
|
|---|
| 26 | #include <stddef.h>
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 |
|
|---|
| 29 | #include "libavutil/common.h"
|
|---|
| 30 | #include "libavutil/float_dsp.h"
|
|---|
| 31 | #include "libavutil/intmath.h"
|
|---|
| 32 | #include "libavutil/intreadwrite.h"
|
|---|
| 33 | #include "libavutil/mathematics.h"
|
|---|
| 34 | #include "libavutil/audioconvert.h"
|
|---|
| 35 | #include "avcodec.h"
|
|---|
| 36 | #include "dsputil.h"
|
|---|
| 37 | #include "fft.h"
|
|---|
| 38 | #include "get_bits.h"
|
|---|
| 39 | #include "put_bits.h"
|
|---|
| 40 | #include "dcadata.h"
|
|---|
| 41 | #include "dcahuff.h"
|
|---|
| 42 | #include "dca.h"
|
|---|
| 43 | #include "dca_parser.h"
|
|---|
| 44 | #include "synth_filter.h"
|
|---|
| 45 | #include "dcadsp.h"
|
|---|
| 46 | #include "fmtconvert.h"
|
|---|
| 47 |
|
|---|
| 48 | #if ARCH_ARM
|
|---|
| 49 | # include "arm/dca.h"
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | //#define TRACE
|
|---|
| 53 |
|
|---|
| 54 | #define DCA_PRIM_CHANNELS_MAX (7)
|
|---|
| 55 | #define DCA_SUBBANDS (64)
|
|---|
| 56 | #define DCA_ABITS_MAX (32) /* Should be 28 */
|
|---|
| 57 | #define DCA_SUBSUBFRAMES_MAX (4)
|
|---|
| 58 | #define DCA_SUBFRAMES_MAX (16)
|
|---|
| 59 | #define DCA_BLOCKS_MAX (16)
|
|---|
| 60 | #define DCA_LFE_MAX (3)
|
|---|
| 61 | #define DCA_CHSETS_MAX (4)
|
|---|
| 62 | #define DCA_CHSET_CHANS_MAX (8)
|
|---|
| 63 |
|
|---|
| 64 | enum DCAMode {
|
|---|
| 65 | DCA_MONO = 0,
|
|---|
| 66 | DCA_CHANNEL,
|
|---|
| 67 | DCA_STEREO,
|
|---|
| 68 | DCA_STEREO_SUMDIFF,
|
|---|
| 69 | DCA_STEREO_TOTAL,
|
|---|
| 70 | DCA_3F,
|
|---|
| 71 | DCA_2F1R,
|
|---|
| 72 | DCA_3F1R,
|
|---|
| 73 | DCA_2F2R,
|
|---|
| 74 | DCA_3F2R,
|
|---|
| 75 | DCA_4F2R
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | /* these are unconfirmed but should be mostly correct */
|
|---|
| 79 | enum DCAExSSSpeakerMask {
|
|---|
| 80 | DCA_EXSS_FRONT_CENTER = 0x0001,
|
|---|
| 81 | DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
|
|---|
| 82 | DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
|
|---|
| 83 | DCA_EXSS_LFE = 0x0008,
|
|---|
| 84 | DCA_EXSS_REAR_CENTER = 0x0010,
|
|---|
| 85 | DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
|
|---|
| 86 | DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
|
|---|
| 87 | DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
|
|---|
| 88 | DCA_EXSS_OVERHEAD = 0x0100,
|
|---|
| 89 | DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
|
|---|
| 90 | DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
|
|---|
| 91 | DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
|
|---|
| 92 | DCA_EXSS_LFE2 = 0x1000,
|
|---|
| 93 | DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
|
|---|
| 94 | DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
|
|---|
| 95 | DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | enum DCAXxchSpeakerMask {
|
|---|
| 99 | DCA_XXCH_FRONT_CENTER = 0x0000001,
|
|---|
| 100 | DCA_XXCH_FRONT_LEFT = 0x0000002,
|
|---|
| 101 | DCA_XXCH_FRONT_RIGHT = 0x0000004,
|
|---|
| 102 | DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
|
|---|
| 103 | DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
|
|---|
| 104 | DCA_XXCH_LFE1 = 0x0000020,
|
|---|
| 105 | DCA_XXCH_REAR_CENTER = 0x0000040,
|
|---|
| 106 | DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
|
|---|
| 107 | DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
|
|---|
| 108 | DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
|
|---|
| 109 | DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
|
|---|
| 110 | DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
|
|---|
| 111 | DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
|
|---|
| 112 | DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
|
|---|
| 113 | DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
|
|---|
| 114 | DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
|
|---|
| 115 | DCA_XXCH_LFE2 = 0x0010000,
|
|---|
| 116 | DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
|
|---|
| 117 | DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
|
|---|
| 118 | DCA_XXCH_OVERHEAD = 0x0080000,
|
|---|
| 119 | DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
|
|---|
| 120 | DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
|
|---|
| 121 | DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
|
|---|
| 122 | DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
|
|---|
| 123 | DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
|
|---|
| 124 | DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
|
|---|
| 125 | DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
|
|---|
| 126 | DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | static const uint32_t map_xxch_to_native[28] = {
|
|---|
| 130 | AV_CH_FRONT_CENTER,
|
|---|
| 131 | AV_CH_FRONT_LEFT,
|
|---|
| 132 | AV_CH_FRONT_RIGHT,
|
|---|
| 133 | AV_CH_SIDE_LEFT,
|
|---|
| 134 | AV_CH_SIDE_RIGHT,
|
|---|
| 135 | AV_CH_LOW_FREQUENCY,
|
|---|
| 136 | AV_CH_BACK_CENTER,
|
|---|
| 137 | AV_CH_BACK_LEFT,
|
|---|
| 138 | AV_CH_BACK_RIGHT,
|
|---|
| 139 | AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
|
|---|
| 140 | AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
|
|---|
| 141 | AV_CH_FRONT_LEFT_OF_CENTER,
|
|---|
| 142 | AV_CH_FRONT_RIGHT_OF_CENTER,
|
|---|
| 143 | AV_CH_TOP_FRONT_LEFT,
|
|---|
| 144 | AV_CH_TOP_FRONT_CENTER,
|
|---|
| 145 | AV_CH_TOP_FRONT_RIGHT,
|
|---|
| 146 | AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
|
|---|
| 147 | AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
|
|---|
| 148 | AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
|
|---|
| 149 | AV_CH_TOP_CENTER, /* overhead */
|
|---|
| 150 | AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
|
|---|
| 151 | AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
|
|---|
| 152 | AV_CH_TOP_BACK_CENTER,
|
|---|
| 153 | AV_CH_TOP_BACK_LEFT,
|
|---|
| 154 | AV_CH_TOP_BACK_RIGHT,
|
|---|
| 155 | AV_CH_BACK_CENTER, /* rear low center -- dup */
|
|---|
| 156 | AV_CH_BACK_LEFT, /* rear low left -- dup */
|
|---|
| 157 | AV_CH_BACK_RIGHT /* read low right -- dup */
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| 160 | enum DCAExtensionMask {
|
|---|
| 161 | DCA_EXT_CORE = 0x001, ///< core in core substream
|
|---|
| 162 | DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
|
|---|
| 163 | DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
|
|---|
| 164 | DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
|
|---|
| 165 | DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
|
|---|
| 166 | DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
|
|---|
| 167 | DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
|
|---|
| 168 | DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
|
|---|
| 169 | DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
|
|---|
| 170 | DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
|
|---|
| 171 | };
|
|---|
| 172 |
|
|---|
| 173 | /* -1 are reserved or unknown */
|
|---|
| 174 | static const int dca_ext_audio_descr_mask[] = {
|
|---|
| 175 | DCA_EXT_XCH,
|
|---|
| 176 | -1,
|
|---|
| 177 | DCA_EXT_X96,
|
|---|
| 178 | DCA_EXT_XCH | DCA_EXT_X96,
|
|---|
| 179 | -1,
|
|---|
| 180 | -1,
|
|---|
| 181 | DCA_EXT_XXCH,
|
|---|
| 182 | -1,
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 185 | /* extensions that reside in core substream */
|
|---|
| 186 | #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
|
|---|
| 187 |
|
|---|
| 188 | /* Tables for mapping dts channel configurations to libavcodec multichannel api.
|
|---|
| 189 | * Some compromises have been made for special configurations. Most configurations
|
|---|
| 190 | * are never used so complete accuracy is not needed.
|
|---|
| 191 | *
|
|---|
| 192 | * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
|
|---|
| 193 | * S -> side, when both rear and back are configured move one of them to the side channel
|
|---|
| 194 | * OV -> center back
|
|---|
| 195 | * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
|
|---|
| 196 | */
|
|---|
| 197 | static const uint64_t dca_core_channel_layout[] = {
|
|---|
| 198 | AV_CH_FRONT_CENTER, ///< 1, A
|
|---|
| 199 | AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
|
|---|
| 200 | AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
|
|---|
| 201 | AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
|
|---|
| 202 | AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
|
|---|
| 203 | AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
|
|---|
| 204 | AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
|
|---|
| 205 | AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
|
|---|
| 206 | AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
|
|---|
| 207 |
|
|---|
| 208 | AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
|
|---|
| 209 | AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
|
|---|
| 210 |
|
|---|
| 211 | AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
|
|---|
| 212 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
|
|---|
| 213 |
|
|---|
| 214 | AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
|
|---|
| 215 | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
|
|---|
| 216 |
|
|---|
| 217 | AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
|
|---|
| 218 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
|
|---|
| 219 | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
|
|---|
| 220 |
|
|---|
| 221 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
|
|---|
| 222 | AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
|
|---|
| 223 | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
|
|---|
| 224 |
|
|---|
| 225 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
|
|---|
| 226 | AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
|
|---|
| 227 | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
|
|---|
| 228 |
|
|---|
| 229 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
|
|---|
| 230 | AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
|
|---|
| 231 | AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
|
|---|
| 232 | };
|
|---|
| 233 |
|
|---|
| 234 | static const int8_t dca_lfe_index[] = {
|
|---|
| 235 | 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
|
|---|
| 236 | };
|
|---|
| 237 |
|
|---|
| 238 | static const int8_t dca_channel_reorder_lfe[][9] = {
|
|---|
| 239 | { 0, -1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 240 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 241 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 242 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 243 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 244 | { 2, 0, 1, -1, -1, -1, -1, -1, -1},
|
|---|
| 245 | { 0, 1, 3, -1, -1, -1, -1, -1, -1},
|
|---|
| 246 | { 2, 0, 1, 4, -1, -1, -1, -1, -1},
|
|---|
| 247 | { 0, 1, 3, 4, -1, -1, -1, -1, -1},
|
|---|
| 248 | { 2, 0, 1, 4, 5, -1, -1, -1, -1},
|
|---|
| 249 | { 3, 4, 0, 1, 5, 6, -1, -1, -1},
|
|---|
| 250 | { 2, 0, 1, 4, 5, 6, -1, -1, -1},
|
|---|
| 251 | { 0, 6, 4, 5, 2, 3, -1, -1, -1},
|
|---|
| 252 | { 4, 2, 5, 0, 1, 6, 7, -1, -1},
|
|---|
| 253 | { 5, 6, 0, 1, 7, 3, 8, 4, -1},
|
|---|
| 254 | { 4, 2, 5, 0, 1, 6, 8, 7, -1},
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 | static const int8_t dca_channel_reorder_lfe_xch[][9] = {
|
|---|
| 258 | { 0, 2, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 259 | { 0, 1, 3, -1, -1, -1, -1, -1, -1},
|
|---|
| 260 | { 0, 1, 3, -1, -1, -1, -1, -1, -1},
|
|---|
| 261 | { 0, 1, 3, -1, -1, -1, -1, -1, -1},
|
|---|
| 262 | { 0, 1, 3, -1, -1, -1, -1, -1, -1},
|
|---|
| 263 | { 2, 0, 1, 4, -1, -1, -1, -1, -1},
|
|---|
| 264 | { 0, 1, 3, 4, -1, -1, -1, -1, -1},
|
|---|
| 265 | { 2, 0, 1, 4, 5, -1, -1, -1, -1},
|
|---|
| 266 | { 0, 1, 4, 5, 3, -1, -1, -1, -1},
|
|---|
| 267 | { 2, 0, 1, 5, 6, 4, -1, -1, -1},
|
|---|
| 268 | { 3, 4, 0, 1, 6, 7, 5, -1, -1},
|
|---|
| 269 | { 2, 0, 1, 4, 5, 6, 7, -1, -1},
|
|---|
| 270 | { 0, 6, 4, 5, 2, 3, 7, -1, -1},
|
|---|
| 271 | { 4, 2, 5, 0, 1, 7, 8, 6, -1},
|
|---|
| 272 | { 5, 6, 0, 1, 8, 3, 9, 4, 7},
|
|---|
| 273 | { 4, 2, 5, 0, 1, 6, 9, 8, 7},
|
|---|
| 274 | };
|
|---|
| 275 |
|
|---|
| 276 | static const int8_t dca_channel_reorder_nolfe[][9] = {
|
|---|
| 277 | { 0, -1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 278 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 279 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 280 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 281 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 282 | { 2, 0, 1, -1, -1, -1, -1, -1, -1},
|
|---|
| 283 | { 0, 1, 2, -1, -1, -1, -1, -1, -1},
|
|---|
| 284 | { 2, 0, 1, 3, -1, -1, -1, -1, -1},
|
|---|
| 285 | { 0, 1, 2, 3, -1, -1, -1, -1, -1},
|
|---|
| 286 | { 2, 0, 1, 3, 4, -1, -1, -1, -1},
|
|---|
| 287 | { 2, 3, 0, 1, 4, 5, -1, -1, -1},
|
|---|
| 288 | { 2, 0, 1, 3, 4, 5, -1, -1, -1},
|
|---|
| 289 | { 0, 5, 3, 4, 1, 2, -1, -1, -1},
|
|---|
| 290 | { 3, 2, 4, 0, 1, 5, 6, -1, -1},
|
|---|
| 291 | { 4, 5, 0, 1, 6, 2, 7, 3, -1},
|
|---|
| 292 | { 3, 2, 4, 0, 1, 5, 7, 6, -1},
|
|---|
| 293 | };
|
|---|
| 294 |
|
|---|
| 295 | static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
|
|---|
| 296 | { 0, 1, -1, -1, -1, -1, -1, -1, -1},
|
|---|
| 297 | { 0, 1, 2, -1, -1, -1, -1, -1, -1},
|
|---|
| 298 | { 0, 1, 2, -1, -1, -1, -1, -1, -1},
|
|---|
| 299 | { 0, 1, 2, -1, -1, -1, -1, -1, -1},
|
|---|
| 300 | { 0, 1, 2, -1, -1, -1, -1, -1, -1},
|
|---|
| 301 | { 2, 0, 1, 3, -1, -1, -1, -1, -1},
|
|---|
| 302 | { 0, 1, 2, 3, -1, -1, -1, -1, -1},
|
|---|
| 303 | { 2, 0, 1, 3, 4, -1, -1, -1, -1},
|
|---|
| 304 | { 0, 1, 3, 4, 2, -1, -1, -1, -1},
|
|---|
| 305 | { 2, 0, 1, 4, 5, 3, -1, -1, -1},
|
|---|
| 306 | { 2, 3, 0, 1, 5, 6, 4, -1, -1},
|
|---|
| 307 | { 2, 0, 1, 3, 4, 5, 6, -1, -1},
|
|---|
| 308 | { 0, 5, 3, 4, 1, 2, 6, -1, -1},
|
|---|
| 309 | { 3, 2, 4, 0, 1, 6, 7, 5, -1},
|
|---|
| 310 | { 4, 5, 0, 1, 7, 2, 8, 3, 6},
|
|---|
| 311 | { 3, 2, 4, 0, 1, 5, 8, 7, 6},
|
|---|
| 312 | };
|
|---|
| 313 |
|
|---|
| 314 | #define DCA_DOLBY 101 /* FIXME */
|
|---|
| 315 |
|
|---|
| 316 | #define DCA_CHANNEL_BITS 6
|
|---|
| 317 | #define DCA_CHANNEL_MASK 0x3F
|
|---|
| 318 |
|
|---|
| 319 | #define DCA_LFE 0x80
|
|---|
| 320 |
|
|---|
| 321 | #define HEADER_SIZE 14
|
|---|
| 322 |
|
|---|
| 323 | #define DCA_MAX_FRAME_SIZE 16384
|
|---|
| 324 | #define DCA_MAX_EXSS_HEADER_SIZE 4096
|
|---|
| 325 |
|
|---|
| 326 | #define DCA_BUFFER_PADDING_SIZE 1024
|
|---|
| 327 |
|
|---|
| 328 | /** Bit allocation */
|
|---|
| 329 | typedef struct {
|
|---|
| 330 | int offset; ///< code values offset
|
|---|
| 331 | int maxbits[8]; ///< max bits in VLC
|
|---|
| 332 | int wrap; ///< wrap for get_vlc2()
|
|---|
| 333 | VLC vlc[8]; ///< actual codes
|
|---|
| 334 | } BitAlloc;
|
|---|
| 335 |
|
|---|
| 336 | static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
|
|---|
| 337 | static BitAlloc dca_tmode; ///< transition mode VLCs
|
|---|
| 338 | static BitAlloc dca_scalefactor; ///< scalefactor VLCs
|
|---|
| 339 | static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
|
|---|
| 340 |
|
|---|
| 341 | static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
|
|---|
| 342 | int idx)
|
|---|
| 343 | {
|
|---|
| 344 | return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
|
|---|
| 345 | ba->offset;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | typedef struct {
|
|---|
| 349 | AVCodecContext *avctx;
|
|---|
| 350 | AVFrame frame;
|
|---|
| 351 | /* Frame header */
|
|---|
| 352 | int frame_type; ///< type of the current frame
|
|---|
| 353 | int samples_deficit; ///< deficit sample count
|
|---|
| 354 | int crc_present; ///< crc is present in the bitstream
|
|---|
| 355 | int sample_blocks; ///< number of PCM sample blocks
|
|---|
| 356 | int frame_size; ///< primary frame byte size
|
|---|
| 357 | int amode; ///< audio channels arrangement
|
|---|
| 358 | int sample_rate; ///< audio sampling rate
|
|---|
| 359 | int bit_rate; ///< transmission bit rate
|
|---|
| 360 | int bit_rate_index; ///< transmission bit rate index
|
|---|
| 361 |
|
|---|
| 362 | int downmix; ///< embedded downmix enabled
|
|---|
| 363 | int dynrange; ///< embedded dynamic range flag
|
|---|
| 364 | int timestamp; ///< embedded time stamp flag
|
|---|
| 365 | int aux_data; ///< auxiliary data flag
|
|---|
| 366 | int hdcd; ///< source material is mastered in HDCD
|
|---|
| 367 | int ext_descr; ///< extension audio descriptor flag
|
|---|
| 368 | int ext_coding; ///< extended coding flag
|
|---|
| 369 | int aspf; ///< audio sync word insertion flag
|
|---|
| 370 | int lfe; ///< low frequency effects flag
|
|---|
| 371 | int predictor_history; ///< predictor history flag
|
|---|
| 372 | int header_crc; ///< header crc check bytes
|
|---|
| 373 | int multirate_inter; ///< multirate interpolator switch
|
|---|
| 374 | int version; ///< encoder software revision
|
|---|
| 375 | int copy_history; ///< copy history
|
|---|
| 376 | int source_pcm_res; ///< source pcm resolution
|
|---|
| 377 | int front_sum; ///< front sum/difference flag
|
|---|
| 378 | int surround_sum; ///< surround sum/difference flag
|
|---|
| 379 | int dialog_norm; ///< dialog normalisation parameter
|
|---|
| 380 |
|
|---|
| 381 | /* Primary audio coding header */
|
|---|
| 382 | int subframes; ///< number of subframes
|
|---|
| 383 | int total_channels; ///< number of channels including extensions
|
|---|
| 384 | int prim_channels; ///< number of primary audio channels
|
|---|
| 385 | int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
|
|---|
| 386 | int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
|
|---|
| 387 | int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
|
|---|
| 388 | int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
|
|---|
| 389 | int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
|
|---|
| 390 | int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
|
|---|
| 391 | int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
|
|---|
| 392 | float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
|
|---|
| 393 |
|
|---|
| 394 | /* Primary audio coding side information */
|
|---|
| 395 | int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
|
|---|
| 396 | int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
|
|---|
| 397 | int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
|
|---|
| 398 | int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
|
|---|
| 399 | int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
|
|---|
| 400 | int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
|
|---|
| 401 | int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
|
|---|
| 402 | int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
|
|---|
| 403 | int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
|
|---|
| 404 | int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
|
|---|
| 405 | int dynrange_coef; ///< dynamic range coefficient
|
|---|
| 406 |
|
|---|
| 407 | int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
|
|---|
| 408 |
|
|---|
| 409 | float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
|
|---|
| 410 | int lfe_scale_factor;
|
|---|
| 411 |
|
|---|
| 412 | /* Subband samples history (for ADPCM) */
|
|---|
| 413 | DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
|
|---|
| 414 | DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
|
|---|
| 415 | DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
|
|---|
| 416 | int hist_index[DCA_PRIM_CHANNELS_MAX];
|
|---|
| 417 | DECLARE_ALIGNED(32, float, raXin)[32];
|
|---|
| 418 |
|
|---|
| 419 | int output; ///< type of output
|
|---|
| 420 | float scale_bias; ///< output scale
|
|---|
| 421 |
|
|---|
| 422 | DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
|
|---|
| 423 | DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256];
|
|---|
| 424 | const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
|
|---|
| 425 |
|
|---|
| 426 | uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
|
|---|
| 427 | int dca_buffer_size; ///< how much data is in the dca_buffer
|
|---|
| 428 |
|
|---|
| 429 | const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
|
|---|
| 430 | GetBitContext gb;
|
|---|
| 431 | /* Current position in DCA frame */
|
|---|
| 432 | int current_subframe;
|
|---|
| 433 | int current_subsubframe;
|
|---|
| 434 |
|
|---|
| 435 | int core_ext_mask; ///< present extensions in the core substream
|
|---|
| 436 |
|
|---|
| 437 | /* XCh extension information */
|
|---|
| 438 | int xch_present; ///< XCh extension present and valid
|
|---|
| 439 | int xch_base_channel; ///< index of first (only) channel containing XCH data
|
|---|
| 440 |
|
|---|
| 441 | /* XXCH extension information */
|
|---|
| 442 | int xxch_chset;
|
|---|
| 443 | int xxch_nbits_spk_mask;
|
|---|
| 444 | uint32_t xxch_core_spkmask;
|
|---|
| 445 | uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
|
|---|
| 446 | int xxch_chset_nch[4];
|
|---|
| 447 | float xxch_dmix_sf[DCA_CHSETS_MAX];
|
|---|
| 448 |
|
|---|
| 449 | uint32_t xxch_downmix; /* downmix enabled per channel set */
|
|---|
| 450 | uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
|
|---|
| 451 | float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
|
|---|
| 452 |
|
|---|
| 453 | int8_t xxch_order_tab[32];
|
|---|
| 454 | int8_t lfe_index;
|
|---|
| 455 |
|
|---|
| 456 | /* ExSS header parser */
|
|---|
| 457 | int static_fields; ///< static fields present
|
|---|
| 458 | int mix_metadata; ///< mixing metadata present
|
|---|
| 459 | int num_mix_configs; ///< number of mix out configurations
|
|---|
| 460 | int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
|
|---|
| 461 |
|
|---|
| 462 | int profile;
|
|---|
| 463 |
|
|---|
| 464 | int debug_flag; ///< used for suppressing repeated error messages output
|
|---|
| 465 | AVFloatDSPContext fdsp;
|
|---|
| 466 | FFTContext imdct;
|
|---|
| 467 | SynthFilterContext synth;
|
|---|
| 468 | DCADSPContext dcadsp;
|
|---|
| 469 | FmtConvertContext fmt_conv;
|
|---|
| 470 | } DCAContext;
|
|---|
| 471 |
|
|---|
| 472 | static const uint16_t dca_vlc_offs[] = {
|
|---|
| 473 | 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
|
|---|
| 474 | 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
|
|---|
| 475 | 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
|
|---|
| 476 | 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
|
|---|
| 477 | 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
|
|---|
| 478 | 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
|
|---|
| 479 | };
|
|---|
| 480 |
|
|---|
| 481 | static av_cold void dca_init_vlcs(void)
|
|---|
| 482 | {
|
|---|
| 483 | static int vlcs_initialized = 0;
|
|---|
| 484 | int i, j, c = 14;
|
|---|
| 485 | static VLC_TYPE dca_table[23622][2];
|
|---|
| 486 |
|
|---|
| 487 | if (vlcs_initialized)
|
|---|
| 488 | return;
|
|---|
| 489 |
|
|---|
| 490 | dca_bitalloc_index.offset = 1;
|
|---|
| 491 | dca_bitalloc_index.wrap = 2;
|
|---|
| 492 | for (i = 0; i < 5; i++) {
|
|---|
| 493 | dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
|
|---|
| 494 | dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
|
|---|
| 495 | init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
|
|---|
| 496 | bitalloc_12_bits[i], 1, 1,
|
|---|
| 497 | bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
|
|---|
| 498 | }
|
|---|
| 499 | dca_scalefactor.offset = -64;
|
|---|
| 500 | dca_scalefactor.wrap = 2;
|
|---|
| 501 | for (i = 0; i < 5; i++) {
|
|---|
| 502 | dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
|
|---|
| 503 | dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
|
|---|
| 504 | init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
|
|---|
| 505 | scales_bits[i], 1, 1,
|
|---|
| 506 | scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
|
|---|
| 507 | }
|
|---|
| 508 | dca_tmode.offset = 0;
|
|---|
| 509 | dca_tmode.wrap = 1;
|
|---|
| 510 | for (i = 0; i < 4; i++) {
|
|---|
| 511 | dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
|
|---|
| 512 | dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
|
|---|
| 513 | init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
|
|---|
| 514 | tmode_bits[i], 1, 1,
|
|---|
| 515 | tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | for (i = 0; i < 10; i++)
|
|---|
| 519 | for (j = 0; j < 7; j++) {
|
|---|
| 520 | if (!bitalloc_codes[i][j])
|
|---|
| 521 | break;
|
|---|
| 522 | dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
|
|---|
| 523 | dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
|
|---|
| 524 | dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
|
|---|
| 525 | dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
|
|---|
| 526 |
|
|---|
| 527 | init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
|
|---|
| 528 | bitalloc_sizes[i],
|
|---|
| 529 | bitalloc_bits[i][j], 1, 1,
|
|---|
| 530 | bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
|
|---|
| 531 | c++;
|
|---|
| 532 | }
|
|---|
| 533 | vlcs_initialized = 1;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
|
|---|
| 537 | {
|
|---|
| 538 | while (len--)
|
|---|
| 539 | *dst++ = get_bits(gb, bits);
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
|
|---|
| 543 | {
|
|---|
| 544 | int i, base, mask;
|
|---|
| 545 |
|
|---|
| 546 | /* locate channel set containing the channel */
|
|---|
| 547 | for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
|
|---|
| 548 | i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
|
|---|
| 549 | base += av_popcount(mask);
|
|---|
| 550 |
|
|---|
| 551 | return base + av_popcount(mask & (xxch_ch - 1));
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
|
|---|
| 555 | int xxch)
|
|---|
| 556 | {
|
|---|
| 557 | int i, j;
|
|---|
| 558 | static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
|
|---|
| 559 | static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
|
|---|
| 560 | static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
|
|---|
| 561 | int hdr_pos = 0, hdr_size = 0;
|
|---|
| 562 | float sign, mag, scale_factor;
|
|---|
| 563 | int this_chans, acc_mask;
|
|---|
| 564 | int embedded_downmix;
|
|---|
| 565 | int nchans, mask[8];
|
|---|
| 566 | int coeff, ichan;
|
|---|
| 567 |
|
|---|
| 568 | /* xxch has arbitrary sized audio coding headers */
|
|---|
| 569 | if (xxch) {
|
|---|
| 570 | hdr_pos = get_bits_count(&s->gb);
|
|---|
| 571 | hdr_size = get_bits(&s->gb, 7) + 1;
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | nchans = get_bits(&s->gb, 3) + 1;
|
|---|
| 575 | s->total_channels = nchans + base_channel;
|
|---|
| 576 | s->prim_channels = s->total_channels;
|
|---|
| 577 |
|
|---|
| 578 | /* obtain speaker layout mask & downmix coefficients for XXCH */
|
|---|
| 579 | if (xxch) {
|
|---|
| 580 | acc_mask = s->xxch_core_spkmask;
|
|---|
| 581 |
|
|---|
| 582 | this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
|
|---|
| 583 | s->xxch_spk_masks[s->xxch_chset] = this_chans;
|
|---|
| 584 | s->xxch_chset_nch[s->xxch_chset] = nchans;
|
|---|
| 585 |
|
|---|
| 586 | for (i = 0; i <= s->xxch_chset; i++)
|
|---|
| 587 | acc_mask |= s->xxch_spk_masks[i];
|
|---|
| 588 |
|
|---|
| 589 | /* check for downmixing information */
|
|---|
| 590 | if (get_bits1(&s->gb)) {
|
|---|
| 591 | embedded_downmix = get_bits1(&s->gb);
|
|---|
| 592 | scale_factor =
|
|---|
| 593 | 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
|
|---|
| 594 |
|
|---|
| 595 | s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
|
|---|
| 596 |
|
|---|
| 597 | for (i = base_channel; i < s->prim_channels; i++) {
|
|---|
| 598 | s->xxch_downmix |= (1 << i);
|
|---|
| 599 | mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 603 | memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
|
|---|
| 604 | s->xxch_dmix_embedded |= (embedded_downmix << j);
|
|---|
| 605 | for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
|
|---|
| 606 | if (mask[j] & (1 << i)) {
|
|---|
| 607 | if ((1 << i) == DCA_XXCH_LFE1) {
|
|---|
| 608 | av_log(s->avctx, AV_LOG_WARNING,
|
|---|
| 609 | "DCA-XXCH: dmix to LFE1 not supported.\n");
|
|---|
| 610 | continue;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | coeff = get_bits(&s->gb, 7);
|
|---|
| 614 | sign = (coeff & 64) ? 1.0 : -1.0;
|
|---|
| 615 | mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
|
|---|
| 616 | ichan = dca_xxch2index(s, 1 << i);
|
|---|
| 617 | s->xxch_dmix_coeff[j][ichan] = sign * mag;
|
|---|
| 618 | }
|
|---|
| 619 | }
|
|---|
| 620 | }
|
|---|
| 621 | }
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
|
|---|
| 625 | s->prim_channels = DCA_PRIM_CHANNELS_MAX;
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 | for (i = base_channel; i < s->prim_channels; i++) {
|
|---|
| 629 | s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
|
|---|
| 630 | if (s->subband_activity[i] > DCA_SUBBANDS)
|
|---|
| 631 | s->subband_activity[i] = DCA_SUBBANDS;
|
|---|
| 632 | }
|
|---|
| 633 | for (i = base_channel; i < s->prim_channels; i++) {
|
|---|
| 634 | s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
|
|---|
| 635 | if (s->vq_start_subband[i] > DCA_SUBBANDS)
|
|---|
| 636 | s->vq_start_subband[i] = DCA_SUBBANDS;
|
|---|
| 637 | }
|
|---|
| 638 | get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
|
|---|
| 639 | get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
|
|---|
| 640 | get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
|
|---|
| 641 | get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
|
|---|
| 642 |
|
|---|
| 643 | /* Get codebooks quantization indexes */
|
|---|
| 644 | if (!base_channel)
|
|---|
| 645 | memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
|
|---|
| 646 | for (j = 1; j < 11; j++)
|
|---|
| 647 | for (i = base_channel; i < s->prim_channels; i++)
|
|---|
| 648 | s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
|
|---|
| 649 |
|
|---|
| 650 | /* Get scale factor adjustment */
|
|---|
| 651 | for (j = 0; j < 11; j++)
|
|---|
| 652 | for (i = base_channel; i < s->prim_channels; i++)
|
|---|
| 653 | s->scalefactor_adj[i][j] = 1;
|
|---|
| 654 |
|
|---|
| 655 | for (j = 1; j < 11; j++)
|
|---|
| 656 | for (i = base_channel; i < s->prim_channels; i++)
|
|---|
| 657 | if (s->quant_index_huffman[i][j] < thr[j])
|
|---|
| 658 | s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
|
|---|
| 659 |
|
|---|
| 660 | if (!xxch) {
|
|---|
| 661 | if (s->crc_present) {
|
|---|
| 662 | /* Audio header CRC check */
|
|---|
| 663 | get_bits(&s->gb, 16);
|
|---|
| 664 | }
|
|---|
| 665 | } else {
|
|---|
| 666 | /* Skip to the end of the header, also ignore CRC if present */
|
|---|
| 667 | i = get_bits_count(&s->gb);
|
|---|
| 668 | if (hdr_pos + 8 * hdr_size > i)
|
|---|
| 669 | skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | s->current_subframe = 0;
|
|---|
| 673 | s->current_subsubframe = 0;
|
|---|
| 674 |
|
|---|
| 675 | #ifdef TRACE
|
|---|
| 676 | av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
|
|---|
| 677 | av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
|
|---|
| 678 | for (i = base_channel; i < s->prim_channels; i++) {
|
|---|
| 679 | av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
|
|---|
| 680 | s->subband_activity[i]);
|
|---|
| 681 | av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
|
|---|
| 682 | s->vq_start_subband[i]);
|
|---|
| 683 | av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
|
|---|
| 684 | s->joint_intensity[i]);
|
|---|
| 685 | av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
|
|---|
| 686 | s->transient_huffman[i]);
|
|---|
| 687 | av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
|
|---|
| 688 | s->scalefactor_huffman[i]);
|
|---|
| 689 | av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
|
|---|
| 690 | s->bitalloc_huffman[i]);
|
|---|
| 691 | av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
|
|---|
| 692 | for (j = 0; j < 11; j++)
|
|---|
| 693 | av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
|
|---|
| 694 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 695 | av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
|
|---|
| 696 | for (j = 0; j < 11; j++)
|
|---|
| 697 | av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
|
|---|
| 698 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 699 | }
|
|---|
| 700 | #endif
|
|---|
| 701 |
|
|---|
| 702 | return 0;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | static int dca_parse_frame_header(DCAContext *s)
|
|---|
| 706 | {
|
|---|
| 707 | init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
|
|---|
| 708 |
|
|---|
| 709 | /* Sync code */
|
|---|
| 710 | skip_bits_long(&s->gb, 32);
|
|---|
| 711 |
|
|---|
| 712 | /* Frame header */
|
|---|
| 713 | s->frame_type = get_bits(&s->gb, 1);
|
|---|
| 714 | s->samples_deficit = get_bits(&s->gb, 5) + 1;
|
|---|
| 715 | s->crc_present = get_bits(&s->gb, 1);
|
|---|
| 716 | s->sample_blocks = get_bits(&s->gb, 7) + 1;
|
|---|
| 717 | s->frame_size = get_bits(&s->gb, 14) + 1;
|
|---|
| 718 | if (s->frame_size < 95)
|
|---|
| 719 | return AVERROR_INVALIDDATA;
|
|---|
| 720 | s->amode = get_bits(&s->gb, 6);
|
|---|
| 721 | s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
|
|---|
| 722 | if (!s->sample_rate)
|
|---|
| 723 | return AVERROR_INVALIDDATA;
|
|---|
| 724 | s->bit_rate_index = get_bits(&s->gb, 5);
|
|---|
| 725 | s->bit_rate = dca_bit_rates[s->bit_rate_index];
|
|---|
| 726 | if (!s->bit_rate)
|
|---|
| 727 | return AVERROR_INVALIDDATA;
|
|---|
| 728 |
|
|---|
| 729 | s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
|
|---|
| 730 | s->dynrange = get_bits(&s->gb, 1);
|
|---|
| 731 | s->timestamp = get_bits(&s->gb, 1);
|
|---|
| 732 | s->aux_data = get_bits(&s->gb, 1);
|
|---|
| 733 | s->hdcd = get_bits(&s->gb, 1);
|
|---|
| 734 | s->ext_descr = get_bits(&s->gb, 3);
|
|---|
| 735 | s->ext_coding = get_bits(&s->gb, 1);
|
|---|
| 736 | s->aspf = get_bits(&s->gb, 1);
|
|---|
| 737 | s->lfe = get_bits(&s->gb, 2);
|
|---|
| 738 | s->predictor_history = get_bits(&s->gb, 1);
|
|---|
| 739 |
|
|---|
| 740 | /* TODO: check CRC */
|
|---|
| 741 | if (s->crc_present)
|
|---|
| 742 | s->header_crc = get_bits(&s->gb, 16);
|
|---|
| 743 |
|
|---|
| 744 | s->multirate_inter = get_bits(&s->gb, 1);
|
|---|
| 745 | s->version = get_bits(&s->gb, 4);
|
|---|
| 746 | s->copy_history = get_bits(&s->gb, 2);
|
|---|
| 747 | s->source_pcm_res = get_bits(&s->gb, 3);
|
|---|
| 748 | s->front_sum = get_bits(&s->gb, 1);
|
|---|
| 749 | s->surround_sum = get_bits(&s->gb, 1);
|
|---|
| 750 | s->dialog_norm = get_bits(&s->gb, 4);
|
|---|
| 751 |
|
|---|
| 752 | /* FIXME: channels mixing levels */
|
|---|
| 753 | s->output = s->amode;
|
|---|
| 754 | if (s->lfe)
|
|---|
| 755 | s->output |= DCA_LFE;
|
|---|
| 756 |
|
|---|
| 757 | #ifdef TRACE
|
|---|
| 758 | av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
|
|---|
| 759 | av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
|
|---|
| 760 | av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
|
|---|
| 761 | av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
|
|---|
| 762 | s->sample_blocks, s->sample_blocks * 32);
|
|---|
| 763 | av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
|
|---|
| 764 | av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
|
|---|
| 765 | s->amode, dca_channels[s->amode]);
|
|---|
| 766 | av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
|
|---|
| 767 | s->sample_rate);
|
|---|
| 768 | av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
|
|---|
| 769 | s->bit_rate);
|
|---|
| 770 | av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
|
|---|
| 771 | av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
|
|---|
| 772 | av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
|
|---|
| 773 | av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
|
|---|
| 774 | av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
|
|---|
| 775 | av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
|
|---|
| 776 | av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
|
|---|
| 777 | av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
|
|---|
| 778 | av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
|
|---|
| 779 | av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
|
|---|
| 780 | s->predictor_history);
|
|---|
| 781 | av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
|
|---|
| 782 | av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
|
|---|
| 783 | s->multirate_inter);
|
|---|
| 784 | av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
|
|---|
| 785 | av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
|
|---|
| 786 | av_log(s->avctx, AV_LOG_DEBUG,
|
|---|
| 787 | "source pcm resolution: %i (%i bits/sample)\n",
|
|---|
| 788 | s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
|
|---|
| 789 | av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
|
|---|
| 790 | av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
|
|---|
| 791 | av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
|
|---|
| 792 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 793 | #endif
|
|---|
| 794 |
|
|---|
| 795 | /* Primary audio coding header */
|
|---|
| 796 | s->subframes = get_bits(&s->gb, 4) + 1;
|
|---|
| 797 |
|
|---|
| 798 | return dca_parse_audio_coding_header(s, 0, 0);
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 | static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
|
|---|
| 803 | {
|
|---|
| 804 | if (level < 5) {
|
|---|
| 805 | /* huffman encoded */
|
|---|
| 806 | value += get_bitalloc(gb, &dca_scalefactor, level);
|
|---|
| 807 | value = av_clip(value, 0, (1 << log2range) - 1);
|
|---|
| 808 | } else if (level < 8) {
|
|---|
| 809 | if (level + 1 > log2range) {
|
|---|
| 810 | skip_bits(gb, level + 1 - log2range);
|
|---|
| 811 | value = get_bits(gb, log2range);
|
|---|
| 812 | } else {
|
|---|
| 813 | value = get_bits(gb, level + 1);
|
|---|
| 814 | }
|
|---|
| 815 | }
|
|---|
| 816 | return value;
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 | static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
|
|---|
| 820 | {
|
|---|
| 821 | /* Primary audio coding side information */
|
|---|
| 822 | int j, k;
|
|---|
| 823 |
|
|---|
| 824 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 825 | return AVERROR_INVALIDDATA;
|
|---|
| 826 |
|
|---|
| 827 | if (!base_channel) {
|
|---|
| 828 | s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
|
|---|
| 829 | s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 833 | for (k = 0; k < s->subband_activity[j]; k++)
|
|---|
| 834 | s->prediction_mode[j][k] = get_bits(&s->gb, 1);
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | /* Get prediction codebook */
|
|---|
| 838 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 839 | for (k = 0; k < s->subband_activity[j]; k++) {
|
|---|
| 840 | if (s->prediction_mode[j][k] > 0) {
|
|---|
| 841 | /* (Prediction coefficient VQ address) */
|
|---|
| 842 | s->prediction_vq[j][k] = get_bits(&s->gb, 12);
|
|---|
| 843 | }
|
|---|
| 844 | }
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | /* Bit allocation index */
|
|---|
| 848 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 849 | for (k = 0; k < s->vq_start_subband[j]; k++) {
|
|---|
| 850 | if (s->bitalloc_huffman[j] == 6)
|
|---|
| 851 | s->bitalloc[j][k] = get_bits(&s->gb, 5);
|
|---|
| 852 | else if (s->bitalloc_huffman[j] == 5)
|
|---|
| 853 | s->bitalloc[j][k] = get_bits(&s->gb, 4);
|
|---|
| 854 | else if (s->bitalloc_huffman[j] == 7) {
|
|---|
| 855 | av_log(s->avctx, AV_LOG_ERROR,
|
|---|
| 856 | "Invalid bit allocation index\n");
|
|---|
| 857 | return AVERROR_INVALIDDATA;
|
|---|
| 858 | } else {
|
|---|
| 859 | s->bitalloc[j][k] =
|
|---|
| 860 | get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | if (s->bitalloc[j][k] > 26) {
|
|---|
| 864 | // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n",
|
|---|
| 865 | // j, k, s->bitalloc[j][k]);
|
|---|
| 866 | return AVERROR_INVALIDDATA;
|
|---|
| 867 | }
|
|---|
| 868 | }
|
|---|
| 869 | }
|
|---|
| 870 |
|
|---|
| 871 | /* Transition mode */
|
|---|
| 872 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 873 | for (k = 0; k < s->subband_activity[j]; k++) {
|
|---|
| 874 | s->transition_mode[j][k] = 0;
|
|---|
| 875 | if (s->subsubframes[s->current_subframe] > 1 &&
|
|---|
| 876 | k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
|
|---|
| 877 | s->transition_mode[j][k] =
|
|---|
| 878 | get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
|
|---|
| 879 | }
|
|---|
| 880 | }
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 884 | return AVERROR_INVALIDDATA;
|
|---|
| 885 |
|
|---|
| 886 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 887 | const uint32_t *scale_table;
|
|---|
| 888 | int scale_sum, log_size;
|
|---|
| 889 |
|
|---|
| 890 | memset(s->scale_factor[j], 0,
|
|---|
| 891 | s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
|
|---|
| 892 |
|
|---|
| 893 | if (s->scalefactor_huffman[j] == 6) {
|
|---|
| 894 | scale_table = scale_factor_quant7;
|
|---|
| 895 | log_size = 7;
|
|---|
| 896 | } else {
|
|---|
| 897 | scale_table = scale_factor_quant6;
|
|---|
| 898 | log_size = 6;
|
|---|
| 899 | }
|
|---|
| 900 |
|
|---|
| 901 | /* When huffman coded, only the difference is encoded */
|
|---|
| 902 | scale_sum = 0;
|
|---|
| 903 |
|
|---|
| 904 | for (k = 0; k < s->subband_activity[j]; k++) {
|
|---|
| 905 | if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
|
|---|
| 906 | scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
|
|---|
| 907 | s->scale_factor[j][k][0] = scale_table[scale_sum];
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
|
|---|
| 911 | /* Get second scale factor */
|
|---|
| 912 | scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
|
|---|
| 913 | s->scale_factor[j][k][1] = scale_table[scale_sum];
|
|---|
| 914 | }
|
|---|
| 915 | }
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | /* Joint subband scale factor codebook select */
|
|---|
| 919 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 920 | /* Transmitted only if joint subband coding enabled */
|
|---|
| 921 | if (s->joint_intensity[j] > 0)
|
|---|
| 922 | s->joint_huff[j] = get_bits(&s->gb, 3);
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 926 | return AVERROR_INVALIDDATA;
|
|---|
| 927 |
|
|---|
| 928 | /* Scale factors for joint subband coding */
|
|---|
| 929 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 930 | int source_channel;
|
|---|
| 931 |
|
|---|
| 932 | /* Transmitted only if joint subband coding enabled */
|
|---|
| 933 | if (s->joint_intensity[j] > 0) {
|
|---|
| 934 | int scale = 0;
|
|---|
| 935 | source_channel = s->joint_intensity[j] - 1;
|
|---|
| 936 |
|
|---|
| 937 | /* When huffman coded, only the difference is encoded
|
|---|
| 938 | * (is this valid as well for joint scales ???) */
|
|---|
| 939 |
|
|---|
| 940 | for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
|
|---|
| 941 | scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
|
|---|
| 942 | s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | if (!(s->debug_flag & 0x02)) {
|
|---|
| 946 | av_log(s->avctx, AV_LOG_DEBUG,
|
|---|
| 947 | "Joint stereo coding not supported\n");
|
|---|
| 948 | s->debug_flag |= 0x02;
|
|---|
| 949 | }
|
|---|
| 950 | }
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | /* Stereo downmix coefficients */
|
|---|
| 954 | if (!base_channel && s->prim_channels > 2) {
|
|---|
| 955 | if (s->downmix) {
|
|---|
| 956 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 957 | s->downmix_coef[j][0] = get_bits(&s->gb, 7);
|
|---|
| 958 | s->downmix_coef[j][1] = get_bits(&s->gb, 7);
|
|---|
| 959 | }
|
|---|
| 960 | } else {
|
|---|
| 961 | int am = s->amode & DCA_CHANNEL_MASK;
|
|---|
| 962 | if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
|
|---|
| 963 | av_log(s->avctx, AV_LOG_ERROR,
|
|---|
| 964 | "Invalid channel mode %d\n", am);
|
|---|
| 965 | return AVERROR_INVALIDDATA;
|
|---|
| 966 | }
|
|---|
| 967 | for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
|
|---|
| 968 | s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
|
|---|
| 969 | s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
|
|---|
| 970 | }
|
|---|
| 971 | }
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | /* Dynamic range coefficient */
|
|---|
| 975 | if (!base_channel && s->dynrange)
|
|---|
| 976 | s->dynrange_coef = get_bits(&s->gb, 8);
|
|---|
| 977 |
|
|---|
| 978 | /* Side information CRC check word */
|
|---|
| 979 | if (s->crc_present) {
|
|---|
| 980 | get_bits(&s->gb, 16);
|
|---|
| 981 | }
|
|---|
| 982 |
|
|---|
| 983 | /*
|
|---|
| 984 | * Primary audio data arrays
|
|---|
| 985 | */
|
|---|
| 986 |
|
|---|
| 987 | /* VQ encoded high frequency subbands */
|
|---|
| 988 | for (j = base_channel; j < s->prim_channels; j++)
|
|---|
| 989 | for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
|
|---|
| 990 | /* 1 vector -> 32 samples */
|
|---|
| 991 | s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
|
|---|
| 992 |
|
|---|
| 993 | /* Low frequency effect data */
|
|---|
| 994 | if (!base_channel && s->lfe) {
|
|---|
| 995 | int quant7;
|
|---|
| 996 | /* LFE samples */
|
|---|
| 997 | int lfe_samples = 2 * s->lfe * (4 + block_index);
|
|---|
| 998 | int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
|
|---|
| 999 | float lfe_scale;
|
|---|
| 1000 |
|
|---|
| 1001 | for (j = lfe_samples; j < lfe_end_sample; j++) {
|
|---|
| 1002 | /* Signed 8 bits int */
|
|---|
| 1003 | s->lfe_data[j] = get_sbits(&s->gb, 8);
|
|---|
| 1004 | }
|
|---|
| 1005 |
|
|---|
| 1006 | /* Scale factor index */
|
|---|
| 1007 | quant7 = get_bits(&s->gb, 8);
|
|---|
| 1008 | if (quant7 > 127) {
|
|---|
| 1009 | av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
|
|---|
| 1010 | return AVERROR_INVALIDDATA;
|
|---|
| 1011 | }
|
|---|
| 1012 | s->lfe_scale_factor = scale_factor_quant7[quant7];
|
|---|
| 1013 |
|
|---|
| 1014 | /* Quantization step size * scale factor */
|
|---|
| 1015 | lfe_scale = 0.035 * s->lfe_scale_factor;
|
|---|
| 1016 |
|
|---|
| 1017 | for (j = lfe_samples; j < lfe_end_sample; j++)
|
|---|
| 1018 | s->lfe_data[j] *= lfe_scale;
|
|---|
| 1019 | }
|
|---|
| 1020 |
|
|---|
| 1021 | #ifdef TRACE
|
|---|
| 1022 | av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
|
|---|
| 1023 | s->subsubframes[s->current_subframe]);
|
|---|
| 1024 | av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
|
|---|
| 1025 | s->partial_samples[s->current_subframe]);
|
|---|
| 1026 |
|
|---|
| 1027 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1028 | av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
|
|---|
| 1029 | for (k = 0; k < s->subband_activity[j]; k++)
|
|---|
| 1030 | av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
|
|---|
| 1031 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1032 | }
|
|---|
| 1033 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1034 | for (k = 0; k < s->subband_activity[j]; k++)
|
|---|
| 1035 | av_log(s->avctx, AV_LOG_DEBUG,
|
|---|
| 1036 | "prediction coefs: %f, %f, %f, %f\n",
|
|---|
| 1037 | (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
|
|---|
| 1038 | (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
|
|---|
| 1039 | (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
|
|---|
| 1040 | (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
|
|---|
| 1041 | }
|
|---|
| 1042 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1043 | av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
|
|---|
| 1044 | for (k = 0; k < s->vq_start_subband[j]; k++)
|
|---|
| 1045 | av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
|
|---|
| 1046 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1047 | }
|
|---|
| 1048 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1049 | av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
|
|---|
| 1050 | for (k = 0; k < s->subband_activity[j]; k++)
|
|---|
| 1051 | av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
|
|---|
| 1052 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1053 | }
|
|---|
| 1054 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1055 | av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
|
|---|
| 1056 | for (k = 0; k < s->subband_activity[j]; k++) {
|
|---|
| 1057 | if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
|
|---|
| 1058 | av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
|
|---|
| 1059 | if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
|
|---|
| 1060 | av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
|
|---|
| 1061 | }
|
|---|
| 1062 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1063 | }
|
|---|
| 1064 | for (j = base_channel; j < s->prim_channels; j++) {
|
|---|
| 1065 | if (s->joint_intensity[j] > 0) {
|
|---|
| 1066 | int source_channel = s->joint_intensity[j] - 1;
|
|---|
| 1067 | av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
|
|---|
| 1068 | for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
|
|---|
| 1069 | av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
|
|---|
| 1070 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1071 | }
|
|---|
| 1072 | }
|
|---|
| 1073 | if (!base_channel && s->prim_channels > 2 && s->downmix) {
|
|---|
| 1074 | av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
|
|---|
| 1075 | for (j = 0; j < s->prim_channels; j++) {
|
|---|
| 1076 | av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
|
|---|
| 1077 | dca_downmix_coeffs[s->downmix_coef[j][0]]);
|
|---|
| 1078 | av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
|
|---|
| 1079 | dca_downmix_coeffs[s->downmix_coef[j][1]]);
|
|---|
| 1080 | }
|
|---|
| 1081 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1082 | }
|
|---|
| 1083 | for (j = base_channel; j < s->prim_channels; j++)
|
|---|
| 1084 | for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
|
|---|
| 1085 | av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
|
|---|
| 1086 | if (!base_channel && s->lfe) {
|
|---|
| 1087 | int lfe_samples = 2 * s->lfe * (4 + block_index);
|
|---|
| 1088 | int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
|
|---|
| 1089 |
|
|---|
| 1090 | av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
|
|---|
| 1091 | for (j = lfe_samples; j < lfe_end_sample; j++)
|
|---|
| 1092 | av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
|
|---|
| 1093 | av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
|---|
| 1094 | }
|
|---|
| 1095 | #endif
|
|---|
| 1096 |
|
|---|
| 1097 | return 0;
|
|---|
| 1098 | }
|
|---|
| 1099 |
|
|---|
| 1100 | static void qmf_32_subbands(DCAContext *s, int chans,
|
|---|
| 1101 | float samples_in[32][8], float *samples_out,
|
|---|
| 1102 | float scale)
|
|---|
| 1103 | {
|
|---|
| 1104 | const float *prCoeff;
|
|---|
| 1105 | int i;
|
|---|
| 1106 |
|
|---|
| 1107 | int sb_act = s->subband_activity[chans];
|
|---|
| 1108 | int subindex;
|
|---|
| 1109 |
|
|---|
| 1110 | scale *= sqrt(1 / 8.0);
|
|---|
| 1111 |
|
|---|
| 1112 | /* Select filter */
|
|---|
| 1113 | if (!s->multirate_inter) /* Non-perfect reconstruction */
|
|---|
| 1114 | prCoeff = fir_32bands_nonperfect;
|
|---|
| 1115 | else /* Perfect reconstruction */
|
|---|
| 1116 | prCoeff = fir_32bands_perfect;
|
|---|
| 1117 |
|
|---|
| 1118 | for (i = sb_act; i < 32; i++)
|
|---|
| 1119 | s->raXin[i] = 0.0;
|
|---|
| 1120 |
|
|---|
| 1121 | /* Reconstructed channel sample index */
|
|---|
| 1122 | for (subindex = 0; subindex < 8; subindex++) {
|
|---|
| 1123 | /* Load in one sample from each subband and clear inactive subbands */
|
|---|
| 1124 | for (i = 0; i < sb_act; i++) {
|
|---|
| 1125 | unsigned sign = (i - 1) & 2;
|
|---|
| 1126 | uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
|
|---|
| 1127 | AV_WN32A(&s->raXin[i], v);
|
|---|
| 1128 | }
|
|---|
| 1129 |
|
|---|
| 1130 | s->synth.synth_filter_float(&s->imdct,
|
|---|
| 1131 | s->subband_fir_hist[chans],
|
|---|
| 1132 | &s->hist_index[chans],
|
|---|
| 1133 | s->subband_fir_noidea[chans], prCoeff,
|
|---|
| 1134 | samples_out, s->raXin, scale);
|
|---|
| 1135 | samples_out += 32;
|
|---|
| 1136 | }
|
|---|
| 1137 | }
|
|---|
| 1138 |
|
|---|
| 1139 | static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
|
|---|
| 1140 | int num_deci_sample, float *samples_in,
|
|---|
| 1141 | float *samples_out, float scale)
|
|---|
| 1142 | {
|
|---|
| 1143 | /* samples_in: An array holding decimated samples.
|
|---|
| 1144 | * Samples in current subframe starts from samples_in[0],
|
|---|
| 1145 | * while samples_in[-1], samples_in[-2], ..., stores samples
|
|---|
| 1146 | * from last subframe as history.
|
|---|
| 1147 | *
|
|---|
| 1148 | * samples_out: An array holding interpolated samples
|
|---|
| 1149 | */
|
|---|
| 1150 |
|
|---|
| 1151 | int decifactor;
|
|---|
| 1152 | const float *prCoeff;
|
|---|
| 1153 | int deciindex;
|
|---|
| 1154 |
|
|---|
| 1155 | /* Select decimation filter */
|
|---|
| 1156 | if (decimation_select == 1) {
|
|---|
| 1157 | decifactor = 64;
|
|---|
| 1158 | prCoeff = lfe_fir_128;
|
|---|
| 1159 | } else {
|
|---|
| 1160 | decifactor = 32;
|
|---|
| 1161 | prCoeff = lfe_fir_64;
|
|---|
| 1162 | }
|
|---|
| 1163 | /* Interpolation */
|
|---|
| 1164 | for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
|
|---|
| 1165 | s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
|
|---|
| 1166 | samples_in++;
|
|---|
| 1167 | samples_out += 2 * decifactor;
|
|---|
| 1168 | }
|
|---|
| 1169 | }
|
|---|
| 1170 |
|
|---|
| 1171 | /* downmixing routines */
|
|---|
| 1172 | #define MIX_REAR1(samples, si1, rs, coef) \
|
|---|
| 1173 | samples[i] += samples[si1] * coef[rs][0]; \
|
|---|
| 1174 | samples[i+256] += samples[si1] * coef[rs][1];
|
|---|
| 1175 |
|
|---|
| 1176 | #define MIX_REAR2(samples, si1, si2, rs, coef) \
|
|---|
| 1177 | samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
|
|---|
| 1178 | samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
|
|---|
| 1179 |
|
|---|
| 1180 | #define MIX_FRONT3(samples, coef) \
|
|---|
| 1181 | t = samples[i + c]; \
|
|---|
| 1182 | u = samples[i + l]; \
|
|---|
| 1183 | v = samples[i + r]; \
|
|---|
| 1184 | samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
|
|---|
| 1185 | samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
|
|---|
| 1186 |
|
|---|
| 1187 | #define DOWNMIX_TO_STEREO(op1, op2) \
|
|---|
| 1188 | for (i = 0; i < 256; i++) { \
|
|---|
| 1189 | op1 \
|
|---|
| 1190 | op2 \
|
|---|
| 1191 | }
|
|---|
| 1192 |
|
|---|
| 1193 | static void dca_downmix(float *samples, int srcfmt,
|
|---|
| 1194 | int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
|
|---|
| 1195 | const int8_t *channel_mapping)
|
|---|
| 1196 | {
|
|---|
| 1197 | int c, l, r, sl, sr, s;
|
|---|
| 1198 | int i;
|
|---|
| 1199 | float t, u, v;
|
|---|
| 1200 | float coef[DCA_PRIM_CHANNELS_MAX][2];
|
|---|
| 1201 |
|
|---|
| 1202 | for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
|
|---|
| 1203 | coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
|
|---|
| 1204 | coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
|
|---|
| 1205 | }
|
|---|
| 1206 |
|
|---|
| 1207 | switch (srcfmt) {
|
|---|
| 1208 | case DCA_MONO:
|
|---|
| 1209 | case DCA_CHANNEL:
|
|---|
| 1210 | case DCA_STEREO_TOTAL:
|
|---|
| 1211 | case DCA_STEREO_SUMDIFF:
|
|---|
| 1212 | case DCA_4F2R:
|
|---|
| 1213 | av_log(NULL, 0, "Not implemented!\n");
|
|---|
| 1214 | break;
|
|---|
| 1215 | case DCA_STEREO:
|
|---|
| 1216 | break;
|
|---|
| 1217 | case DCA_3F:
|
|---|
| 1218 | c = channel_mapping[0] * 256;
|
|---|
| 1219 | l = channel_mapping[1] * 256;
|
|---|
| 1220 | r = channel_mapping[2] * 256;
|
|---|
| 1221 | DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
|
|---|
| 1222 | break;
|
|---|
| 1223 | case DCA_2F1R:
|
|---|
| 1224 | s = channel_mapping[2] * 256;
|
|---|
| 1225 | DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
|
|---|
| 1226 | break;
|
|---|
| 1227 | case DCA_3F1R:
|
|---|
| 1228 | c = channel_mapping[0] * 256;
|
|---|
| 1229 | l = channel_mapping[1] * 256;
|
|---|
| 1230 | r = channel_mapping[2] * 256;
|
|---|
| 1231 | s = channel_mapping[3] * 256;
|
|---|
| 1232 | DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
|
|---|
| 1233 | MIX_REAR1(samples, i + s, 3, coef));
|
|---|
| 1234 | break;
|
|---|
| 1235 | case DCA_2F2R:
|
|---|
| 1236 | sl = channel_mapping[2] * 256;
|
|---|
| 1237 | sr = channel_mapping[3] * 256;
|
|---|
| 1238 | DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
|
|---|
| 1239 | break;
|
|---|
| 1240 | case DCA_3F2R:
|
|---|
| 1241 | c = channel_mapping[0] * 256;
|
|---|
| 1242 | l = channel_mapping[1] * 256;
|
|---|
| 1243 | r = channel_mapping[2] * 256;
|
|---|
| 1244 | sl = channel_mapping[3] * 256;
|
|---|
| 1245 | sr = channel_mapping[4] * 256;
|
|---|
| 1246 | DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
|
|---|
| 1247 | MIX_REAR2(samples, i + sl, i + sr, 3, coef));
|
|---|
| 1248 | break;
|
|---|
| 1249 | }
|
|---|
| 1250 | }
|
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 | #ifndef decode_blockcodes
|
|---|
| 1254 | /* Very compact version of the block code decoder that does not use table
|
|---|
| 1255 | * look-up but is slightly slower */
|
|---|
| 1256 | static int decode_blockcode(int code, int levels, int *values)
|
|---|
| 1257 | {
|
|---|
| 1258 | int i;
|
|---|
| 1259 | int offset = (levels - 1) >> 1;
|
|---|
| 1260 |
|
|---|
| 1261 | for (i = 0; i < 4; i++) {
|
|---|
| 1262 | int div = FASTDIV(code, levels);
|
|---|
| 1263 | values[i] = code - offset - div * levels;
|
|---|
| 1264 | code = div;
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | return code;
|
|---|
| 1268 | }
|
|---|
| 1269 |
|
|---|
| 1270 | static int decode_blockcodes(int code1, int code2, int levels, int *values)
|
|---|
| 1271 | {
|
|---|
| 1272 | return decode_blockcode(code1, levels, values) |
|
|---|
| 1273 | decode_blockcode(code2, levels, values + 4);
|
|---|
| 1274 | }
|
|---|
| 1275 | #endif
|
|---|
| 1276 |
|
|---|
| 1277 | static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
|
|---|
| 1278 | static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
|
|---|
| 1279 |
|
|---|
| 1280 | #ifndef int8x8_fmul_int32
|
|---|
| 1281 | static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
|
|---|
| 1282 | {
|
|---|
| 1283 | float fscale = scale / 16.0;
|
|---|
| 1284 | int i;
|
|---|
| 1285 | for (i = 0; i < 8; i++)
|
|---|
| 1286 | dst[i] = src[i] * fscale;
|
|---|
| 1287 | }
|
|---|
| 1288 | #endif
|
|---|
| 1289 |
|
|---|
| 1290 | static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
|
|---|
| 1291 | {
|
|---|
| 1292 | int k, l;
|
|---|
| 1293 | int subsubframe = s->current_subsubframe;
|
|---|
| 1294 |
|
|---|
| 1295 | const float *quant_step_table;
|
|---|
| 1296 |
|
|---|
| 1297 | /* FIXME */
|
|---|
| 1298 | float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
|
|---|
| 1299 | LOCAL_ALIGNED_16(int, block, [8]);
|
|---|
| 1300 |
|
|---|
| 1301 | /*
|
|---|
| 1302 | * Audio data
|
|---|
| 1303 | */
|
|---|
| 1304 |
|
|---|
| 1305 | /* Select quantization step size table */
|
|---|
| 1306 | if (s->bit_rate_index == 0x1f)
|
|---|
| 1307 | quant_step_table = lossless_quant_d;
|
|---|
| 1308 | else
|
|---|
| 1309 | quant_step_table = lossy_quant_d;
|
|---|
| 1310 |
|
|---|
| 1311 | for (k = base_channel; k < s->prim_channels; k++) {
|
|---|
| 1312 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 1313 | return AVERROR_INVALIDDATA;
|
|---|
| 1314 |
|
|---|
| 1315 | for (l = 0; l < s->vq_start_subband[k]; l++) {
|
|---|
| 1316 | int m;
|
|---|
| 1317 |
|
|---|
| 1318 | /* Select the mid-tread linear quantizer */
|
|---|
| 1319 | int abits = s->bitalloc[k][l];
|
|---|
| 1320 |
|
|---|
| 1321 | float quant_step_size = quant_step_table[abits];
|
|---|
| 1322 |
|
|---|
| 1323 | /*
|
|---|
| 1324 | * Determine quantization index code book and its type
|
|---|
| 1325 | */
|
|---|
| 1326 |
|
|---|
| 1327 | /* Select quantization index code book */
|
|---|
| 1328 | int sel = s->quant_index_huffman[k][abits];
|
|---|
| 1329 |
|
|---|
| 1330 | /*
|
|---|
| 1331 | * Extract bits from the bit stream
|
|---|
| 1332 | */
|
|---|
| 1333 | if (!abits) {
|
|---|
| 1334 | memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
|
|---|
| 1335 | } else {
|
|---|
| 1336 | /* Deal with transients */
|
|---|
| 1337 | int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
|
|---|
| 1338 | float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
|
|---|
| 1339 | s->scalefactor_adj[k][sel];
|
|---|
| 1340 |
|
|---|
| 1341 | if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
|
|---|
| 1342 | if (abits <= 7) {
|
|---|
| 1343 | /* Block code */
|
|---|
| 1344 | int block_code1, block_code2, size, levels, err;
|
|---|
| 1345 |
|
|---|
| 1346 | size = abits_sizes[abits - 1];
|
|---|
| 1347 | levels = abits_levels[abits - 1];
|
|---|
| 1348 |
|
|---|
| 1349 | block_code1 = get_bits(&s->gb, size);
|
|---|
| 1350 | block_code2 = get_bits(&s->gb, size);
|
|---|
| 1351 | err = decode_blockcodes(block_code1, block_code2,
|
|---|
| 1352 | levels, block);
|
|---|
| 1353 | if (err) {
|
|---|
| 1354 | av_log(s->avctx, AV_LOG_ERROR,
|
|---|
| 1355 | "ERROR: block code look-up failed\n");
|
|---|
| 1356 | return AVERROR_INVALIDDATA;
|
|---|
| 1357 | }
|
|---|
| 1358 | } else {
|
|---|
| 1359 | /* no coding */
|
|---|
| 1360 | for (m = 0; m < 8; m++)
|
|---|
| 1361 | block[m] = get_sbits(&s->gb, abits - 3);
|
|---|
| 1362 | }
|
|---|
| 1363 | } else {
|
|---|
| 1364 | /* Huffman coded */
|
|---|
| 1365 | for (m = 0; m < 8; m++)
|
|---|
| 1366 | block[m] = get_bitalloc(&s->gb,
|
|---|
| 1367 | &dca_smpl_bitalloc[abits], sel);
|
|---|
| 1368 | }
|
|---|
| 1369 |
|
|---|
| 1370 | s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
|
|---|
| 1371 | block, rscale, 8);
|
|---|
| 1372 | }
|
|---|
| 1373 |
|
|---|
| 1374 | /*
|
|---|
| 1375 | * Inverse ADPCM if in prediction mode
|
|---|
| 1376 | */
|
|---|
| 1377 | if (s->prediction_mode[k][l]) {
|
|---|
| 1378 | int n;
|
|---|
| 1379 | for (m = 0; m < 8; m++) {
|
|---|
| 1380 | for (n = 1; n <= 4; n++)
|
|---|
| 1381 | if (m >= n)
|
|---|
| 1382 | subband_samples[k][l][m] +=
|
|---|
| 1383 | (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
|
|---|
| 1384 | subband_samples[k][l][m - n] / 8192);
|
|---|
| 1385 | else if (s->predictor_history)
|
|---|
| 1386 | subband_samples[k][l][m] +=
|
|---|
| 1387 | (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
|
|---|
| 1388 | s->subband_samples_hist[k][l][m - n + 4] / 8192);
|
|---|
| 1389 | }
|
|---|
| 1390 | }
|
|---|
| 1391 | }
|
|---|
| 1392 |
|
|---|
| 1393 | /*
|
|---|
| 1394 | * Decode VQ encoded high frequencies
|
|---|
| 1395 | */
|
|---|
| 1396 | for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
|
|---|
| 1397 | /* 1 vector -> 32 samples but we only need the 8 samples
|
|---|
| 1398 | * for this subsubframe. */
|
|---|
| 1399 | int hfvq = s->high_freq_vq[k][l];
|
|---|
| 1400 |
|
|---|
| 1401 | if (!s->debug_flag & 0x01) {
|
|---|
| 1402 | av_log(s->avctx, AV_LOG_DEBUG,
|
|---|
| 1403 | "Stream with high frequencies VQ coding\n");
|
|---|
| 1404 | s->debug_flag |= 0x01;
|
|---|
| 1405 | }
|
|---|
| 1406 |
|
|---|
| 1407 | int8x8_fmul_int32(subband_samples[k][l],
|
|---|
| 1408 | &high_freq_vq[hfvq][subsubframe * 8],
|
|---|
| 1409 | s->scale_factor[k][l][0]);
|
|---|
| 1410 | }
|
|---|
| 1411 | }
|
|---|
| 1412 |
|
|---|
| 1413 | /* Check for DSYNC after subsubframe */
|
|---|
| 1414 | if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
|
|---|
| 1415 | if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
|
|---|
| 1416 | #ifdef TRACE
|
|---|
| 1417 | av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
|
|---|
| 1418 | #endif
|
|---|
| 1419 | } else {
|
|---|
| 1420 | av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
|
|---|
| 1421 | }
|
|---|
| 1422 | }
|
|---|
| 1423 |
|
|---|
| 1424 | /* Backup predictor history for adpcm */
|
|---|
| 1425 | for (k = base_channel; k < s->prim_channels; k++)
|
|---|
| 1426 | for (l = 0; l < s->vq_start_subband[k]; l++)
|
|---|
| 1427 | memcpy(s->subband_samples_hist[k][l],
|
|---|
| 1428 | &subband_samples[k][l][4],
|
|---|
| 1429 | 4 * sizeof(subband_samples[0][0][0]));
|
|---|
| 1430 |
|
|---|
| 1431 | return 0;
|
|---|
| 1432 | }
|
|---|
| 1433 |
|
|---|
| 1434 | static int dca_filter_channels(DCAContext *s, int block_index)
|
|---|
| 1435 | {
|
|---|
| 1436 | float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
|
|---|
| 1437 | int k;
|
|---|
| 1438 |
|
|---|
| 1439 | /* 32 subbands QMF */
|
|---|
| 1440 | for (k = 0; k < s->prim_channels; k++) {
|
|---|
| 1441 | /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
|
|---|
| 1442 | 0, 8388608.0, 8388608.0 };*/
|
|---|
| 1443 | qmf_32_subbands(s, k, subband_samples[k],
|
|---|
| 1444 | &s->samples[256 * s->channel_order_tab[k]],
|
|---|
| 1445 | M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */);
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| 1448 | /* Down mixing */
|
|---|
| 1449 | if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
|
|---|
| 1450 | dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
|
|---|
| 1451 | }
|
|---|
| 1452 |
|
|---|
| 1453 | /* Generate LFE samples for this subsubframe FIXME!!! */
|
|---|
| 1454 | if (s->output & DCA_LFE) {
|
|---|
| 1455 | lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
|
|---|
| 1456 | s->lfe_data + 2 * s->lfe * (block_index + 4),
|
|---|
| 1457 | &s->samples[256 * s->lfe_index],
|
|---|
| 1458 | (1.0 / 256.0) * s->scale_bias);
|
|---|
| 1459 | /* Outputs 20bits pcm samples */
|
|---|
| 1460 | }
|
|---|
| 1461 |
|
|---|
| 1462 | return 0;
|
|---|
| 1463 | }
|
|---|
| 1464 |
|
|---|
| 1465 |
|
|---|
| 1466 | static int dca_subframe_footer(DCAContext *s, int base_channel)
|
|---|
| 1467 | {
|
|---|
| 1468 | int aux_data_count = 0, i;
|
|---|
| 1469 |
|
|---|
| 1470 | /*
|
|---|
| 1471 | * Unpack optional information
|
|---|
| 1472 | */
|
|---|
| 1473 |
|
|---|
| 1474 | /* presumably optional information only appears in the core? */
|
|---|
| 1475 | if (!base_channel) {
|
|---|
| 1476 | if (s->timestamp)
|
|---|
| 1477 | skip_bits_long(&s->gb, 32);
|
|---|
| 1478 |
|
|---|
| 1479 | if (s->aux_data)
|
|---|
| 1480 | aux_data_count = get_bits(&s->gb, 6);
|
|---|
| 1481 |
|
|---|
| 1482 | for (i = 0; i < aux_data_count; i++)
|
|---|
| 1483 | get_bits(&s->gb, 8);
|
|---|
| 1484 |
|
|---|
| 1485 | if (s->crc_present && (s->downmix || s->dynrange))
|
|---|
| 1486 | get_bits(&s->gb, 16);
|
|---|
| 1487 | }
|
|---|
| 1488 |
|
|---|
| 1489 | return 0;
|
|---|
| 1490 | }
|
|---|
| 1491 |
|
|---|
| 1492 | /**
|
|---|
| 1493 | * Decode a dca frame block
|
|---|
| 1494 | *
|
|---|
| 1495 | * @param s pointer to the DCAContext
|
|---|
| 1496 | */
|
|---|
| 1497 |
|
|---|
| 1498 | static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
|
|---|
| 1499 | {
|
|---|
| 1500 | int ret;
|
|---|
| 1501 |
|
|---|
| 1502 | /* Sanity check */
|
|---|
| 1503 | if (s->current_subframe >= s->subframes) {
|
|---|
| 1504 | av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
|
|---|
| 1505 | s->current_subframe, s->subframes);
|
|---|
| 1506 | return AVERROR_INVALIDDATA;
|
|---|
| 1507 | }
|
|---|
| 1508 |
|
|---|
| 1509 | if (!s->current_subsubframe) {
|
|---|
| 1510 | #ifdef TRACE
|
|---|
| 1511 | av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
|
|---|
| 1512 | #endif
|
|---|
| 1513 | /* Read subframe header */
|
|---|
| 1514 | if ((ret = dca_subframe_header(s, base_channel, block_index)))
|
|---|
| 1515 | return ret;
|
|---|
| 1516 | }
|
|---|
| 1517 |
|
|---|
| 1518 | /* Read subsubframe */
|
|---|
| 1519 | #ifdef TRACE
|
|---|
| 1520 | av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
|
|---|
| 1521 | #endif
|
|---|
| 1522 | if ((ret = dca_subsubframe(s, base_channel, block_index)))
|
|---|
| 1523 | return ret;
|
|---|
| 1524 |
|
|---|
| 1525 | /* Update state */
|
|---|
| 1526 | s->current_subsubframe++;
|
|---|
| 1527 | if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
|
|---|
| 1528 | s->current_subsubframe = 0;
|
|---|
| 1529 | s->current_subframe++;
|
|---|
| 1530 | }
|
|---|
| 1531 | if (s->current_subframe >= s->subframes) {
|
|---|
| 1532 | #ifdef TRACE
|
|---|
| 1533 | av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
|
|---|
| 1534 | #endif
|
|---|
| 1535 | /* Read subframe footer */
|
|---|
| 1536 | if ((ret = dca_subframe_footer(s, base_channel)))
|
|---|
| 1537 | return ret;
|
|---|
| 1538 | }
|
|---|
| 1539 |
|
|---|
| 1540 | return 0;
|
|---|
| 1541 | }
|
|---|
| 1542 |
|
|---|
| 1543 | /**
|
|---|
| 1544 | * Return the number of channels in an ExSS speaker mask (HD)
|
|---|
| 1545 | */
|
|---|
| 1546 | static int dca_exss_mask2count(int mask)
|
|---|
| 1547 | {
|
|---|
| 1548 | /* count bits that mean speaker pairs twice */
|
|---|
| 1549 | return av_popcount(mask) +
|
|---|
| 1550 | av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
|
|---|
| 1551 | DCA_EXSS_FRONT_LEFT_RIGHT |
|
|---|
| 1552 | DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
|
|---|
| 1553 | DCA_EXSS_WIDE_LEFT_RIGHT |
|
|---|
| 1554 | DCA_EXSS_SIDE_LEFT_RIGHT |
|
|---|
| 1555 | DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
|
|---|
| 1556 | DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
|
|---|
| 1557 | DCA_EXSS_REAR_LEFT_RIGHT |
|
|---|
| 1558 | DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
|
|---|
| 1559 | }
|
|---|
| 1560 |
|
|---|
| 1561 | /**
|
|---|
| 1562 | * Skip mixing coefficients of a single mix out configuration (HD)
|
|---|
| 1563 | */
|
|---|
| 1564 | static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
|
|---|
| 1565 | {
|
|---|
| 1566 | int i;
|
|---|
| 1567 |
|
|---|
| 1568 | for (i = 0; i < channels; i++) {
|
|---|
| 1569 | int mix_map_mask = get_bits(gb, out_ch);
|
|---|
| 1570 | int num_coeffs = av_popcount(mix_map_mask);
|
|---|
| 1571 | skip_bits_long(gb, num_coeffs * 6);
|
|---|
| 1572 | }
|
|---|
| 1573 | }
|
|---|
| 1574 |
|
|---|
| 1575 | /**
|
|---|
| 1576 | * Parse extension substream asset header (HD)
|
|---|
| 1577 | */
|
|---|
| 1578 | static int dca_exss_parse_asset_header(DCAContext *s)
|
|---|
| 1579 | {
|
|---|
| 1580 | int header_pos = get_bits_count(&s->gb);
|
|---|
| 1581 | int header_size;
|
|---|
| 1582 | int channels = 0;
|
|---|
| 1583 | int embedded_stereo = 0;
|
|---|
| 1584 | int embedded_6ch = 0;
|
|---|
| 1585 | int drc_code_present;
|
|---|
| 1586 | int av_uninit(extensions_mask);
|
|---|
| 1587 | int i, j;
|
|---|
| 1588 |
|
|---|
| 1589 | if (get_bits_left(&s->gb) < 16)
|
|---|
| 1590 | return -1;
|
|---|
| 1591 |
|
|---|
| 1592 | /* We will parse just enough to get to the extensions bitmask with which
|
|---|
| 1593 | * we can set the profile value. */
|
|---|
| 1594 |
|
|---|
| 1595 | header_size = get_bits(&s->gb, 9) + 1;
|
|---|
| 1596 | skip_bits(&s->gb, 3); // asset index
|
|---|
| 1597 |
|
|---|
| 1598 | if (s->static_fields) {
|
|---|
| 1599 | if (get_bits1(&s->gb))
|
|---|
| 1600 | skip_bits(&s->gb, 4); // asset type descriptor
|
|---|
| 1601 | if (get_bits1(&s->gb))
|
|---|
| 1602 | skip_bits_long(&s->gb, 24); // language descriptor
|
|---|
| 1603 |
|
|---|
| 1604 | if (get_bits1(&s->gb)) {
|
|---|
| 1605 | /* How can one fit 1024 bytes of text here if the maximum value
|
|---|
| 1606 | * for the asset header size field above was 512 bytes? */
|
|---|
| 1607 | int text_length = get_bits(&s->gb, 10) + 1;
|
|---|
| 1608 | if (get_bits_left(&s->gb) < text_length * 8)
|
|---|
| 1609 | return -1;
|
|---|
| 1610 | skip_bits_long(&s->gb, text_length * 8); // info text
|
|---|
| 1611 | }
|
|---|
| 1612 |
|
|---|
| 1613 | skip_bits(&s->gb, 5); // bit resolution - 1
|
|---|
| 1614 | skip_bits(&s->gb, 4); // max sample rate code
|
|---|
| 1615 | channels = get_bits(&s->gb, 8) + 1;
|
|---|
| 1616 |
|
|---|
| 1617 | if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
|
|---|
| 1618 | int spkr_remap_sets;
|
|---|
| 1619 | int spkr_mask_size = 16;
|
|---|
| 1620 | int num_spkrs[7];
|
|---|
| 1621 |
|
|---|
| 1622 | if (channels > 2)
|
|---|
| 1623 | embedded_stereo = get_bits1(&s->gb);
|
|---|
| 1624 | if (channels > 6)
|
|---|
| 1625 | embedded_6ch = get_bits1(&s->gb);
|
|---|
| 1626 |
|
|---|
| 1627 | if (get_bits1(&s->gb)) {
|
|---|
| 1628 | spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
|
|---|
| 1629 | skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
|
|---|
| 1630 | }
|
|---|
| 1631 |
|
|---|
| 1632 | spkr_remap_sets = get_bits(&s->gb, 3);
|
|---|
| 1633 |
|
|---|
| 1634 | for (i = 0; i < spkr_remap_sets; i++) {
|
|---|
| 1635 | /* std layout mask for each remap set */
|
|---|
| 1636 | num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
|
|---|
| 1637 | }
|
|---|
| 1638 |
|
|---|
| 1639 | for (i = 0; i < spkr_remap_sets; i++) {
|
|---|
| 1640 | int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
|
|---|
| 1641 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 1642 | return -1;
|
|---|
| 1643 |
|
|---|
| 1644 | for (j = 0; j < num_spkrs[i]; j++) {
|
|---|
| 1645 | int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
|
|---|
| 1646 | int num_dec_ch = av_popcount(remap_dec_ch_mask);
|
|---|
| 1647 | skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
|
|---|
| 1648 | }
|
|---|
| 1649 | }
|
|---|
| 1650 |
|
|---|
| 1651 | } else {
|
|---|
| 1652 | skip_bits(&s->gb, 3); // representation type
|
|---|
| 1653 | }
|
|---|
| 1654 | }
|
|---|
| 1655 |
|
|---|
| 1656 | drc_code_present = get_bits1(&s->gb);
|
|---|
| 1657 | if (drc_code_present)
|
|---|
| 1658 | get_bits(&s->gb, 8); // drc code
|
|---|
| 1659 |
|
|---|
| 1660 | if (get_bits1(&s->gb))
|
|---|
| 1661 | skip_bits(&s->gb, 5); // dialog normalization code
|
|---|
| 1662 |
|
|---|
| 1663 | if (drc_code_present && embedded_stereo)
|
|---|
| 1664 | get_bits(&s->gb, 8); // drc stereo code
|
|---|
| 1665 |
|
|---|
| 1666 | if (s->mix_metadata && get_bits1(&s->gb)) {
|
|---|
| 1667 | skip_bits(&s->gb, 1); // external mix
|
|---|
| 1668 | skip_bits(&s->gb, 6); // post mix gain code
|
|---|
| 1669 |
|
|---|
| 1670 | if (get_bits(&s->gb, 2) != 3) // mixer drc code
|
|---|
| 1671 | skip_bits(&s->gb, 3); // drc limit
|
|---|
| 1672 | else
|
|---|
| 1673 | skip_bits(&s->gb, 8); // custom drc code
|
|---|
| 1674 |
|
|---|
| 1675 | if (get_bits1(&s->gb)) // channel specific scaling
|
|---|
| 1676 | for (i = 0; i < s->num_mix_configs; i++)
|
|---|
| 1677 | skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
|
|---|
| 1678 | else
|
|---|
| 1679 | skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
|
|---|
| 1680 |
|
|---|
| 1681 | for (i = 0; i < s->num_mix_configs; i++) {
|
|---|
| 1682 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 1683 | return -1;
|
|---|
| 1684 | dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
|
|---|
| 1685 | if (embedded_6ch)
|
|---|
| 1686 | dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
|
|---|
| 1687 | if (embedded_stereo)
|
|---|
| 1688 | dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
|
|---|
| 1689 | }
|
|---|
| 1690 | }
|
|---|
| 1691 |
|
|---|
| 1692 | switch (get_bits(&s->gb, 2)) {
|
|---|
| 1693 | case 0: extensions_mask = get_bits(&s->gb, 12); break;
|
|---|
| 1694 | case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
|
|---|
| 1695 | case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
|
|---|
| 1696 | case 3: extensions_mask = 0; /* aux coding */ break;
|
|---|
| 1697 | }
|
|---|
| 1698 |
|
|---|
| 1699 | /* not parsed further, we were only interested in the extensions mask */
|
|---|
| 1700 |
|
|---|
| 1701 | if (get_bits_left(&s->gb) < 0)
|
|---|
| 1702 | return -1;
|
|---|
| 1703 |
|
|---|
| 1704 | if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
|
|---|
| 1705 | av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
|
|---|
| 1706 | return -1;
|
|---|
| 1707 | }
|
|---|
| 1708 | skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
|
|---|
| 1709 |
|
|---|
| 1710 | if (extensions_mask & DCA_EXT_EXSS_XLL)
|
|---|
| 1711 | s->profile = FF_PROFILE_DTS_HD_MA;
|
|---|
| 1712 | else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
|
|---|
| 1713 | DCA_EXT_EXSS_XXCH))
|
|---|
| 1714 | s->profile = FF_PROFILE_DTS_HD_HRA;
|
|---|
| 1715 |
|
|---|
| 1716 | if (!(extensions_mask & DCA_EXT_CORE))
|
|---|
| 1717 | av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
|
|---|
| 1718 | if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
|
|---|
| 1719 | av_log(s->avctx, AV_LOG_WARNING,
|
|---|
| 1720 | "DTS extensions detection mismatch (%d, %d)\n",
|
|---|
| 1721 | extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
|
|---|
| 1722 |
|
|---|
| 1723 | return 0;
|
|---|
| 1724 | }
|
|---|
| 1725 |
|
|---|
| 1726 | static int dca_xbr_parse_frame(DCAContext *s)
|
|---|
| 1727 | {
|
|---|
| 1728 | int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
|
|---|
| 1729 | int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
|
|---|
| 1730 | int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
|
|---|
| 1731 | int anctemp[DCA_CHSET_CHANS_MAX];
|
|---|
| 1732 | int chset_fsize[DCA_CHSETS_MAX];
|
|---|
| 1733 | int n_xbr_ch[DCA_CHSETS_MAX];
|
|---|
| 1734 | int hdr_size, num_chsets, xbr_tmode, hdr_pos;
|
|---|
| 1735 | int i, j, k, l, chset, chan_base;
|
|---|
| 1736 | LOCAL_ALIGNED_16(int, block, [8]);
|
|---|
| 1737 |
|
|---|
| 1738 | av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
|
|---|
| 1739 |
|
|---|
| 1740 | /* get bit position of sync header */
|
|---|
| 1741 | hdr_pos = get_bits_count(&s->gb) - 32;
|
|---|
| 1742 |
|
|---|
| 1743 | hdr_size = get_bits(&s->gb, 6) + 1;
|
|---|
| 1744 | num_chsets = get_bits(&s->gb, 2) + 1;
|
|---|
| 1745 |
|
|---|
| 1746 | for(i = 0; i < num_chsets; i++)
|
|---|
| 1747 | chset_fsize[i] = get_bits(&s->gb, 14) + 1;
|
|---|
| 1748 |
|
|---|
| 1749 | xbr_tmode = get_bits1(&s->gb);
|
|---|
| 1750 |
|
|---|
| 1751 | for(i = 0; i < num_chsets; i++) {
|
|---|
| 1752 | n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
|
|---|
| 1753 | k = get_bits(&s->gb, 2) + 5;
|
|---|
| 1754 | for(j = 0; j < n_xbr_ch[i]; j++)
|
|---|
| 1755 | active_bands[i][j] = get_bits(&s->gb, k) + 1;
|
|---|
| 1756 | }
|
|---|
| 1757 |
|
|---|
| 1758 | /* skip to the end of the header */
|
|---|
| 1759 | i = get_bits_count(&s->gb);
|
|---|
| 1760 | if(hdr_pos + hdr_size * 8 > i)
|
|---|
| 1761 | skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
|
|---|
| 1762 |
|
|---|
| 1763 | /* loop over the channel data sets */
|
|---|
| 1764 | /* only decode as many channels as we've decoded base data for */
|
|---|
| 1765 | for(chset = 0, chan_base = 0;
|
|---|
| 1766 | chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
|
|---|
| 1767 | chan_base += n_xbr_ch[chset++]) {
|
|---|
| 1768 | int start_posn = get_bits_count(&s->gb);
|
|---|
| 1769 | int subsubframe = 0;
|
|---|
| 1770 | int subframe = 0;
|
|---|
| 1771 |
|
|---|
| 1772 | /* loop over subframes */
|
|---|
| 1773 | for (k = 0; k < (s->sample_blocks / 8); k++) {
|
|---|
| 1774 | /* parse header if we're on first subsubframe of a block */
|
|---|
| 1775 | if(subsubframe == 0) {
|
|---|
| 1776 | /* Parse subframe header */
|
|---|
| 1777 | for(i = 0; i < n_xbr_ch[chset]; i++) {
|
|---|
| 1778 | anctemp[i] = get_bits(&s->gb, 2) + 2;
|
|---|
| 1779 | }
|
|---|
| 1780 |
|
|---|
| 1781 | for(i = 0; i < n_xbr_ch[chset]; i++) {
|
|---|
| 1782 | get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
|
|---|
| 1783 | }
|
|---|
| 1784 |
|
|---|
| 1785 | for(i = 0; i < n_xbr_ch[chset]; i++) {
|
|---|
| 1786 | anctemp[i] = get_bits(&s->gb, 3);
|
|---|
| 1787 | if(anctemp[i] < 1) {
|
|---|
| 1788 | av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
|
|---|
| 1789 | return AVERROR_INVALIDDATA;
|
|---|
| 1790 | }
|
|---|
| 1791 | }
|
|---|
| 1792 |
|
|---|
| 1793 | /* generate scale factors */
|
|---|
| 1794 | for(i = 0; i < n_xbr_ch[chset]; i++) {
|
|---|
| 1795 | const uint32_t *scale_table;
|
|---|
| 1796 | int nbits;
|
|---|
| 1797 |
|
|---|
| 1798 | if (s->scalefactor_huffman[chan_base+i] == 6) {
|
|---|
| 1799 | scale_table = scale_factor_quant7;
|
|---|
| 1800 | } else {
|
|---|
| 1801 | scale_table = scale_factor_quant6;
|
|---|
| 1802 | }
|
|---|
| 1803 |
|
|---|
| 1804 | nbits = anctemp[i];
|
|---|
| 1805 |
|
|---|
| 1806 | for(j = 0; j < active_bands[chset][i]; j++) {
|
|---|
| 1807 | if(abits_high[i][j] > 0) {
|
|---|
| 1808 | scale_table_high[i][j][0] =
|
|---|
| 1809 | scale_table[get_bits(&s->gb, nbits)];
|
|---|
| 1810 |
|
|---|
| 1811 | if(xbr_tmode && s->transition_mode[i][j]) {
|
|---|
| 1812 | scale_table_high[i][j][1] =
|
|---|
| 1813 | scale_table[get_bits(&s->gb, nbits)];
|
|---|
| 1814 | }
|
|---|
| 1815 | }
|
|---|
| 1816 | }
|
|---|
| 1817 | }
|
|---|
| 1818 | }
|
|---|
| 1819 |
|
|---|
| 1820 | /* decode audio array for this block */
|
|---|
| 1821 | for(i = 0; i < n_xbr_ch[chset]; i++) {
|
|---|
| 1822 | for(j = 0; j < active_bands[chset][i]; j++) {
|
|---|
| 1823 | const int xbr_abits = abits_high[i][j];
|
|---|
| 1824 | const float quant_step_size = lossless_quant_d[xbr_abits];
|
|---|
| 1825 | const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
|
|---|
| 1826 | const float rscale = quant_step_size * scale_table_high[i][j][sfi];
|
|---|
| 1827 | float *subband_samples = s->subband_samples[k][chan_base+i][j];
|
|---|
| 1828 |
|
|---|
| 1829 | if(xbr_abits <= 0)
|
|---|
| 1830 | continue;
|
|---|
| 1831 |
|
|---|
| 1832 | if(xbr_abits > 7) {
|
|---|
| 1833 | get_array(&s->gb, block, 8, xbr_abits - 3);
|
|---|
| 1834 | } else {
|
|---|
| 1835 | int block_code1, block_code2, size, levels, err;
|
|---|
| 1836 |
|
|---|
| 1837 | size = abits_sizes[xbr_abits - 1];
|
|---|
| 1838 | levels = abits_levels[xbr_abits - 1];
|
|---|
| 1839 |
|
|---|
| 1840 | block_code1 = get_bits(&s->gb, size);
|
|---|
| 1841 | block_code2 = get_bits(&s->gb, size);
|
|---|
| 1842 | err = decode_blockcodes(block_code1, block_code2,
|
|---|
| 1843 | levels, block);
|
|---|
| 1844 | if (err) {
|
|---|
| 1845 | av_log(s->avctx, AV_LOG_ERROR,
|
|---|
| 1846 | "ERROR: DTS-XBR: block code look-up failed\n");
|
|---|
| 1847 | return AVERROR_INVALIDDATA;
|
|---|
| 1848 | }
|
|---|
| 1849 | }
|
|---|
| 1850 |
|
|---|
| 1851 | /* scale & sum into subband */
|
|---|
| 1852 | for(l = 0; l < 8; l++)
|
|---|
| 1853 | subband_samples[l] += (float)block[l] * rscale;
|
|---|
| 1854 | }
|
|---|
| 1855 | }
|
|---|
| 1856 |
|
|---|
| 1857 | /* check DSYNC marker */
|
|---|
| 1858 | if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
|
|---|
| 1859 | if(get_bits(&s->gb, 16) != 0xffff) {
|
|---|
| 1860 | av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
|
|---|
| 1861 | return AVERROR_INVALIDDATA;
|
|---|
| 1862 | }
|
|---|
| 1863 | }
|
|---|
| 1864 |
|
|---|
| 1865 | /* advance sub-sub-frame index */
|
|---|
| 1866 | if(++subsubframe >= s->subsubframes[subframe]) {
|
|---|
| 1867 | subsubframe = 0;
|
|---|
| 1868 | subframe++;
|
|---|
| 1869 | }
|
|---|
| 1870 | }
|
|---|
| 1871 |
|
|---|
| 1872 | /* skip to next channel set */
|
|---|
| 1873 | i = get_bits_count(&s->gb);
|
|---|
| 1874 | if(start_posn + chset_fsize[chset] * 8 != i) {
|
|---|
| 1875 | j = start_posn + chset_fsize[chset] * 8 - i;
|
|---|
| 1876 | if(j < 0 || j >= 8)
|
|---|
| 1877 | av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
|
|---|
| 1878 | " skipping further than expected (%d bits)\n", j);
|
|---|
| 1879 | skip_bits_long(&s->gb, j);
|
|---|
| 1880 | }
|
|---|
| 1881 | }
|
|---|
| 1882 |
|
|---|
| 1883 | return 0;
|
|---|
| 1884 | }
|
|---|
| 1885 |
|
|---|
| 1886 | /* parse initial header for XXCH and dump details */
|
|---|
| 1887 | static int dca_xxch_decode_frame(DCAContext *s)
|
|---|
| 1888 | {
|
|---|
| 1889 | int hdr_size, chhdr_crc, spkmsk_bits, num_chsets, core_spk, hdr_pos;
|
|---|
| 1890 | int i, chset, base_channel, chstart, fsize[8];
|
|---|
| 1891 |
|
|---|
| 1892 | /* assume header word has already been parsed */
|
|---|
| 1893 | hdr_pos = get_bits_count(&s->gb) - 32;
|
|---|
| 1894 | hdr_size = get_bits(&s->gb, 6) + 1;
|
|---|
| 1895 | chhdr_crc = get_bits1(&s->gb);
|
|---|
| 1896 | spkmsk_bits = get_bits(&s->gb, 5) + 1;
|
|---|
| 1897 | num_chsets = get_bits(&s->gb, 2) + 1;
|
|---|
| 1898 |
|
|---|
| 1899 | for (i = 0; i < num_chsets; i++)
|
|---|
| 1900 | fsize[i] = get_bits(&s->gb, 14) + 1;
|
|---|
| 1901 |
|
|---|
| 1902 | core_spk = get_bits(&s->gb, spkmsk_bits);
|
|---|
| 1903 | s->xxch_core_spkmask = core_spk;
|
|---|
| 1904 | s->xxch_nbits_spk_mask = spkmsk_bits;
|
|---|
| 1905 | s->xxch_downmix = 0;
|
|---|
| 1906 | s->xxch_dmix_embedded = 0;
|
|---|
| 1907 |
|
|---|
| 1908 | /* skip to the end of the header */
|
|---|
| 1909 | i = get_bits_count(&s->gb);
|
|---|
| 1910 | if (hdr_pos + hdr_size * 8 > i)
|
|---|
| 1911 | skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
|
|---|
| 1912 |
|
|---|
| 1913 | for (chset = 0; chset < num_chsets; chset++) {
|
|---|
| 1914 | chstart = get_bits_count(&s->gb);
|
|---|
| 1915 | base_channel = s->prim_channels;
|
|---|
| 1916 | s->xxch_chset = chset;
|
|---|
| 1917 |
|
|---|
| 1918 | /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
|
|---|
| 1919 | 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
|
|---|
| 1920 | dca_parse_audio_coding_header(s, base_channel, 1);
|
|---|
| 1921 |
|
|---|
| 1922 | /* decode channel data */
|
|---|
| 1923 | for (i = 0; i < (s->sample_blocks / 8); i++) {
|
|---|
| 1924 | if (dca_decode_block(s, base_channel, i)) {
|
|---|
| 1925 | av_log(s->avctx, AV_LOG_ERROR,
|
|---|
| 1926 | "Error decoding DTS-XXCH extension\n");
|
|---|
| 1927 | continue;
|
|---|
| 1928 | }
|
|---|
| 1929 | }
|
|---|
| 1930 |
|
|---|
| 1931 | /* skip to end of this section */
|
|---|
| 1932 | i = get_bits_count(&s->gb);
|
|---|
| 1933 | if (chstart + fsize[chset] * 8 > i)
|
|---|
| 1934 | skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
|
|---|
| 1935 | }
|
|---|
| 1936 | s->xxch_chset = num_chsets;
|
|---|
| 1937 |
|
|---|
| 1938 | return 0;
|
|---|
| 1939 | }
|
|---|
| 1940 |
|
|---|
| 1941 | /**
|
|---|
| 1942 | * Parse extension substream header (HD)
|
|---|
| 1943 | */
|
|---|
| 1944 | static void dca_exss_parse_header(DCAContext *s)
|
|---|
| 1945 | {
|
|---|
| 1946 | int asset_size[8];
|
|---|
| 1947 | int ss_index;
|
|---|
| 1948 | int blownup;
|
|---|
| 1949 | int num_audiop = 1;
|
|---|
| 1950 | int num_assets = 1;
|
|---|
| 1951 | int active_ss_mask[8];
|
|---|
| 1952 | int i, j;
|
|---|
| 1953 | int start_posn;
|
|---|
| 1954 | int hdrsize;
|
|---|
| 1955 | uint32_t mkr;
|
|---|
| 1956 |
|
|---|
| 1957 | if (get_bits_left(&s->gb) < 52)
|
|---|
| 1958 | return;
|
|---|
| 1959 |
|
|---|
| 1960 | start_posn = get_bits_count(&s->gb) - 32;
|
|---|
| 1961 |
|
|---|
| 1962 | skip_bits(&s->gb, 8); // user data
|
|---|
| 1963 | ss_index = get_bits(&s->gb, 2);
|
|---|
| 1964 |
|
|---|
| 1965 | blownup = get_bits1(&s->gb);
|
|---|
| 1966 | hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
|
|---|
| 1967 | skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
|
|---|
| 1968 |
|
|---|
| 1969 | s->static_fields = get_bits1(&s->gb);
|
|---|
| 1970 | if (s->static_fields) {
|
|---|
| 1971 | skip_bits(&s->gb, 2); // reference clock code
|
|---|
| 1972 | skip_bits(&s->gb, 3); // frame duration code
|
|---|
| 1973 |
|
|---|
| 1974 | if (get_bits1(&s->gb))
|
|---|
| 1975 | skip_bits_long(&s->gb, 36); // timestamp
|
|---|
| 1976 |
|
|---|
| 1977 | /* a single stream can contain multiple audio assets that can be
|
|---|
| 1978 | * combined to form multiple audio presentations */
|
|---|
| 1979 |
|
|---|
| 1980 | num_audiop = get_bits(&s->gb, 3) + 1;
|
|---|
| 1981 | if (num_audiop > 1) {
|
|---|
| 1982 | av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
|
|---|
| 1983 | /* ignore such streams for now */
|
|---|
| 1984 | return;
|
|---|
| 1985 | }
|
|---|
| 1986 |
|
|---|
| 1987 | num_assets = get_bits(&s->gb, 3) + 1;
|
|---|
| 1988 | if (num_assets > 1) {
|
|---|
| 1989 | av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
|
|---|
| 1990 | /* ignore such streams for now */
|
|---|
| 1991 | return;
|
|---|
| 1992 | }
|
|---|
| 1993 |
|
|---|
| 1994 | for (i = 0; i < num_audiop; i++)
|
|---|
| 1995 | active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
|
|---|
| 1996 |
|
|---|
| 1997 | for (i = 0; i < num_audiop; i++)
|
|---|
| 1998 | for (j = 0; j <= ss_index; j++)
|
|---|
| 1999 | if (active_ss_mask[i] & (1 << j))
|
|---|
| 2000 | skip_bits(&s->gb, 8); // active asset mask
|
|---|
| 2001 |
|
|---|
| 2002 | s->mix_metadata = get_bits1(&s->gb);
|
|---|
| 2003 | if (s->mix_metadata) {
|
|---|
| 2004 | int mix_out_mask_size;
|
|---|
| 2005 |
|
|---|
| 2006 | skip_bits(&s->gb, 2); // adjustment level
|
|---|
| 2007 | mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
|
|---|
| 2008 | s->num_mix_configs = get_bits(&s->gb, 2) + 1;
|
|---|
| 2009 |
|
|---|
| 2010 | for (i = 0; i < s->num_mix_configs; i++) {
|
|---|
| 2011 | int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
|
|---|
| 2012 | s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
|
|---|
| 2013 | }
|
|---|
| 2014 | }
|
|---|
| 2015 | }
|
|---|
| 2016 |
|
|---|
| 2017 | for (i = 0; i < num_assets; i++)
|
|---|
| 2018 | asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
|
|---|
| 2019 |
|
|---|
| 2020 | for (i = 0; i < num_assets; i++) {
|
|---|
| 2021 | if (dca_exss_parse_asset_header(s))
|
|---|
| 2022 | return;
|
|---|
| 2023 | }
|
|---|
| 2024 |
|
|---|
| 2025 | /* not parsed further, we were only interested in the extensions mask
|
|---|
| 2026 | * from the asset header */
|
|---|
| 2027 |
|
|---|
| 2028 | if (num_assets > 0) {
|
|---|
| 2029 | j = get_bits_count(&s->gb);
|
|---|
| 2030 | if (start_posn + hdrsize * 8 > j)
|
|---|
| 2031 | skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
|
|---|
| 2032 |
|
|---|
| 2033 | for (i = 0; i < num_assets; i++) {
|
|---|
| 2034 | start_posn = get_bits_count(&s->gb);
|
|---|
| 2035 | mkr = get_bits_long(&s->gb, 32);
|
|---|
| 2036 |
|
|---|
| 2037 | /* parse extensions that we know about */
|
|---|
| 2038 | if (mkr == 0x655e315e) {
|
|---|
| 2039 | dca_xbr_parse_frame(s);
|
|---|
| 2040 | } else if (mkr == 0x47004a03) {
|
|---|
| 2041 | dca_xxch_decode_frame(s);
|
|---|
| 2042 | s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
|
|---|
| 2043 | } else {
|
|---|
| 2044 | av_log(s->avctx, AV_LOG_DEBUG,
|
|---|
| 2045 | "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
|
|---|
| 2046 | }
|
|---|
| 2047 |
|
|---|
| 2048 | /* skip to end of block */
|
|---|
| 2049 | j = get_bits_count(&s->gb);
|
|---|
| 2050 | if (start_posn + asset_size[i] * 8 > j)
|
|---|
| 2051 | skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
|
|---|
| 2052 | }
|
|---|
| 2053 | }
|
|---|
| 2054 | }
|
|---|
| 2055 |
|
|---|
| 2056 | /**
|
|---|
| 2057 | * Main frame decoding function
|
|---|
| 2058 | * FIXME add arguments
|
|---|
| 2059 | */
|
|---|
| 2060 | static int dca_decode_frame(AVCodecContext *avctx, void *data,
|
|---|
| 2061 | int *got_frame_ptr, AVPacket *avpkt)
|
|---|
| 2062 | {
|
|---|
| 2063 | const uint8_t *buf = avpkt->data;
|
|---|
| 2064 | int buf_size = avpkt->size;
|
|---|
| 2065 | int channel_mask;
|
|---|
| 2066 | int channel_layout;
|
|---|
| 2067 | int lfe_samples;
|
|---|
| 2068 | int num_core_channels = 0;
|
|---|
| 2069 | int i, ret;
|
|---|
| 2070 | float *samples_flt;
|
|---|
| 2071 | float *src_chan;
|
|---|
| 2072 | float *dst_chan;
|
|---|
| 2073 | int16_t *samples_s16;
|
|---|
| 2074 | DCAContext *s = avctx->priv_data;
|
|---|
| 2075 | int core_ss_end;
|
|---|
| 2076 | int channels;
|
|---|
| 2077 | float scale;
|
|---|
| 2078 | int achan;
|
|---|
| 2079 | int chset;
|
|---|
| 2080 | int mask;
|
|---|
| 2081 | int lavc;
|
|---|
| 2082 | int posn;
|
|---|
| 2083 | int j, k;
|
|---|
| 2084 | int ch;
|
|---|
| 2085 | int endch;
|
|---|
| 2086 |
|
|---|
| 2087 | s->xch_present = 0;
|
|---|
| 2088 |
|
|---|
| 2089 | s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
|
|---|
| 2090 | DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
|
|---|
| 2091 | if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
|
|---|
| 2092 | av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
|
|---|
| 2093 | return AVERROR_INVALIDDATA;
|
|---|
| 2094 | }
|
|---|
| 2095 |
|
|---|
| 2096 | init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
|
|---|
| 2097 | if ((ret = dca_parse_frame_header(s)) < 0) {
|
|---|
| 2098 | //seems like the frame is corrupt, try with the next one
|
|---|
| 2099 | return ret;
|
|---|
| 2100 | }
|
|---|
| 2101 | //set AVCodec values with parsed data
|
|---|
| 2102 | avctx->sample_rate = s->sample_rate;
|
|---|
| 2103 | avctx->bit_rate = s->bit_rate;
|
|---|
| 2104 |
|
|---|
| 2105 | s->profile = FF_PROFILE_DTS;
|
|---|
| 2106 |
|
|---|
| 2107 | for (i = 0; i < (s->sample_blocks / 8); i++) {
|
|---|
| 2108 | if ((ret = dca_decode_block(s, 0, i))) {
|
|---|
| 2109 | av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
|
|---|
| 2110 | return ret;
|
|---|
| 2111 | }
|
|---|
| 2112 | }
|
|---|
| 2113 |
|
|---|
| 2114 | /* record number of core channels incase less than max channels are requested */
|
|---|
| 2115 | num_core_channels = s->prim_channels;
|
|---|
| 2116 |
|
|---|
| 2117 | if (s->ext_coding)
|
|---|
| 2118 | s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
|
|---|
| 2119 | else
|
|---|
| 2120 | s->core_ext_mask = 0;
|
|---|
| 2121 |
|
|---|
| 2122 | core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
|
|---|
| 2123 |
|
|---|
| 2124 | /* only scan for extensions if ext_descr was unknown or indicated a
|
|---|
| 2125 | * supported XCh extension */
|
|---|
| 2126 | if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
|
|---|
| 2127 |
|
|---|
| 2128 | /* if ext_descr was unknown, clear s->core_ext_mask so that the
|
|---|
| 2129 | * extensions scan can fill it up */
|
|---|
| 2130 | s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
|
|---|
| 2131 |
|
|---|
| 2132 | /* extensions start at 32-bit boundaries into bitstream */
|
|---|
| 2133 | skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
|
|---|
| 2134 |
|
|---|
| 2135 | while (core_ss_end - get_bits_count(&s->gb) >= 32) {
|
|---|
| 2136 | uint32_t bits = get_bits_long(&s->gb, 32);
|
|---|
| 2137 |
|
|---|
| 2138 | switch (bits) {
|
|---|
| 2139 | case 0x5a5a5a5a: {
|
|---|
| 2140 | int ext_amode, xch_fsize;
|
|---|
| 2141 |
|
|---|
| 2142 | s->xch_base_channel = s->prim_channels;
|
|---|
| 2143 |
|
|---|
| 2144 | /* validate sync word using XCHFSIZE field */
|
|---|
| 2145 | xch_fsize = show_bits(&s->gb, 10);
|
|---|
| 2146 | if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
|
|---|
| 2147 | (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
|
|---|
| 2148 | continue;
|
|---|
| 2149 |
|
|---|
| 2150 | /* skip length-to-end-of-frame field for the moment */
|
|---|
| 2151 | skip_bits(&s->gb, 10);
|
|---|
| 2152 |
|
|---|
| 2153 | s->core_ext_mask |= DCA_EXT_XCH;
|
|---|
| 2154 |
|
|---|
| 2155 | /* extension amode(number of channels in extension) should be 1 */
|
|---|
| 2156 | /* AFAIK XCh is not used for more channels */
|
|---|
| 2157 | if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
|
|---|
| 2158 | av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
|
|---|
| 2159 | " supported!\n", ext_amode);
|
|---|
| 2160 | continue;
|
|---|
| 2161 | }
|
|---|
| 2162 |
|
|---|
| 2163 | /* much like core primary audio coding header */
|
|---|
| 2164 | dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
|
|---|
| 2165 |
|
|---|
| 2166 | for (i = 0; i < (s->sample_blocks / 8); i++)
|
|---|
| 2167 | if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
|
|---|
| 2168 | av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
|
|---|
| 2169 | continue;
|
|---|
| 2170 | }
|
|---|
| 2171 |
|
|---|
| 2172 | s->xch_present = 1;
|
|---|
| 2173 | break;
|
|---|
| 2174 | }
|
|---|
| 2175 | case 0x47004a03:
|
|---|
| 2176 | /* XXCh: extended channels */
|
|---|
| 2177 | /* usually found either in core or HD part in DTS-HD HRA streams,
|
|---|
| 2178 | * but not in DTS-ES which contains XCh extensions instead */
|
|---|
| 2179 | s->core_ext_mask |= DCA_EXT_XXCH;
|
|---|
| 2180 | dca_xxch_decode_frame(s);
|
|---|
| 2181 | break;
|
|---|
| 2182 |
|
|---|
| 2183 | case 0x1d95f262: {
|
|---|
| 2184 | int fsize96 = show_bits(&s->gb, 12) + 1;
|
|---|
| 2185 | if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
|
|---|
| 2186 | continue;
|
|---|
| 2187 |
|
|---|
| 2188 | av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
|
|---|
| 2189 | get_bits_count(&s->gb));
|
|---|
| 2190 | skip_bits(&s->gb, 12);
|
|---|
| 2191 | av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
|
|---|
| 2192 | av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
|
|---|
| 2193 |
|
|---|
| 2194 | s->core_ext_mask |= DCA_EXT_X96;
|
|---|
| 2195 | break;
|
|---|
| 2196 | }
|
|---|
| 2197 | }
|
|---|
| 2198 |
|
|---|
| 2199 | skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
|
|---|
| 2200 | }
|
|---|
| 2201 | } else {
|
|---|
| 2202 | /* no supported extensions, skip the rest of the core substream */
|
|---|
| 2203 | skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
|
|---|
| 2204 | }
|
|---|
| 2205 |
|
|---|
| 2206 | if (s->core_ext_mask & DCA_EXT_X96)
|
|---|
| 2207 | s->profile = FF_PROFILE_DTS_96_24;
|
|---|
| 2208 | else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
|
|---|
| 2209 | s->profile = FF_PROFILE_DTS_ES;
|
|---|
| 2210 |
|
|---|
| 2211 | /* check for ExSS (HD part) */
|
|---|
| 2212 | if (s->dca_buffer_size - s->frame_size > 32 &&
|
|---|
| 2213 | get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
|
|---|
| 2214 | dca_exss_parse_header(s);
|
|---|
| 2215 |
|
|---|
| 2216 | avctx->profile = s->profile;
|
|---|
| 2217 |
|
|---|
| 2218 | channels = s->prim_channels + !!s->lfe;
|
|---|
| 2219 |
|
|---|
| 2220 | /* If we have XXCH then the channel layout is managed differently */
|
|---|
| 2221 | /* note that XLL will also have another way to do things */
|
|---|
| 2222 | if (!(s->core_ext_mask & DCA_EXT_XXCH)
|
|---|
| 2223 | || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
|
|---|
| 2224 | && avctx->request_channels
|
|---|
| 2225 | < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
|
|---|
| 2226 | { /* xxx should also do MA extensions */
|
|---|
| 2227 | if (s->amode < 16) {
|
|---|
| 2228 | avctx->channel_layout = dca_core_channel_layout[s->amode];
|
|---|
| 2229 |
|
|---|
| 2230 | if (s->xch_present && (!avctx->request_channels ||
|
|---|
| 2231 | avctx->request_channels
|
|---|
| 2232 | > num_core_channels + !!s->lfe)) {
|
|---|
| 2233 | avctx->channel_layout |= AV_CH_BACK_CENTER;
|
|---|
| 2234 | if (s->lfe) {
|
|---|
| 2235 | avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
|
|---|
| 2236 | s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
|
|---|
| 2237 | } else {
|
|---|
| 2238 | s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
|
|---|
| 2239 | }
|
|---|
| 2240 | } else {
|
|---|
| 2241 | channels = num_core_channels + !!s->lfe;
|
|---|
| 2242 | s->xch_present = 0; /* disable further xch processing */
|
|---|
| 2243 | if (s->lfe) {
|
|---|
| 2244 | avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
|
|---|
| 2245 | s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
|
|---|
| 2246 | } else
|
|---|
| 2247 | s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
|
|---|
| 2248 | }
|
|---|
| 2249 |
|
|---|
| 2250 | if (channels > !!s->lfe &&
|
|---|
| 2251 | s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
|
|---|
| 2252 | return AVERROR_INVALIDDATA;
|
|---|
| 2253 |
|
|---|
| 2254 | if (avctx->request_channels == 2 && s->prim_channels > 2) {
|
|---|
| 2255 | channels = 2;
|
|---|
| 2256 | s->output = DCA_STEREO;
|
|---|
| 2257 | avctx->channel_layout = AV_CH_LAYOUT_STEREO;
|
|---|
| 2258 | }
|
|---|
| 2259 | else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
|
|---|
| 2260 | static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
|
|---|
| 2261 | s->channel_order_tab = dca_channel_order_native;
|
|---|
| 2262 | }
|
|---|
| 2263 | s->lfe_index = dca_lfe_index[s->amode];
|
|---|
| 2264 | } else {
|
|---|
| 2265 | av_log(avctx, AV_LOG_ERROR,
|
|---|
| 2266 | "Non standard configuration %d !\n", s->amode);
|
|---|
| 2267 | return AVERROR_INVALIDDATA;
|
|---|
| 2268 | }
|
|---|
| 2269 |
|
|---|
| 2270 | s->xxch_downmix = 0;
|
|---|
| 2271 | } else {
|
|---|
| 2272 | /* we only get here if an XXCH channel set can be added to the mix */
|
|---|
| 2273 | channel_mask = s->xxch_core_spkmask;
|
|---|
| 2274 |
|
|---|
| 2275 | if (avctx->request_channels > 0
|
|---|
| 2276 | && avctx->request_channels < s->prim_channels) {
|
|---|
| 2277 | channels = num_core_channels + !!s->lfe;
|
|---|
| 2278 | for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
|
|---|
| 2279 | <= avctx->request_channels; i++) {
|
|---|
| 2280 | channels += s->xxch_chset_nch[i];
|
|---|
| 2281 | channel_mask |= s->xxch_spk_masks[i];
|
|---|
| 2282 | }
|
|---|
| 2283 | } else {
|
|---|
| 2284 | channels = s->prim_channels + !!s->lfe;
|
|---|
| 2285 | for (i = 0; i < s->xxch_chset; i++) {
|
|---|
| 2286 | channel_mask |= s->xxch_spk_masks[i];
|
|---|
| 2287 | }
|
|---|
| 2288 | }
|
|---|
| 2289 |
|
|---|
| 2290 | /* Given the DTS spec'ed channel mask, generate an avcodec version */
|
|---|
| 2291 | channel_layout = 0;
|
|---|
| 2292 | for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
|
|---|
| 2293 | if (channel_mask & (1 << i)) {
|
|---|
| 2294 | channel_layout |= map_xxch_to_native[i];
|
|---|
| 2295 | }
|
|---|
| 2296 | }
|
|---|
| 2297 |
|
|---|
| 2298 | /* make sure that we have managed to get equivelant dts/avcodec channel
|
|---|
| 2299 | * masks in some sense -- unfortunately some channels could overlap */
|
|---|
| 2300 | if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
|
|---|
| 2301 | av_log(avctx, AV_LOG_DEBUG,
|
|---|
| 2302 | "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
|
|---|
| 2303 | return AVERROR_INVALIDDATA;
|
|---|
| 2304 | }
|
|---|
| 2305 |
|
|---|
| 2306 | avctx->channel_layout = channel_layout;
|
|---|
| 2307 |
|
|---|
| 2308 | if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
|
|---|
| 2309 | /* Estimate DTS --> avcodec ordering table */
|
|---|
| 2310 | for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
|
|---|
| 2311 | mask = chset >= 0 ? s->xxch_spk_masks[chset]
|
|---|
| 2312 | : s->xxch_core_spkmask;
|
|---|
| 2313 | for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
|
|---|
| 2314 | if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
|
|---|
| 2315 | lavc = map_xxch_to_native[i];
|
|---|
| 2316 | posn = av_popcount(channel_layout & (lavc - 1));
|
|---|
| 2317 | s->xxch_order_tab[j++] = posn;
|
|---|
| 2318 | }
|
|---|
| 2319 | }
|
|---|
| 2320 | }
|
|---|
| 2321 |
|
|---|
| 2322 | s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
|
|---|
| 2323 | } else { /* native ordering */
|
|---|
| 2324 | for (i = 0; i < channels; i++)
|
|---|
| 2325 | s->xxch_order_tab[i] = i;
|
|---|
| 2326 |
|
|---|
| 2327 | s->lfe_index = channels - 1;
|
|---|
| 2328 | }
|
|---|
| 2329 |
|
|---|
| 2330 | s->channel_order_tab = s->xxch_order_tab;
|
|---|
| 2331 | }
|
|---|
| 2332 |
|
|---|
| 2333 | if (avctx->channels != channels) {
|
|---|
| 2334 | if (avctx->channels)
|
|---|
| 2335 | av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
|
|---|
| 2336 | avctx->channels = channels;
|
|---|
| 2337 | }
|
|---|
| 2338 |
|
|---|
| 2339 | /* get output buffer */
|
|---|
| 2340 | s->frame.nb_samples = 256 * (s->sample_blocks / 8);
|
|---|
| 2341 | if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
|
|---|
| 2342 | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|---|
| 2343 | return ret;
|
|---|
| 2344 | }
|
|---|
| 2345 | samples_flt = (float *) s->frame.data[0];
|
|---|
| 2346 | samples_s16 = (int16_t *) s->frame.data[0];
|
|---|
| 2347 |
|
|---|
| 2348 | /* filter to get final output */
|
|---|
| 2349 | for (i = 0; i < (s->sample_blocks / 8); i++) {
|
|---|
| 2350 | dca_filter_channels(s, i);
|
|---|
| 2351 |
|
|---|
| 2352 | /* If this was marked as a DTS-ES stream we need to subtract back- */
|
|---|
| 2353 | /* channel from SL & SR to remove matrixed back-channel signal */
|
|---|
| 2354 | if ((s->source_pcm_res & 1) && s->xch_present) {
|
|---|
| 2355 | float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
|
|---|
| 2356 | float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
|
|---|
| 2357 | float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
|
|---|
| 2358 | s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
|
|---|
| 2359 | s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
|
|---|
| 2360 | }
|
|---|
| 2361 |
|
|---|
| 2362 | /* If stream contains XXCH, we might need to undo an embedded downmix */
|
|---|
| 2363 | if (s->xxch_dmix_embedded) {
|
|---|
| 2364 | /* Loop over channel sets in turn */
|
|---|
| 2365 | ch = num_core_channels;
|
|---|
| 2366 | for (chset = 0; chset < s->xxch_chset; chset++) {
|
|---|
| 2367 | endch = ch + s->xxch_chset_nch[chset];
|
|---|
| 2368 | mask = s->xxch_dmix_embedded;
|
|---|
| 2369 |
|
|---|
| 2370 | /* undo downmix */
|
|---|
| 2371 | for (j = ch; j < endch; j++) {
|
|---|
| 2372 | if (mask & (1 << j)) { /* this channel has been mixed-out */
|
|---|
| 2373 | src_chan = s->samples + s->channel_order_tab[j] * 256;
|
|---|
| 2374 | for (k = 0; k < endch; k++) {
|
|---|
| 2375 | achan = s->channel_order_tab[k];
|
|---|
| 2376 | scale = s->xxch_dmix_coeff[j][k];
|
|---|
| 2377 | if (scale != 0.0) {
|
|---|
| 2378 | dst_chan = s->samples + achan * 256;
|
|---|
| 2379 | s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
|
|---|
| 2380 | -scale, 256);
|
|---|
| 2381 | }
|
|---|
| 2382 | }
|
|---|
| 2383 | }
|
|---|
| 2384 | }
|
|---|
| 2385 |
|
|---|
| 2386 | /* if a downmix has been embedded then undo the pre-scaling */
|
|---|
| 2387 | if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
|
|---|
| 2388 | scale = s->xxch_dmix_sf[chset];
|
|---|
| 2389 |
|
|---|
| 2390 | for (j = 0; j < ch; j++) {
|
|---|
| 2391 | src_chan = s->samples + s->channel_order_tab[j] * 256;
|
|---|
| 2392 | for (k = 0; k < 256; k++)
|
|---|
| 2393 | src_chan[k] *= scale;
|
|---|
| 2394 | }
|
|---|
| 2395 |
|
|---|
| 2396 | /* LFE channel is always part of core, scale if it exists */
|
|---|
| 2397 | if (s->lfe) {
|
|---|
| 2398 | src_chan = s->samples + s->lfe_index * 256;
|
|---|
| 2399 | for (k = 0; k < 256; k++)
|
|---|
| 2400 | src_chan[k] *= scale;
|
|---|
| 2401 | }
|
|---|
| 2402 | }
|
|---|
| 2403 |
|
|---|
| 2404 | ch = endch;
|
|---|
| 2405 | }
|
|---|
| 2406 |
|
|---|
| 2407 | }
|
|---|
| 2408 |
|
|---|
| 2409 | if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
|
|---|
| 2410 | s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
|
|---|
| 2411 | channels);
|
|---|
| 2412 | samples_flt += 256 * channels;
|
|---|
| 2413 | } else {
|
|---|
| 2414 | s->fmt_conv.float_to_int16_interleave(samples_s16,
|
|---|
| 2415 | s->samples_chanptr, 256,
|
|---|
| 2416 | channels);
|
|---|
| 2417 | samples_s16 += 256 * channels;
|
|---|
| 2418 | }
|
|---|
| 2419 | }
|
|---|
| 2420 |
|
|---|
| 2421 | /* update lfe history */
|
|---|
| 2422 | lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
|
|---|
| 2423 | for (i = 0; i < 2 * s->lfe * 4; i++)
|
|---|
| 2424 | s->lfe_data[i] = s->lfe_data[i + lfe_samples];
|
|---|
| 2425 |
|
|---|
| 2426 | *got_frame_ptr = 1;
|
|---|
| 2427 | *(AVFrame *) data = s->frame;
|
|---|
| 2428 |
|
|---|
| 2429 | return buf_size;
|
|---|
| 2430 | }
|
|---|
| 2431 |
|
|---|
| 2432 |
|
|---|
| 2433 |
|
|---|
| 2434 | /**
|
|---|
| 2435 | * DCA initialization
|
|---|
| 2436 | *
|
|---|
| 2437 | * @param avctx pointer to the AVCodecContext
|
|---|
| 2438 | */
|
|---|
| 2439 |
|
|---|
| 2440 | static av_cold int dca_decode_init(AVCodecContext *avctx)
|
|---|
| 2441 | {
|
|---|
| 2442 | DCAContext *s = avctx->priv_data;
|
|---|
| 2443 | int i;
|
|---|
| 2444 |
|
|---|
| 2445 | s->avctx = avctx;
|
|---|
| 2446 | dca_init_vlcs();
|
|---|
| 2447 |
|
|---|
| 2448 | avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
|
|---|
| 2449 | ff_mdct_init(&s->imdct, 6, 1, 1.0);
|
|---|
| 2450 | ff_synth_filter_init(&s->synth);
|
|---|
| 2451 | ff_dcadsp_init(&s->dcadsp);
|
|---|
| 2452 | ff_fmt_convert_init(&s->fmt_conv, avctx);
|
|---|
| 2453 |
|
|---|
| 2454 | for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
|
|---|
| 2455 | s->samples_chanptr[i] = s->samples + i * 256;
|
|---|
| 2456 |
|
|---|
| 2457 | if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
|
|---|
| 2458 | avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
|
|---|
| 2459 | s->scale_bias = 1.0 / 32768.0;
|
|---|
| 2460 | } else {
|
|---|
| 2461 | avctx->sample_fmt = AV_SAMPLE_FMT_S16;
|
|---|
| 2462 | s->scale_bias = 1.0;
|
|---|
| 2463 | }
|
|---|
| 2464 |
|
|---|
| 2465 | /* allow downmixing to stereo */
|
|---|
| 2466 | if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
|
|---|
| 2467 | avctx->request_channels == 2) {
|
|---|
| 2468 | avctx->channels = avctx->request_channels;
|
|---|
| 2469 | }
|
|---|
| 2470 |
|
|---|
| 2471 | avcodec_get_frame_defaults(&s->frame);
|
|---|
| 2472 | avctx->coded_frame = &s->frame;
|
|---|
| 2473 |
|
|---|
| 2474 | return 0;
|
|---|
| 2475 | }
|
|---|
| 2476 |
|
|---|
| 2477 | static av_cold int dca_decode_end(AVCodecContext *avctx)
|
|---|
| 2478 | {
|
|---|
| 2479 | DCAContext *s = avctx->priv_data;
|
|---|
| 2480 | ff_mdct_end(&s->imdct);
|
|---|
| 2481 | return 0;
|
|---|
| 2482 | }
|
|---|
| 2483 |
|
|---|
| 2484 | static const AVProfile profiles[] = {
|
|---|
| 2485 | { FF_PROFILE_DTS, "DTS" },
|
|---|
| 2486 | { FF_PROFILE_DTS_ES, "DTS-ES" },
|
|---|
| 2487 | { FF_PROFILE_DTS_96_24, "DTS 96/24" },
|
|---|
| 2488 | { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
|
|---|
| 2489 | { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
|
|---|
| 2490 | { FF_PROFILE_UNKNOWN },
|
|---|
| 2491 | };
|
|---|
| 2492 |
|
|---|
| 2493 | AVCodec ff_dca_decoder = {
|
|---|
| 2494 | .name = "dca",
|
|---|
| 2495 | .type = AVMEDIA_TYPE_AUDIO,
|
|---|
| 2496 | .id = AV_CODEC_ID_DTS,
|
|---|
| 2497 | .priv_data_size = sizeof(DCAContext),
|
|---|
| 2498 | .init = dca_decode_init,
|
|---|
| 2499 | .decode = dca_decode_frame,
|
|---|
| 2500 | .close = dca_decode_end,
|
|---|
| 2501 | .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
|
|---|
| 2502 | .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
|
|---|
| 2503 | .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
|
|---|
| 2504 | AV_SAMPLE_FMT_S16,
|
|---|
| 2505 | AV_SAMPLE_FMT_NONE },
|
|---|
| 2506 | .profiles = NULL_IF_CONFIG_SMALL(profiles),
|
|---|
| 2507 | };
|
|---|