Skip to content

Commit

Permalink
Use ffmpeg memory routines.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jan 22, 2024
1 parent df5e634 commit da4de00
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 90 deletions.
110 changes: 55 additions & 55 deletions av/av_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void free_stream(stream_t *stream) {
if (stream->codec_context)
avcodec_free_context(&stream->codec_context);

free(stream);
av_free(stream);
}

static void close_av(av_t *av) {
Expand All @@ -111,7 +111,7 @@ static void close_av(av_t *av) {
if (av->streams[i])
free_stream(av->streams[i]);
}
free(av->streams);
av_free(av->streams);
av->streams = NULL;
}

Expand Down Expand Up @@ -146,7 +146,7 @@ static void close_av(av_t *av) {
av->closed = 1;
}

static void finalize_av(value v) { free(Av_base_val(v)); }
static void finalize_av(value v) { av_free(Av_base_val(v)); }

static struct custom_operations av_ops = {
"ocaml_av_context", finalize_av,
Expand Down Expand Up @@ -299,7 +299,7 @@ static int ocaml_avio_read_callback(void *private, uint8_t *buf, int buf_size) {
caml_exn = caml_format_exception(res);
if (caml_exn) {
exn_len = strlen(caml_exn) + 1;
c_exn = malloc(exn_len);
c_exn = av_malloc(exn_len);
if (!c_exn)
caml_raise_out_of_memory();
memcpy(c_exn, caml_exn, exn_len);
Expand All @@ -309,7 +309,7 @@ static int ocaml_avio_read_callback(void *private, uint8_t *buf, int buf_size) {
if (c_exn) {
av_log(avio->avio_context, AV_LOG_ERROR,
"Error while executing OCaml read callback: %s\n", c_exn);
free(c_exn);
av_free(c_exn);
}

caml_remove_generational_global_root(&buffer);
Expand Down Expand Up @@ -375,7 +375,7 @@ static int ocaml_avio_write_callback(void *private, uint8_t *buf,
caml_exn = caml_format_exception(res);
if (caml_exn) {
exn_len = strlen(caml_exn) + 1;
c_exn = malloc(exn_len);
c_exn = av_malloc(exn_len);
if (!c_exn)
caml_raise_out_of_memory();
memcpy(c_exn, caml_exn, exn_len);
Expand All @@ -385,7 +385,7 @@ static int ocaml_avio_write_callback(void *private, uint8_t *buf,
if (c_exn) {
av_log(avio->avio_context, AV_LOG_ERROR,
"Error while executing OCaml write callback: %s\n", c_exn);
free(c_exn);
av_free(c_exn);
}

caml_remove_generational_global_root(&buffer);
Expand Down Expand Up @@ -478,7 +478,7 @@ CAMLprim value ocaml_av_create_io(value bufsize, value _read_cb,
unsigned char *buffer;
int buffer_size;

avio_t *avio = (avio_t *)calloc(1, sizeof(avio_t));
avio_t *avio = (avio_t *)av_mallocz(sizeof(avio_t));
if (!avio)
caml_raise_out_of_memory();

Expand All @@ -489,7 +489,7 @@ CAMLprim value ocaml_av_create_io(value bufsize, value _read_cb,
avio->format_context = avformat_alloc_context();

if (!avio->format_context) {
free(avio);
av_free(avio);
caml_raise_out_of_memory();
}

Expand All @@ -498,7 +498,7 @@ CAMLprim value ocaml_av_create_io(value bufsize, value _read_cb,

if (!buffer) {
avformat_free_context(avio->format_context);
free(avio);
av_free(avio);
caml_raise_out_of_memory();
}

Expand Down Expand Up @@ -537,7 +537,7 @@ CAMLprim value ocaml_av_create_io(value bufsize, value _read_cb,

av_freep(buffer);
avformat_free_context(avio->format_context);
free(avio);
av_free(avio);
caml_raise_out_of_memory();
}

Expand Down Expand Up @@ -567,7 +567,7 @@ CAMLprim value caml_av_input_io_finalise(value _avio) {
if (avio->seek_cb)
caml_remove_generational_global_root(&avio->seek_cb);

free(avio);
av_free(avio);

CAMLreturn(Val_unit);
}
Expand All @@ -587,7 +587,7 @@ CAMLprim value ocaml_av_find_input_format(value _short_name) {
CAMLparam1(_short_name);
CAMLlocal1(ret);
char *short_name =
strndup(String_val(_short_name), caml_string_length(_short_name));
av_strndup(String_val(_short_name), caml_string_length(_short_name));

if (!short_name)
caml_raise_out_of_memory();
Expand All @@ -596,7 +596,7 @@ CAMLprim value ocaml_av_find_input_format(value _short_name) {
avioformat_const AVInputFormat *format = av_find_input_format(short_name);
caml_acquire_runtime_system();

free(short_name);
av_free(short_name);

if (!format)
caml_raise_not_found();
Expand All @@ -623,10 +623,10 @@ static av_t *open_input(char *url, avioformat_const AVInputFormat *format,
AVDictionary **options) {
int err;

av_t *av = (av_t *)calloc(1, sizeof(av_t));
av_t *av = (av_t *)av_mallocz(sizeof(av_t));
if (!av) {
if (url)
free(url);
av_free(url);
caml_raise_out_of_memory();
}

Expand All @@ -638,8 +638,8 @@ static av_t *open_input(char *url, avioformat_const AVInputFormat *format,

if (!av->format_context) {
if (url)
free(url);
free(av);
av_free(url);
av_free(av);
caml_raise_out_of_memory();
}

Expand Down Expand Up @@ -667,9 +667,9 @@ static av_t *open_input(char *url, avioformat_const AVInputFormat *format,
caml_remove_generational_global_root(&av->interrupt_cb);
av->interrupt_cb = Val_none;
}
free(av);
av_free(av);
if (url)
free(url);
av_free(url);
av_dict_free(options);
ocaml_avutil_raise_error(err);
}
Expand All @@ -685,9 +685,9 @@ static av_t *open_input(char *url, avioformat_const AVInputFormat *format,
caml_remove_generational_global_root(&av->interrupt_cb);
av->interrupt_cb = Val_none;
}
free(av);
av_free(av);
if (url)
free(url);
av_free(url);
av_dict_free(options);
ocaml_avutil_raise_error(err);
}
Expand Down Expand Up @@ -719,7 +719,7 @@ CAMLprim value ocaml_av_open_input(value _url, value _format, value _interrupt,
}

if (ulen > 0)
url = strndup(String_val(_url), ulen);
url = av_strndup(String_val(_url), ulen);

if (_format != Val_none)
format = InputFormat_val(Some_val(_format));
Expand All @@ -733,7 +733,7 @@ CAMLprim value ocaml_av_open_input(value _url, value _format, value _interrupt,
av_t *av = open_input(url, format, NULL, _interrupt, &options);

if (url)
free(url);
av_free(url);

// Return unused keys
count = av_dict_count(options);
Expand Down Expand Up @@ -872,8 +872,8 @@ static stream_t **allocate_input_context(av_t *av) {
Fail("Failed to read closed input");

// Allocate streams context array
av->streams =
(stream_t **)calloc(av->format_context->nb_streams, sizeof(stream_t *));
av->streams = (stream_t **)av_malloc_array(av->format_context->nb_streams,
sizeof(stream_t *));
if (!av->streams)
caml_raise_out_of_memory();

Expand All @@ -891,7 +891,7 @@ static stream_t *allocate_stream_context(av_t *av, int index,
av_get_media_type_string(type));
}

stream_t *stream = (stream_t *)calloc(1, sizeof(stream_t));
stream_t *stream = (stream_t *)av_mallocz(sizeof(stream_t));
if (!stream)
caml_raise_out_of_memory();

Expand Down Expand Up @@ -941,7 +941,7 @@ static stream_t *open_stream_index(av_t *av, int index, const AVCodec *dec) {
err = avcodec_parameters_to_context(stream->codec_context, dec_param);

if (err < 0) {
free(stream);
av_free(stream);
ocaml_avutil_raise_error(err);
}

Expand All @@ -951,7 +951,7 @@ static stream_t *open_stream_index(av_t *av, int index, const AVCodec *dec) {
caml_acquire_runtime_system();

if (err < 0) {
free(stream);
av_free(stream);
ocaml_avutil_raise_error(err);
}

Expand Down Expand Up @@ -1155,7 +1155,7 @@ CAMLprim value ocaml_av_read_input(value _packet, value _frame, value _av) {
// Assign OCaml values right away to account for potential exceptions
// raised below.
if (stream->codec_context->codec_type == AVMEDIA_TYPE_SUBTITLE) {
frame = (AVFrame *)calloc(1, sizeof(AVSubtitle));
frame = (AVFrame *)av_mallocz(sizeof(AVSubtitle));

if (!frame) {
av_packet_free(&packet);
Expand Down Expand Up @@ -1294,27 +1294,27 @@ CAMLprim value ocaml_av_output_format_guess(value _short_name, value _filename,

if (caml_string_length(_short_name) > 0) {
short_name =
strndup(String_val(_short_name), caml_string_length(_short_name));
av_strndup(String_val(_short_name), caml_string_length(_short_name));
if (!short_name)
caml_raise_out_of_memory();
};

if (caml_string_length(_filename) > 0) {
filename = strndup(String_val(_filename), caml_string_length(_filename));
filename = av_strndup(String_val(_filename), caml_string_length(_filename));
if (!filename) {
if (short_name)
free(short_name);
av_free(short_name);
caml_raise_out_of_memory();
}
}

if (caml_string_length(_mime) > 0) {
mime = strndup(String_val(_mime), caml_string_length(_mime));
mime = av_strndup(String_val(_mime), caml_string_length(_mime));
if (!mime) {
if (short_name)
free(short_name);
av_free(short_name);
if (filename)
free(filename);
av_free(filename);
caml_raise_out_of_memory();
}
}
Expand All @@ -1324,11 +1324,11 @@ CAMLprim value ocaml_av_output_format_guess(value _short_name, value _filename,
caml_acquire_runtime_system();

if (short_name)
free(short_name);
av_free(short_name);
if (filename)
free(filename);
av_free(filename);
if (mime)
free(mime);
av_free(mime);

if (!guessed)
CAMLreturn(Val_none);
Expand Down Expand Up @@ -1375,11 +1375,11 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
int ret;
AVIOInterruptCB interrupt_cb = {ocaml_av_interrupt_callback, NULL};
AVIOInterruptCB *interrupt_cb_ptr = NULL;
av_t *av = (av_t *)calloc(1, sizeof(av_t));
av_t *av = (av_t *)av_mallocz(sizeof(av_t));

if (!av) {
if (file_name)
free(file_name);
av_free(file_name);
av_dict_free(options);
caml_raise_out_of_memory();
}
Expand All @@ -1406,9 +1406,9 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,

if (ret < 0) {
if (file_name)
free(file_name);
av_free(file_name);
av_dict_free(options);
free(av);
av_free(av);

ocaml_avutil_raise_error(ret);
}
Expand All @@ -1421,12 +1421,12 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
av->interrupt_cb = Val_none;
}

free(av);
av_free(av);
if (file_name)
free(file_name);
av_free(file_name);

av_dict_free(options);
free(av);
av_free(av);

ocaml_avutil_raise_error(ret);
}
Expand All @@ -1435,9 +1435,9 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
ret = av_opt_set_dict(av->format_context->priv_data, options);

if (ret < 0) {
free(av);
av_free(av);
if (file_name)
free(file_name);
av_free(file_name);

av_dict_free(options);
ocaml_avutil_raise_error(ret);
Expand All @@ -1447,9 +1447,9 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
// open the output file, if needed
if (avio_context) {
if (av->format_context->oformat->flags & AVFMT_NOFILE) {
free(av);
av_free(av);
if (file_name)
free(file_name);
av_free(file_name);
av_dict_free(options);

av_dict_free(options);
Expand All @@ -1471,9 +1471,9 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
av->interrupt_cb = Val_none;
}

free(av);
av_free(av);
if (file_name)
free(file_name);
av_free(file_name);
av_dict_free(options);

av_dict_free(options);
Expand All @@ -1485,7 +1485,7 @@ static av_t *open_output(avioformat_const AVOutputFormat *format,
}

if (file_name)
free(file_name);
av_free(file_name);

return av;
}
Expand All @@ -1496,7 +1496,7 @@ CAMLprim value ocaml_av_open_output(value _interrupt, value _format,
CAMLparam3(_interrupt, _filename, _opts);
CAMLlocal3(ans, ret, unused);
char *filename =
strndup(String_val(_filename), caml_string_length(_filename));
av_strndup(String_val(_filename), caml_string_length(_filename));
avioformat_const AVOutputFormat *format = NULL;
AVDictionary *options = NULL;
char *key, *val;
Expand Down Expand Up @@ -1708,7 +1708,7 @@ static stream_t *new_stream(av_t *av, const AVCodec *codec) {

AVStream *avstream = avformat_new_stream(av->format_context, codec);
if (!avstream) {
free(stream);
av_free(stream);
caml_raise_out_of_memory();
}

Expand Down
Loading

0 comments on commit da4de00

Please sign in to comment.