| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * ATRAC3+ compatible decoder | ||
| 3 | * | ||
| 4 | * Copyright (c) 2010-2013 Maxim Poliakovski | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * Sony ATRAC3+ compatible decoder. | ||
| 26 | * | ||
| 27 | * Container formats used to store its data: | ||
| 28 | * RIFF WAV (.at3) and Sony OpenMG (.oma, .aa3). | ||
| 29 | * | ||
| 30 | * Technical description of this codec can be found here: | ||
| 31 | * http://wiki.multimedia.cx/index.php?title=ATRAC3plus | ||
| 32 | * | ||
| 33 | * Kudos to Benjamin Larsson and Michael Karcher | ||
| 34 | * for their precious technical help! | ||
| 35 | */ | ||
| 36 | |||
| 37 | #include <stdint.h> | ||
| 38 | #include <string.h> | ||
| 39 | |||
| 40 | #include "libavutil/channel_layout.h" | ||
| 41 | #include "libavutil/float_dsp.h" | ||
| 42 | #include "libavutil/mem.h" | ||
| 43 | #include "libavutil/mem_internal.h" | ||
| 44 | #include "libavutil/thread.h" | ||
| 45 | #include "avcodec.h" | ||
| 46 | #include "codec_internal.h" | ||
| 47 | #include "decode.h" | ||
| 48 | #include "get_bits.h" | ||
| 49 | #include "atrac.h" | ||
| 50 | #include "atrac3plus.h" | ||
| 51 | |||
| 52 | static const uint8_t channel_map[8][8] = { | ||
| 53 | { 0, }, | ||
| 54 | { 0, 1, }, | ||
| 55 | { 0, 1, 2, }, | ||
| 56 | { 0, 1, 2, 3, }, | ||
| 57 | { 0, }, | ||
| 58 | { 0, 1, 2, 4, 5, 3, }, | ||
| 59 | { 0, 1, 2, 4, 5, 6, 3, }, | ||
| 60 | { 0, 1, 2, 4, 5, 6, 7, 3, }, | ||
| 61 | }; | ||
| 62 | |||
| 63 | typedef struct ATRAC3PContext { | ||
| 64 | GetBitContext gb; | ||
| 65 | AVFloatDSPContext *fdsp; | ||
| 66 | |||
| 67 | DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES]; ///< quantized MDCT spectrum | ||
| 68 | DECLARE_ALIGNED(32, float, mdct_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the IMDCT | ||
| 69 | DECLARE_ALIGNED(32, float, time_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the gain compensation | ||
| 70 | DECLARE_ALIGNED(32, float, outp_buf)[2][ATRAC3P_FRAME_SAMPLES]; | ||
| 71 | |||
| 72 | AtracGCContext gainc_ctx; ///< gain compensation context | ||
| 73 | AVTXContext *mdct_ctx; | ||
| 74 | av_tx_fn mdct_fn; | ||
| 75 | AVTXContext *ipqf_dct_ctx; ///< IDCT context used by IPQF | ||
| 76 | av_tx_fn ipqf_dct_fn; | ||
| 77 | |||
| 78 | Atrac3pChanUnitCtx *ch_units; ///< global channel units | ||
| 79 | |||
| 80 | int num_channel_blocks; ///< number of channel blocks | ||
| 81 | uint8_t channel_blocks[5]; ///< channel configuration descriptor | ||
| 82 | const uint8_t *channel_map; ///< channel layout map | ||
| 83 | } ATRAC3PContext; | ||
| 84 | |||
| 85 | 8 | static av_cold int atrac3p_decode_close(AVCodecContext *avctx) | |
| 86 | { | ||
| 87 | 8 | ATRAC3PContext *ctx = avctx->priv_data; | |
| 88 | |||
| 89 | 8 | av_freep(&ctx->ch_units); | |
| 90 | 8 | av_freep(&ctx->fdsp); | |
| 91 | |||
| 92 | 8 | av_tx_uninit(&ctx->mdct_ctx); | |
| 93 | 8 | av_tx_uninit(&ctx->ipqf_dct_ctx); | |
| 94 | |||
| 95 | 8 | return 0; | |
| 96 | } | ||
| 97 | |||
| 98 | 8 | static av_cold int set_channel_params(ATRAC3PContext *ctx, | |
| 99 | AVCodecContext *avctx) | ||
| 100 | { | ||
| 101 | 8 | int channels = avctx->ch_layout.nb_channels; | |
| 102 | 8 | memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks)); | |
| 103 | |||
| 104 | 8 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 105 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | switch (channels) { |
| 106 | ✗ | case 1: | |
| 107 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 108 | ✗ | ctx->num_channel_blocks = 1; | |
| 109 | ✗ | ctx->channel_blocks[0] = CH_UNIT_MONO; | |
| 110 | ✗ | break; | |
| 111 | 8 | case 2: | |
| 112 | 8 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
| 113 | 8 | ctx->num_channel_blocks = 1; | |
| 114 | 8 | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 115 | 8 | break; | |
| 116 | ✗ | case 3: | |
| 117 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND; | |
| 118 | ✗ | ctx->num_channel_blocks = 2; | |
| 119 | ✗ | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 120 | ✗ | ctx->channel_blocks[1] = CH_UNIT_MONO; | |
| 121 | ✗ | break; | |
| 122 | ✗ | case 4: | |
| 123 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT0; | |
| 124 | ✗ | ctx->num_channel_blocks = 3; | |
| 125 | ✗ | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 126 | ✗ | ctx->channel_blocks[1] = CH_UNIT_MONO; | |
| 127 | ✗ | ctx->channel_blocks[2] = CH_UNIT_MONO; | |
| 128 | ✗ | break; | |
| 129 | ✗ | case 6: | |
| 130 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK; | |
| 131 | ✗ | ctx->num_channel_blocks = 4; | |
| 132 | ✗ | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 133 | ✗ | ctx->channel_blocks[1] = CH_UNIT_MONO; | |
| 134 | ✗ | ctx->channel_blocks[2] = CH_UNIT_STEREO; | |
| 135 | ✗ | ctx->channel_blocks[3] = CH_UNIT_MONO; | |
| 136 | ✗ | break; | |
| 137 | ✗ | case 7: | |
| 138 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1_BACK; | |
| 139 | ✗ | ctx->num_channel_blocks = 5; | |
| 140 | ✗ | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 141 | ✗ | ctx->channel_blocks[1] = CH_UNIT_MONO; | |
| 142 | ✗ | ctx->channel_blocks[2] = CH_UNIT_STEREO; | |
| 143 | ✗ | ctx->channel_blocks[3] = CH_UNIT_MONO; | |
| 144 | ✗ | ctx->channel_blocks[4] = CH_UNIT_MONO; | |
| 145 | ✗ | break; | |
| 146 | ✗ | case 8: | |
| 147 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1; | |
| 148 | ✗ | ctx->num_channel_blocks = 5; | |
| 149 | ✗ | ctx->channel_blocks[0] = CH_UNIT_STEREO; | |
| 150 | ✗ | ctx->channel_blocks[1] = CH_UNIT_MONO; | |
| 151 | ✗ | ctx->channel_blocks[2] = CH_UNIT_STEREO; | |
| 152 | ✗ | ctx->channel_blocks[3] = CH_UNIT_STEREO; | |
| 153 | ✗ | ctx->channel_blocks[4] = CH_UNIT_MONO; | |
| 154 | ✗ | break; | |
| 155 | ✗ | default: | |
| 156 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 157 | "Unsupported channel count: %d!\n", channels); | ||
| 158 | ✗ | return AVERROR_INVALIDDATA; | |
| 159 | } | ||
| 160 | |||
| 161 | 8 | ctx->channel_map = channel_map[channels - 1]; | |
| 162 | |||
| 163 | 8 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | 5 | static av_cold void atrac3p_init_static(void) | |
| 167 | { | ||
| 168 | 5 | ff_atrac3p_init_vlcs(); | |
| 169 | 5 | ff_atrac3p_init_dsp_static(); | |
| 170 | 5 | } | |
| 171 | |||
| 172 | 8 | static av_cold int atrac3p_decode_init(AVCodecContext *avctx) | |
| 173 | { | ||
| 174 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 175 | 8 | ATRAC3PContext *ctx = avctx->priv_data; | |
| 176 | float scale; | ||
| 177 | int i, ch, ret; | ||
| 178 | |||
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (!avctx->block_align) { |
| 180 | ✗ | av_log(avctx, AV_LOG_ERROR, "block_align is not set\n"); | |
| 181 | ✗ | return AVERROR(EINVAL); | |
| 182 | } | ||
| 183 | |||
| 184 | /* initialize IPQF */ | ||
| 185 | 8 | scale = 32.0 / 32768.0; | |
| 186 | 8 | ret = av_tx_init(&ctx->ipqf_dct_ctx, &ctx->ipqf_dct_fn, AV_TX_FLOAT_MDCT, | |
| 187 | 1, 16, &scale, 0); | ||
| 188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) |
| 189 | ✗ | return ret; | |
| 190 | |||
| 191 | 8 | scale = -1.0f; | |
| 192 | 8 | ret = av_tx_init(&ctx->mdct_ctx, &ctx->mdct_fn, AV_TX_FLOAT_MDCT, | |
| 193 | 1, 128, &scale, AV_TX_FULL_IMDCT); | ||
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) |
| 195 | ✗ | return ret; | |
| 196 | |||
| 197 | 8 | ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2); | |
| 198 | |||
| 199 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if ((ret = set_channel_params(ctx, avctx)) < 0) |
| 200 | ✗ | return ret; | |
| 201 | |||
| 202 | 8 | ctx->ch_units = av_calloc(ctx->num_channel_blocks, sizeof(*ctx->ch_units)); | |
| 203 | 8 | ctx->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
| 204 | |||
| 205 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (!ctx->ch_units || !ctx->fdsp) { |
| 206 | ✗ | return AVERROR(ENOMEM); | |
| 207 | } | ||
| 208 | |||
| 209 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | for (i = 0; i < ctx->num_channel_blocks; i++) { |
| 210 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | for (ch = 0; ch < 2; ch++) { |
| 211 | 16 | ctx->ch_units[i].channels[ch].ch_num = ch; | |
| 212 | 16 | ctx->ch_units[i].channels[ch].wnd_shape = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0]; | |
| 213 | 16 | ctx->ch_units[i].channels[ch].wnd_shape_prev = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0]; | |
| 214 | 16 | ctx->ch_units[i].channels[ch].gain_data = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0]; | |
| 215 | 16 | ctx->ch_units[i].channels[ch].gain_data_prev = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0]; | |
| 216 | 16 | ctx->ch_units[i].channels[ch].tones_info = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0]; | |
| 217 | 16 | ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0]; | |
| 218 | } | ||
| 219 | |||
| 220 | 8 | ctx->ch_units[i].waves_info = &ctx->ch_units[i].wave_synth_hist[0]; | |
| 221 | 8 | ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1]; | |
| 222 | } | ||
| 223 | |||
| 224 | 8 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
| 225 | |||
| 226 | 8 | ff_thread_once(&init_static_once, atrac3p_init_static); | |
| 227 | |||
| 228 | 8 | return 0; | |
| 229 | } | ||
| 230 | |||
| 231 | 813 | static void decode_residual_spectrum(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit, | |
| 232 | float out[2][ATRAC3P_FRAME_SAMPLES], | ||
| 233 | int num_channels, | ||
| 234 | AVCodecContext *avctx) | ||
| 235 | { | ||
| 236 | int i, sb, ch, qu, nspeclines, RNG_index; | ||
| 237 | float *dst, q; | ||
| 238 | int16_t *src; | ||
| 239 | /* calculate RNG table index for each subband */ | ||
| 240 | 813 | int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 }; | |
| 241 | |||
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 813 times.
|
813 | if (ch_unit->mute_flag) { |
| 243 | ✗ | for (ch = 0; ch < num_channels; ch++) | |
| 244 | ✗ | memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch])); | |
| 245 | ✗ | return; | |
| 246 | } | ||
| 247 | |||
| 248 |
2/2✓ Branch 0 taken 21980 times.
✓ Branch 1 taken 813 times.
|
22793 | for (qu = 0, RNG_index = 0; qu < ch_unit->used_quant_units; qu++) |
| 249 | 21980 | RNG_index += ch_unit->channels[0].qu_sf_idx[qu] + | |
| 250 | 21980 | ch_unit->channels[1].qu_sf_idx[qu]; | |
| 251 | |||
| 252 |
2/2✓ Branch 0 taken 9564 times.
✓ Branch 1 taken 813 times.
|
10377 | for (sb = 0; sb < ch_unit->num_coded_subbands; sb++, RNG_index += 128) |
| 253 | 9564 | sb_RNG_index[sb] = RNG_index & 0x3FC; | |
| 254 | |||
| 255 | /* inverse quant and power compensation */ | ||
| 256 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch = 0; ch < num_channels; ch++) { |
| 257 | /* clear channel's residual spectrum */ | ||
| 258 | 1626 | memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch])); | |
| 259 | |||
| 260 |
2/2✓ Branch 0 taken 43960 times.
✓ Branch 1 taken 1626 times.
|
45586 | for (qu = 0; qu < ch_unit->used_quant_units; qu++) { |
| 261 | 43960 | src = &ch_unit->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]]; | |
| 262 | 43960 | dst = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]]; | |
| 263 | 43960 | nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] - | |
| 264 | 43960 | ff_atrac3p_qu_to_spec_pos[qu]; | |
| 265 | |||
| 266 |
1/2✓ Branch 0 taken 43960 times.
✗ Branch 1 not taken.
|
43960 | if (ch_unit->channels[ch].qu_wordlen[qu] > 0) { |
| 267 | 43960 | q = ff_atrac3p_sf_tab[ch_unit->channels[ch].qu_sf_idx[qu]] * | |
| 268 | 43960 | ff_atrac3p_mant_tab[ch_unit->channels[ch].qu_wordlen[qu]]; | |
| 269 |
2/2✓ Branch 0 taken 2448384 times.
✓ Branch 1 taken 43960 times.
|
2492344 | for (i = 0; i < nspeclines; i++) |
| 270 | 2448384 | dst[i] = src[i] * q; | |
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 19128 times.
✓ Branch 1 taken 1626 times.
|
20754 | for (sb = 0; sb < ch_unit->num_coded_subbands; sb++) |
| 275 | 19128 | ff_atrac3p_power_compensation(ch_unit, ctx->fdsp, ch, &out[ch][0], | |
| 276 | sb_RNG_index[sb], sb); | ||
| 277 | } | ||
| 278 | |||
| 279 |
1/2✓ Branch 0 taken 813 times.
✗ Branch 1 not taken.
|
813 | if (ch_unit->unit_type == CH_UNIT_STEREO) { |
| 280 |
2/2✓ Branch 0 taken 9564 times.
✓ Branch 1 taken 813 times.
|
10377 | for (sb = 0; sb < ch_unit->num_coded_subbands; sb++) { |
| 281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9564 times.
|
9564 | if (ch_unit->swap_channels[sb]) { |
| 282 | ✗ | for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++) | |
| 283 | ✗ | FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i], | |
| 284 | out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]); | ||
| 285 | } | ||
| 286 | |||
| 287 | /* flip coefficients' sign if requested */ | ||
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9564 times.
|
9564 | if (ch_unit->negate_coeffs[sb]) |
| 289 | ✗ | for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++) | |
| 290 | ✗ | out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]); | |
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | 813 | static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit, | |
| 296 | int num_channels, AVCodecContext *avctx) | ||
| 297 | { | ||
| 298 | int ch, sb; | ||
| 299 | |||
| 300 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch = 0; ch < num_channels; ch++) { |
| 301 |
2/2✓ Branch 0 taken 21672 times.
✓ Branch 1 taken 1626 times.
|
23298 | for (sb = 0; sb < ch_unit->num_subbands; sb++) { |
| 302 | /* inverse transform and windowing */ | ||
| 303 | 21672 | ff_atrac3p_imdct(ctx->fdsp, ctx->mdct_ctx, ctx->mdct_fn, | |
| 304 | 21672 | &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES], | |
| 305 | 21672 | &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES], | |
| 306 | 21672 | (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) + | |
| 307 | 21672 | ch_unit->channels[ch].wnd_shape[sb], sb); | |
| 308 | |||
| 309 | /* gain compensation and overlapping */ | ||
| 310 | 21672 | ff_atrac_gain_compensation(&ctx->gainc_ctx, | |
| 311 | 21672 | &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES], | |
| 312 | 21672 | &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES], | |
| 313 | 21672 | &ch_unit->channels[ch].gain_data_prev[sb], | |
| 314 | 21672 | &ch_unit->channels[ch].gain_data[sb], | |
| 315 | ATRAC3P_SUBBAND_SAMPLES, | ||
| 316 | 21672 | &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]); | |
| 317 | } | ||
| 318 | |||
| 319 | /* zero unused subbands in both output and overlapping buffers */ | ||
| 320 | 1626 | memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES], | |
| 321 | 0, | ||
| 322 | 1626 | (ATRAC3P_SUBBANDS - ch_unit->num_subbands) * | |
| 323 | ATRAC3P_SUBBAND_SAMPLES * | ||
| 324 | sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES])); | ||
| 325 | 1626 | memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES], | |
| 326 | 0, | ||
| 327 | 1626 | (ATRAC3P_SUBBANDS - ch_unit->num_subbands) * | |
| 328 | ATRAC3P_SUBBAND_SAMPLES * | ||
| 329 | sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES])); | ||
| 330 | |||
| 331 | /* resynthesize and add tonal signal */ | ||
| 332 |
1/2✓ Branch 0 taken 1626 times.
✗ Branch 1 not taken.
|
1626 | if (ch_unit->waves_info->tones_present || |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1626 times.
|
1626 | ch_unit->waves_info_prev->tones_present) { |
| 334 | ✗ | for (sb = 0; sb < ch_unit->num_subbands; sb++) | |
| 335 | ✗ | if (ch_unit->channels[ch].tones_info[sb].num_wavs || | |
| 336 | ✗ | ch_unit->channels[ch].tones_info_prev[sb].num_wavs) { | |
| 337 | ✗ | ff_atrac3p_generate_tones(ch_unit, ctx->fdsp, ch, sb, | |
| 338 | ✗ | &ctx->time_buf[ch][sb * 128]); | |
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | /* subband synthesis and acoustic signal output */ | ||
| 343 | 1626 | ff_atrac3p_ipqf(ctx->ipqf_dct_ctx, ctx->ipqf_dct_fn, | |
| 344 | 1626 | &ch_unit->ipqf_ctx[ch], &ctx->time_buf[ch][0], | |
| 345 | &ctx->outp_buf[ch][0]); | ||
| 346 | } | ||
| 347 | |||
| 348 | /* swap window shape and gain control buffers. */ | ||
| 349 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch = 0; ch < num_channels; ch++) { |
| 350 | 1626 | FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape, | |
| 351 | ch_unit->channels[ch].wnd_shape_prev); | ||
| 352 | 1626 | FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data, | |
| 353 | ch_unit->channels[ch].gain_data_prev); | ||
| 354 | 1626 | FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info, | |
| 355 | ch_unit->channels[ch].tones_info_prev); | ||
| 356 | } | ||
| 357 | |||
| 358 | 813 | FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev); | |
| 359 | 813 | } | |
| 360 | |||
| 361 | 813 | static int atrac3p_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 362 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 363 | { | ||
| 364 | 813 | ATRAC3PContext *ctx = avctx->priv_data; | |
| 365 | 813 | int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process; | |
| 366 | 813 | float **samples_p = (float **)frame->extended_data; | |
| 367 | |||
| 368 | 813 | frame->nb_samples = ATRAC3P_FRAME_SAMPLES; | |
| 369 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 370 | ✗ | return ret; | |
| 371 | |||
| 372 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0) |
| 373 | ✗ | return ret; | |
| 374 | |||
| 375 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if (get_bits1(&ctx->gb)) { |
| 376 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n"); | |
| 377 | ✗ | return AVERROR_INVALIDDATA; | |
| 378 | } | ||
| 379 | |||
| 380 |
2/2✓ Branch 1 taken 1625 times.
✓ Branch 2 taken 1 times.
|
1626 | while (get_bits_left(&ctx->gb) >= 2 && |
| 381 |
2/2✓ Branch 1 taken 813 times.
✓ Branch 2 taken 812 times.
|
1625 | (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) { |
| 382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 813 times.
|
813 | if (ch_unit_id == CH_UNIT_EXTENSION) { |
| 383 | ✗ | avpriv_report_missing_feature(avctx, "Channel unit extension"); | |
| 384 | ✗ | return AVERROR_PATCHWELCOME; | |
| 385 | } | ||
| 386 |
1/2✓ Branch 0 taken 813 times.
✗ Branch 1 not taken.
|
813 | if (ch_block >= ctx->num_channel_blocks || |
| 387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 813 times.
|
813 | ctx->channel_blocks[ch_block] != ch_unit_id) { |
| 388 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 389 | "Frame data doesn't match channel configuration!\n"); | ||
| 390 | ✗ | return AVERROR_INVALIDDATA; | |
| 391 | } | ||
| 392 | |||
| 393 | 813 | ctx->ch_units[ch_block].unit_type = ch_unit_id; | |
| 394 | 813 | channels_to_process = ch_unit_id + 1; | |
| 395 | |||
| 396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 813 times.
|
813 | if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb, |
| 397 | 813 | &ctx->ch_units[ch_block], | |
| 398 | channels_to_process, | ||
| 399 | avctx)) < 0) | ||
| 400 | ✗ | return ret; | |
| 401 | |||
| 402 | 813 | decode_residual_spectrum(ctx, &ctx->ch_units[ch_block], ctx->samples, | |
| 403 | channels_to_process, avctx); | ||
| 404 | 813 | reconstruct_frame(ctx, &ctx->ch_units[ch_block], | |
| 405 | channels_to_process, avctx); | ||
| 406 | |||
| 407 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (i = 0; i < channels_to_process; i++) |
| 408 | 1626 | memcpy(samples_p[ctx->channel_map[out_ch_index + i]], ctx->outp_buf[i], | |
| 409 | ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p)); | ||
| 410 | |||
| 411 | 813 | ch_block++; | |
| 412 | 813 | out_ch_index += channels_to_process; | |
| 413 | } | ||
| 414 | |||
| 415 | 813 | *got_frame_ptr = 1; | |
| 416 | |||
| 417 |
1/2✓ Branch 0 taken 813 times.
✗ Branch 1 not taken.
|
813 | return avctx->codec_id == AV_CODEC_ID_ATRAC3P ? FFMIN(avctx->block_align, avpkt->size) : avpkt->size; |
| 418 | } | ||
| 419 | |||
| 420 | const FFCodec ff_atrac3p_decoder = { | ||
| 421 | .p.name = "atrac3plus", | ||
| 422 | CODEC_LONG_NAME("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"), | ||
| 423 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 424 | .p.id = AV_CODEC_ID_ATRAC3P, | ||
| 425 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 426 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 427 | .priv_data_size = sizeof(ATRAC3PContext), | ||
| 428 | .init = atrac3p_decode_init, | ||
| 429 | .close = atrac3p_decode_close, | ||
| 430 | FF_CODEC_DECODE_CB(atrac3p_decode_frame), | ||
| 431 | }; | ||
| 432 | |||
| 433 | const FFCodec ff_atrac3pal_decoder = { | ||
| 434 | .p.name = "atrac3plusal", | ||
| 435 | CODEC_LONG_NAME("ATRAC3+ AL (Adaptive TRansform Acoustic Coding 3+ Advanced Lossless)"), | ||
| 436 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 437 | .p.id = AV_CODEC_ID_ATRAC3PAL, | ||
| 438 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 439 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 440 | .priv_data_size = sizeof(ATRAC3PContext), | ||
| 441 | .init = atrac3p_decode_init, | ||
| 442 | .close = atrac3p_decode_close, | ||
| 443 | FF_CODEC_DECODE_CB(atrac3p_decode_frame), | ||
| 444 | }; | ||
| 445 |