digiKam
encoder-types.h
Go to the documentation of this file.
1 /*
2  * H.265 video codec.
3  * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4  *
5  * Authors: Dirk Farin <farin@struktur.de>
6  *
7  * This file is part of libde265.
8  *
9  * libde265 is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation, either version 3 of
12  * the License, or (at your option) any later version.
13  *
14  * libde265 is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with libde265. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef ENCODE_H
24 #define ENCODE_H
25 
26 #include "libde265/image.h"
27 #include "libde265/decctx.h"
28 #include "libde265/image-io.h"
29 #include "libde265/alloc_pool.h"
30 
31 #include <memory>
32 
33 class encoder_context;
34 class enc_cb;
35 
36 
38 {
39  public:
40  explicit small_image_buffer(int log2Size,int bytes_per_pixel=1);
42 
43  uint8_t* get_buffer_u8() const { return mBuf; }
44  int16_t* get_buffer_s16() const { return (int16_t*)mBuf; }
45  uint16_t* get_buffer_u16() const { return (uint16_t*)mBuf; }
46  template <class pixel_t> pixel_t* get_buffer() const { return (pixel_t*)mBuf; }
47 
48  void copy_to(small_image_buffer& b) const {
49  assert(b.mHeight==mHeight);
50  assert(b.mBytesPerRow==mBytesPerRow);
51  memcpy(b.mBuf, mBuf, mBytesPerRow*mHeight);
52  }
53 
54  int getWidth() const { return mWidth; }
55  int getHeight() const { return mHeight; }
56 
57  int getStride() const { return mStride; } // pixels per row
58 
59  private:
60  uint8_t* mBuf;
61  uint16_t mStride;
62  uint16_t mBytesPerRow;
63 
64  uint8_t mWidth, mHeight;
65 
66  // small_image_buffer cannot be copied
67 
68  small_image_buffer(const small_image_buffer&) { assert(false); } // = delete;
69  small_image_buffer& operator=(const small_image_buffer&) { assert(false); return *this; } // = delete;
70 };
71 
72 
73 class enc_node
74 {
75 public:
76  enc_node() { }
77  enc_node(int _x,int _y, int _log2Size) : x(_x), y(_y), log2Size(_log2Size) { }
78  virtual ~enc_node() { }
79 
80  uint16_t x,y;
81  uint8_t log2Size : 3;
82 
83 
84  static const int DUMPTREE_INTRA_PREDICTION = (1<<0);
85  static const int DUMPTREE_RESIDUAL = (1<<1);
86  static const int DUMPTREE_RECONSTRUCTION = (1<<2);
87  static const int DUMPTREE_ALL = 0xFFFF;
88 
89  virtual void debug_dumpTree(int flags, int indent=0) const = 0;
90 };
91 
92 
94 {
95  public:
96  PixelAccessor(small_image_buffer& buf, int x0,int y0) {
97  mBase = buf.get_buffer_u8();
98  mStride = buf.getStride();
99  mXMin = x0;
100  mYMin = y0;
101  mWidth = buf.getWidth();
102  mHeight= buf.getHeight();
103 
104  mBase -= x0 + y0*mStride;
105  }
106 
107  const uint8_t* operator[](int y) const { return mBase+y*mStride; }
108 
109  int getLeft() const { return mXMin; }
110  int getWidth() const { return mWidth; }
111  int getTop() const { return mYMin; }
112  int getHeight() const { return mHeight; }
113 
114  void copyToImage(de265_image* img, int cIdx) const;
115  void copyFromImage(const de265_image* img, int cIdx);
116 
118  return PixelAccessor();
119  }
120 
121  private:
122  uint8_t* mBase;
123  short mStride;
124  short mXMin, mYMin;
125  uint8_t mWidth, mHeight;
126 
127  PixelAccessor() {
128  mBase = NULL;
129  mStride = mXMin = mYMin = mWidth = mHeight = 0;
130  }
131 };
132 
133 
134 class enc_tb : public enc_node
135 {
136  public:
137  enc_tb(int x,int y,int log2TbSize, enc_cb* _cb);
139 
143 
144  uint8_t split_transform_flag : 1;
145  uint8_t TrafoDepth : 2; // 2 bits enough ? (TODO)
146  uint8_t blkIdx : 2;
147 
149 
150  // Note: in NxN partition mode, the chroma mode is always derived from
151  // the top-left child's intra mode (for chroma 4:2:0).
153 
154  uint8_t cbf[3];
155 
156 
157  /* intra_prediction and residual is filled in tb-split, because this is where we decide
158  on the final block-size the TB is coded with.
159  */
160  //mutable uint8_t debug_intra_border[2*64+1];
161  std::shared_ptr<small_image_buffer> intra_prediction[3];
162  std::shared_ptr<small_image_buffer> residual[3];
163 
164  /* Reconstruction is computed on-demand in writeMetadata().
165  */
166  mutable std::shared_ptr<small_image_buffer> reconstruction[3];
167 
168  union {
169  // split
170  struct {
172  };
173 
174  // leaf node
175  struct {
176  int16_t* coeff[3];
177 
178  bool skip_transform[3][2];
179  uint8_t explicit_rdpcm[3][2];
180  };
181  };
182 
183  float distortion; // total distortion for this level of the TB tree (including all children)
184  float rate; // total rate for coding this TB level and all children
186 
188 
189  void reconstruct(encoder_context* ectx, de265_image* img) const;
191 
192  bool isZeroBlock() const { return cbf[0]==false && cbf[1]==false && cbf[2]==false; }
193 
194  void alloc_coeff_memory(int cIdx, int tbSize);
195 
196  const enc_tb* getTB(int x,int y) const;
197 
198  PixelAccessor getPixels(int x,int y, int cIdx, const seq_parameter_set& sps);
199 
201  const seq_parameter_set* sps) const;
202 
203  /*
204  static void* operator new(const size_t size) { return mMemPool.new_obj(size); }
205  static void operator delete(void* obj) { mMemPool.delete_obj(obj); }
206  */
207 
208 
209  virtual void debug_dumpTree(int flags, int indent=0) const;
210 
211 private:
212  static alloc_pool mMemPool;
213 
214  void reconstruct_tb(encoder_context* ectx,
215  de265_image* img, int x0,int y0, int log2TbSize,
216  int cIdx) const;
217 };
218 
219 
221 {
222  /* absolute motion information (for MV-prediction candidates)
223  */
225 
226  /* specification how to code the motion vector in the bitstream
227  */
229 
230 
231  // NOT TRUE: refIdx in 'spec' is not used. It is taken from 'motion'
232  // Currently, information is duplicated. Same as with inter_pred_idc/predFlag[].
233 
234  /* SPEC:
235  int8_t refIdx[2]; // not used
236  int16_t mvd[2][2];
237 
238  uint8_t inter_pred_idc : 2; // enum InterPredIdc
239  uint8_t mvp_l0_flag : 1;
240  uint8_t mvp_l1_flag : 1;
241  uint8_t merge_flag : 1;
242  uint8_t merge_idx : 3;
243  */
244 };
245 
246 
247 class enc_cb : public enc_node
248 {
249 public:
252 
255 
256  uint8_t split_cu_flag : 1;
257  uint8_t ctDepth : 2;
258 
259 
260  union {
261  // split
262  struct {
263  enc_cb* children[4]; // undefined when split_cu_flag==false
264  };
265 
266  // non-split
267  struct {
268  uint8_t qp : 6;
269  uint8_t cu_transquant_bypass_flag : 1; // currently unused
270  uint8_t pcm_flag : 1;
271 
272  enum PredMode PredMode; // : 6;
273  enum PartMode PartMode; // : 3;
274 
275  union {
276  struct {
277  //enum IntraPredMode pred_mode[4];
278  //enum IntraPredMode chroma_mode;
279  } intra;
280 
281  struct {
283 
284  uint8_t rqt_root_cbf : 1;
285  } inter;
286  };
287 
289  };
290  };
291 
292 
293  float distortion;
294  float rate;
295 
296 
298 
299  /* Save CB reconstruction in the node and restore it again to the image.
300  Pixel data and metadata.
301  */
302  //virtual void save(const de265_image*);
303  //virtual void restore(de265_image*);
304 
305 
306  /* Decode this CB: pixel data and write metadata to image.
307  */
308  void reconstruct(encoder_context* ectx,de265_image* img) const;
309 
310  // can only be called on the lowest-level CB (with TB-tree as its direct child)
311  const enc_tb* getTB(int x,int y) const;
312 
314  const seq_parameter_set* sps) const;
315 
316 
317  virtual void debug_dumpTree(int flags, int indent=0) const;
318 
319 
320  // memory management
321 
322  static void* operator new(const size_t size) {
323  void* p = mMemPool.new_obj(size);
324  //printf("ALLOC %p\n",p);
325  return p;
326  }
327  static void operator delete(void* obj) {
328  //printf("DELETE %p\n",obj);
329  mMemPool.delete_obj(obj);
330  }
331 
332  private:
333  //void write_to_image(de265_image*) const;
334 
335  static alloc_pool mMemPool;
336 };
337 
338 
339 
341 {
342  public:
343  CTBTreeMatrix() : mWidthCtbs(0), mHeightCtbs(0), mLog2CtbSize(0) { }
344  ~CTBTreeMatrix() { free(); }
345 
346  void alloc(int w,int h, int log2CtbSize);
347  void clear() { free(); }
348 
349  void setCTB(int xCTB, int yCTB, enc_cb* ctb) {
350  int idx = xCTB + yCTB*mWidthCtbs;
351  assert(idx < mCTBs.size());
352  if (mCTBs[idx]) { delete mCTBs[idx]; }
353  mCTBs[idx] = ctb;
354  }
355 
356  const enc_cb* getCTB(int xCTB, int yCTB) const {
357  int idx = xCTB + yCTB*mWidthCtbs;
358  assert(idx < mCTBs.size());
359  return mCTBs[idx];
360  }
361 
362  enc_cb** getCTBRootPointer(int x, int y) {
363  x >>= mLog2CtbSize;
364  y >>= mLog2CtbSize;
365 
366  int idx = x + y*mWidthCtbs;
367  assert(idx < mCTBs.size());
368  return &mCTBs[idx];
369  }
370 
371  const enc_cb* getCB(int x,int y) const;
372  const enc_tb* getTB(int x,int y) const;
373  const enc_pb_inter* getPB(int x,int y) const;
374 
376  const seq_parameter_set*) const;
377 
378  private:
379  std::vector<enc_cb*> mCTBs;
380  int mWidthCtbs;
381  int mHeightCtbs;
382  int mLog2CtbSize;
383 
384  void free() {
385  for (int i=0 ; i<mWidthCtbs*mHeightCtbs ; i++) {
386  if (mCTBs[i]) {
387  delete mCTBs[i];
388  mCTBs[i]=NULL;
389  }
390  }
391  }
392 };
393 
394 
395 
396 
397 inline int childX(int x0, int idx, int log2CbSize)
398 {
399  return x0 + ((idx&1) << (log2CbSize-1));
400 }
401 
402 inline int childY(int y0, int idx, int log2CbSize)
403 {
404  return y0 + ((idx>>1) << (log2CbSize-1));
405 }
406 
407 
408 
409 #endif
Definition: encoder-types.h:341
void alloc(int w, int h, int log2CtbSize)
const enc_cb * getCB(int x, int y) const
void writeReconstructionToImage(de265_image *img, const seq_parameter_set *) const
const enc_tb * getTB(int x, int y) const
CTBTreeMatrix()
Definition: encoder-types.h:343
const enc_pb_inter * getPB(int x, int y) const
~CTBTreeMatrix()
Definition: encoder-types.h:344
void clear()
Definition: encoder-types.h:347
const enc_cb * getCTB(int xCTB, int yCTB) const
Definition: encoder-types.h:356
void setCTB(int xCTB, int yCTB, enc_cb *ctb)
Definition: encoder-types.h:349
enc_cb ** getCTBRootPointer(int x, int y)
Definition: encoder-types.h:362
Definition: motion.h:49
Definition: motion.h:38
Definition: encoder-types.h:94
PixelAccessor(small_image_buffer &buf, int x0, int y0)
Definition: encoder-types.h:96
void copyToImage(de265_image *img, int cIdx) const
int getWidth() const
Definition: encoder-types.h:110
int getHeight() const
Definition: encoder-types.h:112
void copyFromImage(const de265_image *img, int cIdx)
static PixelAccessor invalid()
Definition: encoder-types.h:117
int getTop() const
Definition: encoder-types.h:111
const uint8_t * operator[](int y) const
Definition: encoder-types.h:107
int getLeft() const
Definition: encoder-types.h:109
Definition: alloc_pool.h:41
void delete_obj(void *)
void * new_obj(const size_t size)
Definition: encoder-types.h:248
float distortion
Definition: encoder-types.h:293
enum PartMode PartMode
Definition: encoder-types.h:273
uint8_t rqt_root_cbf
Definition: encoder-types.h:284
uint8_t ctDepth
Definition: encoder-types.h:257
enc_pb_inter pb[4]
Definition: encoder-types.h:282
enc_cb * children[4]
Definition: encoder-types.h:263
enum PredMode PredMode
Definition: encoder-types.h:272
enc_cb * parent
Definition: encoder-types.h:253
float rate
Definition: encoder-types.h:294
void writeReconstructionToImage(de265_image *img, const seq_parameter_set *sps) const
uint8_t qp
Definition: encoder-types.h:268
virtual void debug_dumpTree(int flags, int indent=0) const
void set_rqt_root_bf_from_children_cbf()
enc_cb ** downPtr
Definition: encoder-types.h:254
enc_tb * transform_tree
Definition: encoder-types.h:288
uint8_t pcm_flag
Definition: encoder-types.h:270
const enc_tb * getTB(int x, int y) const
uint8_t split_cu_flag
Definition: encoder-types.h:256
uint8_t cu_transquant_bypass_flag
Definition: encoder-types.h:269
void reconstruct(encoder_context *ectx, de265_image *img) const
struct enc_cb::@34::@38::@40::@43 inter
struct enc_cb::@34::@38::@40::@42 intra
Definition: encoder-types.h:74
static const int DUMPTREE_RECONSTRUCTION
Definition: encoder-types.h:86
virtual ~enc_node()
Definition: encoder-types.h:78
static const int DUMPTREE_RESIDUAL
Definition: encoder-types.h:85
enc_node()
Definition: encoder-types.h:76
uint16_t x
Definition: encoder-types.h:80
uint16_t y
Definition: encoder-types.h:80
virtual void debug_dumpTree(int flags, int indent=0) const =0
static const int DUMPTREE_ALL
Definition: encoder-types.h:87
uint8_t log2Size
Definition: encoder-types.h:81
enc_node(int _x, int _y, int _log2Size)
Definition: encoder-types.h:77
static const int DUMPTREE_INTRA_PREDICTION
Definition: encoder-types.h:84
Definition: encoder-types.h:135
uint8_t TrafoDepth
Definition: encoder-types.h:145
PixelAccessor getPixels(int x, int y, int cIdx, const seq_parameter_set &sps)
std::shared_ptr< small_image_buffer > intra_prediction[3]
Definition: encoder-types.h:161
enc_tb(int x, int y, int log2TbSize, enc_cb *_cb)
void reconstruct(encoder_context *ectx, de265_image *img) const
void debug_writeBlack(encoder_context *ectx, de265_image *img) const
void writeReconstructionToImage(de265_image *img, const seq_parameter_set *sps) const
uint8_t blkIdx
Definition: encoder-types.h:146
uint8_t split_transform_flag
Definition: encoder-types.h:144
std::shared_ptr< small_image_buffer > reconstruction[3]
Definition: encoder-types.h:166
enum IntraPredMode intra_mode
Definition: encoder-types.h:148
const enc_tb * getTB(int x, int y) const
std::shared_ptr< small_image_buffer > residual[3]
Definition: encoder-types.h:162
enc_tb ** downPtr
Definition: encoder-types.h:142
bool skip_transform[3][2]
Definition: encoder-types.h:178
float rate
Definition: encoder-types.h:184
enum IntraPredMode intra_mode_chroma
Definition: encoder-types.h:152
enc_cb * cb
Definition: encoder-types.h:141
enc_tb * parent
Definition: encoder-types.h:140
uint8_t explicit_rdpcm[3][2]
Definition: encoder-types.h:179
float rate_withoutCbfChroma
Definition: encoder-types.h:185
int16_t * coeff[3]
Definition: encoder-types.h:176
void set_cbf_flags_from_children()
void alloc_coeff_memory(int cIdx, int tbSize)
enc_tb * children[4]
Definition: encoder-types.h:171
float distortion
Definition: encoder-types.h:183
virtual void debug_dumpTree(int flags, int indent=0) const
uint8_t cbf[3]
Definition: encoder-types.h:154
bool isZeroBlock() const
Definition: encoder-types.h:192
Definition: encoder-context.h:39
Definition: sps.h:86
Definition: encoder-types.h:38
void copy_to(small_image_buffer &b) const
Definition: encoder-types.h:48
pixel_t * get_buffer() const
Definition: encoder-types.h:46
uint8_t * get_buffer_u8() const
Definition: encoder-types.h:43
int getHeight() const
Definition: encoder-types.h:55
int16_t * get_buffer_s16() const
Definition: encoder-types.h:44
int getStride() const
Definition: encoder-types.h:57
small_image_buffer(int log2Size, int bytes_per_pixel=1)
int getWidth() const
Definition: encoder-types.h:54
uint16_t * get_buffer_u16() const
Definition: encoder-types.h:45
int childY(int y0, int idx, int log2CbSize)
Definition: encoder-types.h:402
int childX(int x0, int idx, int log2CbSize)
Definition: encoder-types.h:397
PredMode
Definition: slice.h:90
PartMode
Definition: slice.h:75
IntraPredMode
Definition: slice.h:95
Definition: image.h:222
Definition: encoder-types.h:221
PBMotion motion
Definition: encoder-types.h:224
PBMotionCoding spec
Definition: encoder-types.h:228