2.5: globals cleanup
[blender.git] / source / blender / blenkernel / intern / writeffmpeg.c
1 /*
2  * ffmpeg-write support
3  *
4  * Partial Copyright (c) 2006 Peter Schlaile
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18
19 #ifdef WITH_FFMPEG
20 #include <string.h>
21 #include <stdio.h>
22
23 #if defined(_WIN32) && defined(_DEBUG) && !defined(__MINGW32__) && !defined(__CYGWIN__)
24 /* This does not seem necessary or present on MSVC 8, but may be needed in earlier versions? */
25 #if _MSC_VER < 1400
26 #include <stdint.h>
27 #endif
28 #endif
29
30 #include <stdlib.h>
31
32 #include <ffmpeg/avformat.h>
33 #include <ffmpeg/avcodec.h>
34 #include <ffmpeg/rational.h>
35 #include <ffmpeg/swscale.h>
36 #include <ffmpeg/opt.h>
37
38 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
39 #define FFMPEG_OLD_FRAME_RATE 1
40 #else
41 #define FFMPEG_CODEC_IS_POINTER 1
42 #define FFMPEG_CODEC_TIME_BASE  1
43 #endif
44
45 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
46 #define OUTFILE_PB (outfile->pb)
47 #else
48 #define OUTFILE_PB (&outfile->pb)
49 #endif
50
51 #if defined(WIN32) && (!(defined snprintf))
52 #define snprintf _snprintf
53 #endif
54
55 #include "BKE_writeffmpeg.h"
56
57 #include "MEM_guardedalloc.h"
58 #include "BLI_blenlib.h"
59
60 #include "BKE_global.h"
61 #include "BKE_idprop.h"
62
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
65
66 #include "DNA_scene_types.h"
67
68 #ifdef HAVE_CONFIG_H
69 #include <config.h>
70 #endif
71
72 extern void do_init_ffmpeg();
73 void makeffmpegstring(char* string);
74
75 static int ffmpeg_type = 0;
76 static int ffmpeg_codec = CODEC_ID_MPEG4;
77 static int ffmpeg_audio_codec = CODEC_ID_MP2;
78 static int ffmpeg_video_bitrate = 1150;
79 static int ffmpeg_audio_bitrate = 128;
80 static int ffmpeg_gop_size = 12;
81 static int ffmpeg_multiplex_audio = 1;
82 static int ffmpeg_autosplit = 0;
83 static int ffmpeg_autosplit_count = 0;
84
85 static AVFormatContext* outfile = 0;
86 static AVStream* video_stream = 0;
87 static AVStream* audio_stream = 0;
88 static AVFrame* current_frame = 0;
89 static struct SwsContext *img_convert_ctx = 0;
90
91 static uint8_t* video_buffer = 0;
92 static int video_buffersize = 0;
93
94 static uint8_t* audio_input_buffer = 0;
95 static int audio_input_frame_size = 0;
96 static uint8_t* audio_output_buffer = 0;
97 static int audio_outbuf_size = 0;
98
99 static RenderData *ffmpeg_renderdata = 0;
100
101 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
102
103 /* Delete a picture buffer */
104
105 static void delete_picture(AVFrame* f)
106 {
107         if (f) {
108                 if (f->data[0]) MEM_freeN(f->data[0]);
109                 av_free(f);
110         }
111 }
112
113 #ifdef FFMPEG_CODEC_IS_POINTER
114 static AVCodecContext* get_codec_from_stream(AVStream* stream)
115 {
116         return stream->codec;
117 }
118 #else
119 static AVCodecContext* get_codec_from_stream(AVStream* stream)
120 {
121         return &stream->codec;
122 }
123 #endif
124
125 static int write_audio_frame(void) 
126 {
127         AVCodecContext* c = NULL;
128         AVPacket pkt;
129
130         c = get_codec_from_stream(audio_stream);
131
132         //XXX audiostream_fill(audio_input_buffer, 
133         //               audio_input_frame_size 
134         //               * sizeof(short) * c->channels);
135
136         av_init_packet(&pkt);
137
138         pkt.size = avcodec_encode_audio(c, audio_output_buffer,
139                                         audio_outbuf_size, 
140                                         (short*) audio_input_buffer);
141         pkt.data = audio_output_buffer;
142 #ifdef FFMPEG_CODEC_TIME_BASE
143         pkt.pts = av_rescale_q(c->coded_frame->pts, 
144                                c->time_base, audio_stream->time_base);
145 #else
146         pkt.pts = c->coded_frame->pts;
147 #endif
148         fprintf(stderr, "Audio Frame PTS: %lld\n", pkt.pts);
149
150         pkt.stream_index = audio_stream->index;
151         pkt.flags |= PKT_FLAG_KEY;
152         if (av_interleaved_write_frame(outfile, &pkt) != 0) {
153                 //XXX error("Error writing audio packet");
154                 return -1;
155         }
156         return 0;
157 }
158
159 /* Allocate a temporary frame */
160 static AVFrame* alloc_picture(int pix_fmt, int width, int height) 
161 {
162         AVFrame* f;
163         uint8_t* buf;
164         int size;
165         
166         /* allocate space for the struct */
167         f = avcodec_alloc_frame();
168         if (!f) return NULL;
169         size = avpicture_get_size(pix_fmt, width, height);
170         /* allocate the actual picture buffer */
171         buf = MEM_mallocN(size, "AVFrame buffer");
172         if (!buf) {
173                 free(f);
174                 return NULL;
175         }
176         avpicture_fill((AVPicture*)f, buf, pix_fmt, width, height);
177         return f;
178 }
179
180 /* Get the correct file extensions for the requested format,
181    first is always desired guess_format parameter */
182 static const char** get_file_extensions(int format) 
183 {
184         switch(format) {
185         case FFMPEG_DV: {
186                 static const char * rv[] = { ".dv", NULL };
187                 return rv;
188         }
189         case FFMPEG_MPEG1: {
190                 static const char * rv[] = { ".mpg", ".mpeg", NULL };
191                 return rv;
192         }
193         case FFMPEG_MPEG2: {
194                 static const char * rv[] = { ".dvd", ".vob", ".mpg", ".mpeg",
195                                              NULL };
196                 return rv;
197         }
198         case FFMPEG_MPEG4: {
199                 static const char * rv[] = { ".mp4", ".mpg", ".mpeg", NULL };
200                 return rv;
201         }
202         case FFMPEG_AVI: {
203                 static const char * rv[] = { ".avi", NULL };
204                 return rv;
205         }
206         case FFMPEG_MOV: {
207                 static const char * rv[] = { ".mov", NULL };
208                 return rv;
209         }
210         case FFMPEG_H264: {
211                 /* FIXME: avi for now... */
212                 static const char * rv[] = { ".avi", NULL };
213                 return rv;
214         }
215
216         case FFMPEG_XVID: {
217                 /* FIXME: avi for now... */
218                 static const char * rv[] = { ".avi", NULL };
219                 return rv;
220         }
221         case FFMPEG_FLV: {
222                 static const char * rv[] = { ".flv", NULL };
223                 return rv;
224         }
225         case FFMPEG_MKV: {
226                 static const char * rv[] = { ".mkv", NULL };
227                 return rv;
228         }
229         case FFMPEG_OGG: {
230                 static const char * rv[] = { ".ogg", ".ogv", NULL };
231                 return rv;
232         }
233         default:
234                 return NULL;
235         }
236 }
237
238 /* Write a frame to the output file */
239 static void write_video_frame(AVFrame* frame) 
240 {
241         int outsize = 0;
242         int ret;
243         AVCodecContext* c = get_codec_from_stream(video_stream);
244 #ifdef FFMPEG_CODEC_TIME_BASE
245         frame->pts = G.scene->r.cfra - G.scene->r.sfra;
246 #endif
247
248         outsize = avcodec_encode_video(c, video_buffer, video_buffersize, 
249                                        frame);
250         if (outsize != 0) {
251                 AVPacket packet;
252                 av_init_packet(&packet);
253
254                 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
255 #ifdef FFMPEG_CODEC_TIME_BASE
256                         packet.pts = av_rescale_q(c->coded_frame->pts,
257                                                   c->time_base,
258                                                   video_stream->time_base);
259 #else
260                         packet.pts = c->coded_frame->pts;
261 #endif
262                         fprintf(stderr, "Video Frame PTS: %lld\n", packet.pts);
263                 } else {
264                         fprintf(stderr, "Video Frame PTS: not set\n");
265                 }
266                 if (c->coded_frame->key_frame)
267                         packet.flags |= PKT_FLAG_KEY;
268                 packet.stream_index = video_stream->index;
269                 packet.data = video_buffer;
270                 packet.size = outsize;
271                 ret = av_interleaved_write_frame(outfile, &packet);
272         } else ret = 0;
273         if (ret != 0) {
274                 G.afbreek = 1;
275                 //XXX error("Error writing frame");
276         }
277 }
278
279 /* read and encode a frame of audio from the buffer */
280 static AVFrame* generate_video_frame(uint8_t* pixels) 
281 {
282         uint8_t* rendered_frame;
283
284         AVCodecContext* c = get_codec_from_stream(video_stream);
285         int width = c->width;
286         int height = c->height;
287         AVFrame* rgb_frame;
288
289         if (c->pix_fmt != PIX_FMT_RGBA32) {
290                 rgb_frame = alloc_picture(PIX_FMT_RGBA32, width, height);
291                 if (!rgb_frame) {
292                         G.afbreek=1;
293                         //XXX error("Couldn't allocate temporary frame");
294                         return NULL;
295                 }
296         } else {
297                 rgb_frame = current_frame;
298         }
299
300         rendered_frame = pixels;
301
302         /* Do RGBA-conversion and flipping in one step depending
303            on CPU-Endianess */
304
305         if (ENDIAN_ORDER == L_ENDIAN) {
306                 int y;
307                 for (y = 0; y < height; y++) {
308                         uint8_t* target = rgb_frame->data[0]
309                                 + width * 4 * (height - y - 1);
310                         uint8_t* src = rendered_frame + width * 4 * y;
311                         uint8_t* end = src + width * 4;
312                         while (src != end) {
313                                 target[3] = src[3];
314                                 target[2] = src[0];
315                                 target[1] = src[1];
316                                 target[0] = src[2];
317
318                                 target += 4;
319                                 src += 4;
320                         }
321                 }
322         } else {
323                 int y;
324                 for (y = 0; y < height; y++) {
325                         uint8_t* target = rgb_frame->data[0]
326                                 + width * 4 * (height - y - 1);
327                         uint8_t* src = rendered_frame + width * 4 * y;
328                         uint8_t* end = src + width * 4;
329                         while (src != end) {
330                                 target[3] = src[2];
331                                 target[2] = src[1];
332                                 target[1] = src[0];
333                                 target[0] = src[3];
334
335                                 target += 4;
336                                 src += 4;
337                         }
338                 }
339         }
340
341         if (c->pix_fmt != PIX_FMT_RGBA32) {
342                 sws_scale(img_convert_ctx, rgb_frame->data,
343                           rgb_frame->linesize, 0, c->height, 
344                           current_frame->data, current_frame->linesize);
345                 delete_picture(rgb_frame);
346         }
347         return current_frame;
348 }
349
350 static void set_ffmpeg_property_option(AVCodecContext* c, IDProperty * prop)
351 {
352         char name[128];
353         char * param;
354         const AVOption * rv = NULL;
355
356         fprintf(stderr, "FFMPEG expert option: %s: ", prop->name);
357
358         strncpy(name, prop->name, 128);
359
360         param = strchr(name, ':');
361
362         if (param) {
363                 *param++ = 0;
364         }
365
366         switch(prop->type) {
367         case IDP_STRING:
368                 fprintf(stderr, "%s.\n", IDP_String(prop));
369                 rv = av_set_string(c, prop->name, IDP_String(prop));
370                 break;
371         case IDP_FLOAT:
372                 fprintf(stderr, "%g.\n", IDP_Float(prop));
373                 rv = av_set_double(c, prop->name, IDP_Float(prop));
374                 break;
375         case IDP_INT:
376                 fprintf(stderr, "%d.\n", IDP_Int(prop));
377                 
378                 if (param) {
379                         if (IDP_Int(prop)) {
380                                 rv = av_set_string(c, name, param);
381                         } else {
382                                 return;
383                         }
384                 } else {
385                         rv = av_set_int(c, prop->name, IDP_Int(prop));
386                 }
387                 break;
388         }
389
390         if (!rv) {
391                 fprintf(stderr, "ffmpeg-option not supported: %s! Skipping.\n",
392                         prop->name);
393         }
394 }
395
396 static void set_ffmpeg_properties(AVCodecContext* c, const char * prop_name)
397 {
398         IDProperty * prop;
399         void * iter;
400         IDProperty * curr;
401
402         if (!G.scene->r.ffcodecdata.properties) {
403                 return;
404         }
405         
406         prop = IDP_GetPropertyFromGroup(
407                 G.scene->r.ffcodecdata.properties, (char*) prop_name);
408         if (!prop) {
409                 return;
410         }
411
412         iter = IDP_GetGroupIterator(prop);
413
414         while ((curr = IDP_GroupIterNext(iter)) != NULL) {
415                 set_ffmpeg_property_option(c, curr);
416         }
417 }
418
419 /* prepare a video stream for the output file */
420
421 static AVStream* alloc_video_stream(int codec_id, AVFormatContext* of,
422                                     int rectx, int recty) 
423 {
424         AVStream* st;
425         AVCodecContext* c;
426         AVCodec* codec;
427         st = av_new_stream(of, 0);
428         if (!st) return NULL;
429
430         /* Set up the codec context */
431         
432         c = get_codec_from_stream(st);
433         c->codec_id = codec_id;
434         c->codec_type = CODEC_TYPE_VIDEO;
435
436
437         /* Get some values from the current render settings */
438         
439         c->width = rectx;
440         c->height = recty;
441
442 #ifdef FFMPEG_CODEC_TIME_BASE
443         /* FIXME: Really bad hack (tm) for NTSC support */
444         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
445                 c->time_base.den = 2997;
446                 c->time_base.num = 100;
447         } else if ((double) ((int) G.scene->r.frs_sec_base) == 
448                    G.scene->r.frs_sec_base) {
449                 c->time_base.den = G.scene->r.frs_sec;
450                 c->time_base.num = (int) G.scene->r.frs_sec_base;
451         } else {
452                 c->time_base.den = G.scene->r.frs_sec * 100000;
453                 c->time_base.num = ((double) G.scene->r.frs_sec_base) * 100000;
454         }
455 #else
456         /* FIXME: Really bad hack (tm) for NTSC support */
457         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
458                 c->frame_rate = 2997;
459                 c->frame_rate_base = 100;
460         } else if ((double) ((int) G.scene->r.frs_sec_base) == 
461                    G.scene->r.frs_sec_base) {
462                 c->frame_rate = G.scene->r.frs_sec;
463                 c->frame_rate_base = G.scene->r.frs_sec_base;
464         } else {
465                 c->frame_rate = G.scene->r.frs_sec * 100000;
466                 c->frame_rate_base = ((double) G.scene->r.frs_sec_base)*100000;
467         }
468 #endif
469         
470         c->gop_size = ffmpeg_gop_size;
471         c->bit_rate = ffmpeg_video_bitrate*1000;
472         c->rc_max_rate = G.scene->r.ffcodecdata.rc_max_rate*1000;
473         c->rc_min_rate = G.scene->r.ffcodecdata.rc_min_rate*1000;
474         c->rc_buffer_size = G.scene->r.ffcodecdata.rc_buffer_size * 1024;
475         c->rc_initial_buffer_occupancy 
476                 = G.scene->r.ffcodecdata.rc_buffer_size*3/4;
477         c->rc_buffer_aggressivity = 1.0;
478         c->me_method = ME_EPZS;
479         
480         codec = avcodec_find_encoder(c->codec_id);
481         if (!codec) return NULL;
482         
483         /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
484         
485         if (codec->pix_fmts) {
486                 c->pix_fmt = codec->pix_fmts[0];
487         } else {
488                 /* makes HuffYUV happy ... */
489                 c->pix_fmt = PIX_FMT_YUV422P;
490         }
491
492         if (codec_id == CODEC_ID_XVID) {
493                 /* arghhhh ... */
494                 c->pix_fmt = PIX_FMT_YUV420P;
495         }
496         
497         if (!strcmp(of->oformat->name, "mp4") || 
498             !strcmp(of->oformat->name, "mov") ||
499             !strcmp(of->oformat->name, "3gp")) {
500                 fprintf(stderr, "Using global header\n");
501                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
502         }
503         
504         /* Determine whether we are encoding interlaced material or not */
505         if (G.scene->r.mode & R_FIELDS) {
506                 fprintf(stderr, "Encoding interlaced video\n");
507                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
508                 c->flags |= CODEC_FLAG_INTERLACED_ME;
509         }
510
511         /* xasp & yasp got float lately... */
512
513         c->sample_aspect_ratio = av_d2q(
514                 ((double) G.scene->r.xasp / (double) G.scene->r.yasp), 255);
515
516         set_ffmpeg_properties(c, "video");
517         
518         if (avcodec_open(c, codec) < 0) {
519                 //
520                 //XXX error("Couldn't initialize codec");
521                 return NULL;
522         }
523
524         video_buffersize = 2000000;
525         video_buffer = (uint8_t*)MEM_mallocN(video_buffersize, 
526                                              "FFMPEG video buffer");
527         
528         current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
529
530         img_convert_ctx = sws_getContext(c->width, c->height,
531                                          PIX_FMT_RGBA32,
532                                          c->width, c->height,
533                                          c->pix_fmt,
534                                          SWS_BICUBIC,
535                                          NULL, NULL, NULL);
536         return st;
537 }
538
539 /* Prepare an audio stream for the output file */
540
541 static AVStream* alloc_audio_stream(int codec_id, AVFormatContext* of) 
542 {
543         AVStream* st;
544         AVCodecContext* c;
545         AVCodec* codec;
546
547         st = av_new_stream(of, 1);
548         if (!st) return NULL;
549
550         c = get_codec_from_stream(st);
551         c->codec_id = codec_id;
552         c->codec_type = CODEC_TYPE_AUDIO;
553
554         c->sample_rate = G.scene->audio.mixrate;
555         c->bit_rate = ffmpeg_audio_bitrate*1000;
556         c->channels = 2;
557         codec = avcodec_find_encoder(c->codec_id);
558         if (!codec) {
559                 //XXX error("Couldn't find a valid audio codec");
560                 return NULL;
561         }
562
563         set_ffmpeg_properties(c, "audio");
564
565         if (avcodec_open(c, codec) < 0) {
566                 //XXX error("Couldn't initialize audio codec");
567                 return NULL;
568         }
569
570         /* FIXME: Should be user configurable */
571         if (ffmpeg_type == FFMPEG_DV) {
572                 /* this is a hack around the poor ffmpeg dv multiplexer. */
573                 /* only fixes PAL for now 
574                    (NTSC is a lot more complicated here...)! */
575                 audio_outbuf_size = 7680;
576         } else {
577                 audio_outbuf_size = 10000;
578         }
579         audio_output_buffer = (uint8_t*)MEM_mallocN(
580                 audio_outbuf_size, "FFMPEG audio encoder input buffer");
581
582        /* ugly hack for PCM codecs */
583
584         if (c->frame_size <= 1) {
585                 audio_input_frame_size = audio_outbuf_size / c->channels;
586                 switch(c->codec_id) {
587                 case CODEC_ID_PCM_S16LE:
588                 case CODEC_ID_PCM_S16BE:
589                 case CODEC_ID_PCM_U16LE:
590                 case CODEC_ID_PCM_U16BE:
591                         audio_input_frame_size >>= 1;
592                         break;
593                 default:
594                         break;
595                 }
596         } else {
597                 audio_input_frame_size = c->frame_size;
598         }
599
600         audio_input_buffer = (uint8_t*)MEM_mallocN(
601                 audio_input_frame_size * sizeof(short) * c->channels, 
602                 "FFMPEG audio encoder output buffer");
603
604         return st;
605 }
606 /* essential functions -- start, append, end */
607
608 void start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty)
609 {
610         /* Handle to the output file */
611         AVFormatContext* of;
612         AVOutputFormat* fmt;
613         char name[256];
614         const char ** exts;
615
616         ffmpeg_type = rd->ffcodecdata.type;
617         ffmpeg_codec = rd->ffcodecdata.codec;
618         ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
619         ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
620         ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
621         ffmpeg_gop_size = rd->ffcodecdata.gop_size;
622         ffmpeg_multiplex_audio = rd->ffcodecdata.flags
623                 & FFMPEG_MULTIPLEX_AUDIO;
624         ffmpeg_autosplit = rd->ffcodecdata.flags
625                 & FFMPEG_AUTOSPLIT_OUTPUT;
626         
627         do_init_ffmpeg();
628
629         /* Determine the correct filename */
630         makeffmpegstring(name);
631         fprintf(stderr, "Starting output to %s(ffmpeg)...\n"
632                 "  Using type=%d, codec=%d, audio_codec=%d,\n"
633                 "  video_bitrate=%d, audio_bitrate=%d,\n"
634                 "  gop_size=%d, multiplex=%d, autosplit=%d\n"
635                 "  render width=%d, render height=%d\n", 
636                 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
637                 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
638                 ffmpeg_gop_size, ffmpeg_multiplex_audio,
639                 ffmpeg_autosplit, rectx, recty);
640         
641         exts = get_file_extensions(ffmpeg_type);
642         if (!exts) {
643                 G.afbreek = 1; /* Abort render */
644                 //XXX error("No valid formats found");
645                 return;
646         }
647         fmt = guess_format(NULL, exts[0], NULL);
648         if (!fmt) {
649                 G.afbreek = 1; /* Abort render */
650                 //XXX error("No valid formats found");
651                 return;
652         }
653
654         of = av_alloc_format_context();
655         if (!of) {
656                 G.afbreek = 1;
657                 //XXX error("Error opening output file");
658                 return;
659         }
660         
661         of->oformat = fmt;
662         of->packet_size= G.scene->r.ffcodecdata.mux_packet_size;
663         if (ffmpeg_multiplex_audio) {
664                 of->mux_rate = G.scene->r.ffcodecdata.mux_rate;
665         } else {
666                 of->mux_rate = 0;
667         }
668
669         of->preload = (int)(0.5*AV_TIME_BASE);
670         of->max_delay = (int)(0.7*AV_TIME_BASE);
671
672         snprintf(of->filename, sizeof(of->filename), "%s", name);
673         /* set the codec to the user's selection */
674         switch(ffmpeg_type) {
675         case FFMPEG_AVI:
676         case FFMPEG_MOV:
677         case FFMPEG_OGG:
678         case FFMPEG_MKV:
679                 fmt->video_codec = ffmpeg_codec;
680                 break;
681         case FFMPEG_DV:
682                 fmt->video_codec = CODEC_ID_DVVIDEO;
683                 break;
684         case FFMPEG_MPEG1:
685                 fmt->video_codec = CODEC_ID_MPEG1VIDEO;
686                 break;
687         case FFMPEG_MPEG2:
688                 fmt->video_codec = CODEC_ID_MPEG2VIDEO;
689                 break;
690         case FFMPEG_H264:
691                 fmt->video_codec = CODEC_ID_H264;
692                 break;
693         case FFMPEG_XVID:
694                 fmt->video_codec = CODEC_ID_XVID;
695                 break;
696         case FFMPEG_FLV:
697                 fmt->video_codec = CODEC_ID_FLV1;
698                 break;
699         case FFMPEG_MPEG4:
700         default:
701                 fmt->video_codec = CODEC_ID_MPEG4;
702                 break;
703         }
704         if (fmt->video_codec == CODEC_ID_DVVIDEO) {
705                 if (rectx != 720) {
706                         G.afbreek = 1;
707                         //XXX error("Render width has to be 720 pixels for DV!");
708                         return;
709                 }
710                 if (G.scene->r.frs_sec != 25 && recty != 480) {
711                         G.afbreek = 1;
712                         //XXX error("Render height has to be 480 pixels "
713                         //      "for DV-NTSC!");
714                         return;
715                         
716                 }
717                 if (G.scene->r.frs_sec == 25 && recty != 576) {
718                         G.afbreek = 1;
719                         //XXX error("Render height has to be 576 pixels "
720                         //      "for DV-PAL!");
721                         return;
722                 }
723         }
724         
725         fmt->audio_codec = ffmpeg_audio_codec;
726
727         if (ffmpeg_type == FFMPEG_DV) {
728                 fmt->audio_codec = CODEC_ID_PCM_S16LE;
729                 if (ffmpeg_multiplex_audio 
730                     && G.scene->audio.mixrate != 48000) {
731                         G.afbreek = 1;
732                         //XXX error("FFMPEG only supports 48khz / stereo "
733                         //      "audio for DV!");
734                         return;
735                 }
736         }
737         
738         video_stream = alloc_video_stream(fmt->video_codec, of, rectx, recty);
739         if (!video_stream) {
740                 G.afbreek = 1;
741                 //XXX error("Error initializing video stream");
742                 return;
743         }
744         
745         if (ffmpeg_multiplex_audio) {
746                 audio_stream = alloc_audio_stream(fmt->audio_codec, of);
747                 if (!audio_stream) {
748                         G.afbreek = 1;
749                         //XXX error("Error initializing audio stream");
750                         return;
751                 }
752                 //XXX audiostream_play(SFRA, 0, 1);
753         }
754         if (av_set_parameters(of, NULL) < 0) {
755                 G.afbreek = 1;
756                 //XXX error("Error setting output parameters");
757                 return;
758         }
759         if (!(fmt->flags & AVFMT_NOFILE)) {
760                 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
761                         G.afbreek = 1;
762                         //
763                         //XXX error("Could not open file for writing");
764                         return;
765                 }
766         }
767
768         av_write_header(of);
769         outfile = of;
770         dump_format(of, 0, name, 1);
771 }
772
773 /* **********************************************************************
774    * public interface
775    ********************************************************************** */
776
777 /* Get the output filename-- similar to the other output formats */
778 void makeffmpegstring(char* string) {
779
780         // XXX quick define, solve!
781 #define FILE_MAXDIR 256
782 #define FILE_MAXFILE 126
783                 
784         char txt[FILE_MAXDIR+FILE_MAXFILE];
785         // XXX
786 #undef FILE_MAXDIR
787 #undef FILE_MAXFILE
788         char autosplit[20];
789
790         const char ** exts = get_file_extensions(G.scene->r.ffcodecdata.type);
791         const char ** fe = exts;
792
793         if (!string || !exts) return;
794
795         strcpy(string, G.scene->r.pic);
796         BLI_convertstringcode(string, G.sce);
797         BLI_convertstringframe(string, G.scene->r.cfra);
798
799         BLI_make_existing_file(string);
800
801         autosplit[0] = 0;
802
803         if ((G.scene->r.ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
804                 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
805         }
806
807         while (*fe) {
808                 if (BLI_strcasecmp(string + strlen(string) - strlen(*fe), 
809                                    *fe) == 0) {
810                         break;
811                 }
812                 fe++;
813         }
814
815         if (!*fe) {
816                 strcat(string, autosplit);
817                 sprintf(txt, "%04d_%04d%s", (G.scene->r.sfra), 
818                         (G.scene->r.efra), *exts);
819                 strcat(string, txt);
820         } else {
821                 *(string + strlen(string) - strlen(*fe)) = 0;
822                 strcat(string, autosplit);
823                 strcat(string, *fe);
824         }
825 }
826
827
828 void start_ffmpeg(RenderData *rd, int rectx, int recty)
829 {
830         ffmpeg_autosplit_count = 0;
831
832         ffmpeg_renderdata = rd;
833
834         start_ffmpeg_impl(rd, rectx, recty);
835 }
836
837 void end_ffmpeg(void);
838
839 static void write_audio_frames()
840 {
841         int finished = 0;
842
843         while (ffmpeg_multiplex_audio && !finished) {
844                 double a_pts = ((double)audio_stream->pts.val 
845                                 * audio_stream->time_base.num 
846                                 / audio_stream->time_base.den);
847                 double v_pts = ((double)video_stream->pts.val 
848                                 * video_stream->time_base.num 
849                                 / video_stream->time_base.den);
850                 
851                 if (a_pts < v_pts) {
852                         write_audio_frame();
853                 } else {
854                         finished = 1;
855                 }
856         }
857 }
858
859 void append_ffmpeg(int frame, int *pixels, int rectx, int recty) 
860 {
861         fprintf(stderr, "Writing frame %i, "
862                 "render width=%d, render height=%d\n", frame,
863                 rectx, recty);
864
865         write_audio_frames();
866         write_video_frame(generate_video_frame((unsigned char*) pixels));
867
868         if (ffmpeg_autosplit) {
869                 if (url_ftell(OUTFILE_PB) > FFMPEG_AUTOSPLIT_SIZE) {
870                         end_ffmpeg();
871                         ffmpeg_autosplit_count++;
872                         start_ffmpeg_impl(ffmpeg_renderdata,
873                                           rectx, recty);
874                 }
875         }
876 }
877
878
879 void end_ffmpeg(void)
880 {
881         int i;
882         
883         fprintf(stderr, "Closing ffmpeg...\n");
884
885         if (audio_stream && video_stream) {
886                 write_audio_frames();
887         }
888         
889         if (outfile) {
890                 av_write_trailer(outfile);
891         }
892         
893         /* Close the video codec */
894
895         if (video_stream && get_codec_from_stream(video_stream)) {
896                 avcodec_close(get_codec_from_stream(video_stream));
897                 video_stream = 0;
898         }
899
900         
901         /* Close the output file */
902         if (outfile) {
903                 for (i = 0; i < outfile->nb_streams; i++) {
904                         if (&outfile->streams[i]) {
905                                 av_freep(&outfile->streams[i]);
906                         }
907                 }
908         }
909         /* free the temp buffer */
910         if (current_frame) {
911                 delete_picture(current_frame);
912                 current_frame = 0;
913         }
914         if (outfile && outfile->oformat) {
915                 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
916                         url_fclose(OUTFILE_PB);
917                 }
918         }
919         if (outfile) {
920                 av_free(outfile);
921                 outfile = 0;
922         }
923         if (video_buffer) {
924                 MEM_freeN(video_buffer);
925                 video_buffer = 0;
926         }
927         if (audio_output_buffer) {
928                 MEM_freeN(audio_output_buffer);
929                 audio_output_buffer = 0;
930         }
931         if (audio_input_buffer) {
932                 MEM_freeN(audio_input_buffer);
933                 audio_input_buffer = 0;
934         }
935
936         if (img_convert_ctx) {
937                 sws_freeContext(img_convert_ctx);
938                 img_convert_ctx = 0;
939         }
940 }
941 #endif
942