00001 #include "funcprotos.h"
00002 #include "quicktime.h"
00003 #include <string.h>
00004
00005
00006 static char* make_tag(int number, char *tag)
00007 {
00008 tag[0] = 'i';
00009 tag[1] = 'x';
00010 tag[2] = '0' + (number / 10);
00011 tag[3] = '0' + (number % 10);
00012 return tag;
00013 }
00014
00015 quicktime_ix_t* quicktime_new_ix(quicktime_t *file,
00016 quicktime_trak_t *trak,
00017 quicktime_strl_t *strl)
00018 {
00019 quicktime_ix_t *ix = calloc(1, sizeof(quicktime_ix_t));
00020 ix->base_offset = quicktime_position(file);
00021 make_tag(trak->tkhd.track_id - 1, ix->tag);
00022 ix->longs_per_entry = 2;
00023 ix->index_type = AVI_INDEX_OF_CHUNKS;
00024 memcpy(ix->chunk_id, strl->tag, 4);
00025 return ix;
00026 }
00027
00028
00029 void quicktime_delete_ix(quicktime_ix_t *ix)
00030 {
00031 if(ix->table) free(ix->table);
00032 free(ix);
00033 }
00034
00035 void quicktime_update_ixtable(quicktime_t *file,
00036 quicktime_trak_t *trak,
00037 int64_t offset,
00038 int size)
00039 {
00040 quicktime_riff_t *riff = file->riff[file->total_riffs - 1];
00041 quicktime_movi_t *movi = &riff->movi;
00042 quicktime_ix_t *ix = movi->ix[trak->tkhd.track_id - 1];
00043 quicktime_ixtable_t *ix_table;
00044
00045
00046 if(ix->table_size >= ix->table_allocation)
00047 {
00048 quicktime_ixtable_t *old_table = ix->table;
00049 int new_allocation = ix->table_allocation * 2;
00050 if(new_allocation < 1) new_allocation = 1;
00051 ix->table = calloc(1, sizeof(quicktime_ixtable_t) * new_allocation);
00052 if(old_table)
00053 {
00054 memcpy(ix->table, old_table, sizeof(quicktime_ixtable_t) * ix->table_size);
00055 free(old_table);
00056 }
00057 ix->table_allocation = new_allocation;
00058 }
00059
00060
00061 ix_table = &ix->table[ix->table_size++];
00062 ix_table->relative_offset = offset - ix->base_offset;
00063 ix_table->size = size;
00064 }
00065
00066
00067 void quicktime_write_ix(quicktime_t *file,
00068 quicktime_ix_t *ix,
00069 int track)
00070 {
00071 int i;
00072 quicktime_atom_write_header(file, &ix->atom, ix->tag);
00073
00074
00075 quicktime_write_int16_le(file, ix->longs_per_entry);
00076
00077 quicktime_write_char(file, 0);
00078
00079 quicktime_write_char(file, ix->index_type);
00080
00081 quicktime_write_int32_le(file, ix->table_size);
00082
00083 quicktime_write_char32(file, ix->chunk_id);
00084
00085 quicktime_write_int64_le(file, ix->base_offset);
00086
00087 quicktime_write_int32_le(file, 0);
00088
00089
00090 for(i = 0; i < ix->table_size; i++)
00091 {
00092 quicktime_ixtable_t *table = &ix->table[i];
00093 quicktime_write_int32_le(file, table->relative_offset);
00094 quicktime_write_int32_le(file, table->size);
00095 }
00096
00097 quicktime_atom_write_footer(file, &ix->atom);
00098
00099
00100
00101 quicktime_riff_t *riff = file->riff[0];
00102 quicktime_hdrl_t *hdrl = &riff->hdrl;
00103 quicktime_strl_t *strl = hdrl->strl[track];
00104 quicktime_indx_t *indx = &strl->indx;
00105
00106 quicktime_update_indx(file, indx, ix);
00107 }
00108
00109 void quicktime_read_ix(quicktime_t *file,
00110 quicktime_ix_t *ix)
00111 {
00112 int i;
00113 quicktime_atom_t leaf_atom;
00114 quicktime_atom_read_header(file, &leaf_atom);
00115
00116 ix->longs_per_entry = quicktime_read_int16_le(file);
00117
00118 quicktime_read_char(file);
00119 ix->index_type = quicktime_read_char(file);
00120 ix->table_size = quicktime_read_int32_le(file);
00121 quicktime_read_char32(file, ix->chunk_id);
00122 ix->base_offset = quicktime_read_int64_le(file);
00123
00124 quicktime_read_int32_le(file);
00125
00126 ix->table = calloc(ix->table_size, sizeof(quicktime_ixtable_t));
00127
00128 for(i = 0; i < ix->table_size; i++)
00129 {
00130 quicktime_ixtable_t *ixtable = &ix->table[i];
00131 ixtable->relative_offset = quicktime_read_int32_le(file);
00132 ixtable->size = quicktime_read_int32_le(file);
00133 }
00134 }
00135
00136
00137
00138
00139
00140
00141