Debian Codegraph @

src/mpgfile.h

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #ifndef _DVBCUT_MPGFILE_H
22 #define _DVBCUT_MPGFILE_H
23
24 #include <string>
25 #include <vector>
26 #include <list>
27
28 #include "port.h"
29 #include "buffer.h"
30 #include "types.h"
31 #include "index.h"
32 #include "pts.h"
33 #include "defines.h"
34 #include "stream.h"
35
36 /**
37 @author Sven Over
38 */
39 class avframe;
40 class muxer;
41 class logoutput;
42
43 class mpgfile
44 {
45 public:
46
47 protected:
48 inbuffer &buf;
49 stream s[MAXAVSTREAMS];
50 int videostreams, audiostreams;
51 int initialoffset;
52 class index idx;
53 int pictures;
54 int time_base_num, time_base_den;
55
56 mpgfile(inbuffer &b, int initial_offset);
57
58
59 public:
60 virtual ~mpgfile();
61
62 static mpgfile *open(inbuffer &b, std::string *errormessage = 0);
63 virtual int streamreader(struct streamhandle &s)=0;
64 virtual int mplayeraudioid(int audiostream)=0;
65 virtual bool istransportstream()
66 {
67 return false;
68 }
69 virtual std::vector<int> getbookmarks()
70 {
71 std::vector<int> pic_bookmarks;
72 return pic_bookmarks;
73 }
74
75 int getinitialoffset() const
76 {
77 return initialoffset;
78 }
79 int getpictures() const
80 {
81 return pictures;
82 }
83 int getaudiostreams() const
84 {
85 return audiostreams;
86 }
87 streamtype::type getstreamtype(int str) const
88 {
89 return s[str].type;
90 }
91 const std::string &getstreaminfo(int str) const
92 {
93 return s[str].getinfo();
94 }
95 const index::picture &operator[](unsigned int i) const
96 {
97 return idx[idx.indexnr(i)];
98 }
99 int lastseqheader(int i) const
100 {
101 while (i>0 && !idx[idx.indexnr(i)].getseqheader())
102 --i;
103 return i;
104 }
105 int lastiframe(int i) const
106 {
107 while (i>0 && !idx[idx.indexnr(i)].isiframe())
108 --i;
109 return i;
110 }
111 int nextseqheader(int i) const
112 {
113 while (i+1<pictures && !idx[idx.indexnr(i)].getseqheader())
114 ++i;
115 return i;
116 }
117 int nextiframe(int i) const
118 {
119 while (i+1<pictures && !idx[idx.indexnr(i)].isiframe())
120 ++i;
121 return i;
122 }
123 int nextaspectdiscontinuity(int i) const
124 {
125 if (i >= 0 && i < pictures)
126 {
127 int aspect = idx[idx.indexnr(i)].getaspectratio();
128 while (++i < pictures)
129 {
130 if (aspect != idx[idx.indexnr(i)].getaspectratio())
131 return i;
132 }
133 }
134 return -1;
135 }
136 int getpictureatposition(dvbcut_off_t position) const
137 {
138 // binary search for the picture at nearest file position (in bytes)
139 int firstpic=0, lastpic=pictures-1;
140 int first=idx.indexnr(firstpic), mid, last=idx.indexnr(lastpic);
141 dvbcut_off_t firstpos=idx[first].getpos();
142 dvbcut_off_t lastpos=idx[last].getpos();
143 dvbcut_off_t midpos;
144
145 if(position<firstpos || position>lastpos) return -1;
146
147 while( last-first > 1 )
148 {
149 mid = (first + last)/2;
150 midpos = idx[mid].getpos();
151
152 if(position > midpos) {
153 first = mid;
154 firstpos = midpos;
155 } else if(position < midpos) {
156 last = mid;
157 lastpos = midpos;
158 } else { // equality
159 first = mid;
160 firstpos = midpos;
161 last = mid;
162 lastpos = midpos;
163 }
164 }
165
166 // index of picture at nearest file position
167 int ind = (position-firstpos)<(lastpos-position) ? first : last;
168
169 // get the corresponding picture number
170 return idx.picturenr(ind);
171 }
172 int getpictureattime(pts_t time) const
173 {
174 int first=idx.indexnr(0), second=idx.indexnr(1);
175 pts_t firstpts=idx[first].getpts(), secondpts=idx[second].getpts();
176
177 int timeperframe=secondpts-firstpts;
178 timeperframe = timeperframe>0 && timeperframe<5000 ? timeperframe : 3003;
179
180 return time/timeperframe;
181 }
182 AVCodecContext *getavcc(int str)
183 {
184 return s[str].avcc;
185 }
186 void setvideoencodingparameters()
187 {
188 s[videostream()].setvideoencodingparameters(time_base_num, time_base_den);
189 }
190
191 int generateindex(const char *savefilename=0, std::string *errorstring=0, logoutput *log=0)
192 {
193 int rv=idx.generate(savefilename,errorstring,log);
194 pictures=(rv>0)?rv:0;
195 return rv;
196 }
197 int loadindex(const char *filename, std::string *errorstring=0)
198 {
199 int rv=idx.load(filename,errorstring);
200 pictures=(rv>0)?rv:0;
201 return rv;
202 }
203 int saveindex(const char *filename, std::string *errorstring=0)
204 {
205 return idx.save(filename,errorstring);
206 }
207 int getwidth(int res)
208 {
209 return idx.getwidth(res);
210 }
211 int getheight(int res)
212 {
213 return idx.getheight(res);
214 }
215
216 void decodegop(int start, int stop, std::list<avframe*> &framelist);
217 void initaudiocodeccontext(int aud);
218 void initcodeccontexts(int vid);
219 void playaudio(int aud, int picture, int ms);
220 void savempg(muxer &mux, int start, int stop, int progresspics=0,
221 int progresstotal=0, logoutput *log=0);
222 void recodevideo(muxer &mux, int start, int stop, pts_t offset,
223 int progresspics=0, int progresstotal=0, logoutput *log=0);
224 void fixtimecode(uint8_t *buf, int len, pts_t pts);
225 ssize_t readfile(std::string filename, uint8_t **buffer);
226
227 dvbcut_off_t getfilesize()
228 {
229 return buf.getfilesize();
230 }
231 dvbcut_off_t getfilepos() const
232 {
233 return buf.getfilepos();
234 }
235
236 static pts_t char2pts(const unsigned char *h)
237 {
238 return pts_t((uint32_t(h[4] & 0xfe) >> 1) |
239 (uint32_t(h[3]) << 7) |
240 (uint32_t(h[2] & 0xfe) << 14) |
241 (uint32_t(h[1]) << 22)) | pts_t((uint32_t) (h[0] & 0x0e) << 29);
242 }
243 static pts_t char2pts(const unsigned char *h, pts_t reference)
244 {
245 return ptsreference(char2pts(h),reference);
246 }
247 static const int frameratescr[16];
248 };
249
250 #endif