1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Shin Yee <shinyee@multicorewareinc.com>
5 * Min Chen <chenm003@163.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
21 * This program is also available under a commercial proprietary license.
22 * For more information, contact us at license @ x265.com.
23 *****************************************************************************/
25 #ifndef X265_FRAMEENCODER_H
26 #define X265_FRAMEENCODER_H
29 #include "wavefront.h"
30 #include "bitstream.h"
39 #include "framefilter.h"
40 #include "ratecontrol.h"
41 #include "reference.h"
45 // private x265 namespace
50 #define ANGULAR_MODE_ID 2
59 uint64_t cuInterDistribution
[4][INTER_MODES
];
60 uint64_t cuIntraDistribution
[4][INTRA_MODES
];
62 uint64_t cntSkipCu
[4];
63 uint64_t cntTotalCu
[4];
66 /* These states store the count of inter,intra and skip ctus within quad tree structure of each CU */
67 uint32_t qTreeInterCnt
[4];
68 uint32_t qTreeIntraCnt
[4];
69 uint32_t qTreeSkipCnt
[4];
73 memset(this, 0, sizeof(StatisticLog
));
77 /* manages the state of encoding one row of CTU blocks. When
78 * WPP is active, several rows will be simultaneously encoded. */
81 Entropy bufferedEntropy
; /* store CTU2 context for next row CTU0 */
82 Entropy rowGoOnCoder
; /* store context between CTUs, code bitstream if !SAO */
86 /* Threading variables */
88 /* This lock must be acquired when reading or writing m_active or m_busy */
91 /* row is ready to run, has no neighbor dependencies. The row may have
92 * external dependencies (reference frame pixels) that prevent it from being
93 * processed, so it may stay with m_active=true for some time before it is
94 * encoded by a worker thread. */
97 /* row is being processed by a worker thread. This flag is only true when a
98 * worker thread is within the context of FrameEncoder::processRow(). This
99 * flag is used to detect multiple possible wavefront problems. */
102 /* count of completed CUs in this row */
103 volatile uint32_t completed
;
105 /* called at the start of each frame to initialize state */
106 void init(Entropy
& initContext
)
111 memset(&rowStats
, 0, sizeof(rowStats
));
112 rowGoOnCoder
.load(initContext
);
116 // Manages the wave-front processing of a single encoding frame
117 class FrameEncoder
: public WaveFront
, public Thread
123 virtual ~FrameEncoder() {}
125 bool init(Encoder
*top
, int numRows
, int numCols
, int id
);
129 /* triggers encode of a new frame by the worker thread */
130 bool startCompressFrame(Frame
* curFrame
);
132 /* blocks until worker thread is done, returns access unit */
133 Frame
*getEncodedPicture(NALList
& list
);
143 RateControlEntry m_rce
;
144 SEIDecodedPictureHash m_seiReconPictureDigest
;
151 MD5Context m_state
[3];
153 uint32_t m_checksum
[3];
154 double m_elapsedCompressTime
; // elapsed time spent in worker threads
155 double m_frameTime
; // wall time from frame start to finish
156 StatisticLog m_sliceTypeLog
[3]; // per-slice type CU statistics
157 FrameStats m_frameStats
; // stats of current frame for multi-pass encodes
158 volatile bool m_bAllRowsStop
;
159 volatile int m_vbvResetTriggerRow
;
160 uint64_t m_accessUnitBits
;
165 NoiseReduction
* m_nr
;
166 ThreadLocalData
* m_tld
; /* for --no-wpp */
167 Bitstream
* m_outStreams
;
168 uint32_t* m_substreamSizes
;
171 uint32_t* m_ctuGeomMap
;
174 MotionReference m_mref
[2][MAX_NUM_REF
+ 1];
175 Entropy m_entropyCoder
;
176 Entropy m_initSliceContext
;
177 FrameFilter m_frameFilter
;
180 int m_filterRowDelay
;
181 int m_filterRowDelayCus
;
182 Event m_completionEvent
;
184 int m_frameEncoderID
;
188 bool initializeGeoms();
190 /* analyze / compress frame, can be run in parallel within reference constraints */
191 void compressFrame();
193 /* called by compressFrame to perform wave-front compression analysis */
194 void compressCTURows();
196 /* called by compressFrame to generate final per-row bitstreams */
200 int calcQpForCu(uint32_t cuAddr
, double baseQp
);
201 void collectCTUStatistics(CUData
& ctu
);
202 void noiseReductionUpdate();
204 /* Called by WaveFront::findJob() */
205 void processRow(int row
, int threadId
);
206 void processRowEncoder(int row
, ThreadLocalData
& tld
);
208 void enqueueRowEncoder(int row
) { WaveFront::enqueueRow(row
* 2 + 0); }
209 void enqueueRowFilter(int row
) { WaveFront::enqueueRow(row
* 2 + 1); }
210 void enableRowEncoder(int row
) { WaveFront::enableRow(row
* 2 + 0); }
211 void enableRowFilter(int row
) { WaveFront::enableRow(row
* 2 + 1); }
215 #endif // ifndef X265_FRAMEENCODER_H