Don't cache sample format, fix up various things.
authorCarl Hetherington <cth@carlh.net>
Sat, 20 Oct 2012 16:03:32 +0000 (17:03 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 20 Oct 2012 19:05:26 +0000 (20:05 +0100)
src/lib/decoder.cc
src/lib/decoder.h
src/lib/ffmpeg_decoder.h
src/lib/film_state.cc
src/lib/film_state.h
src/lib/j2k_wav_encoder.cc
src/lib/util.cc
src/lib/util.h

index ec046fcafe5d537cf329df098178da585f4856c0..16d815a077e0b85328ad419eafff90abce516cc8 100644 (file)
@@ -90,7 +90,7 @@ Decoder::~Decoder ()
 void
 Decoder::process_begin ()
 {
-       _delay_in_bytes = _fs->audio_delay() * _fs->audio_sample_rate() * _fs->audio_channels() * _fs->bytes_per_sample() / 1000;
+       _delay_in_bytes = _fs->audio_delay() * _fs->audio_sample_rate() * _fs->audio_channels() * bytes_per_audio_sample() / 1000;
        delete _delay_line;
        _delay_line = new DelayLine (_delay_in_bytes);
 
@@ -104,7 +104,7 @@ Decoder::process_end ()
        if (_delay_in_bytes < 0) {
                uint8_t remainder[-_delay_in_bytes];
                _delay_line->get_remaining (remainder);
-               _audio_frames_processed += _delay_in_bytes / (_fs->audio_channels() * _fs->bytes_per_sample());
+               _audio_frames_processed += _delay_in_bytes / (_fs->audio_channels() * bytes_per_audio_sample());
                emit_audio (remainder, _delay_in_bytes);
        }
 
@@ -122,7 +122,7 @@ Decoder::process_end ()
                s << "Adding " << audio_short_by_frames << " frames of silence to the end.";
                _log->log (s.str ());
 
-               int64_t bytes = audio_short_by_frames * _fs->audio_channels() * _fs->bytes_per_sample();
+               int64_t bytes = audio_short_by_frames * _fs->audio_channels() * bytes_per_audio_sample();
                
                int64_t const silence_size = 64 * 1024;
                uint8_t silence[silence_size];
@@ -193,13 +193,13 @@ Decoder::emit_audio (uint8_t* data, int size)
        /* Deinterleave and convert to float */
 
        float* samples[_fs->audio_channels()];
-       int const total_samples = size / _fs->bytes_per_sample();
+       int const total_samples = size / bytes_per_audio_sample();
        int const frames = total_samples / _fs->audio_channels();
        for (int i = 0; i < _fs->audio_channels(); ++i) {
                samples[i] = new float[frames];
        }
 
-       switch (_fs->audio_sample_format()) {
+       switch (audio_sample_format()) {
        case AV_SAMPLE_FMT_S16:
        {
                uint8_t* p = data;
@@ -213,8 +213,6 @@ Decoder::emit_audio (uint8_t* data, int size)
                        /* float sample */
                        samples[channel][sample] = float(os) / 0x8000;
 
-                       cout << samples[channel][sample] << " from s16\n";
-                       
                        ++channel;
                        if (channel == _fs->audio_channels()) {
                                channel = 0;
@@ -230,11 +228,8 @@ Decoder::emit_audio (uint8_t* data, int size)
        {
                float* p = reinterpret_cast<float*> (data);
                for (int i = 0; i < _fs->audio_channels(); ++i) {
-                       for (int j = 0; j < frames; ++j) {
-                               samples[i][j] = *p++;
-                               cout << samples[i][j] << " from float.\n";
-                               ++p;
-                       }
+                       memcpy (samples[i], p, frames * sizeof(float));
+                       p += frames;
                }
        }
        break;
@@ -460,3 +455,10 @@ Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
                _timed_subtitle->subtitle()->set_position (Position (p.x - _fs->crop().left, p.y - _fs->crop().top));
        }
 }
+
+
+int
+Decoder::bytes_per_audio_sample () const
+{
+       return av_get_bytes_per_sample (audio_sample_format ());
+}
index 88dd90f0c43bee70737ec31765377ad5a8e5e1ae..14e602aceac3fa51c8a16b0642a31f74093e7ffc 100644 (file)
@@ -138,6 +138,7 @@ protected:
 private:
        void setup_video_filters ();
        void emit_audio (uint8_t* data, int size);
+       int bytes_per_audio_sample () const;
        
        /** last video frame to be processed */
        int _video_frame;
index 9abc7f49ad743760ef9843b19a9b918a31e8f6d3..0d256b37e4b0152237c605744589f0f302ae95e2 100644 (file)
@@ -65,6 +65,7 @@ public:
        AVSampleFormat audio_sample_format () const;
        int64_t audio_channel_layout () const;
        bool has_subtitles () const;
+       int bytes_per_audio_sample () const;
 
        std::vector<Stream> audio_streams () const;
        std::vector<Stream> subtitle_streams () const;
index f2d83e4e27839a3d0ef824673b1d569950358afb..46359677c1e0e3091fcaab4d3c9cf1f677407e13 100644 (file)
@@ -115,7 +115,6 @@ FilmState::write_metadata () const
        f << "length " << _length << "\n";
        f << "audio_channels " << _audio_channels << "\n";
        f << "audio_sample_rate " << _audio_sample_rate << "\n";
-       f << "audio_sample_format " << audio_sample_format_to_string (_audio_sample_format) << "\n";
        f << "content_digest " << _content_digest << "\n";
        f << "has_subtitles " << _has_subtitles << "\n";
 
@@ -224,8 +223,6 @@ FilmState::read_metadata ()
                        _audio_channels = atoi (v.c_str ());
                } else if (k == "audio_sample_rate") {
                        _audio_sample_rate = atoi (v.c_str ());
-               } else if (k == "audio_sample_format") {
-                       _audio_sample_format = audio_sample_format_from_string (v);
                } else if (k == "content_digest") {
                        _content_digest = v;
                } else if (k == "has_subtitles") {
@@ -354,13 +351,6 @@ FilmState::content_type () const
        return VIDEO;
 }
 
-/** @return Number of bytes per sample of a single channel */
-int
-FilmState::bytes_per_sample () const
-{
-       return av_get_bytes_per_sample (_audio_sample_format);
-}
-
 int
 FilmState::target_sample_rate () const
 {
@@ -543,7 +533,6 @@ FilmState::set_content (string c)
        set_frames_per_second (d->frames_per_second ());
        set_audio_channels (d->audio_channels ());
        set_audio_sample_rate (d->audio_sample_rate ());
-       set_audio_sample_format (d->audio_sample_format ());
        set_has_subtitles (d->has_subtitles ());
        set_audio_streams (d->audio_streams ());
        set_subtitle_streams (d->subtitle_streams ());
@@ -795,13 +784,6 @@ FilmState::set_audio_sample_rate (int r)
        signal_changed (AUDIO_SAMPLE_RATE);
 }
 
-void
-FilmState::set_audio_sample_format (AVSampleFormat f)
-{
-       _audio_sample_format = f;
-       _dirty = true;
-}
-
 void
 FilmState::set_content_digest (string d)
 {
index 2a00c844a01ab08fe52eebdb2b9a797331082c3b..40908693f26c40dd7edfbd049adb4462549de67d 100644 (file)
@@ -72,7 +72,6 @@ public:
                , _length (0)
                , _audio_channels (0)
                , _audio_sample_rate (0)
-               , _audio_sample_format (AV_SAMPLE_FMT_NONE)
                , _has_subtitles (false)
                , _frames_per_second (0)
                , _dirty (false)
@@ -90,7 +89,6 @@ public:
        std::string thumb_base (int) const;
        int thumb_frame (int) const;
 
-       int bytes_per_sample () const;
        int target_sample_rate () const;
        
        void write_metadata () const;
@@ -272,10 +270,6 @@ public:
                return _audio_sample_rate;
        }
        
-       AVSampleFormat audio_sample_format () const {
-               return _audio_sample_format;
-       }
-       
        std::string content_digest () const {
                return _content_digest;
        }
@@ -335,7 +329,6 @@ public:
        void set_length (int);
        void set_audio_channels (int);
        void set_audio_sample_rate (int);
-       void set_audio_sample_format (AVSampleFormat);
        void set_content_digest (std::string);
        void set_has_subtitles (bool);
        void set_audio_streams (std::vector<Stream>);
@@ -422,8 +415,6 @@ private:
        int _audio_channels;
        /** Sample rate of the source audio, in Hz */
        int _audio_sample_rate;
-       /** Format of the audio samples */
-       AVSampleFormat _audio_sample_format;
        /** MD5 digest of our content file */
        std::string _content_digest;
        /** true if the source has subtitles */
index 58f8a101f7440c0be6312846114c9ebeed474011..7697a5e783dbcfb2159b89b7636759c0dd7ccf3c 100644 (file)
@@ -351,16 +351,16 @@ J2KWAVEncoder::process_audio (float** data, int frames)
        if (_swr_context) {
 
                /* Compute the resampled frames count and add 32 for luck */
-               int const resampled_frames = ceil (frames * _fs->target_sample_rate() / _fs->audio_sample_rate()) + 32;
+               int const max_resampled_frames = ceil (frames * _fs->target_sample_rate() / _fs->audio_sample_rate()) + 32;
 
                /* Make a buffer to put the result in */
                for (int i = 0; i < _fs->audio_channels(); ++i) {
-                       resampled[i] = new float[resampled_frames];
+                       resampled[i] = new float[max_resampled_frames];
                }
 
                /* Resample audio */
-               int out_frames = swr_convert (_swr_context, (uint8_t **) resampled, resampled_frames, (uint8_t const **) data, frames);
-               if (out_frames < 0) {
+               int const resampled_frames = swr_convert (_swr_context, (uint8_t **) resampled, max_resampled_frames, (uint8_t const **) data, frames);
+               if (resampled_frames < 0) {
                        throw EncodeError ("could not run sample-rate converter");
                }
 
index 47a86da9e09907a98fd563d9c395d11f6fc890f4..3f5200ead1b2a2fdc80b3aac85d3f766fab71d51 100644 (file)
@@ -189,35 +189,6 @@ stacktrace (ostream& out, int levels)
 }
 #endif
 
-/** @param s Sample format.
- *  @return String representation.
- */
-string
-audio_sample_format_to_string (AVSampleFormat s)
-{
-       /* Our sample format handling is not exactly complete */
-       
-       switch (s) {
-       case AV_SAMPLE_FMT_S16:
-               return "S16";
-       default:
-               assert (false);
-       }
-}
-
-/** @param s String representation of a sample format, as returned from audio_sample_format_to_string().
- *  @return Sample format.
- */
-AVSampleFormat
-audio_sample_format_from_string (string s)
-{
-       if (s == "S16") {
-               return AV_SAMPLE_FMT_S16;
-       }
-
-       assert (false);
-}
-
 /** @return Version of vobcopy that is on the path (and hence that we will use) */
 static string
 vobcopy_version ()
index c2706a5948b2230d0ff12fa753e336246afd35ed..f2087b3f042476d4d42c286dfb834cf6b9c7d02d 100644 (file)
@@ -46,8 +46,6 @@ class Scaler;
 extern std::string seconds_to_hms (int);
 extern std::string seconds_to_approximate_hms (int);
 extern void stacktrace (std::ostream &, int);
-extern std::string audio_sample_format_to_string (AVSampleFormat);
-extern AVSampleFormat audio_sample_format_from_string (std::string);
 extern std::string dependency_version_summary ();
 extern double seconds (struct timeval);
 extern void dvdomatic_setup ();