2016-06-24 10:37:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-06-24 10:37:29 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
#include "media/clip/media_clip_implementation.h"
|
|
|
|
#include "ffmpeg/ffmpeg_utility.h"
|
2016-07-19 16:02:39 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
extern "C" {
|
2016-06-24 10:37:29 +00:00
|
|
|
#include <libswscale/swscale.h>
|
2020-10-13 16:43:18 +00:00
|
|
|
#include <libavutil/opt.h>
|
2016-07-19 16:02:39 +00:00
|
|
|
} // extern "C"
|
2020-10-13 16:43:18 +00:00
|
|
|
#include <deque>
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
//#include "media/streaming/media_streaming_utility.h"
|
2016-07-14 11:20:46 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace internal {
|
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
constexpr auto kMaxInMemory = 10 * 1024 * 1024;
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
class FFMpegReaderImplementation : public ReaderImplementation {
|
|
|
|
public:
|
2020-10-13 16:43:18 +00:00
|
|
|
FFMpegReaderImplementation(Core::FileLocation *location, QByteArray *data);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ReadResult readFramesTill(crl::time frameMs, crl::time systemMs) override;
|
2016-07-13 11:24:31 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time frameRealTime() const override;
|
|
|
|
crl::time framePresentationTime() const override;
|
2016-07-13 11:24:31 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
bool renderFrame(QImage &to, bool &hasAlpha, const QSize &size) override;
|
2016-07-13 11:24:31 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time durationMs() const override;
|
2016-07-13 11:24:31 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool start(Mode mode, crl::time &positionMs) override;
|
|
|
|
bool inspectAt(crl::time &positionMs);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
QString logData() const;
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
bool isGifv() const;
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
~FFMpegReaderImplementation();
|
|
|
|
|
|
|
|
private:
|
2016-07-12 18:04:34 +00:00
|
|
|
ReadResult readNextFrame();
|
2016-07-22 15:01:24 +00:00
|
|
|
void processReadFrame();
|
2016-07-10 19:44:55 +00:00
|
|
|
|
2016-07-05 17:43:30 +00:00
|
|
|
enum class PacketResult {
|
|
|
|
Ok,
|
|
|
|
EndOfFile,
|
|
|
|
Error,
|
|
|
|
};
|
2019-06-26 15:04:38 +00:00
|
|
|
PacketResult readPacket(FFmpeg::Packet &packet);
|
|
|
|
void processPacket(FFmpeg::Packet &&packet);
|
|
|
|
crl::time countPacketMs(const FFmpeg::Packet &packet) const;
|
2016-07-14 11:20:46 +00:00
|
|
|
PacketResult readAndProcessPacket();
|
|
|
|
|
2016-09-26 07:58:04 +00:00
|
|
|
enum class Rotation {
|
|
|
|
None,
|
|
|
|
Degrees90,
|
|
|
|
Degrees180,
|
|
|
|
Degrees270,
|
|
|
|
};
|
|
|
|
Rotation rotationFromDegrees(int degrees) const;
|
|
|
|
bool rotationSwapWidthHeight() const {
|
|
|
|
return (_rotation == Rotation::Degrees90) || (_rotation == Rotation::Degrees270);
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
static int _read(void *opaque, uint8_t *buf, int buf_size);
|
|
|
|
static int64_t _seek(void *opaque, int64_t offset, int whence);
|
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
Mode _mode = Mode::Silent;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2016-09-26 07:58:04 +00:00
|
|
|
Rotation _rotation = Rotation::None;
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
uchar *_ioBuffer = nullptr;
|
|
|
|
AVIOContext *_ioContext = nullptr;
|
|
|
|
AVFormatContext *_fmtContext = nullptr;
|
|
|
|
AVCodecContext *_codecContext = nullptr;
|
|
|
|
int _streamId = 0;
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::FramePointer _frame;
|
2016-06-24 10:37:29 +00:00
|
|
|
bool _opened = false;
|
|
|
|
bool _hadFrame = false;
|
|
|
|
bool _frameRead = false;
|
2016-12-18 09:12:42 +00:00
|
|
|
int _skippedInvalidDataPackets = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
bool _hasAudioStream = false;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _lastReadVideoMs = 0;
|
|
|
|
crl::time _lastReadAudioMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
std::deque<FFmpeg::Packet> _packetQueue;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
|
|
|
SwsContext *_swsContext = nullptr;
|
|
|
|
QSize _swsSize;
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _frameMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
int _nextFrameDelay = 0;
|
|
|
|
int _currentFrameDelay = 0;
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _frameTime = 0;
|
|
|
|
crl::time _frameTimeCorrection = 0;
|
2016-07-10 19:44:55 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|