Show location for backsights out of tolerance
[survex.git] / src / moviemaker.cc
blob3a6f09678328433d7d26059c070da91bb434118b
1 //
2 // moviemaker.cc
3 //
4 // Class for writing movies from Aven.
5 //
6 // Copyright (C) 2004,2011,2012,2013,2014,2015,2016,2018 Olly Betts
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* Based on output-example.c:
25 * Libavformat API example: Output a media file in any supported
26 * libavformat format. The default codecs are used.
28 * Copyright (c) 2003 Fabrice Bellard
30 * Permission is hereby granted, free of charge, to any person obtaining a copy
31 * of this software and associated documentation files (the "Software"), to deal
32 * in the Software without restriction, including without limitation the rights
33 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 * copies of the Software, and to permit persons to whom the Software is
35 * furnished to do so, subject to the following conditions:
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
43 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 * THE SOFTWARE.
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
53 #define __STDC_CONSTANT_MACROS
55 #include <assert.h>
56 #include <stdlib.h>
57 #include <string.h>
59 #include "moviemaker.h"
61 #ifdef WITH_FFMPEG
62 extern "C" {
63 # include <libavcodec/avcodec.h>
64 # include <libavutil/imgutils.h>
65 # include <libavutil/mathematics.h>
66 # include <libavformat/avformat.h>
67 # include <libswscale/swscale.h>
69 #endif
71 // Handle the "no FFmpeg" case in this file.
72 #if !defined WITH_FFMPEG || LIBAVCODEC_VERSION_MAJOR >= 57
74 #ifdef WITH_FFMPEG
75 enum {
76 MOVIE_NO_SUITABLE_FORMAT = 1,
77 MOVIE_AUDIO_ONLY,
78 MOVIE_FILENAME_TOO_LONG
80 #endif
82 MovieMaker::MovieMaker()
83 #ifdef WITH_FFMPEG
84 : oc(0), video_st(0), context(0), frame(0), pixels(0), sws_ctx(0), averrno(0)
85 #endif
87 #ifdef WITH_FFMPEG
88 static bool initialised_ffmpeg = false;
89 if (initialised_ffmpeg) return;
91 #if LIBAVCODEC_VERSION_MAJOR < 58
92 avcodec_register_all();
93 av_register_all();
94 #endif
96 initialised_ffmpeg = true;
97 #endif
100 #ifdef WITH_FFMPEG
101 static int
102 write_packet(void *opaque, uint8_t *buf, int buf_size) {
103 FILE * fh = (FILE*)opaque;
104 size_t res = fwrite(buf, 1, buf_size, fh);
105 return res > 0 ? res : -1;
108 static int64_t
109 seek_stream(void *opaque, int64_t offset, int whence) {
110 FILE * fh = (FILE*)opaque;
111 return fseek(fh, offset, whence);
113 #endif
115 #define MAX_EXTENSION_LEN 8
117 bool MovieMaker::Open(FILE* fh, const char * ext, int width, int height)
119 #ifdef WITH_FFMPEG
120 fh_to_close = fh;
122 /* Allocate the output media context. */
123 char dummy_filename[MAX_EXTENSION_LEN + 3] = "x.";
124 oc = NULL;
125 if (strlen(ext) <= MAX_EXTENSION_LEN) {
126 // Use "x." + extension for format detection to avoid having to deal
127 // with wide character filenames.
128 strcpy(dummy_filename + 2, ext);
129 avformat_alloc_output_context2(&oc, NULL, NULL, dummy_filename);
131 if (!oc) {
132 averrno = MOVIE_NO_SUITABLE_FORMAT;
133 return false;
136 auto fmt = oc->oformat;
137 if (fmt->video_codec == AV_CODEC_ID_NONE) {
138 averrno = MOVIE_AUDIO_ONLY;
139 return false;
142 /* find the video encoder */
143 auto codec = avcodec_find_encoder(fmt->video_codec);
144 if (!codec) {
145 // FIXME : Erm - internal ffmpeg library problem?
146 averrno = AVERROR(ENOMEM);
147 return false;
150 // Add the video stream.
151 video_st = avformat_new_stream(oc, NULL);
152 if (!video_st) {
153 averrno = AVERROR(ENOMEM);
154 return false;
157 context = avcodec_alloc_context3(codec);
158 context->codec_id = fmt->video_codec;
159 context->width = width;
160 context->height = height;
161 video_st->time_base.den = 25; // Frames per second.
162 video_st->time_base.num = 1;
163 context->time_base = video_st->time_base;
164 context->bit_rate = width * height * (4 * 0.07) * context->time_base.den / context->time_base.num;
165 context->bit_rate_tolerance = context->bit_rate;
166 context->global_quality = 4;
167 context->rc_buffer_size = 2 * 1024 * 1024;
168 context->rc_max_rate = context->bit_rate * 8;
169 context->gop_size = 50; /* Twice the framerate */
170 context->pix_fmt = AV_PIX_FMT_YUV420P;
171 if (context->has_b_frames) {
172 // B frames are backwards predicted - they can improve compression,
173 // but may slow encoding and decoding.
174 context->max_b_frames = 4;
177 /* Some formats want stream headers to be separate. */
178 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
179 context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
181 int retval;
182 retval = avcodec_open2(context, codec, NULL);
183 if (retval < 0) {
184 averrno = retval;
185 return false;
188 /* Allocate the encoded raw picture. */
189 frame = av_frame_alloc();
190 if (!frame) {
191 averrno = AVERROR(ENOMEM);
192 return false;
195 frame->format = context->pix_fmt;
196 frame->width = width;
197 frame->height = height;
198 frame->pts = 0;
200 retval = av_frame_get_buffer(frame, 32);
201 if (retval < 0) {
202 averrno = retval;
203 return false;
206 if (frame->format != AV_PIX_FMT_YUV420P) {
207 // FIXME need to allocate another frame for this case if we stop
208 // hardcoding AV_PIX_FMT_YUV420P.
209 abort();
212 /* copy the stream parameters to the muxer */
213 retval = avcodec_parameters_from_context(video_st->codecpar, context);
214 if (retval < 0) {
215 averrno = retval;
216 return false;
219 pixels = (unsigned char *)av_malloc(width * height * 6);
220 if (!pixels) {
221 averrno = AVERROR(ENOMEM);
222 return false;
225 // Show the format we've ended up with (for debug purposes).
226 // av_dump_format(oc, 0, dummy_filename, 1);
228 av_free(sws_ctx);
229 sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24,
230 width, height, context->pix_fmt, SWS_BICUBIC,
231 NULL, NULL, NULL);
232 if (sws_ctx == NULL) {
233 fprintf(stderr, "Cannot initialize the conversion context!\n");
234 averrno = AVERROR(ENOMEM);
235 return false;
238 if (!(fmt->flags & AVFMT_NOFILE)) {
239 const int buf_size = 8192;
240 void * buf = av_malloc(buf_size);
241 oc->pb = avio_alloc_context(static_cast<uint8_t*>(buf), buf_size, 1,
242 fh, NULL, write_packet, seek_stream);
243 if (!oc->pb) {
244 averrno = AVERROR(ENOMEM);
245 return false;
249 // Write the stream header, if any.
250 retval = avformat_write_header(oc, NULL);
251 if (retval < 0) {
252 averrno = retval;
253 return false;
256 averrno = 0;
257 return true;
258 #else
259 (void)fh;
260 (void)ext;
261 (void)width;
262 (void)height;
263 return false;
264 #endif
267 unsigned char * MovieMaker::GetBuffer() const {
268 #ifdef WITH_FFMPEG
269 return pixels + GetWidth() * GetHeight() * 3;
270 #else
271 return NULL;
272 #endif
275 int MovieMaker::GetWidth() const {
276 #ifdef WITH_FFMPEG
277 assert(video_st);
278 return video_st->codecpar->width;
279 #else
280 return 0;
281 #endif
284 int MovieMaker::GetHeight() const {
285 #ifdef WITH_FFMPEG
286 assert(video_st);
287 return video_st->codecpar->height;
288 #else
289 return 0;
290 #endif
293 #ifdef WITH_FFMPEG
294 // Call with frame=NULL when done.
296 MovieMaker::encode_frame(AVFrame* frame_or_null)
298 int ret = avcodec_send_frame(context, frame_or_null);
299 if (ret < 0) return ret;
301 AVPacket *pkt = av_packet_alloc();
302 pkt->size = 0;
303 while ((ret = avcodec_receive_packet(context, pkt)) == 0) {
304 // Rescale output packet timestamp values from codec to stream timebase.
305 av_packet_rescale_ts(pkt, context->time_base, video_st->time_base);
306 pkt->stream_index = video_st->index;
308 // Write the compressed frame to the media file.
309 ret = av_interleaved_write_frame(oc, pkt);
310 if (ret < 0) {
311 av_packet_free(&pkt);
312 release();
313 return ret;
316 av_packet_free(&pkt);
317 return 0;
319 #endif
321 bool MovieMaker::AddFrame()
323 #ifdef WITH_FFMPEG
324 int ret = av_frame_make_writable(frame);
325 if (ret < 0) {
326 averrno = ret;
327 return false;
330 enum AVPixelFormat pix_fmt = context->pix_fmt;
332 if (pix_fmt != AV_PIX_FMT_YUV420P) {
333 // FIXME convert...
334 abort();
337 int len = 3 * GetWidth();
339 // Flip image vertically
340 int h = GetHeight();
341 unsigned char * src = pixels + h * len;
342 unsigned char * dest = src - len;
343 while (h--) {
344 memcpy(dest, src, len);
345 src += len;
346 dest -= len;
349 sws_scale(sws_ctx, &pixels, &len, 0, GetHeight(),
350 frame->data, frame->linesize);
352 ++frame->pts;
354 // Encode this frame.
355 ret = encode_frame(frame);
356 if (ret < 0) {
357 averrno = ret;
358 return false;
360 #endif
361 return true;
364 bool
365 MovieMaker::Close()
367 #ifdef WITH_FFMPEG
368 if (video_st && averrno == 0) {
369 // Flush out any remaining data.
370 int ret = encode_frame(NULL);
371 if (ret < 0) {
372 averrno = ret;
373 return false;
375 av_write_trailer(oc);
378 release();
379 #endif
380 return true;
383 #ifdef WITH_FFMPEG
384 void
385 MovieMaker::release()
387 // Close codec.
388 avcodec_free_context(&context);
389 av_frame_free(&frame);
390 av_free(pixels);
391 pixels = NULL;
392 sws_freeContext(sws_ctx);
393 sws_ctx = NULL;
395 // Free the stream.
396 avformat_free_context(oc);
397 oc = NULL;
399 if (fh_to_close) {
400 fclose(fh_to_close);
401 fh_to_close = NULL;
404 #endif
406 MovieMaker::~MovieMaker()
408 #ifdef WITH_FFMPEG
409 release();
410 #endif
413 const char *
414 MovieMaker::get_error_string() const
416 #ifdef WITH_FFMPEG
417 switch (averrno) {
418 case AVERROR(EIO):
419 return "I/O error";
420 case AVERROR(EDOM):
421 return "Number syntax expected in filename";
422 case AVERROR_INVALIDDATA:
423 /* same as AVERROR_UNKNOWN: return "unknown error"; */
424 return "invalid data found";
425 case AVERROR(ENOMEM):
426 return "not enough memory";
427 case AVERROR(EILSEQ):
428 return "unknown format";
429 case AVERROR(ENOSYS):
430 return "Operation not supported";
431 case AVERROR(ENOENT):
432 return "No such file or directory";
433 case AVERROR_EOF:
434 return "End of file";
435 case AVERROR_PATCHWELCOME:
436 return "Not implemented in FFmpeg";
437 case 0:
438 return "No error";
439 case MOVIE_NO_SUITABLE_FORMAT:
440 return "Couldn't find a suitable output format";
441 case MOVIE_AUDIO_ONLY:
442 return "Audio-only format specified";
443 case MOVIE_FILENAME_TOO_LONG:
444 return "Filename too long";
446 #endif
447 return "Unknown error";
450 #else
452 #include "moviemaker-legacy.cc"
454 #endif