1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
25 #include "bitstream.h"
37 x265_encoder
*x265_encoder_open(x265_param
*p
)
42 x265_param
*param
= X265_MALLOC(x265_param
, 1);
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
);
50 x265_setup_primitives(param
, param
->cpuid
);
52 if (x265_check_params(param
))
55 if (x265_set_globals(param
))
58 Encoder
*encoder
= new Encoder
;
59 if (!param
->rc
.bEnableSlowFirstPass
)
60 x265_param_apply_fastfirstpass(param
);
62 // may change params for auto-detect, etc
63 encoder
->configure(param
);
65 // may change rate control and CPB params
66 if (!enforceLevel(*param
, encoder
->m_vps
))
72 // will detect and set profile/tier/level in VPS
73 determineLevel(*param
, encoder
->m_vps
);
76 if (encoder
->m_aborted
)
82 x265_print_params(param
);
88 int x265_encoder_headers(x265_encoder
*enc
, x265_nal
**pp_nal
, uint32_t *pi_nal
)
92 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
95 encoder
->getStreamHeaders(encoder
->m_nalList
, sbacCoder
, bs
);
96 *pp_nal
= &encoder
->m_nalList
.m_nal
[0];
97 if (pi_nal
) *pi_nal
= encoder
->m_nalList
.m_numNal
;
98 return encoder
->m_nalList
.m_occupancy
;
105 void x265_encoder_parameters(x265_encoder
*enc
, x265_param
*out
)
109 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
110 memcpy(out
, encoder
->m_param
, sizeof(x265_param
));
115 int x265_encoder_encode(x265_encoder
*enc
, x265_nal
**pp_nal
, uint32_t *pi_nal
, x265_picture
*pic_in
, x265_picture
*pic_out
)
120 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
123 // While flushing, we cannot return 0 until the entire stream is flushed
126 numEncoded
= encoder
->encode(pic_in
, pic_out
);
128 while (numEncoded
== 0 && !pic_in
&& encoder
->m_numDelayedPic
);
130 // do not allow reuse of these buffers for more than one picture. The
131 // encoder now owns these analysisData buffers.
134 pic_in
->analysisData
.intraData
= NULL
;
135 pic_in
->analysisData
.interData
= NULL
;
138 if (pp_nal
&& numEncoded
> 0)
140 *pp_nal
= &encoder
->m_nalList
.m_nal
[0];
141 if (pi_nal
) *pi_nal
= encoder
->m_nalList
.m_numNal
;
150 void x265_encoder_get_stats(x265_encoder
*enc
, x265_stats
*outputStats
, uint32_t statsSizeBytes
)
152 if (enc
&& outputStats
)
154 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
155 encoder
->fetchStats(outputStats
, statsSizeBytes
);
160 void x265_encoder_log(x265_encoder
* enc
, int argc
, char **argv
)
164 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
165 encoder
->writeLog(argc
, argv
);
170 void x265_encoder_close(x265_encoder
*enc
)
174 Encoder
*encoder
= static_cast<Encoder
*>(enc
);
176 encoder
->printSummary();
183 void x265_cleanup(void)
189 x265_picture
*x265_picture_alloc()
191 return (x265_picture
*)x265_malloc(sizeof(x265_picture
));
195 void x265_picture_init(x265_param
*param
, x265_picture
*pic
)
197 memset(pic
, 0, sizeof(x265_picture
));
199 pic
->bitDepth
= param
->internalBitDepth
;
200 pic
->colorSpace
= param
->internalCsp
;
201 pic
->forceqp
= X265_QP_AUTO
;
202 if (param
->analysisMode
)
204 uint32_t widthInCU
= (param
->sourceWidth
+ g_maxCUSize
- 1) >> g_maxLog2CUSize
;
205 uint32_t heightInCU
= (param
->sourceHeight
+ g_maxCUSize
- 1) >> g_maxLog2CUSize
;
207 uint32_t numCUsInFrame
= widthInCU
* heightInCU
;
208 pic
->analysisData
.numCUsInFrame
= numCUsInFrame
;
209 pic
->analysisData
.numPartitions
= NUM_CU_PARTITIONS
;
214 void x265_picture_free(x265_picture
*p
)