a19e4142894ead3bc8570c26c21fa76ad617b7e8
[blender-staging.git] / source / blender / blenkernel / intern / writeffmpeg.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s):
19  *
20  * Partial Copyright (c) 2006 Peter Schlaile
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/blenkernel/intern/writeffmpeg.c
26  *  \ingroup bke
27  */
28
29 #ifdef WITH_FFMPEG
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <stdlib.h>
34
35 #include <libavformat/avformat.h>
36 #include <libavcodec/avcodec.h>
37 #include <libavutil/rational.h>
38 #include <libavutil/samplefmt.h>
39 #include <libswscale/swscale.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_scene_types.h"
44
45 #include "BLI_blenlib.h"
46
47 #ifdef WITH_AUDASPACE
48 #  include AUD_DEVICE_H
49 #  include AUD_SPECIAL_H
50 #endif
51
52 #include "BLI_utildefines.h"
53
54 #include "BKE_global.h"
55 #include "BKE_idprop.h"
56 #include "BKE_main.h"
57 #include "BKE_report.h"
58 #include "BKE_sound.h"
59 #include "BKE_writeffmpeg.h"
60
61 #include "IMB_imbuf.h"
62
63 #include "ffmpeg_compat.h"
64
65 typedef struct FFMpegContext {
66         int ffmpeg_type;
67         int ffmpeg_codec;
68         int ffmpeg_audio_codec;
69         int ffmpeg_video_bitrate;
70         int ffmpeg_audio_bitrate;
71         int ffmpeg_gop_size;
72         int ffmpeg_max_b_frames;
73         int ffmpeg_autosplit;
74         int ffmpeg_autosplit_count;
75         bool ffmpeg_preview;
76
77         int ffmpeg_crf;  /* set to 0 to not use CRF mode; we have another flag for lossless anyway. */
78         int ffmpeg_preset; /* see FFMpegPreset */
79
80         AVFormatContext *outfile;
81         AVStream *video_stream;
82         AVStream *audio_stream;
83         AVFrame *current_frame;
84         struct SwsContext *img_convert_ctx;
85
86         uint8_t *audio_input_buffer;
87         uint8_t *audio_deinterleave_buffer;
88         int audio_input_samples;
89 #ifndef FFMPEG_HAVE_ENCODE_AUDIO2
90         uint8_t *audio_output_buffer;
91         int audio_outbuf_size;
92 #endif
93         double audio_time;
94         bool audio_deinterleave;
95         int audio_sample_size;
96
97 #ifdef WITH_AUDASPACE
98         AUD_Device *audio_mixdown_device;
99 #endif
100 } FFMpegContext;
101
102 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
103
104 #define PRINT if (G.debug & G_DEBUG_FFMPEG) printf
105
106 static void ffmpeg_dict_set_int(AVDictionary **dict, const char *key, int value);
107 static void ffmpeg_dict_set_float(AVDictionary **dict, const char *key, float value);
108 static void ffmpeg_set_expert_options(RenderData *rd);
109 static void ffmpeg_filepath_get(FFMpegContext *context, char *string, struct RenderData *rd, bool preview, const char *suffix);
110
111 /* Delete a picture buffer */
112
113 static void delete_picture(AVFrame *f)
114 {
115         if (f) {
116                 if (f->data[0]) MEM_freeN(f->data[0]);
117                 av_free(f);
118         }
119 }
120
121 static int request_float_audio_buffer(int codec_id)
122 {
123         /* If any of these codecs, we prefer the float sample format (if supported) */
124         return codec_id == AV_CODEC_ID_AAC || codec_id == AV_CODEC_ID_AC3 || codec_id == AV_CODEC_ID_VORBIS;
125 }
126
127 #ifdef WITH_AUDASPACE
128 static int write_audio_frame(FFMpegContext *context)
129 {
130         AVCodecContext *c = NULL;
131         AVPacket pkt;
132         AVFrame *frame = NULL;
133         int got_output = 0;
134
135         c = context->audio_stream->codec;
136
137         av_init_packet(&pkt);
138         pkt.size = 0;
139         pkt.data = NULL;
140
141         AUD_Device_read(context->audio_mixdown_device, context->audio_input_buffer, context->audio_input_samples);
142         context->audio_time += (double) context->audio_input_samples / (double) c->sample_rate;
143
144 #ifdef FFMPEG_HAVE_ENCODE_AUDIO2
145         frame = av_frame_alloc();
146         av_frame_unref(frame);
147         frame->pts = context->audio_time / av_q2d(c->time_base);
148         frame->nb_samples = context->audio_input_samples;
149         frame->format = c->sample_fmt;
150 #ifdef FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
151         frame->channel_layout = c->channel_layout;
152 #endif
153
154         if (context->audio_deinterleave) {
155                 int channel, i;
156                 uint8_t *temp;
157
158                 for (channel = 0; channel < c->channels; channel++) {
159                         for (i = 0; i < frame->nb_samples; i++) {
160                                 memcpy(context->audio_deinterleave_buffer + (i + channel * frame->nb_samples) * context->audio_sample_size,
161                                            context->audio_input_buffer + (c->channels * i + channel) * context->audio_sample_size, context->audio_sample_size);
162                         }
163                 }
164
165                 temp = context->audio_deinterleave_buffer;
166                 context->audio_deinterleave_buffer = context->audio_input_buffer;
167                 context->audio_input_buffer = temp;
168         }
169
170         avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, context->audio_input_buffer,
171                                  context->audio_input_samples * c->channels * context->audio_sample_size, 1);
172
173         if (avcodec_encode_audio2(c, &pkt, frame, &got_output) < 0) {
174                 // XXX error("Error writing audio packet");
175                 return -1;
176         }
177
178         if (!got_output) {
179                 av_frame_free(&frame);
180                 return 0;
181         }
182 #else
183         pkt.size = avcodec_encode_audio(c, context->audio_output_buffer, context->audio_outbuf_size, (short *) context->audio_input_buffer);
184
185         if (pkt.size < 0) {
186                 // XXX error("Error writing audio packet");
187                 return -1;
188         }
189
190         pkt.data = context->audio_output_buffer;
191         got_output = 1;
192 #endif
193
194         if (got_output) {
195                 if (pkt.pts != AV_NOPTS_VALUE)
196                         pkt.pts = av_rescale_q(pkt.pts, c->time_base, context->audio_stream->time_base);
197                 if (pkt.dts != AV_NOPTS_VALUE)
198                         pkt.dts = av_rescale_q(pkt.dts, c->time_base, context->audio_stream->time_base);
199                 if (pkt.duration > 0)
200                         pkt.duration = av_rescale_q(pkt.duration, c->time_base, context->audio_stream->time_base);
201
202                 pkt.stream_index = context->audio_stream->index;
203
204                 pkt.flags |= AV_PKT_FLAG_KEY;
205
206                 if (av_interleaved_write_frame(context->outfile, &pkt) != 0) {
207                         fprintf(stderr, "Error writing audio packet!\n");
208                         if (frame)
209                                 av_frame_free(&frame);
210                         return -1;
211                 }
212
213                 av_free_packet(&pkt);
214         }
215
216         if (frame)
217                 av_frame_free(&frame);
218
219         return 0;
220 }
221 #endif // #ifdef WITH_AUDASPACE
222
223 /* Allocate a temporary frame */
224 static AVFrame *alloc_picture(int pix_fmt, int width, int height)
225 {
226         AVFrame *f;
227         uint8_t *buf;
228         int size;
229         
230         /* allocate space for the struct */
231         f = av_frame_alloc();
232         if (!f) return NULL;
233         size = avpicture_get_size(pix_fmt, width, height);
234         /* allocate the actual picture buffer */
235         buf = MEM_mallocN(size, "AVFrame buffer");
236         if (!buf) {
237                 free(f);
238                 return NULL;
239         }
240         avpicture_fill((AVPicture *)f, buf, pix_fmt, width, height);
241         return f;
242 }
243
244 /* Get the correct file extensions for the requested format,
245  * first is always desired guess_format parameter */
246 static const char **get_file_extensions(int format)
247 {
248         switch (format) {
249                 case FFMPEG_DV:
250                 {
251                         static const char *rv[] = { ".dv", NULL };
252                         return rv;
253                 }
254                 case FFMPEG_MPEG1:
255                 {
256                         static const char *rv[] = { ".mpg", ".mpeg", NULL };
257                         return rv;
258                 }
259                 case FFMPEG_MPEG2:
260                 {
261                         static const char *rv[] = { ".dvd", ".vob", ".mpg", ".mpeg", NULL };
262                         return rv;
263                 }
264                 case FFMPEG_MPEG4:
265                 {
266                         static const char *rv[] = { ".mp4", ".mpg", ".mpeg", NULL };
267                         return rv;
268                 }
269                 case FFMPEG_AVI:
270                 {
271                         static const char *rv[] = { ".avi", NULL };
272                         return rv;
273                 }
274                 case FFMPEG_MOV:
275                 {
276                         static const char *rv[] = { ".mov", NULL };
277                         return rv;
278                 }
279                 case FFMPEG_H264:
280                 {
281                         /* FIXME: avi for now... */
282                         static const char *rv[] = { ".avi", NULL };
283                         return rv;
284                 }
285
286                 case FFMPEG_XVID:
287                 {
288                         /* FIXME: avi for now... */
289                         static const char *rv[] = { ".avi", NULL };
290                         return rv;
291                 }
292                 case FFMPEG_FLV:
293                 {
294                         static const char *rv[] = { ".flv", NULL };
295                         return rv;
296                 }
297                 case FFMPEG_MKV:
298                 {
299                         static const char *rv[] = { ".mkv", NULL };
300                         return rv;
301                 }
302                 case FFMPEG_OGG:
303                 {
304                         static const char *rv[] = { ".ogv", ".ogg", NULL };
305                         return rv;
306                 }
307                 default:
308                         return NULL;
309         }
310 }
311
312 /* Write a frame to the output file */
313 static int write_video_frame(FFMpegContext *context, RenderData *rd, int cfra, AVFrame *frame, ReportList *reports)
314 {
315         int got_output;
316         int ret, success = 1;
317         AVCodecContext *c = context->video_stream->codec;
318         AVPacket packet = { 0 };
319
320         av_init_packet(&packet);
321
322         frame->pts = cfra;
323
324         if (rd->mode & R_FIELDS) {
325                 frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
326         }
327
328         ret = avcodec_encode_video2(c, &packet, frame, &got_output);
329
330         if (ret >= 0 && got_output) {
331                 if (packet.pts != AV_NOPTS_VALUE) {
332                         packet.pts = av_rescale_q(packet.pts, c->time_base, context->video_stream->time_base);
333                         PRINT("Video Frame PTS: %d\n", (int)packet.pts);
334                 }
335                 else {
336                         PRINT("Video Frame PTS: not set\n");
337                 }
338                 if (packet.dts != AV_NOPTS_VALUE) {
339                         packet.dts = av_rescale_q(packet.dts, c->time_base, context->video_stream->time_base);
340                         PRINT("Video Frame DTS: %d\n", (int)packet.dts);
341                 }
342                 else {
343                         PRINT("Video Frame DTS: not set\n");
344                 }
345
346                 packet.stream_index = context->video_stream->index;
347                 ret = av_interleaved_write_frame(context->outfile, &packet);
348                 success = (ret == 0);
349         }
350         else if (ret < 0) {
351                 success = 0;
352         }
353
354         if (!success)
355                 BKE_report(reports, RPT_ERROR, "Error writing frame");
356
357         return success;
358 }
359
360 /* read and encode a frame of audio from the buffer */
361 static AVFrame *generate_video_frame(FFMpegContext *context, uint8_t *pixels, ReportList *reports)
362 {
363         uint8_t *rendered_frame;
364
365         AVCodecContext *c = context->video_stream->codec;
366         int width = c->width;
367         int height = c->height;
368         AVFrame *rgb_frame;
369
370         if (c->pix_fmt != AV_PIX_FMT_BGR32) {
371                 rgb_frame = alloc_picture(AV_PIX_FMT_BGR32, width, height);
372                 if (!rgb_frame) {
373                         BKE_report(reports, RPT_ERROR, "Could not allocate temporary frame");
374                         return NULL;
375                 }
376         }
377         else {
378                 rgb_frame = context->current_frame;
379         }
380
381         rendered_frame = pixels;
382
383         /* Do RGBA-conversion and flipping in one step depending
384          * on CPU-Endianess */
385
386         if (ENDIAN_ORDER == L_ENDIAN) {
387                 int y;
388                 for (y = 0; y < height; y++) {
389                         uint8_t *target = rgb_frame->data[0] + width * 4 * (height - y - 1);
390                         uint8_t *src = rendered_frame + width * 4 * y;
391                         uint8_t *end = src + width * 4;
392                         while (src != end) {
393                                 target[3] = src[3];
394                                 target[2] = src[2];
395                                 target[1] = src[1];
396                                 target[0] = src[0];
397
398                                 target += 4;
399                                 src += 4;
400                         }
401                 }
402         }
403         else {
404                 int y;
405                 for (y = 0; y < height; y++) {
406                         uint8_t *target = rgb_frame->data[0] + width * 4 * (height - y - 1);
407                         uint8_t *src = rendered_frame + width * 4 * y;
408                         uint8_t *end = src + width * 4;
409                         while (src != end) {
410                                 target[3] = src[0];
411                                 target[2] = src[1];
412                                 target[1] = src[2];
413                                 target[0] = src[3];
414
415                                 target += 4;
416                                 src += 4;
417                         }
418                 }
419         }
420
421         if (c->pix_fmt != AV_PIX_FMT_BGR32) {
422                 sws_scale(context->img_convert_ctx, (const uint8_t *const *) rgb_frame->data,
423                           rgb_frame->linesize, 0, c->height,
424                           context->current_frame->data, context->current_frame->linesize);
425                 delete_picture(rgb_frame);
426         }
427
428         context->current_frame->format = AV_PIX_FMT_BGR32;
429         context->current_frame->width = width;
430         context->current_frame->height = height;
431
432         return context->current_frame;
433 }
434
435 static void set_ffmpeg_property_option(AVCodecContext *c, IDProperty *prop, AVDictionary **dictionary)
436 {
437         char name[128];
438         char *param;
439
440         PRINT("FFMPEG expert option: %s: ", prop->name);
441
442         BLI_strncpy(name, prop->name, sizeof(name));
443
444         param = strchr(name, ':');
445
446         if (param) {
447                 *param++ = '\0';
448         }
449
450         switch (prop->type) {
451                 case IDP_STRING:
452                         PRINT("%s.\n", IDP_String(prop));
453                         av_dict_set(dictionary, name, IDP_String(prop), 0);
454                         break;
455                 case IDP_FLOAT:
456                         PRINT("%g.\n", IDP_Float(prop));
457                         ffmpeg_dict_set_float(dictionary, prop->name, IDP_Float(prop));
458                         break;
459                 case IDP_INT:
460                         PRINT("%d.\n", IDP_Int(prop));
461
462                         if (param) {
463                                 if (IDP_Int(prop)) {
464                                         av_dict_set(dictionary, name, param, 0);
465                                 }
466                                 else {
467                                         return;
468                                 }
469                         }
470                         else {
471                                 ffmpeg_dict_set_int(dictionary, prop->name, IDP_Int(prop));
472                         }
473                         break;
474         }
475 }
476
477 static int ffmpeg_proprty_valid(AVCodecContext *c, const char *prop_name, IDProperty *curr)
478 {
479         int valid = 1;
480
481         if (STREQ(prop_name, "video")) {
482                 if (STREQ(curr->name, "bf")) {
483                         /* flash codec doesn't support b frames */
484                         valid &= c->codec_id != AV_CODEC_ID_FLV1;
485                 }
486         }
487
488         return valid;
489 }
490
491 static void set_ffmpeg_properties(RenderData *rd, AVCodecContext *c, const char *prop_name,
492                                   AVDictionary **dictionary)
493 {
494         IDProperty *prop;
495         IDProperty *curr;
496
497         /* TODO(sergey): This is actually rather stupid, because changing
498          * codec settings in render panel would also set expert options.
499          *
500          * But we need ti here in order to get rid of deprecated settings
501          * when opening old files in new blender.
502          *
503          * For as long we don't allow editing properties in the interface
504          * it's all good. bug if we allow editing them, we'll need to
505          * replace it with some smarter code which would port settings
506          * from deprecated to new one.
507          */
508         ffmpeg_set_expert_options(rd);
509
510         if (!rd->ffcodecdata.properties) {
511                 return;
512         }
513         
514         prop = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, prop_name);
515         if (!prop) {
516                 return;
517         }
518
519         for (curr = prop->data.group.first; curr; curr = curr->next) {
520                 if (ffmpeg_proprty_valid(c, prop_name, curr))
521                         set_ffmpeg_property_option(c, curr, dictionary);
522         }
523 }
524
525 /* prepare a video stream for the output file */
526
527 static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int codec_id, AVFormatContext *of,
528                                     int rectx, int recty, char *error, int error_size)
529 {
530         AVStream *st;
531         AVCodecContext *c;
532         AVCodec *codec;
533         AVDictionary *opts = NULL;
534
535         error[0] = '\0';
536
537         st = avformat_new_stream(of, NULL);
538         if (!st) return NULL;
539         st->id = 0;
540
541         /* Set up the codec context */
542         
543         c = st->codec;
544         c->codec_id = codec_id;
545         c->codec_type = AVMEDIA_TYPE_VIDEO;
546
547         /* Get some values from the current render settings */
548         
549         c->width = rectx;
550         c->height = recty;
551
552         /* FIXME: Really bad hack (tm) for NTSC support */
553         if (context->ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
554                 c->time_base.den = 2997;
555                 c->time_base.num = 100;
556         }
557         else if ((float) ((int) rd->frs_sec_base) == rd->frs_sec_base) {
558                 c->time_base.den = rd->frs_sec;
559                 c->time_base.num = (int) rd->frs_sec_base;
560         }
561         else {
562                 c->time_base.den = rd->frs_sec * 100000;
563                 c->time_base.num = ((double) rd->frs_sec_base) * 100000;
564         }
565         
566         c->gop_size = context->ffmpeg_gop_size;
567         c->max_b_frames = context->ffmpeg_max_b_frames;
568
569         if (context->ffmpeg_crf >= 0) {
570                 ffmpeg_dict_set_int(&opts, "crf", context->ffmpeg_crf);
571         }
572         else {
573                 c->bit_rate = context->ffmpeg_video_bitrate * 1000;
574                 c->rc_max_rate = rd->ffcodecdata.rc_max_rate * 1000;
575                 c->rc_min_rate = rd->ffcodecdata.rc_min_rate * 1000;
576                 c->rc_buffer_size = rd->ffcodecdata.rc_buffer_size * 1024;
577         }
578
579         if (context->ffmpeg_preset) {
580                 char const *preset_name;
581                 switch (context->ffmpeg_preset) {
582                         case FFM_PRESET_ULTRAFAST: preset_name = "ultrafast"; break;
583                         case FFM_PRESET_SUPERFAST: preset_name = "superfast"; break;
584                         case FFM_PRESET_VERYFAST: preset_name = "veryfast"; break;
585                         case FFM_PRESET_FASTER: preset_name = "faster"; break;
586                         case FFM_PRESET_FAST: preset_name = "fast"; break;
587                         case FFM_PRESET_MEDIUM: preset_name = "medium"; break;
588                         case FFM_PRESET_SLOW: preset_name = "slow"; break;
589                         case FFM_PRESET_SLOWER: preset_name = "slower"; break;
590                         case FFM_PRESET_VERYSLOW: preset_name = "veryslow"; break;
591                         default:
592                                 printf("Unknown preset number %i, ignoring.\n", context->ffmpeg_preset);
593                                 preset_name = NULL;
594                 }
595                 if (preset_name != NULL) {
596                         av_dict_set(&opts, "preset", preset_name, 0);
597                 }
598         }
599
600 #if 0
601         /* this options are not set in ffmpeg.c and leads to artifacts with MPEG-4
602          * see #33586: Encoding to mpeg4 makes first frame(s) blocky
603          */
604         c->rc_initial_buffer_occupancy = rd->ffcodecdata.rc_buffer_size * 3 / 4;
605         c->rc_buffer_aggressivity = 1.0;
606 #endif
607
608         c->me_method = ME_EPZS;
609         
610         codec = avcodec_find_encoder(c->codec_id);
611         if (!codec)
612                 return NULL;
613         
614         /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
615
616         if (codec->pix_fmts) {
617                 c->pix_fmt = codec->pix_fmts[0];
618         }
619         else {
620                 /* makes HuffYUV happy ... */
621                 c->pix_fmt = AV_PIX_FMT_YUV422P;
622         }
623
624         if (context->ffmpeg_type == FFMPEG_XVID) {
625                 /* arghhhh ... */
626                 c->pix_fmt = AV_PIX_FMT_YUV420P;
627                 c->codec_tag = (('D' << 24) + ('I' << 16) + ('V' << 8) + 'X');
628         }
629
630         if (codec_id == AV_CODEC_ID_H264) {
631                 /* correct wrong default ffmpeg param which crash x264 */
632                 c->qmin = 10;
633                 c->qmax = 51;
634         }
635         
636         /* Keep lossless encodes in the RGB domain. */
637         if (codec_id == AV_CODEC_ID_HUFFYUV) {
638                 if (rd->im_format.planes == R_IMF_PLANES_RGBA) {
639                         c->pix_fmt = AV_PIX_FMT_BGRA;
640                 }
641                 else {
642                         c->pix_fmt = AV_PIX_FMT_RGB32;
643                 }
644         }
645
646         if (codec_id == AV_CODEC_ID_FFV1) {
647                 c->pix_fmt = AV_PIX_FMT_RGB32;
648         }
649
650         if (codec_id == AV_CODEC_ID_QTRLE) {
651                 if (rd->im_format.planes == R_IMF_PLANES_RGBA) {
652                         c->pix_fmt = AV_PIX_FMT_ARGB;
653                 }
654         }
655
656         if (codec_id == AV_CODEC_ID_PNG) {
657                 if (rd->im_format.planes == R_IMF_PLANES_RGBA) {
658                         c->pix_fmt = AV_PIX_FMT_RGBA;
659                 }
660         }
661
662         if ((of->oformat->flags & AVFMT_GLOBALHEADER)
663 #if 0
664             || STREQ(of->oformat->name, "mp4")
665             || STREQ(of->oformat->name, "mov")
666             || STREQ(of->oformat->name, "3gp")
667 #endif
668             )
669         {
670                 PRINT("Using global header\n");
671                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
672         }
673         
674         /* Determine whether we are encoding interlaced material or not */
675         if (rd->mode & R_FIELDS) {
676                 PRINT("Encoding interlaced video\n");
677                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
678                 c->flags |= CODEC_FLAG_INTERLACED_ME;
679         }
680
681         /* xasp & yasp got float lately... */
682
683         st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(((double) rd->xasp / (double) rd->yasp), 255);
684         st->avg_frame_rate = av_inv_q(c->time_base);
685
686         set_ffmpeg_properties(rd, c, "video", &opts);
687
688         if (avcodec_open2(c, codec, &opts) < 0) {
689                 BLI_strncpy(error, IMB_ffmpeg_last_error(), error_size);
690                 av_dict_free(&opts);
691                 return NULL;
692         }
693         av_dict_free(&opts);
694
695         context->current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
696
697         context->img_convert_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_BGR32, c->width, c->height, c->pix_fmt, SWS_BICUBIC,
698                                                   NULL, NULL, NULL);
699         return st;
700 }
701
702 static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int codec_id, AVFormatContext *of, char *error, int error_size)
703 {
704         AVStream *st;
705         AVCodecContext *c;
706         AVCodec *codec;
707         AVDictionary *opts = NULL;
708
709         error[0] = '\0';
710
711         st = avformat_new_stream(of, NULL);
712         if (!st) return NULL;
713         st->id = 1;
714
715         c = st->codec;
716         c->codec_id = codec_id;
717         c->codec_type = AVMEDIA_TYPE_AUDIO;
718
719         c->sample_rate = rd->ffcodecdata.audio_mixrate;
720         c->bit_rate = context->ffmpeg_audio_bitrate * 1000;
721         c->sample_fmt = AV_SAMPLE_FMT_S16;
722         c->channels = rd->ffcodecdata.audio_channels;
723
724         if (request_float_audio_buffer(codec_id)) {
725                 /* mainly for AAC codec which is experimental */
726                 c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
727                 c->sample_fmt = AV_SAMPLE_FMT_FLT;
728         }
729
730         codec = avcodec_find_encoder(c->codec_id);
731         if (!codec) {
732                 //XXX error("Couldn't find a valid audio codec");
733                 return NULL;
734         }
735
736         if (codec->sample_fmts) {
737                 /* check if the preferred sample format for this codec is supported.
738                  * this is because, depending on the version of libav, and with the whole ffmpeg/libav fork situation,
739                  * you have various implementations around. float samples in particular are not always supported.
740                  */
741                 const enum AVSampleFormat *p = codec->sample_fmts;
742                 for (; *p != -1; p++) {
743                         if (*p == st->codec->sample_fmt)
744                                 break;
745                 }
746                 if (*p == -1) {
747                         /* sample format incompatible with codec. Defaulting to a format known to work */
748                         st->codec->sample_fmt = codec->sample_fmts[0];
749                 }
750         }
751
752         if (codec->supported_samplerates) {
753                 const int *p = codec->supported_samplerates;
754                 int best = 0;
755                 int best_dist = INT_MAX;
756                 for (; *p; p++) {
757                         int dist = abs(st->codec->sample_rate - *p);
758                         if (dist < best_dist) {
759                                 best_dist = dist;
760                                 best = *p;
761                         }
762                 }
763                 /* best is the closest supported sample rate (same as selected if best_dist == 0) */
764                 st->codec->sample_rate = best;
765         }
766
767         if (of->oformat->flags & AVFMT_GLOBALHEADER) {
768                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
769         }
770
771         set_ffmpeg_properties(rd, c, "audio", &opts);
772
773         if (avcodec_open2(c, codec, &opts) < 0) {
774                 //XXX error("Couldn't initialize audio codec");
775                 BLI_strncpy(error, IMB_ffmpeg_last_error(), error_size);
776                 av_dict_free(&opts);
777                 return NULL;
778         }
779         av_dict_free(&opts);
780
781         /* need to prevent floating point exception when using vorbis audio codec,
782          * initialize this value in the same way as it's done in FFmpeg itself (sergey) */
783         st->codec->time_base.num = 1;
784         st->codec->time_base.den = st->codec->sample_rate;
785
786 #ifndef FFMPEG_HAVE_ENCODE_AUDIO2
787         context->audio_outbuf_size = FF_MIN_BUFFER_SIZE;
788 #endif
789
790         if (c->frame_size == 0)
791                 // used to be if ((c->codec_id >= CODEC_ID_PCM_S16LE) && (c->codec_id <= CODEC_ID_PCM_DVD))
792                 // not sure if that is needed anymore, so let's try out if there are any
793                 // complaints regarding some ffmpeg versions users might have
794                 context->audio_input_samples = FF_MIN_BUFFER_SIZE * 8 / c->bits_per_coded_sample / c->channels;
795         else {
796                 context->audio_input_samples = c->frame_size;
797 #ifndef FFMPEG_HAVE_ENCODE_AUDIO2
798                 if (c->frame_size * c->channels * sizeof(int16_t) * 4 > context->audio_outbuf_size)
799                         context->audio_outbuf_size = c->frame_size * c->channels * sizeof(int16_t) * 4;
800 #endif
801         }
802
803         context->audio_deinterleave = av_sample_fmt_is_planar(c->sample_fmt);
804
805         context->audio_sample_size = av_get_bytes_per_sample(c->sample_fmt);
806
807         context->audio_input_buffer = (uint8_t *) av_malloc(context->audio_input_samples * c->channels * context->audio_sample_size);
808 #ifndef FFMPEG_HAVE_ENCODE_AUDIO2
809         context->audio_output_buffer = (uint8_t *) av_malloc(context->audio_outbuf_size);
810 #endif
811
812         if (context->audio_deinterleave)
813                 context->audio_deinterleave_buffer = (uint8_t *) av_malloc(context->audio_input_samples * c->channels * context->audio_sample_size);
814
815         context->audio_time = 0.0f;
816
817         return st;
818 }
819 /* essential functions -- start, append, end */
820
821 static void ffmpeg_dict_set_int(AVDictionary **dict, const char *key, int value)
822 {
823         char buffer[32];
824
825         BLI_snprintf(buffer, sizeof(buffer), "%d", value);
826
827         av_dict_set(dict, key, buffer, 0);
828 }
829
830 static void ffmpeg_dict_set_float(AVDictionary **dict, const char *key, float value)
831 {
832         char buffer[32];
833
834         BLI_snprintf(buffer, sizeof(buffer), "%.8f", value);
835
836         av_dict_set(dict, key, buffer, 0);
837 }
838
839 static int start_ffmpeg_impl(FFMpegContext *context, struct RenderData *rd, int rectx, int recty, const char *suffix, ReportList *reports)
840 {
841         /* Handle to the output file */
842         AVFormatContext *of;
843         AVOutputFormat *fmt;
844         AVDictionary *opts = NULL;
845         char name[FILE_MAX], error[1024];
846         const char **exts;
847
848         context->ffmpeg_type = rd->ffcodecdata.type;
849         context->ffmpeg_codec = rd->ffcodecdata.codec;
850         context->ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
851         context->ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
852         context->ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
853         context->ffmpeg_gop_size = rd->ffcodecdata.gop_size;
854         context->ffmpeg_autosplit = rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT;
855         context->ffmpeg_crf = rd->ffcodecdata.constant_rate_factor;
856         context->ffmpeg_preset = rd->ffcodecdata.ffmpeg_preset;
857
858         if ((rd->ffcodecdata.flags & FFMPEG_USE_MAX_B_FRAMES) != 0) {
859                 context->ffmpeg_max_b_frames = rd->ffcodecdata.max_b_frames;
860         }
861
862         /* Determine the correct filename */
863         ffmpeg_filepath_get(context, name, rd, context->ffmpeg_preview, suffix);
864         PRINT("Starting output to %s(ffmpeg)...\n"
865                 "  Using type=%d, codec=%d, audio_codec=%d,\n"
866                 "  video_bitrate=%d, audio_bitrate=%d,\n"
867                 "  gop_size=%d, autosplit=%d\n"
868                 "  render width=%d, render height=%d\n",
869                 name, context->ffmpeg_type, context->ffmpeg_codec, context->ffmpeg_audio_codec,
870                 context->ffmpeg_video_bitrate, context->ffmpeg_audio_bitrate,
871                 context->ffmpeg_gop_size, context->ffmpeg_autosplit, rectx, recty);
872         
873         exts = get_file_extensions(context->ffmpeg_type);
874         if (!exts) {
875                 BKE_report(reports, RPT_ERROR, "No valid formats found");
876                 return 0;
877         }
878         fmt = av_guess_format(NULL, exts[0], NULL);
879         if (!fmt) {
880                 BKE_report(reports, RPT_ERROR, "No valid formats found");
881                 return 0;
882         }
883
884         of = avformat_alloc_context();
885         if (!of) {
886                 BKE_report(reports, RPT_ERROR, "Error opening output file");
887                 return 0;
888         }
889
890
891         /* Returns after this must 'goto fail;' */
892
893         of->oformat = fmt;
894
895         /* Only bother with setting packet size & mux rate when CRF is not used. */
896         if (context->ffmpeg_crf == 0) {
897                 of->packet_size = rd->ffcodecdata.mux_packet_size;
898                 if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE) {
899                         ffmpeg_dict_set_int(&opts, "muxrate", rd->ffcodecdata.mux_rate);
900                 }
901                 else {
902                         av_dict_set(&opts, "muxrate", "0", 0);
903                 }
904         }
905
906         ffmpeg_dict_set_int(&opts, "preload", (int)(0.5 * AV_TIME_BASE));
907
908         of->max_delay = (int)(0.7 * AV_TIME_BASE);
909
910         fmt->audio_codec = context->ffmpeg_audio_codec;
911
912         BLI_strncpy(of->filename, name, sizeof(of->filename));
913         /* set the codec to the user's selection */
914         switch (context->ffmpeg_type) {
915                 case FFMPEG_AVI:
916                 case FFMPEG_MOV:
917                 case FFMPEG_MKV:
918                         fmt->video_codec = context->ffmpeg_codec;
919                         break;
920                 case FFMPEG_OGG:
921                         fmt->video_codec = AV_CODEC_ID_THEORA;
922                         break;
923                 case FFMPEG_DV:
924                         fmt->video_codec = AV_CODEC_ID_DVVIDEO;
925                         break;
926                 case FFMPEG_MPEG1:
927                         fmt->video_codec = AV_CODEC_ID_MPEG1VIDEO;
928                         break;
929                 case FFMPEG_MPEG2:
930                         fmt->video_codec = AV_CODEC_ID_MPEG2VIDEO;
931                         break;
932                 case FFMPEG_H264:
933                         fmt->video_codec = AV_CODEC_ID_H264;
934                         break;
935                 case FFMPEG_XVID:
936                         fmt->video_codec = AV_CODEC_ID_MPEG4;
937                         break;
938                 case FFMPEG_FLV:
939                         fmt->video_codec = AV_CODEC_ID_FLV1;
940                         break;
941                 case FFMPEG_MPEG4:
942                 default:
943                         fmt->video_codec = context->ffmpeg_codec;
944                         break;
945         }
946         if (fmt->video_codec == AV_CODEC_ID_DVVIDEO) {
947                 if (rectx != 720) {
948                         BKE_report(reports, RPT_ERROR, "Render width has to be 720 pixels for DV!");
949                         goto fail;
950                 }
951                 if (rd->frs_sec != 25 && recty != 480) {
952                         BKE_report(reports, RPT_ERROR, "Render height has to be 480 pixels for DV-NTSC!");
953                         goto fail;
954                 }
955                 if (rd->frs_sec == 25 && recty != 576) {
956                         BKE_report(reports, RPT_ERROR, "Render height has to be 576 pixels for DV-PAL!");
957                         goto fail;
958                 }
959         }
960         
961         if (context->ffmpeg_type == FFMPEG_DV) {
962                 fmt->audio_codec = AV_CODEC_ID_PCM_S16LE;
963                 if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE && rd->ffcodecdata.audio_mixrate != 48000 && rd->ffcodecdata.audio_channels != 2) {
964                         BKE_report(reports, RPT_ERROR, "FFMPEG only supports 48khz / stereo audio for DV!");
965                         goto fail;
966                 }
967         }
968         
969         if (fmt->video_codec != AV_CODEC_ID_NONE) {
970                 context->video_stream = alloc_video_stream(context, rd, fmt->video_codec, of, rectx, recty, error, sizeof(error));
971                 PRINT("alloc video stream %p\n", context->video_stream);
972                 if (!context->video_stream) {
973                         if (error[0])
974                                 BKE_report(reports, RPT_ERROR, error);
975                         else
976                                 BKE_report(reports, RPT_ERROR, "Error initializing video stream");
977                         goto fail;
978                 }
979         }
980
981         if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE) {
982                 context->audio_stream = alloc_audio_stream(context, rd, fmt->audio_codec, of, error, sizeof(error));
983                 if (!context->audio_stream) {
984                         if (error[0])
985                                 BKE_report(reports, RPT_ERROR, error);
986                         else
987                                 BKE_report(reports, RPT_ERROR, "Error initializing audio stream");
988                         goto fail;
989                 }
990         }
991         if (!(fmt->flags & AVFMT_NOFILE)) {
992                 if (avio_open(&of->pb, name, AVIO_FLAG_WRITE) < 0) {
993                         BKE_report(reports, RPT_ERROR, "Could not open file for writing");
994                         goto fail;
995                 }
996         }
997         if (avformat_write_header(of, NULL) < 0) {
998                 BKE_report(reports, RPT_ERROR, "Could not initialize streams, probably unsupported codec combination");
999                 goto fail;
1000         }
1001
1002         context->outfile = of;
1003         av_dump_format(of, 0, name, 1);
1004         av_dict_free(&opts);
1005
1006         return 1;
1007
1008
1009 fail:
1010         if (of->pb) {
1011                 avio_close(of->pb);
1012         }
1013
1014         if (context->video_stream && context->video_stream->codec) {
1015                 avcodec_close(context->video_stream->codec);
1016                 context->video_stream = NULL;
1017         }
1018
1019         if (context->audio_stream && context->audio_stream->codec) {
1020                 avcodec_close(context->audio_stream->codec);
1021                 context->audio_stream = NULL;
1022         }
1023
1024         av_dict_free(&opts);
1025         avformat_free_context(of);
1026         return 0;
1027 }
1028
1029 /**
1030  * Writes any delayed frames in the encoder. This function is called before 
1031  * closing the encoder.
1032  *
1033  * <p>
1034  * Since an encoder may use both past and future frames to predict 
1035  * inter-frames (H.264 B-frames, for example), it can output the frames 
1036  * in a different order from the one it was given.
1037  * For example, when sending frames 1, 2, 3, 4 to the encoder, it may write
1038  * them in the order 1, 4, 2, 3 - first the two frames used for prediction,
1039  * and then the bidirectionally-predicted frames. What this means in practice 
1040  * is that the encoder may not immediately produce one output frame for each 
1041  * input frame. These delayed frames must be flushed before we close the 
1042  * stream. We do this by calling avcodec_encode_video with NULL for the last 
1043  * parameter.
1044  * </p>
1045  */
1046 static void flush_ffmpeg(FFMpegContext *context)
1047 {
1048         int ret = 0;
1049         
1050         AVCodecContext *c = context->video_stream->codec;
1051         /* get the delayed frames */
1052         while (1) {
1053                 int got_output;
1054                 AVPacket packet = { 0 };
1055                 av_init_packet(&packet);
1056                 
1057                 ret = avcodec_encode_video2(c, &packet, NULL, &got_output);
1058                 if (ret < 0) {
1059                         fprintf(stderr, "Error encoding delayed frame %d\n", ret);
1060                         break;
1061                 }
1062                 if (!got_output) {
1063                         break;
1064                 }
1065                 if (packet.pts != AV_NOPTS_VALUE) {
1066                         packet.pts = av_rescale_q(packet.pts, c->time_base, context->video_stream->time_base);
1067                         PRINT("Video Frame PTS: %d\n", (int) packet.pts);
1068                 }
1069                 else {
1070                         PRINT("Video Frame PTS: not set\n");
1071                 }
1072                 if (packet.dts != AV_NOPTS_VALUE) {
1073                         packet.dts = av_rescale_q(packet.dts, c->time_base, context->video_stream->time_base);
1074                         PRINT("Video Frame DTS: %d\n", (int) packet.dts);
1075                 }
1076                 else {
1077                         PRINT("Video Frame DTS: not set\n");
1078                 }
1079
1080                 packet.stream_index = context->video_stream->index;
1081                 ret = av_interleaved_write_frame(context->outfile, &packet);
1082                 if (ret != 0) {
1083                         fprintf(stderr, "Error writing delayed frame %d\n", ret);
1084                         break;
1085                 }
1086         }
1087         avcodec_flush_buffers(context->video_stream->codec);
1088 }
1089
1090 /* **********************************************************************
1091  * * public interface
1092  * ********************************************************************** */
1093
1094 /* Get the output filename-- similar to the other output formats */
1095 static void ffmpeg_filepath_get(FFMpegContext *context, char *string, RenderData *rd, bool preview, const char *suffix)
1096 {
1097         char autosplit[20];
1098
1099         const char **exts = get_file_extensions(rd->ffcodecdata.type);
1100         const char **fe = exts;
1101         int sfra, efra;
1102
1103         if (!string || !exts) return;
1104
1105         if (preview) {
1106                 sfra = rd->psfra;
1107                 efra = rd->pefra;
1108         }
1109         else {
1110                 sfra = rd->sfra;
1111                 efra = rd->efra;
1112         }
1113
1114         strcpy(string, rd->pic);
1115         BLI_path_abs(string, G.main->name);
1116
1117         BLI_make_existing_file(string);
1118
1119         autosplit[0] = '\0';
1120
1121         if ((rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
1122                 if (context) {
1123                         sprintf(autosplit, "_%03d", context->ffmpeg_autosplit_count);
1124                 }
1125         }
1126
1127         if (rd->scemode & R_EXTENSION) {
1128                 while (*fe) {
1129                         if (BLI_strcasecmp(string + strlen(string) - strlen(*fe), *fe) == 0) {
1130                                 break;
1131                         }
1132                         fe++;
1133                 }
1134
1135                 if (*fe == NULL) {
1136                         strcat(string, autosplit);
1137
1138                         BLI_path_frame_range(string, sfra, efra, 4);
1139                         strcat(string, *exts);
1140                 }
1141                 else {
1142                         *(string + strlen(string) - strlen(*fe)) = '\0';
1143                         strcat(string, autosplit);
1144                         strcat(string, *fe);
1145                 }
1146         }
1147         else {
1148                 if (BLI_path_frame_check_chars(string)) {
1149                         BLI_path_frame_range(string, sfra, efra, 4);
1150                 }
1151
1152                 strcat(string, autosplit);
1153         }
1154
1155         BLI_path_suffix(string, FILE_MAX, suffix, "");
1156 }
1157
1158 void BKE_ffmpeg_filepath_get(char *string, RenderData *rd, bool preview, const char *suffix)
1159 {
1160         ffmpeg_filepath_get(NULL, string, rd, preview, suffix);
1161 }
1162
1163 int BKE_ffmpeg_start(void *context_v, struct Scene *scene, RenderData *rd, int rectx, int recty,
1164                      ReportList *reports, bool preview, const char *suffix)
1165 {
1166         int success;
1167         FFMpegContext *context = context_v;
1168
1169         context->ffmpeg_autosplit_count = 0;
1170         context->ffmpeg_preview = preview;
1171
1172         success = start_ffmpeg_impl(context, rd, rectx, recty, suffix, reports);
1173 #ifdef WITH_AUDASPACE
1174         if (context->audio_stream) {
1175                 AVCodecContext *c = context->audio_stream->codec;
1176                 AUD_DeviceSpecs specs;
1177                 specs.channels = c->channels;
1178
1179                 switch (av_get_packed_sample_fmt(c->sample_fmt)) {
1180                         case AV_SAMPLE_FMT_U8:
1181                                 specs.format = AUD_FORMAT_U8;
1182                                 break;
1183                         case AV_SAMPLE_FMT_S16:
1184                                 specs.format = AUD_FORMAT_S16;
1185                                 break;
1186                         case AV_SAMPLE_FMT_S32:
1187                                 specs.format = AUD_FORMAT_S32;
1188                                 break;
1189                         case AV_SAMPLE_FMT_FLT:
1190                                 specs.format = AUD_FORMAT_FLOAT32;
1191                                 break;
1192                         case AV_SAMPLE_FMT_DBL:
1193                                 specs.format = AUD_FORMAT_FLOAT64;
1194                                 break;
1195                         default:
1196                                 return -31415;
1197                 }
1198
1199                 specs.rate = rd->ffcodecdata.audio_mixrate;
1200                 context->audio_mixdown_device = BKE_sound_mixdown(scene, specs, preview ? rd->psfra : rd->sfra, rd->ffcodecdata.audio_volume);
1201 #ifdef FFMPEG_CODEC_TIME_BASE
1202                 c->time_base.den = specs.rate;
1203                 c->time_base.num = 1;
1204 #endif
1205         }
1206 #endif
1207         return success;
1208 }
1209
1210 static void end_ffmpeg_impl(FFMpegContext *context, int is_autosplit);
1211
1212 #ifdef WITH_AUDASPACE
1213 static void write_audio_frames(FFMpegContext *context, double to_pts)
1214 {
1215         int finished = 0;
1216
1217         while (context->audio_stream && !finished) {
1218                 if ((context->audio_time >= to_pts) ||
1219                     (write_audio_frame(context)))
1220                 {
1221                         finished = 1;
1222                 }
1223         }
1224 }
1225 #endif
1226
1227 int BKE_ffmpeg_append(void *context_v, RenderData *rd, int start_frame, int frame, int *pixels,
1228                       int rectx, int recty, const char *suffix, ReportList *reports)
1229 {
1230         FFMpegContext *context = context_v;
1231         AVFrame *avframe;
1232         int success = 1;
1233
1234         PRINT("Writing frame %i, render width=%d, render height=%d\n", frame, rectx, recty);
1235
1236 /* why is this done before writing the video frame and again at end_ffmpeg? */
1237 //      write_audio_frames(frame / (((double)rd->frs_sec) / rd->frs_sec_base));
1238
1239         if (context->video_stream) {
1240                 avframe = generate_video_frame(context, (unsigned char *) pixels, reports);
1241                 success = (avframe && write_video_frame(context, rd, frame - start_frame, avframe, reports));
1242
1243                 if (context->ffmpeg_autosplit) {
1244                         if (avio_tell(context->outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
1245                                 end_ffmpeg_impl(context, true);
1246                                 context->ffmpeg_autosplit_count++;
1247                                 success &= start_ffmpeg_impl(context, rd, rectx, recty, suffix, reports);
1248                         }
1249                 }
1250         }
1251
1252 #ifdef WITH_AUDASPACE
1253         write_audio_frames(context, (frame - start_frame) / (((double)rd->frs_sec) / (double)rd->frs_sec_base));
1254 #endif
1255         return success;
1256 }
1257
1258 static void end_ffmpeg_impl(FFMpegContext *context, int is_autosplit)
1259 {
1260         PRINT("Closing ffmpeg...\n");
1261
1262 #if 0
1263         if (context->audio_stream) { /* SEE UPPER */
1264                 write_audio_frames(context);
1265         }
1266 #endif
1267
1268 #ifdef WITH_AUDASPACE
1269         if (is_autosplit == false) {
1270                 if (context->audio_mixdown_device) {
1271                         AUD_Device_free(context->audio_mixdown_device);
1272                         context->audio_mixdown_device = NULL;
1273                 }
1274         }
1275 #endif
1276
1277         if (context->video_stream && context->video_stream->codec) {
1278                 PRINT("Flushing delayed frames...\n");
1279                 flush_ffmpeg(context);
1280         }
1281         
1282         if (context->outfile) {
1283                 av_write_trailer(context->outfile);
1284         }
1285         
1286         /* Close the video codec */
1287
1288         if (context->video_stream != NULL && context->video_stream->codec != NULL) {
1289                 avcodec_close(context->video_stream->codec);
1290                 PRINT("zero video stream %p\n", context->video_stream);
1291                 context->video_stream = NULL;
1292         }
1293
1294         if (context->audio_stream != NULL && context->audio_stream->codec != NULL) {
1295                 avcodec_close(context->audio_stream->codec);
1296                 context->audio_stream = NULL;
1297         }
1298
1299         /* free the temp buffer */
1300         if (context->current_frame != NULL) {
1301                 delete_picture(context->current_frame);
1302                 context->current_frame = NULL;
1303         }
1304         if (context->outfile != NULL && context->outfile->oformat) {
1305                 if (!(context->outfile->oformat->flags & AVFMT_NOFILE)) {
1306                         avio_close(context->outfile->pb);
1307                 }
1308         }
1309         if (context->outfile != NULL) {
1310                 avformat_free_context(context->outfile);
1311                 context->outfile = NULL;
1312         }
1313         if (context->audio_input_buffer != NULL) {
1314                 av_free(context->audio_input_buffer);
1315                 context->audio_input_buffer = NULL;
1316         }
1317 #ifndef FFMPEG_HAVE_ENCODE_AUDIO2
1318         if (context->audio_output_buffer != NULL) {
1319                 av_free(context->audio_output_buffer);
1320                 context->audio_output_buffer = NULL;
1321         }
1322 #endif
1323
1324         if (context->audio_deinterleave_buffer != NULL) {
1325                 av_free(context->audio_deinterleave_buffer);
1326                 context->audio_deinterleave_buffer = NULL;
1327         }
1328
1329         if (context->img_convert_ctx != NULL) {
1330                 sws_freeContext(context->img_convert_ctx);
1331                 context->img_convert_ctx = NULL;
1332         }
1333 }
1334
1335 void BKE_ffmpeg_end(void *context_v)
1336 {
1337         FFMpegContext *context = context_v;
1338         end_ffmpeg_impl(context, false);
1339 }
1340
1341 /* properties */
1342
1343 void BKE_ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
1344 {
1345         struct IDProperty *prop = (struct IDProperty *) prop_;
1346         IDProperty *group;
1347         
1348         if (!rd->ffcodecdata.properties) {
1349                 return;
1350         }
1351
1352         group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
1353         if (group && prop) {
1354                 IDP_FreeFromGroup(group, prop);
1355         }
1356 }
1357
1358 static IDProperty *BKE_ffmpeg_property_add(RenderData *rd, const char *type, const AVOption *o, const AVOption *parent)
1359 {
1360         AVCodecContext c;
1361         IDProperty *group;
1362         IDProperty *prop;
1363         IDPropertyTemplate val;
1364         int idp_type;
1365         char name[256];
1366         
1367         val.i = 0;
1368
1369         avcodec_get_context_defaults3(&c, NULL);
1370
1371         if (!rd->ffcodecdata.properties) {
1372                 rd->ffcodecdata.properties = IDP_New(IDP_GROUP, &val, "ffmpeg"); 
1373         }
1374
1375         group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
1376         
1377         if (!group) {
1378                 group = IDP_New(IDP_GROUP, &val, type);
1379                 IDP_AddToGroup(rd->ffcodecdata.properties, group);
1380         }
1381
1382         if (parent) {
1383                 BLI_snprintf(name, sizeof(name), "%s:%s", parent->name, o->name);
1384         }
1385         else {
1386                 BLI_strncpy(name, o->name, sizeof(name));
1387         }
1388
1389         PRINT("ffmpeg_property_add: %s %s\n", type, name);
1390
1391         prop = IDP_GetPropertyFromGroup(group, name);
1392         if (prop) {
1393                 return prop;
1394         }
1395
1396         switch (o->type) {
1397                 case AV_OPT_TYPE_INT:
1398                 case AV_OPT_TYPE_INT64:
1399                         val.i = FFMPEG_DEF_OPT_VAL_INT(o);
1400                         idp_type = IDP_INT;
1401                         break;
1402                 case AV_OPT_TYPE_DOUBLE:
1403                 case AV_OPT_TYPE_FLOAT:
1404                         val.f = FFMPEG_DEF_OPT_VAL_DOUBLE(o);
1405                         idp_type = IDP_FLOAT;
1406                         break;
1407                 case AV_OPT_TYPE_STRING:
1408                         val.string.str = (char *)"                                                                               ";
1409                         val.string.len = 80;
1410 /*              val.str = (char *)"                                                                               ";*/
1411                         idp_type = IDP_STRING;
1412                         break;
1413                 case AV_OPT_TYPE_CONST:
1414                         val.i = 1;
1415                         idp_type = IDP_INT;
1416                         break;
1417                 default:
1418                         return NULL;
1419         }
1420         prop = IDP_New(idp_type, &val, name);
1421         IDP_AddToGroup(group, prop);
1422         return prop;
1423 }
1424
1425 /* not all versions of ffmpeg include that, so here we go ... */
1426
1427 int BKE_ffmpeg_property_add_string(RenderData *rd, const char *type, const char *str)
1428 {
1429         AVCodecContext c;
1430         const AVOption *o = NULL;
1431         const AVOption *p = NULL;
1432         char name_[128];
1433         char *name;
1434         char *param;
1435         IDProperty *prop = NULL;
1436         
1437         avcodec_get_context_defaults3(&c, NULL);
1438
1439         BLI_strncpy(name_, str, sizeof(name_));
1440
1441         name = name_;
1442         while (*name == ' ') name++;
1443
1444         param = strchr(name, ':');
1445
1446         if (!param) {
1447                 param = strchr(name, ' ');
1448         }
1449         if (param) {
1450                 *param++ = '\0';
1451                 while (*param == ' ') param++;
1452         }
1453         
1454         o = av_opt_find(&c, name, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1455         if (!o) {
1456                 PRINT("Ignoring unknown expert option %s\n", str);
1457                 return 0;
1458         }
1459         if (param && o->type == AV_OPT_TYPE_CONST) {
1460                 return 0;
1461         }
1462         if (param && o->type != AV_OPT_TYPE_CONST && o->unit) {
1463                 p = av_opt_find(&c, param, o->unit, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1464                 if (p) {
1465                         prop = BKE_ffmpeg_property_add(rd, (char *) type, p, o);
1466                 }
1467                 else {
1468                         PRINT("Ignoring unknown expert option %s\n", str);
1469                 }
1470         }
1471         else {
1472                 prop = BKE_ffmpeg_property_add(rd, (char *) type, o, NULL);
1473         }
1474                 
1475
1476         if (!prop) {
1477                 return 0;
1478         }
1479
1480         if (param && !p) {
1481                 switch (prop->type) {
1482                         case IDP_INT:
1483                                 IDP_Int(prop) = atoi(param);
1484                                 break;
1485                         case IDP_FLOAT:
1486                                 IDP_Float(prop) = atof(param);
1487                                 break;
1488                         case IDP_STRING:
1489                                 strncpy(IDP_String(prop), param, prop->len);
1490                                 break;
1491                 }
1492         }
1493         return 1;
1494 }
1495
1496 static void ffmpeg_set_expert_options(RenderData *rd)
1497 {
1498         int codec_id = rd->ffcodecdata.codec;
1499
1500         if (rd->ffcodecdata.properties)
1501                 IDP_FreeProperty(rd->ffcodecdata.properties);
1502
1503         if (codec_id == AV_CODEC_ID_H264) {
1504                 /*
1505                  * All options here are for x264, but must be set via ffmpeg.
1506                  * The names are therefore different - Search for "x264 to FFmpeg option mapping"
1507                  * to get a list.
1508                  */
1509
1510                 /*
1511                  * Use CABAC coder. Using "coder:1", which should be equivalent,
1512                  * crashes Blender for some reason. Either way - this is no big deal.
1513                  */
1514                 BKE_ffmpeg_property_add_string(rd, "video", "coder:vlc");
1515
1516                 /*
1517                  * The other options were taken from the libx264-default.preset
1518                  * included in the ffmpeg distribution.
1519                  */
1520 //              ffmpeg_property_add_string(rd, "video", "flags:loop"); // this breaks compatibility for QT
1521                 BKE_ffmpeg_property_add_string(rd, "video", "cmp:chroma");
1522                 BKE_ffmpeg_property_add_string(rd, "video", "partitions:parti4x4");  // Deprecated.
1523                 BKE_ffmpeg_property_add_string(rd, "video", "partitions:partp8x8");  // Deprecated.
1524                 BKE_ffmpeg_property_add_string(rd, "video", "partitions:partb8x8");  // Deprecated.
1525                 BKE_ffmpeg_property_add_string(rd, "video", "me:hex");
1526                 BKE_ffmpeg_property_add_string(rd, "video", "subq:6");
1527                 BKE_ffmpeg_property_add_string(rd, "video", "me_range:16");
1528                 BKE_ffmpeg_property_add_string(rd, "video", "qdiff:4");
1529                 BKE_ffmpeg_property_add_string(rd, "video", "keyint_min:25");
1530                 BKE_ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
1531                 BKE_ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
1532                 BKE_ffmpeg_property_add_string(rd, "video", "b_strategy:1");
1533                 BKE_ffmpeg_property_add_string(rd, "video", "bf:3");
1534                 BKE_ffmpeg_property_add_string(rd, "video", "refs:2");
1535                 BKE_ffmpeg_property_add_string(rd, "video", "qcomp:0.6");
1536
1537                 BKE_ffmpeg_property_add_string(rd, "video", "trellis:0");
1538                 BKE_ffmpeg_property_add_string(rd, "video", "weightb:1");
1539 #ifdef FFMPEG_HAVE_DEPRECATED_FLAGS2
1540                 BKE_ffmpeg_property_add_string(rd, "video", "flags2:dct8x8");
1541                 BKE_ffmpeg_property_add_string(rd, "video", "directpred:3");
1542                 BKE_ffmpeg_property_add_string(rd, "video", "flags2:fastpskip");
1543                 BKE_ffmpeg_property_add_string(rd, "video", "flags2:wpred");
1544 #else
1545                 BKE_ffmpeg_property_add_string(rd, "video", "8x8dct:1");
1546                 BKE_ffmpeg_property_add_string(rd, "video", "fast-pskip:1");
1547                 BKE_ffmpeg_property_add_string(rd, "video", "wpredp:2");
1548 #endif
1549         }
1550         else if (codec_id == AV_CODEC_ID_DNXHD) {
1551                 if (rd->ffcodecdata.flags & FFMPEG_LOSSLESS_OUTPUT)
1552                         BKE_ffmpeg_property_add_string(rd, "video", "mbd:rd");
1553         }
1554 }
1555
1556 void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
1557 {
1558         int isntsc = (rd->frs_sec != 25);
1559
1560         if (rd->ffcodecdata.properties)
1561                 IDP_FreeProperty(rd->ffcodecdata.properties);
1562
1563         switch (preset) {
1564                 case FFMPEG_PRESET_VCD:
1565                         rd->ffcodecdata.type = FFMPEG_MPEG1;
1566                         rd->ffcodecdata.video_bitrate = 1150;
1567                         rd->xsch = 352;
1568                         rd->ysch = isntsc ? 240 : 288;
1569                         rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1570                         rd->ffcodecdata.rc_max_rate = 1150;
1571                         rd->ffcodecdata.rc_min_rate = 1150;
1572                         rd->ffcodecdata.rc_buffer_size = 40 * 8;
1573                         rd->ffcodecdata.mux_packet_size = 2324;
1574                         rd->ffcodecdata.mux_rate = 2352 * 75 * 8;
1575                         break;
1576
1577                 case FFMPEG_PRESET_SVCD:
1578                         rd->ffcodecdata.type = FFMPEG_MPEG2;
1579                         rd->ffcodecdata.video_bitrate = 2040;
1580                         rd->xsch = 480;
1581                         rd->ysch = isntsc ? 480 : 576;
1582                         rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1583                         rd->ffcodecdata.rc_max_rate = 2516;
1584                         rd->ffcodecdata.rc_min_rate = 0;
1585                         rd->ffcodecdata.rc_buffer_size = 224 * 8;
1586                         rd->ffcodecdata.mux_packet_size = 2324;
1587                         rd->ffcodecdata.mux_rate = 0;
1588                         break;
1589
1590                 case FFMPEG_PRESET_DVD:
1591                         rd->ffcodecdata.type = FFMPEG_MPEG2;
1592                         rd->ffcodecdata.video_bitrate = 6000;
1593
1594                         /* Don't set resolution, see [#21351]
1595                          * rd->xsch = 720;
1596                          * rd->ysch = isntsc ? 480 : 576; */
1597
1598                         rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1599                         rd->ffcodecdata.rc_max_rate = 9000;
1600                         rd->ffcodecdata.rc_min_rate = 0;
1601                         rd->ffcodecdata.rc_buffer_size = 224 * 8;
1602                         rd->ffcodecdata.mux_packet_size = 2048;
1603                         rd->ffcodecdata.mux_rate = 10080000;
1604                         break;
1605
1606                 case FFMPEG_PRESET_DV:
1607                         rd->ffcodecdata.type = FFMPEG_DV;
1608                         rd->xsch = 720;
1609                         rd->ysch = isntsc ? 480 : 576;
1610                         break;
1611
1612                 case FFMPEG_PRESET_H264:
1613                         rd->ffcodecdata.type = FFMPEG_AVI;
1614                         rd->ffcodecdata.codec = AV_CODEC_ID_H264;
1615                         rd->ffcodecdata.video_bitrate = 6000;
1616                         rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1617                         rd->ffcodecdata.rc_max_rate = 9000;
1618                         rd->ffcodecdata.rc_min_rate = 0;
1619                         rd->ffcodecdata.rc_buffer_size = 224 * 8;
1620                         rd->ffcodecdata.mux_packet_size = 2048;
1621                         rd->ffcodecdata.mux_rate = 10080000;
1622
1623                         break;
1624
1625                 case FFMPEG_PRESET_THEORA:
1626                 case FFMPEG_PRESET_XVID:
1627                         if (preset == FFMPEG_PRESET_XVID) {
1628                                 rd->ffcodecdata.type = FFMPEG_AVI;
1629                                 rd->ffcodecdata.codec = AV_CODEC_ID_MPEG4;
1630                         }
1631                         else if (preset == FFMPEG_PRESET_THEORA) {
1632                                 rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
1633                                 rd->ffcodecdata.codec = AV_CODEC_ID_THEORA;
1634                         }
1635
1636                         rd->ffcodecdata.video_bitrate = 6000;
1637                         rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1638                         rd->ffcodecdata.rc_max_rate = 9000;
1639                         rd->ffcodecdata.rc_min_rate = 0;
1640                         rd->ffcodecdata.rc_buffer_size = 224 * 8;
1641                         rd->ffcodecdata.mux_packet_size = 2048;
1642                         rd->ffcodecdata.mux_rate = 10080000;
1643                         break;
1644
1645         }
1646
1647         ffmpeg_set_expert_options(rd);
1648 }
1649
1650 void BKE_ffmpeg_image_type_verify(RenderData *rd, ImageFormatData *imf)
1651 {
1652         int audio = 0;
1653
1654         if (imf->imtype == R_IMF_IMTYPE_FFMPEG) {
1655                 if (rd->ffcodecdata.type <= 0 ||
1656                     rd->ffcodecdata.codec <= 0 ||
1657                     rd->ffcodecdata.audio_codec <= 0 ||
1658                     rd->ffcodecdata.video_bitrate <= 1)
1659                 {
1660                         BKE_ffmpeg_preset_set(rd, FFMPEG_PRESET_H264);
1661                         rd->ffcodecdata.constant_rate_factor = FFM_CRF_MEDIUM;
1662                         rd->ffcodecdata.ffmpeg_preset = FFM_PRESET_MEDIUM;
1663                         rd->ffcodecdata.type = FFMPEG_MKV;
1664                 }
1665                 if (rd->ffcodecdata.type == FFMPEG_OGG) {
1666                         rd->ffcodecdata.type = FFMPEG_MPEG2;
1667                 }
1668
1669                 audio = 1;
1670         }
1671         else if (imf->imtype == R_IMF_IMTYPE_H264) {
1672                 if (rd->ffcodecdata.codec != AV_CODEC_ID_H264) {
1673                         BKE_ffmpeg_preset_set(rd, FFMPEG_PRESET_H264);
1674                         audio = 1;
1675                 }
1676         }
1677         else if (imf->imtype == R_IMF_IMTYPE_XVID) {
1678                 if (rd->ffcodecdata.codec != AV_CODEC_ID_MPEG4) {
1679                         BKE_ffmpeg_preset_set(rd, FFMPEG_PRESET_XVID);
1680                         audio = 1;
1681                 }
1682         }
1683         else if (imf->imtype == R_IMF_IMTYPE_THEORA) {
1684                 if (rd->ffcodecdata.codec != AV_CODEC_ID_THEORA) {
1685                         BKE_ffmpeg_preset_set(rd, FFMPEG_PRESET_THEORA);
1686                         audio = 1;
1687                 }
1688         }
1689
1690         if (audio && rd->ffcodecdata.audio_codec < 0) {
1691                 rd->ffcodecdata.audio_codec = AV_CODEC_ID_NONE;
1692                 rd->ffcodecdata.audio_bitrate = 128;
1693         }
1694 }
1695
1696 void BKE_ffmpeg_codec_settings_verify(RenderData *rd)
1697 {
1698         ffmpeg_set_expert_options(rd);
1699 }
1700
1701 bool BKE_ffmpeg_alpha_channel_is_supported(RenderData *rd)
1702 {
1703         int codec = rd->ffcodecdata.codec;
1704
1705         if (codec == AV_CODEC_ID_QTRLE)
1706                 return true;
1707
1708         if (codec == AV_CODEC_ID_PNG)
1709                 return true;
1710
1711         if (codec == AV_CODEC_ID_HUFFYUV)
1712                 return true;
1713
1714 #ifdef FFMPEG_FFV1_ALPHA_SUPPORTED
1715         if (codec == AV_CODEC_ID_FFV1)
1716                 return true;
1717 #endif
1718
1719         return false;
1720 }
1721
1722 void *BKE_ffmpeg_context_create(void)
1723 {
1724         FFMpegContext *context;
1725
1726         /* new ffmpeg data struct */
1727         context = MEM_callocN(sizeof(FFMpegContext), "new ffmpeg context");
1728
1729         context->ffmpeg_codec = AV_CODEC_ID_MPEG4;
1730         context->ffmpeg_audio_codec = AV_CODEC_ID_NONE;
1731         context->ffmpeg_video_bitrate = 1150;
1732         context->ffmpeg_audio_bitrate = 128;
1733         context->ffmpeg_gop_size = 12;
1734         context->ffmpeg_autosplit = 0;
1735         context->ffmpeg_autosplit_count = 0;
1736         context->ffmpeg_preview = false;
1737
1738         return context;
1739 }
1740
1741 void BKE_ffmpeg_context_free(void *context_v)
1742 {
1743         FFMpegContext *context = context_v;
1744         if (context) {
1745                 MEM_freeN(context);
1746         }
1747 }
1748
1749 #endif /* WITH_FFMPEG */