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 <libavcodec/avcodec.h>
64 # include <libavutil/imgutils.h>
65 # include <libavutil/mathematics.h>
66 # include <libavformat/avformat.h>
67 # include <libswscale/swscale.h>
71 // Handle the "no FFmpeg" case in this file.
72 #if !defined WITH_FFMPEG || LIBAVCODEC_VERSION_MAJOR >= 57
76 MOVIE_NO_SUITABLE_FORMAT
= 1,
78 MOVIE_FILENAME_TOO_LONG
82 MovieMaker::MovieMaker()
84 : oc(0), video_st(0), context(0), frame(0), pixels(0), sws_ctx(0), averrno(0)
88 static bool initialised_ffmpeg
= false;
89 if (initialised_ffmpeg
) return;
91 #if LIBAVCODEC_VERSION_MAJOR < 58
92 avcodec_register_all();
96 initialised_ffmpeg
= true;
102 write_packet(void *opaque
, uint8_t *buf
, int buf_size
) {
103 FILE * fh
= (FILE*)opaque
;
104 size_t res
= fwrite(buf
, 1, buf_size
, fh
);
105 return res
> 0 ? res
: -1;
109 seek_stream(void *opaque
, int64_t offset
, int whence
) {
110 FILE * fh
= (FILE*)opaque
;
111 return fseek(fh
, offset
, whence
);
115 #define MAX_EXTENSION_LEN 8
117 bool MovieMaker::Open(FILE* fh
, const char * ext
, int width
, int height
)
122 /* Allocate the output media context. */
123 char dummy_filename
[MAX_EXTENSION_LEN
+ 3] = "x.";
125 if (strlen(ext
) <= MAX_EXTENSION_LEN
) {
126 // Use "x." + extension for format detection to avoid having to deal
127 // with wide character filenames.
128 strcpy(dummy_filename
+ 2, ext
);
129 avformat_alloc_output_context2(&oc
, NULL
, NULL
, dummy_filename
);
132 averrno
= MOVIE_NO_SUITABLE_FORMAT
;
136 auto fmt
= oc
->oformat
;
137 if (fmt
->video_codec
== AV_CODEC_ID_NONE
) {
138 averrno
= MOVIE_AUDIO_ONLY
;
142 /* find the video encoder */
143 auto codec
= avcodec_find_encoder(fmt
->video_codec
);
145 // FIXME : Erm - internal ffmpeg library problem?
146 averrno
= AVERROR(ENOMEM
);
150 // Add the video stream.
151 video_st
= avformat_new_stream(oc
, NULL
);
153 averrno
= AVERROR(ENOMEM
);
157 context
= avcodec_alloc_context3(codec
);
158 context
->codec_id
= fmt
->video_codec
;
159 context
->width
= width
;
160 context
->height
= height
;
161 video_st
->time_base
.den
= 25; // Frames per second.
162 video_st
->time_base
.num
= 1;
163 context
->time_base
= video_st
->time_base
;
164 context
->bit_rate
= width
* height
* (4 * 0.07) * context
->time_base
.den
/ context
->time_base
.num
;
165 context
->bit_rate_tolerance
= context
->bit_rate
;
166 context
->global_quality
= 4;
167 context
->rc_buffer_size
= 2 * 1024 * 1024;
168 context
->rc_max_rate
= context
->bit_rate
* 8;
169 context
->gop_size
= 50; /* Twice the framerate */
170 context
->pix_fmt
= AV_PIX_FMT_YUV420P
;
171 if (context
->has_b_frames
) {
172 // B frames are backwards predicted - they can improve compression,
173 // but may slow encoding and decoding.
174 context
->max_b_frames
= 4;
177 /* Some formats want stream headers to be separate. */
178 if (oc
->oformat
->flags
& AVFMT_GLOBALHEADER
)
179 context
->flags
|= AV_CODEC_FLAG_GLOBAL_HEADER
;
182 retval
= avcodec_open2(context
, codec
, NULL
);
188 /* Allocate the encoded raw picture. */
189 frame
= av_frame_alloc();
191 averrno
= AVERROR(ENOMEM
);
195 frame
->format
= context
->pix_fmt
;
196 frame
->width
= width
;
197 frame
->height
= height
;
200 retval
= av_frame_get_buffer(frame
, 32);
206 if (frame
->format
!= AV_PIX_FMT_YUV420P
) {
207 // FIXME need to allocate another frame for this case if we stop
208 // hardcoding AV_PIX_FMT_YUV420P.
212 /* copy the stream parameters to the muxer */
213 retval
= avcodec_parameters_from_context(video_st
->codecpar
, context
);
219 pixels
= (unsigned char *)av_malloc(width
* height
* 6);
221 averrno
= AVERROR(ENOMEM
);
225 // Show the format we've ended up with (for debug purposes).
226 // av_dump_format(oc, 0, dummy_filename, 1);
229 sws_ctx
= sws_getContext(width
, height
, AV_PIX_FMT_RGB24
,
230 width
, height
, context
->pix_fmt
, SWS_BICUBIC
,
232 if (sws_ctx
== NULL
) {
233 fprintf(stderr
, "Cannot initialize the conversion context!\n");
234 averrno
= AVERROR(ENOMEM
);
238 if (!(fmt
->flags
& AVFMT_NOFILE
)) {
239 const int buf_size
= 8192;
240 void * buf
= av_malloc(buf_size
);
241 oc
->pb
= avio_alloc_context(static_cast<uint8_t*>(buf
), buf_size
, 1,
242 fh
, NULL
, write_packet
, seek_stream
);
244 averrno
= AVERROR(ENOMEM
);
249 // Write the stream header, if any.
250 retval
= avformat_write_header(oc
, NULL
);
267 unsigned char * MovieMaker::GetBuffer() const {
269 return pixels
+ GetWidth() * GetHeight() * 3;
275 int MovieMaker::GetWidth() const {
278 return video_st
->codecpar
->width
;
284 int MovieMaker::GetHeight() const {
287 return video_st
->codecpar
->height
;
294 // Call with frame=NULL when done.
296 MovieMaker::encode_frame(AVFrame
* frame_or_null
)
298 int ret
= avcodec_send_frame(context
, frame_or_null
);
299 if (ret
< 0) return ret
;
301 AVPacket
*pkt
= av_packet_alloc();
303 while ((ret
= avcodec_receive_packet(context
, pkt
)) == 0) {
304 // Rescale output packet timestamp values from codec to stream timebase.
305 av_packet_rescale_ts(pkt
, context
->time_base
, video_st
->time_base
);
306 pkt
->stream_index
= video_st
->index
;
308 // Write the compressed frame to the media file.
309 ret
= av_interleaved_write_frame(oc
, pkt
);
311 av_packet_free(&pkt
);
316 av_packet_free(&pkt
);
321 bool MovieMaker::AddFrame()
324 int ret
= av_frame_make_writable(frame
);
330 enum AVPixelFormat pix_fmt
= context
->pix_fmt
;
332 if (pix_fmt
!= AV_PIX_FMT_YUV420P
) {
337 int len
= 3 * GetWidth();
339 // Flip image vertically
341 unsigned char * src
= pixels
+ h
* len
;
342 unsigned char * dest
= src
- len
;
344 memcpy(dest
, src
, len
);
349 sws_scale(sws_ctx
, &pixels
, &len
, 0, GetHeight(),
350 frame
->data
, frame
->linesize
);
354 // Encode this frame.
355 ret
= encode_frame(frame
);
368 if (video_st
&& averrno
== 0) {
369 // Flush out any remaining data.
370 int ret
= encode_frame(NULL
);
375 av_write_trailer(oc
);
385 MovieMaker::release()
388 avcodec_free_context(&context
);
389 av_frame_free(&frame
);
392 sws_freeContext(sws_ctx
);
396 avformat_free_context(oc
);
406 MovieMaker::~MovieMaker()
414 MovieMaker::get_error_string() const
421 return "Number syntax expected in filename";
422 case AVERROR_INVALIDDATA
:
423 /* same as AVERROR_UNKNOWN: return "unknown error"; */
424 return "invalid data found";
425 case AVERROR(ENOMEM
):
426 return "not enough memory";
427 case AVERROR(EILSEQ
):
428 return "unknown format";
429 case AVERROR(ENOSYS
):
430 return "Operation not supported";
431 case AVERROR(ENOENT
):
432 return "No such file or directory";
434 return "End of file";
435 case AVERROR_PATCHWELCOME
:
436 return "Not implemented in FFmpeg";
439 case MOVIE_NO_SUITABLE_FORMAT
:
440 return "Couldn't find a suitable output format";
441 case MOVIE_AUDIO_ONLY
:
442 return "Audio-only format specified";
443 case MOVIE_FILENAME_TOO_LONG
:
444 return "Filename too long";
447 return "Unknown error";
452 #include "moviemaker-legacy.cc"