aboutsummaryrefslogtreecommitdiff
path: root/cast/standalone_receiver/decoder.cc
blob: 8421cd0f2e8c51bf364728f6882f8f8316703ec5 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "cast/standalone_receiver/decoder.h"

#include <sstream>

#include "util/logging.h"

namespace cast {
namespace streaming {

Decoder::Buffer::Buffer() {
  Resize(0);
}

Decoder::Buffer::~Buffer() = default;

void Decoder::Buffer::Resize(int new_size) {
  const int padded_size = new_size + AV_INPUT_BUFFER_PADDING_SIZE;
  if (static_cast<int>(buffer_.size()) == padded_size) {
    return;
  }
  buffer_.resize(padded_size);
  // libavcodec requires zero-padding the region at the end, as some decoders
  // will treat this as a stop marker.
  memset(buffer_.data() + new_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
}

absl::Span<const uint8_t> Decoder::Buffer::GetSpan() const {
  return const_cast<Buffer*>(this)->GetSpan();
}

absl::Span<uint8_t> Decoder::Buffer::GetSpan() {
  return absl::Span<uint8_t>(buffer_.data(),
                             buffer_.size() - AV_INPUT_BUFFER_PADDING_SIZE);
}

Decoder::Client::Client() = default;
Decoder::Client::~Client() = default;

Decoder::Decoder() = default;
Decoder::~Decoder() = default;

void Decoder::Decode(FrameId frame_id, const Decoder::Buffer& buffer) {
  if (!codec_) {
    InitFromFirstBuffer(buffer);
    if (!codec_) {
      OnError("unable to detect codec", AVERROR(EINVAL), frame_id);
      return;
    }
  }

  // Parse the buffer for the required metadata and the packet to send to the
  // decoder.
  const absl::Span<const uint8_t> input = buffer.GetSpan();
  const int bytes_consumed = av_parser_parse2(
      parser_.get(), context_.get(), &packet_->data, &packet_->size,
      input.data(), input.size(), AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  if (bytes_consumed < 0) {
    OnError("av_parser_parse2", bytes_consumed, frame_id);
    return;
  }
  if (!packet_->data) {
    OnError("av_parser_parse2 found no packet", AVERROR_BUFFER_TOO_SMALL,
            frame_id);
    return;
  }

  // Send the packet to the decoder.
  const int send_packet_result =
      avcodec_send_packet(context_.get(), packet_.get());
  if (send_packet_result < 0) {
    // The result should not be EAGAIN because this code always pulls out all
    // the decoded frames after feeding-in each AVPacket.
    OSP_DCHECK_NE(send_packet_result, AVERROR(EAGAIN));
    OnError("avcodec_send_packet", send_packet_result, frame_id);
    return;
  }
  frames_decoding_.push_back(frame_id);

  // Receive zero or more frames from the decoder.
  for (;;) {
    const int receive_frame_result =
        avcodec_receive_frame(context_.get(), decoded_frame_.get());
    if (receive_frame_result == AVERROR(EAGAIN)) {
      break;  // Decoder needs more input to produce another frame.
    }
    const FrameId decoded_frame_id = DidReceiveFrameFromDecoder();
    if (receive_frame_result < 0) {
      OnError("avcodec_receive_frame", receive_frame_result, decoded_frame_id);
      return;
    }
    if (client_) {
      client_->OnFrameDecoded(decoded_frame_id, *decoded_frame_);
    }
    av_frame_unref(decoded_frame_.get());
  }
}

void Decoder::InitFromFirstBuffer(const Buffer& buffer) {
  const AVCodecID codec_id = Detect(buffer);
  if (codec_id == AV_CODEC_ID_NONE) {
    return;
  }

  codec_ = avcodec_find_decoder(codec_id);
  OSP_CHECK(codec_);
  parser_ = MakeUniqueAVCodecParserContext(codec_id);
  OSP_CHECK(parser_);
  context_ = MakeUniqueAVCodecContext(codec_);
  OSP_CHECK(context_);
  const int open_result = avcodec_open2(context_.get(), codec_, nullptr);
  OSP_CHECK_EQ(open_result, 0);
  packet_ = MakeUniqueAVPacket();
  OSP_CHECK(packet_);
  decoded_frame_ = MakeUniqueAVFrame();
  OSP_CHECK(decoded_frame_);
}

FrameId Decoder::DidReceiveFrameFromDecoder() {
  const auto it = frames_decoding_.begin();
  OSP_DCHECK(it != frames_decoding_.end());
  const auto frame_id = *it;
  frames_decoding_.erase(it);
  return frame_id;
}

void Decoder::OnError(const char* what, int av_errnum, FrameId frame_id) {
  if (!client_) {
    return;
  }

  // Make a human-readable string from the libavcodec error.
  std::ostringstream error;
  if (!frame_id.is_null()) {
    error << "frame: " << frame_id << "; ";
  }
  error << "what: " << what << "; error: " << av_err2str(av_errnum);

  // Dispatch to either the fatal error handler, or the one for decode errors,
  // as appropriate.
  switch (av_errnum) {
    case AVERROR_EOF:
    case AVERROR(EINVAL):
    case AVERROR(ENOMEM):
      client_->OnFatalError(error.str());
      break;
    default:
      client_->OnDecodeError(frame_id, error.str());
      break;
  }
}

// static
AVCodecID Decoder::Detect(const Buffer& buffer) {
  static constexpr AVCodecID kCodecsToTry[] = {
      AV_CODEC_ID_VP8,  AV_CODEC_ID_VP9,  AV_CODEC_ID_H264,
      AV_CODEC_ID_H265, AV_CODEC_ID_OPUS, AV_CODEC_ID_FLAC,
  };

  const absl::Span<const uint8_t> input = buffer.GetSpan();
  for (AVCodecID codec_id : kCodecsToTry) {
    AVCodec* const codec = avcodec_find_decoder(codec_id);
    if (!codec) {
      OSP_LOG_WARN << "Video codec not available to try: "
                   << avcodec_get_name(codec_id);
      continue;
    }
    const auto parser = MakeUniqueAVCodecParserContext(codec_id);
    if (!parser) {
      OSP_LOG_ERROR << "Failed to init parser for codec: "
                    << avcodec_get_name(codec_id);
      continue;
    }
    const auto context = MakeUniqueAVCodecContext(codec);
    if (!context || avcodec_open2(context.get(), codec, nullptr) != 0) {
      OSP_LOG_ERROR << "Failed to open codec: " << avcodec_get_name(codec_id);
      continue;
    }
    const auto packet = MakeUniqueAVPacket();
    OSP_CHECK(packet);
    if (av_parser_parse2(parser.get(), context.get(), &packet->data,
                         &packet->size, input.data(), input.size(),
                         AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0) < 0 ||
        !packet->data || packet->size == 0) {
      OSP_VLOG << "Does not parse as codec: " << avcodec_get_name(codec_id);
      continue;
    }
    if (avcodec_send_packet(context.get(), packet.get()) < 0) {
      OSP_VLOG << "avcodec_send_packet() failed, probably wrong codec version: "
               << avcodec_get_name(codec_id);
      continue;
    }
    OSP_VLOG << "Detected codec: " << avcodec_get_name(codec_id);
    return codec_id;
  }

  return AV_CODEC_ID_NONE;
}

}  // namespace streaming
}  // namespace cast