|
17 | 17 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
18 | 18 |
|
19 | 19 | #include "ExportVerify.h" |
| 20 | +#include <QDebug> |
20 | 21 | #include <QJsonDocument> |
21 | 22 | #include <QJsonParseError> |
| 23 | +#include "utils/AEDataTypeConverter.h" |
22 | 24 | #include "utils/AEHelper.h" |
23 | 25 | #include "utils/LayerHelper.h" |
24 | 26 | #include "utils/ScopedHelper.h" |
@@ -355,6 +357,180 @@ static void CheckVideoCompositionInUIScenes(std::shared_ptr<PAGExportSession> se |
355 | 357 | } |
356 | 358 | } |
357 | 359 |
|
| 360 | +static void CollectVideoCompositionReferences(const pag::Composition* composition, |
| 361 | + const pag::VideoComposition* targetVideo, |
| 362 | + float mainCompFrameRate, pag::Frame mainCompClipStart, |
| 363 | + pag::Frame localClipStart, pag::Frame localClipEnd, |
| 364 | + std::vector<pag::TimeRange>* referenceRanges) { |
| 365 | + if (composition->type() != pag::CompositionType::Vector) { |
| 366 | + return; |
| 367 | + } |
| 368 | + |
| 369 | + float currentFrameRate = composition->frameRate; |
| 370 | + for (auto layer : static_cast<const pag::VectorComposition*>(composition)->layers) { |
| 371 | + if (layer->type() != pag::LayerType::PreCompose) { |
| 372 | + continue; |
| 373 | + } |
| 374 | + |
| 375 | + auto preComposeLayer = static_cast<pag::PreComposeLayer*>(layer); |
| 376 | + pag::Frame offset = preComposeLayer->compositionStartTime; |
| 377 | + |
| 378 | + pag::Frame visibleStart = std::max(layer->startTime, localClipStart); |
| 379 | + pag::Frame visibleEnd = std::min(layer->startTime + layer->duration, localClipEnd); |
| 380 | + if (visibleStart >= visibleEnd) { |
| 381 | + continue; |
| 382 | + } |
| 383 | + |
| 384 | + // Convert frame offset from current composition's frame rate to main composition's frame rate |
| 385 | + float toMainFactor = mainCompFrameRate / currentFrameRate; |
| 386 | + pag::Frame absoluteStart = |
| 387 | + mainCompClipStart + static_cast<pag::Frame>((visibleStart - localClipStart) * toMainFactor); |
| 388 | + pag::Frame absoluteEnd = |
| 389 | + mainCompClipStart + static_cast<pag::Frame>((visibleEnd - localClipStart) * toMainFactor); |
| 390 | + |
| 391 | + if (preComposeLayer->composition == targetVideo) { |
| 392 | + referenceRanges->push_back({absoluteStart, absoluteEnd}); |
| 393 | + } else if (preComposeLayer->composition->type() == pag::CompositionType::Vector) { |
| 394 | + auto subComposition = preComposeLayer->composition; |
| 395 | + float toSubFactor = subComposition->frameRate / currentFrameRate; |
| 396 | + pag::Frame newLocalClipStart = |
| 397 | + static_cast<pag::Frame>((visibleStart - offset) * toSubFactor); |
| 398 | + pag::Frame newLocalClipEnd = static_cast<pag::Frame>((visibleEnd - offset) * toSubFactor); |
| 399 | + if (newLocalClipStart < newLocalClipEnd) { |
| 400 | + CollectVideoCompositionReferences(subComposition, targetVideo, mainCompFrameRate, |
| 401 | + absoluteStart, newLocalClipStart, newLocalClipEnd, |
| 402 | + referenceRanges); |
| 403 | + } |
| 404 | + } |
| 405 | + } |
| 406 | +} |
| 407 | + |
| 408 | +static std::string GetOverlapFrameRanges(const std::vector<pag::TimeRange>& referenceRanges) { |
| 409 | + if (referenceRanges.size() < 2) { |
| 410 | + return ""; |
| 411 | + } |
| 412 | + |
| 413 | + std::vector<std::pair<pag::Frame, int>> events; |
| 414 | + for (const auto& range : referenceRanges) { |
| 415 | + events.emplace_back(range.start, 1); |
| 416 | + events.emplace_back(range.end, -1); |
| 417 | + } |
| 418 | + |
| 419 | + std::sort(events.begin(), events.end(), [](const auto& a, const auto& b) { |
| 420 | + return a.first != b.first ? a.first < b.first : a.second > b.second; |
| 421 | + }); |
| 422 | + |
| 423 | + std::vector<pag::TimeRange> overlapRanges; |
| 424 | + int count = 0; |
| 425 | + pag::Frame overlapStart = 0; |
| 426 | + bool inOverlap = false; |
| 427 | + |
| 428 | + for (const auto& event : events) { |
| 429 | + if (count > 1 && !inOverlap) { |
| 430 | + overlapStart = event.first; |
| 431 | + inOverlap = true; |
| 432 | + } |
| 433 | + count += event.second; |
| 434 | + if (count <= 1 && inOverlap) { |
| 435 | + overlapRanges.push_back({overlapStart, event.first}); |
| 436 | + inOverlap = false; |
| 437 | + } else if (count > 1 && !inOverlap) { |
| 438 | + overlapStart = event.first; |
| 439 | + inOverlap = true; |
| 440 | + } |
| 441 | + } |
| 442 | + |
| 443 | + std::string result; |
| 444 | + for (size_t i = 0; i < overlapRanges.size(); i++) { |
| 445 | + if (i > 0) { |
| 446 | + result += ", "; |
| 447 | + } |
| 448 | + pag::Frame start = overlapRanges[i].start; |
| 449 | + pag::Frame end = overlapRanges[i].end - 1; |
| 450 | + if (start == end) { |
| 451 | + result += std::to_string(start); |
| 452 | + } else { |
| 453 | + result += "[" + std::to_string(start) + "-" + std::to_string(end) + "]"; |
| 454 | + } |
| 455 | + } |
| 456 | + return result; |
| 457 | +} |
| 458 | + |
| 459 | +static size_t GetMaxOverlapCount(const std::vector<pag::TimeRange>& referenceRanges) { |
| 460 | + if (referenceRanges.empty()) { |
| 461 | + return 0; |
| 462 | + } |
| 463 | + |
| 464 | + std::vector<pag::Frame> startTimes, endTimes; |
| 465 | + for (const auto& range : referenceRanges) { |
| 466 | + startTimes.push_back(range.start); |
| 467 | + endTimes.push_back(range.end); |
| 468 | + } |
| 469 | + |
| 470 | + std::sort(startTimes.begin(), startTimes.end()); |
| 471 | + std::sort(endTimes.begin(), endTimes.end()); |
| 472 | + |
| 473 | + size_t startIndex = 0, endIndex = 0, maxCount = 0, count = 0; |
| 474 | + while (startIndex < startTimes.size()) { |
| 475 | + if (startTimes[startIndex] < endTimes[endIndex]) { |
| 476 | + maxCount = std::max(maxCount, ++count); |
| 477 | + startIndex++; |
| 478 | + } else { |
| 479 | + count--; |
| 480 | + endIndex++; |
| 481 | + } |
| 482 | + } |
| 483 | + return maxCount; |
| 484 | +} |
| 485 | + |
| 486 | +static void CheckVideoCompositionOverlap(std::shared_ptr<PAGExportSession> session, |
| 487 | + std::vector<pag::Composition*>& compositions) { |
| 488 | + constexpr size_t maxAllowedOverlap = 1; |
| 489 | + |
| 490 | + auto mainCompId = GetItemID(session->itemHandle); |
| 491 | + pag::Composition* mainComposition = nullptr; |
| 492 | + for (auto comp : compositions) { |
| 493 | + if (comp->id == mainCompId) { |
| 494 | + mainComposition = comp; |
| 495 | + break; |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + if (mainComposition == nullptr || mainComposition->type() != pag::CompositionType::Vector) { |
| 500 | + return; |
| 501 | + } |
| 502 | + |
| 503 | + const auto& Suites = GetSuites(); |
| 504 | + AEGP_CompH compositionHandle = GetItemCompH(session->itemHandle); |
| 505 | + A_Time workAreaStart = {}; |
| 506 | + A_Time workAreaDuration = {}; |
| 507 | + Suites->CompSuite6()->AEGP_GetCompWorkAreaStart(compositionHandle, &workAreaStart); |
| 508 | + Suites->CompSuite6()->AEGP_GetCompWorkAreaDuration(compositionHandle, &workAreaDuration); |
| 509 | + pag::Frame clipStart = AEDurationToFrame(workAreaStart, session->frameRate); |
| 510 | + pag::Frame clipEnd = clipStart + AEDurationToFrame(workAreaDuration, session->frameRate); |
| 511 | + |
| 512 | + for (auto composition : compositions) { |
| 513 | + if (composition->type() != pag::CompositionType::Video) { |
| 514 | + continue; |
| 515 | + } |
| 516 | + |
| 517 | + auto videoComposition = static_cast<pag::VideoComposition*>(composition); |
| 518 | + std::vector<pag::TimeRange> referenceRanges; |
| 519 | + CollectVideoCompositionReferences(mainComposition, videoComposition, mainComposition->frameRate, |
| 520 | + clipStart, clipStart, clipEnd, &referenceRanges); |
| 521 | + |
| 522 | + size_t maxOverlap = GetMaxOverlapCount(referenceRanges); |
| 523 | + if (maxOverlap > maxAllowedOverlap) { |
| 524 | + auto itemHandle = session->itemHandleMap[videoComposition->id]; |
| 525 | + auto name = GetItemName(itemHandle); |
| 526 | + auto overlapFrames = GetOverlapFrameRanges(referenceRanges); |
| 527 | + qDebug() << "References to Composition" << name.c_str() |
| 528 | + << "overlap at frames: " << overlapFrames.c_str(); |
| 529 | + session->pushWarning(AlertInfoType::VideoCompositionOverlap, name); |
| 530 | + } |
| 531 | + } |
| 532 | +} |
| 533 | + |
358 | 534 | static bool CompareVideoComposition(std::shared_ptr<PAGExportSession> session, |
359 | 535 | pag::VideoComposition* composition1, |
360 | 536 | pag::VideoComposition* composition2) { |
@@ -654,6 +830,7 @@ void CheckBeforeExport(std::shared_ptr<PAGExportSession> session, |
654 | 830 | CheckLayerNum(session, compositions); |
655 | 831 | CheckImageFillRule(session, compositions.back()); |
656 | 832 | CheckVideoCompositionInUIScenes(session, compositions); |
| 833 | + CheckVideoCompositionOverlap(session, compositions); |
657 | 834 | CheckDuplicateVideoSequence(session, compositions); |
658 | 835 | CheckLayerProperty(session, compositions); |
659 | 836 | } |
|
0 commit comments