1 /*******************************************************************************
2 stco.c
3
4 libquicktime - A library for reading and writing quicktime/avi/mp4 files.
5 http://libquicktime.sourceforge.net
6
7 Copyright (C) 2002 Heroine Virtual Ltd.
8 Copyright (C) 2002-2011 Members of the libquicktime project.
9
10 This library is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 2.1 of the License, or (at your option)
13 any later version.
14
15 This library is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 details.
19
20 You should have received a copy of the GNU Lesser General Public License along
21 with this library; if not, write to the Free Software Foundation, Inc., 51
22 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *******************************************************************************/
24
25 #include "lqt_private.h"
26 #include <stdlib.h>
27
28 #define LOG_DOMAIN "stco"
29
quicktime_stco_init(quicktime_stco_t * stco)30 void quicktime_stco_init(quicktime_stco_t *stco)
31 {
32 stco->version = 0;
33 stco->flags = 0;
34 stco->total_entries = 0;
35 stco->entries_allocated = 0;
36 }
37
quicktime_stco_delete(quicktime_stco_t * stco)38 void quicktime_stco_delete(quicktime_stco_t *stco)
39 {
40 if(stco->table) free(stco->table);
41 stco->total_entries = 0;
42 stco->entries_allocated = 0;
43 }
44
quicktime_stco_init_common(quicktime_t * file,quicktime_stco_t * stco)45 void quicktime_stco_init_common(quicktime_t *file, quicktime_stco_t *stco)
46 {
47 if(!stco->entries_allocated)
48 {
49 stco->entries_allocated = 2048;
50 stco->total_entries = 0;
51 stco->table = (quicktime_stco_table_t*)malloc(sizeof(quicktime_stco_table_t) * stco->entries_allocated);
52 }
53 }
54
quicktime_stco_dump(quicktime_stco_t * stco)55 void quicktime_stco_dump(quicktime_stco_t *stco)
56 {
57 int i;
58 if(stco->co64)
59 lqt_dump(" chunk offset (co64)\n");
60 else
61 lqt_dump(" chunk offset (stco)\n");
62 lqt_dump(" version %d\n", stco->version);
63 lqt_dump(" flags %ld\n", stco->flags);
64 lqt_dump(" total_entries %ld\n", stco->total_entries);
65 for(i = 0; i < stco->total_entries; i++)
66 {
67 lqt_dump(" offset %d %"PRId64" (%"PRIx64")\n",
68 i, stco->table[i].offset,
69 stco->table[i].offset);
70 }
71 }
72
quicktime_read_stco(quicktime_t * file,quicktime_stco_t * stco)73 void quicktime_read_stco(quicktime_t *file, quicktime_stco_t *stco)
74 {
75 int i;
76 stco->version = quicktime_read_char(file);
77 stco->flags = quicktime_read_int24(file);
78 stco->total_entries = quicktime_read_int32(file);
79 stco->entries_allocated = stco->total_entries;
80 stco->table = (quicktime_stco_table_t*)calloc(1, sizeof(quicktime_stco_table_t) * stco->entries_allocated);
81 for(i = 0; i < stco->total_entries; i++)
82 {
83 stco->table[i].offset = quicktime_read_uint32(file);
84 }
85 }
86
quicktime_read_stco64(quicktime_t * file,quicktime_stco_t * stco)87 void quicktime_read_stco64(quicktime_t *file, quicktime_stco_t *stco)
88 {
89 int i;
90 stco->version = quicktime_read_char(file);
91 stco->flags = quicktime_read_int24(file);
92 stco->total_entries = quicktime_read_int32(file);
93 stco->entries_allocated = stco->total_entries;
94 stco->table = (quicktime_stco_table_t*)calloc(1, sizeof(quicktime_stco_table_t) * stco->entries_allocated);
95 for(i = 0; i < stco->total_entries; i++)
96 {
97 stco->table[i].offset = quicktime_read_int64(file);
98 }
99 stco->co64 = 1;
100 }
101
quicktime_write_stco(quicktime_t * file,quicktime_stco_t * stco)102 void quicktime_write_stco(quicktime_t *file, quicktime_stco_t *stco)
103 {
104 int i;
105 quicktime_atom_t atom;
106
107 if(stco->co64)
108 quicktime_atom_write_header(file, &atom, "co64");
109 else
110 quicktime_atom_write_header(file, &atom, "stco");
111
112 quicktime_write_char(file, stco->version);
113 quicktime_write_int24(file, stco->flags);
114 quicktime_write_int32(file, stco->total_entries);
115
116 if(stco->co64)
117 {
118 for(i = 0; i < stco->total_entries; i++)
119 quicktime_write_int64(file, stco->table[i].offset);
120 }
121 else
122 {
123 for(i = 0; i < stco->total_entries; i++)
124 quicktime_write_int32(file, stco->table[i].offset);
125 }
126 quicktime_atom_write_footer(file, &atom);
127 }
128
quicktime_update_stco(quicktime_stco_t * stco,long chunk,int64_t offset)129 void quicktime_update_stco(quicktime_stco_t *stco, long chunk, int64_t offset)
130 {
131 // Chunk starts at 1
132 chunk++;
133 if(chunk <= 0)
134 lqt_log(NULL, LQT_LOG_ERROR, LOG_DOMAIN,
135 "quicktime_update_stco chunk must start at 1. chunk=%ld\n",
136 chunk);
137
138 if(chunk > stco->entries_allocated)
139 {
140 stco->entries_allocated = chunk * 2;
141 stco->table = (quicktime_stco_table_t*)realloc(stco->table, sizeof(quicktime_stco_table_t) * stco->entries_allocated);
142 }
143
144 stco->table[chunk - 1].offset = offset;
145 if(chunk > stco->total_entries) stco->total_entries = chunk;
146 if(offset >= 0x100000000LL)
147 stco->co64 = 1;
148 }
149
150