comparison libmpdemux/demux_mkv.c @ 20140:1861d40674ca

Simplify (by using realloc with NULL parameter) and refactor code to grow array in 32-increments
author reimar
date Tue, 10 Oct 2006 15:01:11 +0000
parents d4e7b776da63
children 2e7baee989ad
comparison
equal deleted inserted replaced
20139:1ef90de62efa 20140:1861d40674ca
271 /* for e.g. "-slang ger" */ 271 /* for e.g. "-slang ger" */
272 extern char *dvdsub_lang; 272 extern char *dvdsub_lang;
273 extern char *audio_lang; 273 extern char *audio_lang;
274 extern int dvdsub_id; 274 extern int dvdsub_id;
275 275
276 /**
277 * \brief ensures there is space for at least one additional element
278 * \param array array to grow
279 * \param nelem current number of elements in array
280 * \param elsize size of one array element
281 */
282 static void grow_array(void **array, int nelem, size_t elsize) {
283 if (!(nelem & 31))
284 *array = realloc(*array, (nelem + 32) * elsize);
285 }
276 286
277 static mkv_track_t * 287 static mkv_track_t *
278 demux_mkv_find_track_by_num (mkv_demuxer_t *d, int n, int type) 288 demux_mkv_find_track_by_num (mkv_demuxer_t *d, int n, int type)
279 { 289 {
280 int i, id; 290 int i, id;
314 324
315 while (i--) 325 while (i--)
316 if (mkv_d->cluster_positions[i] == position) 326 if (mkv_d->cluster_positions[i] == position)
317 return; 327 return;
318 328
319 if (!mkv_d->cluster_positions) 329 grow_array(&mkv_d->cluster_positions, mkv_d->num_cluster_pos,
320 mkv_d->cluster_positions = malloc (32 * sizeof (uint64_t)); 330 sizeof(uint64_t));
321 else if (!(mkv_d->num_cluster_pos % 32))
322 mkv_d->cluster_positions = realloc(mkv_d->cluster_positions,
323 (mkv_d->num_cluster_pos+32)
324 * sizeof (uint64_t));
325 mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position; 331 mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position;
326 } 332 }
327 333
328 334
329 #define AAC_SYNC_EXTENSION_TYPE 0x02b7 335 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
1353 length -= l + il; 1359 length -= l + il;
1354 1360
1355 if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID 1361 if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID
1356 && pos != EBML_UINT_INVALID) 1362 && pos != EBML_UINT_INVALID)
1357 { 1363 {
1358 if (mkv_d->indexes == NULL) 1364 grow_array(&mkv_d->indexes, mkv_d->num_indexes, sizeof(mkv_index_t));
1359 mkv_d->indexes = malloc (32*sizeof (mkv_index_t));
1360 else if (mkv_d->num_indexes % 32 == 0)
1361 mkv_d->indexes = realloc (mkv_d->indexes,
1362 (mkv_d->num_indexes+32)
1363 *sizeof (mkv_index_t));
1364 mkv_d->indexes[mkv_d->num_indexes].tnum = track; 1365 mkv_d->indexes[mkv_d->num_indexes].tnum = track;
1365 mkv_d->indexes[mkv_d->num_indexes].timecode = time; 1366 mkv_d->indexes[mkv_d->num_indexes].timecode = time;
1366 mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos; 1367 mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos;
1367 mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] |+ found cue point " 1368 mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] |+ found cue point "
1368 "for track %"PRIu64": timecode %"PRIu64", filepos: %"PRIu64"\n", 1369 "for track %"PRIu64": timecode %"PRIu64", filepos: %"PRIu64"\n",
1547 len = ebml_read_length (s, &i); 1548 len = ebml_read_length (s, &i);
1548 l = len + i; 1549 l = len + i;
1549 1550
1550 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n"); 1551 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n");
1551 1552
1552 if (mkv_d->attachments == NULL) 1553 grow_array(&mkv_d->attachments, mkv_d->num_attachments,
1553 mkv_d->attachments = malloc (32*sizeof(*mkv_d->attachments)); 1554 sizeof(*mkv_d->attachments));
1554 else if (!(mkv_d->num_attachments % 32))
1555 mkv_d->attachments = realloc (mkv_d->attachments,
1556 (mkv_d->num_attachments + 32)
1557 * sizeof(*mkv_d->attachments));
1558 1555
1559 while (len > 0) 1556 while (len > 0)
1560 { 1557 {
1561 uint64_t l; 1558 uint64_t l;
1562 int il; 1559 int il;