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