void
ExamineContentJob::run ()
{
- /* Decode the content to get an accurate length */
- float progress_remaining = 1;
++ descend (1);
- /* We don't want to use any existing length here, as progress
- will be messed up.
+ /* Set the film's length to either
+ a) a length judged by running through the content or
+ b) the length from a decoder's header.
*/
- _film->unset_length ();
- _film->set_crop (Crop ());
-
+ if (!_film->trust_content_header()) {
+ /* Decode the content to get an accurate length */
+
+ /* We don't want to use any existing length here, as progress
+ will be messed up.
+ */
+ _film->unset_length ();
++ _film->set_crop (Crop ());
+
- shared_ptr<Options> o (new Options ("", "", ""));
- o->out_size = Size (512, 512);
- o->apply_crop = false;
++ shared_ptr<DecodeOptions> o (new DecodeOptions);
+ o->decode_audio = false;
+
- descend (0.5);
-
- pair<shared_ptr<VideoDecoder>, shared_ptr<AudioDecoder> > decoders = decoder_factory (_film, o, this);
++ Decoders decoders = decoder_factory (_film, o, this);
+
+ set_progress_unknown ();
- while (!decoders.first->pass()) {
++ while (!decoders.video->pass()) {
+ /* keep going */
+ }
+
- _film->set_length (decoders.first->video_frame());
++ _film->set_length (decoders.video->video_frame());
+
+ _film->log()->log (String::compose ("Video length examined as %1 frames", _film->length().get()));
+
- ascend ();
-
- progress_remaining -= 0.5;
-
+ } else {
+
- /* Get a quick decoder to get the content's length from its header.
- It would have been nice to just use the thumbnail transcoder's decoder,
- but that's a bit fiddly, and this isn't too expensive.
- */
++ /* Get a quick decoder to get the content's length from its header */
+
- shared_ptr<Options> o (new Options ("", "", ""));
- o->out_size = Size (1024, 1024);
- pair<shared_ptr<VideoDecoder>, shared_ptr<AudioDecoder> > d = decoder_factory (_film, o, 0);
- _film->set_length (d.first->length());
++ shared_ptr<DecodeOptions> o (new DecodeOptions);
++ Decoders d = decoder_factory (_film, o, 0);
++ _film->set_length (d.video->length());
- shared_ptr<DecodeOptions> o (new DecodeOptions);
- o->decode_audio = false;
-
- descend (1);
-
- Decoders decoders = decoder_factory (_film, o, this);
-
- set_progress_unknown ();
- while (!decoders.video->pass()) {
- /* keep going */
+ _film->log()->log (String::compose ("Video length obtained from header as %1 frames", _film->length().get()));
}
- _film->set_length (decoders.video->video_frame());
- /* Now make thumbnails for it */
--
- _film->log()->log (String::compose ("Video length is %1 frames", _film->length()));
- descend (progress_remaining);
-
- try {
- shared_ptr<Options> o (new Options (_film->dir ("thumbs"), ".png", ""));
- o->out_size = _film->size ();
- o->apply_crop = false;
- o->decode_audio = false;
- o->decode_video_skip = _film->length().get() / 128;
- o->decode_subtitles = true;
- shared_ptr<ImageMagickEncoder> e (new ImageMagickEncoder (_film, o));
- Transcoder w (_film, o, this, e);
- w.go ();
-
- /* Now set the film's length from the transcoder's decoder, since we
- went to all the trouble of going through the content.
- */
-
- _film->set_length (w.video_decoder()->video_frame());
-
- } catch (std::exception& e) {
-
- ascend ();
- set_progress (1);
- set_error (e.what ());
- set_state (FINISHED_ERROR);
- return;
-
- }
-
- string const tdir = _film->dir ("thumbs");
- vector<SourceFrame> thumbs;
-
- for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (tdir); i != boost::filesystem::directory_iterator(); ++i) {
-
- /* Aah, the sweet smell of progress */
-#if BOOST_FILESYSTEM_VERSION == 3
- string const l = boost::filesystem::path(*i).leaf().generic_string();
-#else
- string const l = i->leaf ();
-#endif
-
- size_t const d = l.find (".png");
- size_t const t = l.find (".tmp");
- if (d != string::npos && t == string::npos) {
- thumbs.push_back (atoi (l.substr (0, d).c_str()));
- }
- }
-
- sort (thumbs.begin(), thumbs.end());
- _film->set_thumbs (thumbs);
--
ascend ();
set_progress (1);
set_state (FINISHED_OK);
return String::compose ("ffmpeg %1 %2 %3 %4", _id, _sample_rate, _channel_layout, _name);
}
-
+void
+FFmpegDecoder::out_with_sync ()
+{
+ /* Where we are in the output, in seconds */
+ double const out_pts_seconds = video_frame() / frames_per_second();
+
+ /* Where we are in the source, in seconds */
+ double const source_pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base)
+ * av_frame_get_best_effort_timestamp(_frame);
+
+ _film->log()->log (
+ String::compose ("Source video frame ready; source at %1, output at %2", source_pts_seconds, out_pts_seconds),
+ Log::VERBOSE
+ );
+
+ if (!_first_video) {
+ _first_video = source_pts_seconds;
+ }
+
+ /* Difference between where we are and where we should be */
+ double const delta = source_pts_seconds - _first_video.get() - out_pts_seconds;
+ double const one_frame = 1 / frames_per_second();
+
+ /* Insert frames if required to get out_pts_seconds up to pts_seconds */
+ if (delta > one_frame) {
+ int const extra = rint (delta / one_frame);
+ for (int i = 0; i < extra; ++i) {
+ repeat_last_video ();
+ _film->log()->log (
+ String::compose (
+ "Extra video frame inserted at %1s; source frame %2, source PTS %3 (at %4 fps)",
+ out_pts_seconds, video_frame(), source_pts_seconds, frames_per_second()
+ )
+ );
+ }
+ }
+
+ if (delta > -one_frame) {
+ /* Process this frame */
+ filter_and_emit_video (_frame);
+ } else {
+ /* Otherwise we are omitting a frame to keep things right */
+ _film->log()->log (String::compose ("Frame removed at %1s", out_pts_seconds));
+ }
+}
+
+void
+FFmpegDecoder::film_changed (Film::Property p)
+{
+ switch (p) {
+ case Film::CROP:
+ {
+ boost::mutex::scoped_lock lm (_filter_graphs_mutex);
+ _filter_graphs.clear ();
+ }
+ OutputChanged ();
+ break;
+
+ default:
+ break;
+ }
+}
+
+ /** @return Length (in video frames) according to our content's header */
+ SourceFrame
+ FFmpegDecoder::length () const
+ {
+ return (double(_format_context->duration) / AV_TIME_BASE) * frames_per_second();
+ }
++
using std::string;
using std::pair;
using std::max;
+using std::cout;
using boost::shared_ptr;
-class ThumbPanel : public wxPanel
+FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
+ : wxPanel (p)
+ , _panel (new wxPanel (this))
+ , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
+ , _play_button (new wxToggleButton (this, wxID_ANY, wxT ("Play")))
+ , _out_width (0)
+ , _out_height (0)
+ , _panel_width (0)
+ , _panel_height (0)
{
-public:
- ThumbPanel (wxPanel* parent, shared_ptr<Film> film)
- : wxPanel (parent)
- , _film (film)
- , _index (0)
- , _frame_rebuild_needed (false)
- , _composition_needed (false)
- {}
-
- /** Handle a paint event */
- void paint_event (wxPaintEvent& ev)
- {
- if (!_film || _film->thumbs().size() == 0) {
- wxPaintDC dc (this);
- return;
- }
-
- if (_frame_rebuild_needed) {
- _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
-
- _subtitle.reset ();
- pair<Position, string> s = _film->thumb_subtitle (_index);
- if (!s.second.empty ()) {
- _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
- }
-
- _frame_rebuild_needed = false;
- compose ();
- }
+ wxBoxSizer* v_sizer = new wxBoxSizer (wxVERTICAL);
+ SetSizer (v_sizer);
- if (_composition_needed) {
- compose ();
- }
+ v_sizer->Add (_panel, 1, wxEXPAND);
- wxPaintDC dc (this);
- if (_bitmap) {
- dc.DrawBitmap (*_bitmap, 0, 0, false);
- }
+ wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
+ h_sizer->Add (_play_button, 0, wxEXPAND);
+ h_sizer->Add (_slider, 1, wxEXPAND);
- if (_film->with_subtitles() && _subtitle) {
- dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
- }
- }
+ v_sizer->Add (h_sizer, 0, wxEXPAND);
- /** Handle a size event */
- void size_event (wxSizeEvent &)
- {
- if (!_image) {
- return;
- }
+ _panel->Bind (wxEVT_PAINT, &FilmViewer::paint_panel, this);
+ _panel->Bind (wxEVT_SIZE, &FilmViewer::panel_sized, this);
+ _slider->Bind (wxEVT_SCROLL_THUMBTRACK, &FilmViewer::slider_moved, this);
+ _slider->Bind (wxEVT_SCROLL_PAGEUP, &FilmViewer::slider_moved, this);
+ _slider->Bind (wxEVT_SCROLL_PAGEDOWN, &FilmViewer::slider_moved, this);
+ _play_button->Bind (wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &FilmViewer::play_clicked, this);
+ _timer.Bind (wxEVT_TIMER, &FilmViewer::timer, this);
- recompose ();
- }
+ set_film (_film);
+}
- /** @param n Thumbnail index */
- void set (int n)
+void
+FilmViewer::film_changed (Film::Property p)
+{
+ switch (p) {
+ case Film::FORMAT:
+ calculate_sizes ();
+ update_from_raw ();
+ break;
+ case Film::CONTENT:
{
- _index = n;
- _frame_rebuild_needed = true;
- Refresh ();
+ shared_ptr<DecodeOptions> o (new DecodeOptions);
+ o->decode_audio = false;
+ o->video_sync = false;
+ _decoders = decoder_factory (_film, o, 0);
+ _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2));
+ _decoders.video->OutputChanged.connect (boost::bind (&FilmViewer::decoder_changed, this));
+ break;
}
-
- void set_film (shared_ptr<Film> f)
- {
- _film = f;
- if (!_film) {
- clear ();
- _frame_rebuild_needed = true;
- Refresh ();
- } else {
- _frame_rebuild_needed = true;
- Refresh ();
- }
+ default:
+ break;
}
+}
- /** Clear our thumbnail image */
- void clear ()
- {
- _bitmap.reset ();
- _image.reset ();
- _subtitle.reset ();
+void
+FilmViewer::set_film (shared_ptr<Film> f)
+{
+ if (_film == f) {
+ return;
}
+
+ _film = f;
- void recompose ()
- {
- _composition_needed = true;
- Refresh ();
+ if (!_film) {
+ return;
}
- DECLARE_EVENT_TABLE ();
-
-private:
+ _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
- void compose ()
- {
- _composition_needed = false;
-
- if (!_film || !_image) {
- return;
- }
-
- /* Size of the view */
- int vw, vh;
- GetSize (&vw, &vh);
-
- Crop const fc = _film->crop ();
-
- /* Cropped rectangle */
- Rect cropped_area (
- fc.left,
- fc.top,
- _image->GetWidth() - (fc.left + fc.right),
- _image->GetHeight() - (fc.top + fc.bottom)
- );
-
- /* Target ratio */
- float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
-
- _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
-
- float x_scale = 1;
- float y_scale = 1;
-
- if ((float (vw) / vh) > target) {
- /* view is longer (horizontally) than the ratio; fit height */
- _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
- x_scale = vh * target / cropped_area.width;
- y_scale = float (vh) / cropped_area.height;
- } else {
- /* view is shorter (horizontally) than the ratio; fit width */
- _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
- x_scale = float (vw) / cropped_area.width;
- y_scale = (vw / target) / cropped_area.height;
- }
+ film_changed (Film::CONTENT);
+ film_changed (Film::CROP);
+ film_changed (Film::FORMAT);
+}
- _bitmap.reset (new wxBitmap (_transformed_image));
+void
+FilmViewer::decoder_changed ()
+{
+ seek_and_update (_decoders.video->last_source_frame ());
+}
- if (_subtitle) {
+void
+FilmViewer::timer (wxTimerEvent& ev)
+{
+ _panel->Refresh ();
+ _panel->Update ();
- _subtitle->transformed_area = subtitle_transformed_area (
- x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
- );
+ shared_ptr<Image> last = _display;
+ while (last == _display) {
+ _decoders.video->pass ();
+ }
- _subtitle->transformed_image = _subtitle->base_image;
- _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
- _subtitle->transformed_area.x -= rint (_film->crop().left * x_scale);
- _subtitle->transformed_area.y -= rint (_film->crop().top * y_scale);
- _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
+ if (_film->length()) {
+ int const new_slider_position = 4096 * _decoders.video->last_source_frame() / _film->length().get();
+ if (new_slider_position != _slider->GetValue()) {
+ _slider->SetValue (new_slider_position);
}
}
+}
- shared_ptr<Film> _film;
- shared_ptr<wxImage> _image;
- wxImage _transformed_image;
- /** currently-displayed thumbnail index */
- int _index;
- shared_ptr<wxBitmap> _bitmap;
- bool _frame_rebuild_needed;
- bool _composition_needed;
- struct SubtitleView
- {
- SubtitleView (Position p, wxString const & i)
- : base_image (i)
- {
- base_area.x = p.x;
- base_area.y = p.y;
- base_area.width = base_image.GetWidth ();
- base_area.height = base_image.GetHeight ();
- }
+void
+FilmViewer::paint_panel (wxPaintEvent& ev)
+{
+ wxPaintDC dc (_panel);
+ if (!_display) {
+ return;
+ }
- Rect base_area;
- Rect transformed_area;
- wxImage base_image;
- wxImage transformed_image;
- shared_ptr<wxBitmap> bitmap;
- };
+ wxImage i (_out_width, _out_height, _display->data()[0], true);
+ wxBitmap b (i);
+ dc.DrawBitmap (b, 0, 0);
+}
- shared_ptr<SubtitleView> _subtitle;
-};
-BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
-EVT_PAINT (ThumbPanel::paint_event)
-EVT_SIZE (ThumbPanel::size_event)
-END_EVENT_TABLE ()
+void
+FilmViewer::slider_moved (wxCommandEvent& ev)
+{
+ if (_film->length()) {
+ seek_and_update (_slider->GetValue() * _film->length().get() / 4096);
+ }
+}
-FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
- : wxPanel (p)
+void
+FilmViewer::seek_and_update (SourceFrame f)
{
- _decoders.video->seek (f);
- _sizer = new wxBoxSizer (wxVERTICAL);
- SetSizer (_sizer);
++ if (_decoders.video->seek (f)) {
++ return;
++ }
- _thumb_panel = new ThumbPanel (this, f);
- _sizer->Add (_thumb_panel, 1, wxEXPAND);
-
- int const m = max ((size_t) 1, f ? f->thumbs().size() - 1 : 0);
- _slider = new wxSlider (this, wxID_ANY, 0, 0, m);
- _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
- set_thumbnail (0);
-
- _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
+ shared_ptr<Image> last = _display;
+ while (last == _display) {
+ _decoders.video->pass ();
+ }
+ _panel->Refresh ();
+ _panel->Update ();
+}
- set_film (_film);
+void
+FilmViewer::panel_sized (wxSizeEvent& ev)
+{
+ _panel_width = ev.GetSize().GetWidth();
+ _panel_height = ev.GetSize().GetHeight();
+ calculate_sizes ();
+ update_from_raw ();
}
void