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