source: git/src/moviemaker.cc @ ec81f086

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

src/moviemaker.cc: Fix to call avcodec_free_frame() correctly.

  • Property mode set to 100644
File size: 14.3 KB
Line 
1//
2//  moviemaker.cc
3//
4//  Class for writing movies from Aven.
5//
6//  Copyright (C) 2004,2011,2012,2013 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    // B frames are backwards predicted - they can improve compression,
193    // but may slow encoding and decoding.
194    // if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
195    //     c->max_b_frames = 2;
196    // }
197
198    /* Some formats want stream headers to be separate. */
199    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
200        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
201
202    int retval;
203#ifndef HAVE_AVFORMAT_WRITE_HEADER
204    // Set the output parameters (must be done even if no parameters).
205    retval = av_set_parameters(oc, NULL);
206    if (retval < 0) {
207        averrno = retval;
208        return false;
209    }
210#endif
211
212    retval = avcodec_open2(c, NULL, NULL);
213    if (retval < 0) {
214        averrno = retval;
215        return false;
216    }
217
218#ifndef HAVE_AVCODEC_ENCODE_VIDEO2
219    outbuf = NULL;
220    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
221        outbuf = (unsigned char *)malloc(OUTBUF_SIZE);
222        if (!outbuf) {
223            averrno = AVERROR(ENOMEM);
224            return false;
225        }
226    }
227#endif
228
229    /* Allocate the encoded raw picture. */
230    frame = avcodec_alloc_frame();
231    if (!frame) {
232        averrno = AVERROR(ENOMEM);
233        return false;
234    }
235    retval = avpicture_alloc((AVPicture *)frame, c->pix_fmt, c->width, c->height);
236    if (retval < 0) {
237        averrno = retval;
238        return false;
239    }
240
241    if (c->pix_fmt != PIX_FMT_YUV420P) {
242        // FIXME need to allocate another frame for this case if we stop
243        // hardcoding PIX_FMT_YUV420P.
244        abort();
245    }
246
247    pixels = (unsigned char *)malloc(width * height * 6);
248    if (!pixels) {
249        averrno = AVERROR(ENOMEM);
250        return false;
251    }
252
253    // Show the format we've ended up with (for debug purposes).
254    // av_dump_format(oc, 0, fnm, 1);
255
256    av_free(sws_ctx);
257    sws_ctx = sws_getContext(width, height, PIX_FMT_RGB24,
258                             width, height, c->pix_fmt, SWS_BICUBIC,
259                             NULL, NULL, NULL);
260    if (sws_ctx == NULL) {
261        fprintf(stderr, "Cannot initialize the conversion context!\n");
262        averrno = AVERROR(ENOMEM);
263        return false;
264    }
265
266    if (!(fmt->flags & AVFMT_NOFILE)) {
267        retval = avio_open(&oc->pb, fnm, AVIO_FLAG_WRITE);
268        if (retval < 0) {
269            averrno = retval;
270            return false;
271        }
272    }
273
274    // Write the stream header, if any.
275#ifdef HAVE_AVFORMAT_WRITE_HEADER
276    retval = avformat_write_header(oc, NULL);
277#else
278    retval = av_write_header(oc);
279#endif
280    if (retval < 0) {
281        averrno = retval;
282        return false;
283    }
284
285    averrno = 0;
286    return true;
287#else
288    (void)fnm;
289    (void)width;
290    (void)height;
291    averrno = MOVIE_NOT_ENABLED;
292    return false;
293#endif
294}
295
296unsigned char * MovieMaker::GetBuffer() const {
297#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
298    AVCodecContext * c = video_st->codec;
299    return pixels + c->height * c->width * 3;
300#else
301    return NULL;
302#endif
303}
304
305int MovieMaker::GetWidth() const {
306    assert(video_st);
307#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
308    AVCodecContext *c = video_st->codec;
309    return c->width;
310#else
311    return 0;
312#endif
313}
314
315int MovieMaker::GetHeight() const {
316    assert(video_st);
317#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
318    AVCodecContext *c = video_st->codec;
319    return c->height;
320#else
321    return 0;
322#endif
323}
324
325bool MovieMaker::AddFrame()
326{
327#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
328    AVCodecContext * c = video_st->codec;
329
330    if (c->pix_fmt != PIX_FMT_YUV420P) {
331        // FIXME convert...
332        abort();
333    }
334
335    int len = 3 * c->width;
336    {
337        // Flip image vertically
338        int h = c->height;
339        unsigned char * src = pixels + h * len;
340        unsigned char * dest = src - len;
341        while (h--) {
342            memcpy(dest, src, len);
343            src += len;
344            dest -= len;
345        }
346    }
347    sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize);
348
349    if (oc->oformat->flags & AVFMT_RAWPICTURE) {
350        abort();
351    }
352
353    // Encode this frame.
354#ifdef HAVE_AVCODEC_ENCODE_VIDEO2
355    AVPacket pkt = { 0 };
356    int got_packet;
357    av_init_packet(&pkt);
358
359    int ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
360    if (ret < 0) {
361        averrno = ret;
362        return false;
363    }
364    if (got_packet && pkt.size) {
365        // Write the compressed frame to the media file.
366        if (pkt.pts != AV_NOPTS_VALUE) {
367            pkt.pts = av_rescale_q(pkt.pts,
368                                   c->time_base, video_st->time_base);
369        }
370        if (pkt.dts != AV_NOPTS_VALUE) {
371            pkt.dts = av_rescale_q(pkt.dts,
372                                   c->time_base, video_st->time_base);
373        }
374        pkt.stream_index = video_st->index;
375
376        /* Write the compressed frame to the media file. */
377        ret = av_interleaved_write_frame(oc, &pkt);
378        if (ret < 0) {
379            averrno = ret;
380            return false;
381        }
382    }
383#else
384    out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame);
385    // outsize == 0 means that this frame has been buffered, so there's nothing
386    // to write yet.
387    if (out_size) {
388        // Write the compressed frame to the media file.
389        AVPacket pkt;
390        av_init_packet(&pkt);
391
392        if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
393            pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
394        if (c->coded_frame->key_frame)
395            pkt.flags |= AV_PKT_FLAG_KEY;
396        pkt.stream_index = video_st->index;
397        pkt.data = outbuf;
398        pkt.size = out_size;
399
400        /* Write the compressed frame to the media file. */
401        int ret = av_interleaved_write_frame(oc, &pkt);
402        if (ret < 0) {
403            averrno = ret;
404            return false;
405        }
406    }
407#endif
408#endif
409    return true;
410}
411
412bool
413MovieMaker::Close()
414{
415#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
416    if (video_st && averrno == 0) {
417        // No more frames to compress.  The codec may have a few frames
418        // buffered if we're using B frames, so write those too.
419        AVCodecContext * c = video_st->codec;
420
421#ifdef HAVE_AVCODEC_ENCODE_VIDEO2
422        while (1) {
423            AVPacket pkt = { 0 };
424            int got_packet;
425            av_init_packet(&pkt);
426
427            int ret = avcodec_encode_video2(c, &pkt, NULL, &got_packet);
428            if (ret < 0) {
429                release();
430                averrno = ret;
431                return false;
432            }
433            if (!got_packet) break;
434            if (!pkt.size) continue;
435
436            // Write the compressed frame to the media file.
437            if (pkt.pts != AV_NOPTS_VALUE) {
438                pkt.pts = av_rescale_q(pkt.pts,
439                                       c->time_base, video_st->time_base);
440            }
441            if (pkt.dts != AV_NOPTS_VALUE) {
442                pkt.dts = av_rescale_q(pkt.dts,
443                                       c->time_base, video_st->time_base);
444            }
445            pkt.stream_index = video_st->index;
446
447            /* Write the compressed frame to the media file. */
448            ret = av_interleaved_write_frame(oc, &pkt);
449            if (ret < 0) {
450                release();
451                averrno = ret;
452                return false;
453            }
454        }
455#else
456        while (out_size) {
457            out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL);
458            if (out_size) {
459                // Write the compressed frame to the media file.
460                AVPacket pkt;
461                av_init_packet(&pkt);
462
463                if (c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
464                    pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
465                if (c->coded_frame->key_frame)
466                    pkt.flags |= AV_PKT_FLAG_KEY;
467                pkt.stream_index = video_st->index;
468                pkt.data = outbuf;
469                pkt.size = out_size;
470
471                /* write the compressed frame in the media file */
472                int ret = av_interleaved_write_frame(oc, &pkt);
473                if (ret < 0) {
474                    release();
475                    averrno = ret;
476                    return false;
477                }
478            }
479        }
480#endif
481
482        av_write_trailer(oc);
483    }
484
485    release();
486#endif
487    return true;
488}
489
490void
491MovieMaker::release()
492{
493#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
494    if (video_st) {
495        // Close codec.
496        avcodec_close(video_st->codec);
497        video_st = NULL;
498    }
499
500    if (frame) {
501        avcodec_free_frame(&frame);
502    }
503    free(pixels);
504    pixels = NULL;
505    free(outbuf);
506    outbuf = NULL;
507    av_free(sws_ctx);
508    sws_ctx = NULL;
509
510    if (oc) {
511        // Free the streams.
512        for (size_t i = 0; i < oc->nb_streams; ++i) {
513            av_freep(&oc->streams[i]->codec);
514            av_freep(&oc->streams[i]);
515        }
516
517        if (!(oc->oformat->flags & AVFMT_NOFILE)) {
518            // Close the output file.
519            avio_close(oc->pb);
520        }
521
522        // Free the stream.
523        av_free(oc);
524        oc = NULL;
525    }
526#endif
527}
528
529MovieMaker::~MovieMaker()
530{
531    release();
532}
533
534const char *
535MovieMaker::get_error_string() const
536{
537#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
538    switch (averrno) {
539        case AVERROR(EIO):
540            return "I/O error";
541        case AVERROR(EDOM):
542            return "Number syntax expected in filename";
543        case AVERROR_INVALIDDATA:
544            /* same as AVERROR_UNKNOWN: return "unknown error"; */
545            return "invalid data found";
546        case AVERROR(ENOMEM):
547            return "not enough memory";
548        case AVERROR(EILSEQ):
549            return "unknown format";
550        case AVERROR(ENOSYS):
551            return "Operation not supported";
552        case AVERROR(ENOENT):
553            return "No such file or directory";
554        case AVERROR_EOF:
555            return "End of file";
556        case AVERROR_PATCHWELCOME:
557            return "Not implemented in FFmpeg";
558        case 0:
559            return "No error";
560        case MOVIE_NO_SUITABLE_FORMAT:
561            return "Couldn't find a suitable output format";
562        case MOVIE_AUDIO_ONLY:
563            return "Audio-only format specified";
564        case MOVIE_FILENAME_TOO_LONG:
565            return "Filename too long";
566        case MOVIE_NOT_ENABLED:
567            return "Movie export support not included";
568    }
569    return "Unknown error";
570#else
571    return "Movie generation support code not present";
572#endif
573}
Note: See TracBrowser for help on using the repository browser.