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