TheoraEncoder.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 "theoraencoder.h"
00034 
00035 TheoraEncoder::TheoraEncoder(void)
00036 {
00037 }
00038 
00039 TheoraEncoder::~TheoraEncoder(void)
00040 {
00041 }
00042 
00045 StampedOggPacket** TheoraEncoder::initCodec(theora_info inTheoraInfo)
00046 {
00047         mTheoraInfo = inTheoraInfo;
00048         theora_encode_init(&mTheoraState,&mTheoraInfo);
00049 
00050         StampedOggPacket** locHeaders = new StampedOggPacket*[3];
00051 
00052         ogg_packet locOldPacket;
00053         theora_encode_header(&mTheoraState, &locOldPacket);
00054 
00055         locHeaders[0] = oldToNewPacket(&locOldPacket);
00056         
00057         theora_comment_init(&mTheoraComment);
00058         theora_encode_comment(&mTheoraComment, &locOldPacket);
00059 
00060         locHeaders[1] = oldToNewPacket(&locOldPacket);
00061 
00062         theora_encode_tables(&mTheoraState, &locOldPacket);
00063         
00064         locHeaders[2] = oldToNewPacket(&locOldPacket);
00065 
00066         //This should really have some error checking ! And trash packets if faild.
00067         return locHeaders;
00068 }
00069 
00070 
00074 StampedOggPacket* TheoraEncoder::oldToNewPacket(ogg_packet* inOldPacket)
00075 {
00076         const unsigned char NOT_USED = 0;
00077 
00078         //Need to clone the packet data
00079         unsigned char* locBuff = new unsigned char[inOldPacket->bytes];
00080         memcpy((void*)locBuff, (const void*)inOldPacket->packet, inOldPacket->bytes);
00081                                                                                                                                                                         //Not truncated or continued... it's a full packet.
00082         StampedOggPacket* locOggPacket = new StampedOggPacket(locBuff, inOldPacket->bytes, false, false, NOT_USED, inOldPacket->granulepos, StampedOggPacket::OGG_END_ONLY);
00083         return locOggPacket;
00084 
00085 }
00086 
00089 StampedOggPacket* TheoraEncoder::encodeTheora(yuv_buffer* inYUVBuffer) 
00090 {
00091         const int NOT_LAST_FRAME = 0;
00092         //const int IS_LAST_FRAME = 1;
00093         int retVal = 0;
00094 
00095 
00096         ogg_packet locOldOggPacket;
00097         retVal = theora_encode_YUVin(&mTheoraState, inYUVBuffer);
00098         
00099         if (retVal != 0) {
00100                 //FAILED
00101                 return NULL;
00102         }
00103 
00104         //We don't delete the buffer we get back in the old ogg packet, it's owned by libtheora
00105         retVal = theora_encode_packetout(&mTheoraState, NOT_LAST_FRAME, &locOldOggPacket);
00106         
00107         if (retVal != 1) {
00108                 //Weird return convention.
00109                 return NULL;
00110         }
00111 
00112         return oldToNewPacket(&locOldOggPacket);
00113         
00114 }

Generated on Tue Feb 15 14:54:17 2005 for oggdsf by  doxygen 1.3.9