#include "film.h"
#include "format.h"
#include "job.h"
-#include "film_state.h"
#include "options.h"
#include "exceptions.h"
#include "image.h"
#include "ffmpeg_compatibility.h"
#include "subtitle.h"
-using namespace std;
-using namespace boost;
+using std::string;
+using std::stringstream;
+using std::min;
+using boost::shared_ptr;
-/** @param s FilmState of the Film.
+/** @param f Film.
* @param o Options.
* @param j Job that we are running within, or 0
- * @param l Log to use.
* @param minimal true to do the bare minimum of work; just run through the content. Useful for acquiring
* accurate frame counts as quickly as possible. This generates no video or audio output.
* @param ignore_length Ignore the content's claimed length when computing progress.
*/
-Decoder::Decoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
- : _fs (s)
+Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
+ : _film (f)
, _opt (o)
, _job (j)
- , _log (l)
, _minimal (minimal)
, _ignore_length (ignore_length)
, _video_frame (0)
, _delay_in_bytes (0)
, _audio_frames_processed (0)
{
- if (_opt->decode_video_frequency != 0 && _fs->length() == 0) {
- throw DecodeError ("cannot do a partial decode if length == 0");
+ if (_opt->decode_video_frequency != 0 && !_film->length()) {
+ throw DecodeError ("cannot do a partial decode if length is unknown");
}
}
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 = _film->audio_delay() * audio_sample_rate() * audio_channels() * bytes_per_audio_sample() / 1000;
delete _delay_line;
_delay_line = new DelayLine (_delay_in_bytes);
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());
- emit_audio (remainder, _delay_in_bytes);
+ _audio_frames_processed += _delay_in_bytes / (audio_channels() * bytes_per_audio_sample());
+ emit_audio (remainder, -_delay_in_bytes);
}
/* If we cut the decode off, the audio may be short; push some silence
in to get it to the right length.
*/
- int64_t const audio_short_by_frames =
- ((int64_t) _fs->dcp_length() * _fs->target_sample_rate() / _fs->frames_per_second())
- - _audio_frames_processed;
+ int64_t const video_length_in_audio_frames = ((int64_t) last_video_frame() * audio_sample_rate() / frames_per_second());
+ int64_t const audio_short_by_frames = video_length_in_audio_frames - _audio_frames_processed;
- if (audio_short_by_frames >= 0) {
+ _film->log()->log (
+ String::compose ("DCP length is %1 (%2 audio frames); %3 frames of audio processed.",
+ last_video_frame(),
+ video_length_in_audio_frames,
+ _audio_frames_processed)
+ );
+
+ if (audio_short_by_frames >= 0 && _opt->decode_audio) {
+
+ _film->log()->log (String::compose ("DCP length is %1; %2 frames of audio processed.", last_video_frame(), _audio_frames_processed));
+ _film->log()->log (String::compose ("Adding %1 frames of silence to the end.", audio_short_by_frames));
- stringstream s;
- s << "Adding " << audio_short_by_frames << " frames of silence to the end.";
- _log->log (s.str ());
+ /* XXX: this is slightly questionable; does memset () give silence with all
+ sample formats?
+ */
- int64_t bytes = audio_short_by_frames * _fs->audio_channels() * _fs->bytes_per_sample();
+ int64_t bytes = audio_short_by_frames * _film->audio_channels() * bytes_per_audio_sample();
- int64_t const silence_size = 64 * 1024;
+ int64_t const silence_size = 16 * 1024 * _film->audio_channels() * bytes_per_audio_sample();
uint8_t silence[silence_size];
memset (silence, 0, silence_size);
{
process_begin ();
- if (_job && _ignore_length) {
+ if (_job && !_film->dcp_length()) {
_job->set_progress_unknown ();
}
while (pass () == false) {
- if (_job && !_ignore_length) {
- _job->set_progress (float (_video_frame) / _fs->dcp_length());
+ if (_job && _film->dcp_length()) {
+ _job->set_progress (float (_video_frame) / _film->dcp_length().get());
}
}
_have_setup_video_filters = true;
}
- if (_video_frame >= _fs->dcp_length()) {
+ if (!_ignore_length && _video_frame >= _film->dcp_length()) {
return true;
}
}
/** Called by subclasses to tell the world that some audio data is ready
- * @param data Audio data, in FilmState::audio_sample_format.
+ * @param data Audio data, in Film::audio_sample_format.
* @param size Number of bytes of data.
*/
void
{
/* Deinterleave and convert to float */
- float* samples[_fs->audio_channels()];
- int const total_samples = size / _fs->bytes_per_sample();
- int const frames = total_samples / _fs->audio_channels();
- for (int i = 0; i < _fs->audio_channels(); ++i) {
- samples[i] = new float[frames];
- }
+ 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 (_fs->audio_sample_format()) {
+ switch (audio_sample_format()) {
case AV_SAMPLE_FMT_S16:
{
- uint8_t* p = data;
+ int16_t* p = (int16_t *) data;
int sample = 0;
int channel = 0;
for (int i = 0; i < total_samples; ++i) {
- /* unsigned sample */
- int const ou = p[0] | (p[1] << 8);
- /* signed sample */
- int const os = ou >= 0x8000 ? (- 0x10000 + ou) : ou;
- /* float sample */
- samples[channel][sample] = float(os) / 0x8000;
-
- cout << samples[channel][sample] << " from s16\n";
-
+ audio->data(channel)[sample] = float(*p++) / (1 << 15);
+
++channel;
- if (channel == _fs->audio_channels()) {
+ if (channel == _film->audio_channels()) {
channel = 0;
++sample;
}
-
- p += 2;
}
}
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 < _fs->audio_channels(); ++i) {
- for (int j = 0; j < frames; ++j) {
- samples[i][j] = *p++;
- cout << samples[i][j] << " from float.\n";
- ++p;
- }
+ for (int i = 0; i < _film->audio_channels(); ++i) {
+ memcpy (audio->data(i), p, frames * sizeof(float));
+ p += frames;
}
}
break;
}
/* Maybe apply gain */
- if (_fs->audio_gain() != 0) {
- float const linear_gain = pow (10, _fs->audio_gain() / 20);
- for (int i = 0; i < _fs->audio_channels(); ++i) {
+ 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) {
- samples[i][j] *= linear_gain;
+ audio->data(i)[j] *= linear_gain;
}
}
}
/* Update the number of audio frames we've pushed to the encoder */
- _audio_frames_processed += frames;
-
- Audio (samples, frames);
+ _audio_frames_processed += audio->frames ();
- for (int i = 0; i < _fs->audio_channels(); ++i) {
- delete[] samples[i];
- }
+ Audio (audio);
}
/** Called by subclasses to tell the world that some video data is ready.
return;
}
- /* Use FilmState::length here as our one may be wrong */
+ /* Use Film::length here as our one may be wrong */
int gap = 0;
if (_opt->decode_video_frequency != 0) {
- gap = _fs->length() / _opt->decode_video_frequency;
+ gap = _film->length().get() / _opt->decode_video_frequency;
}
if (_opt->decode_video_frequency != 0 && gap != 0 && (_video_frame % gap) != 0) {
}
shared_ptr<Subtitle> sub;
- if (_timed_subtitle && _timed_subtitle->displayed_at (double (last_video_frame()) / rint (_fs->frames_per_second()))) {
+ if (_timed_subtitle && _timed_subtitle->displayed_at (double (last_video_frame()) / _film->frames_per_second())) {
sub = _timed_subtitle->subtitle ();
}
Size size_after_crop;
if (_opt->apply_crop) {
- size_after_crop = _fs->cropped_size (native_size ());
- fs << crop_string (Position (_fs->crop().left, _fs->crop().top), size_after_crop);
+ size_after_crop = _film->cropped_size (native_size ());
+ fs << crop_string (Position (_film->crop().left, _film->crop().top), size_after_crop);
} else {
size_after_crop = native_size ();
fs << crop_string (Position (0, 0), size_after_crop);
}
- string filters = Filter::ffmpeg_strings (_fs->filters()).first;
+ string filters = Filter::ffmpeg_strings (_film->filters()).first;
if (!filters.empty ()) {
filters += ",";
}
inputs->pad_idx = 0;
inputs->next = 0;
- _log->log ("Using filter chain `" + filters + "'");
+ _film->log()->log ("Using filter chain `" + filters + "'");
#if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
{
_timed_subtitle = s;
- if (_opt->apply_crop) {
+ if (_timed_subtitle && _opt->apply_crop) {
Position const p = _timed_subtitle->subtitle()->position ();
- _timed_subtitle->subtitle()->set_position (Position (p.x - _fs->crop().left, p.y - _fs->crop().top));
+ _timed_subtitle->subtitle()->set_position (Position (p.x - _film->crop().left, p.y - _film->crop().top));
}
}
+
+
+int
+Decoder::bytes_per_audio_sample () const
+{
+ return av_get_bytes_per_sample (audio_sample_format ());
+}