| 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_isFinishedLoadingFile; |
| 44 | jmethodID jclass_AnimatedFileDrawableStream_getFinishedFilePath; |
| 45 |
| 46 | typedef struct H2645NAL { |
| 47 | uint8_t *rbsp_buffer; |
| 48 | int size; |
| 49 | const uint8_t *data; |
| 50 | int size_bits; |
| 51 | int raw_size; |
| 52 | const uint8_t *raw_data; |
| 53 | int type; |
| 54 | int temporal_id; |
| 55 | int nuh_layer_id; |
| 56 | int skipped_bytes; |
| 57 | int skipped_bytes_pos_size; |
| 58 | int *skipped_bytes_pos; |
| 59 | int ref_idc; |
| 60 | GetBitContext gb; |
| 61 | } H2645NAL; |
| 62 |
| 63 | typedef struct H2645RBSP { |
| 64 | uint8_t *rbsp_buffer; |
| 65 | AVBufferRef *rbsp_buffer_ref; |
| 66 | int rbsp_buffer_alloc_size; |
| 67 | int rbsp_buffer_size; |
| 68 | } H2645RBSP; |
| 69 |
| 70 | typedef struct H2645Packet { |
| 71 | H2645NAL *nals; |
| 72 | H2645RBSP rbsp; |
| 73 | int nb_nals; |
| 74 | int nals_allocated; |
| 75 | unsigned nal_buffer_size; |
| 76 | } H2645Packet; |
| 77 |
| 78 | void ff_h2645_packet_uninit(H2645Packet *pkt) { |
| 79 | int i; |
| 80 | for (i = 0; i < pkt->nals_allocated; i++) { |
| 81 | av_freep(&pkt->nals[i].skipped_bytes_pos); |
| 82 | } |
| 83 | av_freep(&pkt->nals); |
| 84 | pkt->nals_allocated = pkt->nal_buffer_size = 0; |
| 85 | if (pkt->rbsp.rbsp_buffer_ref) { |
| 86 | av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref); |
| 87 | pkt->rbsp.rbsp_buffer = NULL; |
| 88 | } else |
| 89 | av_freep(&pkt->rbsp.rbsp_buffer); |
| 90 | pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0; |
| 91 | } |
| 92 |
| 93 | typedef struct VideoInfo { |
| 94 |
| 95 | ~VideoInfo() { |
| 96 | if (video_dec_ctx) { |
| 97 | avcodec_close(video_dec_ctx); |
| 98 | video_dec_ctx = nullptr; |
| 99 | } |
| 100 | if (fmt_ctx) { |
| 101 | avformat_close_input(&fmt_ctx); |
| 102 | fmt_ctx = nullptr; |
| 103 | } |
| 104 | if (frame) { |
| 105 | av_frame_free(&frame); |
| 106 | frame = nullptr; |
| 107 | } |
| 108 | if (src) { |
| 109 | delete [] src; |
| 110 | src = nullptr; |
| 111 | } |
| 112 | if (stream != nullptr) { |
| 113 | JNIEnv *jniEnv = nullptr; |
| 114 | JavaVMAttachArgs jvmArgs; |
| 115 | jvmArgs.version = JNI_VERSION_1_6; |
| 116 |
| 117 | bool attached; |
| 118 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 119 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 120 | attached = true; |
| 121 | } else { |
| 122 | attached = false; |
| 123 | } |
| 124 | jniEnv->DeleteGlobalRef(stream); |
| 125 | if (attached) { |
| 126 | javaVm->DetachCurrentThread(); |
| 127 | } |
| 128 | stream = nullptr; |
| 129 | } |
| 130 | if (ioContext != nullptr) { |
| 131 | if (ioContext->buffer) { |
| 132 | av_freep(&ioContext->buffer); |
| 133 | } |
| 134 | avio_context_free(&ioContext); |
| 135 | ioContext = nullptr; |
| 136 | } |
| 137 | if (sws_ctx != nullptr) { |
| 138 | sws_freeContext(sws_ctx); |
| 139 | sws_ctx = nullptr; |
| 140 | } |
| 141 | if (fd >= 0) { |
| 142 | close(fd); |
| 143 | fd = -1; |
| 144 | } |
| 145 |
| 146 | ff_h2645_packet_uninit(&h2645Packet); |
| 147 | av_packet_unref(&orig_pkt); |
| 148 |
| 149 | video_stream_idx = -1; |
| 150 | video_stream = nullptr; |
| 151 | audio_stream = nullptr; |
| 152 | } |
| 153 |
| 154 | AVFormatContext *fmt_ctx = nullptr; |
| 155 | char *src = nullptr; |
| 156 | int video_stream_idx = -1; |
| 157 | AVStream *video_stream = nullptr; |
| 158 | AVStream *audio_stream = nullptr; |
| 159 | AVCodecContext *video_dec_ctx = nullptr; |
| 160 | AVFrame *frame = nullptr; |
| 161 | bool has_decoded_frames = false; |
| 162 | AVPacket pkt; |
| 163 | AVPacket orig_pkt; |
| 164 | bool stopped = false; |
| 165 | bool seeking = false; |
| 166 |
| 167 | int firstWidth = 0; |
| 168 | int firstHeight = 0; |
| 169 |
| 170 | bool dropFrames = false; |
| 171 |
| 172 | H2645Packet h2645Packet = {nullptr}; |
| 173 |
| 174 | int32_t dst_linesize[1]; |
| 175 |
| 176 | struct SwsContext *sws_ctx = nullptr; |
| 177 |
| 178 | AVIOContext *ioContext = nullptr; |
| 179 | unsigned char *ioBuffer = nullptr; |
| 180 | jobject stream = nullptr; |
| 181 | int32_t account = 0; |
| 182 | int fd = -1; |
| 183 | int64_t file_size = 0; |
| 184 | int64_t last_seek_p = 0; |
| 185 | }; |
| 186 |
| 187 | void custom_log(void *ptr, int level, const char* fmt, va_list vl){ |
| 188 | va_list vl2; |
| 189 | char line[1024]; |
| 190 | static int print_prefix = 1; |
| 191 |
| 192 | va_copy(vl2, vl); |
| 193 | av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix); |
| 194 | va_end(vl2); |
| 195 |
| 196 | LOGE(line); |
| 197 | } |
| 198 |
| 199 | static enum AVPixelFormat get_format(AVCodecContext *ctx, |
| 200 | const enum AVPixelFormat *pix_fmts) |
| 201 | { |
| 202 | const enum AVPixelFormat *p; |
| 203 |
| 204 | for (p = pix_fmts; *p != -1; p++) { |
| 205 | LOGE("available format %d", p); |
| 206 | } |
| 207 |
| 208 | return pix_fmts[0]; |
| 209 | } |
| 210 |
| 211 | int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 212 | int ret, stream_index; |
| 213 | AVStream *st; |
| 214 | AVCodec *dec = NULL; |
| 215 | AVDictionary *opts = NULL; |
| 216 |
| 217 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 218 | if (ret < 0) { |
| 219 | LOGE("can't find %s stream in input file", av_get_media_type_string(type)); |
| 220 | return ret; |
| 221 | } else { |
| 222 | stream_index = ret; |
| 223 | st = fmt_ctx->streams[stream_index]; |
| 224 |
| 225 | dec = avcodec_find_decoder(st->codecpar->codec_id); |
| 226 | if (!dec) { |
| 227 | LOGE("failed to find %s codec", av_get_media_type_string(type)); |
| 228 | return AVERROR(EINVAL); |
| 229 | } |
| 230 |
| 231 | *dec_ctx = avcodec_alloc_context3(dec); |
| 232 | if (!*dec_ctx) { |
| 233 | LOGE("Failed to allocate the %s codec context", av_get_media_type_string(type)); |
| 234 | return AVERROR(ENOMEM); |
| 235 | } |
| 236 |
| 237 | if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { |
| 238 | LOGE("Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); |
| 239 | return ret; |
| 240 | } |
| 241 |
| 242 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 243 | if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { |
| 244 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 245 | return ret; |
| 246 | } |
| 247 | *stream_idx = stream_index; |
| 248 | } |
| 249 |
| 250 | return 0; |
| 251 | } |
| 252 |
| 253 | #define MAX_MBPAIR_SIZE (256*1024) |
| 254 |
| 255 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp, H2645NAL *nal) |
| 256 | { |
| 257 | int i, si, di; |
| 258 | uint8_t *dst; |
| 259 |
| 260 | nal->skipped_bytes = 0; |
| 261 | #define STARTCODE_TEST \ |
| 262 | if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \ |
| 263 | if (src[i + 2] != 3 && src[i + 2] != 0) { \ |
| 264 | /* startcode, so we must be past the end */ \ |
| 265 | length = i; \ |
| 266 | } \ |
| 267 | break; \ |
| 268 | } |
| 269 |
| 270 | for (i = 0; i + 1 < length; i += 2) { |
| 271 | if (src[i]) |
| 272 | continue; |
| 273 | if (i > 0 && src[i - 1] == 0) |
| 274 | i--; |
| 275 | STARTCODE_TEST; |
| 276 | } |
| 277 |
| 278 | if (i > length) |
| 279 | i = length; |
| 280 |
| 281 | nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size]; |
| 282 | dst = nal->rbsp_buffer; |
| 283 |
| 284 | memcpy(dst, src, i); |
| 285 | si = di = i; |
| 286 | while (si + 2 < length) { |
| 287 | if (src[si + 2] > 3) { |
| 288 | dst[di++] = src[si++]; |
| 289 | dst[di++] = src[si++]; |
| 290 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
| 291 | if (src[si + 2] == 3) { |
| 292 | dst[di++] = 0; |
| 293 | dst[di++] = 0; |
| 294 | si += 3; |
| 295 |
| 296 | if (nal->skipped_bytes_pos) { |
| 297 | nal->skipped_bytes++; |
| 298 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
| 299 | nal->skipped_bytes_pos_size *= 2; |
| 300 | av_reallocp_array(&nal->skipped_bytes_pos, |
| 301 | nal->skipped_bytes_pos_size, |
| 302 | sizeof(*nal->skipped_bytes_pos)); |
| 303 | if (!nal->skipped_bytes_pos) { |
| 304 | nal->skipped_bytes_pos_size = 0; |
| 305 | return AVERROR(ENOMEM); |
| 306 | } |
| 307 | } |
| 308 | if (nal->skipped_bytes_pos) |
| 309 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; |
| 310 | } |
| 311 | continue; |
| 312 | } else // next start code |
| 313 | goto nsc; |
| 314 | } |
| 315 |
| 316 | dst[di++] = src[si++]; |
| 317 | } |
| 318 | while (si < length) |
| 319 | dst[di++] = src[si++]; |
| 320 |
| 321 | nsc: |
| 322 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 323 |
| 324 | nal->data = dst; |
| 325 | nal->size = di; |
| 326 | nal->raw_data = src; |
| 327 | nal->raw_size = si; |
| 328 | rbsp->rbsp_buffer_size += si; |
| 329 |
| 330 | return si; |
| 331 | } |
| 332 |
| 333 | static inline int get_nalsize(int nal_length_size, const uint8_t *buf, int buf_size, int *buf_index) { |
| 334 | int i, nalsize = 0; |
| 335 | if (*buf_index >= buf_size - nal_length_size) { |
| 336 | return AVERROR(EAGAIN); |
| 337 | } |
| 338 | for (i = 0; i < nal_length_size; i++) |
| 339 | nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++]; |
| 340 | if (nalsize <= 0 || nalsize > buf_size - *buf_index) { |
| 341 | return AVERROR_INVALIDDATA; |
| 342 | } |
| 343 | return nalsize; |
| 344 | } |
| 345 |
| 346 | static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc) { |
| 347 | int i = 0; |
| 348 | if (buf + 3 >= next_avc) |
| 349 | return next_avc - buf; |
| 350 | while (buf + i + 3 < next_avc) { |
| 351 | if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) |
| 352 | break; |
| 353 | i++; |
| 354 | } |
| 355 | return i + 3; |
| 356 | } |
| 357 |
| 358 | static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros) { |
| 359 | int size = nal->size; |
| 360 | int v; |
| 361 |
| 362 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
| 363 | size--; |
| 364 |
| 365 | if (!size) |
| 366 | return 0; |
| 367 |
| 368 | v = nal->data[size - 1]; |
| 369 |
| 370 | if (size > INT_MAX / 8) |
| 371 | return AVERROR(ERANGE); |
| 372 | size *= 8; |
| 373 |
| 374 | /* remove the stop bit and following trailing zeros, |
| 375 | * or nothing for damaged bitstreams */ |
| 376 | if (v) |
| 377 | size -= ff_ctz(v) + 1; |
| 378 |
| 379 | return size; |
| 380 | } |
| 381 |
| 382 | static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size) { |
| 383 | int min_size = size; |
| 384 |
| 385 | if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
| 386 | goto fail; |
| 387 | size += AV_INPUT_BUFFER_PADDING_SIZE; |
| 388 |
| 389 | if (rbsp->rbsp_buffer_alloc_size >= size && |
| 390 | (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) { |
| 391 | memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 392 | return; |
| 393 | } |
| 394 |
| 395 | size = FFMIN(size + size / 16 + 32, INT_MAX); |
| 396 |
| 397 | if (rbsp->rbsp_buffer_ref) |
| 398 | av_buffer_unref(&rbsp->rbsp_buffer_ref); |
| 399 | else |
| 400 | av_free(rbsp->rbsp_buffer); |
| 401 |
| 402 | rbsp->rbsp_buffer = (uint8_t *) av_mallocz(size); |
| 403 | if (!rbsp->rbsp_buffer) |
| 404 | goto fail; |
| 405 | rbsp->rbsp_buffer_alloc_size = size; |
| 406 |
| 407 | return; |
| 408 |
| 409 | fail: |
| 410 | rbsp->rbsp_buffer_alloc_size = 0; |
| 411 | if (rbsp->rbsp_buffer_ref) { |
| 412 | av_buffer_unref(&rbsp->rbsp_buffer_ref); |
| 413 | rbsp->rbsp_buffer = NULL; |
| 414 | } else |
| 415 | av_freep(&rbsp->rbsp_buffer); |
| 416 |
| 417 | return; |
| 418 | } |
| 419 |
| 420 | static int h264_parse_nal_header(H2645NAL *nal) { |
| 421 | GetBitContext *gb = &nal->gb; |
| 422 |
| 423 | if (get_bits1(gb) != 0) |
| 424 | return AVERROR_INVALIDDATA; |
| 425 |
| 426 | nal->ref_idc = get_bits(gb, 2); |
| 427 | nal->type = get_bits(gb, 5); |
| 428 |
| 429 | return 1; |
| 430 | } |
| 431 |
| 432 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, int is_nalff, int nal_length_size) { |
| 433 | GetByteContext bc; |
| 434 | int consumed, ret = 0; |
| 435 | int next_avc = is_nalff ? 0 : length; |
| 436 | int64_t padding = MAX_MBPAIR_SIZE; |
| 437 |
| 438 | bytestream2_init(&bc, buf, length); |
| 439 | alloc_rbsp_buffer(&pkt->rbsp, length + padding); |
| 440 |
| 441 | if (!pkt->rbsp.rbsp_buffer) |
| 442 | return AVERROR(ENOMEM); |
| 443 |
| 444 | pkt->rbsp.rbsp_buffer_size = 0; |
| 445 | pkt->nb_nals = 0; |
| 446 | while (bytestream2_get_bytes_left(&bc) >= 4) { |
| 447 | H2645NAL *nal; |
| 448 | int extract_length = 0; |
| 449 | int skip_trailing_zeros = 1; |
| 450 |
| 451 | if (bytestream2_tell(&bc) == next_avc) { |
| 452 | int i = 0; |
| 453 | extract_length = get_nalsize(nal_length_size, bc.buffer, bytestream2_get_bytes_left(&bc), &i); |
| 454 | if (extract_length < 0) |
| 455 | return extract_length; |
| 456 |
| 457 | bytestream2_skip(&bc, nal_length_size); |
| 458 |
| 459 | next_avc = bytestream2_tell(&bc) + extract_length; |
| 460 | } else { |
| 461 | int buf_index; |
| 462 | buf_index = find_next_start_code(bc.buffer, buf + next_avc); |
| 463 | bytestream2_skip(&bc, buf_index); |
| 464 | if (!bytestream2_get_bytes_left(&bc)) { |
| 465 | if (pkt->nb_nals > 0) { |
| 466 | return 0; |
| 467 | } else { |
| 468 | return AVERROR_INVALIDDATA; |
| 469 | } |
| 470 | } |
| 471 | extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc)); |
| 472 | if (bytestream2_tell(&bc) >= next_avc) { |
| 473 | bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc)); |
| 474 | continue; |
| 475 | } |
| 476 | } |
| 477 |
| 478 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
| 479 | int new_size = pkt->nals_allocated + 1; |
| 480 | void *tmp; |
| 481 |
| 482 | if (new_size >= INT_MAX / sizeof(*pkt->nals)) |
| 483 | return AVERROR(ENOMEM); |
| 484 |
| 485 | tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals)); |
| 486 | if (!tmp) |
| 487 | return AVERROR(ENOMEM); |
| 488 |
| 489 | pkt->nals = (H2645NAL *) tmp; |
| 490 | memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals)); |
| 491 |
| 492 | nal = &pkt->nals[pkt->nb_nals]; |
| 493 | nal->skipped_bytes_pos_size = 1024; |
| 494 | nal->skipped_bytes_pos = (int *) av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); |
| 495 | if (!nal->skipped_bytes_pos) |
| 496 | return AVERROR(ENOMEM); |
| 497 |
| 498 | pkt->nals_allocated = new_size; |
| 499 | } |
| 500 | nal = &pkt->nals[pkt->nb_nals]; |
| 501 |
| 502 | consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal); |
| 503 | if (consumed < 0) |
| 504 | return consumed; |
| 505 |
| 506 | pkt->nb_nals++; |
| 507 |
| 508 | bytestream2_skip(&bc, consumed); |
| 509 |
| 510 | /* see commit 3566042a0 */ |
| 511 | if (bytestream2_get_bytes_left(&bc) >= 4 && |
| 512 | bytestream2_peek_be32(&bc) == 0x000001E0) |
| 513 | skip_trailing_zeros = 0; |
| 514 |
| 515 | nal->size_bits = get_bit_length(nal, skip_trailing_zeros); |
| 516 |
| 517 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); |
| 518 | if (ret < 0) |
| 519 | return ret; |
| 520 |
| 521 | ret = h264_parse_nal_header(nal); |
| 522 | if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) { |
| 523 | pkt->nb_nals--; |
| 524 | } |
| 525 | } |
| 526 |
| 527 | return 0; |
| 528 | } |
| 529 |
| 530 | #define MAX_SPS_COUNT 32 |
| 531 |
| 532 | const uint8_t ff_zigzag_direct[64] = { |
| 533 | 0, 1, 8, 16, 9, 2, 3, 10, |
| 534 | 17, 24, 32, 25, 18, 11, 4, 5, |
| 535 | 12, 19, 26, 33, 40, 48, 41, 34, |
| 536 | 27, 20, 13, 6, 7, 14, 21, 28, |
| 537 | 35, 42, 49, 56, 57, 50, 43, 36, |
| 538 | 29, 22, 15, 23, 30, 37, 44, 51, |
| 539 | 58, 59, 52, 45, 38, 31, 39, 46, |
| 540 | 53, 60, 61, 54, 47, 55, 62, 63 |
| 541 | }; |
| 542 |
| 543 | const uint8_t ff_zigzag_scan[16+1] = { |
| 544 | 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, |
| 545 | 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4, |
| 546 | 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4, |
| 547 | 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4, |
| 548 | }; |
| 549 |
| 550 | static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size) { |
| 551 | int i, last = 8, next = 8; |
| 552 | const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct; |
| 553 | if (!get_bits1(gb)) { |
| 554 |
| 555 | } else { |
| 556 | for (i = 0; i < size; i++) { |
| 557 | if (next) { |
| 558 | int v = get_se_golomb(gb); |
| 559 | if (v < -128 || v > 127) { |
| 560 | return AVERROR_INVALIDDATA; |
| 561 | } |
| 562 | next = (last + v) & 0xff; |
| 563 | } |
| 564 | if (!i && !next) { /* matrix not written, we use the preset one */ |
| 565 | break; |
| 566 | } |
| 567 | last = factors[scan[i]] = next ? next : last; |
| 568 | } |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 |
| 573 | static int decode_scaling_matrices(GetBitContext *gb, int chroma_format_idc, uint8_t(*scaling_matrix4)[16], uint8_t(*scaling_matrix8)[64]) { |
| 574 | int ret = 0; |
| 575 | if (get_bits1(gb)) { |
| 576 | ret |= decode_scaling_list(gb, scaling_matrix4[0], 16); // Intra, Y |
| 577 | ret |= decode_scaling_list(gb, scaling_matrix4[1], 16); // Intra, Cr |
| 578 | ret |= decode_scaling_list(gb, scaling_matrix4[2], 16); // Intra, Cb |
| 579 | ret |= decode_scaling_list(gb, scaling_matrix4[3], 16); // Inter, Y |
| 580 | ret |= decode_scaling_list(gb, scaling_matrix4[4], 16); // Inter, Cr |
| 581 | ret |= decode_scaling_list(gb, scaling_matrix4[5], 16); // Inter, Cb |
| 582 |
| 583 | ret |= decode_scaling_list(gb, scaling_matrix8[0], 64); // Intra, Y |
| 584 | ret |= decode_scaling_list(gb, scaling_matrix8[3], 64); // Inter, Y |
| 585 | if (chroma_format_idc == 3) { |
| 586 | ret |= decode_scaling_list(gb, scaling_matrix8[1], 64); // Intra, Cr |
| 587 | ret |= decode_scaling_list(gb, scaling_matrix8[4], 64); // Inter, Cr |
| 588 | ret |= decode_scaling_list(gb, scaling_matrix8[2], 64); // Intra, Cb |
| 589 | ret |= decode_scaling_list(gb, scaling_matrix8[5], 64); // Inter, Cb |
| 590 | } |
| 591 | if (!ret) |
| 592 | ret = 1; |
| 593 | } |
| 594 |
| 595 | return ret; |
| 596 | } |
| 597 |
| 598 | int ff_h264_decode_seq_parameter_set(GetBitContext *gb, int &width, int &height) { |
| 599 | int profile_idc, level_idc, constraint_set_flags = 0; |
| 600 | unsigned int sps_id; |
| 601 | int i, log2_max_frame_num_minus4; |
| 602 | int ret; |
| 603 |
| 604 | profile_idc = get_bits(gb, 8); |
| 605 | constraint_set_flags |= get_bits1(gb) << 0; |
| 606 | constraint_set_flags |= get_bits1(gb) << 1; |
| 607 | constraint_set_flags |= get_bits1(gb) << 2; |
| 608 | constraint_set_flags |= get_bits1(gb) << 3; |
| 609 | constraint_set_flags |= get_bits1(gb) << 4; |
| 610 | constraint_set_flags |= get_bits1(gb) << 5; |
| 611 | skip_bits(gb, 2); |
| 612 | level_idc = get_bits(gb, 8); |
| 613 | sps_id = get_ue_golomb_31(gb); |
| 614 |
| 615 | if (sps_id >= MAX_SPS_COUNT) { |
| 616 | return false; |
| 617 | } |
| 618 |
| 619 | if (profile_idc == 100 || // High profile |
| 620 | profile_idc == 110 || // High10 profile |
| 621 | profile_idc == 122 || // High422 profile |
| 622 | profile_idc == 244 || // High444 Predictive profile |
| 623 | profile_idc == 44 || // Cavlc444 profile |
| 624 | profile_idc == 83 || // Scalable Constrained High profile (SVC) |
| 625 | profile_idc == 86 || // Scalable High Intra profile (SVC) |
| 626 | profile_idc == 118 || // Stereo High profile (MVC) |
| 627 | profile_idc == 128 || // Multiview High profile (MVC) |
| 628 | profile_idc == 138 || // Multiview Depth High profile (MVCD) |
| 629 | profile_idc == 144) { // old High444 profile |
| 630 | int chroma_format_idc = get_ue_golomb_31(gb); |
| 631 | if (chroma_format_idc > 3U) { |
| 632 | return false; |
| 633 | } else if (chroma_format_idc == 3) { |
| 634 | int residual_color_transform_flag = get_bits1(gb); |
| 635 | if (residual_color_transform_flag) { |
| 636 | return false; |
| 637 | } |
| 638 | } |
| 639 | int bit_depth_luma = get_ue_golomb(gb) + 8; |
| 640 | int bit_depth_chroma = get_ue_golomb(gb) + 8; |
| 641 | if (bit_depth_chroma != bit_depth_luma) { |
| 642 | return false; |
| 643 | } |
| 644 | if (bit_depth_luma < 8 || bit_depth_luma > 14 || bit_depth_chroma < 8 || bit_depth_chroma > 14) { |
| 645 | return false; |
| 646 | } |
| 647 | get_bits1(gb); |
| 648 | uint8_t scaling_matrix4[6][16]; |
| 649 | uint8_t scaling_matrix8[6][64]; |
| 650 | ret = decode_scaling_matrices(gb, chroma_format_idc, scaling_matrix4, scaling_matrix8); |
| 651 | if (ret < 0) |
| 652 | return false; |
| 653 | } |
| 654 |
| 655 | get_ue_golomb(gb); |
| 656 |
| 657 | int poc_type = get_ue_golomb_31(gb); |
| 658 |
| 659 | if (poc_type == 0) { |
| 660 | unsigned t = get_ue_golomb(gb); |
| 661 | if (t > 12) { |
| 662 | return false; |
| 663 | } |
| 664 | } else if (poc_type == 1) { |
| 665 | get_bits1(gb); |
| 666 | int offset_for_non_ref_pic = get_se_golomb_long(gb); |
| 667 | int offset_for_top_to_bottom_field = get_se_golomb_long(gb); |
| 668 |
| 669 | if (offset_for_non_ref_pic == INT32_MIN || offset_for_top_to_bottom_field == INT32_MIN) { |
| 670 | return false; |
| 671 | } |
| 672 |
| 673 | int poc_cycle_length = get_ue_golomb(gb); |
| 674 |
| 675 | if ((unsigned) poc_cycle_length >= 256) { |
| 676 | return false; |
| 677 | } |
| 678 |
| 679 | for (i = 0; i < poc_cycle_length; i++) { |
| 680 | int offset_for_ref_frame = get_se_golomb_long(gb); |
| 681 | if (offset_for_ref_frame == INT32_MIN) { |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | } else if (poc_type != 2) { |
| 686 | return false; |
| 687 | } |
| 688 |
| 689 | get_ue_golomb_31(gb); |
| 690 | get_bits1(gb); |
| 691 | int mb_width = get_ue_golomb(gb) + 1; |
| 692 | int mb_height = get_ue_golomb(gb) + 1; |
| 693 |
| 694 | if (width == 0 || height == 0) { |
| 695 | width = mb_width; |
| 696 | height = mb_height; |
| 697 | } |
| 698 | return mb_width != width || mb_height != height; |
| 699 | } |
| 700 |
| 701 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 702 | int ret = 0; |
| 703 | int decoded = info->pkt.size; |
| 704 | *got_frame = 0; |
| 705 |
| 706 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 707 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 && decoded > 0) { |
| 708 | ff_h2645_packet_split(&info->h2645Packet, info->pkt.data, info->pkt.size, 1, 4); |
| 709 | for (int i = 0; i < info->h2645Packet.nb_nals; i++) { |
| 710 | H2645NAL *nal = &info->h2645Packet.nals[i]; |
| 711 | switch (nal->type) { |
| 712 | case 7: { |
| 713 | GetBitContext tmp_gb = nal->gb; |
| 714 | info->dropFrames = ff_h264_decode_seq_parameter_set(&tmp_gb, info->firstWidth, info->firstHeight); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | if (!info->dropFrames) { |
| 720 | ret = avcodec_decode_video2(info->video_dec_ctx, info->frame, got_frame, &info->pkt); |
| 721 | if (ret != 0) { |
| 722 | return ret; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 |
| 727 | return decoded; |
| 728 | } |
| 729 |
| 730 | void requestFd(VideoInfo *info) { |
| 731 | JNIEnv *jniEnv = nullptr; |
| 732 |
| 733 | JavaVMAttachArgs jvmArgs; |
| 734 | jvmArgs.version = JNI_VERSION_1_6; |
| 735 |
| 736 | bool attached; |
| 737 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 738 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 739 | attached = true; |
| 740 | } else { |
| 741 | attached = false; |
| 742 | } |
| 743 | jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) 0, (jint) 1); |
| 744 | jboolean loaded = jniEnv->CallBooleanMethod(info->stream, jclass_AnimatedFileDrawableStream_isFinishedLoadingFile); |
| 745 | if (loaded) { |
| 746 | delete[] info->src; |
| 747 | jstring src = (jstring) jniEnv->CallObjectMethod(info->stream, jclass_AnimatedFileDrawableStream_getFinishedFilePath); |
| 748 | char const *srcString = jniEnv->GetStringUTFChars(src, 0); |
| 749 | size_t len = strlen(srcString); |
| 750 | info->src = new char[len + 1]; |
| 751 | memcpy(info->src, srcString, len); |
| 752 | info->src[len] = '\0'; |
| 753 | if (srcString != 0) { |
| 754 | jniEnv->ReleaseStringUTFChars(src, srcString); |
| 755 | } |
| 756 | } |
| 757 |
| 758 | if (attached) { |
| 759 | javaVm->DetachCurrentThread(); |
| 760 | } |
| 761 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 762 | } |
| 763 |
| 764 | int readCallback(void *opaque, uint8_t *buf, int buf_size) { |
| 765 | VideoInfo *info = (VideoInfo *) opaque; |
| 766 | if (!info->stopped) { |
| 767 | if (info->fd < 0) { |
| 768 | requestFd(info); |
| 769 | } |
| 770 | if (info->fd >= 0) { |
| 771 | if (info->last_seek_p + buf_size > info->file_size) { |
| 772 | buf_size = (int) (info->file_size - info->last_seek_p); |
| 773 | } |
| 774 | if (buf_size > 0) { |
| 775 | JNIEnv *jniEnv = nullptr; |
| 776 |
| 777 | JavaVMAttachArgs jvmArgs; |
| 778 | jvmArgs.version = JNI_VERSION_1_6; |
| 779 |
| 780 | bool attached; |
| 781 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 782 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 783 | attached = true; |
| 784 | } else { |
| 785 | attached = false; |
| 786 | } |
| 787 |
| 788 | buf_size = jniEnv->CallIntMethod(info->stream, jclass_AnimatedFileDrawableStream_read, (jint) info->last_seek_p, (jint) buf_size); |
| 789 | info->last_seek_p += buf_size; |
| 790 | if (attached) { |
| 791 | javaVm->DetachCurrentThread(); |
| 792 | } |
| 793 | int ret = (int) read(info->fd, buf, (size_t) buf_size); |
| 794 | return ret ? ret : AVERROR_EOF; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | return AVERROR_EOF; |
| 799 | } |
| 800 |
| 801 | int64_t seekCallback(void *opaque, int64_t offset, int whence) { |
| 802 | VideoInfo *info = (VideoInfo *) opaque; |
| 803 | if (!info->stopped) { |
| 804 | if (info->fd < 0) { |
| 805 | requestFd(info); |
| 806 | } |
| 807 | if (info->fd >= 0) { |
| 808 | if (whence & FFMPEG_AVSEEK_SIZE) { |
| 809 | return info->file_size; |
| 810 | } else { |
| 811 | info->last_seek_p = offset; |
| 812 | lseek(info->fd, off_t(offset), SEEK_SET); |
| 813 | return offset; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 |
| 820 | enum PARAM_NUM { |
| 821 | PARAM_NUM_SUPPORTED_VIDEO_CODEC = 0, |
| 822 | PARAM_NUM_WIDTH = 1, |
| 823 | PARAM_NUM_HEIGHT = 2, |
| 824 | PARAM_NUM_BITRATE = 3, |
| 825 | PARAM_NUM_DURATION = 4, |
| 826 | PARAM_NUM_AUDIO_FRAME_SIZE = 5, |
| 827 | PARAM_NUM_VIDEO_FRAME_SIZE = 6, |
| 828 | PARAM_NUM_FRAMERATE = 7, |
| 829 | PARAM_NUM_ROTATION = 8, |
| 830 | PARAM_NUM_SUPPORTED_AUDIO_CODEC = 9, |
| 831 | PARAM_NUM_HAS_AUDIO = 10, |
| 832 | PARAM_NUM_COUNT = 11, |
| 833 | }; |
| 834 |
| 835 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoInfo(JNIEnv *env, jclass clazz,jint sdkVersion, jstring src, jintArray data) { |
| 836 | VideoInfo *info = new VideoInfo(); |
| 837 |
| 838 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 839 | size_t len = strlen(srcString); |
| 840 | info->src = new char[len + 1]; |
| 841 | memcpy(info->src, srcString, len); |
| 842 | info->src[len] = '\0'; |
| 843 | if (srcString != nullptr) { |
| 844 | env->ReleaseStringUTFChars(src, srcString); |
| 845 | } |
| 846 |
| 847 | int ret; |
| 848 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 849 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 850 | delete info; |
| 851 | return; |
| 852 | } |
| 853 |
| 854 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 855 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 856 | delete info; |
| 857 | return; |
| 858 | } |
| 859 |
| 860 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0)) >= 0) { |
| 861 | info->video_stream = info->fmt_ctx->streams[ret]; |
| 862 | } |
| 863 |
| 864 | if ((ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) >= 0) { |
| 865 | info->audio_stream = info->fmt_ctx->streams[ret]; |
| 866 | } |
| 867 |
| 868 | if (info->video_stream == nullptr) { |
| 869 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 870 | delete info; |
| 871 | return; |
| 872 | } |
| 873 |
| 874 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 875 | if (dataArr != nullptr) { |
| 876 | //https://developer.android.com/guide/topics/media/media-formats |
| 877 | dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC] = |
| 878 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || |
| 879 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_H263 || |
| 880 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_MPEG4 || |
| 881 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP8 || |
| 882 | info->video_stream->codecpar->codec_id == AV_CODEC_ID_VP9 || |
| 883 | (sdkVersion > 21 && info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC); |
| 884 |
| 885 | if (strstr(info->fmt_ctx->iformat->name, "mov") != 0 && dataArr[PARAM_NUM_SUPPORTED_VIDEO_CODEC]) { |
| 886 | MOVStreamContext *mov = (MOVStreamContext *) info->video_stream->priv_data; |
| 887 | dataArr[PARAM_NUM_VIDEO_FRAME_SIZE] = (jint) mov->data_size; |
| 888 |
| 889 | if (info->audio_stream != nullptr) { |
| 890 | mov = (MOVStreamContext *) info->audio_stream->priv_data; |
| 891 | dataArr[PARAM_NUM_AUDIO_FRAME_SIZE] = (jint) mov->data_size; |
| 892 | } |
| 893 | } |
| 894 |
| 895 | if (info->audio_stream != nullptr) { |
| 896 | //https://developer.android.com/guide/topics/media/media-formats |
| 897 | dataArr[PARAM_NUM_SUPPORTED_AUDIO_CODEC] = |
| 898 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC || |
| 899 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AAC_LATM || |
| 900 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_VORBIS || |
| 901 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_NB || |
| 902 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_AMR_WB || |
| 903 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_FLAC || |
| 904 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_MP3 || |
| 905 | // not supported codec, skip audio in this case |
| 906 | info->audio_stream->codecpar->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV || |
| 907 | (sdkVersion > 21 && info->audio_stream->codecpar->codec_id == AV_CODEC_ID_OPUS); |
| 908 | dataArr[PARAM_NUM_HAS_AUDIO] = 1; |
| 909 | } else { |
| 910 | dataArr[PARAM_NUM_HAS_AUDIO] = 0; |
| 911 | } |
| 912 |
| 913 | dataArr[PARAM_NUM_BITRATE] = (jint) info->video_stream->codecpar->bit_rate; |
| 914 | dataArr[PARAM_NUM_WIDTH] = info->video_stream->codecpar->width; |
| 915 | dataArr[PARAM_NUM_HEIGHT] = info->video_stream->codecpar->height; |
| 916 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 917 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0") != 0) { |
| 918 | char *tail; |
| 919 | dataArr[PARAM_NUM_ROTATION] = (jint) av_strtod(rotate_tag->value, &tail); |
| 920 | if (*tail) { |
| 921 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 922 | } |
| 923 | } else { |
| 924 | dataArr[PARAM_NUM_ROTATION] = 0; |
| 925 | } |
| 926 | if (info->video_stream->codecpar->codec_id == AV_CODEC_ID_H264 || info->video_stream->codecpar->codec_id == AV_CODEC_ID_HEVC) { |
| 927 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->avg_frame_rate); |
| 928 | } else { |
| 929 | dataArr[PARAM_NUM_FRAMERATE] = (jint) av_q2d(info->video_stream->r_frame_rate); |
| 930 | } |
| 931 | dataArr[PARAM_NUM_DURATION] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 932 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 933 | delete info; |
| 934 | } |
| 935 | } |
| 936 |
| 937 | 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) { |
| 938 | VideoInfo *info = new VideoInfo(); |
| 939 |
| 940 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 941 | size_t len = strlen(srcString); |
| 942 | info->src = new char[len + 1]; |
| 943 | memcpy(info->src, srcString, len); |
| 944 | info->src[len] = '\0'; |
| 945 | if (srcString != 0) { |
| 946 | env->ReleaseStringUTFChars(src, srcString); |
| 947 | } |
| 948 |
| 949 | int ret; |
| 950 | if (streamFileSize != 0) { |
| 951 | info->file_size = streamFileSize; |
| 952 | info->stream = env->NewGlobalRef(stream); |
| 953 | info->account = account; |
| 954 | info->fd = open(info->src, O_RDONLY, S_IRUSR); |
| 955 |
| 956 | info->ioBuffer = (unsigned char *) av_malloc(64 * 1024); |
| 957 | info->ioContext = avio_alloc_context(info->ioBuffer, 64 * 1024, 0, info, readCallback, nullptr, seekCallback); |
| 958 | if (info->ioContext == nullptr) { |
| 959 | delete info; |
| 960 | return 0; |
| 961 | } |
| 962 |
| 963 | info->fmt_ctx = avformat_alloc_context(); |
| 964 | info->fmt_ctx->pb = info->ioContext; |
| 965 |
| 966 | AVDictionary *options = NULL; |
| 967 | av_dict_set(&options, "usetoc", "1", 0); |
| 968 | ret = avformat_open_input(&info->fmt_ctx, "http://localhost/file", NULL, &options); |
| 969 | av_dict_free(&options); |
| 970 | if (ret < 0) { |
| 971 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 972 | delete info; |
| 973 | return 0; |
| 974 | } |
| 975 | info->fmt_ctx->flags |= AVFMT_FLAG_FAST_SEEK; |
| 976 | if (preview) { |
| 977 | info->fmt_ctx->flags |= AVFMT_FLAG_NOBUFFER; |
| 978 | } |
| 979 | } else { |
| 980 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 981 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 982 | delete info; |
| 983 | return 0; |
| 984 | } |
| 985 | } |
| 986 |
| 987 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 988 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 989 | delete info; |
| 990 | return 0; |
| 991 | } |
| 992 |
| 993 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 994 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 995 | } |
| 996 |
| 997 | if (info->video_stream == nullptr) { |
| 998 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 999 | delete info; |
| 1000 | return 0; |
| 1001 | } |
| 1002 |
| 1003 | info->frame = av_frame_alloc(); |
| 1004 | if (info->frame == nullptr) { |
| 1005 | LOGE("can't allocate frame %s", info->src); |
| 1006 | delete info; |
| 1007 | return 0; |
| 1008 | } |
| 1009 |
| 1010 | av_init_packet(&info->pkt); |
| 1011 | info->pkt.data = NULL; |
| 1012 | info->pkt.size = 0; |
| 1013 |
| 1014 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 1015 | if (dataArr != nullptr) { |
| 1016 | dataArr[0] = info->video_dec_ctx->width; |
| 1017 | dataArr[1] = info->video_dec_ctx->height; |
| 1018 | //float pixelWidthHeightRatio = info->video_dec_ctx->sample_aspect_ratio.num / info->video_dec_ctx->sample_aspect_ratio.den; TODO support |
| 1019 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 1020 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 1021 | char *tail; |
| 1022 | dataArr[2] = (jint) av_strtod(rotate_tag->value, &tail); |
| 1023 | if (*tail) { |
| 1024 | dataArr[2] = 0; |
| 1025 | } |
| 1026 | } else { |
| 1027 | dataArr[2] = 0; |
| 1028 | } |
| 1029 | dataArr[4] = (int32_t) (info->fmt_ctx->duration * 1000 / AV_TIME_BASE); |
| 1030 | //(int32_t) (1000 * info->video_stream->duration * av_q2d(info->video_stream->time_base)); |
| 1031 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 1032 | } |
| 1033 |
| 1034 | //LOGD("successfully opened file %s", info->src); |
| 1035 |
| 1036 | return (jlong) (intptr_t) info; |
| 1037 | } |
| 1038 |
| 1039 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1040 | if (ptr == NULL) { |
| 1041 | return; |
| 1042 | } |
| 1043 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1044 | if (info->stream != nullptr) { |
| 1045 | JNIEnv *jniEnv = nullptr; |
| 1046 | JavaVMAttachArgs jvmArgs; |
| 1047 | jvmArgs.version = JNI_VERSION_1_6; |
| 1048 |
| 1049 | bool attached; |
| 1050 | if (JNI_EDETACHED == javaVm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6)) { |
| 1051 | javaVm->AttachCurrentThread(&jniEnv, &jvmArgs); |
| 1052 | attached = true; |
| 1053 | } else { |
| 1054 | attached = false; |
| 1055 | } |
| 1056 | jniEnv->CallVoidMethod(info->stream, jclass_AnimatedFileDrawableStream_cancel); |
| 1057 | if (attached) { |
| 1058 | javaVm->DetachCurrentThread(); |
| 1059 | } |
| 1060 | } |
| 1061 | delete info; |
| 1062 | } |
| 1063 |
| 1064 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_stopDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1065 | if (ptr == NULL) { |
| 1066 | return; |
| 1067 | } |
| 1068 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1069 | info->stopped = true; |
| 1070 | } |
| 1071 |
| 1072 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_prepareToSeek(JNIEnv *env, jclass clazz, jlong ptr) { |
| 1073 | if (ptr == NULL) { |
| 1074 | return; |
| 1075 | } |
| 1076 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1077 | info->seeking = true; |
| 1078 | } |
| 1079 |
| 1080 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_ui_Components_AnimatedFileDrawable_seekToMs(JNIEnv *env, jclass clazz, jlong ptr, jlong ms, jboolean precise) { |
| 1081 | if (ptr == NULL) { |
| 1082 | return; |
| 1083 | } |
| 1084 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1085 | info->seeking = false; |
| 1086 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 1087 | int ret = 0; |
| 1088 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1089 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 1090 | return; |
| 1091 | } else { |
| 1092 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1093 | if (!precise) { |
| 1094 | return; |
| 1095 | } |
| 1096 | int got_frame = 0; |
| 1097 | int32_t tries = 1000; |
| 1098 | while (tries > 0) { |
| 1099 | if (info->pkt.size == 0) { |
| 1100 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1101 | if (ret >= 0) { |
| 1102 | info->orig_pkt = info->pkt; |
| 1103 | } |
| 1104 | } |
| 1105 |
| 1106 | if (info->pkt.size > 0) { |
| 1107 | ret = decode_packet(info, &got_frame); |
| 1108 | if (ret < 0) { |
| 1109 | if (info->has_decoded_frames) { |
| 1110 | ret = 0; |
| 1111 | } |
| 1112 | info->pkt.size = 0; |
| 1113 | } else { |
| 1114 | info->pkt.data += ret; |
| 1115 | info->pkt.size -= ret; |
| 1116 | } |
| 1117 | if (info->pkt.size == 0) { |
| 1118 | av_packet_unref(&info->orig_pkt); |
| 1119 | } |
| 1120 | } else { |
| 1121 | info->pkt.data = NULL; |
| 1122 | info->pkt.size = 0; |
| 1123 | ret = decode_packet(info, &got_frame); |
| 1124 | if (ret < 0) { |
| 1125 | return; |
| 1126 | } |
| 1127 | if (got_frame == 0) { |
| 1128 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 1129 | return; |
| 1130 | } |
| 1131 | } |
| 1132 | if (ret < 0) { |
| 1133 | return; |
| 1134 | } |
| 1135 | if (got_frame) { |
| 1136 | info->has_decoded_frames = true; |
| 1137 | bool finished = false; |
| 1138 | 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) { |
| 1139 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 1140 | if (pkt_pts >= pts) { |
| 1141 | finished = true; |
| 1142 | } |
| 1143 | } |
| 1144 | av_frame_unref(info->frame); |
| 1145 | if (finished) { |
| 1146 | return; |
| 1147 | } |
| 1148 | } |
| 1149 | tries--; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 |
| 1154 | uint32_t premultiply_channel_value(const uint32_t pixel, const uint8_t offset, const float normalizedAlpha) { |
| 1155 | auto multipliedValue = ((pixel >> offset) & 0xFF) * normalizedAlpha; |
| 1156 | return ((uint32_t)std::min(multipliedValue, 255.0f)) << offset; |
| 1157 | } |
| 1158 |
| 1159 | static inline void writeFrameToBitmap(JNIEnv *env, VideoInfo *info, jintArray data, jobject bitmap, jint stride) { |
| 1160 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 1161 | int32_t wantedWidth; |
| 1162 | int32_t wantedHeight; |
| 1163 |
| 1164 | AndroidBitmapInfo bitmapInfo; |
| 1165 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 1166 | int32_t bitmapWidth = bitmapInfo.width; |
| 1167 | int32_t bitmapHeight = bitmapInfo.height; |
| 1168 | if (dataArr != nullptr) { |
| 1169 | wantedWidth = dataArr[0]; |
| 1170 | wantedHeight = dataArr[1]; |
| 1171 | dataArr[3] = (jint) (1000 * info->frame->best_effort_timestamp * av_q2d(info->video_stream->time_base)); |
| 1172 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 1173 | } else { |
| 1174 | wantedWidth = bitmapWidth; |
| 1175 | wantedHeight = bitmapHeight; |
| 1176 | } |
| 1177 |
| 1178 | if (wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width) { |
| 1179 | void *pixels; |
| 1180 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 1181 | if (info->sws_ctx == nullptr) { |
| 1182 | if (info->frame->format > AV_PIX_FMT_NONE && info->frame->format < AV_PIX_FMT_NB && info->frame->format != AV_PIX_FMT_YUVA420P) { |
| 1183 | 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); |
| 1184 | } 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) { |
| 1185 | 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); |
| 1186 | } |
| 1187 | } |
| 1188 | if (info->sws_ctx == nullptr || ((intptr_t) pixels) % 16 != 0) { |
| 1189 | if (info->frame->format == AV_PIX_FMT_YUVA420P) { |
| 1190 | 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); |
| 1191 | } else if (info->frame->format == AV_PIX_FMT_YUV444P) { |
| 1192 | 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); |
| 1193 | } else if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 1194 | if (info->frame->colorspace == AVColorSpace::AVCOL_SPC_BT709) { |
| 1195 | 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); |
| 1196 | } else { |
| 1197 | 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); |
| 1198 | } |
| 1199 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 1200 | libyuv::ABGRToARGB(info->frame->data[0], info->frame->linesize[0], (uint8_t *) pixels, info->frame->width * 4, info->frame->width, info->frame->height); |
| 1201 | } |
| 1202 | } else { |
| 1203 | uint8_t __attribute__ ((aligned (16))) *dst_data[1]; |
| 1204 | dst_data[0] = (uint8_t *) pixels; |
| 1205 | info->dst_linesize[0] = stride; |
| 1206 | sws_scale(info->sws_ctx, info->frame->data, info->frame->linesize, 0, info->frame->height, dst_data, info->dst_linesize); |
| 1207 | } |
| 1208 | } |
| 1209 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1210 | } |
| 1211 | } |
| 1212 |
| 1213 | 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) { |
| 1214 | if (ptr == NULL || bitmap == nullptr || data == nullptr) { |
| 1215 | return 0; |
| 1216 | } |
| 1217 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1218 | info->seeking = false; |
| 1219 | int64_t pts = (int64_t) (ms / av_q2d(info->video_stream->time_base) / 1000); |
| 1220 | int ret = 0; |
| 1221 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, pts, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1222 | LOGE("can't seek file %s, %s", info->src, av_err2str(ret)); |
| 1223 | return 0; |
| 1224 | } else { |
| 1225 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1226 | int got_frame = 0; |
| 1227 | int32_t tries = 1000; |
| 1228 | bool readNextPacket = true; |
| 1229 | while (tries > 0) { |
| 1230 | if (info->pkt.size == 0 && readNextPacket) { |
| 1231 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1232 | if (ret >= 0) { |
| 1233 | info->orig_pkt = info->pkt; |
| 1234 | } |
| 1235 | } |
| 1236 |
| 1237 | if (info->pkt.size > 0) { |
| 1238 | ret = decode_packet(info, &got_frame); |
| 1239 | if (ret < 0) { |
| 1240 | if (info->has_decoded_frames) { |
| 1241 | ret = 0; |
| 1242 | } |
| 1243 | info->pkt.size = 0; |
| 1244 | } else { |
| 1245 | info->pkt.data += ret; |
| 1246 | info->pkt.size -= ret; |
| 1247 | } |
| 1248 | if (info->pkt.size == 0) { |
| 1249 | av_packet_unref(&info->orig_pkt); |
| 1250 | } |
| 1251 | } else { |
| 1252 | info->pkt.data = NULL; |
| 1253 | info->pkt.size = 0; |
| 1254 | ret = decode_packet(info, &got_frame); |
| 1255 | if (ret < 0) { |
| 1256 | return 0; |
| 1257 | } |
| 1258 | if (got_frame == 0) { |
| 1259 | av_seek_frame(info->fmt_ctx, info->video_stream_idx, 0, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); |
| 1260 | return 0; |
| 1261 | } |
| 1262 | } |
| 1263 | if (ret < 0) { |
| 1264 | return 0; |
| 1265 | } |
| 1266 | if (got_frame) { |
| 1267 | bool finished = false; |
| 1268 | 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) { |
| 1269 | int64_t pkt_pts = info->frame->best_effort_timestamp; |
| 1270 | bool isLastPacket = false; |
| 1271 | if (info->pkt.size == 0) { |
| 1272 | readNextPacket = false; |
| 1273 | isLastPacket = av_read_frame(info->fmt_ctx, &info->pkt) < 0; |
| 1274 | } |
| 1275 | if (pkt_pts >= pts || isLastPacket) { |
| 1276 | writeFrameToBitmap(env, info, data, bitmap, stride); |
| 1277 | finished = true; |
| 1278 | } |
| 1279 | } |
| 1280 | av_frame_unref(info->frame); |
| 1281 | if (finished) { |
| 1282 | return 1; |
| 1283 | } |
| 1284 | } else { |
| 1285 | readNextPacket = true; |
| 1286 | } |
| 1287 | tries--; |
| 1288 | } |
| 1289 | return 0; |
| 1290 | } |
| 1291 | } |
| 1292 |
| 1293 | 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) { |
| 1294 | if (ptr == NULL || bitmap == nullptr) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | //int64_t time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 1298 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 1299 | int ret = 0; |
| 1300 | int got_frame = 0; |
| 1301 | int32_t triesCount = preview ? 50 : 6; |
| 1302 | //info->has_decoded_frames = false; |
| 1303 | while (!info->stopped && triesCount != 0) { |
| 1304 | if (info->pkt.size == 0) { |
| 1305 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 1306 | if (ret >= 0) { |
| 1307 | double pts = info->pkt.pts * av_q2d(info->video_stream->time_base); |
| 1308 | if (end_time > 0 && info->pkt.stream_index == info->video_stream_idx && pts > end_time) { |
| 1309 | av_packet_unref(&info->pkt); |
| 1310 | info->pkt.data = NULL; |
| 1311 | info->pkt.size = 0; |
| 1312 | } else { |
| 1313 | info->orig_pkt = info->pkt; |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 |
| 1318 | if (info->pkt.size > 0) { |
| 1319 | ret = decode_packet(info, &got_frame); |
| 1320 | if (ret < 0) { |
| 1321 | if (info->has_decoded_frames) { |
| 1322 | ret = 0; |
| 1323 | } |
| 1324 | info->pkt.size = 0; |
| 1325 | } else { |
| 1326 | //LOGD("read size %d from packet", ret); |
| 1327 | info->pkt.data += ret; |
| 1328 | info->pkt.size -= ret; |
| 1329 | } |
| 1330 |
| 1331 | if (info->pkt.size == 0) { |
| 1332 | av_packet_unref(&info->orig_pkt); |
| 1333 | } |
| 1334 | } else { |
| 1335 | info->pkt.data = NULL; |
| 1336 | info->pkt.size = 0; |
| 1337 | ret = decode_packet(info, &got_frame); |
| 1338 | if (ret < 0) { |
| 1339 | LOGE("can't decode packet flushed %s", info->src); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | if (!preview && got_frame == 0) { |
| 1343 | if (info->has_decoded_frames) { |
| 1344 | int64_t start_from = 0; |
| 1345 | if (start_time > 0) { |
| 1346 | start_from = (int64_t)(start_time / av_q2d(info->video_stream->time_base)); |
| 1347 | } |
| 1348 | if ((ret = av_seek_frame(info->fmt_ctx, info->video_stream_idx, start_from, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME)) < 0) { |
| 1349 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 1350 | return 0; |
| 1351 | } else { |
| 1352 | avcodec_flush_buffers(info->video_dec_ctx); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | if (ret < 0 || info->seeking) { |
| 1358 | return 0; |
| 1359 | } |
| 1360 | if (got_frame) { |
| 1361 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 1362 | if (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) { |
| 1363 | writeFrameToBitmap(env, info, data, bitmap, stride); |
| 1364 | } |
| 1365 | info->has_decoded_frames = true; |
| 1366 | av_frame_unref(info->frame); |
| 1367 | return 1; |
| 1368 | } |
| 1369 | if (!info->has_decoded_frames) { |
| 1370 | triesCount--; |
| 1371 | } |
| 1372 | } |
| 1373 | return 0; |
| 1374 | } |
| 1375 |
| 1376 | extern "C" jint videoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 1377 | //av_log_set_callback(custom_log); |
| 1378 | jclass_AnimatedFileDrawableStream = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/AnimatedFileDrawableStream")); |
| 1379 | if (jclass_AnimatedFileDrawableStream == 0) { |
| 1380 | return JNI_FALSE; |
| 1381 | } |
| 1382 | jclass_AnimatedFileDrawableStream_read = env->GetMethodID(jclass_AnimatedFileDrawableStream, "read", "(II)I"); |
| 1383 | if (jclass_AnimatedFileDrawableStream_read == 0) { |
| 1384 | return JNI_FALSE; |
| 1385 | } |
| 1386 | jclass_AnimatedFileDrawableStream_cancel = env->GetMethodID(jclass_AnimatedFileDrawableStream, "cancel", "()V"); |
| 1387 | if (jclass_AnimatedFileDrawableStream_cancel == 0) { |
| 1388 | return JNI_FALSE; |
| 1389 | } |
| 1390 | jclass_AnimatedFileDrawableStream_isFinishedLoadingFile = env->GetMethodID(jclass_AnimatedFileDrawableStream, "isFinishedLoadingFile", "()Z"); |
| 1391 | if (jclass_AnimatedFileDrawableStream_isFinishedLoadingFile == 0) { |
| 1392 | return JNI_FALSE; |
| 1393 | } |
| 1394 | jclass_AnimatedFileDrawableStream_getFinishedFilePath = env->GetMethodID(jclass_AnimatedFileDrawableStream, "getFinishedFilePath", "()Ljava/lang/String;"); |
| 1395 | if (jclass_AnimatedFileDrawableStream_getFinishedFilePath == 0) { |
| 1396 | return JNI_FALSE; |
| 1397 | } |
| 1398 |
| 1399 | return JNI_TRUE; |
| 1400 | } |
| 1401 |