MythTV  master
PGMConverter.cpp
Go to the documentation of this file.
1 // POSIX headers
2 #include <sys/time.h> // for gettimeofday
3 
4 // Qt headers
5 #include <QSize>
6 
7 // MythTV headers
8 #include "mythlogging.h"
9 #include "mythplayer.h"
10 #include "mythframe.h" /* VideoFrame */
11 #include "mythavutil.h"
12 
13 // Commercial Flagging headers
14 #include "CommDetector2.h"
15 #include "pgm.h"
16 #include "PGMConverter.h"
17 
18 extern "C" {
19 #include "libavutil/imgutils.h"
20 }
21 
22 using namespace commDetector2;
23 
25 {
26  m_width = -1;
27 #ifdef PGM_CONVERT_GREYSCALE
28  av_freep(&m_pgm.data[0]);
29  memset(&m_pgm, 0, sizeof(m_pgm));
30  delete m_copy;
31 #endif /* PGM_CONVERT_GREYSCALE */
32 }
33 
34 int
36 {
37 #ifdef PGM_CONVERT_GREYSCALE
38  m_timeReported = false;
39  memset(&m_convertTime, 0, sizeof(m_convertTime));
40 #endif /* PGM_CONVERT_GREYSCALE */
41 
42  if (m_width != -1)
43  return 0;
44 
45  QSize buf_dim = player->GetVideoBufferSize();
46  m_width = buf_dim.width();
47  m_height = buf_dim.height();
48 
49 #ifdef PGM_CONVERT_GREYSCALE
50  if (av_image_alloc(m_pgm.data, m_pgm.linesize,
51  m_width, m_height, AV_PIX_FMT_GRAY8, IMAGE_ALIGN))
52  {
53  LOG(VB_COMMFLAG, LOG_ERR, QString("PGMConverter::MythPlayerInited "
54  "av_image_alloc m_pgm (%1x%2) failed")
55  .arg(m_width).arg(m_height));
56  return -1;
57  }
58 
59  delete m_copy;
60  m_copy = new MythAVCopy;
61  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
62  "using true greyscale conversion"));
63 #else /* !PGM_CONVERT_GREYSCALE */
64  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
65  "(YUV shortcut)"));
66 #endif /* !PGM_CONVERT_GREYSCALE */
67 
68  return 0;
69 }
70 
71 const AVFrame *
72 PGMConverter::getImage(const VideoFrame *frame, long long _frameno,
73  int *pwidth, int *pheight)
74 {
75 #ifdef PGM_CONVERT_GREYSCALE
76  struct timeval start {};
77  struct timeval end {};
78  struct timeval elapsed {};
79 #endif /* PGM_CONVERT_GREYSCALE */
80 
81  if (m_frameNo == _frameno)
82  goto out;
83 
84  if (!frame->buf)
85  {
86  LOG(VB_COMMFLAG, LOG_ERR, "PGMConverter::getImage no buf");
87  goto error;
88  }
89 
90 #ifdef PGM_CONVERT_GREYSCALE
91  (void)gettimeofday(&start, nullptr);
92  if (m_copy->Copy(&m_pgm, frame, m_pgm.data[0], AV_PIX_FMT_GRAY8) < 0)
93  goto error;
94  (void)gettimeofday(&end, nullptr);
95  timersub(&end, &start, &elapsed);
96  timeradd(&m_convertTime, &elapsed, &m_convertTime);
97 #else /* !PGM_CONVERT_GREYSCALE */
98  if (av_image_fill_arrays(m_pgm.data, m_pgm.linesize,
99  frame->buf, AV_PIX_FMT_GRAY8, m_width, m_height,IMAGE_ALIGN) < 0)
100  {
101  LOG(VB_COMMFLAG, LOG_ERR,
102  QString("PGMConverter::getImage error at frame %1 (%2x%3)")
103  .arg(_frameno).arg(m_width).arg(m_height));
104  goto error;
105  }
106 #endif /* !PGM_CONVERT_GREYSCALE */
107 
108  m_frameNo = _frameno;
109 
110 out:
111  *pwidth = m_width;
112  *pheight = m_height;
113  return &m_pgm;
114 
115 error:
116  return nullptr;
117 }
118 
119 int
121 {
122 #ifdef PGM_CONVERT_GREYSCALE
123  if (!m_timeReported)
124  {
125  LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
126  .arg(strftimeval(&m_convertTime)));
127  m_timeReported = true;
128  }
129 #endif /* PGM_CONVERT_GREYSCALE */
130  return 0;
131 }
132 
133 /* vim: set expandtab tabstop=4 shiftwidth=4: */
unsigned char * buf
Definition: mythframe.h:139
static void error(const char *str,...)
Definition: vbi.c:42
#define timeradd(a, b, result)
Definition: compat.h:300
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:114
struct AVFrame AVFrame
~PGMConverter(void)
int MythPlayerInited(const MythPlayer *player)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QSize GetVideoBufferSize(void) const
Definition: mythplayer.h:208
QString strftimeval(const struct timeval *tv)
#define timersub(a, b, result)
Definition: compat.h:310
const AVFrame * getImage(const VideoFrame *frame, long long frameno, int *pwidth, int *pheight)
int reportTime(void)