00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "libavutil/common.h"
00029
00030
00031
00032
00033
00034
00035
00036 static const int16_t MACEtab1[] = {-13, 8, 76, 222, 222, 76, 8, -13};
00037
00038 static const int16_t MACEtab3[] = {-18, 140, 140, -18};
00039
00040 static const int16_t MACEtab2[][4] = {
00041 { 37, 116, 206, 330}, { 39, 121, 216, 346},
00042 { 41, 127, 225, 361}, { 42, 132, 235, 377},
00043 { 44, 137, 245, 392}, { 46, 144, 256, 410},
00044 { 48, 150, 267, 428}, { 51, 157, 280, 449},
00045 { 53, 165, 293, 470}, { 55, 172, 306, 490},
00046 { 58, 179, 319, 511}, { 60, 187, 333, 534},
00047 { 63, 195, 348, 557}, { 66, 205, 364, 583},
00048 { 69, 214, 380, 609}, { 72, 223, 396, 635},
00049 { 75, 233, 414, 663}, { 79, 244, 433, 694},
00050 { 82, 254, 453, 725}, { 86, 265, 472, 756},
00051 { 90, 278, 495, 792}, { 94, 290, 516, 826},
00052 { 98, 303, 538, 862}, { 102, 316, 562, 901},
00053 { 107, 331, 588, 942}, { 112, 345, 614, 983},
00054 { 117, 361, 641, 1027}, { 122, 377, 670, 1074},
00055 { 127, 394, 701, 1123}, { 133, 411, 732, 1172},
00056 { 139, 430, 764, 1224}, { 145, 449, 799, 1280},
00057 { 152, 469, 835, 1337}, { 159, 490, 872, 1397},
00058 { 166, 512, 911, 1459}, { 173, 535, 951, 1523},
00059 { 181, 558, 993, 1590}, { 189, 584, 1038, 1663},
00060 { 197, 610, 1085, 1738}, { 206, 637, 1133, 1815},
00061 { 215, 665, 1183, 1895}, { 225, 695, 1237, 1980},
00062 { 235, 726, 1291, 2068}, { 246, 759, 1349, 2161},
00063 { 257, 792, 1409, 2257}, { 268, 828, 1472, 2357},
00064 { 280, 865, 1538, 2463}, { 293, 903, 1606, 2572},
00065 { 306, 944, 1678, 2688}, { 319, 986, 1753, 2807},
00066 { 334, 1030, 1832, 2933}, { 349, 1076, 1914, 3065},
00067 { 364, 1124, 1999, 3202}, { 380, 1174, 2088, 3344},
00068 { 398, 1227, 2182, 3494}, { 415, 1281, 2278, 3649},
00069 { 434, 1339, 2380, 3811}, { 453, 1398, 2486, 3982},
00070 { 473, 1461, 2598, 4160}, { 495, 1526, 2714, 4346},
00071 { 517, 1594, 2835, 4540}, { 540, 1665, 2961, 4741},
00072 { 564, 1740, 3093, 4953}, { 589, 1818, 3232, 5175},
00073 { 615, 1898, 3375, 5405}, { 643, 1984, 3527, 5647},
00074 { 671, 2072, 3683, 5898}, { 701, 2164, 3848, 6161},
00075 { 733, 2261, 4020, 6438}, { 766, 2362, 4199, 6724},
00076 { 800, 2467, 4386, 7024}, { 836, 2578, 4583, 7339},
00077 { 873, 2692, 4786, 7664}, { 912, 2813, 5001, 8008},
00078 { 952, 2938, 5223, 8364}, { 995, 3070, 5457, 8739},
00079 { 1039, 3207, 5701, 9129}, { 1086, 3350, 5956, 9537},
00080 { 1134, 3499, 6220, 9960}, { 1185, 3655, 6497, 10404},
00081 { 1238, 3818, 6788, 10869}, { 1293, 3989, 7091, 11355},
00082 { 1351, 4166, 7407, 11861}, { 1411, 4352, 7738, 12390},
00083 { 1474, 4547, 8084, 12946}, { 1540, 4750, 8444, 13522},
00084 { 1609, 4962, 8821, 14126}, { 1680, 5183, 9215, 14756},
00085 { 1756, 5415, 9626, 15415}, { 1834, 5657, 10057, 16104},
00086 { 1916, 5909, 10505, 16822}, { 2001, 6173, 10975, 17574},
00087 { 2091, 6448, 11463, 18356}, { 2184, 6736, 11974, 19175},
00088 { 2282, 7037, 12510, 20032}, { 2383, 7351, 13068, 20926},
00089 { 2490, 7679, 13652, 21861}, { 2601, 8021, 14260, 22834},
00090 { 2717, 8380, 14897, 23854}, { 2838, 8753, 15561, 24918},
00091 { 2965, 9144, 16256, 26031}, { 3097, 9553, 16982, 27193},
00092 { 3236, 9979, 17740, 28407}, { 3380, 10424, 18532, 29675},
00093 { 3531, 10890, 19359, 31000}, { 3688, 11375, 20222, 32382},
00094 { 3853, 11883, 21125, 32767}, { 4025, 12414, 22069, 32767},
00095 { 4205, 12967, 23053, 32767}, { 4392, 13546, 24082, 32767},
00096 { 4589, 14151, 25157, 32767}, { 4793, 14783, 26280, 32767},
00097 { 5007, 15442, 27452, 32767}, { 5231, 16132, 28678, 32767},
00098 { 5464, 16851, 29957, 32767}, { 5708, 17603, 31294, 32767},
00099 { 5963, 18389, 32691, 32767}, { 6229, 19210, 32767, 32767},
00100 { 6507, 20067, 32767, 32767}, { 6797, 20963, 32767, 32767},
00101 { 7101, 21899, 32767, 32767}, { 7418, 22876, 32767, 32767},
00102 { 7749, 23897, 32767, 32767}, { 8095, 24964, 32767, 32767},
00103 { 8456, 26078, 32767, 32767}, { 8833, 27242, 32767, 32767},
00104 { 9228, 28457, 32767, 32767}, { 9639, 29727, 32767, 32767}
00105 };
00106
00107 static const int16_t MACEtab4[][2] = {
00108 { 64, 216}, { 67, 226}, { 70, 236}, { 74, 246},
00109 { 77, 257}, { 80, 268}, { 84, 280}, { 88, 294},
00110 { 92, 307}, { 96, 321}, { 100, 334}, { 104, 350},
00111 { 109, 365}, { 114, 382}, { 119, 399}, { 124, 416},
00112 { 130, 434}, { 136, 454}, { 142, 475}, { 148, 495},
00113 { 155, 519}, { 162, 541}, { 169, 564}, { 176, 590},
00114 { 185, 617}, { 193, 644}, { 201, 673}, { 210, 703},
00115 { 220, 735}, { 230, 767}, { 240, 801}, { 251, 838},
00116 { 262, 876}, { 274, 914}, { 286, 955}, { 299, 997},
00117 { 312, 1041}, { 326, 1089}, { 341, 1138}, { 356, 1188},
00118 { 372, 1241}, { 388, 1297}, { 406, 1354}, { 424, 1415},
00119 { 443, 1478}, { 462, 1544}, { 483, 1613}, { 505, 1684},
00120 { 527, 1760}, { 551, 1838}, { 576, 1921}, { 601, 2007},
00121 { 628, 2097}, { 656, 2190}, { 686, 2288}, { 716, 2389},
00122 { 748, 2496}, { 781, 2607}, { 816, 2724}, { 853, 2846},
00123 { 891, 2973}, { 930, 3104}, { 972, 3243}, { 1016, 3389},
00124 { 1061, 3539}, { 1108, 3698}, { 1158, 3862}, { 1209, 4035},
00125 { 1264, 4216}, { 1320, 4403}, { 1379, 4599}, { 1441, 4806},
00126 { 1505, 5019}, { 1572, 5244}, { 1642, 5477}, { 1715, 5722},
00127 { 1792, 5978}, { 1872, 6245}, { 1955, 6522}, { 2043, 6813},
00128 { 2134, 7118}, { 2229, 7436}, { 2329, 7767}, { 2432, 8114},
00129 { 2541, 8477}, { 2655, 8854}, { 2773, 9250}, { 2897, 9663},
00130 { 3026, 10094}, { 3162, 10546}, { 3303, 11016}, { 3450, 11508},
00131 { 3604, 12020}, { 3765, 12556}, { 3933, 13118}, { 4108, 13703},
00132 { 4292, 14315}, { 4483, 14953}, { 4683, 15621}, { 4892, 16318},
00133 { 5111, 17046}, { 5339, 17807}, { 5577, 18602}, { 5826, 19433},
00134 { 6086, 20300}, { 6358, 21205}, { 6642, 22152}, { 6938, 23141},
00135 { 7248, 24173}, { 7571, 25252}, { 7909, 26380}, { 8262, 27557},
00136 { 8631, 28786}, { 9016, 30072}, { 9419, 31413}, { 9839, 32767},
00137 { 10278, 32767}, { 10737, 32767}, { 11216, 32767}, { 11717, 32767},
00138 { 12240, 32767}, { 12786, 32767}, { 13356, 32767}, { 13953, 32767},
00139 { 14576, 32767}, { 15226, 32767}, { 15906, 32767}, { 16615, 32767}
00140 };
00141
00142 static const struct {
00143 const int16_t *tab1; const int16_t *tab2; int stride;
00144 } tabs[] = {
00145 {MACEtab1, &MACEtab2[0][0], 4},
00146 {MACEtab3, &MACEtab4[0][0], 2},
00147 {MACEtab1, &MACEtab2[0][0], 4}
00148 };
00149
00150 #define QT_8S_2_16S(x) (((x) & 0xFF00) | (((x) >> 8) & 0xFF))
00151
00152 typedef struct ChannelData {
00153 int16_t index, factor, prev2, previous, level;
00154 } ChannelData;
00155
00156 typedef struct MACEContext {
00157 AVFrame frame;
00158 ChannelData chd[2];
00159 } MACEContext;
00160
00165 static inline int16_t mace_broken_clip_int16(int n)
00166 {
00167 if (n > 32767)
00168 return 32767;
00169 else if (n < -32768)
00170 return -32767;
00171 else
00172 return n;
00173 }
00174
00175 static int16_t read_table(ChannelData *chd, uint8_t val, int tab_idx)
00176 {
00177 int16_t current;
00178
00179 if (val < tabs[tab_idx].stride)
00180 current = tabs[tab_idx].tab2[((chd->index & 0x7f0) >> 4) * tabs[tab_idx].stride + val];
00181 else
00182 current = - 1 - tabs[tab_idx].tab2[((chd->index & 0x7f0) >> 4)*tabs[tab_idx].stride + 2*tabs[tab_idx].stride-val-1];
00183
00184 if (( chd->index += tabs[tab_idx].tab1[val]-(chd->index >> 5) ) < 0)
00185 chd->index = 0;
00186
00187 return current;
00188 }
00189
00190 static void chomp3(ChannelData *chd, int16_t *output, uint8_t val,
00191 int tab_idx,
00192 uint32_t numChannels)
00193 {
00194
00195 int16_t current = read_table(chd, val, tab_idx);
00196
00197 current = mace_broken_clip_int16(current + chd->level);
00198
00199 chd->level = current - (current >> 3);
00200 *output = QT_8S_2_16S(current);
00201 }
00202
00203 static void chomp6(ChannelData *chd, int16_t *output, uint8_t val,
00204 int tab_idx,
00205 uint32_t numChannels)
00206 {
00207 int16_t current = read_table(chd, val, tab_idx);
00208
00209 if ((chd->previous ^ current) >= 0) {
00210 chd->factor = FFMIN(chd->factor + 506, 32767);
00211 } else {
00212 if (chd->factor - 314 < -32768)
00213 chd->factor = -32767;
00214 else
00215 chd->factor -= 314;
00216 }
00217
00218 current = mace_broken_clip_int16(current + chd->level);
00219
00220 chd->level = (current*chd->factor) >> 15;
00221 current >>= 1;
00222
00223 output[0] = QT_8S_2_16S(chd->previous + chd->prev2 -
00224 ((chd->prev2-current) >> 2));
00225 output[numChannels] = QT_8S_2_16S(chd->previous + current +
00226 ((chd->prev2-current) >> 2));
00227 chd->prev2 = chd->previous;
00228 chd->previous = current;
00229 }
00230
00231 static av_cold int mace_decode_init(AVCodecContext * avctx)
00232 {
00233 MACEContext *ctx = avctx->priv_data;
00234
00235 if (avctx->channels > 2 || avctx->channels <= 0)
00236 return -1;
00237 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00238
00239 avcodec_get_frame_defaults(&ctx->frame);
00240 avctx->coded_frame = &ctx->frame;
00241
00242 return 0;
00243 }
00244
00245 static int mace_decode_frame(AVCodecContext *avctx, void *data,
00246 int *got_frame_ptr, AVPacket *avpkt)
00247 {
00248 const uint8_t *buf = avpkt->data;
00249 int buf_size = avpkt->size;
00250 int16_t *samples;
00251 MACEContext *ctx = avctx->priv_data;
00252 int i, j, k, l, ret;
00253 int is_mace3 = (avctx->codec_id == AV_CODEC_ID_MACE3);
00254
00255
00256 ctx->frame.nb_samples = 3 * (buf_size << (1 - is_mace3)) / avctx->channels;
00257 if ((ret = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
00258 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00259 return ret;
00260 }
00261 samples = (int16_t *)ctx->frame.data[0];
00262
00263 for(i = 0; i < avctx->channels; i++) {
00264 int16_t *output = samples + i;
00265
00266 for (j=0; j < buf_size / (avctx->channels << is_mace3); j++)
00267 for (k=0; k < (1 << is_mace3); k++) {
00268 uint8_t pkt = buf[(i << is_mace3) +
00269 (j*avctx->channels << is_mace3) + k];
00270
00271 uint8_t val[2][3] = {{pkt >> 5, (pkt >> 3) & 3, pkt & 7 },
00272 {pkt & 7 , (pkt >> 3) & 3, pkt >> 5}};
00273
00274 for (l=0; l < 3; l++) {
00275 if (is_mace3)
00276 chomp3(&ctx->chd[i], output, val[1][l], l,
00277 avctx->channels);
00278 else
00279 chomp6(&ctx->chd[i], output, val[0][l], l,
00280 avctx->channels);
00281
00282 output += avctx->channels << (1-is_mace3);
00283 }
00284 }
00285 }
00286
00287 *got_frame_ptr = 1;
00288 *(AVFrame *)data = ctx->frame;
00289
00290 return buf_size;
00291 }
00292
00293 AVCodec ff_mace3_decoder = {
00294 .name = "mace3",
00295 .type = AVMEDIA_TYPE_AUDIO,
00296 .id = AV_CODEC_ID_MACE3,
00297 .priv_data_size = sizeof(MACEContext),
00298 .init = mace_decode_init,
00299 .decode = mace_decode_frame,
00300 .capabilities = CODEC_CAP_DR1,
00301 .long_name = NULL_IF_CONFIG_SMALL("MACE (Macintosh Audio Compression/Expansion) 3:1"),
00302 };
00303
00304 AVCodec ff_mace6_decoder = {
00305 .name = "mace6",
00306 .type = AVMEDIA_TYPE_AUDIO,
00307 .id = AV_CODEC_ID_MACE6,
00308 .priv_data_size = sizeof(MACEContext),
00309 .init = mace_decode_init,
00310 .decode = mace_decode_frame,
00311 .capabilities = CODEC_CAP_DR1,
00312 .long_name = NULL_IF_CONFIG_SMALL("MACE (Macintosh Audio Compression/Expansion) 6:1"),
00313 };