Add Trimmer class; not linked in.
authorCarl Hetherington <cth@carlh.net>
Sun, 14 Apr 2013 22:41:57 +0000 (23:41 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 14 Apr 2013 22:41:57 +0000 (23:41 +0100)
src/lib/trimmer.cc [new file with mode: 0644]
src/lib/trimmer.h [new file with mode: 0644]
src/lib/writer.cc
src/lib/wscript
test/metadata.ref
test/test.cc

diff --git a/src/lib/trimmer.cc b/src/lib/trimmer.cc
new file mode 100644 (file)
index 0000000..68364e5
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+    Copyright (C) 2013 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
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <boost/shared_ptr.hpp>
+#include "trimmer.h"
+
+using std::cout;
+using std::max;
+using boost::shared_ptr;
+
+/** @param audio_sample_rate Audio sampling rate, or 0 if there is no audio */
+Trimmer::Trimmer (
+       shared_ptr<Log> log,
+       int video_trim_start,
+       int video_trim_end, int video_length,
+       int audio_sample_rate,
+       float frames_per_second,
+       int dcp_frames_per_second
+       )
+       : AudioVideoProcessor (log)
+       , _video_start (video_trim_start)
+       , _video_end (video_length - video_trim_end)
+       , _video_in (0)
+       , _audio_in (0)
+{
+       FrameRateConversion frc (frames_per_second, dcp_frames_per_second);
+
+       if (frc.skip) {
+               _video_start /= 2;
+               _video_end /= 2;
+       } else if (frc.repeat) {
+               _video_start *= 2;
+               _video_end *= 2;
+       }
+
+       if (audio_sample_rate) {
+               _audio_start = video_frames_to_audio_frames (_video_start, audio_sample_rate, frames_per_second);
+               _audio_end = video_frames_to_audio_frames (_video_end, audio_sample_rate, frames_per_second);
+       }
+}
+
+void
+Trimmer::process_video (shared_ptr<Image> image, bool same, shared_ptr<Subtitle> sub)
+{
+       if (_video_in >= _video_start && _video_in <= _video_end) {
+               Video (image, same, sub);
+       }
+       
+       ++_video_in;
+}
+
+void
+Trimmer::process_audio (shared_ptr<AudioBuffers> audio)
+{
+       int64_t offset = _audio_start - _audio_in;
+       if (offset > audio->frames()) {
+               _audio_in += audio->frames ();
+               return;
+       }
+
+       if (offset < 0) {
+               offset = 0;
+       }
+
+       int64_t length = _audio_end - max (_audio_in, _audio_start);
+       if (length < 0) {
+               _audio_in += audio->frames ();
+               return;
+       }
+
+       if (length > (audio->frames() - offset)) {
+               length = audio->frames () - offset;
+       }
+
+       _audio_in += audio->frames ();
+       
+       if (offset != 0 || length != audio->frames ()) {
+               audio->move (offset, 0, length);
+               audio->set_frames (length);
+       }
+       
+       Audio (audio);
+}
+
diff --git a/src/lib/trimmer.h b/src/lib/trimmer.h
new file mode 100644 (file)
index 0000000..ff7e951
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+    Copyright (C) 2013 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
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "processor.h"
+
+class Trimmer : public AudioVideoProcessor
+{
+public:
+       Trimmer (boost::shared_ptr<Log>, int, int, int, int, float, int);
+
+       void process_video (boost::shared_ptr<Image> i, bool, boost::shared_ptr<Subtitle> s);
+       void process_audio (boost::shared_ptr<AudioBuffers>);
+
+private:
+       friend class trimmer_test;
+
+       int _video_start;
+       int _video_end;
+       int _video_in;
+       int64_t _audio_start;
+       int64_t _audio_end;
+       int64_t _audio_in;
+};
index c6ce4711d6890fd2e2b92d8585d003ab23da5cdb..ad81686d108a9e4ea3603a90250b4a2046383925 100644 (file)
@@ -259,9 +259,14 @@ Writer::finish ()
        }
 
        int const frames = _last_written_frame + 1;
-       int const duration = frames - _film->trim_start() - _film->trim_end();
+       int duration = 0;
+       if (_film->trim_type() == Film::CPL) {
+               duration = frames - _film->trim_start() - _film->trim_end();
+               _picture_asset->set_entry_point (_film->trim_start ());
+       } else {
+               duration = frames;
+       }
        
-       _picture_asset->set_entry_point (_film->trim_start ());
        _picture_asset->set_duration (duration);
 
        /* Hard-link the video MXF into the DCP */
@@ -288,7 +293,9 @@ Writer::finish ()
        _picture_asset->set_file_name (_film->dcp_video_mxf_filename ());
 
        if (_sound_asset) {
-               _sound_asset->set_entry_point (_film->trim_start ());
+               if (_film->trim_type() == Film::CPL) {
+                       _sound_asset->set_entry_point (_film->trim_start ());
+               }
                _sound_asset->set_duration (duration);
        }
        
index 8e9d3470665281269b3ad4c5ebdcdb2c323f9eb8..51b103afdc3e639fb357a8e09551551e6f3864b9 100644 (file)
@@ -45,6 +45,7 @@ sources = """
           timer.cc
           transcode_job.cc
           transcoder.cc
+          trimmer.cc
           ui_signaller.cc
           util.cc
           video_decoder.cc
index 16e3837c8c64274a4d409f5be719c63e490e99e2..b7a03188341e1d320f35ea6ae3aa321cc1aa014e 100644 (file)
@@ -14,6 +14,7 @@ filter unsharp
 scaler bicubic
 trim_start 42
 trim_end 99
+trim_type cpl
 dcp_ab 1
 use_content_audio 1
 audio_gain 0
index 6fad0986b4c8728c0d2f7fcdbf6e3a9de7ecbc75..ac7ab85fe5bd2df8a205181dcfe778c722b7553c 100644 (file)
@@ -40,6 +40,7 @@
 #include "scaler.h"
 #include "ffmpeg_decoder.h"
 #include "sndfile_decoder.h"
+#include "trimmer.h"
 #define BOOST_TEST_DYN_LINK
 #define BOOST_TEST_MODULE dvdomatic_test
 #include <boost/test/unit_test.hpp>
@@ -123,6 +124,55 @@ BOOST_AUTO_TEST_CASE (make_black_test)
        }
 }
 
+shared_ptr<AudioBuffers> trimmer_test_last;
+
+void
+trimmer_test_helper (shared_ptr<AudioBuffers> audio)
+{
+       trimmer_test_last = audio;
+}
+
+/** Test the audio handling of the Trimmer */
+BOOST_AUTO_TEST_CASE (trimmer_test)
+{
+       Trimmer trimmer (shared_ptr<Log> (), 25, 75, 200, 48000, 25, 25);
+
+       trimmer.Audio.connect (bind (&trimmer_test_helper, _1));
+
+       /* 21 video frames-worth of audio frames; should be completely stripped */
+       trimmer_test_last.reset ();
+       shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 21 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last == 0);
+
+       /* 42 more video frames-worth, 4 should be stripped from the start */
+       audio.reset (new AudioBuffers (6, 42 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last);
+       BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 38 * 1920);
+
+       /* 42 more video frames-worth, should be kept as-is */
+       trimmer_test_last.reset ();
+       audio.reset (new AudioBuffers (6, 42 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last);
+       BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 42 * 1920);
+
+       /* 25 more video frames-worth, 5 should be trimmed from the end */
+       trimmer_test_last.reset ();
+       audio.reset (new AudioBuffers (6, 25 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last);
+       BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 20 * 1920);
+
+       /* Now some more; all should be trimmed */
+       trimmer_test_last.reset ();
+       audio.reset (new AudioBuffers (6, 100 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last == 0);
+}
+
+
 BOOST_AUTO_TEST_CASE (film_metadata_test)
 {
        setup_test_config ();
@@ -863,3 +913,4 @@ BOOST_AUTO_TEST_CASE (aligned_image_test)
        delete t;
        delete u;
 }
+