forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkFFMPEGWriter.cxx
529 lines (439 loc) · 13.4 KB
/
vtkFFMPEGWriter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*=========================================================================
Program: Visualization Toolkit
Module: vtkFFMPEGWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkFFMPEGWriter.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkErrorCode.h"
#include "vtkFFMPEGConfig.h"
extern "C" {
#ifdef VTK_FFMPEG_HAS_OLD_HEADER
# include <ffmpeg/avformat.h>
#else
# include <libavformat/avformat.h>
#endif
#ifndef VTK_FFMPEG_HAS_IMG_CONVERT
# ifdef VTK_FFMPEG_HAS_OLD_HEADER
# include <ffmpeg/swscale.h>
# else
# include <libswscale/swscale.h>
# endif
#endif
}
//---------------------------------------------------------------------------
class vtkFFMPEGWriterInternal
{
public:
vtkFFMPEGWriterInternal(vtkFFMPEGWriter *creator);
~vtkFFMPEGWriterInternal();
int Start();
int Write(vtkImageData *id);
void End();
int Dim[2];
int FrameRate;
private:
vtkFFMPEGWriter *Writer;
AVFormatContext *avFormatContext;
AVOutputFormat *avOutputFormat;
AVStream *avStream;
unsigned char *codecBuf;
int codecBufSize;
AVFrame *rgbInput;
AVFrame *yuvOutput;
int openedFile;
int closedFile;
};
//---------------------------------------------------------------------------
vtkFFMPEGWriterInternal::vtkFFMPEGWriterInternal(vtkFFMPEGWriter *creator)
{
this->Writer = creator;
this->Dim[0] = 0;
this->Dim[1] = 0;
this->avFormatContext = NULL;
this->avOutputFormat = NULL;
this->avStream = NULL;
this->codecBuf = NULL;
this->rgbInput = NULL;
this->yuvOutput = NULL;
this->openedFile = 0;
this->closedFile = 1;
this->FrameRate = 25;
}
//---------------------------------------------------------------------------
vtkFFMPEGWriterInternal::~vtkFFMPEGWriterInternal()
{
if (!this->closedFile)
{
this->End();
}
}
//---------------------------------------------------------------------------
int vtkFFMPEGWriterInternal::Start()
{
this->closedFile = 0;
//initialize libavcodec, and register all codecs and formats
av_register_all();
//create the format context that wraps all of the media output structures
#ifdef VTK_FFMPEG_NEW_ALLOC
this->avFormatContext = avformat_alloc_context();
#else
this->avFormatContext = av_alloc_format_context();
#endif
if (!this->avFormatContext)
{
vtkGenericWarningMacro (<< "Coult not open the format context.");
return 0;
}
//choose avi media file format
this->avOutputFormat = guess_format("avi", NULL, NULL);
if (!this->avOutputFormat)
{
vtkGenericWarningMacro (<< "Could not open the avi media file format.");
return 0;
}
//chosen a codec that is easily playable on windows
this->avOutputFormat->video_codec = CODEC_ID_MJPEG;
//assign the format to the context
this->avFormatContext->oformat = this->avOutputFormat;
//choose a filename for the output
strcpy(this->avFormatContext->filename, this->Writer->GetFileName());
//create a stream for that file
this->avStream = av_new_stream(this->avFormatContext, 0);
if (!this->avStream)
{
vtkGenericWarningMacro (<< "Could not create video stream.");
return 0;
}
//Set up the codec.
AVCodecContext *c = this->avStream->codec;
c->codec_id = (CodecID)this->avOutputFormat->video_codec;
c->codec_type = CODEC_TYPE_VIDEO;
c->width = this->Dim[0];
c->height = this->Dim[1];
c->pix_fmt = PIX_FMT_YUVJ420P;
//change DIV3 to MP43 fourCC to be easily playable on windows
//c->codec_tag = ('3'<<24) + ('4'<<16) + ('P'<<8) + 'M';
//to do playback at actual recorded rate, this will need more work see also below
c->time_base.den = this->FrameRate;
c->time_base.num = 1;
//about one full frame per second
c->gop_size = this->FrameRate;
//allow a variable quality/size tradeoff
switch (this->Writer->GetQuality())
{
case 0:
c->bit_rate = 3*1024*1024;
break;
case 1:
c->bit_rate = 6*1024*1024;
break;
default:
c->bit_rate = 12*1024*1024;
break;
}
c->bit_rate_tolerance = c->bit_rate/this->FrameRate;
//apply the chosen parameters
if (av_set_parameters(this->avFormatContext, NULL) < 0)
{
vtkGenericWarningMacro (<< "Invalid output format parameters." );
return 0;
}
//manufacture a codec with the chosen parameters
AVCodec *codec = avcodec_find_encoder(c->codec_id);
if (!codec)
{
vtkGenericWarningMacro (<< "Codec not found." );
return 0;
}
if (avcodec_open(c, codec) < 0)
{
vtkGenericWarningMacro (<< "Could not open codec.");
return 0;
}
//create buffers for the codec to work with.
//working compression space
this->codecBufSize = 2*c->width*c->height*4; //hopefully this is enough
this->codecBuf = new unsigned char[this->codecBufSize];
if (!this->codecBuf)
{
vtkGenericWarningMacro (<< "Could not make codec working space." );
return 0;
}
//for the output of the writer's input...
this->rgbInput = avcodec_alloc_frame();
if (!this->rgbInput)
{
vtkGenericWarningMacro (<< "Could not make rgbInput avframe." );
return 0;
}
int RGBsize = avpicture_get_size(PIX_FMT_RGB24, c->width, c->height);
unsigned char *rgb = new unsigned char[RGBsize];
if (!rgb)
{
vtkGenericWarningMacro (<< "Could not make rgbInput's buffer." );
return 0;
}
//The rgb buffer should get deleted when this->rgbInput is.
avpicture_fill((AVPicture *)this->rgbInput, rgb, PIX_FMT_RGB24, c->width, c->height);
//and for the output to the codec's input.
this->yuvOutput = avcodec_alloc_frame();
if (!this->yuvOutput)
{
vtkGenericWarningMacro (<< "Could not make yuvOutput avframe." );
return 0;
}
int YUVsize = avpicture_get_size(c->pix_fmt, c->width, c->height);
unsigned char *yuv = new unsigned char[YUVsize];
if (!yuv)
{
vtkGenericWarningMacro (<< "Could not make yuvOutput's buffer." );
return 0;
}
//The yuv buffer should get deleted when this->yuv_input is.
avpicture_fill((AVPicture *)this->yuvOutput, yuv, c->pix_fmt, c->width, c->height);
//Finally, open the file and start it off.
if (url_fopen(&this->avFormatContext->pb, this->avFormatContext->filename, URL_WRONLY) < 0)
{
vtkGenericWarningMacro (<< "Could not open " << this->Writer->GetFileName() << "." );
return 0;
}
this->openedFile = 1;
av_write_header(this->avFormatContext);
return 1;
}
//---------------------------------------------------------------------------
int vtkFFMPEGWriterInternal::Write(vtkImageData *id)
{
id->Update();
AVCodecContext *cc = this->avStream->codec;
//copy the image from the input to the RGB buffer while flipping Y
unsigned char *rgb = (unsigned char*)id->GetScalarPointer();
unsigned char *src;
for (int y = 0; y < cc->height; y++)
{
src = rgb + (cc->height-y-1) * cc->width * 3; //flip Y
unsigned char *dest =
&this->rgbInput->data[0][y*this->rgbInput->linesize[0]];
memcpy((void*)dest, (void*)src, cc->width*3);
}
//convert that to YUV for input to the codec
#ifdef VTK_FFMPEG_HAS_IMG_CONVERT
img_convert((AVPicture *)this->yuvOutput, cc->pix_fmt,
(AVPicture *)this->rgbInput, PIX_FMT_RGB24,
cc->width, cc->height);
#else
//convert that to YUV for input to the codec
SwsContext* convert_ctx = sws_getContext(
cc->width, cc->height, PIX_FMT_RGB24,
cc->width, cc->height, cc->pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);
if(convert_ctx == NULL)
{
vtkGenericWarningMacro(<< "swscale context initialization failed");
return 0;
}
int result = sws_scale(convert_ctx,
this->rgbInput->data, this->rgbInput->linesize,
0, cc->height,
this->yuvOutput->data, this->yuvOutput->linesize
);
sws_freeContext(convert_ctx);
if(!result)
{
vtkGenericWarningMacro(<< "sws_scale() failed");
return 0;
}
#endif
//run the encoder
int toAdd = avcodec_encode_video(cc,
this->codecBuf,
this->codecBufSize,
this->yuvOutput);
//dump the compressed result to file
if (toAdd)
{
//create an avpacket to output the compressed result
AVPacket pkt;
av_init_packet(&pkt);
//to do playback at actual recorded rate, this will need more work
pkt.pts = cc->coded_frame->pts;
//pkt.dts = ?; not dure what decompression time stamp should be
pkt.data = this->codecBuf;
pkt.size = toAdd;
pkt.stream_index = this->avStream->index;
if (cc->coded_frame->key_frame) //treat keyframes well
{
pkt.flags |= PKT_FLAG_KEY;
}
pkt.duration = 0; //presentation duration in time_base units or 0 if NA
pkt.pos = -1; //byte position in stream or -1 if NA
toAdd = av_write_frame(this->avFormatContext, &pkt);
}
if (toAdd) //should not have anything left over
{
vtkGenericWarningMacro (<< "Problem encoding frame." );
return 0;
}
return 1;
}
//---------------------------------------------------------------------------
void vtkFFMPEGWriterInternal::End()
{
if (this->yuvOutput)
{
av_free(this->yuvOutput->data[0]);
av_free(this->yuvOutput);
this->yuvOutput = 0;
}
if (this->rgbInput)
{
av_free(this->rgbInput->data[0]);
av_free(this->rgbInput);
this->rgbInput = 0;
}
if (this->codecBuf)
{
av_free(this->codecBuf);
this->codecBuf = 0;
}
if (this->avStream)
{
av_free(this->avStream);
this->avStream = 0;
}
if (this->avFormatContext)
{
if (this->openedFile)
{
av_write_trailer(this->avFormatContext);
#ifdef VTK_FFMPEG_OLD_URL_FCLOSE
url_fclose(&this->avFormatContext->pb);
#else
url_fclose(this->avFormatContext->pb);
#endif
this->openedFile = 0;
}
av_free(this->avFormatContext);
this->avFormatContext = 0;
}
if (this->avOutputFormat)
{
//Next line was done inside av_free(this->avFormatContext).
//av_free(this->avOutputFormat);
this->avOutputFormat = 0;
}
this->closedFile = 1;
}
//---------------------------------------------------------------------------
vtkStandardNewMacro(vtkFFMPEGWriter);
//---------------------------------------------------------------------------
vtkFFMPEGWriter::vtkFFMPEGWriter()
{
this->Internals = 0;
this->Quality = 2;
this->Rate = 25;
}
//---------------------------------------------------------------------------
vtkFFMPEGWriter::~vtkFFMPEGWriter()
{
delete this->Internals;
}
//---------------------------------------------------------------------------
void vtkFFMPEGWriter::Start()
{
this->Error = 1;
if ( this->Internals )
{
vtkErrorMacro("Movie already started.");
this->SetErrorCode(vtkGenericMovieWriter::InitError);
return;
}
if ( this->GetInput() == NULL )
{
vtkErrorMacro("Please specify an input.");
this->SetErrorCode(vtkGenericMovieWriter::NoInputError);
return;
}
if (!this->FileName)
{
vtkErrorMacro("Please specify a filename.");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return;
}
this->Internals = new vtkFFMPEGWriterInternal(this);
this->Error = 0;
this->Initialized = 0;
}
//---------------------------------------------------------------------------
void vtkFFMPEGWriter::Write()
{
if (this->Error)
{
return;
}
if ( !this->Internals )
{
vtkErrorMacro("Movie not started.");
this->Error = 1;
this->SetErrorCode(vtkGenericMovieWriter::InitError);
return;
}
// get the data
this->GetInput()->UpdateInformation();
int *wExtent = this->GetInput()->GetWholeExtent();
this->GetInput()->SetUpdateExtent(wExtent);
this->GetInput()->Update();
int dim[4];
this->GetInput()->GetDimensions(dim);
if ( this->Internals->Dim[0] == 0 && this->Internals->Dim[1] == 0 )
{
this->Internals->Dim[0] = dim[0];
this->Internals->Dim[1] = dim[1];
}
if (this->Internals->Dim[0]!= dim[0] || this->Internals->Dim[1]!= dim[1])
{
vtkErrorMacro("Image not of the same size.");
this->Error = 1;
this->SetErrorCode(vtkGenericMovieWriter::ChangedResolutionError);
return;
}
if ( !this->Initialized )
{
this->Internals->FrameRate = this->Rate;
if (!this->Internals->Start())
{
vtkErrorMacro("Error initializing video stream.");
this->Error = 1;
this->SetErrorCode(vtkGenericMovieWriter::InitError);
return;
}
this->Initialized = 1;
}
if (!this->Internals->Write(this->GetInput()))
{
vtkErrorMacro("Error storing image.");
this->Error = 1;
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
}
}
//---------------------------------------------------------------------------
void vtkFFMPEGWriter::End()
{
this->Internals->End();
delete this->Internals;
this->Internals = 0;
}
//---------------------------------------------------------------------------
void vtkFFMPEGWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Quality: " << this->Quality << endl;
os << indent << "Rate: " << this->Rate << endl;
}