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