TheoraEncoder.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: TheoraEncoder.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 #include "TheoraEncoder.hpp" 00045 #include "io/OGGMuxDemuxIO.hpp" 00046 00059 class OGGMuxDemuxIO; 00060 00061 /* 00062 VideoESInfo(u32 streamId, u32 streamtype, ContainerInfo * vob, u32 timeIncrement, 00063 u8 * encodeddecconf, u32 objtype, u32 bufsize, 00064 u32 avgBW, u32 maxBW, u32 timeScale, 00065 u64 duration, u32 size, 00066 u32 w, u32 h, bool colorVideo=true, 00067 bool complete=true, 00068 CodecID codec_id = CODEC_ID_MPEG4, float quality = 1.0, 00069 float aspect_ratio = 352.0/288.0, bool staticFramePattern=true, u32 GOP_size = 50, 00070 u32 num_B_frames = 0, float avgBFrameSize=-1.0); 00071 */ 00072 00073 00074 TheoraEncoder::TheoraEncoder(VideoESInfo *ves){ 00075 dprintf_full("TheoraEncoder::TheoraEncoder\n"); 00076 00077 OGGMuxDemuxIO::initTheora(ves); 00078 00079 } 00080 00081 TheoraEncoder::~TheoraEncoder(){} 00082 00083 list <Frame *>TheoraEncoder::adapt(Frame * frm){ 00084 list <Frame *>frmList; 00085 ogg_packet *op; 00086 yuv_buffer *yuv; 00087 theora_info *ti; 00088 CompressedVideoFrame *theoraFrame; 00089 AU *pAU; 00090 00091 AU *au = frm->getAU(); 00092 op = new ogg_packet; 00093 yuv = new yuv_buffer; 00094 ti = OGGMuxDemuxIO::getTheoraInfo(); 00095 00096 dprintf_full("TheoraEncoder::adapt(%p)\n",frm); 00097 { 00098 yuv->y_width = ti->width; 00099 yuv->y_height = ti->height; 00100 yuv->y_stride = ti->width; 00101 00102 yuv->uv_width = ti->width / 2; 00103 yuv->uv_height = ti->height / 2; 00104 yuv->uv_stride = ti->width / 2; 00105 00106 yuv->y = au->payload; 00107 yuv->u = au->payload + ti->width * ti->height; 00108 yuv->v = au->payload + ti->width * ti->height * 5/4; 00109 } 00110 00111 double start_dtime, now_dtime; 00112 struct timeval now_tv; 00113 gettimeofday(&now_tv,NULL); 00114 start_dtime = now_tv.tv_sec + now_tv.tv_usec/1000000.0; 00115 00116 theora_encode_YUVin( OGGMuxDemuxIO::getTheoraState(), yuv); 00117 theora_encode_packetout(OGGMuxDemuxIO::getTheoraState(), 0, op); 00118 00119 int op_size = sizeof(ogg_packet); 00120 u8* op_copy = new u8[op_size + op->bytes]; 00121 00122 { 00123 ogg_packet *tmp = (ogg_packet*)op_copy; 00124 00125 // op_copy = (u8*)&op; 00126 // tmp->packet = op->packet; 00127 00128 memcpy(op_copy, op, op_size); 00129 tmp->packet = op_copy + op_size; 00130 memcpy(tmp->packet, op->packet, op->bytes); 00131 } 00132 00133 gettimeofday(&now_tv,NULL); 00134 now_dtime = now_tv.tv_sec + now_tv.tv_usec/1000000.0; 00135 00136 pAU = new AU(); 00137 pAU->payload = op_copy; 00138 pAU->size = op->bytes; 00139 00140 theoraFrame = new CompressedVideoFrame(Frame::UNKNOWN_VOP, ti->width, ti->height); 00141 theoraFrame->detectFrameType(); 00142 theoraFrame->setAU(pAU); 00143 theoraFrame->setMediaTimeScale(frm->getMediaTimeScale()); 00144 frmList.push_back(theoraFrame); 00145 00146 dprintf_full("TheoraEncoder::adapt(%p) %li bytes ... %u bytes in %2.0f sec\n",frm,op->bytes,au->size, (now_dtime-start_dtime)*1000.0); 00147 00148 return frmList; 00149 } 00150 00151 void TheoraEncoder::initialize(){ 00152 00153 /* 00154 theora_encode_init(td, ti); 00155 theora_info_clear(ti); 00156 00157 // first, encode theora header 00158 //theora_encode_header(td, &op); 00159 00160 //VideoFrame *vf = new CompressedVideoFrame(FrameType t, int width, int height); 00161 VideoFrame *vf; 00162 vf = new CompressedVideoFrame(Frame::NN_VOP, frame_x, frame_y); 00163 // OGGMuxDemuxIO::writeFrame(Frame *frm, ESInfo *out_es = NULL); 00164 // OGGMuxDemuxIO::writeFrame(vf); 00165 initialized = true; 00166 */ 00167 }