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