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