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_time_reported = false;
39  memset(&m_convert_time, 0, sizeof(m_convert_time));
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 {}, end {}, elapsed {};
77 #endif /* PGM_CONVERT_GREYSCALE */
78 
79  if (m_frameno == _frameno)
80  goto out;
81 
82  if (!frame->buf)
83  {
84  LOG(VB_COMMFLAG, LOG_ERR, "PGMConverter::getImage no buf");
85  goto error;
86  }
87 
88 #ifdef PGM_CONVERT_GREYSCALE
89  (void)gettimeofday(&start, nullptr);
90  if (m_copy->Copy(&m_pgm, frame, m_pgm.data[0], AV_PIX_FMT_GRAY8) < 0)
91  goto error;
92  (void)gettimeofday(&end, nullptr);
93  timersub(&end, &start, &elapsed);
95 #else /* !PGM_CONVERT_GREYSCALE */
96  if (av_image_fill_arrays(m_pgm.data, m_pgm.linesize,
97  frame->buf, AV_PIX_FMT_GRAY8, m_width, m_height,IMAGE_ALIGN) < 0)
98  {
99  LOG(VB_COMMFLAG, LOG_ERR,
100  QString("PGMConverter::getImage error at frame %1 (%2x%3)")
101  .arg(_frameno).arg(m_width).arg(m_height));
102  goto error;
103  }
104 #endif /* !PGM_CONVERT_GREYSCALE */
105 
106  m_frameno = _frameno;
107 
108 out:
109  *pwidth = m_width;
110  *pheight = m_height;
111  return &m_pgm;
112 
113 error:
114  return nullptr;
115 }
116 
117 int
119 {
120 #ifdef PGM_CONVERT_GREYSCALE
121  if (!m_time_reported)
122  {
123  LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
124  .arg(strftimeval(&m_convert_time)));
125  m_time_reported = true;
126  }
127 #endif /* PGM_CONVERT_GREYSCALE */
128  return 0;
129 }
130 
131 /* vim: set expandtab tabstop=4 shiftwidth=4: */
static void error(const char *str,...)
Definition: vbi.c:42
#define timeradd(a, b, result)
Definition: compat.h:300
struct AVFrame AVFrame
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:114
~PGMConverter(void)
int MythPlayerInited(const MythPlayer *player)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QSize GetVideoBufferSize(void) const
Definition: mythplayer.h:173
QString strftimeval(const struct timeval *tv)
#define timersub(a, b, result)
Definition: compat.h:310
unsigned char * buf
Definition: mythframe.h:39
const AVFrame * getImage(const VideoFrame *frame, long long frameno, int *pwidth, int *pheight)
int reportTime(void)