| 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 | #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 | const int OUTBUF_SIZE = 200000; | 
|---|
| 72 |  | 
|---|
| 73 | MovieMaker::MovieMaker() | 
|---|
| 74 |     : oc(0), st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0) | 
|---|
| 75 | { | 
|---|
| 76 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H | 
|---|
| 77 |     static bool initialised_ffmpeg = false; | 
|---|
| 78 |     if (initialised_ffmpeg) return; | 
|---|
| 79 |  | 
|---|
| 80 |     // FIXME: register only the codec(s) we want to use... | 
|---|
| 81 |     av_register_all(); | 
|---|
| 82 |  | 
|---|
| 83 |     initialised_ffmpeg = true; | 
|---|
| 84 | #endif | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | bool MovieMaker::Open(const char *fnm, int width, int height) | 
|---|
| 88 | { | 
|---|
| 89 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H | 
|---|
| 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; | 
|---|
| 103 |     } | 
|---|
| 104 |  | 
|---|
| 105 |     // Allocate the output media context. | 
|---|
| 106 |     oc = avformat_alloc_context(); | 
|---|
| 107 |     if (!oc) { | 
|---|
| 108 |         // FIXME : out of memory | 
|---|
| 109 |         return false; | 
|---|
| 110 |     } | 
|---|
| 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); | 
|---|
| 117 |  | 
|---|
| 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; | 
|---|
| 124 |     } | 
|---|
| 125 |  | 
|---|
| 126 |     // Initialise the code. | 
|---|
| 127 |     AVCodecContext *c = st->codec; | 
|---|
| 128 |     c->codec_id = fmt->video_codec; | 
|---|
| 129 |     c->codec_type = CODEC_TYPE_VIDEO; | 
|---|
| 130 |  | 
|---|
| 131 |     // Set sample parameters. | 
|---|
| 132 |     c->bit_rate = 400000; | 
|---|
| 133 |     c->width = width; | 
|---|
| 134 |     c->height = height; | 
|---|
| 135 |     c->time_base.num = 1; | 
|---|
| 136 |     c->time_base.den = 25; // Frames per second. | 
|---|
| 137 |     c->gop_size = 12; // One intra frame every twelve frames. | 
|---|
| 138 |     c->pix_fmt = PIX_FMT_YUV420P; | 
|---|
| 139 |     // B frames are backwards predicted - they can improve compression, | 
|---|
| 140 |     // but may slow encoding and decoding. | 
|---|
| 141 |     // c->max_b_frames = 2; | 
|---|
| 142 |  | 
|---|
| 143 |     if (oc->oformat->flags & AVFMT_GLOBALHEADER) | 
|---|
| 144 |         c->flags |= CODEC_FLAG_GLOBAL_HEADER; | 
|---|
| 145 |  | 
|---|
| 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 |     } | 
|---|
| 162 |     if (avcodec_open(c, codec) < 0) { | 
|---|
| 163 |         // FIXME : return value is an AVERROR_* value - probably | 
|---|
| 164 |         // AVERROR_NOMEM or AVERROR_UNKNOWN. | 
|---|
| 165 |         return false; | 
|---|
| 166 |     } | 
|---|
| 167 |  | 
|---|
| 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 |         } | 
|---|
| 176 |     } | 
|---|
| 177 |  | 
|---|
| 178 |     frame = avcodec_alloc_frame(); | 
|---|
| 179 |     if (!frame) { | 
|---|
| 180 |         // FIXME : out of memory | 
|---|
| 181 |         return false; | 
|---|
| 182 |     } | 
|---|
| 183 |     int size = avpicture_get_size(c->pix_fmt, width, height); | 
|---|
| 184 |     uint8_t * picture_buf = (uint8_t*)av_malloc(size); | 
|---|
| 185 |     if (!picture_buf) { | 
|---|
| 186 |         av_free(frame); | 
|---|
| 187 |         // FIXME : out of memory | 
|---|
| 188 |         return false; | 
|---|
| 189 |     } | 
|---|
| 190 |     avpicture_fill((AVPicture *)frame, picture_buf, c->pix_fmt, width, height); | 
|---|
| 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 |     } | 
|---|
| 197 |  | 
|---|
| 198 |     pixels = (unsigned char *)malloc(width * height * 6); | 
|---|
| 199 |     if (!pixels) { | 
|---|
| 200 |         // FIXME : out of memory | 
|---|
| 201 |         return false; | 
|---|
| 202 |     } | 
|---|
| 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 |     } | 
|---|
| 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 |  | 
|---|
| 224 |     return true; | 
|---|
| 225 | #else | 
|---|
| 226 |     return false; | 
|---|
| 227 | #endif | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | unsigned char * MovieMaker::GetBuffer() const { | 
|---|
| 231 |     AVCodecContext * c = st->codec; | 
|---|
| 232 |     return pixels + c->height * c->width * 3; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | int MovieMaker::GetWidth() const { | 
|---|
| 236 |     assert(st); | 
|---|
| 237 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H | 
|---|
| 238 |     AVCodecContext *c = st->codec; | 
|---|
| 239 |     return c->width; | 
|---|
| 240 | #else | 
|---|
| 241 |     return 0; | 
|---|
| 242 | #endif | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | int MovieMaker::GetHeight() const { | 
|---|
| 246 |     assert(st); | 
|---|
| 247 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H | 
|---|
| 248 |     AVCodecContext *c = st->codec; | 
|---|
| 249 |     return c->height; | 
|---|
| 250 | #else | 
|---|
| 251 |     return 0; | 
|---|
| 252 | #endif | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | void MovieMaker::AddFrame() | 
|---|
| 256 | { | 
|---|
| 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 |     } | 
|---|
| 264 |  | 
|---|
| 265 |     int len = 3 * c->width; | 
|---|
| 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 |         } | 
|---|
| 276 |     } | 
|---|
| 277 |     sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); | 
|---|
| 278 |  | 
|---|
| 279 |     if (oc->oformat->flags & AVFMT_RAWPICTURE) { | 
|---|
| 280 |         abort(); | 
|---|
| 281 |     } | 
|---|
| 282 |  | 
|---|
| 283 |     // Encode this frame. | 
|---|
| 284 |     out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); | 
|---|
| 285 |     // outsize == 0 means that this frame has been buffered, so there's nothing | 
|---|
| 286 |     // to write yet. | 
|---|
| 287 |     if (out_size) { | 
|---|
| 288 |         // Write the compressed frame to the media file. | 
|---|
| 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(); | 
|---|
| 303 |         } | 
|---|
| 304 |     } | 
|---|
| 305 | #endif | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | MovieMaker::~MovieMaker() | 
|---|
| 309 | { | 
|---|
| 310 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H | 
|---|
| 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. | 
|---|
| 314 |         AVCodecContext * c = st->codec; | 
|---|
| 315 |  | 
|---|
| 316 |         while (out_size) { | 
|---|
| 317 |             out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); | 
|---|
| 318 |             if (out_size) { | 
|---|
| 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(); | 
|---|
| 334 |                 } | 
|---|
| 335 |             } | 
|---|
| 336 |         } | 
|---|
| 337 |  | 
|---|
| 338 |         av_write_trailer(oc); | 
|---|
| 339 |  | 
|---|
| 340 |         // Close codec. | 
|---|
| 341 |         avcodec_close(c); | 
|---|
| 342 |     } | 
|---|
| 343 |  | 
|---|
| 344 |     if (frame) { | 
|---|
| 345 |         free(frame->data[0]); | 
|---|
| 346 |         free(frame); | 
|---|
| 347 |     } | 
|---|
| 348 |     free(outbuf); | 
|---|
| 349 |     free(pixels); | 
|---|
| 350 |     av_free(sws_ctx); | 
|---|
| 351 |  | 
|---|
| 352 |     if (oc) { | 
|---|
| 353 |         // Free the streams. | 
|---|
| 354 |         for (size_t i = 0; i < oc->nb_streams; ++i) { | 
|---|
| 355 |             av_freep(&oc->streams[i]->codec); | 
|---|
| 356 |             av_freep(&oc->streams[i]); | 
|---|
| 357 |         } | 
|---|
| 358 |  | 
|---|
| 359 |         if (!(oc->oformat->flags & AVFMT_NOFILE)) { | 
|---|
| 360 |             // Close the output file. | 
|---|
| 361 |             url_fclose(oc->pb); | 
|---|
| 362 |         } | 
|---|
| 363 |  | 
|---|
| 364 |         // Free the stream. | 
|---|
| 365 |         av_free(oc); | 
|---|
| 366 |     } | 
|---|
| 367 | #endif | 
|---|
| 368 | } | 
|---|