[6a4cdcb6] | 1 | // |
---|
| 2 | // moviemaker.cc |
---|
| 3 | // |
---|
| 4 | // Class for writing movies from Aven. |
---|
| 5 | // |
---|
| 6 | // Copyright (C) 2004 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 |
---|
[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" { |
---|
| 63 | #include "libavformat/avformat.h" |
---|
[9e516d0d] | 64 | #include "libswscale/swscale.h" |
---|
[cc9e7a06] | 65 | } |
---|
| 66 | #ifndef AV_PKT_FLAG_KEY |
---|
| 67 | # define AV_PKT_FLAG_KEY PKT_FLAG_KEY |
---|
| 68 | #endif |
---|
[6a4cdcb6] | 69 | #endif |
---|
| 70 | |
---|
[1805274] | 71 | const int OUTBUF_SIZE = 200000; |
---|
[6a4cdcb6] | 72 | |
---|
[1805274] | 73 | MovieMaker::MovieMaker() |
---|
[9e516d0d] | 74 | : oc(0), st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0) |
---|
[6a4cdcb6] | 75 | { |
---|
[cc9e7a06] | 76 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
[6a4cdcb6] | 77 | static bool initialised_ffmpeg = false; |
---|
[1805274] | 78 | if (initialised_ffmpeg) return; |
---|
[6a4cdcb6] | 79 | |
---|
[1805274] | 80 | // FIXME: register only the codec(s) we want to use... |
---|
| 81 | av_register_all(); |
---|
[6a4cdcb6] | 82 | |
---|
[1805274] | 83 | initialised_ffmpeg = true; |
---|
| 84 | #endif |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | bool MovieMaker::Open(const char *fnm, int width, int height) |
---|
| 88 | { |
---|
[cc9e7a06] | 89 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
[1805274] | 90 | AVOutputFormat * fmt = guess_format(NULL, fnm, NULL); |
---|
| 91 | if (!fmt) { |
---|
| 92 | // We couldn't deduce the output format from file extension so default |
---|
| 93 | // to MPEG. |
---|
| 94 | fmt = guess_format("mpeg", NULL, NULL); |
---|
| 95 | if (!fmt) { |
---|
| 96 | // FIXME : error finding a format... |
---|
| 97 | return false; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | if (fmt->video_codec == CODEC_ID_NONE) { |
---|
| 101 | // FIXME : The user asked for a format which is audio-only! |
---|
| 102 | return false; |
---|
[6a4cdcb6] | 103 | } |
---|
| 104 | |
---|
[1805274] | 105 | // Allocate the output media context. |
---|
[6ed625e] | 106 | oc = avformat_alloc_context(); |
---|
[1805274] | 107 | if (!oc) { |
---|
| 108 | // FIXME : out of memory |
---|
| 109 | return false; |
---|
[6a4cdcb6] | 110 | } |
---|
[1805274] | 111 | oc->oformat = fmt; |
---|
| 112 | if (strlen(fnm) >= sizeof(oc->filename)) { |
---|
| 113 | // FIXME : filename too long |
---|
| 114 | return false; |
---|
| 115 | } |
---|
| 116 | strcpy(oc->filename, fnm); |
---|
[6a4cdcb6] | 117 | |
---|
[1805274] | 118 | // Add the video stream using the default format codec. |
---|
| 119 | st = av_new_stream(oc, 0); |
---|
| 120 | if (!st) { |
---|
| 121 | // FIXME : possible errors are "too many streams" (can't be - we only |
---|
| 122 | // ask for one) and "out of memory" |
---|
| 123 | return false; |
---|
[6a4cdcb6] | 124 | } |
---|
| 125 | |
---|
[1805274] | 126 | // Initialise the code. |
---|
[cc9e7a06] | 127 | AVCodecContext *c = st->codec; |
---|
[1805274] | 128 | c->codec_id = fmt->video_codec; |
---|
| 129 | c->codec_type = CODEC_TYPE_VIDEO; |
---|
[6a4cdcb6] | 130 | |
---|
| 131 | // Set sample parameters. |
---|
| 132 | c->bit_rate = 400000; |
---|
[1805274] | 133 | c->width = width; |
---|
| 134 | c->height = height; |
---|
[cc9e7a06] | 135 | c->time_base.num = 1; |
---|
| 136 | c->time_base.den = 25; // Frames per second. |
---|
[1805274] | 137 | c->gop_size = 12; // One intra frame every twelve frames. |
---|
[6a4cdcb6] | 138 | c->pix_fmt = PIX_FMT_YUV420P; |
---|
[1805274] | 139 | // B frames are backwards predicted - they can improve compression, |
---|
| 140 | // but may slow encoding and decoding. |
---|
| 141 | // c->max_b_frames = 2; |
---|
[6a4cdcb6] | 142 | |
---|
[cc9e7a06] | 143 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
---|
| 144 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
---|
| 145 | |
---|
[1805274] | 146 | // Set the output parameters (must be done even if no parameters). |
---|
| 147 | if (av_set_parameters(oc, NULL) < 0) { |
---|
| 148 | // FIXME : return value is an AVERROR_* value - probably |
---|
| 149 | // AVERROR_NOMEM or AVERROR_UNKNOWN. |
---|
| 150 | return false; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // Show the format we've ended up with (for debug purposes). |
---|
| 154 | // dump_format(oc, 0, fnm, 1); |
---|
| 155 | |
---|
| 156 | // Open the video codec and allocate the necessary encode buffers. |
---|
| 157 | AVCodec * codec = avcodec_find_encoder(c->codec_id); |
---|
| 158 | if (!codec) { |
---|
| 159 | // FIXME : Erm - internal ffmpeg library problem? |
---|
| 160 | return false; |
---|
| 161 | } |
---|
[6a4cdcb6] | 162 | if (avcodec_open(c, codec) < 0) { |
---|
[1805274] | 163 | // FIXME : return value is an AVERROR_* value - probably |
---|
| 164 | // AVERROR_NOMEM or AVERROR_UNKNOWN. |
---|
| 165 | return false; |
---|
[6a4cdcb6] | 166 | } |
---|
[1805274] | 167 | |
---|
[cc9e7a06] | 168 | if ((oc->oformat->flags & AVFMT_RAWPICTURE)) { |
---|
| 169 | outbuf = NULL; |
---|
| 170 | } else { |
---|
| 171 | outbuf = (unsigned char *)malloc(OUTBUF_SIZE); |
---|
| 172 | if (!outbuf) { |
---|
| 173 | // FIXME : out of memory |
---|
| 174 | return false; |
---|
| 175 | } |
---|
[6a4cdcb6] | 176 | } |
---|
[1805274] | 177 | |
---|
[6a4cdcb6] | 178 | frame = avcodec_alloc_frame(); |
---|
[cc9e7a06] | 179 | if (!frame) { |
---|
[1805274] | 180 | // FIXME : out of memory |
---|
| 181 | return false; |
---|
[6a4cdcb6] | 182 | } |
---|
[cc9e7a06] | 183 | int size = avpicture_get_size(c->pix_fmt, width, height); |
---|
[9e516d0d] | 184 | uint8_t * picture_buf = (uint8_t*)av_malloc(size); |
---|
| 185 | if (!picture_buf) { |
---|
[cc9e7a06] | 186 | av_free(frame); |
---|
[1805274] | 187 | // FIXME : out of memory |
---|
| 188 | return false; |
---|
[6a4cdcb6] | 189 | } |
---|
[9e516d0d] | 190 | avpicture_fill((AVPicture *)frame, picture_buf, c->pix_fmt, width, height); |
---|
[cc9e7a06] | 191 | |
---|
| 192 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
---|
| 193 | // FIXME need to allocate another frame for this case if we stop |
---|
| 194 | // hardcoding PIX_FMT_YUV420P. |
---|
| 195 | abort(); |
---|
| 196 | } |
---|
[6a4cdcb6] | 197 | |
---|
| 198 | pixels = (unsigned char *)malloc(width * height * 6); |
---|
| 199 | if (!pixels) { |
---|
[1805274] | 200 | // FIXME : out of memory |
---|
| 201 | return false; |
---|
[6a4cdcb6] | 202 | } |
---|
[1805274] | 203 | |
---|
| 204 | if (url_fopen(&oc->pb, fnm, URL_WRONLY) < 0) { |
---|
| 205 | // FIXME : return value is -E* (e.g. -EIO). |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | // Write the stream header, if any. |
---|
| 210 | if (av_write_header(oc) < 0) { |
---|
| 211 | // FIXME : return value is an AVERROR_* value. |
---|
| 212 | return false; |
---|
| 213 | } |
---|
[9e516d0d] | 214 | |
---|
| 215 | av_free(sws_ctx); |
---|
| 216 | sws_ctx = sws_getContext(width, height, PIX_FMT_RGB24, |
---|
| 217 | width, height, c->pix_fmt, SWS_BICUBIC, |
---|
| 218 | NULL, NULL, NULL); |
---|
| 219 | if (sws_ctx == NULL) { |
---|
| 220 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
---|
| 221 | return false; |
---|
| 222 | } |
---|
| 223 | |
---|
[1805274] | 224 | return true; |
---|
[6a4cdcb6] | 225 | #else |
---|
| 226 | return false; |
---|
| 227 | #endif |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | unsigned char * MovieMaker::GetBuffer() const { |
---|
[9e516d0d] | 231 | AVCodecContext * c = st->codec; |
---|
| 232 | return pixels + c->height * c->width * 3; |
---|
[6a4cdcb6] | 233 | } |
---|
| 234 | |
---|
| 235 | int MovieMaker::GetWidth() const { |
---|
[1805274] | 236 | assert(st); |
---|
[cc9e7a06] | 237 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
| 238 | AVCodecContext *c = st->codec; |
---|
[6a4cdcb6] | 239 | return c->width; |
---|
[1805274] | 240 | #else |
---|
| 241 | return 0; |
---|
| 242 | #endif |
---|
[6a4cdcb6] | 243 | } |
---|
| 244 | |
---|
| 245 | int MovieMaker::GetHeight() const { |
---|
[1805274] | 246 | assert(st); |
---|
[cc9e7a06] | 247 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
| 248 | AVCodecContext *c = st->codec; |
---|
[6a4cdcb6] | 249 | return c->height; |
---|
[1805274] | 250 | #else |
---|
| 251 | return 0; |
---|
| 252 | #endif |
---|
[6a4cdcb6] | 253 | } |
---|
| 254 | |
---|
| 255 | void MovieMaker::AddFrame() |
---|
| 256 | { |
---|
[cc9e7a06] | 257 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
| 258 | AVCodecContext * c = st->codec; |
---|
| 259 | |
---|
| 260 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
---|
| 261 | // FIXME convert... |
---|
| 262 | abort(); |
---|
| 263 | } |
---|
[1805274] | 264 | |
---|
[9e516d0d] | 265 | int len = 3 * c->width; |
---|
[fed3713] | 266 | { |
---|
| 267 | // Flip image vertically |
---|
| 268 | int h = c->height; |
---|
| 269 | unsigned char * src = pixels + h * len; |
---|
| 270 | unsigned char * dest = src - len; |
---|
| 271 | while (h--) { |
---|
| 272 | memcpy(dest, src, len); |
---|
| 273 | src += len; |
---|
| 274 | dest -= len; |
---|
| 275 | } |
---|
[6a4cdcb6] | 276 | } |
---|
[9e516d0d] | 277 | sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); |
---|
[6a4cdcb6] | 278 | |
---|
[cc9e7a06] | 279 | if (oc->oformat->flags & AVFMT_RAWPICTURE) { |
---|
| 280 | abort(); |
---|
| 281 | } |
---|
[6a4cdcb6] | 282 | |
---|
[1805274] | 283 | // Encode this frame. |
---|
[6a4cdcb6] | 284 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
---|
[1805274] | 285 | // outsize == 0 means that this frame has been buffered, so there's nothing |
---|
| 286 | // to write yet. |
---|
[cc9e7a06] | 287 | if (out_size) { |
---|
[1805274] | 288 | // Write the compressed frame to the media file. |
---|
[cc9e7a06] | 289 | AVPacket pkt; |
---|
| 290 | av_init_packet(&pkt); |
---|
| 291 | |
---|
| 292 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
---|
| 293 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
---|
| 294 | if (c->coded_frame->key_frame) |
---|
| 295 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
| 296 | pkt.stream_index = st->index; |
---|
| 297 | pkt.data = outbuf; |
---|
| 298 | pkt.size = out_size; |
---|
| 299 | |
---|
| 300 | /* write the compressed frame in the media file */ |
---|
| 301 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
---|
| 302 | abort(); |
---|
[1805274] | 303 | } |
---|
[6a4cdcb6] | 304 | } |
---|
| 305 | #endif |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | MovieMaker::~MovieMaker() |
---|
| 309 | { |
---|
[cc9e7a06] | 310 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
[1805274] | 311 | if (st) { |
---|
| 312 | // No more frames to compress. The codec may have a few frames |
---|
| 313 | // buffered if we're using B frames, so write those too. |
---|
[cc9e7a06] | 314 | AVCodecContext * c = st->codec; |
---|
[1805274] | 315 | |
---|
| 316 | while (out_size) { |
---|
| 317 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
---|
| 318 | if (out_size) { |
---|
[cc9e7a06] | 319 | // Write the compressed frame to the media file. |
---|
| 320 | AVPacket pkt; |
---|
| 321 | av_init_packet(&pkt); |
---|
| 322 | |
---|
| 323 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
---|
| 324 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
---|
| 325 | if (c->coded_frame->key_frame) |
---|
| 326 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
| 327 | pkt.stream_index = st->index; |
---|
| 328 | pkt.data = outbuf; |
---|
| 329 | pkt.size = out_size; |
---|
| 330 | |
---|
| 331 | /* write the compressed frame in the media file */ |
---|
| 332 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
---|
| 333 | abort(); |
---|
[1805274] | 334 | } |
---|
| 335 | } |
---|
[6a4cdcb6] | 336 | } |
---|
| 337 | |
---|
[cc9e7a06] | 338 | av_write_trailer(oc); |
---|
| 339 | |
---|
[1805274] | 340 | // Close codec. |
---|
| 341 | avcodec_close(c); |
---|
[6a4cdcb6] | 342 | } |
---|
| 343 | |
---|
[cc9e7a06] | 344 | if (frame) { |
---|
| 345 | free(frame->data[0]); |
---|
| 346 | free(frame); |
---|
| 347 | } |
---|
[1805274] | 348 | free(outbuf); |
---|
[6a4cdcb6] | 349 | free(pixels); |
---|
[9e516d0d] | 350 | av_free(sws_ctx); |
---|
[1805274] | 351 | |
---|
| 352 | if (oc) { |
---|
| 353 | // Free the streams. |
---|
[cc9e7a06] | 354 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
---|
| 355 | av_freep(&oc->streams[i]->codec); |
---|
[1805274] | 356 | av_freep(&oc->streams[i]); |
---|
| 357 | } |
---|
| 358 | |
---|
[cc9e7a06] | 359 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
---|
| 360 | // Close the output file. |
---|
| 361 | url_fclose(oc->pb); |
---|
| 362 | } |
---|
[1805274] | 363 | |
---|
| 364 | // Free the stream. |
---|
[6ed625e] | 365 | av_free(oc); |
---|
[1805274] | 366 | } |
---|
[6a4cdcb6] | 367 | #endif |
---|
| 368 | } |
---|