this patch features several cleanups and bugfixes for the sequencer:
[blender.git] / source / blender / blenkernel / intern / writeffmpeg.c
1 /*
2  * ffmpeg-write support
3  *
4  * Partial Copyright (c) 2006 Peter Schlaile
5  *
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.
10  *
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.
15  *
16  */
17
18
19 #ifdef WITH_FFMPEG
20 #include <string.h>
21 #include <stdio.h>
22
23 #include <stdlib.h>
24 #include <ffmpeg/rational.h>
25 #include <ffmpeg/avformat.h>
26 #include <ffmpeg/avcodec.h>
27
28 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
29 #define FFMPEG_OLD_FRAME_RATE 1
30 #else
31 #define FFMPEG_CODEC_IS_POINTER 1
32 #define FFMPEG_CODEC_TIME_BASE  1
33 #endif
34
35 #include "BKE_writeffmpeg.h"
36
37 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39
40 #include "BKE_bad_level_calls.h"
41 #include "BKE_global.h"
42
43 #include "IMB_imbuf_types.h"
44 #include "IMB_imbuf.h"
45
46 #include "BSE_seqaudio.h"
47
48 #include "DNA_scene_types.h"
49 #include "blendef.h"
50
51 #ifdef HAVE_CONFIG_H
52 #include <config.h>
53 #endif
54
55 extern void makewavstring (char *string);
56 extern void do_init_ffmpeg();
57
58 static int ffmpeg_type = 0;
59 static int ffmpeg_codec = CODEC_ID_MPEG4;
60 static int ffmpeg_audio_codec = CODEC_ID_MP2;
61 static int ffmpeg_video_bitrate = 1150;
62 static int ffmpeg_audio_bitrate = 128;
63 static int ffmpeg_gop_size = 12;
64 static int ffmpeg_multiplex_audio = 1;
65 static int ffmpeg_autosplit = 0;
66 static int ffmpeg_autosplit_count = 0;
67
68 static AVFormatContext* outfile;
69 static AVStream* video_stream;
70 static AVStream* audio_stream;
71 static AVFrame* current_frame;
72
73 static uint8_t* video_buffer = 0;
74 static int video_buffersize = 0;
75
76 static uint8_t* audio_input_buffer = 0;
77 static int audio_input_frame_size = 0;
78 static uint8_t* audio_output_buffer = 0;
79 static int audio_outbuf_size = 0;
80
81 static RenderData *ffmpeg_renderdata;
82
83 #define FFMPEG_AUTOSPLIT_SIZE 2000000000
84
85 /* Delete a picture buffer */
86
87 void delete_picture(AVFrame* f)
88 {
89         if (f) {
90                 if (f->data[0]) MEM_freeN(f->data[0]);
91                 av_free(f);
92         }
93 }
94
95 #ifdef FFMPEG_CODEC_IS_POINTER
96 static AVCodecContext* get_codec_from_stream(AVStream* stream)
97 {
98         return stream->codec;
99 }
100 #else
101 static AVCodecContext* get_codec_from_stream(AVStream* stream)
102 {
103         return &stream->codec;
104 }
105 #endif
106
107 int write_audio_frame(void) {
108         AVCodecContext* c = NULL;
109         AVPacket pkt;
110
111         c = get_codec_from_stream(audio_stream);
112
113         audiostream_fill(audio_input_buffer, 
114                          audio_input_frame_size 
115                          * sizeof(short) * c->channels);
116
117         av_init_packet(&pkt);
118
119         pkt.size = avcodec_encode_audio(c, audio_output_buffer,
120                                         audio_outbuf_size, 
121                                         (short*) audio_input_buffer);
122         pkt.data = audio_output_buffer;
123 #ifdef FFMPEG_CODEC_TIME_BASE
124         pkt.pts = av_rescale_q(c->coded_frame->pts, 
125                                c->time_base, audio_stream->time_base);
126 #else
127         pkt.pts = c->coded_frame->pts;
128 #endif
129         pkt.stream_index = audio_stream->index;
130         pkt.flags |= PKT_FLAG_KEY;
131         if (av_write_frame(outfile, &pkt) != 0) {
132                 error("Error writing audio packet");
133                 return -1;
134         }
135         return 0;
136 }
137
138 /* Allocate a temporary frame */
139
140 AVFrame* alloc_picture(int pix_fmt, int width, int height) 
141 {
142         AVFrame* f;
143         uint8_t* buf;
144         int size;
145         
146         /* allocate space for the struct */
147         f = avcodec_alloc_frame();
148         if (!f) return NULL;
149         size = avpicture_get_size(pix_fmt, width, height);
150         /* allocate the actual picture buffer */
151         buf = MEM_mallocN(size, "AVFrame buffer");
152         if (!buf) {
153                 free(f);
154                 return NULL;
155         }
156         avpicture_fill((AVPicture*)f, buf, pix_fmt, width, height);
157         return f;
158 }
159
160 /* Get an output format based on the menu selection */
161 AVOutputFormat* ffmpeg_get_format(int format) 
162 {
163         AVOutputFormat* f;
164         switch(format) {
165                 case FFMPEG_DV:
166                         f = guess_format("dv", NULL, NULL);
167                         break;
168                 case FFMPEG_MPEG1:
169                         f = guess_format("mpeg", NULL, NULL);
170                         break;
171                 case FFMPEG_MPEG2:
172                         f = guess_format("dvd", NULL, NULL);
173                         break;
174                 case FFMPEG_MPEG4:
175                         f = guess_format("mp4", NULL, NULL);
176                         break;
177                 case FFMPEG_AVI:
178                         f = guess_format("avi", NULL, NULL);
179                         break;
180                 case FFMPEG_MOV:
181                         f = guess_format("mov", NULL, NULL);
182                         break;
183                 default:
184                         f = NULL;
185         }
186         return f;
187 }
188
189 /* Get the correct file extension for the requested format */
190 void file_extension(int format, char* string) 
191 {
192         int i;
193         AVOutputFormat* f;
194         const char* ext;
195         f= ffmpeg_get_format(format);
196         ext = f->extensions;
197         for (i = 0; ext[i] != ',' && i < strlen(ext); i++);
198         snprintf(string, i+2, ".%s", ext);
199 }
200
201 /* Get the output filename-- similar to the other output formats */
202 void makeffmpegstring(char* string) {
203         
204         char txt[FILE_MAXDIR+FILE_MAXFILE];
205         char fe[FILE_MAXDIR+FILE_MAXFILE];
206         char autosplit[20];
207
208         file_extension(ffmpeg_type, fe);
209
210         if (!string) return;
211
212         strcpy(string, G.scene->r.pic);
213         BLI_convertstringcode(string, G.sce, G.scene->r.cfra);
214
215         BLI_make_existing_file(string);
216
217         autosplit[0] = 0;
218
219         if (ffmpeg_autosplit) {
220                 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
221         }
222
223         if (BLI_strcasecmp(string + strlen(string) - strlen(fe), fe)) {
224                 strcat(string, autosplit);
225                 sprintf(txt, "%04d_%04d%s", (G.scene->r.sfra), 
226                         (G.scene->r.efra), fe);
227                 strcat(string, txt);
228         } else {
229                 *(string + strlen(string) - strlen(fe)) = 0;
230                 strcat(string, autosplit);
231                 strcat(string, fe);
232         }
233 }
234
235 /* Write a frame to the output file */
236 static void write_video_frame(AVFrame* frame) {
237         int outsize = 0;
238         int ret;
239         AVCodecContext* c = get_codec_from_stream(video_stream);
240         frame->pts = G.scene->r.cfra - G.scene->r.sfra;
241
242         outsize = avcodec_encode_video(c, video_buffer, video_buffersize, 
243                                        frame);
244         if (outsize != 0) {
245                 AVPacket packet;
246                 av_init_packet(&packet);
247
248 #ifdef FFMPEG_CODEC_TIME_BASE
249                 packet.pts = av_rescale_q(c->coded_frame->pts,
250                                           c->time_base,
251                                           video_stream->time_base);
252 #else
253                 packet.pts = c->coded_frame->pts;
254 #endif
255                 if (c->coded_frame->key_frame)
256                         packet.flags |= PKT_FLAG_KEY;
257                 packet.stream_index = video_stream->index;
258                 packet.data = video_buffer;
259                 packet.size = outsize;
260                 ret = av_write_frame(outfile, &packet);
261         } else ret = 0;
262         if (ret != 0) {
263                 G.afbreek = 1;
264                 error("Error writing frame");
265         }
266 }
267
268 /* read and encode a frame of audio from the buffer */
269 static AVFrame* generate_video_frame(uint8_t* pixels) 
270 {
271         uint8_t* rendered_frame;
272
273         AVCodecContext* c = get_codec_from_stream(video_stream);
274         int width = c->width;
275         int height = c->height;
276         AVFrame* rgb_frame;
277
278         if (c->pix_fmt != PIX_FMT_RGBA32) {
279                 rgb_frame = alloc_picture(PIX_FMT_RGBA32, width, height);
280                 if (!rgb_frame) {
281                         G.afbreek=1;
282                         error("Couldn't allocate temporary frame");
283                         return NULL;
284                 }
285         } else {
286                 rgb_frame = current_frame;
287         }
288
289         rendered_frame = pixels;
290
291         /* Do RGBA-conversion and flipping in one step depending
292            on CPU-Endianess */
293
294         if (G.order == L_ENDIAN) {
295                 int y;
296                 for (y = 0; y < height; y++) {
297                         uint8_t* target = rgb_frame->data[0]
298                                 + width * 4 * (height - y - 1);
299                         uint8_t* src = rendered_frame + width * 4 * y;
300                         uint8_t* end = src + width * 4;
301                         while (src != end) {
302                                 target[3] = src[3];
303                                 target[2] = src[0];
304                                 target[1] = src[1];
305                                 target[0] = src[2];
306
307                                 target += 4;
308                                 src += 4;
309                         }
310                 }
311         } else {
312                 int y;
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;
318                         while (src != end) {
319                                 target[3] = src[2];
320                                 target[2] = src[1];
321                                 target[1] = src[0];
322                                 target[0] = src[3];
323
324                                 target += 4;
325                                 src += 4;
326                         }
327                 }
328         }
329
330         if (c->pix_fmt != PIX_FMT_RGBA32) {
331                 img_convert((AVPicture*)current_frame, c->pix_fmt, 
332                         (AVPicture*)rgb_frame, PIX_FMT_RGBA32, width, height);
333                 delete_picture(rgb_frame);
334         }
335         return current_frame;
336 }
337
338 /* prepare a video stream for the output file */
339
340 static AVStream* alloc_video_stream(int codec_id, AVFormatContext* of,
341                                     int rectx, int recty) 
342 {
343         AVStream* st;
344         AVCodecContext* c;
345         AVCodec* codec;
346         st = av_new_stream(of, 0);
347         if (!st) return NULL;
348
349         /* Set up the codec context */
350         
351         c = get_codec_from_stream(st);
352         c->codec_id = codec_id;
353         c->codec_type = CODEC_TYPE_VIDEO;
354
355
356         /* Get some values from the current render settings */
357         
358         c->width = rectx;
359         c->height = recty;
360
361 #ifdef FFMPEG_CODEC_TIME_BASE
362         /* FIXME: Really bad hack (tm) for NTSC support */
363         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
364                 c->time_base.den = 2997;
365                 c->time_base.num = 100;
366         } else {
367                 c->time_base.den = G.scene->r.frs_sec;
368                 c->time_base.num = 1;
369         }
370 #else
371         /* FIXME: Really bad hack (tm) for NTSC support */
372         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
373                 c->frame_rate = 2997;
374                 c->frame_rate_base = 100;
375         } else {
376                 c->frame_rate = G.scene->r.frs_sec;
377                 c->frame_rate_base = 1;
378         }
379 #endif
380         
381         c->gop_size = ffmpeg_gop_size;
382         c->bit_rate = ffmpeg_video_bitrate*1000;
383         c->rc_max_rate = G.scene->r.ffcodecdata.rc_max_rate*1000;
384         c->rc_min_rate = G.scene->r.ffcodecdata.rc_min_rate*1000;
385         c->rc_buffer_size = G.scene->r.ffcodecdata.rc_buffer_size * 1024;
386         c->rc_initial_buffer_occupancy 
387                 = G.scene->r.ffcodecdata.rc_buffer_size*3/4;
388         c->rc_buffer_aggressivity = 1.0;
389         c->me_method = ME_EPZS;
390         
391         codec = avcodec_find_encoder(c->codec_id);
392         if (!codec) return NULL;
393         
394         /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
395         
396         if (codec->pix_fmts) {
397                 c->pix_fmt = codec->pix_fmts[0];
398         } else {
399                 /* makes HuffYUV happy ... */
400                 c->pix_fmt = PIX_FMT_YUV422P;
401         }
402         
403         if (!strcmp(of->oformat->name, "mp4") || 
404             !strcmp(of->oformat->name, "mov") ||
405             !strcmp(of->oformat->name, "3gp")) {
406                 fprintf(stderr, "Using global header\n");
407                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
408         }
409         
410         /* Determine whether we are encoding interlaced material or not */
411         if (G.scene->r.mode & (1 << 6)) {
412                 fprintf(stderr, "Encoding interlaced video\n");
413                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
414                 c->flags |= CODEC_FLAG_INTERLACED_ME;
415         }       
416         c->sample_aspect_ratio.num = G.scene->r.xasp;
417         c->sample_aspect_ratio.den = G.scene->r.yasp;
418         
419         if (avcodec_open(c, codec) < 0) {
420                 error("Couldn't initialize codec");
421                 return NULL;
422         }
423
424         video_buffersize = 2000000;
425         video_buffer = (uint8_t*)MEM_mallocN(video_buffersize, 
426                                              "FFMPEG video buffer");
427         
428         current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
429         return st;
430 }
431
432 /* Prepare an audio stream for the output file */
433
434 AVStream* alloc_audio_stream(int codec_id, AVFormatContext* of) 
435 {
436         AVStream* st;
437         AVCodecContext* c;
438         AVCodec* codec;
439
440         st = av_new_stream(of, 1);
441         if (!st) return NULL;
442
443         c = get_codec_from_stream(st);
444         c->codec_id = codec_id;
445         c->codec_type = CODEC_TYPE_AUDIO;
446
447         c->sample_rate = G.scene->audio.mixrate;
448         c->bit_rate = ffmpeg_audio_bitrate*1000;
449         c->channels = 2;
450         codec = avcodec_find_encoder(c->codec_id);
451         if (!codec) {
452                 error("Couldn't find a valid audio codec");
453                 return NULL;
454         }
455         if (avcodec_open(c, codec) < 0) {
456                 error("Couldn't initialize audio codec");
457                 return NULL;
458         }
459
460         /* FIXME: Should be user configurable */
461         audio_outbuf_size = 10000;
462         audio_output_buffer = (uint8_t*)MEM_mallocN(
463                 audio_outbuf_size, "FFMPEG audio encoder input buffer");
464
465        /* ugly hack for PCM codecs */
466
467         if (c->frame_size <= 1) {
468                 audio_input_frame_size = audio_outbuf_size / c->channels;
469                 switch(c->codec_id) {
470                 case CODEC_ID_PCM_S16LE:
471                 case CODEC_ID_PCM_S16BE:
472                 case CODEC_ID_PCM_U16LE:
473                 case CODEC_ID_PCM_U16BE:
474                         audio_input_frame_size >>= 1;
475                         break;
476                 default:
477                         break;
478                 }
479         } else {
480                 audio_input_frame_size = c->frame_size;
481         }
482
483         audio_input_buffer = (uint8_t*)MEM_mallocN(
484                 audio_input_frame_size * sizeof(short) * c->channels, 
485                 "FFMPEG audio encoder output buffer");
486
487         return st;
488 }
489 /* essential functions -- start, append, end */
490
491 void start_ffmpeg_impl(RenderData *rd, int rectx, int recty)
492 {
493         /* Handle to the output file */
494         AVFormatContext* of;
495         AVOutputFormat* fmt;
496
497         ffmpeg_type = rd->ffcodecdata.type;
498         ffmpeg_codec = rd->ffcodecdata.codec;
499         ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
500         ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
501         ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
502         ffmpeg_gop_size = rd->ffcodecdata.gop_size;
503         ffmpeg_multiplex_audio = rd->ffcodecdata.flags
504                 & FFMPEG_MULTIPLEX_AUDIO;
505         ffmpeg_autosplit = rd->ffcodecdata.flags
506                 & FFMPEG_AUTOSPLIT_OUTPUT;
507         
508         do_init_ffmpeg();
509
510         /* Determine the correct filename */
511         char name[256];
512         makeffmpegstring(name);
513         fprintf(stderr, "Starting output to %s(ffmpeg)...\n", name);
514         
515         fmt = guess_format(NULL, name, NULL);
516         if (!fmt) {
517                 G.afbreek = 1; /* Abort render */
518                 error("No vaild formats found");
519                 return;
520         }
521
522         of = av_alloc_format_context();
523         if (!of) {
524                 G.afbreek = 1;
525                 error("Error opening output file");
526                 return;
527         }
528         
529         of->oformat = fmt;
530         of->packet_size= G.scene->r.ffcodecdata.mux_packet_size;
531         if (ffmpeg_multiplex_audio) {
532                 of->mux_rate = G.scene->r.ffcodecdata.mux_rate;
533         } else {
534                 of->mux_rate = 0;
535         }
536
537         of->preload = (int)(0.5*AV_TIME_BASE);
538         of->max_delay = (int)(0.7*AV_TIME_BASE);
539         
540         snprintf(of->filename, sizeof(of->filename), "%s", name);
541         /* set the codec to the user's selection */
542         switch(ffmpeg_type) {
543                 case FFMPEG_AVI:
544                 case FFMPEG_MOV:
545                         fmt->video_codec = ffmpeg_codec;
546                         break;
547                 case FFMPEG_DV:
548                         fmt->video_codec = CODEC_ID_DVVIDEO;
549                         break;
550                 case FFMPEG_MPEG1:
551                         fmt->video_codec = CODEC_ID_MPEG1VIDEO;
552                         break;
553                 case FFMPEG_MPEG2:
554                         fmt->video_codec = CODEC_ID_MPEG2VIDEO;
555                         break;
556                 case FFMPEG_MPEG4:
557                 default:
558                         fmt->video_codec = CODEC_ID_MPEG4;
559                         break;
560         }
561         if (ffmpeg_type == FFMPEG_DV) {
562                 fmt->audio_codec = CODEC_ID_PCM_S16LE;
563         } else {
564                 fmt->audio_codec = ffmpeg_audio_codec;
565         }
566         
567         video_stream = alloc_video_stream(fmt->video_codec, of, rectx, recty);
568         if (!video_stream) {
569                 G.afbreek = 1;
570                 error("Error initializing video stream");
571                 return;
572         }
573         
574         if (ffmpeg_multiplex_audio) {
575                 audio_stream = alloc_audio_stream(fmt->audio_codec, of);
576                 if (!audio_stream) {
577                         G.afbreek = 1;
578                         error("Error initializing audio stream");
579                         return;
580                 }
581                 audiostream_play(SFRA, 0, 1);
582         }
583         if (av_set_parameters(of, NULL) < 0) {
584                 G.afbreek = 1;
585                 error("Error setting output parameters");
586                 return;
587         }
588         if (!(fmt->flags & AVFMT_NOFILE)) {
589                 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
590                         G.afbreek = 1;
591                         error("Could not open file for writing");
592                         return;
593                 }
594         }
595
596         av_write_header(of);
597         outfile = of;
598         dump_format(of, 0, name, 1);
599 }
600
601 void start_ffmpeg(RenderData *rd, int rectx, int recty)
602 {
603         ffmpeg_autosplit_count = 0;
604
605         ffmpeg_renderdata = rd;
606
607         start_ffmpeg_impl(rd, rectx, recty);
608 }
609
610 void end_ffmpeg(void);
611
612 void append_ffmpeg(int frame, int *pixels, int rectx, int recty) 
613 {
614         fprintf(stderr, "Writing frame %i\n", frame);
615         while (ffmpeg_multiplex_audio && 
616                (((double)audio_stream->pts.val 
617                  * audio_stream->time_base.num / audio_stream->time_base.den)
618                 < 
619                 ((double)video_stream->pts.val 
620                  * video_stream->time_base.num / video_stream->time_base.den)
621                        )) {
622                 write_audio_frame();
623         }
624         write_video_frame(generate_video_frame((unsigned char*) pixels));
625
626         if (ffmpeg_autosplit) {
627                 if (url_ftell(&outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
628                         end_ffmpeg();
629                         ffmpeg_autosplit_count++;
630                         start_ffmpeg_impl(ffmpeg_renderdata,
631                                           rectx, recty);
632                 }
633         }
634 }
635
636
637 void end_ffmpeg(void)
638 {
639         int i;
640         
641         fprintf(stderr, "Closing ffmpeg...\n");
642
643         if (outfile) {
644                 av_write_trailer(outfile);
645         }
646         
647         /* Close the video codec */
648
649         if (video_stream && get_codec_from_stream(video_stream)) {
650                 avcodec_close(get_codec_from_stream(video_stream));
651         }
652
653         
654         /* Close the output file */
655         if (outfile) {
656                 for (i = 0; i < outfile->nb_streams; i++) {
657                         if (&outfile->streams[i]) {
658                                 av_freep(&outfile->streams[i]);
659                         }
660                 }
661         }
662         /* free the temp buffer */
663         if (current_frame) {
664                 delete_picture(current_frame);
665         }
666         if (outfile && outfile->oformat) {
667                 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
668                         url_fclose(&outfile->pb);
669                 }
670         }
671         if (outfile) {
672                 av_free(outfile);
673                 outfile = 0;
674         }
675         if (video_buffer) {
676                 MEM_freeN(video_buffer);
677                 video_buffer = 0;
678         }
679         if (audio_output_buffer) {
680                 MEM_freeN(audio_output_buffer);
681                 audio_output_buffer = 0;
682         }
683         if (audio_input_buffer) {
684                 MEM_freeN(audio_input_buffer);
685                 audio_input_buffer = 0;
686         }
687 }
688 #endif
689