2016-06-24 10:37:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
extern "C" {
|
2016-07-19 16:02:39 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
2016-07-19 16:02:39 +00:00
|
|
|
} // extern "C"
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-19 16:02:39 +00:00
|
|
|
#include "media/media_clip_implementation.h"
|
|
|
|
#include "media/media_child_ffmpeg_loader.h"
|
2016-07-14 11:20:46 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class FFMpegReaderImplementation : public ReaderImplementation {
|
|
|
|
public:
|
2016-07-12 11:38:16 +00:00
|
|
|
FFMpegReaderImplementation(FileLocation *location, QByteArray *data, uint64 playId);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
ReadResult readFramesTill(TimeMs frameMs, TimeMs systemMs) override;
|
2016-07-13 11:24:31 +00:00
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs frameRealTime() const override;
|
|
|
|
TimeMs 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
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs durationMs() const override;
|
2016-07-12 18:04:34 +00:00
|
|
|
bool hasAudio() const override {
|
|
|
|
return (_audioStreamId >= 0);
|
|
|
|
}
|
2016-07-13 11:24:31 +00:00
|
|
|
void pauseAudio() override;
|
|
|
|
void resumeAudio() override;
|
|
|
|
|
2016-07-14 11:20:46 +00:00
|
|
|
bool start(Mode mode, int64 &positionMs) override;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
QString logData() const;
|
|
|
|
|
|
|
|
~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,
|
|
|
|
};
|
2016-07-14 11:20:46 +00:00
|
|
|
PacketResult readPacket(AVPacket *packet);
|
|
|
|
void processPacket(AVPacket *packet);
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs countPacketMs(AVPacket *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-07-05 17:43:30 +00:00
|
|
|
void startPacket();
|
|
|
|
void finishPacket();
|
|
|
|
void clearPacketQueue();
|
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);
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
Mode _mode = Mode::Normal;
|
|
|
|
|
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;
|
|
|
|
AVCodec *_codec = nullptr;
|
|
|
|
AVCodecContext *_codecContext = nullptr;
|
|
|
|
int _streamId = 0;
|
|
|
|
AVFrame *_frame = nullptr;
|
|
|
|
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
|
|
|
|
2016-08-15 07:39:12 +00:00
|
|
|
int _audioStreamId = -1;
|
2016-07-05 17:44:02 +00:00
|
|
|
uint64 _playId = 0;
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs _lastReadVideoMs = 0;
|
|
|
|
TimeMs _lastReadAudioMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-19 16:02:39 +00:00
|
|
|
QQueue<FFMpeg::AVPacketDataWrap> _packetQueue;
|
2016-07-05 17:43:30 +00:00
|
|
|
AVPacket _packetNull; // for final decoding
|
|
|
|
int _packetStartedSize = 0;
|
|
|
|
uint8_t *_packetStartedData = nullptr;
|
|
|
|
bool _packetStarted = false;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
|
|
|
SwsContext *_swsContext = nullptr;
|
|
|
|
QSize _swsSize;
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs _frameMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
int _nextFrameDelay = 0;
|
|
|
|
int _currentFrameDelay = 0;
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs _frameTime = 0;
|
|
|
|
TimeMs _frameTimeCorrection = 0;
|
2016-07-10 19:44:55 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|