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