bit_manager.h

00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: bit_manager.h,v 1.2 2005/01/30 05:11:40 gabest Exp $ $Name:  $
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 
00049 namespace dirac
00050 {
00052     const unsigned int START_CODE_PREFIX = 0x42424344; //BBCD
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;  //0.2
00076 
00077 
00079     //--------------Bit output stuff--------------//
00081 
00082     class UnitOutputManager;
00083     class FrameOutputManager;
00084     class SequenceOutputManager;
00085 
00087 
00094     class BasicOutputManager
00095     {
00096         // Data cannot be written to file directly, only by other o/p classes
00097         friend class UnitOutputManager;
00098         friend class FrameOutputManager;
00099         friend class SequenceOutputManager;
00100 
00101         public:
00103 
00107             BasicOutputManager(std::ostream* out_data );
00108 
00109             //Copy constructor is default shallow copy
00110 
00111             //Operator= is default shallow=
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             // Number of output bytes written
00161             size_t m_num_out_bytes;
00162             std::ostream* m_op_ptr;
00163             // Buffer used to store output prior to saving to file
00164             std::vector<char> m_buffer;
00165             // Char used for temporary storage of op data bits
00166             char m_current_byte;
00167             // Used to set individual bit within the current header byte
00168             int m_output_mask;
00169 
00170             //functions  
00171 
00173 
00176             void WriteToFile();
00177 
00178             //Initialise the output stream.
00179             void InitOutputStream();
00180 
00181             //Clean out any remaining output bits to the buffer
00182             void FlushOutput();
00183             
00185 
00189             void OutputSkipInterpretStartPrefixByte();
00190     };
00191 
00193 
00196     class UnitOutputManager
00197     {
00198         // Only the FrameOutputManager can make this class write data to file
00199         friend class FrameOutputManager;
00200 
00201         public:
00203 
00207             UnitOutputManager(std::ostream* out_data ); 
00208 
00209             //Copy constructor is default shallow copy
00210 
00211             //Operator= is default shallow=
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             // basic output managers for the header and data
00239             BasicOutputManager m_header,m_data;
00240      
00241             // total number of bytes written in the last unit coded 
00242             size_t m_unit_bytes;
00243 
00244             // number of data bytes for the last unit coded
00245             size_t m_unit_data_bytes;
00246 
00247             // number of data bytes for the last unit coded
00248             size_t m_unit_head_bytes;
00249 
00250             // functions
00251 
00253 
00256             void WriteToFile();
00257     };
00258 
00259     class FrameOutputManager
00260     {
00261     public:
00262 
00263         // Only the SequenceOutputManager can make this class write data to file
00264         friend class SequenceOutputManager;
00265 
00267         /*
00268             Constructs a class which manages output for an entire frame.
00269             \param  out_data  pointer to the output stream 
00270             \param  num_bands  the number of subbands per component
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         // Array of subband outputs, 1 for each component and subband
00332         TwoDArray< UnitOutputManager* > m_data_array;
00333 
00334         // Motion vector output
00335         UnitOutputManager* m_mv_data;
00336 
00337         // Frame header output
00338         BasicOutputManager* m_frame_header;
00339 
00340         // The total number of frame bytes
00341         size_t m_total_bytes;
00342 
00343         // The total number of header bytes
00344         size_t m_header_bytes;
00345 
00346         // The total number of MV header bytes
00347         size_t m_mv_hdr_bytes; 
00348 
00349         // The total number of MV bytes
00350         size_t m_mv_bytes; 
00351 
00352         // The total number of bytes in each component
00353         OneDArray< size_t > m_comp_bytes;
00354 
00355         // The total number of header bytes in each component
00356         OneDArray< size_t > m_comp_hdr_bytes;
00357 
00358         // A copy of a pointer to the output stream
00359         std::ostream* m_out_stream;
00360 
00361         // Functions
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         // The frame output manager
00422         FrameOutputManager m_frame_op_mgr;
00423 
00424         // Output manager for the sequence header
00425         BasicOutputManager m_seq_header;
00426 
00427         // Output manager for the sequence end
00428         BasicOutputManager m_seq_end;
00429 
00430         // The total number of bytes in each component
00431         OneDArray< size_t > m_comp_bytes;
00432 
00433         // The total number of header bits in each component
00434         OneDArray< size_t > m_comp_hdr_bytes;
00435 
00436         // The number of MV header bytes
00437         size_t m_mv_hdr_bytes;
00438 
00439         // The total number of MV bytes
00440         size_t m_mv_bytes;
00441 
00442         // The total number of bytes written so far
00443         size_t m_total_bytes;
00444 
00445         // The total number of header bytes written so far
00446         size_t m_header_bytes;
00447 
00448         // The total number of trailer bytes written so far
00449         size_t m_trailer_bytes;
00450     };
00451 
00453     //--------------Bit input stuff--------------//
00455 
00457     class BitInputManager
00458     {
00459 
00460         public:
00462 
00465             BitInputManager(std::istream* in_data );
00466 
00467             //Copy constructor is default shallow copy
00468 
00469             //Operator= is default shallow=
00470 
00472             ~BitInputManager(){}
00473 
00474             //input functions
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             // Char used for temporary storage of ip bits
00500             char m_current_byte;     
00501             // The number of bits left withint the current input byte being decoded
00502             int m_input_bits_left;
00503 
00504             //used to check if start code is detected
00505             unsigned int m_shift;
00506             //functions 
00507             // Initialise the input stream
00508             void InitInputStream(); 
00509     };
00510 
00511 } // namespace dirac
00512 #endif

Generated on Tue Dec 13 14:47:12 2005 for guliverkli by  doxygen 1.4.5