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