Dirac - A Video Codec

Created by the British Broadcasting Corporation.


bit_manager.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: bit_manager.h,v 1.9 2004/08/19 11:50:49 asuraparaju Exp $ $Name: Dirac_0_4_3 $ 00004 * 00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 00006 * 00007 * The contents of this file are subject to the Mozilla Public License 00008 * Version 1.1 (the "License"); you may not use this file except in compliance 00009 * with the License. You may obtain a copy of the License at 00010 * http://www.mozilla.org/MPL/ 00011 * 00012 * Software distributed under the License is distributed on an "AS IS" basis, 00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 00014 * the specific language governing rights and limitations under the License. 00015 * 00016 * The Original Code is BBC Research and Development code. 00017 * 00018 * The Initial Developer of the Original Code is the British Broadcasting 00019 * Corporation. 00020 * Portions created by the Initial Developer are Copyright (C) 2004. 00021 * All Rights Reserved. 00022 * 00023 * Contributor(s): Thomas Davies (Original Author), 00024 * Robert Scott Ladd, 00025 * Tim Borer 00026 * Anuradha Suraparaju 00027 * 00028 * Alternatively, the contents of this file may be used under the terms of 00029 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser 00030 * Public License Version 2.1 (the "LGPL"), in which case the provisions of 00031 * the GPL or the LGPL are applicable instead of those above. If you wish to 00032 * allow use of your version of this file only under the terms of the either 00033 * the GPL or LGPL and not to allow others to use your version of this file 00034 * under the MPL, indicate your decision by deleting the provisions above 00035 * and replace them with the notice and other provisions required by the GPL 00036 * or LGPL. If you do not delete the provisions above, a recipient may use 00037 * your version of this file under the terms of any one of the MPL, the GPL 00038 * or the LGPL. 00039 * ***** END LICENSE BLOCK ***** */ 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 00050 const unsigned int START_CODE_PREFIX = 0x42424344; //BBCD 00051 const unsigned int START_CODE_PREFIX_BYTE0 = (START_CODE_PREFIX >> 24) & 0xFF; 00052 const unsigned int START_CODE_PREFIX_BYTE1 = (START_CODE_PREFIX >> 16) & 0xFF; 00053 const unsigned int START_CODE_PREFIX_BYTE2 = (START_CODE_PREFIX >> 8) & 0xFF; 00054 const unsigned int START_CODE_PREFIX_BYTE3 = START_CODE_PREFIX & 0xFF; 00055 00057 const unsigned char RAP_START_CODE = 0xD7; 00059 const unsigned char IFRAME_START_CODE = 0xD6; 00061 const unsigned char L1FRAME_START_CODE = 0xD4; 00063 const unsigned char L2FRAME_START_CODE = 0xD5; 00065 const unsigned char SEQ_END_CODE = 0xD0; 00067 const unsigned char NOT_START_CODE = 0xFF; 00069 const unsigned char BITSTREAM_VERSION = 0x01; //0.1 00070 00071 00073 //--------------Bit output stuff--------------// 00075 00076 class UnitOutputManager; 00077 class FrameOutputManager; 00078 class SequenceOutputManager; 00079 00081 00087 class BasicOutputManager 00088 { 00089 // Data cannot be written to file directly, only by other o/p classes 00090 friend class UnitOutputManager; 00091 friend class FrameOutputManager; 00092 friend class SequenceOutputManager; 00093 00094 public: 00096 00100 BasicOutputManager(std::ostream* out_data ); 00101 00102 //Copy constructor is default shallow copy 00103 00104 //Operator= is default shallow= 00105 00107 ~BasicOutputManager(){} 00108 00110 00113 void OutputBit(const bool& bit); 00114 00116 00119 void OutputBit(const bool& bit,int& count);// Ditto, incrementing count 00120 00122 00125 void OutputByte(const char& byte); 00126 00128 00131 void OutputBytes(char* str_array); 00132 00134 00137 void OutputBytes(char* str_array,int num); 00138 00140 00143 size_t GetNumBytes() const {return m_num_out_bytes;} 00144 00146 00149 size_t Size() const {return m_buffer.size();} 00150 00151 private: 00152 size_t m_num_out_bytes; //number of output bytes written 00153 std::ostream* m_op_ptr; 00154 std::vector<char> m_buffer; //buffer used to store output prior to saving to file 00155 char m_current_byte; //Char used for temporary storage of op data bits 00156 int m_output_mask; //Used to set individual bit within the current header byte 00157 00158 //functions 00159 00161 00164 void WriteToFile(); 00165 00166 //Initialise the output stream. 00167 void InitOutputStream(); 00168 00169 //Clean out any remaining output bits to the buffer 00170 void FlushOutput(); 00171 00173 00177 void OutputSkipInterpretStartPrefixByte(); 00178 }; 00179 00181 00184 class UnitOutputManager 00185 { 00186 // Only the FrameOutputManager can make this class write data to file 00187 friend class FrameOutputManager; 00188 00189 public: 00191 00195 UnitOutputManager(std::ostream* out_data ); 00196 00197 //Copy constructor is default shallow copy 00198 00199 //Operator= is default shallow= 00200 00202 ~UnitOutputManager(){} 00203 00205 00208 BasicOutputManager& Header(){return m_header;} 00209 00211 00214 BasicOutputManager& Data(){return m_data;} 00215 00217 00220 const size_t GetUnitBytes() const {return m_unit_bytes;} 00221 00223 const size_t GetUnitHeaderBytes() const {return m_unit_head_bytes;} 00224 00225 private: 00226 // basic output managers for the header and data 00227 BasicOutputManager m_header,m_data; 00228 00229 // total number of bytes written in the last unit coded 00230 size_t m_unit_bytes; 00231 00232 // number of data bytes for the last unit coded 00233 size_t m_unit_data_bytes; 00234 00235 // number of data bytes for the last unit coded 00236 size_t m_unit_head_bytes; 00237 00238 // functions 00239 00241 00244 void WriteToFile(); 00245 }; 00246 00247 class FrameOutputManager 00248 { 00249 public: 00250 00251 // Only the SequenceOutputManager can make this class write data to file 00252 friend class SequenceOutputManager; 00253 00255 /* 00256 Constructs a class which manages output for an entire frame. 00257 \param out_data pointer to the output stream 00258 \param num_bands the number of subbands per component 00259 */ 00260 FrameOutputManager( std::ostream* out_data , const int num_bands=13 ); 00261 00263 ~FrameOutputManager(); 00264 00266 void SetNumBands( const int num_bands ); 00267 00269 00274 UnitOutputManager& BandOutput( const int csort , const int band_num ); 00275 00277 00282 const UnitOutputManager& BandOutput( const int csort , const int band_num ) const; 00283 00285 00288 UnitOutputManager& MVOutput(){ return *m_mv_data; } 00289 00291 00294 const UnitOutputManager& MVOutput() const { return *m_mv_data; } 00295 00297 BasicOutputManager& HeaderOutput(){ return *m_frame_header; } 00298 00300 const size_t ComponentBytes( const int comp_num ) const { return m_comp_bytes[comp_num];} 00301 00303 const size_t ComponentHeadBytes( const int comp_num ) const { return m_comp_hdr_bytes[comp_num];} 00304 00306 const size_t MVBytes() const { return m_mv_bytes;} 00307 00309 const size_t MVHeadBytes() const { return m_mv_hdr_bytes;} 00310 00312 const size_t FrameBytes() const { return m_total_bytes;} 00313 00315 const size_t FrameHeadBytes() const { return m_header_bytes;} 00316 00317 private: 00318 00319 // Array of subband outputs, 1 for each component and subband 00320 TwoDArray< UnitOutputManager* > m_data_array; 00321 00322 // Motion vector output 00323 UnitOutputManager* m_mv_data; 00324 00325 // Frame header output 00326 BasicOutputManager* m_frame_header; 00327 00328 // The total number of frame bytes 00329 size_t m_total_bytes; 00330 00331 // The total number of header bytes 00332 size_t m_header_bytes; 00333 00334 // The total number of MV header bytes 00335 size_t m_mv_hdr_bytes; 00336 00337 // The total number of MV bytes 00338 size_t m_mv_bytes; 00339 00340 // The total number of bytes in each component 00341 OneDArray< size_t > m_comp_bytes; 00342 00343 // The total number of header bytes in each component 00344 OneDArray< size_t > m_comp_hdr_bytes; 00345 00346 // A copy of a pointer to the output stream 00347 std::ostream* m_out_stream; 00348 00349 // Functions 00350 00352 void Init( const int num_bands ); 00353 00355 void Reset(); 00356 00358 void DeleteAll(); 00359 00361 void WriteToFile(); 00362 }; 00363 00364 class SequenceOutputManager 00365 { 00366 public: 00368 SequenceOutputManager( std::ostream* out_data ); 00369 00371 FrameOutputManager& FrameOutput(){ return m_frame_op_mgr; } 00372 00374 BasicOutputManager& HeaderOutput(){ return m_seq_header; } 00375 00377 BasicOutputManager& TrailerOutput(){ return m_seq_end; } 00378 00380 void ResetFrame(){ m_frame_op_mgr.Reset(); } 00381 00383 void WriteSeqHeaderToFile(); 00384 00386 void WriteFrameData(); 00387 00389 void WriteSeqTrailerToFile(); 00390 00392 const size_t SequenceBytes() { return m_total_bytes; } 00393 00395 const size_t SequenceHeadBytes() { return m_header_bytes; } 00396 00398 const size_t MVBytes() { return m_mv_bytes; } 00399 00401 const size_t ComponentBytes( const int comp_num ) { return m_comp_bytes[comp_num]; } 00402 00404 void ResetFrameData(); 00405 00406 00407 private: 00408 00409 // The frame output manager 00410 FrameOutputManager m_frame_op_mgr; 00411 00412 // Output manager for the sequence header 00413 BasicOutputManager m_seq_header; 00414 00415 // Output manager for the sequence end 00416 BasicOutputManager m_seq_end; 00417 00418 // The total number of bytes in each component 00419 OneDArray< size_t > m_comp_bytes; 00420 00421 // The total number of header bits in each component 00422 OneDArray< size_t > m_comp_hdr_bytes; 00423 00424 // The number of MV header bytes 00425 size_t m_mv_hdr_bytes; 00426 00427 // The total number of MV bytes 00428 size_t m_mv_bytes; 00429 00430 // The total number of bytes written so far 00431 size_t m_total_bytes; 00432 00433 // The total number of header bytes written so far 00434 size_t m_header_bytes; 00435 00436 // The total number of trailer bytes written so far 00437 size_t m_trailer_bytes; 00438 }; 00439 00441 //--------------Bit input stuff--------------// 00443 00445 class BitInputManager 00446 { 00447 00448 public: 00450 00453 BitInputManager(std::istream* in_data ); 00454 00455 //Copy constructor is default shallow copy 00456 00457 //Operator= is default shallow= 00458 00460 ~BitInputManager(){} 00461 00462 //input functions 00464 bool InputBit(); // Obtain the next bit 00465 00467 bool InputBit(int& count); // Ditto, incrementing count 00468 00470 bool InputBit(int& count, const int max_count);// Ditto, returns 0 if >=max_count 00471 00473 char InputByte(); 00474 00476 void InputBytes(char* cptr,int num); 00477 00479 void FlushInput(); 00480 00482 bool End() const ; 00483 00484 private: 00485 00486 std::istream* m_ip_ptr; 00487 char m_current_byte; // Char used for temporary storage of ip bits 00488 int m_input_bits_left; // The number of bits left withint the current input byte being decoded 00489 00490 unsigned int m_shift; //used to check if start code is detected 00491 //functions 00492 void InitInputStream(); // Initialise the input stream 00493 }; 00494 00495 #endif

© 2004 British Broadcasting Corporation. Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.