| 5 | #include <cstdint> |
| 6 | #include <limits> |
| 7 | #include <string> |
| 8 |
| 9 | extern "C" { |
| 10 | #include <libavformat/avformat.h> |
| 11 | #include <libavutil/eval.h> |
| 12 |
| 13 | static const std::string av_make_error_str(int errnum) { |
| 14 | char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 15 | av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); |
| 16 | return (std::string) errbuf; |
| 17 | } |
| 18 |
| 19 | #undef av_err2str |
| 20 | #define av_err2str(errnum) av_make_error_str(errnum).c_str() |
| 21 |
| 22 | typedef struct VideoInfo { |
| 23 |
| 24 | ~VideoInfo() { |
| 25 | if (video_dec_ctx) { |
| 26 | avcodec_close(video_dec_ctx); |
| 27 | video_dec_ctx = nullptr; |
| 28 | } |
| 29 | if (fmt_ctx) { |
| 30 | avformat_close_input(&fmt_ctx); |
| 31 | fmt_ctx = nullptr; |
| 32 | } |
| 33 | if (frame) { |
| 34 | av_frame_free(&frame); |
| 35 | frame = nullptr; |
| 36 | } |
| 37 | if (src) { |
| 38 | delete [] src; |
| 39 | src = nullptr; |
| 40 | } |
| 41 | av_packet_unref(&orig_pkt); |
| 42 |
| 43 | video_stream_idx = -1; |
| 44 | video_stream = nullptr; |
| 45 | } |
| 46 |
| 47 | AVFormatContext *fmt_ctx = nullptr; |
| 48 | char *src = nullptr; |
| 49 | int video_stream_idx = -1; |
| 50 | AVStream *video_stream = nullptr; |
| 51 | AVCodecContext *video_dec_ctx = nullptr; |
| 52 | AVFrame *frame = nullptr; |
| 53 | bool has_decoded_frames = false; |
| 54 | AVPacket pkt; |
| 55 | AVPacket orig_pkt; |
| 56 | }; |
| 57 |
| 58 | jobject makeGlobarRef(JNIEnv *env, jobject object) { |
| 59 | if (object) { |
| 60 | return env->NewGlobalRef(object); |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 |
| 65 | int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 66 | int ret, stream_index; |
| 67 | AVStream *st; |
| 68 | AVCodec *dec = NULL; |
| 69 | AVDictionary *opts = NULL; |
| 70 |
| 71 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 72 | if (ret < 0) { |
| 73 | LOGE("can't find %s stream in input file", av_get_media_type_string(type)); |
| 74 | return ret; |
| 75 | } else { |
| 76 | stream_index = ret; |
| 77 | st = fmt_ctx->streams[stream_index]; |
| 78 |
| 79 | dec = avcodec_find_decoder(st->codecpar->codec_id); |
| 80 | if (!dec) { |
| 81 | LOGE("failed to find %s codec", av_get_media_type_string(type)); |
| 82 | return AVERROR(EINVAL); |
| 83 | } |
| 84 |
| 85 | *dec_ctx = avcodec_alloc_context3(dec); |
| 86 | if (!*dec_ctx) { |
| 87 | LOGE("Failed to allocate the %s codec context", av_get_media_type_string(type)); |
| 88 | return AVERROR(ENOMEM); |
| 89 | } |
| 90 |
| 91 | if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { |
| 92 | LOGE("Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); |
| 93 | return ret; |
| 94 | } |
| 95 |
| 96 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 97 | if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { |
| 98 | LOGE("Failed to open %s codec", av_get_media_type_string(type)); |
| 99 | return ret; |
| 100 | } |
| 101 | *stream_idx = stream_index; |
| 102 | } |
| 103 |
| 104 | return 0; |
| 105 | } |
| 106 |
| 107 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 108 | int ret = 0; |
| 109 | int decoded = info->pkt.size; |
| 110 | *got_frame = 0; |
| 111 |
| 112 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 113 | ret = avcodec_decode_video2(info->video_dec_ctx, info->frame, got_frame, &info->pkt); |
| 114 | if (ret != 0) { |
| 115 | return ret; |
| 116 | } |
| 117 | } |
| 118 |
| 119 | return decoded; |
| 120 | } |
| 121 |
| 122 | jlong Java_org_telegram_ui_Components_AnimatedFileDrawable_createDecoder(JNIEnv *env, jclass clazz, jstring src, jintArray data) { |
| 123 | VideoInfo *info = new VideoInfo(); |
| 124 |
| 125 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 126 | size_t len = strlen(srcString); |
| 127 | info->src = new char[len + 1]; |
| 128 | memcpy(info->src, srcString, len); |
| 129 | info->src[len] = '\0'; |
| 130 | if (srcString != 0) { |
| 131 | env->ReleaseStringUTFChars(src, srcString); |
| 132 | } |
| 133 |
| 134 | int ret; |
| 135 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 136 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 137 | delete info; |
| 138 | return 0; |
| 139 | } |
| 140 |
| 141 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 142 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 143 | delete info; |
| 144 | return 0; |
| 145 | } |
| 146 |
| 147 | if (open_codec_context(&info->video_stream_idx, &info->video_dec_ctx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 148 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 149 | } |
| 150 |
| 151 | if (info->video_stream <= 0) { |
| 152 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 153 | delete info; |
| 154 | return 0; |
| 155 | } |
| 156 |
| 157 | info->frame = av_frame_alloc(); |
| 158 | if (info->frame == nullptr) { |
| 159 | LOGE("can't allocate frame %s", info->src); |
| 160 | delete info; |
| 161 | return 0; |
| 162 | } |
| 163 |
| 164 | av_init_packet(&info->pkt); |
| 165 | info->pkt.data = NULL; |
| 166 | info->pkt.size = 0; |
| 167 |
| 168 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 169 | if (dataArr != nullptr) { |
| 170 | dataArr[0] = info->video_dec_ctx->width; |
| 171 | dataArr[1] = info->video_dec_ctx->height; |
| 172 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 173 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 174 | char *tail; |
| 175 | dataArr[2] = (jint) av_strtod(rotate_tag->value, &tail); |
| 176 | if (*tail) { |
| 177 | dataArr[2] = 0; |
| 178 | } |
| 179 | } else { |
| 180 | dataArr[2] = 0; |
| 181 | } |
| 182 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 183 | } |
| 184 |
| 185 | //LOGD("successfully opened file %s", info->src); |
| 186 |
| 187 | return (jlong) (intptr_t) info; |
| 188 | } |
| 189 |
| 190 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jlong ptr) { |
| 191 | if (ptr == NULL) { |
| 192 | return; |
| 193 | } |
| 194 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 195 | delete info; |
| 196 | } |
| 197 |
| 198 |
| 199 | jint Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jintArray data) { |
| 200 | if (ptr == NULL || bitmap == nullptr) { |
| 201 | return 0; |
| 202 | } |
| 203 | VideoInfo *info = (VideoInfo *) (intptr_t) ptr; |
| 204 | int ret = 0; |
| 205 | int got_frame = 0; |
| 206 | int32_t triesCount = 6; |
| 207 | while (triesCount != 0) { |
| 208 | if (info->pkt.size == 0) { |
| 209 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 210 | //LOGD("got packet with size %d", info->pkt.size); |
| 211 | if (ret >= 0) { |
| 212 | info->orig_pkt = info->pkt; |
| 213 | } |
| 214 | } |
| 215 |
| 216 | if (info->pkt.size > 0) { |
| 217 | ret = decode_packet(info, &got_frame); |
| 218 | if (ret < 0) { |
| 219 | if (info->has_decoded_frames) { |
| 220 | ret = 0; |
| 221 | } |
| 222 | info->pkt.size = 0; |
| 223 | } else { |
| 224 | //LOGD("read size %d from packet", ret); |
| 225 | info->pkt.data += ret; |
| 226 | info->pkt.size -= ret; |
| 227 | } |
| 228 |
| 229 | if (info->pkt.size == 0) { |
| 230 | av_packet_unref(&info->orig_pkt); |
| 231 | } |
| 232 | } else { |
| 233 | info->pkt.data = NULL; |
| 234 | info->pkt.size = 0; |
| 235 | ret = decode_packet(info, &got_frame); |
| 236 | if (ret < 0) { |
| 237 | LOGE("can't decode packet flushed %s", info->src); |
| 238 | return 0; |
| 239 | } |
| 240 | if (got_frame == 0) { |
| 241 | if (info->has_decoded_frames) { |
| 242 | //LOGD("file end reached %s", info->src); |
| 243 | if ((ret = avformat_seek_file(info->fmt_ctx, -1, std::numeric_limits<int64_t>::min(), 0, std::numeric_limits<int64_t>::max(), 0)) < 0) { |
| 244 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 245 | return 0; |
| 246 | } else { |
| 247 | avcodec_flush_buffers(info->video_dec_ctx); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | if (ret < 0) { |
| 253 | return 0; |
| 254 | } |
| 255 | if (got_frame) { |
| 256 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 257 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 258 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 259 | int32_t wantedWidth; |
| 260 | int32_t wantedHeight; |
| 261 | if (dataArr != nullptr) { |
| 262 | wantedWidth = dataArr[0]; |
| 263 | wantedHeight = dataArr[1]; |
| 264 | dataArr[3] = (jint) (1000 * info->frame->pts * av_q2d(info->video_stream->time_base)); |
| 265 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 266 | } else { |
| 267 | AndroidBitmapInfo bitmapInfo; |
| 268 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 269 | wantedWidth = bitmapInfo.width; |
| 270 | wantedHeight = bitmapInfo.height; |
| 271 | } |
| 272 |
| 273 | void *pixels; |
| 274 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 275 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 276 | //LOGD("y %d, u %d, v %d, width %d, height %d", info->frame->linesize[0], info->frame->linesize[2], info->frame->linesize[1], info->frame->width, info->frame->height); |
| 277 | if (wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width) { |
| 278 | 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); |
| 279 | } |
| 280 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 281 | libyuv::ABGRToARGB(info->frame->data[0], info->frame->linesize[0], (uint8_t *) pixels, info->frame->width * 4, info->frame->width, info->frame->height); |
| 282 | } |
| 283 | AndroidBitmap_unlockPixels(env, bitmap); |
| 284 | } |
| 285 | } |
| 286 | info->has_decoded_frames = true; |
| 287 | av_frame_unref(info->frame); |
| 288 | return 1; |
| 289 | } |
| 290 | triesCount--; |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 |