| 1 |
#include "recompressor.h" |
| 2 |
#include "filters.h" |
| 3 |
|
| 4 |
#include <avm_except.h> |
| 5 |
#include <avm_cpuinfo.h> |
| 6 |
#include <videodecoder.h> |
| 7 |
#include <videoencoder.h> |
| 8 |
#include <utils.h> |
| 9 |
|
| 10 |
#include <unistd.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> // abs |
| 13 |
|
| 14 |
void* RecKernel::recompressThreadStart(void* arg) |
| 15 |
{ |
| 16 |
return ((RecKernel*) arg)->recompressThread(); |
| 17 |
} |
| 18 |
|
| 19 |
void* RecKernel::recompressThread() |
| 20 |
{ |
| 21 |
try |
| 22 |
{ |
| 23 |
m_Mutex.Lock(); |
| 24 |
m_Cond.Broadcast(); |
| 25 |
m_Mutex.Unlock(); |
| 26 |
|
| 27 |
avm::IWriteFile* wf = |
| 28 |
avm::CreateWriteFile(m_Filename.c_str()); |
| 29 |
|
| 30 |
avm::IWriteStream* a_rws = 0; //audio raw write stream |
| 31 |
avm::IWriteStream* v_rws = 0; //video raw write stream |
| 32 |
avm::IAudioWriteStream* aws = 0; |
| 33 |
avm::IVideoWriteStream* vws = 0; |
| 34 |
avm::IReadStream* ars = 0; |
| 35 |
avm::IReadStream* vrs = 0; |
| 36 |
|
| 37 |
/* |
| 38 |
append - next stream |
| 39 |
add next audio channel |
| 40 |
*/ |
| 41 |
|
| 42 |
if (as && m_Streams[as].mode != Remove) |
| 43 |
{ |
| 44 |
ars = m_Streams[as].stream; |
| 45 |
if (m_Streams[as].mode == Recompress) |
| 46 |
{ |
| 47 |
ars->StopStreaming(); |
| 48 |
ars->StartStreaming(); |
| 49 |
//printf("NONREMOVE %d %d\n", as, m_Streams[as].startpos); |
| 50 |
} |
| 51 |
ars->Seek(m_Streams[as].startpos); |
| 52 |
} |
| 53 |
|
| 54 |
if (vs && m_Streams[0].mode != Remove) |
| 55 |
{ |
| 56 |
vrs = m_Streams[0].stream; |
| 57 |
vrs->Seek(m_Streams[0].startpos); |
| 58 |
} |
| 59 |
if (!vrs && !ars) |
| 60 |
return 0; // nothing to do |
| 61 |
|
| 62 |
if (vrs && m_Streams[0].mode == Recompress) |
| 63 |
{ |
| 64 |
vrs->StopStreaming(); |
| 65 |
vrs->StartStreaming(); |
| 66 |
vrs->Seek(vrs->GetPrevKeyFrame(m_Streams[0].startpos)); |
| 67 |
while (vrs->GetPos()<m_Streams[0].startpos && !vrs->Eof()) |
| 68 |
vrs->ReadFrame(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
BITMAPINFOHEADER bh; |
| 73 |
unsigned int videoFrames = 0; |
| 74 |
double videoStartTime = 0.0; |
| 75 |
if (vrs) |
| 76 |
{ |
| 77 |
videoFrames = vrs->GetLength(); |
| 78 |
videoStartTime = vrs->GetTime(); |
| 79 |
unsigned int sz; |
| 80 |
switch (m_Streams[0].mode) |
| 81 |
{ |
| 82 |
case Recompress: |
| 83 |
vrs->GetOutputFormat(&bh, sizeof(bh)); |
| 84 |
try |
| 85 |
{ |
| 86 |
avm::VideoEncoderInfo vi = m_Streams[0].vi; |
| 87 |
|
| 88 |
for (unsigned i = 0; i < getFilterCount(); i++) |
| 89 |
{ |
| 90 |
Filter* fi = getFilterAt(i); |
| 91 |
fi->adjust(bh); |
| 92 |
} |
| 93 |
vi.header = bh; |
| 94 |
printf("Compresor start fourcc: %.4s\n",(char*)& vi.compressor); |
| 95 |
vws = wf->AddVideoStream(&vi, (unsigned int) (1000000.*vrs->GetFrameTime())); |
| 96 |
if (vws) |
| 97 |
vws->Start(); |
| 98 |
|
| 99 |
} |
| 100 |
catch (avm::FatalError& e) |
| 101 |
{ |
| 102 |
e.Print(); |
| 103 |
vws = 0; |
| 104 |
} |
| 105 |
break; |
| 106 |
case Copy: |
| 107 |
sz = vrs->GetVideoFormat(); |
| 108 |
if (sz > 0) |
| 109 |
{ |
| 110 |
char* f = new char[sz]; |
| 111 |
vrs->GetVideoFormat(f, sz); |
| 112 |
v_rws = wf->AddStream(avm::IStream::Video, f, sz, |
| 113 |
((BITMAPINFOHEADER*)f)->biCompression, |
| 114 |
(unsigned int) (1000000.*vrs->GetFrameTime())); |
| 115 |
memcpy(&bh, f, sizeof(bh)); |
| 116 |
delete[] f; |
| 117 |
} |
| 118 |
break; |
| 119 |
case Remove: |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
WAVEFORMATEX wfmtx; |
| 125 |
char* ext = 0; |
| 126 |
|
| 127 |
unsigned int audioSamples = 0; |
| 128 |
double audioStartTime = 0.0; |
| 129 |
memset(&wfmtx, 0, sizeof(wfmtx)); |
| 130 |
uint_t avsz; |
| 131 |
if (ars) |
| 132 |
{ |
| 133 |
audioSamples = ars->GetLength(); |
| 134 |
audioStartTime = ars->GetTime(); |
| 135 |
switch(m_Streams[as].mode) |
| 136 |
{ |
| 137 |
case Recompress: |
| 138 |
ars->GetOutputFormat(&wfmtx, sizeof(wfmtx)); |
| 139 |
aws = wf->AddAudioStream(m_Streams[as].ai.fmt, &wfmtx, |
| 140 |
m_Streams[as].ai.bitrate); |
| 141 |
if (aws) |
| 142 |
aws->Start(); |
| 143 |
//a_rws=wf->AddStream(AviStream::Audio, (char*)&wfmtx, 18, wfmtx.wFormatTag, |
| 144 |
// wfmtx.nAvgBytesPerSec, wfmtx.nBlockAlign); |
| 145 |
break; |
| 146 |
case Copy: |
| 147 |
avsz = ars->GetAudioFormat(); |
| 148 |
ext = new char[avsz]; |
| 149 |
ars->GetAudioFormat(ext, avsz); |
| 150 |
memcpy(&wfmtx, ext, sizeof(wfmtx)); |
| 151 |
if (wfmtx.nBlockAlign < 1) |
| 152 |
wfmtx.nBlockAlign = 1; |
| 153 |
a_rws = wf->AddStream(avm::IStream::Audio, ext, |
| 154 |
18 + wfmtx.cbSize, |
| 155 |
wfmtx.wFormatTag, |
| 156 |
wfmtx.nAvgBytesPerSec, |
| 157 |
wfmtx.nBlockAlign); |
| 158 |
printf("COPY audio stream %p %d\n", a_rws, wfmtx.nBlockAlign); |
| 159 |
delete[] ext; |
| 160 |
break; |
| 161 |
case Remove: |
| 162 |
printf("COPY remove stream\n"); |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
int comp_frame_size = bh.biWidth * labs(bh.biHeight) * 4; |
| 168 |
uint8_t* comp_frame=0; |
| 169 |
if (m_Streams[0].mode != Remove) |
| 170 |
comp_frame = new uint8_t[comp_frame_size]; |
| 171 |
double bpf = 0; |
| 172 |
uint_t zzsize = 44100; |
| 173 |
if(vrs) |
| 174 |
{ |
| 175 |
if(aws) |
| 176 |
bpf=vrs->GetFrameTime()*(wfmtx.nSamplesPerSec*wfmtx.nChannels*wfmtx.wBitsPerSample)/8; |
| 177 |
else |
| 178 |
bpf=vrs->GetFrameTime()*wfmtx.nAvgBytesPerSec; |
| 179 |
zzsize = (uint_t)((1.0/vrs->GetFrameTime()) / 2 * bpf); |
| 180 |
} |
| 181 |
char* zz = new char[zzsize * 2]; |
| 182 |
|
| 183 |
//printf("VWS %p %d %d\n", vws, m_Streams[0].endpos, m_Streams[0].startpos); |
| 184 |
m_pRecCb->setTotal(m_Streams[0].endpos - m_Streams[0].startpos, |
| 185 |
audioSamples, videoStartTime, audioStartTime); |
| 186 |
|
| 187 |
int64_t time_start = avm_get_time_us(); |
| 188 |
int64_t last_progress = 0; |
| 189 |
int64_t time_current = 0; |
| 190 |
framepos_t written_frames = 0; |
| 191 |
size_t written_audio = 0; |
| 192 |
bool nodata = false; |
| 193 |
while (!nodata && rec_status) |
| 194 |
{ |
| 195 |
if (pause_status) |
| 196 |
{ |
| 197 |
int64_t ts = avm_get_time_us(); |
| 198 |
avm_usleep(100000); |
| 199 |
time_start += (avm_get_time_us() - ts); |
| 200 |
continue; |
| 201 |
} |
| 202 |
nodata = true; |
| 203 |
for (unsigned i = as; i < m_Streams.size(); i++) |
| 204 |
{ |
| 205 |
avm::IReadStream* ars = m_Streams[i].stream; |
| 206 |
if (!ars || ars->Eof() || ars->GetPos() >= m_Streams[as].endpos) |
| 207 |
continue; |
| 208 |
nodata = false; |
| 209 |
/* |
| 210 |
if (vrs && (vrs->Eof() || vrs->GetPos() >= m_Streams[0].endpos)) |
| 211 |
{ |
| 212 |
vrs = 0; |
| 213 |
continue; |
| 214 |
} |
| 215 |
*/ |
| 216 |
size_t samples_read, bytes_read; |
| 217 |
|
| 218 |
size_t awritten = 0; |
| 219 |
if (aws) |
| 220 |
{ |
| 221 |
awritten = aws->GetLength(); |
| 222 |
int64_t excess = (int64_t) (bpf * (written_frames + 1) - written_audio); |
| 223 |
//printf("ExcessA %lld, written audio %d, written frames %d, bpf %f\n", excess, written_audio, written_frames, bpf); |
| 224 |
if (vrs) |
| 225 |
{ |
| 226 |
while ((excess>0) && (!ars->Eof())) |
| 227 |
{ |
| 228 |
ars->ReadFrames(zz, (excess>zzsize) ? excess : zzsize, |
| 229 |
zzsize, samples_read, bytes_read); |
| 230 |
written_audio+=bytes_read; |
| 231 |
excess-=bytes_read; |
| 232 |
aws->AddData(zz, bytes_read); |
| 233 |
} |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
ars->ReadFrames(zz, zzsize, zzsize, |
| 238 |
samples_read, bytes_read); |
| 239 |
written_audio+=bytes_read; |
| 240 |
aws->AddData(zz, bytes_read); |
| 241 |
} |
| 242 |
awritten = aws->GetLength() - awritten; |
| 243 |
} |
| 244 |
else if (a_rws) |
| 245 |
{ |
| 246 |
// preload 1 frames |
| 247 |
int64_t excess = (int64_t) (bpf*(written_frames + 1) - written_audio); |
| 248 |
//printf("ExcessB %lld, written audio %d, written frames %d, bpf %f\n", excess, written_audio, written_frames, bpf); |
| 249 |
size_t wa = 0; |
| 250 |
if (excess > 0) |
| 251 |
{ |
| 252 |
awritten = a_rws->GetLength(); |
| 253 |
if (vrs) |
| 254 |
{ |
| 255 |
int flags=0; |
| 256 |
while (wa < zzsize && !ars->Eof()) |
| 257 |
{ |
| 258 |
ars->ReadDirect(zz + wa, zzsize - wa, zzsize - wa, |
| 259 |
samples_read, bytes_read, &flags); |
| 260 |
written_audio += bytes_read; |
| 261 |
wa += bytes_read; |
| 262 |
} |
| 263 |
a_rws->AddChunk(zz, wa, flags); |
| 264 |
//printf("ADDCHUNK %d exc %d\n", wa, excess); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
// excess=m_Streams[vs].endpos-ars->GetTime(); |
| 269 |
// if(excess>sizeof zz)excess=sizeof zz; |
| 270 |
if (m_Streams[as].endpos <= ars->GetPos()) |
| 271 |
excess=0; |
| 272 |
else |
| 273 |
excess=zzsize; |
| 274 |
|
| 275 |
if (excess < wfmtx.nBlockAlign) |
| 276 |
{ |
| 277 |
ars = 0; |
| 278 |
continue; |
| 279 |
} |
| 280 |
if (excess < wfmtx.nBlockAlign) |
| 281 |
{ |
| 282 |
ars = 0; |
| 283 |
continue; |
| 284 |
} |
| 285 |
int flags=0; |
| 286 |
ars->ReadDirect(zz, excess, |
| 287 |
(excess/wfmtx.nBlockAlign)*wfmtx.nBlockAlign, |
| 288 |
samples_read, bytes_read, &flags); |
| 289 |
written_audio += bytes_read; |
| 290 |
excess -= bytes_read; |
| 291 |
a_rws->AddChunk(zz, bytes_read, flags); |
| 292 |
} |
| 293 |
awritten = a_rws->GetLength() - awritten; |
| 294 |
//printf("AWRITTEN %d\n", awritten); |
| 295 |
} |
| 296 |
} |
| 297 |
m_pRecCb->addAudio(ars->GetPos(), awritten, ars->GetTime()); |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
if (vrs) |
| 302 |
{ |
| 303 |
size_t vsize; |
| 304 |
int iskeyframe; |
| 305 |
bool show_progress = false; |
| 306 |
|
| 307 |
time_current = avm_get_time_us(); |
| 308 |
if (avm_get_time_diff(time_current, last_progress) > 1.0) |
| 309 |
{ |
| 310 |
last_progress = time_current; |
| 311 |
show_progress = true; |
| 312 |
} |
| 313 |
|
| 314 |
if (vws) |
| 315 |
{ |
| 316 |
vrs->ReadFrame(); |
| 317 |
avm::CImage* ptr = vrs->GetFrame(); |
| 318 |
|
| 319 |
if (!ptr) |
| 320 |
{ |
| 321 |
printf("WARNING: zero frame\n"); |
| 322 |
vws->AddFrame(0); |
| 323 |
} |
| 324 |
else |
| 325 |
{ |
| 326 |
avm::CImage* im = new avm::CImage(ptr); |
| 327 |
for (unsigned i = 0; i < getFilterCount(); i++) |
| 328 |
{ |
| 329 |
Filter* fi = getFilterAt(i); |
| 330 |
avm::CImage* new_im = fi->process(im, vrs->GetPos()); |
| 331 |
im->Release(); |
| 332 |
im = new_im; |
| 333 |
} |
| 334 |
im->ToRGB(); |
| 335 |
char *b; |
| 336 |
vws->AddFrame(im, &vsize, &iskeyframe, &b); |
| 337 |
if (show_progress && !iskeyframe) |
| 338 |
{ |
| 339 |
show_progress = false; |
| 340 |
last_progress = 0; |
| 341 |
} |
| 342 |
if (_ctl && show_progress) |
| 343 |
{ |
| 344 |
_ctl->setSourcePicture(ptr); |
| 345 |
avm::CImage* new_im = new avm::CImage(im); |
| 346 |
if (new_im) |
| 347 |
{ |
| 348 |
if (m_pRecFilter && m_pRecFilter->vd) |
| 349 |
m_pRecFilter->vd->DecodeFrame(new_im, b, vsize, 0, iskeyframe); |
| 350 |
_ctl->setDestPicture(new_im); |
| 351 |
new_im->Release(); |
| 352 |
} |
| 353 |
} |
| 354 |
im->Release(); |
| 355 |
ptr->Release(); |
| 356 |
} |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
size_t samples_read; |
| 361 |
vrs->ReadDirect(comp_frame, comp_frame_size, 1, |
| 362 |
samples_read, vsize, &iskeyframe); |
| 363 |
//printf("SAMP %d %d %d\n", vrs->GetPos(), samples_read, iskeyframe); |
| 364 |
if (!samples_read) |
| 365 |
{ |
| 366 |
printf("ERROR: Failed to read video frame\n"); |
| 367 |
vrs = 0; |
| 368 |
continue; |
| 369 |
} |
| 370 |
else if (v_rws) |
| 371 |
v_rws->AddChunk(comp_frame, vsize, iskeyframe); |
| 372 |
} |
| 373 |
written_frames++; |
| 374 |
m_pRecCb->addVideo(written_frames, vsize, vrs->GetTime(), iskeyframe); |
| 375 |
if (show_progress) |
| 376 |
{ |
| 377 |
framepos_t mint = vrs->GetLength(); |
| 378 |
if (mint > m_Streams[0].endpos) |
| 379 |
mint = m_Streams[0].endpos; |
| 380 |
double percent=(vrs->GetPos()-m_Streams[0].startpos)/double(mint - m_Streams[0].startpos); |
| 381 |
m_pRecCb->setNewState(percent, |
| 382 |
avm_get_time_diff(time_current, time_start), |
| 383 |
wf->GetFileSize()); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
if (!vrs) |
| 388 |
{ |
| 389 |
time_current = avm_get_time_us(); |
| 390 |
if (avm_get_time_diff(time_current, last_progress) > 1.0) |
| 391 |
{ |
| 392 |
last_progress = time_current; |
| 393 |
framepos_t mint = ars->GetLength(); |
| 394 |
if (mint > m_Streams[as].endpos) |
| 395 |
mint = m_Streams[as].endpos; |
| 396 |
double percent=(ars->GetPos()-m_Streams[as].startpos)/double(mint - m_Streams[as].startpos); |
| 397 |
m_pRecCb->setNewState(percent, |
| 398 |
avm_get_time_diff(time_current, time_start), |
| 399 |
wf->GetFileSize()); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
m_pRecCb->setNewState(1.0, |
| 405 |
avm_get_time_diff(time_current, time_start), |
| 406 |
wf->GetFileSize()); |
| 407 |
m_pRecCb->finished(); |
| 408 |
delete[] zz; |
| 409 |
delete wf; |
| 410 |
return 0; |
| 411 |
|
| 412 |
} |
| 413 |
catch (avm::FatalError& e) |
| 414 |
{ |
| 415 |
e.PrintAll(); |
| 416 |
return 0; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
int RecKernel::startRecompress() |
| 421 |
{ |
| 422 |
m_Mutex.Lock(); |
| 423 |
rec_status=1; |
| 424 |
pause_status=0; |
| 425 |
rec_thread = new avm::PthreadTask(0, recompressThreadStart, this); |
| 426 |
/* waiting for recompress thread startup */ |
| 427 |
m_Cond.Wait(m_Mutex); |
| 428 |
m_Mutex.Unlock(); |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
int RecKernel::pauseRecompress() |
| 433 |
{ |
| 434 |
pause_status = !pause_status; |
| 435 |
return pause_status; |
| 436 |
} |
| 437 |
|
| 438 |
int RecKernel::stopRecompress() |
| 439 |
{ |
| 440 |
rec_status = 0; |
| 441 |
delete rec_thread; |
| 442 |
return 0; |
| 443 |
} |