"docs/vscode:/vscode.git/clone" did not exist on "72a98a86c6bc17573cbe61a26061689b3830e5af"
stream.h 1.95 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
// Copyright 2004-present Facebook. All Rights Reserved.

#pragma once

#include <atomic>
#include "defs.h"

extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/imgutils.h>
}

namespace ffmpeg {

/**
 * Class uses FFMPEG library to decode one media stream (audio or video).
 */

class Stream {
 public:
  Stream(
      AVFormatContext* inputCtx,
      MediaFormat format,
      bool convertPtsToWallTime);
  virtual ~Stream();

  // returns 0 - on success or negative error
  int openCodec();
  // returns number processed bytes from packet, or negative error
  int decodeFrame(const AVPacket* packet, int* gotFramePtr);
  // returns stream index
  int getIndex() const {
    return format_.stream;
  }
  // returns number decoded/sampled bytes
  int getFrameBytes(DecoderOutputMessage* out, bool headerOnly);
  // returns number decoded/sampled bytes
  int flush(DecoderOutputMessage* out, bool headerOnly);
  // rescale package
  void rescalePackage(AVPacket* packet);
  // return media format
  MediaFormat getMediaFormat() const {
    return format_;
  }

 protected:
  virtual int initFormat() = 0;
  // returns number processed bytes from packet, or negative error
  virtual int analyzePacket(const AVPacket* packet, int* gotFramePtr);
  // returns number decoded/sampled bytes, or negative error
  virtual int copyFrameBytes(ByteStorage* out, bool flush) = 0;
  // initialize codec, returns output buffer size, or negative error
  virtual int estimateBytes(bool flush) = 0;
  // sets output format
  virtual void setHeader(DecoderHeader* header) = 0;
  // finds codec
  virtual AVCodec* findCodec(AVCodecContext* ctx);

 private:
  int fillBuffer(DecoderOutputMessage* out, bool flush, bool headerOnly);

 protected:
  AVFormatContext* const inputCtx_;
  MediaFormat format_;
  const bool convertPtsToWallTime_;

  AVCodecContext* codecCtx_{nullptr};
  AVFrame* frame_{nullptr};

  std::atomic<size_t> numGenerator_{0};
};

} // namespace ffmpeg