Skip to content

Instantly share code, notes, and snippets.

@dwbuiten
Created September 17, 2014 14:47
Show Gist options
  • Save dwbuiten/4df06520f66ce0394afa to your computer and use it in GitHub Desktop.
Save dwbuiten/4df06520f66ce0394afa to your computer and use it in GitHub Desktop.
L-SMASH ts extraction
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lsmash.h>
typedef struct ts {
uint64_t dts;
uint64_t cts;
} ts;
static int tscmp(const void* p1, const void* p2) {
ts *one = (ts *) p1;
ts *two = (ts *) p2;
if (one->cts == two->cts)
return 0;
else if (one->cts < two->cts)
return -1;
else
return 1;
}
int main(int argc, char **argv) {
lsmash_root_t *root = NULL;
lsmash_file_t *file;
lsmash_file_parameters_t fparam = { 0 };
lsmash_movie_parameters_t mparam;
lsmash_media_parameters_t meparam;
lsmash_media_ts_list_t timestamps;
uint32_t trackid;
uint32_t shift;
uint32_t i;
int order;
int track;
int ret;
if (argc < 3) {
fprintf(stderr, "Usage: %s filename tracknumber [order]\n"
" order = 0 (dts), 1 (cts). default 0.\n", argv[0]);
goto err;
}
track = atoi(argv[2]);
if (argc >= 4)
order = atoi(argv[3]);
else
order = 0;
root = lsmash_create_root();
if (!root) {
fprintf(stderr, "Cannot allocate L-SMASH root.\n");
goto err;
}
ret = lsmash_open_file(argv[1], 1, &fparam);
if (ret < 0) {
fprintf(stderr, "Cannot open %s.\n", argv[1]);
goto err;
}
file = lsmash_set_file(root, &fparam);
if (!file) {
fprintf(stderr, "Cannot set file.\n");
goto err;
}
ret = lsmash_read_file(file, &fparam);
if (ret < 0) {
fprintf(stderr, "Cannot read file.\n");
}
lsmash_initialize_movie_parameters(&mparam);
lsmash_get_movie_parameters(root, &mparam);
trackid = lsmash_get_track_ID(root, track + 1);
if (!trackid) {
fprintf(stderr, "Track %d does not exist.\n", track);
goto err;
}
ret = lsmash_get_media_parameters(root, trackid, &meparam);
if (ret < 0) {
fprintf(stderr, "Cannot find media parameters for track %d.\n", track);
goto err;
}
ret = lsmash_construct_timeline(root, trackid);
if (ret < 0) {
fprintf(stderr, "Cannot construct timeline for track %d.\n", track);
goto err;
}
ret = lsmash_get_composition_to_decode_shift_from_media_timeline(root, trackid, &shift);
if (ret < 0) {
fprintf(stderr, "Cannot get timeline shift for track %d.\n", track);
goto err;
}
ret = lsmash_get_media_timestamps(root, trackid, &timestamps);
if (ret < 0 || !timestamps.timestamp) {
fprintf(stderr, "Cannot get timestamps for tarck %d.\n", track);
goto err;
}
printf("timescale:%"PRIu32"\n", meparam.timescale);
if (!order) {
for (i = 0; i < timestamps.sample_count; i++)
printf("dts:%"PRIu64"|cts:%"PRIu64"\n",
timestamps.timestamp[i].dts,
timestamps.timestamp[i].cts + shift);
} else {
ts *stamps = malloc(timestamps.sample_count * sizeof(*stamps));
if (!stamps) {
fprintf(stderr, "Could not allocate timestamp array.\n");
lsmash_free(timestamps.timestamp);
goto err;
}
for (i = 0; i < timestamps.sample_count; i++) {
stamps[i].dts = timestamps.timestamp[i].dts;
stamps[i].cts = timestamps.timestamp[i].cts + shift;
}
qsort(stamps, timestamps.sample_count, sizeof(*stamps), tscmp);
for (i = 0; i < timestamps.sample_count; i++)
printf("dts:%"PRIu64"|cts:%"PRIu64"\n",
stamps[i].dts, stamps[i].cts);
free(stamps);
}
lsmash_free(timestamps.timestamp);
lsmash_destroy_root(root);
lsmash_close_file(&fparam);
return 0;
err:
if (root)
lsmash_destroy_root(root);
lsmash_close_file(&fparam);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment