1 | /********** |
2 | This library is free software; you can redistribute it and/or modify it under |
3 | the terms of the GNU Lesser General Public License as published by the |
4 | Free Software Foundation; either version 3 of the License, or (at your |
5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
6 | |
7 | This library is distributed in the hope that it will be useful, but WITHOUT |
8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
10 | more details. |
11 | |
12 | You should have received a copy of the GNU Lesser General Public License |
13 | along with this library; if not, write to the Free Software Foundation, Inc., |
14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
15 | **********/ |
16 | // "liveMedia" |
17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
18 | // MP3 internal implementation details |
19 | // C++ header |
20 | |
21 | #ifndef _MP3_INTERNALS_HH |
22 | #define _MP3_INTERNALS_HH |
23 | |
24 | #ifndef _BOOLEAN_HH |
25 | #include "Boolean.hh" |
26 | #endif |
27 | #ifndef _BIT_VECTOR_HH |
28 | #include "BitVector.hh" |
29 | #endif |
30 | |
31 | typedef struct MP3SideInfo { |
32 | unsigned main_data_begin; |
33 | unsigned private_bits; |
34 | typedef struct gr_info_s { |
35 | int scfsi; |
36 | unsigned part2_3_length; |
37 | unsigned big_values; |
38 | unsigned global_gain; |
39 | unsigned scalefac_compress; |
40 | unsigned window_switching_flag; |
41 | unsigned block_type; |
42 | unsigned mixed_block_flag; |
43 | unsigned table_select[3]; |
44 | unsigned region0_count; |
45 | unsigned region1_count; |
46 | unsigned subblock_gain[3]; |
47 | unsigned maxband[3]; |
48 | unsigned maxbandl; |
49 | unsigned maxb; |
50 | unsigned region1start; |
51 | unsigned region2start; |
52 | unsigned preflag; |
53 | unsigned scalefac_scale; |
54 | unsigned count1table_select; |
55 | double *full_gain[3]; |
56 | double *pow2gain; |
57 | } gr_info_s_t; |
58 | struct { |
59 | gr_info_s_t gr[2]; |
60 | } ch[2]; |
61 | } MP3SideInfo_t; |
62 | |
63 | #define SBLIMIT 32 |
64 | #define MAX_MP3_FRAME_SIZE 2500 /* also big enough for an 'ADU'ized frame */ |
65 | |
66 | class MP3FrameParams { |
67 | public: |
68 | MP3FrameParams(); |
69 | ~MP3FrameParams(); |
70 | |
71 | // 4-byte MPEG header: |
72 | unsigned hdr; |
73 | |
74 | // a buffer that can be used to hold the rest of the frame: |
75 | unsigned char frameBytes[MAX_MP3_FRAME_SIZE]; |
76 | |
77 | // public parameters derived from the header |
78 | void (); // this sets them |
79 | Boolean isMPEG2; |
80 | unsigned layer; // currently only 3 is supported |
81 | unsigned bitrate; // in kbps |
82 | unsigned samplingFreq; |
83 | Boolean isStereo; |
84 | Boolean isFreeFormat; |
85 | unsigned frameSize; // doesn't include the initial 4-byte header |
86 | unsigned sideInfoSize; |
87 | Boolean hasCRC; |
88 | |
89 | void setBytePointer(unsigned char const* restOfFrame, |
90 | unsigned totNumBytes) {// called during setup |
91 | bv.setup((unsigned char*)restOfFrame, 0, 8*totNumBytes); |
92 | } |
93 | |
94 | // other, public parameters used when parsing input (perhaps get rid of) |
95 | unsigned oldHdr, firstHdr; |
96 | |
97 | // Extract (unpack) the side info from the frame into a struct: |
98 | void getSideInfo(MP3SideInfo& si); |
99 | |
100 | // The bit pointer used for reading data from frame data |
101 | unsigned getBits(unsigned numBits) { return bv.getBits(numBits); } |
102 | unsigned get1Bit() { return bv.get1Bit(); } |
103 | |
104 | private: |
105 | BitVector bv; |
106 | |
107 | // other, private parameters derived from the header |
108 | unsigned bitrateIndex; |
109 | unsigned samplingFreqIndex; |
110 | Boolean isMPEG2_5; |
111 | Boolean padding; |
112 | Boolean extension; |
113 | unsigned mode; |
114 | unsigned mode_ext; |
115 | Boolean copyright; |
116 | Boolean original; |
117 | unsigned emphasis; |
118 | unsigned stereo; |
119 | |
120 | private: |
121 | unsigned computeSideInfoSize(); |
122 | }; |
123 | |
124 | unsigned ComputeFrameSize(unsigned bitrate, unsigned samplingFreq, |
125 | Boolean usePadding, Boolean isMPEG2, |
126 | unsigned char layer); |
127 | |
128 | Boolean GetADUInfoFromMP3Frame(unsigned char const* framePtr, |
129 | unsigned totFrameSize, |
130 | unsigned& hdr, unsigned& frameSize, |
131 | MP3SideInfo& sideInfo, unsigned& sideInfoSize, |
132 | unsigned& backpointer, unsigned& aduSize); |
133 | |
134 | Boolean ZeroOutMP3SideInfo(unsigned char* framePtr, unsigned totFrameSize, |
135 | unsigned newBackpointer); |
136 | |
137 | unsigned TranscodeMP3ADU(unsigned char const* fromPtr, unsigned fromSize, |
138 | unsigned toBitrate, |
139 | unsigned char* toPtr, unsigned toMaxSize, |
140 | unsigned& availableBytesForBackpointer); |
141 | // returns the size of the resulting ADU (0 on failure) |
142 | |
143 | #endif |
144 | |