| 5 |
| #include <string> |
| 6 | #include <unistd.h> |
| 7 | #include <linux/stat.h> |
| 8 | #include <asm/fcntl.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <libyuv.h> |
| 11 | #include <tgnet/FileLog.h> |
| 12 | #include "tgnet/ConnectionsManager.h" |
| 13 | #include "voip/webrtc/common_video/h264/sps_parser.h" |
| 14 | #include "voip/webrtc/common_video/h264/h264_common.h" |
| 15 | #include "c_utils.h" |
| 16 |
| 17 | extern "C" { |
| 18 | #include <libavformat/avformat.h> |
| 19 | #include <libavformat/isom.h> |
| 20 | #include <libavcodec/bytestream.h> |
| 21 | #include <libavcodec/get_bits.h> |
| 22 | #include <libavcodec/golomb.h> |
| 23 | #include <libavutil/eval.h> |
| 24 | #include <libavutil/intmath.h> |
| 25 | #include <libswscale/swscale.h> |
| 26 | } |
| 27 |
| 28 | static const std::string av_make_error_str(int errnum) { |
| 29 | char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 30 | av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); |
| 31 | return (std::string) errbuf; |
| 32 | } |
| 33 |
| 34 | #undef av_err2str |
| 35 | #define av_err2str(errnum) av_make_error_str(errnum).c_str() |
| 36 | #define FFMPEG_AVSEEK_SIZE 0x10000 |
| 37 |
| 38 | jclass jclass_AnimatedFileDrawableStream; |
| 39 | jmethodID jclass_AnimatedFileDrawableStream_read; |
| 40 | jmethodID jclass_AnimatedFileDrawableStream_cancel; |
| 41 | jmethodID jclass_AnimatedFileDrawableStream_isFinishedLoadingFile; |
| 42 | jmethodID jclass_AnimatedFileDrawableStream_getFinishedFilePath; |
| 43 |
| 44 | typedef struct H2645NAL { |
| 45 | uint8_t *rbsp_buffer; |
| 46 | int size; |
| 47 | const uint8_t *data; |
| 48 | int size_bits; |
| 49 | int raw_size; |
| 50 | const uint8_t *raw_data; |
| 51 | int type; |
| 52 | int temporal_id; |
| 53 | int nuh_layer_id; |
| 54 | int skipped_bytes; |
| 55 | int skipped_bytes_pos_size; |
| 56 | int *skipped_bytes_pos; |
| 57 | int ref_idc; |
| 58 | GetBitContext gb; |
| 59 | } H2645NAL; |
| 60 |
| 61 | typedef struct H2645RBSP { |
| 62 | uint8_t *rbsp_buffer; |
| 63 | AVBufferRef *rbsp_buffer_ref; |
| 64 | int rbsp_buffer_alloc_size; |
| 65 | int rbsp_buffer_size; |
| 66 | } H2645RBSP; |
| 67 |
| 68 | typedef struct H2645Packet { |
| 69 | H2645NAL *nals; |
| 70 | H2645RBSP rbsp; |
| 71 | int nb_nals; |
| 72 | int nals_allocated; |
| 73 | unsigned nal_buffer_size; |
| 74 | } H2645Packet; |
| 75 |
| 76 | void ff_h2645_packet_uninit(H2645Packet *pkt) { |
| 77 | int i; |
| 78 | for (i = 0; i < pkt->nals_allocated; i++) { |
| 79 | av_freep(&pkt->nals[i].skipped_bytes_pos); |
| 80 | } |
| 81 | av_freep(&pkt->nals); |
| 82 | pkt->nals_allocated = pkt->nal_buffer_size = 0; |
| 83 | if (pkt->rbsp.rbsp_buffer_ref) { |
| 84 | av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref); |
| 85 | pkt->rbsp.rbsp_buffer = NULL; |
| 86 | } else |
| 87 | av_freep(&pkt->rbsp.rbsp_buffer); |
| 88 | pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0; |
| 89 | } |
| 90 |
| 91 | typedef struct VideoInfo { |
| 92 |
| 93 | ~VideoInfo() { |
| 94 | if (video_dec_ctx) { |
| 95 | avcodec_close(video_dec_ctx); |
| 96 | video_dec_ctx = nullptr; |
| 97 | } |
| 98 | if (fmt_ctx) { |
| 99 | avformat_close_input(&fmt_ctx); |
| 100 | fmt_ctx = nullptr; |
| 101 | } |
| 102 | if (frame) { |
| 103 | av_frame_free(&frame); |
| 104 | frame = nullptr; |
| 105 | } |
| 106 | if (src) { |
| 107 | delete [] src; |
| 108 | src = nullptr; |
| 109 | } |
| 110 | if (stream != nullptr) { |
| 111 | JNIEnv *jniEnv = nullptr; |
| 112 | JavaVMAttachArgs jvmArgs; |
| 113 | jvmArgs.version = JNI_VERSION_1_6; |
| 114 |
| 115 | bool attached; |
| 116 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 117 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 118 | attached = true; |
| 119 | } else { |
| 120 | attached = false; |
| 121 | } |
| 122 | jniEnv->DeleteGlobalRef(stream); |
| 123 | if (attached) { |
| 124 | javaVm->DetachCurrentThread(); |
| 125 | } |
| 126 | stream = nullptr; |
| 127 | } |
| 128 | if (ioContext != nullptr) { |
| 129 | if (ioContext->buffer) { |
| 130 | av_freep(&ioContext->buffer); |
| 131 | } |
| 132 | avio_context_free(&ioContext); |
| 133 | ioContext = nullptr; |
| 134 | } |
| 135 | if (sws_ctx != nullptr) { |
| 136 | sws_freeContext(sws_ctx); |
| 137 | sws_ctx = nullptr; |
| 138 | } |
| 139 | if (fd >= 0) { |
| 140 | close(fd); |
| 141 | fd = -1; |
| 142 | } |
| 143 |
| 144 | ff_h2645_packet_uninit(&h2645Packet); |
| 145 | av_packet_unref(&orig_pkt); |
| 146 |
| 147 | video_stream_idx = -1; |
| 148 | video_stream = nullptr; |
| 149 | audio_stream = nullptr; |
| 150 | } |
| 151 |
| 152 | AVFormatContext *fmt_ctx = nullptr; |
| 153 | char *src = nullptr; |
| 154 | int video_stream_idx = -1; |
| 155 | AVStream *video_stream = nullptr; |
| 156 | AVStream *audio_stream = nullptr; |
| 157 | AVCodecContext *video_dec_ctx = nullptr; |
| 158 | AVFrame *frame = nullptr; |
| 159 | bool has_decoded_frames = false; |
| 160 | AVPacket pkt; |
| 161 | AVPacket orig_pkt; |
| 162 | bool stopped = false; |
| 163 | bool seeking = false; |
| 164 |
| 165 | int firstWidth = 0; |
| 166 | int firstHeight = 0; |
| 167 |
| 168 | bool dropFrames = false; |
| 169 |
| 170 | H2645Packet h2645Packet = {nullptr}; |
| 171 |
| 172 | int32_t dst_linesize[1]; |
| 173 |
| 174 | struct SwsContext *sws_ctx = nullptr; |
| 175 |
| 176 | AVIOContext *ioContext = nullptr; |
| 177 | unsigned char *ioBuffer = nullptr; |
| 178 | jobject stream = nullptr; |
| 179 | int32_t account = 0; |
| 180 | int fd = -1; |
| 181 | int64_t file_size = 0; |
| 182 | int64_t last_seek_p = 0; |
| 183 | }; |
| 184 |
| 185 | void custom_log(void *ptr, int level, const char* fmt, va_list vl){ |
| 186 | va_list vl2; |
| 187 | char line[1024]; |
| 188 | static int print_prefix = 1; |
| 189 |
| 190 | va_copy(vl2, vl); |
| 191 | av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix); |
| 192 | va_end(vl2); |
| 193 |
| 194 | LOGE(line); |
| 195 | } |
| 196 |
| 197 | int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 198 | int ret, stream_index; |
| 199 | AVStream *st; |
| 200 | AVCodec *dec = NULL; |
| 201 | AVDictionary *opts = NULL; |
| 202 |
| 203 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 204 | if (ret < 0) { |
| 205 | LOGE("can't find %s stream in input file", av_get_media_type_string(type)); |
| 206 | return ret; |
| 207 | } else { |
| 208 | stream_index = ret; |
| 209 | st = fmt_ctx->streams[stream_index]; |
| 210 |
| 211 | dec = avcodec_find_decoder(st->codecpar->codec_id); |
| 212 | if (!dec) { |
| 213 | LOGE("failed to find %s codec", av_get_media_type_string(type)); |
| 214 | return AVERROR(EINVAL); |
| 215 | } |
| 216 |
| 217 | *dec_ctx = avcodec_alloc_context3(dec); |
| 218 | if (!*dec_ctx) { |
| 219 | LOGE("Failed to allocate the %s codec context", av_get_media_type_string(type)); |
| 220 | return AVERROR(ENOMEM); |
| 221 | } |
| 222 |
| 223 | if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { |
| 224 | LOGE("Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); |
| 225 | return ret; |
| 226 | } |
| 227 |
| 228 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 229 | if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { |
| 230 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 231 | return ret; |
| 232 | } |
| 233 | *stream_idx = stream_index; |
| 234 | } |
| 235 |
| 236 | return 0; |
| 237 | } |
| 238 |
| 239 | #define MAX_MBPAIR_SIZE (256*1024) |
| 240 |
| 241 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp, H2645NAL *nal) |
| 242 | { |
| 243 | int i, si, di; |
| 244 | uint8_t *dst; |
| 245 |
| 246 | nal->skipped_bytes = 0; |
| 247 | #define STARTCODE_TEST \ |
| 248 | if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \ |
| 249 | if (src[i + 2] != 3 && src[i + 2] != 0) { \ |
| 250 | /* startcode, so we must be past the end */ \ |
| 251 | length = i; \ |
| 252 | } \ |
| 253 | break; \ |
| 254 | } |
| 255 |
| 256 | for (i = 0; i + 1 < length; i += 2) { |
| 257 | if (src[i]) |
| 258 | continue; |
| 259 | if (i > 0 && src[i - 1] == 0) |
| 260 | i--; |
| 261 | STARTCODE_TEST; |
| 262 | } |
| 263 |
| 264 | if (i > length) |
| 265 | i = length; |
| 266 |
| 267 | nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size]; |
| 268 | dst = nal->rbsp_buffer; |
| 269 |
| 270 | memcpy(dst, src, i); |
| 271 | si = di = i; |
| 272 | while (si + 2 < length) { |
| 273 | if (src[si + 2] > 3) { |
| 274 | dst[di++] = src[si++]; |
| 275 | dst[di++] = src[si++]; |
| 276 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
| 277 | if (src[si + 2] == 3) { |
| 278 | dst[di++] = 0; |
| 279 | dst[di++] = 0; |
| 280 | si += 3; |
| 281 |
| 282 | if (nal->skipped_bytes_pos) { |
| 283 | nal->skipped_bytes++; |
| 284 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
| 285 | nal->skipped_bytes_pos_size *= 2; |
| 286 | av_reallocp_array(&nal->skipped_bytes_pos, |
| 287 | nal->skipped_bytes_pos_size, |
| 288 | sizeof(*nal->skipped_bytes_pos)); |
| 289 | if (!nal->skipped_bytes_pos) { |
| 290 | nal->skipped_bytes_pos_size = 0; |
| 291 | return AVERROR(ENOMEM); |
| 292 | } |
| 293 | } |
| 294 | if (nal->skipped_bytes_pos) |
| 295 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; |
| 296 | } |
| 297 | continue; |
| 298 | } else // next start code |
| 299 | goto nsc; |
| 300 | } |
| 301 |
| 302 | dst[di++] = src[si++]; |
| 303 | } |
| 304 | while (si < length) |
| 305 | dst[di++] = src[si++]; |
| 306 |
| 307 | nsc: |
| 308 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 309 |
| 310 | nal->data = dst; |
| 311 | nal->size = di; |
| 312 | nal->raw_data = src; |
| 313 | nal->raw_size = si; |
| 314 | rbsp->rbsp_buffer_size += si; |
| 315 |
| 316 | return si; |
| 317 | } |
| 318 |
| 319 | static inline int get_nalsize(int nal_length_size, const uint8_t *buf, int buf_size, int *buf_index) { |
| 320 | int i, nalsize = 0; |
| 321 | if (*buf_index >= buf_size - nal_length_size) { |
| 322 | return AVERROR(EAGAIN); |
| 323 | } |
| 324 | for (i = 0; i < nal_length_size; i++) |
| 325 | nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++]; |
| 326 | if (nalsize <= 0 || nalsize > buf_size - *buf_index) { |
| 327 | return AVERROR_INVALIDDATA; |
| 328 | } |
| 329 | return nalsize; |
| 330 | } |
| 331 |
| 332 | static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc) { |
| 333 | int i = 0; |
| 334 | if (buf + 3 >= next_avc) |
| 335 | return next_avc - buf; |
| 336 | while (buf + i + 3 < next_avc) { |
| 337 | if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) |
| 338 | break; |
| 339 | i++; |
| 340 | } |
| 341 | return i + 3; |
| 342 | } |
| 343 |
| 344 | static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros) { |
| 345 | int size = nal->size; |
| 346 | int v; |
| 347 |
| 348 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
| 349 | size--; |
| 350 |
| 351 | if (!size) |
| 352 | return 0; |
| 353 |
| 354 | v = nal->data[size - 1]; |
| 355 |
| 356 | if (size > INT_MAX / 8) |
| 357 | return AVERROR(ERANGE); |
| 358 | size *= 8; |
| 359 |
| 360 | /* remove the stop bit and following trailing zeros, |
| 361 | * or nothing for damaged bitstreams */ |
| 362 | if (v) |
| 363 | size -= ff_ctz(v) + 1; |
| 364 |
| 365 | return size; |
| 366 | } |
| 367 |
| 368 | static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size) { |
| 369 | int min_size = size; |
| 370 |
| 371 | if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
| 372 | goto fail; |
| 373 | size += AV_INPUT_BUFFER_PADDING_SIZE; |
| 374 |
| 375 | if (rbsp->rbsp_buffer_alloc_size >= size && |
| 376 | (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) { |
| 377 | memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 378 | return; |
| 379 | } |
| 380 |
| 381 | size = FFMIN(size + size / 16 + 32, INT_MAX); |
| 382 |
| 383 | if (rbsp->rbsp_buffer_ref) |
| 384 | av_buffer_unref(&rbsp->rbsp_buffer_ref); |
| 385 | else |
| 386 | av_free(rbsp->rbsp_buffer); |
| 387 |
| 388 | rbsp->rbsp_buffer = (uint8_t *) av_mallocz(size); |
| 389 | if (!rbsp->rbsp_buffer) |
| 390 | goto fail; |
| 391 | rbsp->rbsp_buffer_alloc_size = size; |
| 392 |
| 393 | return; |
| 394 |
| 395 | fail: |
| 396 | rbsp->rbsp_buffer_alloc_size = 0; |
| 397 | if (rbsp->rbsp_buffer_ref) { |
| 398 | av_buffer_unref(&rbsp->rbsp_buffer_ref); |
| 399 | rbsp->rbsp_buffer = NULL; |
| 400 | } else |
| 401 | av_freep(&rbsp->rbsp_buffer); |
| 402 |
| 403 | return; |
| 404 | } |
| 405 |
| 406 | static int h264_parse_nal_header(H2645NAL *nal) { |
| 407 | GetBitContext *gb = &nal->gb; |
| 408 |
| 409 | if (get_bits1(gb) != 0) |
| 410 | return AVERROR_INVALIDDATA; |
| 411 |
| 412 | nal->ref_idc = get_bits(gb, 2); |
| 413 | nal->type = get_bits(gb, 5); |
| 414 |
| 415 | return 1; |
| 416 | } |
| 417 |
| 418 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, int is_nalff, int nal_length_size) { |
| 419 | GetByteContext bc; |
| 420 | int consumed, ret = 0; |
| 421 | int next_avc = is_nalff ? 0 : length; |
| 422 | int64_t padding = MAX_MBPAIR_SIZE; |
| 423 |
| 424 | bytestream2_init(&bc, buf, length); |
| 425 | alloc_rbsp_buffer(&pkt->rbsp, length + padding); |
| 426 |
| 427 | if (!pkt->rbsp.rbsp_buffer) |
| 428 | return AVERROR(ENOMEM); |
| 429 |
| 430 | pkt->rbsp.rbsp_buffer_size = 0; |
| 431 | pkt->nb_nals = 0; |
| 432 | while (bytestream2_get_bytes_left(&bc) >= 4) { |
| 433 | H2645NAL *nal; |
| 434 | int extract_length = 0; |
| 435 | int skip_trailing_zeros = 1; |
| 436 |
| 437 | if (bytestream2_tell(&bc) == next_avc) { |
| 438 | int i = 0; |
| 439 | extract_length = get_nalsize(nal_length_size, bc.buffer, bytestream2_get_bytes_left(&bc), &i); |
| 440 | if (extract_length < 0) |
| 441 | return extract_length; |
| 442 |
| 443 | bytestream2_skip(&bc, nal_length_size); |
| 444 |
| 445 | next_avc = bytestream2_tell(&bc) + extract_length; |
| 446 | } else { |
| 447 | int buf_index; |
| 448 | buf_index = find_next_start_code(bc.buffer, buf + next_avc); |
| 449 | bytestream2_skip(&bc, buf_index); |
| 450 | if (!bytestream2_get_bytes_left(&bc)) { |
| 451 | if (pkt->nb_nals > 0) { |
| 452 | return 0; |
| 453 | } else { |
| 454 | return AVERROR_INVALIDDATA; |
| 455 | } |
| 456 | } |
| 457 | extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc)); |
| 458 | if (bytestream2_tell(&bc) >= next_avc) { |
| 459 | bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc)); |
| 460 | continue; |
| 461 | } |
| 462 | } |
| 463 |
| 464 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
| 465 | int new_size = pkt->nals_allocated + 1; |
| 466 | void *tmp; |
| 467 |
| 468 | if (new_size >= INT_MAX / sizeof(*pkt->nals)) |
| 469 | return AVERROR(ENOMEM); |
| 470 |
| 471 | tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals)); |
| 472 | if (!tmp) |
| 473 | return AVERROR(ENOMEM); |
| 474 |
| 475 | pkt->nals = (H2645NAL *) tmp; |
| 476 | memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals)); |
| 477 |
| 478 | nal = &pkt->nals[pkt->nb_nals]; |
| 479 | nal->skipped_bytes_pos_size = 1024; |
| 480 | nal->skipped_bytes_pos = (int *) av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); |
| 481 | if (!nal->skipped_bytes_pos) |
| 482 | return AVERROR(ENOMEM); |
| 483 |
| 484 | pkt->nals_allocated = new_size; |
| 485 | } |
| 486 | nal = &pkt->nals[pkt->nb_nals]; |
| 487 |
| 488 | consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal); |
| 489 | if (consumed < 0) |
| 490 | return consumed; |
| 491 |
| 492 | pkt->nb_nals++; |
| 493 |
| 494 | bytestream2_skip(&bc, consumed); |
| 495 |
| 496 | /* see commit 3566042a0 */ |
| 497 | if (bytestream2_get_bytes_left(&bc) >= 4 && |
| 498 | bytestream2_peek_be32(&bc) == 0x000001E0) |
| 499 | skip_trailing_zeros = 0; |
| 500 |
| 501 | nal->size_bits = get_bit_length(nal, skip_trailing_zeros); |
| 502 |
| 503 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); |
| 504 | if (ret < 0) |
| 505 | return ret; |
| 506 |
| 507 | ret = h264_parse_nal_header(nal); |
| 508 | if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) { |
| 509 | pkt->nb_nals--; |
| 510 | } |
| 511 | } |
| 512 |
| 513 | return 0; |
| 514 | } |
| 515 |
| 516 | #define MAX_SPS_COUNT 32 |
| 517 |
| 518 | const uint8_t ff_zigzag_direct[64] = { |
| 519 | 0, 1, 8, 16, 9, 2, 3, 10, |
| 520 | 17, 24, 32, 25, 18, 11, 4, 5, |
| 521 | 12, 19, 26, 33, 40, 48, 41, 34, |
| 522 | 27, 20, 13, 6, 7, 14, 21, 28, |
| 523 | 35, 42, 49, 56, 57, 50, 43, 36, |
| 524 | 29, 22, 15, 23, 30, 37, 44, 51, |
| 525 | 58, 59, 52, 45, 38, 31, 39, 46, |
| 526 | 53, 60, 61, 54, 47, 55, 62, 63 |
| 527 | }; |
| 528 |
| 529 | const uint8_t ff_zigzag_scan[16+1] = { |
| 530 | 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, |
| 531 | 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4, |
| 532 | 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4, |
| 533 | 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4, |
| 534 | }; |
| 535 |
| 536 | static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size) { |
| 537 | int i, last = 8, next = 8; |
| 538 | const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct; |
| 539 | if (!get_bits1(gb)) { |
| 540 |
| 541 | } else { |
| 542 | for (i = 0; i < size; i++) { |
| 543 | if (next) { |
| 544 | int v = get_se_golomb(gb); |
| 545 | if (v < -128 || v > 127) { |
| 546 | return AVERROR_INVALIDDATA; |
| 547 | } |
| 548 | next = (last + v) & 0xff; |
| 549 | } |
| 550 | if (!i && !next) { /* matrix not written, we use the preset one */ |
| 551 | break; |
| 552 | } |
| 553 | last = factors[scan[i]] = next ? next : last; |
| 554 | } |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 |
| 559 | static int decode_scaling_matrices(GetBitContext *gb, int chroma_format_idc, uint8_t(*scaling_matrix4)[16], uint8_t(*scaling_matrix8)[64]) { |
| 560 | int ret = 0; |
| 561 | if (get_bits1(gb)) { |
| 562 | ret |= decode_scaling_list(gb, scaling_matrix4[0], 16); // Intra, Y |
| 563 | ret |= decode_scaling_list(gb, scaling_matrix4[1], 16); // Intra, Cr |
| 564 | ret |= decode_scaling_list(gb, scaling_matrix4[2], 16); // Intra, Cb |
| 565 | ret |= decode_scaling_list(gb, scaling_matrix4[3], 16); // Inter, Y |
| 566 | ret |= decode_scaling_list(gb, scaling_matrix4[4], 16); // Inter, Cr |
| 567 | ret |= decode_scaling_list(gb, scaling_matrix4[5], 16); // Inter, Cb |
| 568 |
| 569 | ret |= decode_scaling_list(gb, scaling_matrix8[0], 64); // Intra, Y |
| 570 | ret |= decode_scaling_list(gb, scaling_matrix8[3], 64); // Inter, Y |
| 571 | if (chroma_format_idc == 3) { |
| 572 | ret |= decode_scaling_list(gb, scaling_matrix8[1], 64); // Intra, Cr |
| 573 | ret |= decode_scaling_list(gb, scaling_matrix8[4], 64); // Inter, Cr |
| 574 | ret |= decode_scaling_list(gb, scaling_matrix8[2], 64); // Intra, Cb |
| 575 | ret |= decode_scaling_list(gb, scaling_matrix8[5], 64); // Inter, Cb |
| 576 | } |
| 577 | if (!ret) |
| 578 | ret = 1; |
| 579 | } |
| 580 |
| 581 | return ret; |
| 582 | } |
| 583 |
| 584 | int ff_h264_decode_seq_parameter_set(GetBitContext *gb, int &width, int &height) { |
| 585 | int profile_idc, level_idc, constraint_set_flags = 0; |
| 586 | unsigned int sps_id; |
| 587 | int i, log2_max_frame_num_minus4; |
| 588 | int ret; |
| 589 |
| 590 | profile_idc = get_bits(gb, 8); |
| 591 | constraint_set_flags |= get_bits1(gb) << 0; |
| 592 | constraint_set_flags |= get_bits1(gb) << 1; |
| 593 | constraint_set_flags |= get_bits1(gb) << 2; |
| 594 | constraint_set_flags |= get_bits1(gb) << 3; |
| 595 | constraint_set_flags |= get_bits1(gb) << 4; |
| 596 | constraint_set_flags |= get_bits1(gb) << 5; |
| 597 | skip_bits(gb, 2); |
| 598 | level_idc = get_bits(gb, 8); |
| 599 | sps_id = get_ue_golomb_31(gb); |
| 600 |
| 601 | if (sps_id >= MAX_SPS_COUNT) { |
| 602 | return false; |
| 603 | } |
| 604 |
| 605 | if (profile_idc == 100 || // High profile |
| 606 | profile_idc == 110 || // High10 profile |
| 607 | profile_idc == 122 || // High422 profile |
| 608 | profile_idc == 244 || // High444 Predictive profile |
| 609 | profile_idc == 44 || // Cavlc444 profile |
| 610 | profile_idc == 83 || // Scalable Constrained High profile (SVC) |
| 611 | profile_idc == 86 || // Scalable High Intra profile (SVC) |
| 612 | profile_idc == 118 || // Stereo High profile (MVC) |
| 613 | profile_idc == 128 || // Multiview High profile (MVC) |
| 614 | profile_idc == 138 || // Multiview Depth High profile (MVCD) |
| 615 | profile_idc == 144) { // old High444 profile |
| 616 | int chroma_format_idc = get_ue_golomb_31(gb); |
| 617 | if (chroma_format_idc > 3U) { |
| 618 | return false; |
| 619 | } else if (chroma_format_idc == 3) { |
| 620 | int residual_color_transform_flag = get_bits1(gb); |
| 621 | if (residual_color_transform_flag) { |
| 622 | return false; |
| 623 | } |
| 624 | } |
| 625 | int bit_depth_luma = get_ue_golomb(gb) + 8; |
| 626 | int bit_depth_chroma = get_ue_golomb(gb) + 8; |
| 627 | if (bit_depth_chroma != bit_depth_luma) { |
| 628 | return false; |
| 629 | } |
| 630 | if (bit_depth_luma < 8 || bit_depth_luma > 14 || bit_depth_chroma < 8 || bit_depth_chroma > 14) { |
| 631 | return false; |
| 632 | } |
| 633 | get_bits1(gb); |
| 634 | uint8_t scaling_matrix4[6][16]; |
| 635 | uint8_t scaling_matrix8[6][64]; |
| 636 | ret = decode_scaling_matrices(gb, chroma_format_idc, scaling_matrix4, scaling_matrix8); |
| 637 | if (ret < 0) |
| 638 | return false; |
| 639 | } |
| 640 |
| 641 | get_ue_golomb(gb); |
| 642 |
| 643 | int poc_type = get_ue_golomb_31(gb); |
| 644 |
| 645 | if (poc_type == 0) { |
| 646 | unsigned t = get_ue_golomb(gb); |
| 647 | if (t > 12) { |
| 648 | return false; |
| 649 | } |
| 650 | } else if (poc_type == 1) { |
| 651 | get_bits1(gb); |
| 652 | int offset_for_non_ref_pic = get_se_golomb_long(gb); |
| 653 | int offset_for_top_to_bottom_field = get_se_golomb_long(gb); |
| 654 |
| 655 | if (offset_for_non_ref_pic == INT32_MIN || offset_for_top_to_bottom_field == INT32_MIN) { |
| 656 | return false; |
| 657 | } |
| 658 |
| 659 | int poc_cycle_length = get_ue_golomb(gb); |
| 660 |
| 661 | if ((unsigned) poc_cycle_length >= 256) { |
| 662 | return false; |
| 663 | } |
| 664 |
| 665 | for (i = 0; i < poc_cycle_length; i++) { |
| 666 | int offset_for_ref_frame = get_se_golomb_long(gb); |
| 667 | if (offset_for_ref_frame == INT32_MIN) { |
| 668 | return false; |
| 669 | } |
| 670 | } |
| 671 | } else if (poc_type != 2) { |
| 672 | return false; |
| 673 | } |
| 674 |
| 675 | get_ue_golomb_31(gb); |
| 676 | get_bits1(gb); |
| 677 | int mb_width = get_ue_golomb(gb) + 1; |
| 678 | int mb_height = get_ue_golomb(gb) + 1; |
| 679 |
| 680 | if (width == 0 || height == 0) { |
| 681 | width = mb_width; |
| 682 | height = mb_height; |
| 683 | } |
| 684 | return mb_width != width || mb_height != height; |
| 685 | } |
| 686 |
| 687 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 688 | int ret = 0; |
| 689 | int decoded = info->pkt.size; |
| 690 | *got_frame = 0; |
| 691 |
| 692 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 693 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 && decoded > 0) { |
| 694 | ff_h2645_packet_split(&info->h2645Packet, info->pkt.data, info->pkt.size, 1, 4); |
| 695 | for (int i = 0; i < info->h2645Packet.nb_nals; i++) { |
| 696 | H2645NAL *nal = &info->h2645Packet.nals[i]; |
| 697 | switch (nal->type) { |
| 698 | case 7: { |
| 699 | GetBitContext tmp_gb = nal->gb; |
| 700 | info->dropFrames = ff_h264_decode_seq_parameter_set(&tmp_gb, info->firstWidth, info->firstHeight); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | if (!info->dropFrames) { |
| 706 | ret = avcodec_decode_video2(info->video_dec_ctx, info->frame, got_frame, &info->pkt); |
| 707 | if (ret != 0) { |
| 708 | return ret; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 |
| 713 | return decoded; |
| 714 | } |
| 715 |
| 716 | void requestFd(VideoInfo *info) { |
| 717 | JNIEnv *jniEnv = nullptr; |
| 718 |
| 719 | JavaVMAttachArgs jvmArgs; |
| 720 | jvmArgs.version = JNI_VERSION_1_6; |
| 721 |
| 722 | bool attached; |
| 723 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 724 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 725 | attached = true; |
| 726 | } else { |
| 727 | attached = false; |
| 728 | } |
| 729 | jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) 0, (jint) 1); |
| 730 | jboolean loaded = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isFinishedLoadingFile); |
| 731 | if (loaded) { |
| 732 | delete[] info->src; |
| 733 | jstring src = (jstring) jniEnv->CallObjectMethod(info->stream, jclass_AnimatedFileDrawableStream_getFinishedFilePath); |
| 734 | char const *srcString = jniEnv->GetStringUTFChars(src, 0); |
| 735 | size_t len = strlen(srcString); |
| 736 | info->src = new char[len + 1]; |
| 737 | memcpy(info->src, srcString, len); |
| 738 | info->src[len] = '\0'; |
| 739 | if (srcString != 0) { |
| 740 | jniEnv->ReleaseStringUTFChars(src, srcString); |
| 741 | } |
| 742 | } |
| 743 |
| 744 | if (attached) { |
| 745 | javaVm->DetachCurrentThread(); |
| 746 | } |
| 747 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 748 | } |
| 749 |
| 750 | int readCallback(void *opaque, uint8_t *buf, int buf_size) { |
| 751 | VideoInfo *info = (VideoInfo *) opaque; |
| 752 | if (!info->stopped) { |
| 753 | if (info->fd < 0) { |
| 754 | requestFd(info); |
| 755 | } |
| 756 | if (info->fd >= 0) { |
| 757 | if (info->last_seek_p + buf_size > info->file_size) { |
| 758 | buf_size = (int) (info->file_size - info->last_seek_p); |
| 759 | } |
| 760 | if (buf_size > 0) { |
| 761 | JNIEnv *jniEnv = nullptr; |
| 762 |
| 763 | JavaVMAttachArgs jvmArgs; |
| 764 | jvmArgs.version = JNI_VERSION_1_6; |
| 765 |
| 766 | bool attached; |
| 767 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 768 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 769 | attached = true; |
| 770 | } else { |
| 771 | attached = false; |
| 772 | } |
| 773 |
| 774 | buf_size = jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) info->last_seek_p, (jint) buf_size); |
| 775 | info->last_seek_p += buf_size; |
| 776 | if (attached) { |
| 777 | javaVm->DetachCurrentThread(); |
| 778 | } |
| 779 | return (int) read(info->fd, buf, (size_t) buf_size); |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | return 0; |
| 784 | } |
| 785 |
| 786 | int64_t seekCallback(void *opaque, int64_t offset, int whence) { |
| 787 | VideoInfo *info = (VideoInfo *) opaque; |
| 788 | if (!info->stopped) { |
| 789 | if (info->fd < 0) { |
| 790 | requestFd(info); |
| 791 | } |
| 792 | if (info->fd >= 0) { |
| 793 | if (whence & FFMPEG_AVSEEK_SIZE) { |
| 794 | return info->file_size; |
| 795 | } else { |
| 796 | info->last_seek_p = offset; |
| 797 | lseek(info->fd, off_t(offset), SEEK_SET); |
| 798 | return offset; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 |
| 805 | enum PARAM_NUM { |
| 806 | PARAM_NUM_SUPPORTED_VIDEO_CODEC = 0, |
| 807 | PARAM_NUM_WIDTH = 1, |
| 808 | PARAM_NUM_HEIGHT = 2, |
| 809 | PARAM_NUM_BITRATE = 3, |
| 810 | PARAM_NUM_DURATION = 4, |
| 811 | PARAM_NUM_AUDIO_FRAME_SIZE = 5, |
| 812 | PARAM_NUM_VIDEO_FRAME_SIZE = 6, |
| 813 | PARAM_NUM_FRAMERATE = 7, |
| 814 | PARAM_NUM_ROTATION = 8, |
| 815 | PARAM_NUM_SUPPORTED_AUDIO_CODEC = 9, |
| 816 | PARAM_NUM_HAS_AUDIO = 10, |
| 817 | PARAM_NUM_COUNT = 11, |
| 818 | }; |
| 819 |
| 820 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoInfo(JNIEnv *env, jclass clazz,jint sdkVersion, jstring src, jintArray data) { |
| 821 | VideoInfo *info = new VideoInfo(); |
| 822 |
| 823 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 824 | size_t len = strlen(srcString); |
| 825 | info->src = new char[len + 1]; |
| 826 | memcpy(info->src, srcString, len); |
| 827 | info->src[len] = '\0'; |
| 828 | if (srcString != nullptr) { |
| 829 | env->ReleaseStringUTFChars(src, srcString); |
| 830 | } |
| 831 |
| 832 | int ret; |
| 833 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 834 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 835 | delete info; |
| 836 | return; |
| 837 | } |
| 838 |
| 839 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 840 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 841 | delete info; |
| 842 | return; |
| 843 | } |
| 844 |
| 845 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0)) >= 0) { |
| 846 | info->video_stream = info->fmt_ctx->streams[ret]; |
| 847 | } |
| 848 |
| 849 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) >= 0) { |
| 850 | info->audio_stream = info->fmt_ctx->streams[ret]; |
| 851 | } |
| 852 |
| 853 | if (info->video_stream == nullptr) { |
| 854 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 855 | delete info; |
| 856 | return; |
| 857 | } |
| 858 |
| 859 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 860 | if (dataArr != nullptr) { |
| 861 | //https://developer.android.com/guide/topics/media/media-formats |
| 862 | dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC] = |
| 863 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || |
| 864 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H263 || |
| 865 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_MPEG4 || |
| 866 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP8 || |
| 867 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP9 || |
| 868 | (sdkVersion > 21 && info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC); |
| 869 |
| 870 | if (strstr(info->fmt_ctx->iformat->name, "mov") != 0 && dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC]) { |
| 871 | MOVStreamContext *mov = (MOVStreamContext *) info->video_stream->priv_data; |
| 872 | dataArr[PARAM_NUM_VIDEO_FRAME_SIZE] = (jint) mov->data_size; |
| 873 |
| 874 | if (info->audio_stream != nullptr) { |
| 875 | mov = (MOVStreamContext *) info->audio_stream->priv_data; |
| 876 | dataArr[PARAM_NUM_AUDIO_FRAME_SIZE] = (jint) mov->data_size; |
| 877 | } |
| 878 | } |
| 879 |
| 880 | if (info->audio_stream != nullptr) { |
| 881 | //https://developer.android.com/guide/topics/media/media-formats |
| 882 | dataArr[PARAM_NUM_SUPPORTED_AUDIO_CODEC] = |
| 883 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC || |
| 884 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC_LATM || |
| 885 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_VORBIS || |
| 886 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_NB || |
| 887 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_WB || |
| 888 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_FLAC || |
| 889 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_MP3 || |
| 890 | // not supported codec, skip audio in this case |
| 891 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV || |
| 892 | (sdkVersion > 21 && info->audio_stream->codecpar->codec_id == AV_CODEC_ID_OPUS); |
| 893 | dataArr[PARAM_NUM_HAS_AUDIO] = 1; |
| 894 | } else { |
| 895 | dataArr[PARAM_NUM_HAS_AUDIO] = 0; |
| 896 | } |
| 897 |
| 898 | dataArr[PARAM_NUM_BITRATE] = (jint) info->video_stream->codecpar->bit_rate; |
| 899 | dataArr[PARAM_NUM_WIDTH] = info->video_stream->codecpar->width; |
| 900 | dataArr[PARAM_NUM_HEIGHT] = info->video_stream->codecpar->height; |
| 901 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 902 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0") != 0) { |
| 903 | char *tail; |
| 904 | dataArr[PARAM_NUM_ROTATION] = (jint) av_strtod(rotate_tag->value, &tail); |
| 905 | if (*tail) { |
| 906 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 907 | } |
| 908 | } else { |
| 909 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 910 | } |
| 911 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC) { |
| 912 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->avg_frame_rate); |
| 913 | } else { |
| 914 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->r_frame_rate); |
| 915 | } |
| 916 | dataArr[PARAM_NUM_DURATION] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 917 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 918 | delete info; |
| 919 | } |
| 920 | } |
| 921 |
| 922 | extern "C" JNIEXPORT jlong JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_createDecoder(JNIEnv *env, jclass clazz, jstring src, jintArray data, jint account, jlong streamFileSize, jobject stream, jboolean preview) { |
| 923 | VideoInfo *info = new VideoInfo(); |
| 924 |
| 925 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 926 | size_t len = strlen(srcString); |
| 927 | info->src = new char[len + 1]; |
| 928 | memcpy(info->src, srcString, len); |
| 929 | info->src[len] = '\0'; |
| 930 | if (srcString != 0) { |
| 931 | env->ReleaseStringUTFChars(src, srcString); |
| 932 | } |
| 933 |
| 934 | int ret; |
| 935 | if (streamFileSize != 0) { |
| 936 | info->file_size = streamFileSize; |
| 937 | info->stream = env->NewGlobalRef(stream); |
| 938 | info->account = account; |
| 939 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 940 |
| 941 | info->ioBuffer = (unsigned char *) av_malloc(64 * 1024); |
| 942 | info->ioContext = avio_alloc_context(info->ioBuffer, 64 * 1024, 0, info, readCallback, nullptr, seekCallback); |
| 943 | if (info->ioContext == nullptr) { |
| 944 | delete info; |
| 945 | return 0; |
| 946 | } |
| 947 |
| 948 | info->fmt_ctx = avformat_alloc_context(); |
| 949 | info->fmt_ctx->pb = info->ioContext; |
| 950 |
| 951 | AVDictionary *options = NULL; |
| 952 | av_dict_set(&options, "usetoc", "1", 0); |
| 953 | ret = avformat_open_input(&info->fmt_ctx, "http://localhost/file", NULL, &options); |
| 954 | av_dict_free(&options); |
| 955 | if (ret < 0) { |
| 956 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 957 | delete info; |
| 958 | return 0; |
| 959 | } |
| 960 | info->fmt_ctx->flags |= AVFMT_FLAG_FAST_SEEK; |
| 961 | if (preview) { |
| 962 | info->fmt_ctx->flags |= AVFMT_FLAG_NOBUFFER; |
| 963 | } |
| 964 | } else { |
| 965 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 966 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 967 | delete info; |
| 968 | return 0; |
| 969 | } |
| 970 | } |
| 971 |
| 972 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 973 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 974 | delete info; |
| 975 | return 0; |
| 976 | } |
| 977 |
| 978 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 979 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 980 | } |
| 981 |
| 982 | if (info->video_stream == nullptr) { |
| 983 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 984 | delete info; |
| 985 | return 0; |
| 986 | } |
| 987 |
| 988 | info->frame = av_frame_alloc(); |
| 989 | if (info->frame == nullptr) { |
| 990 | LOGE("can't allocate frame %s", info->src); |
| 991 | delete info; |
| 992 | return 0; |
| 993 | } |
| 994 |
| 995 | av_init_packet(&info->pkt); |
| 996 | info->pkt.data = NULL; |
| 997 | info->pkt.size = 0; |
| 998 |
| 999 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 1000 | if (dataArr != nullptr) { |
| 1001 | dataArr[0] = info->video_dec_ctx->width; |
| 1002 | dataArr[1] = info->video_dec_ctx->height; |
| 1003 | //float pixelWidthHeightRatio = info->video_dec_ctx->sample_aspect_ratio.num / info->video_dec_ctx->sample_aspect_ratio.den; TODO support |
| 1004 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 1005 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 1006 | char *tail; |
| 1007 | dataArr[2] = (jint) av_strtod(rotate_tag->value, &tail); |
| 1008 | if (*tail) { |
| 1009 | dataArr[2] = 0; |
| 1010 | } |
| 1011 | } else { |
| 1012 | dataArr[2] = 0; |
| 1013 | } |
| 1014 | dataArr[4] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 1015 | //(int32_t) (1000 * info->video_stream->duration * av_q2d(info->video_stream->time_base)); |
| 1016 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 1017 | } |
| 1018 |
| 1019 | //LOGD("successfully opened file %s", info->src); |
| 1020 |
| 1021 | return (jlong) (intptr_t) info; |
| 1022 | } |
| 1023 |
| 1024 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1025 | if (ptr == NULL) { |
| 1026 | return; |
| 1027 | } |
| 1028 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1029 | if (info->stream != nullptr) { |
| 1030 | JNIEnv *jniEnv = nullptr; |
| 1031 | JavaVMAttachArgs jvmArgs; |
| 1032 | jvmArgs.version = JNI_VERSION_1_6; |
| 1033 |
| 1034 | bool attached; |
| 1035 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 1036 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 1037 | attached = true; |
| 1038 | } else { |
| 1039 | attached = false; |
| 1040 | } |
| 1041 | jniEnv->CallVoidMethod(info->stream, jclass_AnimatedFileDrawableStream_cancel); |
| 1042 | if (attached) { |
| 1043 | javaVm->DetachCurrentThread(); |
| 1044 | } |
| 1045 | } |
| 1046 | delete info; |
| 1047 | } |
| 1048 |
| 1049 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_stopDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1050 | if (ptr == NULL) { |
| 1051 | return; |
| 1052 | } |
| 1053 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1054 | info->stopped = true; |
| 1055 | } |
| 1056 |
| 1057 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_prepareToSeek(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1058 | if (ptr == NULL) { |
| 1059 | return; |
| 1060 | } |
| 1061 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1062 | info->seeking = true; |
| 1063 | } |
| 1064 |
| 1065 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_seekToMs(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jboolean precise) { |
| 1066 | if (ptr == NULL) { |
| 1067 | return; |
| 1068 | } |
| 1069 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1070 | info->seeking = false; |
| 1071 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 1072 | int ret = 0; |
| 1073 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1074 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 1075 | return; |
| 1076 | } else { |
| 1077 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1078 | if (!precise) { |
| 1079 | return; |
| 1080 | } |
| 1081 | int got_frame = 0; |
| 1082 | int32_t tries = 1000; |
| 1083 | while (tries > 0) { |
| 1084 | if (info->pkt.size == 0) { |
| 1085 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1086 | if (ret >= 0) { |
| 1087 | info->orig_pkt = info->pkt; |
| 1088 | } |
| 1089 | } |
| 1090 |
| 1091 | if (info->pkt.size > 0) { |
| 1092 | ret = decode_packet(info, &got_frame); |
| 1093 | if (ret < 0) { |
| 1094 | if (info->has_decoded_frames) { |
| 1095 | ret = 0; |
| 1096 | } |
| 1097 | info->pkt.size = 0; |
| 1098 | } else { |
| 1099 | info->pkt.data += ret; |
| 1100 | info->pkt.size -= ret; |
| 1101 | } |
| 1102 | if (info->pkt.size == 0) { |
| 1103 | av_packet_unref(&info->orig_pkt); |
| 1104 | } |
| 1105 | } else { |
| 1106 | info->pkt.data = NULL; |
| 1107 | info->pkt.size = 0; |
| 1108 | ret = decode_packet(info, &got_frame); |
| 1109 | if (ret < 0) { |
| 1110 | return; |
| 1111 | } |
| 1112 | if (got_frame == 0) { |
| 1113 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 1114 | return; |
| 1115 | } |
| 1116 | } |
| 1117 | if (ret < 0) { |
| 1118 | return; |
| 1119 | } |
| 1120 | if (got_frame) { |
| 1121 | info->has_decoded_frames = true; |
| 1122 | bool finished = false; |
| 1123 | if (info->frame->format == AV_PIX_FMT_YUV444P || info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 1124 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 1125 | if (pkt_pts >= pts) { |
| 1126 | finished = true; |
| 1127 | } |
| 1128 | } |
| 1129 | av_frame_unref(info->frame); |
| 1130 | if (finished) { |
| 1131 | return; |
| 1132 | } |
| 1133 | } |
| 1134 | tries--; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 |
| 1139 | static inline void writeFrameToBitmap(JNIEnv *env, VideoInfo *info, jintArray data, jobject bitmap, jint stride) { |
| 1140 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 1141 | int32_t wantedWidth; |
| 1142 | int32_t wantedHeight; |
| 1143 |
| 1144 | AndroidBitmapInfo bitmapInfo; |
| 1145 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 1146 | int32_t bitmapWidth = bitmapInfo.width; |
| 1147 | int32_t bitmapHeight = bitmapInfo.height; |
| 1148 | if (dataArr != nullptr) { |
| 1149 | wantedWidth = dataArr[0]; |
| 1150 | wantedHeight = dataArr[1]; |
| 1151 | dataArr[3] = (jint) (1000 * info->frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 1152 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 1153 | } else { |
| 1154 | wantedWidth = bitmapWidth; |
| 1155 | wantedHeight = bitmapHeight; |
| 1156 | } |
| 1157 |
| 1158 | if (wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width) { |
| 1159 | void *pixels; |
| 1160 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 1161 | if (info->sws_ctx == nullptr) { |
| 1162 | if (info->frame->format > AV_PIX_FMT_NONE && info->frame->format < AV_PIX_FMT_NB) { |
| 1163 | info->sws_ctx = sws_getContext(info->frame->width, info->frame->height, (AVPixelFormat) info->frame->format, bitmapWidth, bitmapHeight, AV_PIX_FMT_RGBA, SWS_BILINEAR, NULL, NULL, NULL); |
| 1164 | } else if (info->video_dec_ctx->pix_fmt > AV_PIX_FMT_NONE && info->video_dec_ctx->pix_fmt < AV_PIX_FMT_NB) { |
| 1165 | info->sws_ctx = sws_getContext(info->video_dec_ctx->width, info->video_dec_ctx->height, info->video_dec_ctx->pix_fmt, bitmapWidth, bitmapHeight, AV_PIX_FMT_RGBA, SWS_BILINEAR, NULL, NULL, NULL); |
| 1166 | } |
| 1167 | } |
| 1168 | if (info->sws_ctx == nullptr || ((intptr_t) pixels) % 16 != 0) { |
| 1169 | if (info->frame->format == AV_PIX_FMT_YUV444P) { |
| 1170 | libyuv::H444ToARGB(info->frame->data[0], info->frame->linesize[0], info->frame->data[2], info->frame->linesize[2], info->frame->data[1], info->frame->linesize[1], (uint8_t *) pixels, bitmapWidth * 4, bitmapWidth, bitmapHeight); |
| 1171 | } else if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 1172 | if (info->frame->colorspace == AVColorSpace::AVCOL_SPC_BT709) { |
| 1173 | libyuv::H420ToARGB(info->frame->data[0], info->frame->linesize[0], info->frame->data[2], info->frame->linesize[2], info->frame->data[1], info->frame->linesize[1], (uint8_t *) pixels, bitmapWidth * 4, bitmapWidth, bitmapHeight); |
| 1174 | } else { |
| 1175 | libyuv::I420ToARGB(info->frame->data[0], info->frame->linesize[0], info->frame->data[2], info->frame->linesize[2], info->frame->data[1], info->frame->linesize[1], (uint8_t *) pixels, bitmapWidth * 4, bitmapWidth, bitmapHeight); |
| 1176 | } |
| 1177 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 1178 | libyuv::ABGRToARGB(info->frame->data[0], info->frame->linesize[0], (uint8_t *) pixels, info->frame->width * 4, info->frame->width, info->frame->height); |
| 1179 | } |
| 1180 | } else { |
| 1181 | uint8_t __attribute__ ((aligned (16))) *dst_data[1]; |
| 1182 | dst_data[0] = (uint8_t *) pixels; |
| 1183 | info->dst_linesize[0] = stride; |
| 1184 | sws_scale(info->sws_ctx, info->frame->data, info->frame->linesize, 0, info->frame->height, dst_data, info->dst_linesize); |
| 1185 | } |
| 1186 | } |
| 1187 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1188 | } |
| 1189 | } |
| 1190 |
| 1191 | extern "C" JNIEXPORT int JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getFrameAtTime(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jobject bitmap, jintArray data, jint stride) { |
| 1192 | if (ptr == NULL || bitmap == nullptr || data == nullptr) { |
| 1193 | return 0; |
| 1194 | } |
| 1195 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1196 | info->seeking = false; |
| 1197 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 1198 | int ret = 0; |
| 1199 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1200 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 1201 | return 0; |
| 1202 | } else { |
| 1203 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1204 | int got_frame = 0; |
| 1205 | int32_t tries = 1000; |
| 1206 | bool readNextPacket = true; |
| 1207 | while (tries > 0) { |
| 1208 | if (info->pkt.size == 0 && readNextPacket) { |
| 1209 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1210 | if (ret >= 0) { |
| 1211 | info->orig_pkt = info->pkt; |
| 1212 | } |
| 1213 | } |
| 1214 |
| 1215 | if (info->pkt.size > 0) { |
| 1216 | ret = decode_packet(info, &got_frame); |
| 1217 | if (ret < 0) { |
| 1218 | if (info->has_decoded_frames) { |
| 1219 | ret = 0; |
| 1220 | } |
| 1221 | info->pkt.size = 0; |
| 1222 | } else { |
| 1223 | info->pkt.data += ret; |
| 1224 | info->pkt.size -= ret; |
| 1225 | } |
| 1226 | if (info->pkt.size == 0) { |
| 1227 | av_packet_unref(&info->orig_pkt); |
| 1228 | } |
| 1229 | } else { |
| 1230 | info->pkt.data = NULL; |
| 1231 | info->pkt.size = 0; |
| 1232 | ret = decode_packet(info, &got_frame); |
| 1233 | if (ret < 0) { |
| 1234 | return 0; |
| 1235 | } |
| 1236 | if (got_frame == 0) { |
| 1237 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | } |
| 1241 | if (ret < 0) { |
| 1242 | return 0; |
| 1243 | } |
| 1244 | if (got_frame) { |
| 1245 | bool finished = false; |
| 1246 | if (info->frame->format == AV_PIX_FMT_YUV444P || info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 1247 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 1248 | bool isLastPacket = false; |
| 1249 | if (info->pkt.size == 0) { |
| 1250 | readNextPacket = false; |
| 1251 | isLastPacket = av_read_frame(info->fmt_ctx, &info->pkt) < 0; |
| 1252 | } |
| 1253 | if (pkt_pts >= pts || isLastPacket) { |
| 1254 | writeFrameToBitmap(env, info, data, bitmap, stride); |
| 1255 | finished = true; |
| 1256 | } |
| 1257 | } |
| 1258 | av_frame_unref(info->frame); |
| 1259 | if (finished) { |
| 1260 | return 1; |
| 1261 | } |
| 1262 | } else { |
| 1263 | readNextPacket = true; |
| 1264 | } |
| 1265 | tries--; |
| 1266 | } |
| 1267 | return 0; |
| 1268 | } |
| 1269 | } |
| 1270 |
| 1271 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jintArray data, jint stride, jboolean preview, jfloat start_time, jfloat end_time) { |
| 1272 | if (ptr == NULL || bitmap == nullptr) { |
| 1273 | return 0; |
| 1274 | } |
| 1275 | //int64_t time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 1276 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1277 | int ret = 0; |
| 1278 | int got_frame = 0; |
| 1279 | int32_t triesCount = preview ? 50 : 6; |
| 1280 | //info->has_decoded_frames = false; |
| 1281 | while (!info->stopped && triesCount != 0) { |
| 1282 | if (info->pkt.size == 0) { |
| 1283 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1284 | if (ret >= 0) { |
| 1285 | double pts = info->pkt.pts * av_q2d(info->video_stream->time_base); |
| 1286 | if (end_time > 0 && info->pkt.stream_index == info->video_stream_idx && pts > end_time) { |
| 1287 | av_packet_unref(&info->pkt); |
| 1288 | info->pkt.data = NULL; |
| 1289 | info->pkt.size = 0; |
| 1290 | } else { |
| 1291 | info->orig_pkt = info->pkt; |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 |
| 1296 | if (info->pkt.size > 0) { |
| 1297 | ret = decode_packet(info, &got_frame); |
| 1298 | if (ret < 0) { |
| 1299 | if (info->has_decoded_frames) { |
| 1300 | ret = 0; |
| 1301 | } |
| 1302 | info->pkt.size = 0; |
| 1303 | } else { |
| 1304 | //LOGD("read size %d from packet", ret); |
| 1305 | info->pkt.data += ret; |
| 1306 | info->pkt.size -= ret; |
| 1307 | } |
| 1308 |
| 1309 | if (info->pkt.size == 0) { |
| 1310 | av_packet_unref(&info->orig_pkt); |
| 1311 | } |
| 1312 | } else { |
| 1313 | info->pkt.data = NULL; |
| 1314 | info->pkt.size = 0; |
| 1315 | ret = decode_packet(info, &got_frame); |
| 1316 | if (ret < 0) { |
| 1317 | LOGE("can't decode packet flushed %s", info->src); |
| 1318 | return 0; |
| 1319 | } |
| 1320 | if (!preview && got_frame == 0) { |
| 1321 | if (info->has_decoded_frames) { |
| 1322 | int64_t start_from = 0; |
| 1323 | if (start_time > 0) { |
| 1324 | start_from = (int64_t)(start_time / av_q2d(info->video_stream->time_base)); |
| 1325 | } |
| 1326 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, start_from, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1327 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 1328 | return 0; |
| 1329 | } else { |
| 1330 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | if (ret < 0 || info->seeking) { |
| 1336 | return 0; |
| 1337 | } |
| 1338 | if (got_frame) { |
| 1339 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 1340 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P || info->frame->format == AV_PIX_FMT_YUV444P) { |
| 1341 | writeFrameToBitmap(env, info, data, bitmap, stride); |
| 1342 | } |
| 1343 | info->has_decoded_frames = true; |
| 1344 | av_frame_unref(info->frame); |
| 1345 | return 1; |
| 1346 | } |
| 1347 | if (!info->has_decoded_frames) { |
| 1348 | triesCount--; |
| 1349 | } |
| 1350 | } |
| 1351 | return 0; |
| 1352 | } |
| 1353 |
| 1354 | extern "C" jint videoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 1355 | //av_log_set_callback(custom_log); |
| 1356 | jclass_AnimatedFileDrawableStream = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/AnimatedFileDrawableStream")); |
| 1357 | if (jclass_AnimatedFileDrawableStream == 0) { |
| 1358 | return JNI_FALSE; |
| 1359 | } |
| 1360 | jclass_AnimatedFileDrawableStream_read = env->GetMethodID(jclass_AnimatedFileDrawableStream, "read", "(II)I"); |
| 1361 | if (jclass_AnimatedFileDrawableStream_read == 0) { |
| 1362 | return JNI_FALSE; |
| 1363 | } |
| 1364 | jclass_AnimatedFileDrawableStream_cancel = env->GetMethodID(jclass_AnimatedFileDrawableStream, "cancel", "()V"); |
| 1365 | if (jclass_AnimatedFileDrawableStream_cancel == 0) { |
| 1366 | return JNI_FALSE; |
| 1367 | } |
| 1368 | jclass_AnimatedFileDrawableStream_isFinishedLoadingFile = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isFinishedLoadingFile", "()Z"); |
| 1369 | if (jclass_AnimatedFileDrawableStream_isFinishedLoadingFile == 0) { |
| 1370 | return JNI_FALSE; |
| 1371 | } |
| 1372 | jclass_AnimatedFileDrawableStream_getFinishedFilePath = env->GetMethodID(jclass_AnimatedFileDrawableStream, "getFinishedFilePath", "()Ljava/lang/String;"); |
| 1373 | if (jclass_AnimatedFileDrawableStream_getFinishedFilePath == 0) { |
| 1374 | return JNI_FALSE; |
| 1375 | } |
| 1376 |
| 1377 | return JNI_TRUE; |
| 1378 | } |
| 1379 |