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  : frameno(-1)
26  , width(-1)
27  , height(-1)
29  , time_reported(false)
30  , m_copy(nullptr)
31 #endif /* PGM_CONVERT_GREYSCALE */
32 {
33  memset(&pgm, 0, sizeof(pgm));
34  memset(&convert_time, 0, sizeof(convert_time));
35 }
36 
38 {
39  width = -1;
40 #ifdef PGM_CONVERT_GREYSCALE
41  av_freep(&pgm.data[0]);
42  memset(&pgm, 0, sizeof(pgm));
43  delete m_copy;
44 #endif /* PGM_CONVERT_GREYSCALE */
45 }
46 
47 int
49 {
50 #ifdef PGM_CONVERT_GREYSCALE
51  time_reported = false;
52  memset(&convert_time, 0, sizeof(convert_time));
53 #endif /* PGM_CONVERT_GREYSCALE */
54 
55  if (width != -1)
56  return 0;
57 
58  QSize buf_dim = player->GetVideoBufferSize();
59  width = buf_dim.width();
60  height = buf_dim.height();
61 
62 #ifdef PGM_CONVERT_GREYSCALE
63  if (av_image_alloc(pgm.data, pgm.linesize,
64  width, height, AV_PIX_FMT_GRAY8, IMAGE_ALIGN))
65  {
66  LOG(VB_COMMFLAG, LOG_ERR, QString("PGMConverter::MythPlayerInited "
67  "av_image_alloc pgm (%1x%2) failed")
68  .arg(width).arg(height));
69  return -1;
70  }
71  if (m_copy)
72  {
73  delete m_copy;
74  }
75  m_copy = new MythAVCopy;
76  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
77  "using true greyscale conversion"));
78 #else /* !PGM_CONVERT_GREYSCALE */
79  LOG(VB_COMMFLAG, LOG_INFO, QString("PGMConverter::MythPlayerInited "
80  "(YUV shortcut)"));
81 #endif /* !PGM_CONVERT_GREYSCALE */
82 
83  return 0;
84 }
85 
86 const AVFrame *
87 PGMConverter::getImage(const VideoFrame *frame, long long _frameno,
88  int *pwidth, int *pheight)
89 {
90 #ifdef PGM_CONVERT_GREYSCALE
91  struct timeval start, end, elapsed;
92 #endif /* PGM_CONVERT_GREYSCALE */
93 
94  if (frameno == _frameno)
95  goto out;
96 
97  if (!frame->buf)
98  {
99  LOG(VB_COMMFLAG, LOG_ERR, "PGMConverter::getImage no buf");
100  goto error;
101  }
102 
103 #ifdef PGM_CONVERT_GREYSCALE
104  (void)gettimeofday(&start, nullptr);
105  if (m_copy->Copy(&pgm, frame, pgm.data[0], AV_PIX_FMT_GRAY8) < 0)
106  goto error;
107  (void)gettimeofday(&end, nullptr);
108  timersub(&end, &start, &elapsed);
109  timeradd(&convert_time, &elapsed, &convert_time);
110 #else /* !PGM_CONVERT_GREYSCALE */
111  if (av_image_fill_arrays(pgm.data, pgm.linesize,
112  frame->buf, AV_PIX_FMT_GRAY8, width, height,IMAGE_ALIGN) < 0)
113  {
114  LOG(VB_COMMFLAG, LOG_ERR,
115  QString("PGMConverter::getImage error at frame %1 (%2x%3)")
116  .arg(_frameno).arg(width).arg(height));
117  goto error;
118  }
119 #endif /* !PGM_CONVERT_GREYSCALE */
120 
121  frameno = _frameno;
122 
123 out:
124  *pwidth = width;
125  *pheight = height;
126  return &pgm;
127 
128 error:
129  return nullptr;
130 }
131 
132 int
134 {
135 #ifdef PGM_CONVERT_GREYSCALE
136  if (!time_reported)
137  {
138  LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
139  .arg(strftimeval(&convert_time)));
140  time_reported = true;
141  }
142 #endif /* PGM_CONVERT_GREYSCALE */
143  return 0;
144 }
145 
146 /* vim: set expandtab tabstop=4 shiftwidth=4: */
#define PGM_CONVERT_GREYSCALE
Definition: PGMConverter.h:28
int Copy(VideoFrame *dst, const VideoFrame *src)
Definition: mythavutil.cpp:196
static void error(const char *str,...)
Definition: vbi.c:41
long long frameno
Definition: PGMConverter.h:43
#define timeradd(a, b, result)
Definition: compat.h:293
struct AVFrame AVFrame
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:114
~PGMConverter(void)
#define IMAGE_ALIGN
Definition: mythconfig.h:19
struct timeval convert_time
Definition: PGMConverter.h:47
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
int MythPlayerInited(const MythPlayer *player)
PGMConverter(void)
AVFrame pgm
Definition: PGMConverter.h:45
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QSize GetVideoBufferSize(void) const
Definition: mythplayer.h:173
QString strftimeval(const struct timeval *tv)
MythAVCopy * m_copy
Definition: PGMConverter.h:49
#define timersub(a, b, result)
Definition: compat.h:303
bool time_reported
Definition: PGMConverter.h:48
unsigned char * buf
Definition: mythframe.h:39
const AVFrame * getImage(const VideoFrame *frame, long long frameno, int *pwidth, int *pheight)
int reportTime(void)