| 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 | #include "sws_context_holder.h" |
| 17 |
| 18 | extern "C" { |
| 19 | #include <libavformat/avformat.h> |
| 20 | #include <libavformat/isom.h> |
| 21 | #include <libavcodec/bytestream.h> |
| 22 | #include <libavcodec/get_bits.h> |
| 23 | #include <libavcodec/golomb.h> |
| 24 | #include <libavutil/eval.h> |
| 25 | #include <libavutil/intmath.h> |
| 26 | #include <libswscale/swscale.h> |
| 27 | } |
| 28 |
| 29 | #define RGB8888_A(p) ((p & (0xff<<24)) >> 24 ) |
| 30 |
| 31 | static const std::string av_make_error_str(int errnum) { |
| 32 | char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 33 | av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); |
| 34 | return (std::string) errbuf; |
| 35 | } |
| 36 |
| 37 | #undef av_err2str |
| 38 | #define av_err2str(errnum) av_make_error_str(errnum).c_str() |
| 39 | #define FFMPEG_AVSEEK_SIZE 0x10000 |
| 40 |
| 41 | jclass jclass_AnimatedFileDrawableStream; |
| 42 | jmethodID jclass_AnimatedFileDrawableStream_read; |
| 43 | jmethodID jclass_AnimatedFileDrawableStream_cancel; |
| 44 | jmethodID jclass_AnimatedFileDrawableStream_isCanceled; |
| 45 | jmethodID jclass_AnimatedFileDrawableStream_isFinishedLoadingFile; |
| 46 | jmethodID jclass_AnimatedFileDrawableStream_getFinishedFilePath; |
| 47 |
| 48 | typedef struct VideoInfo { |
| 49 |
| 50 | ~VideoInfo() { |
| 51 | if (video_dec_ctx) { |
| 52 | avcodec_close(video_dec_ctx); |
| 53 | video_dec_ctx = nullptr; |
| 54 | } |
| 55 | if (fmt_ctx) { |
| 56 | avformat_close_input(&fmt_ctx); |
| 57 | fmt_ctx = nullptr; |
| 58 | } |
| 59 | if (frame) { |
| 60 | av_frame_free(&frame); |
| 61 | frame = nullptr; |
| 62 | } |
| 63 | if (src) { |
| 64 | delete [] src; |
| 65 | src = nullptr; |
| 66 | } |
| 67 | if (stream != nullptr) { |
| 68 | JNIEnv *jniEnv = nullptr; |
| 69 | JavaVMAttachArgs jvmArgs; |
| 70 | jvmArgs.version = JNI_VERSION_1_6; |
| 71 |
| 72 | bool attached; |
| 73 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 74 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 75 | attached = true; |
| 76 | } else { |
| 77 | attached = false; |
| 78 | } |
| 79 | DEBUG_DELREF("gifvideo.cpp stream"); |
| 80 | jniEnv->DeleteGlobalRef(stream); |
| 81 | if (attached) { |
| 82 | javaVm->DetachCurrentThread(); |
| 83 | } |
| 84 | stream = nullptr; |
| 85 | } |
| 86 | if (ioContext != nullptr) { |
| 87 | if (ioContext->buffer) { |
| 88 | av_freep(&ioContext->buffer); |
| 89 | } |
| 90 | avio_context_free(&ioContext); |
| 91 | ioContext = nullptr; |
| 92 | } |
| 93 | if (fd >= 0) { |
| 94 | close(fd); |
| 95 | fd = -1; |
| 96 | } |
| 97 |
| 98 | av_packet_unref(&orig_pkt); |
| 99 |
| 100 | video_stream_idx = -1; |
| 101 | video_stream = nullptr; |
| 102 | audio_stream = nullptr; |
| 103 | } |
| 104 |
| 105 | AVFormatContext *fmt_ctx = nullptr; |
| 106 | char *src = nullptr; |
| 107 | int video_stream_idx = -1; |
| 108 | AVStream *video_stream = nullptr; |
| 109 | AVStream *audio_stream = nullptr; |
| 110 | AVCodecContext *video_dec_ctx = nullptr; |
| 111 | AVFrame *frame = nullptr; |
| 112 | bool has_decoded_frames = false; |
| 113 | AVPacket pkt; |
| 114 | AVPacket orig_pkt; |
| 115 | bool stopped = false; |
| 116 | bool seeking = false; |
| 117 |
| 118 | int firstWidth = 0; |
| 119 | int firstHeight = 0; |
| 120 |
| 121 | bool dropFrames = false; |
| 122 |
| 123 | struct SwsContextHolder sws_ctx_holder; |
| 124 |
| 125 | AVIOContext *ioContext = nullptr; |
| 126 | unsigned char *ioBuffer = nullptr; |
| 127 | jobject stream = nullptr; |
| 128 | int32_t account = 0; |
| 129 | int fd = -1; |
| 130 | int64_t file_size = 0; |
| 131 | int64_t last_seek_p = 0; |
| 132 | }; |
| 133 |
| 134 | void custom_log(void *ptr, int level, const char* fmt, va_list vl){ |
| 135 | va_list vl2; |
| 136 | char line[1024]; |
| 137 | static int print_prefix = 1; |
| 138 |
| 139 | va_copy(vl2, vl); |
| 140 | av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix); |
| 141 | va_end(vl2); |
| 142 |
| 143 | LOGE(line); |
| 144 | } |
| 145 |
| 146 | static enum AVPixelFormat get_format(AVCodecContext *ctx, |
| 147 | const enum AVPixelFormat *pix_fmts) |
| 148 | { |
| 149 | const enum AVPixelFormat *p; |
| 150 |
| 151 | for (p = pix_fmts; *p != -1; p++) { |
| 152 | LOGE("available format %d", p); |
| 153 | } |
| 154 |
| 155 | return pix_fmts[0]; |
| 156 | } |
| 157 |
| 158 | int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 159 | int ret, stream_index; |
| 160 | AVStream *st; |
| 161 | AVCodec *dec = NULL; |
| 162 | AVDictionary *opts = NULL; |
| 163 |
| 164 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 165 | if (ret < 0) { |
| 166 | LOGE("can't find %s stream in input file", av_get_media_type_string(type)); |
| 167 | return ret; |
| 168 | } else { |
| 169 | stream_index = ret; |
| 170 | st = fmt_ctx->streams[stream_index]; |
| 171 |
| 172 | dec = avcodec_find_decoder(st->codecpar->codec_id); |
| 173 | if (!dec) { |
| 174 | LOGE("failed to find %d codec", st->codecpar->codec_id); |
| 175 | return AVERROR(EINVAL); |
| 176 | } |
| 177 |
| 178 | *dec_ctx = avcodec_alloc_context3(dec); |
| 179 | if (!*dec_ctx) { |
| 180 | LOGE("Failed to allocate the %s codec context", av_get_media_type_string(type)); |
| 181 | return AVERROR(ENOMEM); |
| 182 | } |
| 183 |
| 184 | if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { |
| 185 | LOGE("Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); |
| 186 | return ret; |
| 187 | } |
| 188 |
| 189 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 190 | if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { |
| 191 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 192 | return ret; |
| 193 | } |
| 194 | *stream_idx = stream_index; |
| 195 | } |
| 196 |
| 197 | return 0; |
| 198 | } |
| 199 |
| 200 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 201 | int ret = 0; |
| 202 | int decoded = info->pkt.size; |
| 203 | *got_frame = 0; |
| 204 |
| 205 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 206 | while (decoded > 0) { |
| 207 | ret = avcodec_send_packet(info->video_dec_ctx, &info->pkt); |
| 208 | if (ret < 0 && ret != AVERROR(EAGAIN)) { |
| 209 | return ret; |
| 210 | } |
| 211 | if (ret >= 0) { |
| 212 | ret = avcodec_receive_frame(info->video_dec_ctx, info->frame); |
| 213 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 214 | return 0; |
| 215 | } else if (ret < 0) { |
| 216 | return ret; |
| 217 | } |
| 218 | *got_frame = 1; |
| 219 | return info->pkt.size; |
| 220 | } |
| 221 | decoded = info->pkt.size; |
| 222 | } |
| 223 | } |
| 224 |
| 225 | return decoded; |
| 226 | } |
| 227 |
| 228 | void requestFd(VideoInfo *info) { |
| 229 | JNIEnv *jniEnv = nullptr; |
| 230 |
| 231 | JavaVMAttachArgs jvmArgs; |
| 232 | jvmArgs.version = JNI_VERSION_1_6; |
| 233 |
| 234 | bool attached; |
| 235 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 236 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 237 | attached = true; |
| 238 | } else { |
| 239 | attached = false; |
| 240 | } |
| 241 | jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) 0, (jint) 1); |
| 242 | jboolean loaded = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isFinishedLoadingFile); |
| 243 | if (loaded) { |
| 244 | delete[] info->src; |
| 245 | jstring src = (jstring) jniEnv->CallObjectMethod(info->stream, jclass_AnimatedFileDrawableStream_getFinishedFilePath); |
| 246 | char const *srcString = jniEnv->GetStringUTFChars(src, 0); |
| 247 | size_t len = strlen(srcString); |
| 248 | info->src = new char[len + 1]; |
| 249 | memcpy(info->src, srcString, len); |
| 250 | info->src[len] = '\0'; |
| 251 | if (srcString != 0) { |
| 252 | jniEnv->ReleaseStringUTFChars(src, srcString); |
| 253 | } |
| 254 | } |
| 255 |
| 256 | if (attached) { |
| 257 | javaVm->DetachCurrentThread(); |
| 258 | } |
| 259 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 260 | } |
| 261 |
| 262 | int readCallback(void *opaque, uint8_t *buf, int buf_size) { |
| 263 | VideoInfo *info = (VideoInfo *) opaque; |
| 264 | if (!info->stopped) { |
| 265 | if (info->fd < 0) { |
| 266 | requestFd(info); |
| 267 | } |
| 268 | if (info->fd >= 0) { |
| 269 | if (info->last_seek_p + buf_size > info->file_size) { |
| 270 | buf_size = (int) (info->file_size - info->last_seek_p); |
| 271 | } |
| 272 | if (buf_size > 0) { |
| 273 | JNIEnv *jniEnv = nullptr; |
| 274 |
| 275 | JavaVMAttachArgs jvmArgs; |
| 276 | jvmArgs.version = JNI_VERSION_1_6; |
| 277 |
| 278 | bool attached; |
| 279 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 280 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 281 | attached = true; |
| 282 | } else { |
| 283 | attached = false; |
| 284 | } |
| 285 |
| 286 | buf_size = jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) info->last_seek_p, (jint) buf_size); |
| 287 | info->last_seek_p += buf_size; |
| 288 | if (attached) { |
| 289 | javaVm->DetachCurrentThread(); |
| 290 | } |
| 291 | if (buf_size == 0) { |
| 292 | return AVERROR_EXIT; |
| 293 | } |
| 294 | int ret = (int) read(info->fd, buf, (size_t) buf_size); |
| 295 | if (ret <= 0) { |
| 296 | return AVERROR_EOF; |
| 297 | } |
| 298 | return ret; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | return AVERROR_EOF; |
| 303 | } |
| 304 |
| 305 | int64_t seekCallback(void *opaque, int64_t offset, int whence) { |
| 306 | VideoInfo *info = (VideoInfo *) opaque; |
| 307 | if (!info->stopped) { |
| 308 | if (info->fd < 0) { |
| 309 | requestFd(info); |
| 310 | } |
| 311 | if (info->fd >= 0) { |
| 312 | if (whence & FFMPEG_AVSEEK_SIZE) { |
| 313 | return info->file_size; |
| 314 | } else { |
| 315 | info->last_seek_p = offset; |
| 316 | lseek(info->fd, off_t(offset), SEEK_SET); |
| 317 | return offset; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 |
| 324 | enum PARAM_NUM { |
| 325 | PARAM_NUM_SUPPORTED_VIDEO_CODEC = 0, |
| 326 | PARAM_NUM_WIDTH = 1, |
| 327 | PARAM_NUM_HEIGHT = 2, |
| 328 | PARAM_NUM_BITRATE = 3, |
| 329 | PARAM_NUM_DURATION = 4, |
| 330 | PARAM_NUM_AUDIO_FRAME_SIZE = 5, |
| 331 | PARAM_NUM_VIDEO_FRAME_SIZE = 6, |
| 332 | PARAM_NUM_FRAMERATE = 7, |
| 333 | PARAM_NUM_ROTATION = 8, |
| 334 | PARAM_NUM_SUPPORTED_AUDIO_CODEC = 9, |
| 335 | PARAM_NUM_HAS_AUDIO = 10, |
| 336 | PARAM_NUM_COUNT = 11, |
| 337 | }; |
| 338 |
| 339 | struct OffsetIOContext { |
| 340 | int fd; |
| 341 | int64_t offset; |
| 342 | }; |
| 343 |
| 344 | static int offsetRead(void *opaque, uint8_t *buf, int size) { |
| 345 | OffsetIOContext *ctx = (OffsetIOContext *)opaque; |
| 346 | return read(ctx->fd, buf, size); |
| 347 | } |
| 348 |
| 349 | static int64_t offsetSeek(void *opaque, int64_t pos, int whence) { |
| 350 | OffsetIOContext *ctx = (OffsetIOContext *)opaque; |
| 351 | if (whence == AVSEEK_SIZE) return -1; // unknown size, FFmpeg handles this |
| 352 | return lseek(ctx->fd, ctx->offset + pos, whence); |
| 353 | } |
| 354 |
| 355 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoInfo(JNIEnv *env, jclass clazz, jint sdkVersion, jstring src, jintArray data, jlong fileOffset) { |
| 356 | VideoInfo *info = new VideoInfo(); |
| 357 |
| 358 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 359 | size_t len = strlen(srcString); |
| 360 | info->src = new char[len + 1]; |
| 361 | memcpy(info->src, srcString, len); |
| 362 | info->src[len] = '\0'; |
| 363 | if (srcString != nullptr) { |
| 364 | env->ReleaseStringUTFChars(src, srcString); |
| 365 | } |
| 366 |
| 367 | int ret; |
| 368 | if (fileOffset > 0) { |
| 369 | int fd = open(info->src, O_RDONLY); |
| 370 | if (fd < 0) { |
| 371 | LOGE("can't open source file %s", info->src); |
| 372 | delete info; |
| 373 | return; |
| 374 | } |
| 375 | if (lseek(fd, fileOffset, SEEK_SET) < 0) { |
| 376 | LOGE("can't seek to offset %lld in file %s", (long long)fileOffset, info->src); |
| 377 | close(fd); |
| 378 | delete info; |
| 379 | return; |
| 380 | } |
| 381 |
| 382 | OffsetIOContext *ioCtx = new OffsetIOContext{fd, fileOffset}; |
| 383 | uint8_t *ioBuf = (uint8_t *)av_malloc(32 * 1024); |
| 384 | AVIOContext *avio = avio_alloc_context(ioBuf, 32 * 1024, 0, ioCtx, offsetRead, nullptr, offsetSeek); |
| 385 |
| 386 | info->fmt_ctx = avformat_alloc_context(); |
| 387 | info->fmt_ctx->pb = avio; |
| 388 |
| 389 | if ((ret = avformat_open_input(&info->fmt_ctx, nullptr, nullptr, nullptr)) < 0) { |
| 390 | LOGE("can't open source file at offset %s (offset=%lld), %s", info->src, fileOffset, av_err2str(ret)); |
| 391 | info->fmt_ctx = nullptr; |
| 392 | if (avio) { |
| 393 | av_freep(&avio->buffer); |
| 394 | avio_context_free(&avio); |
| 395 | } |
| 396 | close(fd); |
| 397 | delete ioCtx; |
| 398 | delete info; |
| 399 | return; |
| 400 | } |
| 401 | } else { |
| 402 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, nullptr, nullptr)) < 0) { |
| 403 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 404 | delete info; |
| 405 | return; |
| 406 | } |
| 407 | } |
| 408 |
| 409 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 410 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 411 | delete info; |
| 412 | return; |
| 413 | } |
| 414 |
| 415 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0)) >= 0) { |
| 416 | info->video_stream = info->fmt_ctx->streams[ret]; |
| 417 | } |
| 418 |
| 419 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) >= 0) { |
| 420 | info->audio_stream = info->fmt_ctx->streams[ret]; |
| 421 | } |
| 422 |
| 423 | if (info->video_stream == nullptr) { |
| 424 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 425 | delete info; |
| 426 | return; |
| 427 | } |
| 428 |
| 429 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 430 | if (dataArr != nullptr) { |
| 431 | //https://developer.android.com/guide/topics/media/media-formats |
| 432 | dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC] = |
| 433 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || |
| 434 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H263 || |
| 435 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_MPEG4 || |
| 436 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP8 || |
| 437 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP9 || |
| 438 | (sdkVersion > 21 && info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC); |
| 439 |
| 440 | if (strstr(info->fmt_ctx->iformat->name, "mov") != 0 && dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC]) { |
| 441 | MOVStreamContext *mov = (MOVStreamContext *) info->video_stream->priv_data; |
| 442 | dataArr[PARAM_NUM_VIDEO_FRAME_SIZE] = (jint) mov->data_size; |
| 443 |
| 444 | if (info->audio_stream != nullptr) { |
| 445 | mov = (MOVStreamContext *) info->audio_stream->priv_data; |
| 446 | dataArr[PARAM_NUM_AUDIO_FRAME_SIZE] = (jint) mov->data_size; |
| 447 | } |
| 448 | } |
| 449 |
| 450 | if (info->audio_stream != nullptr) { |
| 451 | //https://developer.android.com/guide/topics/media/media-formats |
| 452 | dataArr[PARAM_NUM_SUPPORTED_AUDIO_CODEC] = |
| 453 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC || |
| 454 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC_LATM || |
| 455 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_VORBIS || |
| 456 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_NB || |
| 457 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_WB || |
| 458 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_FLAC || |
| 459 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_MP3 || |
| 460 | // not supported codec, skip audio in this case |
| 461 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV || |
| 462 | (sdkVersion > 21 && info->audio_stream->codecpar->codec_id == AV_CODEC_ID_OPUS); |
| 463 | dataArr[PARAM_NUM_HAS_AUDIO] = 1; |
| 464 | } else { |
| 465 | dataArr[PARAM_NUM_HAS_AUDIO] = 0; |
| 466 | } |
| 467 |
| 468 | dataArr[PARAM_NUM_BITRATE] = (jint) info->video_stream->codecpar->bit_rate; |
| 469 | dataArr[PARAM_NUM_WIDTH] = info->video_stream->codecpar->width; |
| 470 | dataArr[PARAM_NUM_HEIGHT] = info->video_stream->codecpar->height; |
| 471 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 472 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0") != 0) { |
| 473 | char *tail; |
| 474 | dataArr[PARAM_NUM_ROTATION] = (jint) av_strtod(rotate_tag->value, &tail); |
| 475 | if (*tail) { |
| 476 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 477 | } |
| 478 | } else { |
| 479 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 480 | } |
| 481 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC) { |
| 482 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->avg_frame_rate); |
| 483 | } else { |
| 484 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->r_frame_rate); |
| 485 | } |
| 486 | dataArr[PARAM_NUM_DURATION] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 487 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 488 | delete info; |
| 489 | } |
| 490 | } |
| 491 |
| 492 | 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) { |
| 493 | VideoInfo *info = new VideoInfo(); |
| 494 |
| 495 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 496 | size_t len = strlen(srcString); |
| 497 | info->src = new char[len + 1]; |
| 498 | memcpy(info->src, srcString, len); |
| 499 | info->src[len] = '\0'; |
| 500 | if (srcString != 0) { |
| 501 | env->ReleaseStringUTFChars(src, srcString); |
| 502 | } |
| 503 |
| 504 | int ret; |
| 505 | if (streamFileSize != 0) { |
| 506 | info->file_size = streamFileSize; |
| 507 | DEBUG_REF("gifvideo.cpp new stream"); |
| 508 | info->stream = env->NewGlobalRef(stream); |
| 509 | info->account = account; |
| 510 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 511 |
| 512 | info->ioBuffer = (unsigned char *) av_malloc(64 * 1024); |
| 513 | info->ioContext = avio_alloc_context(info->ioBuffer, 64 * 1024, 0, info, readCallback, nullptr, seekCallback); |
| 514 | if (info->ioContext == nullptr) { |
| 515 | delete info; |
| 516 | return 0; |
| 517 | } |
| 518 |
| 519 | info->fmt_ctx = avformat_alloc_context(); |
| 520 | info->fmt_ctx->pb = info->ioContext; |
| 521 |
| 522 | AVDictionary *options = NULL; |
| 523 | av_dict_set(&options, "usetoc", "1", 0); |
| 524 | ret = avformat_open_input(&info->fmt_ctx, "http://localhost/file", NULL, &options); |
| 525 | av_dict_free(&options); |
| 526 | if (ret < 0) { |
| 527 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 528 | delete info; |
| 529 | return 0; |
| 530 | } |
| 531 | info->fmt_ctx->flags |= AVFMT_FLAG_FAST_SEEK; |
| 532 | if (preview) { |
| 533 | info->fmt_ctx->flags |= AVFMT_FLAG_NOBUFFER; |
| 534 | } |
| 535 | } else { |
| 536 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 537 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 538 | delete info; |
| 539 | return 0; |
| 540 | } |
| 541 | } |
| 542 |
| 543 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 544 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 545 | delete info; |
| 546 | return 0; |
| 547 | } |
| 548 |
| 549 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 550 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 551 | } |
| 552 |
| 553 | if (info->video_stream == nullptr) { |
| 554 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 555 | delete info; |
| 556 | return 0; |
| 557 | } |
| 558 |
| 559 | info->frame = av_frame_alloc(); |
| 560 | if (info->frame == nullptr) { |
| 561 | LOGE("can't allocate frame %s", info->src); |
| 562 | delete info; |
| 563 | return 0; |
| 564 | } |
| 565 |
| 566 | av_init_packet(&info->pkt); |
| 567 | info->pkt.data = NULL; |
| 568 | info->pkt.size = 0; |
| 569 |
| 570 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 571 | if (dataArr != nullptr) { |
| 572 | dataArr[0] = info->video_dec_ctx->width; |
| 573 | dataArr[1] = info->video_dec_ctx->height; |
| 574 | //float pixelWidthHeightRatio = info->video_dec_ctx->sample_aspect_ratio.num / info->video_dec_ctx->sample_aspect_ratio.den; TODO support |
| 575 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 576 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 577 | char *tail; |
| 578 | dataArr[2] = (jint) av_strtod(rotate_tag->value, &tail); |
| 579 | if (*tail) { |
| 580 | dataArr[2] = 0; |
| 581 | } |
| 582 | } else { |
| 583 | dataArr[2] = 0; |
| 584 | } |
| 585 | dataArr[4] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 586 | int video_stream_index = -1; |
| 587 | double fps = 30.0; |
| 588 | for (int i = 0; i < info->fmt_ctx->nb_streams; i++) { |
| 589 | if (info->fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 590 | video_stream_index = i; |
| 591 | break; |
| 592 | } |
| 593 | } |
| 594 | if (video_stream_index != -1) { |
| 595 | AVStream* video_stream = info->fmt_ctx->streams[video_stream_index]; |
| 596 | if (video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num) { |
| 597 | fps = av_q2d(video_stream->avg_frame_rate); |
| 598 | } else if(video_stream->r_frame_rate.den && video_stream->r_frame_rate.num) { |
| 599 | fps = av_q2d(video_stream->r_frame_rate); |
| 600 | } else { |
| 601 | int ticks = video_stream->codec->ticks_per_frame; |
| 602 | fps = 1.0 / (ticks * av_q2d(video_stream->time_base)); |
| 603 | } |
| 604 | } |
| 605 | dataArr[5] = (int32_t) fps; |
| 606 | //(int32_t) (1000 * info->video_stream->duration * av_q2d(info->video_stream->time_base)); |
| 607 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 608 | } |
| 609 |
| 610 | //LOGD("successfully opened file %s", info->src); |
| 611 |
| 612 | return (jlong) (intptr_t) info; |
| 613 | } |
| 614 |
| 615 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 616 | if (ptr == NULL) { |
| 617 | return; |
| 618 | } |
| 619 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 620 | if (info->stream != nullptr) { |
| 621 | JNIEnv *jniEnv = nullptr; |
| 622 | JavaVMAttachArgs jvmArgs; |
| 623 | jvmArgs.version = JNI_VERSION_1_6; |
| 624 |
| 625 | bool attached; |
| 626 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 627 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 628 | attached = true; |
| 629 | } else { |
| 630 | attached = false; |
| 631 | } |
| 632 | jniEnv->CallVoidMethod(info->stream, jclass_AnimatedFileDrawableStream_cancel); |
| 633 | if (attached) { |
| 634 | javaVm->DetachCurrentThread(); |
| 635 | } |
| 636 | } |
| 637 | delete info; |
| 638 | } |
| 639 |
| 640 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_stopDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 641 | if (ptr == NULL) { |
| 642 | return; |
| 643 | } |
| 644 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 645 | info->stopped = true; |
| 646 | } |
| 647 |
| 648 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_prepareToSeek(JNIEnv *env, jclass clazz, jlong ptr) { |
| 649 | if (ptr == NULL) { |
| 650 | return; |
| 651 | } |
| 652 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 653 | info->seeking = true; |
| 654 | } |
| 655 |
| 656 | void push_time(JNIEnv *env, VideoInfo* info, jintArray data) { |
| 657 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 658 | dataArr[3] = (jint) (1000 * info->frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 659 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 660 | } |
| 661 |
| 662 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_seekToMs(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jintArray data, jboolean precise) { |
| 663 | if (ptr == NULL) { |
| 664 | return; |
| 665 | } |
| 666 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 667 | info->seeking = false; |
| 668 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 669 | int ret = 0; |
| 670 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 671 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 672 | return; |
| 673 | } else { |
| 674 | avcodec_flush_buffers(info->video_dec_ctx); |
| 675 | if (!precise) { |
| 676 | push_time(env, info, data); |
| 677 | return; |
| 678 | } |
| 679 | int got_frame = 0; |
| 680 | int32_t tries = 100; |
| 681 | while (tries > 0) { |
| 682 | if (info->pkt.size == 0) { |
| 683 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 684 | if (ret >= 0) { |
| 685 | info->orig_pkt = info->pkt; |
| 686 | } |
| 687 | } |
| 688 |
| 689 | if (info->pkt.size > 0) { |
| 690 | ret = decode_packet(info, &got_frame); |
| 691 | if (ret < 0) { |
| 692 | if (info->has_decoded_frames) { |
| 693 | ret = 0; |
| 694 | } |
| 695 | info->pkt.size = 0; |
| 696 | } else { |
| 697 | info->pkt.data += ret; |
| 698 | info->pkt.size -= ret; |
| 699 | } |
| 700 | if (info->pkt.size == 0) { |
| 701 | av_packet_unref(&info->orig_pkt); |
| 702 | } |
| 703 | } else { |
| 704 | info->pkt.data = NULL; |
| 705 | info->pkt.size = 0; |
| 706 | ret = decode_packet(info, &got_frame); |
| 707 | if (ret < 0) { |
| 708 | push_time(env, info, data); |
| 709 | return; |
| 710 | } |
| 711 | if (got_frame == 0) { |
| 712 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 713 | push_time(env, info, data); |
| 714 | return; |
| 715 | } |
| 716 | } |
| 717 | if (ret < 0) { |
| 718 | push_time(env, info, data); |
| 719 | return; |
| 720 | } |
| 721 | if (got_frame) { |
| 722 | info->has_decoded_frames = true; |
| 723 | bool finished = false; |
| 724 | 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) { |
| 725 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 726 | if (pkt_pts >= pts) { |
| 727 | finished = true; |
| 728 | } |
| 729 | } |
| 730 | av_frame_unref(info->frame); |
| 731 | if (finished) { |
| 732 | push_time(env, info, data); |
| 733 | return; |
| 734 | } |
| 735 | } |
| 736 | tries--; |
| 737 | } |
| 738 | } |
| 739 | push_time(env, info, data); |
| 740 | } |
| 741 |
| 742 | uint32_t premultiply_channel_value(const uint32_t pixel, const uint8_t offset, const float normalizedAlpha) { |
| 743 | auto multipliedValue = ((pixel >> offset) & 0xFF) * normalizedAlpha; |
| 744 | return ((uint32_t)std::min(multipliedValue, 255.0f)) << offset; |
| 745 | } |
| 746 |
| 747 | static inline void writeFrameToBitmap(JNIEnv *env, VideoInfo *info, jintArray data, jobject bitmap) { |
| 748 | if (env->IsSameObject(bitmap, NULL)) { |
| 749 | push_time(env, info, data); |
| 750 | return; |
| 751 | } |
| 752 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 753 | int32_t wantedWidth; |
| 754 | int32_t wantedHeight; |
| 755 |
| 756 | AndroidBitmapInfo bitmapInfo; |
| 757 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 758 | int32_t bitmapWidth = bitmapInfo.width; |
| 759 | int32_t bitmapHeight = bitmapInfo.height; |
| 760 | int32_t bitmapStride = bitmapInfo.stride; |
| 761 |
| 762 | if (dataArr != nullptr) { |
| 763 | wantedWidth = dataArr[0]; |
| 764 | wantedHeight = dataArr[1]; |
| 765 | dataArr[3] = (jint) (1000 * info->frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 766 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 767 | } else { |
| 768 | wantedWidth = bitmapWidth; |
| 769 | wantedHeight = bitmapHeight; |
| 770 | } |
| 771 |
| 772 | if (!(wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width)) { |
| 773 | return; |
| 774 | } |
| 775 |
| 776 | void *pixels; |
| 777 | if (__builtin_expect(AndroidBitmap_lockPixels(env, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 778 | return; |
| 779 | } |
| 780 |
| 781 | SwsContext* sws_ctx = nullptr; |
| 782 | if (info->frame->format > AV_PIX_FMT_NONE && info->frame->format < AV_PIX_FMT_NB && info->frame->format != AV_PIX_FMT_YUVA420P) { |
| 783 | sws_ctx = info->sws_ctx_holder.get( |
| 784 | info->frame->width, |
| 785 | info->frame->height, |
| 786 | (AVPixelFormat) info->frame->format, |
| 787 | bitmapWidth, |
| 788 | bitmapHeight, |
| 789 | AV_PIX_FMT_RGBA); |
| 790 | } 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) { |
| 791 | sws_ctx = info->sws_ctx_holder.get( |
| 792 | info->video_dec_ctx->width, |
| 793 | info->video_dec_ctx->height, |
| 794 | info->video_dec_ctx->pix_fmt, |
| 795 | bitmapWidth, |
| 796 | bitmapHeight, |
| 797 | AV_PIX_FMT_RGBA); |
| 798 | } |
| 799 |
| 800 | if (sws_ctx != nullptr && ((intptr_t) pixels) % 16 == 0) { |
| 801 | uint8_t __attribute__ ((aligned (16))) *dst_data[1]; |
| 802 | dst_data[0] = (uint8_t *) pixels; |
| 803 |
| 804 | int32_t dst_stride[1]; |
| 805 | dst_stride[0] = bitmapStride; |
| 806 | sws_scale(sws_ctx, |
| 807 | info->frame->data, |
| 808 | info->frame->linesize, |
| 809 | 0, |
| 810 | info->frame->height, |
| 811 | dst_data, |
| 812 | dst_stride |
| 813 | ); |
| 814 | } else if (info->frame->width == bitmapWidth && info->frame->height == bitmapHeight) { |
| 815 | if (info->frame->format == AV_PIX_FMT_YUVA420P) { |
| 816 | libyuv::I420AlphaToARGBMatrix( |
| 817 | info->frame->data[0], info->frame->linesize[0], |
| 818 | info->frame->data[2], info->frame->linesize[2], |
| 819 | info->frame->data[1], info->frame->linesize[1], |
| 820 | info->frame->data[3], info->frame->linesize[3], |
| 821 | (uint8_t *) pixels, |
| 822 | bitmapStride, |
| 823 | &libyuv::kYvuI601Constants, |
| 824 | bitmapWidth, |
| 825 | bitmapHeight, |
| 826 | 1 |
| 827 | ); |
| 828 | } else if (info->frame->format == AV_PIX_FMT_YUV444P) { |
| 829 | libyuv::H444ToARGB( |
| 830 | info->frame->data[0], info->frame->linesize[0], |
| 831 | info->frame->data[2], info->frame->linesize[2], |
| 832 | info->frame->data[1], info->frame->linesize[1], |
| 833 | (uint8_t *) pixels, |
| 834 | bitmapStride, |
| 835 | bitmapWidth, |
| 836 | bitmapHeight |
| 837 | ); |
| 838 | } else if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 839 | if (info->frame->colorspace == AVColorSpace::AVCOL_SPC_BT709) { |
| 840 | libyuv::H420ToARGB( |
| 841 | info->frame->data[0], info->frame->linesize[0], |
| 842 | info->frame->data[2], info->frame->linesize[2], |
| 843 | info->frame->data[1], info->frame->linesize[1], |
| 844 | (uint8_t *) pixels, |
| 845 | bitmapStride, |
| 846 | bitmapWidth, |
| 847 | bitmapHeight |
| 848 | ); |
| 849 | } else { |
| 850 | libyuv::I420ToARGB( |
| 851 | info->frame->data[0], info->frame->linesize[0], |
| 852 | info->frame->data[2], info->frame->linesize[2], |
| 853 | info->frame->data[1], info->frame->linesize[1], |
| 854 | (uint8_t *) pixels, |
| 855 | bitmapStride, |
| 856 | bitmapWidth, |
| 857 | bitmapHeight |
| 858 | ); |
| 859 | } |
| 860 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 861 | libyuv::ABGRToARGB( |
| 862 | info->frame->data[0], info->frame->linesize[0], |
| 863 | (uint8_t *) pixels, |
| 864 | bitmapStride, |
| 865 | bitmapWidth, |
| 866 | bitmapHeight |
| 867 | ); |
| 868 | } |
| 869 | } |
| 870 |
| 871 | AndroidBitmap_unlockPixels(env, bitmap); |
| 872 | } |
| 873 |
| 874 | extern "C" JNIEXPORT int JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getFrameAtTime(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jobject bitmap, jintArray data) { |
| 875 | if (ptr == NULL || bitmap == nullptr || data == nullptr) { |
| 876 | return 0; |
| 877 | } |
| 878 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 879 | info->seeking = false; |
| 880 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 881 | int ret = 0; |
| 882 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 883 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 884 | return 0; |
| 885 | } else { |
| 886 | avcodec_flush_buffers(info->video_dec_ctx); |
| 887 | int got_frame = 0; |
| 888 | int32_t tries = 1000; |
| 889 | bool readNextPacket = true; |
| 890 | while (tries > 0) { |
| 891 | if (info->stream != nullptr) { |
| 892 | JNIEnv *jniEnv = nullptr; |
| 893 | JavaVMAttachArgs jvmArgs; |
| 894 | jvmArgs.version = JNI_VERSION_1_6; |
| 895 |
| 896 | bool attached; |
| 897 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 898 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 899 | attached = true; |
| 900 | } else { |
| 901 | attached = false; |
| 902 | } |
| 903 | jboolean canceled = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isCanceled); |
| 904 | if (attached) { |
| 905 | javaVm->DetachCurrentThread(); |
| 906 | } |
| 907 | if (canceled) { |
| 908 | return 0; |
| 909 | } |
| 910 | } |
| 911 | if (info->pkt.size == 0 && readNextPacket) { |
| 912 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 913 | if (ret >= 0) { |
| 914 | info->orig_pkt = info->pkt; |
| 915 | } |
| 916 | } |
| 917 |
| 918 | if (info->pkt.size > 0) { |
| 919 | ret = decode_packet(info, &got_frame); |
| 920 | if (ret < 0) { |
| 921 | if (info->has_decoded_frames) { |
| 922 | ret = 0; |
| 923 | } |
| 924 | info->pkt.size = 0; |
| 925 | } else { |
| 926 | info->pkt.data += ret; |
| 927 | info->pkt.size -= ret; |
| 928 | } |
| 929 | if (info->pkt.size == 0) { |
| 930 | av_packet_unref(&info->orig_pkt); |
| 931 | } |
| 932 | } else { |
| 933 | info->pkt.data = NULL; |
| 934 | info->pkt.size = 0; |
| 935 | ret = decode_packet(info, &got_frame); |
| 936 | if (ret < 0) { |
| 937 | return 0; |
| 938 | } |
| 939 | if (got_frame == 0) { |
| 940 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 941 | return 0; |
| 942 | } |
| 943 | } |
| 944 | if (ret < 0) { |
| 945 | return 0; |
| 946 | } |
| 947 | if (got_frame) { |
| 948 | bool finished = false; |
| 949 | 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) { |
| 950 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 951 | bool isLastPacket = false; |
| 952 | if (info->pkt.size == 0) { |
| 953 | readNextPacket = false; |
| 954 | isLastPacket = av_read_frame(info->fmt_ctx, &info->pkt) < 0; |
| 955 | } |
| 956 | if (pkt_pts >= pts || isLastPacket) { |
| 957 | writeFrameToBitmap(env, info, data, bitmap); |
| 958 | finished = true; |
| 959 | } |
| 960 | } |
| 961 | av_frame_unref(info->frame); |
| 962 | if (finished) { |
| 963 | return 1; |
| 964 | } |
| 965 | } else { |
| 966 | readNextPacket = true; |
| 967 | } |
| 968 | tries--; |
| 969 | } |
| 970 | return 0; |
| 971 | } |
| 972 | } |
| 973 |
| 974 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jintArray data, jboolean preview, jfloat start_time, jfloat end_time, jboolean loop) { |
| 975 | if (ptr == NULL) { |
| 976 | return 0; |
| 977 | } |
| 978 | //int64_t time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 979 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 980 | int ret = 0; |
| 981 | int got_frame = 0; |
| 982 | int32_t triesCount = preview ? 50 : 6; |
| 983 | //info->has_decoded_frames = false; |
| 984 | while (!info->stopped && triesCount != 0) { |
| 985 | if (info->stream != nullptr) { |
| 986 | JNIEnv *jniEnv = nullptr; |
| 987 | JavaVMAttachArgs jvmArgs; |
| 988 | jvmArgs.version = JNI_VERSION_1_6; |
| 989 |
| 990 | bool attached; |
| 991 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 992 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 993 | attached = true; |
| 994 | } else { |
| 995 | attached = false; |
| 996 | } |
| 997 | jboolean canceled = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isCanceled); |
| 998 | if (attached) { |
| 999 | javaVm->DetachCurrentThread(); |
| 1000 | } |
| 1001 | if (canceled) { |
| 1002 | return 0; |
| 1003 | } |
| 1004 | } |
| 1005 | if (info->pkt.size == 0) { |
| 1006 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1007 | if (ret >= 0) { |
| 1008 | double pts = info->pkt.pts * av_q2d(info->video_stream->time_base); |
| 1009 | if (end_time > 0 && info->pkt.stream_index == info->video_stream_idx && pts > end_time) { |
| 1010 | av_packet_unref(&info->pkt); |
| 1011 | info->pkt.data = NULL; |
| 1012 | info->pkt.size = 0; |
| 1013 | } else { |
| 1014 | info->orig_pkt = info->pkt; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 |
| 1019 | if (info->pkt.size > 0) { |
| 1020 | ret = decode_packet(info, &got_frame); |
| 1021 | if (ret < 0) { |
| 1022 | if (info->has_decoded_frames) { |
| 1023 | ret = 0; |
| 1024 | } |
| 1025 | info->pkt.size = 0; |
| 1026 | } else { |
| 1027 | //LOGD("read size %d from packet", ret); |
| 1028 | info->pkt.data += ret; |
| 1029 | info->pkt.size -= ret; |
| 1030 | } |
| 1031 |
| 1032 | if (info->pkt.size == 0) { |
| 1033 | av_packet_unref(&info->orig_pkt); |
| 1034 | } |
| 1035 | } else { |
| 1036 | info->pkt.data = NULL; |
| 1037 | info->pkt.size = 0; |
| 1038 | ret = decode_packet(info, &got_frame); |
| 1039 | if (ret < 0) { |
| 1040 | LOGE("can't decode packet flushed %s", info->src); |
| 1041 | return 0; |
| 1042 | } |
| 1043 | if (!preview && got_frame == 0 && info->has_decoded_frames) { |
| 1044 | if (!loop) { |
| 1045 | return 0; |
| 1046 | } |
| 1047 | int64_t start_from = 0; |
| 1048 | if (start_time > 0) { |
| 1049 | start_from = (int64_t)(start_time / av_q2d(info->video_stream->time_base)); |
| 1050 | } |
| 1051 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, start_from, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1052 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 1053 | return 0; |
| 1054 | } else { |
| 1055 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | if (ret < 0 || info->seeking) { |
| 1060 | return 0; |
| 1061 | } |
| 1062 | if (got_frame) { |
| 1063 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 1064 | if (bitmap != nullptr && (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)) { |
| 1065 | writeFrameToBitmap(env, info, data, bitmap); |
| 1066 | } |
| 1067 | info->has_decoded_frames = true; |
| 1068 | push_time(env, info, data); |
| 1069 | av_frame_unref(info->frame); |
| 1070 | return 1; |
| 1071 | } |
| 1072 | if (!info->has_decoded_frames) { |
| 1073 | triesCount--; |
| 1074 | } |
| 1075 | } |
| 1076 | return 0; |
| 1077 | } |
| 1078 |
| 1079 | extern "C" jint videoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 1080 | //av_log_set_callback(custom_log); |
| 1081 | DEBUG_REF("gifvideo.cpp AnimatedFileDrawableStream ref"); |
| 1082 | jclass_AnimatedFileDrawableStream = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/AnimatedFileDrawableStream")); |
| 1083 | if (jclass_AnimatedFileDrawableStream == 0) { |
| 1084 | return JNI_FALSE; |
| 1085 | } |
| 1086 | jclass_AnimatedFileDrawableStream_read = env->GetMethodID(jclass_AnimatedFileDrawableStream, "read", "(II)I"); |
| 1087 | if (jclass_AnimatedFileDrawableStream_read == 0) { |
| 1088 | return JNI_FALSE; |
| 1089 | } |
| 1090 | jclass_AnimatedFileDrawableStream_cancel = env->GetMethodID(jclass_AnimatedFileDrawableStream, "cancel", "()V"); |
| 1091 | if (jclass_AnimatedFileDrawableStream_cancel == 0) { |
| 1092 | return JNI_FALSE; |
| 1093 | } |
| 1094 | jclass_AnimatedFileDrawableStream_isFinishedLoadingFile = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isFinishedLoadingFile", "()Z"); |
| 1095 | if (jclass_AnimatedFileDrawableStream_isFinishedLoadingFile == 0) { |
| 1096 | return JNI_FALSE; |
| 1097 | } |
| 1098 | jclass_AnimatedFileDrawableStream_isCanceled = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isCanceled", "()Z"); |
| 1099 | if (jclass_AnimatedFileDrawableStream_isCanceled == 0) { |
| 1100 | return JNI_FALSE; |
| 1101 | } |
| 1102 | jclass_AnimatedFileDrawableStream_getFinishedFilePath = env->GetMethodID(jclass_AnimatedFileDrawableStream, "getFinishedFilePath", "()Ljava/lang/String;"); |
| 1103 | if (jclass_AnimatedFileDrawableStream_getFinishedFilePath == 0) { |
| 1104 | return JNI_FALSE; |
| 1105 | } |
| 1106 |
| 1107 | return JNI_TRUE; |
| 1108 | } |
| 1109 |