turtletooth 04d8fd
turtletooth 04d8fd
#include "tiio_webm.h"
turtletooth 04d8fd
#include "tsystem.h"
turtletooth 04d8fd
#include "trasterimage.h"
turtletooth 04d8fd
#include "tsound.h"
turtletooth 04d8fd
#include "timageinfo.h"
Jeremy Bullock 2b9d8f
#include "toonz/stage.h"
turtletooth 04d8fd
#include <qstringlist></qstringlist>
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TImageWriterWebm
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
class TImageWriterWebm : public TImageWriter {
turtletooth 04d8fd
public:
turtletooth 04d8fd
  int m_frameIndex;
turtletooth 04d8fd
turtletooth 04d8fd
  TImageWriterWebm(const TFilePath &path, int frameIndex, TLevelWriterWebm *lwg)
turtletooth 04d8fd
      : TImageWriter(path), m_frameIndex(frameIndex), m_lwg(lwg) {
turtletooth 04d8fd
    m_lwg->addRef();
turtletooth 04d8fd
  }
turtletooth 04d8fd
  ~TImageWriterWebm() { m_lwg->release(); }
turtletooth 04d8fd
turtletooth 04d8fd
  bool is64bitOutputSupported() override { return false; }
turtletooth 04d8fd
  void save(const TImageP &img) override { m_lwg->save(img, m_frameIndex); }
turtletooth 04d8fd
turtletooth 04d8fd
private:
turtletooth 04d8fd
  TLevelWriterWebm *m_lwg;
turtletooth 04d8fd
};
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TLevelWriterWebm;
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
TLevelWriterWebm::TLevelWriterWebm(const TFilePath &path, TPropertyGroup *winfo)
turtletooth 04d8fd
    : TLevelWriter(path, winfo) {
turtletooth 04d8fd
  if (!m_properties) m_properties = new Tiio::WebmWriterProperties();
turtletooth 04d8fd
  std::string scale = m_properties->getProperty("Scale")->getValueAsString();
shun-iwasawa 27b0cf
  m_scale           = QString::fromStdString(scale).toInt();
shun-iwasawa 27b0cf
  std::string quality =
shun-iwasawa 27b0cf
      m_properties->getProperty("Quality")->getValueAsString();
turtletooth 04d8fd
  m_vidQuality = QString::fromStdString(quality).toInt();
turtletooth 04d8fd
  ffmpegWriter = new Ffmpeg();
turtletooth 04d8fd
  ffmpegWriter->setPath(m_path);
turtletooth 04d8fd
  if (TSystem::doesExistFileOrLevel(m_path)) TSystem::deleteFile(m_path);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelWriterWebm::~TLevelWriterWebm() {
turtletooth 04d8fd
  // QProcess createWebm;
turtletooth 04d8fd
  QStringList preIArgs;
turtletooth 04d8fd
  QStringList postIArgs;
turtletooth 04d8fd
turtletooth 04d8fd
  int outLx = m_lx;
turtletooth 04d8fd
  int outLy = m_ly;
turtletooth 04d8fd
turtletooth 04d8fd
  // set scaling
turtletooth 04d8fd
  if (m_scale != 0) {
turtletooth 04d8fd
    outLx = m_lx * m_scale / 100;
turtletooth 04d8fd
    outLy = m_ly * m_scale / 100;
turtletooth 04d8fd
  }
shun-iwasawa 27b0cf
  // ffmpeg doesn't like resolutions that aren't divisible by 2.
turtletooth 04d8fd
  if (outLx % 2 != 0) outLx++;
turtletooth 04d8fd
  if (outLy % 2 != 0) outLy++;
turtletooth 04d8fd
turtletooth 04d8fd
  // calculate quality (bitrate)
turtletooth 04d8fd
  int pixelCount   = m_lx * m_ly;
turtletooth 04d8fd
  int bitRate      = pixelCount / 150;  // crude but gets decent values
turtletooth 04d8fd
  double quality   = m_vidQuality / 100.0;
turtletooth 04d8fd
  double tempRate  = (double)bitRate * quality;
turtletooth 04d8fd
  int finalBitrate = (int)tempRate;
turtletooth 04d8fd
  int crf          = 51 - (m_vidQuality * 51 / 100);
turtletooth 04d8fd
turtletooth 04d8fd
  preIArgs << "-framerate";
turtletooth 04d8fd
  preIArgs << QString::number(m_frameRate);
turtletooth 04d8fd
turtletooth 04d8fd
  postIArgs << "-c:v";
turtletooth 04d8fd
  postIArgs << "libvpx";
turtletooth 04d8fd
  postIArgs << "-s";
turtletooth 04d8fd
  postIArgs << QString::number(outLx) + "x" + QString::number(outLy);
turtletooth 04d8fd
  postIArgs << "-b";
turtletooth 04d8fd
  postIArgs << QString::number(finalBitrate) + "k";
turtletooth 04d8fd
  postIArgs << "-speed";
turtletooth 04d8fd
  postIArgs << "3";
turtletooth 04d8fd
  postIArgs << "-quality";
turtletooth 04d8fd
  postIArgs << "good";
turtletooth 04d8fd
turtletooth 04d8fd
  ffmpegWriter->runFfmpeg(preIArgs, postIArgs, false, false, true);
turtletooth 04d8fd
  ffmpegWriter->cleanUpFiles();
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageWriterP TLevelWriterWebm::getFrameWriter(TFrameId fid) {
turtletooth 04d8fd
  // if (IOError != 0)
turtletooth 04d8fd
  //	throw TImageException(m_path, buildGifExceptionString(IOError));
turtletooth 04d8fd
  if (fid.getLetter() != 0) return TImageWriterP(0);
turtletooth 04d8fd
  int index             = fid.getNumber();
turtletooth 04d8fd
  TImageWriterWebm *iwg = new TImageWriterWebm(m_path, index, this);
turtletooth 04d8fd
  return TImageWriterP(iwg);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
void TLevelWriterWebm::setFrameRate(double fps) {
turtletooth 04d8fd
  m_frameRate = fps;
turtletooth 04d8fd
  ffmpegWriter->setFrameRate(fps);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
void TLevelWriterWebm::saveSoundTrack(TSoundTrack *st) {
turtletooth 04d8fd
  ffmpegWriter->saveSoundTrack(st);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
void TLevelWriterWebm::save(const TImageP &img, int frameIndex) {
turtletooth 04d8fd
  TRasterImageP image(img);
turtletooth 04d8fd
  m_lx = image->getRaster()->getLx();
turtletooth 04d8fd
  m_ly = image->getRaster()->getLy();
turtletooth 04d8fd
  ffmpegWriter->createIntermediateImage(img, frameIndex);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TImageReaderWebm
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
class TImageReaderWebm final : public TImageReader {
turtletooth 04d8fd
public:
turtletooth 04d8fd
  int m_frameIndex;
turtletooth 04d8fd
Jeremy Bullock 2b9d8f
  TImageReaderWebm(const TFilePath &path, int index, TLevelReaderWebm *lra,
Jeremy Bullock 2b9d8f
                   TImageInfo *info)
Jeremy Bullock 2b9d8f
      : TImageReader(path), m_lra(lra), m_frameIndex(index), m_info(info) {
turtletooth 04d8fd
    m_lra->addRef();
turtletooth 04d8fd
  }
turtletooth 04d8fd
  ~TImageReaderWebm() { m_lra->release(); }
turtletooth 04d8fd
turtletooth 04d8fd
  TImageP load() override { return m_lra->load(m_frameIndex); }
turtletooth 04d8fd
  TDimension getSize() const { return m_lra->getSize(); }
turtletooth 04d8fd
  TRect getBBox() const { return TRect(); }
Jeremy Bullock 2b9d8f
  const TImageInfo *getImageInfo() const override { return m_info; }
turtletooth 04d8fd
turtletooth 04d8fd
private:
turtletooth 04d8fd
  TLevelReaderWebm *m_lra;
Jeremy Bullock 2b9d8f
  TImageInfo *m_info;
turtletooth 04d8fd
turtletooth 04d8fd
  // not implemented
turtletooth 04d8fd
  TImageReaderWebm(const TImageReaderWebm &);
turtletooth 04d8fd
  TImageReaderWebm &operator=(const TImageReaderWebm &src);
turtletooth 04d8fd
};
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TLevelReaderWebm
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
TLevelReaderWebm::TLevelReaderWebm(const TFilePath &path) : TLevelReader(path) {
turtletooth 04d8fd
  ffmpegReader = new Ffmpeg();
turtletooth 04d8fd
  ffmpegReader->setPath(m_path);
turtletooth 04d8fd
  ffmpegReader->disablePrecompute();
turtletooth 04d8fd
  ffmpegFileInfo tempInfo = ffmpegReader->getInfo();
turtletooth 04d8fd
  double fps              = tempInfo.m_frameRate;
turtletooth 04d8fd
  m_frameCount            = tempInfo.m_frameCount;
turtletooth 04d8fd
  m_size                  = TDimension(tempInfo.m_lx, tempInfo.m_ly);
turtletooth 04d8fd
  m_lx                    = m_size.lx;
turtletooth 04d8fd
  m_ly                    = m_size.ly;
turtletooth 04d8fd
turtletooth 04d8fd
  ffmpegReader->getFramesFromMovie();
turtletooth 04d8fd
turtletooth 04d8fd
  // set values
turtletooth 04d8fd
  m_info                   = new TImageInfo();
turtletooth 04d8fd
  m_info->m_frameRate      = fps;
turtletooth 04d8fd
  m_info->m_lx             = m_lx;
turtletooth 04d8fd
  m_info->m_ly             = m_ly;
turtletooth 04d8fd
  m_info->m_bitsPerSample  = 8;
turtletooth 04d8fd
  m_info->m_samplePerPixel = 4;
Jeremy Bullock 2b9d8f
  m_info->m_dpix           = Stage::standardDpi;
Jeremy Bullock 2b9d8f
  m_info->m_dpiy           = Stage::standardDpi;
turtletooth 04d8fd
}
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelReaderWebm::~TLevelReaderWebm() {
turtletooth 04d8fd
  // ffmpegReader->cleanUpFiles();
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelP TLevelReaderWebm::loadInfo() {
turtletooth 04d8fd
  if (m_frameCount == -1) return TLevelP();
turtletooth 04d8fd
  TLevelP level;
turtletooth 04d8fd
  for (int i = 1; i <= m_frameCount; i++) level->setFrame(i, TImageP());
turtletooth 04d8fd
  return level;
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageReaderP TLevelReaderWebm::getFrameReader(TFrameId fid) {
turtletooth 04d8fd
  // if (IOError != 0)
turtletooth 04d8fd
  //	throw TImageException(m_path, buildAVIExceptionString(IOError));
turtletooth 04d8fd
  if (fid.getLetter() != 0) return TImageReaderP(0);
turtletooth 04d8fd
  int index = fid.getNumber();
turtletooth 04d8fd
Jeremy Bullock 2b9d8f
  TImageReaderWebm *irm = new TImageReaderWebm(m_path, index, this, m_info);
turtletooth 04d8fd
  return TImageReaderP(irm);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//------------------------------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TDimension TLevelReaderWebm::getSize() { return m_size; }
turtletooth 04d8fd
turtletooth 04d8fd
//------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageP TLevelReaderWebm::load(int frameIndex) {
turtletooth 04d8fd
  return ffmpegReader->getImage(frameIndex);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
Tiio::WebmWriterProperties::WebmWriterProperties()
shun-iwasawa 27b0cf
    : m_vidQuality("Quality", 1, 100, 90), m_scale("Scale", 1, 100, 100) {
shun-iwasawa 27b0cf
  bind(m_vidQuality);
shun-iwasawa 27b0cf
  bind(m_scale);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
// Tiio::Reader* Tiio::makeWebmReader(){ return nullptr; }
turtletooth 04d8fd
// Tiio::Writer* Tiio::makeWebmWriter(){ return nullptr; }