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