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