Repository navigation
fix: fix non-monotonic video PTS causing playback seek-back #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/eagle
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1868,7 +1868,7 @@ int encoder_encode_audio(encoder_context_t *encoder_ctx, void *audio_data) | |
| } | ||
|
|
||
| memcpy(enc_audio_ctx->outbuf, audio_data, buffer_size); | ||
| enc_audio_ctx->duration = (audio_codec_data->codec_context->time_base.num * 1000 / audio_codec_data->codec_context->time_base.den) * 90; | ||
| enc_audio_ctx->duration = (audio_codec_data->codec_context->time_base.num * 1000 * 90) / audio_codec_data->codec_context->time_base.den; | ||
| enc_audio_ctx->pts += enc_audio_ctx->duration; | ||
| enc_audio_ctx->dts = enc_audio_ctx->pts; | ||
| enc_audio_ctx->flags = 0; | ||
|
|
@@ -1937,7 +1937,7 @@ int encoder_encode_audio(encoder_context_t *encoder_ctx, void *audio_data) | |
| audio_codec_data->frame->pts += ((enc_audio_ctx->pts - last_audio_pts) / 1000) * 90; | ||
| else if (audio_codec_data->codec_context->time_base.den > 0) /*generate a true monotonic pts based on the codec fps*/ | ||
| audio_codec_data->frame->pts += | ||
| (audio_codec_data->codec_context->time_base.num * 1000 / audio_codec_data->codec_context->time_base.den) * 90; | ||
| (audio_codec_data->codec_context->time_base.num * 1000 * 90) / audio_codec_data->codec_context->time_base.den; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Video frame PTS still accumulate truncation When a caller uses either video encoder’s monotonic-PTS path, the same monotonic-PTS calculation remains divide-before-multiply in both video encoders. With a 1/30 time base it adds 2970 rather than 3000 ticks per frame, accumulating video timestamp drift for callers that use the encoded frame PTS. Apply the same multiply-before-divide calculation to both video encoder paths, with a suitable wider intermediate if needed. Prompt for AI agents |
||
| else { | ||
| fprintf(stderr, "ENCODER: (encoder_encode_audio) couldn't generate a monotonic ts: time_base.den(%d)\n", | ||
| audio_codec_data->codec_context->time_base.den); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,8 @@ | |
|
|
||
| static int64_t video_pts = 0; | ||
| static int64_t audio_pts = 0; | ||
| static int64_t first_pts = 0; | ||
| static int64_t first_pts_video = 0; | ||
| static int64_t first_pts_audio = 0; | ||
|
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Startup A/V offset disappears When audio and video begin capture or deliver their first packets at different times, the audio and video paths subtract separate first-packet timestamps, so both streams start at PTS 0 and the recording loses their actual startup offset. Normalize both streams against one shared recording start timestamp. Also at Prompt for AI agents |
||
| static int64_t last_pts = 0; | ||
| static int64_t last_video_timestamp = 0; // 添加上一帧视频时间戳记录 | ||
|
|
||
|
|
@@ -125,7 +126,7 @@ | |
| AVRational video_time = mp4_ctx->streams[stream_index]->time_base; | ||
| AVRational time_base = codec_data->codec_context->time_base; | ||
|
|
||
| if (video_pts > last_pts || video_pts == 0) { | ||
| if (video_pts >= last_pts) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Rejected frame lowers timestamp watermark When a video timestamp moves backwards during an active recording and causes this check to reject a packet, Only advance the watermark after a successful write, and keep it at the greatest PTS actually written. Prompt for AI agentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Equal video timestamps are still written When two consecutive packets have the same Require Prompt for AI agents |
||
| getLoadLibsInstance()->m_av_packet_rescale_ts(outpacket, time_base, video_time); | ||
| getAvformat()->m_av_write_frame(mp4_ctx, outpacket); | ||
| set_video_time_capture((double)(pts)/1000/1000000); | ||
|
|
@@ -134,14 +135,14 @@ | |
| } | ||
|
|
||
| last_pts = video_pts; | ||
| if (first_pts == 0) { | ||
| first_pts = pts; | ||
| if (first_pts_video == 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Opening packets repeat timestamps When a stream's first packet has timestamp zero, Track whether each stream's origin has been initialized separately from its timestamp value. Also at Prompt for AI agents |
||
| first_pts_video = pts; | ||
| last_video_timestamp = pts; | ||
| video_pts = 0; // 第一个视频包的PTS为0 | ||
| } else { | ||
| // 使用真实时间戳计算,而不是硬编码的33ms | ||
| // 将纳秒时间戳转换为帧数(基于time_base) | ||
| int64_t timestamp_diff = pts - first_pts; // 从开始录制的时间差(纳秒) | ||
| int64_t timestamp_diff = pts - first_pts_video; // 从开始录制的时间差(纳秒) | ||
|
|
||
| // 根据编码器的time_base计算PTS | ||
| // time_base通常是1/fps,所以pts = 时间差 * fps | ||
|
|
@@ -167,14 +168,14 @@ | |
| video_pts = last_pts + 1; | ||
|
|
||
| if (verbosity > 2) { | ||
| fprintf(stderr, "video_pts: %ld (timestamp: %ld, first_pts: %ld)\n", | ||
| video_pts, pts, first_pts); | ||
| video_pts, pts, first_pts_video); | ||
| } | ||
|
|
||
| // 每100帧检查一次音视频同步状态 | ||
| frame_count++; | ||
| if (frame_count % 100 == 0) { | ||
| int64_t video_time_ms = (pts - first_pts) / 1000000; // 视频实际时间(毫秒) | ||
| int64_t video_time_ms = (pts - first_pts_video) / 1000000; // 视频实际时间(毫秒) | ||
| int64_t video_pts_time_ms = 0; | ||
|
|
||
| // 根据time_base计算视频PTS对应的时间 | ||
|
|
@@ -206,12 +207,12 @@ | |
| getAvformat()->m_av_write_frame(mp4_ctx, outpacket); | ||
|
|
||
| // 使用基于真实时间的音频PTS计算 | ||
| if (first_pts == 0) { | ||
| first_pts = pts; // 设置first_pts | ||
| if (first_pts_audio == 0) { | ||
| first_pts_audio = pts; // 设置first_pts_audio | ||
| audio_pts = 0; // 第一个音频包的PTS为0 | ||
| } else { | ||
| // 将纳秒时间戳转换为音频时间基准 | ||
| int64_t timestamp_diff = pts - first_pts; // 从开始录制的时间差(纳秒) | ||
| int64_t timestamp_diff = pts - first_pts_audio; // 从开始录制的时间差(纳秒) | ||
|
|
||
| if (time_base.den > 0 && time_base.num > 0) { | ||
| // 根据音频编码器的time_base计算PTS | ||
|
|
@@ -227,8 +228,8 @@ | |
| } | ||
|
|
||
| if (verbosity > 2) { | ||
| fprintf(stderr, "audio_pts: %ld (timestamp: %ld, first_pts: %ld)\n", | ||
| audio_pts, pts, first_pts); | ||
| audio_pts, pts, first_pts_audio); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -245,7 +246,8 @@ | |
| { | ||
| video_pts = 0; | ||
| audio_pts = 0; | ||
| first_pts = 0; | ||
| first_pts_video = 0; | ||
| first_pts_audio = 0; | ||
| last_pts = 0; | ||
| last_video_timestamp = 0; // 重置新增的变量 | ||
| frame_count = 0; // 重置帧计数 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High · Audio timestamps lag frame duration
When audio uses a sample-rate time base and frames contain multiple samples,
encoder_encode_audioadvancesframe->ptsandenc_audio_ctx->durationby about one tick per frame instead of accounting for the samples in that frame, so encoded audio timing and A/V synchronization are wrong.Calculate both increments from the number of samples in each frame and the applicable time base.
Also at
libcam/libcam_encoder/encoder.c:1871-1872.Prompt for AI agents