Patch by Thilo Borgmann, thilo D borgmann A googlemail
Originally committed as revision 20517 to svn://svn.ffmpeg.org/ffmpeg/trunk
... | ... |
@@ -569,6 +569,7 @@ following image formats are supported: |
569 | 569 |
@item MP2 (MPEG audio layer 2) @tab IX @tab IX |
570 | 570 |
@item MP3 (MPEG audio layer 3) @tab E @tab IX |
571 | 571 |
@tab encoding supported through external library LAME, ADU MP3 and MP3onMP4 also supported |
572 |
+@item MPEG-4 Audio Lossless Coding (ALS) @tab @tab X |
|
572 | 573 |
@item Musepack SV7 @tab @tab X |
573 | 574 |
@item Musepack SV8 @tab @tab X |
574 | 575 |
@item Nellymoser Asao @tab X @tab X |
... | ... |
@@ -51,6 +51,7 @@ OBJS-$(CONFIG_AC3_DECODER) += ac3dec.o ac3tab.o \ |
51 | 51 |
OBJS-$(CONFIG_AC3_ENCODER) += ac3enc.o ac3tab.o ac3.o |
52 | 52 |
OBJS-$(CONFIG_ALAC_DECODER) += alac.o |
53 | 53 |
OBJS-$(CONFIG_ALAC_ENCODER) += alacenc.o |
54 |
+OBJS-$(CONFIG_ALS_DECODER) += alsdec.o |
|
54 | 55 |
OBJS-$(CONFIG_AMV_DECODER) += sp5xdec.o mjpegdec.o mjpeg.o |
55 | 56 |
OBJS-$(CONFIG_APE_DECODER) += apedec.o |
56 | 57 |
OBJS-$(CONFIG_ASV1_DECODER) += asv1.o mpeg12data.o |
... | ... |
@@ -200,6 +200,7 @@ void avcodec_register_all(void) |
200 | 200 |
REGISTER_ENCDEC (AAC, aac); |
201 | 201 |
REGISTER_ENCDEC (AC3, ac3); |
202 | 202 |
REGISTER_ENCDEC (ALAC, alac); |
203 |
+ REGISTER_DECODER (ALS, als); |
|
203 | 204 |
REGISTER_DECODER (APE, ape); |
204 | 205 |
REGISTER_DECODER (ATRAC1, atrac1); |
205 | 206 |
REGISTER_DECODER (ATRAC3, atrac3); |
206 | 207 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,95 @@ |
0 |
+/* |
|
1 |
+ * ALS header file for common data |
|
2 |
+ * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#ifndef AVCODEC_ALS_DATA_H |
|
22 |
+#define AVCODEC_ALS_DATA_H |
|
23 |
+ |
|
24 |
+/** |
|
25 |
+ * @file libavcodec/als_data.h |
|
26 |
+ * MPEG-4 ALS common data tables |
|
27 |
+ * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com> |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+ |
|
31 |
+#include <stdint.h> |
|
32 |
+ |
|
33 |
+/** Rice parameters and corresponding index offsets for decoding the |
|
34 |
+ * indices of scaled PARCOR values. The table choosen is set globally |
|
35 |
+ * by the encoder and stored in ALSSpecificConfig. |
|
36 |
+ */ |
|
37 |
+static const int8_t parcor_rice_table[3][20][2] = { |
|
38 |
+ { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4}, |
|
39 |
+ { 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3}, |
|
40 |
+ { -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2}, |
|
41 |
+ { 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} }, |
|
42 |
+ { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4}, |
|
43 |
+ { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4}, |
|
44 |
+ {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4}, |
|
45 |
+ { 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} }, |
|
46 |
+ { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4}, |
|
47 |
+ { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3}, |
|
48 |
+ {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3}, |
|
49 |
+ { 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} } |
|
50 |
+}; |
|
51 |
+ |
|
52 |
+ |
|
53 |
+/** Scaled PARCOR values used for the first two PARCOR coefficients. |
|
54 |
+ * To be indexed by the Rice coded indices. |
|
55 |
+ * Generated by: parcor_scaled_values[i] = 32 + ((i * (i+1)) << 7) - (1 << 20) |
|
56 |
+ * Actual values are divided by 32 in order to be stored in 16 bits. |
|
57 |
+ */ |
|
58 |
+static const int16_t parcor_scaled_values[] = { |
|
59 |
+ -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32, |
|
60 |
+ -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32, |
|
61 |
+ -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32, |
|
62 |
+ -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32, |
|
63 |
+ -1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32, |
|
64 |
+ -994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32, |
|
65 |
+ -971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32, |
|
66 |
+ -944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32, |
|
67 |
+ -913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32, |
|
68 |
+ -878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32, |
|
69 |
+ -838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32, |
|
70 |
+ -795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32, |
|
71 |
+ -747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32, |
|
72 |
+ -695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32, |
|
73 |
+ -639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32, |
|
74 |
+ -580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32, |
|
75 |
+ -516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32, |
|
76 |
+ -447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32, |
|
77 |
+ -375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32, |
|
78 |
+ -299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32, |
|
79 |
+ -219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32, |
|
80 |
+ -134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32, |
|
81 |
+ -46048 / 32, -23264 / 32, -224 / 32, 23072 / 32, |
|
82 |
+ 46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32, |
|
83 |
+ 143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32, |
|
84 |
+ 244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32, |
|
85 |
+ 349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32, |
|
86 |
+ 458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32, |
|
87 |
+ 571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32, |
|
88 |
+ 688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32, |
|
89 |
+ 810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32, |
|
90 |
+ 935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32 |
|
91 |
+}; |
|
92 |
+ |
|
93 |
+ |
|
94 |
+#endif /* AVCODEC_ALS_DATA_H */ |
0 | 95 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,997 @@ |
0 |
+/* |
|
1 |
+ * MPEG-4 ALS decoder |
|
2 |
+ * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file libavcodec/alsdec.c |
|
23 |
+ * MPEG-4 ALS decoder |
|
24 |
+ * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com> |
|
25 |
+ */ |
|
26 |
+ |
|
27 |
+ |
|
28 |
+//#define DEBUG |
|
29 |
+ |
|
30 |
+ |
|
31 |
+#include "avcodec.h" |
|
32 |
+#include "get_bits.h" |
|
33 |
+#include "unary.h" |
|
34 |
+#include "mpeg4audio.h" |
|
35 |
+#include "bytestream.h" |
|
36 |
+ |
|
37 |
+#include "als_data.h" |
|
38 |
+ |
|
39 |
+enum RA_Flag { |
|
40 |
+ RA_FLAG_NONE, |
|
41 |
+ RA_FLAG_FRAMES, |
|
42 |
+ RA_FLAG_HEADER |
|
43 |
+}; |
|
44 |
+ |
|
45 |
+ |
|
46 |
+typedef struct { |
|
47 |
+ uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown |
|
48 |
+ int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit |
|
49 |
+ int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer |
|
50 |
+ int frame_length; ///< frame length for each frame (last frame may differ) |
|
51 |
+ int ra_distance; ///< distance between RA frames (in frames, 0...255) |
|
52 |
+ enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored |
|
53 |
+ int adapt_order; ///< adaptive order: 1 = on, 0 = off |
|
54 |
+ int coef_table; ///< table index of Rice code parameters |
|
55 |
+ int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off |
|
56 |
+ int max_order; ///< maximum prediction order (0..1023) |
|
57 |
+ int block_switching; ///< number of block switching levels |
|
58 |
+ int bgmc; ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only) |
|
59 |
+ int sb_part; ///< sub-block partition |
|
60 |
+ int joint_stereo; ///< joint stereo: 1 = on, 0 = off |
|
61 |
+ int mc_coding; ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off |
|
62 |
+ int chan_config; ///< indicates that a chan_config_info field is present |
|
63 |
+ int chan_sort; ///< channel rearrangement: 1 = on, 0 = off |
|
64 |
+ int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off |
|
65 |
+ int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented. |
|
66 |
+ int *chan_pos; ///< original channel positions |
|
67 |
+ uint32_t header_size; ///< header size of original audio file in bytes, provided for debugging |
|
68 |
+ uint32_t trailer_size; ///< trailer size of original audio file in bytes, provided for debugging |
|
69 |
+} ALSSpecificConfig; |
|
70 |
+ |
|
71 |
+ |
|
72 |
+typedef struct { |
|
73 |
+ AVCodecContext *avctx; |
|
74 |
+ ALSSpecificConfig sconf; |
|
75 |
+ GetBitContext gb; |
|
76 |
+ unsigned int cur_frame_length; ///< length of the current frame to decode |
|
77 |
+ unsigned int frame_id; ///< the frame ID / number of the current frame |
|
78 |
+ unsigned int js_switch; ///< if true, joint-stereo decoding is enforced |
|
79 |
+ unsigned int num_blocks; ///< number of blocks used in the current frame |
|
80 |
+ int32_t *quant_cof; ///< quantized parcor coefficients |
|
81 |
+ int32_t *lpc_cof; ///< coefficients of the direct form prediction filter |
|
82 |
+ int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block |
|
83 |
+ int32_t **raw_samples; ///< decoded raw samples for each channel |
|
84 |
+ int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples |
|
85 |
+} ALSDecContext; |
|
86 |
+ |
|
87 |
+ |
|
88 |
+static av_cold void dprint_specific_config(ALSDecContext *ctx) |
|
89 |
+{ |
|
90 |
+#ifdef DEBUG |
|
91 |
+ AVCodecContext *avctx = ctx->avctx; |
|
92 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
93 |
+ |
|
94 |
+ dprintf(avctx, "resolution = %i\n", sconf->resolution); |
|
95 |
+ dprintf(avctx, "floating = %i\n", sconf->floating); |
|
96 |
+ dprintf(avctx, "frame_length = %i\n", sconf->frame_length); |
|
97 |
+ dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance); |
|
98 |
+ dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag); |
|
99 |
+ dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order); |
|
100 |
+ dprintf(avctx, "coef_table = %i\n", sconf->coef_table); |
|
101 |
+ dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction); |
|
102 |
+ dprintf(avctx, "max_order = %i\n", sconf->max_order); |
|
103 |
+ dprintf(avctx, "block_switching = %i\n", sconf->block_switching); |
|
104 |
+ dprintf(avctx, "bgmc = %i\n", sconf->bgmc); |
|
105 |
+ dprintf(avctx, "sb_part = %i\n", sconf->sb_part); |
|
106 |
+ dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo); |
|
107 |
+ dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding); |
|
108 |
+ dprintf(avctx, "chan_config = %i\n", sconf->chan_config); |
|
109 |
+ dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort); |
|
110 |
+ dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms); |
|
111 |
+ dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info); |
|
112 |
+ dprintf(avctx, "header_size = %i\n", sconf->header_size); |
|
113 |
+ dprintf(avctx, "trailer_size = %i\n", sconf->trailer_size); |
|
114 |
+#endif |
|
115 |
+} |
|
116 |
+ |
|
117 |
+ |
|
118 |
+/** Reads an ALSSpecificConfig from a buffer into the output struct. |
|
119 |
+ */ |
|
120 |
+static av_cold int read_specific_config(ALSDecContext *ctx) |
|
121 |
+{ |
|
122 |
+ GetBitContext gb; |
|
123 |
+ uint64_t ht_size; |
|
124 |
+ int i, config_offset, crc_enabled; |
|
125 |
+ MPEG4AudioConfig m4ac; |
|
126 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
127 |
+ AVCodecContext *avctx = ctx->avctx; |
|
128 |
+ uint32_t als_id; |
|
129 |
+ |
|
130 |
+ init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8); |
|
131 |
+ |
|
132 |
+ config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata, |
|
133 |
+ avctx->extradata_size); |
|
134 |
+ |
|
135 |
+ if (config_offset < 0) |
|
136 |
+ return -1; |
|
137 |
+ |
|
138 |
+ skip_bits_long(&gb, config_offset); |
|
139 |
+ |
|
140 |
+ if (get_bits_left(&gb) < (30 << 3)) |
|
141 |
+ return -1; |
|
142 |
+ |
|
143 |
+ // read the fixed items |
|
144 |
+ als_id = get_bits_long(&gb, 32); |
|
145 |
+ avctx->sample_rate = m4ac.sample_rate; |
|
146 |
+ skip_bits_long(&gb, 32); // sample rate already known |
|
147 |
+ sconf->samples = get_bits_long(&gb, 32); |
|
148 |
+ avctx->channels = m4ac.channels; |
|
149 |
+ skip_bits(&gb, 16); // number of channels already knwon |
|
150 |
+ skip_bits(&gb, 3); // skip file_type |
|
151 |
+ sconf->resolution = get_bits(&gb, 3); |
|
152 |
+ sconf->floating = get_bits1(&gb); |
|
153 |
+ skip_bits1(&gb); // skip msb_first |
|
154 |
+ sconf->frame_length = get_bits(&gb, 16) + 1; |
|
155 |
+ sconf->ra_distance = get_bits(&gb, 8); |
|
156 |
+ sconf->ra_flag = get_bits(&gb, 2); |
|
157 |
+ sconf->adapt_order = get_bits1(&gb); |
|
158 |
+ sconf->coef_table = get_bits(&gb, 2); |
|
159 |
+ sconf->long_term_prediction = get_bits1(&gb); |
|
160 |
+ sconf->max_order = get_bits(&gb, 10); |
|
161 |
+ sconf->block_switching = get_bits(&gb, 2); |
|
162 |
+ sconf->bgmc = get_bits1(&gb); |
|
163 |
+ sconf->sb_part = get_bits1(&gb); |
|
164 |
+ sconf->joint_stereo = get_bits1(&gb); |
|
165 |
+ sconf->mc_coding = get_bits1(&gb); |
|
166 |
+ sconf->chan_config = get_bits1(&gb); |
|
167 |
+ sconf->chan_sort = get_bits1(&gb); |
|
168 |
+ crc_enabled = get_bits1(&gb); |
|
169 |
+ sconf->rlslms = get_bits1(&gb); |
|
170 |
+ skip_bits(&gb, 5); // skip 5 reserved bits |
|
171 |
+ skip_bits1(&gb); // skip aux_data_enabled |
|
172 |
+ |
|
173 |
+ |
|
174 |
+ // check for ALSSpecificConfig struct |
|
175 |
+ if (als_id != MKBETAG('A','L','S','\0')) |
|
176 |
+ return -1; |
|
177 |
+ |
|
178 |
+ ctx->cur_frame_length = sconf->frame_length; |
|
179 |
+ |
|
180 |
+ // allocate quantized parcor coefficient buffer |
|
181 |
+ if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) || |
|
182 |
+ !(ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * sconf->max_order))) { |
|
183 |
+ av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); |
|
184 |
+ return AVERROR(ENOMEM); |
|
185 |
+ } |
|
186 |
+ |
|
187 |
+ // read channel config |
|
188 |
+ if (sconf->chan_config) |
|
189 |
+ sconf->chan_config_info = get_bits(&gb, 16); |
|
190 |
+ // TODO: use this to set avctx->channel_layout |
|
191 |
+ |
|
192 |
+ |
|
193 |
+ // read channel sorting |
|
194 |
+ if (sconf->chan_sort && avctx->channels > 1) { |
|
195 |
+ int chan_pos_bits = av_ceil_log2(avctx->channels); |
|
196 |
+ int bits_needed = avctx->channels * chan_pos_bits + 7; |
|
197 |
+ if (get_bits_left(&gb) < bits_needed) |
|
198 |
+ return -1; |
|
199 |
+ |
|
200 |
+ if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos)))) |
|
201 |
+ return AVERROR(ENOMEM); |
|
202 |
+ |
|
203 |
+ for (i = 0; i < avctx->channels; i++) |
|
204 |
+ sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits); |
|
205 |
+ |
|
206 |
+ align_get_bits(&gb); |
|
207 |
+ // TODO: use this to actually do channel sorting |
|
208 |
+ } else { |
|
209 |
+ sconf->chan_sort = 0; |
|
210 |
+ } |
|
211 |
+ |
|
212 |
+ |
|
213 |
+ // read fixed header and trailer sizes, |
|
214 |
+ // if size = 0xFFFFFFFF then there is no data field! |
|
215 |
+ if (get_bits_left(&gb) < 64) |
|
216 |
+ return -1; |
|
217 |
+ |
|
218 |
+ sconf->header_size = get_bits_long(&gb, 32); |
|
219 |
+ sconf->trailer_size = get_bits_long(&gb, 32); |
|
220 |
+ if (sconf->header_size == 0xFFFFFFFF) |
|
221 |
+ sconf->header_size = 0; |
|
222 |
+ if (sconf->trailer_size == 0xFFFFFFFF) |
|
223 |
+ sconf->trailer_size = 0; |
|
224 |
+ |
|
225 |
+ ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3; |
|
226 |
+ |
|
227 |
+ |
|
228 |
+ // skip the header and trailer data |
|
229 |
+ if (get_bits_left(&gb) < ht_size) |
|
230 |
+ return -1; |
|
231 |
+ |
|
232 |
+ if (ht_size > INT32_MAX) |
|
233 |
+ return -1; |
|
234 |
+ |
|
235 |
+ skip_bits_long(&gb, ht_size); |
|
236 |
+ |
|
237 |
+ |
|
238 |
+ // skip the crc data |
|
239 |
+ if (crc_enabled) { |
|
240 |
+ if (get_bits_left(&gb) < 32) |
|
241 |
+ return -1; |
|
242 |
+ |
|
243 |
+ skip_bits_long(&gb, 32); |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ |
|
247 |
+ // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data) |
|
248 |
+ |
|
249 |
+ dprint_specific_config(ctx); |
|
250 |
+ |
|
251 |
+ return 0; |
|
252 |
+} |
|
253 |
+ |
|
254 |
+ |
|
255 |
+/** Checks the ALSSpecificConfig for unsupported features. |
|
256 |
+ */ |
|
257 |
+static int check_specific_config(ALSDecContext *ctx) |
|
258 |
+{ |
|
259 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
260 |
+ int error = 0; |
|
261 |
+ |
|
262 |
+ // report unsupported feature and set error value |
|
263 |
+ #define MISSING_ERR(cond, str, errval) \ |
|
264 |
+ { \ |
|
265 |
+ if (cond) { \ |
|
266 |
+ av_log_missing_feature(ctx->avctx, str, 0); \ |
|
267 |
+ error = errval; \ |
|
268 |
+ } \ |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ MISSING_ERR(sconf->floating, "Floating point decoding", -1); |
|
272 |
+ MISSING_ERR(sconf->long_term_prediction, "Long-term prediction", -1); |
|
273 |
+ MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1); |
|
274 |
+ MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1); |
|
275 |
+ MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1); |
|
276 |
+ MISSING_ERR(sconf->chan_sort, "Channel sorting", 0); |
|
277 |
+ |
|
278 |
+ return error; |
|
279 |
+} |
|
280 |
+ |
|
281 |
+ |
|
282 |
+/** Parses the bs_info field to extract the block partitioning used in |
|
283 |
+ * block switching mode, refer to ISO/IEC 14496-3, section 11.6.2. |
|
284 |
+ */ |
|
285 |
+static void parse_bs_info(const uint32_t bs_info, unsigned int n, |
|
286 |
+ unsigned int div, unsigned int **div_blocks, |
|
287 |
+ unsigned int *num_blocks) |
|
288 |
+{ |
|
289 |
+ if (n < 31 && ((bs_info << n) & 0x40000000)) { |
|
290 |
+ // if the level is valid and the investigated bit n is set |
|
291 |
+ // then recursively check both children at bits (2n+1) and (2n+2) |
|
292 |
+ n *= 2; |
|
293 |
+ div += 1; |
|
294 |
+ parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks); |
|
295 |
+ parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks); |
|
296 |
+ } else { |
|
297 |
+ // else the bit is not set or the last level has been reached |
|
298 |
+ // (bit implicitly not set) |
|
299 |
+ **div_blocks = div; |
|
300 |
+ (*div_blocks)++; |
|
301 |
+ (*num_blocks)++; |
|
302 |
+ } |
|
303 |
+} |
|
304 |
+ |
|
305 |
+ |
|
306 |
+/** Reads and decodes a Rice codeword. |
|
307 |
+ */ |
|
308 |
+static int32_t decode_rice(GetBitContext *gb, unsigned int k) |
|
309 |
+{ |
|
310 |
+ int max = gb->size_in_bits - get_bits_count(gb) - k; |
|
311 |
+ int q = get_unary(gb, 0, max); |
|
312 |
+ int r = k ? get_bits1(gb) : !(q & 1); |
|
313 |
+ |
|
314 |
+ if (k > 1) { |
|
315 |
+ q <<= (k - 1); |
|
316 |
+ q += get_bits_long(gb, k - 1); |
|
317 |
+ } else if (!k) { |
|
318 |
+ q >>= 1; |
|
319 |
+ } |
|
320 |
+ return r ? q : ~q; |
|
321 |
+} |
|
322 |
+ |
|
323 |
+ |
|
324 |
+/** Converts PARCOR coefficient k to direct filter coefficient. |
|
325 |
+ */ |
|
326 |
+static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof) |
|
327 |
+{ |
|
328 |
+ int i, j; |
|
329 |
+ |
|
330 |
+ for (i = 0, j = k - 1; i < j; i++, j--) { |
|
331 |
+ int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); |
|
332 |
+ cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20); |
|
333 |
+ cof[i] += tmp1; |
|
334 |
+ } |
|
335 |
+ if (i == j) |
|
336 |
+ cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); |
|
337 |
+ |
|
338 |
+ cof[k] = par[k]; |
|
339 |
+} |
|
340 |
+ |
|
341 |
+ |
|
342 |
+/** Reads block switching field if necessary and sets actual block sizes. |
|
343 |
+ * Also assures that the block sizes of the last frame correspond to the |
|
344 |
+ * actual number of samples. |
|
345 |
+ */ |
|
346 |
+static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks, |
|
347 |
+ uint32_t *bs_info) |
|
348 |
+{ |
|
349 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
350 |
+ GetBitContext *gb = &ctx->gb; |
|
351 |
+ unsigned int *ptr_div_blocks = div_blocks; |
|
352 |
+ unsigned int b; |
|
353 |
+ |
|
354 |
+ if (sconf->block_switching) { |
|
355 |
+ unsigned int bs_info_len = 1 << (sconf->block_switching + 2); |
|
356 |
+ *bs_info = get_bits_long(gb, bs_info_len); |
|
357 |
+ *bs_info <<= (32 - bs_info_len); |
|
358 |
+ } |
|
359 |
+ |
|
360 |
+ ctx->num_blocks = 0; |
|
361 |
+ parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks); |
|
362 |
+ |
|
363 |
+ // The last frame may have an overdetermined block structure given in |
|
364 |
+ // the bitstream. In that case the defined block structure would need |
|
365 |
+ // more samples than available to be consistent. |
|
366 |
+ // The block structure is actually used but the block sizes are adapted |
|
367 |
+ // to fit the actual number of available samples. |
|
368 |
+ // Example: 5 samples, 2nd level block sizes: 2 2 2 2. |
|
369 |
+ // This results in the actual block sizes: 2 2 1 0. |
|
370 |
+ // This is not specified in 14496-3 but actually done by the reference |
|
371 |
+ // codec RM22 revision 2. |
|
372 |
+ // This appears to happen in case of an odd number of samples in the last |
|
373 |
+ // frame which is actually not allowed by the block length switching part |
|
374 |
+ // of 14496-3. |
|
375 |
+ // The ALS conformance files feature an odd number of samples in the last |
|
376 |
+ // frame. |
|
377 |
+ |
|
378 |
+ for (b = 0; b < ctx->num_blocks; b++) |
|
379 |
+ div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b]; |
|
380 |
+ |
|
381 |
+ if (ctx->cur_frame_length != ctx->sconf.frame_length) { |
|
382 |
+ unsigned int remaining = ctx->cur_frame_length; |
|
383 |
+ |
|
384 |
+ for (b = 0; b < ctx->num_blocks; b++) { |
|
385 |
+ if (remaining < div_blocks[b]) { |
|
386 |
+ div_blocks[b] = remaining; |
|
387 |
+ ctx->num_blocks = b + 1; |
|
388 |
+ break; |
|
389 |
+ } |
|
390 |
+ |
|
391 |
+ remaining -= div_blocks[b]; |
|
392 |
+ } |
|
393 |
+ } |
|
394 |
+} |
|
395 |
+ |
|
396 |
+ |
|
397 |
+/** Reads the block data for a constant block |
|
398 |
+ */ |
|
399 |
+static void read_const_block(ALSDecContext *ctx, int32_t *raw_samples, |
|
400 |
+ unsigned int block_length, unsigned int *js_blocks) |
|
401 |
+{ |
|
402 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
403 |
+ AVCodecContext *avctx = ctx->avctx; |
|
404 |
+ GetBitContext *gb = &ctx->gb; |
|
405 |
+ int32_t const_val = 0; |
|
406 |
+ unsigned int const_block, k; |
|
407 |
+ |
|
408 |
+ const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence) |
|
409 |
+ *js_blocks = get_bits1(gb); |
|
410 |
+ |
|
411 |
+ // skip 5 reserved bits |
|
412 |
+ skip_bits(gb, 5); |
|
413 |
+ |
|
414 |
+ if (const_block) { |
|
415 |
+ unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample; |
|
416 |
+ const_val = get_sbits_long(gb, const_val_bits); |
|
417 |
+ } |
|
418 |
+ |
|
419 |
+ // write raw samples into buffer |
|
420 |
+ for (k = 0; k < block_length; k++) |
|
421 |
+ raw_samples[k] = const_val; |
|
422 |
+} |
|
423 |
+ |
|
424 |
+ |
|
425 |
+/** Reads the block data for a non-constant block |
|
426 |
+ */ |
|
427 |
+static int read_var_block(ALSDecContext *ctx, unsigned int ra_block, |
|
428 |
+ int32_t *raw_samples, unsigned int block_length, |
|
429 |
+ unsigned int *js_blocks, int32_t *raw_other, |
|
430 |
+ unsigned int *shift_lsbs) |
|
431 |
+{ |
|
432 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
433 |
+ AVCodecContext *avctx = ctx->avctx; |
|
434 |
+ GetBitContext *gb = &ctx->gb; |
|
435 |
+ unsigned int k; |
|
436 |
+ unsigned int s[8]; |
|
437 |
+ unsigned int sub_blocks, log2_sub_blocks, sb_length; |
|
438 |
+ unsigned int opt_order = 1; |
|
439 |
+ int32_t *quant_cof = ctx->quant_cof; |
|
440 |
+ int32_t *lpc_cof = ctx->lpc_cof; |
|
441 |
+ unsigned int start = 0; |
|
442 |
+ int smp = 0; |
|
443 |
+ int sb, store_prev_samples; |
|
444 |
+ int64_t y; |
|
445 |
+ |
|
446 |
+ *js_blocks = get_bits1(gb); |
|
447 |
+ |
|
448 |
+ // determine the number of subblocks for entropy decoding |
|
449 |
+ if (!sconf->bgmc && !sconf->sb_part) { |
|
450 |
+ log2_sub_blocks = 0; |
|
451 |
+ } else { |
|
452 |
+ if (sconf->bgmc && sconf->sb_part) |
|
453 |
+ log2_sub_blocks = get_bits(gb, 2); |
|
454 |
+ else |
|
455 |
+ log2_sub_blocks = 2 * get_bits1(gb); |
|
456 |
+ } |
|
457 |
+ |
|
458 |
+ sub_blocks = 1 << log2_sub_blocks; |
|
459 |
+ |
|
460 |
+ // do not continue in case of a damaged stream since |
|
461 |
+ // block_length must be evenly divisible by sub_blocks |
|
462 |
+ if (block_length & (sub_blocks - 1)) { |
|
463 |
+ av_log(avctx, AV_LOG_WARNING, |
|
464 |
+ "Block length is not evenly divisible by the number of subblocks.\n"); |
|
465 |
+ return -1; |
|
466 |
+ } |
|
467 |
+ |
|
468 |
+ sb_length = block_length >> log2_sub_blocks; |
|
469 |
+ |
|
470 |
+ |
|
471 |
+ if (sconf->bgmc) { |
|
472 |
+ // TODO: BGMC mode |
|
473 |
+ } else { |
|
474 |
+ s[0] = get_bits(gb, 4 + (sconf->resolution > 1)); |
|
475 |
+ for (k = 1; k < sub_blocks; k++) |
|
476 |
+ s[k] = s[k - 1] + decode_rice(gb, 0); |
|
477 |
+ } |
|
478 |
+ |
|
479 |
+ if (get_bits1(gb)) |
|
480 |
+ *shift_lsbs = get_bits(gb, 4) + 1; |
|
481 |
+ |
|
482 |
+ store_prev_samples = (*js_blocks && raw_other) || *shift_lsbs; |
|
483 |
+ |
|
484 |
+ |
|
485 |
+ if (!sconf->rlslms) { |
|
486 |
+ if (sconf->adapt_order) { |
|
487 |
+ int opt_order_length = av_ceil_log2(av_clip((block_length >> 3) - 1, |
|
488 |
+ 2, sconf->max_order + 1)); |
|
489 |
+ opt_order = get_bits(gb, opt_order_length); |
|
490 |
+ } else { |
|
491 |
+ opt_order = sconf->max_order; |
|
492 |
+ } |
|
493 |
+ |
|
494 |
+ if (opt_order) { |
|
495 |
+ int add_base; |
|
496 |
+ |
|
497 |
+ if (sconf->coef_table == 3) { |
|
498 |
+ add_base = 0x7F; |
|
499 |
+ |
|
500 |
+ // read coefficient 0 |
|
501 |
+ quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)]; |
|
502 |
+ |
|
503 |
+ // read coefficient 1 |
|
504 |
+ if (opt_order > 1) |
|
505 |
+ quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)]; |
|
506 |
+ |
|
507 |
+ // read coefficients 2 to opt_order |
|
508 |
+ for (k = 2; k < opt_order; k++) |
|
509 |
+ quant_cof[k] = get_bits(gb, 7); |
|
510 |
+ } else { |
|
511 |
+ int k_max; |
|
512 |
+ add_base = 1; |
|
513 |
+ |
|
514 |
+ // read coefficient 0 to 19 |
|
515 |
+ k_max = FFMIN(opt_order, 20); |
|
516 |
+ for (k = 0; k < k_max; k++) { |
|
517 |
+ int rice_param = parcor_rice_table[sconf->coef_table][k][1]; |
|
518 |
+ int offset = parcor_rice_table[sconf->coef_table][k][0]; |
|
519 |
+ quant_cof[k] = decode_rice(gb, rice_param) + offset; |
|
520 |
+ } |
|
521 |
+ |
|
522 |
+ // read coefficients 20 to 126 |
|
523 |
+ k_max = FFMIN(opt_order, 127); |
|
524 |
+ for (; k < k_max; k++) |
|
525 |
+ quant_cof[k] = decode_rice(gb, 2) + (k & 1); |
|
526 |
+ |
|
527 |
+ // read coefficients 127 to opt_order |
|
528 |
+ for (; k < opt_order; k++) |
|
529 |
+ quant_cof[k] = decode_rice(gb, 1); |
|
530 |
+ |
|
531 |
+ quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64]; |
|
532 |
+ |
|
533 |
+ if (opt_order > 1) |
|
534 |
+ quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64]; |
|
535 |
+ } |
|
536 |
+ |
|
537 |
+ for (k = 2; k < opt_order; k++) |
|
538 |
+ quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13); |
|
539 |
+ } |
|
540 |
+ } |
|
541 |
+ |
|
542 |
+ // TODO: LTP mode |
|
543 |
+ |
|
544 |
+ // read first value and residuals in case of a random access block |
|
545 |
+ if (ra_block) { |
|
546 |
+ if (opt_order) |
|
547 |
+ raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4); |
|
548 |
+ if (opt_order > 1) |
|
549 |
+ raw_samples[1] = decode_rice(gb, s[0] + 3); |
|
550 |
+ if (opt_order > 2) |
|
551 |
+ raw_samples[2] = decode_rice(gb, s[0] + 1); |
|
552 |
+ |
|
553 |
+ start = FFMIN(opt_order, 3); |
|
554 |
+ } |
|
555 |
+ |
|
556 |
+ // read all residuals |
|
557 |
+ if (sconf->bgmc) { |
|
558 |
+ // TODO: BGMC mode |
|
559 |
+ } else { |
|
560 |
+ int32_t *current_res = raw_samples + start; |
|
561 |
+ |
|
562 |
+ for (sb = 0; sb < sub_blocks; sb++, start = 0) |
|
563 |
+ for (; start < sb_length; start++) |
|
564 |
+ *current_res++ = decode_rice(gb, s[sb]); |
|
565 |
+ } |
|
566 |
+ |
|
567 |
+ // reconstruct all samples from residuals |
|
568 |
+ if (ra_block) { |
|
569 |
+ for (smp = 0; smp < opt_order; smp++) { |
|
570 |
+ y = 1 << 19; |
|
571 |
+ |
|
572 |
+ for (sb = 0; sb < smp; sb++) |
|
573 |
+ y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]); |
|
574 |
+ |
|
575 |
+ raw_samples[smp] -= y >> 20; |
|
576 |
+ parcor_to_lpc(smp, quant_cof, lpc_cof); |
|
577 |
+ } |
|
578 |
+ } else { |
|
579 |
+ for (k = 0; k < opt_order; k++) |
|
580 |
+ parcor_to_lpc(k, quant_cof, lpc_cof); |
|
581 |
+ |
|
582 |
+ // store previous samples in case that they have to be altered |
|
583 |
+ if (store_prev_samples) |
|
584 |
+ memcpy(ctx->prev_raw_samples, raw_samples - sconf->max_order, |
|
585 |
+ sizeof(*ctx->prev_raw_samples) * sconf->max_order); |
|
586 |
+ |
|
587 |
+ // reconstruct difference signal for prediction (joint-stereo) |
|
588 |
+ if (*js_blocks && raw_other) { |
|
589 |
+ int32_t *left, *right; |
|
590 |
+ |
|
591 |
+ if (raw_other > raw_samples) { // D = R - L |
|
592 |
+ left = raw_samples; |
|
593 |
+ right = raw_other; |
|
594 |
+ } else { // D = R - L |
|
595 |
+ left = raw_other; |
|
596 |
+ right = raw_samples; |
|
597 |
+ } |
|
598 |
+ |
|
599 |
+ for (sb = -1; sb >= -sconf->max_order; sb--) |
|
600 |
+ raw_samples[sb] = right[sb] - left[sb]; |
|
601 |
+ } |
|
602 |
+ |
|
603 |
+ // reconstruct shifted signal |
|
604 |
+ if (*shift_lsbs) |
|
605 |
+ for (sb = -1; sb >= -sconf->max_order; sb--) |
|
606 |
+ raw_samples[sb] >>= *shift_lsbs; |
|
607 |
+ } |
|
608 |
+ |
|
609 |
+ // reconstruct raw samples |
|
610 |
+ for (; smp < block_length; smp++) { |
|
611 |
+ y = 1 << 19; |
|
612 |
+ |
|
613 |
+ for (sb = 0; sb < opt_order; sb++) |
|
614 |
+ y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]); |
|
615 |
+ |
|
616 |
+ raw_samples[smp] -= y >> 20; |
|
617 |
+ } |
|
618 |
+ |
|
619 |
+ // restore previous samples in case that they have been altered |
|
620 |
+ if (store_prev_samples) |
|
621 |
+ memcpy(raw_samples - sconf->max_order, ctx->prev_raw_samples, |
|
622 |
+ sizeof(*raw_samples) * sconf->max_order); |
|
623 |
+ |
|
624 |
+ return 0; |
|
625 |
+} |
|
626 |
+ |
|
627 |
+ |
|
628 |
+/** Reads the block data. |
|
629 |
+ */ |
|
630 |
+static int read_block_data(ALSDecContext *ctx, unsigned int ra_block, |
|
631 |
+ int32_t *raw_samples, unsigned int block_length, |
|
632 |
+ unsigned int *js_blocks, int32_t *raw_other) |
|
633 |
+{ |
|
634 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
635 |
+ GetBitContext *gb = &ctx->gb; |
|
636 |
+ unsigned int shift_lsbs = 0; |
|
637 |
+ unsigned int k; |
|
638 |
+ |
|
639 |
+ // read block type flag and read the samples accordingly |
|
640 |
+ if (get_bits1(gb)) { |
|
641 |
+ if (read_var_block(ctx, ra_block, raw_samples, block_length, js_blocks, |
|
642 |
+ raw_other, &shift_lsbs)) |
|
643 |
+ return -1; |
|
644 |
+ } else { |
|
645 |
+ read_const_block(ctx, raw_samples, block_length, js_blocks); |
|
646 |
+ } |
|
647 |
+ |
|
648 |
+ // TODO: read RLSLMS extension data |
|
649 |
+ |
|
650 |
+ if (!sconf->mc_coding || ctx->js_switch) |
|
651 |
+ align_get_bits(gb); |
|
652 |
+ |
|
653 |
+ if (shift_lsbs) |
|
654 |
+ for (k = 0; k < block_length; k++) |
|
655 |
+ raw_samples[k] <<= shift_lsbs; |
|
656 |
+ |
|
657 |
+ return 0; |
|
658 |
+} |
|
659 |
+ |
|
660 |
+ |
|
661 |
+/** Computes the number of samples left to decode for the current frame and |
|
662 |
+ * sets these samples to zero. |
|
663 |
+ */ |
|
664 |
+static void zero_remaining(unsigned int b, unsigned int b_max, |
|
665 |
+ const unsigned int *div_blocks, int32_t *buf) |
|
666 |
+{ |
|
667 |
+ unsigned int count = 0; |
|
668 |
+ |
|
669 |
+ while (b < b_max) |
|
670 |
+ count += div_blocks[b]; |
|
671 |
+ |
|
672 |
+ memset(buf, 0, sizeof(*buf) * count); |
|
673 |
+} |
|
674 |
+ |
|
675 |
+ |
|
676 |
+/** Decodes blocks independently. |
|
677 |
+ */ |
|
678 |
+static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame, |
|
679 |
+ unsigned int c, const unsigned int *div_blocks, |
|
680 |
+ unsigned int *js_blocks) |
|
681 |
+{ |
|
682 |
+ int32_t *raw_sample; |
|
683 |
+ unsigned int b; |
|
684 |
+ raw_sample = ctx->raw_samples[c]; |
|
685 |
+ |
|
686 |
+ for (b = 0; b < ctx->num_blocks; b++) { |
|
687 |
+ if (read_block_data(ctx, ra_frame, raw_sample, |
|
688 |
+ div_blocks[b], &js_blocks[0], NULL)) { |
|
689 |
+ // damaged block, write zero for the rest of the frame |
|
690 |
+ zero_remaining(b, ctx->num_blocks, div_blocks, raw_sample); |
|
691 |
+ return -1; |
|
692 |
+ } |
|
693 |
+ raw_sample += div_blocks[b]; |
|
694 |
+ ra_frame = 0; |
|
695 |
+ } |
|
696 |
+ |
|
697 |
+ return 0; |
|
698 |
+} |
|
699 |
+ |
|
700 |
+ |
|
701 |
+/** Decodes blocks dependently. |
|
702 |
+ */ |
|
703 |
+static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame, |
|
704 |
+ unsigned int c, const unsigned int *div_blocks, |
|
705 |
+ unsigned int *js_blocks) |
|
706 |
+{ |
|
707 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
708 |
+ unsigned int offset = 0; |
|
709 |
+ int32_t *raw_samples_R; |
|
710 |
+ int32_t *raw_samples_L; |
|
711 |
+ unsigned int b; |
|
712 |
+ |
|
713 |
+ // decode all blocks |
|
714 |
+ for (b = 0; b < ctx->num_blocks; b++) { |
|
715 |
+ unsigned int s; |
|
716 |
+ raw_samples_L = ctx->raw_samples[c ] + offset; |
|
717 |
+ raw_samples_R = ctx->raw_samples[c + 1] + offset; |
|
718 |
+ if (read_block_data(ctx, ra_frame, raw_samples_L, div_blocks[b], |
|
719 |
+ &js_blocks[0], raw_samples_R) || |
|
720 |
+ read_block_data(ctx, ra_frame, raw_samples_R, div_blocks[b], |
|
721 |
+ &js_blocks[1], raw_samples_L)) { |
|
722 |
+ // damaged block, write zero for the rest of the frame |
|
723 |
+ zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_L); |
|
724 |
+ zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_R); |
|
725 |
+ return -1; |
|
726 |
+ } |
|
727 |
+ |
|
728 |
+ // reconstruct joint-stereo blocks |
|
729 |
+ if (js_blocks[0]) { |
|
730 |
+ if (js_blocks[1]) |
|
731 |
+ av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n"); |
|
732 |
+ |
|
733 |
+ for (s = 0; s < div_blocks[b]; s++) |
|
734 |
+ raw_samples_L[s] = raw_samples_R[s] - raw_samples_L[s]; |
|
735 |
+ } else if (js_blocks[1]) { |
|
736 |
+ for (s = 0; s < div_blocks[b]; s++) |
|
737 |
+ raw_samples_R[s] = raw_samples_R[s] + raw_samples_L[s]; |
|
738 |
+ } |
|
739 |
+ |
|
740 |
+ offset += div_blocks[b]; |
|
741 |
+ ra_frame = 0; |
|
742 |
+ } |
|
743 |
+ |
|
744 |
+ // store carryover raw samples, |
|
745 |
+ // the others channel raw samples are stored by the calling function. |
|
746 |
+ memmove(ctx->raw_samples[c] - sconf->max_order, |
|
747 |
+ ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, |
|
748 |
+ sizeof(*ctx->raw_samples[c]) * sconf->max_order); |
|
749 |
+ |
|
750 |
+ return 0; |
|
751 |
+} |
|
752 |
+ |
|
753 |
+ |
|
754 |
+/** Reads the frame data. |
|
755 |
+ */ |
|
756 |
+static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame) |
|
757 |
+{ |
|
758 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
759 |
+ AVCodecContext *avctx = ctx->avctx; |
|
760 |
+ GetBitContext *gb = &ctx->gb; |
|
761 |
+ unsigned int div_blocks[32]; ///< block sizes. |
|
762 |
+ unsigned int c; |
|
763 |
+ unsigned int js_blocks[2]; |
|
764 |
+ |
|
765 |
+ uint32_t bs_info = 0; |
|
766 |
+ |
|
767 |
+ // skip the size of the ra unit if present in the frame |
|
768 |
+ if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame) |
|
769 |
+ skip_bits_long(gb, 32); |
|
770 |
+ |
|
771 |
+ if (sconf->mc_coding && sconf->joint_stereo) { |
|
772 |
+ ctx->js_switch = get_bits1(gb); |
|
773 |
+ align_get_bits(gb); |
|
774 |
+ } |
|
775 |
+ |
|
776 |
+ if (!sconf->mc_coding || ctx->js_switch) { |
|
777 |
+ int independent_bs = !sconf->joint_stereo; |
|
778 |
+ |
|
779 |
+ for (c = 0; c < avctx->channels; c++) { |
|
780 |
+ js_blocks[0] = 0; |
|
781 |
+ js_blocks[1] = 0; |
|
782 |
+ |
|
783 |
+ get_block_sizes(ctx, div_blocks, &bs_info); |
|
784 |
+ |
|
785 |
+ // if joint_stereo and block_switching is set, independent decoding |
|
786 |
+ // is signaled via the first bit of bs_info |
|
787 |
+ if (sconf->joint_stereo && sconf->block_switching) |
|
788 |
+ if (bs_info >> 31) |
|
789 |
+ independent_bs = 2; |
|
790 |
+ |
|
791 |
+ // if this is the last channel, it has to be decoded independently |
|
792 |
+ if (c == avctx->channels - 1) |
|
793 |
+ independent_bs = 1; |
|
794 |
+ |
|
795 |
+ if (independent_bs) { |
|
796 |
+ if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks)) |
|
797 |
+ return -1; |
|
798 |
+ |
|
799 |
+ independent_bs--; |
|
800 |
+ } else { |
|
801 |
+ if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks)) |
|
802 |
+ return -1; |
|
803 |
+ |
|
804 |
+ c++; |
|
805 |
+ } |
|
806 |
+ |
|
807 |
+ // store carryover raw samples |
|
808 |
+ memmove(ctx->raw_samples[c] - sconf->max_order, |
|
809 |
+ ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, |
|
810 |
+ sizeof(*ctx->raw_samples[c]) * sconf->max_order); |
|
811 |
+ } |
|
812 |
+ } else { // multi-channel coding |
|
813 |
+ get_block_sizes(ctx, div_blocks, &bs_info); |
|
814 |
+ |
|
815 |
+ // TODO: multi channel coding might use a temporary buffer instead as |
|
816 |
+ // the actual channel is not known when read_block-data is called |
|
817 |
+ if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks)) |
|
818 |
+ return -1; |
|
819 |
+ // TODO: read_channel_data |
|
820 |
+ } |
|
821 |
+ |
|
822 |
+ // TODO: read_diff_float_data |
|
823 |
+ |
|
824 |
+ return 0; |
|
825 |
+} |
|
826 |
+ |
|
827 |
+ |
|
828 |
+/** Decodes an ALS frame. |
|
829 |
+ */ |
|
830 |
+static int decode_frame(AVCodecContext *avctx, |
|
831 |
+ void *data, int *data_size, |
|
832 |
+ AVPacket *avpkt) |
|
833 |
+{ |
|
834 |
+ ALSDecContext *ctx = avctx->priv_data; |
|
835 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
836 |
+ const uint8_t *buffer = avpkt->data; |
|
837 |
+ int buffer_size = avpkt->size; |
|
838 |
+ int invalid_frame, size; |
|
839 |
+ unsigned int c, sample, ra_frame, bytes_read, shift; |
|
840 |
+ |
|
841 |
+ init_get_bits(&ctx->gb, buffer, buffer_size * 8); |
|
842 |
+ |
|
843 |
+ // In the case that the distance between random access frames is set to zero |
|
844 |
+ // (sconf->ra_distance == 0) no frame is treated as a random access frame. |
|
845 |
+ // For the first frame, if prediction is used, all samples used from the |
|
846 |
+ // previous frame are assumed to be zero. |
|
847 |
+ ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance); |
|
848 |
+ |
|
849 |
+ // the last frame to decode might have a different length |
|
850 |
+ if (sconf->samples != 0xFFFFFFFF) |
|
851 |
+ ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length, |
|
852 |
+ sconf->frame_length); |
|
853 |
+ else |
|
854 |
+ ctx->cur_frame_length = sconf->frame_length; |
|
855 |
+ |
|
856 |
+ // decode the frame data |
|
857 |
+ if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0)) |
|
858 |
+ av_log(ctx->avctx, AV_LOG_WARNING, |
|
859 |
+ "Reading frame data failed. Skipping RA unit.\n"); |
|
860 |
+ |
|
861 |
+ ctx->frame_id++; |
|
862 |
+ |
|
863 |
+ // check for size of decoded data |
|
864 |
+ size = ctx->cur_frame_length * avctx->channels * |
|
865 |
+ (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3); |
|
866 |
+ |
|
867 |
+ if (size > *data_size) { |
|
868 |
+ av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n"); |
|
869 |
+ return -1; |
|
870 |
+ } |
|
871 |
+ |
|
872 |
+ *data_size = size; |
|
873 |
+ |
|
874 |
+ // transform decoded frame into output format |
|
875 |
+ #define INTERLEAVE_OUTPUT(bps) \ |
|
876 |
+ { \ |
|
877 |
+ int##bps##_t *dest = (int##bps##_t*) data; \ |
|
878 |
+ shift = bps - ctx->avctx->bits_per_raw_sample; \ |
|
879 |
+ for (sample = 0; sample < ctx->cur_frame_length; sample++) \ |
|
880 |
+ for (c = 0; c < avctx->channels; c++) \ |
|
881 |
+ *dest++ = ctx->raw_samples[c][sample] << shift; \ |
|
882 |
+ } |
|
883 |
+ |
|
884 |
+ if (ctx->avctx->bits_per_raw_sample <= 16) { |
|
885 |
+ INTERLEAVE_OUTPUT(16) |
|
886 |
+ } else { |
|
887 |
+ INTERLEAVE_OUTPUT(32) |
|
888 |
+ } |
|
889 |
+ |
|
890 |
+ bytes_read = invalid_frame ? buffer_size : |
|
891 |
+ (get_bits_count(&ctx->gb) + 7) >> 3; |
|
892 |
+ |
|
893 |
+ return bytes_read; |
|
894 |
+} |
|
895 |
+ |
|
896 |
+ |
|
897 |
+/** Uninitializes the ALS decoder. |
|
898 |
+ */ |
|
899 |
+static av_cold int decode_end(AVCodecContext *avctx) |
|
900 |
+{ |
|
901 |
+ ALSDecContext *ctx = avctx->priv_data; |
|
902 |
+ |
|
903 |
+ av_freep(&ctx->sconf.chan_pos); |
|
904 |
+ |
|
905 |
+ av_freep(&ctx->quant_cof); |
|
906 |
+ av_freep(&ctx->lpc_cof); |
|
907 |
+ av_freep(&ctx->prev_raw_samples); |
|
908 |
+ av_freep(&ctx->raw_samples); |
|
909 |
+ av_freep(&ctx->raw_buffer); |
|
910 |
+ |
|
911 |
+ return 0; |
|
912 |
+} |
|
913 |
+ |
|
914 |
+ |
|
915 |
+/** Initializes the ALS decoder. |
|
916 |
+ */ |
|
917 |
+static av_cold int decode_init(AVCodecContext *avctx) |
|
918 |
+{ |
|
919 |
+ unsigned int c; |
|
920 |
+ unsigned int channel_size; |
|
921 |
+ ALSDecContext *ctx = avctx->priv_data; |
|
922 |
+ ALSSpecificConfig *sconf = &ctx->sconf; |
|
923 |
+ ctx->avctx = avctx; |
|
924 |
+ |
|
925 |
+ if (!avctx->extradata) { |
|
926 |
+ av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n"); |
|
927 |
+ return -1; |
|
928 |
+ } |
|
929 |
+ |
|
930 |
+ if (read_specific_config(ctx)) { |
|
931 |
+ av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n"); |
|
932 |
+ decode_end(avctx); |
|
933 |
+ return -1; |
|
934 |
+ } |
|
935 |
+ |
|
936 |
+ if (check_specific_config(ctx)) { |
|
937 |
+ decode_end(avctx); |
|
938 |
+ return -1; |
|
939 |
+ } |
|
940 |
+ |
|
941 |
+ if (sconf->floating) { |
|
942 |
+ avctx->sample_fmt = SAMPLE_FMT_FLT; |
|
943 |
+ avctx->bits_per_raw_sample = 32; |
|
944 |
+ } else { |
|
945 |
+ avctx->sample_fmt = sconf->resolution > 1 |
|
946 |
+ ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16; |
|
947 |
+ avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8; |
|
948 |
+ } |
|
949 |
+ |
|
950 |
+ avctx->frame_size = sconf->frame_length; |
|
951 |
+ channel_size = sconf->frame_length + sconf->max_order; |
|
952 |
+ |
|
953 |
+ ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order); |
|
954 |
+ ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size); |
|
955 |
+ ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels); |
|
956 |
+ |
|
957 |
+ // allocate previous raw sample buffer |
|
958 |
+ if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) { |
|
959 |
+ av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); |
|
960 |
+ decode_end(avctx); |
|
961 |
+ return AVERROR(ENOMEM); |
|
962 |
+ } |
|
963 |
+ |
|
964 |
+ // assign raw samples buffers |
|
965 |
+ ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order; |
|
966 |
+ for (c = 1; c < avctx->channels; c++) |
|
967 |
+ ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size; |
|
968 |
+ |
|
969 |
+ return 0; |
|
970 |
+} |
|
971 |
+ |
|
972 |
+ |
|
973 |
+/** Flushes (resets) the frame ID after seeking. |
|
974 |
+ */ |
|
975 |
+static av_cold void flush(AVCodecContext *avctx) |
|
976 |
+{ |
|
977 |
+ ALSDecContext *ctx = avctx->priv_data; |
|
978 |
+ |
|
979 |
+ ctx->frame_id = 0; |
|
980 |
+} |
|
981 |
+ |
|
982 |
+ |
|
983 |
+AVCodec als_decoder = { |
|
984 |
+ "als", |
|
985 |
+ CODEC_TYPE_AUDIO, |
|
986 |
+ CODEC_ID_MP4ALS, |
|
987 |
+ sizeof(ALSDecContext), |
|
988 |
+ decode_init, |
|
989 |
+ NULL, |
|
990 |
+ decode_end, |
|
991 |
+ decode_frame, |
|
992 |
+ .flush = flush, |
|
993 |
+ .capabilities = CODEC_CAP_SUBFRAMES, |
|
994 |
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"), |
|
995 |
+}; |
|
996 |
+ |