Skip to content

Commit

Permalink
Enforce dts validity when writing
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-riehm committed Jun 12, 2023
1 parent 9b237ac commit e57cecd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 26 additions & 2 deletions arrows/ffmpeg/ffmpeg_video_output.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ffmpeg_video_output::impl
codec_context_uptr codec_context;
AVCodec const* codec;
sws_context_uptr image_conversion_context;
int64_t prev_video_dts;
};

impl();
Expand Down Expand Up @@ -413,7 +414,8 @@ ::open_video_state(
metadata_stream{ nullptr },
codec_context{ nullptr },
codec{ nullptr },
image_conversion_context{ nullptr }
image_conversion_context{ nullptr },
prev_video_dts{ AV_NOPTS_VALUE }
{
// Allocate output format context
{
Expand Down Expand Up @@ -740,8 +742,30 @@ ::add_image( kv::video_raw_image const& image )
dynamic_cast< ffmpeg_video_raw_image const& >( image );
for( auto const& packet : ffmpeg_image.packets )
{
// Ensure this packet has sensible timestamps or FFmpeg will complain
if( packet->pts == AV_NOPTS_VALUE || packet->dts == AV_NOPTS_VALUE ||
packet->dts <= prev_video_dts || packet->dts > packet->pts )
{
LOG_ERROR(
parent->logger,
"Dropping video packet with invalid dts/pts "
<< packet->dts << "/" << packet->pts << " "
<< "with prev dts " << prev_video_dts );
continue;
}

// Copy the packet so we can switch the video stream index
packet_uptr tmp_packet{
throw_error_null(
av_packet_clone( packet.get() ), "Could not copy video packet" ) };
tmp_packet->stream_index = video_stream->index;

// Record this DTS for next time
prev_video_dts = packet->dts;

// Write the packet
throw_error_code(
av_interleaved_write_frame( format_context.get(), packet.get() ),
av_interleaved_write_frame( format_context.get(), tmp_packet.get() ),
"Could not write frame to file" );
}
++frame_count;
Expand Down
4 changes: 4 additions & 0 deletions doc/release-notes/master.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Arrows: Core

* Made the transcode applet's failure to open a video result in a more graceful exit.

Arrows: FFmpeg

* Added check for incoming raw video packets' timestamps and stream indices.

Arrows: KLV

* Ensured that NaN comparisons happen consistently across all data structures.
Expand Down

0 comments on commit e57cecd

Please sign in to comment.