00001 //=========================================================================== 00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh 00003 // 00004 //Redistribution and use in source and binary forms, with or without 00005 //modification, are permitted provided that the following conditions 00006 //are met: 00007 // 00008 //- Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 00011 //- Redistributions in binary form must reproduce the above copyright 00012 // notice, this list of conditions and the following disclaimer in the 00013 // documentation and/or other materials provided with the distribution. 00014 // 00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 00016 // may be used to endorse or promote products derived from this software 00017 // without specific prior written permission. 00018 // 00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00022 //PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR 00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 //=========================================================================== 00031 00032 #include "stdafx.h" 00033 #include "theoradecoder.h" 00034 00035 TheoraDecoder::TheoraDecoder(void) 00036 : mFirstPacket(true) 00037 , mFirstHeader(true) 00038 , mPacketCount(0) 00039 { 00040 } 00041 00042 TheoraDecoder::~TheoraDecoder(void) 00043 { 00044 } 00045 00046 bool TheoraDecoder::initCodec() 00047 { 00048 theora_comment_init(&mTheoraComment); 00049 theora_info_init(&mTheoraInfo); 00050 00051 return true; 00052 } 00053 00054 00055 00056 yuv_buffer* TheoraDecoder::decodeTheora(StampedOggPacket* inPacket) { //Accepts packet and deletes it. 00057 00058 if (mPacketCount < 3) { 00059 decodeHeader(inPacket); //Accepts header and deletes it. 00060 00061 return NULL; 00062 } else { 00063 if (mFirstPacket) { 00064 theora_decode_init(&mTheoraState, &mTheoraInfo); 00065 mFirstPacket = false; 00066 } 00067 ogg_packet* locOldPack = simulateOldOggPacket(inPacket); //Accepts the packet and deletes it. 00068 theora_decode_packetin(&mTheoraState, locOldPack); 00069 delete locOldPack->packet; 00070 delete locOldPack; 00071 00072 //Ignore return value... always returns 0 (or crashes :) 00073 theora_decode_YUVout(&mTheoraState, &mYUVBuffer); 00074 00075 return &mYUVBuffer; 00076 } 00077 00078 } 00079 00080 ogg_packet* TheoraDecoder::simulateOldOggPacket(StampedOggPacket* inPacket) { //inPacket is accepted and deleted. 00081 const unsigned char NOT_USED = 0; 00082 ogg_packet* locOldPacket = new ogg_packet; //Returns this... the caller is responsible for it. 00083 if (mFirstHeader) { 00084 locOldPacket->b_o_s = 1; 00085 mFirstHeader = false; 00086 } else { 00087 locOldPacket->b_o_s = NOT_USED; 00088 } 00089 locOldPacket->e_o_s = NOT_USED; 00090 locOldPacket->bytes = inPacket->packetSize(); 00091 locOldPacket->granulepos = inPacket->endTime(); 00092 locOldPacket->packet = inPacket->packetData(); 00093 locOldPacket->packetno = NOT_USED; 00094 00095 //Set this to NULL do it doesn't get deleted by the destructor we are about invoke. 00096 inPacket->setPacketData(NULL); 00097 delete inPacket; 00098 00099 return locOldPacket; //Gives a poitner to the caller. 00100 } 00101 00102 bool TheoraDecoder::isKeyFrame(StampedOggPacket* inPacket) 00103 { 00104 const unsigned char KEY_FRAME_FLAG = 0x40; 00105 if ((inPacket->packetSize() > 0) && (inPacket->packetData() != NULL)) { 00106 return ((inPacket->packetData()[0] & KEY_FRAME_FLAG) == KEY_FRAME_FLAG) ? false : true; 00107 } else { 00108 return false; 00109 } 00110 } 00111 bool TheoraDecoder::decodeHeader(StampedOggPacket* inHeaderPacket) { //inHeaderPacket is accepted and deleted. 00112 00113 ogg_packet* locOldPack = simulateOldOggPacket(inHeaderPacket); //Accepts packet and deletes it. 00114 theora_decode_header(&mTheoraInfo, &mTheoraComment, locOldPack); 00115 00116 delete locOldPack->packet; 00117 delete locOldPack; 00118 mPacketCount++; 00119 return true; 00120 }