MythTV master
mpeg2fix.h
Go to the documentation of this file.
1// POSIX
2#include <pthread.h>
3
4// C++
5#include <cstdlib>
6
7extern "C"
8{
9//AVFormat/AVCodec
10#include "libavcodec/avcodec.h"
11#include "libavformat/avformat.h"
12
13//libmpeg2
14#if CONFIG_LIBMPEG2EXTERNAL
15#include <mpeg2dec/mpeg2.h>
16#else
17#include "libmythmpeg2/mpeg2.h"
18#endif
19}
20
21//replex
24
25// Qt
26#include <QMap>
27#include <QList>
28#include <QQueue>
29#include <QStringList>
30#include <QDateTime>
31
32// MythTV
35
36// MythTranscode
37#include "transcodedefs.h"
38
39enum MPFListType : std::uint8_t {
42};
43
45{
46 public:
47 explicit MPEG2frame(int size);
49 void ensure_size(int size) const;
50 void set_pkt(AVPacket *newpkt) const;
51
52 AVPacket *m_pkt {nullptr};
53 bool m_isSequence {false};
54 bool m_isGop {false};
55 uint8_t *m_framePos {nullptr};
56 uint8_t *m_gopPos {nullptr};
57 mpeg2_sequence_t m_mpeg2_seq;
58 mpeg2_gop_t m_mpeg2_gop;
59 mpeg2_picture_t m_mpeg2_pic;
60};
61
62struct poq_idx_t {
63 int64_t newPTS;
64 int64_t pos_pts;
66 bool type;
67};
68
70{
71 public:
72 PTSOffsetQueue(int vidid, QList<int> keys, int64_t initPTS);
73 void SetNextPTS(int64_t newPTS, int64_t atPTS);
74 void SetNextPos(int64_t newPTS, AVPacket *pkt);
75 int64_t Get(int idx, AVPacket *pkt);
76 int64_t UpdateOrigPTS(int idx, int64_t &origPTS, AVPacket *pkt);
77 private:
78 QMap<int, QList<poq_idx_t> > m_offset;
79 QMap<int, QList<poq_idx_t> > m_orig;
80 QList<int> m_keyList;
82};
83
84//container for all multiplex related variables
85using RingbufferArray = std::array<ringbuffer,N_AUDIO>;
86using ExtTypeIntArray = std::array<int,N_AUDIO>;
87using AudioFrameArray = std::array<audio_frame_t,N_AUDIO>;
88
90{
91 public:
92 MPEG2replex() = default;
94 void Start();
95 int WaitBuffers();
96 int m_done {0};
97 QString m_outfile;
98 int m_otype {0};
103 int m_extCount {0};
106
107 pthread_mutex_t m_mutex {};
108 pthread_cond_t m_cond {};
111
112 private:
114};
115
116using FrameList = QList<MPEG2frame *>;
117using FrameQueue = QQueue<MPEG2frame *>;
118using FrameMap = QMap<int, FrameList *>;
119
121{
122 public:
123 MPEG2fixup(const QString &inf, const QString &outf,
124 frm_dir_map_t *deleteMap, const char *fmt, bool norp,
125 bool fixPTS, int maxf, bool showprog, int otype,
126 void (*update_func)(float) = nullptr, int (*check_func)() = nullptr);
127 ~MPEG2fixup();
128 int Start();
129 void AddRangeList(const QStringList& rangelist, int type);
130 static void ShowRangeMap(frm_dir_map_t *mapPtr, QString msg);
131 int BuildKeyframeIndex(const QString &file, frm_pos_map_t &posMap, frm_pos_map_t &durMap);
132
133 void SetAllAudio(bool keep) { m_allAudio = keep; }
134
135 static void dec2x33(int64_t *pts1, int64_t pts2);
136 static void inc2x33(int64_t *pts1, int64_t pts2);
137 static int64_t udiff2x33(int64_t pts1, int64_t pts2);
138 static int64_t diff2x33(int64_t pts1, int64_t pts2);
139 static int64_t add2x33(int64_t pts1, int64_t pts2);
140 static int cmp2x33(int64_t pts1, int64_t pts2);
141
142 protected:
143 static void *ReplexStart(void *data);
145
146 private:
147 static int FindMPEG2Header(const uint8_t *buf, int size, uint8_t code);
148 void InitReplex();
149 void FrameInfo(MPEG2frame *f);
150 int AddFrame(MPEG2frame *f);
151 bool InitAV(const QString& inputfile, const char *type, int64_t offset);
152 void ScanAudio();
153 int ProcessVideo(MPEG2frame *vf, mpeg2dec_t *dec);
154 void WriteFrame(const QString& filename, MPEG2frame *f);
155 void WriteFrame(const QString& filename, AVPacket *pkt);
156 static void WriteYUV(const QString& filename, const mpeg2_info_t *info);
157 static void WriteData(const QString& filename, uint8_t *data, int size);
158 bool BuildFrame(AVPacket *pkt, const QString& fname);
159 MPEG2frame *GetPoolFrame(AVPacket *pkt);
161 int GetFrame(AVPacket *pkt);
162 bool FindStart();
163 static void SetRepeat(MPEG2frame *vf, int nb_fields, bool topff);
164 static void SetRepeat(uint8_t *ptr, int size, int fields, bool topff);
165 MPEG2frame *FindFrameNum(int frameNum);
166 void RenumberFrames(int start_pos, int delta);
167 void StoreSecondary();
168 int PlaybackSecondary();
169 MPEG2frame *DecodeToFrame(int frameNum, int skip_reset);
170 int ConvertToI(FrameList *orderedFrames, int headPos);
171 int InsertFrame(int frameNum, int64_t deltaPTS,
172 int64_t ptsIncrement, int64_t initPTS);
173 void AddSequence(MPEG2frame *frame1, MPEG2frame *frame2);
174 static FrameList ReorderDTStoPTS(FrameList *dtsOrder, int pos);
175 void InitialPTSFixup(MPEG2frame *curFrame, int64_t &origvPTS,
176 int64_t &PTSdiscrep, int numframes, bool fix) const;
177 static void SetFrameNum(uint8_t *ptr, int num);
178 static int GetFrameNum(const MPEG2frame *frame)
179 {
180 return frame->m_mpeg2_pic.temporal_reference;
181 }
182 static int GetFrameTypeN(const MPEG2frame *frame)
183 {
184 return frame->m_mpeg2_pic.flags & PIC_MASK_CODING_TYPE;
185 }
186 static char GetFrameTypeT(const MPEG2frame *frame)
187 {
188 int type = GetFrameTypeN(frame);
189 if (type == 1)
190 return 'I';
191 if (type == 2)
192 return 'P';
193 if (type == 3)
194 return 'B';
195 return 'X';
196 }
197 static int GetNbFields(const MPEG2frame *frame)
198 {
199 return frame->m_mpeg2_pic.nb_fields;
200 }
201 int GetStreamType(int id) const
202 {
203 return (m_inputFC->streams[id]->codecpar->codec_id == AV_CODEC_ID_AC3) ?
204 AV_CODEC_ID_AC3 : AV_CODEC_ID_MP2;
205 }
206 AVCodecContext *getCodecContext(uint id)
207 {
208 if (id >= m_inputFC->nb_streams)
209 return nullptr;
210 return m_codecMap.GetCodecContext(m_inputFC->streams[id]);
211 }
212 AVCodecParserContext *getCodecParserContext(uint id)
213 {
214 if (id >= m_inputFC->nb_streams)
215 return nullptr;
216 return av_stream_get_parser(m_inputFC->streams[id]);
217 }
218
219 static void dumpList(FrameList *list);
220
221 int (*m_checkAbort)() {nullptr};
222 void (*m_updateStatus)(float percent_done) {nullptr};
223
225 bool m_useSecondary {false};
226
232 mpeg2dec_t *m_headerDecoder {nullptr};
233 mpeg2dec_t *m_imgDecoder {nullptr};
234
237
238 pthread_t m_thread {};
239
241 AVFormatContext *m_inputFC {nullptr};
242 AVFrame *m_picture {nullptr};
243
244 int m_vidId {-1};
245 int m_extCount {0};
246 QMap <int, int> m_audMap;
247 int64_t m_ptsIncrement {0};
248 bool m_mkvFile {false};
249
250 bool m_discard {false};
251 //control options
255 QString m_infile;
256 const char *m_format {nullptr};
257 bool m_allAudio {false};
258
259 //complete?
260 bool m_fileEnd {false};
261 bool m_realFileEnd {false};
262
263 //progress indicators
264 QDateTime m_statusTime;
265 bool m_showProgress {false};
266 uint64_t m_fileSize {0};
267 int m_frameNum {0};
269 uint64_t m_lastWrittenPos {0};
270};
271
272#ifdef NO_MYTH
273 #include <QDateTime>
274 #include <iostream>
275
276 extern int verboseMask;
277 #undef LOG
278 #define LOG(mask,level,args...) \
279 do { \
280 if ((verboseMask & mask) != 0) \
281 { \
282 cout << args << endl; \
283 } \
284 } while (0)
285
286 // Be sure to keep these the same as in libmythbase/exitcodes.h or expect
287 // odd behavior eventually
288 #define GENERIC_EXIT_OK 0
289 #define GENERIC_EXIT_NOT_OK 128
290 #define GENERIC_EXIT_WRITE_FRAME_ERROR 149
291 #define GENERIC_EXIT_DEADLOCK 150
292#else
293 #include "libmythbase/exitcodes.h"
294#endif
295
296/*
297 * vim:ts=4:sw=4:ai:et:si:sts=4
298 */
AVFrame AVFrame
bool InitAV(const QString &inputfile, const char *type, int64_t offset)
Definition: mpeg2fix.cpp:773
bool BuildFrame(AVPacket *pkt, const QString &fname)
Definition: mpeg2fix.cpp:1145
MPEG2frame * DecodeToFrame(int frameNum, int skip_reset)
Definition: mpeg2fix.cpp:1742
QString m_infile
Definition: mpeg2fix.h:255
static int GetFrameNum(const MPEG2frame *frame)
Definition: mpeg2fix.h:178
void AddSequence(MPEG2frame *frame1, MPEG2frame *frame2)
Definition: mpeg2fix.cpp:886
bool m_fileEnd
Definition: mpeg2fix.h:260
QMap< int, int > m_audMap
Definition: mpeg2fix.h:246
void RenumberFrames(int start_pos, int delta)
Definition: mpeg2fix.cpp:1701
bool m_allAudio
Definition: mpeg2fix.h:257
static void SetRepeat(MPEG2frame *vf, int nb_fields, bool topff)
Definition: mpeg2fix.cpp:1653
MPEG2replex m_rx
Definition: mpeg2fix.h:144
static void WriteYUV(const QString &filename, const mpeg2_info_t *info)
Definition: mpeg2fix.cpp:1083
static int64_t diff2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:378
QDateTime m_statusTime
Definition: mpeg2fix.h:264
pthread_t m_thread
Definition: mpeg2fix.h:238
void WriteFrame(const QString &filename, MPEG2frame *f)
Definition: mpeg2fix.cpp:1037
bool m_discard
Definition: mpeg2fix.h:250
static int GetNbFields(const MPEG2frame *frame)
Definition: mpeg2fix.h:197
void ScanAudio()
static FrameList ReorderDTStoPTS(FrameList *dtsOrder, int pos)
Definition: mpeg2fix.cpp:2032
static char GetFrameTypeT(const MPEG2frame *frame)
Definition: mpeg2fix.h:186
uint64_t m_lastWrittenPos
Definition: mpeg2fix.h:269
void InitialPTSFixup(MPEG2frame *curFrame, int64_t &origvPTS, int64_t &PTSdiscrep, int numframes, bool fix) const
Definition: mpeg2fix.cpp:2049
int Start()
Definition: mpeg2fix.cpp:2108
MythCodecMap m_codecMap
Definition: mpeg2fix.h:240
static int FindMPEG2Header(const uint8_t *buf, int size, uint8_t code)
Definition: mpeg2fix.cpp:436
bool m_mkvFile
Definition: mpeg2fix.h:248
FrameQueue m_framePool
Definition: mpeg2fix.h:229
static void WriteData(const QString &filename, uint8_t *data, int size)
Definition: mpeg2fix.cpp:1127
static int GetFrameTypeN(const MPEG2frame *frame)
Definition: mpeg2fix.h:182
AVCodecParserContext * getCodecParserContext(uint id)
Definition: mpeg2fix.h:212
int m_vidId
Definition: mpeg2fix.h:244
int InsertFrame(int frameNum, int64_t deltaPTS, int64_t ptsIncrement, int64_t initPTS)
Definition: mpeg2fix.cpp:1889
AVCodecContext * getCodecContext(uint id)
Definition: mpeg2fix.h:206
static void inc2x33(int64_t *pts1, int64_t pts2)
Definition: mpeg2fix.cpp:362
int AddFrame(MPEG2frame *f)
Definition: mpeg2fix.cpp:666
static void * ReplexStart(void *data)
Definition: mpeg2fix.cpp:516
MPEG2frame * FindFrameNum(int frameNum)
Definition: mpeg2fix.cpp:1690
FrameList m_vSecondary
Definition: mpeg2fix.h:224
static int64_t add2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:403
static int64_t udiff2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:367
bool m_noRepeat
Definition: mpeg2fix.h:252
int m_extCount
Definition: mpeg2fix.h:245
mpeg2dec_t * m_headerDecoder
Definition: mpeg2fix.h:232
bool FindStart()
Definition: mpeg2fix.cpp:1520
static int cmp2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:411
MPEG2frame * GetPoolFrame(AVPacket *pkt)
Definition: mpeg2fix.cpp:1364
int ProcessVideo(MPEG2frame *vf, mpeg2dec_t *dec)
Definition: mpeg2fix.cpp:908
uint64_t m_fileSize
Definition: mpeg2fix.h:266
FrameList m_vFrame
Definition: mpeg2fix.h:227
void(* m_updateStatus)(float percent_done)
Definition: mpeg2fix.h:222
static void ShowRangeMap(frm_dir_map_t *mapPtr, QString msg)
Definition: mpeg2fix.cpp:2013
int BuildKeyframeIndex(const QString &file, frm_pos_map_t &posMap, frm_pos_map_t &durMap)
Definition: mpeg2fix.cpp:2897
int PlaybackSecondary()
Definition: mpeg2fix.cpp:1729
bool m_realFileEnd
Definition: mpeg2fix.h:261
int64_t m_ptsIncrement
Definition: mpeg2fix.h:247
void SetAllAudio(bool keep)
Definition: mpeg2fix.h:133
bool m_useSecondary
Definition: mpeg2fix.h:225
static void SetFrameNum(uint8_t *ptr, int num)
Definition: mpeg2fix.cpp:881
void FrameInfo(MPEG2frame *f)
Definition: mpeg2fix.cpp:650
static void dumpList(FrameList *list)
Definition: mpeg2fix.cpp:2088
int m_frameNum
Definition: mpeg2fix.h:267
FrameQueue m_unreadFrames
Definition: mpeg2fix.h:230
int GetFrame(AVPacket *pkt)
Definition: mpeg2fix.cpp:1403
frm_dir_map_t m_saveMap
Definition: mpeg2fix.h:236
int GetStreamType(int id) const
Definition: mpeg2fix.h:201
frm_dir_map_t m_delMap
Definition: mpeg2fix.h:235
int m_displayFrame
Definition: mpeg2fix.h:231
int m_maxFrames
Definition: mpeg2fix.h:254
void AddRangeList(const QStringList &rangelist, int type)
Definition: mpeg2fix.cpp:1966
void InitReplex()
Definition: mpeg2fix.cpp:578
mpeg2dec_t * m_imgDecoder
Definition: mpeg2fix.h:233
MPEG2fixup(const QString &inf, const QString &outf, frm_dir_map_t *deleteMap, const char *fmt, bool norp, bool fixPTS, int maxf, bool showprog, int otype, void(*update_func)(float)=nullptr, int(*check_func)()=nullptr)
Definition: mpeg2fix.cpp:219
bool m_showProgress
Definition: mpeg2fix.h:265
static void dec2x33(int64_t *pts1, int64_t pts2)
Definition: mpeg2fix.cpp:357
int m_statusUpdateTime
Definition: mpeg2fix.h:268
int(* m_checkAbort)()
Definition: mpeg2fix.h:221
FrameMap m_aFrame
Definition: mpeg2fix.h:228
const char * m_format
Definition: mpeg2fix.h:256
AVFrame * m_picture
Definition: mpeg2fix.h:242
int ConvertToI(FrameList *orderedFrames, int headPos)
Definition: mpeg2fix.cpp:1819
AVFormatContext * m_inputFC
Definition: mpeg2fix.h:241
bool m_fixPts
Definition: mpeg2fix.h:253
void StoreSecondary()
Definition: mpeg2fix.cpp:1713
bool m_isGop
Definition: mpeg2fix.h:54
mpeg2_picture_t m_mpeg2_pic
Definition: mpeg2fix.h:59
MPEG2frame(int size)
Definition: mpeg2fix.cpp:82
AVPacket * m_pkt
Definition: mpeg2fix.h:52
void ensure_size(int size) const
Definition: mpeg2fix.cpp:94
bool m_isSequence
Definition: mpeg2fix.h:53
uint8_t * m_framePos
Definition: mpeg2fix.h:55
mpeg2_gop_t m_mpeg2_gop
Definition: mpeg2fix.h:58
void set_pkt(AVPacket *newpkt) const
Definition: mpeg2fix.cpp:110
mpeg2_sequence_t m_mpeg2_seq
Definition: mpeg2fix.h:57
uint8_t * m_gopPos
Definition: mpeg2fix.h:56
int WaitBuffers()
Definition: mpeg2fix.cpp:477
ExtTypeIntArray m_exttypcnt
Definition: mpeg2fix.h:105
ringbuffer m_vrBuf
Definition: mpeg2fix.h:99
pthread_mutex_t m_mutex
Definition: mpeg2fix.h:107
RingbufferArray m_indexExtrbuf
Definition: mpeg2fix.h:102
RingbufferArray m_extrbuf
Definition: mpeg2fix.h:100
multiplex_t * m_mplex
Definition: mpeg2fix.h:113
void Start()
Definition: mpeg2fix.cpp:527
AudioFrameArray m_extframe
Definition: mpeg2fix.h:109
ExtTypeIntArray m_exttype
Definition: mpeg2fix.h:104
sequence_t m_seq_head
Definition: mpeg2fix.h:110
pthread_cond_t m_cond
Definition: mpeg2fix.h:108
QString m_outfile
Definition: mpeg2fix.h:97
MPEG2replex()=default
ringbuffer m_indexVrbuf
Definition: mpeg2fix.h:101
int m_otype
Definition: mpeg2fix.h:98
int m_extCount
Definition: mpeg2fix.h:103
int m_done
Definition: mpeg2fix.h:96
AVCodecContext * GetCodecContext(const AVStream *Stream, const AVCodec *Codec=nullptr, bool NullCodec=false)
Definition: mythavutil.cpp:288
QList< int > m_keyList
Definition: mpeg2fix.h:80
void SetNextPos(int64_t newPTS, AVPacket *pkt)
Definition: mpeg2fix.cpp:176
void SetNextPTS(int64_t newPTS, int64_t atPTS)
Definition: mpeg2fix.cpp:163
int64_t Get(int idx, AVPacket *pkt)
Definition: mpeg2fix.cpp:133
int64_t UpdateOrigPTS(int idx, int64_t &origPTS, AVPacket *pkt)
Definition: mpeg2fix.cpp:198
QMap< int, QList< poq_idx_t > > m_offset
Definition: mpeg2fix.h:78
PTSOffsetQueue(int vidid, QList< int > keys, int64_t initPTS)
Definition: mpeg2fix.cpp:117
QMap< int, QList< poq_idx_t > > m_orig
Definition: mpeg2fix.h:79
unsigned int uint
Definition: freesurround.h:24
uint64_t verboseMask
Definition: logging.cpp:97
QList< MPEG2frame * > FrameList
Definition: mpeg2fix.h:116
QQueue< MPEG2frame * > FrameQueue
Definition: mpeg2fix.h:117
std::array< int, N_AUDIO > ExtTypeIntArray
Definition: mpeg2fix.h:86
MPFListType
Definition: mpeg2fix.h:39
@ MPF_TYPE_CUTLIST
Definition: mpeg2fix.h:40
@ MPF_TYPE_SAVELIST
Definition: mpeg2fix.h:41
QMap< int, FrameList * > FrameMap
Definition: mpeg2fix.h:118
std::array< audio_frame_t, N_AUDIO > AudioFrameArray
Definition: mpeg2fix.h:87
std::array< ringbuffer, N_AUDIO > RingbufferArray
Definition: mpeg2fix.h:85
dictionary info
Definition: azlyrics.py:7
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:117
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:44
int64_t newPTS
Definition: mpeg2fix.h:63
bool type
Definition: mpeg2fix.h:66
int64_t pos_pts
Definition: mpeg2fix.h:64
int framenum
Definition: mpeg2fix.h:65