panoStitcher.cpp 24.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#include "panoStitcher.hpp"

#include "bounds.hpp"
#include "imageMapping.hpp"
#include "imageMerger.hpp"
#include "panoRemapper.hpp"

#include "common/angles.hpp"
#include "common/container.hpp"
#include "core/geoTransform.hpp"
#include "core/photoTransform.hpp"
#include "core/stitchOutput/stitchOutput.hpp"
#include "gpu/core1/strip.hpp"
#include "gpu/core1/transform.hpp"
#include "gpu/image/imgInsert.hpp"
#include "image/unpack.hpp"
#include "input/maskedReader.hpp"
#include "processors/photoCorrProcessor.hpp"
#include "parallax/mergerPair.hpp"
#include "parallax/imageWarper.hpp"
#include "parallax/imageFlow.hpp"

#include "libvideostitch/logging.hpp"
#include "libvideostitch/output.hpp"
#include "libvideostitch/panoDef.hpp"
#include "libvideostitch/geometryDef.hpp"
#include "libvideostitch/profile.hpp"
#include "libvideostitch/ptv.hpp"
#include "libvideostitch/stereoRigDef.hpp"
#include "libvideostitch/imageMergerFactory.hpp"
#include "libvideostitch/imageFlowFactory.hpp"
#include "libvideostitch/imageWarperFactory.hpp"
#include "libvideostitch/stitchOutput.hpp"

#include "inputsMap.hpp"
#include <cassert>
#include <fstream>
#include <memory>
#include <sstream>
#include <iomanip>

//#define PROGRESSIVE_RESULT

#if defined(PROGRESSIVE_RESULT)
#include "util/pngutil.hpp"
#include "util/pnm.hpp"
#include "util/debugUtils.hpp"
#endif

namespace VideoStitch {
namespace Core {

template <typename Output>
PanoStitcherImplV1<Output>::PanoStitcherImplV1(const std::string& name, const PanoDefinition& pano, Eye eye)
    : PanoStitcherImplBase<Output>(name, pano, eye),
      rigDef(nullptr),
      alignSize(ImageMerger::CudaBlockSize),
      merger(nullptr) {}

template <typename Output>
PanoStitcherImplV1<Output>::~PanoStitcherImplV1() {
  deleteAllValues(imageMappings);
}

//--------------------------------------- Runtime ---------------------------------------------

template <typename Output>
Status PanoStitcherImplV1<Output>::merge(frameid_t frame,
                                         const std::map<readerid_t, Input::PotentialFrame>& inputFrames,
                                         const std::map<readerid_t, Input::VideoReader*>& readers,
                                         const std::map<readerid_t, PreProcessor*>& preprocessors, PanoSurface& pano) {
  Status success = Status::OK();
  bool isFirstMerger = true;
  GPU::Event previousMergeFinishedEvent;
  std::vector<GPU::Event> readingFinishedEvents;

  pano.pimpl->reset(merger);

  FAIL_RETURN(adaptInputsMap(frame, readers));

  bool firstInput = true;
  for (auto mapping : imageMappings) {
    Input::VideoReader* reader = readers.at(mapping.first);
    const InputDefinition& inputDef = getPano().getInput(mapping.first);
    PreProcessor* preprocessor = preprocessors.at(mapping.first);
    GPU::Stream inputStream = getStreamForInput(mapping.first);

    FAIL_RETURN(mapping.second->setupTexArrayAsync(frame, inputFrames.find(mapping.first)->second, inputDef,
                                                   inputStream, reader, preprocessor));
    PotentialValue<GPU::Event> readingDone = inputStream.recordEvent();
    FAIL_RETURN(readingDone.status());
    readingFinishedEvents.push_back(readingDone.value());

    if (inputDef.getIsVideoEnabled()) {
      if (mapping.second->getMerger().warpMergeType() == ImageMerger::Format::None) {
        /* if merge is combined with wrap, stream synchro should be done before warp */
        FAIL_RETURN(pano.pimpl->warp(mapping.second, frame, getPano(), inputStream));
      }

      if (firstInput) {
        // setup may be done on merge stream, first stream waits for that
        FAIL_RETURN(inputStream.synchronizeOnStream(pano.pimpl->stream));
        firstInput = false;
      } else {
        // To launch merge for stream i, merging must be done for stream i-1.
        FAIL_RETURN(inputStream.waitOnEvent(previousMergeFinishedEvent));
      }

      if (mapping.second->getMerger().warpMergeType() != ImageMerger::Format::None) {
        FAIL_RETURN(pano.pimpl->warp(mapping.second, frame, getPano(), inputStream));
      }

      FAIL_RETURN(pano.pimpl->blend(getPano(), *mapping.second, isFirstMerger, inputStream));
      FAIL_RETURN(pano.pimpl->reconstruct(getPano(), *mapping.second, inputStream, false));
      isFirstMerger = false;
    }
    PotentialValue<GPU::Event> potEvent = inputStream.recordEvent();
    FAIL_RETURN(potEvent.status());
    previousMergeFinishedEvent = potEvent.value();

    // TODO_OPENCL_IMPL remove
    // workaround for VSA-5829
    inputStream.flush();

#ifdef PROGRESSIVE_RESULT
    std::stringstream ss;
    ss << "pano-output-" << mapping.first << ".png";
    if (warper != nullptr && warper->needImageFlow() && imageMergers[mapping.first]->isMultiScale()) {
      Debug::dumpRGBADeviceBuffer(ss.str().c_str(), tmpDevOut.borrow_const(), getPano().getWidth(),
                                  getPano().getHeight());
    } else {
      FAIL_RETURN(imageMergers[mapping.first]->finalizeToBuffer(pano.pimpl->buffer, inputStream));
      Debug::dumpRGBADeviceBuffer(ss.str().c_str(), pano.pimpl->buffer, getPano().getWidth(), getPano().getHeight());
    }
#endif
  }

  {
    // merge stream needs to wait for all operations to be finished on the input streams
    const Status status = pano.pimpl->stream.waitOnEvent(previousMergeFinishedEvent);
    if (!status.ok()) {
      Logger::get(Logger::Warning) << "Skipping output for frame " << frame << std::endl;
      if (success.ok()) {
        success = status;
      }
    }
  }

  if (success.ok()) {
    FAIL_RETURN(pano.pimpl->reconstruct(getPano(), *imageMappings.begin()->second, pano.pimpl->stream));

    SIMPLEPROFILE_MS("global pano remapping:");
    // apply global rotation
    Quaternion<double> stabilization = getPano().getStabilization().at(frame);
    Matrix33<double> perspective = stabilization.toRotationMatrix();
    Quaternion<double> orientation = getPano().getGlobalOrientation().at(frame);
    perspective *= orientation.toRotationMatrix();
    perspective *= getInteractivePersp();

    FAIL_RETURN(pano.pimpl->reproject(getPano(), perspective, merger));
  }

  // input frames are borrowed only for this call, make sure
  // all read operations have finished before returning
  for (auto& readFinished : readingFinishedEvents) {
    readFinished.synchronize();
  }

  return success;
}

//--------------------------------------- Setup ---------------------------------------------

template <typename Output>
Status PanoStitcherImplV1<Output>::computeSetupImage(const std::map<readerid_t, Input::VideoReader*>& readers,
                                                     const StereoRigDefinition* rigDef) {
  if (getPano().getProjection() == PanoProjection::Cubemap ||
      getPano().getProjection() == PanoProjection::EquiangularCubemap) {
    FAIL_RETURN(inputsMapCubemap->compute(readers, getPano()));
  }
  return inputsMap->compute(readers, getPano(), rigDef, this->getEye(), true);
}

template <typename Output>
Status PanoStitcherImplV1<Output>::setupImpl(const ImageMergerFactory& mergerFactory,
                                             const ImageWarperFactory& warperFactory,
                                             const ImageFlowFactory& flowFactory,
                                             const std::map<readerid_t, Input::VideoReader*>& readers,
                                             const StereoRigDefinition* rig) {
  Potential<InputsMap> potInputsMap = InputsMap::create(getPano());
  FAIL_RETURN(potInputsMap.status());
  inputsMap = std::shared_ptr<InputsMap>(potInputsMap.release());

  Potential<InputsMapCubemap> potInputsMapCubemap = InputsMapCubemap::create(getPano());
  FAIL_RETURN(potInputsMapCubemap.status());
  inputsMapCubemap = std::shared_ptr<InputsMapCubemap>(potInputsMapCubemap.release());

  PROPAGATE_FAILURE_CAUSE(setupCommon(mergerFactory, warperFactory, flowFactory, readers, rig), Origin::Stitcher,
                          ErrType::SetupFailure, "Could not setup stitcher");

  // Allocate buffers for mapping.
  // This must be done after merge setup since mergers can resize the mapping rectangles.
  {
    SIMPLEPROFILE_MS("Allocate buffers");
    for (auto reader : readers) {
      const Input::VideoReader::Spec& spec = reader.second->getSpec();
      const Status status = imageMappings[reader.second->id]->allocateUnpackBuffer(spec.frameDataSize);
      if (getPano().getProjection() == PanoProjection::Cubemap ||
          getPano().getProjection() == PanoProjection::EquiangularCubemap) {
        for (int target = CUBE_MAP_POSITIVE_X; target <= CUBE_MAP_NEGATIVE_Z; ++target) {
          const Status status =
              imageMappings[reader.second->id]->allocateBuffers((TextureTarget)target, spec.width, spec.height);
          if (!status.ok()) {
            return Status(Origin::Stitcher, ErrType::SetupFailure,
                          "Could not setup mapper for input " + std::to_string(reader.second->id), status);
          }
        }
      } else {
        const Status status =
            imageMappings[reader.second->id]->allocateBuffers(EQUIRECTANGULAR, spec.width, spec.height);
        if (!status.ok()) {
          return Status(Origin::Stitcher, ErrType::SetupFailure,
                        "Could not setup mapper for input " + std::to_string(reader.second->id), status);
        }
      }
    }
  }
  return Status::OK();
}

template <typename Output>
Status PanoStitcherImplV1<Output>::adaptInputsMap(const frameid_t frameId,
                                                  std::map<readerid_t, Input::VideoReader*> readers) {
  std::pair<int64_t, int64_t> boundedFrames = getPano().getBlendingMaskBoundedFrameIds(frameId);
  std::pair<int64_t, int64_t> inputsMapBoundedFrames = inputsMap->getBoundedFrameIds();
  const bool reloadBoundedFrame =
      boundedFrames.first != inputsMapBoundedFrames.first || boundedFrames.second != inputsMapBoundedFrames.second;
  const bool interpolateFrame = getPano().getBlendingMaskInterpolationEnabled() && frameId <= boundedFrames.second &&
                                frameId >= boundedFrames.first && boundedFrames.first != boundedFrames.second;

  if (reloadBoundedFrame || interpolateFrame) {
    bool loaded = false;
#ifndef VS_OPENCL
    FAIL_RETURN(inputsMap->loadPrecomputedMap(frameId, getPano(), readers, maskInterpolation, loaded));
#endif
    if (loaded) {
      FAIL_RETURN(prepareMappers(rigDef));

      // Update image merging
      for (auto mapping : imageMappings) {
        // Do each setup in a parallel stream, they are totally parallel (setupBuffer is const).
        FAIL_CAUSE(
            mapping.second->getMerger().setup(getPano(), *inputsMap, *mapping.second, getStreamForInput(mapping.first)),
            Origin::Stitcher, ErrType::SetupFailure,
            "Could not setup merger for input " + std::to_string(mapping.first));
      }

      // Precompute coordinate buffer
      for (auto mapping : imageMappings) {
        GPU::Stream inputStream = getStreamForInput(mapping.first);
        FAIL_RETURN(mapping.second->precomputedCoord(0, getPano(), inputStream));
      }

      // Synchronize all streams
      for (auto mapping : imageMappings) {
        FAIL_RETURN(getStreamForInput(mapping.first).synchronize());
      }
    }
  }
  return Status::OK();
}

template <typename Output>
Status PanoStitcherImplV1<Output>::setupCommon(const ImageMergerFactory& mergerFactory,
                                               const ImageWarperFactory& warperFactory,
                                               const ImageFlowFactory& flowFactory,
                                               const std::map<readerid_t, Input::VideoReader*>& readers,
                                               const StereoRigDefinition* rig) {
  this->rigDef = rig;

  FAIL_RETURN(computeSetupImage(readers, rig));

  const bool isFlowBasedBlending{warperFactory.needsInputPreProcessing() || flowFactory.needsInputPreProcessing()};

  // Create the mappers.
  std::map<readerid_t, ImageMappingFlow*> mappingsFlow;
  for (auto reader : readers) {
    if (isFlowBasedBlending) {
      mappingsFlow[reader.second->id] = new ImageMappingFlow(reader.second->id);
      imageMappings[reader.second->id] = mappingsFlow[reader.second->id];
    } else {
      imageMappings[reader.second->id] = new ImageMapping(reader.second->id);
    }
  }

  // Compute the bounding boxes.
  alignSize = mergerFactory.getBlockAlignment();
  FAIL_RETURN(prepareMappers(rig));

  // Setup the mappers
  if (isFlowBasedBlending) {
    std::vector<readerid_t> id0s;
    ImageMappingFlow* prevMapping = nullptr;
    for (auto mapping : mappingsFlow) {
      FAIL_RETURN(mapping.second->setup(prevMapping, getPano(), rig, mergerFactory, id0s, inputsMap, warperFactory,
                                        flowFactory, getStreamForInput(mapping.first)));
      id0s.push_back(mapping.first);
      prevMapping = mapping.second;
    }
    // OpenCL 1.2 does not support read_write images
    // We can not remove the internal memcopy at the beginning of the reproject()
    if (prevMapping && (GPU::getFramework() == Discovery::Framework::CUDA)) {
      merger = &prevMapping->getMerger();
    }
  } else {
    ImageMapping* prevMapping = nullptr;
    for (auto mapping : imageMappings) {
      FAIL_RETURN(
          mapping.second->setup(prevMapping, getPano(), mergerFactory, inputsMap, getStreamForInput(mapping.first)));
      prevMapping = mapping.second;
    }
    if (prevMapping && (GPU::getFramework() == Discovery::Framework::CUDA)) {
      merger = &prevMapping->getMerger();
    }
  }

  // Synchronize all streams.
  // for (auto order : maskOrders) {
  //  const int imId = order.second;
  for (auto mapping : imageMappings) {
    FAIL_RETURN(getStreamForInput(mapping.first).synchronize());
  }

#ifndef VS_OPENCL
  // Prepare mask interpolation
  Potential<MaskInterpolation::InputMaskInterpolation> potInputMaskInterpolation =
      MaskInterpolation::InputMaskInterpolation::create(getPano(), readers);
  if (potInputMaskInterpolation.status().ok()) {
    maskInterpolation.reset(potInputMaskInterpolation.release());
  }
#endif

  return Status::OK();
}

/**
 * Detect image boundaries
 */
template <typename Output>
Status PanoStitcherImplV1<Output>::prepareMappers(const StereoRigDefinition* rig) {
  int64_t maxDim = std::max(getPano().getWidth(), getPano().getHeight());
  maxDim = std::max(maxDim, getPano().getLength());

  auto tmpDevBuffer = GPU::Buffer<uint32_t>::allocate(maxDim, "Input Bounding boxes");
  FAIL_RETURN(tmpDevBuffer.status());

  auto tmpHostBuffer = GPU::HostBuffer<uint32_t>::allocate(maxDim, "Input Bounding boxes");
  FAIL_RETURN(tmpHostBuffer.status());

  SIMPLEPROFILE_MS("compute image bounding boxes");
  // const std::vector<size_t> maskOrders = getPano().getMasksOrder();
  GPU::Stream stream = getStreamForInput(imageMappings.begin()->first);
  for (int t = EQUIRECTANGULAR; t <= CUBE_MAP_NEGATIVE_Z; ++t) {
    TextureTarget target = (TextureTarget)t;
    GPU::Buffer<uint32_t> inputsMask;
    int64_t width, height;
    if (target == EQUIRECTANGULAR) {
      inputsMask = inputsMap->getMask();
      width = getPano().getWidth();
      height = getPano().getHeight();
    } else {
      inputsMask = inputsMapCubemap->getMask(target);
      width = getPano().getLength();
      height = getPano().getLength();
    }
    FAIL_RETURN(computeHBounds(target, width, height, imageMappings, rig, this->getEye(), inputsMask,
                               tmpHostBuffer.value(), tmpDevBuffer.value(), stream, true));
    FAIL_RETURN(computeVBounds(target, width, height, imageMappings, inputsMask, tmpHostBuffer.value(),
                               tmpDevBuffer.value(), stream));
  }

  for (auto mapping : imageMappings) {
    for (int t = EQUIRECTANGULAR; t <= CUBE_MAP_NEGATIVE_Z; ++t) {
      TextureTarget target = (TextureTarget)t;
      if (!mapping.second->getOutputRect(target).empty()) {
        // Make sure the left and top offsets are levels times divisible by two.
        // (see example in the Laplacian merger merge() for why).
        mapping.second->getOutputRect(target).growToAlignTo(alignSize, alignSize);
        mapping.second->getOutputRect(target).growToMultipleSizeOf(alignSize, alignSize);
      }
    }
  }

  FAIL_RETURN(tmpHostBuffer.value().release());
  FAIL_RETURN(tmpDevBuffer.value().release());
  return Status::OK();
}

template <typename Output>
Status PanoStitcherImplV1<Output>::redoSetupImpl(const ImageMergerFactory& mergerFactory,
                                                 const ImageWarperFactory& warperFactory,
                                                 const ImageFlowFactory& flowFactory,
                                                 const std::map<readerid_t, Input::VideoReader*>& readers,
                                                 const StereoRigDefinition* rig) {
  if (imageMappings.empty()) {
    return Status::OK();
  }

  // Delete all mappers, but before that steal all input buffers.
  // This will enable restitching directly because we will still have the reader data in the input buffer.
  std::map<videoreaderid_t, SourceSurface*> mapperHostInputBuffers;
  for (auto mapping : imageMappings) {
    SourceSurface* sourceSurf = nullptr;
    mapping.second->releaseInputBuffers(&sourceSurf);
    mapperHostInputBuffers[mapping.first] = sourceSurf;
  }
  deleteAllValues(imageMappings);

  FAIL_CAUSE(setupCommon(mergerFactory, warperFactory, flowFactory, readers, rig), Origin::Stitcher,
             ErrType::SetupFailure, "Could not set up stitcher");

  // Allocate buffers for mapping.
  // This must be done after merge setup since mergers can resize the mapping rectangles.
  {
    SIMPLEPROFILE_MS("Allocate buffers");
    for (auto reader : readers) {
      const Input::VideoReader::Spec& spec = reader.second->getSpec();
      const Status status = imageMappings[reader.second->id]->allocateUnpackBuffer(spec.frameDataSize);
      for (int t = EQUIRECTANGULAR; t <= CUBE_MAP_NEGATIVE_Z; ++t) {
        const Status status = imageMappings[reader.second->id]->allocateBuffersPartial(
            (TextureTarget)t, spec.width, spec.height, mapperHostInputBuffers[reader.second->id]);
        if (!status.ok()) {
          return Status(Origin::Stitcher, ErrType::SetupFailure,
                        "Could not setup mapper for input " + std::to_string(reader.second->id), status);
        }
      }
    }
  }
  return Status::OK();
}

template <typename Output>
ChangeCompatibility PanoStitcherImplV1<Output>::getCompatibility(const InputDefinition& im,
                                                                 const InputDefinition& newIm) {
  ChangeCompatibility compat = SetupCompatibleChanges;
#define DECLARE_INCOMPATIBLE(accessor)                                                              \
  static_assert(!std::is_pointer<decltype(im.accessor())>::value, "Are you comparing pointers ?!"); \
  if (im.accessor() != newIm.accessor()) {                                                          \
    return IncompatibleChanges;                                                                     \
  }
#define DECLARE_SETUPINCOMPATIBLE(accessor)                                                         \
  static_assert(!std::is_pointer<decltype(im.accessor())>::value, "Are you comparing pointers ?!"); \
  if (im.accessor() != newIm.accessor()) {                                                          \
    compat = worstCompatibility(compat, SetupIncompatibleChanges);                                  \
  }
#define DECLARE_SETUPCOMPATIBLE(accessor)
  DECLARE_INCOMPATIBLE(getReaderConfig);
  DECLARE_INCOMPATIBLE(getMaskData);
  DECLARE_SETUPINCOMPATIBLE(getWidth);
  DECLARE_SETUPINCOMPATIBLE(getHeight);
  DECLARE_SETUPINCOMPATIBLE(getCroppedWidth);
  DECLARE_SETUPINCOMPATIBLE(getCroppedHeight);
  DECLARE_SETUPINCOMPATIBLE(getCropLeft);
  DECLARE_SETUPINCOMPATIBLE(getCropRight);
  DECLARE_SETUPINCOMPATIBLE(getCropTop);
  DECLARE_SETUPINCOMPATIBLE(getCropBottom);
  DECLARE_SETUPINCOMPATIBLE(getFormat);
  DECLARE_SETUPCOMPATIBLE(getRedCB);
  DECLARE_SETUPCOMPATIBLE(getGreenCB);
  DECLARE_SETUPCOMPATIBLE(getBlueCB);
  DECLARE_SETUPCOMPATIBLE(getExposureValue);
  DECLARE_SETUPINCOMPATIBLE(getEmorA);
  DECLARE_SETUPINCOMPATIBLE(getEmorB);
  DECLARE_SETUPINCOMPATIBLE(getEmorC);
  DECLARE_SETUPINCOMPATIBLE(getEmorD);
  DECLARE_SETUPINCOMPATIBLE(getEmorE);
  DECLARE_SETUPINCOMPATIBLE(getGamma);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCoeff0);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCoeff1);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCoeff2);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCoeff3);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCenterX);
  DECLARE_SETUPINCOMPATIBLE(getVignettingCenterY);
  DECLARE_SETUPINCOMPATIBLE(getPhotoResponse);
  DECLARE_SETUPINCOMPATIBLE(hasCroppedArea);
  DECLARE_SETUPCOMPATIBLE(getFrameOffset);
  DECLARE_SETUPCOMPATIBLE(getSynchroCost);
  DECLARE_SETUPCOMPATIBLE(getStack);  // ignored
  DECLARE_SETUPINCOMPATIBLE(getGeometries);
#undef DECLARE_INCOMPATIBLE
#undef DECLARE_SETUPINCOMPATIBLE
#undef DECLARE_SETUPCOMPATIBLE

  return compat;
}

template <typename Output>
ChangeCompatibility PanoStitcherImplV1<Output>::getCompatibility(const OverlayInputDefinition& im,
                                                                 const OverlayInputDefinition& newIm) {
  ChangeCompatibility compat = SetupCompatibleChanges;
#define DECLARE_INCOMPATIBLE(accessor)                                                              \
  static_assert(!std::is_pointer<decltype(im.accessor())>::value, "Are you comparing pointers ?!"); \
  if (im.accessor() != newIm.accessor()) {                                                          \
    return IncompatibleChanges;                                                                     \
  }
#define DECLARE_SETUPINCOMPATIBLE(accessor)                                                         \
  static_assert(!std::is_pointer<decltype(im.accessor())>::value, "Are you comparing pointers ?!"); \
  if (im.accessor() != newIm.accessor()) {                                                          \
    compat = worstCompatibility(compat, SetupIncompatibleChanges);                                  \
  }
#define DECLARE_SETUPCOMPATIBLE(accessor)
  DECLARE_INCOMPATIBLE(getReaderConfig);
  DECLARE_SETUPINCOMPATIBLE(getWidth);
  DECLARE_SETUPINCOMPATIBLE(getHeight);
  DECLARE_SETUPCOMPATIBLE(getFrameOffset);
  DECLARE_SETUPCOMPATIBLE(getScaleCurve);
  DECLARE_SETUPCOMPATIBLE(getAlphaCurve);
  DECLARE_SETUPCOMPATIBLE(getTransXCurve);
  DECLARE_SETUPCOMPATIBLE(getTransYCurve);
  DECLARE_SETUPCOMPATIBLE(getTransZCurve);
  DECLARE_SETUPCOMPATIBLE(getRotationCurve);
  DECLARE_SETUPCOMPATIBLE(getGlobalOrietationApplied);
#undef DECLARE_INCOMPATIBLE
#undef DECLARE_SETUPINCOMPATIBLE
#undef DECLARE_SETUPCOMPATIBLE

  return compat;
}

template <typename Output>
ChangeCompatibility PanoStitcherImplV1<Output>::getCompatibility(const PanoDefinition& pano,
                                                                 const PanoDefinition& newPano) const {
  if (pano.numInputs() != newPano.numInputs()) {
    return IncompatibleChanges;
  }
  ChangeCompatibility compat = SetupCompatibleChanges;
  // Inputs.
  for (readerid_t i = 0; i < pano.numInputs() && compat != IncompatibleChanges; ++i) {
    compat = worstCompatibility(compat, getCompatibility(pano.getInput(i), newPano.getInput(i)));
  }

  // Overlays.
  for (overlayreaderid_t i = 0; i < pano.numOverlays() && compat != IncompatibleChanges; ++i) {
    compat = worstCompatibility(compat, getCompatibility(pano.getOverlay(i), newPano.getOverlay(i)));
  }

#define DECLARE_INCOMPATIBLE(accessor)         \
  if (pano.accessor() != newPano.accessor()) { \
    return IncompatibleChanges;                \
  }
#define DECLARE_SETUPINCOMPATIBLE(accessor)                        \
  if (pano.accessor() != newPano.accessor()) {                     \
    compat = worstCompatibility(compat, SetupIncompatibleChanges); \
  }
#define DECLARE_SETUPCOMPATIBLE(accessor)
  DECLARE_INCOMPATIBLE(getWidth);
  DECLARE_INCOMPATIBLE(getHeight);
  DECLARE_SETUPINCOMPATIBLE(getLength);
  DECLARE_SETUPCOMPATIBLE(getExposureValue);
  DECLARE_SETUPINCOMPATIBLE(getProjection);
  DECLARE_SETUPINCOMPATIBLE(getHFOV);
  DECLARE_SETUPINCOMPATIBLE(getBlendingMaskEnabled);
  DECLARE_SETUPINCOMPATIBLE(getBlendingMaskWidth);
  DECLARE_SETUPINCOMPATIBLE(getBlendingMaskHeight);
  DECLARE_SETUPINCOMPATIBLE(getSphereScale);

#undef DECLARE_INCOMPATIBLE
#undef DECLARE_SETUPINCOMPATIBLE
#undef DECLARE_SETUPCOMPATIBLE
  return compat;
}

// explicit instantiations

template class PanoStitcherImplV1<StitchOutput>;
template class PanoStitcherImplV1<StereoOutput>;

}  // namespace Core
}  // namespace VideoStitch