svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22668:22701
[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 #include "AUD_C-API.h"
69 #include "BKE_sound.h"
70 #include "BKE_main.h"
71
72 #ifdef HAVE_CONFIG_H
73 #include <config.h>
74 #endif
75
76 extern void do_init_ffmpeg();
77 static void makeffmpegstring(RenderData* rd, 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 AUD_Device* audio_mixdown_device = 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         if(audio_mixdown_device)
137                 AUD_readDevice(audio_mixdown_device, audio_input_buffer, audio_input_frame_size);
138
139         av_init_packet(&pkt);
140
141         pkt.size = avcodec_encode_audio(c, audio_output_buffer,
142                                         audio_outbuf_size, 
143                                         (short*) audio_input_buffer);
144         pkt.data = audio_output_buffer;
145 #ifdef FFMPEG_CODEC_TIME_BASE
146         pkt.pts = av_rescale_q(c->coded_frame->pts, 
147                                c->time_base, audio_stream->time_base);
148 #else
149         pkt.pts = c->coded_frame->pts;
150 #endif
151         fprintf(stderr, "Audio Frame PTS: %lld\n", pkt.pts);
152
153         pkt.stream_index = audio_stream->index;
154         pkt.flags |= PKT_FLAG_KEY;
155         if (av_interleaved_write_frame(outfile, &pkt) != 0) {
156                 //XXX error("Error writing audio packet");
157                 return -1;
158         }
159         return 0;
160 }
161
162 /* Allocate a temporary frame */
163 static AVFrame* alloc_picture(int pix_fmt, int width, int height) 
164 {
165         AVFrame* f;
166         uint8_t* buf;
167         int size;
168         
169         /* allocate space for the struct */
170         f = avcodec_alloc_frame();
171         if (!f) return NULL;
172         size = avpicture_get_size(pix_fmt, width, height);
173         /* allocate the actual picture buffer */
174         buf = MEM_mallocN(size, "AVFrame buffer");
175         if (!buf) {
176                 free(f);
177                 return NULL;
178         }
179         avpicture_fill((AVPicture*)f, buf, pix_fmt, width, height);
180         return f;
181 }
182
183 /* Get the correct file extensions for the requested format,
184    first is always desired guess_format parameter */
185 static const char** get_file_extensions(int format) 
186 {
187         switch(format) {
188         case FFMPEG_DV: {
189                 static const char * rv[] = { ".dv", NULL };
190                 return rv;
191         }
192         case FFMPEG_MPEG1: {
193                 static const char * rv[] = { ".mpg", ".mpeg", NULL };
194                 return rv;
195         }
196         case FFMPEG_MPEG2: {
197                 static const char * rv[] = { ".dvd", ".vob", ".mpg", ".mpeg",
198                                              NULL };
199                 return rv;
200         }
201         case FFMPEG_MPEG4: {
202                 static const char * rv[] = { ".mp4", ".mpg", ".mpeg", NULL };
203                 return rv;
204         }
205         case FFMPEG_AVI: {
206                 static const char * rv[] = { ".avi", NULL };
207                 return rv;
208         }
209         case FFMPEG_MOV: {
210                 static const char * rv[] = { ".mov", NULL };
211                 return rv;
212         }
213         case FFMPEG_H264: {
214                 /* FIXME: avi for now... */
215                 static const char * rv[] = { ".avi", NULL };
216                 return rv;
217         }
218
219         case FFMPEG_XVID: {
220                 /* FIXME: avi for now... */
221                 static const char * rv[] = { ".avi", NULL };
222                 return rv;
223         }
224         case FFMPEG_FLV: {
225                 static const char * rv[] = { ".flv", NULL };
226                 return rv;
227         }
228         case FFMPEG_MKV: {
229                 static const char * rv[] = { ".mkv", NULL };
230                 return rv;
231         }
232         case FFMPEG_OGG: {
233                 static const char * rv[] = { ".ogg", ".ogv", NULL };
234                 return rv;
235         }
236         default:
237                 return NULL;
238         }
239 }
240
241 /* Write a frame to the output file */
242 static void write_video_frame(RenderData *rd, AVFrame* frame) 
243 {
244         int outsize = 0;
245         int ret;
246         AVCodecContext* c = get_codec_from_stream(video_stream);
247 #ifdef FFMPEG_CODEC_TIME_BASE
248         frame->pts = rd->cfra - rd->sfra;
249 #endif
250         if (rd->mode & R_FIELDS) {
251                 frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
252         }
253
254         outsize = avcodec_encode_video(c, video_buffer, video_buffersize, 
255                                        frame);
256         if (outsize != 0) {
257                 AVPacket packet;
258                 av_init_packet(&packet);
259
260                 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
261 #ifdef FFMPEG_CODEC_TIME_BASE
262                         packet.pts = av_rescale_q(c->coded_frame->pts,
263                                                   c->time_base,
264                                                   video_stream->time_base);
265 #else
266                         packet.pts = c->coded_frame->pts;
267 #endif
268                         fprintf(stderr, "Video Frame PTS: %lld\n", packet.pts);
269                 } else {
270                         fprintf(stderr, "Video Frame PTS: not set\n");
271                 }
272                 if (c->coded_frame->key_frame)
273                         packet.flags |= PKT_FLAG_KEY;
274                 packet.stream_index = video_stream->index;
275                 packet.data = video_buffer;
276                 packet.size = outsize;
277                 ret = av_interleaved_write_frame(outfile, &packet);
278         } else ret = 0;
279         if (ret != 0) {
280                 G.afbreek = 1;
281                 //XXX error("Error writing frame");
282         }
283 }
284
285 /* read and encode a frame of audio from the buffer */
286 static AVFrame* generate_video_frame(uint8_t* pixels) 
287 {
288         uint8_t* rendered_frame;
289
290         AVCodecContext* c = get_codec_from_stream(video_stream);
291         int width = c->width;
292         int height = c->height;
293         AVFrame* rgb_frame;
294
295         if (c->pix_fmt != PIX_FMT_BGR32) {
296                 rgb_frame = alloc_picture(PIX_FMT_BGR32, width, height);
297                 if (!rgb_frame) {
298                         G.afbreek=1;
299                         //XXX error("Couldn't allocate temporary frame");
300                         return NULL;
301                 }
302         } else {
303                 rgb_frame = current_frame;
304         }
305
306         rendered_frame = pixels;
307
308         /* Do RGBA-conversion and flipping in one step depending
309            on CPU-Endianess */
310
311         if (ENDIAN_ORDER == L_ENDIAN) {
312                 int y;
313                 for (y = 0; y < height; y++) {
314                         uint8_t* target = rgb_frame->data[0]
315                                 + width * 4 * (height - y - 1);
316                         uint8_t* src = rendered_frame + width * 4 * y;
317                         uint8_t* end = src + width * 4;
318                         while (src != end) {
319                                 target[3] = src[3];
320                                 target[2] = src[2];
321                                 target[1] = src[1];
322                                 target[0] = src[0];
323
324                                 target += 4;
325                                 src += 4;
326                         }
327                 }
328         } else {
329                 int y;
330                 for (y = 0; y < height; y++) {
331                         uint8_t* target = rgb_frame->data[0]
332                                 + width * 4 * (height - y - 1);
333                         uint8_t* src = rendered_frame + width * 4 * y;
334                         uint8_t* end = src + width * 4;
335                         while (src != end) {
336                                 target[3] = src[0];
337                                 target[2] = src[1];
338                                 target[1] = src[2];
339                                 target[0] = src[3];
340
341                                 target += 4;
342                                 src += 4;
343                         }
344                 }
345         }
346
347         if (c->pix_fmt != PIX_FMT_BGR32) {
348                 sws_scale(img_convert_ctx, rgb_frame->data,
349                           rgb_frame->linesize, 0, c->height, 
350                           current_frame->data, current_frame->linesize);
351                 delete_picture(rgb_frame);
352         }
353         return current_frame;
354 }
355
356 static void set_ffmpeg_property_option(AVCodecContext* c, IDProperty * prop)
357 {
358         char name[128];
359         char * param;
360         const AVOption * rv = NULL;
361
362         fprintf(stderr, "FFMPEG expert option: %s: ", prop->name);
363
364         strncpy(name, prop->name, 128);
365
366         param = strchr(name, ':');
367
368         if (param) {
369                 *param++ = 0;
370         }
371
372         switch(prop->type) {
373         case IDP_STRING:
374                 fprintf(stderr, "%s.\n", IDP_String(prop));
375                 rv = av_set_string(c, prop->name, IDP_String(prop));
376                 break;
377         case IDP_FLOAT:
378                 fprintf(stderr, "%g.\n", IDP_Float(prop));
379                 rv = av_set_double(c, prop->name, IDP_Float(prop));
380                 break;
381         case IDP_INT:
382                 fprintf(stderr, "%d.\n", IDP_Int(prop));
383                 
384                 if (param) {
385                         if (IDP_Int(prop)) {
386                                 rv = av_set_string(c, name, param);
387                         } else {
388                                 return;
389                         }
390                 } else {
391                         rv = av_set_int(c, prop->name, IDP_Int(prop));
392                 }
393                 break;
394         }
395
396         if (!rv) {
397                 fprintf(stderr, "ffmpeg-option not supported: %s! Skipping.\n",
398                         prop->name);
399         }
400 }
401
402 static void set_ffmpeg_properties(RenderData *rd, AVCodecContext *c, const char * prop_name)
403 {
404         IDProperty * prop;
405         void * iter;
406         IDProperty * curr;
407
408         if (!rd->ffcodecdata.properties) {
409                 return;
410         }
411         
412         prop = IDP_GetPropertyFromGroup(
413                 rd->ffcodecdata.properties, (char*) prop_name);
414         if (!prop) {
415                 return;
416         }
417
418         iter = IDP_GetGroupIterator(prop);
419
420         while ((curr = IDP_GroupIterNext(iter)) != NULL) {
421                 set_ffmpeg_property_option(c, curr);
422         }
423 }
424
425 /* prepare a video stream for the output file */
426
427 static AVStream* alloc_video_stream(RenderData *rd, int codec_id, AVFormatContext* of,
428                                     int rectx, int recty) 
429 {
430         AVStream* st;
431         AVCodecContext* c;
432         AVCodec* codec;
433         st = av_new_stream(of, 0);
434         if (!st) return NULL;
435
436         /* Set up the codec context */
437         
438         c = get_codec_from_stream(st);
439         c->codec_id = codec_id;
440         c->codec_type = CODEC_TYPE_VIDEO;
441
442
443         /* Get some values from the current render settings */
444         
445         c->width = rectx;
446         c->height = recty;
447
448 #ifdef FFMPEG_CODEC_TIME_BASE
449         /* FIXME: Really bad hack (tm) for NTSC support */
450         if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
451                 c->time_base.den = 2997;
452                 c->time_base.num = 100;
453         } else if ((double) ((int) rd->frs_sec_base) == 
454                    rd->frs_sec_base) {
455                 c->time_base.den = rd->frs_sec;
456                 c->time_base.num = (int) rd->frs_sec_base;
457         } else {
458                 c->time_base.den = rd->frs_sec * 100000;
459                 c->time_base.num = ((double) rd->frs_sec_base) * 100000;
460         }
461 #else
462         /* FIXME: Really bad hack (tm) for NTSC support */
463         if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
464                 c->frame_rate = 2997;
465                 c->frame_rate_base = 100;
466         } else if ((double) ((int) rd->frs_sec_base) == 
467                    rd->frs_sec_base) {
468                 c->frame_rate = rd->frs_sec;
469                 c->frame_rate_base = rd->frs_sec_base;
470         } else {
471                 c->frame_rate = rd->frs_sec * 100000;
472                 c->frame_rate_base = ((double) rd->frs_sec_base)*100000;
473         }
474 #endif
475         
476         c->gop_size = ffmpeg_gop_size;
477         c->bit_rate = ffmpeg_video_bitrate*1000;
478         c->rc_max_rate = rd->ffcodecdata.rc_max_rate*1000;
479         c->rc_min_rate = rd->ffcodecdata.rc_min_rate*1000;
480         c->rc_buffer_size = rd->ffcodecdata.rc_buffer_size * 1024;
481         c->rc_initial_buffer_occupancy 
482                 = rd->ffcodecdata.rc_buffer_size*3/4;
483         c->rc_buffer_aggressivity = 1.0;
484         c->me_method = ME_EPZS;
485         
486         codec = avcodec_find_encoder(c->codec_id);
487         if (!codec) return NULL;
488         
489         /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
490         
491         if (codec->pix_fmts) {
492                 c->pix_fmt = codec->pix_fmts[0];
493         } else {
494                 /* makes HuffYUV happy ... */
495                 c->pix_fmt = PIX_FMT_YUV422P;
496         }
497
498         if (codec_id == CODEC_ID_XVID) {
499                 /* arghhhh ... */
500                 c->pix_fmt = PIX_FMT_YUV420P;
501         }
502         
503         if ((of->oformat->flags & AVFMT_GLOBALHEADER)
504 //              || !strcmp(of->oformat->name, "mp4")
505 //          || !strcmp(of->oformat->name, "mov")
506 //          || !strcmp(of->oformat->name, "3gp")
507                 ) {
508                 fprintf(stderr, "Using global header\n");
509                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
510         }
511         
512         /* Determine whether we are encoding interlaced material or not */
513         if (rd->mode & R_FIELDS) {
514                 fprintf(stderr, "Encoding interlaced video\n");
515                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
516                 c->flags |= CODEC_FLAG_INTERLACED_ME;
517         }
518
519         /* xasp & yasp got float lately... */
520
521         st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(
522                 ((double) rd->xasp / (double) rd->yasp), 255);
523
524         set_ffmpeg_properties(rd, c, "video");
525         
526         if (avcodec_open(c, codec) < 0) {
527                 //
528                 //XXX error("Couldn't initialize codec");
529                 return NULL;
530         }
531
532         video_buffersize = 2000000;
533         video_buffer = (uint8_t*)MEM_mallocN(video_buffersize, 
534                                              "FFMPEG video buffer");
535         
536         current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
537
538         img_convert_ctx = sws_getContext(c->width, c->height,
539                                          PIX_FMT_BGR32,
540                                          c->width, c->height,
541                                          c->pix_fmt,
542                                          SWS_BICUBIC,
543                                          NULL, NULL, NULL);
544         return st;
545 }
546
547 /* Prepare an audio stream for the output file */
548
549 static AVStream* alloc_audio_stream(RenderData *rd, int codec_id, AVFormatContext* of) 
550 {
551         AVStream* st;
552         AVCodecContext* c;
553         AVCodec* codec;
554
555         st = av_new_stream(of, 1);
556         if (!st) return NULL;
557
558         c = get_codec_from_stream(st);
559         c->codec_id = codec_id;
560         c->codec_type = CODEC_TYPE_AUDIO;
561
562         c->sample_rate = rd->audio.mixrate;
563         c->bit_rate = ffmpeg_audio_bitrate*1000;
564         c->channels = 2;
565         codec = avcodec_find_encoder(c->codec_id);
566         if (!codec) {
567                 //XXX error("Couldn't find a valid audio codec");
568                 return NULL;
569         }
570
571         set_ffmpeg_properties(rd, c, "audio");
572
573         if (avcodec_open(c, codec) < 0) {
574                 //XXX error("Couldn't initialize audio codec");
575                 return NULL;
576         }
577
578         /* FIXME: Should be user configurable */
579         if (ffmpeg_type == FFMPEG_DV) {
580                 /* this is a hack around the poor ffmpeg dv multiplexer. */
581                 /* only fixes PAL for now 
582                    (NTSC is a lot more complicated here...)! */
583                 audio_outbuf_size = 7680;
584         } else {
585                 audio_outbuf_size = 10000;
586         }
587         audio_output_buffer = (uint8_t*)MEM_mallocN(
588                 audio_outbuf_size, "FFMPEG audio encoder input buffer");
589
590        /* ugly hack for PCM codecs */
591
592         if (c->frame_size <= 1) {
593                 audio_input_frame_size = audio_outbuf_size / c->channels;
594                 switch(c->codec_id) {
595                 case CODEC_ID_PCM_S16LE:
596                 case CODEC_ID_PCM_S16BE:
597                 case CODEC_ID_PCM_U16LE:
598                 case CODEC_ID_PCM_U16BE:
599                         audio_input_frame_size >>= 1;
600                         break;
601                 default:
602                         break;
603                 }
604         } else {
605                 audio_input_frame_size = c->frame_size;
606         }
607
608         audio_input_buffer = (uint8_t*)MEM_mallocN(
609                 audio_input_frame_size * sizeof(short) * c->channels, 
610                 "FFMPEG audio encoder output buffer");
611
612         return st;
613 }
614 /* essential functions -- start, append, end */
615
616 void start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty)
617 {
618         /* Handle to the output file */
619         AVFormatContext* of;
620         AVOutputFormat* fmt;
621         char name[256];
622         const char ** exts;
623
624         ffmpeg_type = rd->ffcodecdata.type;
625         ffmpeg_codec = rd->ffcodecdata.codec;
626         ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
627         ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
628         ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
629         ffmpeg_gop_size = rd->ffcodecdata.gop_size;
630         ffmpeg_multiplex_audio = rd->ffcodecdata.flags
631                 & FFMPEG_MULTIPLEX_AUDIO;
632         ffmpeg_autosplit = rd->ffcodecdata.flags
633                 & FFMPEG_AUTOSPLIT_OUTPUT;
634         
635         do_init_ffmpeg();
636
637         /* Determine the correct filename */
638         makeffmpegstring(rd, name);
639         fprintf(stderr, "Starting output to %s(ffmpeg)...\n"
640                 "  Using type=%d, codec=%d, audio_codec=%d,\n"
641                 "  video_bitrate=%d, audio_bitrate=%d,\n"
642                 "  gop_size=%d, multiplex=%d, autosplit=%d\n"
643                 "  render width=%d, render height=%d\n", 
644                 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
645                 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
646                 ffmpeg_gop_size, ffmpeg_multiplex_audio,
647                 ffmpeg_autosplit, rectx, recty);
648         
649         exts = get_file_extensions(ffmpeg_type);
650         if (!exts) {
651                 G.afbreek = 1; /* Abort render */
652                 //XXX error("No valid formats found");
653                 return;
654         }
655         fmt = guess_format(NULL, exts[0], NULL);
656         if (!fmt) {
657                 G.afbreek = 1; /* Abort render */
658                 //XXX error("No valid formats found");
659                 return;
660         }
661
662         of = av_alloc_format_context();
663         if (!of) {
664                 G.afbreek = 1;
665                 //XXX error("Error opening output file");
666                 return;
667         }
668         
669         of->oformat = fmt;
670         of->packet_size= rd->ffcodecdata.mux_packet_size;
671         if (ffmpeg_multiplex_audio) {
672                 of->mux_rate = rd->ffcodecdata.mux_rate;
673         } else {
674                 of->mux_rate = 0;
675         }
676
677         of->preload = (int)(0.5*AV_TIME_BASE);
678         of->max_delay = (int)(0.7*AV_TIME_BASE);
679
680         snprintf(of->filename, sizeof(of->filename), "%s", name);
681         /* set the codec to the user's selection */
682         switch(ffmpeg_type) {
683         case FFMPEG_AVI:
684         case FFMPEG_MOV:
685         case FFMPEG_OGG:
686         case FFMPEG_MKV:
687                 fmt->video_codec = ffmpeg_codec;
688                 break;
689         case FFMPEG_DV:
690                 fmt->video_codec = CODEC_ID_DVVIDEO;
691                 break;
692         case FFMPEG_MPEG1:
693                 fmt->video_codec = CODEC_ID_MPEG1VIDEO;
694                 break;
695         case FFMPEG_MPEG2:
696                 fmt->video_codec = CODEC_ID_MPEG2VIDEO;
697                 break;
698         case FFMPEG_H264:
699                 fmt->video_codec = CODEC_ID_H264;
700                 break;
701         case FFMPEG_XVID:
702                 fmt->video_codec = CODEC_ID_XVID;
703                 break;
704         case FFMPEG_FLV:
705                 fmt->video_codec = CODEC_ID_FLV1;
706                 break;
707         case FFMPEG_MPEG4:
708         default:
709                 fmt->video_codec = CODEC_ID_MPEG4;
710                 break;
711         }
712         if (fmt->video_codec == CODEC_ID_DVVIDEO) {
713                 if (rectx != 720) {
714                         G.afbreek = 1;
715                         //XXX error("Render width has to be 720 pixels for DV!");
716                         return;
717                 }
718                 if (rd->frs_sec != 25 && recty != 480) {
719                         G.afbreek = 1;
720                         //XXX error("Render height has to be 480 pixels "
721                         //      "for DV-NTSC!");
722                         return;
723                         
724                 }
725                 if (rd->frs_sec == 25 && recty != 576) {
726                         G.afbreek = 1;
727                         //XXX error("Render height has to be 576 pixels "
728                         //      "for DV-PAL!");
729                         return;
730                 }
731         }
732         
733         fmt->audio_codec = ffmpeg_audio_codec;
734
735         if (ffmpeg_type == FFMPEG_DV) {
736                 fmt->audio_codec = CODEC_ID_PCM_S16LE;
737                 if (ffmpeg_multiplex_audio && rd->audio.mixrate != 48000) {
738                         G.afbreek = 1;
739                         //XXX error("FFMPEG only supports 48khz / stereo "
740                         //      "audio for DV!");
741                         return;
742                 }
743         }
744         
745         video_stream = alloc_video_stream(rd, fmt->video_codec, of, rectx, recty);
746         if (!video_stream) {
747                 G.afbreek = 1;
748                 //XXX error("Error initializing video stream");
749                 return;
750         }
751         
752         if (ffmpeg_multiplex_audio) {
753                 audio_stream = alloc_audio_stream(rd, fmt->audio_codec, of);
754                 if (!audio_stream) {
755                         G.afbreek = 1;
756                         //XXX error("Error initializing audio stream");
757                         return;
758                 }
759                 //XXX audiostream_play(SFRA, 0, 1);
760         }
761         if (av_set_parameters(of, NULL) < 0) {
762                 G.afbreek = 1;
763                 //XXX error("Error setting output parameters");
764                 return;
765         }
766         if (!(fmt->flags & AVFMT_NOFILE)) {
767                 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
768                         G.afbreek = 1;
769                         //
770                         //XXX error("Could not open file for writing");
771                         return;
772                 }
773         }
774
775         av_write_header(of);
776         outfile = of;
777         dump_format(of, 0, name, 1);
778 }
779
780 /* **********************************************************************
781    * public interface
782    ********************************************************************** */
783
784 /* Get the output filename-- similar to the other output formats */
785 static void makeffmpegstring(RenderData* rd, char* string) {
786
787         // XXX quick define, solve!
788 #define FILE_MAXDIR 256
789 #define FILE_MAXFILE 126
790                 
791         char txt[FILE_MAXDIR+FILE_MAXFILE];
792         // XXX
793 #undef FILE_MAXDIR
794 #undef FILE_MAXFILE
795         char autosplit[20];
796
797         const char ** exts = get_file_extensions(rd->ffcodecdata.type);
798         const char ** fe = exts;
799
800         if (!string || !exts) return;
801
802         strcpy(string, rd->pic);
803         BLI_convertstringcode(string, G.sce);
804         BLI_convertstringframe(string, rd->cfra);
805
806         BLI_make_existing_file(string);
807
808         autosplit[0] = 0;
809
810         if ((rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
811                 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
812         }
813
814         while (*fe) {
815                 if (BLI_strcasecmp(string + strlen(string) - strlen(*fe), 
816                                    *fe) == 0) {
817                         break;
818                 }
819                 fe++;
820         }
821
822         if (!*fe) {
823                 strcat(string, autosplit);
824                 sprintf(txt, "%04d_%04d%s", (rd->sfra), 
825                         (rd->efra), *exts);
826                 strcat(string, txt);
827         } else {
828                 *(string + strlen(string) - strlen(*fe)) = 0;
829                 strcat(string, autosplit);
830                 strcat(string, *fe);
831         }
832 }
833
834
835 void start_ffmpeg(struct Scene *scene, RenderData *rd, int rectx, int recty)
836 {
837         ffmpeg_autosplit_count = 0;
838
839         start_ffmpeg_impl(rd, rectx, recty);
840
841         if(ffmpeg_multiplex_audio && audio_stream)
842         {
843                 AVCodecContext* c = get_codec_from_stream(audio_stream);
844                 AUD_Specs specs;
845                 specs.channels = c->channels;
846                 specs.format = AUD_FORMAT_S16;
847                 specs.rate = rd->audio.mixrate;
848                 audio_mixdown_device = sound_mixdown(scene, specs, rd->sfra, rd->efra);
849         }
850 }
851
852 void end_ffmpeg(void);
853
854 static void write_audio_frames()
855 {
856         int finished = 0;
857
858         while (ffmpeg_multiplex_audio && !finished) {
859                 double a_pts = ((double)audio_stream->pts.val 
860                                 * audio_stream->time_base.num 
861                                 / audio_stream->time_base.den);
862                 double v_pts = ((double)video_stream->pts.val 
863                                 * video_stream->time_base.num 
864                                 / video_stream->time_base.den);
865                 
866                 if (a_pts < v_pts) {
867                         write_audio_frame();
868                 } else {
869                         finished = 1;
870                 }
871         }
872 }
873
874 void append_ffmpeg(RenderData *rd, int frame, int *pixels, int rectx, int recty) 
875 {
876         fprintf(stderr, "Writing frame %i, "
877                 "render width=%d, render height=%d\n", frame,
878                 rectx, recty);
879
880         write_audio_frames();
881         write_video_frame(rd, generate_video_frame((unsigned char*) pixels));
882
883         if (ffmpeg_autosplit) {
884                 if (url_ftell(OUTFILE_PB) > FFMPEG_AUTOSPLIT_SIZE) {
885                         end_ffmpeg();
886                         ffmpeg_autosplit_count++;
887                         start_ffmpeg_impl(rd, rectx, recty);
888                 }
889         }
890 }
891
892
893 void end_ffmpeg(void)
894 {
895         int i;
896         
897         fprintf(stderr, "Closing ffmpeg...\n");
898
899         if (audio_stream && video_stream) {
900                 write_audio_frames();
901         }
902
903         if(audio_mixdown_device)
904         {
905                 AUD_closeReadDevice(audio_mixdown_device);
906                 audio_mixdown_device = 0;
907         }
908         
909         if (outfile) {
910                 av_write_trailer(outfile);
911         }
912         
913         /* Close the video codec */
914
915         if (video_stream && get_codec_from_stream(video_stream)) {
916                 avcodec_close(get_codec_from_stream(video_stream));
917                 video_stream = 0;
918         }
919
920         
921         /* Close the output file */
922         if (outfile) {
923                 for (i = 0; i < outfile->nb_streams; i++) {
924                         if (&outfile->streams[i]) {
925                                 av_freep(&outfile->streams[i]);
926                         }
927                 }
928         }
929         /* free the temp buffer */
930         if (current_frame) {
931                 delete_picture(current_frame);
932                 current_frame = 0;
933         }
934         if (outfile && outfile->oformat) {
935                 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
936                         url_fclose(OUTFILE_PB);
937                 }
938         }
939         if (outfile) {
940                 av_free(outfile);
941                 outfile = 0;
942         }
943         if (video_buffer) {
944                 MEM_freeN(video_buffer);
945                 video_buffer = 0;
946         }
947         if (audio_output_buffer) {
948                 MEM_freeN(audio_output_buffer);
949                 audio_output_buffer = 0;
950         }
951         if (audio_input_buffer) {
952                 MEM_freeN(audio_input_buffer);
953                 audio_input_buffer = 0;
954         }
955
956         if (img_convert_ctx) {
957                 sws_freeContext(img_convert_ctx);
958                 img_convert_ctx = 0;
959         }
960 }
961
962 /* properties */
963
964 void ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
965 {
966         struct IDProperty *prop = (struct IDProperty *) prop_;
967         IDProperty * group;
968         
969         if (!rd->ffcodecdata.properties) {
970                 return;
971         }
972
973         group = IDP_GetPropertyFromGroup(
974                 rd->ffcodecdata.properties, (char*) type);
975         if (group && prop) {
976                 IDP_RemFromGroup(group, prop);
977                 IDP_FreeProperty(prop);
978                 MEM_freeN(prop);
979         }
980 }
981
982 IDProperty *ffmpeg_property_add(RenderData *rd, char * type, int opt_index, int parent_index)
983 {
984         AVCodecContext c;
985         const AVOption * o;
986         const AVOption * parent;
987         IDProperty * group;
988         IDProperty * prop;
989         IDPropertyTemplate val;
990         int idp_type;
991         char name[256];
992
993         avcodec_get_context_defaults(&c);
994
995         o = c.av_class->option + opt_index;
996         parent = c.av_class->option + parent_index;
997
998         if (!rd->ffcodecdata.properties) {
999                 IDPropertyTemplate val;
1000
1001                 rd->ffcodecdata.properties 
1002                         = IDP_New(IDP_GROUP, val, "ffmpeg"); 
1003         }
1004
1005         group = IDP_GetPropertyFromGroup(
1006                 rd->ffcodecdata.properties, (char*) type);
1007         
1008         if (!group) {
1009                 IDPropertyTemplate val;
1010                 
1011                 group = IDP_New(IDP_GROUP, val, (char*) type); 
1012                 IDP_AddToGroup(rd->ffcodecdata.properties, group);
1013         }
1014
1015         if (parent_index) {
1016                 sprintf(name, "%s:%s", parent->name, o->name);
1017         } else {
1018                 strcpy(name, o->name);
1019         }
1020
1021         fprintf(stderr, "ffmpeg_property_add: %s %d %d %s\n",
1022                 type, parent_index, opt_index, name);
1023
1024         prop = IDP_GetPropertyFromGroup(group, name);
1025         if (prop) {
1026                 return prop;
1027         }
1028
1029         switch (o->type) {
1030         case FF_OPT_TYPE_INT:
1031         case FF_OPT_TYPE_INT64:
1032                 val.i = o->default_val;
1033                 idp_type = IDP_INT;
1034                 break;
1035         case FF_OPT_TYPE_DOUBLE:
1036         case FF_OPT_TYPE_FLOAT:
1037                 val.f = o->default_val;
1038                 idp_type = IDP_FLOAT;
1039                 break;
1040         case FF_OPT_TYPE_STRING:
1041                 val.str = "                                                                               ";
1042                 idp_type = IDP_STRING;
1043                 break;
1044         case FF_OPT_TYPE_CONST:
1045                 val.i = 1;
1046                 idp_type = IDP_INT;
1047                 break;
1048         default:
1049                 return NULL;
1050         }
1051         prop = IDP_New(idp_type, val, name);
1052         IDP_AddToGroup(group, prop);
1053         return prop;
1054 }
1055
1056 /* not all versions of ffmpeg include that, so here we go ... */
1057
1058 static const AVOption *my_av_find_opt(void *v, const char *name, 
1059                                       const char *unit, int mask, int flags){
1060         AVClass *c= *(AVClass**)v; 
1061         const AVOption *o= c->option;
1062
1063         for(;o && o->name; o++){
1064                 if(!strcmp(o->name, name) && 
1065                    (!unit || (o->unit && !strcmp(o->unit, unit))) && 
1066                    (o->flags & mask) == flags )
1067                         return o;
1068         }
1069         return NULL;
1070 }
1071
1072 int ffmpeg_property_add_string(RenderData *rd, const char * type, const char * str)
1073 {
1074         AVCodecContext c;
1075         const AVOption * o = 0;
1076         const AVOption * p = 0;
1077         char name_[128];
1078         char * name;
1079         char * param;
1080         IDProperty * prop;
1081         
1082         avcodec_get_context_defaults(&c);
1083
1084         strncpy(name_, str, 128);
1085
1086         name = name_;
1087         while (*name == ' ') name++;
1088
1089         param = strchr(name, ':');
1090
1091         if (!param) {
1092                 param = strchr(name, ' ');
1093         }
1094         if (param) {
1095                 *param++ = 0;
1096                 while (*param == ' ') param++;
1097         }
1098         
1099         o = my_av_find_opt(&c, name, NULL, 0, 0);       
1100         if (!o) {
1101                 return 0;
1102         }
1103         if (param && o->type == FF_OPT_TYPE_CONST) {
1104                 return 0;
1105         }
1106         if (param && o->type != FF_OPT_TYPE_CONST && o->unit) {
1107                 p = my_av_find_opt(&c, param, o->unit, 0, 0);   
1108                 prop = ffmpeg_property_add(rd,
1109                         (char*) type, p - c.av_class->option, 
1110                         o - c.av_class->option);
1111         } else {
1112                 prop = ffmpeg_property_add(rd,
1113                         (char*) type, o - c.av_class->option, 0);
1114         }
1115                 
1116
1117         if (!prop) {
1118                 return 0;
1119         }
1120
1121         if (param && !p) {
1122                 switch (prop->type) {
1123                 case IDP_INT:
1124                         IDP_Int(prop) = atoi(param);
1125                         break;
1126                 case IDP_FLOAT:
1127                         IDP_Float(prop) = atof(param);
1128                         break;
1129                 case IDP_STRING:
1130                         strncpy(IDP_String(prop), param, prop->len);
1131                         break;
1132                 }
1133         }
1134         return 1;
1135 }
1136
1137 void ffmpeg_set_preset(RenderData *rd, int preset)
1138 {
1139         int isntsc = (rd->frs_sec != 25);
1140
1141         switch (preset) {
1142         case FFMPEG_PRESET_VCD:
1143                 rd->ffcodecdata.type = FFMPEG_MPEG1;
1144                 rd->ffcodecdata.video_bitrate = 1150;
1145                 rd->xsch = 352;
1146                 rd->ysch = isntsc ? 240 : 288;
1147                 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1148                 rd->ffcodecdata.rc_max_rate = 1150;
1149                 rd->ffcodecdata.rc_min_rate = 1150;
1150                 rd->ffcodecdata.rc_buffer_size = 40*8;
1151                 rd->ffcodecdata.mux_packet_size = 2324;
1152                 rd->ffcodecdata.mux_rate = 2352 * 75 * 8;
1153                 break;
1154
1155         case FFMPEG_PRESET_SVCD:
1156                 rd->ffcodecdata.type = FFMPEG_MPEG2;
1157                 rd->ffcodecdata.video_bitrate = 2040;
1158                 rd->xsch = 480;
1159                 rd->ysch = isntsc ? 480 : 576;
1160                 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1161                 rd->ffcodecdata.rc_max_rate = 2516;
1162                 rd->ffcodecdata.rc_min_rate = 0;
1163                 rd->ffcodecdata.rc_buffer_size = 224*8;
1164                 rd->ffcodecdata.mux_packet_size = 2324;
1165                 rd->ffcodecdata.mux_rate = 0;
1166                 break;
1167
1168         case FFMPEG_PRESET_DVD:
1169                 rd->ffcodecdata.type = FFMPEG_MPEG2;
1170                 rd->ffcodecdata.video_bitrate = 6000;
1171                 rd->xsch = 720;
1172                 rd->ysch = isntsc ? 480 : 576;
1173                 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1174                 rd->ffcodecdata.rc_max_rate = 9000;
1175                 rd->ffcodecdata.rc_min_rate = 0;
1176                 rd->ffcodecdata.rc_buffer_size = 224*8;
1177                 rd->ffcodecdata.mux_packet_size = 2048;
1178                 rd->ffcodecdata.mux_rate = 10080000;
1179                 break;
1180
1181         case FFMPEG_PRESET_DV:
1182                 rd->ffcodecdata.type = FFMPEG_DV;
1183                 rd->xsch = 720;
1184                 rd->ysch = isntsc ? 480 : 576;
1185                 break;
1186
1187         case FFMPEG_PRESET_H264:
1188                 rd->ffcodecdata.type = FFMPEG_AVI;
1189                 rd->ffcodecdata.codec = CODEC_ID_H264;
1190                 rd->ffcodecdata.video_bitrate = 6000;
1191                 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1192                 rd->ffcodecdata.rc_max_rate = 9000;
1193                 rd->ffcodecdata.rc_min_rate = 0;
1194                 rd->ffcodecdata.rc_buffer_size = 224*8;
1195                 rd->ffcodecdata.mux_packet_size = 2048;
1196                 rd->ffcodecdata.mux_rate = 10080000;
1197
1198                 ffmpeg_property_add_string(rd, "video", "coder:vlc");
1199                 ffmpeg_property_add_string(rd, "video", "flags:loop");
1200                 ffmpeg_property_add_string(rd, "video", "cmp:chroma");
1201                 ffmpeg_property_add_string(rd, "video", "partitions:parti4x4");
1202                 ffmpeg_property_add_string(rd, "video", "partitions:partp8x8");
1203                 ffmpeg_property_add_string(rd, "video", "partitions:partb8x8");
1204                 ffmpeg_property_add_string(rd, "video", "me:hex");
1205                 ffmpeg_property_add_string(rd, "video", "subq:5");
1206                 ffmpeg_property_add_string(rd, "video", "me_range:16");
1207                 ffmpeg_property_add_string(rd, "video", "keyint_min:25");
1208                 ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
1209                 ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
1210                 ffmpeg_property_add_string(rd, "video", "b_strategy:1");
1211
1212                 break;
1213
1214         case FFMPEG_PRESET_THEORA:
1215         case FFMPEG_PRESET_XVID:
1216                 if(preset == FFMPEG_PRESET_XVID) {
1217                         rd->ffcodecdata.type = FFMPEG_AVI;
1218                         rd->ffcodecdata.codec = CODEC_ID_XVID;
1219                 }
1220                 else if(preset == FFMPEG_PRESET_THEORA) {
1221                         rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
1222                         rd->ffcodecdata.codec = CODEC_ID_THEORA;
1223                 }
1224
1225                 rd->ffcodecdata.video_bitrate = 6000;
1226                 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1227                 rd->ffcodecdata.rc_max_rate = 9000;
1228                 rd->ffcodecdata.rc_min_rate = 0;
1229                 rd->ffcodecdata.rc_buffer_size = 224*8;
1230                 rd->ffcodecdata.mux_packet_size = 2048;
1231                 rd->ffcodecdata.mux_rate = 10080000;
1232                 break;
1233
1234         }
1235 }
1236
1237 void ffmpeg_verify_image_type(RenderData *rd)
1238 {
1239         int audio= 0;
1240
1241         if(rd->imtype == R_FFMPEG) {
1242                 if(rd->ffcodecdata.type <= 0 ||
1243                    rd->ffcodecdata.codec <= 0 ||
1244                    rd->ffcodecdata.audio_codec <= 0 ||
1245                    rd->ffcodecdata.video_bitrate <= 1) {
1246
1247                         rd->ffcodecdata.codec = CODEC_ID_MPEG2VIDEO;
1248                         ffmpeg_set_preset(rd, FFMPEG_PRESET_DVD);
1249                 }
1250
1251                 audio= 1;
1252         }
1253         else if(rd->imtype == R_H264) {
1254                 if(rd->ffcodecdata.codec != CODEC_ID_H264) {
1255                         ffmpeg_set_preset(rd, FFMPEG_PRESET_H264);
1256                         audio= 1;
1257                 }
1258         }
1259         else if(rd->imtype == R_XVID) {
1260                 if(rd->ffcodecdata.codec != CODEC_ID_XVID) {
1261                         ffmpeg_set_preset(rd, FFMPEG_PRESET_XVID);
1262                         audio= 1;
1263                 }
1264         }
1265         else if(rd->imtype == R_THEORA) {
1266                 if(rd->ffcodecdata.codec != CODEC_ID_THEORA) {
1267                         ffmpeg_set_preset(rd, FFMPEG_PRESET_THEORA);
1268                         audio= 1;
1269                 }
1270         }
1271
1272         if(audio && rd->ffcodecdata.audio_codec <= 0) {
1273                 rd->ffcodecdata.audio_codec = CODEC_ID_MP2;
1274                 rd->ffcodecdata.audio_bitrate = 128;
1275         }
1276 }
1277
1278 #endif
1279