source: git/src/moviemaker.cc @ 997509d

line_contentsstereotravis-osx
Last change on this file since 997509d was 997509d, checked in by Olly Betts <olly@…>, 6 years ago

src/moviemaker.cc: Set rc_buffer_size to avoid warning from libav
when exporting MPEG.

  • Property mode set to 100644
File size: 14.5 KB
Line 
1//
2//  moviemaker.cc
3//
4//  Class for writing movies from Aven.
5//
6//  Copyright (C) 2004,2011,2012,2013,2014 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
62extern "C" {
63# include <libavutil/mathematics.h>
64# include <libavformat/avformat.h>
65# include <libswscale/swscale.h>
66}
67# ifndef AV_PKT_FLAG_KEY
68#  define AV_PKT_FLAG_KEY PKT_FLAG_KEY
69# endif
70# ifndef HAVE_AV_GUESS_FORMAT
71#  define av_guess_format guess_format
72# endif
73# ifndef HAVE_AVIO_OPEN
74#  define avio_open url_fopen
75# endif
76# ifndef HAVE_AVIO_CLOSE
77#  define avio_close url_fclose
78# endif
79# ifndef HAVE_AVCODEC_FREE_FRAME
80static inline void avcodec_free_frame(AVFrame ** frame) {
81    free((*frame)->data[0]);
82    free(*frame);
83    *frame = NULL
84}
85# endif
86# ifndef HAVE_AVCODEC_OPEN2
87// We always pass NULL for OPTS below.
88#  define avcodec_open2(CTX, CODEC, OPTS) avcodec_open(CTX, CODEC)
89# endif
90# ifndef HAVE_AVFORMAT_NEW_STREAM
91// We always pass NULL for CODEC below.
92#  define avformat_new_stream(S, CODEC) av_new_stream(S, 0)
93# endif
94# if !HAVE_DECL_AVMEDIA_TYPE_VIDEO
95#  define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
96# endif
97# if !HAVE_DECL_AVCODEC_ID_NONE
98#  define AVCODEC_ID_NONE CODEC_ID_NONE
99# endif
100# if !HAVE_DECL_AV_PIX_FMT_YUV420P
101#  define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
102# endif
103# ifndef AVIO_FLAG_WRITE
104#  define AVIO_FLAG_WRITE URL_WRONLY
105# endif
106#endif
107
108enum {
109    MOVIE_NO_SUITABLE_FORMAT = 1,
110    MOVIE_AUDIO_ONLY,
111    MOVIE_FILENAME_TOO_LONG,
112    MOVIE_NOT_ENABLED
113};
114
115const int OUTBUF_SIZE = 200000;
116
117MovieMaker::MovieMaker()
118    : oc(0), video_st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0), averrno(0)
119{
120#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
121    static bool initialised_ffmpeg = false;
122    if (initialised_ffmpeg) return;
123
124    // FIXME: register only the codec(s) we want to use...
125    avcodec_register_all();
126    av_register_all();
127
128    initialised_ffmpeg = true;
129#endif
130}
131
132bool MovieMaker::Open(const char *fnm, int width, int height)
133{
134#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
135    AVOutputFormat * fmt = av_guess_format(NULL, fnm, NULL);
136    if (!fmt) {
137        // We couldn't deduce the output format from file extension so default
138        // to MPEG.
139        fmt = av_guess_format("mpeg", NULL, NULL);
140        if (!fmt) {
141            averrno = MOVIE_NO_SUITABLE_FORMAT;
142            return false;
143        }
144    }
145    if (fmt->video_codec == AVCODEC_ID_NONE) {
146        averrno = MOVIE_AUDIO_ONLY;
147        return false;
148    }
149
150    /* Allocate the output media context. */
151    oc = avformat_alloc_context();
152    if (!oc) {
153        averrno = AVERROR(ENOMEM);
154        return false;
155    }
156    oc->oformat = fmt;
157    if (strlen(fnm) >= sizeof(oc->filename)) {
158        averrno = MOVIE_FILENAME_TOO_LONG;
159        return false;
160    }
161    strcpy(oc->filename, fnm);
162
163    /* find the video encoder */
164    AVCodec *codec = avcodec_find_encoder(fmt->video_codec);
165    if (!codec) {
166        // FIXME : Erm - internal ffmpeg library problem?
167        averrno = AVERROR(ENOMEM);
168        return false;
169    }
170
171    // Add the video stream.
172    video_st = avformat_new_stream(oc, codec);
173    if (!video_st) {
174        averrno = AVERROR(ENOMEM);
175        return false;
176    }
177
178    // Set sample parameters.
179    AVCodecContext *c = video_st->codec;
180    c->bit_rate = 400000;
181    /* Resolution must be a multiple of two. */
182    c->width = width;
183    c->height = height;
184    /* timebase: This is the fundamental unit of time (in seconds) in terms
185     * of which frame timestamps are represented. For fixed-fps content,
186     * timebase should be 1/framerate and timestamp increments should be
187     * identical to 1. */
188    c->time_base.den = 25; // Frames per second.
189    c->time_base.num = 1;
190    c->gop_size = 12; /* emit one intra frame every twelve frames at most */
191    c->pix_fmt = AV_PIX_FMT_YUV420P;
192    c->rc_buffer_size = c->bit_rate * 4; // Enough for 4 seconds
193    // B frames are backwards predicted - they can improve compression,
194    // but may slow encoding and decoding.
195    // if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
196    //     c->max_b_frames = 2;
197    // }
198
199    /* Some formats want stream headers to be separate. */
200    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
201        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
202
203    int retval;
204#ifndef HAVE_AVFORMAT_WRITE_HEADER
205    // Set the output parameters (must be done even if no parameters).
206    retval = av_set_parameters(oc, NULL);
207    if (retval < 0) {
208        averrno = retval;
209        return false;
210    }
211#endif
212
213    retval = avcodec_open2(c, NULL, NULL);
214    if (retval < 0) {
215        averrno = retval;
216        return false;
217    }
218
219#ifndef HAVE_AVCODEC_ENCODE_VIDEO2
220    outbuf = NULL;
221    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
222        outbuf = (unsigned char *)malloc(OUTBUF_SIZE);
223        if (!outbuf) {
224            averrno = AVERROR(ENOMEM);
225            return false;
226        }
227    }
228#endif
229
230    /* Allocate the encoded raw picture. */
231    frame = avcodec_alloc_frame();
232    if (!frame) {
233        averrno = AVERROR(ENOMEM);
234        return false;
235    }
236    retval = avpicture_alloc((AVPicture *)frame, c->pix_fmt, c->width, c->height);
237    if (retval < 0) {
238        averrno = retval;
239        return false;
240    }
241
242    if (c->pix_fmt != PIX_FMT_YUV420P) {
243        // FIXME need to allocate another frame for this case if we stop
244        // hardcoding PIX_FMT_YUV420P.
245        abort();
246    }
247
248    pixels = (unsigned char *)malloc(width * height * 6);
249    if (!pixels) {
250        averrno = AVERROR(ENOMEM);
251        return false;
252    }
253
254    // Show the format we've ended up with (for debug purposes).
255    // av_dump_format(oc, 0, fnm, 1);
256
257    av_free(sws_ctx);
258    sws_ctx = sws_getContext(width, height, PIX_FMT_RGB24,
259                             width, height, c->pix_fmt, SWS_BICUBIC,
260                             NULL, NULL, NULL);
261    if (sws_ctx == NULL) {
262        fprintf(stderr, "Cannot initialize the conversion context!\n");
263        averrno = AVERROR(ENOMEM);
264        return false;
265    }
266
267    if (!(fmt->flags & AVFMT_NOFILE)) {
268        retval = avio_open(&oc->pb, fnm, AVIO_FLAG_WRITE);
269        if (retval < 0) {
270            averrno = retval;
271            return false;
272        }
273    }
274
275    // Write the stream header, if any.
276#ifdef HAVE_AVFORMAT_WRITE_HEADER
277    retval = avformat_write_header(oc, NULL);
278#else
279    retval = av_write_header(oc);
280#endif
281    if (retval < 0) {
282        averrno = retval;
283        return false;
284    }
285
286    averrno = 0;
287    return true;
288#else
289    (void)fnm;
290    (void)width;
291    (void)height;
292    averrno = MOVIE_NOT_ENABLED;
293    return false;
294#endif
295}
296
297unsigned char * MovieMaker::GetBuffer() const {
298#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
299    AVCodecContext * c = video_st->codec;
300    return pixels + c->height * c->width * 3;
301#else
302    return NULL;
303#endif
304}
305
306int MovieMaker::GetWidth() const {
307    assert(video_st);
308#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
309    AVCodecContext *c = video_st->codec;
310    return c->width;
311#else
312    return 0;
313#endif
314}
315
316int MovieMaker::GetHeight() const {
317    assert(video_st);
318#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
319    AVCodecContext *c = video_st->codec;
320    return c->height;
321#else
322    return 0;
323#endif
324}
325
326bool MovieMaker::AddFrame()
327{
328#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
329    AVCodecContext * c = video_st->codec;
330
331    if (c->pix_fmt != PIX_FMT_YUV420P) {
332        // FIXME convert...
333        abort();
334    }
335
336    int len = 3 * c->width;
337    {
338        // Flip image vertically
339        int h = c->height;
340        unsigned char * src = pixels + h * len;
341        unsigned char * dest = src - len;
342        while (h--) {
343            memcpy(dest, src, len);
344            src += len;
345            dest -= len;
346        }
347    }
348    sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize);
349
350    if (oc->oformat->flags & AVFMT_RAWPICTURE) {
351        abort();
352    }
353
354    // Encode this frame.
355#ifdef HAVE_AVCODEC_ENCODE_VIDEO2
356    AVPacket pkt;
357    int got_packet;
358    av_init_packet(&pkt);
359    pkt.data = NULL;
360
361    int ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
362    if (ret < 0) {
363        averrno = ret;
364        return false;
365    }
366    if (got_packet && pkt.size) {
367        // Write the compressed frame to the media file.
368        if (pkt.pts != int64_t(AV_NOPTS_VALUE)) {
369            pkt.pts = av_rescale_q(pkt.pts,
370                                   c->time_base, video_st->time_base);
371        }
372        if (pkt.dts != int64_t(AV_NOPTS_VALUE)) {
373            pkt.dts = av_rescale_q(pkt.dts,
374                                   c->time_base, video_st->time_base);
375        }
376        pkt.stream_index = video_st->index;
377
378        /* Write the compressed frame to the media file. */
379        ret = av_interleaved_write_frame(oc, &pkt);
380        if (ret < 0) {
381            averrno = ret;
382            return false;
383        }
384    }
385#else
386    out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame);
387    // outsize == 0 means that this frame has been buffered, so there's nothing
388    // to write yet.
389    if (out_size) {
390        // Write the compressed frame to the media file.
391        AVPacket pkt;
392        av_init_packet(&pkt);
393
394        if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
395            pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
396        if (c->coded_frame->key_frame)
397            pkt.flags |= AV_PKT_FLAG_KEY;
398        pkt.stream_index = video_st->index;
399        pkt.data = outbuf;
400        pkt.size = out_size;
401
402        /* Write the compressed frame to the media file. */
403        int ret = av_interleaved_write_frame(oc, &pkt);
404        if (ret < 0) {
405            averrno = ret;
406            return false;
407        }
408    }
409#endif
410#endif
411    return true;
412}
413
414bool
415MovieMaker::Close()
416{
417#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
418    if (video_st && averrno == 0) {
419        // No more frames to compress.  The codec may have a few frames
420        // buffered if we're using B frames, so write those too.
421        AVCodecContext * c = video_st->codec;
422
423#ifdef HAVE_AVCODEC_ENCODE_VIDEO2
424        while (1) {
425            AVPacket pkt;
426            int got_packet;
427            av_init_packet(&pkt);
428            pkt.data = NULL;
429
430            int ret = avcodec_encode_video2(c, &pkt, NULL, &got_packet);
431            if (ret < 0) {
432                release();
433                averrno = ret;
434                return false;
435            }
436            if (!got_packet) break;
437            if (!pkt.size) continue;
438
439            // Write the compressed frame to the media file.
440            if (pkt.pts != int64_t(AV_NOPTS_VALUE)) {
441                pkt.pts = av_rescale_q(pkt.pts,
442                                       c->time_base, video_st->time_base);
443            }
444            if (pkt.dts != int64_t(AV_NOPTS_VALUE)) {
445                pkt.dts = av_rescale_q(pkt.dts,
446                                       c->time_base, video_st->time_base);
447            }
448            pkt.stream_index = video_st->index;
449
450            /* Write the compressed frame to the media file. */
451            ret = av_interleaved_write_frame(oc, &pkt);
452            if (ret < 0) {
453                release();
454                averrno = ret;
455                return false;
456            }
457        }
458#else
459        while (out_size) {
460            out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL);
461            if (out_size) {
462                // Write the compressed frame to the media file.
463                AVPacket pkt;
464                av_init_packet(&pkt);
465
466                if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
467                    pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
468                if (c->coded_frame->key_frame)
469                    pkt.flags |= AV_PKT_FLAG_KEY;
470                pkt.stream_index = video_st->index;
471                pkt.data = outbuf;
472                pkt.size = out_size;
473
474                /* write the compressed frame in the media file */
475                int ret = av_interleaved_write_frame(oc, &pkt);
476                if (ret < 0) {
477                    release();
478                    averrno = ret;
479                    return false;
480                }
481            }
482        }
483#endif
484
485        av_write_trailer(oc);
486    }
487
488    release();
489#endif
490    return true;
491}
492
493void
494MovieMaker::release()
495{
496#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
497    if (video_st) {
498        // Close codec.
499        avcodec_close(video_st->codec);
500        video_st = NULL;
501    }
502
503    if (frame) {
504        avcodec_free_frame(&frame);
505    }
506    free(pixels);
507    pixels = NULL;
508    free(outbuf);
509    outbuf = NULL;
510    av_free(sws_ctx);
511    sws_ctx = NULL;
512
513    if (oc) {
514        // Free the streams.
515        for (size_t i = 0; i < oc->nb_streams; ++i) {
516            av_freep(&oc->streams[i]->codec);
517            av_freep(&oc->streams[i]);
518        }
519
520        if (!(oc->oformat->flags & AVFMT_NOFILE)) {
521            // Close the output file.
522            avio_close(oc->pb);
523        }
524
525        // Free the stream.
526        av_free(oc);
527        oc = NULL;
528    }
529#endif
530}
531
532MovieMaker::~MovieMaker()
533{
534    release();
535}
536
537const char *
538MovieMaker::get_error_string() const
539{
540#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
541    switch (averrno) {
542        case AVERROR(EIO):
543            return "I/O error";
544        case AVERROR(EDOM):
545            return "Number syntax expected in filename";
546        case AVERROR_INVALIDDATA:
547            /* same as AVERROR_UNKNOWN: return "unknown error"; */
548            return "invalid data found";
549        case AVERROR(ENOMEM):
550            return "not enough memory";
551        case AVERROR(EILSEQ):
552            return "unknown format";
553        case AVERROR(ENOSYS):
554            return "Operation not supported";
555        case AVERROR(ENOENT):
556            return "No such file or directory";
557        case AVERROR_EOF:
558            return "End of file";
559        case AVERROR_PATCHWELCOME:
560            return "Not implemented in FFmpeg";
561        case 0:
562            return "No error";
563        case MOVIE_NO_SUITABLE_FORMAT:
564            return "Couldn't find a suitable output format";
565        case MOVIE_AUDIO_ONLY:
566            return "Audio-only format specified";
567        case MOVIE_FILENAME_TOO_LONG:
568            return "Filename too long";
569        case MOVIE_NOT_ENABLED:
570            return "Movie export support not included";
571    }
572    return "Unknown error";
573#else
574    return "Movie generation support code not present";
575#endif
576}
Note: See TracBrowser for help on using the repository browser.