~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/transcode/mpglib/mpglib/VbrTag.c

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Xing VBR tagging for LAME.
 
3
 *
 
4
 *      Copyright (c) 1999 A.L. Faber
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Library General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Library General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public
 
17
 * License along with this library; if not, write to the
 
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 * Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
/* $Id: VbrTag.c,v 1.77 2004/03/23 01:57:16 olcios Exp $ */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
# include <config.h>
 
26
#endif
 
27
 
 
28
#include "machine.h"
 
29
#include "bitstream.h"
 
30
#include "lame.h"
 
31
#include "VbrTag.h"
 
32
#include "version.h"
 
33
 
 
34
#include        <assert.h>
 
35
 
 
36
#ifdef WITH_DMALLOC
 
37
#include <dmalloc.h>
 
38
#endif
 
39
 
 
40
#ifdef __sun__
 
41
/* woraround for SunOS 4.x, it has SEEK_* defined here */
 
42
#include <unistd.h>
 
43
#endif
 
44
 
 
45
 
 
46
#ifdef _DEBUG
 
47
/*  #define DEBUG_VBRTAG */
 
48
#endif
 
49
 
 
50
/*
 
51
 *    4 bytes for Header Tag 
 
52
 *    4 bytes for Header Flags 
 
53
 *  100 bytes for entry (NUMTOCENTRIES) 
 
54
 *    4 bytes for FRAME SIZE 
 
55
 *    4 bytes for STREAM_SIZE 
 
56
 *    4 bytes for VBR SCALE. a VBR quality indicator: 0=best 100=worst 
 
57
 *   20 bytes for LAME tag.  for example, "LAME3.12 (beta 6)" 
 
58
 * ___________ 
 
59
 *  140 bytes 
 
60
*/
 
61
#define VBRHEADERSIZE (NUMTOCENTRIES+4+4+4+4+4)
 
62
 
 
63
#define LAMEHEADERSIZE (VBRHEADERSIZE + 9 + 1 + 1 + 8 + 1 + 1 + 3 + 1 + 1 + 2 + 4 + 2 + 2)
 
64
 
 
65
/* the size of the Xing header (MPEG1 and MPEG2) in kbps */
 
66
#define XING_BITRATE1 128
 
67
#define XING_BITRATE2  64
 
68
#define XING_BITRATE25 32
 
69
 
 
70
 
 
71
 
 
72
const static char       VBRTag[]={"Xing"};
 
73
const static char       VBRTag2[]={"Info"};
 
74
 
 
75
 
 
76
 
 
77
 
 
78
/* Lookup table for fast CRC computation
 
79
 * See 'CRC_update_lookup'
 
80
 * Uses the polynomial x^16+x^15+x^2+1 */
 
81
 
 
82
unsigned int crc16_lookup[256] =
 
83
{
 
84
        0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
 
85
        0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
 
86
        0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 
 
87
        0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
 
88
        0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 
 
89
        0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 
 
90
        0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
 
91
        0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
 
92
        0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
 
93
        0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 
 
94
        0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 
 
95
        0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
 
96
        0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 
 
97
        0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
 
98
        0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 
 
99
        0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
 
100
        0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 
 
101
        0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, 
 
102
        0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
 
103
        0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 
 
104
        0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
 
105
        0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
 
106
        0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 
 
107
        0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, 
 
108
        0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 
 
109
        0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
 
110
        0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 
 
111
        0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
 
112
        0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
 
113
        0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
 
114
        0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
 
115
        0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
 
116
};
 
117
 
 
118
 
 
119
 
 
120
 
 
121
 
 
122
/***********************************************************************
 
123
 *  Robert Hegemann 2001-01-17
 
124
 ***********************************************************************/
 
125
 
 
126
static void addVbr(VBR_seek_info_t * v, int bitrate)
 
127
{
 
128
    int i;
 
129
 
 
130
    v->sum += bitrate;
 
131
    v->seen ++;
 
132
    
 
133
    if (v->seen < v->want) {
 
134
        return;
 
135
    }
 
136
 
 
137
    if (v->pos < v->size) {
 
138
        v->bag[v->pos] = v->sum;
 
139
        v->pos ++;
 
140
        v->seen = 0;
 
141
    }
 
142
    if (v->pos == v->size) {
 
143
        for (i = 1; i < v->size; i += 2) {
 
144
            v->bag[i/2] = v->bag[i]; 
 
145
        }
 
146
        v->want *= 2;
 
147
        v->pos  /= 2;
 
148
    }
 
149
}
 
150
 
 
151
static void Xing_seek_table(VBR_seek_info_t * v, unsigned char *t)
 
152
{
 
153
    int i, index;
 
154
    int seek_point;
 
155
    
 
156
    if (v->pos <= 0)
 
157
        return;
 
158
        
 
159
    for (i = 1; i < NUMTOCENTRIES; ++i) {
 
160
        float j = i/(float)NUMTOCENTRIES, act, sum;
 
161
        index = (int)(floor(j * v->pos));
 
162
        if (index > v->pos-1)
 
163
            index = v->pos-1;
 
164
        act = v->bag[index];
 
165
        sum = v->sum;
 
166
        seek_point = (int)(256. * act / sum);
 
167
        if (seek_point > 255)
 
168
            seek_point = 255;
 
169
        t[i] = seek_point;
 
170
    }
 
171
}
 
172
 
 
173
void print_seeking(unsigned char *t)
 
174
{
 
175
    int i;
 
176
    
 
177
    printf("seeking table ");
 
178
    for (i = 0; i < NUMTOCENTRIES; ++i) {
 
179
        printf(" %d ", t[i]);
 
180
    }
 
181
    printf("\n");
 
182
}
 
183
 
 
184
 
 
185
 
 
186
/****************************************************************************
 
187
 * AddVbrFrame: Add VBR entry, used to fill the VBR the TOC entries
 
188
 * Paramters:
 
189
 *      nStreamPos: how many bytes did we write to the bitstream so far
 
190
 *                              (in Bytes NOT Bits)
 
191
 ****************************************************************************
 
192
*/
 
193
void AddVbrFrame(lame_global_flags *gfp)
 
194
{
 
195
    lame_internal_flags *gfc = gfp->internal_flags;
 
196
 
 
197
    int kbps = bitrate_table[gfp->version][gfc->bitrate_index];
 
198
    assert(gfc->VBR_seek_table.bag);
 
199
    addVbr(&gfc->VBR_seek_table, kbps);
 
200
    gfp->nVbrNumFrames++;
 
201
}
 
202
 
 
203
 
 
204
/*-------------------------------------------------------------*/
 
205
static int ExtractI4(unsigned char *buf)
 
206
{
 
207
        int x;
 
208
        /* big endian extract */
 
209
        x = buf[0];
 
210
        x <<= 8;
 
211
        x |= buf[1];
 
212
        x <<= 8;
 
213
        x |= buf[2];
 
214
        x <<= 8;
 
215
        x |= buf[3];
 
216
        return x;
 
217
}
 
218
 
 
219
static void CreateI4(unsigned char *buf, int nValue)
 
220
{
 
221
        /* big endian create */
 
222
        buf[0]=(nValue>>24)&0xff;
 
223
        buf[1]=(nValue>>16)&0xff;
 
224
        buf[2]=(nValue>> 8)&0xff;
 
225
        buf[3]=(nValue    )&0xff;
 
226
}
 
227
 
 
228
 
 
229
 
 
230
static void CreateI2(unsigned char *buf, int nValue)
 
231
{
 
232
        /* big endian create */
 
233
        buf[0]=(nValue>> 8)&0xff;
 
234
        buf[1]=(nValue    )&0xff;
 
235
}
 
236
 
 
237
 
 
238
/*-------------------------------------------------------------*/
 
239
/* Same as GetVbrTag below, but only checks for the Xing tag.
 
240
   requires buf to contain only 40 bytes */
 
241
/*-------------------------------------------------------------*/
 
242
int CheckVbrTag(unsigned char *buf)
 
243
{
 
244
        int                     h_id, h_mode, h_sr_index;
 
245
 
 
246
        /* get selected MPEG header data */
 
247
        h_id       = (buf[1] >> 3) & 1;
 
248
        h_sr_index = (buf[2] >> 2) & 3;
 
249
        h_mode     = (buf[3] >> 6) & 3;
 
250
 
 
251
        /*  determine offset of header */
 
252
        if( h_id )
 
253
        {
 
254
                /* mpeg1 */
 
255
                if( h_mode != 3 )       buf+=(32+4);
 
256
                else                            buf+=(17+4);
 
257
        }
 
258
        else
 
259
        {
 
260
                /* mpeg2 */
 
261
                if( h_mode != 3 ) buf+=(17+4);
 
262
                else              buf+=(9+4);
 
263
        }
 
264
 
 
265
        if( buf[0] != VBRTag[0] && buf[0] != VBRTag2[0] ) return 0;    /* fail */
 
266
        if( buf[1] != VBRTag[1] && buf[1] != VBRTag2[1]) return 0;    /* header not found*/
 
267
        if( buf[2] != VBRTag[2] && buf[2] != VBRTag2[2]) return 0;
 
268
        if( buf[3] != VBRTag[3] && buf[3] != VBRTag2[3]) return 0;
 
269
        return 1;
 
270
}
 
271
 
 
272
int GetVbrTag(VBRTAGDATA *pTagData,  unsigned char *buf)
 
273
{
 
274
        int                     i, head_flags;
 
275
        int                     h_bitrate,h_id, h_mode, h_sr_index;
 
276
        int enc_delay,enc_padding; 
 
277
 
 
278
        /* get Vbr header data */
 
279
        pTagData->flags = 0;
 
280
 
 
281
        /* get selected MPEG header data */
 
282
        h_id       = (buf[1] >> 3) & 1;
 
283
        h_sr_index = (buf[2] >> 2) & 3;
 
284
        h_mode     = (buf[3] >> 6) & 3;
 
285
        h_bitrate  = ((buf[2]>>4)&0xf);
 
286
        h_bitrate = bitrate_table[h_id][h_bitrate];
 
287
 
 
288
        /* check for FFE syncword */
 
289
        if ((buf[1]>>4)==0xE) 
 
290
            pTagData->samprate = samplerate_table[2][h_sr_index];
 
291
        else
 
292
            pTagData->samprate = samplerate_table[h_id][h_sr_index];
 
293
        /*      if( h_id == 0 ) */
 
294
        /*              pTagData->samprate >>= 1; */
 
295
 
 
296
 
 
297
 
 
298
        /*  determine offset of header */
 
299
        if( h_id )
 
300
        {
 
301
                /* mpeg1 */
 
302
                if( h_mode != 3 )       buf+=(32+4);
 
303
                else                            buf+=(17+4);
 
304
        }
 
305
        else
 
306
        {
 
307
                /* mpeg2 */
 
308
                if( h_mode != 3 ) buf+=(17+4);
 
309
                else              buf+=(9+4);
 
310
        }
 
311
 
 
312
        if( buf[0] != VBRTag[0] && buf[0] != VBRTag2[0] ) return 0;    /* fail */
 
313
        if( buf[1] != VBRTag[1] && buf[1] != VBRTag2[1]) return 0;    /* header not found*/
 
314
        if( buf[2] != VBRTag[2] && buf[2] != VBRTag2[2]) return 0;
 
315
        if( buf[3] != VBRTag[3] && buf[3] != VBRTag2[3]) return 0;
 
316
 
 
317
        buf+=4;
 
318
 
 
319
        pTagData->h_id = h_id;
 
320
 
 
321
        head_flags = pTagData->flags = ExtractI4(buf); buf+=4;      /* get flags */
 
322
 
 
323
        if( head_flags & FRAMES_FLAG )
 
324
        {
 
325
                pTagData->frames   = ExtractI4(buf); buf+=4;
 
326
        }
 
327
 
 
328
        if( head_flags & BYTES_FLAG )
 
329
        {
 
330
                pTagData->bytes = ExtractI4(buf); buf+=4;
 
331
        }
 
332
 
 
333
        if( head_flags & TOC_FLAG )
 
334
        {
 
335
                if( pTagData->toc != NULL )
 
336
                {
 
337
                        for(i=0;i<NUMTOCENTRIES;i++)
 
338
                                pTagData->toc[i] = buf[i];
 
339
                }
 
340
                buf+=NUMTOCENTRIES;
 
341
        }
 
342
 
 
343
        pTagData->vbr_scale = -1;
 
344
 
 
345
        if( head_flags & VBR_SCALE_FLAG )
 
346
        {
 
347
                pTagData->vbr_scale = ExtractI4(buf); buf+=4;
 
348
        }
 
349
 
 
350
        pTagData->headersize = 
 
351
          ((h_id+1)*72000*h_bitrate) / pTagData->samprate;
 
352
 
 
353
        buf+=21;
 
354
        enc_delay = buf[0] << 4;
 
355
        enc_delay += buf[1] >> 4;
 
356
        enc_padding= (buf[1] & 0x0F)<<8;
 
357
        enc_padding += buf[2];
 
358
        /* check for reasonable values (this may be an old Xing header, */
 
359
        /* not a INFO tag) */
 
360
        if (enc_delay<0 || enc_delay > 3000) enc_delay=-1;
 
361
        if (enc_padding<0 || enc_padding > 3000) enc_padding=-1;
 
362
 
 
363
        pTagData->enc_delay=enc_delay;
 
364
        pTagData->enc_padding=enc_padding;
 
365
 
 
366
#ifdef DEBUG_VBRTAG
 
367
        fprintf(stderr,"\n\n********************* VBR TAG INFO *****************\n");
 
368
        fprintf(stderr,"tag         :%s\n",VBRTag);
 
369
        fprintf(stderr,"head_flags  :%d\n",head_flags);
 
370
        fprintf(stderr,"bytes       :%d\n",pTagData->bytes);
 
371
        fprintf(stderr,"frames      :%d\n",pTagData->frames);
 
372
        fprintf(stderr,"VBR Scale   :%d\n",pTagData->vbr_scale);
 
373
        fprintf(stderr,"enc_delay  = %i \n",enc_delay);
 
374
        fprintf(stderr,"enc_padding= %i \n",enc_padding);
 
375
        fprintf(stderr,"toc:\n");
 
376
        if( pTagData->toc != NULL )
 
377
        {
 
378
                for(i=0;i<NUMTOCENTRIES;i++)
 
379
                {
 
380
                        if( (i%10) == 0 ) fprintf(stderr,"\n");
 
381
                        fprintf(stderr," %3d", (int)(pTagData->toc[i]));
 
382
                }
 
383
        }
 
384
        fprintf(stderr,"\n***************** END OF VBR TAG INFO ***************\n");
 
385
#endif
 
386
        return 1;       /* success */
 
387
}
 
388
 
 
389
 
 
390
/****************************************************************************
 
391
 * InitVbrTag: Initializes the header, and write empty frame to stream
 
392
 * Paramters:
 
393
 *                              fpStream: pointer to output file stream
 
394
 *                              nMode   : Channel Mode: 0=STEREO 1=JS 2=DS 3=MONO
 
395
 ****************************************************************************
 
396
*/
 
397
int InitVbrTag(lame_global_flags *gfp)
 
398
{
 
399
        int nMode,SampIndex;
 
400
        int i,kbps_header,tot;
 
401
        lame_internal_flags *gfc = gfp->internal_flags;
 
402
#define MAXFRAMESIZE 2880 /* or 0xB40, the max freeformat 640 32kHz framesize */
 
403
        /*      uint8_t pbtStreamBuffer[MAXFRAMESIZE]; */
 
404
        nMode = gfp->mode;
 
405
        SampIndex = gfc->samplerate_index;
 
406
 
 
407
 
 
408
 
 
409
        gfp->nVbrNumFrames=0;
 
410
        /*gfp->nVbrFrameBufferSize=0;*/
 
411
 
 
412
 
 
413
        /* Clear stream buffer */
 
414
        /*      memset(pbtStreamBuffer,0x00,sizeof(pbtStreamBuffer)); */
 
415
 
 
416
 
 
417
 
 
418
        /*
 
419
         * Xing VBR pretends to be a 48kbs layer III frame.  (at 44.1kHz). 
 
420
         * (at 48kHz they use 56kbs since 48kbs frame not big enough for 
 
421
         * table of contents) 
 
422
         * let's always embed Xing header inside a 64kbs layer III frame. 
 
423
         * this gives us enough room for a LAME version string too. 
 
424
         * size determined by sampling frequency (MPEG1) 
 
425
         * 32kHz:    216 bytes@48kbs    288bytes@ 64kbs 
 
426
         * 44.1kHz:  156 bytes          208bytes@64kbs     (+1 if padding = 1) 
 
427
         * 48kHz:    144 bytes          192 
 
428
         * 
 
429
         * MPEG 2 values are the same since the framesize and samplerate 
 
430
         * are each reduced by a factor of 2. 
 
431
        */
 
432
 
 
433
 
 
434
        if (1==gfp->version) {
 
435
          kbps_header = XING_BITRATE1;
 
436
        } else {
 
437
          if (gfp->out_samplerate < 16000 )
 
438
            kbps_header = XING_BITRATE25;
 
439
          else
 
440
            kbps_header = XING_BITRATE2;
 
441
        }
 
442
 
 
443
        if (gfp->VBR==vbr_off)
 
444
                        kbps_header = gfp->brate;
 
445
        
 
446
        gfp->TotalFrameSize= 
 
447
          ((gfp->version+1)*72000*kbps_header) / gfp->out_samplerate;
 
448
 
 
449
        tot = (gfc->sideinfo_len+LAMEHEADERSIZE);
 
450
 
 
451
        if (gfp->TotalFrameSize < tot || 
 
452
            gfp->TotalFrameSize > MAXFRAMESIZE ) {
 
453
            /* disable tag, it wont fit */
 
454
            gfp->bWriteVbrTag = 0;
 
455
            return 0;
 
456
        }
 
457
 
 
458
        /* write dummy VBR tag of all 0's into bitstream */ 
 
459
        for (i=0; i<gfp->TotalFrameSize; ++i)
 
460
          add_dummy_byte(gfp,0);
 
461
 
 
462
 
 
463
        gfc->VBR_seek_table.sum  = 0;
 
464
        
 
465
        gfc->VBR_seek_table.seen = 0;
 
466
        gfc->VBR_seek_table.want = 1;
 
467
        gfc->VBR_seek_table.pos  = 0;
 
468
        
 
469
        if (gfc->VBR_seek_table.bag == NULL) {
 
470
            gfc->VBR_seek_table.bag  = malloc (400*sizeof(int));
 
471
            if (gfc->VBR_seek_table.bag != NULL) {
 
472
                gfc->VBR_seek_table.size = 400;
 
473
            }
 
474
            else {
 
475
                gfc->VBR_seek_table.size = 0;
 
476
                ERRORF (gfc,"Error: can't allocate VbrFrames buffer\n");
 
477
                return -1;
 
478
            }   
 
479
        }
 
480
        /* Success */
 
481
        return 0;
 
482
}
 
483
 
 
484
 
 
485
 
 
486
/* fast CRC-16 computation - uses table crc16_lookup 8*/
 
487
int CRC_update_lookup(int value, int crc)
 
488
{
 
489
        int tmp;
 
490
        tmp=crc^value;
 
491
        crc=(crc>>8)^crc16_lookup[tmp & 0xff];
 
492
        return crc;
 
493
}
 
494
 
 
495
void UpdateMusicCRC(uint16_t *crc,unsigned char *buffer, int size){
 
496
    int i;
 
497
    for (i=0; i<size; ++i) 
 
498
        *crc = CRC_update_lookup(buffer[i],*crc);
 
499
}
 
500
 
 
501
 
 
502
 
 
503
 
 
504
 
 
505
/****************************************************************************
 
506
 * Jonathan Dee 2001/08/31
 
507
 *
 
508
 * PutLameVBR: Write LAME info: mini version + info on various switches used
 
509
 * Paramters:
 
510
 *                              pbtStreamBuffer : pointer to output buffer  
 
511
 *                              id3v2size               : size of id3v2 tag in bytes
 
512
 *                              crc                             : computation of crc-16 of Lame Tag so far (starting at frame sync)
 
513
 *                              
 
514
 ****************************************************************************
 
515
*/
 
516
int PutLameVBR(lame_global_flags *gfp, FILE *fpStream, uint8_t *pbtStreamBuffer, uint32_t id3v2size,  uint16_t crc)
 
517
{
 
518
    lame_internal_flags *gfc = gfp->internal_flags;
 
519
/*      FLOAT fVersion = LAME_MAJOR_VERSION + 0.01 * LAME_MINOR_VERSION; */
 
520
 
 
521
        int nBytesWritten = 0;
 
522
        int nFilesize     = 0;          /*size of fpStream. Will be equal to size after process finishes. */
 
523
        int i;
 
524
 
 
525
    int enc_delay=lame_get_encoder_delay(gfp);       /* encoder delay */
 
526
    int enc_padding=lame_get_encoder_padding(gfp);   /* encoder padding  */
 
527
 
 
528
        /*recall:       gfp->VBR_q is for example set by the switch -V  */
 
529
        /*                      gfp->quality by -q, -h, -f, etc */
 
530
        
 
531
        int nQuality            = (100 - 10 * gfp->VBR_q - gfp->quality);
 
532
        
 
533
 
 
534
        const char *szVersion   = get_lame_very_short_version();
 
535
        uint8_t nVBR;
 
536
        uint8_t nRevision = 0x00;
 
537
        uint8_t nRevMethod;
 
538
        uint8_t vbr_type_translator[] = {1,5,3,2,4,0,3};                /*numbering different in vbr_mode vs. Lame tag */
 
539
 
 
540
        uint8_t nLowpass                = ( ((gfp->lowpassfreq / 100.0)+.5) > 255 ? 255 : (gfp->lowpassfreq / 100.0)+.5 );
 
541
 
 
542
        uint32_t nPeakSignalAmplitude = 0;
 
543
 
 
544
        uint16_t nRadioReplayGain               = 0; 
 
545
        uint16_t nAudiophileReplayGain          = 0;
 
546
 
 
547
        uint8_t nNoiseShaping                   = gfp->internal_flags->noise_shaping;
 
548
        uint8_t nStereoMode                             = 0;
 
549
        int             bNonOptimal                             = 0;
 
550
        uint8_t nSourceFreq                             = 0;
 
551
        uint8_t nMisc                                   = 0;
 
552
        uint32_t nMusicLength                   = 0;
 
553
        int             bId3v1Present                   = ((gfp->internal_flags->tag_spec.flags & CHANGED_FLAG)
 
554
                && !(gfp->internal_flags->tag_spec.flags & V2_ONLY_FLAG));
 
555
        uint16_t nMusicCRC                              = 0;
 
556
 
 
557
        /*psy model type: Gpsycho or NsPsytune */
 
558
        unsigned char    bExpNPsyTune   = gfp->exp_nspsytune & 1;
 
559
        unsigned char    bSafeJoint             = (gfp->exp_nspsytune & 2)!=0;
 
560
 
 
561
        unsigned char    bNoGapMore             = 0;
 
562
        unsigned char    bNoGapPrevious = 0;
 
563
 
 
564
        int              nNoGapCount    = gfp->internal_flags->nogap_total;
 
565
        int              nNoGapCurr             = gfp->internal_flags->nogap_current;
 
566
 
 
567
 
 
568
        uint8_t  nAthType               = gfp->ATHtype; /*4 bits. */
 
569
        
 
570
        uint8_t  nFlags                 = 0;
 
571
 
 
572
        /* if ABR, {store bitrate <=255} else { store "-b"} */
 
573
        int nABRBitrate;
 
574
    switch (gfp->VBR) {
 
575
        case vbr_abr:{
 
576
            nABRBitrate = gfp->VBR_mean_bitrate_kbps;
 
577
            break;
 
578
        }
 
579
        case vbr_off:{
 
580
            nABRBitrate = gfp->brate;
 
581
            break;
 
582
        }
 
583
        default:{ /*vbr modes*/
 
584
            nABRBitrate = gfp->VBR_min_bitrate_kbps;
 
585
        }
 
586
    }
 
587
        
 
588
 
 
589
        /*revision and vbr method */
 
590
        if (gfp->VBR>=0 && gfp->VBR < sizeof(vbr_type_translator))
 
591
                nVBR = vbr_type_translator[gfp->VBR];
 
592
        else
 
593
                nVBR = 0x00;            /*unknown. */
 
594
 
 
595
        nRevMethod = 0x10 * nRevision + nVBR; 
 
596
 
 
597
 
 
598
        /* ReplayGain */
 
599
        if (gfp->findReplayGain) { 
 
600
          if (gfc->RadioGain > 0x1FE)
 
601
            gfc->RadioGain = 0x1FE;
 
602
          if (gfc->RadioGain < -0x1FE)
 
603
            gfc->RadioGain = -0x1FE;
 
604
 
 
605
          nRadioReplayGain = 0x2000; /* set name code */
 
606
          nRadioReplayGain |= 0xC00; /* set originator code to `determined automatically' */
 
607
 
 
608
          if (gfc->RadioGain >= 0) 
 
609
            nRadioReplayGain |= gfc->RadioGain; /* set gain adjustment */
 
610
          else {
 
611
            nRadioReplayGain |= 0x200; /* set the sign bit */
 
612
            nRadioReplayGain |= -gfc->RadioGain; /* set gain adjustment */
 
613
          }
 
614
        }
 
615
 
 
616
        /* peak sample */       
 
617
        if(gfc->findPeakSample)
 
618
          nPeakSignalAmplitude = abs((int)((((FLOAT8)gfc->PeakSample) / 32767.0 ) * pow(2,23) +.5));
 
619
 
 
620
        /*nogap */
 
621
        if (nNoGapCount != -1)
 
622
        {
 
623
                if (nNoGapCurr > 0)
 
624
                        bNoGapPrevious = 1;
 
625
 
 
626
                if (nNoGapCurr < nNoGapCount-1)
 
627
                        bNoGapMore = 1;
 
628
        }
 
629
 
 
630
        /*flags */
 
631
 
 
632
        nFlags  = nAthType      + (bExpNPsyTune         << 4)
 
633
                                                + (bSafeJoint           << 5)
 
634
                                                + (bNoGapMore           << 6)
 
635
                                                + (bNoGapPrevious       << 7);
 
636
 
 
637
 
 
638
        if (nQuality < 0)
 
639
                nQuality = 0;
 
640
 
 
641
        /*stereo mode field... a bit ugly.*/
 
642
 
 
643
        switch(gfp->mode)
 
644
        {
 
645
        case MONO:
 
646
                nStereoMode = 0;
 
647
                break;
 
648
        case STEREO:
 
649
                nStereoMode = 1;
 
650
                break;
 
651
        case DUAL_CHANNEL:
 
652
                nStereoMode = 2;
 
653
                break;
 
654
        case JOINT_STEREO:
 
655
                if (gfp->force_ms)
 
656
                        nStereoMode = 4;
 
657
                else
 
658
                        nStereoMode = 3;
 
659
                break;
 
660
        case NOT_SET:
 
661
            /* FALLTHROUGH */
 
662
        default:
 
663
                nStereoMode = 7;
 
664
                break;
 
665
        }
 
666
 
 
667
        /*Intensity stereo : nStereoMode = 6. IS is not implemented */
 
668
 
 
669
        if (gfp->in_samplerate <= 32000)
 
670
                nSourceFreq = 0x00;
 
671
        else if (gfp->in_samplerate ==48000)
 
672
                nSourceFreq = 0x02;
 
673
        else if (gfp->in_samplerate > 48000)
 
674
                nSourceFreq = 0x03;
 
675
        else
 
676
                nSourceFreq = 0x01;  /*default is 44100Hz. */
 
677
 
 
678
 
 
679
        /*Check if the user overrided the default LAME behaviour with some nasty options */
 
680
 
 
681
        if (gfp->short_blocks == short_block_forced                     ||
 
682
                gfp->short_blocks == short_block_dispensed              ||
 
683
                ((gfp->lowpassfreq == -1) && (gfp->highpassfreq == -1)) || /* "-k" */
 
684
                (gfp->scale_left != gfp->scale_right)                   ||
 
685
                gfp->disable_reservoir          ||
 
686
                gfp->noATH                      ||
 
687
                gfp->ATHonly                    ||
 
688
                (nAthType == 0)    ||
 
689
                gfp->in_samplerate <= 32000)
 
690
                        bNonOptimal = 1;
 
691
        
 
692
        nMisc =         nNoiseShaping
 
693
                        +       (nStereoMode << 2)
 
694
                        +       (bNonOptimal << 5)
 
695
                        +       (nSourceFreq << 6);
 
696
 
 
697
 
 
698
        
 
699
        /*get filesize */
 
700
        fseek(fpStream, 0, SEEK_END);
 
701
        nFilesize = ftell(fpStream);
 
702
 
 
703
        
 
704
        nMusicLength = nFilesize - id3v2size;           /*omit current frame */
 
705
        if (bId3v1Present)
 
706
                nMusicLength-=128;                     /*id3v1 present. */
 
707
        nMusicCRC = gfc->nMusicCRC;
 
708
 
 
709
 
 
710
        /*Write all this information into the stream*/
 
711
        CreateI4(&pbtStreamBuffer[nBytesWritten], nQuality);
 
712
        nBytesWritten+=4;
 
713
 
 
714
        strncpy(&pbtStreamBuffer[nBytesWritten], szVersion, 9);
 
715
        nBytesWritten+=9;
 
716
 
 
717
        pbtStreamBuffer[nBytesWritten] = nRevMethod ;
 
718
        nBytesWritten++;
 
719
 
 
720
        pbtStreamBuffer[nBytesWritten] = nLowpass;
 
721
        nBytesWritten++;
 
722
 
 
723
        CreateI4(&pbtStreamBuffer[nBytesWritten], nPeakSignalAmplitude);
 
724
        nBytesWritten+=4;
 
725
 
 
726
        CreateI2(&pbtStreamBuffer[nBytesWritten],nRadioReplayGain);
 
727
        nBytesWritten+=2;
 
728
 
 
729
        CreateI2(&pbtStreamBuffer[nBytesWritten],nAudiophileReplayGain);
 
730
        nBytesWritten+=2;
 
731
 
 
732
        pbtStreamBuffer[nBytesWritten] = nFlags;
 
733
        nBytesWritten++;
 
734
 
 
735
        if (nABRBitrate >= 255)
 
736
                pbtStreamBuffer[nBytesWritten] = 0xFF;
 
737
        else
 
738
                pbtStreamBuffer[nBytesWritten] = nABRBitrate;
 
739
        nBytesWritten++;
 
740
 
 
741
        pbtStreamBuffer[nBytesWritten   ] = enc_delay >> 4; /* works for win32, does it for unix? */
 
742
        pbtStreamBuffer[nBytesWritten +1] = (enc_delay << 4) + (enc_padding >> 8);
 
743
        pbtStreamBuffer[nBytesWritten +2] = enc_padding;
 
744
 
 
745
        nBytesWritten+=3;
 
746
 
 
747
        pbtStreamBuffer[nBytesWritten] = nMisc;
 
748
        nBytesWritten++;
 
749
 
 
750
 
 
751
        pbtStreamBuffer[nBytesWritten++] = 0;   /*unused in rev0 */
 
752
 
 
753
        CreateI2(&pbtStreamBuffer[nBytesWritten], gfp->preset);
 
754
        nBytesWritten+=2;
 
755
 
 
756
        CreateI4(&pbtStreamBuffer[nBytesWritten], nMusicLength);
 
757
        nBytesWritten+=4;
 
758
 
 
759
        CreateI2(&pbtStreamBuffer[nBytesWritten], nMusicCRC);
 
760
        nBytesWritten+=2;
 
761
 
 
762
        /*Calculate tag CRC.... must be done here, since it includes
 
763
         *previous information*/
 
764
        
 
765
        for (i = 0;i<nBytesWritten;i++)
 
766
                crc = CRC_update_lookup(pbtStreamBuffer[i], crc);
 
767
        
 
768
        CreateI2(&pbtStreamBuffer[nBytesWritten], crc);
 
769
        nBytesWritten+=2;
 
770
 
 
771
        return nBytesWritten;
 
772
}
 
773
 
 
774
/***********************************************************************
 
775
 * 
 
776
 * PutVbrTag: Write final VBR tag to the file
 
777
 * Paramters:
 
778
 *                              lpszFileName: filename of MP3 bit stream
 
779
 *                              nVbrScale       : encoder quality indicator (0..100)
 
780
 ****************************************************************************
 
781
*/
 
782
int PutVbrTag(lame_global_flags *gfp,FILE *fpStream,int nVbrScale)
 
783
{
 
784
    lame_internal_flags * gfc = gfp->internal_flags;
 
785
 
 
786
        long lFileSize;
 
787
        int nStreamIndex;
 
788
        char abyte,bbyte;
 
789
        uint8_t         btToc[NUMTOCENTRIES];
 
790
        uint8_t pbtStreamBuffer[MAXFRAMESIZE];
 
791
        
 
792
        int i;
 
793
        uint16_t crc = 0x00;
 
794
        
 
795
    unsigned char id3v2Header[10];
 
796
    size_t id3v2TagSize;
 
797
 
 
798
    if (gfc->VBR_seek_table.pos <= 0)
 
799
        return -1;
 
800
 
 
801
 
 
802
        /* Clear stream buffer */
 
803
        memset(pbtStreamBuffer,0x00,sizeof(pbtStreamBuffer));
 
804
 
 
805
        /* Seek to end of file*/
 
806
        fseek(fpStream,0,SEEK_END);
 
807
 
 
808
        /* Get file size */
 
809
        lFileSize=ftell(fpStream);
 
810
 
 
811
        /* Abort if file has zero length. Yes, it can happen :) */
 
812
        if (lFileSize==0)
 
813
                return -1;
 
814
 
 
815
        /*
 
816
         * The VBR tag may NOT be located at the beginning of the stream.
 
817
         * If an ID3 version 2 tag was added, then it must be skipped to write
 
818
         * the VBR tag data.
 
819
         */
 
820
 
 
821
        /* seek to the beginning of the stream */
 
822
        fseek(fpStream,0,SEEK_SET);
 
823
        /* read 10 bytes in case there's an ID3 version 2 header here */
 
824
        fread(id3v2Header,1,sizeof id3v2Header,fpStream);
 
825
        /* does the stream begin with the ID3 version 2 file identifier? */
 
826
        if (!strncmp((char *)id3v2Header,"ID3",3)) {
 
827
          /* the tag size (minus the 10-byte header) is encoded into four
 
828
           * bytes where the most significant bit is clear in each byte */
 
829
          id3v2TagSize=(((id3v2Header[6] & 0x7f)<<21)
 
830
            | ((id3v2Header[7] & 0x7f)<<14)
 
831
            | ((id3v2Header[8] & 0x7f)<<7)
 
832
            | (id3v2Header[9] & 0x7f))
 
833
            + sizeof id3v2Header;
 
834
        } else {
 
835
          /* no ID3 version 2 tag in this stream */
 
836
          id3v2TagSize=0;
 
837
        }
 
838
 
 
839
        /* Seek to first real frame */
 
840
        fseek(fpStream,id3v2TagSize+gfp->TotalFrameSize,SEEK_SET);
 
841
 
 
842
        /* Read the header (first valid frame) */
 
843
        fread(pbtStreamBuffer,4,1,fpStream);
 
844
 
 
845
        /* the default VBR header. 48 kbps layer III, no padding, no crc */
 
846
        /* but sampling freq, mode andy copyright/copy protection taken */
 
847
        /* from first valid frame */
 
848
        pbtStreamBuffer[0]=(uint8_t) 0xff;
 
849
        abyte = (pbtStreamBuffer[1] & (char) 0xf1);
 
850
        {       
 
851
                int bitrate;
 
852
                if (1==gfp->version) {
 
853
                  bitrate = XING_BITRATE1;
 
854
                } else {
 
855
                  if (gfp->out_samplerate < 16000 )
 
856
                        bitrate = XING_BITRATE25;
 
857
                  else
 
858
                        bitrate = XING_BITRATE2;
 
859
                }
 
860
                
 
861
                if (gfp->VBR==vbr_off)
 
862
                        bitrate = gfp->brate;
 
863
        
 
864
        if (gfp->free_format)
 
865
            bbyte = 0x00;
 
866
        else
 
867
                    bbyte = 16*BitrateIndex(bitrate,gfp->version,gfp->out_samplerate);
 
868
        }
 
869
 
 
870
        /* Use as much of the info from the real frames in the
 
871
         * Xing header:  samplerate, channels, crc, etc...
 
872
         */ 
 
873
        if (gfp->version==1) {
 
874
          /* MPEG1 */
 
875
          pbtStreamBuffer[1]=abyte | (char) 0x0a;     /* was 0x0b; */
 
876
          abyte = pbtStreamBuffer[2] & (char) 0x0d;   /* AF keep also private bit */
 
877
          pbtStreamBuffer[2]=(char) bbyte | abyte;     /* 64kbs MPEG1 frame */
 
878
        }else{
 
879
          /* MPEG2 */
 
880
          pbtStreamBuffer[1]=abyte | (char) 0x02;     /* was 0x03; */
 
881
          abyte = pbtStreamBuffer[2] & (char) 0x0d;   /* AF keep also private bit */
 
882
          pbtStreamBuffer[2]=(char) bbyte | abyte;     /* 64kbs MPEG2 frame */
 
883
        }
 
884
 
 
885
        /* Clear all TOC entries */
 
886
        memset(btToc,0,sizeof(btToc));
 
887
 
 
888
    if (gfp->free_format) {
 
889
        int i;
 
890
        for (i = 1; i < NUMTOCENTRIES; ++i)
 
891
            btToc[i] = 255*i/100;
 
892
    } else {
 
893
        Xing_seek_table(&gfc->VBR_seek_table, btToc);
 
894
    }
 
895
    /*print_seeking (btToc);*/
 
896
 
 
897
        /* Start writing the tag after the zero frame */
 
898
        nStreamIndex=gfc->sideinfo_len;
 
899
        /* note! Xing header specifies that Xing data goes in the
 
900
         * ancillary data with NO ERROR PROTECTION.  If error protecton
 
901
         * in enabled, the Xing data still starts at the same offset,
 
902
         * and now it is in sideinfo data block, and thus will not
 
903
         * decode correctly by non-Xing tag aware players */
 
904
        if (gfp->error_protection) nStreamIndex -= 2;
 
905
 
 
906
        /* Put Vbr tag */
 
907
        if (gfp->VBR == vbr_off)
 
908
        {
 
909
                pbtStreamBuffer[nStreamIndex++]=VBRTag2[0];
 
910
                pbtStreamBuffer[nStreamIndex++]=VBRTag2[1];
 
911
                pbtStreamBuffer[nStreamIndex++]=VBRTag2[2];
 
912
                pbtStreamBuffer[nStreamIndex++]=VBRTag2[3];
 
913
 
 
914
        }
 
915
        else
 
916
        {
 
917
                pbtStreamBuffer[nStreamIndex++]=VBRTag[0];
 
918
                pbtStreamBuffer[nStreamIndex++]=VBRTag[1];
 
919
                pbtStreamBuffer[nStreamIndex++]=VBRTag[2];
 
920
                pbtStreamBuffer[nStreamIndex++]=VBRTag[3];
 
921
        }       
 
922
 
 
923
        /* Put header flags */
 
924
        CreateI4(&pbtStreamBuffer[nStreamIndex],FRAMES_FLAG+BYTES_FLAG+TOC_FLAG+VBR_SCALE_FLAG);
 
925
        nStreamIndex+=4;
 
926
 
 
927
        /* Put Total Number of frames */
 
928
        CreateI4(&pbtStreamBuffer[nStreamIndex],gfp->nVbrNumFrames);
 
929
        nStreamIndex+=4;
 
930
 
 
931
        /* Put Total file size */
 
932
        CreateI4(&pbtStreamBuffer[nStreamIndex],(int)lFileSize);
 
933
        nStreamIndex+=4;
 
934
 
 
935
        /* Put TOC */
 
936
        memcpy(&pbtStreamBuffer[nStreamIndex],btToc,sizeof(btToc));
 
937
        nStreamIndex+=sizeof(btToc);
 
938
 
 
939
 
 
940
        if (gfp->error_protection) {
 
941
          /* (jo) error_protection: add crc16 information to header */
 
942
          CRC_writeheader(gfc, (char*)pbtStreamBuffer);
 
943
        }
 
944
 
 
945
 
 
946
 
 
947
        /*work out CRC so far: initially crc = 0 */
 
948
        for (i = 0;i< nStreamIndex ;i++)
 
949
                crc = CRC_update_lookup(pbtStreamBuffer[i], crc);
 
950
 
 
951
        /*Put LAME VBR info*/
 
952
        nStreamIndex+=PutLameVBR(gfp, fpStream, pbtStreamBuffer + nStreamIndex, id3v2TagSize,crc);
 
953
 
 
954
#ifdef DEBUG_VBRTAG
 
955
        {
 
956
          VBRTAGDATA TestHeader;
 
957
          GetVbrTag(&TestHeader,pbtStreamBuffer);
 
958
        }
 
959
#endif
 
960
 
 
961
        /*Seek to the beginning of the stream */
 
962
        fseek(fpStream,id3v2TagSize,SEEK_SET);
 
963
 
 
964
        /* Put it all to disk again */
 
965
        if (fwrite(pbtStreamBuffer,(unsigned int)gfp->TotalFrameSize,1,fpStream)!=1)
 
966
        {
 
967
                return -1;
 
968
        }
 
969
 
 
970
 
 
971
        return 0;       /* success */
 
972
}
 
973
 
 
974