#include "log.h"
#include "ffmpeg_subtitle_stream.h"
#include "ffmpeg_audio_stream.h"
+#include "md5_digester.h"
#include "compose.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
);
}
+string
+FFmpeg::subtitle_id (AVSubtitle const & sub)
+{
+ MD5Digester digester;
+ digester.add (sub.start_display_time);
+ digester.add (sub.end_display_time);
+ digester.add (sub.pts);
+ for (unsigned int i = 0; i < sub.num_rects; ++i) {
+ AVSubtitleRect* rect = sub.rects[i];
+ digester.add (rect->x);
+ digester.add (rect->y);
+ digester.add (rect->w);
+ digester.add (rect->h);
+ int const line = rect->pict.linesize[0];
+ for (int j = 0; j < rect->h; ++j) {
+ digester.add (rect->pict.data[0] + j * line, line);
+ }
+ }
+ return digester.get ();
+}
+
/** Compute the pts offset to use given a set of audio streams and some video details.
* Sometimes these parameters will have just been determined by an Examiner, sometimes
* they will have been retrieved from a piece of Content, hence the need for this method
std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams, boost::optional<ContentTime> first_video, double video_frame_rate
) const;
- static FFmpegSubtitlePeriod subtitle_period (AVSubtitle const &);
+ static FFmpegSubtitlePeriod subtitle_period (AVSubtitle const & sub);
+ static std::string subtitle_id (AVSubtitle const & sub);
boost::shared_ptr<const FFmpegContent> _ffmpeg_content;
{
list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
- _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
+ _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i, version)));
if ((*i)->optional_number_child<int> ("Selected")) {
_subtitle_stream = _subtitle_streams.back ();
}
/*
- Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "ffmpeg_content.h"
#include "raw_image_proxy.h"
#include "film.h"
+#include "md5_digester.h"
#include "compose.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
period.to = sub_period.to.get() + _pts_offset;
} else {
/* We have to look up the `to' time in the stream's records */
- period.to = ffmpeg_content()->subtitle_stream()->find_subtitle_to (period.from);
+ period.to = ffmpeg_content()->subtitle_stream()->find_subtitle_to (subtitle_id (sub));
}
for (unsigned int i = 0; i < sub.num_rects; ++i) {
}
}
+ /* Finish off any hanging subtitles at the end */
for (LastSubtitleMap::const_iterator i = _last_subtitle_start.begin(); i != _last_subtitle_start.end(); ++i) {
if (i->second) {
i->first->add_subtitle (
+ i->second->id,
ContentTimePeriod (
- i->second.get (),
+ i->second->time,
ContentTime::from_frames (video_length(), video_frame_rate().get_value_or (24))
)
);
int frame_finished;
AVSubtitle sub;
if (avcodec_decode_subtitle2 (context, &sub, &frame_finished, &_packet) >= 0 && frame_finished) {
+ string id = subtitle_id (sub);
FFmpegSubtitlePeriod const period = subtitle_period (sub);
LastSubtitleMap::iterator last = _last_subtitle_start.find (stream);
if (last != _last_subtitle_start.end() && last->second) {
/* We have seen the start of a subtitle but not yet the end. Whatever this is
finishes the previous subtitle, so add it */
- stream->add_subtitle (ContentTimePeriod (last->second.get (), period.from));
+ stream->add_subtitle (last->second->id, ContentTimePeriod (last->second->time, period.from));
if (sub.num_rects == 0) {
/* This is a `proper' end-of-subtitle */
- _last_subtitle_start[stream] = optional<ContentTime> ();
+ _last_subtitle_start[stream] = optional<SubtitleStart> ();
} else {
/* This is just another subtitle, so we start again */
- _last_subtitle_start[stream] = period.from;
+ _last_subtitle_start[stream] = SubtitleStart (id, period.from);
}
} else if (sub.num_rects == 1) {
if (period.to) {
- stream->add_subtitle (ContentTimePeriod (period.from, period.to.get ()));
+ stream->add_subtitle (id, ContentTimePeriod (period.from, period.to.get ()));
} else {
- _last_subtitle_start[stream] = period.from;
+ _last_subtitle_start[stream] = SubtitleStart (id, period.from);
}
}
avsubtitle_free (&sub);
Frame _video_length;
bool _need_video_length;
- typedef std::map<boost::shared_ptr<FFmpegSubtitleStream>, boost::optional<ContentTime> > LastSubtitleMap;
+ struct SubtitleStart
+ {
+ SubtitleStart (std::string id_, ContentTime time_)
+ : id (id_)
+ , time (time_)
+ {}
+
+ std::string id;
+ ContentTime time;
+ };
+
+ typedef std::map<boost::shared_ptr<FFmpegSubtitleStream>, boost::optional<SubtitleStart> > LastSubtitleMap;
LastSubtitleMap _last_subtitle_start;
};
/*
- Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
* @param t String returned from to_string().
* @param v State file version.
*/
-FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node)
+FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
: FFmpegStream (node)
{
- BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
- add_subtitle (
- ContentTimePeriod (
- ContentTime (i->number_child<ContentTime::Type> ("From")),
- ContentTime (i->number_child<ContentTime::Type> ("To"))
- )
- );
+ if (version == 32) {
+ BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
+ /* In version 32 we assumed that from times were unique, so they weer
+ used as identifiers.
+ */
+ add_subtitle (
+ raw_convert<string> (i->string_child ("From")),
+ ContentTimePeriod (
+ ContentTime (i->number_child<ContentTime::Type> ("From")),
+ ContentTime (i->number_child<ContentTime::Type> ("To"))
+ )
+ );
+ }
+ } else {
+ /* In version 33 we use a hash of various parts of the subtitle as the id */
+ BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
+ add_subtitle (
+ raw_convert<string> (i->string_child ("Id")),
+ ContentTimePeriod (
+ ContentTime (i->number_child<ContentTime::Type> ("From")),
+ ContentTime (i->number_child<ContentTime::Type> ("To"))
+ )
+ );
+ }
}
}
{
FFmpegStream::as_xml (root);
- for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
- xmlpp::Node* node = root->add_child ("Period");
- node->add_child("From")->add_child_text (raw_convert<string> (i->first.get ()));
- node->add_child("To")->add_child_text (raw_convert<string> (i->second.get ()));
+ for (map<string, ContentTimePeriod>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+ xmlpp::Node* node = root->add_child ("Subtitle");
+ node->add_child("Id")->add_child_text (i->first);
+ node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
+ node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
}
}
void
-FFmpegSubtitleStream::add_subtitle (ContentTimePeriod period)
+FFmpegSubtitleStream::add_subtitle (string id, ContentTimePeriod period)
{
- DCPOMATIC_ASSERT (_subtitles.find (period.from) == _subtitles.end ());
- _subtitles[period.from] = period.to;
+ DCPOMATIC_ASSERT (_subtitles.find (id) == _subtitles.end ());
+ _subtitles[id] = period;
}
list<ContentTimePeriod>
list<ContentTimePeriod> d;
/* XXX: inefficient */
- for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
- if ((starting && period.contains (i->first)) || (!starting && period.overlaps (ContentTimePeriod (i->first, i->second)))) {
- d.push_back (ContentTimePeriod (i->first, i->second));
+ for (map<string, ContentTimePeriod>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+ if ((starting && period.contains (i->second.from)) || (!starting && period.overlaps (i->second))) {
+ d.push_back (i->second);
}
}
}
ContentTime
-FFmpegSubtitleStream::find_subtitle_to (ContentTime from) const
+FFmpegSubtitleStream::find_subtitle_to (string id) const
{
- map<ContentTime, ContentTime>::const_iterator i = _subtitles.find (from);
+ map<string, ContentTimePeriod>::const_iterator i = _subtitles.find (id);
DCPOMATIC_ASSERT (i != _subtitles.end ());
- return i->second;
+ return i->second.to;
}
/** Add some offset to all the times in the stream */
void
FFmpegSubtitleStream::add_offset (ContentTime offset)
{
- map<ContentTime, ContentTime> fixed;
- for (map<ContentTime, ContentTime>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
- fixed[i->first + offset] = i->second + offset;
+ for (map<string, ContentTimePeriod>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+ i->second.from += offset;
+ i->second.to += offset;
}
- _subtitles = fixed;
}
/*
- Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
: FFmpegStream (n, i)
{}
- FFmpegSubtitleStream (cxml::ConstNodePtr);
+ FFmpegSubtitleStream (cxml::ConstNodePtr, int version);
void as_xml (xmlpp::Node *) const;
- void add_subtitle (ContentTimePeriod period);
+ void add_subtitle (std::string id, ContentTimePeriod period);
std::list<ContentTimePeriod> subtitles_during (ContentTimePeriod period, bool starting) const;
- ContentTime find_subtitle_to (ContentTime from) const;
+ ContentTime find_subtitle_to (std::string id) const;
void add_offset (ContentTime offset);
private:
- std::map<ContentTime, ContentTime> _subtitles;
+ std::map<std::string, ContentTimePeriod> _subtitles;
};
*
* Bumped to 32 for 2.0 branch; some times are expressed in Times rather
* than frames now.
+ *
+ * 32 -> 33
+ * Changed <Period> to <Subtitle> in FFmpegSubtitleStream
*/
-int const Film::current_state_version = 32;
+int const Film::current_state_version = 33;
/** Construct a Film object in a given directory.
*