4 * Partial Copyright (c) 2006 Peter Schlaile
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
23 #if defined(_WIN32) && defined(_DEBUG) && !defined(__MINGW32__) && !defined(__CYGWIN__)
24 /* This does not seem necessary or present on MSVC 8, but may be needed in earlier versions? */
32 #include <libavformat/avformat.h>
33 #include <libavcodec/avcodec.h>
34 #include <libavutil/rational.h>
35 #include <libswscale/swscale.h>
36 #include <libavcodec/opt.h>
38 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
39 #define FFMPEG_OLD_FRAME_RATE 1
41 #define FFMPEG_CODEC_IS_POINTER 1
42 #define FFMPEG_CODEC_TIME_BASE 1
45 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
46 #define OUTFILE_PB (outfile->pb)
48 #define OUTFILE_PB (&outfile->pb)
51 #if defined(WIN32) && (!(defined snprintf))
52 #define snprintf _snprintf
55 #include "BKE_writeffmpeg.h"
57 #include "MEM_guardedalloc.h"
58 #include "BLI_blenlib.h"
60 #include "BKE_global.h"
61 #include "BKE_idprop.h"
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
66 #include "DNA_scene_types.h"
68 #include "AUD_C-API.h"
69 #include "BKE_sound.h"
76 extern void do_init_ffmpeg();
77 static void makeffmpegstring(RenderData* rd, char* string);
79 static int ffmpeg_type = 0;
80 static int ffmpeg_codec = CODEC_ID_MPEG4;
81 static int ffmpeg_audio_codec = CODEC_ID_MP2;
82 static int ffmpeg_video_bitrate = 1150;
83 static int ffmpeg_audio_bitrate = 128;
84 static int ffmpeg_gop_size = 12;
85 static int ffmpeg_multiplex_audio = 1;
86 static int ffmpeg_autosplit = 0;
87 static int ffmpeg_autosplit_count = 0;
89 static AVFormatContext* outfile = 0;
90 static AVStream* video_stream = 0;
91 static AVStream* audio_stream = 0;
92 static AVFrame* current_frame = 0;
93 static struct SwsContext *img_convert_ctx = 0;
95 static uint8_t* video_buffer = 0;
96 static int video_buffersize = 0;
98 static uint8_t* audio_input_buffer = 0;
99 static int audio_input_frame_size = 0;
100 static uint8_t* audio_output_buffer = 0;
101 static int audio_outbuf_size = 0;
103 static AUD_Device* audio_mixdown_device = 0;
105 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
107 /* Delete a picture buffer */
109 static void delete_picture(AVFrame* f)
112 if (f->data[0]) MEM_freeN(f->data[0]);
117 #ifdef FFMPEG_CODEC_IS_POINTER
118 static AVCodecContext* get_codec_from_stream(AVStream* stream)
120 return stream->codec;
123 static AVCodecContext* get_codec_from_stream(AVStream* stream)
125 return &stream->codec;
129 static int write_audio_frame(void)
131 AVCodecContext* c = NULL;
134 c = get_codec_from_stream(audio_stream);
136 if(audio_mixdown_device)
137 AUD_readDevice(audio_mixdown_device, audio_input_buffer, audio_input_frame_size);
139 av_init_packet(&pkt);
141 pkt.size = avcodec_encode_audio(c, audio_output_buffer,
143 (short*) audio_input_buffer);
144 pkt.data = audio_output_buffer;
145 #ifdef FFMPEG_CODEC_TIME_BASE
146 pkt.pts = av_rescale_q(c->coded_frame->pts,
147 c->time_base, audio_stream->time_base);
149 pkt.pts = c->coded_frame->pts;
151 fprintf(stderr, "Audio Frame PTS: %d\n", (int)pkt.pts);
153 pkt.stream_index = audio_stream->index;
154 pkt.flags |= PKT_FLAG_KEY;
155 if (av_interleaved_write_frame(outfile, &pkt) != 0) {
156 //XXX error("Error writing audio packet");
162 /* Allocate a temporary frame */
163 static AVFrame* alloc_picture(int pix_fmt, int width, int height)
169 /* allocate space for the struct */
170 f = avcodec_alloc_frame();
172 size = avpicture_get_size(pix_fmt, width, height);
173 /* allocate the actual picture buffer */
174 buf = MEM_mallocN(size, "AVFrame buffer");
179 avpicture_fill((AVPicture*)f, buf, pix_fmt, width, height);
183 /* Get the correct file extensions for the requested format,
184 first is always desired guess_format parameter */
185 static const char** get_file_extensions(int format)
189 static const char * rv[] = { ".dv", NULL };
193 static const char * rv[] = { ".mpg", ".mpeg", NULL };
197 static const char * rv[] = { ".dvd", ".vob", ".mpg", ".mpeg",
202 static const char * rv[] = { ".mp4", ".mpg", ".mpeg", NULL };
206 static const char * rv[] = { ".avi", NULL };
210 static const char * rv[] = { ".mov", NULL };
214 /* FIXME: avi for now... */
215 static const char * rv[] = { ".avi", NULL };
220 /* FIXME: avi for now... */
221 static const char * rv[] = { ".avi", NULL };
225 static const char * rv[] = { ".flv", NULL };
229 static const char * rv[] = { ".mkv", NULL };
233 static const char * rv[] = { ".ogg", ".ogv", NULL };
241 /* Write a frame to the output file */
242 static void write_video_frame(RenderData *rd, AVFrame* frame)
246 AVCodecContext* c = get_codec_from_stream(video_stream);
247 #ifdef FFMPEG_CODEC_TIME_BASE
248 frame->pts = rd->cfra - rd->sfra;
250 if (rd->mode & R_FIELDS) {
251 frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
254 outsize = avcodec_encode_video(c, video_buffer, video_buffersize,
258 av_init_packet(&packet);
260 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
261 #ifdef FFMPEG_CODEC_TIME_BASE
262 packet.pts = av_rescale_q(c->coded_frame->pts,
264 video_stream->time_base);
266 packet.pts = c->coded_frame->pts;
268 fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
270 fprintf(stderr, "Video Frame PTS: not set\n");
272 if (c->coded_frame->key_frame)
273 packet.flags |= PKT_FLAG_KEY;
274 packet.stream_index = video_stream->index;
275 packet.data = video_buffer;
276 packet.size = outsize;
277 ret = av_interleaved_write_frame(outfile, &packet);
281 //XXX error("Error writing frame");
285 /* read and encode a frame of audio from the buffer */
286 static AVFrame* generate_video_frame(uint8_t* pixels)
288 uint8_t* rendered_frame;
290 AVCodecContext* c = get_codec_from_stream(video_stream);
291 int width = c->width;
292 int height = c->height;
295 if (c->pix_fmt != PIX_FMT_BGR32) {
296 rgb_frame = alloc_picture(PIX_FMT_BGR32, width, height);
299 //XXX error("Couldn't allocate temporary frame");
303 rgb_frame = current_frame;
306 rendered_frame = pixels;
308 /* Do RGBA-conversion and flipping in one step depending
311 if (ENDIAN_ORDER == L_ENDIAN) {
313 for (y = 0; y < height; y++) {
314 uint8_t* target = rgb_frame->data[0]
315 + width * 4 * (height - y - 1);
316 uint8_t* src = rendered_frame + width * 4 * y;
317 uint8_t* end = src + width * 4;
330 for (y = 0; y < height; y++) {
331 uint8_t* target = rgb_frame->data[0]
332 + width * 4 * (height - y - 1);
333 uint8_t* src = rendered_frame + width * 4 * y;
334 uint8_t* end = src + width * 4;
347 if (c->pix_fmt != PIX_FMT_BGR32) {
348 sws_scale(img_convert_ctx, rgb_frame->data,
349 rgb_frame->linesize, 0, c->height,
350 current_frame->data, current_frame->linesize);
351 delete_picture(rgb_frame);
353 return current_frame;
356 static void set_ffmpeg_property_option(AVCodecContext* c, IDProperty * prop)
360 const AVOption * rv = NULL;
362 fprintf(stderr, "FFMPEG expert option: %s: ", prop->name);
364 strncpy(name, prop->name, 128);
366 param = strchr(name, ':');
374 fprintf(stderr, "%s.\n", IDP_String(prop));
375 rv = av_set_string(c, prop->name, IDP_String(prop));
378 fprintf(stderr, "%g.\n", IDP_Float(prop));
379 rv = av_set_double(c, prop->name, IDP_Float(prop));
382 fprintf(stderr, "%d.\n", IDP_Int(prop));
386 rv = av_set_string(c, name, param);
391 rv = av_set_int(c, prop->name, IDP_Int(prop));
397 fprintf(stderr, "ffmpeg-option not supported: %s! Skipping.\n",
402 static void set_ffmpeg_properties(RenderData *rd, AVCodecContext *c, const char * prop_name)
408 if (!rd->ffcodecdata.properties) {
412 prop = IDP_GetPropertyFromGroup(
413 rd->ffcodecdata.properties, (char*) prop_name);
418 iter = IDP_GetGroupIterator(prop);
420 while ((curr = IDP_GroupIterNext(iter)) != NULL) {
421 set_ffmpeg_property_option(c, curr);
425 /* prepare a video stream for the output file */
427 static AVStream* alloc_video_stream(RenderData *rd, int codec_id, AVFormatContext* of,
428 int rectx, int recty)
433 st = av_new_stream(of, 0);
434 if (!st) return NULL;
436 /* Set up the codec context */
438 c = get_codec_from_stream(st);
439 c->codec_id = codec_id;
440 c->codec_type = CODEC_TYPE_VIDEO;
443 /* Get some values from the current render settings */
448 #ifdef FFMPEG_CODEC_TIME_BASE
449 /* FIXME: Really bad hack (tm) for NTSC support */
450 if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
451 c->time_base.den = 2997;
452 c->time_base.num = 100;
453 } else if ((double) ((int) rd->frs_sec_base) ==
455 c->time_base.den = rd->frs_sec;
456 c->time_base.num = (int) rd->frs_sec_base;
458 c->time_base.den = rd->frs_sec * 100000;
459 c->time_base.num = ((double) rd->frs_sec_base) * 100000;
462 /* FIXME: Really bad hack (tm) for NTSC support */
463 if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
464 c->frame_rate = 2997;
465 c->frame_rate_base = 100;
466 } else if ((double) ((int) rd->frs_sec_base) ==
468 c->frame_rate = rd->frs_sec;
469 c->frame_rate_base = rd->frs_sec_base;
471 c->frame_rate = rd->frs_sec * 100000;
472 c->frame_rate_base = ((double) rd->frs_sec_base)*100000;
476 c->gop_size = ffmpeg_gop_size;
477 c->bit_rate = ffmpeg_video_bitrate*1000;
478 c->rc_max_rate = rd->ffcodecdata.rc_max_rate*1000;
479 c->rc_min_rate = rd->ffcodecdata.rc_min_rate*1000;
480 c->rc_buffer_size = rd->ffcodecdata.rc_buffer_size * 1024;
481 c->rc_initial_buffer_occupancy
482 = rd->ffcodecdata.rc_buffer_size*3/4;
483 c->rc_buffer_aggressivity = 1.0;
484 c->me_method = ME_EPZS;
486 codec = avcodec_find_encoder(c->codec_id);
487 if (!codec) return NULL;
489 /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
491 if (codec->pix_fmts) {
492 c->pix_fmt = codec->pix_fmts[0];
494 /* makes HuffYUV happy ... */
495 c->pix_fmt = PIX_FMT_YUV422P;
498 if (codec_id == CODEC_ID_XVID) {
500 c->pix_fmt = PIX_FMT_YUV420P;
503 if ((of->oformat->flags & AVFMT_GLOBALHEADER)
504 // || !strcmp(of->oformat->name, "mp4")
505 // || !strcmp(of->oformat->name, "mov")
506 // || !strcmp(of->oformat->name, "3gp")
508 fprintf(stderr, "Using global header\n");
509 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
512 /* Determine whether we are encoding interlaced material or not */
513 if (rd->mode & R_FIELDS) {
514 fprintf(stderr, "Encoding interlaced video\n");
515 c->flags |= CODEC_FLAG_INTERLACED_DCT;
516 c->flags |= CODEC_FLAG_INTERLACED_ME;
519 /* xasp & yasp got float lately... */
521 st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(
522 ((double) rd->xasp / (double) rd->yasp), 255);
524 set_ffmpeg_properties(rd, c, "video");
526 if (avcodec_open(c, codec) < 0) {
528 //XXX error("Couldn't initialize codec");
532 video_buffersize = 2000000;
533 video_buffer = (uint8_t*)MEM_mallocN(video_buffersize,
534 "FFMPEG video buffer");
536 current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
538 img_convert_ctx = sws_getContext(c->width, c->height,
547 /* Prepare an audio stream for the output file */
549 static AVStream* alloc_audio_stream(RenderData *rd, int codec_id, AVFormatContext* of)
555 st = av_new_stream(of, 1);
556 if (!st) return NULL;
558 c = get_codec_from_stream(st);
559 c->codec_id = codec_id;
560 c->codec_type = CODEC_TYPE_AUDIO;
562 c->sample_rate = rd->ffcodecdata.audio_mixrate;
563 c->bit_rate = ffmpeg_audio_bitrate*1000;
565 codec = avcodec_find_encoder(c->codec_id);
567 //XXX error("Couldn't find a valid audio codec");
571 set_ffmpeg_properties(rd, c, "audio");
573 if (avcodec_open(c, codec) < 0) {
574 //XXX error("Couldn't initialize audio codec");
578 /* FIXME: Should be user configurable */
579 if (ffmpeg_type == FFMPEG_DV) {
580 /* this is a hack around the poor ffmpeg dv multiplexer. */
581 /* only fixes PAL for now
582 (NTSC is a lot more complicated here...)! */
583 audio_outbuf_size = 7680;
585 audio_outbuf_size = 10000;
587 audio_output_buffer = (uint8_t*)MEM_mallocN(
588 audio_outbuf_size, "FFMPEG audio encoder input buffer");
590 /* ugly hack for PCM codecs */
592 if (c->frame_size <= 1) {
593 audio_input_frame_size = audio_outbuf_size / c->channels;
594 switch(c->codec_id) {
595 case CODEC_ID_PCM_S16LE:
596 case CODEC_ID_PCM_S16BE:
597 case CODEC_ID_PCM_U16LE:
598 case CODEC_ID_PCM_U16BE:
599 audio_input_frame_size >>= 1;
605 audio_input_frame_size = c->frame_size;
608 audio_input_buffer = (uint8_t*)MEM_mallocN(
609 audio_input_frame_size * sizeof(short) * c->channels,
610 "FFMPEG audio encoder output buffer");
614 /* essential functions -- start, append, end */
616 static void start_ffmpeg_impl(struct RenderData *rd, int rectx, int recty)
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_multiplex_audio = rd->ffcodecdata.flags
631 & FFMPEG_MULTIPLEX_AUDIO;
632 ffmpeg_autosplit = rd->ffcodecdata.flags
633 & FFMPEG_AUTOSPLIT_OUTPUT;
637 /* Determine the correct filename */
638 makeffmpegstring(rd, name);
639 fprintf(stderr, "Starting output to %s(ffmpeg)...\n"
640 " Using type=%d, codec=%d, audio_codec=%d,\n"
641 " video_bitrate=%d, audio_bitrate=%d,\n"
642 " gop_size=%d, multiplex=%d, autosplit=%d\n"
643 " render width=%d, render height=%d\n",
644 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
645 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
646 ffmpeg_gop_size, ffmpeg_multiplex_audio,
647 ffmpeg_autosplit, rectx, recty);
649 exts = get_file_extensions(ffmpeg_type);
651 G.afbreek = 1; /* Abort render */
652 //XXX error("No valid formats found");
655 fmt = guess_format(NULL, exts[0], NULL);
657 G.afbreek = 1; /* Abort render */
658 //XXX error("No valid formats found");
662 of = av_alloc_format_context();
665 //XXX error("Error opening output file");
670 of->packet_size= rd->ffcodecdata.mux_packet_size;
671 if (ffmpeg_multiplex_audio) {
672 of->mux_rate = rd->ffcodecdata.mux_rate;
677 of->preload = (int)(0.5*AV_TIME_BASE);
678 of->max_delay = (int)(0.7*AV_TIME_BASE);
680 snprintf(of->filename, sizeof(of->filename), "%s", name);
681 /* set the codec to the user's selection */
682 switch(ffmpeg_type) {
687 fmt->video_codec = ffmpeg_codec;
690 fmt->video_codec = CODEC_ID_DVVIDEO;
693 fmt->video_codec = CODEC_ID_MPEG1VIDEO;
696 fmt->video_codec = CODEC_ID_MPEG2VIDEO;
699 fmt->video_codec = CODEC_ID_H264;
702 fmt->video_codec = CODEC_ID_XVID;
705 fmt->video_codec = CODEC_ID_FLV1;
709 fmt->video_codec = CODEC_ID_MPEG4;
712 if (fmt->video_codec == CODEC_ID_DVVIDEO) {
715 //XXX error("Render width has to be 720 pixels for DV!");
718 if (rd->frs_sec != 25 && recty != 480) {
720 //XXX error("Render height has to be 480 pixels "
725 if (rd->frs_sec == 25 && recty != 576) {
727 //XXX error("Render height has to be 576 pixels "
733 fmt->audio_codec = ffmpeg_audio_codec;
735 if (ffmpeg_type == FFMPEG_DV) {
736 fmt->audio_codec = CODEC_ID_PCM_S16LE;
737 if (ffmpeg_multiplex_audio && rd->ffcodecdata.audio_mixrate != 48000) {
739 //XXX error("FFMPEG only supports 48khz / stereo "
745 video_stream = alloc_video_stream(rd, fmt->video_codec, of, rectx, recty);
748 //XXX error("Error initializing video stream");
752 if (ffmpeg_multiplex_audio) {
753 audio_stream = alloc_audio_stream(rd, fmt->audio_codec, of);
756 //XXX error("Error initializing audio stream");
759 //XXX audiostream_play(SFRA, 0, 1);
761 if (av_set_parameters(of, NULL) < 0) {
763 //XXX error("Error setting output parameters");
766 if (!(fmt->flags & AVFMT_NOFILE)) {
767 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
770 //XXX error("Could not open file for writing");
777 dump_format(of, 0, name, 1);
780 /* **********************************************************************
782 ********************************************************************** */
784 /* Get the output filename-- similar to the other output formats */
785 static void makeffmpegstring(RenderData* rd, char* string) {
787 // XXX quick define, solve!
788 #define FILE_MAXDIR 256
789 #define FILE_MAXFILE 126
791 char txt[FILE_MAXDIR+FILE_MAXFILE];
797 const char ** exts = get_file_extensions(rd->ffcodecdata.type);
798 const char ** fe = exts;
800 if (!string || !exts) return;
802 strcpy(string, rd->pic);
803 BLI_convertstringcode(string, G.sce);
804 BLI_convertstringframe(string, rd->cfra);
806 BLI_make_existing_file(string);
810 if ((rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
811 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
815 if (BLI_strcasecmp(string + strlen(string) - strlen(*fe),
823 strcat(string, autosplit);
824 sprintf(txt, "%04d_%04d%s", (rd->sfra),
828 *(string + strlen(string) - strlen(*fe)) = 0;
829 strcat(string, autosplit);
834 void start_ffmpeg(struct Scene *scene, RenderData *rd, int rectx, int recty)
836 ffmpeg_autosplit_count = 0;
838 start_ffmpeg_impl(rd, rectx, recty);
840 if(ffmpeg_multiplex_audio && audio_stream)
842 AVCodecContext* c = get_codec_from_stream(audio_stream);
844 specs.channels = c->channels;
845 specs.format = AUD_FORMAT_S16;
846 specs.rate = rd->ffcodecdata.audio_mixrate;
847 audio_mixdown_device = sound_mixdown(scene, specs, rd->sfra, rd->efra, rd->ffcodecdata.audio_volume);
851 void end_ffmpeg(void);
853 static void write_audio_frames()
857 while (ffmpeg_multiplex_audio && !finished) {
858 double a_pts = ((double)audio_stream->pts.val
859 * audio_stream->time_base.num
860 / audio_stream->time_base.den);
861 double v_pts = ((double)video_stream->pts.val
862 * video_stream->time_base.num
863 / video_stream->time_base.den);
873 void append_ffmpeg(RenderData *rd, int frame, int *pixels, int rectx, int recty)
875 fprintf(stderr, "Writing frame %i, "
876 "render width=%d, render height=%d\n", frame,
879 write_audio_frames();
880 write_video_frame(rd, generate_video_frame((unsigned char*) pixels));
882 if (ffmpeg_autosplit) {
883 if (url_ftell(OUTFILE_PB) > FFMPEG_AUTOSPLIT_SIZE) {
885 ffmpeg_autosplit_count++;
886 start_ffmpeg_impl(rd, rectx, recty);
892 void end_ffmpeg(void)
896 fprintf(stderr, "Closing ffmpeg...\n");
898 if (audio_stream && video_stream) {
899 write_audio_frames();
902 if(audio_mixdown_device)
904 AUD_closeReadDevice(audio_mixdown_device);
905 audio_mixdown_device = 0;
909 av_write_trailer(outfile);
912 /* Close the video codec */
914 if (video_stream && get_codec_from_stream(video_stream)) {
915 avcodec_close(get_codec_from_stream(video_stream));
920 /* Close the output file */
922 for (i = 0; i < outfile->nb_streams; i++) {
923 if (&outfile->streams[i]) {
924 av_freep(&outfile->streams[i]);
928 /* free the temp buffer */
930 delete_picture(current_frame);
933 if (outfile && outfile->oformat) {
934 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
935 url_fclose(OUTFILE_PB);
943 MEM_freeN(video_buffer);
946 if (audio_output_buffer) {
947 MEM_freeN(audio_output_buffer);
948 audio_output_buffer = 0;
950 if (audio_input_buffer) {
951 MEM_freeN(audio_input_buffer);
952 audio_input_buffer = 0;
955 if (img_convert_ctx) {
956 sws_freeContext(img_convert_ctx);
963 void ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
965 struct IDProperty *prop = (struct IDProperty *) prop_;
968 if (!rd->ffcodecdata.properties) {
972 group = IDP_GetPropertyFromGroup(
973 rd->ffcodecdata.properties, (char*) type);
975 IDP_RemFromGroup(group, prop);
976 IDP_FreeProperty(prop);
981 IDProperty *ffmpeg_property_add(RenderData *rd, char * type, int opt_index, int parent_index)
985 const AVOption * parent;
988 IDPropertyTemplate val;
992 avcodec_get_context_defaults(&c);
994 o = c.av_class->option + opt_index;
995 parent = c.av_class->option + parent_index;
997 if (!rd->ffcodecdata.properties) {
998 IDPropertyTemplate val;
1000 rd->ffcodecdata.properties
1001 = IDP_New(IDP_GROUP, val, "ffmpeg");
1004 group = IDP_GetPropertyFromGroup(
1005 rd->ffcodecdata.properties, (char*) type);
1008 IDPropertyTemplate val;
1010 group = IDP_New(IDP_GROUP, val, (char*) type);
1011 IDP_AddToGroup(rd->ffcodecdata.properties, group);
1015 sprintf(name, "%s:%s", parent->name, o->name);
1017 strcpy(name, o->name);
1020 fprintf(stderr, "ffmpeg_property_add: %s %d %d %s\n",
1021 type, parent_index, opt_index, name);
1023 prop = IDP_GetPropertyFromGroup(group, name);
1029 case FF_OPT_TYPE_INT:
1030 case FF_OPT_TYPE_INT64:
1031 val.i = o->default_val;
1034 case FF_OPT_TYPE_DOUBLE:
1035 case FF_OPT_TYPE_FLOAT:
1036 val.f = o->default_val;
1037 idp_type = IDP_FLOAT;
1039 case FF_OPT_TYPE_STRING:
1041 idp_type = IDP_STRING;
1043 case FF_OPT_TYPE_CONST:
1050 prop = IDP_New(idp_type, val, name);
1051 IDP_AddToGroup(group, prop);
1055 /* not all versions of ffmpeg include that, so here we go ... */
1057 static const AVOption *my_av_find_opt(void *v, const char *name,
1058 const char *unit, int mask, int flags){
1059 AVClass *c= *(AVClass**)v;
1060 const AVOption *o= c->option;
1062 for(;o && o->name; o++){
1063 if(!strcmp(o->name, name) &&
1064 (!unit || (o->unit && !strcmp(o->unit, unit))) &&
1065 (o->flags & mask) == flags )
1071 int ffmpeg_property_add_string(RenderData *rd, const char * type, const char * str)
1074 const AVOption * o = 0;
1075 const AVOption * p = 0;
1081 avcodec_get_context_defaults(&c);
1083 strncpy(name_, str, 128);
1086 while (*name == ' ') name++;
1088 param = strchr(name, ':');
1091 param = strchr(name, ' ');
1095 while (*param == ' ') param++;
1098 o = my_av_find_opt(&c, name, NULL, 0, 0);
1102 if (param && o->type == FF_OPT_TYPE_CONST) {
1105 if (param && o->type != FF_OPT_TYPE_CONST && o->unit) {
1106 p = my_av_find_opt(&c, param, o->unit, 0, 0);
1107 prop = ffmpeg_property_add(rd,
1108 (char*) type, p - c.av_class->option,
1109 o - c.av_class->option);
1111 prop = ffmpeg_property_add(rd,
1112 (char*) type, o - c.av_class->option, 0);
1121 switch (prop->type) {
1123 IDP_Int(prop) = atoi(param);
1126 IDP_Float(prop) = atof(param);
1129 strncpy(IDP_String(prop), param, prop->len);
1136 void ffmpeg_set_preset(RenderData *rd, int preset)
1138 int isntsc = (rd->frs_sec != 25);
1141 case FFMPEG_PRESET_VCD:
1142 rd->ffcodecdata.type = FFMPEG_MPEG1;
1143 rd->ffcodecdata.video_bitrate = 1150;
1145 rd->ysch = isntsc ? 240 : 288;
1146 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1147 rd->ffcodecdata.rc_max_rate = 1150;
1148 rd->ffcodecdata.rc_min_rate = 1150;
1149 rd->ffcodecdata.rc_buffer_size = 40*8;
1150 rd->ffcodecdata.mux_packet_size = 2324;
1151 rd->ffcodecdata.mux_rate = 2352 * 75 * 8;
1154 case FFMPEG_PRESET_SVCD:
1155 rd->ffcodecdata.type = FFMPEG_MPEG2;
1156 rd->ffcodecdata.video_bitrate = 2040;
1158 rd->ysch = isntsc ? 480 : 576;
1159 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1160 rd->ffcodecdata.rc_max_rate = 2516;
1161 rd->ffcodecdata.rc_min_rate = 0;
1162 rd->ffcodecdata.rc_buffer_size = 224*8;
1163 rd->ffcodecdata.mux_packet_size = 2324;
1164 rd->ffcodecdata.mux_rate = 0;
1167 case FFMPEG_PRESET_DVD:
1168 rd->ffcodecdata.type = FFMPEG_MPEG2;
1169 rd->ffcodecdata.video_bitrate = 6000;
1171 rd->ysch = isntsc ? 480 : 576;
1172 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1173 rd->ffcodecdata.rc_max_rate = 9000;
1174 rd->ffcodecdata.rc_min_rate = 0;
1175 rd->ffcodecdata.rc_buffer_size = 224*8;
1176 rd->ffcodecdata.mux_packet_size = 2048;
1177 rd->ffcodecdata.mux_rate = 10080000;
1180 case FFMPEG_PRESET_DV:
1181 rd->ffcodecdata.type = FFMPEG_DV;
1183 rd->ysch = isntsc ? 480 : 576;
1186 case FFMPEG_PRESET_H264:
1187 rd->ffcodecdata.type = FFMPEG_AVI;
1188 rd->ffcodecdata.codec = CODEC_ID_H264;
1189 rd->ffcodecdata.video_bitrate = 6000;
1190 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1191 rd->ffcodecdata.rc_max_rate = 9000;
1192 rd->ffcodecdata.rc_min_rate = 0;
1193 rd->ffcodecdata.rc_buffer_size = 224*8;
1194 rd->ffcodecdata.mux_packet_size = 2048;
1195 rd->ffcodecdata.mux_rate = 10080000;
1197 ffmpeg_property_add_string(rd, "video", "coder:vlc");
1198 ffmpeg_property_add_string(rd, "video", "flags:loop");
1199 ffmpeg_property_add_string(rd, "video", "cmp:chroma");
1200 ffmpeg_property_add_string(rd, "video", "partitions:parti4x4");
1201 ffmpeg_property_add_string(rd, "video", "partitions:partp8x8");
1202 ffmpeg_property_add_string(rd, "video", "partitions:partb8x8");
1203 ffmpeg_property_add_string(rd, "video", "me:hex");
1204 ffmpeg_property_add_string(rd, "video", "subq:5");
1205 ffmpeg_property_add_string(rd, "video", "me_range:16");
1206 ffmpeg_property_add_string(rd, "video", "keyint_min:25");
1207 ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
1208 ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
1209 ffmpeg_property_add_string(rd, "video", "b_strategy:1");
1213 case FFMPEG_PRESET_THEORA:
1214 case FFMPEG_PRESET_XVID:
1215 if(preset == FFMPEG_PRESET_XVID) {
1216 rd->ffcodecdata.type = FFMPEG_AVI;
1217 rd->ffcodecdata.codec = CODEC_ID_XVID;
1219 else if(preset == FFMPEG_PRESET_THEORA) {
1220 rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
1221 rd->ffcodecdata.codec = CODEC_ID_THEORA;
1224 rd->ffcodecdata.video_bitrate = 6000;
1225 rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
1226 rd->ffcodecdata.rc_max_rate = 9000;
1227 rd->ffcodecdata.rc_min_rate = 0;
1228 rd->ffcodecdata.rc_buffer_size = 224*8;
1229 rd->ffcodecdata.mux_packet_size = 2048;
1230 rd->ffcodecdata.mux_rate = 10080000;
1236 void ffmpeg_verify_image_type(RenderData *rd)
1240 if(rd->imtype == R_FFMPEG) {
1241 if(rd->ffcodecdata.type <= 0 ||
1242 rd->ffcodecdata.codec <= 0 ||
1243 rd->ffcodecdata.audio_codec <= 0 ||
1244 rd->ffcodecdata.video_bitrate <= 1) {
1246 rd->ffcodecdata.codec = CODEC_ID_MPEG2VIDEO;
1247 ffmpeg_set_preset(rd, FFMPEG_PRESET_DVD);
1252 else if(rd->imtype == R_H264) {
1253 if(rd->ffcodecdata.codec != CODEC_ID_H264) {
1254 ffmpeg_set_preset(rd, FFMPEG_PRESET_H264);
1258 else if(rd->imtype == R_XVID) {
1259 if(rd->ffcodecdata.codec != CODEC_ID_XVID) {
1260 ffmpeg_set_preset(rd, FFMPEG_PRESET_XVID);
1264 else if(rd->imtype == R_THEORA) {
1265 if(rd->ffcodecdata.codec != CODEC_ID_THEORA) {
1266 ffmpeg_set_preset(rd, FFMPEG_PRESET_THEORA);
1271 if(audio && rd->ffcodecdata.audio_codec <= 0) {
1272 rd->ffcodecdata.audio_codec = CODEC_ID_MP2;
1273 rd->ffcodecdata.audio_bitrate = 128;