Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libcam/libcam_encoder/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown

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_audio advances frame->pts and enc_audio_ctx->duration by 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
In `libcam/libcam_encoder/encoder.c` at line 1940:

**Audio timestamps lag frame duration**

When audio uses a sample-rate time base and frames contain multiple samples, `encoder_encode_audio` advances `frame->pts` and `enc_audio_ctx->duration` by 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`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
In `libcam/libcam_encoder/encoder.c` at line 1940:

**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.

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);
Expand Down
26 changes: 14 additions & 12 deletions libcam/libcam_encoder/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 libcam/libcam_encoder/mp4.c:138-145, libcam/libcam_encoder/mp4.c:210-215.

Prompt for AI agents
In `libcam/libcam_encoder/mp4.c` at lines 32-33:

**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 `libcam/libcam_encoder/mp4.c:138-145`, `libcam/libcam_encoder/mp4.c:210-215`.

static int64_t last_pts = 0;
static int64_t last_video_timestamp = 0; // 添加上一帧视频时间戳记录

Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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, last_pts is assigned the rejected video_pts after this check, so the next packet is compared against the lower value and can be written with a PTS below the last packet already stored; playback then sees a timestamp regression.

Only advance the watermark after a successful write, and keep it at the greatest PTS actually written.

Prompt for AI agents
In `libcam/libcam_encoder/mp4.c` at line 129:

**Rejected frame lowers timestamp watermark**

When a video timestamp moves backwards during an active recording and causes this check to reject a packet, `last_pts` is assigned the rejected `video_pts` after this check, so the next packet is compared against the lower value and can be written with a PTS below the last packet already stored; playback then sees a timestamp regression.

Only advance the watermark after a successful write, and keep it at the greatest PTS actually written.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 video_pts, video_pts >= last_pts accepts equal values, so mp4_write_packet writes duplicate PTS despite the later calculation explicitly trying to make PTS strictly increase; playback can still encounter repeated timestamps.

Require video_pts > last_pts before writing, while handling the initial packet separately so it is retained.

Prompt for AI agents
In `libcam/libcam_encoder/mp4.c` at line 129:

**Equal video timestamps are still written**

When two consecutive packets have the same `video_pts`, `video_pts >= last_pts` accepts equal values, so `mp4_write_packet` writes duplicate PTS despite the later calculation explicitly trying to make PTS strictly increase; playback can still encounter repeated timestamps.

Require `video_pts > last_pts` before writing, while handling the initial packet separately so it is retained.

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);
Expand All @@ -134,14 +135,14 @@
}

last_pts = video_pts;
if (first_pts == 0) {
first_pts = pts;
if (first_pts_video == 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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, first_pts_video or first_pts_audio remains zero, so the corresponding first-packet branch runs again and resets the stream PTS origin; the >= guard then writes duplicate initial timestamps.

Track whether each stream's origin has been initialized separately from its timestamp value.

Also at libcam/libcam_encoder/mp4.c:129, libcam/libcam_encoder/mp4.c:139, libcam/libcam_encoder/mp4.c:210.

Prompt for AI agents
In `libcam/libcam_encoder/mp4.c` at line 138:

**Opening packets repeat timestamps**

When a stream's first packet has timestamp zero, `first_pts_video` or `first_pts_audio` remains zero, so the corresponding first-packet branch runs again and resets the stream PTS origin; the `>=` guard then writes duplicate initial timestamps.

Track whether each stream's origin has been initialized separately from its timestamp value.

Also at `libcam/libcam_encoder/mp4.c:129`, `libcam/libcam_encoder/mp4.c:139`, `libcam/libcam_encoder/mp4.c:210`.

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
Expand All @@ -167,14 +168,14 @@
video_pts = last_pts + 1;

if (verbosity > 2) {
fprintf(stderr, "video_pts: %ld (timestamp: %ld, first_pts: %ld)\n",

Check warning on line 171 in libcam/libcam_encoder/mp4.c

View workflow job for this annotation

GitHub Actions / cppcheck

%ld in format string (no. 2) requires 'long' but the argument type is 'unsigned long'.
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对应的时间
Expand Down Expand Up @@ -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
Expand All @@ -227,8 +228,8 @@
}

if (verbosity > 2) {
fprintf(stderr, "audio_pts: %ld (timestamp: %ld, first_pts: %ld)\n",

Check warning on line 231 in libcam/libcam_encoder/mp4.c

View workflow job for this annotation

GitHub Actions / cppcheck

%ld in format string (no. 2) requires 'long' but the argument type is 'unsigned long'.
audio_pts, pts, first_pts);
audio_pts, pts, first_pts_audio);
}
}

Expand All @@ -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; // 重置帧计数
Expand Down
Loading