videoeditorengine/h263decoder/src/sync.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 * H.263 sync code functions.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include "h263dConfig.h"
       
    24 
       
    25 #include "sync.h"
       
    26 #include "errcodes.h"
       
    27 #include "debug.h"
       
    28 #include "biblin.h"
       
    29 
       
    30 
       
    31 
       
    32 /*
       
    33  * Global functions
       
    34  */
       
    35 
       
    36 /* {{-output"sncCheckSync.txt"}} */
       
    37 /*
       
    38  * sncCheckSync
       
    39  *    
       
    40  *
       
    41  * Parameters:
       
    42  *    buffer                     a pointer to a bit buffer structure
       
    43  *    numStuffBits               the number of stuffing bits
       
    44  *    error                      error code
       
    45  *
       
    46  * Function:
       
    47  *    This function checks if the Picture Start Code (PSC),
       
    48  *    the End Of Sequence code (EOS) or the GOB synchronization code (GBSC)
       
    49  *    are the next codes in the bit buffer.
       
    50  *    Stuffing (PSTUF, ESTUF or GSTUF) is allowed before the code and
       
    51  *    the number of stuffing bits is returned in numStuffBits parameter.
       
    52  *    The code is not flushed from the buffer.
       
    53  *
       
    54  * Returns:
       
    55  *    SNC_NO_SYNC                if no sync code is found
       
    56  *    SNC_PSC                    if PSC is found
       
    57  *    SNC_GBSC                   if GBSC is found
       
    58  *    SNC_EOS                    if EOS is found
       
    59  *    SNC_STUFFING               if there are less than 8 zeros starting from 
       
    60  *                               the current position and after 
       
    61  *                               them the buffer ends
       
    62  *
       
    63  * Error codes:
       
    64  *    Error codes returned by bibFlushBits/bibGetBits/bibShowBits.
       
    65  *
       
    66  */
       
    67 
       
    68 int sncCheckSync(bibBuffer_t *buffer, int *numStuffBits, int16 *error)
       
    69 /* {{-output"sncCheckSync.txt"}} */
       
    70 {
       
    71    u_int32 result;
       
    72    int numBitsGot, i;
       
    73    int16 newError = 0;
       
    74    int bitErrorIndication = 0;
       
    75 
       
    76    for(i = 0; i < 8; i++) {
       
    77       bitErrorIndication = 0;
       
    78       result = bibShowBits(i + 22, buffer, &numBitsGot, &bitErrorIndication, 
       
    79          &newError);
       
    80       if (result == 32) {
       
    81          /* PSC */
       
    82          *numStuffBits = i;
       
    83          return SNC_PSC;
       
    84       }
       
    85 
       
    86       else if (result == 63) {
       
    87         /* EOS */
       
    88         *numStuffBits = i;
       
    89         return SNC_EOS;
       
    90       }
       
    91 
       
    92       else if (result > 32 && result < 63) {
       
    93          /* GBSC */
       
    94          /* Notice that the allowed Group Numbers are listed in section 5.2.3
       
    95             of the H.263 recommendation. They are not checked here but rather
       
    96             they are assumed to be checked in the corresponding header reading
       
    97             function, e.g. vdxGetGOBHeader. */
       
    98          *numStuffBits = i;
       
    99          return SNC_GBSC;
       
   100       }
       
   101 
       
   102       else if (result >= 64) {
       
   103          return SNC_NO_SYNC;
       
   104       }
       
   105       else if ( buffer->error )
       
   106           {
       
   107           // out of bits
       
   108           *error = (int16)buffer->error;
       
   109           return SNC_NO_SYNC;
       
   110           }
       
   111    }
       
   112 
       
   113    return SNC_NO_SYNC;
       
   114 }
       
   115 
       
   116 
       
   117 /* {{-output"sncRewindAndSeekNewSync.txt"}} */
       
   118 /*
       
   119  * sncRewindAndSeekNewSync
       
   120  *    
       
   121  *
       
   122  * Parameters:
       
   123  *    earliestBitPos             location of previously found sync code
       
   124  *                               so no sense to rewind over it
       
   125  *    buffer                     a pointer to a bit buffer structure
       
   126  *    error                      error code
       
   127  *
       
   128  * Function:
       
   129  *    This function is intended to be called after GOB header is corrupted.
       
   130  *    It rewinds some (already read) bits in order not to miss any sync code.
       
   131  *    Then, this function finds the next Picture Start Code (PSC),
       
   132  *    End Of Sequence code (EOS) or Group of Blocks Start Code (GBSC)
       
   133  *    The function discards the bits before the synchronization code 
       
   134  *    but does not remove the found code from the buffer.
       
   135  *
       
   136  * Returns:
       
   137  *    SNC_NO_SYNC                if no PSC, EOS either GBSC was found and 
       
   138  *                               no more bits are available
       
   139  *    SNC_PSC                    if PSC is found
       
   140  *    SNC_GBSC                   if GBSC is found
       
   141  *    SNC_EOS                    if EOS is found
       
   142  *
       
   143  * Error codes:
       
   144  *    Error codes returned by bibFlushBits/bibGetBits/bibShowBits.
       
   145  *
       
   146  */
       
   147 
       
   148 int sncRewindAndSeekNewSync(u_int32 earliestBitPos, bibBuffer_t *buffer, 
       
   149    int16 *error)
       
   150 /* {{-output"sncRewindAndSeekNewSync.txt"}} */
       
   151 {
       
   152    int sncCode;                     /* found sync code */
       
   153    u_int32 numRewindableBits;       /* number of bits which can be rewinded */
       
   154    u_int32 bitPosBeforeRewinding;   /* initial buffer bit index */
       
   155    u_int32 numBitsToRewind;         /* number of bits to rewind before seeking the sync code */
       
   156 
       
   157    *error = 0;
       
   158 
       
   159    bitPosBeforeRewinding = bibNumberOfFlushedBits(buffer);
       
   160 
       
   161    if ( bitPosBeforeRewinding > earliestBitPos+17 ) {
       
   162       numBitsToRewind = bitPosBeforeRewinding - earliestBitPos - 17; 
       
   163       /* 17 is the sync code length, and one sync code exists in earliestBitPos => do not read it again */
       
   164    }
       
   165    else if ( bitPosBeforeRewinding > earliestBitPos ) {
       
   166       /* This might be possible in slice mode */
       
   167       numBitsToRewind = bitPosBeforeRewinding - earliestBitPos; 
       
   168    }
       
   169    else {
       
   170       /* Bit counter overrun? */
       
   171       numBitsToRewind = bitPosBeforeRewinding + (0xffffffff - earliestBitPos) - 17; 
       
   172    }
       
   173    numRewindableBits = bibNumberOfRewBits(buffer);
       
   174 
       
   175    if (numRewindableBits < (u_int32) numBitsToRewind)
       
   176       numBitsToRewind = (int) numRewindableBits;
       
   177 
       
   178    bibRewindBits(numBitsToRewind, buffer, error);
       
   179    if (*error)
       
   180       return SNC_NO_SYNC;
       
   181 
       
   182    /* Seek the next synchronization code */
       
   183    sncCode = sncSeekSync(buffer, error);
       
   184    if (*error)
       
   185       return sncCode;
       
   186 
       
   187    return sncCode;
       
   188 }
       
   189 
       
   190 
       
   191 /* {{-output"sncSeekFrameSync.txt"}} */
       
   192 /*
       
   193  * sncSeekFrameSync
       
   194  *    
       
   195  *
       
   196  * Parameters:
       
   197  *    buffer                     a pointer to a bit buffer structure
       
   198  *    error                      error code
       
   199  *
       
   200  * Function:
       
   201  *    This function finds the next Picture Start Code (PSC) or
       
   202  *    End Of Sequence code (EOS) from the bit buffer. The function skips
       
   203  *    any other codes. It discards the bits before the found synchronization
       
   204  *    code but does not remove the found code from the buffer.
       
   205  *
       
   206  * Returns:
       
   207  *    SNC_NO_SYNC                if no PSC or EOS was found and 
       
   208  *                               no more bits are available
       
   209  *    SNC_PSC                    if PSC is found
       
   210  *    SNC_EOS                    if EOS is found
       
   211  *
       
   212  * Error codes:
       
   213  *    Error codes returned by bibFlushBits/bibGetBits/bibShowBits.
       
   214  *
       
   215  */
       
   216 
       
   217 int sncSeekFrameSync(bibBuffer_t *buffer, int16 *error)
       
   218 /* {{-output"sncSeekFrameSync.txt"}} */
       
   219 {
       
   220    int numBitsGot, syncCode, bitErrorIndication = 0;
       
   221    int16 newError = 0;
       
   222 
       
   223    for (;;) {
       
   224       syncCode = sncSeekSync(buffer, &newError);
       
   225 
       
   226       if (newError) {
       
   227          *error = newError;
       
   228          return SNC_NO_SYNC;
       
   229       }
       
   230 
       
   231       if (syncCode == SNC_GBSC) {
       
   232          bibFlushBits(17, buffer, &numBitsGot, &bitErrorIndication, &newError);
       
   233       }
       
   234 
       
   235       else
       
   236          return syncCode;
       
   237    }
       
   238 }
       
   239 
       
   240 
       
   241 /* {{-output"sncSeekSync.txt"}} */
       
   242 /*
       
   243  * sncSeekSync
       
   244  *    
       
   245  *
       
   246  * Parameters:
       
   247  *    buffer                     a pointer to a bit buffer structure
       
   248  *    error                      error code
       
   249  *
       
   250  * Function:
       
   251  *    This function finds the next Picture Start Code (PSC),
       
   252  *    End Of Sequence code (EOS) or Group of Blocks Start Code (GBSC)
       
   253  *    from the bit buffer. It discards the bits before the synchronization
       
   254  *    code but does not remove the found code from the buffer.
       
   255  *
       
   256  * Returns:
       
   257  *    SNC_NO_SYNC                if no PSC, EOS either GBSC was found and 
       
   258  *                               no more bits are available
       
   259  *    SNC_PSC                    if PSC is found
       
   260  *    SNC_GBSC                   if GBSC is found
       
   261  *    SNC_EOS                    if EOS is found
       
   262  *
       
   263  * Error codes:
       
   264  *    Error codes returned by bibFlushBits/bibGetBits/bibShowBits.
       
   265  *
       
   266  */
       
   267 
       
   268 int sncSeekSync(bibBuffer_t *buffer, int16 *error)
       
   269 /* {{-output"sncSeekSync.txt"}} */
       
   270 {
       
   271    u_int32 result;
       
   272    int numBitsGot;
       
   273    int16 newError = 0;
       
   274    int bitErrorIndication = 0;
       
   275 
       
   276    for (;;) {
       
   277       bitErrorIndication = 0;
       
   278       result = bibShowBits(22, buffer, &numBitsGot, &bitErrorIndication, 
       
   279          &newError);
       
   280 
       
   281 
       
   282       if (result == 32) {
       
   283          /* PSC */
       
   284          return SNC_PSC;
       
   285       }
       
   286 
       
   287       else if (result == 63) {
       
   288             /* EOS */
       
   289             return SNC_EOS;
       
   290       }
       
   291 
       
   292       else if (result > 32 && result < 63) {
       
   293          /* GBSC */
       
   294          /* Notice that the allowed Group Numbers are listed in section 5.2.3
       
   295             of the H.263 recommendation. They are not checked here but rather
       
   296             they are assumed to be checked in the corresponding header reading
       
   297             function, e.g. vdxGetGOBHeader. */
       
   298          return SNC_GBSC;
       
   299       }
       
   300       else if ( buffer->error )
       
   301           {
       
   302           // out of bits
       
   303           *error = (int16)buffer->error;
       
   304           return SNC_NO_SYNC;
       
   305           }
       
   306 
       
   307       bibFlushBits(1, buffer, &numBitsGot, &bitErrorIndication, error);
       
   308    }
       
   309 }
       
   310 
       
   311 // End of file