TheoraDecoder.cpp

Go to the documentation of this file.
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                 if ((inPacket->packetSize() > 0) && ((inPacket->packetData()[0] & 128) != 0)) {
00068                         //Ignore header packets
00069                         delete inPacket;
00070                         return NULL;
00071                 }
00072 
00073                 ogg_packet* locOldPack = simulateOldOggPacket(inPacket);                //Accepts the packet and deletes it.
00074                 theora_decode_packetin(&mTheoraState, locOldPack);
00075                 delete locOldPack->packet;
00076                 delete locOldPack;
00077                 
00078                 //Ignore return value... always returns 0 (or crashes :)
00079                 theora_decode_YUVout(&mTheoraState, &mYUVBuffer);
00080                 
00081                 return &mYUVBuffer;
00082         }
00083 
00084 }
00085 
00086 ogg_packet* TheoraDecoder::simulateOldOggPacket(StampedOggPacket* inPacket) {           //inPacket is accepted and deleted.
00087         const unsigned char NOT_USED = 0;
00088         ogg_packet* locOldPacket = new ogg_packet;              //Returns this... the caller is responsible for it.
00089         if (mFirstHeader) {
00090                 locOldPacket->b_o_s = 1;
00091                 mFirstHeader = false;
00092         } else {
00093                 locOldPacket->b_o_s = NOT_USED;
00094         }
00095         locOldPacket->e_o_s = NOT_USED;
00096         locOldPacket->bytes = inPacket->packetSize();
00097         locOldPacket->granulepos = inPacket->endTime();
00098         locOldPacket->packet = inPacket->packetData();
00099         locOldPacket->packetno = NOT_USED;
00100         
00101         //Set this to NULL do it doesn't get deleted by the destructor we are about invoke.
00102         inPacket->setPacketData(NULL);
00103         delete inPacket;
00104 
00105         return locOldPacket;            //Gives a poitner to the caller.
00106 }
00107 
00108 bool TheoraDecoder::isKeyFrame(StampedOggPacket* inPacket)
00109 {
00110         const unsigned char KEY_FRAME_FLAG = 0x40;
00111         if ((inPacket->packetSize() > 0) && (inPacket->packetData() != NULL)) {
00112                 return ((inPacket->packetData()[0] & KEY_FRAME_FLAG) == KEY_FRAME_FLAG) ? false : true;
00113         } else {
00114                 return false;
00115         }
00116 }
00117 bool TheoraDecoder::decodeHeader(StampedOggPacket* inHeaderPacket) {            //inHeaderPacket is accepted and deleted.
00118 
00119         ogg_packet* locOldPack = simulateOldOggPacket(inHeaderPacket);          //Accepts packet and deletes it.
00120         theora_decode_header(&mTheoraInfo, &mTheoraComment, locOldPack);
00121 
00122         delete locOldPack->packet;
00123         delete locOldPack;
00124         mPacketCount++;
00125         return true;
00126 }

Generated on Thu Feb 16 23:48:09 2006 for oggdsf by  doxygen 1.3.9