00001 #include "NuppelVideoPlayer.h"
00002 #include "frame.h"
00003
00004 #include "CommDetector2.h"
00005 #include "pgm.h"
00006 #include "PGMConverter.h"
00007
00008 using namespace commDetector2;
00009
00010 PGMConverter::PGMConverter(void)
00011 : frameno(-1)
00012 , width(-1)
00013 , height(-1)
00014 #ifdef PGM_CONVERT_GREYSCALE
00015 , time_reported(false)
00016 #endif
00017 {
00018 memset(&pgm, 0, sizeof(pgm));
00019 }
00020
00021 PGMConverter::~PGMConverter(void)
00022 {
00023 width = -1;
00024 #ifdef PGM_CONVERT_GREYSCALE
00025 avpicture_free(&pgm);
00026 memset(&pgm, 0, sizeof(pgm));
00027 #endif
00028 }
00029
00030 int
00031 PGMConverter::nuppelVideoPlayerInited(const NuppelVideoPlayer *nvp)
00032 {
00033 #ifdef PGM_CONVERT_GREYSCALE
00034 time_reported = false;
00035 memset(&convert_time, 0, sizeof(convert_time));
00036 #endif
00037
00038 if (width != -1)
00039 return 0;
00040
00041 QSize buf_dim = nvp->GetVideoBufferSize();
00042 width = buf_dim.width();
00043 height = buf_dim.height();
00044
00045 #ifdef PGM_CONVERT_GREYSCALE
00046 if (avpicture_alloc(&pgm, PIX_FMT_GRAY8, width, height))
00047 {
00048 VERBOSE(VB_COMMFLAG, QString("PGMConverter::nuppelVideoPlayerInited "
00049 "avpicture_alloc pgm (%1x%2) failed")
00050 .arg(width).arg(height));
00051 return -1;
00052 }
00053 VERBOSE(VB_COMMFLAG, QString("PGMConverter::nuppelVideoPlayerInited "
00054 "using true greyscale conversion"));
00055 #else
00056 VERBOSE(VB_COMMFLAG, QString("PGMConverter::nuppelVideoPlayerInited "
00057 "(YUV shortcut)"));
00058 #endif
00059
00060 return 0;
00061 }
00062
00063 const AVPicture *
00064 PGMConverter::getImage(const VideoFrame *frame, long long _frameno,
00065 int *pwidth, int *pheight)
00066 {
00067 #ifdef PGM_CONVERT_GREYSCALE
00068 struct timeval start, end, elapsed;
00069 #endif
00070
00071 if (frameno == _frameno)
00072 goto out;
00073
00074 if (!frame->buf)
00075 {
00076 VERBOSE(VB_COMMFLAG, "PGMConverter::getImage no buf");
00077 goto error;
00078 }
00079
00080 #ifdef PGM_CONVERT_GREYSCALE
00081 (void)gettimeofday(&start, NULL);
00082 if (pgm_fill(&pgm, frame))
00083 goto error;
00084 (void)gettimeofday(&end, NULL);
00085 timersub(&end, &start, &elapsed);
00086 timeradd(&convert_time, &elapsed, &convert_time);
00087 #else
00088 if (avpicture_fill(&pgm, frame->buf, PIX_FMT_GRAY8, width, height) == -1)
00089 {
00090 VERBOSE(VB_COMMFLAG, QString(
00091 "PGMConverter::getImage error at frame %1 (%2x%3)")
00092 .arg(_frameno).arg(width).arg(height));
00093 goto error;
00094 }
00095 #endif
00096
00097 frameno = _frameno;
00098
00099 out:
00100 *pwidth = width;
00101 *pheight = height;
00102 return &pgm;
00103
00104 error:
00105 return NULL;
00106 }
00107
00108 int
00109 PGMConverter::reportTime(void)
00110 {
00111 #ifdef PGM_CONVERT_GREYSCALE
00112 if (!time_reported)
00113 {
00114 VERBOSE(VB_COMMFLAG, QString("PGM Time: convert=%1s")
00115 .arg(strftimeval(&convert_time)));
00116 time_reported = true;
00117 }
00118 #endif
00119 return 0;
00120 }
00121
00122