FfmpegDecoder.cpp 10.3 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "FfmpegDecoder.h"
#include "FfmpegAudioStream.h"
#include "FfmpegUtil.h"
#include "FfmpegVideoStream.h"

using namespace std;

static AVPacket avPkt;

namespace {

unique_ptr<FfmpegStream> createFfmpegStream(
    MediaType type,
    AVFormatContext* ctx,
    int idx,
    MediaFormat& mediaFormat,
    double seekFrameMargin) {
  enum AVMediaType avType;
  CHECK(ffmpeg_util::mapMediaType(type, &avType));
  switch (type) {
    case MediaType::TYPE_VIDEO:
      return make_unique<FfmpegVideoStream>(
          ctx, idx, avType, mediaFormat, seekFrameMargin);
    case MediaType::TYPE_AUDIO:
      return make_unique<FfmpegAudioStream>(
          ctx, idx, avType, mediaFormat, seekFrameMargin);
    default:
      return nullptr;
  }
}

} // namespace

FfmpegAvioContext::FfmpegAvioContext()
    : workBuffersize_(VIO_BUFFER_SZ),
      workBuffer_((uint8_t*)av_malloc(workBuffersize_)),
      inputFile_(nullptr),
      inputBuffer_(nullptr),
      inputBufferSize_(0) {}

int FfmpegAvioContext::initAVIOContext(const uint8_t* buffer, int64_t size) {
  inputBuffer_ = buffer;
  inputBufferSize_ = size;
  avioCtx_ = avio_alloc_context(
      workBuffer_,
      workBuffersize_,
      0,
      reinterpret_cast<void*>(this),
      &FfmpegAvioContext::readMemory,
      nullptr, // no write function
      &FfmpegAvioContext::seekMemory);
  return 0;
}

FfmpegAvioContext::~FfmpegAvioContext() {
  /* note: the internal buffer could have changed, and be != workBuffer_ */
  if (avioCtx_) {
    av_freep(&avioCtx_->buffer);
    av_freep(&avioCtx_);
  } else {
    av_freep(&workBuffer_);
  }
  if (inputFile_) {
    fclose(inputFile_);
  }
}

int FfmpegAvioContext::read(uint8_t* buf, int buf_size) {
  if (inputBuffer_) {
    return readMemory(this, buf, buf_size);
  } else {
    return -1;
  }
}

int FfmpegAvioContext::readMemory(void* opaque, uint8_t* buf, int buf_size) {
  FfmpegAvioContext* h = static_cast<FfmpegAvioContext*>(opaque);
  if (buf_size < 0) {
    return -1;
  }

  int reminder = h->inputBufferSize_ - h->offset_;
  int r = buf_size < reminder ? buf_size : reminder;
  if (r < 0) {
    return AVERROR_EOF;
  }

  memcpy(buf, h->inputBuffer_ + h->offset_, r);
  h->offset_ += r;
  return r;
}

int64_t FfmpegAvioContext::seek(int64_t offset, int whence) {
  if (inputBuffer_) {
    return seekMemory(this, offset, whence);
  } else {
    return -1;
  }
}

int64_t FfmpegAvioContext::seekMemory(
    void* opaque,
    int64_t offset,
    int whence) {
  FfmpegAvioContext* h = static_cast<FfmpegAvioContext*>(opaque);
  switch (whence) {
    case SEEK_CUR: // from current position
      h->offset_ += offset;
      break;
    case SEEK_END: // from eof
      h->offset_ = h->inputBufferSize_ + offset;
      break;
    case SEEK_SET: // from beginning of file
      h->offset_ = offset;
      break;
    case AVSEEK_SIZE:
      return h->inputBufferSize_;
  }
  return h->offset_;
}

int FfmpegDecoder::init(
    const std::string& filename,
    bool isDecodeFile,
    FfmpegAvioContext& ioctx,
    DecoderOutput& decoderOutput) {
  cleanUp();

  int ret = 0;
  if (!isDecodeFile) {
    formatCtx_ = avformat_alloc_context();
    if (!formatCtx_) {
      LOG(ERROR) << "avformat_alloc_context failed";
      return -1;
    }
    formatCtx_->pb = ioctx.get_avio();
    formatCtx_->flags |= AVFMT_FLAG_CUSTOM_IO;

    // Determining the input format:
    int probeSz = AVPROBE_SIZE + AVPROBE_PADDING_SIZE;
    uint8_t* probe((uint8_t*)av_malloc(probeSz));
    memset(probe, 0, probeSz);
    int len = ioctx.read(probe, probeSz - AVPROBE_PADDING_SIZE);
    if (len < probeSz - AVPROBE_PADDING_SIZE) {
      LOG(ERROR) << "Insufficient data to determine video format";
      av_freep(&probe);
      return -1;
    }
    // seek back to start of stream
    ioctx.seek(0, SEEK_SET);

    unique_ptr<AVProbeData> probeData(new AVProbeData());
    probeData->buf = probe;
    probeData->buf_size = len;
    probeData->filename = "";
    // Determine the input-format:
    formatCtx_->iformat = av_probe_input_format(probeData.get(), 1);
    // this is to avoid the double-free error
    if (formatCtx_->iformat == nullptr) {
      LOG(ERROR) << "av_probe_input_format fails";
      return -1;
    }
    VLOG(1) << "av_probe_input_format succeeds";
    av_freep(&probe);

    ret = avformat_open_input(&formatCtx_, "", nullptr, nullptr);
  } else {
    ret = avformat_open_input(&formatCtx_, filename.c_str(), nullptr, nullptr);
  }

  if (ret < 0) {
    LOG(ERROR) << "avformat_open_input failed, error: "
               << ffmpeg_util::getErrorDesc(ret);
    cleanUp();
    return ret;
  }
  ret = avformat_find_stream_info(formatCtx_, nullptr);
  if (ret < 0) {
    LOG(ERROR) << "avformat_find_stream_info failed, error: "
               << ffmpeg_util::getErrorDesc(ret);
    cleanUp();
    return ret;
  }
  if (!initStreams()) {
    LOG(ERROR) << "Cannot activate streams";
    cleanUp();
    return -1;
  }

  for (auto& stream : streams_) {
    MediaType mediaType = stream.second->getMediaType();
    decoderOutput.initMediaType(mediaType, stream.second->getMediaFormat());
  }
  VLOG(1) << "FfmpegDecoder initialized";
  return 0;
}

int FfmpegDecoder::decodeFile(
    unique_ptr<DecoderParameters> params,
    const string& fileName,
    DecoderOutput& decoderOutput) {
  VLOG(1) << "decode file: " << fileName;
  FfmpegAvioContext ioctx;
  int ret = decodeLoop(std::move(params), fileName, true, ioctx, decoderOutput);
  return ret;
}

int FfmpegDecoder::decodeMemory(
    unique_ptr<DecoderParameters> params,
    const uint8_t* buffer,
    int64_t size,
    DecoderOutput& decoderOutput) {
  VLOG(1) << "decode video data in memory";
  FfmpegAvioContext ioctx;
  int ret = ioctx.initAVIOContext(buffer, size);
  if (ret == 0) {
    ret =
        decodeLoop(std::move(params), string(""), false, ioctx, decoderOutput);
  }
  return ret;
}

void FfmpegDecoder::cleanUp() {
  if (formatCtx_) {
    for (auto& stream : streams_) {
      // Drain stream buffers.
      DecoderOutput decoderOutput;
      stream.second->flush(1, decoderOutput);
      stream.second.reset();
    }
    streams_.clear();
    avformat_close_input(&formatCtx_);
  }
}

FfmpegStream* FfmpegDecoder::findStreamByIndex(int streamIndex) const {
  auto it = streams_.find(streamIndex);
  return it != streams_.end() ? it->second.get() : nullptr;
}

/*
Reference implementation:
https://ffmpeg.org/doxygen/3.4/demuxing_decoding_8c-example.html
*/
int FfmpegDecoder::decodeLoop(
    unique_ptr<DecoderParameters> params,
    const std::string& filename,
    bool isDecodeFile,
    FfmpegAvioContext& ioctx,
    DecoderOutput& decoderOutput) {
  params_ = std::move(params);

  int ret = init(filename, isDecodeFile, ioctx, decoderOutput);
  if (ret < 0) {
    return ret;
  }
  // init package
  av_init_packet(&avPkt);
  avPkt.data = nullptr;
  avPkt.size = 0;

  int result = 0;
  bool ptsInRange = true;
  while (ptsInRange) {
    result = av_read_frame(formatCtx_, &avPkt);
    if (result == AVERROR(EAGAIN)) {
      VLOG(1) << "Decoder is busy";
      ret = 0;
      break;
    } else if (result == AVERROR_EOF) {
      VLOG(1) << "Stream decoding is completed";
      ret = 0;
      break;
    } else if (result < 0) {
      VLOG(1) << "av_read_frame fails. Break decoder loop. Error: "
              << ffmpeg_util::getErrorDesc(result);
      ret = result;
      break;
    }

    ret = 0;
    auto stream = findStreamByIndex(avPkt.stream_index);
    if (stream == nullptr) {
      // the packet is from a stream the caller is not interested. Ignore it
      VLOG(2) << "avPkt ignored. stream index: " << avPkt.stream_index;
      // Need to free the memory of AVPacket. Otherwise, memory leak happens
      av_packet_unref(&avPkt);
      continue;
    }

    do {
      result = stream->sendPacket(&avPkt);
      if (result == AVERROR(EAGAIN)) {
        VLOG(2) << "avcodec_send_packet returns AVERROR(EAGAIN)";
        // start to recevie available frames from internal buffer
        stream->receiveAvailFrames(params_->getPtsOnly, decoderOutput);
        if (isPtsExceedRange()) {
          // exit the most-outer while loop
          VLOG(1) << "In all streams, exceed the end pts. Exit decoding loop";
          ret = 0;
          ptsInRange = false;
          break;
        }
      } else if (result < 0) {
        LOG(WARNING) << "avcodec_send_packet failed. Error: "
                     << ffmpeg_util::getErrorDesc(result);
        ret = result;
        break;
      } else {
        VLOG(2) << "avcodec_send_packet succeeds";
        // succeed. Read the next AVPacket and send out it
        break;
      }
    } while (ptsInRange);
    // Need to free the memory of AVPacket. Otherwise, memory leak happens
    av_packet_unref(&avPkt);
  }
  /* flush cached frames */
  flushStreams(decoderOutput);
  return ret;
}

bool FfmpegDecoder::initStreams() {
  for (auto it = params_->formats.begin(); it != params_->formats.end(); ++it) {
    AVMediaType mediaType;
    if (!ffmpeg_util::mapMediaType(it->first, &mediaType)) {
      LOG(ERROR) << "Unknown media type: " << it->first;
      return false;
    }
    int streamIdx =
        av_find_best_stream(formatCtx_, mediaType, -1, -1, nullptr, 0);

    if (streamIdx >= 0) {
      VLOG(2) << "find stream index: " << streamIdx;
      auto stream = createFfmpegStream(
          it->first,
          formatCtx_,
          streamIdx,
          it->second,
          params_->seekFrameMargin);

      CHECK(stream);
      if (stream->openCodecContext() < 0) {
        LOG(ERROR) << "Cannot open codec. Stream index: " << streamIdx;
        return false;
      }
      streams_.emplace(streamIdx, move(stream));
    } else {
      VLOG(1) << "Cannot open find stream of type " << it->first;
    }
  }
  // Seek frames in each stream
  int ret = 0;
  for (auto& stream : streams_) {
    auto startPts = stream.second->getStartPts();
    VLOG(1) << "stream: " << stream.first << " startPts: " << startPts;
    if (startPts > 0 && (ret = stream.second->seekFrame(startPts)) < 0) {
      LOG(WARNING) << "seekFrame in stream fails";
      return false;
    }
  }
  VLOG(1) << "initStreams succeeds";
  return true;
}

bool FfmpegDecoder::isPtsExceedRange() {
  bool exceed = true;
  for (auto& stream : streams_) {
    exceed = exceed && stream.second->isFramePtsExceedRange();
  }
  return exceed;
}

void FfmpegDecoder::flushStreams(DecoderOutput& decoderOutput) {
  for (auto& stream : streams_) {
    stream.second->flush(params_->getPtsOnly, decoderOutput);
  }
}