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.15 2005/08/10 07:42:17 tjdwave Exp $ $Name: Dirac_0_5_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 
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 = 0x05;  //0.5
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;
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 
00238 
00241             size_t Size() const;
00242 
00243         private:
00244             // basic output managers for the header and data
00245             BasicOutputManager m_header,m_data;
00246      
00247             // total number of bytes written in the last unit coded 
00248             size_t m_unit_bytes;
00249 
00250             // number of data bytes for the last unit coded
00251             size_t m_unit_data_bytes;
00252 
00253             // number of data bytes for the last unit coded
00254             size_t m_unit_head_bytes;
00255 
00256             // functions
00257 
00259 
00262             void WriteToFile();
00263     };
00264 
00265     class FrameOutputManager
00266     {
00267     public:
00268 
00269         // Only the SequenceOutputManager can make this class write data to file
00270         friend class SequenceOutputManager;
00271 
00273         /*
00274             Constructs a class which manages output for an entire frame.
00275             \param  out_data  pointer to the output stream 
00276             \param  num_bands  the number of subbands per component
00277         */
00278         FrameOutputManager( std::ostream* out_data , const int num_bands=13 ); 
00279 
00281         ~FrameOutputManager();
00282 
00284         void SetNumBands( const int num_bands );
00285 
00287 
00292         UnitOutputManager& BandOutput( const int csort , const int band_num );
00293 
00295 
00300         const UnitOutputManager& BandOutput( const int csort , const int band_num ) const;
00301 
00303 
00306         UnitOutputManager& MVOutput(){ return *m_mv_data; }
00307 
00309 
00312         const UnitOutputManager& MVOutput() const { return *m_mv_data; }
00313 
00315         BasicOutputManager& HeaderOutput(){ return *m_frame_header; }
00316 
00318         const size_t ComponentBytes( const int comp_num ) const { return m_comp_bytes[comp_num];}
00319 
00321         const size_t ComponentHeadBytes( const int comp_num ) const { return m_comp_hdr_bytes[comp_num];}
00322 
00324         const size_t MVBytes() const { return m_mv_bytes;}
00325 
00327         const size_t MVHeadBytes() const { return m_mv_hdr_bytes;}
00328 
00330         const size_t FrameBytes() const { return m_total_bytes;}
00331 
00333         const size_t FrameHeadBytes() const { return m_header_bytes;}
00334 
00336 
00339             size_t Size() const;
00340 
00341     private:
00342 
00343         // Array of subband outputs, 1 for each component and subband
00344         TwoDArray< UnitOutputManager* > m_data_array;
00345 
00346         // Motion vector output
00347         UnitOutputManager* m_mv_data;
00348 
00349         // Frame header output
00350         BasicOutputManager* m_frame_header;
00351 
00352         // The total number of frame bytes
00353         size_t m_total_bytes;
00354 
00355         // The total number of header bytes
00356         size_t m_header_bytes;
00357 
00358         // The total number of MV header bytes
00359         size_t m_mv_hdr_bytes; 
00360 
00361         // The total number of MV bytes
00362         size_t m_mv_bytes; 
00363 
00364         // The total number of bytes in each component
00365         OneDArray< size_t > m_comp_bytes;
00366 
00367         // The total number of header bytes in each component
00368         OneDArray< size_t > m_comp_hdr_bytes;
00369 
00370         // A copy of a pointer to the output stream
00371         std::ostream* m_out_stream;
00372 
00373         // Functions
00374 
00376         void Init( const int num_bands );
00377 
00379         void Reset();
00380 
00382         void DeleteAll();
00383 
00385         void WriteToFile();
00386     };
00387 
00388     class SequenceOutputManager
00389     {
00390     public:
00392         SequenceOutputManager( std::ostream* out_data );
00393 
00395         FrameOutputManager& FrameOutput(){ return m_frame_op_mgr; }
00396 
00398         BasicOutputManager& HeaderOutput(){ return m_seq_header; }
00399 
00401         BasicOutputManager& TrailerOutput(){ return m_seq_end; }
00402 
00404         void ResetFrame(){ m_frame_op_mgr.Reset(); }
00405 
00407         void WriteSeqHeaderToFile();
00408 
00410         void WriteFrameData();
00411 
00413         void WriteSeqTrailerToFile();
00414 
00416         const size_t SequenceBytes() { return m_total_bytes; } 
00417 
00419         const size_t SequenceHeadBytes() { return m_header_bytes; } 
00420 
00422         const size_t MVBytes() { return m_mv_bytes; }
00423 
00425         const size_t ComponentBytes( const int comp_num ) { return m_comp_bytes[comp_num]; }
00426 
00428         void ResetFrameData();
00429 
00430 
00431     private:
00432 
00433         // The frame output manager
00434         FrameOutputManager m_frame_op_mgr;
00435 
00436         // Output manager for the sequence header
00437         BasicOutputManager m_seq_header;
00438 
00439         // Output manager for the sequence end
00440         BasicOutputManager m_seq_end;
00441 
00442         // The total number of bytes in each component
00443         OneDArray< size_t > m_comp_bytes;
00444 
00445         // The total number of header bits in each component
00446         OneDArray< size_t > m_comp_hdr_bytes;
00447 
00448         // The number of MV header bytes
00449         size_t m_mv_hdr_bytes;
00450 
00451         // The total number of MV bytes
00452         size_t m_mv_bytes;
00453 
00454         // The total number of bytes written so far
00455         size_t m_total_bytes;
00456 
00457         // The total number of header bytes written so far
00458         size_t m_header_bytes;
00459 
00460         // The total number of trailer bytes written so far
00461         size_t m_trailer_bytes;
00462     };
00463 
00465     //--------------Bit input stuff--------------//
00467 
00469     class BitInputManager
00470     {
00471 
00472         public:
00474 
00477             BitInputManager(std::istream* in_data );
00478 
00479             //Copy constructor is default shallow copy
00480 
00481             //Operator= is default shallow=
00482 
00484             ~BitInputManager(){}
00485 
00486             //input functions
00488             bool InputBit();
00489 
00491             bool InputBit(int& count);
00492 
00494             bool InputBit(int& count, const int max_count);
00495 
00497             char InputByte();
00498 
00500             void InputBytes(char* cptr,int num);
00501 
00503             void FlushInput();
00504 
00506             bool End() const ;
00507 
00508         private:
00509 
00510             std::istream* m_ip_ptr;
00511             // Char used for temporary storage of ip bits
00512             char m_current_byte;     
00513             // The number of bits left withint the current input byte being decoded
00514             int m_input_bits_left;
00515 
00516             //used to check if start code is detected
00517             unsigned int m_shift;
00518             //functions 
00519             // Initialise the input stream
00520             void InitInputStream(); 
00521     };
00522 
00523 } // namespace dirac
00524 #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.