]> git.sesse.net Git - nageru/blob - image_input.cpp
Make load_image() return nullptr instead of exit()-ing.
[nageru] / image_input.cpp
1 #include "image_input.h"
2
3 #include <movit/image_format.h>
4
5 extern "C" {
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>
11 }
12
13 using namespace std;
14
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
18 {
19         const uint8_t *pixel_data = load_image(filename);
20         if (pixel_data == nullptr) {
21                 fprintf(stderr, "Couldn't load image, exiting.\n");
22                 exit(1);
23         }
24         set_pixel_data(pixel_data);
25 }
26
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.
31
32 namespace {
33
34 void avformat_close_input_unique(AVFormatContext *format_ctx)
35 {
36         avformat_close_input(&format_ctx);
37 }
38
39 unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
40 avformat_open_input_unique(const char *filename,
41                            AVInputFormat *fmt, AVDictionary **options)
42 {
43         AVFormatContext *format_ctx = nullptr;
44         if (avformat_open_input(&format_ctx, filename, fmt, options) != 0) {
45                 format_ctx = nullptr;
46         }
47         return unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>(
48                 format_ctx, avformat_close_input_unique);
49 }
50
51 void av_frame_free_unique(AVFrame *frame)
52 {
53         av_frame_free(&frame);
54 }
55
56 unique_ptr<AVFrame, decltype(av_frame_free_unique)*>
57 av_frame_alloc_unique()
58 {
59         AVFrame *frame = av_frame_alloc();
60         return unique_ptr<AVFrame, decltype(av_frame_free_unique)*>(
61                 frame, av_frame_free_unique);
62 }
63
64 }  // namespace
65
66 const uint8_t *ImageInput::load_image(const string &filename)
67 {
68         if (all_images.count(filename)) {
69                 return all_images[filename].get();
70         }
71
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());
75                 return nullptr;
76         }
77
78         if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
79                 fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
80                 return nullptr;
81         }
82
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) {
86                         stream_index = i;
87                         break;
88                 }
89         }
90         if (stream_index == -1) {
91                 fprintf(stderr, "%s: No video stream found\n", filename.c_str());
92                 return nullptr;
93         }
94
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());
99                 return nullptr;
100         }
101         if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
102                 fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
103                 return nullptr;
104         }
105         unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
106                 codec_ctx, avcodec_close);
107
108         // Read packets until we have a frame.
109         int frame_finished = 0;
110         auto frame = av_frame_alloc_unique();
111         do {
112                 AVPacket pkt;
113                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
114                         &pkt, av_packet_unref);
115                 av_init_packet(&pkt);
116                 pkt.data = nullptr;
117                 pkt.size = 0;
118                 if (av_read_frame(format_ctx.get(), &pkt) < 0) {
119                         fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
120                         return nullptr;
121                 }
122                 if (pkt.stream_index != stream_index) {
123                         continue;
124                 }
125
126                 if (avcodec_decode_video2(codec_ctx, frame.get(), &frame_finished, &pkt) < 0) {
127                         fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
128                         return nullptr;
129                 }
130         } while (!frame_finished);
131
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);
136         int linesizes[4];
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());
139                 return nullptr;
140         }
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),
145                 sws_freeContext);
146         if (sws_ctx == nullptr) {
147                 fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
148                 return nullptr;
149         }
150         sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
151
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);
155
156         all_images[filename] = move(image_data);
157         return all_images[filename].get();
158 }
159
160 map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;