| 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 |
|---|
| 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 | } |
|---|
| 65 | #ifndef AV_PKT_FLAG_KEY |
|---|
| 66 | # define AV_PKT_FLAG_KEY PKT_FLAG_KEY |
|---|
| 67 | #endif |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 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 | |
|---|
| 74 | const int OUTBUF_SIZE = 200000; |
|---|
| 75 | |
|---|
| 76 | MovieMaker::MovieMaker() |
|---|
| 77 | : oc(0), st(0), frame(0), outbuf(0), in(0), out(0), pixels(0) |
|---|
| 78 | { |
|---|
| 79 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 80 | static bool initialised_ffmpeg = false; |
|---|
| 81 | if (initialised_ffmpeg) return; |
|---|
| 82 | |
|---|
| 83 | // FIXME: register only the codec(s) we want to use... |
|---|
| 84 | av_register_all(); |
|---|
| 85 | |
|---|
| 86 | initialised_ffmpeg = true; |
|---|
| 87 | #endif |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | bool MovieMaker::Open(const char *fnm, int width, int height) |
|---|
| 91 | { |
|---|
| 92 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 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; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | // Allocate the output media context. |
|---|
| 109 | oc = av_alloc_format_context(); |
|---|
| 110 | if (!oc) { |
|---|
| 111 | // FIXME : out of memory |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 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); |
|---|
| 120 | |
|---|
| 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; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | // Initialise the code. |
|---|
| 130 | AVCodecContext *c = st->codec; |
|---|
| 131 | c->codec_id = fmt->video_codec; |
|---|
| 132 | c->codec_type = CODEC_TYPE_VIDEO; |
|---|
| 133 | |
|---|
| 134 | // Set sample parameters. |
|---|
| 135 | c->bit_rate = 400000; |
|---|
| 136 | c->width = width; |
|---|
| 137 | c->height = height; |
|---|
| 138 | c->time_base.num = 1; |
|---|
| 139 | c->time_base.den = 25; // Frames per second. |
|---|
| 140 | c->gop_size = 12; // One intra frame every twelve frames. |
|---|
| 141 | c->pix_fmt = PIX_FMT_YUV420P; |
|---|
| 142 | // B frames are backwards predicted - they can improve compression, |
|---|
| 143 | // but may slow encoding and decoding. |
|---|
| 144 | // c->max_b_frames = 2; |
|---|
| 145 | |
|---|
| 146 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
|---|
| 147 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|---|
| 148 | |
|---|
| 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 | } |
|---|
| 165 | if (avcodec_open(c, codec) < 0) { |
|---|
| 166 | // FIXME : return value is an AVERROR_* value - probably |
|---|
| 167 | // AVERROR_NOMEM or AVERROR_UNKNOWN. |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 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 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | frame = avcodec_alloc_frame(); |
|---|
| 182 | if (!frame) { |
|---|
| 183 | // FIXME : out of memory |
|---|
| 184 | return false; |
|---|
| 185 | } |
|---|
| 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); |
|---|
| 190 | // FIXME : out of memory |
|---|
| 191 | return false; |
|---|
| 192 | } |
|---|
| 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 | } |
|---|
| 201 | |
|---|
| 202 | pixels = (unsigned char *)malloc(width * height * 6); |
|---|
| 203 | if (!pixels) { |
|---|
| 204 | // FIXME : out of memory |
|---|
| 205 | return false; |
|---|
| 206 | } |
|---|
| 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; |
|---|
| 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 { |
|---|
| 229 | assert(st); |
|---|
| 230 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 231 | AVCodecContext *c = st->codec; |
|---|
| 232 | return c->width; |
|---|
| 233 | #else |
|---|
| 234 | return 0; |
|---|
| 235 | #endif |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | int MovieMaker::GetHeight() const { |
|---|
| 239 | assert(st); |
|---|
| 240 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 241 | AVCodecContext *c = st->codec; |
|---|
| 242 | return c->height; |
|---|
| 243 | #else |
|---|
| 244 | return 0; |
|---|
| 245 | #endif |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | void MovieMaker::AddFrame() |
|---|
| 249 | { |
|---|
| 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 | } |
|---|
| 257 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|
| 270 | |
|---|
| 271 | // Encode this frame. |
|---|
| 272 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
|---|
| 273 | // outsize == 0 means that this frame has been buffered, so there's nothing |
|---|
| 274 | // to write yet. |
|---|
| 275 | if (out_size) { |
|---|
| 276 | // Write the compressed frame to the media file. |
|---|
| 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(); |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | #endif |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | MovieMaker::~MovieMaker() |
|---|
| 297 | { |
|---|
| 298 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
|---|
| 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. |
|---|
| 302 | AVCodecContext * c = st->codec; |
|---|
| 303 | |
|---|
| 304 | while (out_size) { |
|---|
| 305 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
|---|
| 306 | if (out_size) { |
|---|
| 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(); |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | av_write_trailer(oc); |
|---|
| 327 | |
|---|
| 328 | // Close codec. |
|---|
| 329 | avcodec_close(c); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | if (frame) { |
|---|
| 333 | free(frame->data[0]); |
|---|
| 334 | free(frame); |
|---|
| 335 | } |
|---|
| 336 | free(outbuf); |
|---|
| 337 | free(pixels); |
|---|
| 338 | free(in); |
|---|
| 339 | free(out); |
|---|
| 340 | |
|---|
| 341 | if (oc) { |
|---|
| 342 | // Free the streams. |
|---|
| 343 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
|---|
| 344 | av_freep(&oc->streams[i]->codec); |
|---|
| 345 | av_freep(&oc->streams[i]); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
|---|
| 349 | // Close the output file. |
|---|
| 350 | url_fclose(oc->pb); |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | // Free the stream. |
|---|
| 354 | free(oc); |
|---|
| 355 | } |
|---|
| 356 | #endif |
|---|
| 357 | } |
|---|