ffmpeg_frame_generator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ffmpeg/ffmpeg_frame_generator.h"
  8. #include "ffmpeg/ffmpeg_utility.h"
  9. #include "base/debug_log.h"
  10. namespace FFmpeg {
  11. namespace {
  12. constexpr auto kMaxArea = 1920 * 1080 * 4;
  13. } // namespace
  14. class FrameGenerator::Impl final {
  15. public:
  16. explicit Impl(const QByteArray &bytes);
  17. [[nodiscard]] Frame renderNext(
  18. QImage storage,
  19. QSize size,
  20. Qt::AspectRatioMode mode);
  21. [[nodiscard]] Frame renderCurrent(
  22. QImage storage,
  23. QSize size,
  24. Qt::AspectRatioMode mode);
  25. void jumpToStart();
  26. private:
  27. struct ReadFrame {
  28. FramePointer frame;
  29. crl::time position = 0;
  30. crl::time duration = 0;
  31. };
  32. void readNextFrame();
  33. void resolveNextFrameTiming();
  34. [[nodiscard]] QString wrapError(int result) const;
  35. bool rotationSwapWidthHeight() const {
  36. return (_rotation == 90) || (_rotation == 270);
  37. }
  38. [[nodiscard]] static int Read(
  39. void *opaque,
  40. uint8_t *buf,
  41. int buf_size);
  42. [[nodiscard]] static int64_t Seek(
  43. void *opaque,
  44. int64_t offset,
  45. int whence);
  46. [[nodiscard]] int read(uint8_t *buf, int buf_size);
  47. [[nodiscard]] int64_t seek(int64_t offset, int whence);
  48. const QByteArray _bytes;
  49. int _deviceOffset = 0;
  50. FormatPointer _format;
  51. ReadFrame _current;
  52. ReadFrame _next;
  53. CodecPointer _codec;
  54. SwscalePointer _scale;
  55. int _streamId = 0;
  56. int _rotation = 0;
  57. //AVRational _aspect = kNormalAspect;
  58. crl::time _framePosition = 0;
  59. int _nextFrameDelay = 0;
  60. int _currentFrameDelay = 0;
  61. };
  62. FrameGenerator::Impl::Impl(const QByteArray &bytes)
  63. : _bytes(bytes) {
  64. _format = MakeFormatPointer(
  65. static_cast<void*>(this),
  66. &FrameGenerator::Impl::Read,
  67. nullptr,
  68. &FrameGenerator::Impl::Seek);
  69. auto error = 0;
  70. if ((error = avformat_find_stream_info(_format.get(), nullptr))) {
  71. return;
  72. }
  73. _streamId = av_find_best_stream(
  74. _format.get(),
  75. AVMEDIA_TYPE_VIDEO,
  76. -1,
  77. -1,
  78. nullptr,
  79. 0);
  80. if (_streamId < 0) {
  81. return;
  82. }
  83. const auto info = _format->streams[_streamId];
  84. _rotation = ReadRotationFromMetadata(info);
  85. //_aspect = ValidateAspectRatio(info->sample_aspect_ratio);
  86. _codec = MakeCodecPointer({ .stream = info });
  87. }
  88. int FrameGenerator::Impl::Read(void *opaque, uint8_t *buf, int buf_size) {
  89. return static_cast<Impl*>(opaque)->read(buf, buf_size);
  90. }
  91. int FrameGenerator::Impl::read(uint8_t *buf, int buf_size) {
  92. const auto available = _bytes.size() - _deviceOffset;
  93. if (available <= 0) {
  94. return AVERROR_EOF;
  95. }
  96. const auto fill = std::min(int(available), buf_size);
  97. memcpy(buf, _bytes.data() + _deviceOffset, fill);
  98. _deviceOffset += fill;
  99. return fill;
  100. }
  101. int64_t FrameGenerator::Impl::Seek(
  102. void *opaque,
  103. int64_t offset,
  104. int whence) {
  105. return static_cast<Impl*>(opaque)->seek(offset, whence);
  106. }
  107. int64_t FrameGenerator::Impl::seek(int64_t offset, int whence) {
  108. if (whence == AVSEEK_SIZE) {
  109. return _bytes.size();
  110. }
  111. const auto now = [&] {
  112. switch (whence) {
  113. case SEEK_SET: return offset;
  114. case SEEK_CUR: return _deviceOffset + offset;
  115. case SEEK_END: return int64_t(_bytes.size()) + offset;
  116. }
  117. return int64_t(-1);
  118. }();
  119. if (now < 0 || now > _bytes.size()) {
  120. return -1;
  121. }
  122. _deviceOffset = now;
  123. return now;
  124. }
  125. FrameGenerator::Frame FrameGenerator::Impl::renderCurrent(
  126. QImage storage,
  127. QSize size,
  128. Qt::AspectRatioMode mode) {
  129. Expects(_current.frame != nullptr);
  130. const auto frame = _current.frame.get();
  131. const auto width = frame->width;
  132. const auto height = frame->height;
  133. if (!width || !height) {
  134. LOG(("Webm Error: Bad frame size: %1x%2 ").arg(width).arg(height));
  135. return {};
  136. }
  137. auto scaled = QSize(width, height).scaled(size, mode);
  138. if (!scaled.isEmpty() && rotationSwapWidthHeight()) {
  139. scaled.transpose();
  140. }
  141. if (!GoodStorageForFrame(storage, size)) {
  142. storage = CreateFrameStorage(size);
  143. }
  144. const auto dx = (size.width() - scaled.width()) / 2;
  145. const auto dy = (size.height() - scaled.height()) / 2;
  146. Assert(dx >= 0 && dy >= 0 && (!dx || !dy));
  147. const auto srcFormat = (frame->format == AV_PIX_FMT_NONE)
  148. ? _codec->pix_fmt
  149. : frame->format;
  150. const auto srcSize = QSize(frame->width, frame->height);
  151. const auto dstFormat = AV_PIX_FMT_BGRA;
  152. const auto dstSize = scaled;
  153. const auto bgra = (srcFormat == AV_PIX_FMT_BGRA);
  154. const auto withAlpha = bgra || (srcFormat == AV_PIX_FMT_YUVA420P);
  155. const auto dstPerLine = storage.bytesPerLine();
  156. auto dst = storage.bits() + dx * sizeof(int32) + dy * dstPerLine;
  157. if (srcSize == dstSize && bgra) {
  158. const auto srcPerLine = frame->linesize[0];
  159. const auto perLine = std::min(srcPerLine, int(dstPerLine));
  160. auto src = frame->data[0];
  161. for (auto y = 0, height = srcSize.height(); y != height; ++y) {
  162. memcpy(dst, src, perLine);
  163. src += srcPerLine;
  164. dst += dstPerLine;
  165. }
  166. } else {
  167. _scale = MakeSwscalePointer(
  168. srcSize,
  169. srcFormat,
  170. dstSize,
  171. dstFormat,
  172. &_scale);
  173. Assert(_scale != nullptr);
  174. // AV_NUM_DATA_POINTERS defined in AVFrame struct
  175. uint8_t *dstData[AV_NUM_DATA_POINTERS] = { dst, nullptr };
  176. int dstLinesize[AV_NUM_DATA_POINTERS] = { int(dstPerLine), 0 };
  177. sws_scale(
  178. _scale.get(),
  179. frame->data,
  180. frame->linesize,
  181. 0,
  182. frame->height,
  183. dstData,
  184. dstLinesize);
  185. }
  186. if (dx && size.height() > 0) {
  187. auto dst = storage.bits();
  188. const auto line = scaled.width() * sizeof(int32);
  189. memset(dst, 0, dx * sizeof(int32));
  190. dst += dx * sizeof(int32);
  191. for (auto y = 0; y != size.height() - 1; ++y) {
  192. memset(dst + line, 0, (dstPerLine - line));
  193. dst += dstPerLine;
  194. }
  195. dst += line;
  196. memset(dst, 0, (size.width() - scaled.width() - dx) * sizeof(int32));
  197. } else if (dy && size.width() > 0) {
  198. const auto dst = storage.bits();
  199. memset(dst, 0, dstPerLine * dy);
  200. memset(
  201. dst + dstPerLine * (dy + scaled.height()),
  202. 0,
  203. dstPerLine * (size.height() - scaled.height() - dy));
  204. }
  205. if (withAlpha) {
  206. PremultiplyInplace(storage);
  207. }
  208. if (_rotation != 0) {
  209. auto transform = QTransform();
  210. transform.rotate(_rotation);
  211. storage = storage.transformed(transform);
  212. }
  213. const auto duration = _next.frame
  214. ? (_next.position - _current.position)
  215. : _current.duration;
  216. return {
  217. .duration = duration,
  218. .image = std::move(storage),
  219. .last = !_next.frame,
  220. };
  221. }
  222. FrameGenerator::Frame FrameGenerator::Impl::renderNext(
  223. QImage storage,
  224. QSize size,
  225. Qt::AspectRatioMode mode) {
  226. if (!_codec) {
  227. return {};
  228. } else if (!_current.frame) {
  229. readNextFrame();
  230. }
  231. std::swap(_current, _next);
  232. if (!_current.frame) {
  233. return {};
  234. }
  235. readNextFrame();
  236. return renderCurrent(std::move(storage), size, mode);
  237. }
  238. void FrameGenerator::Impl::jumpToStart() {
  239. if (!_codec) {
  240. return;
  241. }
  242. auto result = 0;
  243. if ((result = avformat_seek_file(_format.get(), _streamId, std::numeric_limits<int64_t>::min(), 0, std::numeric_limits<int64_t>::max(), 0)) < 0) {
  244. if ((result = av_seek_frame(_format.get(), _streamId, 0, AVSEEK_FLAG_BYTE)) < 0) {
  245. if ((result = av_seek_frame(_format.get(), _streamId, 0, AVSEEK_FLAG_FRAME)) < 0) {
  246. if ((result = av_seek_frame(_format.get(), _streamId, 0, 0)) < 0) {
  247. LOG(("Webm Error: Unable to av_seek_frame() to the start, ") + wrapError(result));
  248. return;
  249. }
  250. }
  251. }
  252. }
  253. avcodec_flush_buffers(_codec.get());
  254. _current = ReadFrame();
  255. _next = ReadFrame();
  256. _currentFrameDelay = _nextFrameDelay = 0;
  257. _framePosition = 0;
  258. }
  259. void FrameGenerator::Impl::resolveNextFrameTiming() {
  260. const auto base = _format->streams[_streamId]->time_base;
  261. #if DA_FFMPEG_HAVE_DURATION
  262. const auto duration = _next.frame->duration;
  263. #else
  264. const auto duration = _next.frame->pkt_duration;
  265. #endif
  266. const auto framePts = _next.frame->pts;
  267. auto framePosition = (framePts * 1000LL * base.num) / base.den;
  268. _currentFrameDelay = _nextFrameDelay;
  269. if (_framePosition + _currentFrameDelay < framePosition) {
  270. _currentFrameDelay = int32(framePosition - _framePosition);
  271. } else if (framePosition < _framePosition + _currentFrameDelay) {
  272. framePosition = _framePosition + _currentFrameDelay;
  273. }
  274. if (duration == AV_NOPTS_VALUE) {
  275. _nextFrameDelay = 0;
  276. } else {
  277. _nextFrameDelay = (duration * 1000LL * base.num) / base.den;
  278. }
  279. _framePosition = framePosition;
  280. _next.position = _framePosition;
  281. _next.duration = _nextFrameDelay;
  282. }
  283. void FrameGenerator::Impl::readNextFrame() {
  284. auto frame = _next.frame ? base::take(_next.frame) : MakeFramePointer();
  285. while (true) {
  286. auto result = avcodec_receive_frame(_codec.get(), frame.get());
  287. if (result >= 0) {
  288. if (frame->width * frame->height > kMaxArea) {
  289. return;
  290. }
  291. _next.frame = std::move(frame);
  292. resolveNextFrameTiming();
  293. return;
  294. }
  295. if (result == AVERROR_EOF) {
  296. return;
  297. } else if (result != AVERROR(EAGAIN)) {
  298. LOG(("Webm Error: Unable to avcodec_receive_frame(), ")
  299. + wrapError(result));
  300. return;
  301. }
  302. auto packet = Packet();
  303. auto finished = false;
  304. do {
  305. const auto result = av_read_frame(
  306. _format.get(),
  307. &packet.fields());
  308. if (result == AVERROR_EOF) {
  309. finished = true;
  310. break;
  311. } else if (result < 0) {
  312. LOG(("Webm Error: Unable to av_read_frame(), ")
  313. + wrapError(result));
  314. return;
  315. }
  316. } while (packet.fields().stream_index != _streamId);
  317. if (finished) {
  318. result = avcodec_send_packet(_codec.get(), nullptr); // Drain.
  319. } else {
  320. const auto native = &packet.fields();
  321. const auto guard = gsl::finally([
  322. &,
  323. size = native->size,
  324. data = native->data
  325. ] {
  326. native->size = size;
  327. native->data = data;
  328. packet = Packet();
  329. });
  330. result = avcodec_send_packet(_codec.get(), native);
  331. }
  332. if (result < 0) {
  333. LOG(("Webm Error: Unable to avcodec_send_packet(), ")
  334. + wrapError(result));
  335. return;
  336. }
  337. }
  338. }
  339. QString FrameGenerator::Impl::wrapError(int result) const {
  340. auto error = std::array<char, AV_ERROR_MAX_STRING_SIZE>{};
  341. return u"error %1, %2"_q
  342. .arg(result)
  343. .arg(av_make_error_string(error.data(), error.size(), result));
  344. }
  345. FrameGenerator::FrameGenerator(const QByteArray &bytes)
  346. : _impl(std::make_unique<Impl>(bytes)) {
  347. }
  348. FrameGenerator::~FrameGenerator() = default;
  349. int FrameGenerator::count() {
  350. return 0;
  351. }
  352. double FrameGenerator::rate() {
  353. return 0.;
  354. }
  355. FrameGenerator::Frame FrameGenerator::renderNext(
  356. QImage storage,
  357. QSize size,
  358. Qt::AspectRatioMode mode) {
  359. return _impl->renderNext(std::move(storage), size, mode);
  360. }
  361. FrameGenerator::Frame FrameGenerator::renderCurrent(
  362. QImage storage,
  363. QSize size,
  364. Qt::AspectRatioMode mode) {
  365. return _impl->renderCurrent(std::move(storage), size, mode);
  366. }
  367. void FrameGenerator::jumpToStart() {
  368. _impl->jumpToStart();
  369. }
  370. } // namespace FFmpeg