Showing the changes to plugins/opusCodec/src/OpusEncoder.cpp (1 of 4 files). Show all
Unified · Split
| @@ -16,40 +16,39 @@ · 15 unmodified lines | |||
| 16 | 16 | ||
| 17 | static QLoggingCategory encoder("AthenaOpusEncoder"); | 17 | static QLoggingCategory encoder("AthenaOpusEncoder"); |
| 18 | 18 | ||
| 19 | static QString error_to_string(int error) { | 19 | static QString errorToString(int error) { |
| 20 | switch (error) { | 20 | switch (error) { |
| 21 | case OPUS_OK: | 21 | case OPUS_OK: |
| 22 | return "OK"; | 22 | return "OK"; |
| 23 | case OPUS_BAD_ARG: | 23 | case OPUS_BAD_ARG: |
| 24 | return "One or more invalid/out of range arguments."; | 24 | return "One or more invalid/out of range arguments."; |
| 25 | case OPUS_BUFFER_TOO_SMALL: | 25 | case OPUS_BUFFER_TOO_SMALL: |
| 26 | return "The mode struct passed is invalid."; | 26 | return "The mode struct passed is invalid."; |
| 27 | case OPUS_INTERNAL_ERROR: | 27 | case OPUS_INTERNAL_ERROR: |
| 28 | return "An internal error was detected."; | 28 | return "An internal error was detected."; |
| 29 | case OPUS_INVALID_PACKET: | 29 | case OPUS_INVALID_PACKET: |
| 30 | return "The compressed data passed is corrupted."; | 30 | return "The compressed data passed is corrupted."; |
| 31 | case OPUS_UNIMPLEMENTED: | 31 | case OPUS_UNIMPLEMENTED: |
| 32 | return "Invalid/unsupported request number."; | 32 | return "Invalid/unsupported request number."; |
| 33 | case OPUS_INVALID_STATE: | 33 | case OPUS_INVALID_STATE: |
| 34 | return "An encoder or decoder structure is invalid or already freed."; | 34 | return "An encoder or decoder structure is invalid or already freed."; |
| 35 | default: | 35 | default: |
| 36 | return QString("Unknown error code: %i").arg(error); | 36 | return QString("Unknown error code: %i").arg(error); |
| 37 | } | 37 | } |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | AthenaOpusEncoder::AthenaOpusEncoder(int sampleRate, int numChannels) | 42 | AthenaOpusEncoder::AthenaOpusEncoder(int sampleRate, int numChannels) { |
| 43 | { | 43 | _opusSampleRate = sampleRate; |
| 44 | _opus_sample_rate = sampleRate; | 44 | _opusChannels = numChannels; |
| 45 | _opus_channels = numChannels; | ||
| 46 | 45 | ||
| 47 | int error; | 46 | int error; |
| 48 | 47 | ||
| 49 | _encoder = opus_encoder_create(sampleRate, numChannels, DEFAULT_APPLICATION, &error); | 48 | _encoder = opus_encoder_create(sampleRate, numChannels, DEFAULT_APPLICATION, &error); |
| 50 | 49 | ||
| 51 | if ( error != OPUS_OK ) { | 50 | if (error != OPUS_OK) { |
| 52 | qCCritical(encoder) << "Failed to initialize Opus encoder: " << error_to_string(error); | 51 | qCCritical(encoder) << "Failed to initialize Opus encoder: " << errorToString(error); |
| 53 | _encoder = nullptr; | 52 | _encoder = nullptr; |
| 54 | return; | 53 | return; |
| 55 | } | 54 | } |
| @@ -62,8 +61,7 @@ AthenaOpusEncoder::AthenaOpusEncoder(int sampleRate, int numChannels) · 6 unmodified lines | |||
| 62 | qCDebug(encoder) << "Opus encoder initialized, sampleRate = " << sampleRate << "; numChannels = " << numChannels; | 61 | qCDebug(encoder) << "Opus encoder initialized, sampleRate = " << sampleRate << "; numChannels = " << numChannels; |
| 63 | } | 62 | } |
| 64 | 63 | ||
| 65 | AthenaOpusEncoder::~AthenaOpusEncoder() | 64 | AthenaOpusEncoder::~AthenaOpusEncoder() { |
| 66 | { | ||
| 67 | opus_encoder_destroy(_encoder); | 65 | opus_encoder_destroy(_encoder); |
| 68 | } | 66 | } |
| 69 | 67 | ||
| @@ -75,225 +73,201 @@ void AthenaOpusEncoder::encode(const QByteArray& decodedBuffer, QByteArray& enco · 5 unmodified lines | |||
| 75 | assert(_encoder); | 73 | assert(_encoder); |
| 76 | 74 | ||
| 77 | encodedBuffer.resize( decodedBuffer.size() ); | 75 | encodedBuffer.resize( decodedBuffer.size() ); |
| 78 | int frame_size = decodedBuffer.length()/ _opus_channels / static_cast<int>(sizeof(opus_int16)); | 76 | int frameSize = decodedBuffer.length()/ _opusChannels / static_cast<int>(sizeof(opus_int16)); |
| 79 | 77 | ||
| 80 | int bytes = opus_encode(_encoder, reinterpret_cast<const opus_int16*>(decodedBuffer.constData()), frame_size, | 78 | int bytes = opus_encode(_encoder, reinterpret_cast<const opus_int16*>(decodedBuffer.constData()), frameSize, |
| 81 | reinterpret_cast<unsigned char*>(encodedBuffer.data()), encodedBuffer.size() ); | 79 | reinterpret_cast<unsigned char*>(encodedBuffer.data()), encodedBuffer.size() ); |
| 82 | 80 | ||
| 83 | if ( bytes >= 0 ) { | 81 | if (bytes >= 0) { |
| 84 | //qCDebug(encoder) << "Encoded " << decodedBuffer.length() << " bytes into " << bytes << " opus bytes"; | ||
| 85 | encodedBuffer.resize(bytes); | 82 | encodedBuffer.resize(bytes); |
| 86 | } else { | 83 | } else { |
| 87 | encodedBuffer.resize(0); | 84 | encodedBuffer.resize(0); |
| 88 | 85 | ||
| 89 | qCWarning(encoder) << "Error when encoding " << decodedBuffer.length() << " bytes of audio: " | 86 | qCWarning(encoder) << "Error when encoding " << decodedBuffer.length() << " bytes of audio: " |
| 90 | << error_to_string(bytes); | 87 | << errorToString(bytes); |
| 91 | } | 88 | } |
| 92 | 89 | ||
| 93 | } | 90 | } |
| 94 | 91 | ||
| 95 | int AthenaOpusEncoder::getComplexity() const | 92 | int AthenaOpusEncoder::getComplexity() const { |
| 96 | { | ||
| 97 | assert(_encoder); | 93 | assert(_encoder); |
| 98 | int ret; | 94 | int returnValue; |
| 99 | opus_encoder_ctl(_encoder, OPUS_GET_COMPLEXITY(&ret)); | 95 | opus_encoder_ctl(_encoder, OPUS_GET_COMPLEXITY(&returnValue)); |
| 100 | return ret; | 96 | return returnValue; |
| 101 | } | 97 | } |
| 102 | 98 | ||
| 103 | void AthenaOpusEncoder::setComplexity(int complexity) | 99 | void AthenaOpusEncoder::setComplexity(int complexity) { |
| 104 | { | ||
| 105 | assert(_encoder); | 100 | assert(_encoder); |
| 106 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_COMPLEXITY(complexity)); | 101 | int returnValue = opus_encoder_ctl(_encoder, OPUS_SET_COMPLEXITY(complexity)); |
| 107 | 102 | ||
| 108 | if (ret != OPUS_OK) { | 103 | if (returnValue != OPUS_OK) { |
| 109 | qCWarning(encoder) << "Error when setting complexity to " << complexity << ": " << error_to_string(ret); | 104 | qCWarning(encoder) << "Error when setting complexity to " << complexity << ": " << errorToString(returnValue); |
| 110 | } | 105 | } |
| 111 | } | 106 | } |
| 112 | 107 | ||
| 113 | int AthenaOpusEncoder::getBitrate() const | 108 | int AthenaOpusEncoder::getBitrate() const { |
| 114 | { | ||
| 115 | assert(_encoder); | 109 | assert(_encoder); |
| 116 | int ret; | 110 | int returnValue; |
| 117 | opus_encoder_ctl(_encoder, OPUS_GET_BITRATE(&ret)); | 111 | opus_encoder_ctl(_encoder, OPUS_GET_BITRATE(&returnValue)); |
| 118 | return ret; | 112 | return returnValue; |
| 119 | } | 113 | } |
| 120 | 114 | ||
| 121 | void AthenaOpusEncoder::setBitrate(int bitrate) | 115 | void AthenaOpusEncoder::setBitrate(int bitrate) { |
| 122 | { | ||
| 123 | assert(_encoder); | 116 | assert(_encoder); |
| 124 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate)); | 117 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate)); |
| 125 | 118 | ||
| 126 | if (ret != OPUS_OK) { | 119 | if (errorCode != OPUS_OK) { |
| 127 | qCWarning(encoder) << "Error when setting bitrate to " << bitrate << ": " << error_to_string(ret); | 120 | qCWarning(encoder) << "Error when setting bitrate to " << bitrate << ": " << errorToString(errorCode); |
| 128 | } | 121 | } |
| 129 | } | 122 | } |
| 130 | 123 | ||
| 131 | int AthenaOpusEncoder::getVBR() const | 124 | int AthenaOpusEncoder::getVBR() const { |
| 132 | { | ||
| 133 | assert(_encoder); | 125 | assert(_encoder); |
| 134 | int ret; | 126 | int returnValue; |
| 135 | opus_encoder_ctl(_encoder, OPUS_GET_VBR(&ret)); | 127 | opus_encoder_ctl(_encoder, OPUS_GET_VBR(&returnValue)); |
| 136 | return ret; | 128 | return returnValue; |
| 137 | } | 129 | } |
| 138 | 130 | ||
| 139 | void AthenaOpusEncoder::setVBR(int vbr) | 131 | void AthenaOpusEncoder::setVBR(int vbr) { |
| 140 | { | ||
| 141 | assert(_encoder); | 132 | assert(_encoder); |
| 142 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_VBR(vbr)); | 133 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_VBR(vbr)); |
| 143 | 134 | ||
| 144 | if (ret != OPUS_OK) { | 135 | if (errorCode != OPUS_OK) { |
| 145 | qCWarning(encoder) << "Error when setting VBR to " << vbr << ": " << error_to_string(ret); | 136 | qCWarning(encoder) << "Error when setting VBR to " << vbr << ": " << errorToString(errorCode); |
| 146 | } | 137 | } |
| 147 | } | 138 | } |
| 148 | 139 | ||
| 149 | int AthenaOpusEncoder::getVBRConstraint() const | 140 | int AthenaOpusEncoder::getVBRConstraint() const { |
| 150 | { | ||
| 151 | assert(_encoder); | 141 | assert(_encoder); |
| 152 | int ret; | 142 | int returnValue; |
| 153 | opus_encoder_ctl(_encoder, OPUS_GET_VBR_CONSTRAINT(&ret)); | 143 | opus_encoder_ctl(_encoder, OPUS_GET_VBR_CONSTRAINT(&returnValue)); |
| 154 | return ret; | 144 | return returnValue; |
| 155 | } | 145 | } |
| 156 | 146 | ||
| 157 | void AthenaOpusEncoder::setVBRConstraint(int vbr_const) | 147 | void AthenaOpusEncoder::setVBRConstraint(int vbr_const) { |
| 158 | { | ||
| 159 | assert(_encoder); | 148 | assert(_encoder); |
| 160 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_VBR_CONSTRAINT(vbr_const)); | 149 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_VBR_CONSTRAINT(vbr_const)); |
| 161 | 150 | ||
| 162 | if (ret != OPUS_OK) { | 151 | if (errorCode != OPUS_OK) { |
| 163 | qCWarning(encoder) << "Error when setting VBR constraint to " << vbr_const << ": " << error_to_string(ret); | 152 | qCWarning(encoder) << "Error when setting VBR constraint to " << vbr_const << ": " << errorToString(errorCode); |
| 164 | } | 153 | } |
| 165 | } | 154 | } |
| 166 | 155 | ||
| 167 | int AthenaOpusEncoder::getMaxBandwidth() const | 156 | int AthenaOpusEncoder::getMaxBandwidth() const { |
| 168 | { | ||
| 169 | assert(_encoder); | 157 | assert(_encoder); |
| 170 | int ret; | 158 | int returnValue; |
| 171 | opus_encoder_ctl(_encoder, OPUS_GET_MAX_BANDWIDTH(&ret)); | 159 | opus_encoder_ctl(_encoder, OPUS_GET_MAX_BANDWIDTH(&returnValue)); |
| 172 | return ret; | 160 | return returnValue; |
| 173 | } | 161 | } |
| 174 | 162 | ||
| 175 | void AthenaOpusEncoder::setMaxBandwidth(int maxbw) | 163 | void AthenaOpusEncoder::setMaxBandwidth(int maxbw) { |
| 176 | { | ||
| 177 | assert(_encoder); | 164 | assert(_encoder); |
| 178 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_MAX_BANDWIDTH(maxbw)); | 165 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_MAX_BANDWIDTH(maxbw)); |
| 179 | 166 | ||
| 180 | if (ret != OPUS_OK) { | 167 | if (errorCode != OPUS_OK) { |
| 181 | qCWarning(encoder) << "Error when setting max bandwidth to " << maxbw << ": " << error_to_string(ret); | 168 | qCWarning(encoder) << "Error when setting max bandwidth to " << maxbw << ": " << errorToString(errorCode); |
| 182 | } | 169 | } |
| 183 | } | 170 | } |
| 184 | 171 | ||
| 185 | int AthenaOpusEncoder::getBandwidth() const | 172 | int AthenaOpusEncoder::getBandwidth() const { |
| 186 | { | ||
| 187 | assert(_encoder); | 173 | assert(_encoder); |
| 188 | int ret; | 174 | int bandwidth; |
| 189 | opus_encoder_ctl(_encoder, OPUS_GET_BANDWIDTH(&ret)); | 175 | opus_encoder_ctl(_encoder, OPUS_GET_BANDWIDTH(&bandwidth)); |
| 190 | return ret; | 176 | return bandwidth; |
| 191 | } | 177 | } |
| 192 | 178 | ||
| 193 | void AthenaOpusEncoder::setBandwidth(int bw) | 179 | void AthenaOpusEncoder::setBandwidth(int bandwidth) { |
| 194 | { | ||
| 195 | assert(_encoder); | 180 | assert(_encoder); |
| 196 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_BANDWIDTH(bw)); | 181 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_BANDWIDTH(bandwidth)); |
| 197 | 182 | ||
| 198 | if (ret != OPUS_OK) { | 183 | if (errorCode != OPUS_OK) { |
| 199 | qCWarning(encoder) << "Error when setting bandwidth to " << bw << ": " << error_to_string(ret); | 184 | qCWarning(encoder) << "Error when setting bandwidth to " << bandwidth << ": " << errorToString(errorCode); |
| 200 | } | 185 | } |
| 201 | } | 186 | } |
| 202 | 187 | ||
| 203 | int AthenaOpusEncoder::getSignal() const | 188 | int AthenaOpusEncoder::getSignal() const { |
| 204 | { | ||
| 205 | assert(_encoder); | 189 | assert(_encoder); |
| 206 | int ret; | 190 | int signal; |
| 207 | opus_encoder_ctl(_encoder, OPUS_GET_SIGNAL(&ret)); | 191 | opus_encoder_ctl(_encoder, OPUS_GET_SIGNAL(&signal)); |
| 208 | return ret; | 192 | return signal; |
| 209 | } | 193 | } |
| 210 | 194 | ||
| 211 | void AthenaOpusEncoder::setSignal(int signal) | 195 | void AthenaOpusEncoder::setSignal(int signal) { |
| 212 | { | ||
| 213 | assert(_encoder); | 196 | assert(_encoder); |
| 214 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_SIGNAL(signal)); | 197 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_SIGNAL(signal)); |
| 215 | 198 | ||
| 216 | if (ret != OPUS_OK) { | 199 | if (errorCode != OPUS_OK) { |
| 217 | qCWarning(encoder) << "Error when setting signal to " << signal << ": " << error_to_string(ret); | 200 | qCWarning(encoder) << "Error when setting signal to " << signal << ": " << errorToString(errorCode); |
| 218 | } | 201 | } |
| 219 | } | 202 | } |
| 220 | 203 | ||
| 221 | int AthenaOpusEncoder::getApplication() const | 204 | int AthenaOpusEncoder::getApplication() const { |
| 222 | { | ||
| 223 | assert(_encoder); | 205 | assert(_encoder); |
| 224 | int ret; | 206 | int applicationValue; |
| 225 | opus_encoder_ctl(_encoder, OPUS_GET_APPLICATION(&ret)); | 207 | opus_encoder_ctl(_encoder, OPUS_GET_APPLICATION(&applicationValue)); |
| 226 | return ret; | 208 | return applicationValue; |
| 227 | } | 209 | } |
| 228 | 210 | ||
| 229 | void AthenaOpusEncoder::setApplication(int application) | 211 | void AthenaOpusEncoder::setApplication(int application) { |
| 230 | { | ||
| 231 | assert(_encoder); | 212 | assert(_encoder); |
| 232 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_APPLICATION(application)); | 213 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_APPLICATION(application)); |
| 233 | 214 | ||
| 234 | if (ret != OPUS_OK) { | 215 | if (errorCode != OPUS_OK) { |
| 235 | qCWarning(encoder) << "Error when setting application to " << application << ": " << error_to_string(ret); | 216 | qCWarning(encoder) << "Error when setting application to " << application << ": " << errorToString(errorCode); |
| 236 | } | 217 | } |
| 237 | } | 218 | } |
| 238 | 219 | ||
| 239 | int AthenaOpusEncoder::getLookahead() const | 220 | int AthenaOpusEncoder::getLookahead() const { |
| 240 | { | ||
| 241 | assert(_encoder); | 221 | assert(_encoder); |
| 242 | int ret; | 222 | int lookAhead; |
| 243 | opus_encoder_ctl(_encoder, OPUS_GET_LOOKAHEAD(&ret)); | 223 | opus_encoder_ctl(_encoder, OPUS_GET_LOOKAHEAD(&lookAhead)); |
| 244 | return ret; | 224 | return lookAhead; |
| 245 | } | 225 | } |
| 246 | 226 | ||
| 247 | int AthenaOpusEncoder::getInbandFEC() const | 227 | int AthenaOpusEncoder::getInbandFEC() const { |
| 248 | { | ||
| 249 | assert(_encoder); | 228 | assert(_encoder); |
| 250 | int ret; | 229 | int fec; |
| 251 | opus_encoder_ctl(_encoder, OPUS_GET_INBAND_FEC(&ret)); | 230 | opus_encoder_ctl(_encoder, OPUS_GET_INBAND_FEC(&fec)); |
| 252 | return ret; | 231 | return fec; |
| 253 | } | 232 | } |
| 254 | 233 | ||
| 255 | void AthenaOpusEncoder::setInbandFEC(int fec) | 234 | void AthenaOpusEncoder::setInbandFEC(int fec) { |
| 256 | { | ||
| 257 | assert(_encoder); | 235 | assert(_encoder); |
| 258 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_INBAND_FEC(fec)); | 236 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_INBAND_FEC(fec)); |
| 259 | 237 | ||
| 260 | if (ret != OPUS_OK) { | 238 | if (errorCode != OPUS_OK) { |
| 261 | qCWarning(encoder) << "Error when setting inband FEC to " << fec << ": " << error_to_string(ret); | 239 | qCWarning(encoder) << "Error when setting inband FEC to " << fec << ": " << errorToString(errorCode); |
| 262 | } | 240 | } |
| 263 | } | 241 | } |
| 264 | 242 | ||
| 265 | int AthenaOpusEncoder::getExpectedPacketLossPercent() const | 243 | int AthenaOpusEncoder::getExpectedPacketLossPercent() const { |
| 266 | { | ||
| 267 | assert(_encoder); | 244 | assert(_encoder); |
| 268 | int ret; | 245 | int lossPercentage; |
| 269 | opus_encoder_ctl(_encoder, OPUS_GET_PACKET_LOSS_PERC(&ret)); | 246 | opus_encoder_ctl(_encoder, OPUS_GET_PACKET_LOSS_PERC(&lossPercentage)); |
| 270 | return ret; | 247 | return lossPercentage; |
| 271 | } | 248 | } |
| 272 | 249 | ||
| 273 | void AthenaOpusEncoder::setExpectedPacketLossPercent(int perc) | 250 | void AthenaOpusEncoder::setExpectedPacketLossPercentage(int percent) { |
| 274 | { | ||
| 275 | assert(_encoder); | 251 | assert(_encoder); |
| 276 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_PACKET_LOSS_PERC(perc)); | 252 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_PACKET_LOSS_PERC(percent)); |
| 277 | 253 | ||
| 278 | if (ret != OPUS_OK) { | 254 | if (errorCode != OPUS_OK) { |
| 279 | qCWarning(encoder) << "Error when setting loss percent to " << perc << ": " << error_to_string(ret); | 255 | qCWarning(encoder) << "Error when setting loss percent to " << percent << ": " << errorToString(errorCode); |
| 280 | } | 256 | } |
| 281 | } | 257 | } |
| 282 | 258 | ||
| 283 | int AthenaOpusEncoder::getDTX() const | 259 | int AthenaOpusEncoder::getDTX() const { |
| 284 | { | ||
| 285 | assert(_encoder); | 260 | assert(_encoder); |
| 286 | int ret; | 261 | int dtx; |
| 287 | opus_encoder_ctl(_encoder, OPUS_GET_DTX(&ret)); | 262 | opus_encoder_ctl(_encoder, OPUS_GET_DTX(&dtx)); |
| 288 | return ret; | 263 | return dtx; |
| 289 | } | 264 | } |
| 290 | 265 | ||
| 291 | void AthenaOpusEncoder::setDTX(int dtx) | 266 | void AthenaOpusEncoder::setDTX(int dtx) { |
| 292 | { | ||
| 293 | assert(_encoder); | 267 | assert(_encoder); |
| 294 | int ret = opus_encoder_ctl(_encoder, OPUS_SET_DTX(dtx)); | 268 | int errorCode = opus_encoder_ctl(_encoder, OPUS_SET_DTX(dtx)); |
| 295 | 269 | ||
| 296 | if (ret != OPUS_OK) { | 270 | if (errorCode != OPUS_OK) { |
| 297 | qCWarning(encoder) << "Error when setting DTX to " << dtx << ": " << error_to_string(ret); | 271 | qCWarning(encoder) << "Error when setting DTX to " << dtx << ": " << errorToString(errorCode); |
| 298 | } | 272 | } |
| 299 | } | 273 | } |