| 1 |
/* |
| 2 |
* TwoLAME: an optimized MPEG Audio Layer Two encoder |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2004 Michael Cheng |
| 5 |
* Copyright (C) 2004-2018 The TwoLAME Project |
| 6 |
* |
| 7 |
* This library 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 |
* This library 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 this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <math.h> |
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
#include "twolame.h" |
| 30 |
#include "bitbuffer.h" |
| 31 |
#include "common.h" |
| 32 |
#include "mem.h" |
| 33 |
#include "util.h" |
| 34 |
#include "energy.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/*********************************************/ |
| 39 |
|
| 40 |
int twolame_set_mode(twolame_options * glopts, TWOLAME_MPEG_mode mode) |
| 41 |
{ |
| 42 |
if (mode < TWOLAME_AUTO_MODE || mode > TWOLAME_MONO) { |
| 43 |
fprintf(stderr, "invalid mode %i\n", mode); |
| 44 |
return (-1); |
| 45 |
} |
| 46 |
glopts->mode = mode; |
| 47 |
return (0); |
| 48 |
} |
| 49 |
|
| 50 |
TWOLAME_MPEG_mode twolame_get_mode(twolame_options * glopts) |
| 51 |
{ |
| 52 |
return (glopts->mode); |
| 53 |
} |
| 54 |
|
| 55 |
const char *twolame_get_mode_name(twolame_options * glopts) |
| 56 |
{ |
| 57 |
static const char *mode_name[6] = |
| 58 |
{ "Auto", "Stereo", "J-Stereo", "Dual-Channel", "Mono", "Illegal Mode" }; |
| 59 |
int mode = glopts->mode; |
| 60 |
if (mode >= TWOLAME_AUTO_MODE && mode <= TWOLAME_MONO) |
| 61 |
return (mode_name[mode + 1]); |
| 62 |
else |
| 63 |
return (mode_name[5]); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
int twolame_set_psymodel(twolame_options * glopts, int psymodel) |
| 68 |
{ |
| 69 |
glopts->psymodel = psymodel; |
| 70 |
return (0); |
| 71 |
} |
| 72 |
|
| 73 |
int twolame_get_psymodel(twolame_options * glopts) |
| 74 |
{ |
| 75 |
return (glopts->psymodel); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/* number of channels on the input stream */ |
| 80 |
int twolame_set_num_channels(twolame_options * glopts, int num_channels) |
| 81 |
{ |
| 82 |
glopts->num_channels_in = num_channels; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
int twolame_get_num_channels(twolame_options * glopts) |
| 87 |
{ |
| 88 |
return (glopts->num_channels_in); |
| 89 |
} |
| 90 |
|
| 91 |
int twolame_set_scale(twolame_options * glopts, float scale) |
| 92 |
{ |
| 93 |
if (scale < 0) { |
| 94 |
fprintf(stderr, "invalid scaling amount %f\n", scale); |
| 95 |
return (-1); |
| 96 |
} |
| 97 |
glopts->scale = scale; |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
float twolame_get_scale(twolame_options * glopts) |
| 102 |
{ |
| 103 |
return (glopts->scale); |
| 104 |
} |
| 105 |
|
| 106 |
int twolame_set_scale_left(twolame_options * glopts, float scale) |
| 107 |
{ |
| 108 |
if (scale < 0) { |
| 109 |
fprintf(stderr, "invalid scaling amount %f\n", scale); |
| 110 |
return (-1); |
| 111 |
} |
| 112 |
glopts->scale_left = scale; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
float twolame_get_scale_left(twolame_options * glopts) |
| 117 |
{ |
| 118 |
return (glopts->scale_left); |
| 119 |
} |
| 120 |
|
| 121 |
int twolame_set_scale_right(twolame_options * glopts, float scale) |
| 122 |
{ |
| 123 |
if (scale < 0) { |
| 124 |
fprintf(stderr, "invalid scaling amount %f\n", scale); |
| 125 |
return (-1); |
| 126 |
} |
| 127 |
glopts->scale_right = scale; |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
float twolame_get_scale_right(twolame_options * glopts) |
| 132 |
{ |
| 133 |
return (glopts->scale_right); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
int twolame_set_in_samplerate(twolame_options * glopts, int samplerate) |
| 138 |
{ |
| 139 |
glopts->samplerate_in = samplerate; |
| 140 |
return (0); |
| 141 |
} |
| 142 |
|
| 143 |
int twolame_get_in_samplerate(twolame_options * glopts) |
| 144 |
{ |
| 145 |
return (glopts->samplerate_in); |
| 146 |
} |
| 147 |
|
| 148 |
int twolame_set_out_samplerate(twolame_options * glopts, int samplerate) |
| 149 |
{ |
| 150 |
glopts->samplerate_out = samplerate; |
| 151 |
return (0); |
| 152 |
} |
| 153 |
|
| 154 |
int twolame_get_out_samplerate(twolame_options * glopts) |
| 155 |
{ |
| 156 |
return (glopts->samplerate_out); |
| 157 |
} |
| 158 |
|
| 159 |
int twolame_set_brate(twolame_options * glopts, int bitrate) |
| 160 |
{ |
| 161 |
glopts->bitrate = bitrate; |
| 162 |
return (0); |
| 163 |
} |
| 164 |
|
| 165 |
int twolame_get_brate(twolame_options * glopts) |
| 166 |
{ |
| 167 |
return (glopts->bitrate); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/* Because the LAME API isn't always the best way ;) */ |
| 172 |
int twolame_set_bitrate(twolame_options * glopts, int bitrate) |
| 173 |
{ |
| 174 |
glopts->bitrate = bitrate; |
| 175 |
return (0); |
| 176 |
} |
| 177 |
|
| 178 |
int twolame_get_bitrate(twolame_options * glopts) |
| 179 |
{ |
| 180 |
return (glopts->bitrate); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
int twolame_set_emphasis(twolame_options * glopts, TWOLAME_Emphasis emphasis) |
| 185 |
{ |
| 186 |
if (emphasis != TWOLAME_EMPHASIS_N && |
| 187 |
emphasis != TWOLAME_EMPHASIS_5 && emphasis != TWOLAME_EMPHASIS_C) |
| 188 |
return (-1); |
| 189 |
glopts->emphasis = emphasis; |
| 190 |
return (0); |
| 191 |
} |
| 192 |
|
| 193 |
TWOLAME_Emphasis twolame_get_emphasis(twolame_options * glopts) |
| 194 |
{ |
| 195 |
return (glopts->emphasis); |
| 196 |
} |
| 197 |
|
| 198 |
int twolame_set_error_protection(twolame_options * glopts, int error_protection) |
| 199 |
{ |
| 200 |
if (error_protection) |
| 201 |
glopts->error_protection = TRUE; |
| 202 |
else |
| 203 |
glopts->error_protection = FALSE; |
| 204 |
return (0); |
| 205 |
} |
| 206 |
|
| 207 |
int twolame_get_error_protection(twolame_options * glopts) |
| 208 |
{ |
| 209 |
return (glopts->error_protection); |
| 210 |
} |
| 211 |
|
| 212 |
int twolame_set_copyright(twolame_options * glopts, int copyright) |
| 213 |
{ |
| 214 |
if (copyright) |
| 215 |
glopts->copyright = TRUE; |
| 216 |
else |
| 217 |
glopts->copyright = FALSE; |
| 218 |
return (0); |
| 219 |
} |
| 220 |
|
| 221 |
int twolame_get_copyright(twolame_options * glopts) |
| 222 |
{ |
| 223 |
return (glopts->copyright); |
| 224 |
} |
| 225 |
|
| 226 |
int twolame_set_original(twolame_options * glopts, int original) |
| 227 |
{ |
| 228 |
if (original) |
| 229 |
glopts->original = TRUE; |
| 230 |
else |
| 231 |
glopts->original = FALSE; |
| 232 |
return (0); |
| 233 |
} |
| 234 |
|
| 235 |
int twolame_get_original(twolame_options * glopts) |
| 236 |
{ |
| 237 |
return (glopts->original); |
| 238 |
} |
| 239 |
|
| 240 |
int twolame_set_extension(twolame_options * glopts, int extension) |
| 241 |
{ |
| 242 |
if (extension) |
| 243 |
glopts->private_extension = TRUE; |
| 244 |
else |
| 245 |
glopts->private_extension = FALSE; |
| 246 |
return (0); |
| 247 |
} |
| 248 |
|
| 249 |
int twolame_get_extension(twolame_options * glopts) |
| 250 |
{ |
| 251 |
return (glopts->private_extension); |
| 252 |
} |
| 253 |
|
| 254 |
int twolame_set_padding(twolame_options * glopts, TWOLAME_Padding padding) |
| 255 |
{ |
| 256 |
if (padding) |
| 257 |
glopts->padding = TRUE; |
| 258 |
else |
| 259 |
glopts->padding = FALSE; |
| 260 |
|
| 261 |
return (0); |
| 262 |
} |
| 263 |
|
| 264 |
TWOLAME_Padding twolame_get_padding(twolame_options * glopts) |
| 265 |
{ |
| 266 |
return (glopts->padding); |
| 267 |
} |
| 268 |
|
| 269 |
int twolame_set_VBR(twolame_options * glopts, int vbr) |
| 270 |
{ |
| 271 |
if (vbr) |
| 272 |
glopts->vbr = TRUE; |
| 273 |
else |
| 274 |
glopts->vbr = FALSE; |
| 275 |
return (0); |
| 276 |
} |
| 277 |
|
| 278 |
int twolame_get_VBR(twolame_options * glopts) |
| 279 |
{ |
| 280 |
return (glopts->vbr); |
| 281 |
} |
| 282 |
|
| 283 |
int twolame_set_VBR_level(twolame_options * glopts, float level) |
| 284 |
{ |
| 285 |
// Limit is -50 to 50, but useful range is -10 to 10 |
| 286 |
if (fabs(level) > 50.0) |
| 287 |
return (-1); |
| 288 |
else |
| 289 |
glopts->vbrlevel = level; |
| 290 |
return (0); |
| 291 |
} |
| 292 |
|
| 293 |
float twolame_get_VBR_level(twolame_options * glopts) |
| 294 |
{ |
| 295 |
return (glopts->vbrlevel); |
| 296 |
} |
| 297 |
|
| 298 |
// Deprecated: |
| 299 |
int twolame_set_VBR_q(twolame_options * glopts, float level) |
| 300 |
{ |
| 301 |
// Limit is -50 to 50, but useful range is -10 to 10 |
| 302 |
if (fabs(level) > 50.0) |
| 303 |
return (-1); |
| 304 |
else |
| 305 |
glopts->vbrlevel = level; |
| 306 |
return (0); |
| 307 |
} |
| 308 |
|
| 309 |
// Deprecated: |
| 310 |
float twolame_get_VBR_q(twolame_options * glopts) |
| 311 |
{ |
| 312 |
return (glopts->vbrlevel); |
| 313 |
} |
| 314 |
|
| 315 |
int twolame_set_ATH_level(twolame_options * glopts, float level) |
| 316 |
{ |
| 317 |
glopts->athlevel = level; |
| 318 |
return (0); |
| 319 |
} |
| 320 |
|
| 321 |
float twolame_get_ATH_level(twolame_options * glopts) |
| 322 |
{ |
| 323 |
return (glopts->athlevel); |
| 324 |
} |
| 325 |
|
| 326 |
int twolame_set_quick_mode(twolame_options * glopts, int quickmode) |
| 327 |
{ |
| 328 |
if (quickmode) |
| 329 |
glopts->quickmode = TRUE; |
| 330 |
else |
| 331 |
glopts->quickmode = FALSE; |
| 332 |
return (0); |
| 333 |
} |
| 334 |
|
| 335 |
int twolame_get_quick_mode(twolame_options * glopts) |
| 336 |
{ |
| 337 |
return (glopts->quickmode); |
| 338 |
} |
| 339 |
|
| 340 |
int twolame_set_quick_count(twolame_options * glopts, int quickcount) |
| 341 |
{ |
| 342 |
glopts->quickcount = quickcount; |
| 343 |
return (0); |
| 344 |
} |
| 345 |
|
| 346 |
int twolame_get_quick_count(twolame_options * glopts) |
| 347 |
{ |
| 348 |
return (glopts->quickcount); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
int twolame_set_verbosity(twolame_options * glopts, int verbosity) |
| 353 |
{ |
| 354 |
if (verbosity < 0 || verbosity > 10) { |
| 355 |
fprintf(stderr, "invalid verbosity level %i\n", verbosity); |
| 356 |
return (-1); |
| 357 |
} |
| 358 |
glopts->verbosity = verbosity; |
| 359 |
return (0); |
| 360 |
} |
| 361 |
|
| 362 |
int twolame_get_verbosity(twolame_options * glopts) |
| 363 |
{ |
| 364 |
return (glopts->verbosity); |
| 365 |
} |
| 366 |
|
| 367 |
int twolame_set_VBR_max_bitrate_kbps(twolame_options * glopts, int bitrate) |
| 368 |
{ |
| 369 |
glopts->vbr_max_bitrate = bitrate; |
| 370 |
return (0); |
| 371 |
} |
| 372 |
|
| 373 |
int twolame_get_VBR_max_bitrate_kbps(twolame_options * glopts) |
| 374 |
{ |
| 375 |
return (glopts->vbr_max_bitrate); |
| 376 |
} |
| 377 |
|
| 378 |
int twolame_set_num_ancillary_bits(twolame_options * glopts, int num) |
| 379 |
{ |
| 380 |
if (num < 0) |
| 381 |
return (-1); |
| 382 |
glopts->num_ancillary_bits = num; |
| 383 |
return (0); |
| 384 |
} |
| 385 |
|
| 386 |
int twolame_get_num_ancillary_bits(twolame_options * glopts) |
| 387 |
{ |
| 388 |
return (glopts->num_ancillary_bits); |
| 389 |
} |
| 390 |
|
| 391 |
int twolame_set_energy_levels(twolame_options * glopts, int energylevels) |
| 392 |
{ |
| 393 |
if (energylevels) { |
| 394 |
glopts->do_energy_levels = TRUE; |
| 395 |
} else { |
| 396 |
glopts->do_energy_levels = FALSE; |
| 397 |
} |
| 398 |
|
| 399 |
return (0); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
int twolame_get_energy_levels(twolame_options * glopts) |
| 404 |
{ |
| 405 |
return (glopts->do_energy_levels); |
| 406 |
} |
| 407 |
|
| 408 |
int twolame_set_version(twolame_options * glopts, TWOLAME_MPEG_version version) |
| 409 |
{ |
| 410 |
if (version != 0 && version != 1) |
| 411 |
return (-1); |
| 412 |
glopts->version = version; |
| 413 |
return (0); |
| 414 |
} |
| 415 |
|
| 416 |
TWOLAME_MPEG_version twolame_get_version(twolame_options * glopts) |
| 417 |
{ |
| 418 |
return (glopts->version); |
| 419 |
} |
| 420 |
|
| 421 |
const char *twolame_get_version_name(twolame_options * glopts) |
| 422 |
{ |
| 423 |
return twolame_mpeg_version_name(glopts->version); |
| 424 |
} |
| 425 |
|
| 426 |
int twolame_set_freeformat(twolame_options * glopts, int freef) |
| 427 |
{ |
| 428 |
if (freef) { |
| 429 |
glopts->freeformat = TRUE; |
| 430 |
} else { |
| 431 |
glopts->freeformat = FALSE; |
| 432 |
} |
| 433 |
|
| 434 |
return (0); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
/* Jörgen Scott 09May 2014. Added some utility methods to make DAB support easier. |
| 442 |
- twolame_set_DAB_scf_crc_length |
| 443 |
- twolame_set_DAB_scf_crc |
| 444 |
Supporting DAB requires the front-end to buffer at least two mp2 frames. |
| 445 |
However given this handling, DAB seems to work just fine. So I removed the previous warning message. |
| 446 |
*/ |
| 447 |
int twolame_set_DAB(twolame_options * glopts, int dab) |
| 448 |
{ |
| 449 |
if (dab) |
| 450 |
glopts->do_dab = TRUE; |
| 451 |
else |
| 452 |
glopts->do_dab = FALSE; |
| 453 |
return (0); |
| 454 |
} |
| 455 |
|
| 456 |
int twolame_get_DAB(twolame_options * glopts) |
| 457 |
{ |
| 458 |
return (glopts->do_dab); |
| 459 |
} |
| 460 |
|
| 461 |
int twolame_set_DAB_xpad_length(twolame_options * glopts, int length) |
| 462 |
{ |
| 463 |
if (length < 0) |
| 464 |
return (-1); |
| 465 |
glopts->dab_xpad_len = length; |
| 466 |
return (0); |
| 467 |
} |
| 468 |
|
| 469 |
int twolame_get_DAB_xpad_length(twolame_options * glopts) |
| 470 |
{ |
| 471 |
return (glopts->dab_xpad_len); |
| 472 |
} |
| 473 |
|
| 474 |
int twolame_set_DAB_crc_length(twolame_options * glopts, int length) |
| 475 |
{ |
| 476 |
if (length < 0) |
| 477 |
return (-1); |
| 478 |
else |
| 479 |
glopts->dab_crc_len = length; |
| 480 |
return (0); |
| 481 |
} |
| 482 |
|
| 483 |
int twolame_set_DAB_scf_crc_length(twolame_options * glopts) |
| 484 |
{ |
| 485 |
if (glopts->version == TWOLAME_MPEG2) { |
| 486 |
glopts->dab_crc_len = 4; |
| 487 |
} |
| 488 |
else if (glopts->mode == TWOLAME_MONO) { |
| 489 |
if (glopts->bitrate >= 56) |
| 490 |
glopts->dab_crc_len = 4; |
| 491 |
else |
| 492 |
glopts->dab_crc_len = 2; |
| 493 |
} |
| 494 |
else if (glopts->bitrate >= 112) |
| 495 |
glopts->dab_crc_len = 4; |
| 496 |
else |
| 497 |
glopts->dab_crc_len = 2; |
| 498 |
return (0); |
| 499 |
} |
| 500 |
|
| 501 |
int twolame_get_DAB_crc_length(twolame_options * glopts) |
| 502 |
{ |
| 503 |
return (glopts->dab_crc_len); |
| 504 |
} |
| 505 |
|
| 506 |
int twolame_set_DAB_scf_crc(twolame_options * glopts, |
| 507 |
unsigned char *mp2buffer, |
| 508 |
int mp2buffer_size) |
| 509 |
{ |
| 510 |
/* ScF-CRC field precedes two bytes of F-PAD */ |
| 511 |
unsigned char *pTo = &mp2buffer[mp2buffer_size - 3]; |
| 512 |
|
| 513 |
if (glopts->dab_crc_len == 4) |
| 514 |
{ |
| 515 |
*pTo-- = glopts->dab_crc[0]; |
| 516 |
*pTo-- = glopts->dab_crc[1]; |
| 517 |
*pTo-- = glopts->dab_crc[2];; |
| 518 |
*pTo-- = glopts->dab_crc[3]; |
| 519 |
} |
| 520 |
else if (glopts->dab_crc_len == 2) |
| 521 |
{ |
| 522 |
*pTo-- = glopts->dab_crc[0]; |
| 523 |
*pTo-- = glopts->dab_crc[1]; |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
fprintf(stderr, "Invalid size of DAB scf-crc field\n"); |
| 528 |
return (-1); |
| 529 |
} |
| 530 |
return (0); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
// vim:ts=4:sw=4:nowrap: |