plugins depending on libaffine
[cinelerra_cv/mob.git] / libmpeg3 / mpeg3strack.c
blobfbb56bf0356a1cf44eeba2eb7d5da2f66a91511a
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
4 #include <stdlib.h>
5 #include <string.h>
8 mpeg3_strack_t* mpeg3_new_strack(int id)
10 mpeg3_strack_t *result = calloc(sizeof(mpeg3_strack_t), 1);
11 result->id = id;
12 return result;
15 void mpeg3_delete_strack(mpeg3_strack_t *ptr)
17 int i;
18 for(i = 0; i < ptr->total_subtitles; i++)
20 mpeg3_delete_subtitle(ptr->subtitles[i]);
22 if(ptr->subtitles) free(ptr->subtitles);
23 if(ptr->offsets) free(ptr->offsets);
24 free(ptr);
27 void mpeg3_delete_subtitle(mpeg3_subtitle_t *subtitle)
29 if(subtitle->data) free(subtitle->data);
30 if(subtitle->image_y) free(subtitle->image_y);
31 if(subtitle->image_u) free(subtitle->image_u);
32 if(subtitle->image_v) free(subtitle->image_v);
33 if(subtitle->image_a) free(subtitle->image_a);
34 free(subtitle);
38 void mpeg3_copy_strack(mpeg3_strack_t *dst, mpeg3_strack_t *src)
40 dst->id = src->id;
41 dst->offsets = calloc(sizeof(int64_t) * src->allocated_offsets, 1);
42 memcpy(dst->offsets, src->offsets, sizeof(int64_t) * src->total_offsets);
43 dst->total_offsets = src->total_offsets;
44 dst->allocated_offsets = src->allocated_offsets;
47 mpeg3_strack_t* mpeg3_get_strack_id(mpeg3_t *file, int id)
49 int i;
50 for(i = 0; i < file->total_sstreams; i++)
52 if(file->strack[i]->id == id) return file->strack[i];
54 return 0;
57 mpeg3_strack_t* mpeg3_get_strack(mpeg3_t *file, int number)
59 int i;
60 if(number >= file->total_sstreams || number < 0) return 0;
61 return file->strack[number];
64 mpeg3_strack_t* mpeg3_create_strack(mpeg3_t *file, int id)
66 int i;
67 int j;
68 mpeg3_strack_t *result = 0;
70 if(!(result = mpeg3_get_strack_id(file, id)))
72 result = mpeg3_new_strack(id);
73 for(i = 0; i < file->total_sstreams; i++)
75 /* Search for first ID > current id */
76 if(file->strack[i]->id > id)
78 /* Shift back 1 */
79 for(j = file->total_sstreams; j >= i; j--)
81 file->strack[j] = file->strack[j - 1];
83 break;
87 /* Store in table */
88 file->strack[i] = result;
89 file->total_sstreams++;
92 return result;
95 void mpeg3_append_subtitle_offset(mpeg3_strack_t *dst, int64_t program_offset)
97 int new_total = dst->total_offsets + 1;
98 if(new_total >= dst->allocated_offsets)
100 int new_allocated = MAX(new_total, dst->allocated_offsets * 2);
101 int64_t *new_offsets = malloc(sizeof(int64_t) * new_allocated);
103 if(dst->offsets)
105 memcpy(new_offsets, dst->offsets, dst->total_offsets * sizeof(int64_t));
106 free(dst->offsets);
109 dst->offsets = new_offsets;
110 dst->allocated_offsets = new_allocated;
113 dst->offsets[dst->total_offsets++] = program_offset;
116 void mpeg3_append_subtitle(mpeg3_strack_t *strack, mpeg3_subtitle_t *subtitle)
118 int new_total = strack->total_subtitles + 1;
119 if(new_total >= strack->allocated_subtitles)
121 int new_allocated = MAX(new_total, strack->allocated_subtitles * 2);
122 mpeg3_subtitle_t **new_subtitles = malloc(sizeof(mpeg3_subtitle_t*) * new_allocated);
125 if(strack->subtitles)
127 memcpy(new_subtitles,
128 strack->subtitles,
129 strack->total_subtitles * sizeof(mpeg3_subtitle_t*));
130 free(strack->subtitles);
133 strack->subtitles = new_subtitles;
134 strack->allocated_subtitles = new_allocated;
137 strack->subtitles[strack->total_subtitles++] = subtitle;
140 while(strack->total_subtitles > MPEG3_MAX_SUBTITLES)
141 mpeg3_pop_subtitle(strack, 0, 1);
144 void mpeg3_pop_subtitle(mpeg3_strack_t *strack, int number, int delete_it)
146 int i;
147 if(strack->total_subtitles)
149 if(delete_it) mpeg3_delete_subtitle(strack->subtitles[number]);
150 for(i = number; i < strack->total_subtitles - 1; i++)
151 strack->subtitles[i] = strack->subtitles[i + 1];
152 strack->total_subtitles--;
157 void mpeg3_pop_all_subtitles(mpeg3_strack_t *strack)
159 int i;
160 for(i = 0; i < strack->total_subtitles; i++)
162 mpeg3_delete_subtitle(strack->subtitles[i]);
164 strack->total_subtitles = 0;
168 mpeg3_subtitle_t* mpeg3_get_subtitle(mpeg3_strack_t *strack)
170 int i;
171 for(i = 0; i < strack->total_subtitles; i++)
173 if(!strack->subtitles[i]->active)
174 return strack->subtitles[i];
176 return 0;
179 int mpeg3_subtitle_tracks(mpeg3_t *file)
181 return file->total_sstreams;
184 void mpeg3_show_subtitle(mpeg3_t *file, int track)
186 file->subtitle_track = track;
189 void mpeg3_reset_subtitles(mpeg3_t *file)
191 int i;
192 for(i = 0; i < file->total_sstreams; i++)
194 mpeg3_pop_all_subtitles(file->strack[i]);