allow ffmpeg video to resample curr_pos, add bluray format
[goodguy/history.git] / cinelerra-5.0 / quicktime / avi_ix.c
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include <string.h>
4
5
6 static char* make_tag(int number, char *tag)
7 {
8         tag[0] = 'i';
9         tag[1] = 'x';
10         tag[2] = '0' + (number / 10);
11         tag[3] = '0' + (number % 10);
12         return tag;
13 }
14
15 quicktime_ix_t* quicktime_new_ix(quicktime_t *file, 
16         quicktime_trak_t *trak,
17         quicktime_strl_t *strl)
18 {
19         quicktime_ix_t *ix = calloc(1, sizeof(quicktime_ix_t));
20         ix->base_offset = quicktime_position(file);
21         make_tag(trak->tkhd.track_id - 1, ix->tag);
22         ix->longs_per_entry = 2;
23         ix->index_type = AVI_INDEX_OF_CHUNKS;
24         memcpy(ix->chunk_id, strl->tag, 4);
25         return ix;
26 }
27
28
29 void quicktime_delete_ix(quicktime_ix_t *ix)
30 {
31         if(ix->table) free(ix->table);
32         free(ix);
33 }
34
35 void quicktime_update_ixtable(quicktime_t *file, 
36         quicktime_trak_t *trak, 
37         int64_t offset,
38         int size)
39 {
40         quicktime_riff_t *riff = file->riff[file->total_riffs - 1];
41         quicktime_movi_t *movi = &riff->movi;
42         quicktime_ix_t *ix = movi->ix[trak->tkhd.track_id - 1];
43         quicktime_ixtable_t *ix_table;
44
45 /* Allocation */
46         if(ix->table_size >= ix->table_allocation)
47         {
48                 quicktime_ixtable_t *old_table = ix->table;
49                 int new_allocation = ix->table_allocation * 2;
50                 if(new_allocation < 1) new_allocation = 1;
51                 ix->table = calloc(1, sizeof(quicktime_ixtable_t) * new_allocation);
52                 if(old_table)
53                 {
54                         memcpy(ix->table, old_table, sizeof(quicktime_ixtable_t) * ix->table_size);
55                         free(old_table);
56                 }
57                 ix->table_allocation = new_allocation;
58         }
59
60 /* Appendage */
61         ix_table = &ix->table[ix->table_size++];
62         ix_table->relative_offset = offset - ix->base_offset;
63         ix_table->size = size;
64 }
65
66
67 void quicktime_write_ix(quicktime_t *file,
68         quicktime_ix_t *ix,
69         int track)
70 {
71         int i;
72         quicktime_atom_write_header(file, &ix->atom, ix->tag);
73
74 /* longs per entry */
75         quicktime_write_int16_le(file, ix->longs_per_entry);
76 /* index sub type */
77         quicktime_write_char(file, 0);
78 /* index type */
79         quicktime_write_char(file, ix->index_type);
80 /* entries in use */
81         quicktime_write_int32_le(file, ix->table_size);
82 /* chunk ID */
83         quicktime_write_char32(file, ix->chunk_id);
84 /* base offset */
85         quicktime_write_int64_le(file, ix->base_offset);
86 /* reserved */
87         quicktime_write_int32_le(file, 0);
88
89 /* table */
90         for(i = 0; i < ix->table_size; i++)
91         {
92                 quicktime_ixtable_t *table = &ix->table[i];
93                 quicktime_write_int32_le(file, table->relative_offset);
94                 quicktime_write_int32_le(file, table->size);
95         }
96
97         quicktime_atom_write_footer(file, &ix->atom);
98
99
100 /* Update super index */
101         quicktime_riff_t *riff = file->riff[0];
102         quicktime_hdrl_t *hdrl = &riff->hdrl;
103         quicktime_strl_t *strl = hdrl->strl[track];
104         quicktime_indx_t *indx = &strl->indx;
105
106         quicktime_update_indx(file, indx, ix);
107 }
108
109 void quicktime_read_ix(quicktime_t *file,
110         quicktime_ix_t *ix)
111 {
112         int i;
113         quicktime_atom_t leaf_atom;
114         quicktime_atom_read_header(file, &leaf_atom);
115
116         ix->longs_per_entry = quicktime_read_int16_le(file);
117 /* sub type */
118         quicktime_read_char(file);
119         ix->index_type = quicktime_read_char(file);
120         ix->table_size = quicktime_read_int32_le(file);
121         quicktime_read_char32(file, ix->chunk_id);
122         ix->base_offset = quicktime_read_int64_le(file);
123 /* reserved */
124         quicktime_read_int32_le(file);
125
126         ix->table = calloc(ix->table_size, sizeof(quicktime_ixtable_t));
127
128         for(i = 0; i < ix->table_size; i++)
129         {
130                 quicktime_ixtable_t *ixtable = &ix->table[i];
131                 ixtable->relative_offset = quicktime_read_int32_le(file);
132                 ixtable->size = quicktime_read_int32_le(file);
133         }
134 }
135
136
137
138
139
140
141