use static functions where possible for some local functions.
[blender.git] / source / blender / blenkernel / intern / writeffmpeg.c
index 472a6612a5051f7d87c56e8b4756d8c033f6d149..9e86dcbe49118b2a4cc2d63fd720fbfe873fbd45 100644 (file)
 
 #include "DNA_scene_types.h"
 
+#include "AUD_C-API.h"
+#include "BKE_sound.h"
+#include "BKE_main.h"
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -96,6 +100,8 @@ static int audio_input_frame_size = 0;
 static uint8_t* audio_output_buffer = 0;
 static int audio_outbuf_size = 0;
 
+static AUD_Device* audio_mixdown_device = 0;
+
 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
 
 /* Delete a picture buffer */
@@ -127,9 +133,8 @@ static int write_audio_frame(void)
 
        c = get_codec_from_stream(audio_stream);
 
-       //XXX audiostream_fill(audio_input_buffer, 
-       //               audio_input_frame_size 
-       //               * sizeof(short) * c->channels);
+       if(audio_mixdown_device)
+               AUD_readDevice(audio_mixdown_device, audio_input_buffer, audio_input_frame_size);
 
        av_init_packet(&pkt);
 
@@ -242,6 +247,9 @@ static void write_video_frame(RenderData *rd, AVFrame* frame)
 #ifdef FFMPEG_CODEC_TIME_BASE
        frame->pts = rd->cfra - rd->sfra;
 #endif
+       if (rd->mode & R_FIELDS) {
+               frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
+       }
 
        outsize = avcodec_encode_video(c, video_buffer, video_buffersize, 
                                       frame);
@@ -605,7 +613,7 @@ static AVStream* alloc_audio_stream(RenderData *rd, int codec_id, AVFormatContex
 }
 /* essential functions -- start, append, end */
 
-void start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty)
+static void start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty)
 {
        /* Handle to the output file */
        AVFormatContext* of;
@@ -824,11 +832,21 @@ static void makeffmpegstring(RenderData* rd, char* string) {
 }
 
 
-void start_ffmpeg(RenderData *rd, int rectx, int recty)
+void start_ffmpeg(struct Scene *scene, RenderData *rd, int rectx, int recty)
 {
        ffmpeg_autosplit_count = 0;
 
        start_ffmpeg_impl(rd, rectx, recty);
+
+       if(ffmpeg_multiplex_audio && audio_stream)
+       {
+               AVCodecContext* c = get_codec_from_stream(audio_stream);
+               AUD_Specs specs;
+               specs.channels = c->channels;
+               specs.format = AUD_FORMAT_S16;
+               specs.rate = rd->audio.mixrate;
+               audio_mixdown_device = sound_mixdown(scene, specs, rd->sfra, rd->efra);
+       }
 }
 
 void end_ffmpeg(void);
@@ -881,6 +899,12 @@ void end_ffmpeg(void)
        if (audio_stream && video_stream) {
                write_audio_frames();
        }
+
+       if(audio_mixdown_device)
+       {
+               AUD_closeReadDevice(audio_mixdown_device);
+               audio_mixdown_device = 0;
+       }
        
        if (outfile) {
                av_write_trailer(outfile);
@@ -934,5 +958,322 @@ void end_ffmpeg(void)
                img_convert_ctx = 0;
        }
 }
+
+/* properties */
+
+void ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
+{
+       struct IDProperty *prop = (struct IDProperty *) prop_;
+       IDProperty * group;
+       
+       if (!rd->ffcodecdata.properties) {
+               return;
+       }
+
+       group = IDP_GetPropertyFromGroup(
+               rd->ffcodecdata.properties, (char*) type);
+       if (group && prop) {
+               IDP_RemFromGroup(group, prop);
+               IDP_FreeProperty(prop);
+               MEM_freeN(prop);
+       }
+}
+
+IDProperty *ffmpeg_property_add(RenderData *rd, char * type, int opt_index, int parent_index)
+{
+       AVCodecContext c;
+       const AVOption * o;
+       const AVOption * parent;
+       IDProperty * group;
+       IDProperty * prop;
+       IDPropertyTemplate val;
+       int idp_type;
+       char name[256];
+
+       avcodec_get_context_defaults(&c);
+
+       o = c.av_class->option + opt_index;
+       parent = c.av_class->option + parent_index;
+
+       if (!rd->ffcodecdata.properties) {
+               IDPropertyTemplate val;
+
+               rd->ffcodecdata.properties 
+                       = IDP_New(IDP_GROUP, val, "ffmpeg"); 
+       }
+
+       group = IDP_GetPropertyFromGroup(
+               rd->ffcodecdata.properties, (char*) type);
+       
+       if (!group) {
+               IDPropertyTemplate val;
+               
+               group = IDP_New(IDP_GROUP, val, (char*) type); 
+               IDP_AddToGroup(rd->ffcodecdata.properties, group);
+       }
+
+       if (parent_index) {
+               sprintf(name, "%s:%s", parent->name, o->name);
+       } else {
+               strcpy(name, o->name);
+       }
+
+       fprintf(stderr, "ffmpeg_property_add: %s %d %d %s\n",
+               type, parent_index, opt_index, name);
+
+       prop = IDP_GetPropertyFromGroup(group, name);
+       if (prop) {
+               return prop;
+       }
+
+       switch (o->type) {
+       case FF_OPT_TYPE_INT:
+       case FF_OPT_TYPE_INT64:
+               val.i = o->default_val;
+               idp_type = IDP_INT;
+               break;
+       case FF_OPT_TYPE_DOUBLE:
+       case FF_OPT_TYPE_FLOAT:
+               val.f = o->default_val;
+               idp_type = IDP_FLOAT;
+               break;
+       case FF_OPT_TYPE_STRING:
+               val.str = "                                                                               ";
+               idp_type = IDP_STRING;
+               break;
+       case FF_OPT_TYPE_CONST:
+               val.i = 1;
+               idp_type = IDP_INT;
+               break;
+       default:
+               return NULL;
+       }
+       prop = IDP_New(idp_type, val, name);
+       IDP_AddToGroup(group, prop);
+       return prop;
+}
+
+/* not all versions of ffmpeg include that, so here we go ... */
+
+static const AVOption *my_av_find_opt(void *v, const char *name, 
+                                     const char *unit, int mask, int flags){
+       AVClass *c= *(AVClass**)v; 
+       const AVOption *o= c->option;
+
+       for(;o && o->name; o++){
+               if(!strcmp(o->name, name) && 
+                  (!unit || (o->unit && !strcmp(o->unit, unit))) && 
+                  (o->flags & mask) == flags )
+                       return o;
+       }
+       return NULL;
+}
+
+int ffmpeg_property_add_string(RenderData *rd, const char * type, const char * str)
+{
+       AVCodecContext c;
+       const AVOption * o = 0;
+       const AVOption * p = 0;
+       char name_[128];
+       char * name;
+       char * param;
+       IDProperty * prop;
+       
+       avcodec_get_context_defaults(&c);
+
+       strncpy(name_, str, 128);
+
+       name = name_;
+       while (*name == ' ') name++;
+
+       param = strchr(name, ':');
+
+       if (!param) {
+               param = strchr(name, ' ');
+       }
+       if (param) {
+               *param++ = 0;
+               while (*param == ' ') param++;
+       }
+       
+       o = my_av_find_opt(&c, name, NULL, 0, 0);       
+       if (!o) {
+               return 0;
+       }
+       if (param && o->type == FF_OPT_TYPE_CONST) {
+               return 0;
+       }
+       if (param && o->type != FF_OPT_TYPE_CONST && o->unit) {
+               p = my_av_find_opt(&c, param, o->unit, 0, 0);   
+               prop = ffmpeg_property_add(rd,
+                       (char*) type, p - c.av_class->option, 
+                       o - c.av_class->option);
+       } else {
+               prop = ffmpeg_property_add(rd,
+                       (char*) type, o - c.av_class->option, 0);
+       }
+               
+
+       if (!prop) {
+               return 0;
+       }
+
+       if (param && !p) {
+               switch (prop->type) {
+               case IDP_INT:
+                       IDP_Int(prop) = atoi(param);
+                       break;
+               case IDP_FLOAT:
+                       IDP_Float(prop) = atof(param);
+                       break;
+               case IDP_STRING:
+                       strncpy(IDP_String(prop), param, prop->len);
+                       break;
+               }
+       }
+       return 1;
+}
+
+void ffmpeg_set_preset(RenderData *rd, int preset)
+{
+       int isntsc = (rd->frs_sec != 25);
+
+       switch (preset) {
+       case FFMPEG_PRESET_VCD:
+               rd->ffcodecdata.type = FFMPEG_MPEG1;
+               rd->ffcodecdata.video_bitrate = 1150;
+               rd->xsch = 352;
+               rd->ysch = isntsc ? 240 : 288;
+               rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
+               rd->ffcodecdata.rc_max_rate = 1150;
+               rd->ffcodecdata.rc_min_rate = 1150;
+               rd->ffcodecdata.rc_buffer_size = 40*8;
+               rd->ffcodecdata.mux_packet_size = 2324;
+               rd->ffcodecdata.mux_rate = 2352 * 75 * 8;
+               break;
+
+       case FFMPEG_PRESET_SVCD:
+               rd->ffcodecdata.type = FFMPEG_MPEG2;
+               rd->ffcodecdata.video_bitrate = 2040;
+               rd->xsch = 480;
+               rd->ysch = isntsc ? 480 : 576;
+               rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
+               rd->ffcodecdata.rc_max_rate = 2516;
+               rd->ffcodecdata.rc_min_rate = 0;
+               rd->ffcodecdata.rc_buffer_size = 224*8;
+               rd->ffcodecdata.mux_packet_size = 2324;
+               rd->ffcodecdata.mux_rate = 0;
+               break;
+
+       case FFMPEG_PRESET_DVD:
+               rd->ffcodecdata.type = FFMPEG_MPEG2;
+               rd->ffcodecdata.video_bitrate = 6000;
+               rd->xsch = 720;
+               rd->ysch = isntsc ? 480 : 576;
+               rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
+               rd->ffcodecdata.rc_max_rate = 9000;
+               rd->ffcodecdata.rc_min_rate = 0;
+               rd->ffcodecdata.rc_buffer_size = 224*8;
+               rd->ffcodecdata.mux_packet_size = 2048;
+               rd->ffcodecdata.mux_rate = 10080000;
+               break;
+
+       case FFMPEG_PRESET_DV:
+               rd->ffcodecdata.type = FFMPEG_DV;
+               rd->xsch = 720;
+               rd->ysch = isntsc ? 480 : 576;
+               break;
+
+       case FFMPEG_PRESET_H264:
+               rd->ffcodecdata.type = FFMPEG_AVI;
+               rd->ffcodecdata.codec = CODEC_ID_H264;
+               rd->ffcodecdata.video_bitrate = 6000;
+               rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
+               rd->ffcodecdata.rc_max_rate = 9000;
+               rd->ffcodecdata.rc_min_rate = 0;
+               rd->ffcodecdata.rc_buffer_size = 224*8;
+               rd->ffcodecdata.mux_packet_size = 2048;
+               rd->ffcodecdata.mux_rate = 10080000;
+
+               ffmpeg_property_add_string(rd, "video", "coder:vlc");
+               ffmpeg_property_add_string(rd, "video", "flags:loop");
+               ffmpeg_property_add_string(rd, "video", "cmp:chroma");
+               ffmpeg_property_add_string(rd, "video", "partitions:parti4x4");
+               ffmpeg_property_add_string(rd, "video", "partitions:partp8x8");
+               ffmpeg_property_add_string(rd, "video", "partitions:partb8x8");
+               ffmpeg_property_add_string(rd, "video", "me:hex");
+               ffmpeg_property_add_string(rd, "video", "subq:5");
+               ffmpeg_property_add_string(rd, "video", "me_range:16");
+               ffmpeg_property_add_string(rd, "video", "keyint_min:25");
+               ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
+               ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
+               ffmpeg_property_add_string(rd, "video", "b_strategy:1");
+
+               break;
+
+       case FFMPEG_PRESET_THEORA:
+       case FFMPEG_PRESET_XVID:
+               if(preset == FFMPEG_PRESET_XVID) {
+                       rd->ffcodecdata.type = FFMPEG_AVI;
+                       rd->ffcodecdata.codec = CODEC_ID_XVID;
+               }
+               else if(preset == FFMPEG_PRESET_THEORA) {
+                       rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
+                       rd->ffcodecdata.codec = CODEC_ID_THEORA;
+               }
+
+               rd->ffcodecdata.video_bitrate = 6000;
+               rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
+               rd->ffcodecdata.rc_max_rate = 9000;
+               rd->ffcodecdata.rc_min_rate = 0;
+               rd->ffcodecdata.rc_buffer_size = 224*8;
+               rd->ffcodecdata.mux_packet_size = 2048;
+               rd->ffcodecdata.mux_rate = 10080000;
+               break;
+
+       }
+}
+
+void ffmpeg_verify_image_type(RenderData *rd)
+{
+       int audio= 0;
+
+       if(rd->imtype == R_FFMPEG) {
+               if(rd->ffcodecdata.type <= 0 ||
+                  rd->ffcodecdata.codec <= 0 ||
+                  rd->ffcodecdata.audio_codec <= 0 ||
+                  rd->ffcodecdata.video_bitrate <= 1) {
+
+                       rd->ffcodecdata.codec = CODEC_ID_MPEG2VIDEO;
+                       ffmpeg_set_preset(rd, FFMPEG_PRESET_DVD);
+               }
+
+               audio= 1;
+       }
+       else if(rd->imtype == R_H264) {
+               if(rd->ffcodecdata.codec != CODEC_ID_H264) {
+                       ffmpeg_set_preset(rd, FFMPEG_PRESET_H264);
+                       audio= 1;
+               }
+       }
+       else if(rd->imtype == R_XVID) {
+               if(rd->ffcodecdata.codec != CODEC_ID_XVID) {
+                       ffmpeg_set_preset(rd, FFMPEG_PRESET_XVID);
+                       audio= 1;
+               }
+       }
+       else if(rd->imtype == R_THEORA) {
+               if(rd->ffcodecdata.codec != CODEC_ID_THEORA) {
+                       ffmpeg_set_preset(rd, FFMPEG_PRESET_THEORA);
+                       audio= 1;
+               }
+       }
+
+       if(audio && rd->ffcodecdata.audio_codec <= 0) {
+               rd->ffcodecdata.audio_codec = CODEC_ID_MP2;
+               rd->ffcodecdata.audio_bitrate = 128;
+       }
+}
+
 #endif