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