MythTV  master
mpeg2fix.h
Go to the documentation of this file.
1 // POSIX
2 #include <pthread.h>
3 
4 // C
5 #include <cstdlib>
6 
7 #include "mythconfig.h"
8 
9 extern "C"
10 {
11 //AVFormat/AVCodec
12 #include "libavcodec/avcodec.h"
13 #include "libavformat/avformat.h"
14 
15 //replex
18 
19 //libmpeg2
20 #include "config.h"
21 #if CONFIG_LIBMPEG2EXTERNAL
22 #include <mpeg2dec/mpeg2.h>
23 #else
24 #include "mpeg2.h"
25 #endif
26 }
27 
28 //Qt
29 #include <QMap>
30 #include <QList>
31 #include <QQueue>
32 #include <QStringList>
33 #include <QDateTime>
34 
35 // MythTV
36 #include "transcodedefs.h"
37 #include "programtypes.h"
38 #include "mythavutil.h"
39 
43 };
44 
46 {
47  public:
48  explicit MPEG2frame(int size);
49  ~MPEG2frame();
50  void ensure_size(int size);
51  void set_pkt(AVPacket *newpkt);
52 
53  AVPacket pkt;
54  bool isSequence;
55  bool isGop;
56  uint8_t *framePos;
57  uint8_t *gopPos;
58  mpeg2_sequence_t mpeg2_seq;
59  mpeg2_gop_t mpeg2_gop;
60  mpeg2_picture_t mpeg2_pic;
61 };
62 
63 typedef struct {
64  int64_t newPTS;
65  int64_t pos_pts;
66  int framenum;
67  bool type;
68 } poq_idx_t;
69 
71 {
72  public:
73  PTSOffsetQueue(int vidid, QList<int> keys, int64_t initPTS);
74  void SetNextPTS(int64_t newPTS, int64_t atPTS);
75  void SetNextPos(int64_t newPTS, AVPacket *pkt);
76  int64_t Get(int idx, AVPacket *pkt);
77  int64_t UpdateOrigPTS(int idx, int64_t &origPTS, AVPacket *pkt);
78  private:
79  QMap<int, QList<poq_idx_t> > offset;
80  QMap<int, QList<poq_idx_t> > orig;
81  QList<int> keyList;
82  int vid_id;
83 };
84 
85 //container for all multiplex related variables
87 {
88  public:
89  MPEG2replex();
90  ~MPEG2replex();
91  void Start();
92  int WaitBuffers();
93  int done;
94  QString outfile;
95  int otype;
103 
104  pthread_mutex_t mutex;
105  pthread_cond_t cond;
108 
109  private:
111 };
112 
113 typedef QList<MPEG2frame *> FrameList;
114 typedef QQueue<MPEG2frame *> FrameQueue;
115 typedef QMap<int, FrameList *> FrameMap;
116 
118 {
119  public:
120  MPEG2fixup(const QString &inf, const QString &outf,
121  frm_dir_map_t *deleteMap, const char *fmt, int norp,
122  int fixPTS, int maxf, bool showprog, int otype,
123  void (*update_func)(float) = nullptr, int (*check_func)() = nullptr);
124  ~MPEG2fixup();
125  int Start();
126  void AddRangeList(QStringList cutlist, int type);
127  void ShowRangeMap(frm_dir_map_t *mapPtr, QString msg);
128  int BuildKeyframeIndex(QString &file, frm_pos_map_t &posMap, frm_pos_map_t &durMap);
129 
130  void SetAllAudio(bool keep) { allaudio = keep; }
131 
132  static void dec2x33(int64_t *pts1, int64_t pts2);
133  static void inc2x33(int64_t *pts1, int64_t pts2);
134  static int64_t udiff2x33(int64_t pts1, int64_t pts2);
135  static int64_t diff2x33(int64_t pts1, int64_t pts2);
136  static int64_t add2x33(int64_t pts1, int64_t pts2);
137  static int cmp2x33(int64_t pts1, int64_t pts2);
138 
139  protected:
140  static void *ReplexStart(void *data);
142 
143  private:
144  int FindMPEG2Header(uint8_t *buf, int size, uint8_t code);
145  void InitReplex();
146  void FrameInfo(MPEG2frame *f);
147  int AddFrame(MPEG2frame *f);
148  bool InitAV(QString inputfile, const char *type, int64_t offset);
149  void ScanAudio();
150  int ProcessVideo(MPEG2frame *vf, mpeg2dec_t *dec);
151  void WriteFrame(QString filename, MPEG2frame *f);
152  void WriteFrame(QString filename, AVPacket *pkt);
153  void WriteYUV(QString filename, const mpeg2_info_t *info);
154  void WriteData(QString filename, uint8_t *data, int size);
155  bool BuildFrame(AVPacket *pkt, QString fname);
156  MPEG2frame *GetPoolFrame(AVPacket *pkt);
158  int GetFrame(AVPacket *pkt);
159  bool FindStart();
160  void SetRepeat(MPEG2frame *vf, int nb_fields, bool topff);
161  void SetRepeat(uint8_t *ptr, int size, int fields, bool topff);
162  MPEG2frame *FindFrameNum(int frameNum);
163  void RenumberFrames(int frame_pos, int delta);
164  void StoreSecondary();
165  int PlaybackSecondary();
166  MPEG2frame *DecodeToFrame(int frameNum, int skip_reset);
167  int ConvertToI(FrameList *orderedFrames, int headPos);
168  int InsertFrame(int frameNum, int64_t deltaPTS,
169  int64_t ptsIncrement, int64_t initPTS);
170  void AddSequence(MPEG2frame *frame1, MPEG2frame *frame2);
171  FrameList ReorderDTStoPTS(FrameList *dtsOrder, int pos);
172  void InitialPTSFixup(MPEG2frame *curFrame, int64_t &origvPTS,
173  int64_t &PTSdiscrep, int numframes, bool fix);
174  void SetFrameNum(uint8_t *ptr, int num);
175  static int GetFrameNum(const MPEG2frame *frame)
176  {
177  return frame->mpeg2_pic.temporal_reference;
178  }
179  static int GetFrameTypeN(const MPEG2frame *frame)
180  {
181  return frame->mpeg2_pic.flags & PIC_MASK_CODING_TYPE;
182  }
183  static char GetFrameTypeT(const MPEG2frame *frame)
184  {
185  int type = GetFrameTypeN(frame);
186  return (type == 1 ? 'I' :
187  (type == 2 ? 'P' : (type == 3 ? 'B' : 'X')));
188  }
189  static int GetNbFields(const MPEG2frame *frame)
190  {
191  return frame->mpeg2_pic.nb_fields;
192  }
193  int GetStreamType(int id) const
194  {
195  return (inputFC->streams[id]->codecpar->codec_id == AV_CODEC_ID_AC3) ?
196  AV_CODEC_ID_AC3 : AV_CODEC_ID_MP2;
197  }
198  AVCodecContext *getCodecContext(uint id)
199  {
200  if (id >= inputFC->nb_streams)
201  return nullptr;
202  return gCodecMap->getCodecContext(inputFC->streams[id]);
203  }
204  AVCodecParserContext *getCodecParserContext(uint id)
205  {
206  if (id >= inputFC->nb_streams)
207  return nullptr;
208  return inputFC->streams[id]->parser;
209  }
210 
211  void dumpList(FrameList *list);
212 
213  int (*check_abort)();
214  void (*update_status)(float percent_done);
215 
218 
224  mpeg2dec_t *header_decoder;
225  mpeg2dec_t *img_decoder;
226 
229 
230  pthread_t thread;
231 
232  AVFormatContext *inputFC;
234 
235  int vid_id;
237  QMap <int, int> aud_map;
239  int64_t ptsIncrement;
240  int64_t ptsOffset; //was initPTS
241  bool mkvfile;
242 
243  int discard;
244  //control options
246  QString infile;
247  const char *format;
248  bool allaudio;
249 
250  //complete?
251  bool file_end;
253 
254  //progress indicators
255  QDateTime statustime;
257  uint64_t filesize;
258  int framenum;
263 };
264 
265 #ifdef NO_MYTH
266  #include <QDateTime>
267  #include <iostream>
268 
269  using namespace std;
270 
271  extern int verboseMask;
272  #undef LOG
273  #define LOG(mask,level,args...) \
274  do { \
275  if ((verboseMask & mask) != 0) \
276  { \
277  cout << args << endl; \
278  } \
279  } while (0)
280 
281  // Be sure to keep these the same as in libmythbase/exitcodes.h or expect
282  // odd behavior eventually
283  #define GENERIC_EXIT_OK 0
284  #define GENERIC_EXIT_NOT_OK 128
285  #define GENERIC_EXIT_WRITE_FRAME_ERROR 149
286  #define GENERIC_EXIT_DEADLOCK 150
287 #else
288  #include "exitcodes.h"
289  #include "mythcontext.h"
290 #endif
291 
292 /*
293  * vim:ts=4:sw=4:ai:et:si:sts=4
294  */
FrameQueue unreadFrames
Definition: mpeg2fix.h:222
int PlaybackSecondary()
Definition: mpeg2fix.cpp:1710
int64_t ptsOffset
Definition: mpeg2fix.h:240
frm_dir_map_t delMap
Definition: mpeg2fix.h:227
MPEG2frame * DecodeToFrame(int frameNum, int skip_reset)
Definition: mpeg2fix.cpp:1724
int fix_PTS
Definition: mpeg2fix.h:245
void set_pkt(AVPacket *newpkt)
Definition: mpeg2fix.cpp:129
void RenumberFrames(int frame_pos, int delta)
Definition: mpeg2fix.cpp:1682
uint8_t * gopPos
Definition: mpeg2fix.h:57
static int64_t add2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:439
int ext_count
Definition: mpeg2fix.h:236
void WriteData(QString filename, uint8_t *data, int size)
Definition: mpeg2fix.cpp:1151
mpeg2dec_t * header_decoder
Definition: mpeg2fix.h:224
uint64_t filesize
Definition: mpeg2fix.h:257
void WriteYUV(QString filename, const mpeg2_info_t *info)
Definition: mpeg2fix.cpp:1112
void SetRepeat(MPEG2frame *vf, int nb_fields, bool topff)
Definition: mpeg2fix.cpp:1634
FrameList vFrame
Definition: mpeg2fix.h:219
AVPacket pkt
Definition: mpeg2fix.h:53
pthread_cond_t cond
Definition: mpeg2fix.h:105
MPEG2frame(int size)
Definition: mpeg2fix.cpp:102
pthread_mutex_t mutex
Definition: mpeg2fix.h:104
void ShowRangeMap(frm_dir_map_t *mapPtr, QString msg)
Definition: mpeg2fix.cpp:1968
void AddSequence(MPEG2frame *frame1, MPEG2frame *frame2)
Definition: mpeg2fix.cpp:918
QList< int > keyList
Definition: mpeg2fix.h:81
int BuildKeyframeIndex(QString &file, frm_pos_map_t &posMap, frm_pos_map_t &durMap)
Definition: mpeg2fix.cpp:2800
struct AVFrame AVFrame
int64_t pos_pts
Definition: mpeg2fix.h:65
void SetNextPos(int64_t newPTS, AVPacket *pkt)
Definition: mpeg2fix.cpp:194
pthread_t thread
Definition: mpeg2fix.h:230
multiplex_t * mplex
Definition: mpeg2fix.h:110
uint16_t inv_zigzag_direct16[64]
Definition: mpeg2fix.h:261
ringbuffer index_extrbuf[N_AUDIO]
Definition: mpeg2fix.h:99
int(* check_abort)()
Definition: mpeg2fix.h:213
static int64_t udiff2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:401
ringbuffer vrbuf
Definition: mpeg2fix.h:96
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
unsigned int uint
Definition: compat.h:140
int Start()
Definition: mpeg2fix.cpp:2063
uint64_t verboseMask
Definition: logging.cpp:107
bool isGop
Definition: mpeg2fix.h:55
int discard
Definition: mpeg2fix.h:243
ringbuffer index_vrbuf
Definition: mpeg2fix.h:98
int64_t newPTS
Definition: mpeg2fix.h:64
void InitReplex()
Definition: mpeg2fix.cpp:618
bool use_secondary
Definition: mpeg2fix.h:217
int64_t Get(int idx, AVPacket *pkt)
Definition: mpeg2fix.cpp:153
QMap< int, QList< poq_idx_t > > orig
Definition: mpeg2fix.h:80
AVFormatContext * inputFC
Definition: mpeg2fix.h:232
int AddFrame(MPEG2frame *f)
Definition: mpeg2fix.cpp:702
void dumpList(FrameList *list)
Definition: mpeg2fix.cpp:2041
QMap< int, int > aud_map
Definition: mpeg2fix.h:237
int exttype[N_AUDIO]
Definition: mpeg2fix.h:101
static void * ReplexStart(void *data)
Definition: mpeg2fix.cpp:560
static int GetNbFields(const MPEG2frame *frame)
Definition: mpeg2fix.h:189
QMap< int, QList< poq_idx_t > > offset
Definition: mpeg2fix.h:79
int framenum
Definition: mpeg2fix.h:258
static char GetFrameTypeT(const MPEG2frame *frame)
Definition: mpeg2fix.h:183
int vid_id
Definition: mpeg2fix.h:235
MythCodecMap * gCodecMap
This global variable contains the MythCodecMap instance for the app.
Definition: mythavutil.cpp:377
ringbuffer extrbuf[N_AUDIO]
Definition: mpeg2fix.h:97
FrameMap aFrame
Definition: mpeg2fix.h:220
bool allaudio
Definition: mpeg2fix.h:248
void SetAllAudio(bool keep)
Definition: mpeg2fix.h:130
int WaitBuffers()
Definition: mpeg2fix.cpp:529
bool real_file_end
Definition: mpeg2fix.h:252
bool FindStart()
Definition: mpeg2fix.cpp:1503
QMap< int, FrameList * > FrameMap
Definition: mpeg2fix.h:115
unsigned short uint16_t
Definition: iso6937tables.h:1
int InsertFrame(int frameNum, int64_t deltaPTS, int64_t ptsIncrement, int64_t initPTS)
Definition: mpeg2fix.cpp:1858
int64_t ptsIncrement
Definition: mpeg2fix.h:239
const char * format
Definition: mpeg2fix.h:247
AVFrame * picture
Definition: mpeg2fix.h:233
PTSOffsetQueue(int vidid, QList< int > keys, int64_t initPTS)
Definition: mpeg2fix.cpp:136
int no_repeat
Definition: mpeg2fix.h:245
bool zigzag_init
Definition: mpeg2fix.h:262
int framenum
Definition: mpeg2fix.h:66
int FindMPEG2Header(uint8_t *buf, int size, uint8_t code)
Definition: mpeg2fix.cpp:470
AVCodecContext * getCodecContext(const AVStream *, const AVCodec *pCodec=nullptr, bool nullCodec=false)
Definition: mythavutil.cpp:388
int ext_count
Definition: mpeg2fix.h:100
bool mkvfile
Definition: mpeg2fix.h:241
int exttypcnt[N_AUDIO]
Definition: mpeg2fix.h:102
void Start()
Definition: mpeg2fix.cpp:571
void ensure_size(int size)
Definition: mpeg2fix.cpp:115
void InitialPTSFixup(MPEG2frame *curFrame, int64_t &origvPTS, int64_t &PTSdiscrep, int numframes, bool fix)
Definition: mpeg2fix.cpp:2002
QQueue< MPEG2frame * > FrameQueue
Definition: mpeg2fix.h:114
mpeg2_picture_t mpeg2_pic
Definition: mpeg2fix.h:60
QString outfile
Definition: mpeg2fix.h:94
void SetNextPTS(int64_t newPTS, int64_t atPTS)
Definition: mpeg2fix.cpp:180
int aud_stream_count
Definition: mpeg2fix.h:238
int64_t UpdateOrigPTS(int idx, int64_t &origPTS, AVPacket *pkt)
Definition: mpeg2fix.cpp:216
void FrameInfo(MPEG2frame *f)
Definition: mpeg2fix.cpp:686
void(* update_status)(float percent_done)
Definition: mpeg2fix.h:214
mpeg2_gop_t mpeg2_gop
Definition: mpeg2fix.h:59
AVCodecParserContext * getCodecParserContext(uint id)
Definition: mpeg2fix.h:204
int maxframes
Definition: mpeg2fix.h:245
mpeg2dec_t * img_decoder
Definition: mpeg2fix.h:225
FrameList ReorderDTStoPTS(FrameList *dtsOrder, int pos)
Definition: mpeg2fix.cpp:1985
MPEG2replex rx
Definition: mpeg2fix.h:141
int displayFrame
Definition: mpeg2fix.h:223
AVCodecContext * getCodecContext(uint id)
Definition: mpeg2fix.h:198
static int64_t diff2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:414
QList< MPEG2frame * > FrameList
Definition: mpeg2fix.h:113
sequence_t seq_head
Definition: mpeg2fix.h:107
bool file_end
Definition: mpeg2fix.h:251
FrameQueue framePool
Definition: mpeg2fix.h:221
static void inc2x33(int64_t *pts1, int64_t pts2)
Definition: mpeg2fix.cpp:396
MPEG2frame * FindFrameNum(int frameNum)
Definition: mpeg2fix.cpp:1671
audio_frame_t extframe[N_AUDIO]
Definition: mpeg2fix.h:106
MPFListType
Definition: mpeg2fix.h:40
int done
Definition: mpeg2fix.h:93
FrameList vSecondary
Definition: mpeg2fix.h:216
int GetFrame(AVPacket *pkt)
Definition: mpeg2fix.cpp:1384
int GetStreamType(int id) const
Definition: mpeg2fix.h:193
void WriteFrame(QString filename, MPEG2frame *f)
Definition: mpeg2fix.cpp:1066
int otype
Definition: mpeg2fix.h:95
bool BuildFrame(AVPacket *pkt, QString fname)
Definition: mpeg2fix.cpp:1169
frm_dir_map_t saveMap
Definition: mpeg2fix.h:228
static void dec2x33(int64_t *pts1, int64_t pts2)
Definition: mpeg2fix.cpp:391
static int GetFrameTypeN(const MPEG2frame *frame)
Definition: mpeg2fix.h:179
MPEG2frame * GetPoolFrame(AVPacket *pkt)
Definition: mpeg2fix.cpp:1347
int ConvertToI(FrameList *orderedFrames, int headPos)
Definition: mpeg2fix.cpp:1801
uint8_t * framePos
Definition: mpeg2fix.h:56
static int cmp2x33(int64_t pts1, int64_t pts2)
Definition: mpeg2fix.cpp:447
MPEG2fixup(const QString &inf, const QString &outf, frm_dir_map_t *deleteMap, const char *fmt, int norp, int fixPTS, int maxf, bool showprog, int otype, void(*update_func)(float)=nullptr, int(*check_func)()=nullptr)
Definition: mpeg2fix.cpp:237
QDateTime statustime
Definition: mpeg2fix.h:255
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
#define N_AUDIO
Definition: multiplex.h:33
bool type
Definition: mpeg2fix.h:67
void SetFrameNum(uint8_t *ptr, int num)
Definition: mpeg2fix.cpp:913
mpeg2_sequence_t mpeg2_seq
Definition: mpeg2fix.h:58
bool InitAV(QString inputfile, const char *type, int64_t offset)
Definition: mpeg2fix.cpp:806
bool isSequence
Definition: mpeg2fix.h:54
int ProcessVideo(MPEG2frame *vf, mpeg2dec_t *dec)
Definition: mpeg2fix.cpp:940
static int GetFrameNum(const MPEG2frame *frame)
Definition: mpeg2fix.h:175
bool showprogress
Definition: mpeg2fix.h:256
int status_update_time
Definition: mpeg2fix.h:259
void AddRangeList(QStringList cutlist, int type)
Definition: mpeg2fix.cpp:1924
QString infile
Definition: mpeg2fix.h:246
uint64_t last_written_pos
Definition: mpeg2fix.h:260
void ScanAudio()
void StoreSecondary()
Definition: mpeg2fix.cpp:1694