Overte C++ Documentation
flump3dec.h
1 /*
2  * FLUENDO S.A.
3  * Copyright (C) <2005 - 2011> <support@fluendo.com>
4  *
5  * This Source Code is licensed under MIT license and the explanations attached
6  * in MIT License Statements.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of
9  * this software and associated documentation files (the "Software"), to deal in
10  * the Software without restriction, including without limitation the rights to
11  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is furnished to do
13  * so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all
16  * copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  * MIT license Statements for Fluendo's mp3 plug-in Source Code
27  * ------------------------------------------------------------
28  *
29  * Fluendo's mp3 software Source Code (the "Source Code") is licensed under the
30  * MIT license provisions.
31  *
32  * The MIT license is an open source license that permits the User to operate and
33  * use in many forms the Source Code, which would be governed under its
34  * regulations.
35  *
36  * The purpose of this note is to clarify the intellectual property rights granted
37  * over the Source Code by Fluendo, as well as other legal issues that concern
38  * your use of it.
39  *
40  * MIT license contents and provisions
41  * -----------------------------------
42  *
43  * The MIT license allows you to do the following things with the Source Code:
44  *
45  * - Copy and use the Source Code alone or jointly with other code for any
46  * purposes.
47  * Copy of the Source Code is not limited and is royalty-free.
48  *
49  * - Merge the Source Code with other code for developing new applications with no
50  * limits.
51  *
52  * - Modifying the Source Code for developing the plug-in or for implementing the
53  * plug-in in other applications for any purposes. The MIT License does not
54  * require you to share these modifications with anyone.
55  *
56  * - Publish, distribute, sublicense and sell copies of the Source Code to third
57  * parties.
58  *
59  * - Permit anyone to whom the Source Code is licensed to enjoy the rights above
60  * subject to the MIT license provisions.
61  *
62  * By licensing this Source Code under the MIT License, Fluendo is offering to the
63  * community the rights set out above without restriction and without any
64  * obligation for the User of the Source Code to release his/her modifications
65  * back to the community. Anyone operating with the Source Code released from
66  * Fluendo must grant the same MIT license rights to the community, except for any
67  * modifications operated on the Source Code which can be granted under a
68  * different license (even a proprietary license).
69  *
70  * All these rights granted to the User for the Source Code hold a limitation
71  * which is to include MIT permission notice and the following copyright notice:
72  * "Copyright 2005 Fluendo, S.L. This Source Code is licensed under MIT license
73  * and the explanations attached in MIT License Statements". These notices shall
74  * be included in all copies of the Source Code or in substantial parts of the
75  * Source Code which may be released separately or with modifications.
76  *
77  * Patents over the plug-in and/or Source Code
78  * -------------------------------------------
79  *
80  * The binaries that can be created by compiling this Source Code released by
81  * Fluendo might be covered by patents in various parts of the world. Fluendo
82  * does not own or claim to own any patents on the techniques used in the code.
83  * (Such patents are owned or claimed to be owned by Thompson Licensing, S.A. and
84  * some other entities as the case may be).
85  *
86  * Fluendo has got the relevant licenses to cover its own activities with the
87  * Source Code but it is not authorized to sublicense nor to grant the rights
88  * which it has acquired over the patents. In this sense, you can work and deal
89  * freely with the Source Code under MIT provisions set out above, bearing in mind
90  * that some activities might not be allowed under applicable patent regulations
91  * and that Fluendo is not granting any rights in relation to such patents.
92  *
93  * The patent license granted to Fluendo only covers Fluendo's own Software and
94  * Source Code activities. In any case, this software license does not allow you
95  * to redistribute or copy complete, ready to use mp3 software decoder binaries
96  * made from the Source Code as made available by Fluendo. You can of course
97  * distribute binaries you make yourself under any terms allowed by the MIT
98  * license and whatever necessary rights you have or have acquired according to
99  * applicable patent regulations.
100  *
101  * As Fluendo can not assure that any of the activities you undertake do not
102  * infringe any patents or other industrial or intellectual property rights,
103  * Fluendo hereby disclaims any liability for any patent infringement that may be
104  * claimed to you or to any other person from any legitimate right’s owner, as
105  * stated in MIT license. So it is your responsibility to get information and to
106  * acquire the necessary patent licenses to undertake your activities legally.
107  */
108 
109 //
110 // Modifications and bug fixes copyright 2018 High Fidelity, Inc.
111 // Now passes ISO/IEC 11172-4 "full accuracy" compliance testing.
112 //
113 
114 #ifndef __FLUMP3DEC_H__
115 #define __FLUMP3DEC_H__
116 
117 #include <stdint.h>
118 #include <string.h>
119 #include <assert.h>
120 
121 #if 0
122 #include <stdio.h>
123 #define G_GINT64_FORMAT "lld"
124 #define G_GUINT64_FORMAT "llu"
125 
126 #define GST_LOG(f, ...) do { printf(f "\n", __VA_ARGS__); } while (0)
127 #define GST_DEBUG(f, ...) do { printf(f "\n", __VA_ARGS__); } while (0)
128 #define GST_WARNING(f, ...) do { printf(f "\n", __VA_ARGS__); } while (0)
129 #else
130 #define GST_LOG(f, ...) do {} while (0)
131 #define GST_DEBUG(f, ...) do {} while (0)
132 #define GST_WARNING(f, ...) do {} while (0)
133 #endif
134 
135 #ifndef TRUE
136 #define TRUE 1
137 #endif
138 
139 #ifndef FALSE
140 #define FALSE 0
141 #endif
142 
143 #ifndef MIN
144 #define MIN(a, b) ((a) < (b) ? (a) : (b))
145 #endif
146 
147 #define g_assert(cond) assert(cond)
148 #define g_return_if_fail(cond) { if (!(cond)) return; }
149 #define g_return_val_if_fail(cond, val) { if (!(cond)) return (val); }
150 
151 namespace flump3dec {
152 
153 typedef char gchar;
154 typedef unsigned char guchar;
155 typedef int gint;
156 typedef unsigned int guint;
157 typedef float gfloat;
158 typedef double gdouble;
159 typedef int gboolean;
160 typedef size_t gsize;
161 
162 typedef int8_t gint8;
163 typedef uint8_t guint8;
164 typedef int16_t gint16;
165 typedef uint16_t guint16;
166 typedef int32_t gint32;
167 typedef uint32_t guint32;
168 typedef int64_t gint64;
169 typedef uint64_t guint64;
170 
171 /* Accumulator optimization on bitstream management */
172 #define ENABLE_OPT_BS 1
173 
174 /* Bit stream reader definitions */
175 #define MAX_LENGTH 32 /* Maximum length of word written or
176  read from bit stream */
177 #define BS_BYTE_SIZE 8
178 
179 #if ENABLE_OPT_BS
180 #define BS_ACUM_SIZE 32
181 #else
182 #define BS_ACUM_SIZE 8
183 #endif
184 
185 typedef struct BSReader
186 {
187  guint64 bitpos; /* Number of bits read so far */
188 
189  gsize size; /* Number of bytes in the buffer list */
190  const guint8 *data; /* Current data buffer */
191  guint8 *cur_byte; /* ptr to the current byte */
192  guint8 cur_bit; /* the next bit to be used in the current byte,
193  * numbered from 8 down to 1 */
194  gsize cur_used; /* Number of bytes _completely_ consumed out of
195  * the 'cur buffer' */
196 } BSReader;
197 
198 typedef struct Bit_stream_struc
199 {
200  BSReader master; /* Master tracking position, advanced
201  * by bs_consume() */
202  BSReader read; /* Current read position, set back to the
203  * master by bs_reset() */
204 } Bit_stream_struc;
205 
206 /* Create and initialise a new bitstream reader */
207 Bit_stream_struc *bs_new ();
208 
209 /* Release a bitstream reader */
210 void bs_free (Bit_stream_struc * bs);
211 
212 /* Reset the current read position to the master position */
213 static inline void
214 bs_reset (Bit_stream_struc * bs)
215 {
216  memcpy (&bs->read, &bs->master, sizeof (BSReader));
217 }
218 
219 /* Reset master and read states */
220 static inline void
221 bs_flush (Bit_stream_struc * bs)
222 {
223  g_return_if_fail (bs != NULL);
224 
225  bs->master.cur_bit = 8;
226  bs->master.size = 0;
227  bs->master.cur_used = 0;
228  bs->master.cur_byte = NULL;
229  bs->master.data = NULL;
230  bs->master.bitpos = 0;
231 
232  bs_reset (bs);
233 }
234 
235 /* Set data as the stream for processing */
236 gboolean bs_set_data (Bit_stream_struc * bs, const guint8 * data, gsize size);
237 
238 /* Advance the master position by Nbits */
239 void bs_consume (Bit_stream_struc * bs, guint32 Nbits);
240 
241 /* Number of bits available for reading */
242 static inline gsize bs_bits_avail (Bit_stream_struc * bs)
243 {
244  return ((bs->read.size - bs->read.cur_used) * 8 + (bs->read.cur_bit - 8));
245 }
246 
247 /* Extract N bytes from the bitstream into the out array. */
248 void bs_getbytes (Bit_stream_struc * bs, guint8 * out, guint32 N);
249 
250 /* Advance the read pointer by N bits */
251 void bs_skipbits (Bit_stream_struc * bs, guint32 N);
252 
253 /* give number of consumed bytes */
254 static inline gsize bs_get_consumed (Bit_stream_struc * bs)
255 {
256  return bs->master.cur_used;
257 }
258 
259 /* Current bitstream position in bits */
260 static inline guint64
261 bs_pos (Bit_stream_struc * bs)
262 {
263  return bs->master.bitpos;
264 }
265 
266 /* Current read bitstream position in bits */
267 static inline guint64
268 bs_read_pos (Bit_stream_struc * bs)
269 {
270  return bs->read.bitpos;
271 }
272 
273 /* Advances the read position to the first bit of next frame or
274  * last byte in the buffer when the sync code is not found */
275 gboolean bs_seek_sync (Bit_stream_struc * bs);
276 
277 /* Read N bits from the stream */
278 /* bs - bit stream structure */
279 /* N - number of bits to read from the bit stream */
280 /* v - output value */
281 static inline guint32
282 bs_getbits (Bit_stream_struc * bs, guint32 N)
283 {
284  guint32 val = 0;
285  gint j = N;
286 
287  g_assert (N <= MAX_LENGTH);
288 
289  while (j > 0) {
290  gint tmp;
291  gint k;
292  gint mask;
293 
294  /* Move to the next byte if we consumed the current one */
295  if (bs->read.cur_bit == 0) {
296  bs->read.cur_bit = 8;
297  bs->read.cur_used++;
298  bs->read.cur_byte++;
299  }
300 
301  /* Protect against data limit */
302  if ((bs->read.cur_used >= bs->read.size)) {
303  GST_WARNING ("Attempted to read beyond data");
304  /* Return the bits we got so far */
305  return val;
306  }
307  /* Take as many bits as we can from the current byte */
308  k = MIN (j, bs->read.cur_bit);
309 
310  /* We want the k bits from the current byte, starting from
311  * the cur_bit. Mask out the top 'already used' bits, then shift
312  * the bits we want down to the bottom */
313  mask = (1 << bs->read.cur_bit) - 1;
314  tmp = bs->read.cur_byte[0] & mask;
315 
316  /* Trim off the bits we're leaving for next time */
317  tmp = tmp >> (bs->read.cur_bit - k);
318 
319  /* Adjust our tracking vars */
320  bs->read.cur_bit -= k;
321  j -= k;
322  bs->read.bitpos += k;
323 
324  /* Put these bits in the right spot in the output */
325  val |= tmp << j;
326  }
327 
328  return val;
329 }
330 
331 /* Read 1 bit from the stream */
332 static inline guint32
333 bs_get1bit (Bit_stream_struc * bs)
334 {
335  return bs_getbits (bs, 1);
336 }
337 
338 /* read the next byte aligned N bits from the bit stream */
339 static inline guint32
340 bs_getbits_aligned (Bit_stream_struc * bs, guint32 N)
341 {
342  guint32 align;
343 
344  align = bs->read.cur_bit;
345  if (align != 8 && align != 0)
346  bs_getbits (bs, align);
347 
348  return bs_getbits (bs, N);
349 }
350 
351 /* MPEG Header Definitions - ID Bit Values */
352 #define MPEG_VERSION_1 0x03
353 #define MPEG_VERSION_2 0x02
354 #define MPEG_VERSION_2_5 0x00
355 
356 /* Header Information Structure */
357 typedef struct
358 {
359  /* Stuff read straight from the MPEG header */
360  guint version;
361  guint layer;
362  gboolean error_protection;
363 
364  gint bitrate_idx; /* Index into the bitrate tables */
365  guint srate_idx; /* Index into the sample rate table */
366 
367  gboolean padding;
368  gboolean extension;
369  guint mode;
370  guint mode_ext;
371  gboolean copyright;
372  gboolean original;
373  guint emphasis;
374 
375  /* Derived attributes */
376  guint bitrate; /* Bitrate of the frame, kbps */
377  guint sample_rate; /* sample rate in Hz */
378  guint sample_size; /* in bits */
379  guint frame_samples; /* Number of samples per channels in this
380  frame */
381  guint channels; /* Number of channels in the frame */
382 
383  guint bits_per_slot; /* Number of bits per slot */
384  guint frame_slots; /* Total number of data slots in this frame */
385  guint main_slots; /* Slots of main data in this frame */
386  guint frame_bits; /* Number of bits in the frame, including header
387  and sync word */
388  guint side_info_slots; /* Number of slots of side info in the frame */
389 } fr_header;
390 
391 typedef struct mp3tl mp3tl;
392 typedef enum
393 {
394  MP3TL_ERR_OK = 0, /* Successful return code */
395  MP3TL_ERR_NO_SYNC, /* There was no sync word in the data buffer */
396  MP3TL_ERR_NEED_DATA, /* Not enough data in the buffer for the requested op */
397  MP3TL_ERR_BAD_FRAME, /* The frame data was corrupt and skipped */
398  MP3TL_ERR_STREAM, /* Encountered invalid data in the stream */
399  MP3TL_ERR_UNSUPPORTED_STREAM, /* Encountered valid but unplayable data in
400  * the stream */
401  MP3TL_ERR_PARAM, /* Invalid parameter was passed in */
402  MP3TL_ERR_UNKNOWN /* Unspecified internal decoder error (bug) */
403 } Mp3TlRetcode;
404 
405 typedef enum
406 {
407  MP3TL_MODE_16BIT = 0 /* Decoder mode to use */
408 } Mp3TlMode;
409 
410 mp3tl *mp3tl_new (Bit_stream_struc * bs, Mp3TlMode mode);
411 
412 void mp3tl_free (mp3tl * tl);
413 
414 void mp3tl_set_eos (mp3tl * tl, gboolean more_data);
415 Mp3TlRetcode mp3tl_sync (mp3tl * tl);
416 Mp3TlRetcode mp3tl_gather_frame (mp3tl * tl, guint64 * _offset, gint * _length);
417 Mp3TlRetcode mp3tl_decode_header (mp3tl * tl, const fr_header ** ret_hdr);
418 Mp3TlRetcode mp3tl_skip_frame (mp3tl * tl);
419 Mp3TlRetcode mp3tl_decode_frame (mp3tl * tl, guint8 * samples, guint bufsize);
420 const char *mp3tl_get_err_reason (mp3tl * tl);
421 void mp3tl_flush (mp3tl * tl);
422 
423 Mp3TlRetcode mp3tl_skip_id3 (mp3tl * tl);
424 Mp3TlRetcode mp3tl_skip_xing (mp3tl * tl, const fr_header * hdr);
425 
426 } // namespace flump3dec
427 
428 #endif //__FLUMP3DEC_H__