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