Skip to content

Commit

Permalink
Better.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Mar 18, 2024
1 parent ffb9e27 commit 133bc4a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
10 changes: 1 addition & 9 deletions av/av.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,7 @@ type ('a, 'b, 'c) stream = {

type media_type = MT_audio | MT_video | MT_data | MT_subtitle

(* Dummy function to prevent the container from being
collected before the stream is. *)
external dummy_cleanup : _ container -> unit = "ocaml_av_dummy_cleanup"

let mk_stream container index =
let finalise () = dummy_cleanup container in
let s = { container; index; decoder = None } in
Gc.finalise_last finalise s;
s
let mk_stream container index = { container; index; decoder = None }

external get_codec_params : (_, 'm, _) stream -> 'm Avcodec.params
= "ocaml_av_get_stream_codec_parameters"
Expand Down
47 changes: 27 additions & 20 deletions av/av_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ static inline av_t *Av_val(value v) {

/***** Stream handler *****/

#define StreamAv_val(v) Av_val(Field(v, 0))
#define StreamIndex_val(v) Int_val(Field(v, 1))

/**** Media Type ****/
Expand Down Expand Up @@ -183,15 +182,11 @@ CAMLprim value ocaml_av_get_streams(value _av, value _media_type) {
CAMLreturn(list);
}

CAMLprim value ocaml_av_dummy_cleanup(value _av) {
CAMLparam1(_av);
CAMLreturn(Val_unit);
}

CAMLprim value ocaml_av_get_stream_codec_parameters(value _stream) {
CAMLparam1(_stream);
CAMLlocal1(ans);
av_t *av = StreamAv_val(_stream);
CAMLlocal2(ans, _av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

value_of_codec_parameters_copy(av->format_context->streams[index]->codecpar,
Expand All @@ -202,8 +197,9 @@ CAMLprim value ocaml_av_get_stream_codec_parameters(value _stream) {

CAMLprim value ocaml_av_get_stream_time_base(value _stream) {
CAMLparam1(_stream);
CAMLlocal1(ans);
av_t *av = StreamAv_val(_stream);
CAMLlocal2(ans, _av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

value_of_rational(&av->format_context->streams[index]->time_base, &ans);
Expand All @@ -213,7 +209,9 @@ CAMLprim value ocaml_av_get_stream_time_base(value _stream) {

CAMLprim value ocaml_av_set_stream_time_base(value _stream, value _time_base) {
CAMLparam2(_stream, _time_base);
av_t *av = StreamAv_val(_stream);
CAMLlocal1(_av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

av->format_context->streams[index]->time_base = rational_of_value(_time_base);
Expand All @@ -223,16 +221,19 @@ CAMLprim value ocaml_av_set_stream_time_base(value _stream, value _time_base) {

CAMLprim value ocaml_av_get_stream_frame_size(value _stream) {
CAMLparam1(_stream);
av_t *av = StreamAv_val(_stream);
CAMLlocal1(_av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

CAMLreturn(Val_int(av->streams[index]->codec_context->frame_size));
}

CAMLprim value ocaml_av_get_stream_pixel_aspect(value _stream) {
CAMLparam1(_stream);
CAMLlocal2(ans, ret);
av_t *av = StreamAv_val(_stream);
CAMLlocal3(ans, ret, _av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);
const AVRational pixel_aspect =
av->format_context->streams[index]->sample_aspect_ratio;
Expand Down Expand Up @@ -2004,7 +2005,9 @@ CAMLprim value ocaml_av_new_data_stream(value _av, value _codec_id,
CAMLprim value ocaml_av_write_stream_packet(value _stream, value _time_base,
value _packet) {
CAMLparam3(_stream, _time_base, _packet);
av_t *av = StreamAv_val(_stream);
CAMLlocal1(_av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int ret = 0, stream_index = StreamIndex_val(_stream);
AVPacket *packet = Packet_val(_packet);
AVStream *avstream = av->format_context->streams[stream_index];
Expand Down Expand Up @@ -2269,7 +2272,9 @@ static void write_subtitle_frame(av_t *av, int stream_index,
CAMLprim value ocaml_av_write_stream_frame(value _on_keyframe, value _stream,
value _frame) {
CAMLparam3(_on_keyframe, _stream, _frame);
av_t *av = StreamAv_val(_stream);
CAMLlocal1(_av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

if (!av->streams)
Expand Down Expand Up @@ -2386,10 +2391,11 @@ uint8_t *ocaml_av_ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
// This from libavformat/hlsenc.c
CAMLprim value ocaml_av_codec_attr(value _stream) {
CAMLparam1(_stream);
CAMLlocal2(ans, _attr);
CAMLlocal3(ans, _attr, _av);

char attr[32];
av_t *av = StreamAv_val(_stream);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);

if (!av->format_context || !av->format_context->streams)
Expand Down Expand Up @@ -2490,9 +2496,10 @@ CAMLprim value ocaml_av_codec_attr(value _stream) {

CAMLprim value ocaml_av_stream_bitrate(value _stream) {
CAMLparam1(_stream);
CAMLlocal1(ans);
CAMLlocal2(ans, _av);

av_t *av = StreamAv_val(_stream);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);
AVCPBProperties *props = NULL;

Expand Down

0 comments on commit 133bc4a

Please sign in to comment.