| 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 = OPUS_BITRATE_MAX; |
| 223 | const opus_int32 frame_size = 960; |
| 224 | const int with_cvbr = 1; |
| 225 | const int max_ogg_delay = 0; |
| 226 | const int comment_padding = 512; |
| 227 |
| 228 | opus_int32 rate = 48000; |
| 229 | opus_int32 coding_rate = 48000; |
| 230 |
| 231 | ogg_int32_t _packetId; |
| 232 | OpusEncoder *_encoder = 0; |
| 233 | uint8_t *_packet = 0; |
| 234 | ogg_stream_state os; |
| 235 | FILE *_fileOs = 0; |
| 236 | oe_enc_opt inopt; |
| 237 | OpusHeader header; |
| 238 | opus_int32 min_bytes; |
| 239 | int max_frame_bytes; |
| 240 | ogg_packet op; |
| 241 | ogg_page og; |
| 242 | opus_int64 bytes_written; |
| 243 | opus_int64 pages_out; |
| 244 | opus_int64 total_samples; |
| 245 | ogg_int64_t enc_granulepos; |
| 246 | ogg_int64_t last_granulepos; |
| 247 | int size_segments; |
| 248 | int last_segments; |
| 249 |
| 250 | void cleanupRecorder() { |
| 251 |
| 252 | ogg_stream_flush(&os, &og); |
| 253 |
| 254 | if (_encoder) { |
| 255 | opus_encoder_destroy(_encoder); |
| 256 | _encoder = 0; |
| 257 | } |
| 258 |
| 259 | ogg_stream_clear(&os); |
| 260 |
| 261 | if (_packet) { |
| 262 | free(_packet); |
| 263 | _packet = 0; |
| 264 | } |
| 265 |
| 266 | if (_fileOs) { |
| 267 | fclose(_fileOs); |
| 268 | _fileOs = 0; |
| 269 | } |
| 270 |
| 271 | _packetId = -1; |
| 272 | bytes_written = 0; |
| 273 | pages_out = 0; |
| 274 | total_samples = 0; |
| 275 | enc_granulepos = 0; |
| 276 | size_segments = 0; |
| 277 | last_segments = 0; |
| 278 | last_granulepos = 0; |
| 279 | memset(&os, 0, sizeof(ogg_stream_state)); |
| 280 | memset(&inopt, 0, sizeof(oe_enc_opt)); |
| 281 | memset(&header, 0, sizeof(OpusHeader)); |
| 282 | memset(&op, 0, sizeof(ogg_packet)); |
| 283 | memset(&og, 0, sizeof(ogg_page)); |
| 284 | } |
| 285 |
| 286 | int initRecorder(const char *path, opus_int32 sampleRate) { |
| 287 | cleanupRecorder(); |
| 288 |
| 289 | coding_rate = sampleRate; |
| 290 | rate = sampleRate; |
| 291 |
| 292 | if (!path) { |
| 293 | LOGE("path is null"); |
| 294 | return 0; |
| 295 | } |
| 296 |
| 297 | _fileOs = fopen(path, "w"); |
| 298 | if (!_fileOs) { |
| 299 | LOGE("error cannot open file: %s", path); |
| 300 | return 0; |
| 301 | } |
| 302 |
| 303 | inopt.rate = rate; |
| 304 | inopt.gain = 0; |
| 305 | inopt.endianness = 0; |
| 306 | inopt.copy_comments = 0; |
| 307 | inopt.rawmode = 0; |
| 308 | inopt.ignorelength = 0; |
| 309 | inopt.samplesize = 16; |
| 310 | inopt.channels = 1; |
| 311 | inopt.skip = 0; |
| 312 |
| 313 | comment_init(&inopt.comments, &inopt.comments_length, opus_get_version_string()); |
| 314 |
| 315 | if (rate != coding_rate) { |
| 316 | LOGE("Invalid rate"); |
| 317 | return 0; |
| 318 | } |
| 319 |
| 320 | header.channels = 1; |
| 321 | header.channel_mapping = 0; |
| 322 | header.input_sample_rate = rate; |
| 323 | header.gain = inopt.gain; |
| 324 | header.nb_streams = 1; |
| 325 |
| 326 | int result = OPUS_OK; |
| 327 | _encoder = opus_encoder_create(coding_rate, 1, OPUS_APPLICATION_VOIP, &result); |
| 328 | if (result != OPUS_OK) { |
| 329 | LOGE("Error cannot create encoder: %s", opus_strerror(result)); |
| 330 | return 0; |
| 331 | } |
| 332 |
| 333 | min_bytes = max_frame_bytes = (1275 * 3 + 7) * header.nb_streams; |
| 334 | _packet = malloc(max_frame_bytes); |
| 335 |
| 336 | result = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate)); |
| 337 | //result = opus_encoder_ctl(_encoder, OPUS_SET_COMPLEXITY(10)); |
| 338 | if (result != OPUS_OK) { |
| 339 | LOGE("Error OPUS_SET_BITRATE returned: %s", opus_strerror(result)); |
| 340 | return 0; |
| 341 | } |
| 342 |
| 343 | #ifdef OPUS_SET_LSB_DEPTH |
| 344 | result = opus_encoder_ctl(_encoder, OPUS_SET_LSB_DEPTH(MAX(8, MIN(24, inopt.samplesize)))); |
| 345 | if (result != OPUS_OK) { |
| 346 | LOGE("Warning OPUS_SET_LSB_DEPTH returned: %s", opus_strerror(result)); |
| 347 | } |
| 348 | #endif |
| 349 |
| 350 | opus_int32 lookahead; |
| 351 | result = opus_encoder_ctl(_encoder, OPUS_GET_LOOKAHEAD(&lookahead)); |
| 352 | if (result != OPUS_OK) { |
| 353 | LOGE("Error OPUS_GET_LOOKAHEAD returned: %s", opus_strerror(result)); |
| 354 | return 0; |
| 355 | } |
| 356 |
| 357 | inopt.skip += lookahead; |
| 358 | header.preskip = (int)(inopt.skip * (48000.0 / coding_rate)); |
| 359 | inopt.extraout = (int)(header.preskip * (rate / 48000.0)); |
| 360 |
| 361 | if (ogg_stream_init(&os, rand()) == -1) { |
| 362 | LOGE("Error: stream init failed"); |
| 363 | return 0; |
| 364 | } |
| 365 |
| 366 | unsigned char header_data[100]; |
| 367 | int packet_size = opus_header_to_packet(&header, header_data, 100); |
| 368 | op.packet = header_data; |
| 369 | op.bytes = packet_size; |
| 370 | op.b_o_s = 1; |
| 371 | op.e_o_s = 0; |
| 372 | op.granulepos = 0; |
| 373 | op.packetno = 0; |
| 374 | ogg_stream_packetin(&os, &op); |
| 375 |
| 376 | while ((result = ogg_stream_flush(&os, &og))) { |
| 377 | if (!result) { |
| 378 | break; |
| 379 | } |
| 380 |
| 381 | int pageBytesWritten = writeOggPage(&og, _fileOs); |
| 382 | if (pageBytesWritten != og.header_len + og.body_len) { |
| 383 | LOGE("Error: failed writing header to output stream"); |
| 384 | return 0; |
| 385 | } |
| 386 | bytes_written += pageBytesWritten; |
| 387 | pages_out++; |
| 388 | } |
| 389 |
| 390 | comment_pad(&inopt.comments, &inopt.comments_length, comment_padding); |
| 391 | op.packet = (unsigned char *)inopt.comments; |
| 392 | op.bytes = inopt.comments_length; |
| 393 | op.b_o_s = 0; |
| 394 | op.e_o_s = 0; |
| 395 | op.granulepos = 0; |
| 396 | op.packetno = 1; |
| 397 | ogg_stream_packetin(&os, &op); |
| 398 |
| 399 | while ((result = ogg_stream_flush(&os, &og))) { |
| 400 | if (result == 0) { |
| 401 | break; |
| 402 | } |
| 403 |
| 404 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 405 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 406 | LOGE("Error: failed writing header to output stream"); |
| 407 | return 0; |
| 408 | } |
| 409 |
| 410 | bytes_written += writtenPageBytes; |
| 411 | pages_out++; |
| 412 | } |
| 413 |
| 414 | free(inopt.comments); |
| 415 |
| 416 | return 1; |
| 417 | } |
| 418 | int writeFrame(uint8_t *framePcmBytes, uint32_t frameByteCount) { |
| 419 | size_t cur_frame_size = frame_size; |
| 420 | _packetId++; |
| 421 |
| 422 | opus_int32 nb_samples = frameByteCount / 2; |
| 423 | total_samples += nb_samples; |
| 424 | if (nb_samples < frame_size) { |
| 425 | op.e_o_s = 1; |
| 426 | } else { |
| 427 | op.e_o_s = 0; |
| 428 | } |
| 429 |
| 430 | int nbBytes = 0; |
| 431 |
| 432 | if (nb_samples != 0) { |
| 433 | uint8_t *paddedFrameBytes = framePcmBytes; |
| 434 | int freePaddedFrameBytes = 0; |
| 435 |
| 436 | if (nb_samples < cur_frame_size) { |
| 437 | paddedFrameBytes = malloc(cur_frame_size * 2); |
| 438 | freePaddedFrameBytes = 1; |
| 439 | memcpy(paddedFrameBytes, framePcmBytes, frameByteCount); |
| 440 | memset(paddedFrameBytes + nb_samples * 2, 0, cur_frame_size * 2 - nb_samples * 2); |
| 441 | } |
| 442 |
| 443 | nbBytes = opus_encode(_encoder, (opus_int16 *)paddedFrameBytes, cur_frame_size, _packet, max_frame_bytes / 10); |
| 444 | if (freePaddedFrameBytes) { |
| 445 | free(paddedFrameBytes); |
| 446 | } |
| 447 |
| 448 | if (nbBytes < 0) { |
| 449 | LOGE("Encoding failed: %s. Aborting.", opus_strerror(nbBytes)); |
| 450 | return 0; |
| 451 | } |
| 452 |
| 453 | enc_granulepos += cur_frame_size * 48000 / coding_rate; |
| 454 | size_segments = (nbBytes + 255) / 255; |
| 455 | min_bytes = MIN(nbBytes, min_bytes); |
| 456 | } |
| 457 |
| 458 | while ((((size_segments <= 255) && (last_segments + size_segments > 255)) || (enc_granulepos - last_granulepos > max_ogg_delay)) && ogg_stream_flush_fill(&os, &og, 255 * 255)) { |
| 459 | if (ogg_page_packets(&og) != 0) { |
| 460 | last_granulepos = ogg_page_granulepos(&og); |
| 461 | } |
| 462 |
| 463 | last_segments -= og.header[26]; |
| 464 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 465 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 466 | LOGE("Error: failed writing data to output stream"); |
| 467 | return 0; |
| 468 | } |
| 469 | bytes_written += writtenPageBytes; |
| 470 | pages_out++; |
| 471 | } |
| 472 |
| 473 | op.packet = _packet; |
| 474 | op.bytes = nbBytes; |
| 475 | op.b_o_s = 0; |
| 476 | op.granulepos = enc_granulepos; |
| 477 | if (op.e_o_s) { |
| 478 | op.granulepos = ((total_samples * 48000 + rate - 1) / rate) + header.preskip; |
| 479 | } |
| 480 | op.packetno = 2 + _packetId; |
| 481 | ogg_stream_packetin(&os, &op); |
| 482 | last_segments += size_segments; |
| 483 |
| 484 | 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)) { |
| 485 | if (ogg_page_packets(&og) != 0) { |
| 486 | last_granulepos = ogg_page_granulepos(&og); |
| 487 | } |
| 488 | last_segments -= og.header[26]; |
| 489 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 490 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 491 | LOGE("Error: failed writing data to output stream"); |
| 492 | return 0; |
| 493 | } |
| 494 | bytes_written += writtenPageBytes; |
| 495 | pages_out++; |
| 496 | } |
| 497 |
| 498 | return 1; |
| 499 | } |
| 500 |
| 501 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_startRecord(JNIEnv *env, jclass class, jstring path, jint sampleRate) { |
| 502 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 503 |
| 504 | int32_t result = initRecorder(pathStr, sampleRate); |
| 505 |
| 506 | if (pathStr != 0) { |
| 507 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 508 | } |
| 509 |
| 510 | return result; |
| 511 | } |
| 512 |
| 513 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_writeFrame(JNIEnv *env, jclass class, jobject frame, jint len) { |
| 514 | jbyte *frameBytes = (*env)->GetDirectBufferAddress(env, frame); |
| 515 | return writeFrame((uint8_t *) frameBytes, (uint32_t) len); |
| 516 | } |
| 517 |
| 518 | JNIEXPORT void Java_org_telegram_messenger_MediaController_stopRecord(JNIEnv *env, jclass class) { |
| 519 | cleanupRecorder(); |
| 520 | } |
| 521 |
| 522 | JNIEXPORT jint Java_org_telegram_messenger_MediaController_isOpusFile(JNIEnv *env, jclass class, jstring path) { |
| 523 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 524 |
| 525 | int32_t result = 0; |
| 526 |
| 527 | int32_t error = OPUS_OK; |
| 528 | OggOpusFile *file = op_test_file(pathStr, &error); |
| 529 | if (file != NULL) { |
| 530 | error = op_test_open(file); |
| 531 | op_free(file); |
| 532 | result = error == OPUS_OK; |
| 533 | } |
| 534 |
| 535 | if (pathStr != 0) { |
| 536 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 537 | } |
| 538 |
| 539 | return result; |
| 540 | } |
| 541 |
| 542 | static inline void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t value) { |
| 543 | bytes += bitOffset / 8; |
| 544 | bitOffset %= 8; |
| 545 | *((int32_t *) bytes) |= (value << bitOffset); |
| 546 | } |
| 547 |
| 548 | JNIEXPORT jbyteArray Java_org_telegram_messenger_MediaController_getWaveform2(JNIEnv *env, jclass class, jshortArray array, jint length) { |
| 549 |
| 550 | jshort *sampleBuffer = (*env)->GetShortArrayElements(env, array, 0); |
| 551 |
| 552 | const int32_t resultSamples = 100; |
| 553 | uint16_t *samples = malloc(100 * 2); |
| 554 | uint64_t sampleIndex = 0; |
| 555 | uint16_t peakSample = 0; |
| 556 | int32_t sampleRate = (int32_t) MAX(1, length / resultSamples); |
| 557 | int32_t index = 0; |
| 558 |
| 559 | for (int32_t i = 0; i < length; i++) { |
| 560 | uint16_t sample = (uint16_t) abs(sampleBuffer[i]); |
| 561 | if (sample > peakSample) { |
| 562 | peakSample = sample; |
| 563 | } |
| 564 | if (sampleIndex++ % sampleRate == 0) { |
| 565 | if (index < resultSamples) { |
| 566 | samples[index++] = peakSample; |
| 567 | } |
| 568 | peakSample = 0; |
| 569 | } |
| 570 | } |
| 571 |
| 572 | int64_t sumSamples = 0; |
| 573 | for (int32_t i = 0; i < resultSamples; i++) { |
| 574 | sumSamples += samples[i]; |
| 575 | } |
| 576 | uint16_t peak = (uint16_t) (sumSamples * 1.8f / resultSamples); |
| 577 | if (peak < 2500) { |
| 578 | peak = 2500; |
| 579 | } |
| 580 |
| 581 | for (int32_t i = 0; i < resultSamples; i++) { |
| 582 | uint16_t sample = (uint16_t) ((int64_t) samples[i]); |
| 583 | if (sample > peak) { |
| 584 | samples[i] = peak; |
| 585 | } |
| 586 | } |
| 587 |
| 588 | (*env)->ReleaseShortArrayElements(env, array, sampleBuffer, 0); |
| 589 |
| 590 | uint32_t bitstreamLength = resultSamples * 5 / 8 + 1; |
| 591 | jbyteArray *result = (*env)->NewByteArray(env, bitstreamLength); |
| 592 | if (result) { |
| 593 | uint8_t *bytes = malloc(bitstreamLength + 4); |
| 594 | memset(bytes, 0, bitstreamLength + 4); |
| 595 | for (int32_t i = 0; i < resultSamples; i++) { |
| 596 | int32_t value = MIN(31, abs((int32_t) samples[i]) * 31 / peak); |
| 597 | set_bits(bytes, i * 5, value & 31); |
| 598 | } |
| 599 | (*env)->SetByteArrayRegion(env, result, 0, bitstreamLength, (jbyte *) bytes); |
| 600 | } |
| 601 | free(samples); |
| 602 |
| 603 | return result; |
| 604 | } |
| 605 |
| 606 | int16_t *sampleBuffer = NULL; |
| 607 |
| 608 | JNIEXPORT jbyteArray Java_org_telegram_messenger_MediaController_getWaveform(JNIEnv *env, jclass class, jstring path) { |
| 609 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 610 | jbyteArray result = 0; |
| 611 |
| 612 | int error = OPUS_OK; |
| 613 | OggOpusFile *opusFile = op_open_file(pathStr, &error); |
| 614 | if (opusFile != NULL && error == OPUS_OK) { |
| 615 | int64_t totalSamples = op_pcm_total(opusFile, -1); |
| 616 | const uint32_t resultSamples = 100; |
| 617 | int32_t sampleRate = MAX(1, (int32_t) (totalSamples / resultSamples)); |
| 618 |
| 619 | uint16_t *samples = malloc(100 * 2); |
| 620 |
| 621 | size_t bufferSize = 1024 * 128; |
| 622 | if (sampleBuffer == NULL) { |
| 623 | sampleBuffer = malloc(bufferSize); |
| 624 | } |
| 625 | uint64_t sampleIndex = 0; |
| 626 | uint16_t peakSample = 0; |
| 627 |
| 628 | int32_t index = 0; |
| 629 |
| 630 | while (1) { |
| 631 | int readSamples = op_read(opusFile, sampleBuffer, bufferSize / 2, NULL); |
| 632 | for (int32_t i = 0; i < readSamples; i++) { |
| 633 | uint16_t sample = (uint16_t) abs(sampleBuffer[i]); |
| 634 | if (sample > peakSample) { |
| 635 | peakSample = sample; |
| 636 | } |
| 637 | if (sampleIndex++ % sampleRate == 0) { |
| 638 | if (index < resultSamples) { |
| 639 | samples[index++] = peakSample; |
| 640 | } |
| 641 | peakSample = 0; |
| 642 | } |
| 643 | } |
| 644 | if (readSamples == 0) { |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 |
| 649 | int64_t sumSamples = 0; |
| 650 | for (int32_t i = 0; i < resultSamples; i++) { |
| 651 | sumSamples += samples[i]; |
| 652 | } |
| 653 | uint16_t peak = (uint16_t) (sumSamples * 1.8f / resultSamples); |
| 654 | if (peak < 2500) { |
| 655 | peak = 2500; |
| 656 | } |
| 657 |
| 658 | for (int32_t i = 0; i < resultSamples; i++) { |
| 659 | uint16_t sample = (uint16_t) ((int64_t) samples[i]); |
| 660 | if (sample > peak) { |
| 661 | samples[i] = peak; |
| 662 | } |
| 663 | } |
| 664 |
| 665 | //free(sampleBuffer); |
| 666 | op_free(opusFile); |
| 667 |
| 668 | uint32_t bitstreamLength = (resultSamples * 5) / 8 + 1; |
| 669 | result = (*env)->NewByteArray(env, bitstreamLength); |
| 670 | if (result) { |
| 671 | uint8_t *bytes = malloc(bitstreamLength + 4); |
| 672 | memset(bytes, 0, bitstreamLength + 4); |
| 673 |
| 674 | for (int32_t i = 0; i < resultSamples; i++) { |
| 675 | int32_t value = MIN(31, abs((int32_t) samples[i]) * 31 / peak); |
| 676 | set_bits(bytes, i * 5, value & 31); |
| 677 | } |
| 678 |
| 679 | (*env)->SetByteArrayRegion(env, result, 0, bitstreamLength, (jbyte *) bytes); |
| 680 | } |
| 681 | free(samples); |
| 682 | } |
| 683 |
| 684 | if (pathStr != 0) { |
| 685 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 686 | } |
| 687 |
| 688 | return result; |
| 689 | } |
| 690 |