2 Copyright (C) 2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "ffmpeg_transcoder.h"
25 #include "player_video.h"
28 #include "compose.hpp"
34 using std::runtime_error;
36 using boost::shared_ptr;
38 using boost::weak_ptr;
41 force_pixel_format (AVPixelFormat, AVPixelFormat out)
46 FFmpegTranscoder::FFmpegTranscoder (shared_ptr<const Film> film, weak_ptr<Job> job, boost::filesystem::path output, Format format)
47 : Transcoder (film, job)
53 _pixel_format = AV_PIX_FMT_YUV422P10;
54 _codec_name = "prores_ks";
59 FFmpegTranscoder::go ()
61 AVCodec* codec = avcodec_find_encoder_by_name (_codec_name.c_str());
63 throw runtime_error (String::compose ("could not find FFmpeg codec %1", _codec_name));
66 _codec_context = avcodec_alloc_context3 (codec);
67 if (!_codec_context) {
68 throw runtime_error ("could not allocate FFmpeg context");
71 avcodec_get_context_defaults3 (_codec_context, codec);
73 /* Variable quantisation */
74 _codec_context->global_quality = 0;
75 _codec_context->width = _film->frame_size().width;
76 _codec_context->height = _film->frame_size().height;
77 _codec_context->time_base = (AVRational) { 1, _film->video_frame_rate() };
78 _codec_context->pix_fmt = _pixel_format;
79 _codec_context->flags |= CODEC_FLAG_QSCALE | CODEC_FLAG_GLOBAL_HEADER;
81 boost::filesystem::path filename = _film->file(_film->isdcf_name(true) + ".mov");
82 avformat_alloc_output_context2 (&_format_context, 0, 0, _output.string().c_str());
83 if (!_format_context) {
84 throw runtime_error ("could not allocate FFmpeg format context");
87 _video_stream = avformat_new_stream (_format_context, codec);
89 throw runtime_error ("could not create FFmpeg output video stream");
92 /* Note: needs to increment with each stream */
93 _video_stream->id = 0;
94 _video_stream->codec = _codec_context;
96 AVDictionary* options = 0;
97 av_dict_set (&options, "profile", "3", 0);
98 av_dict_set (&options, "threads", "auto", 0);
100 if (avcodec_open2 (_codec_context, codec, &options) < 0) {
101 throw runtime_error ("could not open FFmpeg codec");
104 if (avio_open (&_format_context->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) {
105 throw runtime_error ("could not open FFmpeg output file");
108 if (avformat_write_header (_format_context, &options) < 0) {
109 throw runtime_error ("could not write header to FFmpeg output file");
113 shared_ptr<Job> job = _job.lock ();
114 DCPOMATIC_ASSERT (job);
115 job->sub (_("Encoding"));
118 while (!_player->pass ()) {}
122 av_init_packet (&packet);
127 avcodec_encode_video2 (_codec_context, &packet, 0, &got_packet);
132 packet.stream_index = 0;
133 av_interleaved_write_frame (_format_context, &packet);
134 av_packet_unref (&packet);
137 av_write_trailer (_format_context);
139 avcodec_close (_codec_context);
140 avio_close (_format_context->pb);
141 avformat_free_context (_format_context);
145 FFmpegTranscoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
147 shared_ptr<Image> image = video->image (
148 bind (&Log::dcp_log, _film->log().get(), _1, _2),
149 bind (&force_pixel_format, _1, _pixel_format),
154 AVFrame* frame = av_frame_alloc ();
156 for (int i = 0; i < 3; ++i) {
157 size_t const size = image->stride()[i] * image->size().height;
158 AVBufferRef* buffer = av_buffer_alloc (size);
159 /* XXX: inefficient */
160 memcpy (buffer->data, image->data()[i], size);
161 frame->buf[i] = av_buffer_ref (buffer);
162 frame->data[i] = buffer->data;
163 frame->linesize[i] = image->stride()[i];
164 av_buffer_unref (&buffer);
167 frame->width = image->size().width;
168 frame->height = image->size().height;
169 frame->format = _pixel_format;
170 frame->pts = time.seconds() / av_q2d (_video_stream->time_base);
173 av_init_packet (&packet);
178 if (avcodec_encode_video2 (_codec_context, &packet, frame, &got_packet) < 0) {
179 throw EncodeError ("FFmpeg video encode failed");
182 if (got_packet && packet.size) {
183 /* XXX: this should not be hard-wired */
184 packet.stream_index = 0;
185 av_interleaved_write_frame (_format_context, &packet);
186 av_packet_unref (&packet);
189 av_frame_free (&frame);
194 boost::mutex::scoped_lock lm (_mutex);
198 shared_ptr<Job> job = _job.lock ();
200 job->set_progress (float(time.get()) / _film->length().get());
205 FFmpegTranscoder::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
211 FFmpegTranscoder::subtitle (PlayerSubtitles subs, DCPTimePeriod period)
217 FFmpegTranscoder::current_rate () const
219 return _history.rate ();
223 FFmpegTranscoder::frames_done () const
225 boost::mutex::scoped_lock lm (_mutex);
226 return _last_time.frames_round (_film->video_frame_rate ());