1 | // |
---|
2 | // moviemaker.cc |
---|
3 | // |
---|
4 | // Class for writing movies from Aven. |
---|
5 | // |
---|
6 | // Copyright (C) 2004 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 |
---|
62 | extern "C" { |
---|
63 | #include "libavformat/avformat.h" |
---|
64 | #include "libswscale/swscale.h" |
---|
65 | } |
---|
66 | #ifndef AV_PKT_FLAG_KEY |
---|
67 | # define AV_PKT_FLAG_KEY PKT_FLAG_KEY |
---|
68 | #endif |
---|
69 | #endif |
---|
70 | |
---|
71 | // ffmpeg CVS has added av_alloc_format_context() - we're ready for it! |
---|
72 | #define av_alloc_format_context() \ |
---|
73 | ((AVFormatContext*)av_mallocz(sizeof(AVFormatContext))) |
---|
74 | |
---|
75 | const int OUTBUF_SIZE = 200000; |
---|
76 | |
---|
77 | MovieMaker::MovieMaker() |
---|
78 | : oc(0), st(0), frame(0), outbuf(0), pixels(0), sws_ctx(0) |
---|
79 | { |
---|
80 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
81 | static bool initialised_ffmpeg = false; |
---|
82 | if (initialised_ffmpeg) return; |
---|
83 | |
---|
84 | // FIXME: register only the codec(s) we want to use... |
---|
85 | av_register_all(); |
---|
86 | |
---|
87 | initialised_ffmpeg = true; |
---|
88 | #endif |
---|
89 | } |
---|
90 | |
---|
91 | bool MovieMaker::Open(const char *fnm, int width, int height) |
---|
92 | { |
---|
93 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
94 | AVOutputFormat * fmt = guess_format(NULL, fnm, NULL); |
---|
95 | if (!fmt) { |
---|
96 | // We couldn't deduce the output format from file extension so default |
---|
97 | // to MPEG. |
---|
98 | fmt = guess_format("mpeg", NULL, NULL); |
---|
99 | if (!fmt) { |
---|
100 | // FIXME : error finding a format... |
---|
101 | return false; |
---|
102 | } |
---|
103 | } |
---|
104 | if (fmt->video_codec == CODEC_ID_NONE) { |
---|
105 | // FIXME : The user asked for a format which is audio-only! |
---|
106 | return false; |
---|
107 | } |
---|
108 | |
---|
109 | // Allocate the output media context. |
---|
110 | oc = av_alloc_format_context(); |
---|
111 | if (!oc) { |
---|
112 | // FIXME : out of memory |
---|
113 | return false; |
---|
114 | } |
---|
115 | oc->oformat = fmt; |
---|
116 | if (strlen(fnm) >= sizeof(oc->filename)) { |
---|
117 | // FIXME : filename too long |
---|
118 | return false; |
---|
119 | } |
---|
120 | strcpy(oc->filename, fnm); |
---|
121 | |
---|
122 | // Add the video stream using the default format codec. |
---|
123 | st = av_new_stream(oc, 0); |
---|
124 | if (!st) { |
---|
125 | // FIXME : possible errors are "too many streams" (can't be - we only |
---|
126 | // ask for one) and "out of memory" |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | // Initialise the code. |
---|
131 | AVCodecContext *c = st->codec; |
---|
132 | c->codec_id = fmt->video_codec; |
---|
133 | c->codec_type = CODEC_TYPE_VIDEO; |
---|
134 | |
---|
135 | // Set sample parameters. |
---|
136 | c->bit_rate = 400000; |
---|
137 | c->width = width; |
---|
138 | c->height = height; |
---|
139 | c->time_base.num = 1; |
---|
140 | c->time_base.den = 25; // Frames per second. |
---|
141 | c->gop_size = 12; // One intra frame every twelve frames. |
---|
142 | c->pix_fmt = PIX_FMT_YUV420P; |
---|
143 | // B frames are backwards predicted - they can improve compression, |
---|
144 | // but may slow encoding and decoding. |
---|
145 | // c->max_b_frames = 2; |
---|
146 | |
---|
147 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) |
---|
148 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
---|
149 | |
---|
150 | // Set the output parameters (must be done even if no parameters). |
---|
151 | if (av_set_parameters(oc, NULL) < 0) { |
---|
152 | // FIXME : return value is an AVERROR_* value - probably |
---|
153 | // AVERROR_NOMEM or AVERROR_UNKNOWN. |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | // Show the format we've ended up with (for debug purposes). |
---|
158 | // dump_format(oc, 0, fnm, 1); |
---|
159 | |
---|
160 | // Open the video codec and allocate the necessary encode buffers. |
---|
161 | AVCodec * codec = avcodec_find_encoder(c->codec_id); |
---|
162 | if (!codec) { |
---|
163 | // FIXME : Erm - internal ffmpeg library problem? |
---|
164 | return false; |
---|
165 | } |
---|
166 | if (avcodec_open(c, codec) < 0) { |
---|
167 | // FIXME : return value is an AVERROR_* value - probably |
---|
168 | // AVERROR_NOMEM or AVERROR_UNKNOWN. |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | if ((oc->oformat->flags & AVFMT_RAWPICTURE)) { |
---|
173 | outbuf = NULL; |
---|
174 | } else { |
---|
175 | outbuf = (unsigned char *)malloc(OUTBUF_SIZE); |
---|
176 | if (!outbuf) { |
---|
177 | // FIXME : out of memory |
---|
178 | return false; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | frame = avcodec_alloc_frame(); |
---|
183 | if (!frame) { |
---|
184 | // FIXME : out of memory |
---|
185 | return false; |
---|
186 | } |
---|
187 | int size = avpicture_get_size(c->pix_fmt, width, height); |
---|
188 | uint8_t * picture_buf = (uint8_t*)av_malloc(size); |
---|
189 | if (!picture_buf) { |
---|
190 | av_free(frame); |
---|
191 | // FIXME : out of memory |
---|
192 | return false; |
---|
193 | } |
---|
194 | avpicture_fill((AVPicture *)frame, picture_buf, c->pix_fmt, width, height); |
---|
195 | |
---|
196 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
---|
197 | // FIXME need to allocate another frame for this case if we stop |
---|
198 | // hardcoding PIX_FMT_YUV420P. |
---|
199 | abort(); |
---|
200 | } |
---|
201 | |
---|
202 | pixels = (unsigned char *)malloc(width * height * 6); |
---|
203 | if (!pixels) { |
---|
204 | // FIXME : out of memory |
---|
205 | return false; |
---|
206 | } |
---|
207 | |
---|
208 | if (url_fopen(&oc->pb, fnm, URL_WRONLY) < 0) { |
---|
209 | // FIXME : return value is -E* (e.g. -EIO). |
---|
210 | return false; |
---|
211 | } |
---|
212 | |
---|
213 | // Write the stream header, if any. |
---|
214 | if (av_write_header(oc) < 0) { |
---|
215 | // FIXME : return value is an AVERROR_* value. |
---|
216 | return false; |
---|
217 | } |
---|
218 | |
---|
219 | av_free(sws_ctx); |
---|
220 | sws_ctx = sws_getContext(width, height, PIX_FMT_RGB24, |
---|
221 | width, height, c->pix_fmt, SWS_BICUBIC, |
---|
222 | NULL, NULL, NULL); |
---|
223 | if (sws_ctx == NULL) { |
---|
224 | fprintf(stderr, "Cannot initialize the conversion context!\n"); |
---|
225 | return false; |
---|
226 | } |
---|
227 | |
---|
228 | return true; |
---|
229 | #else |
---|
230 | return false; |
---|
231 | #endif |
---|
232 | } |
---|
233 | |
---|
234 | unsigned char * MovieMaker::GetBuffer() const { |
---|
235 | AVCodecContext * c = st->codec; |
---|
236 | return pixels + c->height * c->width * 3; |
---|
237 | } |
---|
238 | |
---|
239 | int MovieMaker::GetWidth() const { |
---|
240 | assert(st); |
---|
241 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
242 | AVCodecContext *c = st->codec; |
---|
243 | return c->width; |
---|
244 | #else |
---|
245 | return 0; |
---|
246 | #endif |
---|
247 | } |
---|
248 | |
---|
249 | int MovieMaker::GetHeight() const { |
---|
250 | assert(st); |
---|
251 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
252 | AVCodecContext *c = st->codec; |
---|
253 | return c->height; |
---|
254 | #else |
---|
255 | return 0; |
---|
256 | #endif |
---|
257 | } |
---|
258 | |
---|
259 | void MovieMaker::AddFrame() |
---|
260 | { |
---|
261 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
262 | AVCodecContext * c = st->codec; |
---|
263 | |
---|
264 | if (c->pix_fmt != PIX_FMT_YUV420P) { |
---|
265 | // FIXME convert... |
---|
266 | abort(); |
---|
267 | } |
---|
268 | |
---|
269 | int len = 3 * c->width; |
---|
270 | { |
---|
271 | // Flip image vertically |
---|
272 | int h = c->height; |
---|
273 | unsigned char * src = pixels + h * len; |
---|
274 | unsigned char * dest = src - len; |
---|
275 | while (h--) { |
---|
276 | memcpy(dest, src, len); |
---|
277 | src += len; |
---|
278 | dest -= len; |
---|
279 | } |
---|
280 | } |
---|
281 | sws_scale(sws_ctx, &pixels, &len, 0, c->height, frame->data, frame->linesize); |
---|
282 | |
---|
283 | if (oc->oformat->flags & AVFMT_RAWPICTURE) { |
---|
284 | abort(); |
---|
285 | } |
---|
286 | |
---|
287 | // Encode this frame. |
---|
288 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, frame); |
---|
289 | // outsize == 0 means that this frame has been buffered, so there's nothing |
---|
290 | // to write yet. |
---|
291 | if (out_size) { |
---|
292 | // Write the compressed frame to the media file. |
---|
293 | AVPacket pkt; |
---|
294 | av_init_packet(&pkt); |
---|
295 | |
---|
296 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
---|
297 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
---|
298 | if (c->coded_frame->key_frame) |
---|
299 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
300 | pkt.stream_index = st->index; |
---|
301 | pkt.data = outbuf; |
---|
302 | pkt.size = out_size; |
---|
303 | |
---|
304 | /* write the compressed frame in the media file */ |
---|
305 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
---|
306 | abort(); |
---|
307 | } |
---|
308 | } |
---|
309 | #endif |
---|
310 | } |
---|
311 | |
---|
312 | MovieMaker::~MovieMaker() |
---|
313 | { |
---|
314 | #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H |
---|
315 | if (st) { |
---|
316 | // No more frames to compress. The codec may have a few frames |
---|
317 | // buffered if we're using B frames, so write those too. |
---|
318 | AVCodecContext * c = st->codec; |
---|
319 | |
---|
320 | while (out_size) { |
---|
321 | out_size = avcodec_encode_video(c, outbuf, OUTBUF_SIZE, NULL); |
---|
322 | if (out_size) { |
---|
323 | // Write the compressed frame to the media file. |
---|
324 | AVPacket pkt; |
---|
325 | av_init_packet(&pkt); |
---|
326 | |
---|
327 | if (c->coded_frame->pts != AV_NOPTS_VALUE) |
---|
328 | pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); |
---|
329 | if (c->coded_frame->key_frame) |
---|
330 | pkt.flags |= AV_PKT_FLAG_KEY; |
---|
331 | pkt.stream_index = st->index; |
---|
332 | pkt.data = outbuf; |
---|
333 | pkt.size = out_size; |
---|
334 | |
---|
335 | /* write the compressed frame in the media file */ |
---|
336 | if (av_interleaved_write_frame(oc, &pkt) != 0) { |
---|
337 | abort(); |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | av_write_trailer(oc); |
---|
343 | |
---|
344 | // Close codec. |
---|
345 | avcodec_close(c); |
---|
346 | } |
---|
347 | |
---|
348 | if (frame) { |
---|
349 | free(frame->data[0]); |
---|
350 | free(frame); |
---|
351 | } |
---|
352 | free(outbuf); |
---|
353 | free(pixels); |
---|
354 | av_free(sws_ctx); |
---|
355 | |
---|
356 | if (oc) { |
---|
357 | // Free the streams. |
---|
358 | for (size_t i = 0; i < oc->nb_streams; ++i) { |
---|
359 | av_freep(&oc->streams[i]->codec); |
---|
360 | av_freep(&oc->streams[i]); |
---|
361 | } |
---|
362 | |
---|
363 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
---|
364 | // Close the output file. |
---|
365 | url_fclose(oc->pb); |
---|
366 | } |
---|
367 | |
---|
368 | // Free the stream. |
---|
369 | free(oc); |
---|
370 | } |
---|
371 | #endif |
---|
372 | } |
---|