- 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) {
- audio->data(i)[j] *= linear_gain;
- }
- }
- }
-
- _delay_line->feed (audio);
-
- if (_opt->decode_range) {
- /* Decode range in audio frames */
- pair<int64_t, int64_t> required_range (
- video_frames_to_audio_frames (_opt->decode_range.get().first),
- video_frames_to_audio_frames (_opt->decode_range.get().second)
- );
-
- /* Range of this block of data */
- pair<int64_t, int64_t> this_range (
- _audio_frames_in,
- _audio_frames_in + audio->frames()
- );
-
- /* Trim start */
- if (required_range.first >= this_range.first && required_range.first < this_range.second) {
- int64_t const shift = this_range.first - required_range.first;
- audio->move (shift, 0, audio->frames() - shift);
- audio->set_frames (audio->frames() - shift);
- }
-
- /* Trim end */
- if (required_range.second >= this_range.first && required_range.second < this_range.second) {
- audio->set_frames (this_range.first - required_range.second);
- }
- }
-
- if (audio->frames()) {
- emit_audio (audio);
- }
-}
-
-void
-Decoder::emit_audio (shared_ptr<AudioBuffers> audio)
-{
- Audio (audio);
- _audio_frames_out += audio->frames ();
-}
-
-/** Called by subclasses to tell the world that some video data is ready.
- * We do some post-processing / filtering then emit it for listeners.
- * @param frame to decode; caller manages memory.
- */
-void
-Decoder::process_video (AVFrame* frame)
-{
- if (_minimal) {
- ++_video_frames_in;
- return;
- }
-
- if (_opt->decode_video_skip != 0 && (_video_frames_in % _opt->decode_video_skip) != 0) {
- ++_video_frames_in;
- return;
- }
-
- if (_opt->decode_range) {
- pair<SourceFrame, SourceFrame> r = _opt->decode_range.get();
- if (_video_frames_in < r.first || _video_frames_in >= r.second) {
- ++_video_frames_in;
- return;
- }
- }
-
- shared_ptr<FilterGraph> graph;
-
- list<shared_ptr<FilterGraph> >::iterator i = _filter_graphs.begin();
- while (i != _filter_graphs.end() && !(*i)->can_process (Size (frame->width, frame->height), (AVPixelFormat) frame->format)) {
- ++i;
- }
-
- if (i == _filter_graphs.end ()) {
- graph.reset (new FilterGraph (_film, this, _opt->apply_crop, Size (frame->width, frame->height), (AVPixelFormat) frame->format));
- _filter_graphs.push_back (graph);
- _film->log()->log (String::compose ("New graph for %1x%2, pixel format %3", frame->width, frame->height, frame->format));
- } else {
- graph = *i;
- }
-
- list<shared_ptr<Image> > images = graph->process (frame);
-
- for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
- shared_ptr<Subtitle> sub;
- if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frames_in()) / _film->frames_per_second())) {
- sub = _timed_subtitle->subtitle ();
- }
-
- emit_video (*i, sub);
- }
-
- ++_video_frames_in;
-}
-
-void
-Decoder::repeat_last_video ()
-{
- if (!_last_image) {
- _last_image.reset (new CompactImage (pixel_format(), native_size()));
- _last_image->make_black ();
- }
-
- emit_video (_last_image, _last_subtitle);
-}
-
-void
-Decoder::emit_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
-{
- TIMING ("Decoder emits %1", _video_frames_out);
- Video (image, _video_frames_in, sub);
- ++_video_frames_out;
- _last_image = image;
- _last_subtitle = sub;
-}
-
-void
-Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
-{
- _timed_subtitle = s;
-
- if (_timed_subtitle && _opt->apply_crop) {
- Position const p = _timed_subtitle->subtitle()->position ();
- _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 ());
-}
-
-int64_t
-Decoder::video_frames_to_audio_frames (SourceFrame v) const
-{
- return ((int64_t) v * audio_sample_rate() / frames_per_second());