| 1 | #include "PrePostRollFlagger.h" |
| 2 | |
| 3 | // MythTV headers |
| 4 | #include "mythcontext.h" |
| 5 | #include "programinfo.h" |
| 6 | #include "NuppelVideoPlayer.h" |
| 7 | |
| 8 | PrePostRollFlagger::PrePostRollFlagger(SkipType commDetectMethod, bool showProgress_in, |
| 9 | bool fullSpeed_in, NuppelVideoPlayer* nvp, |
| 10 | const QDateTime& startedAt_in, |
| 11 | const QDateTime& stopsAt_in, |
| 12 | const QDateTime& recordingStartedAt_in, |
| 13 | const QDateTime& recordingStopsAt_in): |
| 14 | ClassicCommDetector( commDetectMethod, showProgress_in, fullSpeed_in, nvp, |
| 15 | startedAt_in, stopsAt_in, |
| 16 | recordingStartedAt_in, recordingStopsAt_in), |
| 17 | myTotalFrames(0), closestAfterPre(0), |
| 18 | closestBeforePre(0), closestAfterPost(0), |
| 19 | closestBeforePost(0) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void PrePostRollFlagger::Init() |
| 24 | { |
| 25 | ClassicCommDetector::Init(); |
| 26 | nvp->SetExactSeeks(true); |
| 27 | } |
| 28 | |
| 29 | bool PrePostRollFlagger::go() |
| 30 | { |
| 31 | nvp->SetNullVideo(); |
| 32 | |
| 33 | int secsSince = 0; |
| 34 | int requiredBuffer = 120; |
| 35 | int requiredHeadStart = requiredBuffer; |
| 36 | bool wereRecording = stillRecording; |
| 37 | |
| 38 | |
| 39 | secsSince = startedAt.secsTo(QDateTime::currentDateTime()); |
| 40 | while (stillRecording && (secsSince < requiredHeadStart)) |
| 41 | { |
| 42 | emit statusUpdate("Waiting to pass preroll + head start"); |
| 43 | |
| 44 | emit breathe(); |
| 45 | if (m_bStop) |
| 46 | return false; |
| 47 | |
| 48 | sleep(5); |
| 49 | secsSince = startedAt.secsTo(QDateTime::currentDateTime()); |
| 50 | } |
| 51 | |
| 52 | if (nvp->OpenFile() < 0) |
| 53 | return false; |
| 54 | |
| 55 | Init(); |
| 56 | |
| 57 | |
| 58 | // Don't bother flagging short ~realtime recordings |
| 59 | if ((wereRecording) && (!stillRecording) && (secsSince < requiredHeadStart)) |
| 60 | return false; |
| 61 | |
| 62 | aggressiveDetection = gContext->GetNumSetting("AggressiveCommDetect", 1); |
| 63 | |
| 64 | if (!nvp->InitVideo()) |
| 65 | { |
| 66 | VERBOSE(VB_IMPORTANT, |
| 67 | "NVP: Unable to initialize video for FlagCommercials."); |
| 68 | return false; |
| 69 | } |
| 70 | nvp->SetCaptionsEnabled(false); |
| 71 | |
| 72 | emit breathe(); |
| 73 | if (m_bStop) |
| 74 | return false; |
| 75 | |
| 76 | QTime flagTime; |
| 77 | flagTime.start(); |
| 78 | |
| 79 | if (recordingStopsAt < QDateTime::currentDateTime() ) |
| 80 | myTotalFrames = nvp->GetTotalFrameCount(); |
| 81 | else |
| 82 | myTotalFrames = (long long)(nvp->GetFrameRate() * |
| 83 | (recordingStartedAt.secsTo(recordingStopsAt))); |
| 84 | |
| 85 | |
| 86 | |
| 87 | if (showProgress) |
| 88 | { |
| 89 | if (myTotalFrames) |
| 90 | cerr << " 0%/ "; |
| 91 | else |
| 92 | cerr << " 0/ "; |
| 93 | cerr.flush(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | float flagFPS; |
| 98 | float aspect = nvp->GetVideoAspect(); |
| 99 | |
| 100 | SetVideoParams(aspect); |
| 101 | |
| 102 | emit breathe(); |
| 103 | |
| 104 | long long stopFrame = preRoll + fps * 120; //look up to 2 minutes past |
| 105 | long long framesToProcess = 0; |
| 106 | if(preRoll) |
| 107 | framesToProcess += stopFrame; |
| 108 | if(postRoll) |
| 109 | framesToProcess += myTotalFrames - postRoll + fps * 120; //guess two minutes before |
| 110 | |
| 111 | |
| 112 | long long framesProcessed = 0; |
| 113 | if(preRoll > 0) { |
| 114 | |
| 115 | //check from preroll after |
| 116 | VERBOSE(VB_COMMFLAG, QString("Finding closest after preroll(%1-%2)").arg(preRoll).arg(stopFrame)); |
| 117 | closestAfterPre = findBreakInrange(preRoll, stopFrame, framesToProcess, framesProcessed, flagTime, false); |
| 118 | VERBOSE(VB_COMMFLAG, QString("Closest after preroll: %1").arg(closestAfterPre)); |
| 119 | |
| 120 | |
| 121 | //check before preroll |
| 122 | long long startFrame = 0; |
| 123 | if(closestAfterPre) |
| 124 | startFrame = preRoll - (closestAfterPre - preRoll) - 1; |
| 125 | |
| 126 | VERBOSE(VB_COMMFLAG, QString("Finding before preroll (%1-%2)").arg(startFrame).arg(preRoll)); |
| 127 | closestBeforePre = findBreakInrange(startFrame, preRoll, framesToProcess, framesProcessed, flagTime, true); |
| 128 | VERBOSE(VB_COMMFLAG, QString("Closest before preroll: %1").arg(closestBeforePre)); |
| 129 | |
| 130 | if(closestBeforePre || closestAfterPre) |
| 131 | emit gotNewCommercialBreakList(); |
| 132 | |
| 133 | framesToProcess -= (stopFrame - framesProcessed); // for better processing |
| 134 | |
| 135 | } |
| 136 | |
| 137 | if(stillRecording) { |
| 138 | while (QDateTime::currentDateTime() <= recordingStopsAt) |
| 139 | { |
| 140 | emit breathe(); |
| 141 | if (m_bStop) |
| 142 | return false; |
| 143 | emit statusUpdate("Waiting for recording to finish"); |
| 144 | sleep(5); |
| 145 | } |
| 146 | stillRecording = false; |
| 147 | myTotalFrames = nvp->GetTotalFrameCount(); |
| 148 | } |
| 149 | |
| 150 | if(postRoll > 0) { |
| 151 | //check from preroll after |
| 152 | long long postRollStartLoc = myTotalFrames - postRoll; |
| 153 | VERBOSE(VB_COMMFLAG, QString("Finding closest after postroll(%1-%2)").arg(postRollStartLoc).arg(myTotalFrames)); |
| 154 | closestAfterPost = findBreakInrange(postRollStartLoc, myTotalFrames, framesToProcess, framesProcessed, flagTime, false); |
| 155 | VERBOSE(VB_COMMFLAG, QString("Closest after postRoll: %1").arg(closestAfterPost)); |
| 156 | |
| 157 | //check before preroll |
| 158 | long long startFrame = 0; |
| 159 | if(closestAfterPost) |
| 160 | startFrame = postRollStartLoc - (closestAfterPost - postRollStartLoc) - 1; |
| 161 | |
| 162 | VERBOSE(VB_COMMFLAG, QString("finding closest before preroll(%1-%2)").arg(startFrame).arg(postRollStartLoc)); |
| 163 | closestBeforePost = findBreakInrange(startFrame, postRollStartLoc, framesToProcess, framesProcessed, flagTime, true); |
| 164 | VERBOSE(VB_COMMFLAG, QString("Closest before postroll: %1").arg(closestBeforePost)); |
| 165 | |
| 166 | framesToProcess = framesProcessed; |
| 167 | } |
| 168 | |
| 169 | if (showProgress) |
| 170 | { |
| 171 | float elapsed = flagTime.elapsed() / 1000.0; |
| 172 | |
| 173 | if (elapsed) |
| 174 | flagFPS = framesProcessed / elapsed; |
| 175 | else |
| 176 | flagFPS = 0.0; |
| 177 | |
| 178 | if (myTotalFrames) |
| 179 | cerr << "\b\b\b\b\b\b \b\b\b\b\b\b"; |
| 180 | else |
| 181 | cerr << "\b\b\b\b\b\b\b\b\b\b\b\b\b " |
| 182 | "\b\b\b\b\b\b\b\b\b\b\b\b\b"; |
| 183 | cerr.flush(); |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | long long PrePostRollFlagger::findBreakInrange(long long startFrame, long long stopFrame, |
| 191 | long long totalFrames, long long &framesProcessed, |
| 192 | QTime &flagTime, bool findLast) { |
| 193 | float flagFPS; |
| 194 | int requiredBuffer = 30; |
| 195 | long long currentFrameNumber; |
| 196 | |
| 197 | if(startFrame > 0) |
| 198 | startFrame--; |
| 199 | else |
| 200 | startFrame = 0; |
| 201 | |
| 202 | long long tmpStartFrame = startFrame; |
| 203 | nvp->SetExactSeeks(true); |
| 204 | VideoFrame* f = nvp->GetRawVideoFrame(tmpStartFrame); |
| 205 | float aspect = nvp->GetVideoAspect(); |
| 206 | currentFrameNumber = f->frameNumber; |
| 207 | VERBOSE(VB_COMMFLAG, QString("Starting with frame %1").arg(currentFrameNumber)); |
| 208 | nvp->DiscardVideoFrame(f); |
| 209 | |
| 210 | //some times we have to call twice to seek to the right spot |
| 211 | tmpStartFrame = startFrame; |
| 212 | f = nvp->GetRawVideoFrame(tmpStartFrame); |
| 213 | aspect = nvp->GetVideoAspect(); |
| 214 | currentFrameNumber = f->frameNumber; |
| 215 | VERBOSE(VB_COMMFLAG, QString("Starting with frame(2) %1").arg(currentFrameNumber)); |
| 216 | nvp->DiscardVideoFrame(f); |
| 217 | |
| 218 | long long foundFrame = 0; |
| 219 | |
| 220 | while (!nvp->GetEof()) |
| 221 | { |
| 222 | struct timeval startTime; |
| 223 | if (stillRecording) |
| 224 | gettimeofday(&startTime, NULL); |
| 225 | |
| 226 | VideoFrame* currentFrame = nvp->GetRawVideoFrame(); |
| 227 | currentFrameNumber = currentFrame->frameNumber; |
| 228 | |
| 229 | if(currentFrameNumber % 1000 == 0) { |
| 230 | VERBOSE(VB_COMMFLAG, QString("Processing frame %1").arg(currentFrameNumber)); |
| 231 | } |
| 232 | |
| 233 | if(currentFrameNumber > stopFrame || (!findLast && foundFrame)) |
| 234 | { |
| 235 | nvp->DiscardVideoFrame(currentFrame); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | //Lucas: maybe we should make the nuppelvideoplayer send out a signal |
| 240 | //when the aspect ratio changes. |
| 241 | //In order to not change too many things at a time, I"m using basic |
| 242 | //polling for now. |
| 243 | double newAspect = nvp->GetVideoAspect(); |
| 244 | if (newAspect != aspect) |
| 245 | { |
| 246 | SetVideoParams(aspect); |
| 247 | aspect = newAspect; |
| 248 | } |
| 249 | |
| 250 | if (((currentFrameNumber % 500) == 0) || |
| 251 | (((currentFrameNumber % 100) == 0) && |
| 252 | (stillRecording))) |
| 253 | { |
| 254 | emit breathe(); |
| 255 | if (m_bStop) |
| 256 | { |
| 257 | nvp->DiscardVideoFrame(currentFrame); |
| 258 | return false; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | while (m_bPaused) |
| 263 | { |
| 264 | emit breathe(); |
| 265 | sleep(1); |
| 266 | } |
| 267 | |
| 268 | // sleep a little so we don't use all cpu even if we're niced |
| 269 | if (!fullSpeed && !stillRecording) |
| 270 | usleep(10000); |
| 271 | |
| 272 | if (((currentFrameNumber % 500) == 0) || |
| 273 | ((showProgress || stillRecording) && |
| 274 | ((currentFrameNumber % 100) == 0))) |
| 275 | { |
| 276 | float elapsed = flagTime.elapsed() / 1000.0; |
| 277 | |
| 278 | if (elapsed) |
| 279 | flagFPS = framesProcessed / elapsed; |
| 280 | else |
| 281 | flagFPS = 0.0; |
| 282 | |
| 283 | int percentage; |
| 284 | if (stopFrame) |
| 285 | percentage = framesProcessed * 100 / totalFrames; |
| 286 | else |
| 287 | percentage = 0; |
| 288 | |
| 289 | if (percentage > 100) |
| 290 | percentage = 100; |
| 291 | |
| 292 | if (showProgress) |
| 293 | { |
| 294 | if (stopFrame) |
| 295 | { |
| 296 | QString tmp = QString("\b\b\b\b\b\b\b\b\b\b\b%1%/%2fps") |
| 297 | .arg(percentage, 3).arg((int)flagFPS, 3); |
| 298 | QByteArray ba = tmp.toAscii(); |
| 299 | cerr << ba.constData() << flush; |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | QString tmp = QString("\b\b\b\b\b\b\b\b\b\b\b\b\b%1/%2fps") |
| 304 | .arg(currentFrameNumber, 6).arg((int)flagFPS, 3); |
| 305 | QByteArray ba = tmp.toAscii(); |
| 306 | cerr << ba.constData() << flush; |
| 307 | } |
| 308 | cerr.flush(); |
| 309 | } |
| 310 | |
| 311 | if (stopFrame) |
| 312 | emit statusUpdate(QObject::tr("%1% Completed @ %2 fps.") |
| 313 | .arg(percentage).arg(flagFPS)); |
| 314 | else |
| 315 | emit statusUpdate(QObject::tr("%1 Frames Completed @ %2 fps.") |
| 316 | .arg((long)currentFrameNumber).arg(flagFPS)); |
| 317 | } |
| 318 | |
| 319 | ProcessFrame(currentFrame, currentFrameNumber); |
| 320 | |
| 321 | if(frameInfo[currentFrameNumber].flagMask & |
| 322 | (COMM_FRAME_SCENE_CHANGE | COMM_FRAME_BLANK | COMM_FRAME_SCENE_CHANGE)){ |
| 323 | foundFrame = currentFrameNumber; |
| 324 | } |
| 325 | |
| 326 | if (stillRecording) |
| 327 | { |
| 328 | int secondsRecorded = |
| 329 | recordingStartedAt.secsTo(QDateTime::currentDateTime()); |
| 330 | int secondsFlagged = (int)(framesProcessed / fps); |
| 331 | int secondsBehind = secondsRecorded - secondsFlagged; |
| 332 | long usecPerFrame = (long)(1.0 / nvp->GetFrameRate() * 1000000); |
| 333 | |
| 334 | struct timeval endTime; |
| 335 | gettimeofday(&endTime, NULL); |
| 336 | |
| 337 | long long usecSleep = |
| 338 | usecPerFrame - |
| 339 | (((endTime.tv_sec - startTime.tv_sec) * 1000000) + |
| 340 | (endTime.tv_usec - startTime.tv_usec)); |
| 341 | |
| 342 | if (secondsBehind > requiredBuffer) |
| 343 | { |
| 344 | if (fullSpeed) |
| 345 | usecSleep = 0; |
| 346 | else |
| 347 | usecSleep = (long)(usecSleep * 0.25); |
| 348 | } |
| 349 | else if (secondsBehind < requiredBuffer) |
| 350 | usecSleep = (long)(usecPerFrame * 1.5); |
| 351 | |
| 352 | if (usecSleep > 0) |
| 353 | usleep(usecSleep); |
| 354 | } |
| 355 | |
| 356 | nvp->DiscardVideoFrame(currentFrame); |
| 357 | framesProcessed++; |
| 358 | } |
| 359 | return foundFrame; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void PrePostRollFlagger::getCommercialBreakList(comm_map_t &marks) |
| 364 | { |
| 365 | VERBOSE(VB_COMMFLAG, "PrePostRollFlagger::GetCommBreakMap()"); |
| 366 | marks.clear(); |
| 367 | |
| 368 | long long end = 0; |
| 369 | if(closestAfterPre && closestBeforePre) { |
| 370 | //choose closest |
| 371 | if(closestAfterPre - preRoll < preRoll - closestBeforePre) |
| 372 | end = closestAfterPre; |
| 373 | else |
| 374 | end = closestBeforePre; |
| 375 | }else if(closestBeforePre) |
| 376 | end = closestBeforePre; |
| 377 | else if(closestAfterPre) |
| 378 | end = closestAfterPre; |
| 379 | else |
| 380 | end = preRoll; |
| 381 | |
| 382 | if(end) { |
| 383 | marks[0] = MARK_COMM_START; |
| 384 | marks[end] = MARK_COMM_END; |
| 385 | } |
| 386 | |
| 387 | long long start = 0; |
| 388 | if(closestAfterPost && closestBeforePost) { |
| 389 | //choose closest |
| 390 | if(closestAfterPost - postRoll < postRoll - closestBeforePost) |
| 391 | start = closestAfterPost; |
| 392 | else |
| 393 | start = closestBeforePost; |
| 394 | } |
| 395 | else if(closestBeforePost) |
| 396 | start = closestBeforePost; |
| 397 | else if(closestAfterPost) |
| 398 | start = closestAfterPre; |
| 399 | else if(postRoll) |
| 400 | start = myTotalFrames - postRoll; |
| 401 | |
| 402 | if(start) { |
| 403 | marks[start] = MARK_COMM_START; |
| 404 | marks[myTotalFrames] = MARK_COMM_END; |
| 405 | } |
| 406 | } |