Imported Upstream version 1.4
[deb_x265.git] / source / encoder / api.cpp
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #include "common.h"
25 #include "bitstream.h"
26 #include "param.h"
27
28 #include "encoder.h"
29 #include "entropy.h"
30 #include "level.h"
31 #include "nal.h"
32 #include "bitcost.h"
33
34 using namespace x265;
35
36 extern "C"
37 x265_encoder *x265_encoder_open(x265_param *p)
38 {
39 if (!p)
40 return NULL;
41
42 x265_param *param = X265_MALLOC(x265_param, 1);
43 if (!param)
44 return NULL;
45
46 memcpy(param, p, sizeof(x265_param));
47 x265_log(param, X265_LOG_INFO, "HEVC encoder version %s\n", x265_version_str);
48 x265_log(param, X265_LOG_INFO, "build info %s\n", x265_build_info_str);
49
50 x265_setup_primitives(param, param->cpuid);
51
52 if (x265_check_params(param))
53 return NULL;
54
55 if (x265_set_globals(param))
56 return NULL;
57
58 Encoder *encoder = new Encoder;
59 if (!param->rc.bEnableSlowFirstPass)
60 x265_param_apply_fastfirstpass(param);
61
62 // may change params for auto-detect, etc
63 encoder->configure(param);
64
65 // may change rate control and CPB params
66 if (!enforceLevel(*param, encoder->m_vps))
67 {
68 delete encoder;
69 return NULL;
70 }
71
72 // will detect and set profile/tier/level in VPS
73 determineLevel(*param, encoder->m_vps);
74
75 encoder->create();
76 encoder->init();
77
78 x265_print_params(param);
79
80 return encoder;
81 }
82
83 extern "C"
84 int x265_encoder_headers(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal)
85 {
86 if (pp_nal && enc)
87 {
88 Encoder *encoder = static_cast<Encoder*>(enc);
89 Entropy sbacCoder;
90 Bitstream bs;
91 encoder->getStreamHeaders(encoder->m_nalList, sbacCoder, bs);
92 *pp_nal = &encoder->m_nalList.m_nal[0];
93 if (pi_nal) *pi_nal = encoder->m_nalList.m_numNal;
94 return encoder->m_nalList.m_occupancy;
95 }
96
97 return -1;
98 }
99
100 extern "C"
101 void x265_encoder_parameters(x265_encoder *enc, x265_param *out)
102 {
103 if (enc && out)
104 {
105 Encoder *encoder = static_cast<Encoder*>(enc);
106 memcpy(out, encoder->m_param, sizeof(x265_param));
107 }
108 }
109
110 extern "C"
111 int x265_encoder_encode(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal, x265_picture *pic_in, x265_picture *pic_out)
112 {
113 if (!enc)
114 return -1;
115
116 Encoder *encoder = static_cast<Encoder*>(enc);
117 int numEncoded;
118
119 // While flushing, we cannot return 0 until the entire stream is flushed
120 do
121 {
122 numEncoded = encoder->encode(pic_in, pic_out);
123 }
124 while (numEncoded == 0 && !pic_in && encoder->m_numDelayedPic);
125
126 // do not allow reuse of these buffers for more than one picture. The
127 // encoder now owns these analysisData buffers.
128 if (pic_in)
129 {
130 pic_in->analysisData.intraData = NULL;
131 pic_in->analysisData.interData = NULL;
132 }
133
134 if (pp_nal && numEncoded > 0)
135 {
136 *pp_nal = &encoder->m_nalList.m_nal[0];
137 if (pi_nal) *pi_nal = encoder->m_nalList.m_numNal;
138 }
139 else if (pi_nal)
140 *pi_nal = 0;
141
142 return numEncoded;
143 }
144
145 extern "C"
146 void x265_encoder_get_stats(x265_encoder *enc, x265_stats *outputStats, uint32_t statsSizeBytes)
147 {
148 if (enc && outputStats)
149 {
150 Encoder *encoder = static_cast<Encoder*>(enc);
151 encoder->fetchStats(outputStats, statsSizeBytes);
152 }
153 }
154
155 extern "C"
156 void x265_encoder_log(x265_encoder* enc, int argc, char **argv)
157 {
158 if (enc)
159 {
160 Encoder *encoder = static_cast<Encoder*>(enc);
161 encoder->writeLog(argc, argv);
162 }
163 }
164
165 extern "C"
166 void x265_encoder_close(x265_encoder *enc)
167 {
168 if (enc)
169 {
170 Encoder *encoder = static_cast<Encoder*>(enc);
171
172 encoder->printSummary();
173 encoder->destroy();
174 delete encoder;
175 }
176 }
177
178 extern "C"
179 void x265_cleanup(void)
180 {
181 destroyROM();
182 BitCost::destroy();
183 }
184
185 extern "C"
186 x265_picture *x265_picture_alloc()
187 {
188 return (x265_picture*)x265_malloc(sizeof(x265_picture));
189 }
190
191 extern "C"
192 void x265_picture_init(x265_param *param, x265_picture *pic)
193 {
194 memset(pic, 0, sizeof(x265_picture));
195
196 pic->bitDepth = param->internalBitDepth;
197 pic->colorSpace = param->internalCsp;
198 pic->forceqp = X265_QP_AUTO;
199 if (param->analysisMode)
200 {
201 uint32_t numPartitions = 1 << (g_maxFullDepth * 2);
202 uint32_t widthInCU = (param->sourceWidth + g_maxCUSize - 1) >> g_maxLog2CUSize;
203 uint32_t heightInCU = (param->sourceHeight + g_maxCUSize - 1) >> g_maxLog2CUSize;
204
205 uint32_t numCUsInFrame = widthInCU * heightInCU;
206 pic->analysisData.numCUsInFrame = numCUsInFrame;
207 pic->analysisData.numPartitions = numPartitions;
208 }
209 }
210
211 extern "C"
212 void x265_picture_free(x265_picture *p)
213 {
214 return x265_free(p);
215 }
216
217 int x265_alloc_analysis_data(x265_picture* pic)
218 {
219 CHECKED_MALLOC(pic->analysisData.interData, x265_inter_data, pic->analysisData.numCUsInFrame * 85);
220 CHECKED_MALLOC(pic->analysisData.intraData, x265_intra_data, 1);
221 pic->analysisData.intraData->cuAddr = NULL;
222 pic->analysisData.intraData->depth = NULL;
223 pic->analysisData.intraData->modes = NULL;
224 pic->analysisData.intraData->partSizes = NULL;
225 pic->analysisData.intraData->poc = NULL;
226 CHECKED_MALLOC(pic->analysisData.intraData->depth, uint8_t, pic->analysisData.numPartitions * pic->analysisData.numCUsInFrame);
227 CHECKED_MALLOC(pic->analysisData.intraData->modes, uint8_t, pic->analysisData.numPartitions * pic->analysisData.numCUsInFrame);
228 CHECKED_MALLOC(pic->analysisData.intraData->partSizes, char, pic->analysisData.numPartitions * pic->analysisData.numCUsInFrame);
229 CHECKED_MALLOC(pic->analysisData.intraData->cuAddr, uint32_t, pic->analysisData.numCUsInFrame);
230 CHECKED_MALLOC(pic->analysisData.intraData->poc, int, pic->analysisData.numCUsInFrame);
231 return 0;
232
233 fail:
234 x265_free_analysis_data(pic);
235 return -1;
236 }
237
238 void x265_free_analysis_data(x265_picture* pic)
239 {
240 X265_FREE(pic->analysisData.interData);
241 pic->analysisData.interData = NULL;
242 X265_FREE(pic->analysisData.intraData->depth);
243 X265_FREE(pic->analysisData.intraData->modes);
244 X265_FREE(pic->analysisData.intraData->partSizes);
245 X265_FREE(pic->analysisData.intraData->cuAddr);
246 X265_FREE(pic->analysisData.intraData->poc);
247 X265_FREE(pic->analysisData.intraData);
248 pic->analysisData.intraData = NULL;
249 }