From 54ef3f25f924a677de0d71e1f773898b56ab5852 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 8 Jul 2018 23:26:21 +0100 Subject: [PATCH] Write image subs to DCPs. --- hacks/pixfmts.c | 4 ++++ src/lib/image.cc | 43 +++++++++++++++++++++++++++++++++++++++--- src/lib/image.h | 7 +++++-- src/lib/reel_writer.cc | 19 ++++++++++++++++++- src/lib/reel_writer.h | 4 ++-- src/lib/writer.cc | 6 +----- 6 files changed, 70 insertions(+), 13 deletions(-) diff --git a/hacks/pixfmts.c b/hacks/pixfmts.c index 8aa5c4a69..e1dd00a9d 100644 --- a/hacks/pixfmts.c +++ b/hacks/pixfmts.c @@ -5,6 +5,10 @@ int main() { SHOW(AV_PIX_FMT_YUV420P); + SHOW(AV_PIX_FMT_ARGB); + SHOW(AV_PIX_FMT_RGBA); + SHOW(AV_PIX_FMT_ABGR); + SHOW(AV_PIX_FMT_BGRA); SHOW(AV_PIX_FMT_YUV420P16LE); SHOW(AV_PIX_FMT_YUV422P10LE); SHOW(AV_PIX_FMT_YUV444P9BE); diff --git a/src/lib/image.cc b/src/lib/image.cc index 792bf3ab4..bdd4f61d8 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -30,6 +30,7 @@ #include "dcpomatic_socket.h" #include #include +#include extern "C" { #include #include @@ -825,7 +826,8 @@ Image::allocate () } Image::Image (Image const & other) - : _size (other._size) + : enable_shared_from_this(other) + , _size (other._size) , _pixel_format (other._pixel_format) , _aligned (other._aligned) , _extra_pixels (other._extra_pixels) @@ -1125,8 +1127,8 @@ Image::fade (float f) } } -shared_ptr -Image::ensure_aligned (shared_ptr image) +shared_ptr +Image::ensure_aligned (shared_ptr image) { if (image->aligned()) { return image; @@ -1144,3 +1146,38 @@ Image::memory_used () const } return m; } + +dcp::Data +Image::as_png () const +{ +#ifdef DCPOMATIC_IMAGE_MAGICK + using namespace MagickCore; +#else + using namespace MagickLib; +#endif + + string format; + switch (_pixel_format) { + case AV_PIX_FMT_RGB24: + format = "RGB"; + break; + case AV_PIX_FMT_BGRA: + format = "BGRA"; + break; + default: + DCPOMATIC_ASSERT (false); + break; + } + + shared_ptr use; + if (aligned()) { + use.reset (new Image(shared_from_this(), false)); + } + + Magick::Image m (size().width, size().height, format, CharPixel, (void *) use->data()[0]); + m.magick ("PNG"); + Magick::Blob blob; + m.write (&blob); + /* XXX: could use a subclass of Data here (storing its data in a Blob) */ + return dcp::Data (static_cast(blob.data()), blob.length()); +} diff --git a/src/lib/image.h b/src/lib/image.h index f71a47b0c..73f2313c1 100644 --- a/src/lib/image.h +++ b/src/lib/image.h @@ -33,11 +33,12 @@ extern "C" { } #include #include +#include struct AVFrame; class Socket; -class Image +class Image : public boost::enable_shared_from_this { public: Image (AVPixelFormat p, dcp::Size s, bool aligned, int extra_pixels = 0); @@ -80,7 +81,9 @@ public: size_t memory_used () const; - static boost::shared_ptr ensure_aligned (boost::shared_ptr image); + dcp::Data as_png () const; + + static boost::shared_ptr ensure_aligned (boost::shared_ptr image); private: friend struct pixel_formats_test; diff --git a/src/lib/reel_writer.cc b/src/lib/reel_writer.cc index 7fca9e245..24ab1c534 100644 --- a/src/lib/reel_writer.cc +++ b/src/lib/reel_writer.cc @@ -27,6 +27,7 @@ #include "font.h" #include "compose.hpp" #include "audio_buffers.h" +#include "image.h" #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include "i18n.h" @@ -526,7 +528,7 @@ ReelWriter::write (shared_ptr audio) } void -ReelWriter::write (PlayerSubtitles subs) +ReelWriter::write (PlayerSubtitles subs, DCPTimePeriod period) { if (!_subtitle_asset) { string lang = _film->subtitle_language (); @@ -555,10 +557,25 @@ ReelWriter::write (PlayerSubtitles subs) } BOOST_FOREACH (SubtitleString i, subs.text) { + /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */ i.set_in (i.in() - dcp::Time (_period.from.seconds(), i.in().tcr)); i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr)); _subtitle_asset->add (shared_ptr(new dcp::SubtitleString(i))); } + + BOOST_FOREACH (ImageSubtitle i, subs.image) { + _subtitle_asset->add ( + shared_ptr( + new dcp::SubtitleImage( + i.image->as_png(), + dcp::Time(period.from.seconds(), _film->video_frame_rate()), + dcp::Time(period.to.seconds(), _film->video_frame_rate()), + i.rectangle.x, dcp::HALIGN_LEFT, i.rectangle.y, dcp::VALIGN_TOP, + dcp::Time(), dcp::Time() + ) + ) + ); + } } bool diff --git a/src/lib/reel_writer.h b/src/lib/reel_writer.h index c0e98f6eb..4357a28f2 100644 --- a/src/lib/reel_writer.h +++ b/src/lib/reel_writer.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2016 Carl Hetherington + Copyright (C) 2012-2018 Carl Hetherington This file is part of DCP-o-matic. @@ -60,7 +60,7 @@ public: void fake_write (Frame frame, Eyes eyes, int size); void repeat_write (Frame frame, Eyes eyes); void write (boost::shared_ptr audio); - void write (PlayerSubtitles subs); + void write (PlayerSubtitles subs, DCPTimePeriod period); void finish (); boost::shared_ptr create_reel (std::list const & refs, std::list > const & fonts); diff --git a/src/lib/writer.cc b/src/lib/writer.cc index cd5b6d7e0..2eec8110e 100644 --- a/src/lib/writer.cc +++ b/src/lib/writer.cc @@ -665,10 +665,6 @@ Writer::can_fake_write (Frame frame) const void Writer::write (PlayerSubtitles subs, DCPTimePeriod period) { - if (subs.text.empty ()) { - return; - } - while (_subtitle_reel->period().to <= period.from) { ++_subtitle_reel; DCPOMATIC_ASSERT (_subtitle_reel != _reels.end()); @@ -676,7 +672,7 @@ Writer::write (PlayerSubtitles subs, DCPTimePeriod period) DCPOMATIC_ASSERT (_subtitle_reel != _reels.end()); - _subtitle_reel->write (subs); + _subtitle_reel->write (subs, period); } void -- 2.30.2