x264Encoder.hpp 3.82 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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "videoEncoder.hpp"
#include "amfIncludes.hpp"

extern "C" {
#if defined(_WIN32)
#include "x264/x264.h"
#else
#include <inttypes.h>
#include <x264.h>
#endif
}

//#include "sps_decode.h"

/*
The different types of Network Abstraction Layer Units for reference:

0      Unspecified                                                    non - VCL
1      Coded slice of a non - IDR picture                             VCL
2      Coded slice data partition A                                   VCL
3      Coded slice data partition B                                   VCL
4      Coded slice data partition C                                   VCL
5      Coded slice of an IDR picture                                  VCL
6      Supplemental enhancement information(SEI)                      non - VCL
7      Sequence parameter set                                         non - VCL
8      Picture parameter set                                          non - VCL
9      Access unit delimiter                                          non - VCL
10     End of sequence                                                non - VCL
11     End of stream                                                  non - VCL
12     Filler data                                                    non - VCL
13     Sequence parameter set extension                               non - VCL
14     Prefix NAL unit                                                non - VCL
15     Subset sequence parameter set                                  non - VCL
16     Depth parameter set                                            non - VCL
17..18 Reserved                                                       non - VCL
19     Coded slice of an auxiliary coded picture without partitioning non - VCL
20     Coded slice extension                                          non - VCL
21     Coded slice extension for depth view components                non - VCL
22..23 Reserved                                                       non - VCL
24..31 Unspecified                                                    non - VCL
*/

namespace VideoStitch {
namespace Output {

class X264Encoder : public VideoEncoder {
 public:
  X264Encoder(FrameRate fps, int width, int height, const std::string& preset, const std::string& tune,
              const std::string& profile, const std::string& level, const std::string& bitrate_mode,
              int quality_balance, const VideoStitch::IO::ColorDescription& colorDesc, int maxBitrate, int bufferSize,
              bool cbr_padding, int gop, int b_frames);

  ~X264Encoder();

  static void x264_log(void*, int i_level, const char* psz, va_list args);
  static int getLevelFromString(const std::string& levelStr);

  static Potential<VideoEncoder> createX264Encoder(const Ptv::Value& config, int width, int height,
                                                   FrameRate framerate);

  bool encode(const Frame& videoFrame, std::vector<VideoStitch::IO::DataPacket>& packets);

  char* metadata(char* enc, char* pend);

  /**
   * Supplemental Enhancement Information unit
   */
  void getSEI(VideoStitch::IO::DataPacket& packet) { packet = sei; }

  int getBitRate() const;

  bool dynamicBitrateSupported() const { return (paramData.i_nal_hrd != X264_NAL_HRD_CBR); }

  bool setBitRate(uint32_t maxBitrate, uint32_t bufferSize);

 private:
  static const AVal av_videocodecid;
  static const AVal av_videodatarate;
  static const AVal av_framerate;

  void setBitRateParams(uint32_t maxBitrate, uint32_t bufferSize);

  x264_param_t paramData;
  x264_t* x264;

  x264_picture_t picOut;

  bool useCBR;
  bool doRequestKeyframe = true;

  unsigned int width, height;
  FrameRate framerate;

  std::string curPreset, curTune, curProfile;

  x264_picture_t pic;
};

}  // namespace Output
}  // namespace VideoStitch