+ const uint8_t *pixel_data = load_image(filename);
+ if (pixel_data == nullptr) {
+ fprintf(stderr, "Couldn't load image, exiting.\n");
+ exit(1);
+ }
+ set_pixel_data(pixel_data);
+}
+
+// Some helpers to make RAII versions of FFmpeg objects.
+// The cleanup functions don't interact all that well with unique_ptr,
+// so things get a bit messy and verbose, but overall it's worth it to ensure
+// we never leak things by accident in error paths.
+
+namespace {
+
+void avformat_close_input_unique(AVFormatContext *format_ctx)
+{
+ avformat_close_input(&format_ctx);
+}
+
+unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
+avformat_open_input_unique(const char *filename,
+ AVInputFormat *fmt, AVDictionary **options)
+{
+ AVFormatContext *format_ctx = nullptr;
+ if (avformat_open_input(&format_ctx, filename, fmt, options) != 0) {
+ format_ctx = nullptr;
+ }
+ return unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>(
+ format_ctx, avformat_close_input_unique);
+}
+
+void av_frame_free_unique(AVFrame *frame)
+{
+ av_frame_free(&frame);