[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 | |
---|
| 150 | bool MovieMaker::Open(const char *fnm, int width, int height) |
---|
| 151 | { |
---|
[f678705] | 152 | #ifdef WITH_LIBAV |
---|
[7831cef] | 153 | AVOutputFormat * fmt = av_guess_format(NULL, fnm, NULL); |
---|
[1805274] | 154 | if (!fmt) { |
---|
| 155 | // We couldn't deduce the output format from file extension so default |
---|
| 156 | // to MPEG. |
---|
[7831cef] | 157 | fmt = av_guess_format("mpeg", NULL, NULL); |
---|
[1805274] | 158 | if (!fmt) { |
---|
[091069f] | 159 | averrno = MOVIE_NO_SUITABLE_FORMAT; |
---|
[1805274] | 160 | return false; |
---|
| 161 | } |
---|
| 162 | } |
---|
[8364c65f] | 163 | if (fmt->video_codec == AV_CODEC_ID_NONE) { |
---|
[091069f] | 164 | averrno = MOVIE_AUDIO_ONLY; |
---|
[1805274] | 165 | return false; |
---|
[6a4cdcb6] | 166 | } |
---|
| 167 | |
---|
[64d06c0] | 168 | /* Allocate the output media context. */ |
---|
[6ed625e] | 169 | oc = avformat_alloc_context(); |
---|
[1805274] | 170 | if (!oc) { |
---|
[63621a7] | 171 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 172 | return false; |
---|
[6a4cdcb6] | 173 | } |
---|
[1805274] | 174 | oc->oformat = fmt; |
---|
| 175 | if (strlen(fnm) >= sizeof(oc->filename)) { |
---|
[091069f] | 176 | averrno = MOVIE_FILENAME_TOO_LONG; |
---|
[1805274] | 177 | return false; |
---|
| 178 | } |
---|
| 179 | strcpy(oc->filename, fnm); |
---|
[6a4cdcb6] | 180 | |
---|
[64d06c0] | 181 | /* find the video encoder */ |
---|
| 182 | AVCodec *codec = avcodec_find_encoder(fmt->video_codec); |
---|
| 183 | if (!codec) { |
---|
| 184 | // FIXME : Erm - internal ffmpeg library problem? |
---|
[63621a7] | 185 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 186 | return false; |
---|
[6a4cdcb6] | 187 | } |
---|
| 188 | |
---|
[64d06c0] | 189 | // Add the video stream. |
---|
| 190 | video_st = avformat_new_stream(oc, codec); |
---|
| 191 | if (!video_st) { |
---|
| 192 | averrno = AVERROR(ENOMEM); |
---|
| 193 | return false; |
---|
| 194 | } |
---|
[6a4cdcb6] | 195 | |
---|
| 196 | // Set sample parameters. |
---|
[64d06c0] | 197 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 198 | c->bit_rate = 400000; |
---|
[64d06c0] | 199 | /* Resolution must be a multiple of two. */ |
---|
[1805274] | 200 | c->width = width; |
---|
| 201 | c->height = height; |
---|
[64d06c0] | 202 | /* timebase: This is the fundamental unit of time (in seconds) in terms |
---|
| 203 | * of which frame timestamps are represented. For fixed-fps content, |
---|
| 204 | * timebase should be 1/framerate and timestamp increments should be |
---|
| 205 | * identical to 1. */ |
---|
[5270758] | 206 | #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(55, 44, 0) |
---|
| 207 | // Old way, which now causes deprecation warnings. |
---|
[cc9e7a06] | 208 | c->time_base.den = 25; // Frames per second. |
---|
[64d06c0] | 209 | c->time_base.num = 1; |
---|
[5270758] | 210 | #else |
---|
| 211 | video_st->time_base.den = 25; // Frames per second. |
---|
| 212 | video_st->time_base.num = 1; |
---|
| 213 | c->time_base = video_st->time_base; |
---|
| 214 | #endif |
---|
[64d06c0] | 215 | c->gop_size = 12; /* emit one intra frame every twelve frames at most */ |
---|
| 216 | c->pix_fmt = AV_PIX_FMT_YUV420P; |
---|
[997509d] | 217 | c->rc_buffer_size = c->bit_rate * 4; // Enough for 4 seconds |
---|
[5270758] | 218 | c->rc_max_rate = c->bit_rate * 2; |
---|
[1805274] | 219 | // B frames are backwards predicted - they can improve compression, |
---|
| 220 | // but may slow encoding and decoding. |
---|
[64d06c0] | 221 | // if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
---|
| 222 | // c->max_b_frames = 2; |
---|
| 223 | // } |
---|
[6a4cdcb6] | 224 | |
---|
[64d06c0] | 225 | /* Some formats want stream headers to be separate. */ |
---|
[cc9e7a06] | 226 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
---|
| 227 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
---|
| 228 | |
---|
[7831cef] | 229 | int retval; |
---|
| 230 | #ifndef HAVE_AVFORMAT_WRITE_HEADER |
---|
[1805274] | 231 | // Set the output parameters (must be done even if no parameters). |
---|
[7831cef] | 232 | retval = av_set_parameters(oc, NULL); |
---|
[091069f] | 233 | if (retval < 0) { |
---|
| 234 | averrno = retval; |
---|
[1805274] | 235 | return false; |
---|
| 236 | } |
---|
[7831cef] | 237 | #endif |
---|
[1805274] | 238 | |
---|
[64d06c0] | 239 | retval = avcodec_open2(c, NULL, NULL); |
---|
[091069f] | 240 | if (retval < 0) { |
---|
| 241 | averrno = retval; |
---|
[1805274] | 242 | return false; |
---|
[6a4cdcb6] | 243 | } |
---|
[1805274] | 244 | |
---|
[64d06c0] | 245 | #ifndef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
| 246 | outbuf = NULL; |
---|
| 247 | if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) { |
---|
[d13aea6] | 248 | outbuf = (unsigned char *)av_malloc(OUTBUF_SIZE); |
---|
[cc9e7a06] | 249 | if (!outbuf) { |
---|
[63621a7] | 250 | averrno = AVERROR(ENOMEM); |
---|
[cc9e7a06] | 251 | return false; |
---|
| 252 | } |
---|
[6a4cdcb6] | 253 | } |
---|
[64d06c0] | 254 | #endif |
---|
[1805274] | 255 | |
---|
[64d06c0] | 256 | /* Allocate the encoded raw picture. */ |
---|
[8364c65f] | 257 | frame = av_frame_alloc(); |
---|
[cc9e7a06] | 258 | if (!frame) { |
---|
[63621a7] | 259 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 260 | return false; |
---|
[6a4cdcb6] | 261 | } |
---|
[b282f44] | 262 | retval = av_image_alloc(frame->data, frame->linesize, |
---|
| 263 | c->width, c->height, c->pix_fmt, 1); |
---|
[64d06c0] | 264 | if (retval < 0) { |
---|
| 265 | averrno = retval; |
---|
[1805274] | 266 | return false; |
---|
[6a4cdcb6] | 267 | } |
---|
[cc9e7a06] | 268 | |
---|
[8364c65f] | 269 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) { |
---|
[cc9e7a06] | 270 | // FIXME need to allocate another frame for this case if we stop |
---|
[8364c65f] | 271 | // hardcoding AV_PIX_FMT_YUV420P. |
---|
[cc9e7a06] | 272 | abort(); |
---|
| 273 | } |
---|
[6a4cdcb6] | 274 | |
---|
[5270758] | 275 | frame->format = c->pix_fmt; |
---|
| 276 | frame->width = c->width; |
---|
| 277 | frame->height = c->height; |
---|
| 278 | |
---|
[d13aea6] | 279 | pixels = (unsigned char *)av_malloc(width * height * 6); |
---|
[6a4cdcb6] | 280 | if (!pixels) { |
---|
[63621a7] | 281 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 282 | return false; |
---|
[6a4cdcb6] | 283 | } |
---|
[1805274] | 284 | |
---|
[64d06c0] | 285 | // Show the format we've ended up with (for debug purposes). |
---|
| 286 | // av_dump_format(oc, 0, fnm, 1); |
---|
| 287 | |
---|
| 288 | av_free(sws_ctx); |
---|
[ad04c87] | 289 | sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24, |
---|
[64d06c0] | 290 | width, height, c->pix_fmt, SWS_BICUBIC, |
---|
| 291 | NULL, NULL, NULL); |
---|
| 292 | if (sws_ctx == NULL) { |
---|
| 293 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
---|
| 294 | averrno = AVERROR(ENOMEM); |
---|
[1805274] | 295 | return false; |
---|
| 296 | } |
---|
| 297 | |
---|
[64d06c0] | 298 | if (!(fmt->flags & AVFMT_NOFILE)) { |
---|
| 299 | retval = avio_open(&oc->pb, fnm, AVIO_FLAG_WRITE); |
---|
| 300 | if (retval < 0) { |
---|
| 301 | averrno = retval; |
---|
| 302 | return false; |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
[1805274] | 306 | // Write the stream header, if any. |
---|
[7831cef] | 307 | #ifdef HAVE_AVFORMAT_WRITE_HEADER |
---|
| 308 | retval = avformat_write_header(oc, NULL); |
---|
| 309 | #else |
---|
[091069f] | 310 | retval = av_write_header(oc); |
---|
[7831cef] | 311 | #endif |
---|
[091069f] | 312 | if (retval < 0) { |
---|
| 313 | averrno = retval; |
---|
[1805274] | 314 | return false; |
---|
| 315 | } |
---|
[9e516d0d] | 316 | |
---|
[091069f] | 317 | averrno = 0; |
---|
[1805274] | 318 | return true; |
---|
[6a4cdcb6] | 319 | #else |
---|
[6e22f11] | 320 | (void)fnm; |
---|
| 321 | (void)width; |
---|
| 322 | (void)height; |
---|
[6a4cdcb6] | 323 | return false; |
---|
| 324 | #endif |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | unsigned char * MovieMaker::GetBuffer() const { |
---|
[f678705] | 328 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 329 | AVCodecContext * c = video_st->codec; |
---|
[9e516d0d] | 330 | return pixels + c->height * c->width * 3; |
---|
[eac4514] | 331 | #else |
---|
| 332 | return NULL; |
---|
| 333 | #endif |
---|
[6a4cdcb6] | 334 | } |
---|
| 335 | |
---|
| 336 | int MovieMaker::GetWidth() const { |
---|
[f678705] | 337 | #ifdef WITH_LIBAV |
---|
[ccb83b7] | 338 | assert(video_st); |
---|
[64d06c0] | 339 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 340 | return c->width; |
---|
[1805274] | 341 | #else |
---|
| 342 | return 0; |
---|
| 343 | #endif |
---|
[6a4cdcb6] | 344 | } |
---|
| 345 | |
---|
| 346 | int MovieMaker::GetHeight() const { |
---|
[f678705] | 347 | #ifdef WITH_LIBAV |
---|
[ccb83b7] | 348 | assert(video_st); |
---|
[64d06c0] | 349 | AVCodecContext *c = video_st->codec; |
---|
[6a4cdcb6] | 350 | return c->height; |
---|
[1805274] | 351 | #else |
---|
| 352 | return 0; |
---|
| 353 | #endif |
---|
[6a4cdcb6] | 354 | } |
---|
| 355 | |
---|
[98fd937] | 356 | bool MovieMaker::AddFrame() |
---|
[6a4cdcb6] | 357 | { |
---|
[f678705] | 358 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 359 | AVCodecContext * c = video_st->codec; |
---|
[cc9e7a06] | 360 | |
---|
[8364c65f] | 361 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) { |
---|
[cc9e7a06] | 362 | // FIXME convert... |
---|
| 363 | abort(); |
---|
| 364 | } |
---|
[1805274] | 365 | |
---|
[9e516d0d] | 366 | int len = 3 * c->width; |
---|
[fed3713] | 367 | { |
---|
| 368 | // Flip image vertically |
---|
| 369 | int h = c->height; |
---|
| 370 | unsigned char * src = pixels + h * len; |
---|
| 371 | unsigned char * dest = src - len; |
---|
| 372 | while (h--) { |
---|
| 373 | memcpy(dest, src, len); |
---|
| 374 | src += len; |
---|
| 375 | dest -= len; |
---|
| 376 | } |
---|
[6a4cdcb6] | 377 | } |
---|
[9e516d0d] | 378 | sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); |
---|
[6a4cdcb6] | 379 | |
---|
[cc9e7a06] | 380 | if (oc->oformat->flags & AVFMT_RAWPICTURE) { |
---|
| 381 | abort(); |
---|
| 382 | } |
---|
[6a4cdcb6] | 383 | |
---|
[1805274] | 384 | // Encode this frame. |
---|
[64d06c0] | 385 | #ifdef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
[710bd97] | 386 | AVPacket pkt; |
---|
[64d06c0] | 387 | int got_packet; |
---|
| 388 | av_init_packet(&pkt); |
---|
[710bd97] | 389 | pkt.data = NULL; |
---|
[64d06c0] | 390 | |
---|
| 391 | int ret = avcodec_encode_video2(c, &pkt, frame, &got_packet); |
---|
| 392 | if (ret < 0) { |
---|
[98fd937] | 393 | averrno = ret; |
---|
| 394 | return false; |
---|
[64d06c0] | 395 | } |
---|
| 396 | if (got_packet && pkt.size) { |
---|
| 397 | // Write the compressed frame to the media file. |
---|
[710bd97] | 398 | if (pkt.pts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 399 | pkt.pts = av_rescale_q(pkt.pts, |
---|
| 400 | c->time_base, video_st->time_base); |
---|
| 401 | } |
---|
[710bd97] | 402 | if (pkt.dts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 403 | pkt.dts = av_rescale_q(pkt.dts, |
---|
| 404 | c->time_base, video_st->time_base); |
---|
| 405 | } |
---|
| 406 | pkt.stream_index = video_st->index; |
---|
| 407 | |
---|
| 408 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 409 | ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 410 | if (ret < 0) { |
---|
| 411 | averrno = ret; |
---|
| 412 | return false; |
---|
[64d06c0] | 413 | } |
---|
| 414 | } |
---|
| 415 | #else |
---|
[6a4cdcb6] | 416 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
---|
[1805274] | 417 | // outsize == 0 means that this frame has been buffered, so there's nothing |
---|
| 418 | // to write yet. |
---|
[cc9e7a06] | 419 | if (out_size) { |
---|
[1805274] | 420 | // Write the compressed frame to the media file. |
---|
[cc9e7a06] | 421 | AVPacket pkt; |
---|
| 422 | av_init_packet(&pkt); |
---|
| 423 | |
---|
[63621a7] | 424 | if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE) |
---|
[64d06c0] | 425 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); |
---|
[cc9e7a06] | 426 | if (c->coded_frame->key_frame) |
---|
| 427 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
[64d06c0] | 428 | pkt.stream_index = video_st->index; |
---|
[cc9e7a06] | 429 | pkt.data = outbuf; |
---|
| 430 | pkt.size = out_size; |
---|
| 431 | |
---|
[64d06c0] | 432 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 433 | int ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 434 | if (ret < 0) { |
---|
| 435 | averrno = ret; |
---|
| 436 | return false; |
---|
[1805274] | 437 | } |
---|
[6a4cdcb6] | 438 | } |
---|
| 439 | #endif |
---|
[64d06c0] | 440 | #endif |
---|
[98fd937] | 441 | return true; |
---|
[6a4cdcb6] | 442 | } |
---|
| 443 | |
---|
[98fd937] | 444 | bool |
---|
| 445 | MovieMaker::Close() |
---|
[6a4cdcb6] | 446 | { |
---|
[f678705] | 447 | #ifdef WITH_LIBAV |
---|
[64d06c0] | 448 | if (video_st && averrno == 0) { |
---|
[1805274] | 449 | // No more frames to compress. The codec may have a few frames |
---|
| 450 | // buffered if we're using B frames, so write those too. |
---|
[64d06c0] | 451 | AVCodecContext * c = video_st->codec; |
---|
[1805274] | 452 | |
---|
[64d06c0] | 453 | #ifdef HAVE_AVCODEC_ENCODE_VIDEO2 |
---|
| 454 | while (1) { |
---|
[710bd97] | 455 | AVPacket pkt; |
---|
[64d06c0] | 456 | int got_packet; |
---|
| 457 | av_init_packet(&pkt); |
---|
[710bd97] | 458 | pkt.data = NULL; |
---|
[5270758] | 459 | pkt.size = 0; |
---|
[64d06c0] | 460 | |
---|
| 461 | int ret = avcodec_encode_video2(c, &pkt, NULL, &got_packet); |
---|
| 462 | if (ret < 0) { |
---|
[98fd937] | 463 | release(); |
---|
| 464 | averrno = ret; |
---|
| 465 | return false; |
---|
[64d06c0] | 466 | } |
---|
| 467 | if (!got_packet) break; |
---|
| 468 | if (!pkt.size) continue; |
---|
| 469 | |
---|
| 470 | // Write the compressed frame to the media file. |
---|
[710bd97] | 471 | if (pkt.pts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 472 | pkt.pts = av_rescale_q(pkt.pts, |
---|
| 473 | c->time_base, video_st->time_base); |
---|
| 474 | } |
---|
[710bd97] | 475 | if (pkt.dts != int64_t(AV_NOPTS_VALUE)) { |
---|
[64d06c0] | 476 | pkt.dts = av_rescale_q(pkt.dts, |
---|
| 477 | c->time_base, video_st->time_base); |
---|
| 478 | } |
---|
| 479 | pkt.stream_index = video_st->index; |
---|
| 480 | |
---|
| 481 | /* Write the compressed frame to the media file. */ |
---|
[98fd937] | 482 | ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 483 | if (ret < 0) { |
---|
| 484 | release(); |
---|
| 485 | averrno = ret; |
---|
| 486 | return false; |
---|
[64d06c0] | 487 | } |
---|
| 488 | } |
---|
| 489 | #else |
---|
[1805274] | 490 | while (out_size) { |
---|
| 491 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
---|
| 492 | if (out_size) { |
---|
[cc9e7a06] | 493 | // Write the compressed frame to the media file. |
---|
| 494 | AVPacket pkt; |
---|
| 495 | av_init_packet(&pkt); |
---|
| 496 | |
---|
[63621a7] | 497 | if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE) |
---|
[64d06c0] | 498 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); |
---|
[cc9e7a06] | 499 | if (c->coded_frame->key_frame) |
---|
| 500 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
[64d06c0] | 501 | pkt.stream_index = video_st->index; |
---|
[cc9e7a06] | 502 | pkt.data = outbuf; |
---|
| 503 | pkt.size = out_size; |
---|
| 504 | |
---|
| 505 | /* write the compressed frame in the media file */ |
---|
[98fd937] | 506 | int ret = av_interleaved_write_frame(oc, &pkt); |
---|
| 507 | if (ret < 0) { |
---|
| 508 | release(); |
---|
| 509 | averrno = ret; |
---|
| 510 | return false; |
---|
[1805274] | 511 | } |
---|
| 512 | } |
---|
[6a4cdcb6] | 513 | } |
---|
[64d06c0] | 514 | #endif |
---|
[6a4cdcb6] | 515 | |
---|
[cc9e7a06] | 516 | av_write_trailer(oc); |
---|
[091069f] | 517 | } |
---|
[cc9e7a06] | 518 | |
---|
[98fd937] | 519 | release(); |
---|
| 520 | #endif |
---|
| 521 | return true; |
---|
| 522 | } |
---|
| 523 | |
---|
[f678705] | 524 | #ifdef WITH_LIBAV |
---|
[98fd937] | 525 | void |
---|
| 526 | MovieMaker::release() |
---|
| 527 | { |
---|
[64d06c0] | 528 | if (video_st) { |
---|
[1805274] | 529 | // Close codec. |
---|
[64d06c0] | 530 | avcodec_close(video_st->codec); |
---|
[98fd937] | 531 | video_st = NULL; |
---|
[6a4cdcb6] | 532 | } |
---|
| 533 | |
---|
[cc9e7a06] | 534 | if (frame) { |
---|
[8364c65f] | 535 | av_frame_free(&frame); |
---|
[cc9e7a06] | 536 | } |
---|
[d13aea6] | 537 | av_free(pixels); |
---|
[98fd937] | 538 | pixels = NULL; |
---|
[d13aea6] | 539 | av_free(outbuf); |
---|
[98fd937] | 540 | outbuf = NULL; |
---|
[9e516d0d] | 541 | av_free(sws_ctx); |
---|
[98fd937] | 542 | sws_ctx = NULL; |
---|
[1805274] | 543 | |
---|
| 544 | if (oc) { |
---|
| 545 | // Free the streams. |
---|
[cc9e7a06] | 546 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
---|
| 547 | av_freep(&oc->streams[i]->codec); |
---|
[1805274] | 548 | av_freep(&oc->streams[i]); |
---|
| 549 | } |
---|
| 550 | |
---|
[cc9e7a06] | 551 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
---|
| 552 | // Close the output file. |
---|
[7831cef] | 553 | avio_close(oc->pb); |
---|
[cc9e7a06] | 554 | } |
---|
[1805274] | 555 | |
---|
| 556 | // Free the stream. |
---|
[6ed625e] | 557 | av_free(oc); |
---|
[98fd937] | 558 | oc = NULL; |
---|
[1805274] | 559 | } |
---|
[6a4cdcb6] | 560 | } |
---|
[c648bd1] | 561 | #endif |
---|
[091069f] | 562 | |
---|
[98fd937] | 563 | MovieMaker::~MovieMaker() |
---|
| 564 | { |
---|
[f678705] | 565 | #ifdef WITH_LIBAV |
---|
[98fd937] | 566 | release(); |
---|
[c648bd1] | 567 | #endif |
---|
[98fd937] | 568 | } |
---|
| 569 | |
---|
[091069f] | 570 | const char * |
---|
| 571 | MovieMaker::get_error_string() const |
---|
| 572 | { |
---|
[f678705] | 573 | #ifdef WITH_LIBAV |
---|
[091069f] | 574 | switch (averrno) { |
---|
[63621a7] | 575 | case AVERROR(EIO): |
---|
[091069f] | 576 | return "I/O error"; |
---|
[63621a7] | 577 | case AVERROR(EDOM): |
---|
[091069f] | 578 | return "Number syntax expected in filename"; |
---|
| 579 | case AVERROR_INVALIDDATA: |
---|
| 580 | /* same as AVERROR_UNKNOWN: return "unknown error"; */ |
---|
| 581 | return "invalid data found"; |
---|
[63621a7] | 582 | case AVERROR(ENOMEM): |
---|
[091069f] | 583 | return "not enough memory"; |
---|
[63621a7] | 584 | case AVERROR(EILSEQ): |
---|
[091069f] | 585 | return "unknown format"; |
---|
[63621a7] | 586 | case AVERROR(ENOSYS): |
---|
[091069f] | 587 | return "Operation not supported"; |
---|
[63621a7] | 588 | case AVERROR(ENOENT): |
---|
[091069f] | 589 | return "No such file or directory"; |
---|
| 590 | case AVERROR_EOF: |
---|
| 591 | return "End of file"; |
---|
| 592 | case AVERROR_PATCHWELCOME: |
---|
| 593 | return "Not implemented in FFmpeg"; |
---|
| 594 | case 0: |
---|
| 595 | return "No error"; |
---|
| 596 | case MOVIE_NO_SUITABLE_FORMAT: |
---|
| 597 | return "Couldn't find a suitable output format"; |
---|
| 598 | case MOVIE_AUDIO_ONLY: |
---|
| 599 | return "Audio-only format specified"; |
---|
| 600 | case MOVIE_FILENAME_TOO_LONG: |
---|
| 601 | return "Filename too long"; |
---|
| 602 | } |
---|
| 603 | return "Unknown error"; |
---|
[eac4514] | 604 | #else |
---|
| 605 | return "Movie generation support code not present"; |
---|
| 606 | #endif |
---|
[091069f] | 607 | } |
---|