"tools/distgraphlaunch.py" did not exist on "b9ef70e5ac65280a48424d554c359bfb92c3e5af"
sync_decoder.cpp 1.86 KB
Newer Older
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
// Copyright 2004-present Facebook. All Rights Reserved.

#include "sync_decoder.h"
#include <c10/util/Logging.h>

namespace ffmpeg {

SyncDecoder::VectorByteStorage::VectorByteStorage(size_t n) {
  buffer_.resize(n);
}

void SyncDecoder::VectorByteStorage::ensure(size_t n) {
  if (tail() < n) {
    buffer_.resize(offset_ + length_ + n);
  }
}

uint8_t* SyncDecoder::VectorByteStorage::writableTail() {
  CHECK_LE(offset_ + length_, buffer_.size());
  return buffer_.data() + offset_ + length_;
}

void SyncDecoder::VectorByteStorage::append(size_t n) {
  CHECK_LE(n, tail());
  length_ += n;
}

void SyncDecoder::VectorByteStorage::trim(size_t n) {
  CHECK_LE(n, length_);
  offset_ += n;
  length_ -= n;
}

const uint8_t* SyncDecoder::VectorByteStorage::data() const {
  return buffer_.data() + offset_;
}

size_t SyncDecoder::VectorByteStorage::length() const {
  return length_;
}

size_t SyncDecoder::VectorByteStorage::tail() const {
  auto size = buffer_.size();
  CHECK_LE(offset_ + length_, buffer_.size());
  return size - offset_ - length_;
}

void SyncDecoder::VectorByteStorage::clear() {
  buffer_.clear();
  offset_ = 0;
  length_ = 0;
}

std::unique_ptr<ByteStorage> SyncDecoder::createByteStorage(size_t n) {
  return std::make_unique<VectorByteStorage>(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 = getBytes(timeoutMs);
    eof_ = result == ENODATA;

    if (result && result != ENODATA) {
      return result;
    }

    // still empty
    if (queue_.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