From ee161be0fb0e93d77d34fb3f13fd562a76738a07 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 1 May 2014 00:25:30 +0100 Subject: [PATCH] Various fixes so that audio_delay_test works again. --- src/lib/audio_buffers.cc | 2 + src/lib/audio_content.cc | 6 + src/lib/audio_content.h | 2 +- src/lib/audio_decoder.cc | 24 +++- src/lib/audio_mapping.cc | 8 +- src/lib/audio_mapping.h | 4 +- src/lib/dcpomatic_time.h | 4 + src/lib/ffmpeg_content.cc | 2 +- src/lib/player.cc | 55 ++++++---- src/lib/player.h | 14 ++- src/lib/sndfile_content.cc | 2 +- test/README | 3 +- test/audio_decoder_test.cc | 165 ++++++++++++++++++++++++++++ test/player_silence_padding_test.cc | 59 ---------- test/player_test.cc | 104 ++++++++++++++++++ test/wscript | 3 +- 16 files changed, 356 insertions(+), 101 deletions(-) create mode 100644 test/audio_decoder_test.cc delete mode 100644 test/player_silence_padding_test.cc create mode 100644 test/player_test.cc diff --git a/src/lib/audio_buffers.cc b/src/lib/audio_buffers.cc index 8d00fa8ba..99e52d92a 100644 --- a/src/lib/audio_buffers.cc +++ b/src/lib/audio_buffers.cc @@ -258,6 +258,8 @@ void AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames) { assert (_channels == from->channels ()); + assert (read_offset >= 0); + assert (write_offset >= 0); for (int i = 0; i < _channels; ++i) { for (int j = 0; j < frames; ++j) { diff --git a/src/lib/audio_content.cc b/src/lib/audio_content.cc index 6da5afa0c..c84f57130 100644 --- a/src/lib/audio_content.cc +++ b/src/lib/audio_content.cc @@ -157,3 +157,9 @@ AudioContent::technical_summary () const output_audio_frame_rate() ); } + +void +AudioContent::set_audio_mapping (AudioMapping) +{ + signal_changed (AudioContentProperty::AUDIO_MAPPING); +} diff --git a/src/lib/audio_content.h b/src/lib/audio_content.h index cecc8f13d..b9ca997a7 100644 --- a/src/lib/audio_content.h +++ b/src/lib/audio_content.h @@ -56,7 +56,7 @@ public: virtual int content_audio_frame_rate () const = 0; virtual int output_audio_frame_rate () const = 0; virtual AudioMapping audio_mapping () const = 0; - virtual void set_audio_mapping (AudioMapping) = 0; + virtual void set_audio_mapping (AudioMapping); virtual boost::filesystem::path audio_analysis_path () const; boost::signals2::connection analyse_audio (boost::function); diff --git a/src/lib/audio_decoder.cc b/src/lib/audio_decoder.cc index 17a534aa4..c0c92476a 100644 --- a/src/lib/audio_decoder.cc +++ b/src/lib/audio_decoder.cc @@ -32,6 +32,7 @@ using std::list; using std::pair; using std::cout; using std::min; +using std::max; using boost::optional; using boost::shared_ptr; @@ -63,6 +64,9 @@ AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate) seek (ContentTime::from_frames (frame, _audio_content->content_audio_frame_rate()), accurate); } + /* Offset of the data that we want from the start of _decoded_audio.audio + (to be set up shortly) + */ AudioFrame decoded_offset = 0; /* Now enough pass() calls will either: @@ -73,22 +77,32 @@ AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate) * otherwise any frames will do. */ if (accurate) { - while (!pass() && _decoded_audio.audio->frames() < length) {} - /* Use decoded_offset of 0, as we don't really care what frames we return */ - } else { + /* Keep stuffing data into _decoded_audio until we have enough data, or the subclass does not want to give us any more */ while (!pass() && (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end)) {} decoded_offset = frame - _decoded_audio.frame; + } else { + while (!pass() && _decoded_audio.audio->frames() < length) {} + /* Use decoded_offset of 0, as we don't really care what frames we return */ } + /* The amount of data available in _decoded_audio.audio starting from `frame'. This could be -ve + if pass() returned true before we got enough data + */ AudioFrame const amount_left = _decoded_audio.audio->frames() - decoded_offset; - AudioFrame const to_return = min (amount_left, length); + /* We will return either that, or the requested amount, whichever is smaller */ + AudioFrame const to_return = max ((AudioFrame) 0, min (amount_left, length)); + + /* Copy our data to the output */ shared_ptr out (new AudioBuffers (_decoded_audio.audio->channels(), to_return)); out->copy_from (_decoded_audio.audio.get(), to_return, decoded_offset, 0); - /* Clean up decoded */ + /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */ _decoded_audio.audio->move (decoded_offset + to_return, 0, amount_left - to_return); + /* And set up the number of frames we have left */ _decoded_audio.audio->set_frames (amount_left - to_return); + /* Also bump where those frames are in terms of the content */ + _decoded_audio.frame += decoded_offset + to_return; return shared_ptr (new ContentAudio (out, frame)); } diff --git a/src/lib/audio_mapping.cc b/src/lib/audio_mapping.cc index 1db827046..f6d747b9b 100644 --- a/src/lib/audio_mapping.cc +++ b/src/lib/audio_mapping.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington 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 @@ -39,11 +39,11 @@ AudioMapping::AudioMapping () } /** Create a default AudioMapping for a given channel count. - * @param c Number of channels. + * @param channels Number of channels. */ -AudioMapping::AudioMapping (int c) +AudioMapping::AudioMapping (int channels) { - setup (c); + setup (channels); } void diff --git a/src/lib/audio_mapping.h b/src/lib/audio_mapping.h index f3096764c..d3f497fc2 100644 --- a/src/lib/audio_mapping.h +++ b/src/lib/audio_mapping.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington 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 @@ -41,7 +41,7 @@ class AudioMapping { public: AudioMapping (); - AudioMapping (int); + AudioMapping (int channels); AudioMapping (boost::shared_ptr, int); /* Default copy constructor is fine */ diff --git a/src/lib/dcpomatic_time.h b/src/lib/dcpomatic_time.h index 76fbe6902..109dc9b7f 100644 --- a/src/lib/dcpomatic_time.h +++ b/src/lib/dcpomatic_time.h @@ -186,6 +186,10 @@ public: return *this; } + DCPTime operator- () const { + return DCPTime (-_t); + } + DCPTime operator- (DCPTime const & o) const { return DCPTime (_t - o._t); } diff --git a/src/lib/ffmpeg_content.cc b/src/lib/ffmpeg_content.cc index 447f3631c..4e14802e8 100644 --- a/src/lib/ffmpeg_content.cc +++ b/src/lib/ffmpeg_content.cc @@ -449,7 +449,7 @@ void FFmpegContent::set_audio_mapping (AudioMapping m) { audio_stream()->mapping = m; - signal_changed (AudioContentProperty::AUDIO_MAPPING); + AudioContent::set_audio_mapping (m); } string diff --git a/src/lib/player.cc b/src/lib/player.cc index 2450ace2e..817a390d6 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -311,7 +311,11 @@ Player::get_video (DCPTime time, bool accurate) setup_pieces (); } - list > ov = overlaps (time); + list > ov = overlaps ( + time, + time + DCPTime::from_frames (1, _film->video_frame_rate ()) + ); + if (ov.empty ()) { /* No video content at this time */ return black_dcp_video (time); @@ -351,7 +355,11 @@ Player::get_video (DCPTime time, bool accurate) /* Add subtitles */ - ov = overlaps (time); + ov = overlaps ( + time, + time + DCPTime::from_frames (1, _film->video_frame_rate ()) + ); + list sub_images; for (list >::const_iterator i = ov.begin(); i != ov.end(); ++i) { @@ -398,7 +406,7 @@ Player::get_audio (DCPTime time, DCPTime length, bool accurate) shared_ptr audio (new AudioBuffers (_film->audio_channels(), length_frames)); audio->make_silent (); - list > ov = overlaps (time); + list > ov = overlaps (time, time + length); if (ov.empty ()) { return audio; } @@ -417,10 +425,21 @@ Player::get_audio (DCPTime time, DCPTime length, bool accurate) continue; } - AudioFrame const content_time = dcp_to_content_audio (*i, time); + /* The time that we should request from the content */ + DCPTime request = time - DCPTime::from_seconds (content->audio_delay() / 1000.0); + DCPTime offset; + if (request < DCPTime ()) { + /* We went off the start of the content, so we will need to offset + the stuff we get back. + */ + offset = -request; + request = DCPTime (); + } + + AudioFrame const content_frame = dcp_to_content_audio (*i, request); - /* Audio from this piece's decoder (which might be more than what we asked for) */ - shared_ptr all = decoder->get_audio (content_time, length_frames, accurate); + /* Audio from this piece's decoder (which might be more or less than what we asked for) */ + shared_ptr all = decoder->get_audio (content_frame, length_frames, accurate); /* Gain */ if (content->audio_gain() != 0) { @@ -447,25 +466,13 @@ Player::get_audio (DCPTime time, DCPTime length, bool accurate) } all->audio = dcp_mapped; - - /* Delay */ - /* XXX - audio->dcp_time += content->audio_delay() * TIME_HZ / 1000; - if (audio->dcp_time < 0) { - int const frames = - audio->dcp_time * _film->audio_frame_rate() / TIME_HZ; - if (frames >= audio->audio->frames ()) { - return; - } - - shared_ptr trimmed (new AudioBuffers (audio->audio->channels(), audio->audio->frames() - frames)); - trimmed->copy_from (audio->audio.get(), audio->audio->frames() - frames, frames, 0); - - audio->audio = trimmed; - audio->dcp_time = 0; - } - */ - audio->accumulate_frames (all->audio.get(), all->frame - content_time, 0, min (AudioFrame (all->audio->frames()), length_frames)); + audio->accumulate_frames ( + all->audio.get(), + content_frame - all->frame, + offset.frames (_film->audio_frame_rate()), + min (AudioFrame (all->audio->frames()), length_frames) - offset.frames (_film->audio_frame_rate ()) + ); } return audio; diff --git a/src/lib/player.h b/src/lib/player.h index 62ba89e6c..b70bd3f64 100644 --- a/src/lib/player.h +++ b/src/lib/player.h @@ -126,6 +126,7 @@ public: private: friend class PlayerWrapper; friend class Piece; + friend class player_overlaps_test; void setup_pieces (); void playlist_changed (); @@ -142,13 +143,22 @@ private: ContentTime dcp_to_content_subtitle (boost::shared_ptr piece, DCPTime t) const; boost::shared_ptr black_dcp_video (DCPTime) const; + /** @return Pieces of content type C that overlap a specified time range in the DCP */ template std::list > - overlaps (DCPTime t) + overlaps (DCPTime from, DCPTime to) { + if (!_have_valid_pieces) { + setup_pieces (); + } + std::list > overlaps; for (typename std::list >::const_iterator i = _pieces.begin(); i != _pieces.end(); ++i) { - if (boost::dynamic_pointer_cast ((*i)->content) && (*i)->content->position() <= t && t < (*i)->content->end()) { + if (!boost::dynamic_pointer_cast ((*i)->content)) { + continue; + } + + if ((*i)->content->position() <= to && (*i)->content->end() >= from) { overlaps.push_back (*i); } } diff --git a/src/lib/sndfile_content.cc b/src/lib/sndfile_content.cc index 2d7fa1c1c..5b408f2da 100644 --- a/src/lib/sndfile_content.cc +++ b/src/lib/sndfile_content.cc @@ -163,6 +163,6 @@ SndfileContent::set_audio_mapping (AudioMapping m) _audio_mapping = m; } - signal_changed (AudioContentProperty::AUDIO_MAPPING); + AudioContent::set_audio_mapping (m); } diff --git a/test/README b/test/README index 6337656f1..a129b6617 100644 --- a/test/README +++ b/test/README @@ -7,10 +7,12 @@ They can be grouped roughly into the following: AudioAnalysis: audio_analysis_test AudioBuffers: audio_buffers_test +AudioDecoder: audio_decoder_test AudioMapping: audio_mapping_test ColourConversion: colour_conversion_test FileGroup: file_group_test Image: image_test, pixel_formats_test, make_black_test +Player: player_test Job/JobManager: job_test SubRip: subrip_test Ratio: ratio_test @@ -30,7 +32,6 @@ black_fill_test client_server_test film_metadata_test frame_rate_test -player_silence_padding_test recover_test repeat_frame_test scaling_test diff --git a/test/audio_decoder_test.cc b/test/audio_decoder_test.cc new file mode 100644 index 000000000..46f9d5ac6 --- /dev/null +++ b/test/audio_decoder_test.cc @@ -0,0 +1,165 @@ +/* + Copyright (C) 2014 Carl Hetherington + + 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. + +*/ + +/** @file test/audio_decoder_test.cc + * @brief Tests of the AudioDecoder class. + */ + +#include +#include +#include "test.h" +#include "lib/audio_decoder.h" +#include "lib/audio_content.h" + +using std::string; +using std::cout; +using std::min; +using boost::shared_ptr; + +class TestAudioDecoder : public AudioDecoder +{ +public: + TestAudioDecoder (shared_ptr content) + : AudioDecoder (content) + , _position (0) + {} + + bool pass () + { + AudioFrame const N = min ( + AudioFrame (2000), + _audio_content->audio_length().frames (_audio_content->output_audio_frame_rate ()) - _position + ); + + shared_ptr buffers (new AudioBuffers (_audio_content->audio_channels(), N)); + for (int i = 0; i < _audio_content->audio_channels(); ++i) { + for (int j = 0; j < N; ++j) { + buffers->data(i)[j] = j + _position; + } + } + + audio (buffers, ContentTime::from_frames (_position, _audio_content->output_audio_frame_rate ())); + _position += N; + + return N < 2000; + } + + void seek (ContentTime t, bool accurate) + { + AudioDecoder::seek (t, accurate); + _position = t.frames (_audio_content->output_audio_frame_rate ()); + } + +private: + AudioFrame _position; +}; + +class TestAudioContent : public AudioContent +{ +public: + TestAudioContent (shared_ptr film) + : Content (film) + , AudioContent (film, DCPTime ()) + {} + + string summary () const { + return ""; + } + + string information () const { + return ""; + } + + DCPTime full_length () const { + return DCPTime (audio_length().get ()); + } + + int audio_channels () const { + return 2; + } + + ContentTime audio_length () const { + return ContentTime::from_seconds (61.2942); + } + + int content_audio_frame_rate () const { + return 48000; + } + + int output_audio_frame_rate () const { + return 48000; + } + + AudioMapping audio_mapping () const { + return AudioMapping (audio_channels ()); + } + + void set_audio_mapping (AudioMapping) {} +}; + +shared_ptr content; +shared_ptr decoder; + +static shared_ptr +get (AudioFrame from, AudioFrame length) +{ + decoder->seek (ContentTime::from_frames (from, content->output_audio_frame_rate ()), true); + shared_ptr ca = decoder->get_audio (from, length, true); + BOOST_CHECK_EQUAL (ca->frame, from); + return ca; +} + +static void +check (AudioFrame from, AudioFrame length) +{ + shared_ptr ca = get (from, length); + for (int i = 0; i < content->audio_channels(); ++i) { + for (int j = 0; j < length; ++j) { + BOOST_CHECK_EQUAL (ca->audio->data(i)[j], j + from); + assert (ca->audio->data(i)[j] == j + from); + } + } +} + +/** Check the logic in AudioDecoder::get_audio */ +BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test) +{ + shared_ptr film = new_test_film ("audio_decoder_test"); + + content.reset (new TestAudioContent (film)); + decoder.reset (new TestAudioDecoder (content)); + + /* Simple reads */ + check (0, 48000); + check (44, 9123); + check (9991, 22); + + /* Read off the end */ + + AudioFrame const from = content->output_audio_frame_rate() * 61; + AudioFrame const length = content->output_audio_frame_rate() * 4; + shared_ptr ca = get (from, length); + + for (int i = 0; i < content->audio_channels(); ++i) { + for (int j = 0; j < ca->audio->frames(); ++j) { + BOOST_CHECK_EQUAL (ca->audio->data(i)[j], j + from); + assert (ca->audio->data(i)[j] == j + from); + } + } +} diff --git a/test/player_silence_padding_test.cc b/test/player_silence_padding_test.cc deleted file mode 100644 index fa8a9b6e6..000000000 --- a/test/player_silence_padding_test.cc +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright (C) 2014 Carl Hetherington - - 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. - -*/ - -/* @file test/player_silence_padding_test.cc - * @brief Check that the Player correctly generates silence when used with a silent FFmpegContent. - */ - -#include -#include -#include "lib/film.h" -#include "lib/ffmpeg_content.h" -#include "lib/dcp_content_type.h" -#include "lib/ratio.h" -#include "lib/audio_buffers.h" -#include "lib/player.h" -#include "test.h" - -using std::cout; -using boost::shared_ptr; - -BOOST_AUTO_TEST_CASE (player_silence_padding_test) -{ - shared_ptr film = new_test_film ("player_silence_padding_test"); - film->set_name ("player_silence_padding_test"); - shared_ptr c (new FFmpegContent (film, "test/data/test.mp4")); - film->set_container (Ratio::from_id ("185")); - film->set_audio_channels (6); - - film->examine_and_add_content (c); - wait_for_jobs (); - - shared_ptr player = film->make_player (); - shared_ptr test = player->get_audio (DCPTime (0), DCPTime::from_seconds (1), true); - BOOST_CHECK_EQUAL (test->frames(), 48000); - BOOST_CHECK_EQUAL (test->channels(), film->audio_channels ()); - - for (int i = 0; i < test->frames(); ++i) { - for (int c = 0; c < test->channels(); ++c) { - BOOST_CHECK_EQUAL (test->data()[c][i], 0); - } - } -} - diff --git a/test/player_test.cc b/test/player_test.cc new file mode 100644 index 000000000..2412fc312 --- /dev/null +++ b/test/player_test.cc @@ -0,0 +1,104 @@ +/* + Copyright (C) 2014 Carl Hetherington + + 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. + +*/ + +/** @file test/player_test.cc + * @brief Various tests of Player. + */ + +#include +#include +#include "lib/film.h" +#include "lib/ffmpeg_content.h" +#include "lib/dcp_content_type.h" +#include "lib/ratio.h" +#include "lib/audio_buffers.h" +#include "lib/player.h" +#include "test.h" + +using std::cout; +using std::list; +using boost::shared_ptr; + +/** Player::overlaps */ +BOOST_AUTO_TEST_CASE (player_overlaps_test) +{ + shared_ptr film = new_test_film ("player_overlaps_test"); + film->set_container (Ratio::from_id ("185")); + shared_ptr A (new FFmpegContent (film, "test/data/test.mp4")); + shared_ptr B (new FFmpegContent (film, "test/data/test.mp4")); + shared_ptr C (new FFmpegContent (film, "test/data/test.mp4")); + + film->examine_and_add_content (A); + film->examine_and_add_content (B); + film->examine_and_add_content (C); + wait_for_jobs (); + + BOOST_CHECK_EQUAL (A->full_length(), DCPTime::from_seconds (3)); + + A->set_position (DCPTime::from_seconds (0)); + B->set_position (DCPTime::from_seconds (10)); + C->set_position (DCPTime::from_seconds (20)); + + shared_ptr player = film->make_player (); + + list > o = player->overlaps (DCPTime::from_seconds (0), DCPTime::from_seconds (5)); + BOOST_CHECK_EQUAL (o.size(), 1); + BOOST_CHECK_EQUAL (o.front()->content, A); + + o = player->overlaps (DCPTime::from_seconds (5), DCPTime::from_seconds (8)); + BOOST_CHECK_EQUAL (o.size(), 0); + + o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (12)); + BOOST_CHECK_EQUAL (o.size(), 1); + BOOST_CHECK_EQUAL (o.front()->content, B); + + o = player->overlaps (DCPTime::from_seconds (2), DCPTime::from_seconds (12)); + BOOST_CHECK_EQUAL (o.size(), 2); + BOOST_CHECK_EQUAL (o.front()->content, A); + BOOST_CHECK_EQUAL (o.back()->content, B); + + o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (11)); + BOOST_CHECK_EQUAL (o.size(), 1); + BOOST_CHECK_EQUAL (o.front()->content, B); +} + +/** Check that the Player correctly generates silence when used with a silent FFmpegContent */ +BOOST_AUTO_TEST_CASE (player_silence_padding_test) +{ + shared_ptr film = new_test_film ("player_silence_padding_test"); + film->set_name ("player_silence_padding_test"); + shared_ptr c (new FFmpegContent (film, "test/data/test.mp4")); + film->set_container (Ratio::from_id ("185")); + film->set_audio_channels (6); + + film->examine_and_add_content (c); + wait_for_jobs (); + + shared_ptr player = film->make_player (); + shared_ptr test = player->get_audio (DCPTime (0), DCPTime::from_seconds (1), true); + BOOST_CHECK_EQUAL (test->frames(), 48000); + BOOST_CHECK_EQUAL (test->channels(), film->audio_channels ()); + + for (int i = 0; i < test->frames(); ++i) { + for (int c = 0; c < test->channels(); ++c) { + BOOST_CHECK_EQUAL (test->data()[c][i], 0); + } + } +} + diff --git a/test/wscript b/test/wscript index ec61915a6..c825506fb 100644 --- a/test/wscript +++ b/test/wscript @@ -19,6 +19,7 @@ def build(bld): audio_analysis_test.cc audio_buffers_test.cc audio_delay_test.cc + audio_decoder_test.cc audio_mapping_test.cc black_fill_test.cc client_server_test.cc @@ -35,7 +36,7 @@ def build(bld): image_test.cc job_test.cc make_black_test.cc - player_silence_padding_test.cc + player_test.cc pixel_formats_test.cc ratio_test.cc repeat_frame_test.cc -- 2.30.2