Main Page | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

vetCodec_MOV.cpp

Go to the documentation of this file.
00001 
00019 #include "vetCodec_MOV.h"
00020 #include "../vetUtility.h"
00021 
00022 //extern "C" {
00023 //      #include "../../support/quicktime4linux/quicktime.h"
00024 //  }
00025 
00026 
00027 
00032 vetCodec_MOV::vetCodec_MOV(char *filename, int stream) : vetCodec()
00033  {
00034         DEBUGMSG("vetCodec_MOV::vetCodec_MOV(char *filename, FileFormat format) [CONTRUCTOR] ", *filename)
00035 
00036         buff = NULL;
00037         file = NULL;
00038         setParameters(NULL);
00039         reset();
00040 
00041         load( filename, stream );
00042 
00043  }
00044 
00052 vetCodec_MOV::vetCodec_MOV( vetCodec_MOVParameters* initParams ) : vetCodec()
00053  {
00054         DEBUGMSG("vetCodec_MOV::vetCodec_MOV(vetCodec_MOVParameters* initParams) [CONTRUCTOR] ", *filename)
00055 
00056         buff = NULL;
00057         file = NULL;
00058         setParameters(initParams);
00059 
00060         reset();
00061  }
00062 
00068 VETRESULT vetCodec_MOV::reset()
00069  {
00070         INFO("VETRESULT vetCodec_MOV::reset() [SET DEFAULT PARAMETERS]")
00071 
00072         setName("MOV Coder");
00073         setDescription("Read or write video stream.");
00074         setVersion(1.0);
00075 
00076         if (file != NULL)
00077          {
00078                 quicktime_close(file);
00079                 file = NULL;
00080          }
00081 
00082         if (buff != NULL)
00083          {
00084                 // free the memory for each of the rows
00085                 for (unsigned int i = 0; i < height; i++)
00086                         delete [] buff[i];
00087 
00088                 // free the rest of the memory
00089                 delete [] buff;
00090          }
00091 
00092         width  = 0;
00093         height = 0;
00094         lenghtFrames = 0;
00095         outFrameRate = 0;
00096 
00097         if (myParams != NULL)
00098                 myParams->reset();
00099 
00100         streamEOF = true;
00101 
00102         return VETRET_OK;
00103  }
00104 
00105 
00106 bool vetCodec_MOV::EoF()        // buggy
00107  {
00108         if (file == NULL)
00109 
00110                 return true;
00111 
00112         if ( myParams->frameIndex >= lenghtFrames)
00113                 return true;
00114 
00115         return false;
00116  }
00117 
00118 
00128 VETRESULT vetCodec_MOV::setParameters (vetCodec_MOVParameters* initParams)
00129  {
00130 
00131         if ( initParams == NULL )
00132                 myParams = new vetCodec_MOVParameters();
00133         else
00134                 myParams = initParams;
00135 
00136         return VETRET_OK;
00137  }
00138 
00142 vetCodec_MOV::~vetCodec_MOV()
00143  {
00144         if (file != NULL)
00145                 quicktime_close(file);
00146 
00147         if (buff != NULL)
00148          {
00149                 // free the memory for each of the rows
00150                 for (unsigned int i = 0; i < height; i++)
00151                         delete [] buff[i];
00152 
00153                 // free the rest of the memory
00154                 delete [] buff;
00155          }
00156  }
00157 
00170 VETRESULT vetCodec_MOV::extractTo(vetFrameYUV420& img)
00171  {
00172         DEBUGMSG("VETRESULT vetCodec_MOV::extractTo(vetFrameYUV420& img) [pushing data]", doBuffering)
00173 
00174         if (file == NULL)
00175                 return VETRET_ILLEGAL_USE;
00176 
00177         return VETRET_NOT_IMPLEMENTED;
00178 
00179  }
00180 
00193 VETRESULT vetCodec_MOV::extractTo(vetFrameRGB24& img)
00194  {
00195         DEBUGMSG("VETRESULT vetCodec_MOV::extractTo(vetFrameRGB24& img) [pushing data]", doBuffering)
00196 
00197         if (file == NULL)
00198                 return VETRET_ILLEGAL_USE;
00199 
00200         if (width != img.width || height != img.height)
00201                 img.reAllocCanvas(width, height);
00202 
00203         // read the frame to a temporary buffer
00204         quicktime_decode_video(file, (unsigned char**)img.data, 0);
00205 
00206 //      vetUtility::conv_rgb24_rgb96(buff, (unsigned char*)img.data[0], width, height);
00207 
00208 //BUG why not memcpy( (unsigned char*)img.data[0], buff, width * height * 3);
00209 
00210         myParams->frameIndex++;
00211 
00212         if(myParams->frameIndex > lenghtFrames)
00213                 streamEOF = true;
00214 
00215         return VETRET_OK;
00216  }
00217 
00230 VETRESULT vetCodec_MOV::extractTo(vetFrameT<unsigned char>& img)
00231  {
00232         DEBUGMSG("VETRESULT vetCodec_MOV::extractTo(vetFrameT& img) [pushing data]", doBuffering)
00233 
00234         if (file == NULL)
00235                 return VETRET_ILLEGAL_USE;
00236 
00237         return VETRET_NOT_IMPLEMENTED;
00238  }
00239 
00240 
00252 VETRESULT vetCodec_MOV::importFrom(vetFrameYUV420& img)
00253  {
00254         DEBUGMSG("VETRESULT vetCodec_MOV::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00255 
00256         if (file == NULL)
00257                 return VETRET_ILLEGAL_USE;
00258 
00259         return VETRET_NOT_IMPLEMENTED;
00260 
00261  }
00262 
00274 VETRESULT vetCodec_MOV::importFrom(vetFrameRGB24& img)
00275  {
00276         DEBUGMSG("VETRESULT vetCodec_MOV::importFrom(vetFrameRGB24& img) [reading data]", doBuffering)
00277 
00278         if (file == NULL)
00279                 return VETRET_ILLEGAL_USE;
00280 
00281         PixelRGB24* rgb_ptr = img.data;
00282 
00283         unsigned char *buff_ptr = buff[0];
00284 
00285         for(unsigned int y_cnt=0 ; y_cnt < img.height ; y_cnt++)
00286          {
00287                 buff_ptr = buff[y_cnt];
00288 
00289                 for(unsigned int x_cnt=0 ; x_cnt < img.width ; x_cnt++, rgb_ptr++)
00290                  {
00291                         *(buff_ptr++) = (unsigned char)((*rgb_ptr)[0]);
00292                         *(buff_ptr++) = (unsigned char)((*rgb_ptr)[1]);
00293                         *(buff_ptr++) = (unsigned char)((*rgb_ptr)[2]);
00294                  }
00295          }
00296 
00297         myParams->frameIndex++;
00298         lenghtFrames++;
00299 
00300         //Encode the frame and write to file
00301         if ( quicktime_encode_video(file, buff, 0) )
00302                 return VETRET_OK;
00303         else
00304                 return VETRET_INTERNAL_ERR;
00305 
00306  }
00307 
00319 VETRESULT vetCodec_MOV::importFrom(vetFrameT<unsigned char>& img)
00320  {
00321         DEBUGMSG("VETRESULT vetCodec_MOV::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00322 
00323         return VETRET_NOT_IMPLEMENTED;
00324 
00325  }
00326 
00327 
00328 
00335 VETRESULT vetCodec_MOV::load()
00336  {
00337         INFO("VETRESULT vetCodec_MOV::load() [loading data to buffer]")
00338 
00339         // check the files signature is valid
00340         if (!quicktime_check_sig(myParams->fileName))
00341                 return VETRET_ILLEGAL_USE;
00342 //              throw "incompatible quicktime file";
00343 
00344         // open the QT file
00345         file = quicktime_open(myParams->fileName,1,0); //read access only!
00346         if (file == NULL)
00347                 return VETRET_ILLEGAL_USE;
00348 //              throw "error opening quicktime file";
00349 
00350 
00351 
00352         // check that it is a video QT file, with one track
00353         if (quicktime_video_tracks(file) != 1)
00354                 throw "QT file does not only have one video track";
00355 //        if (quicktime_audio_tracks(file) != 0)
00356 //                throw "QT file has audio tracks";
00357 
00358 //BUG CHECK STREAM RANGE
00359 
00360 
00361         width  = quicktime_video_width(file, myParams->stream);
00362         height = quicktime_video_height(file, myParams->stream);
00363 
00364         lenghtFrames = quicktime_video_length(file, myParams->stream);
00365         myParams->frameIndex = 0;
00366         streamEOF = false;
00367 
00368         //Check Depth: Only RGB allowed, RGBA not supported
00369         //Could be, but requires extra processing in this file
00370         if(quicktime_video_depth(file, 0) != 24)
00371                 return VETRET_ILLEGAL_USE;
00372 //              throw "Depth not 24 ie not RGBRGBRGB encoded QT";
00373 
00374 //      comp_type = quicktime_video_compressor(file, 0);
00375         if (!quicktime_supported_video(file, 0))
00376                 return VETRET_ILLEGAL_USE;
00377 
00378 //              throw "QT data cannot be decoded";
00379 
00380         // allocate memory for rgb rows
00381         buff = new (unsigned char*)[height];
00382         for (unsigned int i = 0; i < height ; i++)
00383                 buff[i] = new (unsigned char)[width*3];
00384 
00385         return VETRET_OK;
00386 }
00387 
00396 VETRESULT vetCodec_MOV::load(char *filename, int stream)
00397  {
00398         DEBUGMSG("VETRESULT vetCodec_MOV::load(char *filename, FileFormat format) [loading data to buffer]", filename)
00399 
00400         myParams->setFileName(filename);
00401         myParams->setStream(stream);
00402 
00403         return load();
00404  }
00405 
00406 
00421 VETRESULT vetCodec_MOV::newVideo(char* filename, unsigned int w, unsigned int h, float frame_rate, int quality)
00422  {
00423         if (file != NULL)
00424                 reset();
00425 
00426         //open the a file for the quicktime movie
00427         file = quicktime_open(filename,0,1);
00428 
00429         if (file == NULL)
00430                 return VETRET_INTERNAL_ERR; // throw "error opening file to write quicktime to";
00431 
00432         width = w;
00433         height = h;
00434         outFrameRate = frame_rate;
00435         streamEOF = true;
00436 
00437         //Set video parameters - Using Interlaced Even Motion JPEG A compression
00438         quicktime_set_video(file, 1, width, height, outFrameRate, "mjpa");
00439 
00440         //Set the JPEG quality factor 1=low 75=normal 90=high 100=best
00441         quicktime_set_jpeg(file, quality, 0);
00442 
00443         //Check compression scheme is supported
00444         if( !quicktime_supported_video(file, 0) )
00445                 return VETRET_INTERNAL_ERR; // throw "QT Compression scheme not supported" ;
00446 
00447         //Check colour model is supported - using RGB888
00448         if( !quicktime_writes_cmodel(file, 9, 0) )
00449                 return VETRET_INTERNAL_ERR; // throw "QT Color Model not supported" ;
00450 
00451         //Set colourmodel to RGB888 format
00452         quicktime_set_cmodel(file, 9);
00453 
00454         // allocate memory for rgb rows
00455         buff = new (unsigned char*)[height];
00456         for (unsigned int i = 0; i < height ; i++)
00457                 buff[i] = new (unsigned char)[width*3];
00458 
00459         return VETRET_OK;
00460  }
00461 
00462 
00471 VETRESULT vetCodec_MOV::save()
00472  {
00473         INFO("VETRESULT vetCodec_MOV::save() [saving buffered data]")
00474 
00475         if ( file == NULL)
00476                 return VETRET_ILLEGAL_USE;
00477 
00478         quicktime_close(file);
00479 
00480         file = NULL;
00481 
00482         reset();
00483 
00484         return VETRET_OK;
00485  }
00486 
00487 
00488 
00489 
00496 VETRESULT vetCodec_MOV::goToStart()
00497  {
00498         if ( file == NULL)
00499                 return VETRET_ILLEGAL_USE;
00500 
00501         return quicktime_seek_start(file);
00502  }
00503 
00510 VETRESULT vetCodec_MOV::goToEnd()
00511  {
00512         if ( file == NULL)
00513                 return VETRET_ILLEGAL_USE;
00514 
00515         return quicktime_seek_end(file);
00516  }
00517 
00518 
00519 
00529 long vetCodec_MOV::getAudioFrameIndex(int stream)
00530  {
00531         if ( hasAudio() && stream == -1  )
00532                 return quicktime_audio_position(file, myParams->stream);
00533 
00534         if ( !hasAudio() ||  stream < 0 || stream >= getVideoStreamCount())
00535                 return -1;
00536 
00537     return quicktime_audio_position(file, stream);
00538  }
00539 
00548 long vetCodec_MOV::getVideoFrameIndex(int stream)
00549  {
00550         if ( hasVideo() && stream == -1  )
00551                 return quicktime_video_position(file, myParams->stream);
00552 
00553         if ( !hasVideo() ||  stream < 0 || stream >= getVideoStreamCount())
00554                 return -1;
00555 
00556     return quicktime_video_position(file, stream);
00557  }
00558 
00569 VETRESULT vetCodec_MOV::setFrameIndex(long index, int stream)
00570  {
00571         if ( file == NULL)
00572                 return VETRET_ILLEGAL_USE;
00573 
00574     myParams->frameIndex = index;
00575 
00576         if ( stream == -1  )
00577                 return quicktime_video_position(file, myParams->stream);
00578 
00579         if ( stream < 0 || stream >= getVideoStreamCount())
00580                 return -VETRET_PARAM_ERR;
00581 
00582     return quicktime_set_video_position(file, index, stream) ;
00583  }
00584 
00585 
00594 int vetCodec_MOV::getVideoDepth(int stream)
00595  {
00596         if ( hasVideo() && stream == -1  )
00597                 return quicktime_video_depth(file, myParams->stream);
00598 
00599         if ( !hasVideo() ||  stream < 0 || stream >= getVideoStreamCount())
00600                 return VETRET_ILLEGAL_USE;
00601 
00602         return quicktime_video_depth(file, stream);
00603  }
00604 
00613 char* vetCodec_MOV::getVideoCompressor(int stream)
00614  {
00615         if ( hasVideo() && stream == -1  )
00616                 return quicktime_video_compressor(file, myParams->stream);
00617 
00618         if ( !hasVideo() ||  stream < 0 || stream >= getVideoStreamCount())
00619                 return NULL;
00620 
00621         return quicktime_video_compressor(file, stream);
00622  }
00623 
00632 char* vetCodec_MOV::getAudioCompressor(int stream)
00633  {
00634         if ( hasVideo() && stream == -1  )
00635                 return quicktime_audio_compressor(file, myParams->stream);
00636 
00637         if ( !hasVideo() ||  stream < 0 || stream >= getAudioStreamCount())
00638                 return NULL;
00639 
00640         return quicktime_audio_compressor(file, stream);
00641  }
00642 
00643 
00652 float vetCodec_MOV::getVideoFrameRate(int stream)
00653  {
00654         if ( hasVideo() && stream == -1  )
00655                 return quicktime_frame_rate(file, myParams->stream);
00656 
00657         if ( file == NULL || stream < 0 || stream >= getVideoStreamCount() )
00658                 return -1;
00659 
00660         return quicktime_frame_rate(file, stream);
00661  }
00662 
00663 
00664 
00670 bool vetCodec_MOV::hasVideo()
00671  {
00672         if ( file == NULL)
00673                 return false;
00674 
00675         if ( quicktime_has_video(file) )
00676                 return true;
00677         else
00678                 return false;
00679  }
00680 
00686 bool vetCodec_MOV::hasAudio()
00687  {
00688         if ( file == NULL)
00689                 return false;
00690 
00691         if ( quicktime_has_audio(file) )
00692                 return true;
00693         else
00694                 return false;
00695  }
00696 
00702 int vetCodec_MOV::getVideoStreamCount()
00703  {
00704         if ( file == NULL)
00705                 return -1;
00706 
00707         return quicktime_video_tracks(file);
00708  }
00709 
00715 int vetCodec_MOV::getAudioStreamCount()
00716  {
00717         if ( file == NULL)
00718                 return -1;
00719 
00720         return quicktime_audio_tracks(file);
00721  }
00722 
00731 long vetCodec_MOV::getAudioStreamLength(int stream)
00732  {
00733         if ( hasAudio() && stream == -1  )
00734                 return quicktime_audio_length(file, myParams->stream);
00735 
00736         if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00737                 return -1;
00738 
00739         return quicktime_audio_length(file, stream);
00740  }
00741 
00750 long vetCodec_MOV::getVideoStreamLength(int stream)
00751  {
00752         if ( hasAudio() && stream == -1  )
00753                 return quicktime_video_length(file, myParams->stream);
00754 
00755         if ( !hasVideo() || stream < 0 || stream >= getVideoStreamCount() )
00756                 return -1;
00757 
00758         return quicktime_video_length(file, stream);
00759  }
00760 
00769 long vetCodec_MOV::getAudioSampleRate(int stream)
00770  {
00771         if ( hasAudio() && stream == -1  )
00772                 return quicktime_sample_rate(file, myParams->stream);
00773 
00774         if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00775                 return -1;
00776 
00777         return quicktime_sample_rate(file, stream);
00778  }
00779 
00788 int vetCodec_MOV::getAudioChannels(int stream)
00789  {
00790         if ( hasAudio() && stream == -1  )
00791                 return quicktime_track_channels(file, myParams->stream);
00792 
00793         if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00794                 return -1;
00795 
00796         return quicktime_track_channels(file, stream);
00797  }
00798 
00799 
00800 
00801 
00802 
00803 
00804 
00805 
00806 
00815 VETRESULT vetCodec_MOV::setName(char *string)
00816  {
00817         if (file == NULL)
00818                 return VETRET_ILLEGAL_USE;
00819 
00820         quicktime_set_name(file, string);
00821 
00822         return VETRET_OK;
00823  }
00824 
00833 VETRESULT vetCodec_MOV::setInfo(char *string)
00834  {
00835         if (file == NULL)
00836                 return VETRET_ILLEGAL_USE;
00837 
00838         quicktime_set_info(file, string);
00839 
00840         return VETRET_OK;
00841  }
00842 
00851 VETRESULT vetCodec_MOV::setCopyRight(char *string)
00852  {
00853         if (file == NULL)
00854                 return VETRET_ILLEGAL_USE;
00855 
00856         quicktime_set_copyright(file, string);
00857 
00858         return VETRET_OK;
00859  }
00860 
00866 char* vetCodec_MOV::getName()
00867  {
00868         if (file == NULL)
00869                 return NULL;
00870 
00871         return quicktime_get_name(file);
00872  }
00873 
00879 char* vetCodec_MOV::getInfo()
00880  {
00881         if (file == NULL)
00882                 return NULL;
00883 
00884         return quicktime_get_info(file);
00885  }
00886 
00892 char* vetCodec_MOV::getCopyRight()
00893  {
00894         if (file == NULL)
00895                 return NULL;
00896 
00897         return quicktime_get_copyright(file);
00898  }
00899 
00900 
00901 
00902 
00903 
00904 
00905 
00906 
00907 
00908 
00909 
00910 
00911 
00912 
00913 
00914 vetCodec_MOVParameters::vetCodec_MOVParameters()
00915  {
00916         reset();
00917  }
00918 
00919 vetCodec_MOVParameters::vetCodec_MOVParameters(const char* filename, int stream, long frameIndex)
00920  {
00921         reset();
00922         setFileName(filename);
00923         setStream(stream);
00924         setFrameIndex(frameIndex);
00925  }
00926 
00927 void vetCodec_MOVParameters::reset()
00928  {
00929         strcpy(fileName, (const char*)"input.mov\0");
00930         frameIndex = 0;
00931  }
00932 
00933 
00934 void vetCodec_MOVParameters::setFileName(const char *filename)
00935  {
00936         strncpy(fileName, filename, 64);
00937  }
00938 
00939 void vetCodec_MOVParameters::setFrameIndex(long index)
00940  {
00941         frameIndex = index;
00942  }
00943 
00944 void vetCodec_MOVParameters::setStream(int s)
00945  {
00946         stream = s;
00947  }
00948 
00949 
00950 int vetCodec_MOVParameters::saveToStreamXML(FILE *fp)
00951  {
00952         if ( fp == NULL )
00953                 return VETRET_PARAM_ERR;
00954 
00955         if( fprintf(fp, "<vetCodec_MOVParameters>\n") == EOF )
00956                 return VETRET_INTERNAL_ERR;
00957 
00958         if ( fprintf(fp, "  <filename value=\"%s\" />\n", fileName) == EOF)
00959                 return VETRET_INTERNAL_ERR;
00960 
00961         if ( fprintf(fp, "  <stream value=\"%d\" />\n", stream) == EOF)
00962                 return VETRET_INTERNAL_ERR;
00963 
00964         if ( fprintf(fp, "  <frameIndex value=\"%ld\" />\n", frameIndex) == EOF)
00965                 return VETRET_INTERNAL_ERR;
00966 
00967         if( fprintf(fp, "</vetCodec_MOVParameters>\n") == EOF )
00968                 return VETRET_INTERNAL_ERR;
00969 
00970         return VETRET_OK;
00971  }
00972 
00973 
00974 int vetCodec_MOVParameters::loadFromStreamXML(FILE *fp)
00975  {
00976         if ( fscanf(fp, "<vetCodec_MOVParameters>\n") == EOF )
00977                 throw "error in XML file, unable to import data.";
00978 
00979         if ( fscanf(fp, "  <filename value=\"%s\" />\n", fileName) == EOF )
00980                 throw "error in XML file, unable to import data.";
00981 
00982         if ( fscanf(fp, "  <stream value=\"%d\" />\n", &stream) == EOF )
00983                 throw "error in XML file, unable to import data.";
00984 
00985         if ( fscanf(fp, "  <frameIndex value=\"%ld\" />\n", &frameIndex) == EOF )
00986                 throw "error in XML file, unable to import data.";
00987 
00988 
00989         return VETRET_OK;
00990  }
00991 
00992 
00993 
00994 

Generated on Tue Jan 24 11:58:59 2006 for VETLib by  doxygen 1.4.4