repair flickering video encode, bug created last checkin
[goodguy/history.git] / cinelerra-5.0 / quicktime / stco.c
1 #include "funcprotos.h"
2 #include "quicktime.h"
3
4
5
6 void quicktime_stco_init(quicktime_stco_t *stco)
7 {
8         stco->version = 0;
9         stco->flags = 0;
10         stco->total_entries = 0;
11         stco->entries_allocated = 0;
12 }
13
14 void quicktime_stco_delete(quicktime_stco_t *stco)
15 {
16         if(stco->total_entries) free(stco->table);
17         stco->total_entries = 0;
18         stco->entries_allocated = 0;
19 }
20
21 void quicktime_stco_init_common(quicktime_t *file, quicktime_stco_t *stco)
22 {
23         if(!stco->entries_allocated)
24         {
25                 stco->entries_allocated = 2048;
26                 stco->total_entries = 0;
27                 stco->table = (quicktime_stco_table_t*)malloc(sizeof(quicktime_stco_table_t) * stco->entries_allocated);
28 /*printf("quicktime_stco_init_common %x\n", stco->table); */
29         }
30 }
31
32 void quicktime_stco_dump(quicktime_stco_t *stco)
33 {
34         int i;
35         printf("     chunk offset\n");
36         printf("      version %d\n", stco->version);
37         printf("      flags %ld\n", stco->flags);
38         printf("      total_entries %ld\n", stco->total_entries);
39         for(i = 0; i < stco->total_entries; i++)
40         {
41                 printf("       offset %d 0x%jx\n", i, stco->table[i].offset);
42         }
43 }
44
45 void quicktime_read_stco(quicktime_t *file, quicktime_stco_t *stco)
46 {
47         int i;
48         stco->version = quicktime_read_char(file);
49         stco->flags = quicktime_read_int24(file);
50         stco->total_entries = quicktime_read_int32(file);
51         stco->entries_allocated = stco->total_entries;
52         stco->table = (quicktime_stco_table_t*)calloc(1, sizeof(quicktime_stco_table_t) * stco->entries_allocated);
53         for(i = 0; i < stco->total_entries; i++)
54         {
55                 stco->table[i].offset = quicktime_read_uint32(file);
56         }
57 }
58
59 void quicktime_read_stco64(quicktime_t *file, quicktime_stco_t *stco)
60 {
61         int i;
62         stco->version = quicktime_read_char(file);
63         stco->flags = quicktime_read_int24(file);
64         stco->total_entries = quicktime_read_int32(file);
65         stco->entries_allocated = stco->total_entries;
66         stco->table = (quicktime_stco_table_t*)calloc(1, sizeof(quicktime_stco_table_t) * stco->entries_allocated);
67         for(i = 0; i < stco->total_entries; i++)
68         {
69                 stco->table[i].offset = quicktime_read_int64(file);
70         }
71 }
72
73 void quicktime_write_stco(quicktime_t *file, quicktime_stco_t *stco)
74 {
75         int i;
76         quicktime_atom_t atom;
77 //      quicktime_atom_write_header(file, &atom, "stco");
78         quicktime_atom_write_header(file, &atom, "co64");
79
80         quicktime_write_char(file, stco->version);
81         quicktime_write_int24(file, stco->flags);
82         quicktime_write_int32(file, stco->total_entries);
83         for(i = 0; i < stco->total_entries; i++)
84         {
85                 quicktime_write_int64(file, stco->table[i].offset);
86         }
87
88         quicktime_atom_write_footer(file, &atom);
89 }
90
91 // Chunk starts at 1
92 void quicktime_update_stco(quicktime_stco_t *stco, long chunk, int64_t offset)
93 {
94         if(chunk <= 0)
95                 printf("quicktime_update_stco chunk must start at 1. chunk=%ld\n", chunk);
96
97         if(chunk > stco->entries_allocated)
98         {
99                 stco->entries_allocated = chunk * 2;
100 //printf("quicktime_update_stco 1\n");
101                 stco->table = (quicktime_stco_table_t*)realloc(stco->table, 
102                         sizeof(quicktime_stco_table_t) * stco->entries_allocated);
103 //printf("quicktime_update_stco 2\n");
104         }
105         
106         stco->table[chunk - 1].offset = offset;
107         if(chunk > stco->total_entries) stco->total_entries = chunk;
108 }
109