4 // Class for writing movies from Aven.
6 // Copyright (C) 2004,2011,2012,2013,2014,2015,2016,2018 Olly Betts
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.
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.
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
23 /* Based on output-example.c:
25 * Libavformat API example: Output a media file in any supported
26 * libavformat format. The default codecs are used.
28 * Copyright (c) 2003 Fabrice Bellard
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:
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
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
53 #define __STDC_CONSTANT_MACROS
59 #include "moviemaker.h"
63 # include <libavutil/imgutils.h>
64 # include <libavutil/mathematics.h>
65 # include <libavformat/avformat.h>
66 # include <libswscale/swscale.h>
70 // Handle the "no libav/FFmpeg" case in this file.
71 #if !defined WITH_LIBAV || LIBAVCODEC_VERSION_MAJOR >= 57
75 MOVIE_NO_SUITABLE_FORMAT
= 1,
77 MOVIE_FILENAME_TOO_LONG
81 MovieMaker::MovieMaker()
83 : oc(0), video_st(0), context(0), frame(0), pixels(0), sws_ctx(0), averrno(0)
87 static bool initialised_ffmpeg
= false;
88 if (initialised_ffmpeg
) return;
90 // FIXME: register only the codec(s) we want to use...
91 avcodec_register_all();
94 initialised_ffmpeg
= true;
100 write_packet(void *opaque
, uint8_t *buf
, int buf_size
) {
101 FILE * fh
= (FILE*)opaque
;
102 size_t res
= fwrite(buf
, 1, buf_size
, fh
);
103 return res
> 0 ? res
: -1;
107 seek_stream(void *opaque
, int64_t offset
, int whence
) {
108 FILE * fh
= (FILE*)opaque
;
109 return fseek(fh
, offset
, whence
);
113 #define MAX_EXTENSION_LEN 8
115 bool MovieMaker::Open(FILE* fh
, const char * ext
, int width
, int height
)
120 /* Allocate the output media context. */
121 char dummy_filename
[MAX_EXTENSION_LEN
+ 3] = "x.";
123 if (strlen(ext
) <= MAX_EXTENSION_LEN
) {
124 // Use "x." + extension for format detection to avoid having to deal
125 // with wide character filenames.
126 strcpy(dummy_filename
+ 2, ext
);
127 avformat_alloc_output_context2(&oc
, NULL
, NULL
, dummy_filename
);
130 averrno
= MOVIE_NO_SUITABLE_FORMAT
;
134 AVOutputFormat
* fmt
= oc
->oformat
;
135 if (fmt
->video_codec
== AV_CODEC_ID_NONE
) {
136 averrno
= MOVIE_AUDIO_ONLY
;
140 /* find the video encoder */
141 AVCodec
*codec
= avcodec_find_encoder(fmt
->video_codec
);
143 // FIXME : Erm - internal ffmpeg library problem?
144 averrno
= AVERROR(ENOMEM
);
148 // Add the video stream.
149 video_st
= avformat_new_stream(oc
, NULL
);
151 averrno
= AVERROR(ENOMEM
);
155 context
= avcodec_alloc_context3(codec
);
156 context
->codec_id
= fmt
->video_codec
;
157 context
->width
= width
;
158 context
->height
= height
;
159 video_st
->time_base
.den
= 25; // Frames per second.
160 video_st
->time_base
.num
= 1;
161 context
->time_base
= video_st
->time_base
;
162 context
->bit_rate
= width
* height
* (4 * 0.07) * context
->time_base
.den
/ context
->time_base
.num
;
163 context
->bit_rate_tolerance
= context
->bit_rate
;
164 context
->global_quality
= 4;
165 context
->rc_buffer_size
= 2 * 1024 * 1024;
166 context
->rc_max_rate
= context
->bit_rate
* 8;
167 context
->gop_size
= 50; /* Twice the framerate */
168 context
->pix_fmt
= AV_PIX_FMT_YUV420P
;
169 if (context
->has_b_frames
) {
170 // B frames are backwards predicted - they can improve compression,
171 // but may slow encoding and decoding.
172 context
->max_b_frames
= 4;
175 /* Some formats want stream headers to be separate. */
176 if (oc
->oformat
->flags
& AVFMT_GLOBALHEADER
)
177 context
->flags
|= AV_CODEC_FLAG_GLOBAL_HEADER
;
180 retval
= avcodec_open2(context
, codec
, NULL
);
186 /* Allocate the encoded raw picture. */
187 frame
= av_frame_alloc();
189 averrno
= AVERROR(ENOMEM
);
193 frame
->format
= context
->pix_fmt
;
194 frame
->width
= width
;
195 frame
->height
= height
;
198 retval
= av_frame_get_buffer(frame
, 32);
204 if (frame
->format
!= AV_PIX_FMT_YUV420P
) {
205 // FIXME need to allocate another frame for this case if we stop
206 // hardcoding AV_PIX_FMT_YUV420P.
210 /* copy the stream parameters to the muxer */
211 retval
= avcodec_parameters_from_context(video_st
->codecpar
, context
);
217 pixels
= (unsigned char *)av_malloc(width
* height
* 6);
219 averrno
= AVERROR(ENOMEM
);
223 // Show the format we've ended up with (for debug purposes).
224 // av_dump_format(oc, 0, dummy_filename, 1);
227 sws_ctx
= sws_getContext(width
, height
, AV_PIX_FMT_RGB24
,
228 width
, height
, context
->pix_fmt
, SWS_BICUBIC
,
230 if (sws_ctx
== NULL
) {
231 fprintf(stderr
, "Cannot initialize the conversion context!\n");
232 averrno
= AVERROR(ENOMEM
);
236 if (!(fmt
->flags
& AVFMT_NOFILE
)) {
237 const int buf_size
= 8192;
238 void * buf
= av_malloc(buf_size
);
239 oc
->pb
= avio_alloc_context(static_cast<uint8_t*>(buf
), buf_size
, 1,
240 fh
, NULL
, write_packet
, seek_stream
);
242 averrno
= AVERROR(ENOMEM
);
247 // Write the stream header, if any.
248 retval
= avformat_write_header(oc
, NULL
);
265 unsigned char * MovieMaker::GetBuffer() const {
267 return pixels
+ GetWidth() * GetHeight() * 3;
273 int MovieMaker::GetWidth() const {
276 return video_st
->codecpar
->width
;
282 int MovieMaker::GetHeight() const {
285 return video_st
->codecpar
->height
;
292 // Call with frame=NULL when done.
294 MovieMaker::encode_frame(AVFrame
* frame_or_null
)
296 int ret
= avcodec_send_frame(context
, frame_or_null
);
297 if (ret
< 0) return ret
;
299 AVPacket
*pkt
= av_packet_alloc();
301 while ((ret
= avcodec_receive_packet(context
, pkt
)) == 0) {
302 // Rescale output packet timestamp values from codec to stream timebase.
303 av_packet_rescale_ts(pkt
, context
->time_base
, video_st
->time_base
);
304 pkt
->stream_index
= video_st
->index
;
306 // Write the compressed frame to the media file.
307 ret
= av_interleaved_write_frame(oc
, pkt
);
309 av_packet_free(&pkt
);
314 av_packet_free(&pkt
);
319 bool MovieMaker::AddFrame()
322 int ret
= av_frame_make_writable(frame
);
328 enum AVPixelFormat pix_fmt
= context
->pix_fmt
;
330 if (pix_fmt
!= AV_PIX_FMT_YUV420P
) {
335 int len
= 3 * GetWidth();
337 // Flip image vertically
339 unsigned char * src
= pixels
+ h
* len
;
340 unsigned char * dest
= src
- len
;
342 memcpy(dest
, src
, len
);
347 sws_scale(sws_ctx
, &pixels
, &len
, 0, GetHeight(),
348 frame
->data
, frame
->linesize
);
352 // Encode this frame.
353 ret
= encode_frame(frame
);
366 if (video_st
&& averrno
== 0) {
367 // Flush out any remaining data.
368 int ret
= encode_frame(NULL
);
373 av_write_trailer(oc
);
383 MovieMaker::release()
386 avcodec_free_context(&context
);
387 av_frame_free(&frame
);
390 sws_freeContext(sws_ctx
);
394 avformat_free_context(oc
);
404 MovieMaker::~MovieMaker()
412 MovieMaker::get_error_string() const
419 return "Number syntax expected in filename";
420 case AVERROR_INVALIDDATA
:
421 /* same as AVERROR_UNKNOWN: return "unknown error"; */
422 return "invalid data found";
423 case AVERROR(ENOMEM
):
424 return "not enough memory";
425 case AVERROR(EILSEQ
):
426 return "unknown format";
427 case AVERROR(ENOSYS
):
428 return "Operation not supported";
429 case AVERROR(ENOENT
):
430 return "No such file or directory";
432 return "End of file";
433 case AVERROR_PATCHWELCOME
:
434 return "Not implemented in FFmpeg";
437 case MOVIE_NO_SUITABLE_FORMAT
:
438 return "Couldn't find a suitable output format";
439 case MOVIE_AUDIO_ONLY
:
440 return "Audio-only format specified";
441 case MOVIE_FILENAME_TOO_LONG
:
442 return "Filename too long";
445 return "Unknown error";
450 #include "moviemaker-legacy.cc"