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