-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmedia_thumbnail_retriever.cpp
More file actions
475 lines (401 loc) · 13.4 KB
/
media_thumbnail_retriever.cpp
File metadata and controls
475 lines (401 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/display.h>
#include <libavutil/imgutils.h>
}
#include <android/bitmap.h>
#include <jni.h>
#include <cstdio>
#include <cstdlib>
struct MediaThumbnailRetrieverContext {
AVFormatContext *formatContext;
int videoStreamIndex;
int rotationDegrees;
};
static MediaThumbnailRetrieverContext *context_from_handle(jlong handle) {
return reinterpret_cast<MediaThumbnailRetrieverContext *>(handle);
}
static jlong handle_from_context(MediaThumbnailRetrieverContext *context) {
return reinterpret_cast<jlong>(context);
}
static int normalize_rotation(int rotation) {
rotation %= 360;
if (rotation < 0) {
rotation += 360;
}
return rotation;
}
static int read_rotation_degrees(AVStream *stream) {
if (!stream) {
return 0;
}
int rotation = 0;
AVDictionaryEntry *rotateTag = av_dict_get(stream->metadata, "rotate", nullptr, 0);
if (rotateTag && rotateTag->value && *rotateTag->value) {
rotation = normalize_rotation(atoi(rotateTag->value));
}
const AVPacketSideData *sideData = av_packet_side_data_get(
stream->codecpar->coded_side_data,
stream->codecpar->nb_coded_side_data,
AV_PKT_DATA_DISPLAYMATRIX
);
if (sideData) {
double theta = av_display_rotation_get((const int32_t *)(sideData->data));
rotation = normalize_rotation(static_cast<int>(-theta));
}
return rotation;
}
static jobject create_bitmap(JNIEnv *env, int width, int height) {
jclass bitmapClass = env->FindClass("android/graphics/Bitmap");
if (!bitmapClass) {
return nullptr;
}
jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config");
if (!bitmapConfigClass) {
return nullptr;
}
jfieldID argb8888Field = env->GetStaticFieldID(
bitmapConfigClass,
"ARGB_8888",
"Landroid/graphics/Bitmap$Config;");
if (!argb8888Field) {
return nullptr;
}
jobject argb8888Obj = env->GetStaticObjectField(bitmapConfigClass, argb8888Field);
if (!argb8888Obj) {
return nullptr;
}
jmethodID createBitmapMethod = env->GetStaticMethodID(
bitmapClass,
"createBitmap",
"(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
if (!createBitmapMethod) {
return nullptr;
}
return env->CallStaticObjectMethod(bitmapClass, createBitmapMethod, width, height, argb8888Obj);
}
static jobject frame_to_bitmap(JNIEnv *env, const AVFrame *frame) {
if (!frame || frame->width <= 0 || frame->height <= 0) {
return nullptr;
}
jobject bitmap = create_bitmap(env, frame->width, frame->height);
if (!bitmap) {
return nullptr;
}
AndroidBitmapInfo bitmapInfo;
if (AndroidBitmap_getInfo(env, bitmap, &bitmapInfo) < 0) {
env->DeleteLocalRef(bitmap);
return nullptr;
}
void *bitmapPixels = nullptr;
if (AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels) < 0 || !bitmapPixels) {
env->DeleteLocalRef(bitmap);
return nullptr;
}
SwsContext *swsContext = sws_getContext(
frame->width,
frame->height,
static_cast<AVPixelFormat>(frame->format),
bitmapInfo.width,
bitmapInfo.height,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr);
if (!swsContext) {
AndroidBitmap_unlockPixels(env, bitmap);
env->DeleteLocalRef(bitmap);
return nullptr;
}
AVFrame *outputFrame = av_frame_alloc();
if (!outputFrame) {
sws_freeContext(swsContext);
AndroidBitmap_unlockPixels(env, bitmap);
env->DeleteLocalRef(bitmap);
return nullptr;
}
av_image_fill_arrays(
outputFrame->data,
outputFrame->linesize,
static_cast<uint8_t *>(bitmapPixels),
AV_PIX_FMT_RGBA,
bitmapInfo.width,
bitmapInfo.height,
1);
sws_scale(
swsContext,
frame->data,
frame->linesize,
0,
frame->height,
outputFrame->data,
outputFrame->linesize);
av_frame_free(&outputFrame);
sws_freeContext(swsContext);
AndroidBitmap_unlockPixels(env, bitmap);
return bitmap;
}
static AVCodecContext *create_decoder_context(MediaThumbnailRetrieverContext *context) {
if (!context || !context->formatContext || context->videoStreamIndex < 0) {
return nullptr;
}
AVStream *videoStream = context->formatContext->streams[context->videoStreamIndex];
if (!videoStream || !videoStream->codecpar) {
return nullptr;
}
const AVCodec *decoder = avcodec_find_decoder(videoStream->codecpar->codec_id);
if (!decoder) {
return nullptr;
}
AVCodecContext *codecContext = avcodec_alloc_context3(decoder);
if (!codecContext) {
return nullptr;
}
if (avcodec_parameters_to_context(codecContext, videoStream->codecpar) < 0 ||
avcodec_open2(codecContext, decoder, nullptr) < 0) {
avcodec_free_context(&codecContext);
return nullptr;
}
return codecContext;
}
static bool decode_next_frame(MediaThumbnailRetrieverContext *context,
AVCodecContext *codecContext,
AVPacket *packet,
AVFrame *frame) {
while (av_read_frame(context->formatContext, packet) >= 0) {
if (packet->stream_index != context->videoStreamIndex) {
av_packet_unref(packet);
continue;
}
int sendResult = avcodec_send_packet(codecContext, packet);
av_packet_unref(packet);
if (sendResult < 0) {
return false;
}
int receiveResult = avcodec_receive_frame(codecContext, frame);
if (receiveResult == AVERROR(EAGAIN) || receiveResult == AVERROR_EOF) {
continue;
}
if (receiveResult < 0) {
return false;
}
return true;
}
avcodec_send_packet(codecContext, nullptr);
if (avcodec_receive_frame(codecContext, frame) >= 0) {
return true;
}
return false;
}
static jobject decode_frame_at_time(JNIEnv *env, MediaThumbnailRetrieverContext *context, int64_t timeUs) {
AVCodecContext *codecContext = create_decoder_context(context);
if (!codecContext) {
return nullptr;
}
AVStream *videoStream = context->formatContext->streams[context->videoStreamIndex];
int64_t targetTimestamp = av_rescale_q(timeUs, AV_TIME_BASE_Q, videoStream->time_base);
int seekResult = av_seek_frame(context->formatContext, context->videoStreamIndex, targetTimestamp, AVSEEK_FLAG_BACKWARD);
if (seekResult < 0) {
// Failed to seek to the requested timestamp; clean up and return null.
avcodec_free_context(&codecContext);
return nullptr;
}
avcodec_flush_buffers(codecContext);
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
if (!packet || !frame) {
av_packet_free(&packet);
av_frame_free(&frame);
avcodec_free_context(&codecContext);
return nullptr;
}
jobject result = nullptr;
if (decode_next_frame(context, codecContext, packet, frame)) {
result = frame_to_bitmap(env, frame);
}
av_packet_free(&packet);
av_frame_free(&frame);
avcodec_free_context(&codecContext);
return result;
}
static jobject decode_frame_at_index(JNIEnv *env, MediaThumbnailRetrieverContext *context, int frameIndex) {
AVCodecContext *codecContext = create_decoder_context(context);
if (!codecContext) {
return nullptr;
}
int seekResult = av_seek_frame(context->formatContext, context->videoStreamIndex, 0, AVSEEK_FLAG_BACKWARD);
if (seekResult < 0) {
avcodec_free_context(&codecContext);
return nullptr;
}
avcodec_flush_buffers(codecContext);
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
if (!packet || !frame) {
av_packet_free(&packet);
av_frame_free(&frame);
avcodec_free_context(&codecContext);
return nullptr;
}
int decodedFrameCount = 0;
jobject result = nullptr;
while (decode_next_frame(context, codecContext, packet, frame)) {
if (decodedFrameCount == frameIndex) {
result = frame_to_bitmap(env, frame);
break;
}
decodedFrameCount++;
av_frame_unref(frame);
}
av_packet_free(&packet);
av_frame_free(&frame);
avcodec_free_context(&codecContext);
return result;
}
static jlong create_context_from_source(const char *source) {
AVFormatContext *formatContext = nullptr;
if (!source || avformat_open_input(&formatContext, source, nullptr, nullptr) < 0) {
return 0;
}
if (avformat_find_stream_info(formatContext, nullptr) < 0) {
avformat_close_input(&formatContext);
return 0;
}
int videoStreamIndex = -1;
for (unsigned int i = 0; i < formatContext->nb_streams; ++i) {
AVStream *stream = formatContext->streams[i];
if (!stream || !stream->codecpar) {
continue;
}
if (stream->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
continue;
}
if ((stream->disposition & AV_DISPOSITION_ATTACHED_PIC) != 0) {
continue;
}
videoStreamIndex = static_cast<int>(i);
break;
}
if (videoStreamIndex < 0) {
videoStreamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
}
auto *context = reinterpret_cast<MediaThumbnailRetrieverContext *>(malloc(sizeof(MediaThumbnailRetrieverContext)));
if (!context) {
avformat_close_input(&formatContext);
return 0;
}
context->formatContext = formatContext;
context->videoStreamIndex = videoStreamIndex;
context->rotationDegrees = (videoStreamIndex >= 0)
? read_rotation_degrees(formatContext->streams[videoStreamIndex])
: 0;
return handle_from_context(context);
}
extern "C"
JNIEXPORT jlong JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeCreateFromPath(
JNIEnv *env,
jobject thiz,
jstring file_path) {
const char *source = env->GetStringUTFChars(file_path, nullptr);
jlong handle = create_context_from_source(source);
env->ReleaseStringUTFChars(file_path, source);
return handle;
}
extern "C"
JNIEXPORT jlong JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeCreateFromFD(
JNIEnv *env,
jobject thiz,
jint file_descriptor) {
char fdPath[64];
snprintf(fdPath, sizeof(fdPath), "/proc/self/fd/%d", file_descriptor);
return create_context_from_source(fdPath);
}
extern "C"
JNIEXPORT jbyteArray JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeGetEmbeddedPicture(
JNIEnv *env,
jobject thiz,
jlong handle) {
auto *context = context_from_handle(handle);
if (!context || !context->formatContext) {
return nullptr;
}
for (unsigned int i = 0; i < context->formatContext->nb_streams; ++i) {
AVStream *stream = context->formatContext->streams[i];
if (!stream) {
continue;
}
if ((stream->disposition & AV_DISPOSITION_ATTACHED_PIC) == 0) {
continue;
}
AVPacket attachedPic = stream->attached_pic;
if (!attachedPic.data || attachedPic.size <= 0) {
continue;
}
jbyteArray result = env->NewByteArray(attachedPic.size);
if (!result) {
return nullptr;
}
env->SetByteArrayRegion(result, 0, attachedPic.size, reinterpret_cast<const jbyte *>(attachedPic.data));
return result;
}
return nullptr;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeGetFrameAtTime(
JNIEnv *env,
jobject thiz,
jlong handle,
jlong time_us) {
auto *context = context_from_handle(handle);
if (!context || !context->formatContext || context->videoStreamIndex < 0) {
return nullptr;
}
return decode_frame_at_time(env, context, time_us);
}
extern "C"
JNIEXPORT jobject JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeGetFrameAtIndex(
JNIEnv *env,
jobject thiz,
jlong handle,
jint frame_index) {
auto *context = context_from_handle(handle);
if (!context || !context->formatContext || context->videoStreamIndex < 0 || frame_index < 0) {
return nullptr;
}
return decode_frame_at_index(env, context, frame_index);
}
extern "C"
JNIEXPORT jint JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeGetRotationDegrees(
JNIEnv *env,
jobject thiz,
jlong handle) {
auto *context = context_from_handle(handle);
if (!context) {
return 0;
}
return context->rotationDegrees;
}
extern "C"
JNIEXPORT void JNICALL
Java_io_github_anilbeesetti_nextlib_mediainfo_MediaThumbnailRetriever_nativeRelease(
JNIEnv *env,
jobject thiz,
jlong handle) {
auto *context = context_from_handle(handle);
if (!context) {
return;
}
if (context->formatContext) {
avformat_close_input(&context->formatContext);
}
free(context);
}