| 1 |
#define USE_REGISTRY |
| 2 |
#include "capproc.h" |
| 3 |
|
| 4 |
#include "dsp.h" |
| 5 |
#include "ccap.h" |
| 6 |
|
| 7 |
#include <avifile.h> |
| 8 |
#include <videoencoder.h> |
| 9 |
#include <image.h> |
| 10 |
#include <avm_fourcc.h> |
| 11 |
#include <avm_cpuinfo.h> |
| 12 |
#include <utils.h> |
| 13 |
#include <avm_creators.h> |
| 14 |
#include <avm_except.h> |
| 15 |
#include <avm_stl.h> |
| 16 |
|
| 17 |
#define DECLARE_REGISTRY_SHORTCUT |
| 18 |
#include <configfile.h> |
| 19 |
#undef DECLARE_REGISTRY_SHORTCUT |
| 20 |
|
| 21 |
#include <unistd.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#include "v4lxif.h" |
| 26 |
//#include "v4lwindow.h" |
| 27 |
|
| 28 |
extern unsigned int m_iMemory; |
| 29 |
|
| 30 |
#define __MODULE__ "Capture Config" |
| 31 |
|
| 32 |
void copy_deinterlace_24(void* outpic, const void* inpic, int xdim, int height); |
| 33 |
extern "C"{ |
| 34 |
void deinterlace_bob_yuv_mmx(void* outpic, const void* inpic, int xdim, int height); |
| 35 |
} |
| 36 |
|
| 37 |
static struct restable_s { |
| 38 |
enum Resolutions res; |
| 39 |
int width; |
| 40 |
int height; |
| 41 |
} restable[] = { |
| 42 |
{ W768, 768, 576 }, |
| 43 |
{ W640, 640, 480 }, |
| 44 |
{ W576, 576, 432 }, |
| 45 |
{ W400, 400, 300 }, |
| 46 |
{ W384, 384, 288 }, |
| 47 |
{ W320, 320, 240 }, |
| 48 |
{ W192, 192, 144 }, |
| 49 |
{ W160, 160, 120 }, |
| 50 |
{ W512, 512, 384 }, |
| 51 |
{ WNONE, 0, 0 } |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
#ifdef USE_REGISTRY |
| 56 |
|
| 57 |
void CaptureConfig::setNamedCodec(avm::string named_codec){ |
| 58 |
if(named_codec==""){ |
| 59 |
return; |
| 60 |
} |
| 61 |
//search the regnumber of our codec |
| 62 |
int regnumber=0; |
| 63 |
|
| 64 |
char regbuf[300]; |
| 65 |
|
| 66 |
sprintf(regbuf,"NamedCodecs-%02d-Savename",regnumber); |
| 67 |
avm::string nc_savename=RS(regbuf,"NONE"); |
| 68 |
|
| 69 |
while(!(named_codec==nc_savename || nc_savename=="NONE")){ |
| 70 |
regnumber++; |
| 71 |
|
| 72 |
sprintf(regbuf,"NamedCodecs-%02d-Savename",regnumber); |
| 73 |
nc_savename=RS(regbuf,"NONE"); |
| 74 |
|
| 75 |
} |
| 76 |
if(nc_savename!="NONE"){ |
| 77 |
sprintf(regbuf,"NamedCodecs-%02d-Compressor",regnumber); |
| 78 |
codec.compressor=RI(regbuf,fccIV50); |
| 79 |
|
| 80 |
sprintf(regbuf,"NamedCodecs-%02d-Quality",regnumber); |
| 81 |
codec.quality=RI(regbuf,9500); |
| 82 |
|
| 83 |
sprintf(regbuf,"NamedCodecs-%02d-Keyframe",regnumber); |
| 84 |
codec.keyfreq=RI(regbuf,15); |
| 85 |
|
| 86 |
printf("capconf: setting named codec to %s/%d\n",named_codec.c_str(), codec.compressor); |
| 87 |
//avm::codecSetSaveName(named_codec); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void CaptureConfig::load() |
| 92 |
{ |
| 93 |
audio_compressor = 1; |
| 94 |
deinterlace = false; |
| 95 |
|
| 96 |
audio_device = RS("AudioDevice", "/dev/dsp"); |
| 97 |
filename = RS("FileName", "./movie.avi"); |
| 98 |
never_overwrite = RI("NeverOverwriteFiles", 0); |
| 99 |
segment_size = RI("IsSegmented", 1)?RI("SegmentSize", 1000*1000):-1; |
| 100 |
codec.compressor = RI("Compressor", fccIV50); |
| 101 |
codec.cname = RS("Codec", "odivx"); |
| 102 |
codec.quality = RI("Quality", 9500); |
| 103 |
codec.keyfreq = RI("Keyframe", 15); |
| 104 |
timelimit = RI("LimitTime", 0)?RI("TimeLimit", 3600):-1; |
| 105 |
sizelimit = RI("LimitSize", 0)?RI("SizLimit", 2000000):-1; |
| 106 |
fps = (float)RI("FPS", 25000) / 1000.f; |
| 107 |
colorspace = (enum Colorspaces)RI("Colorspace", 0); |
| 108 |
|
| 109 |
//avm::codecSetSaveName(""); |
| 110 |
|
| 111 |
avm::string named_codec=RS("NamedCodecsCurrent",""); |
| 112 |
setNamedCodec(named_codec); |
| 113 |
setDimensions((enum Resolutions)RI("Resolution", 0)); |
| 114 |
setFrequency((enum Sound_Freqs)(RI("HaveAudio", 1)?RI("Frequency", 0):NoAudio)); |
| 115 |
if (frequency>0) |
| 116 |
{ |
| 117 |
setSamplesize((enum Sample_Sizes)RI("SampleSize", 0)); |
| 118 |
setChannels((enum Sound_Chans)RI("SndChannels", 0)); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void CaptureConfig::setDimensions(Resolutions res) |
| 123 |
{ |
| 124 |
int i = 0; |
| 125 |
while (restable[i].res != res && restable[i].res != WNONE) |
| 126 |
i++; |
| 127 |
if (restable[i].res == res) |
| 128 |
{ |
| 129 |
over_l = 0; |
| 130 |
over_r = 0; |
| 131 |
over_t = 0; |
| 132 |
over_b = 0; |
| 133 |
res_w = restable[i].width; |
| 134 |
res_h = restable[i].height; |
| 135 |
//cout<<"Resolution: "<<res_w<<" x "<<res_h<<endl; |
| 136 |
} |
| 137 |
else |
| 138 |
throw FATAL("Unknown video resolution"); |
| 139 |
} |
| 140 |
|
| 141 |
void CaptureConfig::setChannels(Sound_Chans c) |
| 142 |
{ |
| 143 |
switch (c) |
| 144 |
{ |
| 145 |
case Mono: |
| 146 |
case Lang1: |
| 147 |
case Lang2: |
| 148 |
chan = 1; |
| 149 |
break; |
| 150 |
case Stereo: |
| 151 |
chan = 2; |
| 152 |
break; |
| 153 |
default: |
| 154 |
throw FATAL("Unknown channel number"); |
| 155 |
} |
| 156 |
} |
| 157 |
void CaptureConfig::setSamplesize(Sample_Sizes ss) |
| 158 |
{ |
| 159 |
switch (ss) |
| 160 |
{ |
| 161 |
case S16: |
| 162 |
samplesize = 16; |
| 163 |
break; |
| 164 |
case S8: |
| 165 |
samplesize = 8; |
| 166 |
break; |
| 167 |
default: |
| 168 |
throw FATAL("Unknown audio sample size"); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void CaptureConfig::setFrequency(Sound_Freqs sfreq) |
| 173 |
{ |
| 174 |
switch (sfreq) |
| 175 |
{ |
| 176 |
case NoAudio: |
| 177 |
frequency=0; |
| 178 |
break; |
| 179 |
case F48: |
| 180 |
frequency = 48000; |
| 181 |
break; |
| 182 |
case F44: |
| 183 |
frequency = 44100; |
| 184 |
break; |
| 185 |
case F32: |
| 186 |
frequency = 32000; |
| 187 |
break; |
| 188 |
case F22: |
| 189 |
frequency = 22050; |
| 190 |
break; |
| 191 |
case F16: |
| 192 |
frequency = 16000; |
| 193 |
break; |
| 194 |
case F12: |
| 195 |
frequency = 12000; |
| 196 |
break; |
| 197 |
case F11: |
| 198 |
frequency = 11025; |
| 199 |
break; |
| 200 |
case F8: |
| 201 |
frequency = 8000; |
| 202 |
break; |
| 203 |
default: |
| 204 |
throw FATAL("Unknown frequency"); |
| 205 |
} |
| 206 |
} |
| 207 |
#endif //USE_REGISTRY |
| 208 |
|
| 209 |
#undef __MODULE__ |
| 210 |
#define __MODULE__ "Capture Process" |
| 211 |
class frame_allocator |
| 212 |
{ |
| 213 |
public: |
| 214 |
int used_frames; |
| 215 |
|
| 216 |
frame_allocator(int w, int h, int limit, int bpp) |
| 217 |
:used_frames(0), _w(w), _h(h), _limit(limit), refs(2) |
| 218 |
{ |
| 219 |
printf("Using %d buffers (%dx%d)\n", limit, w, h); |
| 220 |
while(frames.size()<(unsigned)limit) |
| 221 |
{ |
| 222 |
frame f; |
| 223 |
f.data=new char[_w*_h*bpp/8+4]; |
| 224 |
f.status=0; |
| 225 |
frames.push_back(f); |
| 226 |
} |
| 227 |
} |
| 228 |
~frame_allocator() |
| 229 |
{ |
| 230 |
while (frames.size()) |
| 231 |
{ |
| 232 |
delete frames.back().data; |
| 233 |
frames.pop_back(); |
| 234 |
} |
| 235 |
} |
| 236 |
int get_limit() const { return _limit; } |
| 237 |
void release() |
| 238 |
{ |
| 239 |
refs--; |
| 240 |
if(!refs)delete this; |
| 241 |
} |
| 242 |
char* alloc() |
| 243 |
{ |
| 244 |
unsigned i; |
| 245 |
for(i=0; i<frames.size(); i++) |
| 246 |
{ |
| 247 |
if(frames[i].status==0) |
| 248 |
{ |
| 249 |
frames[i].status=1; |
| 250 |
used_frames++; |
| 251 |
*(int*)(frames[i].data)=i; |
| 252 |
return frames[i].data+4; |
| 253 |
} |
| 254 |
} |
| 255 |
return 0; |
| 256 |
/* |
| 257 |
if ((int)frames.size( )>= _limit) |
| 258 |
return 0; |
| 259 |
frame f; |
| 260 |
f.data=new char[_w*_h*3+4]; |
| 261 |
f.status=1; |
| 262 |
frames.push_back(f); |
| 263 |
used_frames++; |
| 264 |
*(int*)(f.data)=i; |
| 265 |
return f.data+4; |
| 266 |
*/ |
| 267 |
} |
| 268 |
void free(char* mem) |
| 269 |
{ |
| 270 |
if (!mem) |
| 271 |
{ |
| 272 |
fprintf(stderr,"ERROR: Freeing 0!\n"); |
| 273 |
return; |
| 274 |
} |
| 275 |
int id=*(int*)(mem-4); |
| 276 |
if (id < 0 || id >= (int)frames.size() || frames[id].data != (mem-4)) |
| 277 |
{ |
| 278 |
fprintf(stderr,"ERROR: Freeing unknown memory!\n"); |
| 279 |
return; |
| 280 |
} |
| 281 |
if (frames[id].status == 0) |
| 282 |
{ |
| 283 |
fprintf(stderr, "ERROR: Duplicate free()!\n"); |
| 284 |
return; |
| 285 |
} |
| 286 |
used_frames--; |
| 287 |
frames[id].status=0; |
| 288 |
} |
| 289 |
private: |
| 290 |
struct frame |
| 291 |
{ |
| 292 |
char* data; |
| 293 |
int status; |
| 294 |
}; |
| 295 |
avm::vector<frame> frames; |
| 296 |
int _w; |
| 297 |
int _h; |
| 298 |
int _limit; |
| 299 |
int refs; |
| 300 |
}; |
| 301 |
|
| 302 |
CaptureProcess::CaptureProcess(v4lxif* v4l, // v4lx interface pointer |
| 303 |
CaptureConfig* conf, |
| 304 |
ClosedCaption* ccap) |
| 305 |
: m_v4l(v4l), m_qvid(64), m_qaud(64), m_quit(0), |
| 306 |
m_pProgress(0), m_ccap(ccap), m_pAllocator(0), |
| 307 |
m_conf(*conf), error(""), segment_flag(0), vid_clear(0), aud_clear(0) |
| 308 |
{ |
| 309 |
segmenting_name=""; |
| 310 |
starttime=avm_get_time_us(); |
| 311 |
|
| 312 |
if(ccap) |
| 313 |
{ |
| 314 |
avm::string ccname; |
| 315 |
if (m_conf.filename.size() >= 4 |
| 316 |
&& (strncasecmp(m_conf.filename.c_str() + m_conf.filename.size() - 4, ".avi", 4) == 0)) |
| 317 |
ccname=m_conf.filename.substr(0, m_conf.filename.size()-4)+".sub"; |
| 318 |
else |
| 319 |
{ |
| 320 |
ccname=m_conf.filename; |
| 321 |
ccname+=".sub"; |
| 322 |
} |
| 323 |
ccfd=open(ccname.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 00666); |
| 324 |
lastcctime=starttime; |
| 325 |
ccap->add_callback(capwriter, (void*)this); |
| 326 |
} |
| 327 |
else |
| 328 |
ccfd=-1; |
| 329 |
int bpp; |
| 330 |
|
| 331 |
switch(m_conf.colorspace) |
| 332 |
{ |
| 333 |
case cspRGB24: |
| 334 |
default: |
| 335 |
m_v4l->grabSetParams(m_conf.res_w+m_conf.over_l+m_conf.over_r, m_conf.res_h+m_conf.over_t+m_conf.over_b, VIDEO_PALETTE_RGB24); |
| 336 |
bpp=24; |
| 337 |
break; |
| 338 |
case cspRGB15: |
| 339 |
m_v4l->grabSetParams(m_conf.res_w+m_conf.over_l+m_conf.over_r, m_conf.res_h+m_conf.over_t+m_conf.over_b, VIDEO_PALETTE_RGB555); |
| 340 |
bpp=16; |
| 341 |
break; |
| 342 |
case cspRGB32: |
| 343 |
m_v4l->grabSetParams(m_conf.res_w+m_conf.over_l+m_conf.over_r, m_conf.res_h+m_conf.over_t+m_conf.over_b, VIDEO_PALETTE_RGB32); |
| 344 |
bpp=32; |
| 345 |
break; |
| 346 |
case cspYUY2: |
| 347 |
m_v4l->grabSetParams(m_conf.res_w+m_conf.over_l+m_conf.over_r, m_conf.res_h+m_conf.over_t+m_conf.over_b, VIDEO_PALETTE_YUV422); |
| 348 |
bpp=16; |
| 349 |
break; |
| 350 |
case cspYV12: |
| 351 |
case cspI420: |
| 352 |
m_v4l->grabSetParams(m_conf.res_w+m_conf.over_l+m_conf.over_r, m_conf.res_h+m_conf.over_t+m_conf.over_b, VIDEO_PALETTE_YUV420P); |
| 353 |
bpp=12; |
| 354 |
break; |
| 355 |
} |
| 356 |
int maxframes = m_iMemory/(m_conf.res_w * m_conf.res_h * bpp / 8); |
| 357 |
if (maxframes < 5) |
| 358 |
maxframes = 5; |
| 359 |
|
| 360 |
m_v4l->setAudioMode(m_conf.chan); |
| 361 |
if (m_conf.frequency > 0) |
| 362 |
{ |
| 363 |
m_pDsp = new dsp(); |
| 364 |
if (m_pDsp->open(m_conf.samplesize, m_conf.chan, |
| 365 |
m_conf.frequency, m_conf.audio_device.c_str()) < 0)//returns file descriptor |
| 366 |
throw FATAL("Failed to open audio device"); |
| 367 |
m_pAudc = new avm::PthreadTask(0, CaptureProcess::audcap_starter, this); |
| 368 |
} |
| 369 |
try |
| 370 |
{ |
| 371 |
m_pAllocator=new frame_allocator(m_conf.res_w,m_conf.res_h,maxframes,bpp); |
| 372 |
} |
| 373 |
catch(...) |
| 374 |
{ |
| 375 |
throw FATAL("Out of memory"); |
| 376 |
} |
| 377 |
m_pVidc = new avm::PthreadTask(0, CaptureProcess::vidcap_starter, this); |
| 378 |
m_pWriter = new avm::PthreadTask(0, CaptureProcess::writer_starter, this); |
| 379 |
} |
| 380 |
|
| 381 |
CaptureProcess::~CaptureProcess() |
| 382 |
{ |
| 383 |
m_quit=1; |
| 384 |
fprintf(stderr, "waiting for threads to exit...\n"); |
| 385 |
delete m_pWriter; |
| 386 |
fprintf(stderr, "writer has exited...\n"); |
| 387 |
if(m_conf.frequency>0) |
| 388 |
delete m_pAudc; |
| 389 |
fprintf(stderr, "audio capture has exited...\n"); |
| 390 |
delete m_pVidc; |
| 391 |
fprintf(stderr, "video capture has...\n"); |
| 392 |
if(m_ccap) |
| 393 |
m_ccap->remove_callback(capwriter, (void*)this); |
| 394 |
if(ccfd) |
| 395 |
close(ccfd); |
| 396 |
fprintf(stderr, "All threads exited\n"); |
| 397 |
while (m_qaud.size()) |
| 398 |
{ |
| 399 |
chunk& z = m_qaud.front(); |
| 400 |
if (z.data) delete z.data; |
| 401 |
m_qaud.pop(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
void CaptureProcess::capwriter(void* arg, const char* data) |
| 406 |
{ |
| 407 |
CaptureProcess& a=*(CaptureProcess*)arg; |
| 408 |
// printf("%s\n", data); |
| 409 |
if(a.ccfd<0) |
| 410 |
return; |
| 411 |
if(a.lastccstring == data) |
| 412 |
return; |
| 413 |
char s[4096]; |
| 414 |
int64_t curtime=avm_get_time_us(); |
| 415 |
int frame1=(int) ((a.lastcctime-a.starttime)*a.m_conf.fps/(1000000.)); |
| 416 |
int frame2=(int) ((curtime-a.starttime)*a.m_conf.fps/(1000000.)); |
| 417 |
if(a.lastccstring.size()==0) |
| 418 |
{ |
| 419 |
a.lastcctime=curtime; |
| 420 |
a.lastccstring=data; |
| 421 |
return; |
| 422 |
} |
| 423 |
sprintf(s, "{%d}{%d}", frame1, frame2-1); |
| 424 |
char* ptr=&s[strlen(s)]; |
| 425 |
const char* pp=a.lastccstring.c_str(); |
| 426 |
while(*pp) |
| 427 |
{ |
| 428 |
if(*pp==0xa) |
| 429 |
{ |
| 430 |
*ptr++='|'; |
| 431 |
pp++; |
| 432 |
} |
| 433 |
else |
| 434 |
*ptr++=*pp++; |
| 435 |
} |
| 436 |
*ptr++=0xa; |
| 437 |
*ptr++=0; |
| 438 |
write(a.ccfd, s, strlen(s)); |
| 439 |
// printf("%s", s); |
| 440 |
a.lastccstring=data; |
| 441 |
a.lastcctime=curtime; |
| 442 |
} |
| 443 |
|
| 444 |
void* CaptureProcess::vidcap() |
| 445 |
{ |
| 446 |
const float fps = m_conf.fps; |
| 447 |
cnt=0; |
| 448 |
cap_drop=0; |
| 449 |
|
| 450 |
int w = m_conf.res_w; |
| 451 |
int h = m_conf.res_h; |
| 452 |
int line_stride = w + m_conf.over_l + m_conf.over_r; |
| 453 |
int bpp; |
| 454 |
|
| 455 |
switch (m_conf.colorspace) |
| 456 |
{ |
| 457 |
case cspRGB15: bpp = 2; break; |
| 458 |
case cspRGB24: bpp = 3; break; |
| 459 |
case cspRGB32: bpp = 4; break; |
| 460 |
default: bpp = 1; break; |
| 461 |
} |
| 462 |
|
| 463 |
int start_pad = bpp * (w * m_conf.over_t + m_conf.over_l); |
| 464 |
int end_pad = bpp * (w * m_conf.over_b + m_conf.over_r); |
| 465 |
line_stride *= bpp; |
| 466 |
w *= bpp; |
| 467 |
|
| 468 |
//printf("W: %d Stride %d %d %d %d\n", w, line_stride, bpp, start_pad, m_conf.colorspace); |
| 469 |
|
| 470 |
while (!m_quit) |
| 471 |
{ |
| 472 |
int64_t currenttime = avm_get_time_us(); |
| 473 |
// cerr<<currenttime<<" "<<starttime<<" "<<fps<<std::endl; |
| 474 |
// cerr<<avm_get_time_diff(currenttime, starttime)*fps<<" "<<cnt<<std::endl; |
| 475 |
// double freq=550000.; |
| 476 |
double dist=double(currenttime-starttime)/(1000000.); |
| 477 |
// double dist=avm_get_time_diff(currenttime, starttime); |
| 478 |
// cerr<<dist<<" "<<freq<<std::endl; |
| 479 |
if (dist * fps < cnt || m_qvid.size() >= (m_qvid.capacity() - 1)) |
| 480 |
{ |
| 481 |
avm_usleep(10000); |
| 482 |
// std::cerr<<"Sleeping"<<std::endl; |
| 483 |
continue; |
| 484 |
} |
| 485 |
chunk ch; |
| 486 |
//printf("CONF %d\n", m_conf.colorspace); |
| 487 |
if(dist*fps<(cnt+1)) |
| 488 |
{ |
| 489 |
char* z = m_v4l->grabCapture(false); |
| 490 |
|
| 491 |
//4lw->avicap_renderer_display(); |
| 492 |
|
| 493 |
char* tmpframe = m_pAllocator->alloc(); |
| 494 |
char* zptr, *fptr; |
| 495 |
// printf("%f %x %x\n", dist, z, tmpframe); |
| 496 |
if (tmpframe) |
| 497 |
{ |
| 498 |
int i; |
| 499 |
switch (m_conf.colorspace) |
| 500 |
{ |
| 501 |
case cspRGB15: |
| 502 |
case cspRGB24: |
| 503 |
case cspRGB32: |
| 504 |
default: |
| 505 |
zptr = z + start_pad + (h - 1) * line_stride; |
| 506 |
fptr = tmpframe; |
| 507 |
for (i = 0; i < h; i++) { |
| 508 |
memcpy(fptr, zptr, w); |
| 509 |
fptr += w; |
| 510 |
zptr -= line_stride; |
| 511 |
} |
| 512 |
break; |
| 513 |
case cspYUY2: |
| 514 |
zptr = z + start_pad*2; |
| 515 |
fptr = tmpframe; |
| 516 |
for(i=0; i<h; i++) { |
| 517 |
memcpy(fptr, zptr, w*2); |
| 518 |
fptr += w*2; |
| 519 |
zptr += line_stride*2; |
| 520 |
} |
| 521 |
// memcpy(tmpframe, z, 2*w*h); |
| 522 |
break; |
| 523 |
case cspI420: |
| 524 |
zptr = z + start_pad*3/2; |
| 525 |
fptr = tmpframe; |
| 526 |
for(i=0; i<h; i++) { |
| 527 |
memcpy(fptr, zptr, w*3/2); |
| 528 |
fptr += w*3/2; |
| 529 |
zptr += line_stride*3/2; |
| 530 |
} |
| 531 |
// memcpy(tmpframe, z, 3*w*h/2); |
| 532 |
break; |
| 533 |
case cspYV12: |
| 534 |
// LINUX v4l knows only I420 planar format |
| 535 |
// so let's just swap two last planes |
| 536 |
// and prepare YV12 planar surface |
| 537 |
zptr = z + start_pad; |
| 538 |
fptr = tmpframe; |
| 539 |
|
| 540 |
for(i=0; i<h; i++) { |
| 541 |
memcpy(fptr, zptr, w); |
| 542 |
fptr += w; |
| 543 |
zptr += line_stride; |
| 544 |
} |
| 545 |
zptr += end_pad; |
| 546 |
|
| 547 |
zptr += start_pad/4; |
| 548 |
fptr = tmpframe + (w * h) * 5 / 4; |
| 549 |
for(i=0; i<h; i++) { |
| 550 |
memcpy(fptr, zptr, w/4); |
| 551 |
fptr += w/4; |
| 552 |
zptr += line_stride/4; |
| 553 |
} |
| 554 |
zptr += end_pad / 4; |
| 555 |
|
| 556 |
zptr += start_pad/4; |
| 557 |
fptr = tmpframe + (w * h); |
| 558 |
for(i=0; i<h; i++) { |
| 559 |
memcpy(fptr, zptr, w/4); |
| 560 |
fptr += w/4; |
| 561 |
zptr += line_stride/4; |
| 562 |
} |
| 563 |
/* |
| 564 |
memcpy(tmpframe, z, w*h); |
| 565 |
memcpy(tmpframe + (w * h) * 5 / 4, z + w * h, w * h / 4); |
| 566 |
memcpy(tmpframe + (w * h), z + (w * h) * 5 / 4, w * h / 4); |
| 567 |
*/ |
| 568 |
break; |
| 569 |
} |
| 570 |
} |
| 571 |
ch.data=tmpframe; |
| 572 |
} |
| 573 |
else |
| 574 |
{ |
| 575 |
ch.data=0; |
| 576 |
cap_drop++; |
| 577 |
} |
| 578 |
ch.timestamp=dist; |
| 579 |
ch.size = 0; |
| 580 |
m_qvid.push(ch); |
| 581 |
cnt++; |
| 582 |
// if(cnt%100==0) |
| 583 |
// cerr<<"Capture thread: read "<<cnt<<" frames, dropped "<<cap_drop<<" frames"<<std::endl; |
| 584 |
} |
| 585 |
m_pAllocator->release(); |
| 586 |
fprintf(stderr, "Capture thread exiting\n"); |
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
void* CaptureProcess::audcap() |
| 591 |
{ |
| 592 |
float abps=m_conf.samplesize*m_conf.chan*m_conf.frequency/8; |
| 593 |
int bufsize=0; |
| 594 |
int blocksize=m_pDsp->getBufSize(); |
| 595 |
m_pDsp->synch(); |
| 596 |
audioblock=blocksize; |
| 597 |
while (!m_quit) |
| 598 |
{ |
| 599 |
chunk ch; |
| 600 |
ch.data = new char[audioblock]; |
| 601 |
//printf("ACT %d %d %f\n", audioblock, m_pDsp->getSize(), abps); |
| 602 |
double timestamp_shift=(audioblock+m_pDsp->getSize())/abps; |
| 603 |
if(m_pProgress){ |
| 604 |
m_pProgress->setTimestampShift(timestamp_shift); |
| 605 |
} |
| 606 |
ch.size = m_pDsp->read(ch.data, audioblock); |
| 607 |
int64_t ct = avm_get_time_us(); |
| 608 |
//double freq=550000.; |
| 609 |
|
| 610 |
//printf("ACT %d %d %f %f\n", audioblock, m_pDsp->getSize(), abps, timestamp_shift); |
| 611 |
|
| 612 |
#if 0 |
| 613 |
if(starttime) |
| 614 |
ch.timestamp=double(ct-starttime)/(freq*1000.)-timestamp_shift; |
| 615 |
else |
| 616 |
ch.timestamp=0; |
| 617 |
#else |
| 618 |
#warning "compiling without synchfix!" |
| 619 |
ch.timestamp = bufsize / abps; |
| 620 |
#endif |
| 621 |
|
| 622 |
#ifdef AVICAP_PARANOIA1 |
| 623 |
#if 0 |
| 624 |
char tbuf[300]; |
| 625 |
sprintf(tbuf,"TIME %f %f %f\n", avm_get_time_diff(ct, starttime) - ch.timestamp, ch.timestamp, timestamp_shift); |
| 626 |
ParanoiaLog(tbuf); |
| 627 |
|
| 628 |
sprintf(tbuf,"ch.timestamp=%f bufsize/abps=%f\n",ch.timestamp,bufsize / abps); |
| 629 |
ParanoiaLog(tbuf); |
| 630 |
#endif |
| 631 |
#endif |
| 632 |
|
| 633 |
if (m_qaud.size() < m_qaud.capacity() - 1) |
| 634 |
{ |
| 635 |
m_qaud.push(ch); |
| 636 |
bufsize += ch.size; |
| 637 |
//printf("Blocksze %d %d\n", bufsize, blocksize); |
| 638 |
if(blocksize/abps>.1) |
| 639 |
{ |
| 640 |
avm_usleep(10000); |
| 641 |
} |
| 642 |
} |
| 643 |
//else printf("AUDIO qdrop???\n"); |
| 644 |
//avm_usleep(20000); |
| 645 |
} |
| 646 |
delete m_pDsp; |
| 647 |
m_pDsp = 0; |
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
void* CaptureProcess::writer() |
| 652 |
{ |
| 653 |
avm::IWriteFile* file=0; |
| 654 |
avm::IWriteFile* sfile=0; |
| 655 |
const avm::CodecInfo* ci=0; |
| 656 |
// const CodecInfo* ci=0; //use this, if it does not compile... |
| 657 |
|
| 658 |
// int p = getpriority(PRIO_PROCESS, 0); |
| 659 |
//attention: only root is allowed to lower priority |
| 660 |
// setpriority(PRIO_PROCESS, 0, (p - 3 > -20) ? p - 3 : -20); |
| 661 |
|
| 662 |
avm::IVideoWriteStream* stream; |
| 663 |
avm::IWriteStream* audioStream=0; |
| 664 |
avm::IAudioWriteStream* audioCompStream=0; |
| 665 |
|
| 666 |
uint_t cs; |
| 667 |
switch (m_conf.colorspace) |
| 668 |
{ |
| 669 |
case cspRGB24: |
| 670 |
default: |
| 671 |
cs=24; |
| 672 |
break; |
| 673 |
case cspRGB15: |
| 674 |
cs=15; |
| 675 |
break; |
| 676 |
case cspYUY2: |
| 677 |
cs=fccYUY2; |
| 678 |
break; |
| 679 |
case cspYV12: |
| 680 |
cs=fccYV12; |
| 681 |
break; |
| 682 |
} |
| 683 |
|
| 684 |
avm::BitmapInfo bh(m_conf.res_w, m_conf.res_h, cs); |
| 685 |
if (cs <= 32) |
| 686 |
bh.SetDirection(1); // upside down image |
| 687 |
m_conf.codec.header = bh; |
| 688 |
|
| 689 |
printf("colorspace = %d 0x%x\n", m_conf.colorspace, cs); |
| 690 |
|
| 691 |
uint8_t *buf = NULL; |
| 692 |
// if(m_conf.deinterlace && ((m_conf.colorspace == cspRGB24) || (m_conf.colorspace == cspYUY2))) |
| 693 |
if (m_conf.deinterlace) |
| 694 |
buf = new uint8_t[m_conf.res_w * m_conf.res_h * 3]; |
| 695 |
|
| 696 |
const double fps=m_conf.fps; |
| 697 |
|
| 698 |
//m_conf.filename=find_filename(m_conf.filename); |
| 699 |
if(m_conf.never_overwrite) |
| 700 |
usedname=find_filename(m_conf.filename); |
| 701 |
else |
| 702 |
usedname=m_conf.filename; |
| 703 |
|
| 704 |
try |
| 705 |
{ |
| 706 |
if (m_conf.segment_size == -1) |
| 707 |
//file = avm::CreateWriteFile(m_conf.filename.c_str()); |
| 708 |
file = avm::CreateWriteFile(usedname.c_str()); |
| 709 |
else |
| 710 |
{ |
| 711 |
//sfile = avm::CreateWriteFile(m_conf.filename.c_str(), m_conf.segment_size*1024LL); |
| 712 |
sfile = avm::CreateWriteFile(usedname.c_str(), m_conf.segment_size*1024LL); |
| 713 |
file = sfile; |
| 714 |
} |
| 715 |
// FILE* zz=fopen("bin/uncompr.bmp", "rb"); |
| 716 |
stream = file->AddVideoStream(&m_conf.codec, int(1000000./m_conf.fps)); |
| 717 |
// stream=file->AddStream(AviStream::Video); |
| 718 |
// ve.Init(fccIV50, (const char*)&bh); |
| 719 |
} |
| 720 |
catch (avm::FatalError& e) |
| 721 |
{ |
| 722 |
e.Print(); |
| 723 |
error=e.GetDesc(); |
| 724 |
m_quit=1; |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
float abps=(m_conf.samplesize*m_conf.frequency*m_conf.chan)/8; |
| 729 |
|
| 730 |
WAVEFORMATEX wfm; |
| 731 |
wfm.wFormatTag = (uint16_t)m_conf.audio_compressor;//PCM or MP3 |
| 732 |
wfm.nChannels = (uint16_t)m_conf.chan; |
| 733 |
wfm.nSamplesPerSec=m_conf.frequency; |
| 734 |
//wfm.nSamplesPerSec=frequency * chan; CHECK THIS |
| 735 |
wfm.nAvgBytesPerSec=(int)abps; |
| 736 |
wfm.nBlockAlign = (uint16_t)((m_conf.samplesize*m_conf.chan)/8); |
| 737 |
wfm.wBitsPerSample = (uint16_t)m_conf.samplesize; |
| 738 |
wfm.cbSize=0; |
| 739 |
|
| 740 |
// ve.SetQuality(9500); |
| 741 |
// ve.Start(); |
| 742 |
stream->SetQuality(m_conf.codec.quality); |
| 743 |
stream->Start(); |
| 744 |
fprintf(stderr, "Entering loop\n"); |
| 745 |
// BITMAPINFOHEADER obh=ve.GetOutputFormat(); |
| 746 |
// stream->SetFormat((const char*)&obh, sizeof obh); |
| 747 |
int cnt=0; |
| 748 |
int64_t audiodata=0LL; |
| 749 |
int videodata=0; |
| 750 |
double video_error=0; |
| 751 |
int hide_video=0; |
| 752 |
int dup_video=0; |
| 753 |
double aud_time = 0., vid_time = 0.; |
| 754 |
comp_drop=0; |
| 755 |
for (;;) |
| 756 |
{ |
| 757 |
//printf("SIZE v: %d a: %d %d %f %f\n", m_qvid.size(), m_qaud.size(), m_pAllocator->get_limit(), vid_time, aud_time); |
| 758 |
while (m_qvid.size() > m_qvid.capacity() - 2) |
| 759 |
{ |
| 760 |
chunk& ch = m_qvid.front(); |
| 761 |
vid_time = ch.timestamp; |
| 762 |
cnt++; |
| 763 |
if(ch.data) |
| 764 |
{ |
| 765 |
m_pAllocator->free(ch.data); |
| 766 |
ch.data = 0; |
| 767 |
} |
| 768 |
stream->AddFrame(0); |
| 769 |
videodata++; |
| 770 |
comp_drop++; |
| 771 |
|
| 772 |
if(m_pProgress) |
| 773 |
m_pProgress->update(avm_get_time_us()-starttime, |
| 774 |
vid_time, aud_time, |
| 775 |
audiodata, file->GetFileSize(), |
| 776 |
videodata-cnt, 0, m_pAllocator->used_frames, |
| 777 |
cap_drop, comp_drop, file->GetFileName()); |
| 778 |
m_qvid.pop(); |
| 779 |
} |
| 780 |
if ((m_qvid.size()==0) && (m_qaud.size()==0)) |
| 781 |
{ |
| 782 |
if (m_quit) |
| 783 |
break; |
| 784 |
avm_usleep(10000); |
| 785 |
continue; |
| 786 |
} |
| 787 |
if (m_qaud.size()) |
| 788 |
{ |
| 789 |
if (!audioStream && !audioCompStream) |
| 790 |
{ |
| 791 |
if(m_conf.audio_compressor == 0x55) |
| 792 |
{ |
| 793 |
audioCompStream = file->AddAudioStream(m_conf.audio_compressor, |
| 794 |
&wfm, |
| 795 |
m_conf.audio_bitrate*1000/8 |
| 796 |
); |
| 797 |
audioCompStream->Start(); |
| 798 |
} |
| 799 |
else |
| 800 |
{ |
| 801 |
audioStream=file->AddStream(avm::IStream::Audio, |
| 802 |
&wfm, sizeof(wfm), |
| 803 |
1, //uncompressed PCM data |
| 804 |
(int)abps, //bytes/sec |
| 805 |
(m_conf.samplesize*m_conf.chan)/8 |
| 806 |
//bytes/sample |
| 807 |
); |
| 808 |
} |
| 809 |
} |
| 810 |
chunk& ch = m_qaud.front(); |
| 811 |
aud_time = ch.timestamp; |
| 812 |
audiodata += ch.size; |
| 813 |
if(m_conf.audio_compressor == 0x55){ |
| 814 |
audioCompStream->AddData(ch.data, ch.size); |
| 815 |
}else{ |
| 816 |
audioStream->AddChunk(ch.data, ch.size, audioStream->KEYFRAME); |
| 817 |
} |
| 818 |
double audio_error=audiodata/abps-ch.timestamp; |
| 819 |
if(m_pProgress){ |
| 820 |
m_pProgress->update(avm_get_time_us()-starttime, vid_time, aud_time, |
| 821 |
audiodata, file->GetFileSize(), videodata-cnt, |
| 822 |
-1, m_pAllocator->used_frames, cap_drop, |
| 823 |
comp_drop, file->GetFileName()); |
| 824 |
m_pProgress->setAudioError(audio_error); |
| 825 |
} |
| 826 |
//double audio_error=audiodata/abps-ch.timestamp; |
| 827 |
if(audio_error<video_error-5./fps){ |
| 828 |
hide_video=1; |
| 829 |
#ifdef AVICAP_PARANOIA1 |
| 830 |
ParanoiaLog("hiding frame\n"); |
| 831 |
#endif |
| 832 |
} |
| 833 |
if(audio_error>video_error+5./fps){ |
| 834 |
dup_video=1; |
| 835 |
#ifdef AVICAP_PARANOIA1 |
| 836 |
ParanoiaLog("duplicating frame\n"); |
| 837 |
#endif |
| 838 |
} |
| 839 |
delete[] ch.data; |
| 840 |
ch.data = 0; |
| 841 |
m_qaud.pop(); |
| 842 |
} |
| 843 |
if(m_qvid.size()) |
| 844 |
{ |
| 845 |
chunk& ch = m_qvid.front(); |
| 846 |
vid_time = ch.timestamp; |
| 847 |
if (m_qaud.size() && vid_time < m_qaud.front().timestamp) |
| 848 |
continue; |
| 849 |
cnt++; |
| 850 |
if(!hide_video) |
| 851 |
{ |
| 852 |
videodata++; |
| 853 |
avm::CImage* im = 0; |
| 854 |
size_t uiSize; |
| 855 |
int iKeyframe; |
| 856 |
if (ch.data) { |
| 857 |
if (buf) { |
| 858 |
if (m_conf.colorspace == cspRGB24) |
| 859 |
copy_deinterlace_24(buf, ch.data, 3* m_conf.res_w, m_conf.res_h); |
| 860 |
else if(m_conf.colorspace == cspYUY2) |
| 861 |
deinterlace_bob_yuv_mmx(buf, ch.data, 2 * m_conf.res_w, m_conf.res_h); |
| 862 |
else |
| 863 |
fprintf(stderr, "wrong colorspace\n"); |
| 864 |
|
| 865 |
im = new avm::CImage(&bh, buf, false); |
| 866 |
}else |
| 867 |
im = new avm::CImage(&bh, (unsigned char*)ch.data, false); |
| 868 |
} |
| 869 |
int result = stream->AddFrame(im, &uiSize, &iKeyframe); |
| 870 |
//printf("ADDFRAME %d %d %d\n", cnt, uiSize, iKeyframe); |
| 871 |
// fixme - handle errors |
| 872 |
if (result == 0) |
| 873 |
{ |
| 874 |
uiSize &= ~0x40000000; |
| 875 |
if (iKeyframe) |
| 876 |
uiSize |= 0x40000000; |
| 877 |
} |
| 878 |
if(m_pProgress) |
| 879 |
m_pProgress->update(avm_get_time_us()-starttime, vid_time, aud_time, |
| 880 |
audiodata, file->GetFileSize(), videodata-cnt, |
| 881 |
uiSize, m_pAllocator->used_frames, cap_drop, |
| 882 |
comp_drop, file->GetFileName()); |
| 883 |
if (dup_video) |
| 884 |
{ |
| 885 |
videodata++; |
| 886 |
stream->AddFrame(im, &uiSize); |
| 887 |
if(m_pProgress) |
| 888 |
m_pProgress->update(avm_get_time_us()-starttime, vid_time, aud_time, |
| 889 |
audiodata, file->GetFileSize(), videodata-cnt, |
| 890 |
uiSize, m_pAllocator->used_frames, cap_drop, |
| 891 |
comp_drop, file->GetFileName()); |
| 892 |
video_error+=1./fps; |
| 893 |
} |
| 894 |
if (im) |
| 895 |
im->Release(); |
| 896 |
} |
| 897 |
else |
| 898 |
video_error-=1./fps; |
| 899 |
dup_video=hide_video=0; |
| 900 |
if (m_pProgress) |
| 901 |
m_pProgress->setVideoError(video_error); |
| 902 |
|
| 903 |
if(ch.data) |
| 904 |
{ |
| 905 |
m_pAllocator->free(ch.data); |
| 906 |
ch.data = 0; |
| 907 |
} |
| 908 |
m_qvid.pop(); |
| 909 |
} |
| 910 |
if (segment_flag && sfile) |
| 911 |
{ |
| 912 |
segment_flag = 0; |
| 913 |
sfile->SegmentAtKeyframe(); |
| 914 |
// vid_clear=aud_clear=0; |
| 915 |
} |
| 916 |
if (segmenting_name!=""){ |
| 917 |
sfile->SetSegmentName(segmenting_name); |
| 918 |
segmenting_name=""; |
| 919 |
} |
| 920 |
if (m_conf.timelimit!=-1 |
| 921 |
&& (aud_time>m_conf.timelimit |
| 922 |
|| vid_time>m_conf.timelimit)) |
| 923 |
m_quit = 1; |
| 924 |
|
| 925 |
if (m_conf.sizelimit!=-1 |
| 926 |
&& (file->GetFileSize() > m_conf.sizelimit*1024LL)) |
| 927 |
m_quit = 1; |
| 928 |
} |
| 929 |
|
| 930 |
delete[] buf; |
| 931 |
delete file; |
| 932 |
m_pAllocator->release(); |
| 933 |
|
| 934 |
// if(stream) delete stream; |
| 935 |
// if(audioStream) delete audioStream; |
| 936 |
// if(audioCompStream) delete audioCompStream; |
| 937 |
|
| 938 |
printf("RETURN FROM WRITER\n"); |
| 939 |
return 0; |
| 940 |
} |
| 941 |
|
| 942 |
void CaptureProcess::setMessenger(CaptureProgress* pProgress) |
| 943 |
{ |
| 944 |
m_pProgress = pProgress; |
| 945 |
m_pProgress->init(m_conf.filename.c_str(), m_conf.fps, |
| 946 |
m_conf.res_w, m_conf.res_h, |
| 947 |
m_conf.frequency, m_conf.samplesize, |
| 948 |
m_conf.chan, m_pAllocator->get_limit()); |
| 949 |
} |
| 950 |
|
| 951 |
avm::string CaptureProcess::find_filename(avm::string checkname) |
| 952 |
{ |
| 953 |
FILE *checkfile = fopen(checkname.c_str(),"r"); |
| 954 |
printf("checkfile %s\n",checkname.c_str()); |
| 955 |
|
| 956 |
if(checkfile!=NULL){ |
| 957 |
fclose(checkfile); |
| 958 |
avm::string::size_type st=checkname.find(".avi"); |
| 959 |
if (st == avm::string::npos){ |
| 960 |
checkname += ".avi"; |
| 961 |
} |
| 962 |
else{ |
| 963 |
checkname.insert(st,"_"); |
| 964 |
} |
| 965 |
|
| 966 |
return find_filename(checkname); |
| 967 |
} |
| 968 |
else |
| 969 |
return checkname; |
| 970 |
} |