]> git.sesse.net Git - nageru/blob - image_input.cpp
Fix issues with newer ffmpeg.
[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         set_pixel_data(load_image(filename));
20 }
21
22 const uint8_t *ImageInput::load_image(const string &filename)
23 {
24         if (all_images.count(filename)) {
25                 return all_images[filename].get();
26         }
27
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());
31                 exit(1);
32         }
33
34         if (avformat_find_stream_info(format_ctx, nullptr) < 0) {
35                 fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
36                 exit(1);
37         }
38
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) {
42                         stream_index = i;
43                         break;
44                 }
45         }
46         if (stream_index == -1) {
47                 fprintf(stderr, "%s: No video stream found\n", filename.c_str());
48                 exit(1);
49         }
50
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());
55                 exit(1);
56         }
57         if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
58                 fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
59                 exit(1);
60         }
61
62         // Read packets until we have a frame.
63         int frame_finished = 0;
64         AVFrame *frame = av_frame_alloc();
65         do {
66                 AVPacket pkt;
67                 av_init_packet(&pkt);
68                 pkt.data = nullptr;
69                 pkt.size = 0;
70                 if (av_read_frame(format_ctx, &pkt) < 0) {
71                         fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
72                         exit(1);
73                 }
74                 if (pkt.stream_index != stream_index) {
75                         av_free_packet(&pkt);
76                         continue;
77                 }
78
79                 if (avcodec_decode_video2(codec_ctx, frame, &frame_finished, &pkt) < 0) {
80                         fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
81                         exit(1);
82                 }
83                 av_free_packet(&pkt);
84         } while (!frame_finished);
85
86         // TODO: Scale down if needed!
87         AVPicture pic;
88         avpicture_alloc(&pic, AV_PIX_FMT_RGBA, frame->width, frame->height);
89         SwsContext *sws_ctx = sws_getContext(frame->width, frame->height,
90                 (AVPixelFormat)frame->format, frame->width, frame->height,
91                 AV_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());
94                 exit(1);
95         }
96         sws_scale(sws_ctx, frame->data, frame->linesize, 0, frame->height, pic.data, pic.linesize);
97         sws_freeContext(sws_ctx);
98
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, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
102
103         avpicture_free(&pic);
104         av_frame_free(&frame);
105         avcodec_close(codec_ctx);
106         avformat_close_input(&format_ctx);
107
108         all_images[filename] = move(image_data);
109         return all_images[filename].get();
110 }
111
112 map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;