DVDIO.cpp

00001 /************************************************************************ 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: DVDIO.cpp * 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 #include "DVDIO.hpp" 00044 #include "Frame.hpp" 00045 #include "VideoESInfo.hpp" 00046 #include "AudioESInfo.hpp" 00047 #include "CompressedVideoFrame.hpp" 00048 #include "CompressedAudioFrame.hpp" 00049 00050 /* construktor */ 00051 DVDIO::DVDIO(ESInfo *es_info, const char *file_name) { 00052 00053 assert(file_name); 00054 esinfo = es_info; 00055 filename = new char[strlen(file_name)+1]; 00056 strcpy(filename,file_name); 00057 av_input_format = NULL; // has to be set to use av_open_input_file !! 00058 DTS = 0; 00059 currentFrameNumber=0; 00060 }; 00061 00062 /* open vob file */ 00063 bool DVDIO::open() { 00064 if(state!=OPEN) { 00065 int error; 00066 printf("DVDIO::open:\n"); 00067 printf("Opening VOB file \n"); 00068 av_register_all(); 00069 error = av_open_input_file(&av_format_context, filename, av_input_format, 0, av_format_parameters); 00070 state = OPEN; 00071 return (error==0); 00072 currentFrameNumber=0; 00073 } 00074 return true; 00075 00076 }; 00077 00078 /* get frame */ 00079 Frame * DVDIO::getFrame() { 00080 if(state!=OPEN) 00081 return NULL; 00082 00083 printf("============================================================\n"); 00084 printf("DVDIO::getFrame: \n"); 00085 if (( (this->currentFrameNumber > this->endFrameNumber) && (endFrameNumber!=0) )) { 00086 close(true); 00087 state=STREAMEOF; 00088 return NULL; 00089 } 00090 00091 Frame *frame = NULL; 00092 AU *access_unit = new AU(); 00093 AVPacket avpacket_1, *avpacket = &avpacket_1; 00094 streamID = esinfo->getStreamId(); 00095 00096 printf("Getting new Frame from streamID: %i\n",streamID); 00097 printf("IDs:"); 00098 // to get correct stream, brute @ moment, maybe improove! 00099 do { 00100 if(av_read_frame(av_format_context, avpacket)){ 00101 printf("Error reading avpacket!\n"); 00102 return NULL; 00103 } 00104 printf(" %i ",av_format_context->streams[avpacket->stream_index]->id ); 00105 00106 if(av_format_context->streams[avpacket->stream_index]->id != streamID) { 00107 //printf("DVDIO::getFrame: Wrong ID -> dropping packet\n"); 00108 av_free_packet(avpacket); 00109 } 00110 00111 } while (av_format_context->streams[avpacket->stream_index]->id != streamID); 00112 printf("\nCorrect ID found! -> "); 00113 00114 // access_unit stuff copied from ffmp4io 00115 access_unit->size = avpacket->size; 00116 00117 access_unit->cts = DTS; 00118 access_unit->dts = DTS; 00119 DTS+=((VideoESInfo*)esinfo)->getVOPTimeIncrement(); 00120 access_unit->payload = new u8[access_unit->size]; 00121 memcpy(access_unit->payload, avpacket->data, access_unit->size); 00122 00123 // packet no longer needed! 00124 av_free_packet(avpacket); 00125 00126 // create frame copied from ffmp4io 00127 if(esinfo->isVisualStream()) 00128 frame = new CompressedVideoFrame(Frame::NN_VOP, ((VideoESInfo*)esinfo)->getWidth(), ((VideoESInfo*)esinfo)->getHeight()); 00129 else if(esinfo->isAudioStream()) 00130 frame = new CompressedAudioFrame(Frame::NN_VOP); 00131 else //hack for any other stream type maybe subpic in the future! 00132 frame = new CompressedVideoFrame(Frame::NN_VOP,0,0); 00133 00134 if (!frame->setAU(access_unit)) { //automatically detects frame type 00135 printf("Failed to set access unit at frame?\n"); 00136 } 00137 frame->setMediaTimeScale(esinfo->getMediaTimeScale()); 00138 00139 // frame type output 00140 Frame::FrameType ft = frame->getType(); 00141 printf("FrameType = %s\n\n", Frame::VOPTypeToChar(ft)); 00142 currentFrameNumber++; 00143 return frame; 00144 }