| 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 "gifvideo/sws_context_holder.h" |
| 17 | #include "gifvideo/video_frame_reader.h" |
| 18 | #include <cmath> |
| 19 |
| 20 | extern "C" { |
| 21 | #include <libavformat/avformat.h> |
| 22 | #include <libavutil/eval.h> |
| 23 | #include <libswscale/swscale.h> |
| 24 | #include <libavutil/display.h> |
| 25 | } |
| 26 |
| 27 | #define RGB8888_A(p) ((p & (0xff<<24)) >> 24 ) |
| 28 |
| 29 | static const std::string av_make_error_str(int errnum) { |
| 30 | char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 31 | av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); |
| 32 | return (std::string) errbuf; |
| 33 | } |
| 34 |
| 35 | #undef av_err2str |
| 36 | #define av_err2str(errnum) av_make_error_str(errnum).c_str() |
| 37 | #define FFMPEG_AVSEEK_SIZE 0x10000 |
| 38 |
| 39 | jclass jclass_AnimatedFileDrawableStream; |
| 40 | jmethodID jclass_AnimatedFileDrawableStream_read; |
| 41 | jmethodID jclass_AnimatedFileDrawableStream_cancel; |
| 42 | jmethodID jclass_AnimatedFileDrawableStream_isCanceled; |
| 43 | jmethodID jclass_AnimatedFileDrawableStream_isFinishedLoadingFile; |
| 44 | jmethodID jclass_AnimatedFileDrawableStream_getFinishedFilePath; |
| 45 |
| 46 | struct OffsetIOContext; |
| 47 | struct VideoInfo; |
| 48 | static void freeOffsetIO(VideoInfo *info); |
| 49 |
| 50 | typedef struct VideoInfo { |
| 51 |
| 52 | ~VideoInfo() { |
| 53 | delete reader; |
| 54 | reader = nullptr; |
| 55 |
| 56 | if (video_dec_ctx) { |
| 57 | // avcodec_close() frees internals but NOT the context allocated by |
| 58 | // avcodec_alloc_context3(); avcodec_free_context() frees both. |
| 59 | avcodec_free_context(&video_dec_ctx); |
| 60 | } |
| 61 | if (fmt_ctx) { |
| 62 | avformat_close_input(&fmt_ctx); |
| 63 | fmt_ctx = nullptr; |
| 64 | } |
| 65 | if (src) { |
| 66 | delete [] src; |
| 67 | src = nullptr; |
| 68 | } |
| 69 | if (stream != nullptr) { |
| 70 | JNIEnv *jniEnv = nullptr; |
| 71 | JavaVMAttachArgs jvmArgs; |
| 72 | jvmArgs.version = JNI_VERSION_1_6; |
| 73 |
| 74 | bool attached; |
| 75 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 76 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 77 | attached = true; |
| 78 | } else { |
| 79 | attached = false; |
| 80 | } |
| 81 | DEBUG_DELREF("gifvideo.cpp stream"); |
| 82 | jniEnv->DeleteGlobalRef(stream); |
| 83 | if (attached) { |
| 84 | javaVm->DetachCurrentThread(); |
| 85 | } |
| 86 | stream = nullptr; |
| 87 | } |
| 88 | if (ioContext != nullptr) { |
| 89 | if (ioContext->buffer) { |
| 90 | av_freep(&ioContext->buffer); |
| 91 | } |
| 92 | avio_context_free(&ioContext); |
| 93 | ioContext = nullptr; |
| 94 | } |
| 95 | freeOffsetIO(this); |
| 96 | if (fd >= 0) { |
| 97 | close(fd); |
| 98 | fd = -1; |
| 99 | } |
| 100 |
| 101 | video_stream_idx = -1; |
| 102 | video_stream = nullptr; |
| 103 | audio_stream = nullptr; |
| 104 | } |
| 105 |
| 106 | AVFormatContext *fmt_ctx = nullptr; |
| 107 | char *src = nullptr; |
| 108 | int video_stream_idx = -1; |
| 109 | AVStream *video_stream = nullptr; |
| 110 | AVStream *audio_stream = nullptr; |
| 111 | AVCodecContext *video_dec_ctx = nullptr; |
| 112 | // Borrows fmt_ctx and video_dec_ctx; must be deleted before them (see dtor). |
| 113 | VideoFrameReader *reader = nullptr; |
| 114 | bool stopped = false; |
| 115 | bool seeking = false; |
| 116 | bool afterEof = false; |
| 117 | bool isSingleFrame = false; |
| 118 |
| 119 | struct SwsContextHolder sws_ctx_holder; |
| 120 |
| 121 | AVIOContext *ioContext = nullptr; |
| 122 | unsigned char *ioBuffer = nullptr; |
| 123 | // Custom AVIO for the fileOffset path in nGetVideoInfo. Owned here because |
| 124 | // fmt_ctx->pb is not freed by avformat_close_input for a caller-supplied pb. |
| 125 | AVIOContext *offsetIoContext = nullptr; |
| 126 | struct OffsetIOContext *offsetIoOpaque = 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 | const 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 | ret = avcodec_open2(*dec_ctx, dec, &opts); |
| 191 | av_dict_free(&opts); // frees leftover (unconsumed) options on all paths |
| 192 | if (ret < 0) { |
| 193 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 194 | return ret; |
| 195 | } |
| 196 | *stream_idx = stream_index; |
| 197 | } |
| 198 |
| 199 | return 0; |
| 200 | } |
| 201 |
| 202 | void requestFd(VideoInfo *info) { |
| 203 | JNIEnv *jniEnv = nullptr; |
| 204 |
| 205 | JavaVMAttachArgs jvmArgs; |
| 206 | jvmArgs.version = JNI_VERSION_1_6; |
| 207 |
| 208 | bool attached; |
| 209 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 210 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 211 | attached = true; |
| 212 | } else { |
| 213 | attached = false; |
| 214 | } |
| 215 | jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) 0, (jint) 1); |
| 216 | jboolean loaded = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isFinishedLoadingFile); |
| 217 | if (loaded) { |
| 218 | delete[] info->src; |
| 219 | jstring src = (jstring) jniEnv->CallObjectMethod(info->stream, jclass_AnimatedFileDrawableStream_getFinishedFilePath); |
| 220 | char const *srcString = jniEnv->GetStringUTFChars(src, 0); |
| 221 | size_t len = strlen(srcString); |
| 222 | info->src = new char[len + 1]; |
| 223 | memcpy(info->src, srcString, len); |
| 224 | info->src[len] = '\0'; |
| 225 | if (srcString != 0) { |
| 226 | jniEnv->ReleaseStringUTFChars(src, srcString); |
| 227 | } |
| 228 | } |
| 229 |
| 230 | if (attached) { |
| 231 | javaVm->DetachCurrentThread(); |
| 232 | } |
| 233 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 234 | } |
| 235 |
| 236 | int readCallback(void *opaque, uint8_t *buf, int buf_size) { |
| 237 | VideoInfo *info = (VideoInfo *) opaque; |
| 238 | if (!info->stopped) { |
| 239 | if (info->fd < 0) { |
| 240 | requestFd(info); |
| 241 | } |
| 242 | if (info->fd >= 0) { |
| 243 | if (info->last_seek_p + buf_size > info->file_size) { |
| 244 | buf_size = (int) (info->file_size - info->last_seek_p); |
| 245 | } |
| 246 | if (buf_size > 0) { |
| 247 | JNIEnv *jniEnv = nullptr; |
| 248 |
| 249 | JavaVMAttachArgs jvmArgs; |
| 250 | jvmArgs.version = JNI_VERSION_1_6; |
| 251 |
| 252 | bool attached; |
| 253 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 254 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 255 | attached = true; |
| 256 | } else { |
| 257 | attached = false; |
| 258 | } |
| 259 |
| 260 | buf_size = jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) info->last_seek_p, (jint) buf_size); |
| 261 | info->last_seek_p += buf_size; |
| 262 | if (attached) { |
| 263 | javaVm->DetachCurrentThread(); |
| 264 | } |
| 265 | if (buf_size == 0) { |
| 266 | return AVERROR_EXIT; |
| 267 | } |
| 268 | int ret = (int) read(info->fd, buf, (size_t) buf_size); |
| 269 | if (ret <= 0) { |
| 270 | return AVERROR_EOF; |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return AVERROR_EOF; |
| 277 | } |
| 278 |
| 279 | int64_t seekCallback(void *opaque, int64_t offset, int whence) { |
| 280 | VideoInfo *info = (VideoInfo *) opaque; |
| 281 | if (!info->stopped) { |
| 282 | if (info->fd < 0) { |
| 283 | requestFd(info); |
| 284 | } |
| 285 | if (info->fd >= 0) { |
| 286 | if (whence & FFMPEG_AVSEEK_SIZE) { |
| 287 | return info->file_size; |
| 288 | } else { |
| 289 | info->last_seek_p = offset; |
| 290 | lseek(info->fd, off_t(offset), SEEK_SET); |
| 291 | return offset; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 |
| 298 | enum PARAM_NUM { |
| 299 | PARAM_NUM_SUPPORTED_VIDEO_CODEC = 0, |
| 300 | PARAM_NUM_WIDTH = 1, |
| 301 | PARAM_NUM_HEIGHT = 2, |
| 302 | PARAM_NUM_BITRATE = 3, |
| 303 | PARAM_NUM_DURATION = 4, |
| 304 | PARAM_NUM_AUDIO_FRAME_SIZE = 5, |
| 305 | PARAM_NUM_VIDEO_FRAME_SIZE = 6, |
| 306 | PARAM_NUM_FRAMERATE = 7, |
| 307 | PARAM_NUM_ROTATION = 8, |
| 308 | PARAM_NUM_SUPPORTED_AUDIO_CODEC = 9, |
| 309 | PARAM_NUM_HAS_AUDIO = 10, |
| 310 | PARAM_NUM_COUNT = 11, |
| 311 | }; |
| 312 |
| 313 | struct OffsetIOContext { |
| 314 | int fd; |
| 315 | int64_t offset; |
| 316 | }; |
| 317 |
| 318 | static int offsetRead(void *opaque, uint8_t *buf, int size) { |
| 319 | OffsetIOContext *ctx = (OffsetIOContext *)opaque; |
| 320 | return read(ctx->fd, buf, size); |
| 321 | } |
| 322 |
| 323 | static int64_t offsetSeek(void *opaque, int64_t pos, int whence) { |
| 324 | OffsetIOContext *ctx = (OffsetIOContext *)opaque; |
| 325 | if (whence == AVSEEK_SIZE) return -1; // unknown size, FFmpeg handles this |
| 326 | return lseek(ctx->fd, ctx->offset + pos, whence); |
| 327 | } |
| 328 |
| 329 | // Frees the custom AVIO built for the fileOffset path (see nGetVideoInfo). |
| 330 | // Safe to call when nothing was allocated: all fields default to null/-1. |
| 331 | static void freeOffsetIO(VideoInfo *info) { |
| 332 | if (info->offsetIoContext != nullptr) { |
| 333 | av_freep(&info->offsetIoContext->buffer); |
| 334 | avio_context_free(&info->offsetIoContext); |
| 335 | info->offsetIoContext = nullptr; |
| 336 | } |
| 337 | if (info->offsetIoOpaque != nullptr) { |
| 338 | if (info->offsetIoOpaque->fd >= 0) { |
| 339 | close(info->offsetIoOpaque->fd); |
| 340 | } |
| 341 | delete info->offsetIoOpaque; |
| 342 | info->offsetIoOpaque = nullptr; |
| 343 | } |
| 344 | } |
| 345 |
| 346 | int getVideoRotation(const AVStream *stream) { |
| 347 | const AVPacketSideData *displayMatrix = av_packet_side_data_get( |
| 348 | stream->codecpar->coded_side_data, |
| 349 | stream->codecpar->nb_coded_side_data, |
| 350 | AV_PKT_DATA_DISPLAYMATRIX); |
| 351 | if (displayMatrix != nullptr && displayMatrix->size >= 9 * sizeof(int32_t)) { |
| 352 | // av_display_rotation_get() returns the counter-clockwise angle; negate |
| 353 | // to match the legacy clockwise convention, then normalize. |
| 354 | double theta = -av_display_rotation_get((const int32_t *) displayMatrix->data); |
| 355 | if (!std::isnan(theta)) { |
| 356 | int rotation = ((int) lround(theta / 90.0) * 90) % 360; |
| 357 | return rotation < 0 ? rotation + 360 : rotation; |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 |
| 362 | // Fallback for old containers that still carry the legacy metadata tag. |
| 363 | AVDictionaryEntry *rotate_tag = av_dict_get(stream->metadata, "rotate", nullptr, 0); |
| 364 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0") != 0) { |
| 365 | char *tail; |
| 366 | int rotation = (int) av_strtod(rotate_tag->value, &tail); |
| 367 | if (*tail == '\0') { |
| 368 | rotation = ((rotation / 90) * 90) % 360; |
| 369 | return rotation < 0 ? rotation + 360 : rotation; |
| 370 | } |
| 371 | } |
| 372 | return 0; |
| 373 | } |
| 374 |
| 375 |
| 376 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nGetVideoInfo(JNIEnv *env, jclass clazz, jstring src, jintArray data, jlong fileOffset) { |
| 377 | VideoInfo *info = new VideoInfo(); |
| 378 |
| 379 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 380 | size_t len = strlen(srcString); |
| 381 | info->src = new char[len + 1]; |
| 382 | memcpy(info->src, srcString, len); |
| 383 | info->src[len] = '\0'; |
| 384 | if (srcString != nullptr) { |
| 385 | env->ReleaseStringUTFChars(src, srcString); |
| 386 | } |
| 387 |
| 388 | int ret; |
| 389 | if (fileOffset > 0) { |
| 390 | int fd = open(info->src, O_RDONLY); |
| 391 | if (fd < 0) { |
| 392 | LOGE("can't open source file %s", info->src); |
| 393 | delete info; |
| 394 | return; |
| 395 | } |
| 396 | if (lseek(fd, fileOffset, SEEK_SET) < 0) { |
| 397 | LOGE("can't seek to offset %lld in file %s", (long long)fileOffset, info->src); |
| 398 | close(fd); |
| 399 | delete info; |
| 400 | return; |
| 401 | } |
| 402 |
| 403 | OffsetIOContext *ioCtx = new OffsetIOContext{fd, fileOffset}; |
| 404 | uint8_t *ioBuf = (uint8_t *)av_malloc(32 * 1024); |
| 405 | AVIOContext *avio = avio_alloc_context(ioBuf, 32 * 1024, 0, ioCtx, offsetRead, nullptr, offsetSeek); |
| 406 |
| 407 | // Owned by info from here on: ~VideoInfo -> freeOffsetIO frees avio, its |
| 408 | // buffer, ioCtx and its fd. avformat_close_input won't free a custom pb. |
| 409 | info->offsetIoContext = avio; |
| 410 | info->offsetIoOpaque = ioCtx; |
| 411 |
| 412 | info->fmt_ctx = avformat_alloc_context(); |
| 413 | info->fmt_ctx->pb = avio; |
| 414 |
| 415 | if ((ret = avformat_open_input(&info->fmt_ctx, nullptr, nullptr, nullptr)) < 0) { |
| 416 | LOGE("can't open source file at offset %s (offset=%lld), %s", info->src, fileOffset, av_err2str(ret)); |
| 417 | info->fmt_ctx = nullptr; |
| 418 | delete info; |
| 419 | return; |
| 420 | } |
| 421 | } else { |
| 422 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, nullptr, nullptr)) < 0) { |
| 423 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 424 | delete info; |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 |
| 429 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 430 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 431 | delete info; |
| 432 | return; |
| 433 | } |
| 434 |
| 435 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0)) >= 0) { |
| 436 | info->video_stream = info->fmt_ctx->streams[ret]; |
| 437 | } |
| 438 |
| 439 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) >= 0) { |
| 440 | info->audio_stream = info->fmt_ctx->streams[ret]; |
| 441 | } |
| 442 |
| 443 | if (info->video_stream == nullptr) { |
| 444 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 445 | delete info; |
| 446 | return; |
| 447 | } |
| 448 |
| 449 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 450 | if (dataArr != nullptr) { |
| 451 | //https://developer.android.com/guide/topics/media/media-formats |
| 452 | dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC] = |
| 453 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || |
| 454 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H263 || |
| 455 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_MPEG4 || |
| 456 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP8 || |
| 457 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP9 || |
| 458 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC; |
| 459 |
| 460 | /* |
| 461 | if (strstr(info->fmt_ctx->iformat->name, "mov") != 0 && dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC]) { |
| 462 | MOVStreamContext *mov = (MOVStreamContext *) info->video_stream->priv_data; |
| 463 | dataArr[PARAM_NUM_VIDEO_FRAME_SIZE] = (jint) mov->data_size; |
| 464 |
| 465 | if (info->audio_stream != nullptr) { |
| 466 | mov = (MOVStreamContext *) info->audio_stream->priv_data; |
| 467 | dataArr[PARAM_NUM_AUDIO_FRAME_SIZE] = (jint) mov->data_size; |
| 468 | } |
| 469 | } |
| 470 | */ |
| 471 |
| 472 | if (info->audio_stream != nullptr) { |
| 473 | //https://developer.android.com/guide/topics/media/media-formats |
| 474 | dataArr[PARAM_NUM_SUPPORTED_AUDIO_CODEC] = |
| 475 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC || |
| 476 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC_LATM || |
| 477 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_VORBIS || |
| 478 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_NB || |
| 479 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_WB || |
| 480 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_FLAC || |
| 481 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_MP3 || |
| 482 | // not supported codec, skip audio in this case |
| 483 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV || |
| 484 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_OPUS; |
| 485 | dataArr[PARAM_NUM_HAS_AUDIO] = 1; |
| 486 | } else { |
| 487 | dataArr[PARAM_NUM_HAS_AUDIO] = 0; |
| 488 | } |
| 489 |
| 490 | dataArr[PARAM_NUM_BITRATE] = (jint) info->video_stream->codecpar->bit_rate; |
| 491 | dataArr[PARAM_NUM_WIDTH] = info->video_stream->codecpar->width; |
| 492 | dataArr[PARAM_NUM_HEIGHT] = info->video_stream->codecpar->height; |
| 493 | dataArr[PARAM_NUM_ROTATION] = (jint) getVideoRotation(info->video_stream); |
| 494 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC) { |
| 495 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->avg_frame_rate); |
| 496 | } else { |
| 497 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->r_frame_rate); |
| 498 | } |
| 499 | dataArr[PARAM_NUM_DURATION] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 500 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 501 | delete info; |
| 502 | } |
| 503 | } |
| 504 |
| 505 | static bool isStreamCanceled(VideoInfo *info) { |
| 506 | if (info->stream == nullptr) { |
| 507 | return false; |
| 508 | } |
| 509 | JNIEnv *jniEnv = nullptr; |
| 510 | JavaVMAttachArgs jvmArgs; |
| 511 | jvmArgs.version = JNI_VERSION_1_6; |
| 512 |
| 513 | bool attached = false; |
| 514 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 515 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 516 | attached = true; |
| 517 | } |
| 518 | jboolean canceled = jniEnv->CallBooleanMethod( |
| 519 | info->stream, jclass_AnimatedFileDrawableStream_isCanceled); |
| 520 | if (attached) { |
| 521 | javaVm->DetachCurrentThread(); |
| 522 | } |
| 523 | return canceled; |
| 524 | } |
| 525 |
| 526 | extern "C" JNIEXPORT jlong JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nCreateDecoder(JNIEnv *env, jclass clazz, jstring src, jintArray data, jint account, jlong streamFileSize, jobject stream, jboolean preview) { |
| 527 | VideoInfo *info = new VideoInfo(); |
| 528 |
| 529 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 530 | size_t len = strlen(srcString); |
| 531 | info->src = new char[len + 1]; |
| 532 | memcpy(info->src, srcString, len); |
| 533 | info->src[len] = '\0'; |
| 534 | if (srcString != 0) { |
| 535 | env->ReleaseStringUTFChars(src, srcString); |
| 536 | } |
| 537 |
| 538 | int ret; |
| 539 | if (streamFileSize != 0) { |
| 540 | info->file_size = streamFileSize; |
| 541 | DEBUG_REF("gifvideo.cpp new stream"); |
| 542 | info->stream = env->NewGlobalRef(stream); |
| 543 | info->account = account; |
| 544 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 545 |
| 546 | info->ioBuffer = (unsigned char *) av_malloc(64 * 1024); |
| 547 | info->ioContext = avio_alloc_context(info->ioBuffer, 64 * 1024, 0, info, readCallback, nullptr, seekCallback); |
| 548 | if (info->ioContext == nullptr) { |
| 549 | // avio didn't take ownership of the buffer; free it here. After a |
| 550 | // successful alloc the buffer lives in ioContext->buffer and is |
| 551 | // freed by the destructor (and may be reallocated by FFmpeg). |
| 552 | av_freep(&info->ioBuffer); |
| 553 | delete info; |
| 554 | return 0; |
| 555 | } |
| 556 |
| 557 | info->fmt_ctx = avformat_alloc_context(); |
| 558 | info->fmt_ctx->pb = info->ioContext; |
| 559 |
| 560 | AVDictionary *options = NULL; |
| 561 | av_dict_set(&options, "usetoc", "1", 0); |
| 562 | ret = avformat_open_input(&info->fmt_ctx, "http://localhost/file", NULL, &options); |
| 563 | av_dict_free(&options); |
| 564 | if (ret < 0) { |
| 565 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 566 | delete info; |
| 567 | return 0; |
| 568 | } |
| 569 | info->fmt_ctx->flags |= AVFMT_FLAG_FAST_SEEK; |
| 570 | if (preview) { |
| 571 | info->fmt_ctx->flags |= AVFMT_FLAG_NOBUFFER; |
| 572 | } |
| 573 | } else { |
| 574 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 575 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 576 | delete info; |
| 577 | return 0; |
| 578 | } |
| 579 | } |
| 580 |
| 581 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 582 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 583 | delete info; |
| 584 | return 0; |
| 585 | } |
| 586 |
| 587 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 588 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 589 | } |
| 590 |
| 591 | if (info->video_stream == nullptr) { |
| 592 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 593 | delete info; |
| 594 | return 0; |
| 595 | } |
| 596 |
| 597 | info->reader = new VideoFrameReader(info->fmt_ctx, info->video_dec_ctx, info->video_stream_idx); |
| 598 | VideoInfo *self = info; |
| 599 | info->reader->shouldAbort = [self]() { |
| 600 | // Covers nStopDecoder (stopped), nPrepareToSeek (seeking) and stream cancel. |
| 601 | return self->stopped || self->seeking || isStreamCanceled(self); |
| 602 | }; |
| 603 |
| 604 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 605 | if (dataArr != nullptr) { |
| 606 | dataArr[0] = info->video_dec_ctx->width; |
| 607 | dataArr[1] = info->video_dec_ctx->height; |
| 608 | //float pixelWidthHeightRatio = info->video_dec_ctx->sample_aspect_ratio.num / info->video_dec_ctx->sample_aspect_ratio.den; TODO support |
| 609 | dataArr[2] = (jint) getVideoRotation(info->video_stream); |
| 610 | dataArr[4] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 611 | int video_stream_index = -1; |
| 612 | double fps = 30.0; |
| 613 | for (int i = 0; i < info->fmt_ctx->nb_streams; i++) { |
| 614 | if (info->fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 615 | video_stream_index = i; |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | if (video_stream_index != -1) { |
| 620 | AVStream* video_stream = info->fmt_ctx->streams[video_stream_index]; |
| 621 | if (video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num) { |
| 622 | fps = av_q2d(video_stream->avg_frame_rate); |
| 623 | } else if(video_stream->r_frame_rate.den && video_stream->r_frame_rate.num) { |
| 624 | fps = av_q2d(video_stream->r_frame_rate); |
| 625 | } else { |
| 626 | /* |
| 627 | int ticks = video_stream->codec->ticks_per_frame; |
| 628 | fps = 1.0 / (ticks * av_q2d(video_stream->time_base)); |
| 629 | */ |
| 630 | } |
| 631 | } |
| 632 | dataArr[5] = (int32_t) fps; |
| 633 | //(int32_t) (1000 * info->video_stream->duration * av_q2d(info->video_stream->time_base)); |
| 634 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 635 | } |
| 636 |
| 637 | //LOGD("successfully opened file %s", info->src); |
| 638 |
| 639 | return (jlong) (intptr_t) info; |
| 640 | } |
| 641 |
| 642 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nDestroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 643 | if (ptr == NULL) { |
| 644 | return; |
| 645 | } |
| 646 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 647 | if (info->stream != nullptr) { |
| 648 | JNIEnv *jniEnv = nullptr; |
| 649 | JavaVMAttachArgs jvmArgs; |
| 650 | jvmArgs.version = JNI_VERSION_1_6; |
| 651 |
| 652 | bool attached; |
| 653 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 654 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 655 | attached = true; |
| 656 | } else { |
| 657 | attached = false; |
| 658 | } |
| 659 | jniEnv->CallVoidMethod(info->stream, jclass_AnimatedFileDrawableStream_cancel); |
| 660 | if (attached) { |
| 661 | javaVm->DetachCurrentThread(); |
| 662 | } |
| 663 | } |
| 664 | delete info; |
| 665 | } |
| 666 |
| 667 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nStopDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 668 | if (ptr == NULL) { |
| 669 | return; |
| 670 | } |
| 671 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 672 | info->stopped = true; |
| 673 | } |
| 674 |
| 675 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nPrepareToSeek(JNIEnv *env, jclass clazz, jlong ptr) { |
| 676 | if (ptr == NULL) { |
| 677 | return; |
| 678 | } |
| 679 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 680 | info->seeking = true; |
| 681 | } |
| 682 |
| 683 | void push_time(JNIEnv *env, VideoInfo* info, AVFrame *frame, jintArray data) { |
| 684 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 685 | dataArr[3] = (jint) (1000 * frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 686 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 687 | } |
| 688 |
| 689 | void push_single_frame(JNIEnv *env, jintArray data) { |
| 690 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 691 | dataArr[7] = 1; |
| 692 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 693 | } |
| 694 |
| 695 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nSeekToMs(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jintArray data, jboolean precise) { |
| 696 | if (ptr == 0) { |
| 697 | return; |
| 698 | } |
| 699 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 700 | info->seeking = false; // clear before decoding so shouldAbort() won't bail |
| 701 | info->afterEof = false; |
| 702 |
| 703 | AVRational tb = info->video_stream->time_base; |
| 704 | int64_t pts = (int64_t) (ms / av_q2d(tb) / 1000); |
| 705 |
| 706 | if (!info->reader->seek(pts)) { |
| 707 | LOGE("can't seek file %s", info->src); |
| 708 | return; |
| 709 | } |
| 710 |
| 711 | if (!precise) { |
| 712 | // Non-precise: land on the keyframe, don't decode toward the target. |
| 713 | return; |
| 714 | } |
| 715 |
| 716 | double targetSec = ms / 1000.0; |
| 717 | for (;;) { |
| 718 | VideoFrameReader::Status st = info->reader->getNextFrame(); |
| 719 | if (st != VideoFrameReader::Status::Ok) { |
| 720 | // Eof: target lies past the last frame -> rewind to start, as before. |
| 721 | // Aborted/Error: stop without advancing. |
| 722 | if (st == VideoFrameReader::Status::Eof) { |
| 723 | info->reader->seek(0); |
| 724 | } |
| 725 | return; |
| 726 | } |
| 727 |
| 728 | if (info->reader->frameTimeSeconds() >= targetSec) { |
| 729 | push_time(env, info, info->reader->frame(), data); |
| 730 | return; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 |
| 735 | static inline void writeFrameToBitmap(JNIEnv *env, VideoInfo *info, AVFrame *frame, jintArray data, jobject bitmap) { |
| 736 | if (env->IsSameObject(bitmap, NULL)) { |
| 737 | push_time(env, info, frame, data); |
| 738 | return; |
| 739 | } |
| 740 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 741 | int32_t wantedWidth; |
| 742 | int32_t wantedHeight; |
| 743 |
| 744 | AndroidBitmapInfo bitmapInfo; |
| 745 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 746 | int32_t bitmapWidth = bitmapInfo.width; |
| 747 | int32_t bitmapHeight = bitmapInfo.height; |
| 748 | int32_t bitmapStride = bitmapInfo.stride; |
| 749 |
| 750 | if (dataArr != nullptr) { |
| 751 | wantedWidth = dataArr[0]; |
| 752 | wantedHeight = dataArr[1]; |
| 753 | dataArr[3] = (jint) (1000 * frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 754 | if (env->GetArrayLength(data) > 6) { |
| 755 | bool isOpaque = ( |
| 756 | frame->format == AV_PIX_FMT_YUV420P || |
| 757 | frame->format == AV_PIX_FMT_YUVJ420P || |
| 758 | frame->format == AV_PIX_FMT_YUV444P |
| 759 | ); |
| 760 | dataArr[6] = isOpaque ? 1 : 0; |
| 761 | } |
| 762 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 763 | } else { |
| 764 | wantedWidth = bitmapWidth; |
| 765 | wantedHeight = bitmapHeight; |
| 766 | } |
| 767 |
| 768 | if (!(wantedWidth == frame->width && wantedHeight == frame->height || wantedWidth == frame->height && wantedHeight == frame->width)) { |
| 769 | return; |
| 770 | } |
| 771 |
| 772 | void *pixels; |
| 773 | if (__builtin_expect(AndroidBitmap_lockPixels(env, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 774 | return; |
| 775 | } |
| 776 |
| 777 | SwsContext* sws_ctx = nullptr; |
| 778 | if (frame->format > AV_PIX_FMT_NONE && frame->format < AV_PIX_FMT_NB && frame->format != AV_PIX_FMT_YUVA420P) { |
| 779 | sws_ctx = info->sws_ctx_holder.get( |
| 780 | frame->width, |
| 781 | frame->height, |
| 782 | (AVPixelFormat) frame->format, |
| 783 | bitmapWidth, |
| 784 | bitmapHeight, |
| 785 | AV_PIX_FMT_RGBA); |
| 786 | } else if (info->video_dec_ctx->pix_fmt > AV_PIX_FMT_NONE && info->video_dec_ctx->pix_fmt < AV_PIX_FMT_NB && frame->format != AV_PIX_FMT_YUVA420P) { |
| 787 | sws_ctx = info->sws_ctx_holder.get( |
| 788 | info->video_dec_ctx->width, |
| 789 | info->video_dec_ctx->height, |
| 790 | info->video_dec_ctx->pix_fmt, |
| 791 | bitmapWidth, |
| 792 | bitmapHeight, |
| 793 | AV_PIX_FMT_RGBA); |
| 794 | } |
| 795 |
| 796 | if (sws_ctx != nullptr && ((intptr_t) pixels) % 16 == 0) { |
| 797 | uint8_t __attribute__ ((aligned (16))) *dst_data[1]; |
| 798 | dst_data[0] = (uint8_t *) pixels; |
| 799 |
| 800 | int32_t dst_stride[1]; |
| 801 | dst_stride[0] = bitmapStride; |
| 802 | sws_scale(sws_ctx, |
| 803 | frame->data, |
| 804 | frame->linesize, |
| 805 | 0, |
| 806 | frame->height, |
| 807 | dst_data, |
| 808 | dst_stride |
| 809 | ); |
| 810 | } else if (frame->width == bitmapWidth && frame->height == bitmapHeight) { |
| 811 | if (frame->format == AV_PIX_FMT_YUVA420P) { |
| 812 | libyuv::I420AlphaToARGBMatrix( |
| 813 | frame->data[0], frame->linesize[0], |
| 814 | frame->data[2], frame->linesize[2], |
| 815 | frame->data[1], frame->linesize[1], |
| 816 | frame->data[3], frame->linesize[3], |
| 817 | (uint8_t *) pixels, |
| 818 | bitmapStride, |
| 819 | &libyuv::kYvuI601Constants, |
| 820 | bitmapWidth, |
| 821 | bitmapHeight, |
| 822 | 1 |
| 823 | ); |
| 824 | } else if (frame->format == AV_PIX_FMT_YUV444P) { |
| 825 | libyuv::H444ToARGB( |
| 826 | frame->data[0], frame->linesize[0], |
| 827 | frame->data[2], frame->linesize[2], |
| 828 | frame->data[1], frame->linesize[1], |
| 829 | (uint8_t *) pixels, |
| 830 | bitmapStride, |
| 831 | bitmapWidth, |
| 832 | bitmapHeight |
| 833 | ); |
| 834 | } else if (frame->format == AV_PIX_FMT_YUV420P || frame->format == AV_PIX_FMT_YUVJ420P) { |
| 835 | if (frame->colorspace == AVColorSpace::AVCOL_SPC_BT709) { |
| 836 | libyuv::H420ToARGB( |
| 837 | frame->data[0], frame->linesize[0], |
| 838 | frame->data[2], frame->linesize[2], |
| 839 | frame->data[1], frame->linesize[1], |
| 840 | (uint8_t *) pixels, |
| 841 | bitmapStride, |
| 842 | bitmapWidth, |
| 843 | bitmapHeight |
| 844 | ); |
| 845 | } else { |
| 846 | libyuv::I420ToARGB( |
| 847 | frame->data[0], frame->linesize[0], |
| 848 | frame->data[2], frame->linesize[2], |
| 849 | frame->data[1], frame->linesize[1], |
| 850 | (uint8_t *) pixels, |
| 851 | bitmapStride, |
| 852 | bitmapWidth, |
| 853 | bitmapHeight |
| 854 | ); |
| 855 | } |
| 856 | } else if (frame->format == AV_PIX_FMT_BGRA) { |
| 857 | libyuv::ABGRToARGB( |
| 858 | frame->data[0], frame->linesize[0], |
| 859 | (uint8_t *) pixels, |
| 860 | bitmapStride, |
| 861 | bitmapWidth, |
| 862 | bitmapHeight |
| 863 | ); |
| 864 | } |
| 865 | } else if (sws_ctx != nullptr && ((intptr_t) pixels) % 16 != 0) { |
| 866 | // fallback if pixels not aligned |
| 867 | int alignedStride = FFALIGN(bitmapWidth * 4, 16); |
| 868 | int bufSize = alignedStride * bitmapHeight; |
| 869 | uint8_t *alignedBuf = (uint8_t *) av_malloc(bufSize); |
| 870 | if (alignedBuf != nullptr) { |
| 871 | uint8_t *dst_data[1] = { alignedBuf }; |
| 872 | int32_t dst_stride[1] = { alignedStride }; |
| 873 | sws_scale(sws_ctx, |
| 874 | frame->data, |
| 875 | frame->linesize, |
| 876 | 0, |
| 877 | frame->height, |
| 878 | dst_data, |
| 879 | dst_stride |
| 880 | ); |
| 881 | if (alignedStride == bitmapStride) { |
| 882 | memcpy(pixels, alignedBuf, bufSize); |
| 883 | } else { |
| 884 | uint8_t *src = alignedBuf; |
| 885 | uint8_t *dst = (uint8_t *) pixels; |
| 886 | int copyStride = bitmapWidth * 4; |
| 887 | for (int i = 0; i < bitmapHeight; i++) { |
| 888 | memcpy(dst, src, copyStride); |
| 889 | src += alignedStride; |
| 890 | dst += bitmapStride; |
| 891 | } |
| 892 | } |
| 893 | av_free(alignedBuf); |
| 894 | } |
| 895 | } |
| 896 |
| 897 | AndroidBitmap_unlockPixels(env, bitmap); |
| 898 | } |
| 899 |
| 900 | extern "C" JNIEXPORT int JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nGetFrameAtTime(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jobject bitmap, jintArray data) { |
| 901 | if (ptr == 0 || bitmap == nullptr || data == nullptr) { |
| 902 | return 0; |
| 903 | } |
| 904 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 905 | info->seeking = false; // clear before decoding so shouldAbort() won't bail |
| 906 | info->afterEof = false; |
| 907 |
| 908 | AVRational tb = info->video_stream->time_base; |
| 909 | int64_t pts = (int64_t) (ms / av_q2d(tb) / 1000); |
| 910 |
| 911 | if (!info->reader->seek(pts)) { |
| 912 | LOGE("can't seek file %s", info->src); |
| 913 | return 0; |
| 914 | } |
| 915 |
| 916 | double targetSec = ms / 1000.0; |
| 917 | AVFrame *held = av_frame_alloc(); // most recent frame before target (fallback) |
| 918 | bool haveHeld = false; |
| 919 | int result = 0; |
| 920 |
| 921 | for (;;) { |
| 922 | VideoFrameReader::Status st = info->reader->getNextFrame(); |
| 923 | if (st != VideoFrameReader::Status::Ok) { |
| 924 | // Eof: target lies at/after the last frame -> emit the last frame we |
| 925 | // held. Aborted/Error: give up with no frame. |
| 926 | if (st == VideoFrameReader::Status::Eof && haveHeld) { |
| 927 | writeFrameToBitmap(env, info, held, data, bitmap); |
| 928 | result = 1; |
| 929 | } |
| 930 | break; |
| 931 | } |
| 932 |
| 933 | AVFrame *frame = info->reader->frame(); |
| 934 | if (info->reader->frameTimeSeconds() >= targetSec) { |
| 935 | writeFrameToBitmap(env, info, frame, data, bitmap); |
| 936 | result = 1; |
| 937 | break; |
| 938 | } |
| 939 |
| 940 | // Frame is before the target: keep it as the fallback and continue. |
| 941 | av_frame_unref(held); |
| 942 | av_frame_ref(held, frame); |
| 943 | haveHeld = true; |
| 944 | } |
| 945 |
| 946 | av_frame_free(&held); |
| 947 | return result; |
| 948 | } |
| 949 |
| 950 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_ui_Components_AnimatedFileNative_nGetVideoFrame(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jintArray data, jboolean preview, jfloat start_time, jfloat end_time, jboolean loop) { |
| 951 | if (ptr == 0) { |
| 952 | return 0; |
| 953 | } |
| 954 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 955 | if (info->stopped || info->seeking) { |
| 956 | return 0; |
| 957 | } |
| 958 |
| 959 | AVRational tb = info->video_stream->time_base; |
| 960 | int64_t startPts = start_time > 0 ? (int64_t) (start_time / av_q2d(tb)) : 0; |
| 961 |
| 962 | VideoFrameReader::Status st = info->reader->getNextFrame(); |
| 963 |
| 964 | // End of stream, or the frame ran past the trim point -> loop back or finish. |
| 965 | // Note: end_time is compared against the frame's display-order timestamp, |
| 966 | // not a packet pts, so B-frame reordering can no longer drop the wrong frame. |
| 967 | bool pastEnd = st == VideoFrameReader::Status::Ok && |
| 968 | end_time > 0 && info->reader->frameTimeSeconds() > end_time; |
| 969 |
| 970 | if (st == VideoFrameReader::Status::Eof) { |
| 971 | if (info->afterEof && !info->isSingleFrame) { |
| 972 | push_single_frame(env, data); |
| 973 | info->isSingleFrame = true; |
| 974 | } |
| 975 | info->afterEof = true; |
| 976 | } else { |
| 977 | info->afterEof = false; |
| 978 | } |
| 979 |
| 980 | if (st == VideoFrameReader::Status::Eof || pastEnd) { |
| 981 | if (!loop) { |
| 982 | return 0; |
| 983 | } |
| 984 | if (!info->reader->seek(startPts)) { |
| 985 | return 0; |
| 986 | } |
| 987 | st = info->reader->getNextFrame(); |
| 988 | } |
| 989 |
| 990 | if (st != VideoFrameReader::Status::Ok) { |
| 991 | // Aborted (stopped / seeking / canceled), Error, or Eof after looping. |
| 992 | return 0; |
| 993 | } |
| 994 |
| 995 | AVFrame *frame = info->reader->frame(); |
| 996 | if (bitmap != nullptr) { |
| 997 | writeFrameToBitmap(env, info, frame, data, bitmap); |
| 998 | } |
| 999 | push_time(env, info, frame, data); |
| 1000 | return 1; |
| 1001 | } |
| 1002 |
| 1003 | extern "C" jint videoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 1004 | //av_log_set_callback(custom_log); |
| 1005 | DEBUG_REF("gifvideo.cpp AnimatedFileDrawableStream ref"); |
| 1006 | jclass_AnimatedFileDrawableStream = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/AnimatedFileDrawableStream")); |
| 1007 | if (jclass_AnimatedFileDrawableStream == 0) { |
| 1008 | return JNI_FALSE; |
| 1009 | } |
| 1010 | jclass_AnimatedFileDrawableStream_read = env->GetMethodID(jclass_AnimatedFileDrawableStream, "read", "(II)I"); |
| 1011 | if (jclass_AnimatedFileDrawableStream_read == 0) { |
| 1012 | return JNI_FALSE; |
| 1013 | } |
| 1014 | jclass_AnimatedFileDrawableStream_cancel = env->GetMethodID(jclass_AnimatedFileDrawableStream, "cancel", "()V"); |
| 1015 | if (jclass_AnimatedFileDrawableStream_cancel == 0) { |
| 1016 | return JNI_FALSE; |
| 1017 | } |
| 1018 | jclass_AnimatedFileDrawableStream_isFinishedLoadingFile = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isFinishedLoadingFile", "()Z"); |
| 1019 | if (jclass_AnimatedFileDrawableStream_isFinishedLoadingFile == 0) { |
| 1020 | return JNI_FALSE; |
| 1021 | } |
| 1022 | jclass_AnimatedFileDrawableStream_isCanceled = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isCanceled", "()Z"); |
| 1023 | if (jclass_AnimatedFileDrawableStream_isCanceled == 0) { |
| 1024 | return JNI_FALSE; |
| 1025 | } |
| 1026 | jclass_AnimatedFileDrawableStream_getFinishedFilePath = env->GetMethodID(jclass_AnimatedFileDrawableStream, "getFinishedFilePath", "()Ljava/lang/String;"); |
| 1027 | if (jclass_AnimatedFileDrawableStream_getFinishedFilePath == 0) { |
| 1028 | return JNI_FALSE; |
| 1029 | } |
| 1030 |
| 1031 | return JNI_TRUE; |
| 1032 | } |
| 1033 |