Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /*
     19     This PVA_FF_ExpandableBaseClass Class fp the base class for all Descriptors that
     20     allows the encoding the size of the class in bytes with a VARIABLE NUMBER OF BITS
     21 */
     22 
     23 
     24 #define IMPLEMENT_ExpandableBaseClass
     25 
     26 #include "expandablebaseclass.h"
     27 #include "atomutils.h"
     28 
     29 // Constructor
     30 PVA_FF_ExpandableBaseClass::PVA_FF_ExpandableBaseClass()
     31 {
     32     _pparent = NULL;
     33 }
     34 
     35 // Destructor
     36 PVA_FF_ExpandableBaseClass::~PVA_FF_ExpandableBaseClass()
     37 {
     38     // Empty
     39 }
     40 
     41 // Render the size of this class to a file stream using a variable number of bytes (1-4)
     42 // with the leading bit of each byte as the indicator if that byte contains the size.
     43 // Returns the number of bytes rendered
     44 int32
     45 PVA_FF_ExpandableBaseClass::renderSizeOfClassToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP *fp) const
     46 {
     47     // _sizeOfClass fp rendered byte by byte with the leading bit of each byte signaling of the
     48     // byte fp valid - thus only the last 7 bits if each byte hold actual size data.  Therefore
     49     // the max size possible (limited to a 4-byte encoding) fp 2^28 - 1.
     50 
     51     int32 numBytesRendered = 0;
     52 
     53     int32 limit = 0;
     54     uint8 data;
     55 
     56     // Setting the limits on the number of shifts and byte renderes needed
     57     if (_sizeOfClass <= 0x7f)
     58     {
     59         // _sizeOfClass can be rendered in 1 byte (LS 7 bits)
     60         limit = 7;
     61     }
     62     else if (_sizeOfClass <= 0x3fff)
     63     {
     64         // _sizeOfClass can be rendered in 2 bytes  (LS 7 bits of each)
     65         limit = 14;
     66     }
     67     else if (_sizeOfClass <= 0x1fffff)
     68     {
     69         // _sizeOfClass can be rendered in 3 bytes  (LS 7 bits of each)
     70         limit = 21;
     71     }
     72     else if (_sizeOfClass <= 0x0fffffff)
     73     {
     74         // _sizeOfClass can be rendered in 4 bytes  (LS 7 bits of each)
     75         limit = 28;
     76     }
     77     else
     78     {
     79         limit = 0; // ERROR condition
     80     }
     81 
     82     // Need to create size bytes with leading bit = 1 if there fp another
     83     // size byte to follow.
     84     for (int32 i = limit; i > 0; i -= 7)
     85     {
     86         // Max of 4 bytes to represent _sizeOfClass
     87         uint8 nextByte = 0x80;
     88         if (i == 7)
     89         {
     90             // Set leading bit on all but last size byte
     91             nextByte = 0;
     92         }
     93         // Take last 7 bits of size and set leading bit in data
     94         data = (uint8)(((_sizeOfClass >> (i - 7)) & 0x7f) | nextByte);
     95         if (!PVA_FF_AtomUtils::render8(fp, data))
     96         {
     97             numBytesRendered = 0;
     98             break;
     99         }
    100         numBytesRendered += 1;
    101     }
    102 
    103     return numBytesRendered;
    104 }
    105 
    106 
    107