MP4Decoder.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: MP4Decoder.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #ifndef AFX_MP4DECODER_HPP 00045 #define AFX_MP4DECODER_HPP 00046 00047 #if _MSC_VER > 1000 00048 #pragma once 00049 #endif // _MSC_VER > 1000 00050 00051 00052 #include "ffmpegInterface.h" 00053 #include "xvidInterface.h" 00054 00055 #include "VideoAdaptor.hpp" 00056 #include "UncompressedVideoFrame.hpp" 00057 #ifdef ISOMP4 00058 #include "ISOMovies.h" 00059 #endif 00060 #include "IO.hpp" 00061 00062 class VideoESInfo; 00063 class UncompressedVideoFrame; 00064 00065 00076 class MP4Decoder:public VideoAdaptor { 00077 public: 00078 00079 00083 typedef enum eCoderType{XVID = 1, FFMPEG} meCoderType; 00084 00094 MP4Decoder(VideoESInfo * pESInfo, bool enableSwitching = false, 00095 UncompressedVideoFrame::ColorSpace colorSpace = UncompressedVideoFrame::ColorSpaceYV12, 00096 #ifdef ENABLE_FFMPEG // default codec (if avail) is ffmpeg 00097 meCoderType coderType= FFMPEG); 00098 #else 00099 meCoderType coderType= XVID); 00100 #endif 00101 00105 virtual ~ MP4Decoder(); 00106 00107 static bool isValidVisualFrame(const u8* payload); 00108 00109 static bool isValidDecoderConfig(const u8* payload); 00110 00117 list < Frame * >adapt(Frame * frm); 00118 00124 list < Frame * >close(); 00125 00131 Adaptor *clone(); 00132 00136 void initialize(); 00137 00144 void setCoderType(eCoderType coderType = XVID); 00145 00153 static xvidColorSpace mapToXVIDColorspace(UncompressedVideoFrame::ColorSpace colorSpace); 00154 /* 00155 Maps a CodecID to a String, returns the String UNKNOWN_CODEC if @param codec_id is invalid. 00156 */ 00157 static const char* codecToString(CodecID codec_id); 00158 00159 /* 00160 Maps a String to a CodecID 00161 */ 00162 static int stringToCodec(const char* codec_string); 00163 00171 static ffmpegColorSpace mapToFFMPEGColorspace(UncompressedVideoFrame::ColorSpace colorSpace); 00172 00176 u32 getTranscodingCosts() const; 00177 private: 00183 eCoderType meCoder; 00184 00193 int DecoderInit(char **pDecoderHandle, CodecID codecID, unsigned int uiWidth, unsigned int uiHeight); 00194 00201 void DecoderClose(char **pDecoderHandle); 00202 00216 int DecodeFrame(char *pDecoderHandle, char *pCompressedFrame, 00217 int iCFSize, char* pYUVFrame, UncompressedVideoFrame::ColorSpace eColorSpace); 00218 00222 char *mpDecoder; 00223 00227 UncompressedVideoFrame::ColorSpace mColorSpace; 00228 00232 u32 mFrameSize; 00233 00240 char *mpBuffer; 00241 00245 int lenDecoded; 00246 00250 uint muiLastFrameStart; 00251 00255 uint muiLastFrameEnd; 00256 00260 u32 muiFrameNumber; 00261 00265 u8 *mpOutBuf; 00266 00272 AU *auBuffer[2]; 00273 00278 uint muiWidth; 00279 00284 uint muiHeight; 00285 00291 u8* decConfig; 00292 u8* newDecConfig; 00293 u8 decConfigSize; 00294 00297 bool enableSwitching; 00298 00299 00300 typedef struct { 00301 unsigned int aspect_ratio_info; 00302 unsigned int par_width; 00303 unsigned int par_height; 00304 } structAspectRatio; 00305 00306 typedef struct { 00307 unsigned int vol_control_parameters; 00308 unsigned int chroma_format; 00309 unsigned int low_delay; 00310 unsigned int vbv_parameters; 00311 unsigned int first_half_bit_rate; 00312 unsigned int latter_half_bit_rate; 00313 unsigned int first_half_vbv_buffer_size; 00314 unsigned int latter_half_vbv_buffer_size; 00315 unsigned int first_half_vbv_occupancy; 00316 unsigned int latter_half_vbv_occupancy; 00317 } structVOLControlParameters; 00318 00319 typedef struct { 00320 unsigned int quant_type; 00321 unsigned int load_intra_quant_mat; 00322 unsigned int load_nonintra_quant_mat; 00323 unsigned int load_intra_quant_mat_grayscale; 00324 unsigned int load_nonintra_quant_mat_grayscale; 00325 00326 unsigned int intra_quant_matrix[64]; 00327 unsigned int nonintra_quant_matrix[64]; 00328 unsigned int intra_quant_matrix_grayscale[64]; 00329 unsigned int nonintra_quant_matrix_grayscale[64]; 00330 } structQuant; 00331 00336 typedef struct { 00337 // Visual Object Sequence 00338 unsigned int profile_and_level_indication; 00339 // Visual Object 00340 unsigned int is_visual_object_identifier; 00341 unsigned int visual_object_verid; 00342 unsigned int visual_object_priority; 00343 unsigned int visual_object_type; 00344 // Video Object Header 00345 unsigned int video_object_layer_startcode; 00346 // Video Object Layer 00347 unsigned int video_object_layer_start_code; 00348 unsigned int short_video_header; 00349 unsigned int video_object_layer_id; 00350 unsigned int random_accessbile_vol; 00351 unsigned int video_object_type_indication; 00352 unsigned int is_object_layer_identifier; 00353 unsigned int video_object_layer_verid; 00354 unsigned int video_object_layer_priority; 00355 00356 structAspectRatio aspectRatio; 00357 structVOLControlParameters volControlParameters; 00358 00359 unsigned int video_object_layer_shape; 00360 unsigned int vop_time_increment_resolution; 00361 unsigned int vop_time_incrememt_bits_needed; 00362 unsigned int fixed_vop_rate; 00363 unsigned int fixed_vop_time_increment; 00364 00365 unsigned int video_object_layer_width; 00366 unsigned int video_object_layer_height; 00367 00368 unsigned int interlaced; 00369 unsigned int obmc_disable; 00370 unsigned int sprite_enable; 00371 unsigned int not_8_bit; 00372 unsigned int quant_precision; 00373 unsigned int bits_per_pixel; 00374 00375 unsigned int no_gray_quant_update; 00376 unsigned int composition_method; 00377 unsigned int linear_composition; 00378 00379 structQuant quant; 00380 00381 unsigned int complexity_estimation_disable; 00382 unsigned int resync_marker_disable; 00383 unsigned int data_partitioned; 00384 unsigned int reversible_vlc; 00385 unsigned int scalability; 00386 } structMP4Header; 00387 00394 structMP4Header mp4Header; 00395 00404 u8 mcByte; 00405 00413 u8 *mpcData; 00414 00421 int miSize; 00422 00430 bool mbEOD; 00431 00438 int miCurrentNoBytes; 00439 00449 int miCurrentBit; 00450 00459 int miBookmarkedCurrentBit; 00460 00468 int miBookmarkedByte; 00469 00477 int miBookmarkedCurrentNoBytes; 00478 00491 void initializeParser(u8 * pFramem, u32 iSize); 00492 00499 void initializeMP4HeaderStruct(); 00500 00509 void nextByte(); 00510 00517 bool bytealigned(); 00518 00528 int getBit(); 00529 00539 int getBits(int iNumber); 00540 00550 int nextBits(int iNumber); 00551 00555 void nextStartCode(); 00556 00560 void loadQuantizerMatrix(unsigned int puiMatrix[]); 00561 00567 void readMarkerBitFunc(int iLine); 00572 void bookmark(); 00577 void restore(); 00578 00583 bool parseVOSHeader(); 00584 00589 bool parseVOHeader(); 00590 00591 bool video_signal_type_func(); 00592 00597 bool parseUserData(); 00598 00603 bool parseVideoObjectHeader(); 00608 bool parseVOLHeader(); 00609 }; 00610 00611 #endif