kodi
EncoderFFmpeg.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "Encoder.h"
12 
13 extern "C"
14 {
15 #include <libavcodec/avcodec.h>
16 #include <libavformat/avformat.h>
17 #include <libswresample/swresample.h>
18 }
19 
20 namespace KODI
21 {
22 namespace CDRIP
23 {
24 
25 class CEncoderFFmpeg : public CEncoder
26 {
27 public:
28  CEncoderFFmpeg() = default;
29  ~CEncoderFFmpeg() override = default;
30 
31  bool Init() override;
32  ssize_t Encode(uint8_t* pbtStream, size_t nNumBytesRead) override;
33  bool Close() override;
34 
35 private:
36  static int avio_write_callback(void* opaque, uint8_t* buf, int buf_size);
37  static int64_t avio_seek_callback(void* opaque, int64_t offset, int whence);
38 
39  void SetTag(const std::string& tag, const std::string& value);
40  bool WriteFrame();
41  AVSampleFormat GetInputFormat(int inBitsPerSample);
42 
43  AVFormatContext* m_formatCtx{nullptr};
44  AVCodecContext* m_codecCtx{nullptr};
45  SwrContext* m_swrCtx{nullptr};
46  AVStream* m_stream{nullptr};
47  AVSampleFormat m_inFormat;
48  AVSampleFormat m_outFormat;
49 
50  /* From libavformat/avio.h:
51  * The buffer size is very important for performance.
52  * For protocols with fixed blocksize it should be set to this
53  * blocksize.
54  * For others a typical size is a cache page, e.g. 4kb.
55  */
56  static constexpr size_t BUFFER_SIZE = 4096;
57 
58  unsigned int m_neededFrames{0};
59  size_t m_neededBytes{0};
60  uint8_t* m_buffer{nullptr};
61  size_t m_bufferSize{0};
62  AVFrame* m_bufferFrame{nullptr};
63  uint8_t* m_resampledBuffer{nullptr};
64  size_t m_resampledBufferSize{0};
65  AVFrame* m_resampledFrame{nullptr};
66  bool m_needConversion{false};
67  int64_t m_samplesCount{0};
68  int64_t m_samplesCountMultiply{1000};
69 };
70 
71 } /* namespace CDRIP */
72 } /* namespace KODI */
Definition: EncoderFFmpeg.h:25
Definition: Encoder.h:30
Definition: AudioDecoder.h:18