[6a4cdcb6] | 1 | // |
---|
| 2 | // moviemaker.cc |
---|
| 3 | // |
---|
| 4 | // Class for writing movies from Aven. |
---|
| 5 | // |
---|
[b282f44] | 6 | // Copyright (C) 2004,2011,2012,2013,2014,2015,2016 Olly Betts |
---|
[6a4cdcb6] | 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 |
---|
[ecbc6c18] | 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[6a4cdcb6] | 21 | // |
---|
| 22 | |
---|
[cc9e7a06] | 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 | |
---|
[6a4cdcb6] | 49 | #ifdef HAVE_CONFIG_H |
---|
| 50 | #include <config.h> |
---|
| 51 | #endif |
---|
| 52 | |
---|
[cc9e7a06] | 53 | #define __STDC_CONSTANT_MACROS |
---|
| 54 | |
---|
[1805274] | 55 | #include <assert.h> |
---|
[6a4cdcb6] | 56 | #include <stdlib.h> |
---|
| 57 | #include <string.h> |
---|
| 58 | |
---|
| 59 | #include "moviemaker.h" |
---|
| 60 | |
---|
[f678705] | 61 | #ifdef WITH_LIBAV |
---|
[cc9e7a06] | 62 | extern "C" { |
---|
[b282f44] | 63 | # include <libavutil/imgutils.h> |
---|
[008f2f3] | 64 | # include <libavutil/mathematics.h> |
---|
[64d06c0] | 65 | # include <libavformat/avformat.h> |
---|
[86d8ee5] | 66 | # include <libswscale/swscale.h> |
---|
[cc9e7a06] | 67 | } |
---|
[7831cef] | 68 | # ifndef AV_PKT_FLAG_KEY |
---|
| 69 | # define AV_PKT_FLAG_KEY PKT_FLAG_KEY |
---|
| 70 | # endif |
---|
| 71 | # ifndef HAVE_AV_GUESS_FORMAT |
---|
| 72 | # define av_guess_format guess_format |
---|
| 73 | # endif |
---|
| 74 | # ifndef HAVE_AVIO_OPEN |
---|
| 75 | # define avio_open url_fopen |
---|
| 76 | # endif |
---|
| 77 | # ifndef HAVE_AVIO_CLOSE |
---|
| 78 | # define avio_close url_fclose |
---|
| 79 | # endif |
---|
[8364c65f] | 80 | # ifndef HAVE_AV_FRAME_ALLOC |
---|
| 81 | static inline AVFrame * av_frame_alloc() { |
---|
| 82 | return avcodec_alloc_frame(); |
---|
| 83 | } |
---|
| 84 | # endif |
---|
| 85 | # ifndef HAVE_AV_FRAME_FREE |
---|
| 86 | # ifdef HAVE_AVCODEC_FREE_FRAME |
---|
| 87 | static inline void av_frame_free(AVFrame ** frame) { |
---|
| 88 | avcodec_free_frame(frame); |
---|
| 89 | } |
---|
| 90 | # else |
---|
| 91 | static inline void av_frame_free(AVFrame ** frame) { |
---|
[ec81f086] | 92 | free((*frame)->data[0]); |
---|
| 93 | free(*frame); |
---|
[e9ae5837] | 94 | *frame = NULL; |
---|
[cc69cf5] | 95 | } |
---|
[8364c65f] | 96 | # endif |
---|
[cc69cf5] | 97 | # endif |
---|
[86d8ee5] | 98 | # ifndef HAVE_AVCODEC_OPEN2 |
---|
| 99 | // We always pass NULL for OPTS below. |
---|
| 100 | # define avcodec_open2(CTX, CODEC, OPTS) avcodec_open(CTX, CODEC) |
---|
| 101 | # endif |
---|
| 102 | # ifndef HAVE_AVFORMAT_NEW_STREAM |
---|
| 103 | // We always pass NULL for CODEC below. |
---|
| 104 | # define avformat_new_stream(S, CODEC) av_new_stream(S, 0) |
---|
| 105 | # endif |
---|
[7831cef] | 106 | # if !HAVE_DECL_AVMEDIA_TYPE_VIDEO |
---|
| 107 | # define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO |
---|
| 108 | # endif |
---|
[8364c65f] | 109 | # if !HAVE_DECL_AV_CODEC_ID_NONE |
---|
| 110 | # define AV_CODEC_ID_NONE CODEC_ID_NONE |
---|
[64d06c0] | 111 | # endif |
---|
[ad04c87] | 112 | # if !HAVE_DECL_AV_PIX_FMT_RGB24 |
---|
| 113 | # define AV_PIX_FMT_RGB24 PIX_FMT_RGB24 |
---|
| 114 | # endif |
---|
[64d06c0] | 115 | # if !HAVE_DECL_AV_PIX_FMT_YUV420P |
---|
| 116 | # define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P |
---|
| 117 | # endif |
---|
| 118 | # ifndef AVIO_FLAG_WRITE |
---|
| 119 | # define AVIO_FLAG_WRITE URL_WRONLY |
---|
| 120 | # endif |
---|
[6a4cdcb6] | 121 | |
---|
[091069f] | 122 | enum { |
---|
| 123 | MOVIE_NO_SUITABLE_FORMAT = 1, |
---|
| 124 | MOVIE_AUDIO_ONLY, |
---|
[c648bd1] | 125 | MOVIE_FILENAME_TOO_LONG |
---|
[091069f] | 126 | }; |
---|
| 127 | |
---|
[bc6faa9] | 128 | # ifndef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
[1805274] | 129 | const int OUTBUF_SIZE = 200000; |
---|
[bc6faa9] | 130 | # endif |
---|
| 131 | #endif |
---|
[6a4cdcb6] | 132 | |
---|
[1805274] | 133 | MovieMaker::MovieMaker() |
---|
[f678705] | 134 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 135 | : oc(0), video_st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0), averrno(0) |
---|
[ccb83b7] | 136 | #endif |
---|
[6a4cdcb6] | 137 | { |
---|
[f678705] | 138 | #ifdef WITH_LIBAV |
---|
[6a4cdcb6] | 139 | static bool initialised_ffmpeg = false; |
---|
[1805274] | 140 | if (initialised_ffmpeg) return; |
---|
[6a4cdcb6] | 141 | |
---|
[1805274] | 142 | // FIXME: register only the codec(s) we want to use... |
---|
[64d06c0] | 143 | avcodec_register_all(); |
---|
[1805274] | 144 | av_register_all(); |
---|
[6a4cdcb6] | 145 | |
---|
[1805274] | 146 | initialised_ffmpeg = true; |
---|
| 147 | #endif |
---|
| 148 | } |
---|
| 149 | |
---|
[0db0f91] | 150 | #ifdef WITH_LIBAV |
---|
[f4e4b56] | 151 | static int |
---|
| 152 | write_packet(void *opaque, uint8_t *buf, int buf_size) { |
---|
| 153 | FILE * fh = (FILE*)opaque; |
---|
| 154 | size_t res = fwrite(buf, 1, buf_size, fh); |
---|
| 155 | return res > 0 ? res : -1; |
---|
| 156 | } |
---|
[0db0f91] | 157 | #endif |
---|
[f4e4b56] | 158 | |
---|
| 159 | #define MAX_EXTENSION_LEN 8 |
---|
| 160 | |
---|
| 161 | bool MovieMaker::Open(FILE* fh, const char * ext, int width, int height) |
---|
[1805274] | 162 | { |
---|
[f678705] | 163 | #ifdef WITH_LIBAV |
---|
[f4e4b56] | 164 | fh_to_close = fh; |
---|
| 165 | |
---|
| 166 | AVOutputFormat * fmt = NULL; |
---|
| 167 | char dummy_filename[MAX_EXTENSION_LEN + 3] = "x."; |
---|
| 168 | if (strlen(ext) <= MAX_EXTENSION_LEN) { |
---|
| 169 | strcpy(dummy_filename + 2, ext); |
---|
| 170 | // Pass "x." + extension to av_guess_format() to avoid having to deal |
---|
| 171 | // with wide character filenames. |
---|
| 172 | fmt = av_guess_format(NULL, dummy_filename, NULL); |
---|
| 173 | } |
---|
[1805274] | 174 | if (!fmt) { |
---|
| 175 | // We couldn't deduce the output format from file extension so default |
---|
| 176 | // to MPEG. |
---|
[7831cef] | 177 | fmt = av_guess_format("mpeg", NULL, NULL); |
---|
[1805274] | 178 | if (!fmt) { |
---|
[091069f] | 179 | averrno = MOVIE_NO_SUITABLE_FORMAT; |
---|
[1805274] | 180 | return false; |
---|
| 181 | } |
---|
[f4e4b56] | 182 | strcpy(dummy_filename + 2, "mpg"); |
---|
[1805274] | 183 | } |
---|
[8364c65f] | 184 | if (fmt->video_codec == AV_CODEC_ID_NONE) { |
---|
[091069f] | 185 | averrno = MOVIE_AUDIO_ONLY; |
---|
[1805274] | 186 | return false; |
---|
[6a4cdcb6] | 187 | } |
---|
| 188 | |
---|
[64d06c0] | 189 | /* Allocate the output media context. */ |
---|
[6ed625e] | 190 | oc = avformat_alloc_context(); |
---|
[1805274] | 191 | if (!oc) { |
---|
[63621a7] | 192 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 193 | return false; |
---|
[6a4cdcb6] | 194 | } |
---|
[1805274] | 195 | oc->oformat = fmt; |
---|
[f4e4b56] | 196 | strcpy(oc->filename, dummy_filename); |
---|
[6a4cdcb6] | 197 | |
---|
[64d06c0] | 198 | /* find the video encoder */ |
---|
| 199 | AVCodec *codec = avcodec_find_encoder(fmt->video_codec); |
---|
| 200 | if (!codec) { |
---|
| 201 | // FIXME : Erm - internal ffmpeg library problem? |
---|
[63621a7] | 202 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 203 | return false; |
---|
[6a4cdcb6] | 204 | } |
---|
| 205 | |
---|
[64d06c0] | 206 | // Add the video stream. |
---|
| 207 | video_st = avformat_new_stream(oc, codec); |
---|
| 208 | if (!video_st) { |
---|
| 209 | averrno = AVERROR(ENOMEM); |
---|
| 210 | return false; |
---|
| 211 | } |
---|
[6a4cdcb6] | 212 | |
---|
| 213 | // Set sample parameters. |
---|
[64d06c0] | 214 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 215 | c->bit_rate = 400000; |
---|
[64d06c0] | 216 | /* Resolution must be a multiple of two. */ |
---|
[1805274] | 217 | c->width = width; |
---|
| 218 | c->height = height; |
---|
[64d06c0] | 219 | /* timebase: This is the fundamental unit of time (in seconds) in terms |
---|
| 220 | * of which frame timestamps are represented. For fixed-fps content, |
---|
| 221 | * timebase should be 1/framerate and timestamp increments should be |
---|
| 222 | * identical to 1. */ |
---|
[5270758] | 223 | #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(55, 44, 0) |
---|
| 224 | // Old way, which now causes deprecation warnings. |
---|
[cc9e7a06] | 225 | c->time_base.den = 25; // Frames per second. |
---|
[64d06c0] | 226 | c->time_base.num = 1; |
---|
[5270758] | 227 | #else |
---|
| 228 | video_st->time_base.den = 25; // Frames per second. |
---|
| 229 | video_st->time_base.num = 1; |
---|
| 230 | c->time_base = video_st->time_base; |
---|
| 231 | #endif |
---|
[64d06c0] | 232 | c->gop_size = 12; /* emit one intra frame every twelve frames at most */ |
---|
| 233 | c->pix_fmt = AV_PIX_FMT_YUV420P; |
---|
[997509d] | 234 | c->rc_buffer_size = c->bit_rate * 4; // Enough for 4 seconds |
---|
[5270758] | 235 | c->rc_max_rate = c->bit_rate * 2; |
---|
[1805274] | 236 | // B frames are backwards predicted - they can improve compression, |
---|
| 237 | // but may slow encoding and decoding. |
---|
[64d06c0] | 238 | // if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
---|
| 239 | // c->max_b_frames = 2; |
---|
| 240 | // } |
---|
[6a4cdcb6] | 241 | |
---|
[64d06c0] | 242 | /* Some formats want stream headers to be separate. */ |
---|
[cc9e7a06] | 243 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
---|
| 244 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
---|
| 245 | |
---|
[7831cef] | 246 | int retval; |
---|
| 247 | #ifndef HAVE_AVFORMAT_WRITE_HEADER |
---|
[1805274] | 248 | // Set the output parameters (must be done even if no parameters). |
---|
[7831cef] | 249 | retval = av_set_parameters(oc, NULL); |
---|
[091069f] | 250 | if (retval < 0) { |
---|
| 251 | averrno = retval; |
---|
[1805274] | 252 | return false; |
---|
| 253 | } |
---|
[7831cef] | 254 | #endif |
---|
[1805274] | 255 | |
---|
[64d06c0] | 256 | retval = avcodec_open2(c, NULL, NULL); |
---|
[091069f] | 257 | if (retval < 0) { |
---|
| 258 | averrno = retval; |
---|
[1805274] | 259 | return false; |
---|
[6a4cdcb6] | 260 | } |
---|
[1805274] | 261 | |
---|
[64d06c0] | 262 | #ifndef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
| 263 | outbuf = NULL; |
---|
| 264 | if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) { |
---|
[d13aea6] | 265 | outbuf = (unsigned char *)av_malloc(OUTBUF_SIZE); |
---|
[cc9e7a06] | 266 | if (!outbuf) { |
---|
[63621a7] | 267 | averrno = AVERROR(ENOMEM); |
---|
[cc9e7a06] | 268 | return false; |
---|
| 269 | } |
---|
[6a4cdcb6] | 270 | } |
---|
[64d06c0] | 271 | #endif |
---|
[1805274] | 272 | |
---|
[64d06c0] | 273 | /* Allocate the encoded raw picture. */ |
---|
[8364c65f] | 274 | frame = av_frame_alloc(); |
---|
[cc9e7a06] | 275 | if (!frame) { |
---|
[63621a7] | 276 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 277 | return false; |
---|
[6a4cdcb6] | 278 | } |
---|
[b282f44] | 279 | retval = av_image_alloc(frame->data, frame->linesize, |
---|
| 280 | c->width, c->height, c->pix_fmt, 1); |
---|
[64d06c0] | 281 | if (retval < 0) { |
---|
| 282 | averrno = retval; |
---|
[1805274] | 283 | return false; |
---|
[6a4cdcb6] | 284 | } |
---|
[cc9e7a06] | 285 | |
---|
[8364c65f] | 286 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) { |
---|
[cc9e7a06] | 287 | // FIXME need to allocate another frame for this case if we stop |
---|
[8364c65f] | 288 | // hardcoding AV_PIX_FMT_YUV420P. |
---|
[cc9e7a06] | 289 | abort(); |
---|
| 290 | } |
---|
[6a4cdcb6] | 291 | |
---|
[5270758] | 292 | frame->format = c->pix_fmt; |
---|
| 293 | frame->width = c->width; |
---|
| 294 | frame->height = c->height; |
---|
| 295 | |
---|
[d13aea6] | 296 | pixels = (unsigned char *)av_malloc(width * height * 6); |
---|
[6a4cdcb6] | 297 | if (!pixels) { |
---|
[63621a7] | 298 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 299 | return false; |
---|
[6a4cdcb6] | 300 | } |
---|
[1805274] | 301 | |
---|
[64d06c0] | 302 | // Show the format we've ended up with (for debug purposes). |
---|
| 303 | // av_dump_format(oc, 0, fnm, 1); |
---|
| 304 | |
---|
| 305 | av_free(sws_ctx); |
---|
[ad04c87] | 306 | sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24, |
---|
[64d06c0] | 307 | width, height, c->pix_fmt, SWS_BICUBIC, |
---|
| 308 | NULL, NULL, NULL); |
---|
| 309 | if (sws_ctx == NULL) { |
---|
| 310 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
---|
| 311 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 312 | return false; |
---|
| 313 | } |
---|
| 314 | |
---|
[64d06c0] | 315 | if (!(fmt->flags & AVFMT_NOFILE)) { |
---|
[f4e4b56] | 316 | const int buf_size = 8192; |
---|
| 317 | void * buf = av_malloc(buf_size); |
---|
| 318 | oc->pb = avio_alloc_context(static_cast<uint8_t*>(buf), buf_size, 1, |
---|
| 319 | fh, NULL, write_packet, NULL); |
---|
| 320 | if (!oc->pb) { |
---|
| 321 | averrno = AVERROR(ENOMEM); |
---|
[64d06c0] | 322 | return false; |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
[1805274] | 326 | // Write the stream header, if any. |
---|
[7831cef] | 327 | #ifdef HAVE_AVFORMAT_WRITE_HEADER |
---|
| 328 | retval = avformat_write_header(oc, NULL); |
---|
| 329 | #else |
---|
[091069f] | 330 | retval = av_write_header(oc); |
---|
[7831cef] | 331 | #endif |
---|
[091069f] | 332 | if (retval < 0) { |
---|
| 333 | averrno = retval; |
---|
[1805274] | 334 | return false; |
---|
| 335 | } |
---|
[9e516d0d] | 336 | |
---|
[091069f] | 337 | averrno = 0; |
---|
[1805274] | 338 | return true; |
---|
[6a4cdcb6] | 339 | #else |
---|
[f4e4b56] | 340 | (void)fh; |
---|
| 341 | (void)ext; |
---|
[6e22f11] | 342 | (void)width; |
---|
| 343 | (void)height; |
---|
[6a4cdcb6] | 344 | return false; |
---|
| 345 | #endif |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | unsigned char * MovieMaker::GetBuffer() const { |
---|
[f678705] | 349 | #ifdef WITH_LIBAV |
---|
[cd04101] | 350 | return pixels + GetWidth() * GetHeight() * 3; |
---|
[eac4514] | 351 | #else |
---|
| 352 | return NULL; |
---|
| 353 | #endif |
---|
[6a4cdcb6] | 354 | } |
---|
| 355 | |
---|
| 356 | int MovieMaker::GetWidth() const { |
---|
[f678705] | 357 | #ifdef WITH_LIBAV |
---|
[ccb83b7] | 358 | assert(video_st); |
---|
[64d06c0] | 359 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 360 | return c->width; |
---|
[1805274] | 361 | #else |
---|
| 362 | return 0; |
---|
| 363 | #endif |
---|
[6a4cdcb6] | 364 | } |
---|
| 365 | |
---|
| 366 | int MovieMaker::GetHeight() const { |
---|
[f678705] | 367 | #ifdef WITH_LIBAV |
---|
[ccb83b7] | 368 | assert(video_st); |
---|
[64d06c0] | 369 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 370 | return c->height; |
---|
[1805274] | 371 | #else |
---|
| 372 | return 0; |
---|
| 373 | #endif |
---|
[6a4cdcb6] | 374 | } |
---|
| 375 | |
---|
[98fd937] | 376 | bool MovieMaker::AddFrame() |
---|
[6a4cdcb6] | 377 | { |
---|
[f678705] | 378 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 379 | AVCodecContext * c = video_st->codec; |
---|
[cc9e7a06] | 380 | |
---|
[8364c65f] | 381 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) { |
---|
[cc9e7a06] | 382 | // FIXME convert... |
---|
| 383 | abort(); |
---|
| 384 | } |
---|
[1805274] | 385 | |
---|
[9e516d0d] | 386 | int len = 3 * c->width; |
---|
[fed3713] | 387 | { |
---|
| 388 | // Flip image vertically |
---|
| 389 | int h = c->height; |
---|
| 390 | unsigned char * src = pixels + h * len; |
---|
| 391 | unsigned char * dest = src - len; |
---|
| 392 | while (h--) { |
---|
| 393 | memcpy(dest, src, len); |
---|
| 394 | src += len; |
---|
| 395 | dest -= len; |
---|
| 396 | } |
---|
[6a4cdcb6] | 397 | } |
---|
[9e516d0d] | 398 | sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); |
---|
[6a4cdcb6] | 399 | |
---|
[cc9e7a06] | 400 | if (oc->oformat->flags & AVFMT_RAWPICTURE) { |
---|
| 401 | abort(); |
---|
| 402 | } |
---|
[6a4cdcb6] | 403 | |
---|
[1805274] | 404 | // Encode this frame. |
---|
[64d06c0] | 405 | #ifdef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
[710bd97] | 406 | AVPacket pkt; |
---|
[64d06c0] | 407 | int got_packet; |
---|
| 408 | av_init_packet(&pkt); |
---|
[710bd97] | 409 | pkt.data = NULL; |
---|
[64d06c0] | 410 | |
---|
| 411 | int ret = avcodec_encode_video2(c, &pkt, frame, &got_packet); |
---|
| 412 | if (ret < 0) { |
---|
[98fd937] | 413 | averrno = ret; |
---|
| 414 | return false; |
---|
[64d06c0] | 415 | } |
---|
| 416 | if (got_packet && pkt.size) { |
---|
| 417 | // Write the compressed frame to the media file. |
---|
[710bd97] | 418 | if (pkt.pts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 419 | pkt.pts = av_rescale_q(pkt.pts, |
---|
| 420 | c->time_base, video_st->time_base); |
---|
| 421 | } |
---|
[710bd97] | 422 | if (pkt.dts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 423 | pkt.dts = av_rescale_q(pkt.dts, |
---|
| 424 | c->time_base, video_st->time_base); |
---|
| 425 | } |
---|
| 426 | pkt.stream_index = video_st->index; |
---|
| 427 | |
---|
| 428 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 429 | ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 430 | if (ret < 0) { |
---|
| 431 | averrno = ret; |
---|
| 432 | return false; |
---|
[64d06c0] | 433 | } |
---|
| 434 | } |
---|
| 435 | #else |
---|
[6a4cdcb6] | 436 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
---|
[1805274] | 437 | // outsize == 0 means that this frame has been buffered, so there's nothing |
---|
| 438 | // to write yet. |
---|
[cc9e7a06] | 439 | if (out_size) { |
---|
[1805274] | 440 | // Write the compressed frame to the media file. |
---|
[cc9e7a06] | 441 | AVPacket pkt; |
---|
| 442 | av_init_packet(&pkt); |
---|
| 443 | |
---|
[63621a7] | 444 | if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE) |
---|
[64d06c0] | 445 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); |
---|
[cc9e7a06] | 446 | if (c->coded_frame->key_frame) |
---|
| 447 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
[64d06c0] | 448 | pkt.stream_index = video_st->index; |
---|
[cc9e7a06] | 449 | pkt.data = outbuf; |
---|
| 450 | pkt.size = out_size; |
---|
| 451 | |
---|
[64d06c0] | 452 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 453 | int ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 454 | if (ret < 0) { |
---|
| 455 | averrno = ret; |
---|
| 456 | return false; |
---|
[1805274] | 457 | } |
---|
[6a4cdcb6] | 458 | } |
---|
| 459 | #endif |
---|
[64d06c0] | 460 | #endif |
---|
[98fd937] | 461 | return true; |
---|
[6a4cdcb6] | 462 | } |
---|
| 463 | |
---|
[98fd937] | 464 | bool |
---|
| 465 | MovieMaker::Close() |
---|
[6a4cdcb6] | 466 | { |
---|
[f678705] | 467 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 468 | if (video_st && averrno == 0) { |
---|
[1805274] | 469 | // No more frames to compress. The codec may have a few frames |
---|
| 470 | // buffered if we're using B frames, so write those too. |
---|
[64d06c0] | 471 | AVCodecContext * c = video_st->codec; |
---|
[1805274] | 472 | |
---|
[64d06c0] | 473 | #ifdef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
| 474 | while (1) { |
---|
[710bd97] | 475 | AVPacket pkt; |
---|
[64d06c0] | 476 | int got_packet; |
---|
| 477 | av_init_packet(&pkt); |
---|
[710bd97] | 478 | pkt.data = NULL; |
---|
[5270758] | 479 | pkt.size = 0; |
---|
[64d06c0] | 480 | |
---|
| 481 | int ret = avcodec_encode_video2(c, &pkt, NULL, &got_packet); |
---|
| 482 | if (ret < 0) { |
---|
[98fd937] | 483 | release(); |
---|
| 484 | averrno = ret; |
---|
| 485 | return false; |
---|
[64d06c0] | 486 | } |
---|
| 487 | if (!got_packet) break; |
---|
| 488 | if (!pkt.size) continue; |
---|
| 489 | |
---|
| 490 | // Write the compressed frame to the media file. |
---|
[710bd97] | 491 | if (pkt.pts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 492 | pkt.pts = av_rescale_q(pkt.pts, |
---|
| 493 | c->time_base, video_st->time_base); |
---|
| 494 | } |
---|
[710bd97] | 495 | if (pkt.dts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 496 | pkt.dts = av_rescale_q(pkt.dts, |
---|
| 497 | c->time_base, video_st->time_base); |
---|
| 498 | } |
---|
| 499 | pkt.stream_index = video_st->index; |
---|
| 500 | |
---|
| 501 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 502 | ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 503 | if (ret < 0) { |
---|
| 504 | release(); |
---|
| 505 | averrno = ret; |
---|
| 506 | return false; |
---|
[64d06c0] | 507 | } |
---|
| 508 | } |
---|
| 509 | #else |
---|
[1805274] | 510 | while (out_size) { |
---|
| 511 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
---|
| 512 | if (out_size) { |
---|
[cc9e7a06] | 513 | // Write the compressed frame to the media file. |
---|
| 514 | AVPacket pkt; |
---|
| 515 | av_init_packet(&pkt); |
---|
| 516 | |
---|
[63621a7] | 517 | if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE) |
---|
[64d06c0] | 518 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); |
---|
[cc9e7a06] | 519 | if (c->coded_frame->key_frame) |
---|
| 520 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
[64d06c0] | 521 | pkt.stream_index = video_st->index; |
---|
[cc9e7a06] | 522 | pkt.data = outbuf; |
---|
| 523 | pkt.size = out_size; |
---|
| 524 | |
---|
| 525 | /* write the compressed frame in the media file */ |
---|
[98fd937] | 526 | int ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 527 | if (ret < 0) { |
---|
| 528 | release(); |
---|
| 529 | averrno = ret; |
---|
| 530 | return false; |
---|
[1805274] | 531 | } |
---|
| 532 | } |
---|
[6a4cdcb6] | 533 | } |
---|
[64d06c0] | 534 | #endif |
---|
[6a4cdcb6] | 535 | |
---|
[cc9e7a06] | 536 | av_write_trailer(oc); |
---|
[091069f] | 537 | } |
---|
[cc9e7a06] | 538 | |
---|
[98fd937] | 539 | release(); |
---|
| 540 | #endif |
---|
| 541 | return true; |
---|
| 542 | } |
---|
| 543 | |
---|
[f678705] | 544 | #ifdef WITH_LIBAV |
---|
[98fd937] | 545 | void |
---|
| 546 | MovieMaker::release() |
---|
| 547 | { |
---|
[64d06c0] | 548 | if (video_st) { |
---|
[1805274] | 549 | // Close codec. |
---|
[64d06c0] | 550 | avcodec_close(video_st->codec); |
---|
[98fd937] | 551 | video_st = NULL; |
---|
[6a4cdcb6] | 552 | } |
---|
| 553 | |
---|
[cc9e7a06] | 554 | if (frame) { |
---|
[8364c65f] | 555 | av_frame_free(&frame); |
---|
[cc9e7a06] | 556 | } |
---|
[d13aea6] | 557 | av_free(pixels); |
---|
[98fd937] | 558 | pixels = NULL; |
---|
[d13aea6] | 559 | av_free(outbuf); |
---|
[98fd937] | 560 | outbuf = NULL; |
---|
[9e516d0d] | 561 | av_free(sws_ctx); |
---|
[98fd937] | 562 | sws_ctx = NULL; |
---|
[1805274] | 563 | |
---|
| 564 | if (oc) { |
---|
| 565 | // Free the streams. |
---|
[cc9e7a06] | 566 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
---|
| 567 | av_freep(&oc->streams[i]->codec); |
---|
[1805274] | 568 | av_freep(&oc->streams[i]); |
---|
| 569 | } |
---|
| 570 | |
---|
[cc9e7a06] | 571 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
---|
[f4e4b56] | 572 | // Release the AVIOContext. |
---|
| 573 | av_free(oc->pb); |
---|
[cc9e7a06] | 574 | } |
---|
[1805274] | 575 | |
---|
| 576 | // Free the stream. |
---|
[6ed625e] | 577 | av_free(oc); |
---|
[98fd937] | 578 | oc = NULL; |
---|
[1805274] | 579 | } |
---|
[f4e4b56] | 580 | if (fh_to_close) { |
---|
| 581 | fclose(fh_to_close); |
---|
| 582 | fh_to_close = NULL; |
---|
| 583 | } |
---|
[6a4cdcb6] | 584 | } |
---|
[c648bd1] | 585 | #endif |
---|
[091069f] | 586 | |
---|
[98fd937] | 587 | MovieMaker::~MovieMaker() |
---|
| 588 | { |
---|
[f678705] | 589 | #ifdef WITH_LIBAV |
---|
[98fd937] | 590 | release(); |
---|
[c648bd1] | 591 | #endif |
---|
[98fd937] | 592 | } |
---|
| 593 | |
---|
[091069f] | 594 | const char * |
---|
| 595 | MovieMaker::get_error_string() const |
---|
| 596 | { |
---|
[f678705] | 597 | #ifdef WITH_LIBAV |
---|
[091069f] | 598 | switch (averrno) { |
---|
[63621a7] | 599 | case AVERROR(EIO): |
---|
[091069f] | 600 | return "I/O error"; |
---|
[63621a7] | 601 | case AVERROR(EDOM): |
---|
[091069f] | 602 | return "Number syntax expected in filename"; |
---|
| 603 | case AVERROR_INVALIDDATA: |
---|
| 604 | /* same as AVERROR_UNKNOWN: return "unknown error"; */ |
---|
| 605 | return "invalid data found"; |
---|
[63621a7] | 606 | case AVERROR(ENOMEM): |
---|
[091069f] | 607 | return "not enough memory"; |
---|
[63621a7] | 608 | case AVERROR(EILSEQ): |
---|
[091069f] | 609 | return "unknown format"; |
---|
[63621a7] | 610 | case AVERROR(ENOSYS): |
---|
[091069f] | 611 | return "Operation not supported"; |
---|
[63621a7] | 612 | case AVERROR(ENOENT): |
---|
[091069f] | 613 | return "No such file or directory"; |
---|
| 614 | case AVERROR_EOF: |
---|
| 615 | return "End of file"; |
---|
| 616 | case AVERROR_PATCHWELCOME: |
---|
| 617 | return "Not implemented in FFmpeg"; |
---|
| 618 | case 0: |
---|
| 619 | return "No error"; |
---|
| 620 | case MOVIE_NO_SUITABLE_FORMAT: |
---|
| 621 | return "Couldn't find a suitable output format"; |
---|
| 622 | case MOVIE_AUDIO_ONLY: |
---|
| 623 | return "Audio-only format specified"; |
---|
| 624 | case MOVIE_FILENAME_TOO_LONG: |
---|
| 625 | return "Filename too long"; |
---|
| 626 | } |
---|
[eac4514] | 627 | #endif |
---|
[f4e4b56] | 628 | return "Unknown error"; |
---|
[091069f] | 629 | } |
---|