]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Add a static image input (fixed to 1280x720 for now, and uploaded rather inefficiently).
[nageru] / image_input.cpp
diff --git a/image_input.cpp b/image_input.cpp
new file mode 100644 (file)
index 0000000..b4fc657
--- /dev/null
@@ -0,0 +1,92 @@
+#include "image_input.h"
+
+#include <movit/image_format.h>
+
+extern "C" {
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+#include <libavutil/imgutils.h>
+#include <libavutil/pixfmt.h>
+#include <libswscale/swscale.h>
+}
+
+using namespace std;
+
+ImageInput::ImageInput(const std::string &filename)
+       : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
+                          GL_UNSIGNED_BYTE, 1280, 720)  // FIXME
+{
+       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 (avformat_find_stream_info(format_ctx, nullptr) < 0) {
+               fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
+               exit(1);
+       }
+
+       int stream_index = -1;
+       for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
+               if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+                       stream_index = i;
+                       break;
+               }
+       }
+       if (stream_index == -1) {
+               fprintf(stderr, "%s: No video stream found\n", filename.c_str());
+               exit(1);
+       }
+
+       AVCodecContext *codec_ctx = format_ctx->streams[stream_index]->codec;
+       AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
+       if (codec == nullptr) {
+               fprintf(stderr, "%s: Cannot find decoder\n", filename.c_str());
+               exit(1);
+       }
+       if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
+               fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
+               exit(1);
+       }
+
+       // Read packets until we have a frame.
+       int frame_finished = 0;
+       AVFrame *frame = av_frame_alloc();
+       do {
+               AVPacket pkt;
+               av_init_packet(&pkt);
+               pkt.data = nullptr;
+               pkt.size = 0;
+               if (av_read_frame(format_ctx, &pkt) < 0) {
+                       fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
+                       exit(1);
+               }
+               if (pkt.stream_index != stream_index) {
+                       continue;
+               }
+
+               if (avcodec_decode_video2(codec_ctx, frame, &frame_finished, &pkt) < 0) {
+                       fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
+                       exit(1);
+               }
+       } while (!frame_finished);
+
+       // TODO: Scale down if needed!
+       AVPicture pic;
+       avpicture_alloc(&pic, 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);
+       if (sws_ctx == nullptr) {
+               fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
+               exit(1);
+       }
+       sws_scale(sws_ctx, frame->data, frame->linesize, 0, frame->height, pic.data, pic.linesize);
+       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());
+}