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