forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcap_mjpeg_encoder.cpp
More file actions
1904 lines (1582 loc) · 57.1 KB
/
cap_mjpeg_encoder.cpp
File metadata and controls
1904 lines (1582 loc) · 57.1 KB
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2015, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include <vector>
#include <deque>
#if CV_NEON
#define WITH_NEON
#endif
namespace cv
{
namespace mjpeg
{
enum { COLORSPACE_GRAY=0, COLORSPACE_RGBA=1, COLORSPACE_BGR=2, COLORSPACE_YUV444P=3 };
#define fourCC(a,b,c,d) ((int)((uchar(d)<<24) | (uchar(c)<<16) | (uchar(b)<<8) | uchar(a)))
static const int AVIH_STRH_SIZE = 56;
static const int STRF_SIZE = 40;
static const int AVI_DWFLAG = 0x00000910;
static const int AVI_DWSCALE = 1;
static const int AVI_DWQUALITY = -1;
static const int JUNK_SEEK = 4096;
static const int AVIIF_KEYFRAME = 0x10;
static const int MAX_BYTES_PER_SEC = 99999999;
static const int SUG_BUFFER_SIZE = 1048576;
static const unsigned bit_mask[] =
{
0,
0x00000001, 0x00000003, 0x00000007, 0x0000000F,
0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
};
class BitStream
{
public:
enum
{
DEFAULT_BLOCK_SIZE = (1 << 15),
huff_val_shift = 20,
huff_code_mask = (1 << huff_val_shift) - 1
};
BitStream()
{
m_buf.resize(DEFAULT_BLOCK_SIZE + 1024);
m_start = &m_buf[0];
m_end = m_start + DEFAULT_BLOCK_SIZE;
m_is_opened = false;
m_f = 0;
m_current = 0;
m_pos = 0;
}
~BitStream()
{
close();
}
bool open(const String& filename)
{
close();
m_f = fopen(filename.c_str(), "wb");
if( !m_f )
return false;
m_current = m_start;
m_pos = 0;
return true;
}
bool isOpened() const { return m_f != 0; }
void close()
{
writeBlock();
if( m_f )
fclose(m_f);
m_f = 0;
}
void writeBlock()
{
size_t wsz0 = m_current - m_start;
if( wsz0 > 0 && m_f )
{
size_t wsz = fwrite(m_start, 1, wsz0, m_f);
CV_Assert( wsz == wsz0 );
}
m_pos += wsz0;
m_current = m_start;
}
size_t getPos() const
{
return (size_t)(m_current - m_start) + m_pos;
}
void putByte(int val)
{
*m_current++ = (uchar)val;
if( m_current >= m_end )
writeBlock();
}
void putBytes(const uchar* buf, int count)
{
uchar* data = (uchar*)buf;
CV_Assert(m_f && data && m_current && count >= 0);
if( m_current >= m_end )
writeBlock();
while( count )
{
int l = (int)(m_end - m_current);
if (l > count)
l = count;
if( l > 0 )
{
memcpy(m_current, data, l);
m_current += l;
data += l;
count -= l;
}
if( m_current >= m_end )
writeBlock();
}
}
void putShort(int val)
{
m_current[0] = (uchar)val;
m_current[1] = (uchar)(val >> 8);
m_current += 2;
if( m_current >= m_end )
writeBlock();
}
void putInt(int val)
{
m_current[0] = (uchar)val;
m_current[1] = (uchar)(val >> 8);
m_current[2] = (uchar)(val >> 16);
m_current[3] = (uchar)(val >> 24);
m_current += 4;
if( m_current >= m_end )
writeBlock();
}
void jputShort(int val)
{
m_current[0] = (uchar)(val >> 8);
m_current[1] = (uchar)val;
m_current += 2;
if( m_current >= m_end )
writeBlock();
}
void patchInt(int val, size_t pos)
{
if( pos >= m_pos )
{
ptrdiff_t delta = pos - m_pos;
CV_Assert( delta < m_current - m_start );
m_start[delta] = (uchar)val;
m_start[delta+1] = (uchar)(val >> 8);
m_start[delta+2] = (uchar)(val >> 16);
m_start[delta+3] = (uchar)(val >> 24);
}
else
{
long fpos = ftell(m_f);
fseek(m_f, (long)pos, SEEK_SET);
uchar buf[] = { (uchar)val, (uchar)(val >> 8), (uchar)(val >> 16), (uchar)(val >> 24) };
fwrite(buf, 1, 4, m_f);
fseek(m_f, fpos, SEEK_SET);
}
}
void jput(unsigned currval)
{
uchar v;
uchar* ptr = m_current;
v = (uchar)(currval >> 24);
*ptr++ = v;
if( v == 255 )
*ptr++ = 0;
v = (uchar)(currval >> 16);
*ptr++ = v;
if( v == 255 )
*ptr++ = 0;
v = (uchar)(currval >> 8);
*ptr++ = v;
if( v == 255 )
*ptr++ = 0;
v = (uchar)currval;
*ptr++ = v;
if( v == 255 )
*ptr++ = 0;
m_current = ptr;
if( m_current >= m_end )
writeBlock();
}
void jflush(unsigned currval, int bitIdx)
{
uchar v;
uchar* ptr = m_current;
currval |= (1 << bitIdx)-1;
while( bitIdx < 32 )
{
v = (uchar)(currval >> 24);
*ptr++ = v;
if( v == 255 )
*ptr++ = 0;
currval <<= 8;
bitIdx += 8;
}
m_current = ptr;
if( m_current >= m_end )
writeBlock();
}
static bool createEncodeHuffmanTable( const int* src, unsigned* table, int max_size )
{
int i, k;
int min_val = INT_MAX, max_val = INT_MIN;
int size;
/* calc min and max values in the table */
for( i = 1, k = 1; src[k] >= 0; i++ )
{
int code_count = src[k++];
for( code_count += k; k < code_count; k++ )
{
int val = src[k] >> huff_val_shift;
if( val < min_val )
min_val = val;
if( val > max_val )
max_val = val;
}
}
size = max_val - min_val + 3;
if( size > max_size )
{
CV_Error(CV_StsOutOfRange, "too big maximum Huffman code size");
return false;
}
memset( table, 0, size*sizeof(table[0]));
table[0] = min_val;
table[1] = size - 2;
for( i = 1, k = 1; src[k] >= 0; i++ )
{
int code_count = src[k++];
for( code_count += k; k < code_count; k++ )
{
int val = src[k] >> huff_val_shift;
int code = src[k] & huff_code_mask;
table[val - min_val + 2] = (code << 8) | i;
}
}
return true;
}
static int* createSourceHuffmanTable(const uchar* src, int* dst,
int max_bits, int first_bits)
{
int i, val_idx, code = 0;
int* table = dst;
*dst++ = first_bits;
for (i = 1, val_idx = max_bits; i <= max_bits; i++)
{
int code_count = src[i - 1];
dst[0] = code_count;
code <<= 1;
for (int k = 0; k < code_count; k++)
{
dst[k + 1] = (src[val_idx + k] << huff_val_shift) | (code + k);
}
code += code_count;
dst += code_count + 1;
val_idx += code_count;
}
dst[0] = -1;
return table;
}
protected:
std::vector<uchar> m_buf;
uchar* m_start;
uchar* m_end;
uchar* m_current;
size_t m_pos;
bool m_is_opened;
FILE* m_f;
};
class mjpeg_buffer
{
public:
mjpeg_buffer()
{
reset();
}
void resize(int size)
{
data.resize(size);
}
void put(unsigned bits, int len)
{
if((m_pos == (data.size() - 1) && len > bits_free) || m_pos == data.size())
{
resize(int(2*data.size()));
}
bits_free -= (len);
unsigned int tempval = (bits) & bit_mask[(len)];
if( bits_free <= 0 )
{
data[m_pos] |= ((unsigned)tempval >> -bits_free);
bits_free += 32;
++m_pos;
data[m_pos] = bits_free < 32 ? (tempval << bits_free) : 0;
}
else
{
data[m_pos] |= (bits_free == 32) ? tempval : (tempval << bits_free);
}
}
void finish()
{
if(bits_free == 32)
{
bits_free = 0;
m_data_len = m_pos;
}
else
{
m_data_len = m_pos + 1;
}
}
void reset()
{
bits_free = 32;
m_pos = 0;
m_data_len = 0;
}
void clear()
{
//we need to clear only first element, the rest would be overwritten
data[0] = 0;
}
int get_bits_free()
{
return bits_free;
}
unsigned* get_data()
{
return &data[0];
}
unsigned get_len()
{
return m_data_len;
}
private:
std::vector<unsigned> data;
int bits_free;
unsigned m_pos;
unsigned m_data_len;
};
class mjpeg_buffer_keeper
{
public:
mjpeg_buffer_keeper()
{
reset();
}
mjpeg_buffer& operator[](int i)
{
return m_buffer_list[i];
}
void allocate_buffers(int count, int size)
{
for(int i = (int)m_buffer_list.size(); i < count; ++i)
{
m_buffer_list.push_back(mjpeg_buffer());
m_buffer_list.back().resize(size);
}
}
unsigned* get_data()
{
//if there is only one buffer (single thread) there is no need to stack buffers
if(m_buffer_list.size() == 1)
{
m_buffer_list[0].finish();
m_data_len = m_buffer_list[0].get_len();
m_last_bit_len = m_buffer_list[0].get_bits_free() ? 32 - m_buffer_list[0].get_bits_free() : 0;
return m_buffer_list[0].get_data();
}
allocate_output_buffer();
int bits = 0;
unsigned currval = 0;
m_data_len = 0;
for(unsigned j = 0; j < m_buffer_list.size(); ++j)
{
mjpeg_buffer& buffer = m_buffer_list[j];
//if no bit shift required we could use memcpy
if(bits == 0)
{
size_t current_pos = m_data_len;
if(buffer.get_bits_free() == 0)
{
memcpy(&m_output_buffer[current_pos], buffer.get_data(), sizeof(buffer.get_data()[0])*buffer.get_len());
m_data_len += buffer.get_len();
currval = 0;
}
else
{
memcpy(&m_output_buffer[current_pos], buffer.get_data(), sizeof(buffer.get_data()[0])*(buffer.get_len() - 1 ));
m_data_len += buffer.get_len() - 1;
currval = buffer.get_data()[buffer.get_len() - 1];
}
}
else
{
for(unsigned i = 0; i < buffer.get_len() - 1; ++i)
{
currval |= ( (unsigned)buffer.get_data()[i] >> (31 & (-bits)) );
m_output_buffer[m_data_len++] = currval;
currval = buffer.get_data()[i] << (bits + 32);
}
currval |= ( (unsigned)buffer.get_data()[buffer.get_len() - 1] >> (31 & (-bits)) );
if( buffer.get_bits_free() <= -bits)
{
m_output_buffer[m_data_len++] = currval;
currval = buffer.get_data()[buffer.get_len() - 1] << (bits + 32);
}
}
bits += buffer.get_bits_free();
if(bits > 0)
{
bits -= 32;
}
}
//bits == 0 means that last element shouldn't be used.
m_output_buffer[m_data_len++] = currval;
m_last_bit_len = -bits;
return &m_output_buffer[0];
}
int get_last_bit_len()
{
return m_last_bit_len;
}
int get_data_size()
{
return m_data_len;
}
void reset()
{
m_last_bit_len = 0;
for(unsigned i = 0; i < m_buffer_list.size(); ++i)
{
m_buffer_list[i].reset();
}
//there is no need to erase output buffer since it would be overwritten
m_data_len = 0;
}
private:
void allocate_output_buffer()
{
unsigned total_size = 0;
for(unsigned i = 0; i < m_buffer_list.size(); ++i)
{
m_buffer_list[i].finish();
total_size += m_buffer_list[i].get_len();
}
if(total_size > m_output_buffer.size())
{
m_output_buffer.clear();
m_output_buffer.resize(total_size);
}
}
std::deque<mjpeg_buffer> m_buffer_list;
std::vector<unsigned> m_output_buffer;
int m_data_len;
int m_last_bit_len;
};
class MotionJpegWriter : public IVideoWriter
{
public:
MotionJpegWriter()
{
rawstream = false;
nstripes = -1;
height = 0;
width = 0;
moviPointer = 0;
channels = 0;
outfps = 0;
quality = 0;
}
MotionJpegWriter(const String& filename, double fps, Size size, bool iscolor)
{
rawstream = false;
open(filename, fps, size, iscolor);
nstripes = -1;
}
~MotionJpegWriter() { close(); }
void close()
{
if( !strm.isOpened() )
return;
if( !frameOffset.empty() && !rawstream )
{
endWriteChunk(); // end LIST 'movi'
writeIndex();
finishWriteAVI();
}
strm.close();
frameOffset.clear();
frameSize.clear();
AVIChunkSizeIndex.clear();
frameNumIndexes.clear();
}
bool open(const String& filename, double fps, Size size, bool iscolor)
{
close();
if( filename.empty() )
return false;
const char* ext = strrchr(filename.c_str(), '.');
if( !ext )
return false;
if( strcmp(ext, ".avi") != 0 && strcmp(ext, ".AVI") != 0 && strcmp(ext, ".Avi") != 0 )
return false;
bool ok = strm.open(filename);
if( !ok )
return false;
CV_Assert(fps >= 1);
outfps = cvRound(fps);
width = size.width;
height = size.height;
quality = 75;
rawstream = false;
channels = iscolor ? 3 : 1;
if( !rawstream )
{
startWriteAVI();
writeStreamHeader();
}
//printf("motion jpeg stream %s has been successfully opened\n", filename.c_str());
return true;
}
bool isOpened() const { return strm.isOpened(); }
void startWriteAVI()
{
startWriteChunk(fourCC('R', 'I', 'F', 'F'));
strm.putInt(fourCC('A', 'V', 'I', ' '));
startWriteChunk(fourCC('L', 'I', 'S', 'T'));
strm.putInt(fourCC('h', 'd', 'r', 'l'));
strm.putInt(fourCC('a', 'v', 'i', 'h'));
strm.putInt(AVIH_STRH_SIZE);
strm.putInt(cvRound(1e6 / outfps));
strm.putInt(MAX_BYTES_PER_SEC);
strm.putInt(0);
strm.putInt(AVI_DWFLAG);
frameNumIndexes.push_back(strm.getPos());
strm.putInt(0);
strm.putInt(0);
strm.putInt(1); // number of streams
strm.putInt(SUG_BUFFER_SIZE);
strm.putInt(width);
strm.putInt(height);
strm.putInt(0);
strm.putInt(0);
strm.putInt(0);
strm.putInt(0);
}
void writeStreamHeader()
{
// strh
startWriteChunk(fourCC('L', 'I', 'S', 'T'));
strm.putInt(fourCC('s', 't', 'r', 'l'));
strm.putInt(fourCC('s', 't', 'r', 'h'));
strm.putInt(AVIH_STRH_SIZE);
strm.putInt(fourCC('v', 'i', 'd', 's'));
strm.putInt(fourCC('M', 'J', 'P', 'G'));
strm.putInt(0);
strm.putInt(0);
strm.putInt(0);
strm.putInt(AVI_DWSCALE);
strm.putInt(outfps);
strm.putInt(0);
frameNumIndexes.push_back(strm.getPos());
strm.putInt(0);
strm.putInt(SUG_BUFFER_SIZE);
strm.putInt(AVI_DWQUALITY);
strm.putInt(0);
strm.putShort(0);
strm.putShort(0);
strm.putShort(width);
strm.putShort(height);
// strf (use the BITMAPINFOHEADER for video)
startWriteChunk(fourCC('s', 't', 'r', 'f'));
strm.putInt(STRF_SIZE);
strm.putInt(width);
strm.putInt(height);
strm.putShort(1); // planes (1 means interleaved data (after decompression))
strm.putShort(8 * channels); // bits per pixel
strm.putInt(fourCC('M', 'J', 'P', 'G'));
strm.putInt(width * height * channels);
strm.putInt(0);
strm.putInt(0);
strm.putInt(0);
strm.putInt(0);
// Must be indx chunk
endWriteChunk(); // end strf
endWriteChunk(); // end strl
// odml
startWriteChunk(fourCC('L', 'I', 'S', 'T'));
strm.putInt(fourCC('o', 'd', 'm', 'l'));
startWriteChunk(fourCC('d', 'm', 'l', 'h'));
frameNumIndexes.push_back(strm.getPos());
strm.putInt(0);
strm.putInt(0);
endWriteChunk(); // end dmlh
endWriteChunk(); // end odml
endWriteChunk(); // end hdrl
// JUNK
startWriteChunk(fourCC('J', 'U', 'N', 'K'));
size_t pos = strm.getPos();
for( ; pos < (size_t)JUNK_SEEK; pos += 4 )
strm.putInt(0);
endWriteChunk(); // end JUNK
// movi
startWriteChunk(fourCC('L', 'I', 'S', 'T'));
moviPointer = strm.getPos();
strm.putInt(fourCC('m', 'o', 'v', 'i'));
}
void startWriteChunk(int fourcc)
{
CV_Assert(fourcc != 0);
strm.putInt(fourcc);
AVIChunkSizeIndex.push_back(strm.getPos());
strm.putInt(0);
}
void endWriteChunk()
{
if( !AVIChunkSizeIndex.empty() )
{
size_t currpos = strm.getPos();
size_t pospos = AVIChunkSizeIndex.back();
AVIChunkSizeIndex.pop_back();
int chunksz = (int)(currpos - (pospos + 4));
strm.patchInt(chunksz, pospos);
}
}
void writeIndex()
{
// old style AVI index. Must be Open-DML index
startWriteChunk(fourCC('i', 'd', 'x', '1'));
int nframes = (int)frameOffset.size();
for( int i = 0; i < nframes; i++ )
{
strm.putInt(fourCC('0', '0', 'd', 'c'));
strm.putInt(AVIIF_KEYFRAME);
strm.putInt((int)frameOffset[i]);
strm.putInt((int)frameSize[i]);
}
endWriteChunk(); // End idx1
}
void finishWriteAVI()
{
int nframes = (int)frameOffset.size();
// Record frames numbers to AVI Header
while (!frameNumIndexes.empty())
{
size_t ppos = frameNumIndexes.back();
frameNumIndexes.pop_back();
strm.patchInt(nframes, ppos);
}
endWriteChunk(); // end RIFF
}
void write(InputArray _img)
{
Mat img = _img.getMat();
size_t chunkPointer = strm.getPos();
int input_channels = img.channels();
int colorspace = -1;
if( input_channels == 1 && channels == 1 )
{
CV_Assert( img.cols == width && img.rows == height );
colorspace = COLORSPACE_GRAY;
}
else if( input_channels == 4 )
{
CV_Assert( img.cols == width && img.rows == height && channels == 3 );
colorspace = COLORSPACE_RGBA;
}
else if( input_channels == 3 )
{
CV_Assert( img.cols == width && img.rows == height && channels == 3 );
colorspace = COLORSPACE_BGR;
}
else if( input_channels == 1 && channels == 3 )
{
CV_Assert( img.cols == width && img.rows == height*3 );
colorspace = COLORSPACE_YUV444P;
}
else
CV_Error(CV_StsBadArg, "Invalid combination of specified video colorspace and the input image colorspace");
if( !rawstream )
startWriteChunk(fourCC('0', '0', 'd', 'c'));
writeFrameData(img.data, (int)img.step, colorspace, input_channels);
if( !rawstream )
{
frameOffset.push_back(chunkPointer - moviPointer);
frameSize.push_back(strm.getPos() - chunkPointer - 8); // Size excludes '00dc' and size field
endWriteChunk(); // end '00dc'
}
}
double getProperty(int propId) const
{
if( propId == VIDEOWRITER_PROP_QUALITY )
return quality;
if( propId == VIDEOWRITER_PROP_FRAMEBYTES )
return frameSize.empty() ? 0. : (double)frameSize.back();
if( propId == VIDEOWRITER_PROP_NSTRIPES )
return nstripes;
return 0.;
}
bool setProperty(int propId, double value)
{
if( propId == VIDEOWRITER_PROP_QUALITY )
{
quality = value;
return true;
}
if( propId == VIDEOWRITER_PROP_NSTRIPES)
{
nstripes = value;
return true;
}
return false;
}
void writeFrameData( const uchar* data, int step, int colorspace, int input_channels );
protected:
int outfps;
int width, height, channels;
double quality;
size_t moviPointer;
std::vector<size_t> frameOffset, frameSize, AVIChunkSizeIndex, frameNumIndexes;
bool rawstream;
mjpeg_buffer_keeper buffers_list;
double nstripes;
BitStream strm;
};
#define DCT_DESCALE(x, n) (((x) + (((int)1) << ((n) - 1))) >> (n))
#define fix(x, n) (int)((x)*(1 << (n)) + .5);
enum
{
fixb = 14,
fixc = 12,
postshift = 14
};
static const int C0_707 = fix(0.707106781f, fixb);
static const int C0_541 = fix(0.541196100f, fixb);
static const int C0_382 = fix(0.382683432f, fixb);
static const int C1_306 = fix(1.306562965f, fixb);
static const int y_r = fix(0.299, fixc);
static const int y_g = fix(0.587, fixc);
static const int y_b = fix(0.114, fixc);
static const int cb_r = -fix(0.1687, fixc);
static const int cb_g = -fix(0.3313, fixc);
static const int cb_b = fix(0.5, fixc);
static const int cr_r = fix(0.5, fixc);
static const int cr_g = -fix(0.4187, fixc);
static const int cr_b = -fix(0.0813, fixc);
// Standard JPEG quantization tables
static const uchar jpegTableK1_T[] =
{
16, 12, 14, 14, 18, 24, 49, 72,
11, 12, 13, 17, 22, 35, 64, 92,
10, 14, 16, 22, 37, 55, 78, 95,
16, 19, 24, 29, 56, 64, 87, 98,
24, 26, 40, 51, 68, 81, 103, 112,
40, 58, 57, 87, 109, 104, 121, 100,
51, 60, 69, 80, 103, 113, 120, 103,
61, 55, 56, 62, 77, 92, 101, 99
};
static const uchar jpegTableK2_T[] =
{
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
// Standard Huffman tables
// ... for luma DCs.
static const uchar jpegTableK3[] =
{
0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
// ... for chroma DCs.
static const uchar jpegTableK4[] =
{
0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
// ... for luma ACs.
static const uchar jpegTableK5[] =
{
0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125,
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa
};
// ... for chroma ACs
static const uchar jpegTableK6[] =