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
|
|
|
*/
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_ffmpeg.h"
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
|
|
|
#include "media/audio/media_child_ffmpeg_loader.h"
|
2017-08-15 15:59:40 +00:00
|
|
|
#include "storage/file_download.h"
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace internal {
|
2016-12-18 09:12:42 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr int kSkipInvalidDataPackets = 10;
|
2017-01-05 09:08:16 +00:00
|
|
|
constexpr int kAlignImageBy = 16;
|
|
|
|
|
|
|
|
void alignedImageBufferCleanupHandler(void *data) {
|
|
|
|
auto buffer = static_cast<uchar*>(data);
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a QImage of desired size where all the data is aligned to 16 bytes.
|
|
|
|
QImage createAlignedImage(QSize size) {
|
|
|
|
auto width = size.width();
|
|
|
|
auto height = size.height();
|
|
|
|
auto widthalign = kAlignImageBy / 4;
|
|
|
|
auto neededwidth = width + ((width % widthalign) ? (widthalign - (width % widthalign)) : 0);
|
|
|
|
auto bytesperline = neededwidth * 4;
|
|
|
|
auto buffer = new uchar[bytesperline * height + kAlignImageBy];
|
|
|
|
auto cleanupdata = static_cast<void*>(buffer);
|
|
|
|
auto bufferval = reinterpret_cast<uintptr_t>(buffer);
|
2017-01-05 11:38:40 +00:00
|
|
|
auto alignedbuffer = buffer + ((bufferval % kAlignImageBy) ? (kAlignImageBy - (bufferval % kAlignImageBy)) : 0);
|
2019-02-13 12:18:39 +00:00
|
|
|
return QImage(alignedbuffer, width, height, bytesperline, QImage::Format_ARGB32_Premultiplied, alignedImageBufferCleanupHandler, cleanupdata);
|
2017-01-05 09:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isAlignedImage(const QImage &image) {
|
|
|
|
return !(reinterpret_cast<uintptr_t>(image.constBits()) % kAlignImageBy) && !(image.bytesPerLine() % kAlignImageBy);
|
|
|
|
}
|
2016-12-18 09:12:42 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
FFMpegReaderImplementation::FFMpegReaderImplementation(
|
|
|
|
FileLocation *location,
|
|
|
|
QByteArray *data,
|
|
|
|
const AudioMsgId &audio)
|
|
|
|
: ReaderImplementation(location, data)
|
2019-06-26 15:04:38 +00:00
|
|
|
, _frame(FFmpeg::MakeFramePointer())
|
2017-05-18 20:18:59 +00:00
|
|
|
, _audioMsgId(audio) {
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:04:34 +00:00
|
|
|
ReaderImplementation::ReadResult FFMpegReaderImplementation::readNextFrame() {
|
2016-07-22 15:01:24 +00:00
|
|
|
do {
|
2019-02-28 21:03:25 +00:00
|
|
|
int res = avcodec_receive_frame(_codecContext, _frame.get());
|
2016-07-22 15:01:24 +00:00
|
|
|
if (res >= 0) {
|
|
|
|
processReadFrame();
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Success;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 15:01:24 +00:00
|
|
|
if (res == AVERROR_EOF) {
|
2019-02-28 21:03:25 +00:00
|
|
|
_packetQueue.clear();
|
2016-07-05 17:44:02 +00:00
|
|
|
if (_mode == Mode::Normal) {
|
2016-07-30 10:03:44 +00:00
|
|
|
return ReadResult::EndOfFile;
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
2016-12-18 09:12:42 +00:00
|
|
|
if (!_hadFrame) {
|
|
|
|
LOG(("Gif Error: Got EOF before a single frame was read!"));
|
|
|
|
return ReadResult::Error;
|
|
|
|
}
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
if ((res = avformat_seek_file(_fmtContext, _streamId, std::numeric_limits<int64_t>::min(), 0, std::numeric_limits<int64_t>::max(), 0)) < 0) {
|
|
|
|
if ((res = av_seek_frame(_fmtContext, _streamId, 0, AVSEEK_FLAG_BYTE)) < 0) {
|
|
|
|
if ((res = av_seek_frame(_fmtContext, _streamId, 0, AVSEEK_FLAG_FRAME)) < 0) {
|
|
|
|
if ((res = av_seek_frame(_fmtContext, _streamId, 0, 0)) < 0) {
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
|
|
|
LOG(("Gif Error: Unable to av_seek_frame() to the start %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Error;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
avcodec_flush_buffers(_codecContext);
|
|
|
|
_hadFrame = false;
|
|
|
|
_frameMs = 0;
|
2016-07-21 17:35:55 +00:00
|
|
|
_lastReadVideoMs = _lastReadAudioMs = 0;
|
2016-12-18 09:12:42 +00:00
|
|
|
_skippedInvalidDataPackets = 0;
|
2016-07-22 15:01:24 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (res != AVERROR(EAGAIN)) {
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
2016-12-18 09:12:42 +00:00
|
|
|
LOG(("Gif Error: Unable to avcodec_receive_frame() %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-22 15:01:24 +00:00
|
|
|
return ReadResult::Error;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2016-07-22 15:01:24 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
while (_packetQueue.empty()) {
|
2016-07-22 15:01:24 +00:00
|
|
|
auto packetResult = readAndProcessPacket();
|
|
|
|
if (packetResult == PacketResult::Error) {
|
|
|
|
return ReadResult::Error;
|
|
|
|
} else if (packetResult == PacketResult::EndOfFile) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 21:03:25 +00:00
|
|
|
if (_packetQueue.empty()) {
|
2016-07-22 15:01:24 +00:00
|
|
|
avcodec_send_packet(_codecContext, nullptr); // drain
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
auto packet = std::move(_packetQueue.front());
|
|
|
|
_packetQueue.pop_front();
|
|
|
|
|
|
|
|
const auto native = &packet.fields();
|
|
|
|
const auto guard = gsl::finally([
|
|
|
|
&,
|
|
|
|
size = native->size,
|
|
|
|
data = native->data
|
|
|
|
] {
|
|
|
|
native->size = size;
|
|
|
|
native->data = data;
|
2019-06-26 15:04:38 +00:00
|
|
|
packet = FFmpeg::Packet();
|
2019-02-28 21:03:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
res = avcodec_send_packet(_codecContext, native);
|
2016-07-22 15:01:24 +00:00
|
|
|
if (res < 0) {
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
2016-12-18 09:12:42 +00:00
|
|
|
LOG(("Gif Error: Unable to avcodec_send_packet() %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-22 15:01:24 +00:00
|
|
|
if (res == AVERROR_INVALIDDATA) {
|
2016-12-18 09:12:42 +00:00
|
|
|
if (++_skippedInvalidDataPackets < kSkipInvalidDataPackets) {
|
|
|
|
continue; // try to skip bad packet
|
|
|
|
}
|
2016-07-22 15:01:24 +00:00
|
|
|
}
|
|
|
|
return ReadResult::Error;
|
|
|
|
}
|
|
|
|
} while (true);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Error;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 15:01:24 +00:00
|
|
|
void FFMpegReaderImplementation::processReadFrame() {
|
2017-11-13 07:07:31 +00:00
|
|
|
int64 duration = _frame->pkt_duration;
|
2017-01-05 09:08:16 +00:00
|
|
|
int64 framePts = _frame->pts;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time frameMs = (framePts * 1000LL * _fmtContext->streams[_streamId]->time_base.num) / _fmtContext->streams[_streamId]->time_base.den;
|
2016-07-22 15:01:24 +00:00
|
|
|
_currentFrameDelay = _nextFrameDelay;
|
|
|
|
if (_frameMs + _currentFrameDelay < frameMs) {
|
|
|
|
_currentFrameDelay = int32(frameMs - _frameMs);
|
|
|
|
} else if (frameMs < _frameMs + _currentFrameDelay) {
|
|
|
|
frameMs = _frameMs + _currentFrameDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duration == AV_NOPTS_VALUE) {
|
|
|
|
_nextFrameDelay = 0;
|
|
|
|
} else {
|
|
|
|
_nextFrameDelay = (duration * 1000LL * _fmtContext->streams[_streamId]->time_base.num) / _fmtContext->streams[_streamId]->time_base.den;
|
|
|
|
}
|
|
|
|
_frameMs = frameMs;
|
|
|
|
|
|
|
|
_hadFrame = _frameRead = true;
|
|
|
|
_frameTime += _currentFrameDelay;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ReaderImplementation::ReadResult FFMpegReaderImplementation::readFramesTill(crl::time frameMs, crl::time systemMs) {
|
2016-07-12 18:04:34 +00:00
|
|
|
if (_audioStreamId < 0) { // just keep up
|
2016-07-14 11:20:46 +00:00
|
|
|
if (_frameRead && _frameTime > frameMs) {
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Success;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
auto readResult = readNextFrame();
|
2016-07-14 11:20:46 +00:00
|
|
|
if (readResult != ReadResult::Success || _frameTime > frameMs) {
|
2016-07-12 18:04:34 +00:00
|
|
|
return readResult;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
readResult = readNextFrame();
|
2016-07-14 11:20:46 +00:00
|
|
|
if (_frameTime <= frameMs) {
|
|
|
|
_frameTime = frameMs + 5; // keep up
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
return readResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync by audio stream
|
2019-02-28 21:03:25 +00:00
|
|
|
auto correctMs = (frameMs >= 0)
|
|
|
|
? Player::mixer()->getExternalCorrectedTime(
|
|
|
|
_audioMsgId,
|
|
|
|
frameMs,
|
|
|
|
systemMs)
|
|
|
|
: frameMs;
|
2016-07-12 18:04:34 +00:00
|
|
|
if (!_frameRead) {
|
|
|
|
auto readResult = readNextFrame();
|
|
|
|
if (readResult != ReadResult::Success) {
|
|
|
|
return readResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (_frameTime <= correctMs) {
|
|
|
|
auto readResult = readNextFrame();
|
|
|
|
if (readResult != ReadResult::Success) {
|
|
|
|
return readResult;
|
|
|
|
}
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-14 11:20:46 +00:00
|
|
|
if (frameMs >= 0) {
|
|
|
|
_frameTimeCorrection = frameMs - correctMs;
|
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Success;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time FFMpegReaderImplementation::frameRealTime() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
return _frameMs;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time FFMpegReaderImplementation::framePresentationTime() const {
|
2019-03-01 14:41:10 +00:00
|
|
|
return qMax(_frameTime + _frameTimeCorrection, crl::time(0));
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time FFMpegReaderImplementation::durationMs() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
if (_fmtContext->streams[_streamId]->duration == AV_NOPTS_VALUE) return 0;
|
|
|
|
return (_fmtContext->streams[_streamId]->duration * 1000LL * _fmtContext->streams[_streamId]->time_base.num) / _fmtContext->streams[_streamId]->time_base.den;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
bool FFMpegReaderImplementation::renderFrame(QImage &to, bool &hasAlpha, const QSize &size) {
|
2017-05-18 20:18:59 +00:00
|
|
|
Expects(_frameRead);
|
2016-06-24 10:37:29 +00:00
|
|
|
_frameRead = false;
|
|
|
|
|
|
|
|
if (!_width || !_height) {
|
|
|
|
_width = _frame->width;
|
|
|
|
_height = _frame->height;
|
|
|
|
if (!_width || !_height) {
|
|
|
|
LOG(("Gif Error: Bad frame size %1").arg(logData()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QSize toSize(size.isEmpty() ? QSize(_width, _height) : size);
|
2016-09-26 07:58:04 +00:00
|
|
|
if (!size.isEmpty() && rotationSwapWidthHeight()) {
|
|
|
|
toSize.transpose();
|
|
|
|
}
|
2017-01-05 09:08:16 +00:00
|
|
|
if (to.isNull() || to.size() != toSize || !to.isDetached() || !isAlignedImage(to)) {
|
|
|
|
to = createAlignedImage(toSize);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
hasAlpha = (_frame->format == AV_PIX_FMT_BGRA || (_frame->format == -1 && _codecContext->pix_fmt == AV_PIX_FMT_BGRA));
|
|
|
|
if (_frame->width == toSize.width() && _frame->height == toSize.height() && hasAlpha) {
|
|
|
|
int32 sbpl = _frame->linesize[0], dbpl = to.bytesPerLine(), bpl = qMin(sbpl, dbpl);
|
|
|
|
uchar *s = _frame->data[0], *d = to.bits();
|
|
|
|
for (int32 i = 0, l = _frame->height; i < l; ++i) {
|
|
|
|
memcpy(d + i * dbpl, s + i * sbpl, bpl);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((_swsSize != toSize) || (_frame->format != -1 && _frame->format != _codecContext->pix_fmt) || !_swsContext) {
|
|
|
|
_swsSize = toSize;
|
2019-02-28 21:03:25 +00:00
|
|
|
_swsContext = sws_getCachedContext(_swsContext, _frame->width, _frame->height, AVPixelFormat(_frame->format), toSize.width(), toSize.height(), AV_PIX_FMT_BGRA, 0, nullptr, nullptr, nullptr);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2017-01-05 09:08:16 +00:00
|
|
|
// AV_NUM_DATA_POINTERS defined in AVFrame struct
|
|
|
|
uint8_t *toData[AV_NUM_DATA_POINTERS] = { to.bits(), nullptr };
|
|
|
|
int toLinesize[AV_NUM_DATA_POINTERS] = { to.bytesPerLine(), 0 };
|
|
|
|
int res;
|
2016-06-24 10:37:29 +00:00
|
|
|
if ((res = sws_scale(_swsContext, _frame->data, _frame->linesize, 0, _frame->height, toData, toLinesize)) != _swsSize.height()) {
|
|
|
|
LOG(("Gif Error: Unable to sws_scale to good size %1, height %2, should be %3").arg(logData()).arg(res).arg(_swsSize.height()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 11:12:58 +00:00
|
|
|
if (hasAlpha) {
|
|
|
|
FFmpeg::PremultiplyInplace(to);
|
|
|
|
}
|
2016-09-26 07:58:04 +00:00
|
|
|
if (_rotation != Rotation::None) {
|
|
|
|
QTransform rotationTransform;
|
|
|
|
switch (_rotation) {
|
|
|
|
case Rotation::Degrees90: rotationTransform.rotate(90); break;
|
|
|
|
case Rotation::Degrees180: rotationTransform.rotate(180); break;
|
|
|
|
case Rotation::Degrees270: rotationTransform.rotate(270); break;
|
|
|
|
}
|
|
|
|
to = to.transformed(rotationTransform);
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
// Read some future packets for audio stream.
|
2016-07-13 17:34:57 +00:00
|
|
|
if (_audioStreamId >= 0) {
|
2016-07-21 17:35:55 +00:00
|
|
|
while (_frameMs + 5000 > _lastReadAudioMs
|
|
|
|
&& _frameMs + 15000 > _lastReadVideoMs) {
|
2016-07-14 11:20:46 +00:00
|
|
|
auto packetResult = readAndProcessPacket();
|
2016-07-05 17:44:02 +00:00
|
|
|
if (packetResult != PacketResult::Ok) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::ClearFrameMemory(_frame.get());
|
2019-02-28 21:03:25 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-26 07:58:04 +00:00
|
|
|
FFMpegReaderImplementation::Rotation FFMpegReaderImplementation::rotationFromDegrees(int degrees) const {
|
|
|
|
switch (degrees) {
|
|
|
|
case 90: return Rotation::Degrees90;
|
|
|
|
case 180: return Rotation::Degrees180;
|
|
|
|
case 270: return Rotation::Degrees270;
|
|
|
|
}
|
|
|
|
return Rotation::None;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool FFMpegReaderImplementation::start(Mode mode, crl::time &positionMs) {
|
2016-07-05 17:44:02 +00:00
|
|
|
_mode = mode;
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
initDevice();
|
|
|
|
if (!_device->open(QIODevice::ReadOnly)) {
|
|
|
|
LOG(("Gif Error: Unable to open device %1").arg(logData()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_ioBuffer = (uchar*)av_malloc(AVBlockSize);
|
2019-02-28 21:03:25 +00:00
|
|
|
_ioContext = avio_alloc_context(_ioBuffer, AVBlockSize, 0, static_cast<void*>(this), &FFMpegReaderImplementation::_read, nullptr, &FFMpegReaderImplementation::_seek);
|
2016-06-24 10:37:29 +00:00
|
|
|
_fmtContext = avformat_alloc_context();
|
|
|
|
if (!_fmtContext) {
|
|
|
|
LOG(("Gif Error: Unable to avformat_alloc_context %1").arg(logData()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_fmtContext->pb = _ioContext;
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
2019-02-28 21:03:25 +00:00
|
|
|
if ((res = avformat_open_input(&_fmtContext, nullptr, nullptr, nullptr)) < 0) {
|
|
|
|
_ioBuffer = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
LOG(("Gif Error: Unable to avformat_open_input %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_opened = true;
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
if ((res = avformat_find_stream_info(_fmtContext, nullptr)) < 0) {
|
2016-06-24 10:37:29 +00:00
|
|
|
LOG(("Gif Error: Unable to avformat_find_stream_info %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
_streamId = av_find_best_stream(_fmtContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (_streamId < 0) {
|
|
|
|
LOG(("Gif Error: Unable to av_find_best_stream %1, error %2, %3").arg(logData()).arg(_streamId).arg(av_make_error_string(err, sizeof(err), _streamId)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
auto rotateTag = av_dict_get(_fmtContext->streams[_streamId]->metadata, "rotate", nullptr, 0);
|
2016-09-26 07:58:04 +00:00
|
|
|
if (rotateTag && *rotateTag->value) {
|
|
|
|
auto stringRotateTag = QString::fromUtf8(rotateTag->value);
|
|
|
|
auto toIntSucceeded = false;
|
|
|
|
auto rotateDegrees = stringRotateTag.toInt(&toIntSucceeded);
|
|
|
|
if (toIntSucceeded) {
|
|
|
|
_rotation = rotationFromDegrees(rotateDegrees);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 15:01:24 +00:00
|
|
|
_codecContext = avcodec_alloc_context3(nullptr);
|
|
|
|
if (!_codecContext) {
|
2016-12-18 09:12:42 +00:00
|
|
|
LOG(("Gif Error: Unable to avcodec_alloc_context3 %1").arg(logData()));
|
2016-07-22 15:01:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((res = avcodec_parameters_to_context(_codecContext, _fmtContext->streams[_streamId]->codecpar)) < 0) {
|
2016-12-18 09:12:42 +00:00
|
|
|
LOG(("Gif Error: Unable to avcodec_parameters_to_context %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-22 15:01:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
av_codec_set_pkt_timebase(_codecContext, _fmtContext->streams[_streamId]->time_base);
|
|
|
|
av_opt_set_int(_codecContext, "refcounted_frames", 1, 0);
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
const auto codec = avcodec_find_decoder(_codecContext->codec_id);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
_audioStreamId = av_find_best_stream(_fmtContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
|
2017-03-09 09:47:45 +00:00
|
|
|
if (_mode == Mode::Inspecting) {
|
|
|
|
_hasAudioStream = (_audioStreamId >= 0);
|
|
|
|
_audioStreamId = -1;
|
2019-02-28 21:03:25 +00:00
|
|
|
} else if (_mode == Mode::Silent || !_audioMsgId.externalPlayId()) {
|
2016-07-05 17:43:30 +00:00
|
|
|
_audioStreamId = -1;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2016-07-22 15:01:24 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
if ((res = avcodec_open2(_codecContext, codec, nullptr)) < 0) {
|
2016-06-24 10:37:29 +00:00
|
|
|
LOG(("Gif Error: Unable to avcodec_open2 %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
std::unique_ptr<ExternalSoundData> soundData;
|
2016-07-05 17:44:02 +00:00
|
|
|
if (_audioStreamId >= 0) {
|
2017-03-09 09:47:45 +00:00
|
|
|
auto audioContext = avcodec_alloc_context3(nullptr);
|
2016-07-22 15:01:24 +00:00
|
|
|
if (!audioContext) {
|
|
|
|
LOG(("Audio Error: Unable to avcodec_alloc_context3 %1").arg(logData()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((res = avcodec_parameters_to_context(audioContext, _fmtContext->streams[_audioStreamId]->codecpar)) < 0) {
|
|
|
|
LOG(("Audio Error: Unable to avcodec_parameters_to_context %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-05 17:44:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-22 15:01:24 +00:00
|
|
|
av_codec_set_pkt_timebase(audioContext, _fmtContext->streams[_audioStreamId]->time_base);
|
2016-07-05 17:44:02 +00:00
|
|
|
av_opt_set_int(audioContext, "refcounted_frames", 1, 0);
|
2016-07-22 15:01:24 +00:00
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
const auto audioCodec = avcodec_find_decoder(audioContext->codec_id);
|
2016-07-05 17:44:02 +00:00
|
|
|
if ((res = avcodec_open2(audioContext, audioCodec, 0)) < 0) {
|
|
|
|
avcodec_free_context(&audioContext);
|
|
|
|
LOG(("Gif Error: Unable to avcodec_open2 %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-07-21 17:35:55 +00:00
|
|
|
_audioStreamId = -1;
|
2016-07-05 17:44:02 +00:00
|
|
|
} else {
|
2019-02-28 21:03:25 +00:00
|
|
|
soundData = std::make_unique<ExternalSoundData>();
|
2019-06-26 15:04:38 +00:00
|
|
|
soundData->codec = FFmpeg::CodecPointer(audioContext);
|
2016-07-22 15:01:24 +00:00
|
|
|
soundData->frequency = _fmtContext->streams[_audioStreamId]->codecpar->sample_rate;
|
2016-07-21 17:35:55 +00:00
|
|
|
if (_fmtContext->streams[_audioStreamId]->duration == AV_NOPTS_VALUE) {
|
|
|
|
soundData->length = (_fmtContext->duration * soundData->frequency) / AV_TIME_BASE;
|
|
|
|
} else {
|
|
|
|
soundData->length = (_fmtContext->streams[_audioStreamId]->duration * soundData->frequency * _fmtContext->streams[_audioStreamId]->time_base.num) / _fmtContext->streams[_audioStreamId]->time_base.den;
|
|
|
|
}
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
2016-07-14 11:20:46 +00:00
|
|
|
if (positionMs > 0) {
|
2017-12-10 08:52:38 +00:00
|
|
|
const auto timeBase = _fmtContext->streams[_streamId]->time_base;
|
|
|
|
const auto timeStamp = (positionMs * timeBase.den)
|
|
|
|
/ (1000LL * timeBase.num);
|
|
|
|
if (av_seek_frame(_fmtContext, _streamId, timeStamp, 0) < 0) {
|
|
|
|
if (av_seek_frame(_fmtContext, _streamId, timeStamp, AVSEEK_FLAG_BACKWARD) < 0) {
|
2016-07-14 11:20:46 +00:00
|
|
|
return false;
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::Packet packet;
|
2019-02-28 21:03:25 +00:00
|
|
|
auto readResult = readPacket(packet);
|
2016-07-14 11:20:46 +00:00
|
|
|
if (readResult == PacketResult::Ok && positionMs > 0) {
|
2019-02-28 21:03:25 +00:00
|
|
|
positionMs = countPacketMs(packet);
|
2016-07-14 11:20:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 13:16:39 +00:00
|
|
|
if (hasAudio()) {
|
2017-12-10 08:52:38 +00:00
|
|
|
Player::mixer()->play(_audioMsgId, std::move(soundData), positionMs);
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 11:20:46 +00:00
|
|
|
if (readResult == PacketResult::Ok) {
|
2019-02-28 21:03:25 +00:00
|
|
|
processPacket(std::move(packet));
|
2016-07-14 11:20:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool FFMpegReaderImplementation::inspectAt(crl::time &positionMs) {
|
2017-03-09 09:47:45 +00:00
|
|
|
if (positionMs > 0) {
|
2017-12-10 08:52:38 +00:00
|
|
|
const auto timeBase = _fmtContext->streams[_streamId]->time_base;
|
|
|
|
const auto timeStamp = (positionMs * timeBase.den)
|
|
|
|
/ (1000LL * timeBase.num);
|
|
|
|
if (av_seek_frame(_fmtContext, _streamId, timeStamp, 0) < 0) {
|
|
|
|
if (av_seek_frame(_fmtContext, _streamId, timeStamp, AVSEEK_FLAG_BACKWARD) < 0) {
|
2017-03-09 09:47:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_packetQueue.clear();
|
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::Packet packet;
|
2019-02-28 21:03:25 +00:00
|
|
|
auto readResult = readPacket(packet);
|
2017-03-09 09:47:45 +00:00
|
|
|
if (readResult == PacketResult::Ok && positionMs > 0) {
|
2019-02-28 21:03:25 +00:00
|
|
|
positionMs = countPacketMs(packet);
|
2017-03-09 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (readResult == PacketResult::Ok) {
|
2019-02-28 21:03:25 +00:00
|
|
|
processPacket(std::move(packet));
|
2017-03-09 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FFMpegReaderImplementation::isGifv() const {
|
|
|
|
if (_hasAudioStream) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-15 15:59:40 +00:00
|
|
|
if (dataSize() > Storage::kMaxAnimationInMemory) {
|
2017-03-09 09:47:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_codecContext->codec_id != AV_CODEC_ID_H264) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
QString FFMpegReaderImplementation::logData() const {
|
|
|
|
return qsl("for file '%1', data size '%2'").arg(_location ? _location->name() : QString()).arg(_data->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
FFMpegReaderImplementation::~FFMpegReaderImplementation() {
|
2016-07-22 15:01:24 +00:00
|
|
|
if (_codecContext) avcodec_free_context(&_codecContext);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (_swsContext) sws_freeContext(_swsContext);
|
|
|
|
if (_opened) {
|
|
|
|
avformat_close_input(&_fmtContext);
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
if (_ioContext) {
|
2017-01-09 13:12:53 +00:00
|
|
|
av_freep(&_ioContext->buffer);
|
|
|
|
av_freep(&_ioContext);
|
2016-06-24 10:37:29 +00:00
|
|
|
} else if (_ioBuffer) {
|
2017-01-09 13:12:53 +00:00
|
|
|
av_freep(&_ioBuffer);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
if (_fmtContext) avformat_free_context(_fmtContext);
|
2016-07-05 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
FFMpegReaderImplementation::PacketResult FFMpegReaderImplementation::readPacket(FFmpeg::Packet &packet) {
|
2016-07-05 17:43:30 +00:00
|
|
|
int res = 0;
|
2019-02-28 21:03:25 +00:00
|
|
|
if ((res = av_read_frame(_fmtContext, &packet.fields())) < 0) {
|
2016-07-05 17:43:30 +00:00
|
|
|
if (res == AVERROR_EOF) {
|
2016-07-05 17:44:22 +00:00
|
|
|
if (_audioStreamId >= 0) {
|
|
|
|
// queue terminating packet to audio player
|
2019-12-19 15:14:05 +00:00
|
|
|
auto empty = FFmpeg::Packet();
|
2019-02-28 21:03:25 +00:00
|
|
|
Player::mixer()->feedFromExternal({
|
|
|
|
_audioMsgId,
|
2019-12-19 15:14:05 +00:00
|
|
|
gsl::make_span(&empty, 1)
|
2019-02-28 21:03:25 +00:00
|
|
|
});
|
2016-07-05 17:44:22 +00:00
|
|
|
}
|
2016-07-05 17:43:30 +00:00
|
|
|
return PacketResult::EndOfFile;
|
|
|
|
}
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
|
|
|
LOG(("Gif Error: Unable to av_read_frame() %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
|
|
|
return PacketResult::Error;
|
|
|
|
}
|
2016-07-14 11:20:46 +00:00
|
|
|
return PacketResult::Ok;
|
|
|
|
}
|
2016-07-05 17:43:30 +00:00
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
void FFMpegReaderImplementation::processPacket(FFmpeg::Packet &&packet) {
|
2019-02-28 21:03:25 +00:00
|
|
|
const auto &native = packet.fields();
|
|
|
|
auto videoPacket = (native.stream_index == _streamId);
|
|
|
|
auto audioPacket = (_audioStreamId >= 0 && native.stream_index == _audioStreamId);
|
2016-07-05 17:43:30 +00:00
|
|
|
if (audioPacket || videoPacket) {
|
|
|
|
if (videoPacket) {
|
2016-07-21 17:35:55 +00:00
|
|
|
_lastReadVideoMs = countPacketMs(packet);
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
_packetQueue.push_back(std::move(packet));
|
2016-07-05 17:43:30 +00:00
|
|
|
} else if (audioPacket) {
|
2016-07-21 17:35:55 +00:00
|
|
|
_lastReadAudioMs = countPacketMs(packet);
|
|
|
|
|
2016-07-05 17:43:30 +00:00
|
|
|
// queue packet to audio player
|
2019-02-28 21:03:25 +00:00
|
|
|
Player::mixer()->feedFromExternal({
|
|
|
|
_audioMsgId,
|
2019-12-19 15:14:05 +00:00
|
|
|
gsl::make_span(&packet, 1)
|
2019-02-28 21:03:25 +00:00
|
|
|
});
|
2016-07-05 17:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 11:20:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
crl::time FFMpegReaderImplementation::countPacketMs(
|
2019-06-26 15:04:38 +00:00
|
|
|
const FFmpeg::Packet &packet) const {
|
2019-02-28 21:03:25 +00:00
|
|
|
const auto &native = packet.fields();
|
|
|
|
int64 packetPts = (native.pts == AV_NOPTS_VALUE) ? native.dts : native.pts;
|
|
|
|
crl::time packetMs = (packetPts * 1000LL * _fmtContext->streams[native.stream_index]->time_base.num) / _fmtContext->streams[native.stream_index]->time_base.den;
|
2016-07-14 11:20:46 +00:00
|
|
|
return packetMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
FFMpegReaderImplementation::PacketResult FFMpegReaderImplementation::readAndProcessPacket() {
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::Packet packet;
|
2019-02-28 21:03:25 +00:00
|
|
|
auto result = readPacket(packet);
|
2016-07-14 11:20:46 +00:00
|
|
|
if (result == PacketResult::Ok) {
|
2019-02-28 21:03:25 +00:00
|
|
|
processPacket(std::move(packet));
|
2016-07-14 11:20:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
2016-07-05 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
int FFMpegReaderImplementation::_read(void *opaque, uint8_t *buf, int buf_size) {
|
|
|
|
FFMpegReaderImplementation *l = reinterpret_cast<FFMpegReaderImplementation*>(opaque);
|
|
|
|
return int(l->_device->read((char*)(buf), buf_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t FFMpegReaderImplementation::_seek(void *opaque, int64_t offset, int whence) {
|
|
|
|
FFMpegReaderImplementation *l = reinterpret_cast<FFMpegReaderImplementation*>(opaque);
|
|
|
|
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET: return l->_device->seek(offset) ? l->_device->pos() : -1;
|
|
|
|
case SEEK_CUR: return l->_device->seek(l->_device->pos() + offset) ? l->_device->pos() : -1;
|
|
|
|
case SEEK_END: return l->_device->seek(l->_device->size() + offset) ? l->_device->pos() : -1;
|
2017-03-01 07:31:36 +00:00
|
|
|
case AVSEEK_SIZE: {
|
|
|
|
// Special whence for determining filesize without any seek.
|
|
|
|
return l->_dataSize;
|
|
|
|
} break;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|