| 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/ConnectionsManager.h" |
| 12 | #include "c_utils.h" |
| 13 |
| 14 | extern "C" { |
| 15 | #include <libavformat/avformat.h> |
| 16 | #include <libavutil/eval.h> |
| 17 | #include <libswscale/swscale.h> |
| 18 |
| 19 | static const std::string av_make_error_str(int errnum) { |
| 20 | char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 21 | av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); |
| 22 | return (std::string) errbuf; |
| 23 | } |
| 24 |
| 25 | #undef av_err2str |
| 26 | #define av_err2str(errnum) av_make_error_str(errnum).c_str() |
| 27 | #define FFMPEG_AVSEEK_SIZE 0x10000 |
| 28 |
| 29 | jclass jclass_AnimatedFileDrawableStream; |
| 30 | jmethodID jclass_AnimatedFileDrawableStream_read; |
| 31 | jmethodID jclass_AnimatedFileDrawableStream_cancel; |
| 32 |
| 33 | typedef struct VideoInfo { |
| 34 |
| 35 | ~VideoInfo() { |
| 36 | if (video_dec_ctx) { |
| 37 | avcodec_close(video_dec_ctx); |
| 38 | video_dec_ctx = nullptr; |
| 39 | } |
| 40 | if (fmt_ctx) { |
| 41 | avformat_close_input(&fmt_ctx); |
| 42 | fmt_ctx = nullptr; |
| 43 | } |
| 44 | if (frame) { |
| 45 | av_frame_free(&frame); |
| 46 | frame = nullptr; |
| 47 | } |
| 48 | if (src) { |
| 49 | delete [] src; |
| 50 | src = nullptr; |
| 51 | } |
| 52 | if (stream != nullptr) { |
| 53 | JNIEnv *jniEnv = nullptr; |
| 54 | JavaVMAttachArgs jvmArgs; |
| 55 | jvmArgs.version = JNI_VERSION_1_6; |
| 56 |
| 57 | bool attached; |
| 58 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 59 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 60 | attached = true; |
| 61 | } else { |
| 62 | attached = false; |
| 63 | } |
| 64 | jniEnv->DeleteGlobalRef(stream); |
| 65 | if (attached) { |
| 66 | javaVm->DetachCurrentThread(); |
| 67 | } |
| 68 | stream = nullptr; |
| 69 | } |
| 70 | /*if (ioBuffer) { TODO memleak? |
| 71 | av_free(ioBuffer); |
| 72 | ioBuffer = nullptr; |
| 73 | }*/ |
| 74 | if (ioContext != nullptr) { |
| 75 | avio_context_free(&ioContext); |
| 76 | ioContext = nullptr; |
| 77 | } |
| 78 | if (sws_ctx != nullptr) { |
| 79 | sws_freeContext(sws_ctx); |
| 80 | sws_ctx = nullptr; |
| 81 | } |
| 82 | if (fd >= 0) { |
| 83 | close(fd); |
| 84 | fd = -1; |
| 85 | } |
| 86 |
| 87 | av_packet_unref(&orig_pkt); |
| 88 |
| 89 | video_stream_idx = -1; |
| 90 | video_stream = nullptr; |
| 91 | } |
| 92 |
| 93 | AVFormatContext *fmt_ctx = nullptr; |
| 94 | char *src = nullptr; |
| 95 | int video_stream_idx = -1; |
| 96 | AVStream *video_stream = nullptr; |
| 97 | AVCodecContext *video_dec_ctx = nullptr; |
| 98 | AVFrame *frame = nullptr; |
| 99 | bool has_decoded_frames = false; |
| 100 | AVPacket pkt; |
| 101 | AVPacket orig_pkt; |
| 102 | bool stopped = false; |
| 103 | bool seeking = false; |
| 104 |
| 105 | uint8_t __attribute__ ((aligned (16))) *dst_data[1]; |
| 106 | int32_t dst_linesize[1]; |
| 107 |
| 108 | struct SwsContext *sws_ctx = nullptr; |
| 109 |
| 110 | AVIOContext *ioContext = nullptr; |
| 111 | unsigned char *ioBuffer = nullptr; |
| 112 | jobject stream = nullptr; |
| 113 | int32_t account = 0; |
| 114 | int fd = -1; |
| 115 | int64_t file_size = 0; |
| 116 | int64_t last_seek_p = 0; |
| 117 | }; |
| 118 |
| 119 | int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 120 | int ret, stream_index; |
| 121 | AVStream *st; |
| 122 | AVCodec *dec = NULL; |
| 123 | AVDictionary *opts = NULL; |
| 124 |
| 125 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 126 | if (ret < 0) { |
| 127 | LOGE("can't find %s stream in input file", av_get_media_type_string(type)); |
| 128 | return ret; |
| 129 | } else { |
| 130 | stream_index = ret; |
| 131 | st = fmt_ctx->streams[stream_index]; |
| 132 |
| 133 | dec = avcodec_find_decoder(st->codecpar->codec_id); |
| 134 | if (!dec) { |
| 135 | LOGE("failed to find %s codec", av_get_media_type_string(type)); |
| 136 | return AVERROR(EINVAL); |
| 137 | } |
| 138 |
| 139 | *dec_ctx = avcodec_alloc_context3(dec); |
| 140 | if (!*dec_ctx) { |
| 141 | LOGE("Failed to allocate the %s codec context", av_get_media_type_string(type)); |
| 142 | return AVERROR(ENOMEM); |
| 143 | } |
| 144 |
| 145 | if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { |
| 146 | LOGE("Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); |
| 147 | return ret; |
| 148 | } |
| 149 |
| 150 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 151 | if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { |
| 152 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 153 | return ret; |
| 154 | } |
| 155 | *stream_idx = stream_index; |
| 156 | } |
| 157 |
| 158 | return 0; |
| 159 | } |
| 160 |
| 161 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 162 | int ret = 0; |
| 163 | int decoded = info->pkt.size; |
| 164 | *got_frame = 0; |
| 165 |
| 166 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 167 | ret = avcodec_decode_video2(info->video_dec_ctx, info->frame, got_frame, &info->pkt); |
| 168 | if (ret != 0) { |
| 169 | return ret; |
| 170 | } |
| 171 | } |
| 172 |
| 173 | return decoded; |
| 174 | } |
| 175 |
| 176 | void requestFd(VideoInfo *info) { |
| 177 | JNIEnv *jniEnv = nullptr; |
| 178 |
| 179 | JavaVMAttachArgs jvmArgs; |
| 180 | jvmArgs.version = JNI_VERSION_1_6; |
| 181 |
| 182 | bool attached; |
| 183 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 184 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 185 | attached = true; |
| 186 | } else { |
| 187 | attached = false; |
| 188 | } |
| 189 | jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) 0, (jint) 1); |
| 190 | if (attached) { |
| 191 | javaVm->DetachCurrentThread(); |
| 192 | } |
| 193 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 194 | } |
| 195 |
| 196 | int readCallback(void *opaque, uint8_t *buf, int buf_size) { |
| 197 | VideoInfo *info = (VideoInfo *) opaque; |
| 198 | if (!info->stopped) { |
| 199 | if (info->fd < 0) { |
| 200 | requestFd(info); |
| 201 | } |
| 202 | if (info->fd >= 0) { |
| 203 | if (info->last_seek_p + buf_size > info->file_size) { |
| 204 | buf_size = (int) (info->file_size - info->last_seek_p); |
| 205 | } |
| 206 | if (buf_size > 0) { |
| 207 | JNIEnv *jniEnv = nullptr; |
| 208 |
| 209 | JavaVMAttachArgs jvmArgs; |
| 210 | jvmArgs.version = JNI_VERSION_1_6; |
| 211 |
| 212 | bool attached; |
| 213 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 214 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 215 | attached = true; |
| 216 | } else { |
| 217 | attached = false; |
| 218 | } |
| 219 |
| 220 | buf_size = jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) info->last_seek_p, (jint) buf_size); |
| 221 | info->last_seek_p += buf_size; |
| 222 | if (attached) { |
| 223 | javaVm->DetachCurrentThread(); |
| 224 | } |
| 225 | return (int) read(info->fd, buf, (size_t) buf_size); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 |
| 232 | int64_t seekCallback(void *opaque, int64_t offset, int whence) { |
| 233 | VideoInfo *info = (VideoInfo *) opaque; |
| 234 | if (!info->stopped) { |
| 235 | if (info->fd < 0) { |
| 236 | requestFd(info); |
| 237 | } |
| 238 | if (info->fd >= 0) { |
| 239 | if (whence & FFMPEG_AVSEEK_SIZE) { |
| 240 | return info->file_size; |
| 241 | } else { |
| 242 | info->last_seek_p = offset; |
| 243 | lseek(info->fd, off_t(offset), SEEK_SET); |
| 244 | return offset; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 |
| 251 | jlong Java_org_telegram_ui_Components_AnimatedFileDrawable_createDecoder(JNIEnv *env, jclass clazz, jstring src, jintArray data, jint account, jlong streamFileSize, jobject stream) { |
| 252 | VideoInfo *info = new VideoInfo(); |
| 253 |
| 254 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 255 | size_t len = strlen(srcString); |
| 256 | info->src = new char[len + 1]; |
| 257 | memcpy(info->src, srcString, len); |
| 258 | info->src[len] = '\0'; |
| 259 | if (srcString != 0) { |
| 260 | env->ReleaseStringUTFChars(src, srcString); |
| 261 | } |
| 262 |
| 263 | int ret; |
| 264 | if (streamFileSize != 0) { |
| 265 | info->file_size = streamFileSize; |
| 266 | info->stream = env->NewGlobalRef(stream); |
| 267 | info->account = account; |
| 268 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 269 |
| 270 | info->ioBuffer = (unsigned char *) av_malloc(64 * 1024); |
| 271 | info->ioContext = avio_alloc_context(info->ioBuffer, 64 * 1024, 0, info, readCallback, nullptr, seekCallback); |
| 272 | if (info->ioContext == nullptr) { |
| 273 | delete info; |
| 274 | return 0; |
| 275 | } |
| 276 |
| 277 | info->fmt_ctx = avformat_alloc_context(); |
| 278 | info->fmt_ctx->pb = info->ioContext; |
| 279 |
| 280 | AVDictionary *options = NULL; |
| 281 | av_dict_set(&options, "usetoc", "1", 0); |
| 282 | ret = avformat_open_input(&info->fmt_ctx, "http://localhost/file", NULL, &options); |
| 283 | av_dict_free(&options); |
| 284 | if (ret < 0) { |
| 285 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 286 | delete info; |
| 287 | return 0; |
| 288 | } |
| 289 | info->fmt_ctx->flags |= AVFMT_FLAG_FAST_SEEK; |
| 290 | } else { |
| 291 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 292 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 293 | delete info; |
| 294 | return 0; |
| 295 | } |
| 296 | } |
| 297 |
| 298 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 299 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 300 | delete info; |
| 301 | return 0; |
| 302 | } |
| 303 |
| 304 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 305 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 306 | } |
| 307 |
| 308 | if (info->video_stream <= 0) { |
| 309 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 310 | delete info; |
| 311 | return 0; |
| 312 | } |
| 313 |
| 314 | info->frame = av_frame_alloc(); |
| 315 | if (info->frame == nullptr) { |
| 316 | LOGE("can't allocate frame %s", info->src); |
| 317 | delete info; |
| 318 | return 0; |
| 319 | } |
| 320 |
| 321 | av_init_packet(&info->pkt); |
| 322 | info->pkt.data = NULL; |
| 323 | info->pkt.size = 0; |
| 324 |
| 325 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 326 | if (dataArr != nullptr) { |
| 327 | dataArr[0] = info->video_dec_ctx->width; |
| 328 | dataArr[1] = info->video_dec_ctx->height; |
| 329 | //float pixelWidthHeightRatio = info->video_dec_ctx->sample_aspect_ratio.num / info->video_dec_ctx->sample_aspect_ratio.den; TODO support |
| 330 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 331 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 332 | char *tail; |
| 333 | dataArr[2] = (jint) av_strtod(rotate_tag->value, &tail); |
| 334 | if (*tail) { |
| 335 | dataArr[2] = 0; |
| 336 | } |
| 337 | } else { |
| 338 | dataArr[2] = 0; |
| 339 | } |
| 340 | dataArr[4] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 341 | //(int32_t) (1000 * info->video_stream->duration * av_q2d(info->video_stream->time_base)); |
| 342 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 343 | } |
| 344 |
| 345 | //LOGD("successfully opened file %s", info->src); |
| 346 |
| 347 | return (jlong) (intptr_t) info; |
| 348 | } |
| 349 |
| 350 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 351 | if (ptr == NULL) { |
| 352 | return; |
| 353 | } |
| 354 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 355 | if (info->stream != nullptr) { |
| 356 | JNIEnv *jniEnv = nullptr; |
| 357 | JavaVMAttachArgs jvmArgs; |
| 358 | jvmArgs.version = JNI_VERSION_1_6; |
| 359 |
| 360 | bool attached; |
| 361 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 362 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 363 | attached = true; |
| 364 | } else { |
| 365 | attached = false; |
| 366 | } |
| 367 | jniEnv->CallVoidMethod(info->stream, jclass_AnimatedFileDrawableStream_cancel); |
| 368 | if (attached) { |
| 369 | javaVm->DetachCurrentThread(); |
| 370 | } |
| 371 | } |
| 372 | delete info; |
| 373 | } |
| 374 |
| 375 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_stopDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 376 | if (ptr == NULL) { |
| 377 | return; |
| 378 | } |
| 379 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 380 | info->stopped = true; |
| 381 | } |
| 382 |
| 383 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_prepareToSeek(JNIEnv *env, jclass clazz, jlong ptr) { |
| 384 | if (ptr == NULL) { |
| 385 | return; |
| 386 | } |
| 387 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 388 | info->seeking = true; |
| 389 | } |
| 390 |
| 391 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_seekToMs(JNIEnv *env, jclass clazz, jlong ptr, jlong ms) { |
| 392 | if (ptr == NULL) { |
| 393 | return; |
| 394 | } |
| 395 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 396 | info->seeking = false; |
| 397 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 398 | int ret = 0; |
| 399 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 400 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 401 | return; |
| 402 | } else { |
| 403 | avcodec_flush_buffers(info->video_dec_ctx); |
| 404 | int got_frame = 0; |
| 405 | int32_t tries = 1000; |
| 406 | while (tries > 0) { |
| 407 | if (info->pkt.size == 0) { |
| 408 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 409 | if (ret >= 0) { |
| 410 | info->orig_pkt = info->pkt; |
| 411 | } |
| 412 | } |
| 413 |
| 414 | if (info->pkt.size > 0) { |
| 415 | ret = decode_packet(info, &got_frame); |
| 416 | if (ret < 0) { |
| 417 | if (info->has_decoded_frames) { |
| 418 | ret = 0; |
| 419 | } |
| 420 | info->pkt.size = 0; |
| 421 | } else { |
| 422 | info->pkt.data += ret; |
| 423 | info->pkt.size -= ret; |
| 424 | } |
| 425 | if (info->pkt.size == 0) { |
| 426 | av_packet_unref(&info->orig_pkt); |
| 427 | } |
| 428 | } else { |
| 429 | info->pkt.data = NULL; |
| 430 | info->pkt.size = 0; |
| 431 | ret = decode_packet(info, &got_frame); |
| 432 | if (ret < 0) { |
| 433 | return; |
| 434 | } |
| 435 | if (got_frame == 0) { |
| 436 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 437 | return; |
| 438 | } |
| 439 | } |
| 440 | if (ret < 0) { |
| 441 | return; |
| 442 | } |
| 443 | if (got_frame) { |
| 444 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 445 | int64_t pkt_pts; |
| 446 | if (info->frame->pts != AV_NOPTS_VALUE) { |
| 447 | pkt_pts = info->frame->pts; |
| 448 | } else { |
| 449 | pkt_pts = info->frame->pkt_dts; |
| 450 | } |
| 451 | if (pkt_pts >= pts) { |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | av_frame_unref(info->frame); |
| 456 | } |
| 457 | tries--; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 |
| 462 | jint Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jintArray data, jint stride) { |
| 463 | if (ptr == NULL || bitmap == nullptr) { |
| 464 | return 0; |
| 465 | } |
| 466 | //int64_t time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 467 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 468 | int ret = 0; |
| 469 | int got_frame = 0; |
| 470 | int32_t triesCount = 6; |
| 471 | while (!info->stopped && triesCount != 0) { |
| 472 | if (info->pkt.size == 0) { |
| 473 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 474 | //LOGD("got packet with size %d", info->pkt.size); |
| 475 | if (ret >= 0) { |
| 476 | info->orig_pkt = info->pkt; |
| 477 | } |
| 478 | } |
| 479 |
| 480 | if (info->pkt.size > 0) { |
| 481 | ret = decode_packet(info, &got_frame); |
| 482 | if (ret < 0) { |
| 483 | if (info->has_decoded_frames) { |
| 484 | ret = 0; |
| 485 | } |
| 486 | info->pkt.size = 0; |
| 487 | } else { |
| 488 | //LOGD("read size %d from packet", ret); |
| 489 | info->pkt.data += ret; |
| 490 | info->pkt.size -= ret; |
| 491 | } |
| 492 |
| 493 | if (info->pkt.size == 0) { |
| 494 | av_packet_unref(&info->orig_pkt); |
| 495 | } |
| 496 | } else { |
| 497 | info->pkt.data = NULL; |
| 498 | info->pkt.size = 0; |
| 499 | ret = decode_packet(info, &got_frame); |
| 500 | if (ret < 0) { |
| 501 | LOGE("can't decode packet flushed %s", info->src); |
| 502 | return 0; |
| 503 | } |
| 504 | if (got_frame == 0) { |
| 505 | if (info->has_decoded_frames) { |
| 506 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 507 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 508 | return 0; |
| 509 | } else { |
| 510 | avcodec_flush_buffers(info->video_dec_ctx); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | if (ret < 0 || info->seeking) { |
| 516 | return 0; |
| 517 | } |
| 518 | if (got_frame) { |
| 519 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 520 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 521 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 522 | int32_t wantedWidth; |
| 523 | int32_t wantedHeight; |
| 524 | if (dataArr != nullptr) { |
| 525 | wantedWidth = dataArr[0]; |
| 526 | wantedHeight = dataArr[1]; |
| 527 | if (info->frame->pts != AV_NOPTS_VALUE) { |
| 528 | dataArr[3] = (jint) (1000 * info->frame->pts * av_q2d(info->video_stream->time_base)); |
| 529 | } else { |
| 530 | dataArr[3] = (jint) (1000 * info->frame->pkt_dts * av_q2d(info->video_stream->time_base)); |
| 531 | } |
| 532 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 533 | } else { |
| 534 | AndroidBitmapInfo bitmapInfo; |
| 535 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 536 | wantedWidth = bitmapInfo.width; |
| 537 | wantedHeight = bitmapInfo.height; |
| 538 | } |
| 539 |
| 540 | void *pixels; |
| 541 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 542 | if (wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width) { |
| 543 | if (info->sws_ctx == nullptr) { |
| 544 | if (info->frame->format > AV_PIX_FMT_NONE && info->frame->format < AV_PIX_FMT_NB) { |
| 545 | info->sws_ctx = sws_getContext(info->frame->width, info->frame->height, (AVPixelFormat) info->frame->format, info->frame->width, info->frame->height, AV_PIX_FMT_RGBA, SWS_BILINEAR, NULL, NULL, NULL); |
| 546 | } else if (info->video_dec_ctx->pix_fmt > AV_PIX_FMT_NONE && info->video_dec_ctx->pix_fmt < AV_PIX_FMT_NB) { |
| 547 | info->sws_ctx = sws_getContext(info->video_dec_ctx->width, info->video_dec_ctx->height, info->video_dec_ctx->pix_fmt, info->video_dec_ctx->width, info->video_dec_ctx->height, AV_PIX_FMT_RGBA, SWS_BILINEAR, NULL, NULL, NULL); |
| 548 | } |
| 549 | } |
| 550 | if (info->sws_ctx == nullptr || ((intptr_t) pixels) % 16 != 0) { |
| 551 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 552 | if (info->frame->colorspace == AVColorSpace::AVCOL_SPC_BT709) { |
| 553 | 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, info->frame->width * 4, info->frame->width, info->frame->height); |
| 554 | } else { |
| 555 | 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, info->frame->width * 4, info->frame->width, info->frame->height); |
| 556 | } |
| 557 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 558 | libyuv::ABGRToARGB(info->frame->data[0], info->frame->linesize[0], (uint8_t *) pixels, info->frame->width * 4, info->frame->width, info->frame->height); |
| 559 | } |
| 560 | } else { |
| 561 | info->dst_data[0] = (uint8_t *) pixels; |
| 562 | info->dst_linesize[0] = stride; |
| 563 | sws_scale(info->sws_ctx, info->frame->data, info->frame->linesize, 0, info->frame->height, info->dst_data, info->dst_linesize); |
| 564 | } |
| 565 | } |
| 566 | AndroidBitmap_unlockPixels(env, bitmap); |
| 567 | } |
| 568 | } |
| 569 | info->has_decoded_frames = true; |
| 570 | av_frame_unref(info->frame); |
| 571 |
| 572 | //LOGD("frame time %lld ms", ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis() - time); |
| 573 |
| 574 | return 1; |
| 575 | } |
| 576 | if (!info->has_decoded_frames) { |
| 577 | triesCount--; |
| 578 | } |
| 579 | } |
| 580 | return 0; |
| 581 | } |
| 582 |
| 583 | jint videoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 584 | jclass_AnimatedFileDrawableStream = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/AnimatedFileDrawableStream")); |
| 585 | if (jclass_AnimatedFileDrawableStream == 0) { |
| 586 | return JNI_FALSE; |
| 587 | } |
| 588 | jclass_AnimatedFileDrawableStream_read = env->GetMethodID(jclass_AnimatedFileDrawableStream, "read", "(II)I"); |
| 589 | if (jclass_AnimatedFileDrawableStream_read == 0) { |
| 590 | return JNI_FALSE; |
| 591 | } |
| 592 | jclass_AnimatedFileDrawableStream_cancel = env->GetMethodID(jclass_AnimatedFileDrawableStream, "cancel", "()V"); |
| 593 | if (jclass_AnimatedFileDrawableStream_cancel == 0) { |
| 594 | return JNI_FALSE; |
| 595 | } |
| 596 |
| 597 | return JNI_TRUE; |
| 598 | } |
| 599 | } |
| 600 |