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