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  memset(&m_pgm, 0, sizeof(m_pgm));
27  memset(&m_convert_time, 0, sizeof(m_convert_time));
28 }
29 
31 {
32  m_width = -1;
33 #ifdef PGM_CONVERT_GREYSCALE
34  av_freep(&m_pgm.data[0]);
35  memset(&m_pgm, 0, sizeof(m_pgm));
36  delete m_copy;
37 #endif /* PGM_CONVERT_GREYSCALE */
38 }
39 
40 int
42 {
43 #ifdef PGM_CONVERT_GREYSCALE
44  m_time_reported = false;
45  memset(&m_convert_time, 0, sizeof(m_convert_time));
46 #endif /* PGM_CONVERT_GREYSCALE */
47 
48  if (m_width != -1)
49  return 0;
50 
51  QSize buf_dim = player->GetVideoBufferSize();
52  m_width = buf_dim.width();
53  m_height = buf_dim.height();
54 
55 #ifdef PGM_CONVERT_GREYSCALE
56  if (av_image_alloc(m_pgm.data, m_pgm.linesize,
57  m_width, m_height, AV_PIX_FMT_GRAY8, IMAGE_ALIGN))
58  {
59  LOG(VB_COMMFLAG, LOG_ERR, QString("PGMConverter::MythPlayerInited "
60  "av_image_alloc m_pgm (%1x%2) failed")
61  .arg(m_width).arg(m_height));
62  return -1;
63  }
64 
65  delete m_copy;
66  m_copy = new MythAVCopy;
67  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
68  "using true greyscale conversion"));
69 #else /* !PGM_CONVERT_GREYSCALE */
70  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
71  "(YUV shortcut)"));
72 #endif /* !PGM_CONVERT_GREYSCALE */
73 
74  return 0;
75 }
76 
77 const AVFrame *
78 PGMConverter::getImage(const VideoFrame *frame, long long _frameno,
79  int *pwidth, int *pheight)
80 {
81 #ifdef PGM_CONVERT_GREYSCALE
82  struct timeval start, end, elapsed;
83 #endif /* PGM_CONVERT_GREYSCALE */
84 
85  if (m_frameno == _frameno)
86  goto out;
87 
88  if (!frame->buf)
89  {
90  LOG(VB_COMMFLAG, LOG_ERR, "PGMConverter::getImage no buf");
91  goto error;
92  }
93 
94 #ifdef PGM_CONVERT_GREYSCALE
95  (void)gettimeofday(&start, nullptr);
96  if (m_copy->Copy(&m_pgm, frame, m_pgm.data[0], AV_PIX_FMT_GRAY8) < 0)
97  goto error;
98  (void)gettimeofday(&end, nullptr);
99  timersub(&end, &start, &elapsed);
100  timeradd(&m_convert_time, &elapsed, &m_convert_time);
101 #else /* !PGM_CONVERT_GREYSCALE */
102  if (av_image_fill_arrays(m_pgm.data, m_pgm.linesize,
103  frame->buf, AV_PIX_FMT_GRAY8, m_width, m_height,IMAGE_ALIGN) < 0)
104  {
105  LOG(VB_COMMFLAG, LOG_ERR,
106  QString("PGMConverter::getImage error at frame %1 (%2x%3)")
107  .arg(_frameno).arg(m_width).arg(m_height));
108  goto error;
109  }
110 #endif /* !PGM_CONVERT_GREYSCALE */
111 
112  m_frameno = _frameno;
113 
114 out:
115  *pwidth = m_width;
116  *pheight = m_height;
117  return &m_pgm;
118 
119 error:
120  return nullptr;
121 }
122 
123 int
125 {
126 #ifdef PGM_CONVERT_GREYSCALE
127  if (!m_time_reported)
128  {
129  LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
130  .arg(strftimeval(&m_convert_time)));
131  m_time_reported = true;
132  }
133 #endif /* PGM_CONVERT_GREYSCALE */
134  return 0;
135 }
136 
137 /* 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)
PGMConverter(void)
#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)