| 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 |
#include <cassert> |
| 22 |
#include "streamdata.h" |
| 23 |
|
| 24 |
static const int mpegaudio_rates[]= |
| 25 |
{ |
| 26 |
44100,48000,32000,16000 |
| 27 |
}; |
| 28 |
static const int mpegaudio_bitrate[][16]= |
| 29 |
{ |
| 30 |
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // undefined layer |
| 31 |
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320}, // layer 3 |
| 32 |
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384}, // layer 2 |
| 33 |
{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448} // layer 1 |
| 34 |
}; |
| 35 |
|
| 36 |
static inline int is_mpa(const unsigned char *header) |
| 37 |
{ |
| 38 |
return header[0] == 0xff /* sync word */ |
| 39 |
&& (header[1] & 0xe6) > 0xe0 /* sync word & layer */ |
| 40 |
&& (header[2] & 0xf0) != 0xf0 /* bitrate code */ |
| 41 |
&& (header[2] & 0x0c) != 0x0c; /* sampling rate code */ |
| 42 |
} |
| 43 |
|
| 44 |
// return frame size in bytes, or 0 for no valid frame |
| 45 |
static int mpaframe(const void *data, int &pos, int len, int &duration) |
| 46 |
{ |
| 47 |
const unsigned char *d=(const unsigned char *)data; |
| 48 |
|
| 49 |
for (; pos+2<len; ++pos) { |
| 50 |
if (!is_mpa(&d[pos])) |
| 51 |
continue; |
| 52 |
|
| 53 |
int layer=(d[pos+1]>>1)&0x03; |
| 54 |
int samples=(layer==4-1)?384:1152; |
| 55 |
int samplingrate=mpegaudio_rates[(d[pos+2]>>2)&0x03]; |
| 56 |
int bitratecode=(d[pos+2]>>4)&0x0f; |
| 57 |
|
| 58 |
int skipbytes=(mpegaudio_bitrate[layer][bitratecode]*125)*samples/samplingrate; |
| 59 |
if (skipbytes>0 && pos+skipbytes+2<len && is_mpa(&d[pos+skipbytes])) { |
| 60 |
//fprintf(stderr, "mpaframe: samples=%d pos=%d len=%d\n", samples, pos,len); |
| 61 |
duration = samples*90000/samplingrate; // Duration of MPEG audio frame in 90000Hz units |
| 62 |
return skipbytes; |
| 63 |
} |
| 64 |
} |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
// |
| 69 |
|
| 70 |
#define AC3_SYNCWORD (mbo16(0x0b77)) |
| 71 |
|
| 72 |
static int ac3_framelength[256] = { |
| 73 |
128, 128, 160, 160, 192, 192, 224, 224, |
| 74 |
256, 256, 320, 320, 384, 384, 448, 448, |
| 75 |
512, 512, 640, 640, 768, 768, 896, 896, |
| 76 |
1024,1024,1280,1280,1536,1536,1792,1792, |
| 77 |
2048,2048,2304,2304,2560,2560, 2, 2, |
| 78 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 79 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 80 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 81 |
138, 140, 174, 176, 208, 210, 242, 244, |
| 82 |
278, 280, 348, 350, 416, 418, 486, 488, |
| 83 |
556, 558, 696, 698, 834, 836, 974, 976, |
| 84 |
1114,1116,1392,1394,1670,1672,1950,1952, |
| 85 |
2228,2230,2506,2508,2786,2788, 2, 2, |
| 86 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 87 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 88 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 89 |
192, 192, 240, 240, 288, 288, 336, 336, |
| 90 |
384, 384, 480, 480, 576, 576, 672, 672, |
| 91 |
768, 768, 960, 960,1152,1152,1344,1344, |
| 92 |
1536,1536,1920,1920,2304,2304,2688,2688, |
| 93 |
3072,3072,3456,3456,3840,3840, 2, 2, |
| 94 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 95 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 96 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 97 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 98 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 99 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 100 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 101 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 102 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 103 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 104 |
2, 2, 2, 2, 2, 2, 2, 2 |
| 105 |
}; |
| 106 |
|
| 107 |
static int ac3_frameduration[4]={270000*1536/480,270000*1536/441,270000*1536/320,270000*1536/480}; |
| 108 |
|
| 109 |
// return frame size in bytes, or 0 for no valid frame |
| 110 |
static int ac3frame(const void *data, int &pos, int len, int &duration) |
| 111 |
{ |
| 112 |
const unsigned char *d=(const unsigned char *)data; |
| 113 |
|
| 114 |
for(; pos+4<len; ++pos) { |
| 115 |
if ((*(uint16_t*)(d+pos)!=AC3_SYNCWORD)) |
| 116 |
continue; |
| 117 |
|
| 118 |
int framelength = ac3_framelength[d[pos+4]]; |
| 119 |
|
| 120 |
if ((pos+framelength+1)<len && (*(uint16_t*)(d+pos+framelength)==AC3_SYNCWORD)) { |
| 121 |
duration = ac3_frameduration[(d[pos+4]>>6)&3]/300; // Duration of AC3 audio frame in 90000Hz units |
| 122 |
return framelength; |
| 123 |
} |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
void streamdata::audio_addpts(uint32_t startbufferpos, bool onepacket) |
| 129 |
{ |
| 130 |
bool needsync=true; |
| 131 |
itemlisttype::iterator it=items.begin(); |
| 132 |
|
| 133 |
while(it!=items.end()) |
| 134 |
if (!it->headerhaspts()) |
| 135 |
it=items.erase(it); |
| 136 |
else |
| 137 |
++it; |
| 138 |
|
| 139 |
it=items.begin(); |
| 140 |
while (it!=items.end() && it->bufferposition<startbufferpos) |
| 141 |
++it; |
| 142 |
itemlisttype::iterator nx; |
| 143 |
uint32_t stopbufferpos=0; |
| 144 |
pts_t pts=0; |
| 145 |
|
| 146 |
while (it!=items.end()) { |
| 147 |
if (needsync) { |
| 148 |
needsync=false; |
| 149 |
|
| 150 |
assert(it!=items.end()); |
| 151 |
assert(it->headerhaspts()); |
| 152 |
|
| 153 |
if (onepacket) { |
| 154 |
if (stopbufferpos) { |
| 155 |
if (it->bufferposition>=stopbufferpos) |
| 156 |
break; |
| 157 |
} else { |
| 158 |
itemlisttype::iterator n=it; |
| 159 |
for(++n;n!=items.end();++n) |
| 160 |
if (it->headerhaspts()) |
| 161 |
break; |
| 162 |
if (n!=items.end()) |
| 163 |
stopbufferpos=n->bufferposition; |
| 164 |
else |
| 165 |
onepacket=false; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
nx=it; |
| 170 |
++nx; |
| 171 |
} |
| 172 |
|
| 173 |
pts=it->headerpts(); |
| 174 |
uint32_t bufferposition=it->bufferposition; |
| 175 |
|
| 176 |
// parse frames in this item |
| 177 |
int pos=0; |
| 178 |
int ptsplus=0; |
| 179 |
int (*auframe)(const void *data, int &pos, int len, int &duration) = |
| 180 |
(type==streamtype::ac3audio) ? ac3frame : mpaframe; |
| 181 |
uint8_t *data = (uint8_t*)getdata(bufferposition); |
| 182 |
int len = inbytes()-(bufferposition-getoffset()); |
| 183 |
int bytes; |
| 184 |
while ((bytes = auframe(data,pos,len,ptsplus)) != 0) { |
| 185 |
if (pos == 0) { |
| 186 |
//fprintf(stderr, "setting frame at item\n"); |
| 187 |
it->flags |= STREAM_ITEM_FLAG_FRAME; |
| 188 |
} else { |
| 189 |
bufferposition+=pos; |
| 190 |
data += pos; |
| 191 |
len -= pos; |
| 192 |
if (nx != items.end() && bufferposition >= nx->bufferposition) { |
| 193 |
//fprintf(stderr, "hit next %ld>=%ld pts=%lld interp=%lld\n", |
| 194 |
// bufferposition, nx->bufferposition,nx->headerpts(), |
| 195 |
// pts - (bufferposition - nx->bufferposition)*ptsplus/bytes); |
| 196 |
break; |
| 197 |
} |
| 198 |
it=items.insert(nx,item(it->fileposition+(bufferposition-it->bufferposition), |
| 199 |
STREAM_ITEM_FLAG_HAS_PTS|STREAM_ITEM_FLAG_FRAME, |
| 200 |
pts,bufferposition)); |
| 201 |
//fprintf(stderr, "sd: insert %p (pos %ld, nxflags %d) pts=%lld\n", |
| 202 |
// &(*it), bufferposition, nx->flags, pts); |
| 203 |
} |
| 204 |
pos = bytes; |
| 205 |
pts+=ptsplus; |
| 206 |
} |
| 207 |
it=nx; |
| 208 |
++nx; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/// Find the packet with the highest PTS less or equal than the given pts and returns its buffer position. |
| 213 |
uint32_t streamdata::ptsbufferpos(pts_t pts) |
| 214 |
{ |
| 215 |
uint32_t bufferpos=getoffset(); |
| 216 |
streamdata::itemlisttype::const_iterator it=items.begin(); |
| 217 |
|
| 218 |
while(it!=items.end()) |
| 219 |
if (it->headerhaspts()) |
| 220 |
break; |
| 221 |
else |
| 222 |
++it; |
| 223 |
|
| 224 |
if (it!=items.end()) |
| 225 |
for(;;) { |
| 226 |
bufferpos=it->bufferposition; |
| 227 |
pts_t itpts=it->headerpts(pts); |
| 228 |
if (itpts>=pts) |
| 229 |
break; |
| 230 |
streamdata::itemlisttype::const_iterator nx=it; |
| 231 |
++nx; |
| 232 |
while (nx!=items.end() && !nx->headerhaspts()) |
| 233 |
++nx; |
| 234 |
if (nx==items.end()) |
| 235 |
break; |
| 236 |
pts_t nxpts=nx->headerpts(pts); |
| 237 |
if (nxpts>pts) |
| 238 |
break; |
| 239 |
if (nxpts==pts) { |
| 240 |
bufferpos=nx->bufferposition; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
it=nx; |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
return bufferpos; |
| 249 |
} |
| 250 |
|
| 251 |
/// Find the packet with its PTS closest to the given pts and returns its buffer position. |
| 252 |
uint32_t streamdata::closestptsbufferpos(pts_t pts) |
| 253 |
{ |
| 254 |
uint32_t bufferpos=getoffset(); |
| 255 |
streamdata::itemlisttype::const_iterator it=items.begin(); |
| 256 |
|
| 257 |
while(it!=items.end()) |
| 258 |
if (it->headerhaspts()) |
| 259 |
break; |
| 260 |
else |
| 261 |
++it; |
| 262 |
|
| 263 |
for(;it!=items.end();) { |
| 264 |
pts_t itpts=it->headerpts(pts); |
| 265 |
if (itpts>=pts) |
| 266 |
break; |
| 267 |
streamdata::itemlisttype::const_iterator nx=it; |
| 268 |
++nx; |
| 269 |
while (nx!=items.end() && !nx->headerhaspts()) |
| 270 |
++nx; |
| 271 |
if (nx==items.end()) |
| 272 |
break; |
| 273 |
pts_t nxpts=nx->headerpts(pts); |
| 274 |
if (nxpts<pts) { |
| 275 |
bufferpos=nx->bufferposition; |
| 276 |
it=nx; |
| 277 |
continue; |
| 278 |
} |
| 279 |
if (nxpts==pts) { |
| 280 |
bufferpos=nx->bufferposition; |
| 281 |
break; |
| 282 |
} |
| 283 |
|
| 284 |
if (pts-itpts>=nxpts-pts) |
| 285 |
bufferpos=nx->bufferposition; |
| 286 |
|
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
return bufferpos; |
| 291 |
} |
| 292 |
|
| 293 |
void streamdata::appenditem(filepos_t fp, const std::string &header, const void *d, int s) |
| 294 |
{ |
| 295 |
int flags(0); |
| 296 |
pts_t pts(0); |
| 297 |
const uint8_t *data=(const uint8_t*)d; |
| 298 |
|
| 299 |
if (header.size()>=3) { |
| 300 |
if ((header[1]&0x80) && (header.size()>=8)) { |
| 301 |
flags |= STREAM_ITEM_FLAG_HAS_PTS; |
| 302 |
pts=mpgfile::char2pts((const unsigned char *)header.c_str()+3); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (s>0) { |
| 307 |
appenditem(fp, flags, pts, (const void*)data, s); |
| 308 |
} |
| 309 |
} |
| 310 |
|