| 1 |
#include "recompressor.h" |
| 2 |
#include "filters.h" |
| 3 |
#include "conf.h" |
| 4 |
|
| 5 |
#include <infotypes.h> |
| 6 |
#include <avm_cpuinfo.h> |
| 7 |
#include <avm_creators.h> |
| 8 |
#include <avm_except.h> |
| 9 |
#include <utils.h> |
| 10 |
|
| 11 |
#include <qapplication.h> |
| 12 |
#include <qmessagebox.h> |
| 13 |
|
| 14 |
#include <unistd.h> |
| 15 |
#include <string.h> |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
#define DECLARE_REGISTRY_SHORTCUT |
| 19 |
#include <configfile.h> |
| 20 |
|
| 21 |
const char* g_pcProgramName = "avirecompress"; |
| 22 |
|
| 23 |
#define Debug if(0) |
| 24 |
|
| 25 |
#define __MODULE__ "RecompressFilter" |
| 26 |
/************************* |
| 27 |
Work with filters. |
| 28 |
*************************/ |
| 29 |
RecompressFilter::RecompressFilter(const avm::VideoEncoderInfo& in) |
| 30 |
:info(in) |
| 31 |
{ |
| 32 |
ve = 0; |
| 33 |
vd = 0; |
| 34 |
comp_frame = 0; |
| 35 |
//info.header.biHeight = -1; |
| 36 |
printf("RecompressFilter: Create: 0x%x \"%.4s\" name: %s\n", |
| 37 |
in.compressor, (const char*)&in.compressor, info.cname.c_str()); //lazy init - in process |
| 38 |
} |
| 39 |
|
| 40 |
RecompressFilter::~RecompressFilter() |
| 41 |
{ |
| 42 |
Debug printf("RecompressFilter::~RecompressFilter()\n"); |
| 43 |
avm::FreeEncoderVideo(ve); |
| 44 |
avm::FreeDecoderVideo(vd); |
| 45 |
delete[] comp_frame; |
| 46 |
} |
| 47 |
|
| 48 |
avm::CImage* RecompressFilter::process(avm::CImage* im, int pos) |
| 49 |
{ |
| 50 |
avm::BitmapInfo bm(*(im->GetFmt())); |
| 51 |
//printf("***--- PROCESS %d\n", info.header.biHeight); bm.Print(); BitmapInfo(info.header).Print(); |
| 52 |
|
| 53 |
if (labs(bm.biHeight) != labs(info.header.biHeight) |
| 54 |
|| bm.biWidth != info.header.biWidth |
| 55 |
|| bm.biBitCount != info.header.biBitCount |
| 56 |
|| bm.biCompression != info.header.biCompression) |
| 57 |
{ |
| 58 |
try |
| 59 |
{ |
| 60 |
//bm.biCompression = 0; |
| 61 |
//bm.SetSpace(IMG_FMT_YV12); |
| 62 |
bm.SetBits(24); |
| 63 |
bm.SetDirection(true); |
| 64 |
|
| 65 |
//avm::BitmapInfo bh1(info.header); |
| 66 |
//bh1.Print(); |
| 67 |
info.header = bm; |
| 68 |
avm::BitmapInfo bh(bm); |
| 69 |
bh.Print(); |
| 70 |
|
| 71 |
printf("RecompressFilter: Decompress: 0x%x \"%.4s\" Compressor: 0x%x \"%.4s\" %s\n", |
| 72 |
info.header.biCompression, (char*)&info.header.biCompression, |
| 73 |
info.compressor, (char*)&info.compressor, info.cname.c_str()); |
| 74 |
|
| 75 |
avm::FreeDecoderVideo(vd); |
| 76 |
vd = 0; |
| 77 |
avm::FreeEncoderVideo(ve); |
| 78 |
ve = avm::CreateEncoderVideo(info); |
| 79 |
if (!ve) |
| 80 |
throw FATAL(avm::CodecGetError().c_str()); |
| 81 |
ve->SetKeyFrame(1); |
| 82 |
|
| 83 |
const BITMAPINFOHEADER& obh = ve->GetOutputFormat(); |
| 84 |
//printf("OPBEH\n"); BitmapInfo(obh).Print(); |
| 85 |
//vd = CreateVideoDecoder(obh, 24, 1); |
| 86 |
vd = avm::CreateDecoderVideo(obh); |
| 87 |
if (!vd) |
| 88 |
throw FATAL(avm::CodecGetError().c_str()); |
| 89 |
delete[] comp_frame; |
| 90 |
comp_frame = new char[obh.biWidth * labs(obh.biHeight) * 4]; |
| 91 |
vd->Start(); |
| 92 |
} |
| 93 |
catch (avm::FatalError& e) |
| 94 |
{ |
| 95 |
avm::FreeDecoderVideo(vd); |
| 96 |
avm::FreeEncoderVideo(ve); |
| 97 |
delete[] comp_frame; |
| 98 |
vd = 0; |
| 99 |
ve = 0; |
| 100 |
comp_frame = 0; |
| 101 |
e.PrintAll(); |
| 102 |
throw; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
Debug printf("RecompressFilter::process() start\n"); |
| 107 |
if(!ve || !vd) |
| 108 |
{ |
| 109 |
// we tried to start encoding into this format, but failed |
| 110 |
im->AddRef(); |
| 111 |
return im; |
| 112 |
} |
| 113 |
|
| 114 |
ve->Start(); |
| 115 |
int is_keyframe; |
| 116 |
size_t size = 0; |
| 117 |
|
| 118 |
int r = ve->EncodeFrame(im, comp_frame, &is_keyframe, &size); |
| 119 |
ve->Stop(); |
| 120 |
fprintf(stderr, "Encoded to %" PRIsz " bytes, error %d\n", size, r); |
| 121 |
if (r == 0) |
| 122 |
{ |
| 123 |
avm::BitmapInfo bi(vd->GetDestFmt()); |
| 124 |
im = new avm::CImage(&bi); |
| 125 |
//printf("DECOMPRESS\n"); bm.Print(); |
| 126 |
r = vd->DecodeFrame(im, comp_frame, size, 0, 16); |
| 127 |
//vd->Stop(); |
| 128 |
if (r != 0) |
| 129 |
fprintf(stderr, "Decoded result: %d\n", r); |
| 130 |
Debug printf("RecompressFilter::process() stop\n"); |
| 131 |
return im; |
| 132 |
} |
| 133 |
im->AddRef(); |
| 134 |
return im; |
| 135 |
} |
| 136 |
|
| 137 |
QString RecompressFilter::save() |
| 138 |
{ |
| 139 |
return ""; |
| 140 |
} |
| 141 |
|
| 142 |
void RecompressFilter::load(const QString&) |
| 143 |
{ |
| 144 |
} |
| 145 |
|
| 146 |
//creates filter registered under id |
| 147 |
RecKernel::RecKernel() |
| 148 |
:_ctl(0), m_pRecFilter(0), m_pRecCb(0), m_pWriteFile(0), |
| 149 |
vs(0), as(0), rec_status(0), pause_status(0) |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
RecKernel::~RecKernel() |
| 154 |
{ |
| 155 |
delete m_pRecFilter; |
| 156 |
_destruct(); |
| 157 |
} |
| 158 |
|
| 159 |
Filter* RecKernel::getFilter(uint_t id) |
| 160 |
{ |
| 161 |
switch(id) |
| 162 |
{ |
| 163 |
case 0: |
| 164 |
return new GammaFilter(this); |
| 165 |
case 1: |
| 166 |
return new BlurFilter(this); |
| 167 |
case 2: |
| 168 |
return new RefractFilter(this); |
| 169 |
case 3: |
| 170 |
return new NoiseFilter(this); |
| 171 |
case 4: |
| 172 |
return new MoveFilter(this); |
| 173 |
case 5: |
| 174 |
return new ScaleFilter(this); |
| 175 |
/* case 5: |
| 176 |
return new SwapFilter(this);*/ |
| 177 |
default: |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
//returns filter at pos id in current list |
| 184 |
Filter* RecKernel::getFilterAt(uint_t id) |
| 185 |
{ |
| 186 |
if (id >= m_FilterList.size()) |
| 187 |
return 0; |
| 188 |
Filter* fi = m_FilterList[id]; |
| 189 |
fi->addref(); |
| 190 |
|
| 191 |
return fi; |
| 192 |
} |
| 193 |
|
| 194 |
//size of current list |
| 195 |
unsigned int RecKernel::getFilterCount() const |
| 196 |
{ |
| 197 |
return m_FilterList.size(); |
| 198 |
} |
| 199 |
|
| 200 |
//adds filter in the end of list |
| 201 |
int RecKernel::addFilter(Filter* fi) |
| 202 |
{ |
| 203 |
m_FilterList.push_back(fi); |
| 204 |
fi->addref(); |
| 205 |
redraw(); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
int RecKernel::removeFilter(uint_t id) |
| 210 |
{ |
| 211 |
if (id >= m_FilterList.size()) |
| 212 |
{ |
| 213 |
printf("removeFilter: weird id %d\n", id); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
else if (!m_FilterList.size()) |
| 217 |
return 0; |
| 218 |
|
| 219 |
Filter* fi = m_FilterList[id]; |
| 220 |
swap(id, m_FilterList.size() - 1); |
| 221 |
m_FilterList.pop_back(); |
| 222 |
delete fi; |
| 223 |
redraw(); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
int RecKernel::moveUpFilter(uint_t id) |
| 228 |
{ |
| 229 |
if (id >= m_FilterList.size() || id == 0) |
| 230 |
return 0; |
| 231 |
swap(id, id - 1); |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
int RecKernel::moveDownFilter(uint_t id) |
| 237 |
{ |
| 238 |
if (m_FilterList.size() < 1 || id >= (m_FilterList.size()-1)) |
| 239 |
return 0; |
| 240 |
swap(id, id + 1); |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
/* |
| 246 |
* Work with files. |
| 247 |
*/ |
| 248 |
int RecKernel::openFile(const char* fn) |
| 249 |
{ |
| 250 |
if (m_ReadFiles.size()) |
| 251 |
_destruct(); |
| 252 |
try |
| 253 |
{ |
| 254 |
avm::IReadFile* rf = avm::CreateReadFile(fn); |
| 255 |
if (!rf) |
| 256 |
return -1; |
| 257 |
m_ReadFiles.push_back(rf); |
| 258 |
m_ReadFn = fn; |
| 259 |
int i; |
| 260 |
vs = as = 0; |
| 261 |
for(i = 0; i < 16; i++) |
| 262 |
{ |
| 263 |
avm::IReadStream* s = rf->GetStream(i, avm::IStream::Video); |
| 264 |
if (!s) |
| 265 |
break; |
| 266 |
/** |
| 267 |
IV32 has serious problems without this line |
| 268 |
Since we can draw upside-down pictures, who cares. |
| 269 |
**/ |
| 270 |
|
| 271 |
s->SetDirection(true); |
| 272 |
|
| 273 |
full_stream fs; |
| 274 |
fs.stream = s; |
| 275 |
fs.startpos = 0; |
| 276 |
fs.endpos = s->GetLength(); |
| 277 |
fs.mode = Copy; |
| 278 |
|
| 279 |
BITMAPINFOHEADER bh; |
| 280 |
s->GetVideoFormat(&bh, sizeof(bh)); |
| 281 |
fs.vi.compressor = bh.biCompression; |
| 282 |
|
| 283 |
fs.vi.quality = 9500; |
| 284 |
fs.vi.keyfreq = 15; |
| 285 |
avm::StreamInfo* info = s->GetStreamInfo(); |
| 286 |
if (info) |
| 287 |
{ |
| 288 |
fs.vi.quality = info->GetQuality(); |
| 289 |
delete info; |
| 290 |
} |
| 291 |
|
| 292 |
m_Streams.push_back(fs); |
| 293 |
vs++; |
| 294 |
} |
| 295 |
for (i = 0; i < 16; i++) |
| 296 |
{ |
| 297 |
avm::IReadStream* s = rf->GetStream(i, avm::IStream::Audio); |
| 298 |
if (!s) |
| 299 |
break; |
| 300 |
full_stream fs; |
| 301 |
fs.stream = s; |
| 302 |
fs.startpos = 0; |
| 303 |
fs.endpos = s->GetLength(); |
| 304 |
fs.mode = Copy; |
| 305 |
|
| 306 |
WAVEFORMATEX wf; |
| 307 |
s->GetAudioFormat(&wf, sizeof(wf)); |
| 308 |
fs.ai.fmt = 1; |
| 309 |
if (wf.wFormatTag == 0x55) |
| 310 |
{ |
| 311 |
fs.ai.fmt = 0x55; |
| 312 |
fs.ai.bitrate = wf.nAvgBytesPerSec; |
| 313 |
} |
| 314 |
|
| 315 |
m_Streams.push_back(fs); |
| 316 |
as++; |
| 317 |
} |
| 318 |
return 0; |
| 319 |
} |
| 320 |
catch (avm::FatalError& e) |
| 321 |
{ |
| 322 |
strLastError = avm::string(e.GetModule()) + avm::string(": ") + e.GetDesc(); |
| 323 |
return -1; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
int RecKernel::setDestFile(const char* fn) |
| 328 |
{ |
| 329 |
avm::IWriteFile* wf; |
| 330 |
try |
| 331 |
{ |
| 332 |
wf = avm::CreateWriteFile(fn); |
| 333 |
m_Filename = fn; |
| 334 |
delete wf; |
| 335 |
wf = 0; |
| 336 |
return 0; |
| 337 |
} |
| 338 |
catch (avm::FatalError& e) |
| 339 |
{ |
| 340 |
e.Print(); |
| 341 |
return -1; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
//first all video streams, then audio |
| 346 |
avm::string RecKernel::aboutStream(uint_t id) const |
| 347 |
{ |
| 348 |
if (!isStream(id)) |
| 349 |
return ""; |
| 350 |
char s[1024]; |
| 351 |
avm::IReadStream* stream = m_Streams[id].stream; |
| 352 |
if (isAudioStream(id)) |
| 353 |
{ |
| 354 |
WAVEFORMATEX wf; |
| 355 |
stream->GetAudioFormat(&wf, sizeof(wf)); |
| 356 |
const char* enc; |
| 357 |
char dummy[128]; |
| 358 |
const avm::CodecInfo* ci = avm::CodecInfo::match(wf.wFormatTag, avm::CodecInfo::Audio, 0, avm::CodecInfo::Encode); |
| 359 |
if (ci) |
| 360 |
enc = ci->GetName(); |
| 361 |
else |
| 362 |
{ |
| 363 |
sprintf(dummy, "ID %d (%x) (unsupported) ", wf.wFormatTag, |
| 364 |
wf.wFormatTag); |
| 365 |
enc = dummy; |
| 366 |
} |
| 367 |
avm::StreamInfo* info = stream->GetStreamInfo(); |
| 368 |
if (info) |
| 369 |
{ |
| 370 |
sprintf(s, "Audio stream #%d. Average rate: %dkbps (%d bytes/s)\n" |
| 371 |
"Length: %d samples (Sample size %d bytes)\n" |
| 372 |
"Encoding: %s. ( %.2f seconds )", |
| 373 |
(int)id - vs, ((wf.nAvgBytesPerSec * 8 + 500) / 1000), |
| 374 |
wf.nAvgBytesPerSec, |
| 375 |
info->GetStreamFrames(), wf.nBlockAlign, |
| 376 |
enc, info->GetLengthTime()); |
| 377 |
delete info; |
| 378 |
} |
| 379 |
return avm::string(s); |
| 380 |
} |
| 381 |
if (isVideoStream(id)) |
| 382 |
{ |
| 383 |
BITMAPINFOHEADER bh; |
| 384 |
stream->GetVideoFormat(&bh, sizeof(bh)); |
| 385 |
const avm::CodecInfo* ci = avm::CodecInfo::match(bh.biCompression); |
| 386 |
avm::string encoding; |
| 387 |
if (ci) |
| 388 |
encoding = ci->GetName(); |
| 389 |
else |
| 390 |
{ |
| 391 |
char dummy[128]; |
| 392 |
char ft[4]; |
| 393 |
sprintf(dummy, "unsupported %X='%.4s'", bh.biCompression, |
| 394 |
avm_set_le32(ft, bh.biCompression)); |
| 395 |
encoding = dummy; |
| 396 |
} |
| 397 |
avm::StreamInfo* info = stream->GetStreamInfo(); |
| 398 |
if (info) |
| 399 |
{ |
| 400 |
sprintf(s, "Video stream #%d. %dx%d %.2f fps.\n" |
| 401 |
"Length: %d frames ( %.2f seconds ).\nEncoding: %s.", |
| 402 |
(int)id, bh.biWidth, bh.biHeight, |
| 403 |
info->GetFps(), |
| 404 |
info->GetStreamFrames(), |
| 405 |
info->GetLengthTime(), |
| 406 |
encoding.c_str()); |
| 407 |
delete info; |
| 408 |
} |
| 409 |
return avm::string(s); |
| 410 |
} |
| 411 |
return avm::string(""); |
| 412 |
} |
| 413 |
|
| 414 |
avm::string RecKernel::lastError() const |
| 415 |
{ |
| 416 |
return strLastError; |
| 417 |
} |
| 418 |
|
| 419 |
bool RecKernel::isVideoStream(uint_t id) const |
| 420 |
{ |
| 421 |
if (id < m_Streams.size()) |
| 422 |
return (m_Streams[id].stream->GetType() == avm::IStream::Video); |
| 423 |
return false; |
| 424 |
} |
| 425 |
bool RecKernel::isAudioStream(uint_t id) const |
| 426 |
{ |
| 427 |
if (id < m_Streams.size()) |
| 428 |
return (m_Streams[id].stream->GetType() == avm::IStream::Audio); |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
avm::IStream::StreamType RecKernel::getStreamType(uint_t id) const |
| 433 |
{ |
| 434 |
if (id < m_Streams.size()) |
| 435 |
return m_Streams[id].stream->GetType(); |
| 436 |
return avm::IStream::Other; |
| 437 |
} |
| 438 |
|
| 439 |
int RecKernel::getCompress(uint_t id, avm::VideoEncoderInfo& vi) const |
| 440 |
{ |
| 441 |
if (!isVideoStream(id)) |
| 442 |
return -1; |
| 443 |
vi = m_Streams[id].vi; |
| 444 |
return m_Streams[id].stream->GetOutputFormat(&vi.header, sizeof(vi.header)); |
| 445 |
} |
| 446 |
|
| 447 |
int RecKernel::getAudioCompress(uint_t id, AudioEncoderInfo& ai) const |
| 448 |
{ |
| 449 |
if (!isAudioStream(id)) |
| 450 |
return -1; |
| 451 |
ai = m_Streams[id-vs].ai; |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
int RecKernel::setCompress(uint_t id, const avm::VideoEncoderInfo& vi) |
| 456 |
{ |
| 457 |
if (!isVideoStream(id)) |
| 458 |
return -1; |
| 459 |
m_Streams[id].vi = vi; |
| 460 |
try |
| 461 |
{ |
| 462 |
avm::VideoEncoderInfo vinfo; |
| 463 |
RecompressFilter* nrecf = (getCompress(0, vinfo) < 0) |
| 464 |
? 0 : new RecompressFilter(vi); |
| 465 |
if (nrecf) |
| 466 |
{ |
| 467 |
delete m_pRecFilter; |
| 468 |
m_pRecFilter = nrecf; |
| 469 |
} |
| 470 |
} |
| 471 |
catch (avm::FatalError& e) |
| 472 |
{ |
| 473 |
e.Print(); |
| 474 |
} |
| 475 |
redraw(); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
int RecKernel::setAudioCompress(uint_t id, const AudioEncoderInfo& ai) |
| 480 |
{ |
| 481 |
if (!isAudioStream(id)) |
| 482 |
return -1; |
| 483 |
m_Streams[id - vs].ai = ai; |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
//nonzero if stream |
| 488 |
bool RecKernel::isStream(uint_t id) const |
| 489 |
{ |
| 490 |
return (id < m_Streams.size()); |
| 491 |
} |
| 492 |
|
| 493 |
int RecKernel::setStreamMode(uint_t id, KernelMode mode) |
| 494 |
{ |
| 495 |
if (!isStream(id)) |
| 496 |
return -1; |
| 497 |
m_Streams[id].mode = mode; |
| 498 |
redraw(); |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
int RecKernel::getStreamMode(uint_t id, KernelMode& mode) const |
| 503 |
{ |
| 504 |
if (!isStream(id)) |
| 505 |
return -1; |
| 506 |
mode = m_Streams[id].mode; |
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
int RecKernel::getSelection(uint_t id, framepos_t& start, framepos_t& end) const |
| 511 |
{ |
| 512 |
if (!isStream(id)) |
| 513 |
return -1; |
| 514 |
start = m_Streams[id].startpos; |
| 515 |
end = m_Streams[id].endpos; |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
int RecKernel::setSelection(uint_t id, framepos_t start, framepos_t end) |
| 520 |
{ |
| 521 |
if (!isStream(id)) |
| 522 |
return -1; |
| 523 |
m_Streams[id].startpos = start; |
| 524 |
m_Streams[id].endpos = end; |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
int RecKernel::setSelectionStart(uint_t id, framepos_t start) |
| 529 |
{ |
| 530 |
if (!isStream(id)) |
| 531 |
return -1; |
| 532 |
m_Streams[id].startpos = start; |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
int RecKernel::setSelectionEnd(uint_t id, framepos_t end) |
| 537 |
{ |
| 538 |
if (!isStream(id)) |
| 539 |
return -1; |
| 540 |
m_Streams[id].endpos = end; |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
double RecKernel::getFrameTime(uint_t id) const |
| 545 |
{ |
| 546 |
if (!isStream(id)) |
| 547 |
return 0; |
| 548 |
return m_Streams[id].stream->GetFrameTime(); |
| 549 |
} |
| 550 |
|
| 551 |
double RecKernel::getTime(uint_t id) const |
| 552 |
{ |
| 553 |
if (!isStream(id)) |
| 554 |
return 0; |
| 555 |
|
| 556 |
return m_Streams[id].stream->GetTime(); |
| 557 |
} |
| 558 |
|
| 559 |
//should set size & pictures if ctl!=0 & file is opened |
| 560 |
void RecKernel::setImageControl(IImageControl* ctl) |
| 561 |
{ |
| 562 |
if (vs) |
| 563 |
{ |
| 564 |
try |
| 565 |
{ |
| 566 |
m_Streams[0].stream->StopStreaming(); |
| 567 |
m_Streams[0].stream->StartStreaming(); |
| 568 |
m_Streams[0].stream->Seek((framepos_t) 0); |
| 569 |
BITMAPINFOHEADER bh; |
| 570 |
m_Streams[0].stream->GetVideoFormat(&bh, sizeof(bh)); |
| 571 |
_ctl = ctl; |
| 572 |
_ctl->setSize(bh.biWidth, bh.biHeight); |
| 573 |
|
| 574 |
delete m_pRecFilter; |
| 575 |
avm::VideoEncoderInfo info; |
| 576 |
|
| 577 |
m_pRecFilter = (getCompress(0, info) < 0) |
| 578 |
? 0 : new RecompressFilter(info); |
| 579 |
|
| 580 |
redraw(true); |
| 581 |
} |
| 582 |
catch (avm::FatalError& e) |
| 583 |
{ |
| 584 |
e.Print(); |
| 585 |
} |
| 586 |
} |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
avm::string RecKernel::getStreamName(uint_t id) const |
| 591 |
{ |
| 592 |
avm::string s; |
| 593 |
|
| 594 |
if (isStream(id)) |
| 595 |
{ |
| 596 |
if (isAudioStream(id)) |
| 597 |
{ |
| 598 |
s = "Audio"; |
| 599 |
id -= vs; |
| 600 |
} |
| 601 |
else |
| 602 |
s = "Video"; |
| 603 |
char q[64]; |
| 604 |
sprintf(q, " #%d", id); |
| 605 |
s += q; |
| 606 |
} |
| 607 |
return s; |
| 608 |
} |
| 609 |
|
| 610 |
unsigned int RecKernel::getStreamCount() const |
| 611 |
{ |
| 612 |
return m_Streams.size(); |
| 613 |
} |
| 614 |
|
| 615 |
//in frames |
| 616 |
//negative seeks are very slow |
| 617 |
// FIXME: implement frame caching : store frame every second |
| 618 |
|
| 619 |
framepos_t RecKernel::seek(int delta) |
| 620 |
{ |
| 621 |
if (!vs) |
| 622 |
return 0; |
| 623 |
|
| 624 |
avm::IReadStream* stream = m_Streams[0].stream; |
| 625 |
|
| 626 |
framepos_t cur_pos = stream->GetPos(); |
| 627 |
framepos_t endpos = stream->GetLength(); |
| 628 |
framepos_t newpos = cur_pos + delta; |
| 629 |
|
| 630 |
if (delta < 0 && cur_pos < (unsigned) -delta) |
| 631 |
newpos = 0; |
| 632 |
|
| 633 |
if (newpos >= endpos) |
| 634 |
newpos = endpos; |
| 635 |
|
| 636 |
if (newpos == cur_pos) |
| 637 |
return cur_pos; |
| 638 |
|
| 639 |
framepos_t next_kern = stream->GetPrevKeyFrame(newpos); |
| 640 |
|
| 641 |
//cout << "pos:" << cur_pos << " prev_keyfr:" << next_kern << " d:" << delta << " newpos:" << newpos << endl; |
| 642 |
|
| 643 |
if (delta < 0 || next_kern > cur_pos) |
| 644 |
stream->Seek(next_kern); |
| 645 |
|
| 646 |
framepos_t lpos = ~0U, prevp = ~0U; |
| 647 |
int i = 5; |
| 648 |
while (i > 0) // avoid deadlock if can't seek |
| 649 |
{ |
| 650 |
lpos = stream->GetPos(); |
| 651 |
//printf("pos: %d newpos: %d %d %d del:%d\n", lpos, newpos, prevp, cur_pos, delta); |
| 652 |
if ((lpos + 1) >= newpos || lpos == stream->ERR) |
| 653 |
break; |
| 654 |
|
| 655 |
stream->ReadFrame(false); |
| 656 |
|
| 657 |
// some error could happen - prevent deadlock loop |
| 658 |
// we are checking if the position is changing |
| 659 |
if (prevp == lpos) |
| 660 |
i--; |
| 661 |
prevp = lpos; |
| 662 |
} |
| 663 |
redraw(true); |
| 664 |
return lpos; |
| 665 |
} |
| 666 |
|
| 667 |
//seek to nearest keyframe, return its position |
| 668 |
framepos_t RecKernel::seekPos(framepos_t fpos) |
| 669 |
{ |
| 670 |
if (!vs) |
| 671 |
return fpos; |
| 672 |
if (fpos >= m_Streams[0].stream->GetLength()) |
| 673 |
fpos = m_Streams[0].stream->GetLength() - 1; |
| 674 |
|
| 675 |
framepos_t r = m_Streams[0].stream->SeekToKeyFrame(fpos); |
| 676 |
redraw(true); |
| 677 |
|
| 678 |
return m_Streams[0].stream->GetPos(); |
| 679 |
/* if(m_Streams.size()==0)return fpos; |
| 680 |
if(vs==0) |
| 681 |
{ |
| 682 |
for(int i=0; i<m_Streams.size(); i++) |
| 683 |
m_Streams[i].stream->Seek(fpos); |
| 684 |
return fpos; |
| 685 |
} |
| 686 |
else |
| 687 |
{ |
| 688 |
fpos=m_Streams[0].stream->SeekToKeyframe(fpos); |
| 689 |
for(int i=1; i<m_Streams.size(); i++) |
| 690 |
m_Streams[i].stream->Seek(fpos); |
| 691 |
redraw(); |
| 692 |
return fpos; |
| 693 |
} |
| 694 |
*/ |
| 695 |
} |
| 696 |
|
| 697 |
framepos_t RecKernel::seekNextKeyFrame() |
| 698 |
{ |
| 699 |
if (!vs) |
| 700 |
return 0; |
| 701 |
|
| 702 |
avm::IReadStream* stream = m_Streams[0].stream; |
| 703 |
framepos_t op = pos(); |
| 704 |
|
| 705 |
//cout << "POS1 " << pos() << endl; |
| 706 |
if (stream->SeekToNextKeyFrame() != stream->ERR) |
| 707 |
redraw(true); |
| 708 |
return pos(); |
| 709 |
} |
| 710 |
|
| 711 |
framepos_t RecKernel::seekPrevKeyFrame() |
| 712 |
{ |
| 713 |
if (!vs) |
| 714 |
return 0; |
| 715 |
|
| 716 |
avm::IReadStream* stream = m_Streams[0].stream; |
| 717 |
framepos_t op = pos(); |
| 718 |
|
| 719 |
if (stream->SeekToPrevKeyFrame() != stream->ERR) |
| 720 |
redraw(true); |
| 721 |
|
| 722 |
return pos(); |
| 723 |
} |
| 724 |
|
| 725 |
framepos_t RecKernel::pos() const |
| 726 |
{ |
| 727 |
return (vs) ? m_Streams[0].stream->GetPos() : 0; |
| 728 |
} |
| 729 |
|
| 730 |
unsigned int RecKernel::getVideoLength() const |
| 731 |
{ |
| 732 |
if (!vs) |
| 733 |
return 0; |
| 734 |
return (unsigned int) m_Streams[0].stream->GetLength(); |
| 735 |
} |
| 736 |
|
| 737 |
/*********************************** |
| 738 |
|
| 739 |
Actual recompression |
| 740 |
|
| 741 |
************************************/ |
| 742 |
|
| 743 |
int RecKernel::setCallback(IRecompressCallback* pRecCallback) |
| 744 |
{ |
| 745 |
m_pRecCb = pRecCallback; |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
void RecKernel::_destruct() |
| 750 |
{ |
| 751 |
//cout << "RecKernel::_destruct()" << endl; |
| 752 |
m_Streams.clear(); |
| 753 |
while (m_ReadFiles.size()) |
| 754 |
{ |
| 755 |
delete m_ReadFiles.back(); |
| 756 |
m_ReadFiles.pop_back(); |
| 757 |
} |
| 758 |
delete m_pWriteFile; |
| 759 |
m_pWriteFile = 0; |
| 760 |
} |
| 761 |
|
| 762 |
void RecKernel::redraw(bool readFrame) |
| 763 |
{ |
| 764 |
if (!vs || !_ctl) |
| 765 |
return; |
| 766 |
|
| 767 |
avm::CImage* pix = m_Streams[0].stream->GetFrame(readFrame); |
| 768 |
|
| 769 |
if (!pix) |
| 770 |
{ |
| 771 |
///printf("ERROR: zero frame\n"); |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
_ctl->setSourcePicture(pix); |
| 776 |
|
| 777 |
avm::CImage* im = pix; |
| 778 |
if (m_pRecFilter) |
| 779 |
{ |
| 780 |
KernelMode m; |
| 781 |
if (getStreamMode(0, m) == 0) |
| 782 |
{ |
| 783 |
// show compressed image only when recompress is enabled |
| 784 |
if (m == RecKernel::Recompress) |
| 785 |
{ |
| 786 |
im = new avm::CImage(pix); |
| 787 |
for (unsigned i = 0; i < getFilterCount(); i++) |
| 788 |
{ |
| 789 |
Filter* fi = getFilterAt(i); |
| 790 |
avm::CImage* new_im = fi->process(im, m_Streams[0].stream->GetPos()); |
| 791 |
im->Release(); |
| 792 |
im = new_im; |
| 793 |
} |
| 794 |
im->ToRGB(); |
| 795 |
try |
| 796 |
{ |
| 797 |
avm::CImage* new_im = m_pRecFilter->process(im, 0); |
| 798 |
if (new_im) |
| 799 |
{ |
| 800 |
im->Release(); |
| 801 |
im = new_im; |
| 802 |
} |
| 803 |
} |
| 804 |
catch (avm::FatalError& e) |
| 805 |
{ |
| 806 |
QMessageBox::information(0, "Error", "Cannot recompress into this format"); |
| 807 |
} |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
//printf("BEFORE SETDEST\n"); im->GetFmt()->Print(); |
| 813 |
_ctl->setDestPicture(im); |
| 814 |
im->Release(); |
| 815 |
if (pix != im) |
| 816 |
pix->Release(); |
| 817 |
} |
| 818 |
|
| 819 |
int RecKernel::loadConfig(const char* fn) |
| 820 |
{ |
| 821 |
ReadConfig cfg(fn); |
| 822 |
// cfg.Add("SrcFile", m_ReadFn); |
| 823 |
avm::string src = cfg.getString("SrcFile"); |
| 824 |
printf("src: %s\n", src.c_str()); |
| 825 |
openFile(src.c_str()); |
| 826 |
avm::string dest = cfg.getString("DestFile"); |
| 827 |
setDestFile(dest.c_str()); |
| 828 |
|
| 829 |
// cfg.Add("VideoStreams", vs); |
| 830 |
// cfg.Add("AudioStreams", as); |
| 831 |
char s[128]; |
| 832 |
for (unsigned i=0; i<m_Streams.size(); i++) |
| 833 |
{ |
| 834 |
sprintf(s, "Stream %d start", i); |
| 835 |
m_Streams[i].startpos = cfg.getInt(s); |
| 836 |
sprintf(s, "Stream %d end", i); |
| 837 |
m_Streams[i].endpos = cfg.getInt(s); |
| 838 |
sprintf(s, "Stream %d mode", i); |
| 839 |
m_Streams[i].mode = (KernelMode)cfg.getInt(s); |
| 840 |
if (i < vs) |
| 841 |
{ |
| 842 |
sprintf(s, "Stream %d quality", i); |
| 843 |
m_Streams[i].vi.quality = cfg.getInt(s); |
| 844 |
sprintf(s, "Stream %d keyfreq", i); |
| 845 |
m_Streams[i].vi.keyfreq = cfg.getInt(s); |
| 846 |
sprintf(s, "Stream %d compressor", i); |
| 847 |
m_Streams[i].vi.compressor = cfg.getInt(s); |
| 848 |
sprintf(s, "Stream %d compressor name", i); |
| 849 |
m_Streams[i].vi.cname = cfg.getString(s); |
| 850 |
// m_Streams[i].stream->GetOutputFormat(&m_VideoFmt[i].header, sizeof m_VideoFmt[i].header); |
| 851 |
} |
| 852 |
else |
| 853 |
{ |
| 854 |
sprintf(s, "Stream %d fmt", i); |
| 855 |
m_Streams[i - vs].ai.fmt = cfg.getInt(s); |
| 856 |
sprintf(s, "Stream %d bitrate", i); |
| 857 |
m_Streams[i - vs].ai.bitrate = cfg.getInt(s); |
| 858 |
} |
| 859 |
} |
| 860 |
int filters = cfg.getInt("Filters"); |
| 861 |
// cfg.Add("Filters", m_FilterList.size()); |
| 862 |
for(int i=0; i<filters; i++) |
| 863 |
{ |
| 864 |
sprintf(s, "Filter %d", i); |
| 865 |
Filter* f = getFilter(cfg.getInt(s)); |
| 866 |
if(!f)break; |
| 867 |
sprintf(s, "Filter %d info", i); |
| 868 |
f->load(cfg.getString(s)); |
| 869 |
addFilter(f); |
| 870 |
} |
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
int RecKernel::saveConfig(const char* fn) |
| 875 |
{ |
| 876 |
if (!m_ReadFiles.size()) |
| 877 |
return 0; |
| 878 |
WriteConfig cfg(fn); |
| 879 |
cfg.add("SrcFile", m_ReadFn); |
| 880 |
cfg.add("DestFile", m_Filename); |
| 881 |
cfg.add("VideoStreams", (int)vs); |
| 882 |
cfg.add("AudioStreams", (int)as); |
| 883 |
|
| 884 |
char s[128]; |
| 885 |
for (unsigned i = 0; i < m_Streams.size(); i++) |
| 886 |
{ |
| 887 |
sprintf(s, "Stream %d start", i); |
| 888 |
cfg.add(s, (int)m_Streams[i].startpos); |
| 889 |
sprintf(s, "Stream %d end", i); |
| 890 |
cfg.add(s, (int)m_Streams[i].endpos); |
| 891 |
sprintf(s, "Stream %d mode", i); |
| 892 |
cfg.add(s, (int)m_Streams[i].mode); |
| 893 |
if(i < vs) |
| 894 |
{ |
| 895 |
sprintf(s, "Stream %d quality", i); |
| 896 |
cfg.add(s, m_Streams[i].vi.quality); |
| 897 |
sprintf(s, "Stream %d keyfreq", i); |
| 898 |
cfg.add(s, m_Streams[i].vi.keyfreq); |
| 899 |
sprintf(s, "Stream %d compressor", i); |
| 900 |
cfg.add(s, (int)m_Streams[i].vi.compressor); |
| 901 |
sprintf(s, "Stream %d compressor name", i); |
| 902 |
cfg.add(s, m_Streams[i].vi.cname.c_str()); |
| 903 |
} |
| 904 |
else |
| 905 |
{ |
| 906 |
sprintf(s, "Stream %d fmt", i); |
| 907 |
cfg.add(s, m_Streams[i-vs].ai.fmt); |
| 908 |
sprintf(s, "Stream %d bitrate", i); |
| 909 |
cfg.add(s, m_Streams[i-vs].ai.bitrate); |
| 910 |
} |
| 911 |
} |
| 912 |
cfg.add("Filters", (int)m_FilterList.size()); |
| 913 |
for (unsigned i = 0; i < m_FilterList.size(); i++) |
| 914 |
{ |
| 915 |
sprintf(s, "Filter %d", i); |
| 916 |
cfg.add(s, m_FilterList[i]->id()); |
| 917 |
sprintf(s, "Filter %d info", i); |
| 918 |
cfg.add(s, m_FilterList[i]->save().ascii()); |
| 919 |
} |
| 920 |
return 0; |
| 921 |
} |