Skip to content

Commit 816b92b

Browse files
committed
feat: support loading video from buffer
1 parent 2c1d02a commit 816b92b

File tree

3 files changed

+329
-45
lines changed

3 files changed

+329
-45
lines changed

tools/mtmd/mtmd-helper.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigne
423423
return mtmd_bitmap_init_from_audio(pcmf32.size(), pcmf32.data());
424424
}
425425

426+
if(mtmd_video::is_video_buffer(buf, len)) {
427+
return mtmd_video::init_video_bitmap(ctx, buf, len);
428+
}
429+
426430
// otherwise, we assume it's an image
427431
mtmd_bitmap * result = nullptr;
428432
{
@@ -439,11 +443,12 @@ mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigne
439443
}
440444

441445
mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname) {
446+
// although we could read the file into memory and call mtmd_helper_bitmap_init_from_buf,
447+
// but for video files, it's better to let ffmpeg read from file
442448
if(mtmd_video::is_video_file(fname)){
443-
return mtmd_video::init_video_bitmap_from_path(ctx, fname);
449+
return mtmd_video::init_video_bitmap(ctx, fname);
444450
}
445451

446-
std::vector<unsigned char> buf;
447452
FILE * f = fopen(fname, "rb");
448453
if (!f) {
449454
LOG_ERR("Unable to open file %s: %s\n", fname, strerror(errno));
@@ -453,14 +458,16 @@ mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char *
453458
fseek(f, 0, SEEK_END);
454459
long file_size = ftell(f);
455460
fseek(f, 0, SEEK_SET);
456-
buf.resize(file_size);
461+
auto * buf = new unsigned char[file_size];
457462

458-
size_t n_read = fread(buf.data(), 1, file_size, f);
463+
size_t n_read = fread(buf, 1, file_size, f);
459464
fclose(f);
460465
if (n_read != (size_t)file_size) {
461466
LOG_ERR("Failed to read entire file %s", fname);
462467
return nullptr;
463468
}
464469

465-
return mtmd_helper_bitmap_init_from_buf(ctx, buf.data(), buf.size());
470+
auto * res = mtmd_helper_bitmap_init_from_buf(ctx, buf, file_size);
471+
delete [] buf;
472+
return res;
466473
}

0 commit comments

Comments
 (0)