In case we are short of queued buffers, at first v4l2_buffer was enqueued to kernel so it's not owned by user-space anymore. After that it's timestamp field was read, but it might be overwritten by driver at that moment. It resulted in invalid timestamp sometimes.
Unified · Split
| @@ -492,6 +492,7 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) · 491 unmodified lines | ||
| 492 | 492 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 493 | 493 | .memory = V4L2_MEMORY_MMAP |
| 494 | 494 | }; |
| 495 | + struct timeval buf_ts; | |
| 495 | 496 | int res; |
| 496 | 497 | |
| 497 | 498 | pkt->size = 0; |
| @@ -508,6 +509,8 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) · 10 unmodified lines | ||
| 508 | 509 | return res; |
| 509 | 510 | } |
| 510 | 511 | |
| 512 | + buf_ts = buf.timestamp; | |
| 513 | + | |
| 511 | 514 | if (buf.index >= s->buffers) { |
| 512 | 515 | av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n"); |
| 513 | 516 | return AVERROR(EINVAL); |
| @@ -583,7 +586,7 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) · 69 unmodified lines | ||
| 583 | 586 | return AVERROR(ENOMEM); |
| 584 | 587 | } |
| 585 | 588 | } |
| 586 | − pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec; | |
| 589 | + pkt->pts = buf_ts.tv_sec * INT64_C(1000000) + buf_ts.tv_usec; | |
| 587 | 590 | convert_timestamp(ctx, &pkt->pts); |
| 588 | 591 | |
| 589 | 592 | return pkt->size; |