| 1 | // |
|---|
| 2 | // moviemaker.cc |
|---|
| 3 | // |
|---|
| 4 | // Class for writing movies from Aven. |
|---|
| 5 | // |
|---|
| 6 | // Copyright (C) 2004-2024 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. |
|---|
| 12 | // |
|---|
| 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. |
|---|
| 17 | // |
|---|
| 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 |
|---|
| 21 | // |
|---|
| 22 | |
|---|
| 23 | /* Based on output-example.c: |
|---|
| 24 | * |
|---|
| 25 | * Libavformat API example: Output a media file in any supported |
|---|
| 26 | * libavformat format. The default codecs are used. |
|---|
| 27 | * |
|---|
| 28 | * Copyright (c) 2003 Fabrice Bellard |
|---|
| 29 | * |
|---|
| 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: |
|---|
| 36 | * |
|---|
| 37 | * The above copyright notice and this permission notice shall be included in |
|---|
| 38 | * all copies or substantial portions of the Software. |
|---|
| 39 | * |
|---|
| 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. |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | #ifdef HAVE_CONFIG_H |
|---|
| 50 | #include <config.h> |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | #define __STDC_CONSTANT_MACROS |
|---|
| 54 | |
|---|
| 55 | #include <assert.h> |
|---|
| 56 | #include <stdlib.h> |
|---|
| 57 | #include <string.h> |
|---|
| 58 | |
|---|
| 59 | #include "moviemaker.h" |
|---|
| 60 | |
|---|
| 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> |
|---|
| 68 | } |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | // Handle the "no FFmpeg" case in this file. |
|---|
| 72 | #if !defined WITH_FFMPEG || LIBAVCODEC_VERSION_MAJOR >= 57 |
|---|
| 73 | |
|---|
| 74 | #ifdef WITH_FFMPEG |
|---|
| 75 | enum { |
|---|
| 76 | MOVIE_NO_SUITABLE_FORMAT = 1, |
|---|
| 77 | MOVIE_AUDIO_ONLY, |
|---|
| 78 | MOVIE_FILENAME_TOO_LONG |
|---|
| 79 | }; |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 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 |
|---|
| 86 | { |
|---|
| 87 | #ifdef WITH_FFMPEG |
|---|
| 88 | static bool initialised_ffmpeg = false; |
|---|
| 89 | if (initialised_ffmpeg) return; |
|---|
| 90 | |
|---|
| 91 | #if LIBAVCODEC_VERSION_MAJOR < 58 |
|---|
| 92 | avcodec_register_all(); |
|---|
| 93 | av_register_all(); |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | initialised_ffmpeg = true; |
|---|
| 97 | #endif |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | #ifdef WITH_FFMPEG |
|---|
| 101 | static int |
|---|
| 102 | write_packet(void *opaque, |
|---|
| 103 | #if LIBAVFORMAT_VERSION_MAJOR < 61 |
|---|
| 104 | uint8_t *buf, |
|---|
| 105 | #else |
|---|
| 106 | const uint8_t *buf, |
|---|
| 107 | #endif |
|---|
| 108 | int buf_size) |
|---|
| 109 | { |
|---|
| 110 | FILE * fh = (FILE*)opaque; |
|---|
| 111 | size_t res = fwrite(buf, 1, buf_size, fh); |
|---|
| 112 | return res > 0 ? res : -1; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | static int64_t |
|---|
| 116 | seek_stream(void *opaque, int64_t offset, int whence) { |
|---|
| 117 | FILE * fh = (FILE*)opaque; |
|---|
| 118 | return fseek(fh, offset, whence); |
|---|
| 119 | } |
|---|
| 120 | #endif |
|---|
| 121 | |
|---|
| 122 | #define MAX_EXTENSION_LEN 8 |
|---|
| 123 | |
|---|
| 124 | bool MovieMaker::Open(FILE* fh, const char * ext, int width, int height) |
|---|
| 125 | { |
|---|
| 126 | #ifdef WITH_FFMPEG |
|---|
| 127 | fh_to_close = fh; |
|---|
| 128 | |
|---|
| 129 | /* Allocate the output media context. */ |
|---|
| 130 | char dummy_filename[MAX_EXTENSION_LEN + 3] = "x."; |
|---|
| 131 | oc = NULL; |
|---|
| 132 | if (strlen(ext) <= MAX_EXTENSION_LEN) { |
|---|
| 133 | // Use "x." + extension for format detection to avoid having to deal |
|---|
| 134 | // with wide character filenames. |
|---|
| 135 | strcpy(dummy_filename + 2, ext); |
|---|
| 136 | avformat_alloc_output_context2(&oc, NULL, NULL, dummy_filename); |
|---|
| 137 | } |
|---|
| 138 | if (!oc) { |
|---|
| 139 | averrno = MOVIE_NO_SUITABLE_FORMAT; |
|---|
| 140 | return false; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | auto fmt = oc->oformat; |
|---|
| 144 | if (fmt->video_codec == AV_CODEC_ID_NONE) { |
|---|
| 145 | averrno = MOVIE_AUDIO_ONLY; |
|---|
| 146 | return false; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* find the video encoder */ |
|---|
| 150 | auto codec = avcodec_find_encoder(fmt->video_codec); |
|---|
| 151 | if (!codec) { |
|---|
| 152 | // FIXME : Erm - internal ffmpeg library problem? |
|---|
| 153 | averrno = AVERROR(ENOMEM); |
|---|
| 154 | return false; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | // Add the video stream. |
|---|
| 158 | video_st = avformat_new_stream(oc, NULL); |
|---|
| 159 | if (!video_st) { |
|---|
| 160 | averrno = AVERROR(ENOMEM); |
|---|
| 161 | return false; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | context = avcodec_alloc_context3(codec); |
|---|
| 165 | context->codec_id = fmt->video_codec; |
|---|
| 166 | context->width = width; |
|---|
| 167 | context->height = height; |
|---|
| 168 | video_st->time_base.den = 25; // Frames per second. |
|---|
| 169 | video_st->time_base.num = 1; |
|---|
| 170 | context->time_base = video_st->time_base; |
|---|
| 171 | context->bit_rate = width * height * (4 * 0.07) * context->time_base.den / context->time_base.num; |
|---|
| 172 | context->bit_rate_tolerance = context->bit_rate; |
|---|
| 173 | context->global_quality = 4; |
|---|
| 174 | context->rc_buffer_size = 2 * 1024 * 1024; |
|---|
| 175 | context->rc_max_rate = context->bit_rate * 8; |
|---|
| 176 | context->gop_size = 50; /* Twice the framerate */ |
|---|
| 177 | context->pix_fmt = AV_PIX_FMT_YUV420P; |
|---|
| 178 | if (context->has_b_frames) { |
|---|
| 179 | // B frames are backwards predicted - they can improve compression, |
|---|
| 180 | // but may slow encoding and decoding. |
|---|
| 181 | context->max_b_frames = 4; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /* Some formats want stream headers to be separate. */ |
|---|
| 185 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
|---|
| 186 | context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
|---|
| 187 | |
|---|
| 188 | int retval; |
|---|
| 189 | retval = avcodec_open2(context, codec, NULL); |
|---|
| 190 | if (retval < 0) { |
|---|
| 191 | averrno = retval; |
|---|
| 192 | return false; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /* Allocate the encoded raw picture. */ |
|---|
| 196 | frame = av_frame_alloc(); |
|---|
| 197 | if (!frame) { |
|---|
| 198 | averrno = AVERROR(ENOMEM); |
|---|
| 199 | return false; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | frame->format = context->pix_fmt; |
|---|
| 203 | frame->width = width; |
|---|
| 204 | frame->height = height; |
|---|
| 205 | frame->pts = 0; |
|---|
| 206 | |
|---|
| 207 | retval = av_frame_get_buffer(frame, 32); |
|---|
| 208 | if (retval < 0) { |
|---|
| 209 | averrno = retval; |
|---|
| 210 | return false; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if (frame->format != AV_PIX_FMT_YUV420P) { |
|---|
| 214 | // FIXME need to allocate another frame for this case if we stop |
|---|
| 215 | // hardcoding AV_PIX_FMT_YUV420P. |
|---|
| 216 | abort(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /* copy the stream parameters to the muxer */ |
|---|
| 220 | retval = avcodec_parameters_from_context(video_st->codecpar, context); |
|---|
| 221 | if (retval < 0) { |
|---|
| 222 | averrno = retval; |
|---|
| 223 | return false; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | pixels = (unsigned char *)av_malloc(width * height * 6); |
|---|
| 227 | if (!pixels) { |
|---|
| 228 | averrno = AVERROR(ENOMEM); |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | // Show the format we've ended up with (for debug purposes). |
|---|
| 233 | // av_dump_format(oc, 0, dummy_filename, 1); |
|---|
| 234 | |
|---|
| 235 | av_free(sws_ctx); |
|---|
| 236 | sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24, |
|---|
| 237 | width, height, context->pix_fmt, SWS_BICUBIC, |
|---|
| 238 | NULL, NULL, NULL); |
|---|
| 239 | if (sws_ctx == NULL) { |
|---|
| 240 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
|---|
| 241 | averrno = AVERROR(ENOMEM); |
|---|
| 242 | return false; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | if (!(fmt->flags & AVFMT_NOFILE)) { |
|---|
| 246 | const int buf_size = 8192; |
|---|
| 247 | void * buf = av_malloc(buf_size); |
|---|
| 248 | oc->pb = avio_alloc_context(static_cast<uint8_t*>(buf), buf_size, 1, |
|---|
| 249 | fh, NULL, write_packet, seek_stream); |
|---|
| 250 | if (!oc->pb) { |
|---|
| 251 | averrno = AVERROR(ENOMEM); |
|---|
| 252 | return false; |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | // Write the stream header, if any. |
|---|
| 257 | retval = avformat_write_header(oc, NULL); |
|---|
| 258 | if (retval < 0) { |
|---|
| 259 | averrno = retval; |
|---|
| 260 | return false; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | averrno = 0; |
|---|
| 264 | return true; |
|---|
| 265 | #else |
|---|
| 266 | (void)fh; |
|---|
| 267 | (void)ext; |
|---|
| 268 | (void)width; |
|---|
| 269 | (void)height; |
|---|
| 270 | return false; |
|---|
| 271 | #endif |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | unsigned char * MovieMaker::GetBuffer() const { |
|---|
| 275 | #ifdef WITH_FFMPEG |
|---|
| 276 | return pixels + GetWidth() * GetHeight() * 3; |
|---|
| 277 | #else |
|---|
| 278 | return NULL; |
|---|
| 279 | #endif |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | int MovieMaker::GetWidth() const { |
|---|
| 283 | #ifdef WITH_FFMPEG |
|---|
| 284 | assert(video_st); |
|---|
| 285 | return video_st->codecpar->width; |
|---|
| 286 | #else |
|---|
| 287 | return 0; |
|---|
| 288 | #endif |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | int MovieMaker::GetHeight() const { |
|---|
| 292 | #ifdef WITH_FFMPEG |
|---|
| 293 | assert(video_st); |
|---|
| 294 | return video_st->codecpar->height; |
|---|
| 295 | #else |
|---|
| 296 | return 0; |
|---|
| 297 | #endif |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | #ifdef WITH_FFMPEG |
|---|
| 301 | // Call with frame=NULL when done. |
|---|
| 302 | int |
|---|
| 303 | MovieMaker::encode_frame(AVFrame* frame_or_null) |
|---|
| 304 | { |
|---|
| 305 | int ret = avcodec_send_frame(context, frame_or_null); |
|---|
| 306 | if (ret < 0) return ret; |
|---|
| 307 | |
|---|
| 308 | AVPacket *pkt = av_packet_alloc(); |
|---|
| 309 | pkt->size = 0; |
|---|
| 310 | while ((ret = avcodec_receive_packet(context, pkt)) == 0) { |
|---|
| 311 | // Rescale output packet timestamp values from codec to stream timebase. |
|---|
| 312 | av_packet_rescale_ts(pkt, context->time_base, video_st->time_base); |
|---|
| 313 | pkt->stream_index = video_st->index; |
|---|
| 314 | |
|---|
| 315 | // Write the compressed frame to the media file. |
|---|
| 316 | ret = av_interleaved_write_frame(oc, pkt); |
|---|
| 317 | if (ret < 0) { |
|---|
| 318 | av_packet_free(&pkt); |
|---|
| 319 | release(); |
|---|
| 320 | return ret; |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | av_packet_free(&pkt); |
|---|
| 324 | return 0; |
|---|
| 325 | } |
|---|
| 326 | #endif |
|---|
| 327 | |
|---|
| 328 | bool MovieMaker::AddFrame() |
|---|
| 329 | { |
|---|
| 330 | #ifdef WITH_FFMPEG |
|---|
| 331 | int ret = av_frame_make_writable(frame); |
|---|
| 332 | if (ret < 0) { |
|---|
| 333 | averrno = ret; |
|---|
| 334 | return false; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | enum AVPixelFormat pix_fmt = context->pix_fmt; |
|---|
| 338 | |
|---|
| 339 | if (pix_fmt != AV_PIX_FMT_YUV420P) { |
|---|
| 340 | // FIXME convert... |
|---|
| 341 | abort(); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | int len = 3 * GetWidth(); |
|---|
| 345 | { |
|---|
| 346 | // Flip image vertically |
|---|
| 347 | int h = GetHeight(); |
|---|
| 348 | unsigned char * src = pixels + h * len; |
|---|
| 349 | unsigned char * dest = src - len; |
|---|
| 350 | while (h--) { |
|---|
| 351 | memcpy(dest, src, len); |
|---|
| 352 | src += len; |
|---|
| 353 | dest -= len; |
|---|
| 354 | } |
|---|
| 355 | } |
|---|
| 356 | sws_scale(sws_ctx, &pixels, &len, 0, GetHeight(), |
|---|
| 357 | frame->data, frame->linesize); |
|---|
| 358 | |
|---|
| 359 | ++frame->pts; |
|---|
| 360 | |
|---|
| 361 | // Encode this frame. |
|---|
| 362 | ret = encode_frame(frame); |
|---|
| 363 | if (ret < 0) { |
|---|
| 364 | averrno = ret; |
|---|
| 365 | return false; |
|---|
| 366 | } |
|---|
| 367 | #endif |
|---|
| 368 | return true; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | bool |
|---|
| 372 | MovieMaker::Close() |
|---|
| 373 | { |
|---|
| 374 | #ifdef WITH_FFMPEG |
|---|
| 375 | if (video_st && averrno == 0) { |
|---|
| 376 | // Flush out any remaining data. |
|---|
| 377 | int ret = encode_frame(NULL); |
|---|
| 378 | if (ret < 0) { |
|---|
| 379 | averrno = ret; |
|---|
| 380 | return false; |
|---|
| 381 | } |
|---|
| 382 | av_write_trailer(oc); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | release(); |
|---|
| 386 | #endif |
|---|
| 387 | return true; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | #ifdef WITH_FFMPEG |
|---|
| 391 | void |
|---|
| 392 | MovieMaker::release() |
|---|
| 393 | { |
|---|
| 394 | // Close codec. |
|---|
| 395 | avcodec_free_context(&context); |
|---|
| 396 | av_frame_free(&frame); |
|---|
| 397 | av_free(pixels); |
|---|
| 398 | pixels = NULL; |
|---|
| 399 | sws_freeContext(sws_ctx); |
|---|
| 400 | sws_ctx = NULL; |
|---|
| 401 | |
|---|
| 402 | // Free the stream. |
|---|
| 403 | avformat_free_context(oc); |
|---|
| 404 | oc = NULL; |
|---|
| 405 | |
|---|
| 406 | if (fh_to_close) { |
|---|
| 407 | fclose(fh_to_close); |
|---|
| 408 | fh_to_close = NULL; |
|---|
| 409 | } |
|---|
| 410 | } |
|---|
| 411 | #endif |
|---|
| 412 | |
|---|
| 413 | MovieMaker::~MovieMaker() |
|---|
| 414 | { |
|---|
| 415 | #ifdef WITH_FFMPEG |
|---|
| 416 | release(); |
|---|
| 417 | #endif |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | const char * |
|---|
| 421 | MovieMaker::get_error_string() const |
|---|
| 422 | { |
|---|
| 423 | #ifdef WITH_FFMPEG |
|---|
| 424 | switch (averrno) { |
|---|
| 425 | case AVERROR(EIO): |
|---|
| 426 | return "I/O error"; |
|---|
| 427 | case AVERROR(EDOM): |
|---|
| 428 | return "Number syntax expected in filename"; |
|---|
| 429 | case AVERROR_INVALIDDATA: |
|---|
| 430 | /* same as AVERROR_UNKNOWN: return "unknown error"; */ |
|---|
| 431 | return "invalid data found"; |
|---|
| 432 | case AVERROR(ENOMEM): |
|---|
| 433 | return "not enough memory"; |
|---|
| 434 | case AVERROR(EILSEQ): |
|---|
| 435 | return "unknown format"; |
|---|
| 436 | case AVERROR(ENOSYS): |
|---|
| 437 | return "Operation not supported"; |
|---|
| 438 | case AVERROR(ENOENT): |
|---|
| 439 | return "No such file or directory"; |
|---|
| 440 | case AVERROR_EOF: |
|---|
| 441 | return "End of file"; |
|---|
| 442 | case AVERROR_PATCHWELCOME: |
|---|
| 443 | return "Not implemented in FFmpeg"; |
|---|
| 444 | case 0: |
|---|
| 445 | return "No error"; |
|---|
| 446 | case MOVIE_NO_SUITABLE_FORMAT: |
|---|
| 447 | return "Couldn't find a suitable output format"; |
|---|
| 448 | case MOVIE_AUDIO_ONLY: |
|---|
| 449 | return "Audio-only format specified"; |
|---|
| 450 | case MOVIE_FILENAME_TOO_LONG: |
|---|
| 451 | return "Filename too long"; |
|---|
| 452 | } |
|---|
| 453 | #endif |
|---|
| 454 | return "Unknown error"; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | #else |
|---|
| 458 | |
|---|
| 459 | #include "moviemaker-legacy.cc" |
|---|
| 460 | |
|---|
| 461 | #endif |
|---|