00001
00019 #include "vetCodec_MPEG.h"
00020 #include "../vetUtility.h"
00021
00022
00023
00028 vetCodec_MPEG::vetCodec_MPEG(char *filename, int stream) : vetCodec()
00029 {
00030 DEBUGMSG("vetCodec_MPEG::vetCodec_MPEG(char *filename, FileFormat format) [CONTRUCTOR] ", *filename)
00031
00032 buff = NULL;
00033 file = NULL;
00034 setParameters(NULL);
00035 reset();
00036
00037 load( filename, stream );
00038
00039 }
00040
00048 vetCodec_MPEG::vetCodec_MPEG( vetCodec_MPEGParameters* initParams ) : vetCodec()
00049 {
00050 DEBUGMSG("vetCodec_MPEG::vetCodec_MPEG(vetCodec_MPEGParameters* initParams) [CONTRUCTOR] ", *filename)
00051
00052 buff = NULL;
00053 file = NULL;
00054
00055 setParameters(initParams);
00056 reset();
00057 }
00058
00064 VETRESULT vetCodec_MPEG::reset()
00065 {
00066 INFO("VETRESULT vetCodec_MPEG::reset() [SET DEFAULT PARAMETERS]")
00067
00068 setName("MPEG Coder");
00069 setDescription("Read or write video stream.");
00070 setVersion(1.0);
00071
00072 if (file != NULL)
00073 {
00074 mpeg3_close(file);
00075 file = NULL;
00076 }
00077
00078 if (buff != NULL)
00079 {
00080
00081 for (unsigned int i = 0; i < height; i++)
00082 delete [] buff[i];
00083
00084
00085 delete [] buff;
00086 }
00087
00088 width = 0;
00089 height = 0;
00090
00091 if (myParams != NULL)
00092 myParams->reset();
00093
00094 return VETRET_OK;
00095 }
00096
00097
00107 VETRESULT vetCodec_MPEG::setParameters (vetCodec_MPEGParameters* initParams)
00108 {
00109
00110 if ( initParams == NULL )
00111 myParams = new vetCodec_MPEGParameters();
00112 else
00113 myParams = initParams;
00114
00115 return VETRET_OK;
00116 }
00117
00121 vetCodec_MPEG::~vetCodec_MPEG()
00122 {
00123 if (file != NULL)
00124 mpeg3_close(file);
00125
00126 if (buff != NULL)
00127 {
00128
00129 for (unsigned int i = 0; i < height; i++)
00130 delete [] buff[i];
00131
00132
00133 delete [] buff;
00134 }
00135 }
00136
00137 bool vetCodec_MPEG::EoF()
00138 {
00139 if (file == NULL)
00140 return true;
00141
00142 return false;
00143 }
00144
00145
00158 VETRESULT vetCodec_MPEG::extractTo(vetFrameYUV420& img)
00159 {
00160 DEBUGMSG("VETRESULT vetCodec_MPEG::extractTo(vetFrameYUV420& img) [pushing data]", doBuffering)
00161
00162 if (file == NULL)
00163 return VETRET_ILLEGAL_USE;
00164
00165 if (width != img.width || height != img.height)
00166 img.reAllocCanvas(width, height);
00167
00168
00169 mpeg3_read_yuvframe(file,
00170 (char*)img.Y,
00171 (char*)img.U,
00172 (char*)img.V,
00173 0, 0,
00174 width, height,
00175 0);
00176
00177
00178
00179
00180
00181 return VETRET_NOT_IMPLEMENTED;
00182 }
00183
00184
00197 VETRESULT vetCodec_MPEG::extractTo(vetFrameRGB24& img)
00198 {
00199 DEBUGMSG("VETRESULT vetCodec_MPEG::extractTo(vetFrameRGB24& img) [pushing data]", doBuffering)
00200
00201 if (file == NULL)
00202 return VETRET_ILLEGAL_USE;
00203
00204 if (width != img.width || height != img.height)
00205 img.reAllocCanvas(width, height);
00206
00207
00208 mpeg3_read_frame(file, buff,
00209 0, 0,
00210 width, height,
00211 width, height,
00212 MPEG3_RGB888, 0);
00213
00214
00215 vetUtility::conv_rgb24_rgb96(buff, (unsigned char*)img.data[0], width, height);
00216
00217
00218 return VETRET_OK;
00219
00220 }
00221
00222
00235 VETRESULT vetCodec_MPEG::extractTo(vetFrameT<unsigned char>& img)
00236 {
00237 DEBUGMSG("VETRESULT vetCodec_MPEG::extractTo(vetFrameT& img) [pushing data]", doBuffering)
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 return VETRET_NOT_IMPLEMENTED;
00248
00249
00250 }
00251
00252
00264 VETRESULT vetCodec_MPEG::importFrom(vetFrameYUV420& img)
00265 {
00266 DEBUGMSG("VETRESULT vetCodec_MPEG::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00267
00268 return VETRET_NOT_IMPLEMENTED;
00269
00270
00271 }
00272
00284 VETRESULT vetCodec_MPEG::importFrom(vetFrameRGB24& img)
00285 {
00286 DEBUGMSG("VETRESULT vetCodec_MPEG::importFrom(vetFrameRGB24& img) [reading data]", doBuffering)
00287
00288 return VETRET_NOT_IMPLEMENTED;
00289
00290
00291 }
00292
00304 VETRESULT vetCodec_MPEG::importFrom(vetFrameT<unsigned char>& img)
00305 {
00306 DEBUGMSG("VETRESULT vetCodec_MPEG::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00307
00308 return VETRET_NOT_IMPLEMENTED;
00309
00310
00311 }
00312
00313
00314
00321 VETRESULT vetCodec_MPEG::load()
00322 {
00323 INFO("VETRESULT vetCodec_MPEG::load() [loading data to buffer]")
00324
00325
00326 if ( !mpeg3_check_sig(myParams->fileName) )
00327 return VETRET_ILLEGAL_USE;
00328
00329 file = mpeg3_open(myParams->fileName);
00330
00331 if (file == NULL)
00332 return VETRET_ILLEGAL_USE;
00333
00334
00335
00336 if (!mpeg3_has_video(file))
00337 return VETRET_ILLEGAL_USE;
00338
00339
00340
00341 width = mpeg3_video_width(file, myParams->stream);
00342 height = mpeg3_video_height(file, myParams->stream);
00343
00344
00345 buff = new (unsigned char*)[height];
00346 for (unsigned int i = 0; i < height - 1; i++)
00347 buff[i] = new (unsigned char)[width*3];
00348
00349
00350 buff[height - 1] = new (unsigned char)[width*3 + 4];
00351
00352
00353 Y = new char[width*height] ;
00354 U = new char[width*height/4] ;
00355 V = new char[width*height/4] ;
00356
00357 return VETRET_OK;
00358 }
00359
00368 VETRESULT vetCodec_MPEG::load(char *filename, int stream)
00369 {
00370 DEBUGMSG("VETRESULT vetCodec_MPEG::load(char *filename, FileFormat format) [loading data to buffer]", filename)
00371
00372 myParams->setFileName(filename);
00373 myParams->setStream(stream);
00374
00375 return load();
00376 }
00377
00378
00379
00389 VETRESULT vetCodec_MPEG::goToPreviousFrame(int stream)
00390 {
00391 if ( file == NULL)
00392 return VETRET_ILLEGAL_USE;
00393
00394 if ( stream == -1 )
00395 return mpeg3_previous_frame(file, myParams->stream);
00396
00397 if ( stream < 0 || stream >= getVideoStreamCount() )
00398 return VETRET_PARAM_ERR;
00399
00400 return mpeg3_previous_frame(file, stream);
00401 }
00402
00412 VETRESULT vetCodec_MPEG::goToVideoEnd(int stream)
00413 {
00414 if ( file == NULL)
00415 return VETRET_ILLEGAL_USE;
00416
00417 if ( stream == -1 )
00418 return mpeg3_end_of_video(file, myParams->stream);
00419
00420 if ( stream < 0 || stream >= getVideoStreamCount() )
00421 return VETRET_PARAM_ERR;
00422
00423 return mpeg3_end_of_video(file, stream);
00424 }
00425
00435 VETRESULT vetCodec_MPEG::goToAudioEnd(int stream)
00436 {
00437 if ( file == NULL)
00438 return VETRET_ILLEGAL_USE;
00439
00440 if ( stream == -1 )
00441 return mpeg3_end_of_audio(file, myParams->stream);
00442
00443 if ( stream < 0 || stream >= getAudioStreamCount() )
00444 return VETRET_PARAM_ERR;
00445
00446 return mpeg3_end_of_audio(file, stream);
00447 }
00448
00449
00450 double vetCodec_MPEG::getLastPacketTime()
00451 {
00452 if ( file == NULL)
00453 return -1;
00454
00455 return mpeg3_get_time(file);
00456 }
00457
00458
00459
00465 bool vetCodec_MPEG::hasVideo()
00466 {
00467 if ( file == NULL)
00468 return false;
00469
00470 if ( mpeg3_has_video(file) )
00471 return true;
00472 else
00473 return false;
00474 }
00475
00481 int vetCodec_MPEG::getVideoStreamCount()
00482 {
00483 if ( file == NULL)
00484 return -1;
00485
00486 return mpeg3_total_vstreams(file);
00487 }
00488
00494 bool vetCodec_MPEG::hasAudio()
00495 {
00496 if ( file == NULL)
00497 return false;
00498
00499 if ( mpeg3_has_audio(file) )
00500 return true;
00501 else
00502 return false;
00503 }
00504
00510 int vetCodec_MPEG::getAudioStreamCount()
00511 {
00512 if ( file == NULL)
00513 return -1;
00514
00515 return mpeg3_total_astreams(file);
00516 }
00517
00526 long vetCodec_MPEG::getAudioStreamLength(int stream)
00527 {
00528 if ( hasAudio() && stream == -1 )
00529 return mpeg3_audio_samples(file, myParams->stream);
00530
00531 if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00532 return -1;
00533
00534 return mpeg3_audio_samples(file, stream);
00535 }
00536
00545 long vetCodec_MPEG::getVideoStreamLength(int stream)
00546 {
00547 if ( hasVideo() && stream == -1 )
00548 return mpeg3_video_frames(file, myParams->stream);
00549
00550 if ( !hasVideo() || stream < 0 || stream >= getVideoStreamCount() )
00551 return -1;
00552
00553 return mpeg3_video_frames(file, stream);
00554 }
00555
00564 float vetCodec_MPEG::getAudioSampleRate(int stream)
00565 {
00566 if ( hasAudio() && stream == -1 )
00567 return mpeg3_sample_rate(file, myParams->stream);
00568
00569 if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00570 return -1;
00571
00572 return mpeg3_sample_rate(file, stream);
00573 }
00574
00583 int vetCodec_MPEG::getAudioChannels(int stream)
00584 {
00585 if ( hasAudio() && stream == -1 )
00586 return mpeg3_audio_channels(file, myParams->stream);
00587
00588 if ( !hasAudio() || stream < 0 || stream >= getAudioStreamCount() )
00589 return -1;
00590
00591 return mpeg3_audio_channels(file, stream);
00592 }
00593
00602 float vetCodec_MPEG::getVideoFrameRate(int stream)
00603 {
00604 if ( hasVideo() && stream == -1 )
00605 return mpeg3_frame_rate(file, myParams->stream);
00606
00607 if ( !hasVideo() || stream < 0 || stream >= getVideoStreamCount() )
00608 return -1;
00609
00610 return mpeg3_frame_rate(file, stream);
00611 }
00612
00621 int vetCodec_MPEG::getColorModel(int stream)
00622 {
00623 if ( !hasVideo() || stream < 0 || stream >= getVideoStreamCount() )
00624 return -1;
00625
00626 return mpeg3_colormodel(file, stream);
00627 }
00628
00629
00630
00631
00632
00633
00644 VETRESULT vetCodec_MPEG::setFrameIndex(long index, int stream)
00645 {
00646 if ( file == NULL)
00647 return VETRET_ILLEGAL_USE;
00648
00649 if ( hasVideo() && stream == -1 )
00650 return mpeg3_set_frame(file, index, myParams->stream);
00651
00652 if ( !hasVideo() || stream < 0 || stream >= getVideoStreamCount() )
00653 return VETRET_ILLEGAL_USE;
00654
00655 myParams->setFrameIndex(index);
00656
00657 return mpeg3_set_frame(file, index, stream);
00658 }
00659
00665 bool vetCodec_MPEG::eof(int stream) const
00666 {
00667 if ( file == NULL)
00668 return true;
00669
00670 if ( stream == -1 )
00671 if ( mpeg3_end_of_video(file, myParams->stream) == 0 )
00672 return false;
00673 else
00674 return true;
00675
00676 if ( mpeg3_end_of_video(file, stream) == 0 )
00677 return false;
00678 else
00679 return true;
00680 }
00681
00682
00683
00684
00685
00686
00687 int vetCodec_MPEG::getMPEG3_Version_major() { return mpeg3_major(); };
00688 int vetCodec_MPEG::getMPEG3_Version_minor() { return mpeg3_minor(); };
00689 int vetCodec_MPEG::getMPEG3_Version_release() { return mpeg3_release(); };
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 vetCodec_MPEGParameters::vetCodec_MPEGParameters()
00714 {
00715 reset();
00716 }
00717
00718 vetCodec_MPEGParameters::vetCodec_MPEGParameters(const char* filename, int stream, long frameIndex)
00719 {
00720 reset();
00721 setFileName(filename);
00722 setStream(stream);
00723 setFrameIndex(frameIndex);
00724 }
00725
00726 void vetCodec_MPEGParameters::reset()
00727 {
00728 strcpy(fileName, (const char*)"input.mpg\0");
00729 frameIndex = 0;
00730 stream = 0;
00731 }
00732
00733
00734 void vetCodec_MPEGParameters::setFileName(const char *filename)
00735 {
00736 strncpy(fileName, filename, 64);
00737 }
00738
00739 void vetCodec_MPEGParameters::setFrameIndex(long index)
00740 {
00741 frameIndex = index;
00742 }
00743
00744 void vetCodec_MPEGParameters::setStream(int s)
00745 {
00746 stream = s;
00747 }
00748
00749
00750 int vetCodec_MPEGParameters::saveToStreamXML(FILE *fp)
00751 {
00752 if ( fp == NULL )
00753 return VETRET_PARAM_ERR;
00754
00755 if( fprintf(fp, "<vetCodec_MPEGParameters>\n") == EOF )
00756 return VETRET_INTERNAL_ERR;
00757
00758 if ( fprintf(fp, " <filename value=\"%s\" />\n", fileName) == EOF)
00759 return VETRET_INTERNAL_ERR;
00760
00761 if ( fprintf(fp, " <stream value=\"%d\" />\n", stream) == EOF)
00762 return VETRET_INTERNAL_ERR;
00763
00764 if ( fprintf(fp, " <frameIndex value=\"%ld\" />\n", frameIndex) == EOF)
00765 return VETRET_INTERNAL_ERR;
00766
00767 if( fprintf(fp, "</vetCodec_MOVParameters>\n") == EOF )
00768 return VETRET_INTERNAL_ERR;
00769
00770 return VETRET_OK;
00771 }
00772
00773
00774 int vetCodec_MPEGParameters::loadFromStreamXML(FILE *fp)
00775 {
00776 if ( fscanf(fp, "<vetCodec_MPEGParameters>\n") == EOF )
00777 throw "error in XML file, unable to import data.";
00778
00779 if ( fscanf(fp, " <filename value=\"%s\" />\n", fileName) == EOF )
00780 throw "error in XML file, unable to import data.";
00781
00782 if ( fscanf(fp, " <stream value=\"%d\" />\n", &stream) == EOF )
00783 throw "error in XML file, unable to import data.";
00784
00785 if ( fscanf(fp, " <frameIndex value=\"%ld\" />\n", &frameIndex) == EOF )
00786 throw "error in XML file, unable to import data.";
00787
00788 return VETRET_OK;
00789 }