sync_decoder.cpp 2.0 KB

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