MythTV  0.26-pre
PGMConverter.cpp
Go to the documentation of this file.
00001 // POSIX headers
00002 #include <sys/time.h> // for gettimeofday
00003 
00004 // Qt headers
00005 #include <QSize>
00006 
00007 // MythTV headers
00008 #include "mythlogging.h"
00009 #include "mythplayer.h"
00010 #include "frame.h"          /* VideoFrame */
00011 
00012 // Commercial Flagging headers
00013 #include "CommDetector2.h"
00014 #include "pgm.h"
00015 #include "PGMConverter.h"
00016 
00017 using namespace commDetector2;
00018 
00019 PGMConverter::PGMConverter(void)
00020     : frameno(-1)
00021     , width(-1)
00022     , height(-1)
00023 #ifdef PGM_CONVERT_GREYSCALE
00024     , time_reported(false)
00025 #endif /* PGM_CONVERT_GREYSCALE */
00026 {
00027     memset(&pgm, 0, sizeof(pgm));
00028     memset(&convert_time, 0, sizeof(convert_time));
00029 }
00030 
00031 PGMConverter::~PGMConverter(void)
00032 {
00033     width = -1;
00034 #ifdef PGM_CONVERT_GREYSCALE
00035     avpicture_free(&pgm);
00036     memset(&pgm, 0, sizeof(pgm));
00037 #endif /* PGM_CONVERT_GREYSCALE */
00038 }
00039 
00040 int
00041 PGMConverter::MythPlayerInited(const MythPlayer *player)
00042 {
00043 #ifdef PGM_CONVERT_GREYSCALE
00044     time_reported = false;
00045     memset(&convert_time, 0, sizeof(convert_time));
00046 #endif /* PGM_CONVERT_GREYSCALE */
00047 
00048     if (width != -1)
00049         return 0;
00050 
00051     QSize buf_dim = player->GetVideoBufferSize();
00052     width  = buf_dim.width();
00053     height = buf_dim.height();
00054 
00055 #ifdef PGM_CONVERT_GREYSCALE
00056     if (avpicture_alloc(&pgm, PIX_FMT_GRAY8, width, height))
00057     {
00058         LOG(VB_COMMFLAG, LOG_ERR, QString("PGMConverter::MythPlayerInited "
00059                                           "avpicture_alloc pgm (%1x%2) failed")
00060                 .arg(width).arg(height));
00061         return -1;
00062     }
00063     LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
00064                                        "using true greyscale conversion"));
00065 #else  /* !PGM_CONVERT_GREYSCALE */
00066     LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
00067                                        "(YUV shortcut)"));
00068 #endif /* !PGM_CONVERT_GREYSCALE */
00069 
00070     return 0;
00071 }
00072 
00073 const AVPicture *
00074 PGMConverter::getImage(const VideoFrame *frame, long long _frameno,
00075         int *pwidth, int *pheight)
00076 {
00077 #ifdef PGM_CONVERT_GREYSCALE
00078     struct timeval      start, end, elapsed;
00079 #endif /* PGM_CONVERT_GREYSCALE */
00080 
00081     if (frameno == _frameno)
00082         goto out;
00083 
00084     if (!frame->buf)
00085     {
00086         LOG(VB_COMMFLAG, LOG_ERR, "PGMConverter::getImage no buf");
00087         goto error;
00088     }
00089 
00090 #ifdef PGM_CONVERT_GREYSCALE
00091     (void)gettimeofday(&start, NULL);
00092     if (pgm_fill(&pgm, frame))
00093         goto error;
00094     (void)gettimeofday(&end, NULL);
00095     timersub(&end, &start, &elapsed);
00096     timeradd(&convert_time, &elapsed, &convert_time);
00097 #else  /* !PGM_CONVERT_GREYSCALE */
00098     if (avpicture_fill(&pgm, frame->buf, PIX_FMT_GRAY8, width, height) == -1)
00099     {
00100         LOG(VB_COMMFLAG, LOG_ERR,
00101             QString("PGMConverter::getImage error at frame %1 (%2x%3)")
00102                 .arg(_frameno).arg(width).arg(height));
00103         goto error;
00104     }
00105 #endif /* !PGM_CONVERT_GREYSCALE */
00106 
00107     frameno = _frameno;
00108 
00109 out:
00110     *pwidth = width;
00111     *pheight = height;
00112     return &pgm;
00113 
00114 error:
00115     return NULL;
00116 }
00117 
00118 int
00119 PGMConverter::reportTime(void)
00120 {
00121 #ifdef PGM_CONVERT_GREYSCALE
00122     if (!time_reported)
00123     {
00124         LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
00125                 .arg(strftimeval(&convert_time)));
00126         time_reported = true;
00127     }
00128 #endif /* PGM_CONVERT_GREYSCALE */
00129     return 0;
00130 }
00131 
00132 /* vim: set expandtab tabstop=4 shiftwidth=4: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends