bunch of small fixes, add msg.txt to about prefs
[goodguy/history.git] / cinelerra-5.0 / quicktime / stsz.c
1 #include "funcprotos.h"
2 #include "quicktime.h"
3
4
5
6 void quicktime_stsz_init(quicktime_stsz_t *stsz)
7 {
8         stsz->version = 0;
9         stsz->flags = 0;
10         stsz->sample_size = 0;
11         stsz->total_entries = 0;
12         stsz->entries_allocated = 0;
13         stsz->table = 0;
14 }
15
16 void quicktime_stsz_init_video(quicktime_t *file, quicktime_stsz_t *stsz)
17 {
18         stsz->sample_size = 0;
19         if(!stsz->entries_allocated)
20         {
21                 stsz->entries_allocated = 2048;
22                 stsz->total_entries = 0;
23                 stsz->table = (quicktime_stsz_table_t*)calloc(sizeof(quicktime_stsz_table_t),
24                         stsz->entries_allocated);
25 //printf("quicktime_stsz_init_video 1 %p\n", stsz->table);
26         }
27 }
28
29 void quicktime_stsz_init_audio(quicktime_t *file,
30         quicktime_stsz_t *stsz,
31         int channels,
32         int bits,
33         char *compressor)
34 {
35         /*stsz->sample_size = channels * bits / 8; */
36
37         stsz->table = 0;
38         stsz->sample_size = 0;
39
40 //printf("quicktime_stsz_init_audio 1 %d\n", stsz->sample_size);
41         stsz->total_entries = 0;   /* set this when closing */
42         stsz->entries_allocated = 0;
43 }
44
45 void quicktime_stsz_delete(quicktime_stsz_t *stsz)
46 {
47         if(!stsz->sample_size && stsz->total_entries) free(stsz->table);
48         stsz->table = 0;
49         stsz->total_entries = 0;
50         stsz->entries_allocated = 0;
51 }
52
53 void quicktime_stsz_dump(quicktime_stsz_t *stsz)
54 {
55         int i;
56         printf("     sample size\n");
57         printf("      version %d\n", stsz->version);
58         printf("      flags %ld\n", stsz->flags);
59         printf("      sample_size %ld\n", stsz->sample_size);
60         printf("      total_entries %ld\n", stsz->total_entries);
61
62         if(!stsz->sample_size)
63         {
64                 for(i = 0; i < stsz->total_entries; i++)
65                 {
66                         printf("       sample_size 0x%lx\n", stsz->table[i].size);
67                 }
68         }
69 }
70
71 void quicktime_read_stsz(quicktime_t *file, quicktime_stsz_t *stsz)
72 {
73         int i;
74         stsz->version = quicktime_read_char(file);
75         stsz->flags = quicktime_read_int24(file);
76         stsz->sample_size = quicktime_read_int32(file);
77         stsz->total_entries = quicktime_read_int32(file);
78         stsz->entries_allocated = stsz->total_entries;
79 //printf("quicktime_read_stsz 1 %d\n", stsz->sample_size);
80         if(!stsz->sample_size)
81         {
82                 stsz->table = (quicktime_stsz_table_t*)malloc(sizeof(quicktime_stsz_table_t) * stsz->entries_allocated);
83                 for(i = 0; i < stsz->total_entries; i++)
84                 {
85                         stsz->table[i].size = quicktime_read_int32(file);
86                 }
87         }
88 }
89
90 void quicktime_write_stsz(quicktime_t *file, quicktime_stsz_t *stsz)
91 {
92         int i;
93         quicktime_atom_t atom;
94         quicktime_atom_write_header(file, &atom, "stsz");
95
96 /* optimize if possible */
97 /* Xanim requires an unoptimized table for video. */
98 /*      if(!stsz->sample_size) */
99 /*      { */
100 /*              for(i = 0, result = 0; i < stsz->total_entries && !result; i++) */
101 /*              { */
102 /*                      if(stsz->table[i].size != stsz->table[0].size) result = 1; */
103 /*              } */
104 /*               */
105 /*              if(!result) */
106 /*              { */
107 /*                      stsz->sample_size = stsz->table[0].size; */
108 /*                      stsz->total_entries = 0; */
109 /*                      free(stsz->table); */
110 /*              } */
111 /*      } */
112
113         quicktime_write_char(file, stsz->version);
114         quicktime_write_int24(file, stsz->flags);
115
116 // Force idiosynchratic handling of fixed bitrate audio.
117 // Since audio has millions of samples it's not practical to declare a size
118 // of each sample.  Instead Quicktime stores a 1 for every sample's size and
119 // relies on the samples per chunk table to determine the chunk size.
120         quicktime_write_int32(file, stsz->sample_size);
121         quicktime_write_int32(file, stsz->total_entries);
122
123         if(!stsz->sample_size)
124         {
125                 for(i = 0; i < stsz->total_entries; i++)
126                 {
127                         quicktime_write_int32(file, stsz->table[i].size);
128                 }
129         }
130
131         quicktime_atom_write_footer(file, &atom);
132 }
133
134 // Sample starts on 0
135 void quicktime_update_stsz(quicktime_stsz_t *stsz,
136         long sample,
137         long sample_size)
138 {
139         if(!stsz->sample_size)
140         {
141                 if(sample >= stsz->entries_allocated)
142                 {
143                         stsz->entries_allocated = (sample + 1) * 2;
144 //printf("quicktime_update_stsz 1 %d %d\n", sample, sample_size);
145                         stsz->table = (quicktime_stsz_table_t *)realloc(stsz->table,
146                                 sizeof(quicktime_stsz_table_t) * stsz->entries_allocated);
147 //printf("quicktime_update_stsz 2 %d %d\n", sample, sample_size);
148                 }
149
150                 stsz->table[sample].size = sample_size;
151                 if(sample >= stsz->total_entries) stsz->total_entries = sample + 1;
152         }
153
154 //printf("quicktime_update_stsz 5 %d %d\n", sample, sample_size);
155 }
156
157
158 int quicktime_sample_size(quicktime_trak_t *trak, int sample)
159 {
160         quicktime_stsz_t *stsz = &trak->mdia.minf.stbl.stsz;
161         if( stsz->sample_size ) return stsz->sample_size;
162         if( sample < stsz->total_entries && sample >= 0 )
163                 return stsz->table[sample].size;
164         return 0;
165 }