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