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