File: H263plusVideoStreamParser.cpp

package info (click to toggle)
liblivemedia 2006.03.17-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,928 kB
  • ctags: 4,588
  • sloc: cpp: 35,064; ansic: 979; makefile: 78; sh: 73
file content (857 lines) | stat: -rw-r--r-- 35,748 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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

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 Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser 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
**********/
// "liveMedia"
// Copyright (c) 1996-2006 Live Networks, Inc.  All rights reserved.
// Author Bernhard Feiten
// A filter that breaks up an H.263plus video stream into frames.
// Based on MPEG4IP/mp4creator/h263.c

#include "H263plusVideoStreamParser.hh"
#include "H263plusVideoStreamFramer.hh"
//#include <string.h>
//#include "GroupsockHelper.hh"


H263plusVideoStreamParser::H263plusVideoStreamParser(
                              H263plusVideoStreamFramer* usingSource,
                              FramedSource* inputSource)
                              : StreamParser(inputSource,
                                   FramedSource::handleClosure,
                                   usingSource,
                                   &H263plusVideoStreamFramer::continueReadProcessing,
                                   usingSource),
                                fUsingSource(usingSource),
                                fnextTR(0),
                                fcurrentPT(0)
{
   memset(fStates, 0, sizeof(fStates));
   memset(&fNextInfo, 0, sizeof(fNextInfo));
   memset(&fCurrentInfo, 0, sizeof(fCurrentInfo));
   memset(&fMaxBitrateCtx, 0, sizeof(fMaxBitrateCtx));
   memset(fNextHeader,0, H263_REQUIRE_HEADER_SIZE_BYTES);
}

///////////////////////////////////////////////////////////////////////////////
H263plusVideoStreamParser::~H263plusVideoStreamParser()
{
}

///////////////////////////////////////////////////////////////////////////////
void H263plusVideoStreamParser::restoreSavedParserState()
{
   StreamParser::restoreSavedParserState();
   fTo = fSavedTo;
   fNumTruncatedBytes = fSavedNumTruncatedBytes;
}

///////////////////////////////////////////////////////////////////////////////
void H263plusVideoStreamParser::setParseState()
{
   fSavedTo = fTo;
   fSavedNumTruncatedBytes = fNumTruncatedBytes;
   saveParserState();  // Needed for the parsing process in StreamParser
}


///////////////////////////////////////////////////////////////////////////////
void H263plusVideoStreamParser::registerReadInterest(
                                   unsigned char* to,
                                   unsigned maxSize)
{
   fStartOfFrame = fTo = fSavedTo = to;
   fLimit = to + maxSize;
   fMaxSize = maxSize;
   fNumTruncatedBytes = fSavedNumTruncatedBytes = 0;
}

///////////////////////////////////////////////////////////////////////////////
// parse() ,  derived from H263Creator of MPEG4IP, h263.c
unsigned H263plusVideoStreamParser::parse(u_int64_t & currentDuration)
{

//   u_int8_t       frameBuffer[H263_BUFFER_SIZE]; // The input buffer
                 // Pointer which tells LoadNextH263Object where to read data to
//   u_int8_t*      pFrameBuffer = fTo + H263_REQUIRE_HEADER_SIZE_BYTES;
   u_int32_t      frameSize;        // The current frame size
                                // Pointer to receive address of the header data
//   u_int8_t*      pCurrentHeader;// = pFrameBuffer;
//   u_int64_t      currentDuration;  // The current frame's duration
   u_int8_t       trDifference;     // The current TR difference
                                   // The previous TR difference
//   u_int8_t       prevTrDifference = H263_BASIC_FRAME_RATE;
//   u_int64_t      totalDuration = 0;// Duration accumulator
//   u_int64_t      avgBitrate;       // Average bitrate
//   u_int64_t      totalBytes = 0;   // Size accumulator


   try    // The get data routines of the class FramedFilter returns an error when
   {      // the buffer is empty. This occurs at the beginning and at the end of the file.
      fCurrentInfo = fNextInfo;

      // Parse 1 frame
      // For the first time, only the first frame's header is returned.
      // The second time the full first frame is returned
      frameSize = parseH263Frame(); 

      currentDuration = 0;
      if ((frameSize > 0)){ 
         // We were able to acquire a frame from the input.

         // Parse the returned frame header (if any)
         if (!ParseShortHeader(fTo, &fNextInfo))
           ;// fprintf(stderr,"H263plusVideoStreamParser: Fatal error\n");
         trDifference = GetTRDifference(fNextInfo.tr, fCurrentInfo.tr);

         // calculate the current frame duration
         currentDuration = CalculateDuration(trDifference);

         // Accumulate the frame's size and duration for avgBitrate calculation
         //totalDuration += currentDuration;
         //totalBytes += frameSize;
         //  If needed, recalculate bitrate information
         //    if (h263Bitrates)
         //GetMaxBitrate(&fMaxBitrateCtx, frameSize, prevTrDifference);
         //prevTrDifference = trDifference;
      }
   } catch (int /*e*/) {
#ifdef DEBUG
      fprintf(stderr, "H263plusVideoStreamParser::parse() EXCEPTION (This is normal behavior - *not* an error)\n");
#endif
      frameSize=0;
   }

   setParseState(); // Needed for the parsing process in StreamParser

   return frameSize;
}


///////////////////////////////////////////////////////////////////////////////
// parseH263Frame derived from LoadNextH263Object of MPEG4IP 
// - service routine that reads a single frame from the input file.
// It shall fill the input buffer with data up until - and including - the
// next start code and shall report back both the number of bytes read and a
// pointer to the next start code. The first call to this function shall only
// yield a pointer with 0 data bytes and the last call to this function shall
// only yield data bytes with a NULL pointer as the next header.
//
// TODO: This function only supports valid bit streams. Upon error, it fails
// without the possibility to recover. A Better idea would be to skip frames
// until a parsable frame is read from the file.
//
// Parameters:
//      ppNextHeader - output parameter that upon return points to the location
//                     of the next frame's head in the buffer.
//                     This pointer shall be NULL for the last frame read.
// Returns the total number of bytes read.
// Uses FrameFileSource intantiated by constructor.
///////////////////////////////////////////////////////////////////////////////
int H263plusVideoStreamParser::parseH263Frame( )
{
   char     row = 0;
   u_int8_t * bufferIndex = fTo;
   // The buffer end which will allow the loop to leave place for
   // the additionalBytesNeeded
   u_int8_t * bufferEnd = fTo + fMaxSize - ADDITIONAL_BYTES_NEEDED - 1;	

   memcpy(fTo, fNextHeader, H263_REQUIRE_HEADER_SIZE_BYTES);
   bufferIndex += H263_REQUIRE_HEADER_SIZE_BYTES;
   

   // The state table and the following loop implements a state machine enabling
   // us to read bytes from the file until (and inclusing) the requested
   // start code (00 00 8X) is found

   // Initialize the states array, if it hasn't been initialized yet...
   if (!fStates[0][0]) {
      // One 00 was read
      fStates[0][0] = 1;
      // Two sequential 0x00 ware read
      fStates[1][0] = fStates[2][0] = 2;
      // A full start code was read
      fStates[2][128] = fStates[2][129] = fStates[2][130] = fStates[2][131] = -1;
   }

   // Read data from file into the output buffer until either a start code
   // is found, or the end of file has been reached.
   do {
      *bufferIndex = get1Byte();
   } while ((bufferIndex < bufferEnd) &&                    // We have place in the buffer
            ((row = fStates[row][*(bufferIndex++)]) != -1)); // Start code was not found

   if (row != -1) {
      fprintf(stderr, "%s: Buffer too small (%u)\n",
         "h263reader:", bufferEnd - fTo + ADDITIONAL_BYTES_NEEDED);
      return 0;
   }

   // Cool ... now we have a start code
   // Now we just have to read the additionalBytesNeeded
   getBytes(bufferIndex, ADDITIONAL_BYTES_NEEDED);
   memcpy(fNextHeader, bufferIndex - H263_STARTCODE_SIZE_BYTES, H263_REQUIRE_HEADER_SIZE_BYTES);

	int sz = bufferIndex - fTo - H263_STARTCODE_SIZE_BYTES;

   if (sz == 5) // first frame
      memcpy(fTo, fTo+H263_REQUIRE_HEADER_SIZE_BYTES, H263_REQUIRE_HEADER_SIZE_BYTES);

   return sz;
}


////////////////////////////////////////////////////////////////////////////////
// ParseShortHeader - service routine that accepts a buffer containing a frame
// header and extracts relevant codec information from it.
//
// NOTE: the first bit in the following commnets is 0 (zero).
//
//       0                   1                   2                   3
//       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//      |      PSC (Picture Start Code=22 bits)     |  (TR=8 bits)  |   >
//      |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0|               |1 0>
//      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//      <   (PTYPE=13 bits)   |
//      <. . .|(FMT)|Z|. . . .|
//      +-+-+-+-+-+-+-+-+-+-+-+
//      -> PTYPE.FMT contains a width/height identification
//      -> PTYPE.Z   is 1 for P-Frames, 0 for I-Frames
//      Note: When FMT is 111, there is an extended PTYPE...
//
// Inputs:
//      headerBuffer - pointer to the current header buffer
//      outputInfoStruct - pointer to the structure receiving the data
// Outputs:
//      This function returns a structure of important codec-specific
//      information (The Temporal Reference bits, width & height of the current
//      frame and the sync - or "frame type" - bit. It reports success or
//      failure to the calling function.
////////////////////////////////////////////////////////////////////////////////
bool H263plusVideoStreamParser::ParseShortHeader(
                                   u_int8_t *headerBuffer,
                                   H263INFO *outputInfoStruct)
{
   u_int8_t fmt = 0;
   // Extract temporal reference (TR) from the buffer (bits 22-29 inclusive)
   outputInfoStruct->tr  = (headerBuffer[2] << 6) & 0xC0; // 2 LS bits out of the 3rd byte
   outputInfoStruct->tr |= (headerBuffer[3] >> 2) & 0x3F; // 6 MS bits out of the 4th byte
   // Extract the FMT part of PTYPE from the buffer (bits 35-37 inclusive)
   fmt = (headerBuffer[4] >> 2) & 0x07; // bits 3-5 ouf of the 5th byte
   // If PTYPE is not supported, return a failure notice to the calling function
   // FIXME: PLUSPTYPE is not supported
   if (fmt == 0x07) {
      return false;
   }
   // If PTYPE is supported, calculate the current width and height according to
   // a predefined table
   if (!GetWidthAndHeight(fmt, &(outputInfoStruct->width),
                               &(outputInfoStruct->height))) {
      return false;
   }
   // Extract the frame-type bit, which is the 9th bit of PTYPE (bit 38)
   outputInfoStruct->isSyncFrame = !(headerBuffer[4] & 0x02);

  return true;
}

////////////////////////////////////////////////////////////////////////////////
// GetMaxBitrate- service routine that accepts frame information and
// derives bitrate information from it. This function uses a sliding window
// technique to calculate the maximum bitrates in any window of 1 second
// inside the file.
// The sliding window is implemented with a table of bitrates for the last
// second (30 entries - one entry per TR unit).
//
// Inputs:
//      ctx - context for this function
//      frameSize - the size of the current frame in bytes
//      frameTRDiff - the "duration" of the frame in TR units
// Outputs:
//      This function returns the up-to-date maximum bitrate
////////////////////////////////////////////////////////////////////////////////
void H263plusVideoStreamParser::GetMaxBitrate( MaxBitrate_CTX *ctx,
                                               u_int32_t      frameSize,
                                               u_int8_t       frameTRDiff)
{
   if (frameTRDiff == 0)
      return;

   // Calculate the current frame's bitrate as bits per TR unit (round the result
   // upwards)
   u_int32_t frameBitrate = frameSize * 8 / frameTRDiff + 1;

   // for each TRdiff received,
   while (frameTRDiff--) {
      // Subtract the oldest bitrate entry from the current bitrate
      ctx->windowBitrate -= ctx->bitrateTable[ctx->tableIndex];
      // Update the oldest bitrate entry with the current frame's bitrate
      ctx->bitrateTable[ctx->tableIndex] = frameBitrate;
      // Add the current frame's bitrate to the current bitrate
      ctx->windowBitrate += frameBitrate;
      // Check if we have a new maximum bitrate
      if (ctx->windowBitrate > ctx->maxBitrate) {
         ctx->maxBitrate = ctx->windowBitrate;
	  }
      // Advance the table index
      // Wrapping around the bitrateTable size
      ctx->tableIndex = (ctx->tableIndex + 1) %
        ( sizeof(ctx->bitrateTable) / sizeof(ctx->bitrateTable[0]) );
   }
}

////////////////////////////////////////////////////////////////////////////////
// CalculateDuration - service routine that calculates the current frame's
// duration in milli-seconds using it's duration in TR units.
//  - In order not to accumulate the calculation error, we are using the TR
// duration to calculate the current and the next frame's presentation time in
// milli-seconds.
//
// Inputs: trDiff - The current frame's duration in TR units
// Return: The current frame's duration in milli-seconds
////////////////////////////////////////////////////////////////////////////////
u_int64_t H263plusVideoStreamParser::CalculateDuration(u_int8_t trDiff)
{
  //static u_int32_t nextTR    = 0;   // The next frame's presentation time in TR units
  //static u_int64_t currentPT = 0;   // The current frame's presentation time in milli-seconds
  u_int64_t        nextPT;          // The next frame's presentation time in milli-seconds
  u_int64_t        duration;        // The current frame's duration in milli-seconds

  fnextTR += trDiff;
  // Calculate the next frame's presentation time, in milli-seconds
  nextPT = (fnextTR * 1001) / H263_BASIC_FRAME_RATE;
  // The frame's duration is the difference between the next presentation
  // time and the current presentation time.
  duration = nextPT - fcurrentPT;
  // "Remember" the next presentation time for the next time this function is called
  fcurrentPT = nextPT;

  return duration;
}

////////////////////////////////////////////////////////////////////////////////
bool H263plusVideoStreamParser::GetWidthAndHeight( u_int8_t  fmt,
                                                   u_int16_t *width,
                                                   u_int16_t *height)
{
   // The 'fmt' corresponds to bits 5-7 of the PTYPE
   static struct {
      u_int16_t width;
      u_int16_t height;
   } dimensionsTable[8] = {
	   { 0,    0 },      // 000 - 0 - forbidden, generates an error
	   { 128,  96 },     // 001 - 1 - Sub QCIF
	   { 176,  144 },    // 010 - 2 - QCIF
	   { 352,  288 },    // 011 - 3 - CIF
	   { 704,  576 },    // 100 - 4 - 4CIF
	   { 1409, 1152 },   // 101 - 5 - 16CIF
	   { 0,    0 },      // 110 - 6 - reserved, generates an error
	   { 0,    0 }       // 111 - 7 - extended, not supported by profile 0
   };

   if (fmt > 7)
      return false;

   *width  = dimensionsTable[fmt].width;
   *height = dimensionsTable[fmt].height;

   if (*width  == 0)
     return false;

   return true;
}

////////////////////////////////////////////////////////////////////////////////
u_int8_t H263plusVideoStreamParser::GetTRDifference(
                                              u_int8_t nextTR,
                                              u_int8_t currentTR)
{
   if (currentTR > nextTR) {
      // Wrap around 255...
      return nextTR + (256 - currentTR);
   } else {
      return nextTR - currentTR;
   }
}







////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// this is the h263.c file of MPEG4IP mp4creator
/*
#include "mp4creator.h"

// Default timescale for H.263 (1000ms)
#define H263_TIMESCALE 1000
// Default H263 frame rate (30fps)
#define H263_BASIC_FRAME_RATE 30

// Minimum number of bytes needed to parse an H263 header
#define H263_REQUIRE_HEADER_SIZE_BYTES 5
// Number of bytes the start code requries
#define H263_STARTCODE_SIZE_BYTES 3
// This is the input buffer's size. It should contain
// 1 frame with the following start code
#define H263_BUFFER_SIZE 256 * 1024
// The default max different (in %) betwqeen max and average bitrates
#define H263_DEFAULT_CBR_TOLERANCE  10

// The following structure holds information extracted from each frame's header:
typedef struct _H263INFO {
  u_int8_t  tr;                 // Temporal Reference, used in duration calculation
  u_int16_t width;              // Width of the picture
  u_int16_t height;             // Height of the picture
  bool      isSyncFrame;        // Frame type (true = I frame = "sync" frame)
} H263INFO;

// Context for the GetMaxBitrate function
typedef struct _MaxBitrate_CTX {
  u_int32_t  bitrateTable[H263_BASIC_FRAME_RATE];// Window of 1 second
  u_int32_t  windowBitrate;              // The bitrate of the current window
  u_int32_t  maxBitrate;                 // The up-to-date maximum bitrate
  u_int32_t  tableIndex;                 // The next TR unit to update
} MaxBitrate_CTX;

// Forward declarations:
static int LoadNextH263Object(  FILE           *inputFileHandle,
                                u_int8_t       *frameBuffer,
                                u_int32_t      *frameBufferSize,
                                u_int32_t       additionalBytesNeeded,
                                u_int8_t      **ppNextHeader);

static bool ParseShortHeader(   u_int8_t       *headerBuffer,
                                H263INFO       *outputInfoStruct);

static u_int8_t GetTRDifference(u_int8_t        nextTR,
                                u_int8_t        currentTR);

static void GetMaxBitrate(      MaxBitrate_CTX *ctx,
                                u_int32_t       frameSize,
                                u_int8_t        frameTRDiff);

static MP4Duration CalculateDuration(u_int8_t   trDiff);

static bool GetWidthAndHeight(  u_int8_t        fmt,
                                u_int16_t      *width,
                                u_int16_t      *height);

static char   states[3][256];
/ *
 * H263Creator - Main function
 * Inputs:
 *      outputFileHandle - The handle of the output file
 *      inputFileHandle - The handle of the input file
 *      Codec-specific parameters:
 *              H263Level - H.263 Level used for this track
 *              H263Profile - H.263 Profile used for this track
 *              H263Bitrates - A Parameter indicating whether the function
 *                             should calculate H263 bitrates or not.
 *              cbrTolerance - CBR tolerance indicates when to set the
 *                             average bitrate.
 * Outputs:
 *      This function returns either the track ID of the newly added track upon
 *      success or a predefined value representing an erroneous state.
 * /
MP4TrackId H263Creator(MP4FileHandle outputFileHandle,
                       FILE*         inputFileHandle,
                       u_int8_t      h263Profile,
                       u_int8_t      h263Level,
                       bool          h263Bitrates,
                       u_int8_t      cbrTolerance)
{
  H263INFO       nextInfo;   // Holds information about the next frame
  H263INFO       currentInfo;// Holds information about the current frame
  MaxBitrate_CTX maxBitrateCtx;// Context for the GetMaxBitrate function
  memset(&nextInfo, 0, sizeof(nextInfo));
  memset(&currentInfo, 0, sizeof(currentInfo));
  memset(&maxBitrateCtx, 0, sizeof(maxBitrateCtx));
  memset(states, 0, sizeof(states));
  u_int8_t       frameBuffer[H263_BUFFER_SIZE]; // The input buffer
                 // Pointer which tells LoadNextH263Object where to read data to
  u_int8_t*      pFrameBuffer = frameBuffer + H263_REQUIRE_HEADER_SIZE_BYTES;
  u_int32_t      frameSize;        // The current frame size
                                // Pointer to receive address of the header data
  u_int8_t*      pCurrentHeader = pFrameBuffer;
  MP4Duration    currentDuration;  // The current frame's duration
  u_int8_t       trDifference;     // The current TR difference
                                   // The previous TR difference
  u_int8_t       prevTrDifference = H263_BASIC_FRAME_RATE;
  MP4Duration    totalDuration = 0;// Duration accumulator
  MP4Duration    avgBitrate;       // Average bitrate
  u_int64_t      totalBytes = 0;   // Size accumulator
  MP4TrackId     trackId = MP4_INVALID_TRACK_ID; // Our MP4 track
  bool           stay = true;      // loop flag

  while (stay) {
    currentInfo = nextInfo;
    memmove(frameBuffer, pCurrentHeader, H263_REQUIRE_HEADER_SIZE_BYTES);
    frameSize = H263_BUFFER_SIZE - H263_REQUIRE_HEADER_SIZE_BYTES;
    // Read 1 frame and the next frame's header from the file.
    // For the first frame, only the first frame's header is returned.
    // For the last frame, only the last frame's data is returned.
    if (! LoadNextH263Object(inputFileHandle, pFrameBuffer, &frameSize,
          H263_REQUIRE_HEADER_SIZE_BYTES - H263_STARTCODE_SIZE_BYTES,
          &pCurrentHeader))
      break; // Fatal error ...

    if (pCurrentHeader) {
      // Parse the returned frame header (if any)
      if (!ParseShortHeader(pCurrentHeader, &nextInfo))
        break; // Fatal error
      trDifference = GetTRDifference(nextInfo.tr, currentInfo.tr);
    } else {
      // This is the last frame ... we have to fake the trDifference ...
      trDifference = 1;
      // No header data has been read at this iteration, so we have to manually
      // add the frame's header we read at the previous iteration.
      // Note that LoadNextH263Object returns the number of bytes read, which
      // are the current frame's data and the next frame's header
      frameSize += H263_REQUIRE_HEADER_SIZE_BYTES;
      // There is no need for the next iteration ...
      stay = false;
    }

    // If this is the first iteration ...
    if (currentInfo.width == 0) {
      // If we have more data than just the header
      if ((frameSize > H263_REQUIRE_HEADER_SIZE_BYTES) ||
          !pCurrentHeader)  // Or no header at all
        break;     // Fatal error
      else
        continue;  // We have only the first frame's header ...
    }

    if (trackId == MP4_INVALID_TRACK_ID) {
      //  If a track has not been added yet, add the track to the file.
      trackId = MP4AddH263VideoTrack(outputFileHandle, H263_TIMESCALE,
          0, currentInfo.width, currentInfo.height,
          h263Level, h263Profile, 0, 0);
      if (trackId == MP4_INVALID_TRACK_ID)
        break; // Fatal error
    }

    // calculate the current frame duration
    currentDuration = CalculateDuration(trDifference);
    // Write the current frame to the file.
    if (!MP4WriteSample(outputFileHandle, trackId, frameBuffer, frameSize,
          currentDuration, 0, currentInfo.isSyncFrame))
      break; // Fatal error

    // Accumulate the frame's size and duration for avgBitrate calculation
    totalDuration += currentDuration;
    totalBytes += frameSize;
    //  If needed, recalculate bitrate information
    if (h263Bitrates)
      GetMaxBitrate(&maxBitrateCtx, frameSize, prevTrDifference);
    prevTrDifference = trDifference;
  } // while (stay)

  // If this is the last frame,
  if (!stay) {
    // If needed and possible, update bitrate information in the file
    if (h263Bitrates && totalDuration) {
      avgBitrate = (totalBytes * 8 * H263_TIMESCALE) / totalDuration;
      if (cbrTolerance == 0)
        cbrTolerance = H263_DEFAULT_CBR_TOLERANCE;
      // Same as: if (maxBitrate / avgBitrate > (cbrTolerance + 100) / 100.0)
      if (maxBitrateCtx.maxBitrate * 100 > (cbrTolerance + 100) * avgBitrate)
        avgBitrate = 0;
      MP4SetH263Bitrates(outputFileHandle, trackId,
          avgBitrate, maxBitrateCtx.maxBitrate);
    }
    // Return the newly added track ID
    return trackId;
  }

  // If we got to here... something went wrong ...
  fprintf(stderr,
    "%s: Could not parse input file, invalid video stream?\n", ProgName);
  // Upon failure, delete the newly added track if it has been added
  if (trackId != MP4_INVALID_TRACK_ID) {
    MP4DeleteTrack(outputFileHandle, trackId);
  }
  return MP4_INVALID_TRACK_ID;
}

/ *
 * LoadNextH263Object - service routine that reads a single frame from the input
 * file. It shall fill the input buffer with data up until - and including - the
 * next start code and shall report back both the number of bytes read and a
 * pointer to the next start code. The first call to this function shall only
 * yield a pointer with 0 data bytes and the last call to this function shall
 * only yield data bytes with a NULL pointer as the next header.
 *
 * TODO: This function only supports valid bit streams. Upon error, it fails
 * without the possibility to recover. A Better idea would be to skip frames
 * until a parsable frame is read from the file.
 *
 * Parameters:
 *      inputFileHandle - The handle of the input file
 *      frameBuffer - buffer where to place read data
 *      frameBufferSize - in/out parameter indicating the size of the buffer on
 *                          entry and the number of bytes copied to the buffer upon
 *                          return
 *      additionalBytesNeeded - indicates how many additional bytes are to be read
 *                          from the next frame's header (over the 3 bytes that
 *                          are already read).
 *                          NOTE: This number MUST be > 0
 *      ppNextHeader - output parameter that upon return points to the location
 *                     of the next frame's head in the buffer
 * Outputs:
 *      This function returns two pieces of information:
 *      1. The total number of bytes read.
 *      2. A Pointer to the header of the next frame. This pointer shall be NULL
 *      for the last frame read.
 * /
static int LoadNextH263Object(  FILE           *inputFileHandle,
                                u_int8_t       *frameBuffer,
                                u_int32_t      *frameBufferSize,
                                u_int32_t       additionalBytesNeeded,
                                u_int8_t      **ppNextHeader)
{
  // This table and the following loop implements a state machine enabling
  // us to read bytes from the file untill (and inclusing) the requested
  // start code (00 00 8X) is found
  int8_t        row = 0;
  u_int8_t     *bufferStart = frameBuffer;
  // The buffer end which will allow the loop to leave place for
  // the additionalBytesNeeded
  u_int8_t     *bufferEnd = frameBuffer + *frameBufferSize -
                                              additionalBytesNeeded - 1;

  // Initialize the states array, if it hasn't been initialized yet...
  if (!states[0][0]) {
    // One 00 was read
    states[0][0] = 1;
    // Two sequential 0x00 ware read
    states[1][0] = states[2][0] = 2;
    // A full start code was read
    states[2][128] = states[2][129] = states[2][130] = states[2][131] = -1;
  }

  // Read data from file into the output buffer until either a start code
  // is found, or the end of file has been reached.
  do {
    if (fread(frameBuffer, 1, 1, inputFileHandle) != 1){
      // EOF or other error before we got a start code
      *ppNextHeader = NULL;
      *frameBufferSize = frameBuffer - bufferStart;
      return 1;
    }
  } while ((frameBuffer < bufferEnd) &&                    // We have place in the buffer
           ((row = states[row][*(frameBuffer++)]) != -1)); // Start code was not found
  if (row != -1) {
    fprintf(stderr, "%s: Buffer too small (%u)\n",
            ProgName, bufferEnd - bufferStart + additionalBytesNeeded);
    return 0;
  }

  // Cool ... now we have a start code
  *ppNextHeader = frameBuffer - H263_STARTCODE_SIZE_BYTES;
  *frameBufferSize = frameBuffer - bufferStart + additionalBytesNeeded;

  // Now we just have to read the additionalBytesNeeded
  if(fread(frameBuffer, additionalBytesNeeded, 1, inputFileHandle) != 1) {
    /// We got a start code but can't read additionalBytesNeeded ... that's a fatal error
    fprintf(stderr, "%s: Invalid H263 bitstream\n", ProgName);
    return 0;
  }

  return 1;
}


/ *
 * ParseShortHeader - service routine that accepts a buffer containing a frame
 * header and extracts relevant codec information from it.
 *
 * NOTE: the first bit in the following commnets is 0 (zero).
 *
 *
 *       0                   1                   2                   3
 *       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      |      PSC (Picture Start Code=22 bits)     |  (TR=8 bits)  |   >
 *      |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0|               |1 0>
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      <   (PTYPE=13 bits)   |
 *      <. . .|(FMT)|Z|. . . .|
 *      +-+-+-+-+-+-+-+-+-+-+-+
 *      -> PTYPE.FMT contains a width/height identification
 *      -> PTYPE.Z   is 1 for P-Frames, 0 for I-Frames
 *      Note: When FMT is 111, there is an extended PTYPE...
 *
 * Inputs:
 *      headerBuffer - pointer to the current header buffer
 *      outputInfoStruct - pointer to the structure receiving the data
 * Outputs:
 *      This function returns a structure of important codec-specific
 *      information (The Temporal Reference bits, width & height of the current
 *      frame and the sync - or "frame type" - bit. It reports success or
 *      failure to the calling function.
 * /
static bool ParseShortHeader(   u_int8_t       *headerBuffer,
                                H263INFO       *outputInfoStruct)
{
  u_int8_t fmt = 0;
  // Extract temporal reference (TR) from the buffer (bits 22-29 inclusive)
  outputInfoStruct->tr  = (headerBuffer[2] << 6) & 0xC0; // 2 LS bits out of the 3rd byte
  outputInfoStruct->tr |= (headerBuffer[3] >> 2) & 0x3F; // 6 MS bits out of the 4th byte
  // Extract the FMT part of PTYPE from the buffer (bits 35-37 inclusive)
  fmt = (headerBuffer[4] >> 2) & 0x07; // bits 3-5 ouf of the 5th byte
  // If PTYPE is not supported, return a failure notice to the calling function
  // FIXME: PLUSPTYPE is not supported
   if (fmt == 0x07) {
    return false;
  }
  // If PTYPE is supported, calculate the current width and height according to
  // a predefined table
  if (!GetWidthAndHeight(fmt, &(outputInfoStruct->width),
                              &(outputInfoStruct->height))) {
    return false;
  }
  // Extract the frame-type bit, which is the 9th bit of PTYPE (bit 38)
  outputInfoStruct->isSyncFrame = !(headerBuffer[4] & 0x02);

  return true;
}

/ *
 * GetMaxBitrate- service routine that accepts frame information and
 * derives bitrate information from it. This function uses a sliding window
 * technique to calculate the maximum bitrates in any window of 1 second
 * inside the file.
 * The sliding window is implemented with a table of bitrates for the last
 * second (30 entries - one entry per TR unit).
 *
 * Inputs:
 *      ctx - context for this function
 *      frameSize - the size of the current frame in bytes
 *      frameTRDiff - the "duration" of the frame in TR units
 * Outputs:
 *      This function returns the up-to-date maximum bitrate
 * /
static void GetMaxBitrate(      MaxBitrate_CTX *ctx,
                                u_int32_t       frameSize,
                                u_int8_t        frameTRDiff)
{
  if (frameTRDiff == 0)
    return;

  // Calculate the current frame's bitrate as bits per TR unit (round the result
  // upwards)
  u_int32_t frameBitrate = frameSize * 8 / frameTRDiff + 1;

  // for each TRdiff received,
  while (frameTRDiff--) {
    // Subtract the oldest bitrate entry from the current bitrate
    ctx->windowBitrate -= ctx->bitrateTable[ctx->tableIndex];
     // Update the oldest bitrate entry with the current frame's bitrate
    ctx->bitrateTable[ctx->tableIndex] = frameBitrate;
    // Add the current frame's bitrate to the current bitrate
    ctx->windowBitrate += frameBitrate;
    // Check if we have a new maximum bitrate
    if (ctx->windowBitrate > ctx->maxBitrate) {
      ctx->maxBitrate = ctx->windowBitrate;
    }
    // Advance the table index
    ctx->tableIndex = (ctx->tableIndex + 1) %
        // Wrapping around the bitrateTable size
        ( sizeof(ctx->bitrateTable) / sizeof(ctx->bitrateTable[0]) );
  }
}

/ *
 * CalculateDuration - service routine that calculates the current frame's
 * duration in milli-seconds using it's duration in TR units.
 *  - In order not to accumulate the calculation error, we are using the TR
 * duration to calculate the current and the next frame's presentation time in
 * milli-seconds.
 *
 * Inputs:
 *      trDiff - The current frame's duration in TR units
 * Outputs:
 *      The current frame's duration in milli-seconds
 * /
static MP4Duration CalculateDuration(u_int8_t   trDiff)
{
  static u_int32_t    nextTR    = 0;   // The next frame's presentation time in TR units
  static MP4Duration  currentPT = 0;   // The current frame's presentation time in milli-seconds
  MP4Duration         nextPT;          // The next frame's presentation time in milli-seconds
  MP4Duration         duration;        // The current frame's duration in milli-seconds

  nextTR += trDiff;
  // Calculate the next frame's presentation time, in milli-seconds
  nextPT = (nextTR * 1001) / H263_BASIC_FRAME_RATE;
  // The frame's duration is the difference between the next presentation
  // time and the current presentation time.
  duration = nextPT - currentPT;
  // "Remember" the next presentation time for the next time this function is
  // called
  currentPT = nextPT;

  return duration;
}

static bool GetWidthAndHeight(  u_int8_t        fmt,
                                u_int16_t      *width,
                                u_int16_t      *height)
{
  // The 'fmt' corresponds to bits 5-7 of the PTYPE
  static struct {
    u_int16_t width;
    u_int16_t height;
  } dimensionsTable[8] = {
    { 0,    0 },      // 000 - 0 - forbidden, generates an error
    { 128,  96 },     // 001 - 1 - Sub QCIF
    { 176,  144 },    // 010 - 2 - QCIF
    { 352,  288 },    // 011 - 3 - CIF
    { 704,  576 },    // 100 - 4 - 4CIF
    { 1409, 1152 },   // 101 - 5 - 16CIF
    { 0,    0 },      // 110 - 6 - reserved, generates an error
    { 0,    0 }       // 111 - 7 - extended, not supported by profile 0
  };

  if (fmt > 7)
    return false;

  *width  = dimensionsTable[fmt].width;
  *height = dimensionsTable[fmt].height;

  if (*width  == 0)
    return false;

  return true;
}

static u_int8_t GetTRDifference(u_int8_t        nextTR,
                                u_int8_t        currentTR)
{
  if (currentTR > nextTR) {
    // Wrap around 255...
    return nextTR + (256 - currentTR);
  } else {
    return nextTR - currentTR;
  }
}

*/