| 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 | if (encoder->m_aborted) |
| 77 | { |
| 78 | delete encoder; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | x265_print_params(param); |
| 83 | |
| 84 | return encoder; |
| 85 | } |
| 86 | |
| 87 | extern "C" |
| 88 | int x265_encoder_headers(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal) |
| 89 | { |
| 90 | if (pp_nal && enc) |
| 91 | { |
| 92 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 93 | Entropy sbacCoder; |
| 94 | Bitstream bs; |
| 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; |
| 99 | } |
| 100 | |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | extern "C" |
| 105 | void x265_encoder_parameters(x265_encoder *enc, x265_param *out) |
| 106 | { |
| 107 | if (enc && out) |
| 108 | { |
| 109 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 110 | memcpy(out, encoder->m_param, sizeof(x265_param)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | extern "C" |
| 115 | int x265_encoder_encode(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal, x265_picture *pic_in, x265_picture *pic_out) |
| 116 | { |
| 117 | if (!enc) |
| 118 | return -1; |
| 119 | |
| 120 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 121 | int numEncoded; |
| 122 | |
| 123 | // While flushing, we cannot return 0 until the entire stream is flushed |
| 124 | do |
| 125 | { |
| 126 | numEncoded = encoder->encode(pic_in, pic_out); |
| 127 | } |
| 128 | while (numEncoded == 0 && !pic_in && encoder->m_numDelayedPic); |
| 129 | |
| 130 | // do not allow reuse of these buffers for more than one picture. The |
| 131 | // encoder now owns these analysisData buffers. |
| 132 | if (pic_in) |
| 133 | { |
| 134 | pic_in->analysisData.intraData = NULL; |
| 135 | pic_in->analysisData.interData = NULL; |
| 136 | } |
| 137 | |
| 138 | if (pp_nal && numEncoded > 0) |
| 139 | { |
| 140 | *pp_nal = &encoder->m_nalList.m_nal[0]; |
| 141 | if (pi_nal) *pi_nal = encoder->m_nalList.m_numNal; |
| 142 | } |
| 143 | else if (pi_nal) |
| 144 | *pi_nal = 0; |
| 145 | |
| 146 | return numEncoded; |
| 147 | } |
| 148 | |
| 149 | extern "C" |
| 150 | void x265_encoder_get_stats(x265_encoder *enc, x265_stats *outputStats, uint32_t statsSizeBytes) |
| 151 | { |
| 152 | if (enc && outputStats) |
| 153 | { |
| 154 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 155 | encoder->fetchStats(outputStats, statsSizeBytes); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | extern "C" |
| 160 | void x265_encoder_log(x265_encoder* enc, int argc, char **argv) |
| 161 | { |
| 162 | if (enc) |
| 163 | { |
| 164 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 165 | encoder->writeLog(argc, argv); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | extern "C" |
| 170 | void x265_encoder_close(x265_encoder *enc) |
| 171 | { |
| 172 | if (enc) |
| 173 | { |
| 174 | Encoder *encoder = static_cast<Encoder*>(enc); |
| 175 | |
| 176 | encoder->printSummary(); |
| 177 | encoder->destroy(); |
| 178 | delete encoder; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | extern "C" |
| 183 | void x265_cleanup(void) |
| 184 | { |
| 185 | BitCost::destroy(); |
| 186 | } |
| 187 | |
| 188 | extern "C" |
| 189 | x265_picture *x265_picture_alloc() |
| 190 | { |
| 191 | return (x265_picture*)x265_malloc(sizeof(x265_picture)); |
| 192 | } |
| 193 | |
| 194 | extern "C" |
| 195 | void x265_picture_init(x265_param *param, x265_picture *pic) |
| 196 | { |
| 197 | memset(pic, 0, sizeof(x265_picture)); |
| 198 | |
| 199 | pic->bitDepth = param->internalBitDepth; |
| 200 | pic->colorSpace = param->internalCsp; |
| 201 | pic->forceqp = X265_QP_AUTO; |
| 202 | if (param->analysisMode) |
| 203 | { |
| 204 | uint32_t widthInCU = (param->sourceWidth + g_maxCUSize - 1) >> g_maxLog2CUSize; |
| 205 | uint32_t heightInCU = (param->sourceHeight + g_maxCUSize - 1) >> g_maxLog2CUSize; |
| 206 | |
| 207 | uint32_t numCUsInFrame = widthInCU * heightInCU; |
| 208 | pic->analysisData.numCUsInFrame = numCUsInFrame; |
| 209 | pic->analysisData.numPartitions = NUM_CU_PARTITIONS; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | extern "C" |
| 214 | void x265_picture_free(x265_picture *p) |
| 215 | { |
| 216 | return x265_free(p); |
| 217 | } |