1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
8 mpeg3_strack_t
* mpeg3_new_strack(int id
)
10 mpeg3_strack_t
*result
= calloc(sizeof(mpeg3_strack_t
), 1);
15 void mpeg3_delete_strack(mpeg3_strack_t
*ptr
)
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
);
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
);
38 void mpeg3_copy_strack(mpeg3_strack_t
*dst
, mpeg3_strack_t
*src
)
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
)
50 for(i
= 0; i
< file
->total_sstreams
; i
++)
52 if(file
->strack
[i
]->id
== id
) return file
->strack
[i
];
57 mpeg3_strack_t
* mpeg3_get_strack(mpeg3_t
*file
, int number
)
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
)
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
)
79 for(j
= file
->total_sstreams
; j
>= i
; j
--)
81 file
->strack
[j
] = file
->strack
[j
- 1];
88 file
->strack
[i
] = result
;
89 file
->total_sstreams
++;
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
);
105 memcpy(new_offsets
, dst
->offsets
, dst
->total_offsets
* sizeof(int64_t));
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
,
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
)
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
)
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
)
171 for(i
= 0; i
< strack
->total_subtitles
; i
++)
173 if(!strack
->subtitles
[i
]->active
)
174 return strack
->subtitles
[i
];
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
)
192 for(i
= 0; i
< file
->total_sstreams
; i
++)
194 mpeg3_pop_all_subtitles(file
->strack
[i
]);