5 #include "frame_on_disk.h"
6 #include "shared/ffmpeg_raii.h"
7 #include "shared/timebase.h"
10 #include <QProgressDialog>
17 #include <libavformat/avformat.h>
24 // Only used in export_cliplist_clip_multitrack_triggered.
31 bool write_buffered_jpegs(AVFormatContext *avctx, const vector<BufferedJPEG> &buffered_jpegs)
33 for (const BufferedJPEG &jpeg : buffered_jpegs) {
36 pkt.stream_index = jpeg.stream_idx;
37 pkt.data = (uint8_t *)jpeg.jpeg.data();
38 pkt.size = jpeg.jpeg.size();
41 pkt.flags = AV_PKT_FLAG_KEY;
43 if (av_write_frame(avctx, &pkt) < 0) {
52 void export_multitrack_clip(const string &filename, const Clip &clip)
54 AVFormatContext *avctx = nullptr;
55 avformat_alloc_output_context2(&avctx, NULL, NULL, filename.c_str());
56 if (avctx == nullptr) {
58 msgbox.setText("Could not allocate FFmpeg context");
62 AVFormatContextWithCloser closer(avctx);
64 int ret = avio_open(&avctx->pb, filename.c_str(), AVIO_FLAG_WRITE);
67 msgbox.setText(QString::fromStdString("Could not open output file '" + filename + "'"));
72 // Find the first frame for each stream.
73 size_t num_frames = 0;
74 size_t num_streams_with_frames_left = 0;
75 size_t last_stream_idx = 0;
76 FrameReader readers[MAX_STREAMS];
77 bool has_frames[MAX_STREAMS];
78 size_t first_frame_idx[MAX_STREAMS], last_frame_idx[MAX_STREAMS]; // Inclusive, exclusive.
80 lock_guard<mutex> lock(frame_mu);
81 for (size_t stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
82 // Find the first frame such that frame.pts <= pts_in.
83 auto it = find_first_frame_at_or_after(frames[stream_idx], clip.pts_in);
84 first_frame_idx[stream_idx] = distance(frames[stream_idx].begin(), it);
85 has_frames[stream_idx] = (it != frames[stream_idx].end());
87 // Find the first frame such that frame.pts >= pts_out.
88 it = find_first_frame_at_or_after(frames[stream_idx], clip.pts_out);
89 last_frame_idx[stream_idx] = distance(frames[stream_idx].begin(), it);
90 num_frames += last_frame_idx[stream_idx] - first_frame_idx[stream_idx];
92 if (has_frames[stream_idx]) {
93 ++num_streams_with_frames_left;
94 last_stream_idx = stream_idx;
99 // Create the streams. Note that some of them could be without frames
100 // (we try to maintain the stream indexes in the export).
101 vector<AVStream *> video_streams;
102 for (unsigned stream_idx = 0; stream_idx <= last_stream_idx; ++stream_idx) {
103 AVStream *avstream_video = avformat_new_stream(avctx, nullptr);
104 if (avstream_video == nullptr) {
105 fprintf(stderr, "avformat_new_stream() failed\n");
108 avstream_video->time_base = AVRational{1, TIMEBASE};
109 avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
110 avstream_video->codecpar->codec_id = AV_CODEC_ID_MJPEG;
111 avstream_video->codecpar->width = global_flags.width; // Might be wrong, but doesn't matter all that much.
112 avstream_video->codecpar->height = global_flags.height;
114 // TODO: Deduplicate this against Mux.
115 avstream_video->codecpar->color_primaries = AVCOL_PRI_BT709; // RGB colorspace (inout_format.color_space).
116 avstream_video->codecpar->color_trc = AVCOL_TRC_IEC61966_2_1; // Gamma curve (inout_format.gamma_curve).
117 // YUV colorspace (output_ycbcr_format.luma_coefficients).
118 avstream_video->codecpar->color_space = AVCOL_SPC_BT709;
119 avstream_video->codecpar->color_range = AVCOL_RANGE_MPEG; // Full vs. limited range (output_ycbcr_format.full_range).
120 avstream_video->codecpar->chroma_location = AVCHROMA_LOC_LEFT; // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
121 avstream_video->codecpar->field_order = AV_FIELD_PROGRESSIVE;
122 video_streams.push_back(avstream_video);
125 if (avformat_write_header(avctx, nullptr) < 0) {
127 msgbox.setText("Writing header failed");
129 unlink(filename.c_str());
133 QProgressDialog progress(QString::fromStdString("Exporting to " + filename + "..."), "Abort", 0, 1);
134 progress.setWindowTitle("Futatabi");
135 progress.setWindowModality(Qt::WindowModal);
136 progress.setMinimumDuration(1000);
137 progress.setMaximum(num_frames);
138 progress.setValue(0);
140 // We buffer up to 1000 frames at a time, in a hope that we can reduce
141 // the amount of seeking needed on rotational media.
142 vector<BufferedJPEG> buffered_jpegs;
143 size_t frames_written = 0;
144 while (num_streams_with_frames_left > 0) {
145 // Find the stream with the lowest frame. Lower stream indexes win.
146 FrameOnDisk first_frame;
147 unsigned first_frame_stream_idx = 0;
149 lock_guard<mutex> lock(frame_mu);
150 for (size_t stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
151 if (!has_frames[stream_idx]) {
154 if (first_frame.pts == -1 || frames[stream_idx][first_frame_idx[stream_idx]].pts < first_frame.pts) {
155 first_frame = frames[stream_idx][first_frame_idx[stream_idx]];
156 first_frame_stream_idx = stream_idx;
159 ++first_frame_idx[first_frame_stream_idx];
160 if (first_frame_idx[first_frame_stream_idx] >= last_frame_idx[first_frame_stream_idx]) {
161 has_frames[first_frame_stream_idx] = false;
162 --num_streams_with_frames_left;
165 string jpeg = readers[first_frame_stream_idx].read_frame(first_frame);
166 int64_t scaled_pts = av_rescale_q(first_frame.pts, AVRational{1, TIMEBASE},
167 video_streams[first_frame_stream_idx]->time_base);
168 buffered_jpegs.emplace_back(BufferedJPEG{ scaled_pts, first_frame_stream_idx, std::move(jpeg) });
169 if (buffered_jpegs.size() >= 1000) {
170 if (!write_buffered_jpegs(avctx, buffered_jpegs)) {
172 msgbox.setText("Writing frames failed");
174 unlink(filename.c_str());
177 frames_written += buffered_jpegs.size();
178 progress.setValue(frames_written);
179 buffered_jpegs.clear();
181 if (progress.wasCanceled()) {
182 unlink(filename.c_str());
187 if (!write_buffered_jpegs(avctx, buffered_jpegs)) {
189 msgbox.setText("Writing frames failed");
191 unlink(filename.c_str());
194 frames_written += buffered_jpegs.size();
195 progress.setValue(frames_written);