Skip to content

Instantly share code, notes, and snippets.

@misiek08
Created June 27, 2024 17:06
Show Gist options
  • Save misiek08/a68fa0a906364f7d44d0982aecfa4bae to your computer and use it in GitHub Desktop.
Save misiek08/a68fa0a906364f7d44d0982aecfa4bae to your computer and use it in GitHub Desktop.
[WIP] ffmpeg open files in parallel
struct open_file_args {
OptionGroup *optG;
Scheduler *sch;
const char *inout;
int (*func)(const OptionsContext*, const char*, Scheduler*);
};
void *threaded_open_file(void *data);
static int open_files2(OptionGroupList *l, const char *inout, Scheduler *sch,
int (*open_file)(const OptionsContext*, const char*,
Scheduler*));
static int open_files(OptionGroupList *l, const char *inout, Scheduler *sch,
int (*open_file)(const OptionsContext*, const char*,
Scheduler*))
{
return open_files2(l, inout, sch, open_file);
int i, ret;
for (i = 0; i < l->nb_groups; i++) {
OptionGroup *g = &l->groups[i];
OptionsContext o;
init_options(&o);
o.g = g;
ret = parse_optgroup(&o, g, options);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
"%s.\n", inout, g->arg);
uninit_options(&o);
return ret;
}
av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
ret = open_file(&o, g->arg, sch);
uninit_options(&o);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
inout, g->arg);
return ret;
}
av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
}
return 0;
}
static int open_files2(OptionGroupList *l, const char *inout, Scheduler *sch,
int (*open_file)(const OptionsContext*, const char*,
Scheduler*))
{
int i, ret, res;
av_log(NULL, AV_LOG_WARNING, "allocating threads\n");
pthread_t *threads = malloc(l->nb_groups * sizeof(pthread_t));
for (i = 0; i < l->nb_groups; i++) {
av_log(NULL, AV_LOG_WARNING, "starting thread %d\n", i);
struct open_file_args args;
args.func = open_file;
args.sch = sch;
args.optG = &l->groups[i];
args.inout = inout;
pthread_create(&threads[i], NULL, &threaded_open_file, (void*)&args);
}
for (i = 0; i < l->nb_groups; i++) {
av_log(NULL, AV_LOG_WARNING, "joining thread %d\n", i);
void *untyped_res;
av_log(NULL, AV_LOG_WARNING, "reading thread %d\n", i);
pthread_join(threads[i], &untyped_res);
av_log(NULL, AV_LOG_WARNING, "casting thread %d\n", i);
res = (int)untyped_res;
av_log(NULL, AV_LOG_WARNING, "checking thread %d\n", i);
if(res != 0) {
ret = res;
}
}
return ret;
}
void *threaded_open_file(void *data) {
struct open_file_args *args = (struct open_file_args*)data;
OptionGroup *g = (OptionGroup*)args->optG;
OptionsContext o;
int ret = 0;
init_options(&o);
o.g = g;
ret = parse_optgroup(&o, g, options);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
"%s.\n", args->inout, g->arg);
uninit_options(&o);
return (void*)ret;
}
av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", args->inout, g->arg);
ret = args->func(&o, g->arg, args->sch);
uninit_options(&o);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
args->inout, g->arg);
return (void*)ret;
}
av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment