Update for upcoming FFmpeg 3.5
[survex.git] / src / moviemaker.cc
blob5a9fd7f3c61c05ee0f884444a00482d81a0ba863
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_LIBAV
62 extern "C" {
63 # include <libavutil/imgutils.h>
64 # include <libavutil/mathematics.h>
65 # include <libavformat/avformat.h>
66 # include <libswscale/swscale.h>
68 #endif
70 // Handle the "no libav/FFmpeg" case in this file.
71 #if !defined WITH_LIBAV || LIBAVCODEC_VERSION_MAJOR >= 57
73 #ifdef WITH_LIBAV
74 enum {
75 MOVIE_NO_SUITABLE_FORMAT = 1,
76 MOVIE_AUDIO_ONLY,
77 MOVIE_FILENAME_TOO_LONG
79 #endif
81 MovieMaker::MovieMaker()
82 #ifdef WITH_LIBAV
83 : oc(0), video_st(0), context(0), frame(0), pixels(0), sws_ctx(0), averrno(0)
84 #endif
86 #ifdef WITH_LIBAV
87 static bool initialised_ffmpeg = false;
88 if (initialised_ffmpeg) return;
90 // FIXME: register only the codec(s) we want to use...
91 avcodec_register_all();
92 av_register_all();
94 initialised_ffmpeg = true;
95 #endif
98 #ifdef WITH_LIBAV
99 static int
100 write_packet(void *opaque, uint8_t *buf, int buf_size) {
101 FILE * fh = (FILE*)opaque;
102 size_t res = fwrite(buf, 1, buf_size, fh);
103 return res > 0 ? res : -1;
106 static int64_t
107 seek_stream(void *opaque, int64_t offset, int whence) {
108 FILE * fh = (FILE*)opaque;
109 return fseek(fh, offset, whence);
111 #endif
113 #define MAX_EXTENSION_LEN 8
115 bool MovieMaker::Open(FILE* fh, const char * ext, int width, int height)
117 #ifdef WITH_LIBAV
118 fh_to_close = fh;
120 /* Allocate the output media context. */
121 char dummy_filename[MAX_EXTENSION_LEN + 3] = "x.";
122 oc = NULL;
123 if (strlen(ext) <= MAX_EXTENSION_LEN) {
124 // Use "x." + extension for format detection to avoid having to deal
125 // with wide character filenames.
126 strcpy(dummy_filename + 2, ext);
127 avformat_alloc_output_context2(&oc, NULL, NULL, dummy_filename);
129 if (!oc) {
130 averrno = MOVIE_NO_SUITABLE_FORMAT;
131 return false;
134 AVOutputFormat * fmt = oc->oformat;
135 if (fmt->video_codec == AV_CODEC_ID_NONE) {
136 averrno = MOVIE_AUDIO_ONLY;
137 return false;
140 /* find the video encoder */
141 AVCodec *codec = avcodec_find_encoder(fmt->video_codec);
142 if (!codec) {
143 // FIXME : Erm - internal ffmpeg library problem?
144 averrno = AVERROR(ENOMEM);
145 return false;
148 // Add the video stream.
149 video_st = avformat_new_stream(oc, NULL);
150 if (!video_st) {
151 averrno = AVERROR(ENOMEM);
152 return false;
155 context = avcodec_alloc_context3(codec);
156 context->codec_id = fmt->video_codec;
157 context->width = width;
158 context->height = height;
159 video_st->time_base.den = 25; // Frames per second.
160 video_st->time_base.num = 1;
161 context->time_base = video_st->time_base;
162 context->bit_rate = width * height * (4 * 0.07) * context->time_base.den / context->time_base.num;
163 context->bit_rate_tolerance = context->bit_rate;
164 context->global_quality = 4;
165 context->rc_buffer_size = 2 * 1024 * 1024;
166 context->rc_max_rate = context->bit_rate * 8;
167 context->gop_size = 50; /* Twice the framerate */
168 context->pix_fmt = AV_PIX_FMT_YUV420P;
169 if (context->has_b_frames) {
170 // B frames are backwards predicted - they can improve compression,
171 // but may slow encoding and decoding.
172 context->max_b_frames = 4;
175 /* Some formats want stream headers to be separate. */
176 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
177 context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
179 int retval;
180 retval = avcodec_open2(context, codec, NULL);
181 if (retval < 0) {
182 averrno = retval;
183 return false;
186 /* Allocate the encoded raw picture. */
187 frame = av_frame_alloc();
188 if (!frame) {
189 averrno = AVERROR(ENOMEM);
190 return false;
193 frame->format = context->pix_fmt;
194 frame->width = width;
195 frame->height = height;
196 frame->pts = 0;
198 retval = av_frame_get_buffer(frame, 32);
199 if (retval < 0) {
200 averrno = retval;
201 return false;
204 if (frame->format != AV_PIX_FMT_YUV420P) {
205 // FIXME need to allocate another frame for this case if we stop
206 // hardcoding AV_PIX_FMT_YUV420P.
207 abort();
210 /* copy the stream parameters to the muxer */
211 retval = avcodec_parameters_from_context(video_st->codecpar, context);
212 if (retval < 0) {
213 averrno = retval;
214 return false;
217 pixels = (unsigned char *)av_malloc(width * height * 6);
218 if (!pixels) {
219 averrno = AVERROR(ENOMEM);
220 return false;
223 // Show the format we've ended up with (for debug purposes).
224 // av_dump_format(oc, 0, dummy_filename, 1);
226 av_free(sws_ctx);
227 sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24,
228 width, height, context->pix_fmt, SWS_BICUBIC,
229 NULL, NULL, NULL);
230 if (sws_ctx == NULL) {
231 fprintf(stderr, "Cannot initialize the conversion context!\n");
232 averrno = AVERROR(ENOMEM);
233 return false;
236 if (!(fmt->flags & AVFMT_NOFILE)) {
237 const int buf_size = 8192;
238 void * buf = av_malloc(buf_size);
239 oc->pb = avio_alloc_context(static_cast<uint8_t*>(buf), buf_size, 1,
240 fh, NULL, write_packet, seek_stream);
241 if (!oc->pb) {
242 averrno = AVERROR(ENOMEM);
243 return false;
247 // Write the stream header, if any.
248 retval = avformat_write_header(oc, NULL);
249 if (retval < 0) {
250 averrno = retval;
251 return false;
254 averrno = 0;
255 return true;
256 #else
257 (void)fh;
258 (void)ext;
259 (void)width;
260 (void)height;
261 return false;
262 #endif
265 unsigned char * MovieMaker::GetBuffer() const {
266 #ifdef WITH_LIBAV
267 return pixels + GetWidth() * GetHeight() * 3;
268 #else
269 return NULL;
270 #endif
273 int MovieMaker::GetWidth() const {
274 #ifdef WITH_LIBAV
275 assert(video_st);
276 return video_st->codecpar->width;
277 #else
278 return 0;
279 #endif
282 int MovieMaker::GetHeight() const {
283 #ifdef WITH_LIBAV
284 assert(video_st);
285 return video_st->codecpar->height;
286 #else
287 return 0;
288 #endif
291 #ifdef WITH_LIBAV
292 // Call with frame=NULL when done.
294 MovieMaker::encode_frame(AVFrame* frame_or_null)
296 int ret = avcodec_send_frame(context, frame_or_null);
297 if (ret < 0) return ret;
299 AVPacket *pkt = av_packet_alloc();
300 pkt->size = 0;
301 while ((ret = avcodec_receive_packet(context, pkt)) == 0) {
302 // Rescale output packet timestamp values from codec to stream timebase.
303 av_packet_rescale_ts(pkt, context->time_base, video_st->time_base);
304 pkt->stream_index = video_st->index;
306 // Write the compressed frame to the media file.
307 ret = av_interleaved_write_frame(oc, pkt);
308 if (ret < 0) {
309 av_packet_free(&pkt);
310 release();
311 return ret;
314 av_packet_free(&pkt);
315 return 0;
317 #endif
319 bool MovieMaker::AddFrame()
321 #ifdef WITH_LIBAV
322 int ret = av_frame_make_writable(frame);
323 if (ret < 0) {
324 averrno = ret;
325 return false;
328 enum AVPixelFormat pix_fmt = context->pix_fmt;
330 if (pix_fmt != AV_PIX_FMT_YUV420P) {
331 // FIXME convert...
332 abort();
335 int len = 3 * GetWidth();
337 // Flip image vertically
338 int h = GetHeight();
339 unsigned char * src = pixels + h * len;
340 unsigned char * dest = src - len;
341 while (h--) {
342 memcpy(dest, src, len);
343 src += len;
344 dest -= len;
347 sws_scale(sws_ctx, &pixels, &len, 0, GetHeight(),
348 frame->data, frame->linesize);
350 ++frame->pts;
352 // Encode this frame.
353 ret = encode_frame(frame);
354 if (ret < 0) {
355 averrno = ret;
356 return false;
358 #endif
359 return true;
362 bool
363 MovieMaker::Close()
365 #ifdef WITH_LIBAV
366 if (video_st && averrno == 0) {
367 // Flush out any remaining data.
368 int ret = encode_frame(NULL);
369 if (ret < 0) {
370 averrno = ret;
371 return false;
373 av_write_trailer(oc);
376 release();
377 #endif
378 return true;
381 #ifdef WITH_LIBAV
382 void
383 MovieMaker::release()
385 // Close codec.
386 avcodec_free_context(&context);
387 av_frame_free(&frame);
388 av_free(pixels);
389 pixels = NULL;
390 sws_freeContext(sws_ctx);
391 sws_ctx = NULL;
393 // Free the stream.
394 avformat_free_context(oc);
395 oc = NULL;
397 if (fh_to_close) {
398 fclose(fh_to_close);
399 fh_to_close = NULL;
402 #endif
404 MovieMaker::~MovieMaker()
406 #ifdef WITH_LIBAV
407 release();
408 #endif
411 const char *
412 MovieMaker::get_error_string() const
414 #ifdef WITH_LIBAV
415 switch (averrno) {
416 case AVERROR(EIO):
417 return "I/O error";
418 case AVERROR(EDOM):
419 return "Number syntax expected in filename";
420 case AVERROR_INVALIDDATA:
421 /* same as AVERROR_UNKNOWN: return "unknown error"; */
422 return "invalid data found";
423 case AVERROR(ENOMEM):
424 return "not enough memory";
425 case AVERROR(EILSEQ):
426 return "unknown format";
427 case AVERROR(ENOSYS):
428 return "Operation not supported";
429 case AVERROR(ENOENT):
430 return "No such file or directory";
431 case AVERROR_EOF:
432 return "End of file";
433 case AVERROR_PATCHWELCOME:
434 return "Not implemented in FFmpeg";
435 case 0:
436 return "No error";
437 case MOVIE_NO_SUITABLE_FORMAT:
438 return "Couldn't find a suitable output format";
439 case MOVIE_AUDIO_ONLY:
440 return "Audio-only format specified";
441 case MOVIE_FILENAME_TOO_LONG:
442 return "Filename too long";
444 #endif
445 return "Unknown error";
448 #else
450 #include "moviemaker-legacy.cc"
452 #endif