00001 #ifndef MNT_BIT_PARSER_H
00002 #define MNT_BIT_PARSER_H
00003
00004 #include <string.h>
00005 #include "mnt_bit_stream.h"
00006
00007 extern int bitmask[];
00008
00044 class BitParser {
00045 public:
00046
00047 BitStream *bs;
00048 unsigned char *offsetPtr;
00049 int currentBits;
00050 int bitCount;
00052 int find_any_mpeg_hdr();
00053 int next_start_code(unsigned int *offsetPtr);
00054 int next_mpeg_start_code(unsigned int *offsetPtr);
00055 int dump_until_next_start_code(BitParser *outbp, unsigned int *offsetPtr);
00056 int get_curr_start_code();
00057
00058 BitParser::BitParser();
00059 void copy_to(BitParser * dest);
00060 void wrap(BitStream * bs);
00061 void seek(int off);
00062 int tell();
00063 };
00064
00065
00066
00067
00068
00069 #ifndef JPEG_BYTE_STUFF
00070 #define bitfill(bp) \
00071 { \
00072 unsigned short _x; \
00073 unsigned char _z, _y; \
00074 if (bp->bitCount < 16) { \
00075 _z = *(bp->offsetPtr++); \
00076 _y = *(bp->offsetPtr++); \
00077 _x = (_z << 8) | _y; \
00078 bp->currentBits <<= 16;\
00079 bp->currentBits |= _x; \
00080 bp->bitCount += 16; \
00081 } \
00082 }
00083 #else
00084 #define bitfill(bp) \
00085 { \
00086 while (bp->bitCount < 16) { \
00087 unsigned char _x; \
00088 Bp_GetByte(bp, _x); \
00089 bp->currentBits <<= 8; \
00090 bp->currentBits |= _x; \
00091 bp->bitCount += 8; \
00092 if (_x == 0xff) { \
00093 Bp_GetByte(bp, _x); \
00094 if (_x != 0) { \
00095 bp->currentBits <<= 8; \
00096 bp->currentBits |= _x; \
00097 bp->bitCount += 8; \
00098 } \
00099 } \
00100 } \
00101 }
00102 #endif
00103 #ifndef JPEG_BYTE_STUFF
00104 #define bitflush(bp) \
00105 { \
00106 unsigned char _x; \
00107 if (bp->bitCount >= 16) { \
00108 bp->bitCount -= 16; \
00109 _x = ((bp->currentBits) & 0xff000000) >> 24; \
00110 *(bp->offsetPtr++) = _x;\
00111 _x = ((bp->currentBits) & 0x00ff0000) >> 16; \
00112 *(bp->offsetPtr++) = _x;\
00113 bp->currentBits <<= 16; \
00114 bp->bs->endDataPtr_ = bp->offsetPtr; \
00115 } \
00116 }
00117
00118 #define bitflushall(bp) \
00119 { \
00120 unsigned char _x; \
00121 bitflush(bp); \
00122 if (bp->bitCount >= 8) { \
00123 bp->bitCount -= 8; \
00124 _x = ((bp->currentBits) & 0xff000000) >> 24; \
00125 *(bp->offsetPtr++) = _x; \
00126 bp->currentBits <<= 8; \
00127 } \
00128 if (bp->bitCount > 0) { \
00129 bp->currentBits = (bp->currentBits >> 24)|bitmask[(int)(8-bp->bitCount)]; \
00130 _x = bp->currentBits&0xff; \
00131 *(bp->offsetPtr++) = _x; \
00132 bp->bitCount = 0; \
00133 } \
00134 }
00135
00136 #else
00137
00138 #define bitflush(bp) \
00139 { \
00140 unsigned char _x; \
00141 if (bp->bitCount >= 16) { \
00142 bp->bitCount -= 16; \
00143 _x = ((bp->currentBits) & 0xff000000) >> 24; \
00144 *(bp->offsetPtr++) = _x;\
00145 if (_x == 0xff) { \
00146 *(bp->offsetPtr++) = 0x0; \
00147 } \
00148 _x = ((bp->currentBits) & 0x00ff0000) >> 16; \
00149 *(bp->offsetPtr++) = _x;\
00150 if (_x == 0xff) { \
00151 *(bp->offsetPtr++) = 0x0; \
00152 } \
00153 bp->currentBits <<= 16; \
00154 } \
00155 }
00156
00157 #define bitflushall(bp) \
00158 { \
00159 unsigned char _x; \
00160 bitflush(bp); \
00161 if (bp->bitCount >= 8) { \
00162 bp->bitCount -= 8; \
00163 _x = ((bp->currentBits) & 0xff000000) >> 24; \
00164 *(bp->offsetPtr++) = _x; \
00165 if (_x == 0xff) { \
00166 *(bp->offsetPtr++) = 0x0; \
00167 } \
00168 bp->currentBits <<= 8; \
00169 } \
00170 if (bp->bitCount > 0) { \
00171 bp->currentBits = (bp->currentBits >> 24)|bitmask[(int)(8-bp->bitCount)]; \
00172 _x = bp->currentBits&0xff; \
00173 *(bp->offsetPtr++) = _x; \
00174 if (_x == 0xff) { \
00175 *(bp->offsetPtr++) = 0x0; \
00176 } \
00177 bp->bitCount = 0; \
00178 } \
00179 }
00180
00181 #endif
00182
00183
00187 #define Bp_GetBits(bp, num, retval) \
00188 { \
00189 bitfill(bp); \
00190 bp->bitCount -= (num); \
00191 (retval) = (bp->currentBits >> bp->bitCount) & bitmask[(int)num]; \
00192 }
00193
00195 #define Bp_PeekBits(bp, num, retval) \
00196 { \
00197 bitfill(bp); \
00198 (retval) = (bp->currentBits >> (bp->bitCount-(num))) & bitmask[(int)num]; \
00199 }
00200
00202 #define Bp_FlushBits(bp, num) bp->bitCount -= (num)
00203
00205 #define Bp_UngetBits(bp, num) bp->bitCount += (num)
00206
00208 #define Bp_RestoreBits(bp, num) bp->bitCount += (num)
00209
00210
00211 #define Bp_PutBits(bp, num, value) \
00212 { \
00213 bp->currentBits |= ((int)(value & bitmask[(int)num])) << (32 - bp->bitCount - (num));\
00214 bp->bitCount += num; \
00215 bitflush(bp); \
00216 }
00217
00218 #define Bp_UnputBits(bp, num) bp->bitCount += (num)
00219
00220 #define Bp_MoveBits(inbp, outbp, num) \
00221 { \
00222 unsigned int _x; \
00223 Bp_GetBits(inbp,(num),_x); \
00224 Bp_PutBits(outbp,(num),_x); \
00225 }
00226
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 #define Bp_GetByte(bp, retval) retval = *(bp->offsetPtr)++
00238 #define Bp_PeekByte(bp, retval) retval = *(bp->offsetPtr)
00239 #define Bp_FlushByte(bp) bp->offsetPtr++
00240 #define Bp_UngetByte(bp) bp->offsetPtr--
00241 #define Bp_RestoreByte(bp) bp->offsetPtr--
00242
00243 #define Bp_PutByte(bp, value) { \
00244 *(bp->offsetPtr)++ = (value); \
00245 bp->bs->endDataPtr_ = bp->offsetPtr; \
00246 }
00247
00248 #define Bp_UnputByte(bp) { \
00249 *(bp->offsetPtr)--; \
00250 bp->bs->endDataPtr_ = bp->offsetPtr; \
00251 }
00252
00253 #define Bp_MoveByte(outbp, inbp) { \
00254 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00255 outbp->bs->endDataPtr_ = outbp->offsetPtr; \
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 #define Bp_GetShort(bp, retval) {\
00268 unsigned char _x, _y; \
00269 Bp_GetByte(bp, _x); \
00270 Bp_GetByte(bp, _y); \
00271 retval = (_x << 8) | _y; \
00272 }
00273
00274 #define Bp_PeekShort(bp, retval) \
00275 { \
00276 unsigned char _x, _y; \
00277 Bp_GetByte(bp, _x); \
00278 Bp_GetByte(bp, _y); \
00279 retval = (_x << 8) | _y; \
00280 bp->offsetPtr -= 2; \
00281 }
00282
00283 #define Bp_FlushShort(bp) bp->offsetPtr += 2
00284 #define Bp_UngetShort(bp) bp->offsetPtr -= 2
00285 #define Bp_RestoreShort(bp) bp->offsetPtr -= 2
00286
00287 #define Bp_PutShort(bp, value) \
00288 { \
00289 Bp_PutByte(bp, (((value)&0xff00) >> 8)); \
00290 Bp_PutByte(bp, (value)&0x00ff); \
00291 }
00292
00293 #define Bp_UnputShort(bp, value) \
00294 { \
00295 bp->offsetPtr -= 2; \
00296 bp->bs->endDataPtr_ = bp->offsetPtr; \
00297 }
00298
00299 #define Bp_MoveShort(outbp, inbp) \
00300 { \
00301 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00302 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00303 outbp->bs->endDataPtr_ = outbp->offsetPtr; \
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 #define Bp_GetLittleShort(bp, retval) {\
00316 unsigned char _x, _y; \
00317 Bp_GetByte(bp, _x); \
00318 Bp_GetByte(bp, _y); \
00319 retval = _x | (_y << 8); \
00320 }
00321
00322 #define Bp_PeekLittleShort(bp, retval)\
00323 { \
00324 unsigned char _x, _y; \
00325 Bp_GetByte(bp, _x); \
00326 Bp_GetByte(bp, _y); \
00327 retval = _x | (_y << 8); \
00328 bp->offsetPtr -= 2; \
00329 }
00330
00331 #define Bp_PutLittleShort(bp, value) \
00332 { \
00333 *(bp->offsetPtr)++ = (value) & 0x00ff; \
00334 *(bp->offsetPtr)++ = ((value) & 0xff00) >> 8; \
00335 bp->bs->endDataPtr_ = bp->offsetPtr; \
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 #define Bp_GetInt(bp, retval)\
00349 { \
00350 retval = 0; \
00351 retval |= *(bp->offsetPtr++); \
00352 retval <<= 8; \
00353 retval |= *(bp->offsetPtr++); \
00354 retval <<= 8; \
00355 retval |= *(bp->offsetPtr++); \
00356 retval <<= 8; \
00357 retval |= *(bp->offsetPtr++); \
00358 }
00359
00360 #define Bp_PeekInt(bp, retval)\
00361 {\
00362 unsigned char *_c;\
00363 _c = bp->offsetPtr;\
00364 retval = (*_c++ << 24);\
00365 retval |= (*_c++ << 16);\
00366 retval |= (*_c++ << 8);\
00367 retval |= (*_c);\
00368 }
00369
00370 #define Bp_FlushInt(bp) bp->offsetPtr += 4
00371 #define Bp_UngetInt(bp) bp->offsetPtr -= 4
00372 #define Bp_RestoreInt(bp) bp->offsetPtr -= 4
00373
00374 #define Bp_PutInt(bp, value) \
00375 { \
00376 *(bp->offsetPtr)++ = ((value) & 0xff000000)>>24; \
00377 *(bp->offsetPtr)++ = ((value) & 0x00ff0000)>>16; \
00378 *(bp->offsetPtr)++ = ((value) & 0x0000ff00)>>8; \
00379 *(bp->offsetPtr)++ = ((value) & 0x000000ff); \
00380 bp->bs->endDataPtr_ = bp->offsetPtr; \
00381 }
00382
00383 #define Bp_UnputInt(bp) \
00384 { \
00385 bp->offsetPtr -= 4; \
00386 bp->bs->endDataPtr_ = bp->offsetPtr;\
00387 }
00388
00389 #define Bp_MoveInt(outbp, inbp) \
00390 { \
00391 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00392 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00393 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00394 *(outbp->offsetPtr)++ = *(inbp->offsetPtr)++; \
00395 outbp->bs->endDataPtr_ = outbp->offsetPtr; \
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 #define Bp_GetLittleInt(bp, retval) \
00408 { \
00409 int _x; \
00410 \
00411 retval = 0; \
00412 Bp_GetByte(bp, _x); \
00413 retval |= _x; \
00414 Bp_GetByte(bp, _x); \
00415 retval |= (_x << 8); \
00416 Bp_GetByte(bp, _x); \
00417 retval |= (_x << 16); \
00418 Bp_GetByte(bp, _x); \
00419 retval |= (_x << 24); \
00420 }
00421
00422 #define Bp_PeekLittleInt(bp, retval) \
00423 { \
00424 unsigned char *_c;\
00425 _c = bp->offsetPtr;\
00426 retval = (*_c++);\
00427 retval |= (*_c++ << 8);\
00428 retval |= (*_c++ << 16);\
00429 retval |= (*_c << 24);\
00430 }
00431
00432 #define Bp_PutLittleInt(bp, value) \
00433 { \
00434 *(bp->offsetPtr)++ = ((value) & 0x000000ff); \
00435 *(bp->offsetPtr)++ = ((value) & 0x0000ff00)>>8; \
00436 *(bp->offsetPtr)++ = ((value) & 0x00ff0000)>>16; \
00437 *(bp->offsetPtr)++ = ((value) & 0xff000000)>>24; \
00438 bp->bs->endDataPtr_ = bp->offsetPtr; \
00439 }
00440
00441 #define Bp_GetByteArray(bp, bytes, array) \
00442 { \
00443 memcpy(array,bp->offsetPtr,bytes); \
00444 bp->offsetPtr += (bytes); \
00445 }
00446
00447 #define Bp_PutByteArray(bp, bytes, array) \
00448 { \
00449 memcpy(bp->offsetPtr,array,bytes); \
00450 bp->offsetPtr += (bytes); \
00451 bp->bs->endDataPtr_ = bp->offsetPtr; \
00452 }
00453
00454 #define Bp_MoveBytes(inbp, outbp, bytes) \
00455 { \
00456 memcpy(outbp->offsetPtr, inbp->offsetPtr, bytes); \
00457 outbp->offsetPtr += bytes; \
00458 outbp->bs->endDataPtr_ = outbp->offsetPtr; \
00459 inbp->offsetPtr += bytes; \
00460 }
00461
00462 #define Bp_FlushBytes(bp, n) bp->offsetPtr += n
00463 #define Bp_RestoreBytes(bp, n) bp->offsetPtr -= n
00464
00465
00466
00467
00468
00469
00470 #define Bp_ByteAlign(bp) \
00471 { \
00472 int _bytesLeft; \
00473 _bytesLeft = bp->bitCount/8; \
00474 bp->offsetPtr -= _bytesLeft; \
00475 bp->bitCount = 0; \
00476 }
00477
00478 #define Bp_InByteAlign(bp) Bp_ByteAlign(bp)
00479
00480 #define Bp_OutByteAlign(bp) \
00481 { \
00482 if (bp->bitCount <= 8 && bp->bitCount > 0) {\
00483 *(bp->offsetPtr)++ = (unsigned char)(bp->currentBits >> 24);\
00484 } else if (bp->bitCount > 8 && bp->bitCount <= 16) {\
00485 *(bp->offsetPtr)++ = (unsigned char)(bp->currentBits >> 24);\
00486 *(bp->offsetPtr)++ = (unsigned char)((bp->currentBits & 0x00FF0000) >> 16);\
00487 }\
00488 bp->currentBits = 0; \
00489 bp->bitCount = 0; \
00490 bp->bs->endDataPtr_ = bp->offsetPtr;\
00491 }
00492
00493
00494 #define Bp_GetOffset(bp, retval) \
00495 { \
00496 retval = bp->offsetPtr - (bp->bs->buffer_); \
00497 }
00498
00499 #define Bp_ShiftOffset(bp, bytes) \
00500 { \
00501 bp->offsetPtr += bytes; \
00502 }
00503
00504 #define Bp_SetOffset(bp, offset) \
00505 { \
00506 bp->offsetPtr = (bp->bs->buffer_) + offset; \
00507 }
00508
00509
00510
00511
00512 #define Bp_DataRemain(bp) ((bp)->bs->endDataPtr_-(bp)->offsetPtr)
00513 #define Bp_Underflow(bp) (bp->offsetPtr >= bp->bs->endDataPtr_)
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 #endif