-
Notifications
You must be signed in to change notification settings - Fork 15
/
avilib.c
1938 lines (1488 loc) · 51.8 KB
/
avilib.c
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
/*
* avilib.c
*
* Copyright (C) Thomas Östreich - June 2001
* multiple audio track support Copyright (C) 2002 Thomas Östreich
*
* Original code:
* Copyright (C) 1999 Rainer Johanni <[email protected]>
*
* This file is part of transcode, a linux video stream processing tool
*
* transcode 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, or (at your option)
* any later version.
*
* transcode 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 GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
//SLM
#ifdef WIN32
#include <io.h>
#define ftruncate _chsize
#define strncasecmp _strnicmp
typedef int ssize_t;
#endif
#ifdef __CYGWIN__
#include <unistd.h>
#endif
#include "avilib.h"
//#include <time.h>
#define INFO_LIST
/* The following variable indicates the kind of error */
long AVI_errno = 0;
#define MAX_INFO_STRLEN 64
static char id_str[MAX_INFO_STRLEN];
#define FRAME_RATE_SCALE 1000000
/*******************************************************************
* *
* Utilities for writing an AVI File *
* *
*******************************************************************/
static size_t avi_read(int fd, char *buf, size_t len)
{
size_t n = 0;
size_t r = 0;
while (r < len) {
n = read (fd, buf + r, len - r);
if ((ssize_t)n <= 0)
return r;
r += n;
}
return r;
}
static size_t avi_write (int fd, char *buf, size_t len)
{
size_t n = 0;
size_t r = 0;
while (r < len) {
n = write (fd, buf + r, len - r);
if ((ssize_t)n < 0)
return n;
r += n;
}
return r;
}
/* HEADERBYTES: The number of bytes to reserve for the header */
#define HEADERBYTES 2048
/* AVI_MAX_LEN: The maximum length of an AVI file, we stay a bit below
the 2GB limit (Remember: 2*10^9 is smaller than 2 GB) */
#define AVI_MAX_LEN (UINT_MAX-(1<<20)*16-HEADERBYTES)
#define PAD_EVEN(x) ( ((x)+1) & ~1 )
/* Copy n into dst as a 4 byte, little endian number.
Should also work on big endian machines */
static void long2str(unsigned char *dst, int n)
{
dst[0] = (n )&0xff;
dst[1] = (n>> 8)&0xff;
dst[2] = (n>>16)&0xff;
dst[3] = (n>>24)&0xff;
}
/* Convert a string of 4 or 2 bytes to a number,
also working on big endian machines */
static unsigned long str2ulong(unsigned char *str)
{
return ( str[0] | (str[1]<<8) | (str[2]<<16) | (str[3]<<24) );
}
static unsigned long str2ushort(unsigned char *str)
{
return ( str[0] | (str[1]<<8) );
}
/* Calculate audio sample size from number of bits and number of channels.
This may have to be adjusted for eg. 12 bits and stereo */
static int avi_sampsize(avi_t *AVI, int j)
{
int s;
s = ((AVI->track[j].a_bits+7)/8)*AVI->track[j].a_chans;
// if(s==0) s=1; /* avoid possible zero divisions */
if(s<4) s=4; /* avoid possible zero divisions */
return s;
}
/* Add a chunk (=tag and data) to the AVI file,
returns -1 on write error, 0 on success */
static int avi_add_chunk(avi_t *AVI, unsigned char *tag, unsigned char *data, int length)
{
unsigned char c[8];
/* Copy tag and length int c, so that we need only 1 write system call
for these two values */
memcpy(c,tag,4);
long2str(c+4,length);
/* Output tag, length and data, restore previous position
if the write fails */
length = PAD_EVEN(length);
if( avi_write(AVI->fdes,(char *)c,8) != 8 ||
avi_write(AVI->fdes,(char *)data,length) != length )
{
lseek(AVI->fdes,AVI->pos,SEEK_SET);
AVI_errno = AVI_ERR_WRITE;
return -1;
}
/* Update file position */
AVI->pos += 8 + length;
//fprintf(stderr, "pos=%lu %s\n", AVI->pos, tag);
return 0;
}
static int avi_add_index_entry(avi_t *AVI, unsigned char *tag, long flags, unsigned long pos, unsigned long len)
{
void *ptr;
if(AVI->n_idx>=AVI->max_idx) {
ptr = realloc((void *)AVI->idx,(AVI->max_idx+4096)*16);
if(ptr == 0) {
AVI_errno = AVI_ERR_NO_MEM;
return -1;
}
AVI->max_idx += 4096;
AVI->idx = (unsigned char((*)[16]) ) ptr;
}
/* Add index entry */
// fprintf(stderr, "INDEX %s %ld %lu %lu\n", tag, flags, pos, len);
memcpy(AVI->idx[AVI->n_idx],tag,4);
long2str(AVI->idx[AVI->n_idx]+ 4,flags);
long2str(AVI->idx[AVI->n_idx]+ 8, pos);
long2str(AVI->idx[AVI->n_idx]+12, len);
/* Update counter */
AVI->n_idx++;
if(len>AVI->max_len) AVI->max_len=len;
return 0;
}
//SLM
#ifndef S_IRUSR
#define S_IRWXU 00700 /* read, write, execute: owner */
#define S_IRUSR 00400 /* read permission: owner */
#define S_IWUSR 00200 /* write permission: owner */
#define S_IXUSR 00100 /* execute permission: owner */
#define S_IRWXG 00070 /* read, write, execute: group */
#define S_IRGRP 00040 /* read permission: group */
#define S_IWGRP 00020 /* write permission: group */
#define S_IXGRP 00010 /* execute permission: group */
#define S_IRWXO 00007 /* read, write, execute: other */
#define S_IROTH 00004 /* read permission: other */
#define S_IWOTH 00002 /* write permission: other */
#define S_IXOTH 00001 /* execute permission: other */
#endif
/*
AVI_open_output_file: Open an AVI File and write a bunch
of zero bytes as space for the header.
returns a pointer to avi_t on success, a zero pointer on error
*/
avi_t* AVI_open_output_file(char * filename)
{
avi_t *AVI;
int i;
int mask;
unsigned char AVI_header[HEADERBYTES];
/* Allocate the avi_t struct and zero it */
AVI = (avi_t *) malloc(sizeof(avi_t));
if(AVI==0)
{
AVI_errno = AVI_ERR_NO_MEM;
return 0;
}
memset((void *)AVI,0,sizeof(avi_t));
/* Since Linux needs a long time when deleting big files,
we do not truncate the file when we open it.
Instead it is truncated when the AVI file is closed */
mask = umask (0);
umask (mask);
#ifdef WIN32
AVI->fdes = open(filename, O_RDWR|O_CREAT|O_BINARY, (S_IRUSR | S_IWUSR) &~ mask);
#else
AVI->fdes = open(filename, O_RDWR|O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) &~ mask);
#endif
if (AVI->fdes < 0)
{
AVI_errno = AVI_ERR_OPEN;
free(AVI);
return 0;
}
/* Write out HEADERBYTES bytes, the header will go here
when we are finished with writing */
for (i=0;i<HEADERBYTES;i++) AVI_header[i] = 0;
i = avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES);
if (i != HEADERBYTES)
{
close(AVI->fdes);
AVI_errno = AVI_ERR_WRITE;
free(AVI);
return 0;
}
AVI->pos = HEADERBYTES;
AVI->mode = AVI_MODE_WRITE; /* open for writing */
//init
AVI->anum = 0;
AVI->aptr = 0;
return AVI;
}
void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor)
{
/* may only be called if file is open for writing */
if(AVI->mode==AVI_MODE_READ) return;
AVI->width = width;
AVI->height = height;
AVI->fps = fps;
if(strncmp(compressor, "RGB", 3)==0) {
memset(AVI->compressor, 0, 4);
} else {
memcpy(AVI->compressor,compressor,4);
}
AVI->compressor[4] = 0;
avi_update_header(AVI);
}
void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format, long mp3rate)
{
/* may only be called if file is open for writing */
if(AVI->mode==AVI_MODE_READ) return;
//inc audio tracks
AVI->aptr=AVI->anum;
++AVI->anum;
if(AVI->anum > AVI_MAX_TRACKS) {
fprintf(stderr, "error - only %d audio tracks supported\n", AVI_MAX_TRACKS);
exit(1);
}
AVI->track[AVI->aptr].a_chans = channels;
AVI->track[AVI->aptr].a_rate = rate;
AVI->track[AVI->aptr].a_bits = bits;
AVI->track[AVI->aptr].a_fmt = format;
AVI->track[AVI->aptr].mp3rate = mp3rate;
avi_update_header(AVI);
}
#define OUT4CC(s) \
if (nhb <= HEADERBYTES-4) \
memcpy(AVI_header+nhb,s,4);\
nhb += 4
#define OUTLONG(n) \
if (nhb<=HEADERBYTES-4) \
long2str(AVI_header+nhb,n);\
nhb += 4
#define OUTSHRT(n) \
if (nhb<=HEADERBYTES-2) { \
AVI_header[nhb ] = (n )&0xff; \
AVI_header[nhb+1] = (n>>8)&0xff; \
} \
nhb += 2
//ThOe write preliminary AVI file header: 0 frames, max vid/aud size
int avi_update_header(avi_t *AVI)
{
int njunk, sampsize, hasIndex, ms_per_frame, frate, flag;
int movi_len, hdrl_start, strl_start, j;
unsigned char AVI_header[HEADERBYTES];
long nhb;
//assume max size
movi_len = AVI_MAX_LEN - HEADERBYTES + 4;
//assume index will be written
hasIndex=1;
if(AVI->fps < 0.001) {
frate=0;
ms_per_frame=0;
} else {
frate = (int) (FRAME_RATE_SCALE*AVI->fps + 0.5);
ms_per_frame=(int) (1000000/AVI->fps + 0.5);
}
/* Prepare the file header */
nhb = 0;
/* The RIFF header */
OUT4CC ("RIFF");
OUTLONG(movi_len); // assume max size
OUT4CC ("AVI ");
/* Start the header list */
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
hdrl_start = nhb; /* Store start position */
OUT4CC ("hdrl");
/* The main AVI header */
/* The Flags in AVI File header */
#define AVIF_HASINDEX 0x00000010 /* Index at end of file */
#define AVIF_MUSTUSEINDEX 0x00000020
#define AVIF_ISINTERLEAVED 0x00000100
#define AVIF_TRUSTCKTYPE 0x00000800 /* Use CKType to find key frames */
#define AVIF_WASCAPTUREFILE 0x00010000
#define AVIF_COPYRIGHTED 0x00020000
OUT4CC ("avih");
OUTLONG(56); /* # of bytes to follow */
OUTLONG(ms_per_frame); /* Microseconds per frame */
//ThOe ->0
// OUTLONG(10000000); /* MaxBytesPerSec, I hope this will never be used */
OUTLONG(0);
OUTLONG(0); /* PaddingGranularity (whatever that might be) */
/* Other sources call it 'reserved' */
flag = AVIF_ISINTERLEAVED;
if(hasIndex) flag |= AVIF_HASINDEX;
if(hasIndex && AVI->must_use_index) flag |= AVIF_MUSTUSEINDEX;
OUTLONG(flag); /* Flags */
OUTLONG(0); // no frames yet
OUTLONG(0); /* InitialFrames */
OUTLONG(AVI->anum+1);
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(AVI->width); /* Width */
OUTLONG(AVI->height); /* Height */
/* MS calls the following 'reserved': */
OUTLONG(0); /* TimeScale: Unit used to measure time */
OUTLONG(0); /* DataRate: Data rate of playback */
OUTLONG(0); /* StartTime: Starting time of AVI data */
OUTLONG(0); /* DataLength: Size of AVI data chunk */
/* Start the video stream list ---------------------------------- */
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
strl_start = nhb; /* Store start position */
OUT4CC ("strl");
/* The video stream header */
OUT4CC ("strh");
OUTLONG(56); /* # of bytes to follow */
OUT4CC ("vids"); /* Type */
OUT4CC (AVI->compressor); /* Handler */
OUTLONG(0); /* Flags */
OUTLONG(0); /* Reserved, MS says: wPriority, wLanguage */
OUTLONG(0); /* InitialFrames */
OUTLONG(FRAME_RATE_SCALE); /* Scale */
OUTLONG(frate); /* Rate: Rate/Scale == samples/second */
OUTLONG(0); /* Start */
OUTLONG(0); // no frames yet
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(-1); /* Quality */
OUTLONG(0); /* SampleSize */
OUTLONG(0); /* Frame */
OUTLONG(0); /* Frame */
// OUTLONG(0); /* Frame */
//OUTLONG(0); /* Frame */
/* The video stream format */
OUT4CC ("strf");
OUTLONG(40); /* # of bytes to follow */
OUTLONG(40); /* Size */
OUTLONG(AVI->width); /* Width */
OUTLONG(AVI->height); /* Height */
OUTSHRT(1); OUTSHRT(24); /* Planes, Count */
OUT4CC (AVI->compressor); /* Compression */
// ThOe (*3)
OUTLONG(AVI->width*AVI->height*3); /* SizeImage (in bytes?) */
OUTLONG(0); /* XPelsPerMeter */
OUTLONG(0); /* YPelsPerMeter */
OUTLONG(0); /* ClrUsed: Number of colors used */
OUTLONG(0); /* ClrImportant: Number of colors important */
/* Finish stream list, i.e. put number of bytes in the list to proper pos */
long2str(AVI_header+strl_start-4,nhb-strl_start);
/* Start the audio stream list ---------------------------------- */
for(j=0; j<AVI->anum; ++j) {
sampsize = avi_sampsize(AVI, j);
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
strl_start = nhb; /* Store start position */
OUT4CC ("strl");
/* The audio stream header */
OUT4CC ("strh");
OUTLONG(56); /* # of bytes to follow */
OUT4CC ("auds");
// -----------
// ThOe
OUTLONG(0); /* Format (Optionally) */
// -----------
OUTLONG(0); /* Flags */
OUTLONG(0); /* Reserved, MS says: wPriority, wLanguage */
OUTLONG(0); /* InitialFrames */
// ThOe /4
OUTLONG(sampsize/4); /* Scale */
OUTLONG(1000*AVI->track[j].mp3rate/8);
OUTLONG(0); /* Start */
OUTLONG(4*AVI->track[j].audio_bytes/sampsize); /* Length */
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(-1); /* Quality */
// ThOe /4
OUTLONG(sampsize/4); /* SampleSize */
OUTLONG(0); /* Frame */
OUTLONG(0); /* Frame */
// OUTLONG(0); /* Frame */
//OUTLONG(0); /* Frame */
/* The audio stream format */
OUT4CC ("strf");
OUTLONG(16); /* # of bytes to follow */
OUTSHRT(AVI->track[j].a_fmt); /* Format */
OUTSHRT(AVI->track[j].a_chans); /* Number of channels */
OUTLONG(AVI->track[j].a_rate); /* SamplesPerSec */
// ThOe
OUTLONG(1000*AVI->track[j].mp3rate/8);
//ThOe (/4)
OUTSHRT(sampsize/4); /* BlockAlign */
OUTSHRT(AVI->track[j].a_bits); /* BitsPerSample */
/* Finish stream list, i.e. put number of bytes in the list to proper pos */
long2str(AVI_header+strl_start-4,nhb-strl_start);
}
/* Finish header list */
long2str(AVI_header+hdrl_start-4,nhb-hdrl_start);
/* Calculate the needed amount of junk bytes, output junk */
njunk = HEADERBYTES - nhb - 8 - 12;
/* Safety first: if njunk <= 0, somebody has played with
HEADERBYTES without knowing what (s)he did.
This is a fatal error */
if(njunk<=0)
{
fprintf(stderr,"AVI_close_output_file: # of header bytes too small\n");
exit(1);
}
OUT4CC ("JUNK");
OUTLONG(njunk);
memset(AVI_header+nhb,0,njunk);
//2001-11-14 added id string
if(njunk > strlen(id_str)+8) {
//sprintf(id_str, "%s-%s", PACKAGE, VERSION);
//memcpy(AVI_header+nhb, id_str, strlen(id_str));
}
nhb += njunk;
/* Start the movi list */
OUT4CC ("LIST");
OUTLONG(movi_len); /* Length of list in bytes */
OUT4CC ("movi");
/* Output the header, truncate the file to the number of bytes
actually written, report an error if someting goes wrong */
if ( lseek(AVI->fdes,0,SEEK_SET)<0 ||
avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES ||
lseek(AVI->fdes,AVI->pos,SEEK_SET)<0)
{
AVI_errno = AVI_ERR_CLOSE;
return -1;
}
return 0;
}
/*
Write the header of an AVI file and close it.
returns 0 on success, -1 on write error.
*/
static int avi_close_output_file(avi_t *AVI)
{
int ret, njunk, sampsize, hasIndex, ms_per_frame, frate, idxerror, flag;
unsigned long movi_len;
int hdrl_start, strl_start, j;
unsigned char AVI_header[HEADERBYTES];
long nhb;
#ifdef INFO_LIST
long info_len;
// time_t calptr;
#endif
/* Calculate length of movi list */
movi_len = AVI->pos - HEADERBYTES + 4;
/* Try to ouput the index entries. This may fail e.g. if no space
is left on device. We will report this as an error, but we still
try to write the header correctly (so that the file still may be
readable in the most cases */
idxerror = 0;
// fprintf(stderr, "pos=%lu, index_len=%ld \n", AVI->pos, AVI->n_idx*16);
ret = avi_add_chunk(AVI, (unsigned char *)"idx1", (unsigned char *)AVI->idx, AVI->n_idx*16);
hasIndex = (ret==0);
//fprintf(stderr, "pos=%lu, index_len=%d\n", AVI->pos, hasIndex);
if(ret) {
idxerror = 1;
AVI_errno = AVI_ERR_WRITE_INDEX;
}
/* Calculate Microseconds per frame */
if(AVI->fps < 0.001) {
frate=0;
ms_per_frame=0;
} else {
frate = (int) (FRAME_RATE_SCALE*AVI->fps + 0.5);
ms_per_frame=(int) (1000000/AVI->fps + 0.5);
}
/* Prepare the file header */
nhb = 0;
/* The RIFF header */
OUT4CC ("RIFF");
OUTLONG(AVI->pos - 8); /* # of bytes to follow */
OUT4CC ("AVI ");
/* Start the header list */
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
hdrl_start = nhb; /* Store start position */
OUT4CC ("hdrl");
/* The main AVI header */
/* The Flags in AVI File header */
#define AVIF_HASINDEX 0x00000010 /* Index at end of file */
#define AVIF_MUSTUSEINDEX 0x00000020
#define AVIF_ISINTERLEAVED 0x00000100
#define AVIF_TRUSTCKTYPE 0x00000800 /* Use CKType to find key frames */
#define AVIF_WASCAPTUREFILE 0x00010000
#define AVIF_COPYRIGHTED 0x00020000
OUT4CC ("avih");
OUTLONG(56); /* # of bytes to follow */
OUTLONG(ms_per_frame); /* Microseconds per frame */
//ThOe ->0
// OUTLONG(10000000); /* MaxBytesPerSec, I hope this will never be used */
OUTLONG(0);
OUTLONG(0); /* PaddingGranularity (whatever that might be) */
/* Other sources call it 'reserved' */
flag = AVIF_ISINTERLEAVED;
if(hasIndex) flag |= AVIF_HASINDEX;
if(hasIndex && AVI->must_use_index) flag |= AVIF_MUSTUSEINDEX;
OUTLONG(flag); /* Flags */
OUTLONG(AVI->video_frames); /* TotalFrames */
OUTLONG(0); /* InitialFrames */
OUTLONG(AVI->anum+1);
// if (AVI->track[0].audio_bytes)
// { OUTLONG(2); } /* Streams */
// else
// { OUTLONG(1); } /* Streams */
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(AVI->width); /* Width */
OUTLONG(AVI->height); /* Height */
/* MS calls the following 'reserved': */
OUTLONG(0); /* TimeScale: Unit used to measure time */
OUTLONG(0); /* DataRate: Data rate of playback */
OUTLONG(0); /* StartTime: Starting time of AVI data */
OUTLONG(0); /* DataLength: Size of AVI data chunk */
/* Start the video stream list ---------------------------------- */
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
strl_start = nhb; /* Store start position */
OUT4CC ("strl");
/* The video stream header */
OUT4CC ("strh");
OUTLONG(56); /* # of bytes to follow */
OUT4CC ("vids"); /* Type */
OUT4CC (AVI->compressor); /* Handler */
OUTLONG(0); /* Flags */
OUTLONG(0); /* Reserved, MS says: wPriority, wLanguage */
OUTLONG(0); /* InitialFrames */
OUTLONG(FRAME_RATE_SCALE); /* Scale */
OUTLONG(frate); /* Rate: Rate/Scale == samples/second */
OUTLONG(0); /* Start */
OUTLONG(AVI->video_frames); /* Length */
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(-1); /* Quality */
OUTLONG(0); /* SampleSize */
OUTLONG(0); /* Frame */
OUTLONG(0); /* Frame */
// OUTLONG(0); /* Frame */
//OUTLONG(0); /* Frame */
/* The video stream format */
OUT4CC ("strf");
OUTLONG(40); /* # of bytes to follow */
OUTLONG(40); /* Size */
OUTLONG(AVI->width); /* Width */
OUTLONG(AVI->height); /* Height */
OUTSHRT(1); OUTSHRT(24); /* Planes, Count */
OUT4CC (AVI->compressor); /* Compression */
// ThOe (*3)
OUTLONG(AVI->width*AVI->height*3); /* SizeImage (in bytes?) */
OUTLONG(0); /* XPelsPerMeter */
OUTLONG(0); /* YPelsPerMeter */
OUTLONG(0); /* ClrUsed: Number of colors used */
OUTLONG(0); /* ClrImportant: Number of colors important */
/* Finish stream list, i.e. put number of bytes in the list to proper pos */
long2str(AVI_header+strl_start-4,nhb-strl_start);
/* Start the audio stream list ---------------------------------- */
for(j=0; j<AVI->anum; ++j) {
//if (AVI->track[j].a_chans && AVI->track[j].audio_bytes)
{
sampsize = avi_sampsize(AVI, j);
OUT4CC ("LIST");
OUTLONG(0); /* Length of list in bytes, don't know yet */
strl_start = nhb; /* Store start position */
OUT4CC ("strl");
/* The audio stream header */
OUT4CC ("strh");
OUTLONG(56); /* # of bytes to follow */
OUT4CC ("auds");
// -----------
// ThOe
OUTLONG(0); /* Format (Optionally) */
// -----------
OUTLONG(0); /* Flags */
OUTLONG(0); /* Reserved, MS says: wPriority, wLanguage */
OUTLONG(0); /* InitialFrames */
// ThOe /4
OUTLONG(sampsize/4); /* Scale */
OUTLONG(1000*AVI->track[j].mp3rate/8);
OUTLONG(0); /* Start */
OUTLONG(4*AVI->track[j].audio_bytes/sampsize); /* Length */
OUTLONG(0); /* SuggestedBufferSize */
OUTLONG(-1); /* Quality */
// ThOe /4
OUTLONG(sampsize/4); /* SampleSize */
OUTLONG(0); /* Frame */
OUTLONG(0); /* Frame */
// OUTLONG(0); /* Frame */
//OUTLONG(0); /* Frame */
/* The audio stream format */
OUT4CC ("strf");
OUTLONG(16); /* # of bytes to follow */
OUTSHRT(AVI->track[j].a_fmt); /* Format */
OUTSHRT(AVI->track[j].a_chans); /* Number of channels */
OUTLONG(AVI->track[j].a_rate); /* SamplesPerSec */
// ThOe
OUTLONG(1000*AVI->track[j].mp3rate/8);
//ThOe (/4)
OUTSHRT(sampsize/4); /* BlockAlign */
OUTSHRT(AVI->track[j].a_bits); /* BitsPerSample */
/* Finish stream list, i.e. put number of bytes in the list to proper pos */
}
long2str(AVI_header+strl_start-4,nhb-strl_start);
}
/* Finish header list */
long2str(AVI_header+hdrl_start-4,nhb-hdrl_start);
// add INFO list --- (0.6.0pre4)
#ifdef INFO_LIST
OUT4CC ("LIST");
//FIXME
info_len = MAX_INFO_STRLEN + 12;
OUTLONG(info_len);
OUT4CC ("INFO");
// OUT4CC ("INAM");
// OUTLONG(MAX_INFO_STRLEN);
// sprintf(id_str, "\t");
// memset(AVI_header+nhb, 0, MAX_INFO_STRLEN);
// memcpy(AVI_header+nhb, id_str, strlen(id_str));
// nhb += MAX_INFO_STRLEN;
OUT4CC ("ISFT");
OUTLONG(MAX_INFO_STRLEN);
//sprintf(id_str, "%s-%s", PACKAGE, VERSION);
memset(AVI_header+nhb, 0, MAX_INFO_STRLEN);
//memcpy(AVI_header+nhb, id_str, strlen(id_str));
nhb += MAX_INFO_STRLEN;
// OUT4CC ("ICMT");
// OUTLONG(MAX_INFO_STRLEN);
// calptr=time(NULL);
// sprintf(id_str, "\t%s %s", ctime(&calptr), "");
// memset(AVI_header+nhb, 0, MAX_INFO_STRLEN);
// memcpy(AVI_header+nhb, id_str, 25);
// nhb += MAX_INFO_STRLEN;
#endif
// ----------------------------
/* Calculate the needed amount of junk bytes, output junk */
njunk = HEADERBYTES - nhb - 8 - 12;
/* Safety first: if njunk <= 0, somebody has played with
HEADERBYTES without knowing what (s)he did.
This is a fatal error */
if(njunk<=0)
{
fprintf(stderr,"AVI_close_output_file: # of header bytes too small\n");
exit(1);
}
OUT4CC ("JUNK");
OUTLONG(njunk);
memset(AVI_header+nhb,0,njunk);
nhb += njunk;
/* Start the movi list */
OUT4CC ("LIST");
OUTLONG(movi_len); /* Length of list in bytes */
OUT4CC ("movi");
/* Output the header, truncate the file to the number of bytes
actually written, report an error if someting goes wrong */
if ( lseek(AVI->fdes,0,SEEK_SET)<0 ||
avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES ||
ftruncate(AVI->fdes,AVI->pos)<0 )
{
AVI_errno = AVI_ERR_CLOSE;
return -1;
}
if(idxerror) return -1;
return 0;
}
/*
AVI_write_data:
Add video or audio data to the file;
Return values:
0 No error;
-1 Error, AVI_errno is set appropriatly;
*/
static int avi_write_data(avi_t *AVI, char *data, unsigned long length, int audio, int keyframe)
{
int n;
unsigned char astr[15];
/* Check for maximum file length */
if ( (AVI->pos + 8 + length + 8 + (AVI->n_idx+1)*16) > AVI_MAX_LEN ) {
AVI_errno = AVI_ERR_SIZELIM;
return -1;
}
/* Add index entry */
//set tag for current audio track
snprintf((char *)astr, sizeof(astr), "0%1dwb", (int)(AVI->aptr+1));
if(audio)
n = avi_add_index_entry(AVI,astr,0x00,AVI->pos,length);
else
n = avi_add_index_entry(AVI,(unsigned char *)"00db",((keyframe)?0x10:0x0),AVI->pos,length);
if(n) return -1;
/* Output tag and data */
if(audio)
n = avi_add_chunk(AVI,astr,(unsigned char *)data,length);
else
n = avi_add_chunk(AVI,(unsigned char *)"00db",(unsigned char *)data,length);
if (n) return -1;
return 0;
}
int AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe)
{
unsigned long pos;
if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
pos = AVI->pos;
if(avi_write_data(AVI,data,bytes,0,keyframe)) return -1;
AVI->last_pos = pos;
AVI->last_len = bytes;
AVI->video_frames++;
return 0;
}
int AVI_dup_frame(avi_t *AVI)
{
if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
if(AVI->last_pos==0) return 0; /* No previous real frame */
if(avi_add_index_entry(AVI,(unsigned char *)"00db",0x10,AVI->last_pos,AVI->last_len)) return -1;
AVI->video_frames++;
AVI->must_use_index = 1;
return 0;
}
int AVI_write_audio(avi_t *AVI, char *data, long bytes)
{
if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
if( avi_write_data(AVI,data,bytes,1,0) ) return -1;
AVI->track[AVI->aptr].audio_bytes += bytes;
return 0;
}
int AVI_append_audio(avi_t *AVI, char *data, long bytes)
{
long i, length, pos;
unsigned char c[4];
if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
// update last index entry:
--AVI->n_idx;
length = str2ulong(AVI->idx[AVI->n_idx]+12);
pos = str2ulong(AVI->idx[AVI->n_idx]+8);
//update;
long2str(AVI->idx[AVI->n_idx]+12,length+bytes);