00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef _BIT_MANAGER_H_
00042 #define _BIT_MANAGER_H_
00043
00044 #include <libdirac_common/arrays.h>
00045 #include <cstring>
00046 #include <vector>
00047 #include <iostream>
00048
00049 namespace dirac
00050 {
00052 const unsigned int START_CODE_PREFIX = 0x42424344;
00053 const unsigned int START_CODE_PREFIX_BYTE0 =
00054 (START_CODE_PREFIX >> 24) & 0xFF;
00055 const unsigned int START_CODE_PREFIX_BYTE1 =
00056 (START_CODE_PREFIX >> 16) & 0xFF;
00057 const unsigned int START_CODE_PREFIX_BYTE2 =
00058 (START_CODE_PREFIX >> 8) & 0xFF;
00059 const unsigned int START_CODE_PREFIX_BYTE3 =
00060 START_CODE_PREFIX & 0xFF;
00061
00063 const unsigned char RAP_START_CODE = 0xD7;
00065 const unsigned char IFRAME_START_CODE = 0xD6;
00067 const unsigned char L1FRAME_START_CODE = 0xD4;
00069 const unsigned char L2FRAME_START_CODE = 0xD5;
00071 const unsigned char SEQ_END_CODE = 0xD0;
00073 const unsigned char NOT_START_CODE = 0xFF;
00075 const unsigned char BITSTREAM_VERSION = 0x02;
00076
00077
00079
00081
00082 class UnitOutputManager;
00083 class FrameOutputManager;
00084 class SequenceOutputManager;
00085
00087
00094 class BasicOutputManager
00095 {
00096
00097 friend class UnitOutputManager;
00098 friend class FrameOutputManager;
00099 friend class SequenceOutputManager;
00100
00101 public:
00103
00107 BasicOutputManager(std::ostream* out_data );
00108
00109
00110
00111
00112
00114 ~BasicOutputManager(){}
00115
00117
00120 void OutputBit(const bool& bit);
00121
00123
00127 void OutputBit(const bool& bit,int& count);
00128
00130
00133 void OutputByte(const char& byte);
00134
00136
00139 void OutputBytes(char* str_array);
00140
00142
00145 void OutputBytes(char* str_array,int num);
00146
00148
00151 size_t GetNumBytes() const {return m_num_out_bytes;}
00152
00154
00157 size_t Size() const {return m_buffer.size();}
00158
00159 private:
00160
00161 size_t m_num_out_bytes;
00162 std::ostream* m_op_ptr;
00163
00164 std::vector<char> m_buffer;
00165
00166 char m_current_byte;
00167
00168 int m_output_mask;
00169
00170
00171
00173
00176 void WriteToFile();
00177
00178
00179 void InitOutputStream();
00180
00181
00182 void FlushOutput();
00183
00185
00189 void OutputSkipInterpretStartPrefixByte();
00190 };
00191
00193
00196 class UnitOutputManager
00197 {
00198
00199 friend class FrameOutputManager;
00200
00201 public:
00203
00207 UnitOutputManager(std::ostream* out_data );
00208
00209
00210
00211
00212
00214 ~UnitOutputManager(){}
00215
00217
00220 BasicOutputManager& Header(){return m_header;}
00221
00223
00226 BasicOutputManager& Data(){return m_data;}
00227
00229
00232 const size_t GetUnitBytes() const {return m_unit_bytes;}
00233
00235 const size_t GetUnitHeaderBytes() const {return m_unit_head_bytes;}
00236
00237 private:
00238
00239 BasicOutputManager m_header,m_data;
00240
00241
00242 size_t m_unit_bytes;
00243
00244
00245 size_t m_unit_data_bytes;
00246
00247
00248 size_t m_unit_head_bytes;
00249
00250
00251
00253
00256 void WriteToFile();
00257 };
00258
00259 class FrameOutputManager
00260 {
00261 public:
00262
00263
00264 friend class SequenceOutputManager;
00265
00267
00268
00269
00270
00271
00272 FrameOutputManager( std::ostream* out_data , const int num_bands=13 );
00273
00275 ~FrameOutputManager();
00276
00278 void SetNumBands( const int num_bands );
00279
00281
00286 UnitOutputManager& BandOutput( const int csort , const int band_num );
00287
00289
00294 const UnitOutputManager& BandOutput( const int csort , const int band_num ) const;
00295
00297
00300 UnitOutputManager& MVOutput(){ return *m_mv_data; }
00301
00303
00306 const UnitOutputManager& MVOutput() const { return *m_mv_data; }
00307
00309 BasicOutputManager& HeaderOutput(){ return *m_frame_header; }
00310
00312 const size_t ComponentBytes( const int comp_num ) const { return m_comp_bytes[comp_num];}
00313
00315 const size_t ComponentHeadBytes( const int comp_num ) const { return m_comp_hdr_bytes[comp_num];}
00316
00318 const size_t MVBytes() const { return m_mv_bytes;}
00319
00321 const size_t MVHeadBytes() const { return m_mv_hdr_bytes;}
00322
00324 const size_t FrameBytes() const { return m_total_bytes;}
00325
00327 const size_t FrameHeadBytes() const { return m_header_bytes;}
00328
00329 private:
00330
00331
00332 TwoDArray< UnitOutputManager* > m_data_array;
00333
00334
00335 UnitOutputManager* m_mv_data;
00336
00337
00338 BasicOutputManager* m_frame_header;
00339
00340
00341 size_t m_total_bytes;
00342
00343
00344 size_t m_header_bytes;
00345
00346
00347 size_t m_mv_hdr_bytes;
00348
00349
00350 size_t m_mv_bytes;
00351
00352
00353 OneDArray< size_t > m_comp_bytes;
00354
00355
00356 OneDArray< size_t > m_comp_hdr_bytes;
00357
00358
00359 std::ostream* m_out_stream;
00360
00361
00362
00364 void Init( const int num_bands );
00365
00367 void Reset();
00368
00370 void DeleteAll();
00371
00373 void WriteToFile();
00374 };
00375
00376 class SequenceOutputManager
00377 {
00378 public:
00380 SequenceOutputManager( std::ostream* out_data );
00381
00383 FrameOutputManager& FrameOutput(){ return m_frame_op_mgr; }
00384
00386 BasicOutputManager& HeaderOutput(){ return m_seq_header; }
00387
00389 BasicOutputManager& TrailerOutput(){ return m_seq_end; }
00390
00392 void ResetFrame(){ m_frame_op_mgr.Reset(); }
00393
00395 void WriteSeqHeaderToFile();
00396
00398 void WriteFrameData();
00399
00401 void WriteSeqTrailerToFile();
00402
00404 const size_t SequenceBytes() { return m_total_bytes; }
00405
00407 const size_t SequenceHeadBytes() { return m_header_bytes; }
00408
00410 const size_t MVBytes() { return m_mv_bytes; }
00411
00413 const size_t ComponentBytes( const int comp_num ) { return m_comp_bytes[comp_num]; }
00414
00416 void ResetFrameData();
00417
00418
00419 private:
00420
00421
00422 FrameOutputManager m_frame_op_mgr;
00423
00424
00425 BasicOutputManager m_seq_header;
00426
00427
00428 BasicOutputManager m_seq_end;
00429
00430
00431 OneDArray< size_t > m_comp_bytes;
00432
00433
00434 OneDArray< size_t > m_comp_hdr_bytes;
00435
00436
00437 size_t m_mv_hdr_bytes;
00438
00439
00440 size_t m_mv_bytes;
00441
00442
00443 size_t m_total_bytes;
00444
00445
00446 size_t m_header_bytes;
00447
00448
00449 size_t m_trailer_bytes;
00450 };
00451
00453
00455
00457 class BitInputManager
00458 {
00459
00460 public:
00462
00465 BitInputManager(std::istream* in_data );
00466
00467
00468
00469
00470
00472 ~BitInputManager(){}
00473
00474
00476 bool InputBit();
00477
00479 bool InputBit(int& count);
00480
00482 bool InputBit(int& count, const int max_count);
00483
00485 char InputByte();
00486
00488 void InputBytes(char* cptr,int num);
00489
00491 void FlushInput();
00492
00494 bool End() const ;
00495
00496 private:
00497
00498 std::istream* m_ip_ptr;
00499
00500 char m_current_byte;
00501
00502 int m_input_bits_left;
00503
00504
00505 unsigned int m_shift;
00506
00507
00508 void InitInputStream();
00509 };
00510
00511 }
00512 #endif