==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"
515                 "  Using type=%d, codec=%d, audio_codec=%d,\n"
516                 "  video_bitrate=%d, audio_bitrate=%d,\n"
517                 "  gop_size=%d, multiplex=%d, autosplit=%d\n"
518                 "  render width=%d, render height=%d\n", 
519                 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
520                 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
521                 ffmpeg_gop_size, ffmpeg_multiplex_audio,
522                 ffmpeg_autosplit, rectx, recty);
523         
524         fmt = guess_format(NULL, name, NULL);
525         if (!fmt) {
526                 G.afbreek = 1; /* Abort render */
527                 error("No valid formats found");
528                 return;
529         }
530
531         of = av_alloc_format_context();
532         if (!of) {
533                 G.afbreek = 1;
534                 error("Error opening output file");
535                 return;
536         }
537         
538         of->oformat = fmt;
539         of->packet_size= G.scene->r.ffcodecdata.mux_packet_size;
540         if (ffmpeg_multiplex_audio) {
541                 of->mux_rate = G.scene->r.ffcodecdata.mux_rate;
542         } else {
543                 of->mux_rate = 0;
544         }
545
546         of->preload = (int)(0.5*AV_TIME_BASE);
547         of->max_delay = (int)(0.7*AV_TIME_BASE);
548
549         snprintf(of->filename, sizeof(of->filename), "%s", name);
550         /* set the codec to the user's selection */
551         switch(ffmpeg_type) {
552                 case FFMPEG_AVI:
553                 case FFMPEG_MOV:
554                         fmt->video_codec = ffmpeg_codec;
555                         break;
556                 case FFMPEG_DV:
557                         fmt->video_codec = CODEC_ID_DVVIDEO;
558                         break;
559                 case FFMPEG_MPEG1:
560                         fmt->video_codec = CODEC_ID_MPEG1VIDEO;
561                         break;
562                 case FFMPEG_MPEG2:
563                         fmt->video_codec = CODEC_ID_MPEG2VIDEO;
564                         break;
565                 case FFMPEG_MPEG4:
566                 default:
567                         fmt->video_codec = CODEC_ID_MPEG4;
568                         break;
569         }
570         if (fmt->video_codec == CODEC_ID_DVVIDEO) {
571                 if (rectx != 720) {
572                         G.afbreek = 1;
573                         error("Render width has to be 720 pixels for DV!");
574                         return;
575                 }
576                 if (G.scene->r.frs_sec != 25 && recty != 480) {
577                         G.afbreek = 1;
578                         error("Render height has to be 480 pixels "
579                               "for DV-NTSC!");
580                         return;
581                         
582                 }
583                 if (G.scene->r.frs_sec == 25 && recty != 576) {
584                         G.afbreek = 1;
585                         error("Render height has to be 576 pixels "
586                               "for DV-PAL!");
587                         return;
588                 }
589         }
590         if (ffmpeg_type == FFMPEG_DV) {
591                 fmt->audio_codec = CODEC_ID_PCM_S16LE;
592                 if (ffmpeg_multiplex_audio 
593                     && G.scene->audio.mixrate != 48000) {
594                         G.afbreek = 1;
595                         error("FFMPEG only supports 48khz / stereo "
596                               "audio for DV!");
597                         return;
598                 }
599         }
600         
601         video_stream = alloc_video_stream(fmt->video_codec, of, rectx, recty);
602         if (!video_stream) {
603                 G.afbreek = 1;
604                 error("Error initializing video stream");
605                 return;
606         }
607         
608         if (ffmpeg_multiplex_audio) {
609                 audio_stream = alloc_audio_stream(fmt->audio_codec, of);
610                 if (!audio_stream) {
611                         G.afbreek = 1;
612                         error("Error initializing audio stream");
613                         return;
614                 }
615                 audiostream_play(SFRA, 0, 1);
616         }
617         if (av_set_parameters(of, NULL) < 0) {
618                 G.afbreek = 1;
619                 error("Error setting output parameters");
620                 return;
621         }
622         if (!(fmt->flags & AVFMT_NOFILE)) {
623                 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
624                         G.afbreek = 1;
625                         error("Could not open file for writing");
626                         return;
627                 }
628         }
629
630         av_write_header(of);
631         outfile = of;
632         dump_format(of, 0, name, 1);
633 }
634
635 void start_ffmpeg(RenderData *rd, int rectx, int recty)
636 {
637         ffmpeg_autosplit_count = 0;
638
639         ffmpeg_renderdata = rd;
640
641         start_ffmpeg_impl(rd, rectx, recty);
642 }
643
644 void end_ffmpeg(void);
645
646 void append_ffmpeg(int frame, int *pixels, int rectx, int recty) 
647 {
648         fprintf(stderr, "Writing frame %i, "
649                 "render width=%d, render height=%d\n", frame,
650                 rectx, recty);
651         while (ffmpeg_multiplex_audio && 
652                (((double)audio_stream->pts.val 
653                  * audio_stream->time_base.num / audio_stream->time_base.den)
654                 < 
655                 ((double)video_stream->pts.val 
656                  * video_stream->time_base.num / video_stream->time_base.den)
657                        )) {
658                 write_audio_frame();
659         }
660         write_video_frame(generate_video_frame((unsigned char*) pixels));
661
662         if (ffmpeg_autosplit) {
663                 if (url_ftell(&outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
664                         end_ffmpeg();
665                         ffmpeg_autosplit_count++;
666                         start_ffmpeg_impl(ffmpeg_renderdata,
667                                           rectx, recty);
668                 }
669         }
670 }
671
672
673 void end_ffmpeg(void)
674 {
675         int i;
676         
677         fprintf(stderr, "Closing ffmpeg...\n");
678
679         if (outfile) {
680                 av_write_trailer(outfile);
681         }
682         
683         /* Close the video codec */
684
685         if (video_stream && get_codec_from_stream(video_stream)) {
686                 avcodec_close(get_codec_from_stream(video_stream));
687                 video_stream = 0;
688         }
689
690         
691         /* Close the output file */
692         if (outfile) {
693                 for (i = 0; i < outfile->nb_streams; i++) {
694                         if (&outfile->streams[i]) {
695                                 av_freep(&outfile->streams[i]);
696                         }
697                 }
698         }
699         /* free the temp buffer */
700         if (current_frame) {
701                 delete_picture(current_frame);
702                 current_frame = 0;
703         }
704         if (outfile && outfile->oformat) {
705                 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
706                         url_fclose(&outfile->pb);
707                 }
708         }
709         if (outfile) {
710                 av_free(outfile);
711                 outfile = 0;
712         }
713         if (video_buffer) {
714                 MEM_freeN(video_buffer);
715                 video_buffer = 0;
716         }
717         if (audio_output_buffer) {
718                 MEM_freeN(audio_output_buffer);
719                 audio_output_buffer = 0;
720         }
721         if (audio_input_buffer) {
722                 MEM_freeN(audio_input_buffer);
723                 audio_input_buffer = 0;
724         }
725 }
726 #endif
727