Basic video transcoding working.
[dcpomatic.git] / src / lib / ffmpeg_transcoder.cc
1 /*
2     Copyright (C) 2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "ffmpeg_transcoder.h"
22 #include "film.h"
23 #include "job.h"
24 #include "player.h"
25 #include "player_video.h"
26 #include "log.h"
27 #include "image.h"
28 #include "compose.hpp"
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::runtime_error;
34 using boost::shared_ptr;
35 using boost::bind;
36 using boost::weak_ptr;
37
38 static AVPixelFormat
39 force_pixel_format (AVPixelFormat, AVPixelFormat out)
40 {
41         return out;
42 }
43
44 FFmpegTranscoder::FFmpegTranscoder (shared_ptr<const Film> film, weak_ptr<Job> job)
45         : Transcoder (film, job)
46         , _pixel_format (AV_PIX_FMT_YUV422P10)
47 {
48
49 }
50
51 void
52 FFmpegTranscoder::go ()
53 {
54         string const codec_name = "prores_ks";
55         AVCodec* codec = avcodec_find_encoder_by_name (codec_name.c_str());
56         if (!codec) {
57                 throw runtime_error (String::compose ("could not find FFmpeg codec %1", codec_name));
58         }
59
60         _codec_context = avcodec_alloc_context3 (codec);
61         if (!_codec_context) {
62                 throw runtime_error ("could not allocate FFmpeg context");
63         }
64
65         avcodec_get_context_defaults3 (_codec_context, codec);
66
67         /* Variable quantisation */
68         _codec_context->global_quality = 0;
69         _codec_context->width = _film->frame_size().width;
70         _codec_context->height = _film->frame_size().height;
71         _codec_context->time_base = (AVRational) { 1, _film->video_frame_rate() };
72         _codec_context->pix_fmt = _pixel_format;
73         _codec_context->flags |= CODEC_FLAG_QSCALE | CODEC_FLAG_GLOBAL_HEADER;
74
75         boost::filesystem::path filename = _film->file(_film->isdcf_name(true) + ".mov");
76         avformat_alloc_output_context2 (&_format_context, 0, 0, filename.string().c_str());
77         if (!_format_context) {
78                 throw runtime_error ("could not allocate FFmpeg format context");
79         }
80
81         _video_stream = avformat_new_stream (_format_context, codec);
82         if (!_video_stream) {
83                 throw runtime_error ("could not create FFmpeg output video stream");
84         }
85
86         /* Note: needs to increment with each stream */
87         _video_stream->id = 0;
88         _video_stream->codec = _codec_context;
89
90         AVDictionary* options = 0;
91         av_dict_set (&options, "profile", "3", 0);
92         av_dict_set (&options, "threads", "auto", 0);
93
94         if (avcodec_open2 (_codec_context, codec, &options) < 0) {
95                 throw runtime_error ("could not open FFmpeg codec");
96         }
97
98         if (avio_open (&_format_context->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) {
99                 throw runtime_error ("could not open FFmpeg output file");
100         }
101
102         if (avformat_write_header (_format_context, &options) < 0) {
103                 throw runtime_error ("could not write header to FFmpeg output file");
104         }
105
106         {
107                 shared_ptr<Job> job = _job.lock ();
108                 DCPOMATIC_ASSERT (job);
109                 job->sub (_("Encoding"));
110         }
111
112         while (!_player->pass ()) {}
113
114         while (true) {
115                 AVPacket packet;
116                 av_init_packet (&packet);
117                 packet.data = 0;
118                 packet.size = 0;
119
120                 int got_packet;
121                 avcodec_encode_video2 (_codec_context, &packet, 0, &got_packet);
122                 if (!got_packet) {
123                         break;
124                 }
125
126                 packet.stream_index = 0;
127                 av_interleaved_write_frame (_format_context, &packet);
128                 av_packet_unref (&packet);
129         }
130
131         av_write_trailer (_format_context);
132
133         avcodec_close (_codec_context);
134         avio_close (_format_context->pb);
135         avformat_free_context (_format_context);
136 }
137
138 void
139 FFmpegTranscoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
140 {
141         shared_ptr<Image> image = video->image (
142                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
143                 bind (&force_pixel_format, _1, _pixel_format),
144                 true,
145                 false
146                 );
147
148         AVFrame* frame = av_frame_alloc ();
149
150         for (int i = 0; i < 3; ++i) {
151                 size_t const size = image->stride()[i] * image->size().height;
152                 AVBufferRef* buffer = av_buffer_alloc (size);
153                 /* XXX: inefficient */
154                 memcpy (buffer->data, image->data()[i], size);
155                 frame->buf[i] = av_buffer_ref (buffer);
156                 frame->data[i] = buffer->data;
157                 frame->linesize[i] = image->stride()[i];
158                 av_buffer_unref (&buffer);
159         }
160
161         frame->width = image->size().width;
162         frame->height = image->size().height;
163         frame->format = _pixel_format;
164         frame->pts = time.frames_round(_film->video_frame_rate()) / (_film->video_frame_rate() * av_q2d (_video_stream->time_base));
165
166         AVPacket packet;
167         av_init_packet (&packet);
168         packet.data = 0;
169         packet.size = 0;
170
171         int got_packet;
172         if (avcodec_encode_video2 (_codec_context, &packet, frame, &got_packet) < 0) {
173                 throw EncodeError ("FFmpeg video encode failed");
174         }
175
176         if (got_packet && packet.size) {
177                 /* XXX: this should not be hard-wired */
178                 packet.stream_index = 0;
179                 av_interleaved_write_frame (_format_context, &packet);
180                 av_packet_unref (&packet);
181         }
182
183         av_frame_free (&frame);
184 }
185
186 void
187 FFmpegTranscoder::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
188 {
189
190 }
191
192 void
193 FFmpegTranscoder::subtitle (PlayerSubtitles subs, DCPTimePeriod period)
194 {
195
196 }
197
198 float
199 FFmpegTranscoder::current_encoding_rate () const
200 {
201         /* XXX */
202         return 1;
203 }
204
205 int
206 FFmpegTranscoder::video_frames_enqueued () const
207 {
208         /* XXX */
209         return 1;
210 }