Skip to content

Commit

Permalink
Order by PTS/DTS when releasing pending packets/frame queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jan 12, 2024
1 parent ab7b6c7 commit 945da08
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/core/decoder/ffmpeg_decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -762,24 +762,26 @@ let mk_decoder ~streams ~target_position container =
Int64.to_float pts *. float num /. float den
in
let decodable = ref [] in
let push (position, decode) =
let push (position, ts, decode) =
decodable :=
(position, decode)
(position, ts, decode)
:: List.filter
(fun (p, _) ->
(fun (p, _, _) ->
Float.abs (p -. position)
<= Ffmpeg_decoder_common.conf_max_interleave_duration#get)
!decodable
in
let flush position =
let d = List.sort (fun (p, _) (p', _) -> Float.compare p p') !decodable in
let d =
List.sort (fun (_, p, _) (_, p', _) -> Int64.compare p p') !decodable
in
let min_position =
position -. Ffmpeg_decoder_common.conf_max_interleave_delta#get
in
List.iter (fun (p, decode) -> if min_position <= p then decode ()) d;
List.iter (fun (p, _, decode) -> if min_position <= p then decode ()) d;
decodable := []
in
let check_pts ~decode stream pts =
let check_pts ~decode ~ts stream pts =
match (pts, !target_position) with
| Some pts, Some target_position ->
if target_position <= position ~pts stream then decode ()
Expand All @@ -789,7 +791,7 @@ let mk_decoder ~streams ~target_position container =
if Hashtbl.length streams_seen = Streams.cardinal streams then (
flush position;
decode ())
else push (position, decode)
else push (position, ts, decode)
| None, _ ->
log#important
"Got packet or frame with no timestamp! Synchronization issues may \
Expand Down Expand Up @@ -835,13 +837,17 @@ let mk_decoder ~streams ~target_position container =
match Streams.find_opt i streams with
| Some (`Audio_frame (s, decode)) ->
check_pts s
~ts:(Option.value ~default:0L (Avutil.Frame.pts frame))
~decode:(fun () -> decode ~buffer (`Frame frame))
(Avutil.Frame.pts frame)
| _ -> f ())
| `Audio_packet (i, packet) -> (
match Streams.find_opt i streams with
| Some (`Audio_packet (s, decode)) ->
check_pts
~ts:
(Option.value ~default:0L
(Avcodec.Packet.get_dts packet))
~decode:(fun () -> decode ~buffer packet)
s
(Avcodec.Packet.get_pts packet)
Expand All @@ -850,13 +856,17 @@ let mk_decoder ~streams ~target_position container =
match Streams.find_opt i streams with
| Some (`Video_frame (s, decode)) ->
check_pts
~ts:(Option.value ~default:0L (Avutil.Frame.pts frame))
~decode:(fun () -> decode ~buffer (`Frame frame))
s (Avutil.Frame.pts frame)
| _ -> f ())
| `Video_packet (i, packet) -> (
match Streams.find_opt i streams with
| Some (`Video_packet (s, decode)) ->
check_pts
~ts:
(Option.value ~default:0L
(Avcodec.Packet.get_dts packet))
~decode:(fun () -> decode ~buffer packet)
s
(Avcodec.Packet.get_pts packet)
Expand All @@ -865,6 +875,9 @@ let mk_decoder ~streams ~target_position container =
match Streams.find_opt i streams with
| Some (`Data_packet (s, decode)) ->
check_pts
~ts:
(Option.value ~default:0L
(Avcodec.Packet.get_dts packet))
~decode:(fun () -> decode ~buffer packet)
s
(Avcodec.Packet.get_pts packet)
Expand Down

0 comments on commit 945da08

Please sign in to comment.