| 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_INDEX_H |
| 22 |
#define _DVBCUT_INDEX_H |
| 23 |
|
| 24 |
#include <stdint.h> |
| 25 |
#include <set> |
| 26 |
#include <vector> |
| 27 |
#include "types.h" |
| 28 |
#include "pts.h" |
| 29 |
#include "defines.h" |
| 30 |
|
| 31 |
#define IDX_PICTYPE_I 1 |
| 32 |
#define IDX_PICTYPE_P 2 |
| 33 |
#define IDX_PICTYPE_B 3 |
| 34 |
#define IDX_PICTYPE ((char*)".IPB") |
| 35 |
|
| 36 |
class mpgfile; |
| 37 |
class logoutput; |
| 38 |
|
| 39 |
/** |
| 40 |
@author Sven Over |
| 41 |
*/ |
| 42 |
class index |
| 43 |
{ |
| 44 |
public: |
| 45 |
struct picture |
| 46 |
{ |
| 47 |
filepos_t position; |
| 48 |
uint64_t pts; // additional information in the 24 most significant bits: |
| 49 |
// RRRQTTSSSSSSSSSSAAAAFFFF |
| 50 |
// R: resolution number (according to lookup table with WIDTHxHEIGHT at end of index) |
| 51 |
// Q: sequence header flag |
| 52 |
// T: picture type |
| 53 |
// S: picture sequence number |
| 54 |
// A: aspect ratio (mpeg code) |
| 55 |
// F: frame rate (mpeg code) |
| 56 |
|
| 57 |
picture(filepos_t pos, pts_t _pts, int framerate, int aspectratio, |
| 58 |
int sequencenumber, int picturetype, bool seqheader=false, int resolution=0) : |
| 59 |
position( pos ), |
| 60 |
pts((_pts&0xffffffffffll) | ((uint64_t)( (framerate&0xf)|((aspectratio&0xf)<<4)| |
| 61 |
((sequencenumber&0x3ff)<<8)|((picturetype&0x3)<<18)| |
| 62 |
(seqheader?0x100000:0)|((resolution&0x7)<<21) )<<40)) |
| 63 |
{ } |
| 64 |
picture() : position(0), pts(0) |
| 65 |
{} |
| 66 |
|
| 67 |
filepos_t getpos() const |
| 68 |
{ |
| 69 |
return position; |
| 70 |
} |
| 71 |
pts_t getpts() const |
| 72 |
{ |
| 73 |
pts_t l=pts&0xffffffffffll; |
| 74 |
return (l ^ 0x8000000000ll) - 0x8000000000ll; |
| 75 |
} |
| 76 |
int getframerate() const |
| 77 |
{ |
| 78 |
return int(pts>>40)&0xf; |
| 79 |
} |
| 80 |
int getaspectratio() const |
| 81 |
{ |
| 82 |
return int(pts>>44)&0xf; |
| 83 |
} |
| 84 |
int getsequencenumber() const |
| 85 |
{ |
| 86 |
return int(pts>>48)&0x3ff; |
| 87 |
} |
| 88 |
void setsequencenumber(int s) |
| 89 |
{ |
| 90 |
pts=(pts&0xfc00ffffffffffffull)|(uint64_t(s&0x3ff)<<48); |
| 91 |
} |
| 92 |
int getpicturetype() const |
| 93 |
{ |
| 94 |
return int(pts>>58)&0x3; |
| 95 |
} |
| 96 |
bool getseqheader() const |
| 97 |
{ |
| 98 |
return pts&0x1000000000000000ll; |
| 99 |
} |
| 100 |
int getresolution() const |
| 101 |
{ |
| 102 |
return int(pts>>61)&0x7; |
| 103 |
} |
| 104 |
bool isbframe() const |
| 105 |
{ |
| 106 |
return getpicturetype()==IDX_PICTYPE_B; |
| 107 |
} |
| 108 |
bool ispframe() const |
| 109 |
{ |
| 110 |
return getpicturetype()==IDX_PICTYPE_P; |
| 111 |
} |
| 112 |
bool isiframe() const |
| 113 |
{ |
| 114 |
return getpicturetype()==IDX_PICTYPE_I; |
| 115 |
} |
| 116 |
|
| 117 |
bool operator<(picture &a) const |
| 118 |
{ |
| 119 |
return getsequencenumber()<a.getsequencenumber(); |
| 120 |
} |
| 121 |
|
| 122 |
}; |
| 123 |
|
| 124 |
protected: |
| 125 |
static picture zeroitem; |
| 126 |
mpgfile &mpg; |
| 127 |
picture *p; |
| 128 |
int pictures; |
| 129 |
int skipfirst; |
| 130 |
int realpictures; |
| 131 |
|
| 132 |
int check(); |
| 133 |
|
| 134 |
// lookup-tables with found resolutions (stored at end of index file and coded in 3 |
| 135 |
// most significant bits of picture structure, 0 for old type index files) |
| 136 |
std::vector<int> WIDTH, HEIGHT; |
| 137 |
std::set<std::pair<int,int> > resolutions; |
| 138 |
|
| 139 |
public: |
| 140 |
index(mpgfile &m) : mpg(m),p(0),pictures(0) |
| 141 |
{} |
| 142 |
|
| 143 |
~index(); |
| 144 |
|
| 145 |
int generate(const char *savefilename=0, std::string *errorstring=0, logoutput *log=0); |
| 146 |
int save(int fd, std::string *errorstring = 0, bool closeme = false); |
| 147 |
int save(const char *filename, std::string *errorstring=0); |
| 148 |
int load(const char *filename, std::string *errorstring=0); |
| 149 |
|
| 150 |
int getwidth(int res) const |
| 151 |
{ |
| 152 |
return res>0 && res<=int(WIDTH.size()) ? WIDTH[res-1] : -1; |
| 153 |
} |
| 154 |
int getheight(int res) const |
| 155 |
{ |
| 156 |
return res>0 && res<=int(HEIGHT.size()) ? HEIGHT[res-1] : -1; |
| 157 |
} |
| 158 |
|
| 159 |
int indexnr(int pic) const |
| 160 |
{ |
| 161 |
pic+=skipfirst; |
| 162 |
int seq=pic; |
| 163 |
while (seq > 0 && !p[seq].getseqheader()) |
| 164 |
--seq; |
| 165 |
pic-=seq; |
| 166 |
while(seq < pictures && p[seq].getsequencenumber()!=pic) |
| 167 |
++seq; |
| 168 |
return seq; |
| 169 |
} |
| 170 |
int picturenr(int ind) const // the reverse function |
| 171 |
{ |
| 172 |
int pic=p[ind].getsequencenumber(); |
| 173 |
while (ind > 0 && !p[ind].getseqheader()) |
| 174 |
--ind; |
| 175 |
pic+=ind-skipfirst; |
| 176 |
return pic; |
| 177 |
} |
| 178 |
const picture &operator[](unsigned int i) const |
| 179 |
{ |
| 180 |
return p[i]; |
| 181 |
} |
| 182 |
int getpictures() const |
| 183 |
{ |
| 184 |
return pictures; |
| 185 |
} |
| 186 |
int getrealpictures() const |
| 187 |
{ |
| 188 |
return realpictures; |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
#endif |