File: formatBitstream.c

package info (click to toggle)
toolame 02h-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 596 kB
  • ctags: 551
  • sloc: ansic: 10,332; sh: 111; makefile: 96
file content (550 lines) | stat: -rw-r--r-- 14,804 bytes parent folder | download
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*********************************************************************
  Copyright (c) 1995 ISO/IEC JTC1 SC29 WG1, All Rights Reserved
  formatBitstream.c
**********************************************************************/
/*
  Revision History:
 
  Date        Programmer                Comment
  ==========  ========================= ===============================
  1995/09/06  mc@fivebats.com           created
  1995/09/18  mc@fivebats.com           bugfix: WriteMainDataBits
  1995/09/20  mc@fivebats.com           bugfix: store_side_info
*/

#include "formatBitstream.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif

/* globals */
static int BitCount       = 0;
static int ThisFrameSize  = 0;
static int BitsRemaining  = 0;
static BitsFcnPtr PutBits = NULL;

/* forward declarations */
static int store_side_info( BF_FrameData *frameInfo );
static int main_data( BF_FrameData *frameInfo, BF_FrameResults *results );
static int side_queue_elements( int *forwardFrameLength, int *forwardSILength );
static void free_side_queues(void);
static void WriteMainDataBits( unsigned val,
                               unsigned nbits,
                               BF_FrameResults *results );
/*
  BitStreamFrame is the public interface to the bitstream
  formatting package. It writes one frame of main data per call.
 
  Assumptions:
  - The back pointer is zero on the first call
  - An integral number of bytes is written each frame
 
  You should be able to change the frame length, side info
  length, #channels, #granules on a frame-by-frame basis.
 
  See formatBitstream.h for more information about the data
  structures and the bitstream syntax.
*/
void
BF_BitstreamFrame( BF_FrameData *frameInfo, BF_FrameResults *results )
{
  int elements, forwardFrameLength, forwardSILength;

  assert( frameInfo->nGranules <= MAX_GRANULES );
  assert( frameInfo->nChannels <= MAX_CHANNELS );

  /* get ptr to bit writing function */
  PutBits = frameInfo->putbits;
  assert( PutBits );
  /* save SI and compute its length */
  results->SILength = store_side_info( frameInfo );

  /* write the main data, inserting SI to maintain framing */
  results->mainDataLength = main_data( frameInfo, results );

  /*
    Caller must ensure that back SI and main data are
    an integral number of bytes, since the back pointer
    can only point to a byte boundary and this code
    does not add stuffing bits
  */
  assert( (BitsRemaining % 8) == 0 );

  /* calculate nextBackPointer */
  elements = side_queue_elements( &forwardFrameLength, &forwardSILength );
  results->nextBackPtr = (BitsRemaining / 8) + (forwardFrameLength / 8) - (forwardSILength / 8);
}

/*
  FlushBitstream writes zeros into main data
  until all queued headers are written. The
  queue data buffers are also freed.
*/
void
BF_FlushBitstream( BF_FrameData *frameInfo, BF_FrameResults *results )
{
  int elements, forwardFrameLength, forwardSILength;

  /* get ptr to bit writing function */
  PutBits = frameInfo->putbits;
  assert( PutBits );

  elements = side_queue_elements( &forwardFrameLength, &forwardSILength );

  if ( elements )
    {
      int bitsRemaining = forwardFrameLength - forwardSILength;
      int wordsRemaining = bitsRemaining / 32;
      while ( wordsRemaining-- )
        WriteMainDataBits( 0, 32, results );
      WriteMainDataBits( 0, (bitsRemaining % 32), results );
    }

  results->mainDataLength = forwardFrameLength - forwardSILength;
  results->SILength       = forwardSILength;
  results->nextBackPtr    = 0;

  /* reclaim queue space */
  free_side_queues();

  /* reinitialize globals */
  BitCount       = 0;
  ThisFrameSize  = 0;
  BitsRemaining  = 0;
  PutBits        = NULL;
  return;
}

int
BF_PartLength( BF_BitstreamPart *part )
{
  BF_BitstreamElement *ep = part->element;
  int i, bits;

  bits = 0;
  for ( i = 0; i < part->nrEntries; i++, ep++ )
    bits += ep->length;
  return bits;
}


/*
  The following is all private to this file
*/

typedef struct
  {
    int frameLength;
    int SILength;
    int nGranules;
    int nChannels;
    BF_PartHolder *headerPH;
    BF_PartHolder *frameSIPH;
    BF_PartHolder *channelSIPH[MAX_CHANNELS];
    BF_PartHolder *spectrumSIPH[MAX_GRANULES][MAX_CHANNELS];
  }
MYSideInfo;

static MYSideInfo *get_side_info(void);
static int write_side_info(void);
typedef int (*PartWriteFcnPtr)( BF_BitstreamPart *part, BF_FrameResults *results );


static int
writePartMainData( BF_BitstreamPart *part, BF_FrameResults *results )
{
  BF_BitstreamElement *ep;
  int i, bits;

  assert( results );
  assert( part );

  bits = 0;
  ep = part->element;
  for ( i = 0; i < part->nrEntries; i++, ep++ )
    {
      WriteMainDataBits( ep->value, ep->length, results );
      bits += ep->length;
    }
  return bits;
}

static int
writePartSideInfo( BF_BitstreamPart *part, BF_FrameResults *results )
{
  BF_BitstreamElement *ep;
  int i, bits;

  assert( part );

  bits = 0;
  ep = part->element;
  for ( i = 0; i < part->nrEntries; i++, ep++ )
    {
      (*PutBits)( ep->value, ep->length );
      bits += ep->length;
    }
  return bits;
}

static int
main_data( BF_FrameData *fi, BF_FrameResults *results )
{
  int gr, ch, bits;
  PartWriteFcnPtr wp = writePartMainData;
  bits = 0;
  results->mainDataLength = 0;

  for ( gr = 0; gr < fi->nGranules; gr++ )
    for ( ch = 0; ch < fi->nChannels; ch++ )
      {
        bits += (*wp)( fi->scaleFactors[gr][ch], results );
        bits += (*wp)( fi->codedData[gr][ch],    results );
        bits += (*wp)( fi->userSpectrum[gr][ch], results );
      }
  bits += (*wp)( fi->userFrameData, results );
  return bits;
}

/*
  This is a wrapper around PutBits() that makes sure that the
  framing header and side info are inserted at the proper
  locations
*/

static void
WriteMainDataBits( unsigned val,
                   unsigned nbits,
                   BF_FrameResults *results )
{
  assert( nbits <= 32 );
  if ( BitCount == ThisFrameSize )
    {
      BitCount = write_side_info();
      BitsRemaining = ThisFrameSize - BitCount;
    }
  if ( nbits == 0 )
    return;
  if ( nbits > BitsRemaining )
    {
      unsigned extra = val >> (nbits - BitsRemaining);
      nbits -= BitsRemaining;
      (*PutBits)( extra, BitsRemaining );
      BitCount = write_side_info();
      BitsRemaining = ThisFrameSize - BitCount;
      (*PutBits)( val, nbits );
    }
  else
    (*PutBits)( val, nbits );
  BitCount += nbits;
  BitsRemaining -= nbits;
  assert( BitCount <= ThisFrameSize );
  assert( BitsRemaining >= 0 );
  assert( (BitCount + BitsRemaining) == ThisFrameSize );
}


static int
write_side_info(void)
{
  MYSideInfo *si;
  int bits, ch, gr;
  PartWriteFcnPtr wp = writePartSideInfo;

  bits = 0;
  si = get_side_info();
  ThisFrameSize = si->frameLength;
  bits += (*wp)( si->headerPH->part,  NULL );
  bits += (*wp)( si->frameSIPH->part, NULL );

  for ( ch = 0; ch < si->nChannels; ch++ )
    bits += (*wp)( si->channelSIPH[ch]->part, NULL );

  for ( gr = 0; gr < si->nGranules; gr++ )
    for ( ch = 0; ch < si->nChannels; ch++ )
      bits += (*wp)( si->spectrumSIPH[gr][ch]->part, NULL );
  return bits;
}

typedef struct side_info_link
  {
    struct side_info_link *next;
    MYSideInfo           side_info;
  }
side_info_link;

static struct side_info_link *side_queue_head   = NULL;
static struct side_info_link *side_queue_free   = NULL;

static void free_side_info_link( side_info_link *l );

static int
side_queue_elements( int *frameLength, int *SILength )
{
  int elements = 0;
  side_info_link *l = side_queue_head;

  *frameLength = 0;
  *SILength    = 0;

  for ( l = side_queue_head; l; l = l->next )
    {
      elements++;
      *frameLength += l->side_info.frameLength;
      *SILength    += l->side_info.SILength;
    }
  return elements;
}

static int
store_side_info( BF_FrameData *info )
{
  int ch, gr;
  side_info_link *l = NULL;
  /* obtain a side_info_link to store info */
  side_info_link *f = side_queue_free;
  int bits = 0;

  if ( f == NULL )
    { /* must allocate another */
#ifdef DEBUG
      static int n_si = 0;
      n_si += 1;
      fprintf( stderr, "allocating side_info_link number %d\n", n_si );
#endif
      l = (side_info_link *) calloc( 1, sizeof(side_info_link) );
      if ( l == NULL )
        {
          fprintf( stderr, "cannot allocate side_info_link" );
          exit( EXIT_FAILURE );
        }
      l->next = NULL;
      l->side_info.headerPH  = BF_newPartHolder( info->header->nrEntries );
      l->side_info.frameSIPH = BF_newPartHolder( info->frameSI->nrEntries );
      for ( ch = 0; ch < info->nChannels; ch++ )
        l->side_info.channelSIPH[ch] = BF_newPartHolder( info->channelSI[ch]->nrEntries );
      for ( gr = 0; gr < info->nGranules; gr++ )
        for ( ch = 0; ch < info->nChannels; ch++ )
          l->side_info.spectrumSIPH[gr][ch] = BF_newPartHolder( info->spectrumSI[gr][ch]->nrEntries );

    }
  else
    { /* remove from the free list */
      side_queue_free = f->next;
      f->next = NULL;
      l = f;
    }
  /* copy data */
  l->side_info.frameLength = info->frameLength;
  l->side_info.nGranules   = info->nGranules;
  l->side_info.nChannels   = info->nChannels;
  l->side_info.headerPH    = BF_LoadHolderFromBitstreamPart( l->side_info.headerPH,  info->header );
  l->side_info.frameSIPH   = BF_LoadHolderFromBitstreamPart( l->side_info.frameSIPH, info->frameSI );

  bits += BF_PartLength( info->header );
  bits += BF_PartLength( info->frameSI );

  for ( ch = 0; ch < info->nChannels; ch++ )
    {
      l->side_info.channelSIPH[ch] = BF_LoadHolderFromBitstreamPart( l->side_info.channelSIPH[ch],
                                     info->channelSI[ch] );
      bits += BF_PartLength( info->channelSI[ch] );
    }

  for ( gr = 0; gr < info->nGranules; gr++ )
    for ( ch = 0; ch < info->nChannels; ch++ )
      {
        l->side_info.spectrumSIPH[gr][ch] = BF_LoadHolderFromBitstreamPart( l->side_info.spectrumSIPH[gr][ch],
                                            info->spectrumSI[gr][ch] );
        bits += BF_PartLength( info->spectrumSI[gr][ch] );
      }
  l->side_info.SILength = bits;
  /* place at end of queue */
  f = side_queue_head;
  if ( f == NULL )
    {  /* empty queue */
      side_queue_head = l;
    }
  else
    { /* find last element */
      while ( f->next )
        f = f->next;
      f->next = l;
    }
  return bits;
}

static MYSideInfo*
get_side_info(void)
{
  side_info_link *f = side_queue_free;
  side_info_link *l = side_queue_head;

  /*
    If we stop here it means you didn't provide enough
    headers to support the amount of main data that was
    written.
  */
  assert( l );

  /* update queue head */
  side_queue_head = l->next;

  /*
    Append l to the free list. You can continue
    to use it until store_side_info is called
    again, which will not happen again for this
    frame.
  */
  side_queue_free = l;
  l->next = f;
  return &l->side_info;
}

static void
free_side_queues(void)
{
  side_info_link *l, *next;

  for ( l = side_queue_head; l; l = next )
    {
      next = l->next;
      free_side_info_link( l );
    }
  side_queue_head = NULL;

  for ( l = side_queue_free; l; l = next )
    {
      next = l->next;
      free_side_info_link( l );
    }
  side_queue_free = NULL;
}

static void
free_side_info_link( side_info_link *l )
{
  int gr, ch;

  l->side_info.headerPH  = BF_freePartHolder( l->side_info.headerPH );
  l->side_info.frameSIPH = BF_freePartHolder( l->side_info.frameSIPH );

  for ( ch = 0; ch < l->side_info.nChannels; ch++ )
    l->side_info.channelSIPH[ch] = BF_freePartHolder( l->side_info.channelSIPH[ch] );

  for ( gr = 0; gr < l->side_info.nGranules; gr++ )
    for ( ch = 0; ch < l->side_info.nChannels; ch++ )
      l->side_info.spectrumSIPH[gr][ch] = BF_freePartHolder( l->side_info.spectrumSIPH[gr][ch] );

  free( l );
}
/*
  Allocate a new holder of a given size
*/
BF_PartHolder *BF_newPartHolder( int max_elements )
{
  BF_PartHolder *newPH    = calloc( 1, sizeof(BF_PartHolder) );
  assert( newPH );
  newPH->max_elements  = max_elements;
  newPH->part          = calloc( 1, sizeof(BF_BitstreamPart) );
  assert( newPH->part );
  newPH->part->element = calloc( max_elements, sizeof(BF_BitstreamElement) );
  assert( newPH->part->element );
  newPH->part->nrEntries = 0;
  return newPH;
}

BF_PartHolder *BF_NewHolderFromBitstreamPart( BF_BitstreamPart *thePart )
{
  BF_PartHolder *newHolder = BF_newPartHolder( thePart->nrEntries );
  return BF_LoadHolderFromBitstreamPart( newHolder, thePart );
}

BF_PartHolder *BF_LoadHolderFromBitstreamPart( BF_PartHolder *theHolder, BF_BitstreamPart *thePart )
{
  BF_BitstreamElement *pElem;
  int i;

  theHolder->part->nrEntries = 0;
  for ( i = 0; i < thePart->nrEntries; i++ )
    {
      pElem = &(thePart->element[i]);
      theHolder = BF_addElement( theHolder, pElem );
    }
  return theHolder;
}

/*
  Grow or shrink a part holder. Always creates a new
  one of the right length and frees the old one after
  copying the data.
*/
BF_PartHolder *BF_resizePartHolder( BF_PartHolder *oldPH, int max_elements )
{
  int elems, i;
  BF_PartHolder *newPH;

#ifdef DEBUG
  fprintf( stderr, "Resizing part holder from %d to %d\n",
           oldPH->max_elements, max_elements );
#endif
  /* create new holder of the right length */
  newPH = BF_newPartHolder( max_elements );

  /* copy values from old to new */
  elems = (oldPH->max_elements > max_elements) ? max_elements : oldPH->max_elements;
  newPH->part->nrEntries = elems;
  for ( i = 0; i < elems; i++ )
    newPH->part->element[i] = oldPH->part->element[i];

  /* free old holder */
  BF_freePartHolder( oldPH );

  return newPH;
}

BF_PartHolder *BF_freePartHolder( BF_PartHolder *thePH )
{
  free( thePH->part->element );
  free( thePH->part );
  free( thePH );
  return NULL;
}

/*
  Add theElement to thePH, growing the holder if
  necessary. Returns ptr to the holder, which may
  not be the one you called it with!
*/
BF_PartHolder *BF_addElement( BF_PartHolder *thePH, BF_BitstreamElement *theElement )
{
  BF_PartHolder *retPH = thePH;
  int needed_entries = thePH->part->nrEntries + 1;
  int extraPad = 8;  /* add this many more if we need to resize */

  /* grow if necessary */
  if ( needed_entries > thePH->max_elements )
    retPH = BF_resizePartHolder( thePH, needed_entries + extraPad );

  /* copy the data */
  retPH->part->element[retPH->part->nrEntries++] = *theElement;
  return retPH;
}

/*
  Add a bit value and length to the element list in thePH
*/
BF_PartHolder *BF_addEntry( BF_PartHolder *thePH, uint32 value, uint16 length )
{
  BF_BitstreamElement myElement;
  myElement.value  = value;
  myElement.length = length;
  if ( length )
    return BF_addElement( thePH, &myElement );
  else
    return thePH;
}