1 | // |
---|
2 | // moviemaker.cc |
---|
3 | // |
---|
4 | // Class for writing movies from Aven. |
---|
5 | // |
---|
6 | // Copyright (C) 2004,2011,2012,2013,2014,2015,2016,2018 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 WITH_LIBAV |
---|
62 | extern "C" { |
---|
63 | # include <libavutil/imgutils.h> |
---|
64 | # include <libavutil/mathematics.h> |
---|
65 | # include <libavformat/avformat.h> |
---|
66 | # include <libswscale/swscale.h> |
---|
67 | } |
---|
68 | #endif |
---|
69 | |
---|
70 | // Handle the "no libav/FFmpeg" case in this file. |
---|
71 | #if !defined WITH_LIBAV || LIBAVCODEC_VERSION_MAJOR >= 57 |
---|
72 | |
---|
73 | #ifdef WITH_LIBAV |
---|
74 | enum { |
---|
75 | MOVIE_NO_SUITABLE_FORMAT = 1, |
---|
76 | MOVIE_AUDIO_ONLY, |
---|
77 | MOVIE_FILENAME_TOO_LONG |
---|
78 | }; |
---|
79 | #endif |
---|
80 | |
---|
81 | MovieMaker::MovieMaker() |
---|
82 | #ifdef WITH_LIBAV |
---|
83 | : oc(0), video_st(0), context(0), frame(0), pixels(0), sws_ctx(0), averrno(0) |
---|
84 | #endif |
---|
85 | { |
---|
86 | #ifdef WITH_LIBAV |
---|
87 | static bool initialised_ffmpeg = false; |
---|
88 | if (initialised_ffmpeg) return; |
---|
89 | |
---|
90 | #if LIBAVCODEC_VERSION_MAJOR < 58 |
---|
91 | avcodec_register_all(); |
---|
92 | av_register_all(); |
---|
93 | #endif |
---|
94 | |
---|
95 | initialised_ffmpeg = true; |
---|
96 | #endif |
---|
97 | } |
---|
98 | |
---|
99 | #ifdef WITH_LIBAV |
---|
100 | static int |
---|
101 | write_packet(void *opaque, uint8_t *buf, int buf_size) { |
---|
102 | FILE * fh = (FILE*)opaque; |
---|
103 | size_t res = fwrite(buf, 1, buf_size, fh); |
---|
104 | return res > 0 ? res : -1; |
---|
105 | } |
---|
106 | |
---|
107 | static int64_t |
---|
108 | seek_stream(void *opaque, int64_t offset, int whence) { |
---|
109 | FILE * fh = (FILE*)opaque; |
---|
110 | return fseek(fh, offset, whence); |
---|
111 | } |
---|
112 | #endif |
---|
113 | |
---|
114 | #define MAX_EXTENSION_LEN 8 |
---|
115 | |
---|
116 | bool MovieMaker::Open(FILE* fh, const char * ext, int width, int height) |
---|
117 | { |
---|
118 | #ifdef WITH_LIBAV |
---|
119 | fh_to_close = fh; |
---|
120 | |
---|
121 | /* Allocate the output media context. */ |
---|
122 | char dummy_filename[MAX_EXTENSION_LEN + 3] = "x."; |
---|
123 | oc = NULL; |
---|
124 | if (strlen(ext) <= MAX_EXTENSION_LEN) { |
---|
125 | // Use "x." + extension for format detection to avoid having to deal |
---|
126 | // with wide character filenames. |
---|
127 | strcpy(dummy_filename + 2, ext); |
---|
128 | avformat_alloc_output_context2(&oc, NULL, NULL, dummy_filename); |
---|
129 | } |
---|
130 | if (!oc) { |
---|
131 | averrno = MOVIE_NO_SUITABLE_FORMAT; |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | AVOutputFormat * fmt = oc->oformat; |
---|
136 | if (fmt->video_codec == AV_CODEC_ID_NONE) { |
---|
137 | averrno = MOVIE_AUDIO_ONLY; |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | /* find the video encoder */ |
---|
142 | AVCodec *codec = avcodec_find_encoder(fmt->video_codec); |
---|
143 | if (!codec) { |
---|
144 | // FIXME : Erm - internal ffmpeg library problem? |
---|
145 | averrno = AVERROR(ENOMEM); |
---|
146 | return false; |
---|
147 | } |
---|
148 | |
---|
149 | // Add the video stream. |
---|
150 | video_st = avformat_new_stream(oc, NULL); |
---|
151 | if (!video_st) { |
---|
152 | averrno = AVERROR(ENOMEM); |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | context = avcodec_alloc_context3(codec); |
---|
157 | context->codec_id = fmt->video_codec; |
---|
158 | context->width = width; |
---|
159 | context->height = height; |
---|
160 | video_st->time_base.den = 25; // Frames per second. |
---|
161 | video_st->time_base.num = 1; |
---|
162 | context->time_base = video_st->time_base; |
---|
163 | context->bit_rate = width * height * (4 * 0.07) * context->time_base.den / context->time_base.num; |
---|
164 | context->bit_rate_tolerance = context->bit_rate; |
---|
165 | context->global_quality = 4; |
---|
166 | context->rc_buffer_size = 2 * 1024 * 1024; |
---|
167 | context->rc_max_rate = context->bit_rate * 8; |
---|
168 | context->gop_size = 50; /* Twice the framerate */ |
---|
169 | context->pix_fmt = AV_PIX_FMT_YUV420P; |
---|
170 | if (context->has_b_frames) { |
---|
171 | // B frames are backwards predicted - they can improve compression, |
---|
172 | // but may slow encoding and decoding. |
---|
173 | context->max_b_frames = 4; |
---|
174 | } |
---|
175 | |
---|
176 | /* Some formats want stream headers to be separate. */ |
---|
177 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
---|
178 | context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
---|
179 | |
---|
180 | int retval; |
---|
181 | retval = avcodec_open2(context, codec, NULL); |
---|
182 | if (retval < 0) { |
---|
183 | averrno = retval; |
---|
184 | return false; |
---|
185 | } |
---|
186 | |
---|
187 | /* Allocate the encoded raw picture. */ |
---|
188 | frame = av_frame_alloc(); |
---|
189 | if (!frame) { |
---|
190 | averrno = AVERROR(ENOMEM); |
---|
191 | return false; |
---|
192 | } |
---|
193 | |
---|
194 | frame->format = context->pix_fmt; |
---|
195 | frame->width = width; |
---|
196 | frame->height = height; |
---|
197 | frame->pts = 0; |
---|
198 | |
---|
199 | retval = av_frame_get_buffer(frame, 32); |
---|
200 | if (retval < 0) { |
---|
201 | averrno = retval; |
---|
202 | return false; |
---|
203 | } |
---|
204 | |
---|
205 | if (frame->format != AV_PIX_FMT_YUV420P) { |
---|
206 | // FIXME need to allocate another frame for this case if we stop |
---|
207 | // hardcoding AV_PIX_FMT_YUV420P. |
---|
208 | abort(); |
---|
209 | } |
---|
210 | |
---|
211 | /* copy the stream parameters to the muxer */ |
---|
212 | retval = avcodec_parameters_from_context(video_st->codecpar, context); |
---|
213 | if (retval < 0) { |
---|
214 | averrno = retval; |
---|
215 | return false; |
---|
216 | } |
---|
217 | |
---|
218 | pixels = (unsigned char *)av_malloc(width * height * 6); |
---|
219 | if (!pixels) { |
---|
220 | averrno = AVERROR(ENOMEM); |
---|
221 | return false; |
---|
222 | } |
---|
223 | |
---|
224 | // Show the format we've ended up with (for debug purposes). |
---|
225 | // av_dump_format(oc, 0, dummy_filename, 1); |
---|
226 | |
---|
227 | av_free(sws_ctx); |
---|
228 | sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24, |
---|
229 | width, height, context->pix_fmt, SWS_BICUBIC, |
---|
230 | NULL, NULL, NULL); |
---|
231 | if (sws_ctx == NULL) { |
---|
232 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
---|
233 | averrno = AVERROR(ENOMEM); |
---|
234 | return false; |
---|
235 | } |
---|
236 | |
---|
237 | if (!(fmt->flags & AVFMT_NOFILE)) { |
---|
238 | const int buf_size = 8192; |
---|
239 | void * buf = av_malloc(buf_size); |
---|
240 | oc->pb = avio_alloc_context(static_cast<uint8_t*>(buf), buf_size, 1, |
---|
241 | fh, NULL, write_packet, seek_stream); |
---|
242 | if (!oc->pb) { |
---|
243 | averrno = AVERROR(ENOMEM); |
---|
244 | return false; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | // Write the stream header, if any. |
---|
249 | retval = avformat_write_header(oc, NULL); |
---|
250 | if (retval < 0) { |
---|
251 | averrno = retval; |
---|
252 | return false; |
---|
253 | } |
---|
254 | |
---|
255 | averrno = 0; |
---|
256 | return true; |
---|
257 | #else |
---|
258 | (void)fh; |
---|
259 | (void)ext; |
---|
260 | (void)width; |
---|
261 | (void)height; |
---|
262 | return false; |
---|
263 | #endif |
---|
264 | } |
---|
265 | |
---|
266 | unsigned char * MovieMaker::GetBuffer() const { |
---|
267 | #ifdef WITH_LIBAV |
---|
268 | return pixels + GetWidth() * GetHeight() * 3; |
---|
269 | #else |
---|
270 | return NULL; |
---|
271 | #endif |
---|
272 | } |
---|
273 | |
---|
274 | int MovieMaker::GetWidth() const { |
---|
275 | #ifdef WITH_LIBAV |
---|
276 | assert(video_st); |
---|
277 | return video_st->codecpar->width; |
---|
278 | #else |
---|
279 | return 0; |
---|
280 | #endif |
---|
281 | } |
---|
282 | |
---|
283 | int MovieMaker::GetHeight() const { |
---|
284 | #ifdef WITH_LIBAV |
---|
285 | assert(video_st); |
---|
286 | return video_st->codecpar->height; |
---|
287 | #else |
---|
288 | return 0; |
---|
289 | #endif |
---|
290 | } |
---|
291 | |
---|
292 | #ifdef WITH_LIBAV |
---|
293 | // Call with frame=NULL when done. |
---|
294 | int |
---|
295 | MovieMaker::encode_frame(AVFrame* frame_or_null) |
---|
296 | { |
---|
297 | int ret = avcodec_send_frame(context, frame_or_null); |
---|
298 | if (ret < 0) return ret; |
---|
299 | |
---|
300 | AVPacket *pkt = av_packet_alloc(); |
---|
301 | pkt->size = 0; |
---|
302 | while ((ret = avcodec_receive_packet(context, pkt)) == 0) { |
---|
303 | // Rescale output packet timestamp values from codec to stream timebase. |
---|
304 | av_packet_rescale_ts(pkt, context->time_base, video_st->time_base); |
---|
305 | pkt->stream_index = video_st->index; |
---|
306 | |
---|
307 | // Write the compressed frame to the media file. |
---|
308 | ret = av_interleaved_write_frame(oc, pkt); |
---|
309 | if (ret < 0) { |
---|
310 | av_packet_free(&pkt); |
---|
311 | release(); |
---|
312 | return ret; |
---|
313 | } |
---|
314 | } |
---|
315 | av_packet_free(&pkt); |
---|
316 | return 0; |
---|
317 | } |
---|
318 | #endif |
---|
319 | |
---|
320 | bool MovieMaker::AddFrame() |
---|
321 | { |
---|
322 | #ifdef WITH_LIBAV |
---|
323 | int ret = av_frame_make_writable(frame); |
---|
324 | if (ret < 0) { |
---|
325 | averrno = ret; |
---|
326 | return false; |
---|
327 | } |
---|
328 | |
---|
329 | enum AVPixelFormat pix_fmt = context->pix_fmt; |
---|
330 | |
---|
331 | if (pix_fmt != AV_PIX_FMT_YUV420P) { |
---|
332 | // FIXME convert... |
---|
333 | abort(); |
---|
334 | } |
---|
335 | |
---|
336 | int len = 3 * GetWidth(); |
---|
337 | { |
---|
338 | // Flip image vertically |
---|
339 | int h = GetHeight(); |
---|
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, GetHeight(), |
---|
349 | frame->data, frame->linesize); |
---|
350 | |
---|
351 | ++frame->pts; |
---|
352 | |
---|
353 | // Encode this frame. |
---|
354 | ret = encode_frame(frame); |
---|
355 | if (ret < 0) { |
---|
356 | averrno = ret; |
---|
357 | return false; |
---|
358 | } |
---|
359 | #endif |
---|
360 | return true; |
---|
361 | } |
---|
362 | |
---|
363 | bool |
---|
364 | MovieMaker::Close() |
---|
365 | { |
---|
366 | #ifdef WITH_LIBAV |
---|
367 | if (video_st && averrno == 0) { |
---|
368 | // Flush out any remaining data. |
---|
369 | int ret = encode_frame(NULL); |
---|
370 | if (ret < 0) { |
---|
371 | averrno = ret; |
---|
372 | return false; |
---|
373 | } |
---|
374 | av_write_trailer(oc); |
---|
375 | } |
---|
376 | |
---|
377 | release(); |
---|
378 | #endif |
---|
379 | return true; |
---|
380 | } |
---|
381 | |
---|
382 | #ifdef WITH_LIBAV |
---|
383 | void |
---|
384 | MovieMaker::release() |
---|
385 | { |
---|
386 | // Close codec. |
---|
387 | avcodec_free_context(&context); |
---|
388 | av_frame_free(&frame); |
---|
389 | av_free(pixels); |
---|
390 | pixels = NULL; |
---|
391 | sws_freeContext(sws_ctx); |
---|
392 | sws_ctx = NULL; |
---|
393 | |
---|
394 | // Free the stream. |
---|
395 | avformat_free_context(oc); |
---|
396 | oc = NULL; |
---|
397 | |
---|
398 | if (fh_to_close) { |
---|
399 | fclose(fh_to_close); |
---|
400 | fh_to_close = NULL; |
---|
401 | } |
---|
402 | } |
---|
403 | #endif |
---|
404 | |
---|
405 | MovieMaker::~MovieMaker() |
---|
406 | { |
---|
407 | #ifdef WITH_LIBAV |
---|
408 | release(); |
---|
409 | #endif |
---|
410 | } |
---|
411 | |
---|
412 | const char * |
---|
413 | MovieMaker::get_error_string() const |
---|
414 | { |
---|
415 | #ifdef WITH_LIBAV |
---|
416 | switch (averrno) { |
---|
417 | case AVERROR(EIO): |
---|
418 | return "I/O error"; |
---|
419 | case AVERROR(EDOM): |
---|
420 | return "Number syntax expected in filename"; |
---|
421 | case AVERROR_INVALIDDATA: |
---|
422 | /* same as AVERROR_UNKNOWN: return "unknown error"; */ |
---|
423 | return "invalid data found"; |
---|
424 | case AVERROR(ENOMEM): |
---|
425 | return "not enough memory"; |
---|
426 | case AVERROR(EILSEQ): |
---|
427 | return "unknown format"; |
---|
428 | case AVERROR(ENOSYS): |
---|
429 | return "Operation not supported"; |
---|
430 | case AVERROR(ENOENT): |
---|
431 | return "No such file or directory"; |
---|
432 | case AVERROR_EOF: |
---|
433 | return "End of file"; |
---|
434 | case AVERROR_PATCHWELCOME: |
---|
435 | return "Not implemented in FFmpeg"; |
---|
436 | case 0: |
---|
437 | return "No error"; |
---|
438 | case MOVIE_NO_SUITABLE_FORMAT: |
---|
439 | return "Couldn't find a suitable output format"; |
---|
440 | case MOVIE_AUDIO_ONLY: |
---|
441 | return "Audio-only format specified"; |
---|
442 | case MOVIE_FILENAME_TOO_LONG: |
---|
443 | return "Filename too long"; |
---|
444 | } |
---|
445 | #endif |
---|
446 | return "Unknown error"; |
---|
447 | } |
---|
448 | |
---|
449 | #else |
---|
450 | |
---|
451 | #include "moviemaker-legacy.cc" |
---|
452 | |
---|
453 | #endif |
---|