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 #ifndef PV_AVIFILE_STATUS
     19 #include "pv_avifile_status.h"
     20 #endif
     21 
     22 #ifndef PV_AVIFILE_PARSER_UTILS_H_INCLUDED
     23 #include "pv_avifile_parser_utils.h"
     24 #endif
     25 
     26 
     27 
     28 PV_AVI_FILE_PARSER_ERROR_TYPE
     29 PVAviFileParserUtils::ReadNextChunkType(PVFile* aFp, uint32& aChkType)
     30 {
     31     uint32 data = 0;
     32 
     33     if (PV_AVI_FILE_PARSER_SUCCESS != read32(aFp, data))
     34     {
     35         return PV_AVI_FILE_PARSER_READ_ERROR;
     36     }
     37 
     38     aChkType = data;
     39     PV_AVI_FILE_PARSER_ERROR_TYPE error = PV_AVI_FILE_PARSER_ERROR_UNKNOWN;
     40 
     41     if (aChkType == RIFF
     42             || aChkType == AVI
     43             || aChkType == LIST
     44             || aChkType == HDRL
     45             || aChkType == STRL
     46             || aChkType == STRH
     47             || aChkType == STRF
     48             || aChkType == STRN
     49             || aChkType == STRD
     50             || aChkType == IDX1
     51             || aChkType == VIDS
     52             || aChkType == AUDS
     53             || aChkType == MIDS
     54             || aChkType == TXTS
     55             || aChkType == DIB
     56             || aChkType == AVIH
     57             || aChkType == MOVI
     58             || aChkType == JUNK)
     59     {
     60         error = PV_AVI_FILE_PARSER_SUCCESS ;
     61     }
     62     else
     63     {
     64         error = PV_AVI_FILE_PARSER_UNSUPPORTED_CHUNK;
     65     }
     66 
     67     return error;
     68 }
     69 
     70 PV_AVI_FILE_PARSER_ERROR_TYPE
     71 PVAviFileParserUtils::read32(PVFile* aFp, uint32& aBuff, bool aBigEndian)
     72 {
     73     aBuff = 0;
     74     const uint32 sz = 4;
     75     uint8  temp[sz] = {0};
     76 
     77     if (aFp->Read(temp, 1, sz) != sz)
     78     {
     79         return PV_AVI_FILE_PARSER_ERROR_UNKNOWN;
     80     }
     81 
     82     if (aBigEndian)
     83     {
     84         big_endian_to_host((char*)temp, sz);
     85     }
     86 
     87     for (uint32 ii = 0; ii < sz; ii++)
     88     {
     89         aBuff = (aBuff << 8 | temp[ii]);
     90     }
     91 
     92     return PV_AVI_FILE_PARSER_SUCCESS;
     93 }
     94 
     95 uint32 PVAviFileParserUtils::read8(PVFile* aFp, uint8* aBuff, uint32 aLength)
     96 {
     97     for (uint32 ii = 0; ii < aLength; ii++)
     98     {
     99         uint8 temp = 0;
    100         if (aFp->Read(&temp, 1, 1) != 1)
    101         {
    102             return 0;
    103         }
    104         else
    105         {
    106             aBuff[ii] = temp;
    107         }
    108     }
    109     return aLength;
    110 }
    111 
    112 PV_AVI_FILE_PARSER_ERROR_TYPE
    113 PVAviFileParserUtils::read16(PVFile* aFp, uint16& aBuff, bool aBigEndian)
    114 {
    115     aBuff = 0;
    116     const uint32 sz = 2;
    117     uint8  temp[sz] = {0};
    118 
    119     if (aFp->Read(temp, 1, sz) != sz)
    120     {
    121         return PV_AVI_FILE_PARSER_ERROR_UNKNOWN;
    122     }
    123 
    124     if (aBigEndian)
    125     {
    126         big_endian_to_host((char*)temp, sz);
    127     }
    128 
    129     for (uint32 ii = 0; ii < sz; ii++)
    130     {
    131         aBuff = (aBuff << 8 | temp[ii]);
    132     }
    133     return PV_AVI_FILE_PARSER_SUCCESS;
    134 }
    135 
    136 int32 PVAviFileParserUtils::GetStreamNumber(uint32 aData)
    137 {
    138     uint8 temp[3] = {0};
    139     temp[0] = aData >> 24;
    140     temp[1] = ((aData >> 16) & 0xff);
    141     temp[2] = '\0';
    142 
    143     uint32 strNum = 0;
    144     if (!PV_atoi((const char*)temp, 'd', 2, strNum))
    145     {
    146         return -1;
    147     }
    148     else
    149     {
    150         return strNum;
    151     }
    152 }
    153 
    154 
    155