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;
44 using boost::shared_ptr;
48 * @param j Job that we are running within, or 0
49 * @param minimal true to do the bare minimum of work; just run through the content. Useful for acquiring
50 * accurate frame counts as quickly as possible. This generates no video or audio output.
51 * @param ignore_length Ignore the content's claimed length when computing progress.
53 Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
58 , _ignore_length (ignore_length)
59 , _video_frame_index (0)
61 , _delay_in_frames (0)
62 , _audio_frames_processed (0)
72 /** Start off a decode processing run */
74 Decoder::process_begin ()
76 _delay_in_frames = _film->audio_delay() * audio_sample_rate() / 1000;
78 _delay_line = new DelayLine (audio_channels(), _delay_in_frames);
80 _audio_frames_processed = 0;
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 we cut the decode off, the audio may be short; push some silence
94 in to get it to the right length.
97 int64_t const video_length_in_audio_frames = ((int64_t) video_frame_index() * audio_sample_rate() / frames_per_second());
98 int64_t const audio_short_by_frames = video_length_in_audio_frames - _audio_frames_processed;
101 String::compose ("Source length is %1 (%2 audio frames); %3 frames of audio processed.",
103 video_length_in_audio_frames,
104 _audio_frames_processed)
107 if (audio_short_by_frames > 0 && _opt->decode_audio && audio_channels()) {
109 _film->log()->log (String::compose ("Source length is %1; %2 frames of audio processed.", video_frame_index(), _audio_frames_processed));
110 _film->log()->log (String::compose ("Adding %1 frames of silence to the end.", audio_short_by_frames));
112 shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), audio_short_by_frames));
118 /** Start decoding */
124 if (_job && !_film->dcp_length()) {
125 _job->set_progress_unknown ();
128 while (pass () == false) {
129 if (_job && _film->dcp_length()) {
130 _job->set_progress (float (_video_frame_index) / _film->dcp_length().get());
137 /** Run one pass. This may or may not generate any actual video / audio data;
138 * some decoders may require several passes to generate a single frame.
139 * @return true if we have finished processing all data; otherwise false.
144 if (!_ignore_length && _video_frame_index >= _film->dcp_length()) {
151 /** Called by subclasses to tell the world that some audio data is ready
152 * @param data Audio data, in Film::audio_sample_format.
153 * @param size Number of bytes of data.
156 Decoder::process_audio (uint8_t* data, int size)
162 assert (_film->audio_channels());
163 assert (bytes_per_audio_sample());
165 /* Deinterleave and convert to float */
167 assert ((size % (bytes_per_audio_sample() * audio_channels())) == 0);
169 int const total_samples = size / bytes_per_audio_sample();
170 int const frames = total_samples / _film->audio_channels();
171 shared_ptr<AudioBuffers> audio (new AudioBuffers (audio_channels(), frames));
173 switch (audio_sample_format()) {
174 case AV_SAMPLE_FMT_S16:
176 int16_t* p = (int16_t *) data;
179 for (int i = 0; i < total_samples; ++i) {
180 audio->data(channel)[sample] = float(*p++) / (1 << 15);
183 if (channel == _film->audio_channels()) {
191 case AV_SAMPLE_FMT_S32:
193 int32_t* p = (int32_t *) data;
196 for (int i = 0; i < total_samples; ++i) {
197 audio->data(channel)[sample] = float(*p++) / (1 << 31);
200 if (channel == _film->audio_channels()) {
207 case AV_SAMPLE_FMT_FLTP:
209 float* p = reinterpret_cast<float*> (data);
210 for (int i = 0; i < _film->audio_channels(); ++i) {
211 memcpy (audio->data(i), p, frames * sizeof(float));
221 /* Maybe apply gain */
222 if (_film->audio_gain() != 0) {
223 float const linear_gain = pow (10, _film->audio_gain() / 20);
224 for (int i = 0; i < _film->audio_channels(); ++i) {
225 for (int j = 0; j < frames; ++j) {
226 audio->data(i)[j] *= linear_gain;
231 _delay_line->feed (audio);
236 Decoder::emit_audio (shared_ptr<AudioBuffers> audio)
239 _audio_frames_processed += audio->frames ();
242 /** Called by subclasses to tell the world that some video data is ready.
243 * We do some post-processing / filtering then emit it for listeners.
244 * @param frame to decode; caller manages memory.
247 Decoder::process_video (AVFrame* frame)
250 ++_video_frame_index;
254 /* Use Film::length here as our one may be wrong */
256 if (_opt->decode_video_skip != 0 && (_video_frame_index % _opt->decode_video_skip) != 0) {
257 ++_video_frame_index;
261 shared_ptr<FilterGraph> graph;
263 list<shared_ptr<FilterGraph> >::iterator i = _filter_graphs.begin();
264 while (i != _filter_graphs.end() && !(*i)->can_process (Size (frame->width, frame->height), (AVPixelFormat) frame->format)) {
268 if (i == _filter_graphs.end ()) {
269 graph.reset (new FilterGraph (_film, this, _opt->apply_crop, Size (frame->width, frame->height), (AVPixelFormat) frame->format));
270 _filter_graphs.push_back (graph);
271 _film->log()->log (String::compose ("New graph for %1x%2, pixel format %3", frame->width, frame->height, frame->format));
276 list<shared_ptr<Image> > images = graph->process (frame);
278 for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
279 if (_opt->black_after > 0 && _video_frame_index > _opt->black_after) {
283 shared_ptr<Subtitle> sub;
284 if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frame_index()) / _film->frames_per_second())) {
285 sub = _timed_subtitle->subtitle ();
288 TIMING ("Decoder emits %1", _video_frame_index);
289 Video (*i, _video_frame_index, sub);
290 ++_video_frame_index;
292 _last_subtitle = sub;
297 Decoder::repeat_last_video ()
300 _last_image.reset (new CompactImage (pixel_format(), native_size()));
301 _last_image->make_black ();
304 Video (_last_image, _video_frame_index, _last_subtitle);
305 ++_video_frame_index;
309 Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
313 if (_timed_subtitle && _opt->apply_crop) {
314 Position const p = _timed_subtitle->subtitle()->position ();
315 _timed_subtitle->subtitle()->set_position (Position (p.x - _film->crop().left, p.y - _film->crop().top));
321 Decoder::bytes_per_audio_sample () const
323 return av_get_bytes_per_sample (audio_sample_format ());