aa4cb00e21f46805d57ab5b63fe26f31fc33ce86
[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                 case FFMPEG_H264:
185                         /* FIXME: avi for now... */
186                         f = guess_format("avi", NULL, NULL);
187                         break;
188                 case FFMPEG_XVID:
189                         /* FIXME: avi for now... */
190                         f = guess_format("avi", NULL, NULL);
191                         break;
192                 default:
193                         f = NULL;
194         }
195         return f;
196 }
197
198 /* Get the correct file extension for the requested format */
199 void file_extension(int format, char* string) 
200 {
201         unsigned int i;
202         AVOutputFormat* f;
203         const char* ext;
204         f= ffmpeg_get_format(format);
205         ext = f->extensions;
206         for (i = 0; ext[i] != ',' && i < strlen(ext); i++);
207         snprintf(string, i+2, ".%s", ext);
208 }
209
210 /* Get the output filename-- similar to the other output formats */
211 void makeffmpegstring(char* string) {
212         
213         char txt[FILE_MAXDIR+FILE_MAXFILE];
214         char fe[FILE_MAXDIR+FILE_MAXFILE];
215         char autosplit[20];
216
217         file_extension(ffmpeg_type, fe);
218
219         if (!string) return;
220
221         strcpy(string, G.scene->r.pic);
222         BLI_convertstringcode(string, G.sce, G.scene->r.cfra);
223
224         BLI_make_existing_file(string);
225
226         autosplit[0] = 0;
227
228         if (ffmpeg_autosplit) {
229                 sprintf(autosplit, "_%03d", ffmpeg_autosplit_count);
230         }
231
232         if (BLI_strcasecmp(string + strlen(string) - strlen(fe), fe)) {
233                 strcat(string, autosplit);
234                 sprintf(txt, "%04d_%04d%s", (G.scene->r.sfra), 
235                         (G.scene->r.efra), fe);
236                 strcat(string, txt);
237         } else {
238                 *(string + strlen(string) - strlen(fe)) = 0;
239                 strcat(string, autosplit);
240                 strcat(string, fe);
241         }
242 }
243
244 /* Write a frame to the output file */
245 static void write_video_frame(AVFrame* frame) {
246         int outsize = 0;
247         int ret;
248         AVCodecContext* c = get_codec_from_stream(video_stream);
249 #ifdef FFMPEG_CODEC_TIME_BASE
250         frame->pts = G.scene->r.cfra - G.scene->r.sfra;
251 #endif
252
253         outsize = avcodec_encode_video(c, video_buffer, video_buffersize, 
254                                        frame);
255         if (outsize != 0) {
256                 AVPacket packet;
257                 av_init_packet(&packet);
258
259 #ifdef FFMPEG_CODEC_TIME_BASE
260                 packet.pts = av_rescale_q(c->coded_frame->pts,
261                                           c->time_base,
262                                           video_stream->time_base);
263 #else
264                 packet.pts = c->coded_frame->pts;
265 #endif
266                 if (c->coded_frame->key_frame)
267                         packet.flags |= PKT_FLAG_KEY;
268                 packet.stream_index = video_stream->index;
269                 packet.data = video_buffer;
270                 packet.size = outsize;
271                 ret = av_write_frame(outfile, &packet);
272         } else ret = 0;
273         if (ret != 0) {
274                 G.afbreek = 1;
275                 error("Error writing frame");
276         }
277 }
278
279 /* read and encode a frame of audio from the buffer */
280 static AVFrame* generate_video_frame(uint8_t* pixels) 
281 {
282         uint8_t* rendered_frame;
283
284         AVCodecContext* c = get_codec_from_stream(video_stream);
285         int width = c->width;
286         int height = c->height;
287         AVFrame* rgb_frame;
288
289         if (c->pix_fmt != PIX_FMT_RGBA32) {
290                 rgb_frame = alloc_picture(PIX_FMT_RGBA32, width, height);
291                 if (!rgb_frame) {
292                         G.afbreek=1;
293                         error("Couldn't allocate temporary frame");
294                         return NULL;
295                 }
296         } else {
297                 rgb_frame = current_frame;
298         }
299
300         rendered_frame = pixels;
301
302         /* Do RGBA-conversion and flipping in one step depending
303            on CPU-Endianess */
304
305         if (G.order == L_ENDIAN) {
306                 int y;
307                 for (y = 0; y < height; y++) {
308                         uint8_t* target = rgb_frame->data[0]
309                                 + width * 4 * (height - y - 1);
310                         uint8_t* src = rendered_frame + width * 4 * y;
311                         uint8_t* end = src + width * 4;
312                         while (src != end) {
313                                 target[3] = src[3];
314                                 target[2] = src[0];
315                                 target[1] = src[1];
316                                 target[0] = src[2];
317
318                                 target += 4;
319                                 src += 4;
320                         }
321                 }
322         } else {
323                 int y;
324                 for (y = 0; y < height; y++) {
325                         uint8_t* target = rgb_frame->data[0]
326                                 + width * 4 * (height - y - 1);
327                         uint8_t* src = rendered_frame + width * 4 * y;
328                         uint8_t* end = src + width * 4;
329                         while (src != end) {
330                                 target[3] = src[2];
331                                 target[2] = src[1];
332                                 target[1] = src[0];
333                                 target[0] = src[3];
334
335                                 target += 4;
336                                 src += 4;
337                         }
338                 }
339         }
340
341         if (c->pix_fmt != PIX_FMT_RGBA32) {
342                 img_convert((AVPicture*)current_frame, c->pix_fmt, 
343                         (AVPicture*)rgb_frame, PIX_FMT_RGBA32, width, height);
344                 delete_picture(rgb_frame);
345         }
346         return current_frame;
347 }
348
349 /* prepare a video stream for the output file */
350
351 static AVStream* alloc_video_stream(int codec_id, AVFormatContext* of,
352                                     int rectx, int recty) 
353 {
354         AVStream* st;
355         AVCodecContext* c;
356         AVCodec* codec;
357         st = av_new_stream(of, 0);
358         if (!st) return NULL;
359
360         /* Set up the codec context */
361         
362         c = get_codec_from_stream(st);
363         c->codec_id = codec_id;
364         c->codec_type = CODEC_TYPE_VIDEO;
365
366
367         /* Get some values from the current render settings */
368         
369         c->width = rectx;
370         c->height = recty;
371
372 #ifdef FFMPEG_CODEC_TIME_BASE
373         /* FIXME: Really bad hack (tm) for NTSC support */
374         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
375                 c->time_base.den = 2997;
376                 c->time_base.num = 100;
377         } else {
378                 c->time_base.den = G.scene->r.frs_sec;
379                 c->time_base.num = 1;
380         }
381 #else
382         /* FIXME: Really bad hack (tm) for NTSC support */
383         if (ffmpeg_type == FFMPEG_DV && G.scene->r.frs_sec != 25) {
384                 c->frame_rate = 2997;
385                 c->frame_rate_base = 100;
386         } else {
387                 c->frame_rate = G.scene->r.frs_sec;
388                 c->frame_rate_base = 1;
389         }
390 #endif
391         
392         c->gop_size = ffmpeg_gop_size;
393         c->bit_rate = ffmpeg_video_bitrate*1000;
394         c->rc_max_rate = G.scene->r.ffcodecdata.rc_max_rate*1000;
395         c->rc_min_rate = G.scene->r.ffcodecdata.rc_min_rate*1000;
396         c->rc_buffer_size = G.scene->r.ffcodecdata.rc_buffer_size * 1024;
397         c->rc_initial_buffer_occupancy 
398                 = G.scene->r.ffcodecdata.rc_buffer_size*3/4;
399         c->rc_buffer_aggressivity = 1.0;
400         c->me_method = ME_EPZS;
401         
402         codec = avcodec_find_encoder(c->codec_id);
403         if (!codec) return NULL;
404         
405         /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
406         
407         if (codec->pix_fmts) {
408                 c->pix_fmt = codec->pix_fmts[0];
409         } else {
410                 /* makes HuffYUV happy ... */
411                 c->pix_fmt = PIX_FMT_YUV422P;
412         }
413
414         if (codec_id == CODEC_ID_XVID) {
415                 /* arghhhh ... */
416                 c->pix_fmt = PIX_FMT_YUV420P;
417         }
418         
419         if (!strcmp(of->oformat->name, "mp4") || 
420             !strcmp(of->oformat->name, "mov") ||
421             !strcmp(of->oformat->name, "3gp")) {
422                 fprintf(stderr, "Using global header\n");
423                 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
424         }
425         
426         /* Determine whether we are encoding interlaced material or not */
427         if (G.scene->r.mode & (1 << 6)) {
428                 fprintf(stderr, "Encoding interlaced video\n");
429                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
430                 c->flags |= CODEC_FLAG_INTERLACED_ME;
431         }       
432         c->sample_aspect_ratio.num = G.scene->r.xasp;
433         c->sample_aspect_ratio.den = G.scene->r.yasp;
434         
435         if (avcodec_open(c, codec) < 0) {
436                 error("Couldn't initialize codec");
437                 return NULL;
438         }
439
440         video_buffersize = 2000000;
441         video_buffer = (uint8_t*)MEM_mallocN(video_buffersize, 
442                                              "FFMPEG video buffer");
443         
444         current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
445         return st;
446 }
447
448 /* Prepare an audio stream for the output file */
449
450 AVStream* alloc_audio_stream(int codec_id, AVFormatContext* of) 
451 {
452         AVStream* st;
453         AVCodecContext* c;
454         AVCodec* codec;
455
456         st = av_new_stream(of, 1);
457         if (!st) return NULL;
458
459         c = get_codec_from_stream(st);
460         c->codec_id = codec_id;
461         c->codec_type = CODEC_TYPE_AUDIO;
462
463         c->sample_rate = G.scene->audio.mixrate;
464         c->bit_rate = ffmpeg_audio_bitrate*1000;
465         c->channels = 2;
466         codec = avcodec_find_encoder(c->codec_id);
467         if (!codec) {
468                 error("Couldn't find a valid audio codec");
469                 return NULL;
470         }
471         if (avcodec_open(c, codec) < 0) {
472                 error("Couldn't initialize audio codec");
473                 return NULL;
474         }
475
476         /* FIXME: Should be user configurable */
477         audio_outbuf_size = 10000;
478         audio_output_buffer = (uint8_t*)MEM_mallocN(
479                 audio_outbuf_size, "FFMPEG audio encoder input buffer");
480
481        /* ugly hack for PCM codecs */
482
483         if (c->frame_size <= 1) {
484                 audio_input_frame_size = audio_outbuf_size / c->channels;
485                 switch(c->codec_id) {
486                 case CODEC_ID_PCM_S16LE:
487                 case CODEC_ID_PCM_S16BE:
488                 case CODEC_ID_PCM_U16LE:
489                 case CODEC_ID_PCM_U16BE:
490                         audio_input_frame_size >>= 1;
491                         break;
492                 default:
493                         break;
494                 }
495         } else {
496                 audio_input_frame_size = c->frame_size;
497         }
498
499         audio_input_buffer = (uint8_t*)MEM_mallocN(
500                 audio_input_frame_size * sizeof(short) * c->channels, 
501                 "FFMPEG audio encoder output buffer");
502
503         return st;
504 }
505 /* essential functions -- start, append, end */
506
507 void start_ffmpeg_impl(RenderData *rd, int rectx, int recty)
508 {
509         /* Handle to the output file */
510         AVFormatContext* of;
511         AVOutputFormat* fmt;
512         char name[256];
513
514         ffmpeg_type = rd->ffcodecdata.type;
515         ffmpeg_codec = rd->ffcodecdata.codec;
516         ffmpeg_audio_codec = rd->ffcodecdata.audio_codec;
517         ffmpeg_video_bitrate = rd->ffcodecdata.video_bitrate;
518         ffmpeg_audio_bitrate = rd->ffcodecdata.audio_bitrate;
519         ffmpeg_gop_size = rd->ffcodecdata.gop_size;
520         ffmpeg_multiplex_audio = rd->ffcodecdata.flags
521                 & FFMPEG_MULTIPLEX_AUDIO;
522         ffmpeg_autosplit = rd->ffcodecdata.flags
523                 & FFMPEG_AUTOSPLIT_OUTPUT;
524         
525         do_init_ffmpeg();
526
527         /* Determine the correct filename */
528         makeffmpegstring(name);
529         fprintf(stderr, "Starting output to %s(ffmpeg)...\n"
530                 "  Using type=%d, codec=%d, audio_codec=%d,\n"
531                 "  video_bitrate=%d, audio_bitrate=%d,\n"
532                 "  gop_size=%d, multiplex=%d, autosplit=%d\n"
533                 "  render width=%d, render height=%d\n", 
534                 name, ffmpeg_type, ffmpeg_codec, ffmpeg_audio_codec,
535                 ffmpeg_video_bitrate, ffmpeg_audio_bitrate,
536                 ffmpeg_gop_size, ffmpeg_multiplex_audio,
537                 ffmpeg_autosplit, rectx, recty);
538         
539         fmt = guess_format(NULL, name, NULL);
540         if (!fmt) {
541                 G.afbreek = 1; /* Abort render */
542                 error("No valid formats found");
543                 return;
544         }
545
546         of = av_alloc_format_context();
547         if (!of) {
548                 G.afbreek = 1;
549                 error("Error opening output file");
550                 return;
551         }
552         
553         of->oformat = fmt;
554         of->packet_size= G.scene->r.ffcodecdata.mux_packet_size;
555         if (ffmpeg_multiplex_audio) {
556                 of->mux_rate = G.scene->r.ffcodecdata.mux_rate;
557         } else {
558                 of->mux_rate = 0;
559         }
560
561         of->preload = (int)(0.5*AV_TIME_BASE);
562         of->max_delay = (int)(0.7*AV_TIME_BASE);
563
564         snprintf(of->filename, sizeof(of->filename), "%s", name);
565         /* set the codec to the user's selection */
566         switch(ffmpeg_type) {
567         case FFMPEG_AVI:
568         case FFMPEG_MOV:
569                 fmt->video_codec = ffmpeg_codec;
570                 break;
571         case FFMPEG_DV:
572                 fmt->video_codec = CODEC_ID_DVVIDEO;
573                 break;
574         case FFMPEG_MPEG1:
575                 fmt->video_codec = CODEC_ID_MPEG1VIDEO;
576                 break;
577         case FFMPEG_MPEG2:
578                 fmt->video_codec = CODEC_ID_MPEG2VIDEO;
579                 break;
580         case FFMPEG_H264:
581                 fmt->video_codec = CODEC_ID_H264;
582                 break;
583         case FFMPEG_XVID:
584                 fmt->video_codec = CODEC_ID_XVID;
585                 break;
586         case FFMPEG_MPEG4:
587         default:
588                 fmt->video_codec = CODEC_ID_MPEG4;
589                 break;
590         }
591         if (fmt->video_codec == CODEC_ID_DVVIDEO) {
592                 if (rectx != 720) {
593                         G.afbreek = 1;
594                         error("Render width has to be 720 pixels for DV!");
595                         return;
596                 }
597                 if (G.scene->r.frs_sec != 25 && recty != 480) {
598                         G.afbreek = 1;
599                         error("Render height has to be 480 pixels "
600                               "for DV-NTSC!");
601                         return;
602                         
603                 }
604                 if (G.scene->r.frs_sec == 25 && recty != 576) {
605                         G.afbreek = 1;
606                         error("Render height has to be 576 pixels "
607                               "for DV-PAL!");
608                         return;
609                 }
610         }
611         if (ffmpeg_type == FFMPEG_DV) {
612                 fmt->audio_codec = CODEC_ID_PCM_S16LE;
613                 if (ffmpeg_multiplex_audio 
614                     && G.scene->audio.mixrate != 48000) {
615                         G.afbreek = 1;
616                         error("FFMPEG only supports 48khz / stereo "
617                               "audio for DV!");
618                         return;
619                 }
620         }
621         
622         video_stream = alloc_video_stream(fmt->video_codec, of, rectx, recty);
623         if (!video_stream) {
624                 G.afbreek = 1;
625                 error("Error initializing video stream");
626                 return;
627         }
628         
629         if (ffmpeg_multiplex_audio) {
630                 audio_stream = alloc_audio_stream(fmt->audio_codec, of);
631                 if (!audio_stream) {
632                         G.afbreek = 1;
633                         error("Error initializing audio stream");
634                         return;
635                 }
636                 audiostream_play(SFRA, 0, 1);
637         }
638         if (av_set_parameters(of, NULL) < 0) {
639                 G.afbreek = 1;
640                 error("Error setting output parameters");
641                 return;
642         }
643         if (!(fmt->flags & AVFMT_NOFILE)) {
644                 if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
645                         G.afbreek = 1;
646                         error("Could not open file for writing");
647                         return;
648                 }
649         }
650
651         av_write_header(of);
652         outfile = of;
653         dump_format(of, 0, name, 1);
654 }
655
656 void start_ffmpeg(RenderData *rd, int rectx, int recty)
657 {
658         ffmpeg_autosplit_count = 0;
659
660         ffmpeg_renderdata = rd;
661
662         start_ffmpeg_impl(rd, rectx, recty);
663 }
664
665 void end_ffmpeg(void);
666
667 void append_ffmpeg(int frame, int *pixels, int rectx, int recty) 
668 {
669         fprintf(stderr, "Writing frame %i, "
670                 "render width=%d, render height=%d\n", frame,
671                 rectx, recty);
672         while (ffmpeg_multiplex_audio && 
673                (((double)audio_stream->pts.val 
674                  * audio_stream->time_base.num / audio_stream->time_base.den)
675                 < 
676                 ((double)video_stream->pts.val 
677                  * video_stream->time_base.num / video_stream->time_base.den)
678                        )) {
679                 write_audio_frame();
680         }
681         write_video_frame(generate_video_frame((unsigned char*) pixels));
682
683         if (ffmpeg_autosplit) {
684                 if (url_ftell(&outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
685                         end_ffmpeg();
686                         ffmpeg_autosplit_count++;
687                         start_ffmpeg_impl(ffmpeg_renderdata,
688                                           rectx, recty);
689                 }
690         }
691 }
692
693
694 void end_ffmpeg(void)
695 {
696         int i;
697         
698         fprintf(stderr, "Closing ffmpeg...\n");
699
700         if (outfile) {
701                 av_write_trailer(outfile);
702         }
703         
704         /* Close the video codec */
705
706         if (video_stream && get_codec_from_stream(video_stream)) {
707                 avcodec_close(get_codec_from_stream(video_stream));
708                 video_stream = 0;
709         }
710
711         
712         /* Close the output file */
713         if (outfile) {
714                 for (i = 0; i < outfile->nb_streams; i++) {
715                         if (&outfile->streams[i]) {
716                                 av_freep(&outfile->streams[i]);
717                         }
718                 }
719         }
720         /* free the temp buffer */
721         if (current_frame) {
722                 delete_picture(current_frame);
723                 current_frame = 0;
724         }
725         if (outfile && outfile->oformat) {
726                 if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
727                         url_fclose(&outfile->pb);
728                 }
729         }
730         if (outfile) {
731                 av_free(outfile);
732                 outfile = 0;
733         }
734         if (video_buffer) {
735                 MEM_freeN(video_buffer);
736                 video_buffer = 0;
737         }
738         if (audio_output_buffer) {
739                 MEM_freeN(audio_output_buffer);
740                 audio_output_buffer = 0;
741         }
742         if (audio_input_buffer) {
743                 MEM_freeN(audio_input_buffer);
744                 audio_input_buffer = 0;
745         }
746 }
747 #endif
748