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