| 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 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#include "twolame.h" |
| 28 |
#include "common.h" |
| 29 |
#include "bitbuffer.h" |
| 30 |
#include "energy.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
// Returns the desired number of ancillary bits to be |
| 35 |
// reserved at the end of the frame |
| 36 |
int twolame_get_required_energy_bits(twolame_options * glopts) |
| 37 |
{ |
| 38 |
if (glopts->mode == TWOLAME_MONO) { |
| 39 |
// only 2 bytes + zero at n-3 needed for energy level for mono channel |
| 40 |
return 24; |
| 41 |
|
| 42 |
} else { |
| 43 |
// 5 bytes for the stereo energy info |
| 44 |
return 40; |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
// Calculates the energy levels of current frame and |
| 51 |
// inserts it into the end of the frame |
| 52 |
void twolame_do_energy_levels(twolame_options * glopts, bit_stream * bs) |
| 53 |
{ |
| 54 |
/* Reference: Using the BWF Energy Levels in AudioScience Bitstreams |
| 55 |
http://www.audioscience.com/internet/download/notes/note0001_MPEG_energy.pdf |
| 56 |
|
| 57 |
Specification of the Broadcast Wave Format: Supplement 1 - MPEG audio |
| 58 |
http://www.ebu.ch/CMSimages/en/tec_doc_t3285_s1_tcm6-10545.pdf |
| 59 |
http://www.sr.se/utveckling/tu/bwf/ |
| 60 |
|
| 61 |
The absolute peak of the PCM file for the left and right channel in this frame are written |
| 62 |
into the last 5 bytes of the frame. |
| 63 |
|
| 64 |
The last 5 bytes *must* be reserved for this to work correctly (otherwise you'll be |
| 65 |
overwriting mpeg audio data) */ |
| 66 |
|
| 67 |
short int *leftpcm = glopts->buffer[0]; |
| 68 |
short int *rightpcm = glopts->buffer[1]; |
| 69 |
|
| 70 |
int i, leftMax, rightMax; |
| 71 |
unsigned char rhibyte, rlobyte, lhibyte, llobyte; |
| 72 |
|
| 73 |
// Get the position (in butes) of the end of the mpeg audio frame |
| 74 |
int frameEnd = twolame_buffer_sstell(bs) / 8; |
| 75 |
|
| 76 |
|
| 77 |
// find the maximum in the left and right channels |
| 78 |
leftMax = rightMax = -1; |
| 79 |
for (i = 0; i < TWOLAME_SAMPLES_PER_FRAME; i++) { |
| 80 |
if (abs(leftpcm[i]) > leftMax) |
| 81 |
leftMax = abs(leftpcm[i]); |
| 82 |
if (abs(rightpcm[i]) > rightMax) |
| 83 |
rightMax = abs(rightpcm[i]); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
// fix any overflows |
| 89 |
if (leftMax > 32767) |
| 90 |
leftMax = 32767; |
| 91 |
|
| 92 |
if (rightMax > 32767) |
| 93 |
rightMax = 32767; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
// convert max value to hi/lo bytes and write into buffer |
| 98 |
lhibyte = leftMax / 256; |
| 99 |
llobyte = leftMax - 256 * lhibyte; |
| 100 |
|
| 101 |
// Write the left channel into the last two bytes of the frame |
| 102 |
bs->buf[frameEnd - 1] = llobyte; |
| 103 |
bs->buf[frameEnd - 2] = lhibyte; |
| 104 |
bs->buf[frameEnd - 3] = 0; |
| 105 |
|
| 106 |
// Only write the right channel energy info |
| 107 |
// if we're in stereo mode. |
| 108 |
if (glopts->mode != TWOLAME_MONO) { |
| 109 |
|
| 110 |
rhibyte = rightMax / 256; |
| 111 |
rlobyte = rightMax - 256 * rhibyte; |
| 112 |
|
| 113 |
bs->buf[frameEnd - 4] = rlobyte; |
| 114 |
bs->buf[frameEnd - 5] = rhibyte; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
// vim:ts=4:sw=4:nowrap: |