1 /*
2  *   mp3guessenc header
3  *   Copyright (C) 2002-2010 Naoki Shibata
4  *   Copyright (C) 2011-2018 Elio Blanca <eblanca76@users.sourceforge.net>
5  *
6  * Xing VBR tagging for LAME.
7  * Copyright (c) 1999 A.L. Faber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 /* Modifed by Evan Dekker 2019-09-26 */
25 
26 #ifndef MP3GUESSENC_H
27 #define MP3GUESSENC_H
28 
29 #include "tags.h"
30 
31 #define EXIT_CODE_NO_ERROR                  0
32 #define EXIT_CODE_UNKNOWN_OPTION           -1
33 #define EXIT_CODE_STAT_ERROR               -2
34 #define EXIT_CODE_INVALID_FILE             -3
35 #define EXIT_CODE_ACCESS_ERROR             -4
36 #define EXIT_CODE_NO_MPEG_AUDIO            -5
37 #define EXIT_CODE_CANNOT_HANDLE            -6
38 
39 #define EXIT_CODE_CASE_A                    1
40 #define EXIT_CODE_CASE_B                    2
41 #define EXIT_CODE_CASE_C                    3
42 #define EXIT_CODE_CASE_D                    4
43 
44 #define HEADER_FIELD_SYNC          0xFFE00000
45 #define HEADER_FIELD_SYNC_SHIFT            21
46 #define HEADER_FIELD_MPEG_ID         0x180000
47 #define HEADER_FIELD_MPEG_ID_SHIFT         19
48 #define HEADER_FIELD_LSF              0x80000 /* this is not a *real* field, it is just a commodity for accessing lsf bit - 1:mpeg1, 0:mpeg2/2.5*/
49 #define HEADER_FIELD_LSF_SHIFT             19
50 #define HEADER_FIELD_LAYER            0x60000
51 #define HEADER_FIELD_LAYER_SHIFT           17
52 #define HEADER_FIELD_CRC              0x10000
53 #define HEADER_FIELD_CRC_SHIFT             16
54 #define HEADER_FIELD_BITRATE           0xF000
55 #define HEADER_FIELD_BITRATE_SHIFT         12
56 #define HEADER_FIELD_SAMPRATE           0xC00
57 #define HEADER_FIELD_SAMPRATE_SHIFT        10
58 #define HEADER_FIELD_PADDING            0x200
59 #define HEADER_FIELD_PADDING_SHIFT          9
60 #define HEADER_FIELD_PRIVATE            0x100
61 #define HEADER_FIELD_PRIVATE_SHIFT          8
62 #define HEADER_FIELD_CHANNELS            0xC0
63 #define HEADER_FIELD_CHANNELS_SHIFT         6
64 #define HEADER_FIELD_MODEXT              0x30
65 #define HEADER_FIELD_MODEXT_SHIFT           4
66 #define HEADER_FIELD_COPYRIGHT            0x8
67 #define HEADER_FIELD_COPYRIGHT_SHIFT        3
68 #define HEADER_FIELD_ORIGINAL             0x4
69 #define HEADER_FIELD_ORIGINAL_SHIFT         2
70 #define HEADER_FIELD_EMPHASIS             0x3
71 #define HEADER_FIELD_EMPHASIS_SHIFT         0
72 
73 #define HEADER_CONSTANT_FIELDS_STRICT ( \
74         HEADER_FIELD_SYNC |      \
75         HEADER_FIELD_MPEG_ID |   \
76         HEADER_FIELD_LAYER |     \
77         HEADER_FIELD_CRC |       \
78         HEADER_FIELD_CHANNELS |  \
79         HEADER_FIELD_SAMPRATE |  \
80         HEADER_FIELD_COPYRIGHT | \
81         HEADER_FIELD_ORIGINAL |  \
82         HEADER_FIELD_EMPHASIS)
83 
84 #define HEADER_CONSTANT_FIELDS ( \
85         HEADER_FIELD_SYNC |      \
86         HEADER_FIELD_MPEG_ID |   \
87         HEADER_FIELD_LAYER |     \
88         HEADER_FIELD_CRC |       \
89         HEADER_FIELD_SAMPRATE |  \
90         HEADER_FIELD_COPYRIGHT | \
91         HEADER_FIELD_ORIGINAL |  \
92         HEADER_FIELD_EMPHASIS)
93 
94 #define HEADER_ANY_BUT_BITRATE_AND_PADDING_FIELDS ( \
95         HEADER_FIELD_SYNC |      \
96         HEADER_FIELD_MPEG_ID |   \
97         HEADER_FIELD_LAYER |     \
98         HEADER_FIELD_CRC |       \
99         HEADER_FIELD_SAMPRATE |  \
100         HEADER_FIELD_PRIVATE |   \
101         HEADER_FIELD_CHANNELS |  \
102         HEADER_FIELD_MODEXT |    \
103         HEADER_FIELD_COPYRIGHT | \
104         HEADER_FIELD_ORIGINAL |  \
105         HEADER_FIELD_EMPHASIS)
106 
107 
108 #define VERBOSE_FLAG_UNTOUCHED      0x00010000
109 #define VERBOSE_FLAG_GUESSING       0x00000001
110 #define VERBOSE_FLAG_ENC_STRING     0x00000002
111 #define VERBOSE_FLAG_HISTOGRAM      0x00000004
112 #define VERBOSE_FLAG_ADVANCED_BITS  0x00000008
113 #define VERBOSE_FLAG_STREAM_DETAILS 0x00000010
114 #define VERBOSE_FLAG_VBR_TAG        0x00000020
115 #define VERBOSE_FLAG_METADATA       0x00000040
116 #define VERBOSE_FLAG_PROGRESS       0x00000080
117 
118 #define VERBOSE_FLAG_SHOW_EVERYTHING (VERBOSE_FLAG_GUESSING | \
119                                       VERBOSE_FLAG_ENC_STRING | \
120                                       VERBOSE_FLAG_HISTOGRAM | \
121                                       VERBOSE_FLAG_ADVANCED_BITS | \
122                                       VERBOSE_FLAG_STREAM_DETAILS | \
123                                       VERBOSE_FLAG_VBR_TAG | \
124                                       VERBOSE_FLAG_METADATA | \
125                                       VERBOSE_FLAG_PROGRESS)
126 
127 #define OPERATIONAL_FLAG_DETECT_MC_IN_ALL_LAYERS         0x80
128 #define OPERATIONAL_FLAG_VERIFY_MUSIC_CRC                0x40
129 
130 #define SCAN_SIZE 65536  /* default scan length is 64 kB when searching for a valid frame */
131 
132 #define LAYER_CODE_RESERVED      0
133 #define LAYER_CODE_L_III         1
134 #define LAYER_CODE_L__II         2
135 #define LAYER_CODE_L___I         3
136 
137 #define BITRATE_INDEX_FREEFORMAT               0
138 #define BITRATE_INDEX_RESERVED                15
139 
140 #define BITRATE_MAX_ALLOWED_FREEFORMAT     640.0
141 
142 #define LARGEST_BUFFER                 (10*1024)   /* ensure this is larger than LARGEST_FRAME */
143 
144 
145 typedef struct detectionInfo {
146     int     blockCount[4];
147     int     modeCount[5];
148     int     freq;
149     off_t   ancillaryData;
150     char    usesScfsi;         /* this must be set to 0 */
151     char    usesScalefacScale; /* this must be set to 0 */
152     char    usesPadding;       /* this must be set to 0 */
153     char    vbr_stream;
154     char    vbr_tag;
155     char    lame_tag;
156     char    mm_tag;
157     char    frameCountDiffers;
158     char    ancillaryOnlyIntoLastFrame;
159     unsigned char   lay;       /* layer */
160     unsigned char   id;        /* mpeg id (1/2/2.5) */
161     unsigned char   mode;
162     char    encoder_string[LAME_STRING_LENGTH];
163 /*
164  * mp3PRO detection
165  * ----------------
166  * The mp3PRO detection method searchs for a "signature" the encoder puts into its audio data.
167  * The encoder stores higher frequency information "hidden" into ancillary data of layerIII
168  * streams while creating mpeg2 streams only. These infos are splitted into small packets (min
169  * 6 bytes) and, given the first two bytes (big endian), they always show bits 15, 14 and 3 set.
170  * Having said that, identification of the encoder is straightforward, since Fraunhofer IIS is
171  * the patent owner and the only one providing encoders for mp3PRO.
172  */
173 
174 /*
175  * The detection for mp3Surround (5.1 stream into a backward compatible mpeg1 layerIII stream)
176  * works the same way, just the signature is obviously different.
177  */
178     unsigned char enhSignature[2]; /* these bytes of enhanced mp3 features must be initialized to 0xFF */
179     char    ofl;
180 } detectionInfo;
181 
182 typedef struct frameCounterCell {
183     int frameSize;
184     int frameCount;
185 } frameCounterCell;
186 
187 typedef struct mpegMultiChannel {
188     unsigned char mc_stream_verified;
189     unsigned char extension;
190     unsigned char lfe;
191     unsigned char mc_channels;
192     unsigned char multi_lingual;
193     unsigned char multi_lingual_fs;
194     unsigned char multi_lingual_layer;
195     unsigned char configuration_value;
196 } mpegMultiChannel;
197 
198 typedef struct streamInfo {
199     off_t           filesize;
200     off_t           totFrameLen;  /* this must be set to 0 */
201     unsigned int    totFrameNum;  /* this must be set to 0 */
202     unsigned char   lsf;
203     char            crc_present;
204     char            crc_checked;
205     char            copyright;
206     char            original;
207     char            emphasis;
208     /* the following two fields are written by recent fhg encoders and then hidden into
209        ancillary bits - they differ from similar information taken from lame vbr tag */
210     unsigned short  ofl_encDelay;
211     unsigned int    ofl_orig_samples;
212     int             reservoirMax; /* this must be set to 0 */
213     int             bitrateCount[15];
214     frameCounterCell    unpadded[15];
215     frameCounterCell    padded[15];
216     int             nSyncError;
217     int             ancill_min;
218     int             ancill_max;
219     unsigned char   min_global_gain[2];
220     unsigned char   max_global_gain[2];
221     char            freeformat;   /* this must be set to 0 */
222     mpegMultiChannel    mc;
223     unsigned int    mc_verified_frames;
224     unsigned int    mc_coherent_frames;
225     unsigned short  musicCRC;
226 } streamInfo;
227 
228 typedef struct currentFrame {
229     char            maybe_garbage;
230     char            uncertain;
231     char            bitrate_index;
232     char            usesScalefacScale;
233     unsigned char   index;                 /* sub band quantization table index, used in layerII */
234     char            bound;                 /* sub band limit, used in layerI & II */
235     int             expected_framesize;    /* bytes */
236     int             main_data_begin;       /* backpointer - bytes */
237     char            usesScfsi[2];
238     unsigned short  crc;
239     unsigned char   min_global_gain[2];
240     unsigned char   max_global_gain[2];
241     int             blockCount[4];
242     int             part1_length;          /* bits - length up to the last crc-covered bit */
243     int             part2_3_length;        /* bits - remaining audio bits */
244     unsigned char   allocation[5][32];     /* bit allocation data used in both layerI and II */
245     unsigned char   scfsi[2][30];          /* scale factor selection info - used in layerII */
246 } currentFrame;
247 
248 
249 unsigned short crc_reflected_update(unsigned short, unsigned char *, unsigned int);
250 unsigned char reflect_byte(unsigned char);
251 
252 #ifdef __cplusplus
253 extern "C" {
254 #endif
255 int mp3guessenc_timing_shift_case(const char *);
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif
261 
262