5 * Partial Copyright (c) 2006 Peter Schlaile
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 /** \file blender/blenkernel/intern/writeffmpeg.c
27 #if defined(_WIN32) && defined(DEBUG) && !defined(__MINGW32__) && !defined(__CYGWIN__)
28 /* This does not seem necessary or present on MSVC 8, but may be needed in earlier versions? */
36 #include <libavformat/avformat.h>
37 #include <libavcodec/avcodec.h>
38 #include <libavutil/rational.h>
39 #include <libswscale/swscale.h>
40 #include <libavcodec/opt.h>
42 #include "MEM_guardedalloc.h"
44 #include "DNA_scene_types.h"
46 #include "BLI_blenlib.h"
49 # include "AUD_C-API.h"
52 #include "BKE_global.h"
53 #include "BKE_idprop.h"
55 #include "BKE_report.h"
56 #include "BKE_sound.h"
57 #include "BKE_writeffmpeg.h"
59 #include "IMB_imbuf_types.h"
60 #include "IMB_imbuf.h"
62 #include "ffmpeg_compat.h"
64 extern void do_init_ffmpeg(void);
66 static int ffmpeg_type = 0;
67 static int ffmpeg_codec = CODEC_ID_MPEG4;
68 static int ffmpeg_audio_codec = CODEC_ID_NONE;
69 static int ffmpeg_video_bitrate = 1150;
70 static int ffmpeg_audio_bitrate = 128;
71 static int ffmpeg_gop_size = 12;
72 static int ffmpeg_autosplit = 0;
73 static int ffmpeg_autosplit_count = 0;
75 static AVFormatContext* outfile = 0;
76 static AVStream* video_stream = 0;
77 static AVStream* audio_stream = 0;
78 static AVFrame* current_frame = 0;
79 static struct SwsContext *img_convert_ctx = 0;
81 static uint8_t* video_buffer = 0;
82 static int video_buffersize = 0;
84 static uint8_t* audio_input_buffer = 0;
85 static int audio_input_samples = 0;
86 static uint8_t* audio_output_buffer = 0;
87 static int audio_outbuf_size = 0;
88 static double audio_time = 0.0f;
91 static AUD_Device* audio_mixdown_device = 0;
94 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
96 /* Delete a picture buffer */
98 static void delete_picture(AVFrame* f)
101 if (f->data[0]) MEM_freeN(f->data[0]);
106 #ifdef WITH_AUDASPACE
107 static int write_audio_frame(void)
109 AVCodecContext* c = NULL;
112 c = audio_stream->codec;
114 av_init_packet(&pkt);
117 AUD_readDevice(audio_mixdown_device, audio_input_buffer, audio_input_samples);
118 audio_time += (double) audio_input_samples / (double) c->sample_rate;
120 pkt.size = avcodec_encode_audio(c, audio_output_buffer,
122 (short*) audio_input_buffer);
126 // XXX error("Error writing audio packet");
130 pkt.data = audio_output_buffer;
132 if(c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
134 pkt.pts = av_rescale_q(c->coded_frame->pts,
135 c->time_base, audio_stream->time_base);
136 fprintf(stderr, "Audio Frame PTS: %d\n", (int)pkt.pts);
139 pkt.stream_index = audio_stream->index;
141 pkt.flags |= AV_PKT_FLAG_KEY;
143 if (av_interleaved_write_frame(outfile, &pkt) != 0) {
144 fprintf(stderr, "Error writing audio packet!\n");
149 #endif // #ifdef WITH_AUDASPACE
151 /* Allocate a temporary frame */
152 static AVFrame* alloc_picture(int pix_fmt, int width, int height)
158 /* allocate space for the struct */
159 f = avcodec_alloc_frame();
161 size = avpicture_get_size(pix_fmt, width, height);
162 /* allocate the actual picture buffer */
163 buf = MEM_mallocN(size, "AVFrame buffer");
168 avpicture_fill((AVPicture*)f, buf, pix_fmt, width, height);
172 /* Get the correct file extensions for the requested format,
173 first is always desired guess_format parameter */
174 static const char** get_file_extensions(int format)
178 static const char * rv[] = { ".dv", NULL };
182 static const char * rv[] = { ".mpg", ".mpeg", NULL };
186 static const char * rv[] = { ".dvd", ".vob", ".mpg", ".mpeg",
191 static const char * rv[] = { ".mp4", ".mpg", ".mpeg", NULL };
195 static const char * rv[] = { ".avi", NULL };
199 static const char * rv[] = { ".mov", NULL };
203 /* FIXME: avi for now... */
204 static const char * rv[] = { ".avi", NULL };
209 /* FIXME: avi for now... */
210 static const char * rv[] = { ".avi", NULL };
214 static const char * rv[] = { ".flv", NULL };
218 static const char * rv[] = { ".mkv", NULL };
222 static const char * rv[] = { ".ogg", ".ogv", NULL };
226 static const char * rv[] = { ".mp3", NULL };
230 static const char * rv[] = { ".wav", NULL };
238 /* Write a frame to the output file */
239 static int write_video_frame(RenderData *rd, int cfra, AVFrame* frame, ReportList *reports)
243 AVCodecContext* c = video_stream->codec;
247 if (rd->mode & R_FIELDS) {
248 frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
251 outsize = avcodec_encode_video(c, video_buffer, video_buffersize,
255 av_init_packet(&packet);
257 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
258 packet.pts = av_rescale_q(c->coded_frame->pts,
260 video_stream->time_base);
261 fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
263 fprintf(stderr, "Video Frame PTS: not set\n");
265 if (c->coded_frame->key_frame)
266 packet.flags |= AV_PKT_FLAG_KEY;
267 packet.stream_index = video_stream->index;
268 packet.data = video_buffer;
269 packet.size = outsize;
270 ret = av_interleaved_write_frame(outfile, &packet);
277 BKE_report(reports, RPT_ERROR, "Error writing frame.");
283 /* read and encode a frame of audio from the buffer */
284 static AVFrame* generate_video_frame(uint8_t* pixels, ReportList *reports)
286 uint8_t* rendered_frame;
288 AVCodecContext* c = video_stream->codec;
289 int width = c->width;
290 int height = c->height;
293 if (c->pix_fmt != PIX_FMT_BGR32) {
294 rgb_frame = alloc_picture(PIX_FMT_BGR32, width, height);
296 BKE_report(reports, RPT_ERROR, "Couldn't allocate temporary frame.");
300 rgb_frame = current_frame;
303 rendered_frame = pixels;
305 /* Do RGBA-conversion and flipping in one step depending
308 if (ENDIAN_ORDER == L_ENDIAN) {
310 for (y = 0; y < height; y++) {
311 uint8_t* target = rgb_frame->data[0]
312 + width * 4 * (height - y - 1);
313 uint8_t* src = rendered_frame + width * 4 * y;
314 uint8_t* end = src + width * 4;
327 for (y = 0; y < height; y++) {
328 uint8_t* target = rgb_frame->data[0]
329 + width * 4 * (height - y - 1);
330 uint8_t* src = rendered_frame + width * 4 * y;
331 uint8_t* end = src + width * 4;
344 if (c->pix_fmt != PIX_FMT_BGR32) {
345 sws_scale(img_convert_ctx, (const uint8_t * const*) rgb_frame->data,
346 rgb_frame->linesize, 0, c->height,
347 current_frame->data, current_frame->linesize);
348 delete_picture(rgb_frame);
350 return current_frame;
353 static void set_ffmpeg_property_option(AVCodecContext* c, IDProperty * prop)
357 const AVOption * rv = NULL;
359 fprintf(stderr, "FFMPEG expert option: %s: ", prop->name);
361 BLI_strncpy(name, prop->name, sizeof(name));
363 param = strchr(name, ':');
371 fprintf(stderr, "%s.\n", IDP_String(prop));
372 av_set_string3(c, prop->name, IDP_String(prop), 1, &rv);
375 fprintf(stderr, "%g.\n", IDP_Float(prop));
376 rv = av_set_double(c, prop->name, IDP_Float(prop));
379 fprintf(stderr, "%d.\n", IDP_Int(prop));
383 av_set_string3(c, name, param, 1, &rv);
388 rv = av_set_int(c, prop->name, IDP_Int(prop));
394 fprintf(stderr, "ffmpeg-option not supported: %s! Skipping.\n",
399 static int ffmpeg_proprty_valid(AVCodecContext *c, const char *prop_name, IDProperty *curr)
403 if(strcmp(prop_name, "video")==0) {
404 if(strcmp(curr->name, "bf")==0) {
405 /* flash codec doesn't support b frames */
406 valid&= c->codec_id!=CODEC_ID_FLV1;
413 static void set_ffmpeg_properties(RenderData *rd, AVCodecContext *c, const char * prop_name)
419 if (!rd->ffcodecdata.properties) {
423 prop = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, prop_name);
428 iter = IDP_GetGroupIterator(prop);
430 while ((curr = IDP_GroupIterNext(iter)) != NULL) {
431 if(ffmpeg_proprty_valid(c, prop_name, curr))
432 set_ffmpeg_property_option(c, curr);
436 /* prepare a video stream for the output file */
438 static AVStream* alloc_video_stream(RenderData *rd, int codec_id, AVFormatContext* of,
439 int rectx, int recty)
444 st = av_new_stream(of, 0);
445 if (!st) return NULL;
447 /* Set up the codec context */
450 c->codec_id = codec_id;
451 c->codec_type = AVMEDIA_TYPE_VIDEO;
454 /* Get some values from the current render settings */
459 /* FIXME: Really bad hack (tm) for NTSC support */
460 if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
461 c->time_base.den = 2997;
462 c->time_base.num = 100;
463 } else if ((double) ((int) rd->frs_sec_base) ==
465 c->time_base.den = rd->frs_sec;
466 c->time_base.num = (int) rd->frs_sec_base;
468 c->time_base.den = rd->frs_sec * 100000;
469 c->time_base.num = ((double) rd->frs_sec_base) * 100000;
472 c->gop_size = ffmpeg_gop_size;
473 c->bit_rate = ffmpeg_video_bitrate*1000;
474 c->rc_max_rate = rd->ffcodecdata.rc_max_rate*1000;
475 c->rc_min_rate = rd->ffcodecdata.rc_min_rate*1000;
476 c->rc_buffer_size = rd->ffcodecdata.rc_buffer_size * 1024;
477 c->rc_initial_buffer_occupancy
478 = rd->ffcodecdata.rc_buffer_size*3/4;
479 c->rc_buffer_aggressivity = 1.0;
480 c->me_method = ME_EPZS;
482 codec = avcodec_find_encoder(c->codec_id);
483 if (!codec) return NULL;
485 /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
487 if (codec->pix_fmts) {
488 c->pix_fmt = codec->pix_fmts[0];
490 /* makes HuffYUV happy ... */
491 c->pix_fmt = PIX_FMT_YUV422P;
494 if (ffmpeg_type == FFMPEG_XVID) {
496 c->pix_fmt = PIX_FMT_YUV420P;
497 c->codec_tag = (('D'<<24) + ('I'<<16) + ('V'<<8) + 'X');
500 if (codec_id == CODEC_ID_H264) {
501 /* correct wrong default ffmpeg param which crash x264 */
506 // Keep lossless encodes in the RGB domain.
507 if (codec_id == CODEC_ID_HUFFYUV || codec_id == CODEC_ID_FFV1) {
508 /* HUFFYUV was PIX_FMT_YUV422P before */
509 c->pix_fmt = PIX_FMT_RGB32;
512 if ((of->oformat->flags & AVFMT_GLOBALHEADER)
513 // || !strcmp(of->oformat->name, "mp4")
514 // || !strcmp(of->oformat->name, "mov")
515 // || !strcmp(of->oformat->name, "3gp")
517 fprintf(stderr, "Using global header\n");
518 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
521 /* Determine whether we are encoding interlaced material or not */
522 if (rd->mode & R_FIELDS) {
523 fprintf(stderr, "Encoding interlaced video\n");
524 c->flags |= CODEC_FLAG_INTERLACED_DCT;
525 c->flags |= CODEC_FLAG_INTERLACED_ME;
528 /* xasp & yasp got float lately... */
530 st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(
531 ((double) rd->xasp / (double) rd->yasp), 255);
533 set_ffmpeg_properties(rd, c, "video");
535 if (avcodec_open(c, codec) < 0) {
537 //XXX error("Couldn't initialize codec");
541 video_buffersize = avpicture_get_size(c->pix_fmt, c->width, c->height);
542 video_buffer = (uint8_t*)MEM_mallocN(video_buffersize*sizeof(uint8_t),
543 "FFMPEG video buffer");
545 current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
547 img_convert_ctx = sws_getContext(c->width, c->height,
556 /* Prepare an audio stream for the output file */
558 static AVStream* alloc_audio_stream(RenderData *rd, int codec_id, AVFormatContext* of)
564 st = av_new_stream(of, 1);
565 if (!st) return NULL;
568 c->codec_id = codec_id;
569 c->codec_type = AVMEDIA_TYPE_AUDIO;
571 c->sample_rate = rd->ffcodecdata.audio_mixrate;
572 c->bit_rate = ffmpeg_audio_bitrate*1000;
573 c->sample_fmt = SAMPLE_FMT_S16;
574 c->channels = rd->ffcodecdata.audio_channels;
575 codec = avcodec_find_encoder(c->codec_id);
577 //XXX error("Couldn't find a valid audio codec");
581 set_ffmpeg_properties(rd, c, "audio");
583 if (avcodec_open(c, codec) < 0) {
584 //XXX error("Couldn't initialize audio codec");
588 /* need to prevent floating point exception when using vorbis audio codec,
589 initialize this value in the same way as it's done in FFmpeg iteslf (sergey) */
590 st->codec->time_base.num= 1;
591 st->codec->time_base.den= st->codec->sample_rate;
593 audio_outbuf_size = FF_MIN_BUFFER_SIZE;
595 if((c->codec_id >= CODEC_ID_PCM_S16LE) && (c->codec_id <= CODEC_ID_PCM_DVD))
596 audio_input_samples = audio_outbuf_size * 8 / c->bits_per_coded_sample / c->channels;
599 audio_input_samples = c->frame_size;
600 if(c->frame_size * c->channels * sizeof(int16_t) * 4 > audio_outbuf_size)
601 audio_outbuf_size = c->frame_size * c->channels * sizeof(int16_t) * 4;
604 audio_output_buffer = (uint8_t*)av_malloc(
607 audio_input_buffer = (uint8_t*)av_malloc(
608 audio_input_samples * c->channels * sizeof(int16_t));
614 /* essential functions -- start, append, end */
616 static int start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty, ReportList *reports)
618 /* Handle to the output file */
624 ffmpeg_type = rd->ffcodecdata.type;
625 ffmpeg_codec = rd->ffcodecdata.codec;
626 ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
627 ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
628 ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
629 ffmpeg_gop_size = rd->ffcodecdata.gop_size;
630 ffmpeg_autosplit = rd->ffcodecdata.flags
631 & FFMPEG_AUTOSPLIT_OUTPUT;
635 /* Determine the correct filename */
636 filepath_ffmpeg(name, rd);
637 fprintf(stderr, "Starting output to %s(ffmpeg)...\n"
638 " Using type=%d, codec=%d, audio_codec=%d,\n"
639 " video_bitrate=%d, audio_bitrate=%d,\n"
640 " gop_size=%d, autosplit=%d\n"
641 " render width=%d, render height=%d\n",
642 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
643 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
644 ffmpeg_gop_size, ffmpeg_autosplit, rectx, recty);
646 exts = get_file_extensions(ffmpeg_type);
648 BKE_report(reports, RPT_ERROR, "No valid formats found.");
651 fmt = av_guess_format(NULL, exts[0], NULL);
653 BKE_report(reports, RPT_ERROR, "No valid formats found.");
657 of = avformat_alloc_context();
659 BKE_report(reports, RPT_ERROR, "Error opening output file");
664 of->packet_size= rd->ffcodecdata.mux_packet_size;
665 if (ffmpeg_audio_codec != CODEC_ID_NONE) {
666 of->mux_rate = rd->ffcodecdata.mux_rate;
671 of->preload = (int)(0.5*AV_TIME_BASE);
672 of->max_delay = (int)(0.7*AV_TIME_BASE);
674 fmt->audio_codec = ffmpeg_audio_codec;
676 BLI_snprintf(of->filename, sizeof(of->filename), "%s", name);
677 /* set the codec to the user's selection */
678 switch(ffmpeg_type) {
682 fmt->video_codec = ffmpeg_codec;
685 fmt->video_codec = CODEC_ID_THEORA;
688 fmt->video_codec = CODEC_ID_DVVIDEO;
691 fmt->video_codec = CODEC_ID_MPEG1VIDEO;
694 fmt->video_codec = CODEC_ID_MPEG2VIDEO;
697 fmt->video_codec = CODEC_ID_H264;
700 fmt->video_codec = CODEC_ID_MPEG4;
703 fmt->video_codec = CODEC_ID_FLV1;
706 fmt->audio_codec = CODEC_ID_MP3;
708 fmt->video_codec = CODEC_ID_NONE;
712 fmt->video_codec = CODEC_ID_MPEG4;
715 if (fmt->video_codec == CODEC_ID_DVVIDEO) {
717 BKE_report(reports, RPT_ERROR, "Render width has to be 720 pixels for DV!");
720 if (rd->frs_sec != 25 && recty != 480) {
721 BKE_report(reports, RPT_ERROR, "Render height has to be 480 pixels for DV-NTSC!");
724 if (rd->frs_sec == 25 && recty != 576) {
725 BKE_report(reports, RPT_ERROR, "Render height has to be 576 pixels for DV-PAL!");
730 if (ffmpeg_type == FFMPEG_DV) {
731 fmt->audio_codec = CODEC_ID_PCM_S16LE;
732 if (ffmpeg_audio_codec != CODEC_ID_NONE && rd->ffcodecdata.audio_mixrate != 48000 && rd->ffcodecdata.audio_channels != 2) {
733 BKE_report(reports, RPT_ERROR, "FFMPEG only supports 48khz / stereo audio for DV!");
738 if (fmt->video_codec != CODEC_ID_NONE) {
739 video_stream = alloc_video_stream(rd, fmt->video_codec, of, rectx, recty);
740 printf("alloc video stream %p\n", video_stream);
742 BKE_report(reports, RPT_ERROR, "Error initializing video stream.");
747 if (ffmpeg_audio_codec != CODEC_ID_NONE) {
748 audio_stream = alloc_audio_stream(rd, fmt->audio_codec, of);
750 BKE_report(reports, RPT_ERROR, "Error initializing audio stream.");
754 if (av_set_parameters(of, NULL) < 0) {
755 BKE_report(reports, RPT_ERROR, "Error setting output parameters.");
758 if (!(fmt->flags & AVFMT_NOFILE)) {
759 if (avio_open(&of->pb, name, AVIO_FLAG_WRITE) < 0) {
760 BKE_report(reports, RPT_ERROR, "Could not open file for writing.");
765 if (av_write_header(of) < 0) {
766 BKE_report(reports, RPT_ERROR, "Could not initialize streams. Probably unsupported codec combination.");
771 av_dump_format(of, 0, name, 1);
777 * Writes any delayed frames in the encoder. This function is called before
778 * closing the encoder.
781 * Since an encoder may use both past and future frames to predict
782 * inter-frames (H.264 B-frames, for example), it can output the frames
783 * in a different order from the one it was given.
784 * For example, when sending frames 1, 2, 3, 4 to the encoder, it may write
785 * them in the order 1, 4, 2, 3 - first the two frames used for predition,
786 * and then the bidirectionally-predicted frames. What this means in practice
787 * is that the encoder may not immediately produce one output frame for each
788 * input frame. These delayed frames must be flushed before we close the
789 * stream. We do this by calling avcodec_encode_video with NULL for the last
793 void flush_ffmpeg(void)
798 AVCodecContext* c = video_stream->codec;
799 /* get the delayed frames */
802 av_init_packet(&packet);
804 outsize = avcodec_encode_video(c, video_buffer, video_buffersize, NULL);
806 fprintf(stderr, "Error encoding delayed frame %d\n", outsize);
812 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
813 packet.pts = av_rescale_q(c->coded_frame->pts,
815 video_stream->time_base);
816 fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
818 fprintf(stderr, "Video Frame PTS: not set\n");
820 if (c->coded_frame->key_frame) {
821 packet.flags |= AV_PKT_FLAG_KEY;
823 packet.stream_index = video_stream->index;
824 packet.data = video_buffer;
825 packet.size = outsize;
826 ret = av_interleaved_write_frame(outfile, &packet);
828 fprintf(stderr, "Error writing delayed frame %d\n", ret);
832 avcodec_flush_buffers(video_stream->codec);
835 /* **********************************************************************
837 ********************************************************************** */
839 /* Get the output filename-- similar to the other output formats */
840 void filepath_ffmpeg(char* string, RenderData* rd)
844 const char ** exts = get_file_extensions(rd->ffcodecdata.type);
845 const char ** fe = exts;
847 if (!string || !exts) return;
849 strcpy(string, rd->pic);
850 BLI_path_abs(string, G.main->name);
852 BLI_make_existing_file(string);
856 if ((rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
857 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
861 if (BLI_strcasecmp(string + strlen(string) - strlen(*fe),
869 strcat(string, autosplit);
871 BLI_path_frame_range(string, rd->sfra, rd->efra, 4);
872 strcat(string, *exts);
874 *(string + strlen(string) - strlen(*fe)) = 0;
875 strcat(string, autosplit);
880 int start_ffmpeg(struct Scene *scene, RenderData *rd, int rectx, int recty, ReportList *reports)
884 ffmpeg_autosplit_count = 0;
886 success = start_ffmpeg_impl(rd, rectx, recty, reports);
887 #ifdef WITH_AUDASPACE
890 AVCodecContext* c = audio_stream->codec;
891 AUD_DeviceSpecs specs;
892 specs.channels = c->channels;
893 specs.format = AUD_FORMAT_S16;
894 specs.rate = rd->ffcodecdata.audio_mixrate;
895 audio_mixdown_device = sound_mixdown(scene, specs, rd->sfra, rd->ffcodecdata.audio_volume);
896 #ifdef FFMPEG_CODEC_TIME_BASE
897 c->time_base.den = specs.rate;
898 c->time_base.num = 1;
905 void end_ffmpeg(void);
907 #ifdef WITH_AUDASPACE
908 static void write_audio_frames(double to_pts)
912 while (audio_stream && !finished) {
913 if((audio_time >= to_pts) ||
914 (write_audio_frame())) {
921 int append_ffmpeg(RenderData *rd, int start_frame, int frame, int *pixels, int rectx, int recty, ReportList *reports)
926 fprintf(stderr, "Writing frame %i, "
927 "render width=%d, render height=%d\n", frame,
930 // why is this done before writing the video frame and again at end_ffmpeg?
931 // write_audio_frames(frame / (((double)rd->frs_sec) / rd->frs_sec_base));
935 avframe= generate_video_frame((unsigned char*) pixels, reports);
936 success= (avframe && write_video_frame(rd, frame - start_frame, avframe, reports));
938 if (ffmpeg_autosplit) {
939 if (avio_tell(outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
941 ffmpeg_autosplit_count++;
942 success &= start_ffmpeg_impl(rd, rectx, recty, reports);
947 #ifdef WITH_AUDASPACE
948 write_audio_frames((frame - rd->sfra) / (((double)rd->frs_sec) / rd->frs_sec_base));
953 void end_ffmpeg(void)
957 fprintf(stderr, "Closing ffmpeg...\n");
959 /* if (audio_stream) { SEE UPPER
960 write_audio_frames();
963 #ifdef WITH_AUDASPACE
964 if(audio_mixdown_device)
966 AUD_closeReadDevice(audio_mixdown_device);
967 audio_mixdown_device = 0;
971 if (video_stream && video_stream->codec) {
972 fprintf(stderr, "Flushing delayed frames...\n");
977 av_write_trailer(outfile);
980 /* Close the video codec */
982 if (video_stream && video_stream->codec) {
983 avcodec_close(video_stream->codec);
984 printf("zero video stream %p\n", video_stream);
989 /* Close the output file */
991 for (i = 0; i < outfile->nb_streams; i++) {
992 if (&outfile->streams[i]) {
993 av_freep(&outfile->streams[i]);
997 /* free the temp buffer */
999 delete_picture(current_frame);
1002 if (outfile && outfile->oformat) {
1003 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
1004 avio_close(outfile->pb);
1012 MEM_freeN(video_buffer);
1015 if (audio_output_buffer) {
1016 av_free(audio_output_buffer);
1017 audio_output_buffer = 0;
1019 if (audio_input_buffer) {
1020 av_free(audio_input_buffer);
1021 audio_input_buffer = 0;
1024 if (img_convert_ctx) {
1025 sws_freeContext(img_convert_ctx);
1026 img_convert_ctx = 0;
1032 void ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
1034 struct IDProperty *prop = (struct IDProperty *) prop_;
1037 if (!rd->ffcodecdata.properties) {
1041 group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
1042 if (group && prop) {
1043 IDP_RemFromGroup(group, prop);
1044 IDP_FreeProperty(prop);
1049 IDProperty *ffmpeg_property_add(RenderData *rd, const char *type, int opt_index, int parent_index)
1053 const AVOption * parent;
1056 IDPropertyTemplate val;
1062 avcodec_get_context_defaults(&c);
1064 o = c.av_class->option + opt_index;
1065 parent = c.av_class->option + parent_index;
1067 if (!rd->ffcodecdata.properties) {
1068 rd->ffcodecdata.properties
1069 = IDP_New(IDP_GROUP, &val, "ffmpeg");
1072 group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
1075 group = IDP_New(IDP_GROUP, &val, type);
1076 IDP_AddToGroup(rd->ffcodecdata.properties, group);
1080 BLI_snprintf(name, sizeof(name), "%s:%s", parent->name, o->name);
1082 BLI_strncpy(name, o->name, sizeof(name));
1085 fprintf(stderr, "ffmpeg_property_add: %s %d %d %s\n",
1086 type, parent_index, opt_index, name);
1088 prop = IDP_GetPropertyFromGroup(group, name);
1094 case FF_OPT_TYPE_INT:
1095 case FF_OPT_TYPE_INT64:
1096 val.i = FFMPEG_DEF_OPT_VAL_INT(o);
1099 case FF_OPT_TYPE_DOUBLE:
1100 case FF_OPT_TYPE_FLOAT:
1101 val.f = FFMPEG_DEF_OPT_VAL_DOUBLE(o);
1102 idp_type = IDP_FLOAT;
1104 case FF_OPT_TYPE_STRING:
1105 val.string.str = (char *)" ";
1106 val.string.len = 80;
1107 /* val.str = (char *)" ";*/
1108 idp_type = IDP_STRING;
1110 case FF_OPT_TYPE_CONST:
1117 prop = IDP_New(idp_type, &val, name);
1118 IDP_AddToGroup(group, prop);
1122 /* not all versions of ffmpeg include that, so here we go ... */
1124 static const AVOption *my_av_find_opt(void *v, const char *name,
1125 const char *unit, int mask, int flags){
1126 AVClass *c= *(AVClass**)v;
1127 const AVOption *o= c->option;
1129 for(;o && o->name; o++){
1130 if(!strcmp(o->name, name) &&
1131 (!unit || (o->unit && !strcmp(o->unit, unit))) &&
1132 (o->flags & mask) == flags )
1138 int ffmpeg_property_add_string(RenderData *rd, const char * type, const char * str)
1141 const AVOption * o = 0;
1142 const AVOption * p = 0;
1148 avcodec_get_context_defaults(&c);
1150 strncpy(name_, str, sizeof(name_));
1153 while (*name == ' ') name++;
1155 param = strchr(name, ':');
1158 param = strchr(name, ' ');
1162 while (*param == ' ') param++;
1165 o = my_av_find_opt(&c, name, NULL, 0, 0);
1169 if (param && o->type == FF_OPT_TYPE_CONST) {
1172 if (param && o->type != FF_OPT_TYPE_CONST && o->unit) {
1173 p = my_av_find_opt(&c, param, o->unit, 0, 0);
1174 prop = ffmpeg_property_add(rd,
1175 (char*) type, p - c.av_class->option,
1176 o - c.av_class->option);
1178 prop = ffmpeg_property_add(rd,
1179 (char*) type, o - c.av_class->option, 0);
1188 switch (prop->type) {
1190 IDP_Int(prop) = atoi(param);
1193 IDP_Float(prop) = atof(param);
1196 strncpy(IDP_String(prop), param, prop->len);
1203 void ffmpeg_set_preset(RenderData *rd, int preset)
1205 int isntsc = (rd->frs_sec != 25);
1207 if(rd->ffcodecdata.properties)
1208 IDP_FreeProperty(rd->ffcodecdata.properties);
1211 case FFMPEG_PRESET_VCD:
1212 rd->ffcodecdata.type = FFMPEG_MPEG1;
1213 rd->ffcodecdata.video_bitrate = 1150;
1215 rd->ysch = isntsc ? 240 : 288;
1216 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1217 rd->ffcodecdata.rc_max_rate = 1150;
1218 rd->ffcodecdata.rc_min_rate = 1150;
1219 rd->ffcodecdata.rc_buffer_size = 40*8;
1220 rd->ffcodecdata.mux_packet_size = 2324;
1221 rd->ffcodecdata.mux_rate = 2352 * 75 * 8;
1224 case FFMPEG_PRESET_SVCD:
1225 rd->ffcodecdata.type = FFMPEG_MPEG2;
1226 rd->ffcodecdata.video_bitrate = 2040;
1228 rd->ysch = isntsc ? 480 : 576;
1229 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1230 rd->ffcodecdata.rc_max_rate = 2516;
1231 rd->ffcodecdata.rc_min_rate = 0;
1232 rd->ffcodecdata.rc_buffer_size = 224*8;
1233 rd->ffcodecdata.mux_packet_size = 2324;
1234 rd->ffcodecdata.mux_rate = 0;
1237 case FFMPEG_PRESET_DVD:
1238 rd->ffcodecdata.type = FFMPEG_MPEG2;
1239 rd->ffcodecdata.video_bitrate = 6000;
1241 /* Don't set resolution, see [#21351]
1243 * rd->ysch = isntsc ? 480 : 576; */
1245 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1246 rd->ffcodecdata.rc_max_rate = 9000;
1247 rd->ffcodecdata.rc_min_rate = 0;
1248 rd->ffcodecdata.rc_buffer_size = 224*8;
1249 rd->ffcodecdata.mux_packet_size = 2048;
1250 rd->ffcodecdata.mux_rate = 10080000;
1253 case FFMPEG_PRESET_DV:
1254 rd->ffcodecdata.type = FFMPEG_DV;
1256 rd->ysch = isntsc ? 480 : 576;
1259 case FFMPEG_PRESET_H264:
1260 rd->ffcodecdata.type = FFMPEG_AVI;
1261 rd->ffcodecdata.codec = CODEC_ID_H264;
1262 rd->ffcodecdata.video_bitrate = 6000;
1263 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1264 rd->ffcodecdata.rc_max_rate = 9000;
1265 rd->ffcodecdata.rc_min_rate = 0;
1266 rd->ffcodecdata.rc_buffer_size = 224*8;
1267 rd->ffcodecdata.mux_packet_size = 2048;
1268 rd->ffcodecdata.mux_rate = 10080000;
1271 * All options here are for x264, but must be set via ffmpeg.
1272 * The names are therefore different - Search for "x264 to FFmpeg option mapping"
1277 * Use CABAC coder. Using "coder:1", which should be equivalent,
1278 * crashes Blender for some reason. Either way - this is no big deal.
1280 ffmpeg_property_add_string(rd, "video", "coder:vlc");
1283 * The other options were taken from the libx264-default.preset
1284 * included in the ffmpeg distribution.
1286 ffmpeg_property_add_string(rd, "video", "flags:loop");
1287 ffmpeg_property_add_string(rd, "video", "cmp:chroma");
1288 ffmpeg_property_add_string(rd, "video", "partitions:parti4x4");
1289 ffmpeg_property_add_string(rd, "video", "partitions:partp8x8");
1290 ffmpeg_property_add_string(rd, "video", "partitions:partb8x8");
1291 ffmpeg_property_add_string(rd, "video", "me:hex");
1292 ffmpeg_property_add_string(rd, "video", "subq:6");
1293 ffmpeg_property_add_string(rd, "video", "me_range:16");
1294 ffmpeg_property_add_string(rd, "video", "qdiff:4");
1295 ffmpeg_property_add_string(rd, "video", "keyint_min:25");
1296 ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
1297 ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
1298 ffmpeg_property_add_string(rd, "video", "b_strategy:1");
1299 ffmpeg_property_add_string(rd, "video", "bf:3");
1300 ffmpeg_property_add_string(rd, "video", "refs:2");
1301 ffmpeg_property_add_string(rd, "video", "qcomp:0.6");
1302 ffmpeg_property_add_string(rd, "video", "directpred:3");
1303 ffmpeg_property_add_string(rd, "video", "trellis:0");
1304 ffmpeg_property_add_string(rd, "video", "flags2:wpred");
1305 ffmpeg_property_add_string(rd, "video", "flags2:dct8x8");
1306 ffmpeg_property_add_string(rd, "video", "flags2:fastpskip");
1307 ffmpeg_property_add_string(rd, "video", "wpredp:2");
1309 // This makes x264 output lossless. Will be a separate option later.
1310 //ffmpeg_property_add_string(rd, "video", "cqp:0");
1313 case FFMPEG_PRESET_THEORA:
1314 case FFMPEG_PRESET_XVID:
1315 if(preset == FFMPEG_PRESET_XVID) {
1316 rd->ffcodecdata.type = FFMPEG_AVI;
1317 rd->ffcodecdata.codec = CODEC_ID_MPEG4;
1319 else if(preset == FFMPEG_PRESET_THEORA) {
1320 rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
1321 rd->ffcodecdata.codec = CODEC_ID_THEORA;
1324 rd->ffcodecdata.video_bitrate = 6000;
1325 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1326 rd->ffcodecdata.rc_max_rate = 9000;
1327 rd->ffcodecdata.rc_min_rate = 0;
1328 rd->ffcodecdata.rc_buffer_size = 224*8;
1329 rd->ffcodecdata.mux_packet_size = 2048;
1330 rd->ffcodecdata.mux_rate = 10080000;
1336 void ffmpeg_verify_image_type(RenderData *rd, ImageFormatData *imf)
1340 if(imf->imtype == R_IMF_IMTYPE_FFMPEG) {
1341 if(rd->ffcodecdata.type <= 0 ||
1342 rd->ffcodecdata.codec <= 0 ||
1343 rd->ffcodecdata.audio_codec <= 0 ||
1344 rd->ffcodecdata.video_bitrate <= 1) {
1346 rd->ffcodecdata.codec = CODEC_ID_MPEG2VIDEO;
1348 ffmpeg_set_preset(rd, FFMPEG_PRESET_DVD);
1350 if(rd->ffcodecdata.type == FFMPEG_OGG) {
1351 rd->ffcodecdata.type = FFMPEG_MPEG2;
1356 else if(imf->imtype == R_IMF_IMTYPE_H264) {
1357 if(rd->ffcodecdata.codec != CODEC_ID_H264) {
1358 ffmpeg_set_preset(rd, FFMPEG_PRESET_H264);
1362 else if(imf->imtype == R_IMF_IMTYPE_XVID) {
1363 if(rd->ffcodecdata.codec != CODEC_ID_MPEG4) {
1364 ffmpeg_set_preset(rd, FFMPEG_PRESET_XVID);
1368 else if(imf->imtype == R_IMF_IMTYPE_THEORA) {
1369 if(rd->ffcodecdata.codec != CODEC_ID_THEORA) {
1370 ffmpeg_set_preset(rd, FFMPEG_PRESET_THEORA);
1375 if(audio && rd->ffcodecdata.audio_codec < 0) {
1376 rd->ffcodecdata.audio_codec = CODEC_ID_NONE;
1377 rd->ffcodecdata.audio_bitrate = 128;