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