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 const uint8_t *pixel_data = load_image(filename);
20 if (pixel_data == nullptr) {
21 fprintf(stderr, "Couldn't load image, exiting.\n");
24 set_pixel_data(pixel_data);
27 // Some helpers to make RAII versions of FFmpeg objects.
28 // The cleanup functions don't interact all that well with unique_ptr,
29 // so things get a bit messy and verbose, but overall it's worth it to ensure
30 // we never leak things by accident in error paths.
34 void avformat_close_input_unique(AVFormatContext *format_ctx)
36 avformat_close_input(&format_ctx);
39 unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
40 avformat_open_input_unique(const char *filename,
41 AVInputFormat *fmt, AVDictionary **options)
43 AVFormatContext *format_ctx = nullptr;
44 if (avformat_open_input(&format_ctx, filename, fmt, options) != 0) {
47 return unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>(
48 format_ctx, avformat_close_input_unique);
51 void av_frame_free_unique(AVFrame *frame)
53 av_frame_free(&frame);
56 unique_ptr<AVFrame, decltype(av_frame_free_unique)*>
57 av_frame_alloc_unique()
59 AVFrame *frame = av_frame_alloc();
60 return unique_ptr<AVFrame, decltype(av_frame_free_unique)*>(
61 frame, av_frame_free_unique);
66 const uint8_t *ImageInput::load_image(const string &filename)
68 if (all_images.count(filename)) {
69 return all_images[filename].get();
72 auto format_ctx = avformat_open_input_unique(filename.c_str(), nullptr, nullptr);
73 if (format_ctx == nullptr) {
74 fprintf(stderr, "%s: Error opening file\n", filename.c_str());
78 if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
79 fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
83 int stream_index = -1;
84 for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
85 if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
90 if (stream_index == -1) {
91 fprintf(stderr, "%s: No video stream found\n", filename.c_str());
95 AVCodecContext *codec_ctx = format_ctx->streams[stream_index]->codec;
96 AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
97 if (codec == nullptr) {
98 fprintf(stderr, "%s: Cannot find decoder\n", filename.c_str());
101 if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
102 fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
105 unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
106 codec_ctx, avcodec_close);
108 // Read packets until we have a frame.
109 int frame_finished = 0;
110 auto frame = av_frame_alloc_unique();
113 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
114 &pkt, av_packet_unref);
115 av_init_packet(&pkt);
118 if (av_read_frame(format_ctx.get(), &pkt) < 0) {
119 fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
122 if (pkt.stream_index != stream_index) {
126 if (avcodec_decode_video2(codec_ctx, frame.get(), &frame_finished, &pkt) < 0) {
127 fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
130 } while (!frame_finished);
132 // TODO: Scale down if needed!
133 uint8_t *pic_data[4] = {nullptr};
134 unique_ptr<uint8_t *, decltype(av_freep)*> pic_data_cleanup(
135 &pic_data[0], av_freep);
137 if (av_image_alloc(pic_data, linesizes, frame->width, frame->height, AV_PIX_FMT_RGBA, 1) < 0) {
138 fprintf(stderr, "%s: Could not allocate picture data\n", filename.c_str());
141 unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(
142 sws_getContext(frame->width, frame->height,
143 (AVPixelFormat)frame->format, frame->width, frame->height,
144 AV_PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr),
146 if (sws_ctx == nullptr) {
147 fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
150 sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
152 size_t len = frame->width * frame->height * 4;
153 unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
154 av_image_copy_to_buffer(image_data.get(), len, pic_data, linesizes, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
156 all_images[filename] = move(image_data);
157 return all_images[filename].get();
160 map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;