-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFilmScan.cpp
1206 lines (1011 loc) · 29.2 KB
/
FilmScan.cpp
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// This file is part of AEO-Light
//
// Copyright (c) 2016 University of South Carolina
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// AEO-Light is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Funding for AEO-Light development was provided through a grant from the
// National Endowment for the Humanities
//-----------------------------------------------------------------------------
// FilmScan -- objects for handling scanned images of film.
//
// FilmFrame - a scan of a single frame. Pixel values are mapped to [0-1], with
// zero being black and one being white.
// FilmStrip - a sequence of FilmFrames (usually short; not the entire film)
// FilmScan - the main interface to a scanned film source (not used currently,
// see project.h instead for an object that holds the working
// project).
// SoundSignal - a sequence of values (doubles) for the sound extracted from
// the film scan
// each signal value is computed as the mean average of the
// gray level of the pixels across the sound bounds area at
// each scan row.
//
#define FILESCAN_CPP
#include <iostream>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <cmath>
#include <cstring>
#include <vector>
//#include <boost/numeric/ublas/matrix.hpp>
#include <QString>
#include <QMessageBox>
#ifdef _WIN32
// Depending on the way you've built libtiff on Windows, you may have
// to adjust the way tiffio.h declares the libtiff functions.
// If you're getting errors about symbols starting with __imp_TIFF, like
//
// FilmScan.obj : error LNK2019: unresolved external symbol __imp_TIFFClose
//
// then define BUILD_LIBTIFF_DLL (which will make the linker look for
// the __exp_TIFFClose version instead)
//
// If you're getting other link errors about unresolved symbols related to
// libtiff, try defining one of these other constants:
// USE_LIBTIFF_DLL (look for __imp_TIFF)
// USE_LIBTIFF_STATIC (look for _TIFF)
//
#define BUILD_LIBTIFF_DLL
#endif
#include <tiffio.h>
#if (defined __WIN32__) || (defined _WIN32)
# ifdef BUILD_LIBTIFF_DLL
# define LIBTIFF_DLL_IMPEXP __DLL_EXPORT__
# elif defined(LIBTIFF_STATIC)
# define LIBTIFF_DLL_IMPEXP
# elif defined (USE_LIBTIFF_DLL)
# define LIBTIFF_DLL_IMPEXP __DLL_IMPORT__
# elif defined (USE_LIBTIFF_STATIC)
# define LIBTIFF_DLL_IMPEXP
# else /* assume USE_LIBTIFF_DLL */
# define LIBTIFF_DLL_IMPEXP __DLL_IMPORT__
# endif
#else /* __WIN32__ */
# define LIBTIFF_DLL_IMPEXP
#endif
#include "DPX.h"
#include "readframedpx.h"
#include "readframeexr.h"
#include "readframetiff.h"
#include "wav.h"
#include "aeoexception.h"
#include "FilmScan.h"
#ifdef _WIN32
#define isslash(c) (((c)=='/')||((c)=='\\'))
#else
#define isslash(c) ((c)=='/')
#endif
#ifdef USELIBAV
//-----------------------------------------------------------------------------
Video::~Video()
{
if(frameNative) av_frame_free(&frameNative);
if(frameGray16)
{
av_frame_free(&frameGray16);
}
if(frameRGB)
{
av_frame_free(&frameRGB);
}
if(convertGray16) sws_freeContext(convertGray16);
if(convertRGB) sws_freeContext(convertRGB);
if(codec) avcodec_close(codec);
if(format) avformat_close_input(&format);
}
//----------------------------------------------------------------------------
bool Video::ReadNextFrame(size_t currfnum)
{
AVPacket packet;
int done;
int frame_id;
//if(this->frameNative == NULL) this->frameNative = av_frame_alloc();
while(av_read_frame(this->format, &packet)>=0)
{
// Is this a packet from the right stream?
if(packet.stream_index == this->streamIdx)
{
// Decode video frame
avcodec_decode_video2(this->codec, this->frameNative, &done,
&packet);
// Did we get a complete video frame?
if(done)
{
this->dts = packet.dts;
this->curFrame = currfnum;
// this->curFrame = (this->dts - this->dtsBase)/this->dtsStep + 1;
av_packet_unref(&packet);
return true;
}
}
av_packet_unref(&packet);
}
this->dts = 0;
this->curFrame = 0;
return false;
}
bool Video::ReadFrame(size_t frameNum = 0)
{
//if(frameNum == 0) return ReadNextFrame();
// asking for the current frame again?
if(this->curFrame == frameNum) return true;
// asking for the next frame explicitly?
if(this->curFrame+1 == frameNum) return ReadNextFrame(frameNum);
size_t target = this->dtsBase + (frameNum)*this->dtsStep;
int seekflags = AVSEEK_FLAG_ANY;
if(target < this->dts) seekflags |= AVSEEK_FLAG_BACKWARD;
av_seek_frame(this->format, this->streamIdx, target, seekflags);
// do
// {
// if(!ReadNextFrame()) return false;
// } while(this->dts < target);
if(!ReadNextFrame(frameNum)) return false;
return true;
}
double *Video::GetFrame(size_t frameNum, double *buf=NULL)
{
if(buf == NULL)
{
buf = new double [this->codec->width * this->codec->height];
if(buf == NULL)
{
throw AeoException("Out of Memory: DPX buf");
}
}
if(this->ReadFrame(frameNum) == false)
{
throw AeoException(
QString("Could not read requested frame number: %1").
arg(frameNum));
}
//if(this->frameGray16 == NULL) this->frameGray16 = av_frame_alloc();
// Convert the image from its native format to Gray16
sws_scale(this->convertGray16,
(uint8_t const * const *)this->frameNative->data,
this->frameNative->linesize, 0, this->codec->height,
this->frameGray16->data, this->frameGray16->linesize);
// translate Gray16 to double:
const uint16_t *buf16;
double scale = 1.0/(0x00FFFF);
long i(0);
for(int y=0; y<this->codec->height; ++y)
{
buf16 = (uint16_t *)
(this->frameGray16->data[0] + y*this->frameGray16->linesize[0]);
for(int x=0; x<this->codec->width; ++x)
buf[i++] = double(buf16[x]) * scale;
}
return buf;
}
unsigned char *Video::GetFrameImage(size_t frameNum, unsigned char *buf,
int &width,int &height,bool &endian)
{
if(buf == NULL)
{
buf = new unsigned char [this->codec->width * this->codec->height * 8];
if(buf == NULL)
{
throw AeoException("Out of Memory: video buf");
}
}
if(this->ReadFrame(frameNum) == false)
{
throw AeoException(
QString("Could not read requested frame number: %1").
arg(frameNum));
}
// Convert the image from its native format to RGB
sws_scale(this->convertRGB,
(uint8_t const * const *)this->frameNative->data,
this->frameNative->linesize, 0, this->codec->height,
this->frameRGB->data, this->frameRGB->linesize);
width = this->codec->width;
height = this->codec->height;
endian = false;
memcpy(buf,this->frameRGB->data[0],width*height*8);
return buf;
}
#endif
//-----------------------------------------------------------------------------
FilmFrame::FilmFrame(unsigned int r, unsigned int c)
{
rows = r;
cols = c;
buf.resize(r*c);
}
//-----------------------------------------------------------------------------
FilmFrame::~FilmFrame()
{
}
//-----------------------------------------------------------------------------
const char *SourceFormatName[] {
"DPX",
"OpenEXR",
"TIFF",
"Other image",
"LibAV Video",
"WAV",
"Unknown" };
const char *FilmScan::GetFormatStr() const
{
return SourceFormatName[this->srcFormat];
}
SourceFormat FilmScan::StrToSourceFormat(const char *str)
{
SourceFormat i;
for(i=0; i<SOURCE_UNKNOWN; ++i)
{
if(strcmp(SourceFormatName[i], str) == 0) break;
}
return i;
}
//-----------------------------------------------------------------------------
bool FilmScan::Source(const std::string filename, SourceFormat fmt)
{
this->Reset();
this->inputName = filename;
if(fmt != SOURCE_UNKNOWN)
{
this->srcFormat = fmt;
switch(fmt)
{
case SOURCE_DPX:
if(SourceDPX(filename)) return true;
break;
case SOURCE_TIFF:
if(SourceTIFF(filename)) return true;
break;
case SOURCE_LIBAV:
if(SourceLibAV(filename)) return true;
break;
case SOURCE_WAV:
if(SourceWav(filename)) return true;
break;
default:
throw AeoException("Unrecognized FileType Request");
}
}
else
{
try
{
if(SourceDPX(filename)) return true;
}
catch(...) {;}
try
{
if(SourceWav(filename)) return true;
}
catch(...) {;}
try
{
if(SourceLibAV(filename)) return true;
}
catch(...) { throw; }
}
throw AeoException("Unable to recognize source file");
}
//-----------------------------------------------------------------------------
void FilmScan::Reset()
{
if(name) { delete [] name; name = NULL; }
if(path) { delete [] path; path = NULL; }
if(fnbuf) { delete [] fnbuf; fnbuf = NULL; }
srcFormat = SOURCE_UNKNOWN;
firstFrame = 0;
numFrames = 0;
width = 0;
height = 0;
#ifdef USELIBAV
if(vid) { delete vid; vid = NULL; }
#endif
if(synth) {delete synth; synth = NULL; }
inputName.clear();
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceDPX(const std::string filename)
{
// verify the file is a DPX and get the width and height from the header
InStream img;
dpx::Reader dpx;
if(!img.Open(filename.c_str()))
{
QString msg;
int err;
msg += QString("FilmScan: Cannot open ");
msg += QString(filename.c_str());
msg += QString("\n");
if((err=errno)!=0) msg += QString(strerror(err));
throw AeoException(msg);
}
dpx.SetInStream(&img);
if(!dpx.ReadHeader())
{
QString msg;
int err;
msg += QString("FilmScan: Invalid DPX header in ");
msg += QString(filename.c_str());
img.Close();
throw AeoException(msg);
}
this->width = dpx.header.Width();
this->height = dpx.header.Height();
this->srcFormat = SOURCE_DPX;
img.Close();
SourceIdentifyImageSet(filename);
// Pull the TimeCode from the first DPX in the sequence:
sprintf(this->fnbuf+strlen(this->path)+1, this->name, this->FirstFrame());
if(!img.Open(this->fnbuf))
{
QString msg;
int err;
msg += QString("FilmScan: Cannot open ");
msg += QString(this->fnbuf);
msg += QString("\n");
if((err=errno)!=0) msg += QString(strerror(err));
throw AeoException(msg);
}
dpx::Reader firstDPX;
firstDPX.SetInStream(&img);
if(!firstDPX.ReadHeader())
{
QString msg;
int err;
msg += QString("FilmScan: Invalid DPX header in ");
msg += QString(this->fnbuf);
img.Close();
throw AeoException(msg);
}
char TC[16];
firstDPX.header.TimeCode(TC);
this->TimeCode = TC;
img.Close();
return true;
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceEXR(const std::string filename)
{
#ifdef USE_OPENEXR
// Open as an EXR and get the width and height
// The Imf library will throw an exception if the file is not an EXR
Imf::RgbaInputFile exr(filename.c_str());
Imath::Box2i dw = exr.dataWindow();
this->width = dw.max.x - dw.min.x + 1;
this->height = dw.max.y - dw.min.y + 1;
this->fmt = SOURCE_EXR;
SourceIdentifyImageSet(filename);
return true;
#else
return false;
#endif
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceTIFF(const std::string filename)
{
// verify the file is a TIFF and get the width and height from the header
InStream img;
TIFF *tif;
if((tif = TIFFOpen(filename.c_str(), "r")) == NULL)
{
QString msg;
int err;
msg += QString("FilmScan: Cannot open ");
msg += QString(filename.c_str());
msg += QString("\n");
if((err=errno)!=0) msg += QString(strerror(err));
throw AeoException(msg);
}
// just grab the width and height from the header
uint32 w, h;
if(TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w) != 1 ||
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h) != 1)
{
TIFFClose(tif);
throw AeoException("Invalid image size specification in TIFF.");
}
TIFFClose(tif);
this->width = w;
this->height = h;
this->srcFormat = SOURCE_TIFF;
this->TimeCode = "00:00:00:00";
SourceIdentifyImageSet(filename);
return true;
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceIdentifyImageSet(const std::string filename)
{
// Then find all the dpx files that go with this one by adjusting
// the number string at the end of the filename (before the extension)
// to find all the adjacent frames both before and after this one.
// break apart the filename to make a filename pattern for the rest
// of the frames.
int len, lenE, lenF, lenP;
const char *cp, *ext;
len = filename.length();
// allocate a buffer for temporary filenames later
this->fnbuf = new char [len+1];
strcpy(this->fnbuf, filename.c_str());
// find the extension (or leave it blank if none)
ext = "";
lenE = 1;
for(cp = filename.c_str()+len;
!isslash(*cp) && cp>filename.c_str();
--cp, lenE++)
{
if(*cp == '.')
{
ext = cp;
break;
}
}
// Count digits from the end (before the extension, if any)
if(*ext=='.')
cp = ext;
else
{
cp = filename.c_str()+len;
lenE = 0;
}
int numdigits;
for(numdigits = 0; cp>=filename.c_str() && isdigit(*(--cp)); numdigits++) ;
if(numdigits==0)
{
throw AeoException("DPX filename contains no digits for frame number");
}
long inputFrame;
sscanf(cp+1, "%ld", &inputFrame);
// Continue back to the path portion.
lenF = 0;
for( ; cp>=filename.c_str() && !isslash(*cp); --cp, lenF++) ;
// store the filename pattern (with extension, but without the path)
this->name = new char[
lenF // filename prefix
+2 // "%0"
+int(floor(log10(numdigits)))+1 // number of digits in <numdigits>
+2 // "ld"
+lenE // extension (including dot)
+1 // null terminator
];
sprintf(this->name, "%.*s%%0%dld%.*s",lenF,cp+1,numdigits,lenE,ext);
// store the path (without the trailing slash)
if(!isslash(*cp))
{
this->path = new char[2];
strcpy(this->path,".");
}
else
{
int pl = cp - filename.c_str();
this->path = new char[pl+1];
strncpy(this->path, filename.c_str(), pl);
this->path[pl]='\0';
// adjust slashes on windows to avoid complications later
#ifdef _WIN32
for(char *cpw=this->path; *cpw; cpw++)
if(*cpw == '\\') *cpw = '/';
#endif
}
DIR *dirp;
if((dirp = opendir(this->path)) == NULL)
{
QString msg;
int err;
msg += QString("FilmScan: Cannot read directory folder ");
msg += QString(this->path);
msg += QString("\n");
if((err=errno)!=0) msg += QString(strerror(err));
throw AeoException(msg);
}
// find all files that match the pattern and record their frame numbers
std::vector<long> frames;
long curFrame(0);
frames.reserve(1000);
while(1)
{
errno = 0;
struct dirent *dp;
if((dp = readdir(dirp)) != NULL)
{
// is it a file?
#ifdef _DIRENT_HAVE_D_TYPE
if(!(dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN)) continue;
#endif
// leading string of characters matches?
if(strncmp(dp->d_name, this->name, lenF)!=0) continue;
// ... followed by correct number of digits?
for(cp = dp->d_name+lenF; isdigit(*cp); cp++) ;
if(cp - dp->d_name != lenF+numdigits) continue;
// ... followed by extension?
if(strcmp(cp, ext) != 0) continue;
// Then save this frame number in the list (vector) of frames
sscanf(dp->d_name+lenF, "%ld", &curFrame);
if(frames.capacity() == frames.size())
frames.reserve(frames.capacity()+1000);
frames.push_back(curFrame);
}
else
break;
}
if(errno!=0)
{
QString msg;
int err;
msg += QString("FilmScan: Error reading directory folder ");
msg += QString(this->path);
msg += QString("\n");
if((err=errno)!=0) msg += QString(strerror(err));
throw AeoException(msg);
}
// find the frame given in the input argument filename
std::vector<long>::iterator framei;
std::sort(frames.begin(), frames.end());
framei = std::lower_bound(frames.begin(), frames.end(), inputFrame);
// seek in both directions to find the largest contiguous set of
// frames containing the given frame.
long firstIdx(framei - frames.begin());
while(firstIdx > 0 && frames[firstIdx-1] == frames[firstIdx] - 1)
firstIdx--;
this->firstFrame = frames[firstIdx];
long lastIdx(framei - frames.begin());
while(lastIdx < frames.size() && frames[lastIdx+1] == frames[lastIdx] + 1)
lastIdx++;
this->numFrames = frames[lastIdx] - this->firstFrame + 1;
return true;
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceWav(const std::string filename)
{
this->synth = new wav;
if(!synth->read(filename.c_str()))
return false;
// set 10% overlap
this->height = (unsigned int)((double(synth->samplesPerSec) / 24.0) * 1.1);
this->width = this->height;
this->firstFrame = 1;
// round down to whole seconds (ignoring frames in the last partial second)
this->numFrames = (synth->bufSize / synth->samplesPerSec) * 24;
// and reduce by one more, for good measure, in case the last frame
// completed the time second (to ensure the 10% overlap has something to
// overlap into).
this->numFrames--;
int len = filename.length();
const char *cp = filename.c_str() + len;
while(cp>filename.c_str() && *cp != '/' && *cp != '\\') cp--;
this->name = new char[strlen(cp)+1];
strcpy(this->name, cp);
this->path = new char[cp - filename.c_str() + 1];
strncpy(this->path, filename.c_str(), cp-filename.c_str());
this->TimeCode = "00:00:00:00";
this->srcFormat = SOURCE_WAV;
return true;
}
//-----------------------------------------------------------------------------
bool FilmScan::SourceLibAV(const std::string filename)
{
#ifdef USELIBAV
{
// accept any recognized codec
av_register_all();
vid = new Video;
AVDictionary *dict = NULL;
// Open video file
if(avformat_open_input(&vid->format, filename.c_str(), NULL, &dict)<0)
{
av_dict_free(&dict);
delete vid;
vid = NULL;
return false; // Couldn't open file
}
av_dict_free(&dict);
dict = NULL;
// Retrieve stream information
if(avformat_find_stream_info(vid->format, NULL) < 0)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
return false; // Couldn't find stream information
}
// Dump information about file onto standard error
//av_dump_format(vid->format, 0, filename.c_str(), 0);
char TC [20];
AVDictionaryEntry * DE=NULL;
// Find the first video stream
vid->streamIdx = -1;
for(int i=0; i<vid->format->nb_streams; ++i)
{
if(vid->format->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
vid->streamIdx=i;
DE = av_dict_get(vid->format->streams[i]->metadata,"timecode",NULL ,0);
if(DE)
this->TimeCode = DE->value;
else
this->TimeCode = "00:00:00:00" ;
break;
}
}
if(vid->streamIdx == -1)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
return false; // Couldn't find any video stream
}
int ret = QMessageBox::question(NULL,
"Buffer Input?",
"Do you want to buffer the incoming video?",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
if(ret == QMessageBox::No)
{
av_dict_set(&dict, "fflags", "nobuffer",0);
avformat_close_input(&(vid->format));
delete vid;
vid = new Video;
if(avformat_open_input(&vid->format, filename.c_str(), NULL, &dict)<0)
{
av_dict_free(&dict);
delete vid;
vid = NULL;
return false; // Couldn't open file
}
if(avformat_find_stream_info(vid->format, NULL) < 0)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
return false; // Couldn't find stream information
}
// Find the first video stream
vid->streamIdx = -1;
for(int i=0; i<vid->format->nb_streams; ++i)
{
if(vid->format->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
vid->streamIdx=i;
break;
}
}
if(vid->streamIdx == -1)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
return false; // Couldn't find any video stream
}
}
// ask the stream how many frames it has
if((this->numFrames=vid->format->streams[vid->streamIdx]->nb_frames) == 0)
{
// TODO: if it doesn't know, guess based on duration and then validate.
//
this->numFrames =
(int)(( vid->format->streams[vid->streamIdx]->duration /
(double)AV_TIME_BASE ) *
vid->format->streams[vid->streamIdx]->codec->time_base.den +
0.5);
/* FAILED ATTEMPT TO USE LIBAV TO READ IMAGES
if(this->numFrames < 0)
{
SourceIdentifyImageSet(filename);
}
*/
if(this->numFrames <= 0)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Could not determine number of frames in video");
}
}
this->firstFrame = 0;
AVCodecContext *codecOrig;
AVCodec *decoder;
// Get a pointer to the codec context for the video stream
codecOrig=vid->format->streams[vid->streamIdx]->codec;
// Find the decoder for the video stream
decoder=avcodec_find_decoder(codecOrig->codec_id);
if(decoder==NULL)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Unsupported codec.");
}
// Copy codec context
vid->codec = avcodec_alloc_context3(decoder);
if(avcodec_copy_context(vid->codec, codecOrig) != 0)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Couldn't copy codec context");
}
avcodec_close(codecOrig);
// Open codec
if(avcodec_open2(vid->codec, decoder, NULL)<0)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Couldn't open decoder context");
}
// Allocate video frame
vid->frameNative=av_frame_alloc();
if(vid->frameNative==NULL)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Couldn't allocate frame structure");
}
// Allocate an RGB frame
vid->frameRGB=av_frame_alloc();
if(vid->frameRGB==NULL)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException ("Couldn't allocate frame structure");
}
this->width = vid->codec->width;
this->height = vid->codec->height;
// allocate RGB buffer (to be freed when vid is destroyed)
uint8_t *buffer = (uint8_t *)av_malloc(
av_image_get_buffer_size(
AV_PIX_FMT_RGBA64LE,
vid->codec->width, vid->codec->height,1));
// Assign appropriate parts of buffer to image planes in frameRGB
// Note that frameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
//avpicture_fill((AVPicture *)vid->frameRGB, buffer, AV_PIX_FMT_RGBA64LE,
// vid->codec->width, vid->codec->height);
av_image_fill_arrays(vid->frameRGB->data, vid->frameRGB->linesize, buffer,
AV_PIX_FMT_RGBA64LE, vid->codec->width, vid->codec->height, 1);
// Allocate a Gray16 frame
vid->frameGray16=av_frame_alloc();
if(vid->frameGray16==NULL)
{
avformat_close_input(&(vid->format));
delete vid;
vid = NULL;
throw AeoException("Couldn't allocate frame structure");
}
// allocate Gray16 buffer (to be freed when vid is destroyed)
buffer = (uint8_t *)av_malloc(
av_image_get_buffer_size(
AV_PIX_FMT_GRAY16, vid->codec->width, vid->codec->height, 1
));
// Assign appropriate parts of buffer to image planes in frameGray16
//avpicture_fill((AVPicture *)vid->frameGray16, buffer, AV_PIX_FMT_GRAY16,
// vid->codec->width, vid->codec->height);
av_image_fill_arrays(vid->frameGray16->data, vid->frameGray16->linesize,
buffer, AV_PIX_FMT_GRAY16,
vid->codec->width, vid->codec->height, 1);
// initialize SWS context for conversion to RGB
vid->convertRGB = sws_getContext(
vid->codec->width,
vid->codec->height,
vid->codec->pix_fmt,
vid->codec->width,
vid->codec->height,
AV_PIX_FMT_RGBA64LE, //rgba
SWS_BILINEAR,
NULL,
NULL,
NULL
);
// initialize SWS context for conversion to Gray16
vid->convertGray16 = sws_getContext(
vid->codec->width,
vid->codec->height,
vid->codec->pix_fmt,
vid->codec->width,
vid->codec->height,
AV_PIX_FMT_GRAY16,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
// Read two frames to calibrate seek-to-frame parameters
vid->ReadNextFrame(0);
vid->dtsBase = vid->dts;
vid->ReadNextFrame(1);
vid->dtsStep = vid->dts;
// reset to first frame
vid->ReadFrame(0);
int len = filename.length();
const char *cp = filename.c_str() + len;
while(cp>filename.c_str() && *cp != '/' && *cp != '\\') cp--;
this->name = new char[strlen(cp)+1];
strcpy(this->name, cp);
this->path = new char[cp - filename.c_str() + 1];
strncpy(this->path, filename.c_str(), cp-filename.c_str());
this->srcFormat = SOURCE_LIBAV;