1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* common.c: some common bitstream operations
*
* Copyright (C) 1999-2010 The L.A.M.E. project
*
* Initially written by Michael Hipp, see also AUTHORS and README.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* $Id: common.c,v 1.42 2017/08/19 14:20:27 robert Exp $ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef macintosh
#include <types.h>
#include <stat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <assert.h>
#include "common.h"
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif
/* In C++ the array first must be prototyped, why ? */
/* *INDENT-OFF* */
const int tabsel_123 [2] [3] [16] = {
{ {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
{ {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
};
const long freqs[9] = { 44100, 48000, 32000,
22050, 24000, 16000,
11025, 12000, 8000 };
/* *INDENT-ON* */
real muls[27][64];
#if 0
static void
get_II_stuff(struct frame *fr)
{
/* *INDENT-OFF* */
static const int translate [3] [2] [16] = /* char ? */
{ { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
{ 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
{ { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
{ 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
{ { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
{ 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
/* *INDENT-ON* */
int table, sblim;
static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
static int sblims[5] = { 27, 30, 8, 12, 30 };
if (fr->lsf)
table = 4;
else
table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
sblim = sblims[table];
fr->alloc = tables[table];
fr->II_sblimit = sblim;
}
#endif
#define HDRCMPMASK 0xfffffd00
#define MAX_INPUT_FRAMESIZE 4096
int
head_check(unsigned long head, int check_layer)
{
/*
look for a valid header.
if check_layer > 0, then require that
nLayer = check_layer.
*/
/* bits 13-14 = layer 3 */
int nLayer = 4 - ((head >> 17) & 3);
if ((head & 0xffe00000) != 0xffe00000) {
/* syncword */
return FALSE;
}
if (nLayer == 4)
return FALSE;
if (check_layer > 0 && nLayer != check_layer)
return FALSE;
if (((head >> 12) & 0xf) == 0xf) {
/* bits 16,17,18,19 = 1111 invalid bitrate */
return FALSE;
}
if (((head >> 10) & 0x3) == 0x3) {
/* bits 20,21 = 11 invalid sampling freq */
return FALSE;
}
if ((head & 0x3) == 0x2)
/* invalid emphasis */
return FALSE;
return TRUE;
}
#if 0
static void
print_header(PMPSTR mp, struct frame *fr)
{
static const char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
static const char *layers[4] = { "Unknown", "I", "II", "III" };
lame_report_fnc(mp->report_msg, "MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
layers[fr->lay], freqs[fr->sampling_frequency],
modes[fr->mode], fr->mode_ext, fr->framesize + 4);
lame_report_fnc(mp->report_msg, "Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
fr->stereo, fr->copyright ? "Yes" : "No",
fr->original ? "Yes" : "No", fr->error_protection ? "Yes" : "No", fr->emphasis);
lame_report_fnc(mp->report_msg, "Bitrate: %d Kbits/s, Extension value: %d\n",
tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], fr->extension);
}
static void
print_header_compact(PMPSTR mp, struct frame *fr)
{
static const char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
static const char *layers[4] = { "Unknown", "I", "II", "III" };
lame_report_fnc(mp->report_err, "MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
layers[fr->lay],
tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index],
freqs[fr->sampling_frequency], modes[fr->mode]);
}
#endif
/*
* decode a header and write the information
* into the frame structure
*/
int
decode_header(PMPSTR mp, struct frame *fr, unsigned long newhead)
{
if (newhead & (1 << 20)) {
fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
fr->mpeg25 = 0;
}
else {
fr->lsf = 1;
fr->mpeg25 = 1;
}
fr->lay = 4 - ((newhead >> 17) & 3);
if (fr->lay != 3 && fr->mpeg25) {
lame_report_fnc(mp->report_err, "MPEG-2.5 is supported by Layer3 only\n");
return 0;
}
if (((newhead >> 10) & 0x3) == 0x3) {
lame_report_fnc(mp->report_err, "Stream error\n");
return 0;
}
if (fr->mpeg25) {
fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
}
else
fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
fr->bitrate_index = ((newhead >> 12) & 0xf);
fr->bitrate_index = ((newhead >> 12) & 0xf);
fr->padding = ((newhead >> 9) & 0x1);
fr->extension = ((newhead >> 8) & 0x1);
fr->mode = ((newhead >> 6) & 0x3);
fr->mode_ext = ((newhead >> 4) & 0x3);
fr->copyright = ((newhead >> 3) & 0x1);
fr->original = ((newhead >> 2) & 0x1);
fr->emphasis = newhead & 0x3;
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
switch (fr->lay) {
case 1:
fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
fr->framesize /= freqs[fr->sampling_frequency];
fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
fr->down_sample = 0;
fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
break;
case 2:
fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
fr->framesize /= freqs[fr->sampling_frequency];
fr->framesize += fr->padding - 4;
fr->down_sample = 0;
fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
break;
case 3:
#if 0
fr->do_layer = do_layer3;
if (fr->lsf)
ssize = (fr->stereo == 1) ? 9 : 17;
else
ssize = (fr->stereo == 1) ? 17 : 32;
#endif
#if 0
if (fr->error_protection)
ssize += 2;
#endif
if (fr->framesize > MAX_INPUT_FRAMESIZE) {
lame_report_fnc(mp->report_err, "Frame size too big.\n");
fr->framesize = MAX_INPUT_FRAMESIZE;
return (0);
}
if (fr->bitrate_index == 0)
fr->framesize = 0;
else {
fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
fr->framesize = fr->framesize + fr->padding - 4;
}
break;
default:
lame_report_fnc(mp->report_err, "Sorry, layer %d not supported\n", fr->lay);
return (0);
}
/* print_header(mp, fr); */
return 1;
}
unsigned int
getbits(PMPSTR mp, int number_of_bits)
{
unsigned long rval;
if (number_of_bits <= 0 || !mp->wordpointer)
return 0;
{
rval = mp->wordpointer[0];
rval <<= 8;
rval |= mp->wordpointer[1];
rval <<= 8;
rval |= mp->wordpointer[2];
rval <<= mp->bitindex;
rval &= 0xffffff;
mp->bitindex += number_of_bits;
rval >>= (24 - number_of_bits);
mp->wordpointer += (mp->bitindex >> 3);
mp->bitindex &= 7;
}
return rval;
}
unsigned int
getbits_fast(PMPSTR mp, int number_of_bits)
{
unsigned long rval;
{
rval = mp->wordpointer[0];
rval <<= 8;
rval |= mp->wordpointer[1];
rval <<= mp->bitindex;
rval &= 0xffff;
mp->bitindex += number_of_bits;
rval >>= (16 - number_of_bits);
mp->wordpointer += (mp->bitindex >> 3);
mp->bitindex &= 7;
}
return rval;
}
unsigned char
get_leq_8_bits(PMPSTR mp, unsigned int number_of_bits)
{
assert(number_of_bits <= 8);
return (unsigned char) getbits_fast(mp, number_of_bits);
}
unsigned short
get_leq_16_bits(PMPSTR mp, unsigned int number_of_bits)
{
assert(number_of_bits <= 16);
return (unsigned short) getbits_fast(mp, number_of_bits);
}
int
set_pointer(PMPSTR mp, long backstep)
{
unsigned char *bsbufold;
if (mp->fsizeold < 0 && backstep > 0) {
lame_report_fnc(mp->report_err, "hip: Can't step back %ld bytes!\n", backstep);
return MP3_ERR;
}
bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
mp->wordpointer -= backstep;
if (backstep)
memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);
mp->bitindex = 0;
return MP3_OK;
}
|