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