~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/writeffmpeg.c

  • Committer: Bazaar Package Importer
  • Author(s): Kevin Roy
  • Date: 2011-06-24 11:13:28 UTC
  • mto: (14.1.6 experimental) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20110624111328-27ribg6l36edf2ay
Tags: upstream-2.58-svn37702
ImportĀ upstreamĀ versionĀ 2.58-svn37702

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *  \ingroup bke
3
3
 */
4
4
/*
5
 
 * $Id: writeffmpeg.c 35247 2011-02-27 20:40:57Z jesterking $
 
5
 * $Id: writeffmpeg.c 36960 2011-05-27 23:33:40Z schlaile $
6
6
 *
7
7
 * ffmpeg-write support
8
8
 *
39
39
#include <libswscale/swscale.h>
40
40
#include <libavcodec/opt.h>
41
41
 
42
 
#if LIBAVFORMAT_VERSION_INT < (49 << 16)
43
 
#define FFMPEG_OLD_FRAME_RATE 1
44
 
#else
45
 
#define FFMPEG_CODEC_IS_POINTER 1
46
 
#define FFMPEG_CODEC_TIME_BASE  1
47
 
#endif
48
 
 
49
 
#if LIBAVFORMAT_VERSION_INT >= (52 << 16)
50
 
#define OUTFILE_PB (outfile->pb)
51
 
#else
52
 
#define OUTFILE_PB (&outfile->pb)
53
 
#endif
54
 
 
55
42
#if defined(WIN32) && (!(defined snprintf))
56
43
#define snprintf _snprintf
57
44
#endif
74
61
#include "IMB_imbuf_types.h"
75
62
#include "IMB_imbuf.h"
76
63
 
 
64
#include "ffmpeg_compat.h"
 
65
 
77
66
extern void do_init_ffmpeg(void);
78
67
 
79
68
static int ffmpeg_type = 0;
114
103
        }
115
104
}
116
105
 
117
 
#ifdef FFMPEG_CODEC_IS_POINTER
118
 
static AVCodecContext* get_codec_from_stream(AVStream* stream)
119
 
{
120
 
        return stream->codec;
121
 
}
122
 
#else
123
 
static AVCodecContext* get_codec_from_stream(AVStream* stream)
124
 
{
125
 
        return &stream->codec;
126
 
}
127
 
#endif
128
 
 
129
106
static int write_audio_frame(void) 
130
107
{
131
108
        AVCodecContext* c = NULL;
132
109
        AVPacket pkt;
133
110
 
134
 
        c = get_codec_from_stream(audio_stream);
 
111
        c = audio_stream->codec;
135
112
 
136
113
        av_init_packet(&pkt);
137
114
        pkt.size = 0;
153
130
 
154
131
        if(c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
155
132
        {
156
 
#ifdef FFMPEG_CODEC_TIME_BASE
157
133
                pkt.pts = av_rescale_q(c->coded_frame->pts,
158
 
                                           c->time_base, audio_stream->time_base);
159
 
#else
160
 
                pkt.pts = c->coded_frame->pts;
161
 
#endif
 
134
                                       c->time_base, audio_stream->time_base);
162
135
                fprintf(stderr, "Audio Frame PTS: %d\n", (int)pkt.pts);
163
136
        }
164
137
 
165
138
        pkt.stream_index = audio_stream->index;
166
 
        pkt.flags |= PKT_FLAG_KEY;
 
139
 
 
140
        pkt.flags |= AV_PKT_FLAG_KEY;
 
141
 
167
142
        if (av_interleaved_write_frame(outfile, &pkt) != 0) {
168
143
                fprintf(stderr, "Error writing audio packet!\n");
169
144
                return -1;
263
238
{
264
239
        int outsize = 0;
265
240
        int ret, success= 1;
266
 
        AVCodecContext* c = get_codec_from_stream(video_stream);
267
 
#ifdef FFMPEG_CODEC_TIME_BASE
 
241
        AVCodecContext* c = video_stream->codec;
 
242
 
268
243
        frame->pts = rd->cfra - rd->sfra;
269
 
#endif
 
244
 
270
245
        if (rd->mode & R_FIELDS) {
271
246
                frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
272
247
        }
278
253
                av_init_packet(&packet);
279
254
 
280
255
                if (c->coded_frame->pts != AV_NOPTS_VALUE) {
281
 
#ifdef FFMPEG_CODEC_TIME_BASE
282
256
                        packet.pts = av_rescale_q(c->coded_frame->pts,
283
257
                                                  c->time_base,
284
258
                                                  video_stream->time_base);
285
 
#else
286
 
                        packet.pts = c->coded_frame->pts;
287
 
#endif
288
259
                        fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
289
260
                } else {
290
261
                        fprintf(stderr, "Video Frame PTS: not set\n");
291
262
                }
292
263
                if (c->coded_frame->key_frame)
293
 
                        packet.flags |= PKT_FLAG_KEY;
 
264
                        packet.flags |= AV_PKT_FLAG_KEY;
294
265
                packet.stream_index = video_stream->index;
295
266
                packet.data = video_buffer;
296
267
                packet.size = outsize;
312
283
{
313
284
        uint8_t* rendered_frame;
314
285
 
315
 
        AVCodecContext* c = get_codec_from_stream(video_stream);
 
286
        AVCodecContext* c = video_stream->codec;
316
287
        int width = c->width;
317
288
        int height = c->height;
318
289
        AVFrame* rgb_frame;
396
367
        switch(prop->type) {
397
368
        case IDP_STRING:
398
369
                fprintf(stderr, "%s.\n", IDP_String(prop));
399
 
                rv = av_set_string(c, prop->name, IDP_String(prop));
 
370
                av_set_string3(c, prop->name, IDP_String(prop), 1, &rv);
400
371
                break;
401
372
        case IDP_FLOAT:
402
373
                fprintf(stderr, "%g.\n", IDP_Float(prop));
407
378
                
408
379
                if (param) {
409
380
                        if (IDP_Int(prop)) {
410
 
                                rv = av_set_string(c, name, param);
 
381
                                av_set_string3(c, name, param, 1, &rv);
411
382
                        } else {
412
383
                                return;
413
384
                        }
459
430
 
460
431
        /* Set up the codec context */
461
432
        
462
 
        c = get_codec_from_stream(st);
 
433
        c = st->codec;
463
434
        c->codec_id = codec_id;
464
 
        c->codec_type = CODEC_TYPE_VIDEO;
 
435
        c->codec_type = AVMEDIA_TYPE_VIDEO;
465
436
 
466
437
 
467
438
        /* Get some values from the current render settings */
469
440
        c->width = rectx;
470
441
        c->height = recty;
471
442
 
472
 
#ifdef FFMPEG_CODEC_TIME_BASE
473
443
        /* FIXME: Really bad hack (tm) for NTSC support */
474
444
        if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
475
445
                c->time_base.den = 2997;
482
452
                c->time_base.den = rd->frs_sec * 100000;
483
453
                c->time_base.num = ((double) rd->frs_sec_base) * 100000;
484
454
        }
485
 
#else
486
 
        /* FIXME: Really bad hack (tm) for NTSC support */
487
 
        if (ffmpeg_type == FFMPEG_DV && rd->frs_sec != 25) {
488
 
                c->frame_rate = 2997;
489
 
                c->frame_rate_base = 100;
490
 
        } else if ((double) ((int) rd->frs_sec_base) == 
491
 
                   rd->frs_sec_base) {
492
 
                c->frame_rate = rd->frs_sec;
493
 
                c->frame_rate_base = rd->frs_sec_base;
494
 
        } else {
495
 
                c->frame_rate = rd->frs_sec * 100000;
496
 
                c->frame_rate_base = ((double) rd->frs_sec_base)*100000;
497
 
        }
498
 
#endif
499
455
        
500
456
        c->gop_size = ffmpeg_gop_size;
501
457
        c->bit_rate = ffmpeg_video_bitrate*1000;
519
475
                c->pix_fmt = PIX_FMT_YUV422P;
520
476
        }
521
477
 
522
 
        if (codec_id == CODEC_ID_XVID) {
 
478
        if (ffmpeg_type == FFMPEG_XVID) {
523
479
                /* arghhhh ... */
524
480
                c->pix_fmt = PIX_FMT_YUV420P;
525
481
                c->codec_tag = (('D'<<24) + ('I'<<16) + ('V'<<8) + 'X');
586
542
        st = av_new_stream(of, 1);
587
543
        if (!st) return NULL;
588
544
 
589
 
        c = get_codec_from_stream(st);
 
545
        c = st->codec;
590
546
        c->codec_id = codec_id;
591
 
        c->codec_type = CODEC_TYPE_AUDIO;
 
547
        c->codec_type = AVMEDIA_TYPE_AUDIO;
592
548
 
593
549
        c->sample_rate = rd->ffcodecdata.audio_mixrate;
594
550
        c->bit_rate = ffmpeg_audio_bitrate*1000;
666
622
                BKE_report(reports, RPT_ERROR, "No valid formats found.");
667
623
                return 0;
668
624
        }
669
 
        fmt = guess_format(NULL, exts[0], NULL);
 
625
        fmt = av_guess_format(NULL, exts[0], NULL);
670
626
        if (!fmt) {
671
627
                BKE_report(reports, RPT_ERROR, "No valid formats found.");
672
628
                return 0;
673
629
        }
674
630
 
675
 
        of = av_alloc_format_context();
 
631
        of = avformat_alloc_context();
676
632
        if (!of) {
677
633
                BKE_report(reports, RPT_ERROR, "Error opening output file");
678
634
                return 0;
713
669
                fmt->video_codec = CODEC_ID_H264;
714
670
                break;
715
671
        case FFMPEG_XVID:
716
 
                fmt->video_codec = CODEC_ID_XVID;
 
672
                fmt->video_codec = CODEC_ID_MPEG4;
717
673
                break;
718
674
        case FFMPEG_FLV:
719
675
                fmt->video_codec = CODEC_ID_FLV1;
772
728
                return 0;
773
729
        }
774
730
        if (!(fmt->flags & AVFMT_NOFILE)) {
775
 
                if (url_fopen(&of->pb, name, URL_WRONLY) < 0) {
 
731
                if (avio_open(&of->pb, name, AVIO_FLAG_WRITE) < 0) {
776
732
                        BKE_report(reports, RPT_ERROR, "Could not open file for writing.");
777
733
                        return 0;
778
734
                }
780
736
 
781
737
        av_write_header(of);
782
738
        outfile = of;
783
 
        dump_format(of, 0, name, 1);
 
739
        av_dump_format(of, 0, name, 1);
784
740
 
785
741
        return 1;
786
742
}
807
763
        int outsize = 0;
808
764
        int ret = 0;
809
765
        
810
 
        AVCodecContext* c = get_codec_from_stream(video_stream);
 
766
        AVCodecContext* c = video_stream->codec;
811
767
        /* get the delayed frames */
812
768
        while (1) {
813
769
                AVPacket packet;
822
778
                        break;
823
779
                }
824
780
                if (c->coded_frame->pts != AV_NOPTS_VALUE) {
825
 
#ifdef FFMPEG_CODEC_TIME_BASE
826
781
                        packet.pts = av_rescale_q(c->coded_frame->pts,
827
782
                                                  c->time_base,
828
783
                                                  video_stream->time_base);
829
 
#else
830
 
                        packet.pts = c->coded_frame->pts;
831
 
#endif
832
784
                        fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
833
785
                } else {
834
786
                        fprintf(stderr, "Video Frame PTS: not set\n");
835
787
                }
836
788
                if (c->coded_frame->key_frame) {
837
 
                        packet.flags |= PKT_FLAG_KEY;
 
789
                        packet.flags |= AV_PKT_FLAG_KEY;
838
790
                }
839
791
                packet.stream_index = video_stream->index;
840
792
                packet.data = video_buffer;
845
797
                        break;
846
798
                }
847
799
        }
848
 
        avcodec_flush_buffers(get_codec_from_stream(video_stream));
 
800
        avcodec_flush_buffers(video_stream->codec);
849
801
}
850
802
 
851
803
/* **********************************************************************
902
854
 
903
855
        if(audio_stream)
904
856
        {
905
 
                AVCodecContext* c = get_codec_from_stream(audio_stream);
 
857
                AVCodecContext* c = audio_stream->codec;
906
858
                AUD_DeviceSpecs specs;
907
859
                specs.channels = c->channels;
908
860
                specs.format = AUD_FORMAT_S16;
945
897
                success= (avframe && write_video_frame(rd, avframe, reports));
946
898
 
947
899
                if (ffmpeg_autosplit) {
948
 
                        if (url_ftell(OUTFILE_PB) > FFMPEG_AUTOSPLIT_SIZE) {
 
900
                        if (avio_tell(outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
949
901
                                end_ffmpeg();
950
902
                                ffmpeg_autosplit_count++;
951
903
                                success &= start_ffmpeg_impl(rd, rectx, recty, reports);
974
926
                audio_mixdown_device = 0;
975
927
        }
976
928
        
977
 
        if (video_stream && get_codec_from_stream(video_stream)) {
 
929
        if (video_stream && video_stream->codec) {
978
930
                fprintf(stderr, "Flushing delayed frames...\n");
979
931
                flush_ffmpeg ();                
980
932
        }
985
937
        
986
938
        /* Close the video codec */
987
939
 
988
 
        if (video_stream && get_codec_from_stream(video_stream)) {
989
 
                avcodec_close(get_codec_from_stream(video_stream));
 
940
        if (video_stream && video_stream->codec) {
 
941
                avcodec_close(video_stream->codec);
990
942
                printf("zero video stream %p\n", video_stream);
991
943
                video_stream = 0;
992
944
        }
1007
959
        }
1008
960
        if (outfile && outfile->oformat) {
1009
961
                if (!(outfile->oformat->flags & AVFMT_NOFILE)) {
1010
 
                        url_fclose(OUTFILE_PB);
 
962
                        avio_close(outfile->pb);
1011
963
                }
1012
964
        }
1013
965
        if (outfile) {
1101
1053
        switch (o->type) {
1102
1054
        case FF_OPT_TYPE_INT:
1103
1055
        case FF_OPT_TYPE_INT64:
1104
 
                val.i = o->default_val;
 
1056
                val.i = FFMPEG_DEF_OPT_VAL_INT(o);
1105
1057
                idp_type = IDP_INT;
1106
1058
                break;
1107
1059
        case FF_OPT_TYPE_DOUBLE:
1108
1060
        case FF_OPT_TYPE_FLOAT:
1109
 
                val.f = o->default_val;
 
1061
                val.f = FFMPEG_DEF_OPT_VAL_DOUBLE(o);
1110
1062
                idp_type = IDP_FLOAT;
1111
1063
                break;
1112
1064
        case FF_OPT_TYPE_STRING:
1314
1266
        case FFMPEG_PRESET_XVID:
1315
1267
                if(preset == FFMPEG_PRESET_XVID) {
1316
1268
                        rd->ffcodecdata.type = FFMPEG_AVI;
1317
 
                        rd->ffcodecdata.codec = CODEC_ID_XVID;
 
1269
                        rd->ffcodecdata.codec = CODEC_ID_MPEG4;
1318
1270
                }
1319
1271
                else if(preset == FFMPEG_PRESET_THEORA) {
1320
1272
                        rd->ffcodecdata.type = FFMPEG_OGG; // XXX broken
1357
1309
                }
1358
1310
        }
1359
1311
        else if(rd->imtype == R_XVID) {
1360
 
                if(rd->ffcodecdata.codec != CODEC_ID_XVID) {
 
1312
                if(rd->ffcodecdata.codec != CODEC_ID_MPEG4) {
1361
1313
                        ffmpeg_set_preset(rd, FFMPEG_PRESET_XVID);
1362
1314
                        audio= 1;
1363
1315
                }