1 // Copyright (c) 2017 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include "umc_defs.h"
22 
23 #ifndef _UMC_VIDEO_BRC_H_
24 #define _UMC_VIDEO_BRC_H_
25 
26 #include "umc_video_encoder.h"
27 
28 namespace UMC
29 {
30 
31 enum eBrcPictureFlags
32 {
33   BRC_TOP_FIELD = 1,
34   BRC_BOTTOM_FIELD = 2,
35   BRC_FRAME = BRC_TOP_FIELD | BRC_BOTTOM_FIELD,
36   BRC_REPEAT_FIRST_FIELD = 4,
37   BRC_TOP_FIELD_FIRST = 8,
38   BRC_SECOND_FIELD = 12
39 };
40 
41 typedef int32_t BrcPictureFlags;
42 
43 #define BRC_BYTES_IN_KBYTE 1000
44 #define BRC_BITS_IN_KBIT 1000
45 
46 enum BRCMethod
47 {
48   BRC_CBR = 0,
49   BRC_VBR,
50   BRC_AVBR,
51 };
52 
53 enum BRCAlgorithm
54 {
55   BRC_COMMON = -1,
56   BRC_H264,
57   BRC_MPEG2,
58   BRC_SVC
59 };
60 
61 enum eBRCStatus
62 {
63   BRC_ERROR                   = -1,
64   BRC_OK                      = 0x0,
65   BRC_ERR_BIG_FRAME           = 0x1,
66   BRC_BIG_FRAME               = 0x2,
67   BRC_ERR_SMALL_FRAME         = 0x4,
68   BRC_SMALL_FRAME             = 0x8,
69   BRC_NOT_ENOUGH_BUFFER       = 0x10
70 };
71 
72 enum eBRCRecode
73 {
74   BRC_RECODE_NONE           = 0,
75   BRC_RECODE_QP             = 1,
76   BRC_RECODE_PANIC          = 2,
77   BRC_RECODE_EXT_QP         = 3,
78   BRC_RECODE_EXT_PANIC      = 4,
79   BRC_EXT_FRAMESKIP         = 16
80 };
81 
82 typedef int32_t BRCStatus;
83 
84 class VideoBrcParams : public VideoEncoderParams {
85   DYNAMIC_CAST_DECL(VideoBrcParams, VideoEncoderParams)
86 public:
87   VideoBrcParams();
88 
89   int32_t  HRDInitialDelayBytes;
90   int32_t  HRDBufferSizeBytes;
91 
92   int32_t  targetBitrate;
93   int32_t  maxBitrate;
94   int32_t  BRCMode;
95 
96   int32_t GOPPicSize;
97   int32_t GOPRefDist;
98   uint32_t frameRateExtD;
99   uint32_t frameRateExtN;
100   uint32_t frameRateExtN_1;
101 
102   uint16_t accuracy;
103   uint16_t convergence;
104 };
105 
106 class VideoBrc {
107 
108 public:
109 
110   VideoBrc();
111   virtual ~VideoBrc();
112 
113   // Initialize with specified parameter(s)
114   virtual Status Init(BaseCodecParams *init, int32_t numTempLayers = 1) = 0;
115 
116   // Close all resources
117   virtual Status Close() = 0;
118 
119   virtual Status Reset(BaseCodecParams *init, int32_t numTempLayers = 1) = 0;
120 
121   virtual Status SetParams(BaseCodecParams* params, int32_t tid = 0) = 0;
122   virtual Status GetParams(BaseCodecParams* params, int32_t tid = 0) = 0;
123   virtual Status GetHRDBufferFullness(double *hrdBufFullness, int32_t recode = 0, int32_t tid = 0) = 0;
124   virtual Status PreEncFrame(FrameType frameType, int32_t recode = 0, int32_t tid = 0) = 0;
125   virtual BRCStatus PostPackFrame(FrameType picType, int32_t bitsEncodedFrame, int32_t payloadBits = 0, int32_t recode = 0, int32_t poc = 0) = 0;
126 
127   virtual int32_t GetQP(FrameType frameType, int32_t tid = -1) = 0;
128   virtual Status SetQP(int32_t qp, FrameType frameType, int32_t tid = 0) = 0;
129 
130 //  virtual Status ScaleRemovalDelay(double removalDelayScale) = 0;
131   virtual Status SetPictureFlags(FrameType frameType, int32_t picture_structure, int32_t repeat_first_field = 0, int32_t top_field_first = 0, int32_t second_field = 0);
132 
133   virtual Status GetMinMaxFrameSize(int32_t *minFrameSizeInBits, int32_t *maxFrameSizeInBits) = 0;
134 
135   static Status CheckCorrectParams_MPEG2(VideoBrcParams *inBrcParams, VideoBrcParams *outBrcParams = NULL);
136 
137   virtual Status GetInitialCPBRemovalDelay(uint32_t *initial_cpb_removal_delay, int32_t recode = 0);
138 
139 protected:
140   //VideoBrc *brc;
141   VideoBrcParams mParams;
142 };
143 
144 } // namespace UMC
145 #endif
146 
147