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