2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/decoder.cc
21 * @brief Parent class for decoders of content.
26 #include <boost/lexical_cast.hpp>
31 #include "exceptions.h"
36 #include "delay_line.h"
38 #include "filter_graph.h"
41 using std::stringstream;
45 using boost::shared_ptr;
49 * @param j Job that we are running within, or 0
50 * @param minimal true to do the bare minimum of work; just run through the content. Useful for acquiring
51 * accurate frame counts as quickly as possible. This generates no video or audio output.
53 Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal)
58 , _video_frames_in (0)
59 , _video_frames_out (0)
60 , _audio_frames_in (0)
61 , _audio_frames_out (0)
63 , _delay_in_frames (0)
73 /** Start off a decode processing run. This should only be called once on
74 * a given Decoder object.
77 Decoder::process_begin ()
79 _delay_in_frames = _film->audio_delay() * audio_sample_rate() / 1000;
80 _delay_line = new DelayLine (audio_channels(), _delay_in_frames);
83 /** Finish off a decode processing run */
85 Decoder::process_end ()
87 if (_delay_in_frames < 0 && _opt->decode_audio && audio_channels()) {
88 shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), -_delay_in_frames));
93 if (_opt->decode_audio && audio_channels()) {
95 /* Ensure that our video and audio emissions are the same length */
97 int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames_out) - _audio_frames_out;
100 String::compose ("Decoder has emitted %1 video frames (which equals %2 audio frames) and %3 audio frames",
102 video_frames_to_audio_frames (_video_frames_out),
106 if (audio_short_by_frames < 0) {
108 _film->log()->log (String::compose ("Emitted %1 too many audio frames", -audio_short_by_frames));
110 /* We have emitted more audio than video. Emit enough black video frames so that we reverse this */
111 int const black_video_frames = ceil (-audio_short_by_frames * frames_per_second() / audio_sample_rate());
113 _film->log()->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
115 shared_ptr<Image> black (new CompactImage (pixel_format(), native_size()));
116 black->make_black ();
117 for (int i = 0; i < black_video_frames; ++i) {
118 emit_video (black, shared_ptr<Subtitle> ());
121 /* Now recompute our check value */
122 audio_short_by_frames = video_frames_to_audio_frames (_video_frames_out) - _audio_frames_out;
125 if (audio_short_by_frames > 0) {
126 _film->log()->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
127 shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), audio_short_by_frames));
134 /** Start decoding */
140 if (_job && !_film->dcp_length()) {
141 _job->set_progress_unknown ();
144 while (pass () == false) {
145 if (_job && _film->dcp_length()) {
146 _job->set_progress (float (_video_frames_out) / _film->dcp_length().get());
153 /** Called by subclasses to tell the world that some audio data is ready
154 * @param data Audio data, in Film::audio_sample_format.
155 * @param size Number of bytes of data.
158 Decoder::process_audio (uint8_t* data, int size)
164 assert (_film->audio_channels());
165 assert (bytes_per_audio_sample());
167 /* Deinterleave and convert to float */
169 assert ((size % (bytes_per_audio_sample() * audio_channels())) == 0);
171 int const total_samples = size / bytes_per_audio_sample();
172 int const frames = total_samples / _film->audio_channels();
173 shared_ptr<AudioBuffers> audio (new AudioBuffers (audio_channels(), frames));
175 switch (audio_sample_format()) {
176 case AV_SAMPLE_FMT_S16:
178 int16_t* p = (int16_t *) data;
181 for (int i = 0; i < total_samples; ++i) {
182 audio->data(channel)[sample] = float(*p++) / (1 << 15);
185 if (channel == _film->audio_channels()) {
193 case AV_SAMPLE_FMT_S32:
195 int32_t* p = (int32_t *) data;
198 for (int i = 0; i < total_samples; ++i) {
199 audio->data(channel)[sample] = float(*p++) / (1 << 31);
202 if (channel == _film->audio_channels()) {
209 case AV_SAMPLE_FMT_FLTP:
211 float* p = reinterpret_cast<float*> (data);
212 for (int i = 0; i < _film->audio_channels(); ++i) {
213 memcpy (audio->data(i), p, frames * sizeof(float));
223 /* Maybe apply gain */
224 if (_film->audio_gain() != 0) {
225 float const linear_gain = pow (10, _film->audio_gain() / 20);
226 for (int i = 0; i < _film->audio_channels(); ++i) {
227 for (int j = 0; j < frames; ++j) {
228 audio->data(i)[j] *= linear_gain;
233 _delay_line->feed (audio);
235 /* Decode range in audio frames */
236 pair<int64_t, int64_t> required_range (
237 video_frames_to_audio_frames (_film->dcp_trim_start()),
238 video_frames_to_audio_frames (_film->dcp_trim_start() + _film->dcp_length().get())
241 /* Range of this block of data */
242 pair<int64_t, int64_t> this_range (
244 _audio_frames_in + audio->frames()
248 if (required_range.first >= this_range.first && required_range.first < this_range.second) {
249 int64_t const shift = this_range.first - required_range.first;
250 audio->move (shift, 0, audio->frames() - shift);
251 audio->set_frames (audio->frames() - shift);
255 if (required_range.second >= this_range.first && required_range.second < this_range.second) {
256 audio->set_frames (this_range.first - required_range.second);
259 if (audio->frames()) {
265 Decoder::emit_audio (shared_ptr<AudioBuffers> audio)
268 _audio_frames_out += audio->frames ();
271 /** Called by subclasses to tell the world that some video data is ready.
272 * We do some post-processing / filtering then emit it for listeners.
273 * @param frame to decode; caller manages memory.
276 Decoder::process_video (AVFrame* frame)
278 assert (_film->length());
285 /* Use Film::length here as our one may be wrong */
287 if (_opt->decode_video_skip != 0 && (_video_frames_in % _opt->decode_video_skip) != 0) {
292 if (_video_frames_in < _film->dcp_trim_start() || _video_frames_in > (_film->dcp_trim_start() + _film->length().get())) {
297 shared_ptr<FilterGraph> graph;
299 list<shared_ptr<FilterGraph> >::iterator i = _filter_graphs.begin();
300 while (i != _filter_graphs.end() && !(*i)->can_process (Size (frame->width, frame->height), (AVPixelFormat) frame->format)) {
304 if (i == _filter_graphs.end ()) {
305 graph.reset (new FilterGraph (_film, this, _opt->apply_crop, Size (frame->width, frame->height), (AVPixelFormat) frame->format));
306 _filter_graphs.push_back (graph);
307 _film->log()->log (String::compose ("New graph for %1x%2, pixel format %3", frame->width, frame->height, frame->format));
312 list<shared_ptr<Image> > images = graph->process (frame);
314 for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
315 shared_ptr<Subtitle> sub;
316 if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frames_in()) / _film->frames_per_second())) {
317 sub = _timed_subtitle->subtitle ();
320 emit_video (*i, sub);
325 Decoder::repeat_last_video ()
328 _last_image.reset (new CompactImage (pixel_format(), native_size()));
329 _last_image->make_black ();
332 emit_video (_last_image, _last_subtitle);
336 Decoder::emit_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
338 TIMING ("Decoder emits %1", _video_frames_out);
339 Video (image, _video_frames_out, sub);
342 _last_subtitle = sub;
346 Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
350 if (_timed_subtitle && _opt->apply_crop) {
351 Position const p = _timed_subtitle->subtitle()->position ();
352 _timed_subtitle->subtitle()->set_position (Position (p.x - _film->crop().left, p.y - _film->crop().top));
358 Decoder::bytes_per_audio_sample () const
360 return av_get_bytes_per_sample (audio_sample_format ());
364 Decoder::video_frames_to_audio_frames (SourceFrame v) const
366 return ((int64_t) v * audio_sample_rate() / frames_per_second());