| 1 | // |
|---|
| 2 | // moviemaker.cc |
|---|
| 3 | // |
|---|
| 4 | // Class for writing movies from Aven. |
|---|
| 5 | // |
|---|
| 6 | // Copyright (C) 2004,2011 Olly Betts |
|---|
| 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 |
|---|
| 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 21 | // |
|---|
| 22 | |
|---|
| 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 | |
|---|
| 49 | #ifdef HAVE_CONFIG_H |
|---|
| 50 | #include <config.h> |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | #define __STDC_CONSTANT_MACROS |
|---|
| 54 | |
|---|
| 55 | #include <assert.h> |
|---|
| 56 | #include <stdlib.h> |
|---|
| 57 | #include <string.h> |
|---|
| 58 | |
|---|
| 59 | #include "moviemaker.h" |
|---|
| 60 | |
|---|
| 61 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 62 | extern "C" { |
|---|
| 63 | #include "libavformat/avformat.h" |
|---|
| 64 | #include "libswscale/swscale.h" |
|---|
| 65 | } |
|---|
| 66 | #ifndef AV_PKT_FLAG_KEY |
|---|
| 67 | # define AV_PKT_FLAG_KEY PKT_FLAG_KEY |
|---|
| 68 | #endif |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | enum { |
|---|
| 72 | MOVIE_NO_SUITABLE_FORMAT = 1, |
|---|
| 73 | MOVIE_AUDIO_ONLY, |
|---|
| 74 | MOVIE_FILENAME_TOO_LONG, |
|---|
| 75 | MOVIE_NOT_ENABLED |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | const int OUTBUF_SIZE = 200000; |
|---|
| 79 | |
|---|
| 80 | MovieMaker::MovieMaker() |
|---|
| 81 | : oc(0), st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0), averrno(0) |
|---|
| 82 | { |
|---|
| 83 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 84 | static bool initialised_ffmpeg = false; |
|---|
| 85 | if (initialised_ffmpeg) return; |
|---|
| 86 | |
|---|
| 87 | // FIXME: register only the codec(s) we want to use... |
|---|
| 88 | av_register_all(); |
|---|
| 89 | |
|---|
| 90 | initialised_ffmpeg = true; |
|---|
| 91 | #endif |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | bool MovieMaker::Open(const char *fnm, int width, int height) |
|---|
| 95 | { |
|---|
| 96 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 97 | AVOutputFormat * fmt = guess_format(NULL, fnm, NULL); |
|---|
| 98 | if (!fmt) { |
|---|
| 99 | // We couldn't deduce the output format from file extension so default |
|---|
| 100 | // to MPEG. |
|---|
| 101 | fmt = guess_format("mpeg", NULL, NULL); |
|---|
| 102 | if (!fmt) { |
|---|
| 103 | averrno = MOVIE_NO_SUITABLE_FORMAT; |
|---|
| 104 | return false; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | if (fmt->video_codec == CODEC_ID_NONE) { |
|---|
| 108 | averrno = MOVIE_AUDIO_ONLY; |
|---|
| 109 | return false; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // Allocate the output media context. |
|---|
| 113 | oc = avformat_alloc_context(); |
|---|
| 114 | if (!oc) { |
|---|
| 115 | averrno = AVERROR_NOMEM; |
|---|
| 116 | return false; |
|---|
| 117 | } |
|---|
| 118 | oc->oformat = fmt; |
|---|
| 119 | if (strlen(fnm) >= sizeof(oc->filename)) { |
|---|
| 120 | averrno = MOVIE_FILENAME_TOO_LONG; |
|---|
| 121 | return false; |
|---|
| 122 | } |
|---|
| 123 | strcpy(oc->filename, fnm); |
|---|
| 124 | |
|---|
| 125 | // Add the video stream using the default format codec. |
|---|
| 126 | st = av_new_stream(oc, 0); |
|---|
| 127 | if (!st) { |
|---|
| 128 | averrno = AVERROR_NOMEM; |
|---|
| 129 | return false; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // Initialise the code. |
|---|
| 133 | AVCodecContext *c = st->codec; |
|---|
| 134 | c->codec_id = fmt->video_codec; |
|---|
| 135 | c->codec_type = CODEC_TYPE_VIDEO; |
|---|
| 136 | |
|---|
| 137 | // Set sample parameters. |
|---|
| 138 | c->bit_rate = 400000; |
|---|
| 139 | c->width = width; |
|---|
| 140 | c->height = height; |
|---|
| 141 | c->time_base.num = 1; |
|---|
| 142 | c->time_base.den = 25; // Frames per second. |
|---|
| 143 | c->gop_size = 12; // One intra frame every twelve frames. |
|---|
| 144 | c->pix_fmt = PIX_FMT_YUV420P; |
|---|
| 145 | // B frames are backwards predicted - they can improve compression, |
|---|
| 146 | // but may slow encoding and decoding. |
|---|
| 147 | // c->max_b_frames = 2; |
|---|
| 148 | |
|---|
| 149 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
|---|
| 150 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|---|
| 151 | |
|---|
| 152 | // Set the output parameters (must be done even if no parameters). |
|---|
| 153 | int retval = av_set_parameters(oc, NULL); |
|---|
| 154 | if (retval < 0) { |
|---|
| 155 | averrno = retval; |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | // Show the format we've ended up with (for debug purposes). |
|---|
| 160 | // dump_format(oc, 0, fnm, 1); |
|---|
| 161 | |
|---|
| 162 | // Open the video codec and allocate the necessary encode buffers. |
|---|
| 163 | AVCodec * codec = avcodec_find_encoder(c->codec_id); |
|---|
| 164 | if (!codec) { |
|---|
| 165 | // FIXME : Erm - internal ffmpeg library problem? |
|---|
| 166 | return false; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | retval = avcodec_open(c, codec); |
|---|
| 170 | if (retval < 0) { |
|---|
| 171 | averrno = retval; |
|---|
| 172 | return false; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | if ((oc->oformat->flags & AVFMT_RAWPICTURE)) { |
|---|
| 176 | outbuf = NULL; |
|---|
| 177 | } else { |
|---|
| 178 | outbuf = (unsigned char *)malloc(OUTBUF_SIZE); |
|---|
| 179 | if (!outbuf) { |
|---|
| 180 | averrno = AVERROR_NOMEM; |
|---|
| 181 | return false; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | frame = avcodec_alloc_frame(); |
|---|
| 186 | if (!frame) { |
|---|
| 187 | averrno = AVERROR_NOMEM; |
|---|
| 188 | return false; |
|---|
| 189 | } |
|---|
| 190 | int size = avpicture_get_size(c->pix_fmt, width, height); |
|---|
| 191 | uint8_t * picture_buf = (uint8_t*)av_malloc(size); |
|---|
| 192 | if (!picture_buf) { |
|---|
| 193 | av_free(frame); |
|---|
| 194 | averrno = AVERROR_NOMEM; |
|---|
| 195 | return false; |
|---|
| 196 | } |
|---|
| 197 | avpicture_fill((AVPicture *)frame, picture_buf, c->pix_fmt, width, height); |
|---|
| 198 | |
|---|
| 199 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
|---|
| 200 | // FIXME need to allocate another frame for this case if we stop |
|---|
| 201 | // hardcoding PIX_FMT_YUV420P. |
|---|
| 202 | abort(); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | pixels = (unsigned char *)malloc(width * height * 6); |
|---|
| 206 | if (!pixels) { |
|---|
| 207 | averrno = AVERROR_NOMEM; |
|---|
| 208 | return false; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | retval = url_fopen(&oc->pb, fnm, URL_WRONLY); |
|---|
| 212 | if (retval < 0) { |
|---|
| 213 | averrno = retval; |
|---|
| 214 | return false; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | // Write the stream header, if any. |
|---|
| 218 | retval = av_write_header(oc); |
|---|
| 219 | if (retval < 0) { |
|---|
| 220 | averrno = retval; |
|---|
| 221 | return false; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | av_free(sws_ctx); |
|---|
| 225 | sws_ctx = sws_getContext(width, height, PIX_FMT_RGB24, |
|---|
| 226 | width, height, c->pix_fmt, SWS_BICUBIC, |
|---|
| 227 | NULL, NULL, NULL); |
|---|
| 228 | if (sws_ctx == NULL) { |
|---|
| 229 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
|---|
| 230 | return false; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | averrno = 0; |
|---|
| 234 | return true; |
|---|
| 235 | #else |
|---|
| 236 | averrno = MOVIE_NOT_ENABLED; |
|---|
| 237 | return false; |
|---|
| 238 | #endif |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | unsigned char * MovieMaker::GetBuffer() const { |
|---|
| 242 | AVCodecContext * c = st->codec; |
|---|
| 243 | return pixels + c->height * c->width * 3; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | int MovieMaker::GetWidth() const { |
|---|
| 247 | assert(st); |
|---|
| 248 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 249 | AVCodecContext *c = st->codec; |
|---|
| 250 | return c->width; |
|---|
| 251 | #else |
|---|
| 252 | return 0; |
|---|
| 253 | #endif |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | int MovieMaker::GetHeight() const { |
|---|
| 257 | assert(st); |
|---|
| 258 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 259 | AVCodecContext *c = st->codec; |
|---|
| 260 | return c->height; |
|---|
| 261 | #else |
|---|
| 262 | return 0; |
|---|
| 263 | #endif |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | void MovieMaker::AddFrame() |
|---|
| 267 | { |
|---|
| 268 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 269 | AVCodecContext * c = st->codec; |
|---|
| 270 | |
|---|
| 271 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
|---|
| 272 | // FIXME convert... |
|---|
| 273 | abort(); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | int len = 3 * c->width; |
|---|
| 277 | { |
|---|
| 278 | // Flip image vertically |
|---|
| 279 | int h = c->height; |
|---|
| 280 | unsigned char * src = pixels + h * len; |
|---|
| 281 | unsigned char * dest = src - len; |
|---|
| 282 | while (h--) { |
|---|
| 283 | memcpy(dest, src, len); |
|---|
| 284 | src += len; |
|---|
| 285 | dest -= len; |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); |
|---|
| 289 | |
|---|
| 290 | if (oc->oformat->flags & AVFMT_RAWPICTURE) { |
|---|
| 291 | abort(); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | // Encode this frame. |
|---|
| 295 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
|---|
| 296 | // outsize == 0 means that this frame has been buffered, so there's nothing |
|---|
| 297 | // to write yet. |
|---|
| 298 | if (out_size) { |
|---|
| 299 | // Write the compressed frame to the media file. |
|---|
| 300 | AVPacket pkt; |
|---|
| 301 | av_init_packet(&pkt); |
|---|
| 302 | |
|---|
| 303 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
|---|
| 304 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
|---|
| 305 | if (c->coded_frame->key_frame) |
|---|
| 306 | pkt.flags |= AV_PKT_FLAG_KEY; |
|---|
| 307 | pkt.stream_index = st->index; |
|---|
| 308 | pkt.data = outbuf; |
|---|
| 309 | pkt.size = out_size; |
|---|
| 310 | |
|---|
| 311 | /* write the compressed frame in the media file */ |
|---|
| 312 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
|---|
| 313 | abort(); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | #endif |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | MovieMaker::~MovieMaker() |
|---|
| 320 | { |
|---|
| 321 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 322 | if (st && averrno == 0) { |
|---|
| 323 | // No more frames to compress. The codec may have a few frames |
|---|
| 324 | // buffered if we're using B frames, so write those too. |
|---|
| 325 | AVCodecContext * c = st->codec; |
|---|
| 326 | |
|---|
| 327 | while (out_size) { |
|---|
| 328 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
|---|
| 329 | if (out_size) { |
|---|
| 330 | // Write the compressed frame to the media file. |
|---|
| 331 | AVPacket pkt; |
|---|
| 332 | av_init_packet(&pkt); |
|---|
| 333 | |
|---|
| 334 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
|---|
| 335 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
|---|
| 336 | if (c->coded_frame->key_frame) |
|---|
| 337 | pkt.flags |= AV_PKT_FLAG_KEY; |
|---|
| 338 | pkt.stream_index = st->index; |
|---|
| 339 | pkt.data = outbuf; |
|---|
| 340 | pkt.size = out_size; |
|---|
| 341 | |
|---|
| 342 | /* write the compressed frame in the media file */ |
|---|
| 343 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
|---|
| 344 | abort(); |
|---|
| 345 | } |
|---|
| 346 | } |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | av_write_trailer(oc); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | if (st) { |
|---|
| 353 | // Close codec. |
|---|
| 354 | avcodec_close(st->codec); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | if (frame) { |
|---|
| 358 | free(frame->data[0]); |
|---|
| 359 | free(frame); |
|---|
| 360 | } |
|---|
| 361 | free(outbuf); |
|---|
| 362 | free(pixels); |
|---|
| 363 | av_free(sws_ctx); |
|---|
| 364 | |
|---|
| 365 | if (oc) { |
|---|
| 366 | // Free the streams. |
|---|
| 367 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
|---|
| 368 | av_freep(&oc->streams[i]->codec); |
|---|
| 369 | av_freep(&oc->streams[i]); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
|---|
| 373 | // Close the output file. |
|---|
| 374 | url_fclose(oc->pb); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | // Free the stream. |
|---|
| 378 | av_free(oc); |
|---|
| 379 | } |
|---|
| 380 | #endif |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | const char * |
|---|
| 384 | MovieMaker::get_error_string() const |
|---|
| 385 | { |
|---|
| 386 | switch (averrno) { |
|---|
| 387 | case AVERROR_IO: |
|---|
| 388 | return "I/O error"; |
|---|
| 389 | case AVERROR_NUMEXPECTED: |
|---|
| 390 | return "Number syntax expected in filename"; |
|---|
| 391 | case AVERROR_INVALIDDATA: |
|---|
| 392 | /* same as AVERROR_UNKNOWN: return "unknown error"; */ |
|---|
| 393 | return "invalid data found"; |
|---|
| 394 | case AVERROR_NOMEM: |
|---|
| 395 | return "not enough memory"; |
|---|
| 396 | case AVERROR_NOFMT: |
|---|
| 397 | return "unknown format"; |
|---|
| 398 | case AVERROR_NOTSUPP: |
|---|
| 399 | return "Operation not supported"; |
|---|
| 400 | case AVERROR_NOENT: |
|---|
| 401 | return "No such file or directory"; |
|---|
| 402 | case AVERROR_EOF: |
|---|
| 403 | return "End of file"; |
|---|
| 404 | case AVERROR_PATCHWELCOME: |
|---|
| 405 | return "Not implemented in FFmpeg"; |
|---|
| 406 | case 0: |
|---|
| 407 | return "No error"; |
|---|
| 408 | case MOVIE_NO_SUITABLE_FORMAT: |
|---|
| 409 | return "Couldn't find a suitable output format"; |
|---|
| 410 | case MOVIE_AUDIO_ONLY: |
|---|
| 411 | return "Audio-only format specified"; |
|---|
| 412 | case MOVIE_FILENAME_TOO_LONG: |
|---|
| 413 | return "Filename too long"; |
|---|
| 414 | case MOVIE_NOT_ENABLED: |
|---|
| 415 | return "Movie export support not included"; |
|---|
| 416 | } |
|---|
| 417 | return "Unknown error"; |
|---|
| 418 | } |
|---|