1 #include "image_input.h"
3 #include <movit/image_format.h>
6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 #include <libavutil/imgutils.h>
9 #include <libavutil/pixfmt.h>
10 #include <libswscale/swscale.h>
15 ImageInput::ImageInput(const string &filename)
16 : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
17 GL_UNSIGNED_BYTE, 1280, 720) // FIXME
19 set_pixel_data(load_image(filename));
22 const uint8_t *ImageInput::load_image(const string &filename)
24 if (all_images.count(filename)) {
25 return all_images[filename].get();
28 AVFormatContext *format_ctx = nullptr;
29 if (avformat_open_input(&format_ctx, filename.c_str(), nullptr, nullptr) != 0) {
30 fprintf(stderr, "%s: Error opening file\n", filename.c_str());
34 if (avformat_find_stream_info(format_ctx, nullptr) < 0) {
35 fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
39 int stream_index = -1;
40 for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
41 if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
46 if (stream_index == -1) {
47 fprintf(stderr, "%s: No video stream found\n", filename.c_str());
51 AVCodecContext *codec_ctx = format_ctx->streams[stream_index]->codec;
52 AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
53 if (codec == nullptr) {
54 fprintf(stderr, "%s: Cannot find decoder\n", filename.c_str());
57 if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
58 fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
62 // Read packets until we have a frame.
63 int frame_finished = 0;
64 AVFrame *frame = av_frame_alloc();
70 if (av_read_frame(format_ctx, &pkt) < 0) {
71 fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
74 if (pkt.stream_index != stream_index) {
79 if (avcodec_decode_video2(codec_ctx, frame, &frame_finished, &pkt) < 0) {
80 fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
84 } while (!frame_finished);
86 // TODO: Scale down if needed!
88 avpicture_alloc(&pic, PIX_FMT_RGBA, frame->width, frame->height);
89 SwsContext *sws_ctx = sws_getContext(frame->width, frame->height,
90 (PixelFormat)frame->format, frame->width, frame->height,
91 PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr);
92 if (sws_ctx == nullptr) {
93 fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
96 sws_scale(sws_ctx, frame->data, frame->linesize, 0, frame->height, pic.data, pic.linesize);
97 sws_freeContext(sws_ctx);
99 size_t len = frame->width * frame->height * 4;
100 unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
101 av_image_copy_to_buffer(image_data.get(), len, pic.data, pic.linesize, PIX_FMT_RGBA, frame->width, frame->height, 1);
103 avpicture_free(&pic);
104 av_frame_free(&frame);
105 avcodec_close(codec_ctx);
106 avformat_close_input(&format_ctx);
108 all_images[filename] = move(image_data);
109 return all_images[filename].get();
112 map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;