summaryrefslogtreecommitdiff
path: root/src/lib/decoder.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/decoder.cc')
-rw-r--r--src/lib/decoder.cc105
1 files changed, 25 insertions, 80 deletions
diff --git a/src/lib/decoder.cc b/src/lib/decoder.cc
index c9d8f063a..2f66aff68 100644
--- a/src/lib/decoder.cc
+++ b/src/lib/decoder.cc
@@ -43,6 +43,7 @@ using std::min;
using std::pair;
using std::list;
using boost::shared_ptr;
+using boost::optional;
/** @param f Film.
* @param o Options.
@@ -73,31 +74,33 @@ Decoder::~Decoder ()
void
Decoder::process_begin ()
{
- _delay_in_frames = _film->audio_delay() * audio_sample_rate() / 1000;
- _delay_line = new DelayLine (audio_channels(), _delay_in_frames);
+ if (_audio_stream) {
+ _delay_in_frames = _film->audio_delay() * _audio_stream.get().sample_rate() / 1000;
+ _delay_line = new DelayLine (_audio_stream.get().channels(), _delay_in_frames);
+ }
}
/** Finish off a decode processing run */
void
Decoder::process_end ()
{
- if (_delay_in_frames < 0 && _opt->decode_audio && audio_channels()) {
- shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), -_delay_in_frames));
+ if (_delay_in_frames < 0 && _opt->decode_audio && _audio_stream) {
+ shared_ptr<AudioBuffers> b (new AudioBuffers (_audio_stream.get().channels(), -_delay_in_frames));
b->make_silent ();
emit_audio (b);
}
- if (_opt->decode_audio && audio_channels()) {
+ if (_opt->decode_audio && _audio_stream) {
/* Ensure that our video and audio emissions are the same length */
- int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()) - _audio_frame;
+ int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frame, _audio_stream.get().sample_rate(), frames_per_second()) - _audio_frame;
_film->log()->log (
String::compose (
"Decoder has emitted %1 video frames (which equals %2 audio frames) and %3 audio frames",
_video_frame,
- video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()),
+ video_frames_to_audio_frames (_video_frame, _audio_stream.get().sample_rate(), frames_per_second()),
_audio_frame
)
);
@@ -107,7 +110,7 @@ Decoder::process_end ()
_film->log()->log (String::compose ("Emitted %1 too many audio frames", -audio_short_by_frames));
/* We have emitted more audio than video. Emit enough black video frames so that we reverse this */
- int const black_video_frames = ceil (-audio_short_by_frames * frames_per_second() / audio_sample_rate());
+ int const black_video_frames = ceil (-audio_short_by_frames * frames_per_second() / _audio_stream.get().sample_rate());
_film->log()->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
@@ -118,12 +121,12 @@ Decoder::process_end ()
}
/* Now recompute our check value */
- audio_short_by_frames = video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()) - _audio_frame;
+ audio_short_by_frames = video_frames_to_audio_frames (_video_frame, _audio_stream.get().sample_rate(), frames_per_second()) - _audio_frame;
}
if (audio_short_by_frames > 0) {
_film->log()->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
- shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), audio_short_by_frames));
+ shared_ptr<AudioBuffers> b (new AudioBuffers (_audio_stream.get().channels(), audio_short_by_frames));
b->make_silent ();
emit_audio (b);
}
@@ -154,77 +157,13 @@ Decoder::go ()
* @param size Number of bytes of data.
*/
void
-Decoder::process_audio (uint8_t* data, int size)
+Decoder::process_audio (shared_ptr<AudioBuffers> audio)
{
- /* XXX: could this be removed? */
- if (size == 0) {
- return;
- }
-
- assert (_film->audio_channels());
- assert (bytes_per_audio_sample());
-
- /* Deinterleave and convert to float */
-
- assert ((size % (bytes_per_audio_sample() * audio_channels())) == 0);
-
- int const total_samples = size / bytes_per_audio_sample();
- int const frames = total_samples / _film->audio_channels();
- shared_ptr<AudioBuffers> audio (new AudioBuffers (audio_channels(), frames));
-
- switch (audio_sample_format()) {
- case AV_SAMPLE_FMT_S16:
- {
- int16_t* p = (int16_t *) data;
- int sample = 0;
- int channel = 0;
- for (int i = 0; i < total_samples; ++i) {
- audio->data(channel)[sample] = float(*p++) / (1 << 15);
-
- ++channel;
- if (channel == _film->audio_channels()) {
- channel = 0;
- ++sample;
- }
- }
- }
- break;
-
- case AV_SAMPLE_FMT_S32:
- {
- int32_t* p = (int32_t *) data;
- int sample = 0;
- int channel = 0;
- for (int i = 0; i < total_samples; ++i) {
- audio->data(channel)[sample] = float(*p++) / (1 << 31);
-
- ++channel;
- if (channel == _film->audio_channels()) {
- channel = 0;
- ++sample;
- }
- }
- }
-
- case AV_SAMPLE_FMT_FLTP:
- {
- float* p = reinterpret_cast<float*> (data);
- for (int i = 0; i < _film->audio_channels(); ++i) {
- memcpy (audio->data(i), p, frames * sizeof(float));
- p += frames;
- }
- }
- break;
-
- default:
- assert (false);
- }
-
/* Maybe apply gain */
if (_film->audio_gain() != 0) {
float const linear_gain = pow (10, _film->audio_gain() / 20);
- for (int i = 0; i < _film->audio_channels(); ++i) {
- for (int j = 0; j < frames; ++j) {
+ for (int i = 0; i < audio->channels(); ++i) {
+ for (int j = 0; j < audio->frames(); ++j) {
audio->data(i)[j] *= linear_gain;
}
}
@@ -308,8 +247,14 @@ Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
}
}
-int
-Decoder::bytes_per_audio_sample () const
+void
+Decoder::set_audio_stream (optional<AudioStream> s)
+{
+ _audio_stream = s;
+}
+
+void
+Decoder::set_subtitle_stream (optional<SubtitleStream> s)
{
- return av_get_bytes_per_sample (audio_sample_format ());
+ _subtitle_stream = s;
}