1 /*************************************************************
2  *   mpgtx an mpeg toolbox                                   *
3  *   by Laurent Alacoque <laureck@users.sourceforge.net>     *
4  *   (c) 2001                                                *
5  *   You may copy, modify and redistribute this              *
6  *   source file under the terms of the GNU Public License   *
7  ************************************************************/
8 #ifndef __mpegOut_hh_
9 #define __mpegOut_hh_
10 
11 #include "common.hh"
12 #include "mpeg.hh"
13 // required by access()
14 // FIXME: Still needed, because we have mpgtx_access()?
15 #include <unistd.h>
16 
17 
18 class mpeg;
19 
20 
21 class mpegOut
22 {
23 public:
24 	mpegOut(char *filename);
25 	mpegOut(FILE *filehandle);
26 	virtual ~mpegOut();
OutFile()27 	const FILE* OutFile() {return MpegOut;}
WriteChunk(mpeg * Mpeg,off_t from,off_t to)28 	int WriteChunk(mpeg* Mpeg, off_t from, off_t to)
29 			{return WriteChunk(Mpeg, from, false, to, true);};
30 	virtual void WriteHeader(mpeg* Mpeg) = 0;
31 	virtual int WriteChunk (mpeg* Mpeg,
32 		off_t from, bool from_included,
33 		off_t to, bool to_included) = 0;
Finish()34 	virtual void Finish() {};
35 
36 protected:
mpegOut()37 	mpegOut() {};
38 	virtual void Copy(FILE* file, off_t from, off_t to) = 0;
39 	int FileType;
40 	bool HasAudio,HasVideo;
41 	FILE* MpegOut;
42 	byte* buffer;
43 };
44 
45 
46 class mpegOutWithVideo : public mpegOut
47 {
48 protected:
mpegOutWithVideo(char * filename)49 	mpegOutWithVideo(char* filename) :
50 		mpegOut(filename) {mpeg_version = 0;};
mpegOutWithVideo(FILE * filehandle)51 	mpegOutWithVideo(FILE* filehandle) :
52 		mpegOut(filehandle) {mpeg_version = 0;};
53 
mpegOutWithVideo()54 	mpegOutWithVideo(){};
55 	long CorrectTS(long bufferlength);
56 
57 	void memReadTS(long offset, double* ts, bool mpeg2pack = false, bool pack = false);
58 	int  memReadPktTS(long* off, double* pts, double* dts, long bufferlength);
59 	void memWriteTS(long offset, double ts, bool mpeg2pack = false, bool pack = false);
60 
61 	double currentTS;
62 	double ts_correction;
63 	bool first_TS_correction;
64 
65 	int mpeg_version;
66 };
67 
68 class mpegVideoOut : public mpegOutWithVideo
69 {
70 public:
mpegVideoOut(char * filename)71 	mpegVideoOut(char* filename) :
72 		mpegOutWithVideo(filename) {};
mpegVideoOut(FILE * filehandle)73 	mpegVideoOut(FILE* filehandle) :
74 		mpegOutWithVideo(filehandle) {};
75 
76 	void WriteHeader(mpeg* Mpeg);
77 	int WriteChunk (mpeg* Mpeg,
78 		off_t from, bool from_included,
79 		off_t to, bool to_included );
80 protected:
mpegVideoOut()81 	mpegVideoOut(){};
82 	void Copy(FILE* file, off_t from, off_t to);
83 };
84 
85 
86 class mpegSystemOut : public mpegOutWithVideo
87 {
88 public:
mpegSystemOut(char * filename)89 	mpegSystemOut(char* filename) :
90 		mpegOutWithVideo(filename) {partial_packet = 0;};
mpegSystemOut(FILE * filehandle)91 	mpegSystemOut(FILE* filehandle) :
92 		mpegOutWithVideo(filehandle) {partial_packet = 0;};
93 
94 	void WriteHeader(mpeg* Mpeg);
95 	int WriteChunk(mpeg* Mpeg,
96 		off_t from, bool from_included,
97 		off_t to, bool to_included );
98 	void Finish();
99 
100 protected:
mpegSystemOut()101 	mpegSystemOut() {};
102 	void Copy(FILE* file, off_t from, off_t to);
103 
104 	//size of data from the end of the packet header to the end of file
105 	off_t partial_packet_length;
106 	//keep the partial packet in memory (because stdout is not seekable)
107 	byte* partial_packet;
108 };
109 
110 class mpegAudioOut : public mpegOut
111 {
112 public:
mpegAudioOut(char * filename)113 	mpegAudioOut(char* filename):
114 		mpegOut(filename){};
mpegAudioOut(FILE * filehandle)115 	mpegAudioOut(FILE* filehandle):
116 		mpegOut(filehandle){};
117 
WriteHeader(mpeg * Mpeg)118 	void WriteHeader(mpeg* Mpeg){};
119 	int WriteChunk(mpeg* Mpeg,
120 		off_t from, bool from_included,
121 		off_t to, bool to_included );
122 
123 	void Finish();
124 protected:
mpegAudioOut()125 	mpegAudioOut(){};
126 	void Copy(FILE* file, off_t from, off_t to);
127 };
128 
129 class mpegOutFactory
130 {
131 public:
132 	mpegOut* NewMpegFrom(mpeg* MpegIn, char* filename);
133 	mpegOut* NewMpegFrom(mpeg* MpegIn, FILE* filehandle);
134 };
135 
136 
137 class demuxer
138 {
139 public:
140 	demuxer(mpeg* _Mpeg, char* _basename, bool _confirm = true);
141 	~demuxer();
142 	int Process();
143 
144 protected:
145 	int ProcessTransportStream();
146 	int ProcessProgramStream();
147 	int DemuxTrPkt(FILE* out, off_t start, off_t end);
148 	FILE* openfile(char* filename);
149 	off_t Copy(FILE* into, off_t from, off_t to);
demuxer()150 	demuxer() {};
151 	FILE* AudioFile[16];
152 	int n_audio;
153 	FILE* VideoFile[16];
154 	int n_video;
155 	int n_programs;
156 	mpeg* Mpeg;
157 	char* basename;
158 	bool confirm;
159 	byte* buffer;
160 };
161 
162 #endif //__mpegOut_hh_
163