| 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_free_packet(&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 gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 66 | av_register_all(); |
| 67 | return 0; |
| 68 | } |
| 69 |
| 70 | int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 71 | int ret; |
| 72 | AVStream *st; |
| 73 | AVCodecContext *dec_ctx = NULL; |
| 74 | AVCodec *dec = NULL; |
| 75 | AVDictionary *opts = NULL; |
| 76 |
| 77 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 78 | if (ret < 0) { |
| 79 | LOGE("can't find %s stream in input file\n", av_get_media_type_string(type)); |
| 80 | return ret; |
| 81 | } else { |
| 82 | *stream_idx = ret; |
| 83 | st = fmt_ctx->streams[*stream_idx]; |
| 84 |
| 85 | dec_ctx = st->codec; |
| 86 | dec = avcodec_find_decoder(dec_ctx->codec_id); |
| 87 | if (!dec) { |
| 88 | LOGE("failed to find %s codec\n", av_get_media_type_string(type)); |
| 89 | return ret; |
| 90 | } |
| 91 |
| 92 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 93 | if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) { |
| 94 | LOGE("failed to open %s codec\n", av_get_media_type_string(type)); |
| 95 | return ret; |
| 96 | } |
| 97 | } |
| 98 |
| 99 | return 0; |
| 100 | } |
| 101 |
| 102 | int decode_packet(VideoInfo *info, int *got_frame) { |
| 103 | int ret = 0; |
| 104 | int decoded = info->pkt.size; |
| 105 |
| 106 | *got_frame = 0; |
| 107 | if (info->pkt.stream_index == info->video_stream_idx) { |
| 108 | ret = avcodec_decode_video2(info->video_dec_ctx, info->frame, got_frame, &info->pkt); |
| 109 | if (ret != 0) { |
| 110 | return ret; |
| 111 | } |
| 112 | } |
| 113 |
| 114 | return decoded; |
| 115 | } |
| 116 |
| 117 | jint Java_org_telegram_ui_Components_AnimatedFileDrawable_createDecoder(JNIEnv *env, jclass clazz, jstring src, jintArray data) { |
| 118 | VideoInfo *info = new VideoInfo(); |
| 119 |
| 120 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 121 | int len = strlen(srcString); |
| 122 | info->src = new char[len + 1]; |
| 123 | memcpy(info->src, srcString, len); |
| 124 | info->src[len] = '\0'; |
| 125 | if (srcString != 0) { |
| 126 | env->ReleaseStringUTFChars(src, srcString); |
| 127 | } |
| 128 |
| 129 | int ret; |
| 130 | if ((ret = avformat_open_input(&info->fmt_ctx, info->src, NULL, NULL)) < 0) { |
| 131 | LOGE("can't open source file %s, %s", info->src, av_err2str(ret)); |
| 132 | delete info; |
| 133 | return 0; |
| 134 | } |
| 135 |
| 136 | if ((ret = avformat_find_stream_info(info->fmt_ctx, NULL)) < 0) { |
| 137 | LOGE("can't find stream information %s, %s", info->src, av_err2str(ret)); |
| 138 | delete info; |
| 139 | return 0; |
| 140 | } |
| 141 |
| 142 | if (open_codec_context(&info->video_stream_idx, info->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 143 | info->video_stream = info->fmt_ctx->streams[info->video_stream_idx]; |
| 144 | info->video_dec_ctx = info->video_stream->codec; |
| 145 | } |
| 146 |
| 147 | if (info->video_stream <= 0) { |
| 148 | LOGE("can't find video stream in the input, aborting %s", info->src); |
| 149 | delete info; |
| 150 | return 0; |
| 151 | } |
| 152 |
| 153 | info->frame = av_frame_alloc(); |
| 154 | if (info->frame == nullptr) { |
| 155 | LOGE("can't allocate frame %s", info->src); |
| 156 | delete info; |
| 157 | return 0; |
| 158 | } |
| 159 |
| 160 | av_init_packet(&info->pkt); |
| 161 | info->pkt.data = NULL; |
| 162 | info->pkt.size = 0; |
| 163 |
| 164 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 165 | if (dataArr != nullptr) { |
| 166 | dataArr[0] = info->video_dec_ctx->width; |
| 167 | dataArr[1] = info->video_dec_ctx->height; |
| 168 | AVDictionaryEntry *rotate_tag = av_dict_get(info->video_stream->metadata, "rotate", NULL, 0); |
| 169 | if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) { |
| 170 | char *tail; |
| 171 | dataArr[2] = (int) av_strtod(rotate_tag->value, &tail); |
| 172 | if (*tail) { |
| 173 | dataArr[2] = 0; |
| 174 | } |
| 175 | } else { |
| 176 | dataArr[2] = 0; |
| 177 | } |
| 178 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 179 | } |
| 180 |
| 181 | //LOGD("successfully opened file %s", info->src); |
| 182 |
| 183 | return (int) info; |
| 184 | } |
| 185 |
| 186 | void Java_org_telegram_ui_Components_AnimatedFileDrawable_destroyDecoder(JNIEnv *env, jclass clazz, jobject ptr) { |
| 187 | if (ptr == NULL) { |
| 188 | return; |
| 189 | } |
| 190 | VideoInfo *info = (VideoInfo *) ptr; |
| 191 | delete info; |
| 192 | } |
| 193 |
| 194 |
| 195 | jint Java_org_telegram_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, jclass clazz, jobject ptr, jobject bitmap, jintArray data) { |
| 196 | if (ptr == NULL || bitmap == nullptr) { |
| 197 | return 0; |
| 198 | } |
| 199 | VideoInfo *info = (VideoInfo *) ptr; |
| 200 | int ret = 0; |
| 201 | int got_frame = 0; |
| 202 |
| 203 | while (true) { |
| 204 | if (info->pkt.size == 0) { |
| 205 | ret = av_read_frame(info->fmt_ctx, &info->pkt); |
| 206 | //LOGD("got packet with size %d", info->pkt.size); |
| 207 | if (ret >= 0) { |
| 208 | info->orig_pkt = info->pkt; |
| 209 | } |
| 210 | } |
| 211 |
| 212 | if (info->pkt.size > 0) { |
| 213 | ret = decode_packet(info, &got_frame); |
| 214 | if (ret < 0) { |
| 215 | if (info->has_decoded_frames) { |
| 216 | ret = 0; |
| 217 | } |
| 218 | info->pkt.size = 0; |
| 219 | } else { |
| 220 | //LOGD("read size %d from packet", ret); |
| 221 | info->pkt.data += ret; |
| 222 | info->pkt.size -= ret; |
| 223 | } |
| 224 |
| 225 | if (info->pkt.size == 0) { |
| 226 | av_free_packet(&info->orig_pkt); |
| 227 | } |
| 228 | } else { |
| 229 | info->pkt.data = NULL; |
| 230 | info->pkt.size = 0; |
| 231 | ret = decode_packet(info, &got_frame); |
| 232 | if (ret < 0) { |
| 233 | LOGE("can't decode packet flushed %s", info->src); |
| 234 | return 0; |
| 235 | } |
| 236 | if (got_frame == 0) { |
| 237 | if (info->has_decoded_frames) { |
| 238 | //LOGD("file end reached %s", info->src); |
| 239 | if ((ret = avformat_seek_file(info->fmt_ctx, -1, std::numeric_limits<int64_t>::min(), 0, std::numeric_limits<int64_t>::max(), 0)) < 0) { |
| 240 | LOGE("can't seek to begin of file %s, %s", info->src, av_err2str(ret)); |
| 241 | return 0; |
| 242 | } else { |
| 243 | avcodec_flush_buffers(info->video_dec_ctx); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | if (ret < 0) { |
| 249 | return 0; |
| 250 | } |
| 251 | if (got_frame) { |
| 252 | //LOGD("decoded frame with w = %d, h = %d, format = %d", info->frame->width, info->frame->height, info->frame->format); |
| 253 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_BGRA || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 254 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 255 | int wantedWidth; |
| 256 | int wantedHeight; |
| 257 | if (dataArr != nullptr) { |
| 258 | wantedWidth = dataArr[0]; |
| 259 | wantedHeight = dataArr[1]; |
| 260 | dataArr[3] = (int) (1000 * info->frame->pkt_pts * av_q2d(info->video_stream->time_base)); |
| 261 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 262 | } else { |
| 263 | AndroidBitmapInfo bitmapInfo; |
| 264 | AndroidBitmap_getInfo(env, bitmap, &bitmapInfo); |
| 265 | wantedWidth = bitmapInfo.width; |
| 266 | wantedHeight = bitmapInfo.height; |
| 267 | } |
| 268 |
| 269 | void *pixels; |
| 270 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 271 | if (info->frame->format == AV_PIX_FMT_YUV420P || info->frame->format == AV_PIX_FMT_YUVJ420P) { |
| 272 | //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); |
| 273 | if (wantedWidth == info->frame->width && wantedHeight == info->frame->height || wantedWidth == info->frame->height && wantedHeight == info->frame->width) { |
| 274 | 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); |
| 275 | } |
| 276 | } else if (info->frame->format == AV_PIX_FMT_BGRA) { |
| 277 | libyuv::ABGRToARGB(info->frame->data[0], info->frame->linesize[0], (uint8_t *) pixels, info->frame->width * 4, info->frame->width, info->frame->height); |
| 278 | } |
| 279 | AndroidBitmap_unlockPixels(env, bitmap); |
| 280 | } |
| 281 | } |
| 282 | info->has_decoded_frames = true; |
| 283 | av_frame_unref(info->frame); |
| 284 | return 1; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 |