| 5 |
| #include <stdlib.h> |
| 6 | #include <time.h> |
| 7 | #include <opusfile.h> |
| 8 | #include <math.h> |
| 9 | #include "c_utils.h" |
| 10 |
| 11 | typedef struct { |
| 12 | int version; |
| 13 | int channels; /* Number of channels: 1..255 */ |
| 14 | int preskip; |
| 15 | ogg_uint32_t input_sample_rate; |
| 16 | int gain; /* in dB S7.8 should be zero whenever possible */ |
| 17 | int channel_mapping; |
| 18 | /* The rest is only used if channel_mapping != 0 */ |
| 19 | int nb_streams; |
| 20 | int nb_coupled; |
| 21 | unsigned char stream_map[255]; |
| 22 | } OpusHeader; |
| 23 |
| 24 | typedef struct { |
| 25 | unsigned char *data; |
| 26 | int maxlen; |
| 27 | int pos; |
| 28 | } Packet; |
| 29 |
| 30 | typedef struct { |
| 31 | const unsigned char *data; |
| 32 | int maxlen; |
| 33 | int pos; |
| 34 | } ROPacket; |
| 35 |
| 36 | typedef struct { |
| 37 | void *readdata; |
| 38 | opus_int64 total_samples_per_channel; |
| 39 | int rawmode; |
| 40 | int channels; |
| 41 | long rate; |
| 42 | int gain; |
| 43 | int samplesize; |
| 44 | int endianness; |
| 45 | char *infilename; |
| 46 | int ignorelength; |
| 47 | int skip; |
| 48 | int extraout; |
| 49 | char *comments; |
| 50 | int comments_length; |
| 51 | int copy_comments; |
| 52 | } oe_enc_opt; |
| 53 |
| 54 | static int write_uint32(Packet *p, ogg_uint32_t val) { |
| 55 | if (p->pos > p->maxlen - 4) { |
| 56 | return 0; |
| 57 | } |
| 58 | p->data[p->pos ] = (val ) & 0xFF; |
| 59 | p->data[p->pos+1] = (val>> 8) & 0xFF; |
| 60 | p->data[p->pos+2] = (val>>16) & 0xFF; |
| 61 | p->data[p->pos+3] = (val>>24) & 0xFF; |
| 62 | p->pos += 4; |
| 63 | return 1; |
| 64 | } |
| 65 |
| 66 | static int write_uint16(Packet *p, ogg_uint16_t val) { |
| 67 | if (p->pos > p->maxlen-2) { |
| 68 | return 0; |
| 69 | } |
| 70 | p->data[p->pos ] = (val ) & 0xFF; |
| 71 | p->data[p->pos+1] = (val>> 8) & 0xFF; |
| 72 | p->pos += 2; |
| 73 | return 1; |
| 74 | } |
| 75 |
| 76 | static int write_chars(Packet *p, const unsigned char *str, int nb_chars) |
| 77 | { |
| 78 | int i; |
| 79 | if (p->pos>p->maxlen-nb_chars) |
| 80 | return 0; |
| 81 | for (i=0;i<nb_chars;i++) |
| 82 | p->data[p->pos++] = str[i]; |
| 83 | return 1; |
| 84 | } |
| 85 |
| 86 | static int read_uint32(ROPacket *p, ogg_uint32_t *val) |
| 87 | { |
| 88 | if (p->pos>p->maxlen-4) |
| 89 | return 0; |
| 90 | *val = (ogg_uint32_t)p->data[p->pos ]; |
| 91 | *val |= (ogg_uint32_t)p->data[p->pos+1]<< 8; |
| 92 | *val |= (ogg_uint32_t)p->data[p->pos+2]<<16; |
| 93 | *val |= (ogg_uint32_t)p->data[p->pos+3]<<24; |
| 94 | p->pos += 4; |
| 95 | return 1; |
| 96 | } |
| 97 |
| 98 | static int read_uint16(ROPacket *p, ogg_uint16_t *val) |
| 99 | { |
| 100 | if (p->pos>p->maxlen-2) |
| 101 | return 0; |
| 102 | *val = (ogg_uint16_t)p->data[p->pos ]; |
| 103 | *val |= (ogg_uint16_t)p->data[p->pos+1]<<8; |
| 104 | p->pos += 2; |
| 105 | return 1; |
| 106 | } |
| 107 |
| 108 | static int read_chars(ROPacket *p, unsigned char *str, int nb_chars) |
| 109 | { |
| 110 | int i; |
| 111 | if (p->pos>p->maxlen-nb_chars) |
| 112 | return 0; |
| 113 | for (i=0;i<nb_chars;i++) |
| 114 | str[i] = p->data[p->pos++]; |
| 115 | return 1; |
| 116 | } |
| 117 |
| 118 | int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len) { |
| 119 | int i; |
| 120 | Packet p; |
| 121 | unsigned char ch; |
| 122 |
| 123 | p.data = packet; |
| 124 | p.maxlen = len; |
| 125 | p.pos = 0; |
| 126 | if (len < 19) { |
| 127 | return 0; |
| 128 | } |
| 129 | if (!write_chars(&p, (const unsigned char *)"OpusHead", 8)) { |
| 130 | return 0; |
| 131 | } |
| 132 |
| 133 | ch = 1; |
| 134 | if (!write_chars(&p, &ch, 1)) { |
| 135 | return 0; |
| 136 | } |
| 137 |
| 138 | ch = h->channels; |
| 139 | if (!write_chars(&p, &ch, 1)) { |
| 140 | return 0; |
| 141 | } |
| 142 |
| 143 | if (!write_uint16(&p, h->preskip)) { |
| 144 | return 0; |
| 145 | } |
| 146 |
| 147 | if (!write_uint32(&p, h->input_sample_rate)) { |
| 148 | return 0; |
| 149 | } |
| 150 |
| 151 | if (!write_uint16(&p, h->gain)) { |
| 152 | return 0; |
| 153 | } |
| 154 |
| 155 | ch = h->channel_mapping; |
| 156 | if (!write_chars(&p, &ch, 1)) { |
| 157 | return 0; |
| 158 | } |
| 159 |
| 160 | if (h->channel_mapping != 0) { |
| 161 | ch = h->nb_streams; |
| 162 | if (!write_chars(&p, &ch, 1)) { |
| 163 | return 0; |
| 164 | } |
| 165 |
| 166 | ch = h->nb_coupled; |
| 167 | if (!write_chars(&p, &ch, 1)) { |
| 168 | return 0; |
| 169 | } |
| 170 |
| 171 | /* Multi-stream support */ |
| 172 | for (i = 0; i < h->channels; i++) { |
| 173 | if (!write_chars(&p, &h->stream_map[i], 1)) { |
| 174 | return 0; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 |
| 179 | return p.pos; |
| 180 | } |
| 181 |
| 182 | #define writeint(buf, base, val) do { buf[base + 3] = ((val) >> 24) & 0xff; \ |
| 183 | buf[base + 2]=((val) >> 16) & 0xff; \ |
| 184 | buf[base + 1]=((val) >> 8) & 0xff; \ |
| 185 | buf[base] = (val) & 0xff; \ |
| 186 | } while(0) |
| 187 |
| 188 | static void comment_init(char **comments, int *length, const char *vendor_string) { |
| 189 | // The 'vendor' field should be the actual encoding library used |
| 190 | size_t vendor_length = strlen(vendor_string); |
| 191 | int user_comment_list_length = 0; |
| 192 | size_t len = 8 + 4 + vendor_length + 4; |
| 193 | char *p = (char *)malloc(len); |
| 194 | memcpy(p, "OpusTags", 8); |
| 195 | writeint(p, 8, vendor_length); |
| 196 | memcpy(p + 12, vendor_string, vendor_length); |
| 197 | writeint(p, 12 + vendor_length, user_comment_list_length); |
| 198 | *length = len; |
| 199 | *comments = p; |
| 200 | } |
| 201 |
| 202 | static void comment_pad(char **comments, int* length, size_t amount) { |
| 203 | if (amount > 0) { |
| 204 | char *p = *comments; |
| 205 | // Make sure there is at least amount worth of padding free, and round up to the maximum that fits in the current ogg segments |
| 206 | size_t newlen = (*length + amount + 255) / 255 * 255 - 1; |
| 207 | p = realloc(p, newlen); |
| 208 | for (int32_t i = *length; i < newlen; i++) { |
| 209 | p[i] = 0; |
| 210 | } |
| 211 | *comments = p; |
| 212 | *length = newlen; |
| 213 | } |
| 214 | } |
| 215 |
| 216 | static int writeOggPage(ogg_page *page, FILE *os) { |
| 217 | int written = fwrite(page->header, sizeof(unsigned char), (size_t) page->header_len, os); |
| 218 | written += fwrite(page->body, sizeof(unsigned char), (size_t) page->body_len, os); |
| 219 | return written; |
| 220 | } |
| 221 |
| 222 | const opus_int32 bitrate = 16000; |
| 223 | const opus_int32 rate = 16000; |
| 224 | const opus_int32 frame_size = 960; |
| 225 | const int with_cvbr = 1; |
| 226 | const int max_ogg_delay = 0; |
| 227 | const int comment_padding = 512; |
| 228 |
| 229 | opus_int32 coding_rate = 16000; |
| 230 | ogg_int32_t _packetId; |
| 231 | OpusEncoder *_encoder = 0; |
| 232 | uint8_t *_packet = 0; |
| 233 | ogg_stream_state os; |
| 234 | FILE *_fileOs = 0; |
| 235 | oe_enc_opt inopt; |
| 236 | OpusHeader header; |
| 237 | opus_int32 min_bytes; |
| 238 | int max_frame_bytes; |
| 239 | ogg_packet op; |
| 240 | ogg_page og; |
| 241 | opus_int64 bytes_written; |
| 242 | opus_int64 pages_out; |
| 243 | opus_int64 total_samples; |
| 244 | ogg_int64_t enc_granulepos; |
| 245 | ogg_int64_t last_granulepos; |
| 246 | int size_segments; |
| 247 | int last_segments; |
| 248 |
| 249 | void cleanupRecorder() { |
| 250 |
| 251 | ogg_stream_flush(&os, &og); |
| 252 |
| 253 | if (_encoder) { |
| 254 | opus_encoder_destroy(_encoder); |
| 255 | _encoder = 0; |
| 256 | } |
| 257 |
| 258 | ogg_stream_clear(&os); |
| 259 |
| 260 | if (_packet) { |
| 261 | free(_packet); |
| 262 | _packet = 0; |
| 263 | } |
| 264 |
| 265 | if (_fileOs) { |
| 266 | fclose(_fileOs); |
| 267 | _fileOs = 0; |
| 268 | } |
| 269 |
| 270 | _packetId = -1; |
| 271 | bytes_written = 0; |
| 272 | pages_out = 0; |
| 273 | total_samples = 0; |
| 274 | enc_granulepos = 0; |
| 275 | size_segments = 0; |
| 276 | last_segments = 0; |
| 277 | last_granulepos = 0; |
| 278 | memset(&os, 0, sizeof(ogg_stream_state)); |
| 279 | memset(&inopt, 0, sizeof(oe_enc_opt)); |
| 280 | memset(&header, 0, sizeof(OpusHeader)); |
| 281 | memset(&op, 0, sizeof(ogg_packet)); |
| 282 | memset(&og, 0, sizeof(ogg_page)); |
| 283 | } |
| 284 |
| 285 | int initRecorder(const char *path) { |
| 286 | cleanupRecorder(); |
| 287 |
| 288 | if (!path) { |
| 289 | return 0; |
| 290 | } |
| 291 |
| 292 | _fileOs = fopen(path, "wb"); |
| 293 | if (!_fileOs) { |
| 294 | return 0; |
| 295 | } |
| 296 |
| 297 | inopt.rate = rate; |
| 298 | inopt.gain = 0; |
| 299 | inopt.endianness = 0; |
| 300 | inopt.copy_comments = 0; |
| 301 | inopt.rawmode = 1; |
| 302 | inopt.ignorelength = 1; |
| 303 | inopt.samplesize = 16; |
| 304 | inopt.channels = 1; |
| 305 | inopt.skip = 0; |
| 306 |
| 307 | comment_init(&inopt.comments, &inopt.comments_length, opus_get_version_string()); |
| 308 | coding_rate = 16000; |
| 309 |
| 310 | if (rate != coding_rate) { |
| 311 | LOGE("Invalid rate"); |
| 312 | return 0; |
| 313 | } |
| 314 |
| 315 | header.channels = 1; |
| 316 | header.channel_mapping = 0; |
| 317 | header.input_sample_rate = rate; |
| 318 | header.gain = inopt.gain; |
| 319 | header.nb_streams = 1; |
| 320 |
| 321 | int result = OPUS_OK; |
| 322 | _encoder = opus_encoder_create(coding_rate, 1, OPUS_APPLICATION_AUDIO, &result); |
| 323 | if (result != OPUS_OK) { |
| 324 | LOGE("Error cannot create encoder: %s", opus_strerror(result)); |
| 325 | return 0; |
| 326 | } |
| 327 |
| 328 | min_bytes = max_frame_bytes = (1275 * 3 + 7) * header.nb_streams; |
| 329 | _packet = malloc(max_frame_bytes); |
| 330 |
| 331 | result = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate)); |
| 332 | if (result != OPUS_OK) { |
| 333 | LOGE("Error OPUS_SET_BITRATE returned: %s", opus_strerror(result)); |
| 334 | return 0; |
| 335 | } |
| 336 |
| 337 | #ifdef OPUS_SET_LSB_DEPTH |
| 338 | result = opus_encoder_ctl(_encoder, OPUS_SET_LSB_DEPTH(MAX(8, MIN(24, inopt.samplesize)))); |
| 339 | if (result != OPUS_OK) { |
| 340 | LOGE("Warning OPUS_SET_LSB_DEPTH returned: %s", opus_strerror(result)); |
| 341 | } |
| 342 | #endif |
| 343 |
| 344 | opus_int32 lookahead; |
| 345 | result = opus_encoder_ctl(_encoder, OPUS_GET_LOOKAHEAD(&lookahead)); |
| 346 | if (result != OPUS_OK) { |
| 347 | LOGE("Error OPUS_GET_LOOKAHEAD returned: %s", opus_strerror(result)); |
| 348 | return 0; |
| 349 | } |
| 350 |
| 351 | inopt.skip += lookahead; |
| 352 | header.preskip = (int)(inopt.skip * (48000.0 / coding_rate)); |
| 353 | inopt.extraout = (int)(header.preskip * (rate / 48000.0)); |
| 354 |
| 355 | if (ogg_stream_init(&os, rand()) == -1) { |
| 356 | LOGE("Error: stream init failed"); |
| 357 | return 0; |
| 358 | } |
| 359 |
| 360 | unsigned char header_data[100]; |
| 361 | int packet_size = opus_header_to_packet(&header, header_data, 100); |
| 362 | op.packet = header_data; |
| 363 | op.bytes = packet_size; |
| 364 | op.b_o_s = 1; |
| 365 | op.e_o_s = 0; |
| 366 | op.granulepos = 0; |
| 367 | op.packetno = 0; |
| 368 | ogg_stream_packetin(&os, &op); |
| 369 |
| 370 | while ((result = ogg_stream_flush(&os, &og))) { |
| 371 | if (!result) { |
| 372 | break; |
| 373 | } |
| 374 |
| 375 | int pageBytesWritten = writeOggPage(&og, _fileOs); |
| 376 | if (pageBytesWritten != og.header_len + og.body_len) { |
| 377 | LOGE("Error: failed writing header to output stream"); |
| 378 | return 0; |
| 379 | } |
| 380 | bytes_written += pageBytesWritten; |
| 381 | pages_out++; |
| 382 | } |
| 383 |
| 384 | comment_pad(&inopt.comments, &inopt.comments_length, comment_padding); |
| 385 | op.packet = (unsigned char *)inopt.comments; |
| 386 | op.bytes = inopt.comments_length; |
| 387 | op.b_o_s = 0; |
| 388 | op.e_o_s = 0; |
| 389 | op.granulepos = 0; |
| 390 | op.packetno = 1; |
| 391 | ogg_stream_packetin(&os, &op); |
| 392 |
| 393 | while ((result = ogg_stream_flush(&os, &og))) { |
| 394 | if (result == 0) { |
| 395 | break; |
| 396 | } |
| 397 |
| 398 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 399 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 400 | LOGE("Error: failed writing header to output stream"); |
| 401 | return 0; |
| 402 | } |
| 403 |
| 404 | bytes_written += writtenPageBytes; |
| 405 | pages_out++; |
| 406 | } |
| 407 |
| 408 | free(inopt.comments); |
| 409 |
| 410 | return 1; |
| 411 | } |
| 412 |
| 413 | int writeFrame(uint8_t *framePcmBytes, uint32_t frameByteCount) { |
| 414 | size_t cur_frame_size = frame_size; |
| 415 | _packetId++; |
| 416 |
| 417 | opus_int32 nb_samples = frameByteCount / 2; |
| 418 | total_samples += nb_samples; |
| 419 | if (nb_samples < frame_size) { |
| 420 | op.e_o_s = 1; |
| 421 | } else { |
| 422 | op.e_o_s = 0; |
| 423 | } |
| 424 |
| 425 | int nbBytes = 0; |
| 426 |
| 427 | if (nb_samples != 0) { |
| 428 | uint8_t *paddedFrameBytes = framePcmBytes; |
| 429 | int freePaddedFrameBytes = 0; |
| 430 |
| 431 | if (nb_samples < cur_frame_size) { |
| 432 | paddedFrameBytes = malloc(cur_frame_size * 2); |
| 433 | freePaddedFrameBytes = 1; |
| 434 | memcpy(paddedFrameBytes, framePcmBytes, frameByteCount); |
| 435 | memset(paddedFrameBytes + nb_samples * 2, 0, cur_frame_size * 2 - nb_samples * 2); |
| 436 | } |
| 437 |
| 438 | nbBytes = opus_encode(_encoder, (opus_int16 *)paddedFrameBytes, cur_frame_size, _packet, max_frame_bytes / 10); |
| 439 | if (freePaddedFrameBytes) { |
| 440 | free(paddedFrameBytes); |
| 441 | } |
| 442 |
| 443 | if (nbBytes < 0) { |
| 444 | LOGE("Encoding failed: %s. Aborting.", opus_strerror(nbBytes)); |
| 445 | return 0; |
| 446 | } |
| 447 |
| 448 | enc_granulepos += cur_frame_size * 48000 / coding_rate; |
| 449 | size_segments = (nbBytes + 255) / 255; |
| 450 | min_bytes = MIN(nbBytes, min_bytes); |
| 451 | } |
| 452 |
| 453 | while ((((size_segments <= 255) && (last_segments + size_segments > 255)) || (enc_granulepos - last_granulepos > max_ogg_delay)) && ogg_stream_flush_fill(&os, &og, 255 * 255)) { |
| 454 | if (ogg_page_packets(&og) != 0) { |
| 455 | last_granulepos = ogg_page_granulepos(&og); |
| 456 | } |
| 457 |
| 458 | last_segments -= og.header[26]; |
| 459 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 460 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 461 | LOGE("Error: failed writing data to output stream"); |
| 462 | return 0; |
| 463 | } |
| 464 | bytes_written += writtenPageBytes; |
| 465 | pages_out++; |
| 466 | } |
| 467 |
| 468 | op.packet = _packet; |
| 469 | op.bytes = nbBytes; |
| 470 | op.b_o_s = 0; |
| 471 | op.granulepos = enc_granulepos; |
| 472 | if (op.e_o_s) { |
| 473 | op.granulepos = ((total_samples * 48000 + rate - 1) / rate) + header.preskip; |
| 474 | } |
| 475 | op.packetno = 2 + _packetId; |
| 476 | ogg_stream_packetin(&os, &op); |
| 477 | last_segments += size_segments; |
| 478 |
| 479 | while ((op.e_o_s || (enc_granulepos + (frame_size * 48000 / coding_rate) - last_granulepos > max_ogg_delay) || (last_segments >= 255)) ? ogg_stream_flush_fill(&os, &og, 255 * 255) : ogg_stream_pageout_fill(&os, &og, 255 * 255)) { |
| 480 | if (ogg_page_packets(&og) != 0) { |
| 481 | last_granulepos = ogg_page_granulepos(&og); |
| 482 | } |
| 483 | last_segments -= og.header[26]; |
| 484 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 485 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 486 | LOGE("Error: failed writing data to output stream"); |
| 487 | return 0; |
| 488 | } |
| 489 | bytes_written += writtenPageBytes; |
| 490 | pages_out++; |
| 491 | } |
| 492 |
| 493 | return 1; |
| 494 | } |
| 495 |
| 496 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_startRecord(JNIEnv *env, jclass class, jstring path) { |
| 497 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 498 |
| 499 | int32_t result = initRecorder(pathStr); |
| 500 |
| 501 | if (pathStr != 0) { |
| 502 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 503 | } |
| 504 |
| 505 | return result; |
| 506 | } |
| 507 |
| 508 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_writeFrame(JNIEnv *env, jclass class, jobject frame, jint len) { |
| 509 | jbyte *frameBytes = (*env)->GetDirectBufferAddress(env, frame); |
| 510 | return writeFrame((uint8_t *) frameBytes, (uint32_t) len); |
| 511 | } |
| 512 |
| 513 | JNIEXPORT void Java_org_telegram_messenger_MediaController_stopRecord(JNIEnv *env, jclass class) { |
| 514 | cleanupRecorder(); |
| 515 | } |
| 516 |
| 517 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_isOpusFile(JNIEnv *env, jclass class, jstring path) { |
| 518 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 519 |
| 520 | int32_t result = 0; |
| 521 |
| 522 | int32_t error = OPUS_OK; |
| 523 | OggOpusFile *file = op_test_file(pathStr, &error); |
| 524 | if (file != NULL) { |
| 525 | error = op_test_open(file); |
| 526 | op_free(file); |
| 527 | result = error == OPUS_OK; |
| 528 | } |
| 529 |
| 530 | if (pathStr != 0) { |
| 531 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 532 | } |
| 533 |
| 534 | return result; |
| 535 | } |
| 536 |
| 537 | static inline void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t value) { |
| 538 | bytes += bitOffset / 8; |
| 539 | bitOffset %= 8; |
| 540 | *((int32_t *) bytes) |= (value << bitOffset); |
| 541 | } |
| 542 |
| 543 | JNIEXPORT jbyteArray Java_org_telegram_messenger_MediaController_getWaveform2(JNIEnv *env, jclass class, jshortArray array, jint length) { |
| 544 |
| 545 | jshort *sampleBuffer = (*env)->GetShortArrayElements(env, array, 0); |
| 546 |
| 547 | const int32_t resultSamples = 100; |
| 548 | uint16_t *samples = malloc(100 * 2); |
| 549 | uint64_t sampleIndex = 0; |
| 550 | uint16_t peakSample = 0; |
| 551 | int32_t sampleRate = (int32_t) MAX(1, length / resultSamples); |
| 552 | int32_t index = 0; |
| 553 |
| 554 | for (int32_t i = 0; i < length; i++) { |
| 555 | uint16_t sample = (uint16_t) abs(sampleBuffer[i]); |
| 556 | if (sample > peakSample) { |
| 557 | peakSample = sample; |
| 558 | } |
| 559 | if (sampleIndex++ % sampleRate == 0) { |
| 560 | if (index < resultSamples) { |
| 561 | samples[index++] = peakSample; |
| 562 | } |
| 563 | peakSample = 0; |
| 564 | } |
| 565 | } |
| 566 |
| 567 | int64_t sumSamples = 0; |
| 568 | for (int32_t i = 0; i < resultSamples; i++) { |
| 569 | sumSamples += samples[i]; |
| 570 | } |
| 571 | uint16_t peak = (uint16_t) (sumSamples * 1.8f / resultSamples); |
| 572 | if (peak < 2500) { |
| 573 | peak = 2500; |
| 574 | } |
| 575 |
| 576 | for (int32_t i = 0; i < resultSamples; i++) { |
| 577 | uint16_t sample = (uint16_t) ((int64_t) samples[i]); |
| 578 | if (sample > peak) { |
| 579 | samples[i] = peak; |
| 580 | } |
| 581 | } |
| 582 |
| 583 | (*env)->ReleaseShortArrayElements(env, array, sampleBuffer, 0); |
| 584 |
| 585 | uint32_t bitstreamLength = resultSamples * 5 / 8 + 1; |
| 586 | jbyteArray *result = (*env)->NewByteArray(env, bitstreamLength); |
| 587 | if (result) { |
| 588 | uint8_t *bytes = malloc(bitstreamLength + 4); |
| 589 | memset(bytes, 0, bitstreamLength + 4); |
| 590 | for (int32_t i = 0; i < resultSamples; i++) { |
| 591 | int32_t value = MIN(31, abs((int32_t) samples[i]) * 31 / peak); |
| 592 | set_bits(bytes, i * 5, value & 31); |
| 593 | } |
| 594 | (*env)->SetByteArrayRegion(env, result, 0, bitstreamLength, (jbyte *) bytes); |
| 595 | } |
| 596 | free(samples); |
| 597 |
| 598 | return result; |
| 599 | } |
| 600 |
| 601 | int16_t *sampleBuffer = NULL; |
| 602 |
| 603 | JNIEXPORT jbyteArray Java_org_telegram_messenger_MediaController_getWaveform(JNIEnv *env, jclass class, jstring path) { |
| 604 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 605 | jbyteArray result = 0; |
| 606 |
| 607 | int error = OPUS_OK; |
| 608 | OggOpusFile *opusFile = op_open_file(pathStr, &error); |
| 609 | if (opusFile != NULL && error == OPUS_OK) { |
| 610 | int64_t totalSamples = op_pcm_total(opusFile, -1); |
| 611 | const uint32_t resultSamples = 100; |
| 612 | int32_t sampleRate = MAX(1, (int32_t) (totalSamples / resultSamples)); |
| 613 |
| 614 | uint16_t *samples = malloc(100 * 2); |
| 615 |
| 616 | size_t bufferSize = 1024 * 128; |
| 617 | if (sampleBuffer == NULL) { |
| 618 | sampleBuffer = malloc(bufferSize); |
| 619 | } |
| 620 | uint64_t sampleIndex = 0; |
| 621 | uint16_t peakSample = 0; |
| 622 |
| 623 | int32_t index = 0; |
| 624 |
| 625 | while (1) { |
| 626 | int readSamples = op_read(opusFile, sampleBuffer, bufferSize / 2, NULL); |
| 627 | for (int32_t i = 0; i < readSamples; i++) { |
| 628 | uint16_t sample = (uint16_t) abs(sampleBuffer[i]); |
| 629 | if (sample > peakSample) { |
| 630 | peakSample = sample; |
| 631 | } |
| 632 | if (sampleIndex++ % sampleRate == 0) { |
| 633 | if (index < resultSamples) { |
| 634 | samples[index++] = peakSample; |
| 635 | } |
| 636 | peakSample = 0; |
| 637 | } |
| 638 | } |
| 639 | if (readSamples == 0) { |
| 640 | break; |
| 641 | } |
| 642 | } |
| 643 |
| 644 | int64_t sumSamples = 0; |
| 645 | for (int32_t i = 0; i < resultSamples; i++) { |
| 646 | sumSamples += samples[i]; |
| 647 | } |
| 648 | uint16_t peak = (uint16_t) (sumSamples * 1.8f / resultSamples); |
| 649 | if (peak < 2500) { |
| 650 | peak = 2500; |
| 651 | } |
| 652 |
| 653 | for (int32_t i = 0; i < resultSamples; i++) { |
| 654 | uint16_t sample = (uint16_t) ((int64_t) samples[i]); |
| 655 | if (sample > peak) { |
| 656 | samples[i] = peak; |
| 657 | } |
| 658 | } |
| 659 |
| 660 | //free(sampleBuffer); |
| 661 | op_free(opusFile); |
| 662 |
| 663 | uint32_t bitstreamLength = (resultSamples * 5) / 8 + 1; |
| 664 | result = (*env)->NewByteArray(env, bitstreamLength); |
| 665 | if (result) { |
| 666 | uint8_t *bytes = malloc(bitstreamLength + 4); |
| 667 | memset(bytes, 0, bitstreamLength + 4); |
| 668 |
| 669 | for (int32_t i = 0; i < resultSamples; i++) { |
| 670 | int32_t value = MIN(31, abs((int32_t) samples[i]) * 31 / peak); |
| 671 | set_bits(bytes, i * 5, value & 31); |
| 672 | } |
| 673 |
| 674 | (*env)->SetByteArrayRegion(env, result, 0, bitstreamLength, (jbyte *) bytes); |
| 675 | } |
| 676 | free(samples); |
| 677 | } |
| 678 |
| 679 | if (pathStr != 0) { |
| 680 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 681 | } |
| 682 |
| 683 | return result; |
| 684 | } |
| 685 |