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