FFmpeg
mace.c
Go to the documentation of this file.
1 /*
2  * MACE decoder
3  * Copyright (c) 2002 Laszlo Torok <torokl@alpha.dfmk.hu>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * MACE decoder.
25  */
26 
27 #include "avcodec.h"
28 #include "codec_internal.h"
29 #include "decode.h"
30 #include "libavutil/common.h"
31 
32 /*
33  * Adapted to libavcodec by Francois Revol <revol@free.fr>
34  * (removed 68k REG stuff, changed types, added some statics and consts,
35  * libavcodec api, context stuff, interlaced stereo out).
36  */
37 
38 static const int16_t MACEtab1[] = {-13, 8, 76, 222, 222, 76, 8, -13};
39 
40 static const int16_t MACEtab3[] = {-18, 140, 140, -18};
41 
42 static const int16_t MACEtab2[][4] = {
43  { 37, 116, 206, 330}, { 39, 121, 216, 346},
44  { 41, 127, 225, 361}, { 42, 132, 235, 377},
45  { 44, 137, 245, 392}, { 46, 144, 256, 410},
46  { 48, 150, 267, 428}, { 51, 157, 280, 449},
47  { 53, 165, 293, 470}, { 55, 172, 306, 490},
48  { 58, 179, 319, 511}, { 60, 187, 333, 534},
49  { 63, 195, 348, 557}, { 66, 205, 364, 583},
50  { 69, 214, 380, 609}, { 72, 223, 396, 635},
51  { 75, 233, 414, 663}, { 79, 244, 433, 694},
52  { 82, 254, 453, 725}, { 86, 265, 472, 756},
53  { 90, 278, 495, 792}, { 94, 290, 516, 826},
54  { 98, 303, 538, 862}, { 102, 316, 562, 901},
55  { 107, 331, 588, 942}, { 112, 345, 614, 983},
56  { 117, 361, 641, 1027}, { 122, 377, 670, 1074},
57  { 127, 394, 701, 1123}, { 133, 411, 732, 1172},
58  { 139, 430, 764, 1224}, { 145, 449, 799, 1280},
59  { 152, 469, 835, 1337}, { 159, 490, 872, 1397},
60  { 166, 512, 911, 1459}, { 173, 535, 951, 1523},
61  { 181, 558, 993, 1590}, { 189, 584, 1038, 1663},
62  { 197, 610, 1085, 1738}, { 206, 637, 1133, 1815},
63  { 215, 665, 1183, 1895}, { 225, 695, 1237, 1980},
64  { 235, 726, 1291, 2068}, { 246, 759, 1349, 2161},
65  { 257, 792, 1409, 2257}, { 268, 828, 1472, 2357},
66  { 280, 865, 1538, 2463}, { 293, 903, 1606, 2572},
67  { 306, 944, 1678, 2688}, { 319, 986, 1753, 2807},
68  { 334, 1030, 1832, 2933}, { 349, 1076, 1914, 3065},
69  { 364, 1124, 1999, 3202}, { 380, 1174, 2088, 3344},
70  { 398, 1227, 2182, 3494}, { 415, 1281, 2278, 3649},
71  { 434, 1339, 2380, 3811}, { 453, 1398, 2486, 3982},
72  { 473, 1461, 2598, 4160}, { 495, 1526, 2714, 4346},
73  { 517, 1594, 2835, 4540}, { 540, 1665, 2961, 4741},
74  { 564, 1740, 3093, 4953}, { 589, 1818, 3232, 5175},
75  { 615, 1898, 3375, 5405}, { 643, 1984, 3527, 5647},
76  { 671, 2072, 3683, 5898}, { 701, 2164, 3848, 6161},
77  { 733, 2261, 4020, 6438}, { 766, 2362, 4199, 6724},
78  { 800, 2467, 4386, 7024}, { 836, 2578, 4583, 7339},
79  { 873, 2692, 4786, 7664}, { 912, 2813, 5001, 8008},
80  { 952, 2938, 5223, 8364}, { 995, 3070, 5457, 8739},
81  { 1039, 3207, 5701, 9129}, { 1086, 3350, 5956, 9537},
82  { 1134, 3499, 6220, 9960}, { 1185, 3655, 6497, 10404},
83  { 1238, 3818, 6788, 10869}, { 1293, 3989, 7091, 11355},
84  { 1351, 4166, 7407, 11861}, { 1411, 4352, 7738, 12390},
85  { 1474, 4547, 8084, 12946}, { 1540, 4750, 8444, 13522},
86  { 1609, 4962, 8821, 14126}, { 1680, 5183, 9215, 14756},
87  { 1756, 5415, 9626, 15415}, { 1834, 5657, 10057, 16104},
88  { 1916, 5909, 10505, 16822}, { 2001, 6173, 10975, 17574},
89  { 2091, 6448, 11463, 18356}, { 2184, 6736, 11974, 19175},
90  { 2282, 7037, 12510, 20032}, { 2383, 7351, 13068, 20926},
91  { 2490, 7679, 13652, 21861}, { 2601, 8021, 14260, 22834},
92  { 2717, 8380, 14897, 23854}, { 2838, 8753, 15561, 24918},
93  { 2965, 9144, 16256, 26031}, { 3097, 9553, 16982, 27193},
94  { 3236, 9979, 17740, 28407}, { 3380, 10424, 18532, 29675},
95  { 3531, 10890, 19359, 31000}, { 3688, 11375, 20222, 32382},
96  { 3853, 11883, 21125, 32767}, { 4025, 12414, 22069, 32767},
97  { 4205, 12967, 23053, 32767}, { 4392, 13546, 24082, 32767},
98  { 4589, 14151, 25157, 32767}, { 4793, 14783, 26280, 32767},
99  { 5007, 15442, 27452, 32767}, { 5231, 16132, 28678, 32767},
100  { 5464, 16851, 29957, 32767}, { 5708, 17603, 31294, 32767},
101  { 5963, 18389, 32691, 32767}, { 6229, 19210, 32767, 32767},
102  { 6507, 20067, 32767, 32767}, { 6797, 20963, 32767, 32767},
103  { 7101, 21899, 32767, 32767}, { 7418, 22876, 32767, 32767},
104  { 7749, 23897, 32767, 32767}, { 8095, 24964, 32767, 32767},
105  { 8456, 26078, 32767, 32767}, { 8833, 27242, 32767, 32767},
106  { 9228, 28457, 32767, 32767}, { 9639, 29727, 32767, 32767}
107 };
108 
109 static const int16_t MACEtab4[][2] = {
110  { 64, 216}, { 67, 226}, { 70, 236}, { 74, 246},
111  { 77, 257}, { 80, 268}, { 84, 280}, { 88, 294},
112  { 92, 307}, { 96, 321}, { 100, 334}, { 104, 350},
113  { 109, 365}, { 114, 382}, { 119, 399}, { 124, 416},
114  { 130, 434}, { 136, 454}, { 142, 475}, { 148, 495},
115  { 155, 519}, { 162, 541}, { 169, 564}, { 176, 590},
116  { 185, 617}, { 193, 644}, { 201, 673}, { 210, 703},
117  { 220, 735}, { 230, 767}, { 240, 801}, { 251, 838},
118  { 262, 876}, { 274, 914}, { 286, 955}, { 299, 997},
119  { 312, 1041}, { 326, 1089}, { 341, 1138}, { 356, 1188},
120  { 372, 1241}, { 388, 1297}, { 406, 1354}, { 424, 1415},
121  { 443, 1478}, { 462, 1544}, { 483, 1613}, { 505, 1684},
122  { 527, 1760}, { 551, 1838}, { 576, 1921}, { 601, 2007},
123  { 628, 2097}, { 656, 2190}, { 686, 2288}, { 716, 2389},
124  { 748, 2496}, { 781, 2607}, { 816, 2724}, { 853, 2846},
125  { 891, 2973}, { 930, 3104}, { 972, 3243}, { 1016, 3389},
126  { 1061, 3539}, { 1108, 3698}, { 1158, 3862}, { 1209, 4035},
127  { 1264, 4216}, { 1320, 4403}, { 1379, 4599}, { 1441, 4806},
128  { 1505, 5019}, { 1572, 5244}, { 1642, 5477}, { 1715, 5722},
129  { 1792, 5978}, { 1872, 6245}, { 1955, 6522}, { 2043, 6813},
130  { 2134, 7118}, { 2229, 7436}, { 2329, 7767}, { 2432, 8114},
131  { 2541, 8477}, { 2655, 8854}, { 2773, 9250}, { 2897, 9663},
132  { 3026, 10094}, { 3162, 10546}, { 3303, 11016}, { 3450, 11508},
133  { 3604, 12020}, { 3765, 12556}, { 3933, 13118}, { 4108, 13703},
134  { 4292, 14315}, { 4483, 14953}, { 4683, 15621}, { 4892, 16318},
135  { 5111, 17046}, { 5339, 17807}, { 5577, 18602}, { 5826, 19433},
136  { 6086, 20300}, { 6358, 21205}, { 6642, 22152}, { 6938, 23141},
137  { 7248, 24173}, { 7571, 25252}, { 7909, 26380}, { 8262, 27557},
138  { 8631, 28786}, { 9016, 30072}, { 9419, 31413}, { 9839, 32767},
139  { 10278, 32767}, { 10737, 32767}, { 11216, 32767}, { 11717, 32767},
140  { 12240, 32767}, { 12786, 32767}, { 13356, 32767}, { 13953, 32767},
141  { 14576, 32767}, { 15226, 32767}, { 15906, 32767}, { 16615, 32767}
142 };
143 
144 static const struct {
145  const int16_t *tab1; const int16_t *tab2; int stride;
146 } tabs[] = {
147  {MACEtab1, &MACEtab2[0][0], 4},
148  {MACEtab3, &MACEtab4[0][0], 2},
149  {MACEtab1, &MACEtab2[0][0], 4}
150 };
151 
152 #define QT_8S_2_16S(x) (((x) & 0xFF00) | (((x) >> 8) & 0xFF))
153 
154 typedef struct ChannelData {
156 } ChannelData;
157 
158 typedef struct MACEContext {
160 } MACEContext;
161 
162 /**
163  * MACE version of av_clip_int16(). We have to do this to keep binary
164  * identical output to the binary decoder.
165  */
166 static inline int16_t mace_broken_clip_int16(int n)
167 {
168  if (n > 32767)
169  return 32767;
170  else if (n < -32768)
171  return -32767;
172  else
173  return n;
174 }
175 
176 static int16_t read_table(ChannelData *chd, uint8_t val, int tab_idx)
177 {
178  int16_t current;
179 
180  if (val < tabs[tab_idx].stride)
181  current = tabs[tab_idx].tab2[((chd->index & 0x7f0) >> 4) * tabs[tab_idx].stride + val];
182  else
183  current = - 1 - tabs[tab_idx].tab2[((chd->index & 0x7f0) >> 4)*tabs[tab_idx].stride + 2*tabs[tab_idx].stride-val-1];
184 
185  if (( chd->index += tabs[tab_idx].tab1[val]-(chd->index >> 5) ) < 0)
186  chd->index = 0;
187 
188  return current;
189 }
190 
191 static void chomp3(ChannelData *chd, int16_t *output, uint8_t val, int tab_idx)
192 {
193  int16_t current = read_table(chd, val, tab_idx);
194 
195  current = mace_broken_clip_int16(current + chd->level);
196 
197  chd->level = current - (current >> 3);
198  *output = QT_8S_2_16S(current);
199 }
200 
201 static void chomp6(ChannelData *chd, int16_t *output, uint8_t val, int tab_idx)
202 {
203  int16_t current = read_table(chd, val, tab_idx);
204 
205  if ((chd->previous ^ current) >= 0) {
206  chd->factor = FFMIN(chd->factor + 506, 32767);
207  } else {
208  if (chd->factor - 314 < -32768)
209  chd->factor = -32767;
210  else
211  chd->factor -= 314;
212  }
213 
214  current = mace_broken_clip_int16(current + chd->level);
215 
216  chd->level = (current*chd->factor) >> 15;
217  current >>= 1;
218 
219  output[0] = QT_8S_2_16S(chd->previous + chd->prev2 -
220  ((chd->prev2-current) >> 2));
221  output[1] = QT_8S_2_16S(chd->previous + current +
222  ((chd->prev2-current) >> 2));
223  chd->prev2 = chd->previous;
224  chd->previous = current;
225 }
226 
228 {
229  if (avctx->ch_layout.nb_channels > 2 || avctx->ch_layout.nb_channels < 1)
230  return AVERROR(EINVAL);
232 
233  return 0;
234 }
235 
237  int *got_frame_ptr, AVPacket *avpkt)
238 {
239  const uint8_t *buf = avpkt->data;
240  int buf_size = avpkt->size;
241  int channels = avctx->ch_layout.nb_channels;
242  int16_t **samples;
243  MACEContext *ctx = avctx->priv_data;
244  int i, j, k, l, ret;
245  int is_mace3 = (avctx->codec_id == AV_CODEC_ID_MACE3);
246 
247  if (buf_size % (channels << is_mace3)) {
248  av_log(avctx, AV_LOG_ERROR, "buffer size %d is odd\n", buf_size);
249  buf_size -= buf_size % (channels << is_mace3);
250  if (!buf_size)
251  return AVERROR_INVALIDDATA;
252  }
253 
254  /* get output buffer */
255  frame->nb_samples = 3 * (buf_size << (1 - is_mace3)) / channels;
256  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
257  return ret;
258  samples = (int16_t **)frame->extended_data;
259 
260  for(i = 0; i < channels; i++) {
261  int16_t *output = samples[i];
262 
263  for (j=0; j < buf_size / (channels << is_mace3); j++)
264  for (k=0; k < (1 << is_mace3); k++) {
265  uint8_t pkt = buf[(i << is_mace3) +
266  (j * channels << is_mace3) + k];
267 
268  uint8_t val[2][3] = {{pkt >> 5, (pkt >> 3) & 3, pkt & 7 },
269  {pkt & 7 , (pkt >> 3) & 3, pkt >> 5}};
270 
271  for (l=0; l < 3; l++) {
272  if (is_mace3)
273  chomp3(&ctx->chd[i], output, val[1][l], l);
274  else
275  chomp6(&ctx->chd[i], output, val[0][l], l);
276 
277  output += 1 << (1-is_mace3);
278  }
279  }
280  }
281 
282  *got_frame_ptr = 1;
283 
284  return buf_size;
285 }
286 
288  .p.name = "mace3",
289  CODEC_LONG_NAME("MACE (Macintosh Audio Compression/Expansion) 3:1"),
290  .p.type = AVMEDIA_TYPE_AUDIO,
291  .p.id = AV_CODEC_ID_MACE3,
292  .priv_data_size = sizeof(MACEContext),
295  .p.capabilities = AV_CODEC_CAP_DR1,
296  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
298 };
299 
301  .p.name = "mace6",
302  CODEC_LONG_NAME("MACE (Macintosh Audio Compression/Expansion) 6:1"),
303  .p.type = AVMEDIA_TYPE_AUDIO,
304  .p.id = AV_CODEC_ID_MACE6,
305  .priv_data_size = sizeof(MACEContext),
308  .p.capabilities = AV_CODEC_CAP_DR1,
309  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
311 };
chomp6
static void chomp6(ChannelData *chd, int16_t *output, uint8_t val, int tab_idx)
Definition: mace.c:201
AV_CODEC_ID_MACE6
@ AV_CODEC_ID_MACE6
Definition: codec_id.h:456
stride
int stride
Definition: mace.c:145
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
MACEtab4
static const int16_t MACEtab4[][2]
Definition: mace.c:109
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:328
mace_decode_frame
static int mace_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: mace.c:236
mace_broken_clip_int16
static int16_t mace_broken_clip_int16(int n)
MACE version of av_clip_int16().
Definition: mace.c:166
ChannelData::index
int16_t index
Definition: mace.c:155
chomp3
static void chomp3(ChannelData *chd, int16_t *output, uint8_t val, int tab_idx)
Definition: mace.c:191
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1079
ff_mace3_decoder
const FFCodec ff_mace3_decoder
Definition: mace.c:287
ff_mace6_decoder
const FFCodec ff_mace6_decoder
Definition: mace.c:300
ChannelData::prev2
int16_t prev2
Definition: mace.c:155
QT_8S_2_16S
#define QT_8S_2_16S(x)
Definition: mace.c:152
val
static double val(void *priv, double ch)
Definition: aeval.c:77
tab2
const int16_t * tab2
Definition: mace.c:145
tab1
const int16_t * tab1
Definition: mace.c:145
ChannelData
Definition: mace.c:154
MACEtab2
static const int16_t MACEtab2[][4]
Definition: mace.c:42
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AV_CODEC_ID_MACE3
@ AV_CODEC_ID_MACE3
Definition: codec_id.h:455
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:49
channels
channels
Definition: aptx.h:31
decode.h
ChannelData::previous
int16_t previous
Definition: mace.c:155
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
MACEContext
Definition: mace.c:158
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1071
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
mace_decode_init
static av_cold int mace_decode_init(AVCodecContext *avctx)
Definition: mace.c:227
avcodec.h
ChannelData::factor
int16_t factor
Definition: mace.c:155
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ChannelData::level
int16_t level
Definition: mace.c:155
read_table
static int16_t read_table(ChannelData *chd, uint8_t val, int tab_idx)
Definition: mace.c:176
MACEtab1
static const int16_t MACEtab1[]
Definition: mace.c:38
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
MACEContext::chd
ChannelData chd[2]
Definition: mace.c:159
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
tabs
static const struct @167 tabs[]
MACEtab3
static const int16_t MACEtab3[]
Definition: mace.c:40