1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include "sync_decoder.h"
#include <c10/util/Logging.h>
namespace ffmpeg {
SyncDecoder::AVByteStorage::AVByteStorage(size_t n) {
ensure(n);
}
SyncDecoder::AVByteStorage::~AVByteStorage() {
av_free(buffer_);
}
void SyncDecoder::AVByteStorage::ensure(size_t n) {
if (tail() < n) {
capacity_ = offset_ + length_ + n;
buffer_ = static_cast<uint8_t*>(av_realloc(buffer_, capacity_));
}
}
uint8_t* SyncDecoder::AVByteStorage::writableTail() {
TORCH_CHECK_LE(offset_ + length_, capacity_);
return buffer_ + offset_ + length_;
}
void SyncDecoder::AVByteStorage::append(size_t n) {
TORCH_CHECK_LE(n, tail());
length_ += n;
}
void SyncDecoder::AVByteStorage::trim(size_t n) {
TORCH_CHECK_LE(n, length_);
offset_ += n;
length_ -= n;
}
const uint8_t* SyncDecoder::AVByteStorage::data() const {
return buffer_ + offset_;
}
size_t SyncDecoder::AVByteStorage::length() const {
return length_;
}
size_t SyncDecoder::AVByteStorage::tail() const {
TORCH_CHECK_LE(offset_ + length_, capacity_);
return capacity_ - offset_ - length_;
}
void SyncDecoder::AVByteStorage::clear() {
offset_ = 0;
length_ = 0;
}
std::unique_ptr<ByteStorage> SyncDecoder::createByteStorage(size_t n) {
return std::make_unique<AVByteStorage>(n);
}
void SyncDecoder::onInit() {
eof_ = false;
queue_.clear();
}
int SyncDecoder::decode(DecoderOutputMessage* out, uint64_t timeoutMs) {
if (eof_ && queue_.empty()) {
return ENODATA;
}
if (queue_.empty()) {
int result = getFrame(timeoutMs);
// assign EOF
eof_ = result == ENODATA;
// check unrecoverable error, any error but ENODATA
if (result && result != ENODATA) {
return result;
}
// still empty
if (queue_.empty()) {
if (eof_) {
return ENODATA;
} else {
LOG(INFO) << "Queue is empty";
return ETIMEDOUT;
}
}
}
*out = std::move(queue_.front());
queue_.pop_front();
return 0;
}
void SyncDecoder::push(DecoderOutputMessage&& buffer) {
queue_.push_back(std::move(buffer));
}
} // namespace ffmpeg
|