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