using namespace std;
-ImageInput::ImageInput(const std::string &filename)
+ImageInput::ImageInput(const string &filename)
: movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
GL_UNSIGNED_BYTE, 1280, 720) // FIXME
{
+ set_pixel_data(load_image(filename));
+}
+
+const uint8_t *ImageInput::load_image(const string &filename)
+{
+ if (all_images.count(filename)) {
+ return all_images[filename].get();
+ }
+
AVFormatContext *format_ctx = nullptr;
if (avformat_open_input(&format_ctx, filename.c_str(), nullptr, nullptr) != 0) {
fprintf(stderr, "%s: Error opening file\n", filename.c_str());
exit(1);
}
if (pkt.stream_index != stream_index) {
+ av_free_packet(&pkt);
continue;
}
fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
exit(1);
}
+ av_free_packet(&pkt);
} while (!frame_finished);
// TODO: Scale down if needed!
AVPicture pic;
- avpicture_alloc(&pic, PIX_FMT_RGBA, frame->width, frame->height);
+ avpicture_alloc(&pic, AV_PIX_FMT_RGBA, frame->width, frame->height);
SwsContext *sws_ctx = sws_getContext(frame->width, frame->height,
- (PixelFormat)frame->format, frame->width, frame->height,
- PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr);
+ (AVPixelFormat)frame->format, frame->width, frame->height,
+ AV_PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr);
if (sws_ctx == nullptr) {
fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
exit(1);
sws_freeContext(sws_ctx);
size_t len = frame->width * frame->height * 4;
- image_data.reset(new uint8_t[len]);
- av_image_copy_to_buffer(image_data.get(), len, pic.data, pic.linesize, PIX_FMT_RGBA, frame->width, frame->height, 1);
- set_pixel_data(image_data.get());
+ unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
+ av_image_copy_to_buffer(image_data.get(), len, pic.data, pic.linesize, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
+
+ avpicture_free(&pic);
+ av_frame_free(&frame);
+ avcodec_close(codec_ctx);
+ avformat_close_input(&format_ctx);
+
+ all_images[filename] = move(image_data);
+ return all_images[filename].get();
}
+
+map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;