[PATCH] avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers
From 31d73c6774c1ea6d621db57f26439e297cc23c3d Mon Sep 17 00:00:00 2001 From: Vladimir Sobolev <v.sobolev@gmail.com> Date: Sun, 9 Nov 2025 02:28:13 +0200 Subject: [PATCH] avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame. Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Add debug logging for parsed header values This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. --- libavformat/mpjpegdec.c | 69 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 125b17585e..c90d7a2ad4 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/eval.h" +#include "libavutil/intfloat.h" #include "avformat.h" #include "demux.h" @@ -34,6 +37,11 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ + int framerate_applied; /* flag indicating if framerate was applied to stream */ } MPJPEGDemuxContext; static void trim_right(char *p) @@ -97,7 +105,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg); static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +127,7 @@ static int mpjpeg_read_probe(const AVProbeData *p) ffio_init_read_context(&pb, p->buf, p->buf_size); - ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; return ret; } @@ -146,6 +155,12 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; + /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->framerate_applied = 0; + mpjpeg->has_timestamp = 0; avpriv_set_pts_info(st, 60, 1, 25); avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +182,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +251,33 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (!isnan(ts) && isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } } @@ -311,10 +354,21 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } - ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret; + /* Update framerate if it was set in header and hasn't been applied yet */ + if (mpjpeg->framerate_set && !mpjpeg->framerate_applied && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + st->avg_frame_rate = mpjpeg->framerate; + avpriv_set_pts_info(st, 60, mpjpeg->framerate.den, mpjpeg->framerate.num); + mpjpeg->framerate_applied = 1; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +407,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } } + /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + pkt->pts = av_rescale_q(mpjpeg->timestamp, AV_TIME_BASE_Q, st->time_base); + pkt->dts = pkt->pts; + } + return ret; } -- 2.50.1 (Apple Git-155)
Hi Vladimir On Sun, Nov 09, 2025 at 02:38:56AM +0200, Vladimir Sobolev via ffmpeg-devel wrote:
From 31d73c6774c1ea6d621db57f26439e297cc23c3d Mon Sep 17 00:00:00 2001 From: Vladimir Sobolev <v.sobolev@gmail.com> Date: Sun, 9 Nov 2025 02:28:13 +0200 Subject: [PATCH] avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame.
Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Add debug logging for parsed header values
This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. --- libavformat/mpjpegdec.c | 69 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-)
diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 125b17585e..c90d7a2ad4 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/eval.h" +#include "libavutil/intfloat.h"
#include "avformat.h" #include "demux.h" @@ -34,6 +37,11 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ + int framerate_applied; /* flag indicating if framerate was applied to stream */ } MPJPEGDemuxContext;
static void trim_right(char *p) @@ -97,7 +105,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg);
static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +127,7 @@ static int mpjpeg_read_probe(const AVProbeData *p)
ffio_init_read_context(&pb, p->buf, p->buf_size);
- ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0;
return ret; } @@ -146,6 +155,12 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
+ /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->framerate_applied = 0; + mpjpeg->has_timestamp = 0; avpriv_set_pts_info(st, 60, 1, 25);
avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +182,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +251,33 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL);
+ if (!isnan(ts) && isfinite(ts)) {
nan is not finite
+ /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + }
all the if(log_ctx) looks wierd i dont think log_ctx is ever NULL in this code thats under if(mpjpeg)
} }
@@ -311,10 +354,21 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); }
- ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret;
+ /* Update framerate if it was set in header and hasn't been applied yet */ + if (mpjpeg->framerate_set && !mpjpeg->framerate_applied && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + st->avg_frame_rate = mpjpeg->framerate; + avpriv_set_pts_info(st, 60, mpjpeg->framerate.den, mpjpeg->framerate.num); + mpjpeg->framerate_applied = 1; + }
this looks wrong * you should not set the timebase more than once. it was already set to 1/25 * the average framerate can only match the 1/timebase for "constant fps" which is the opposit of what this patch is trying to do
+ if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +407,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } }
+ /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + AVStream *st = s->streams[0];
+ pkt->pts = av_rescale_q(mpjpeg->timestamp, AV_TIME_BASE_Q, st->time_base);
rescaling timestamps suggests you set the timebase wrong thx [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB What does censorship reveal? It reveals fear. -- Julian Assange
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame. Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Use AV_TIME_BASE_Q timebase to support variable fps - Add debug logging for parsed header values This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. Fixes issues from review: - Use isfinite() instead of !isnan() && isfinite() - Remove redundant if(log_ctx) checks when mpjpeg != NULL - Set timebase once in read_header using AV_TIME_BASE_Q - Set timestamps directly without rescaling --- libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 125b17585e..ed02459afe 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,8 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/intfloat.h" #include "avformat.h" #include "demux.h" @@ -34,6 +36,10 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ } MPJPEGDemuxContext; static void trim_right(char *p) @@ -97,7 +103,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg); static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +125,7 @@ static int mpjpeg_read_probe(const AVProbeData *p) ffio_init_read_context(&pb, p->buf, p->buf_size); - ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; return ret; } @@ -146,7 +153,13 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; - avpriv_set_pts_info(st, 60, 1, 25); + /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->has_timestamp = 0; + /* Use AV_TIME_BASE_Q timebase to support variable fps via X-Timestamp */ + avpriv_set_pts_info(st, 60, AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +180,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +249,31 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } } @@ -311,10 +350,20 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } - ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret; + /* Update framerate if it was set in header */ + if (mpjpeg->framerate_set && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + /* Only update avg_frame_rate, timebase should remain 1/AV_TIME_BASE for variable fps */ + st->avg_frame_rate = mpjpeg->framerate; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +402,14 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } } + /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + /* Use AV_TIME_BASE_Q as timebase for timestamps */ + pkt->pts = mpjpeg->timestamp; + pkt->dts = mpjpeg->timestamp; + } + return ret; } -- 2.50.1 (Apple Git-155)
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame. Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Use AV_TIME_BASE_Q timebase to support variable fps - Add debug logging for parsed header values This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. Fixes issues from review: - Use isfinite() instead of !isnan() && isfinite() - Remove redundant if(log_ctx) checks when mpjpeg != NULL - Set timebase once in read_header using AV_TIME_BASE_Q - Set timestamps directly without rescaling - Add missing libavutil/eval.h include for av_strtod - Remove unused variable --- libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 125b17585e..15dd59ed86 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/intfloat.h" +#include "libavutil/eval.h" #include "avformat.h" #include "demux.h" @@ -34,6 +37,10 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ } MPJPEGDemuxContext; static void trim_right(char *p) @@ -97,7 +104,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg); static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +126,7 @@ static int mpjpeg_read_probe(const AVProbeData *p) ffio_init_read_context(&pb, p->buf, p->buf_size); - ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; return ret; } @@ -146,7 +154,13 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; - avpriv_set_pts_info(st, 60, 1, 25); + /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->has_timestamp = 0; + /* Use AV_TIME_BASE_Q timebase to support variable fps via X-Timestamp */ + avpriv_set_pts_info(st, 60, AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +181,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +250,31 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } } @@ -311,10 +351,20 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } - ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret; + /* Update framerate if it was set in header */ + if (mpjpeg->framerate_set && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + /* Only update avg_frame_rate, timebase should remain 1/AV_TIME_BASE for variable fps */ + st->avg_frame_rate = mpjpeg->framerate; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +403,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } } + /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + /* Use AV_TIME_BASE_Q as timebase for timestamps */ + pkt->pts = mpjpeg->timestamp; + pkt->dts = mpjpeg->timestamp; + } + return ret; } -- 2.50.1 (Apple Git-155)
Hi, v2 of this patch was sent on 2025-11-12 but received no further review. It addresses Michael's comments from v1: - use isfinite() instead of !isnan() && isfinite() - remove redundant if(log_ctx) checks when mpjpeg != NULL - set timebase once in read_header using AV_TIME_BASE_Q - set timestamps directly without rescaling This version is rebased on current master and adds an ffprobe_demux FATE test with a minimal multipart MJPEG sample exercising X-Timestamp and X-Framerate header parsing. Thanks, Vladimir Vladimir Sobolev (2): avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers fate: add mpjpeg X-Timestamp and X-Framerate demux test libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++-- tests/fate/demux.mak | 3 ++ tests/mpjpeg-x-timestamp.mjpeg | Bin 0 -> 613 bytes tests/ref/fate/mpjpeg-x-timestamp-demux | 4 ++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/mpjpeg-x-timestamp.mjpeg create mode 100644 tests/ref/fate/mpjpeg-x-timestamp-demux -- 2.50.1 (Apple Git-155)
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame. Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Use AV_TIME_BASE_Q timebase to support variable fps - Add debug logging for parsed header values This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. Fixes issues from review: - Use isfinite() instead of !isnan() && isfinite() - Remove redundant if(log_ctx) checks when mpjpeg != NULL - Set timebase once in read_header using AV_TIME_BASE_Q - Set timestamps directly without rescaling - Add missing libavutil/eval.h include for av_strtod - Remove unused variable --- libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 3b47bccf9d..ea4e3a3ba9 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/intfloat.h" +#include "libavutil/eval.h" #include "avformat.h" #include "demux.h" @@ -34,6 +37,10 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ } MPJPEGDemuxContext; static void trim_right(char *p) @@ -97,7 +104,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg); static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +126,7 @@ static int mpjpeg_read_probe(const AVProbeData *p) ffio_init_read_context(&pb, p->buf, p->buf_size); - ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; return ret; } @@ -146,7 +154,13 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; - avpriv_set_pts_info(st, 60, 1, 25); + /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->has_timestamp = 0; + /* Use AV_TIME_BASE_Q timebase to support variable fps via X-Timestamp */ + avpriv_set_pts_info(st, 60, AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +181,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +250,31 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } } @@ -311,10 +351,20 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } - ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret; + /* Update framerate if it was set in header */ + if (mpjpeg->framerate_set && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + /* Only update avg_frame_rate, timebase should remain 1/AV_TIME_BASE for variable fps */ + st->avg_frame_rate = mpjpeg->framerate; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +403,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } } + /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + /* Use AV_TIME_BASE_Q as timebase for timestamps */ + pkt->pts = mpjpeg->timestamp; + pkt->dts = mpjpeg->timestamp; + } + return ret; } -- 2.50.1 (Apple Git-155)
Add a minimal multipart MJPEG sample with X-Timestamp and X-Framerate headers and an ffprobe_demux FATE test to verify packet timestamps and stream framerate parsing. Co-authored-by: Cursor <cursoragent@cursor.com> --- tests/fate/demux.mak | 3 +++ tests/mpjpeg-x-timestamp.mjpeg | Bin 0 -> 613 bytes tests/ref/fate/mpjpeg-x-timestamp-demux | 4 ++++ 3 files changed, 7 insertions(+) create mode 100644 tests/mpjpeg-x-timestamp.mjpeg create mode 100644 tests/ref/fate/mpjpeg-x-timestamp-demux diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak index 4cdc1a583f..4da5dafe99 100644 --- a/tests/fate/demux.mak +++ b/tests/fate/demux.mak @@ -175,6 +175,9 @@ fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-timed-id3-demux fate-ts-timed-id3-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/mpegts/id3.ts +FATE_FFPROBE_DEMUX-$(CONFIG_MPJPEG_DEMUXER) += fate-mpjpeg-x-timestamp-demux +fate-mpjpeg-x-timestamp-demux: CMD = ffprobe_demux $(SRC_PATH)/tests/mpjpeg-x-timestamp.mjpeg + tests/data/id3.ts: TAG = GEN tests/data/id3.ts: $(SAMPLES)/mpegts/id3.ts | tests/data $(Q)cp $< $@ diff --git a/tests/mpjpeg-x-timestamp.mjpeg b/tests/mpjpeg-x-timestamp.mjpeg new file mode 100644 index 0000000000000000000000000000000000000000..df96b253bc63ee6a695ed9503427dadd5846021d GIT binary patch literal 613 zcmdPZ<>hkD&nrpIE71+9EJ(Fd$jnVlPu0&VNKNPEiqH+o%uOvWNz5&<QZUpr0ExI2 zCFZ6UC6)k{8XM>vBDDLY=B1ZpSSc78neuY+a{a&Y{{e%5mz$>>10xdy10xVJ{Qt)w z;FDOEY-XruXsKstV94<QHiI(*2MY+Wf&m*cU}tCN;NSp~Y;0f_2PY>dPyk4AadGkR zaC7tV@d*lw{6E6r!@vkM9|f>6q6@LH3!p1v5X2Dqe}F-d1LR(2MkNL&K?Y_)hX1!1 ic$gW193b1Cp_?JFzW)DBUM^iy0uLA{uz((EAq4>OG+|8u literal 0 HcmV?d00001 diff --git a/tests/ref/fate/mpjpeg-x-timestamp-demux b/tests/ref/fate/mpjpeg-x-timestamp-demux new file mode 100644 index 0000000000..efd559ae6d --- /dev/null +++ b/tests/ref/fate/mpjpeg-x-timestamp-demux @@ -0,0 +1,4 @@ +packet|codec_type=video|stream_index=0|pts=1000000|pts_time=1.000000|dts=1000000|dts_time=1.000000|duration=33333|duration_time=0.033333|size=225|pos=90|flags=K__|data_hash=CRC32:3f320fa7 +packet|codec_type=video|stream_index=0|pts=1500000|pts_time=1.500000|dts=1500000|dts_time=1.500000|duration=33333|duration_time=0.033333|size=225|pos=388|flags=K__|data_hash=CRC32:3f320fa7 +stream|index=0|codec_name=mjpeg|profile=192|codec_type=video|codec_tag_string=[0][0][0][0]|codec_tag=0x0000|width=1|height=1|coded_width=1|coded_height=1|has_b_frames=0|sample_aspect_ratio=1:1|display_aspect_ratio=1:1|pix_fmt=yuvj420p|level=-99|color_range=pc|color_space=bt470bg|color_transfer=unknown|color_primaries=unknown|chroma_location=center|field_order=unknown|id=N/A|r_frame_rate=1000000/1|avg_frame_rate=30/1|time_base=1/1000000|start_pts=1000000|start_time=1.000000|duration_ts=N/A|duration=N/A|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=8|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=2|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|disposition:multilayer=0 +format|filename=mpjpeg-x-timestamp.mjpeg|nb_streams=1|nb_programs=0|nb_stream_groups=0|format_name=mpjpeg|start_time=1.000000|duration=N/A|size=613|bit_rate=N/A|probe_score=100 -- 2.50.1 (Apple Git-155)
Add a minimal multipart MJPEG sample with X-Timestamp and X-Framerate headers and an ffprobe_demux FATE test to verify packet timestamps and stream framerate parsing. --- tests/fate/demux.mak | 3 +++ tests/mpjpeg-x-timestamp.mjpeg | Bin 0 -> 613 bytes tests/ref/fate/mpjpeg-x-timestamp-demux | 4 ++++ 3 files changed, 7 insertions(+) create mode 100644 tests/mpjpeg-x-timestamp.mjpeg create mode 100644 tests/ref/fate/mpjpeg-x-timestamp-demux diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak index 4cdc1a583f..4da5dafe99 100644 --- a/tests/fate/demux.mak +++ b/tests/fate/demux.mak @@ -175,6 +175,9 @@ fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-timed-id3-demux fate-ts-timed-id3-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/mpegts/id3.ts +FATE_FFPROBE_DEMUX-$(CONFIG_MPJPEG_DEMUXER) += fate-mpjpeg-x-timestamp-demux +fate-mpjpeg-x-timestamp-demux: CMD = ffprobe_demux $(SRC_PATH)/tests/mpjpeg-x-timestamp.mjpeg + tests/data/id3.ts: TAG = GEN tests/data/id3.ts: $(SAMPLES)/mpegts/id3.ts | tests/data $(Q)cp $< $@ diff --git a/tests/mpjpeg-x-timestamp.mjpeg b/tests/mpjpeg-x-timestamp.mjpeg new file mode 100644 index 0000000000000000000000000000000000000000..df96b253bc63ee6a695ed9503427dadd5846021d GIT binary patch literal 613 zcmdPZ<>hkD&nrpIE71+9EJ(Fd$jnVlPu0&VNKNPEiqH+o%uOvWNz5&<QZUpr0ExI2 zCFZ6UC6)k{8XM>vBDDLY=B1ZpSSc78neuY+a{a&Y{{e%5mz$>>10xdy10xVJ{Qt)w z;FDOEY-XruXsKstV94<QHiI(*2MY+Wf&m*cU}tCN;NSp~Y;0f_2PY>dPyk4AadGkR zaC7tV@d*lw{6E6r!@vkM9|f>6q6@LH3!p1v5X2Dqe}F-d1LR(2MkNL&K?Y_)hX1!1 ic$gW193b1Cp_?JFzW)DBUM^iy0uLA{uz((EAq4>OG+|8u literal 0 HcmV?d00001 diff --git a/tests/ref/fate/mpjpeg-x-timestamp-demux b/tests/ref/fate/mpjpeg-x-timestamp-demux new file mode 100644 index 0000000000..efd559ae6d --- /dev/null +++ b/tests/ref/fate/mpjpeg-x-timestamp-demux @@ -0,0 +1,4 @@ +packet|codec_type=video|stream_index=0|pts=1000000|pts_time=1.000000|dts=1000000|dts_time=1.000000|duration=33333|duration_time=0.033333|size=225|pos=90|flags=K__|data_hash=CRC32:3f320fa7 +packet|codec_type=video|stream_index=0|pts=1500000|pts_time=1.500000|dts=1500000|dts_time=1.500000|duration=33333|duration_time=0.033333|size=225|pos=388|flags=K__|data_hash=CRC32:3f320fa7 +stream|index=0|codec_name=mjpeg|profile=192|codec_type=video|codec_tag_string=[0][0][0][0]|codec_tag=0x0000|width=1|height=1|coded_width=1|coded_height=1|has_b_frames=0|sample_aspect_ratio=1:1|display_aspect_ratio=1:1|pix_fmt=yuvj420p|level=-99|color_range=pc|color_space=bt470bg|color_transfer=unknown|color_primaries=unknown|chroma_location=center|field_order=unknown|id=N/A|r_frame_rate=1000000/1|avg_frame_rate=30/1|time_base=1/1000000|start_pts=1000000|start_time=1.000000|duration_ts=N/A|duration=N/A|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=8|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=2|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|disposition:multilayer=0 +format|filename=mpjpeg-x-timestamp.mjpeg|nb_streams=1|nb_programs=0|nb_stream_groups=0|format_name=mpjpeg|start_time=1.000000|duration=N/A|size=613|bit_rate=N/A|probe_score=100 -- 2.50.1 (Apple Git-155)
Hi, Resending v3 after re-subscribing to ffmpeg-devel (previous attempt on 2026-06-11 may not have reached the list while unsubscribed). v2 was sent on 2025-11-12 but received no further review. This version addresses Michael's comments from v1: - use isfinite() instead of !isnan() && isfinite() - remove redundant if(log_ctx) checks when mpjpeg != NULL - set timebase once in read_header using AV_TIME_BASE_Q - set timestamps directly without rescaling Rebased on current master. Adds an ffprobe_demux FATE test with a minimal multipart MJPEG sample exercising X-Timestamp and X-Framerate header parsing. Thanks, Vladimir Vladimir Sobolev (2): avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers fate: add mpjpeg X-Timestamp and X-Framerate demux test libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++-- tests/fate/demux.mak | 3 ++ tests/mpjpeg-x-timestamp.mjpeg | Bin 0 -> 613 bytes tests/ref/fate/mpjpeg-x-timestamp-demux | 4 ++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/mpjpeg-x-timestamp.mjpeg create mode 100644 tests/ref/fate/mpjpeg-x-timestamp-demux -- 2.50.1 (Apple Git-155)
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame. Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Use AV_TIME_BASE_Q timebase to support variable fps - Add debug logging for parsed header values This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. Fixes issues from review: - Use isfinite() instead of !isnan() && isfinite() - Remove redundant if(log_ctx) checks when mpjpeg != NULL - Set timebase once in read_header using AV_TIME_BASE_Q - Set timestamps directly without rescaling - Add missing libavutil/eval.h include for av_strtod - Remove unused variable --- libavformat/mpjpegdec.c | 67 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 3b47bccf9d..ea4e3a3ba9 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/intfloat.h" +#include "libavutil/eval.h" #include "avformat.h" #include "demux.h" @@ -34,6 +37,10 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ } MPJPEGDemuxContext; static void trim_right(char *p) @@ -97,7 +104,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg); static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +126,7 @@ static int mpjpeg_read_probe(const AVProbeData *p) ffio_init_read_context(&pb, p->buf, p->buf_size); - ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; return ret; } @@ -146,7 +154,13 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; - avpriv_set_pts_info(st, 60, 1, 25); + /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->has_timestamp = 0; + /* Use AV_TIME_BASE_Q timebase to support variable fps via X-Timestamp */ + avpriv_set_pts_info(st, 60, AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +181,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +250,31 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } } @@ -311,10 +351,20 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } - ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret; + /* Update framerate if it was set in header */ + if (mpjpeg->framerate_set && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + /* Only update avg_frame_rate, timebase should remain 1/AV_TIME_BASE for variable fps */ + st->avg_frame_rate = mpjpeg->framerate; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +403,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } } + /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + /* Use AV_TIME_BASE_Q as timebase for timestamps */ + pkt->pts = mpjpeg->timestamp; + pkt->dts = mpjpeg->timestamp; + } + return ret; } -- 2.50.1 (Apple Git-155)
Add a minimal multipart MJPEG sample with X-Timestamp and X-Framerate headers and an ffprobe_demux FATE test to verify packet timestamps and stream framerate parsing. --- tests/fate/demux.mak | 3 +++ tests/mpjpeg-x-timestamp.mjpeg | Bin 0 -> 613 bytes tests/ref/fate/mpjpeg-x-timestamp-demux | 4 ++++ 3 files changed, 7 insertions(+) create mode 100644 tests/mpjpeg-x-timestamp.mjpeg create mode 100644 tests/ref/fate/mpjpeg-x-timestamp-demux diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak index 4cdc1a583f..4da5dafe99 100644 --- a/tests/fate/demux.mak +++ b/tests/fate/demux.mak @@ -175,6 +175,9 @@ fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-timed-id3-demux fate-ts-timed-id3-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/mpegts/id3.ts +FATE_FFPROBE_DEMUX-$(CONFIG_MPJPEG_DEMUXER) += fate-mpjpeg-x-timestamp-demux +fate-mpjpeg-x-timestamp-demux: CMD = ffprobe_demux $(SRC_PATH)/tests/mpjpeg-x-timestamp.mjpeg + tests/data/id3.ts: TAG = GEN tests/data/id3.ts: $(SAMPLES)/mpegts/id3.ts | tests/data $(Q)cp $< $@ diff --git a/tests/mpjpeg-x-timestamp.mjpeg b/tests/mpjpeg-x-timestamp.mjpeg new file mode 100644 index 0000000000000000000000000000000000000000..df96b253bc63ee6a695ed9503427dadd5846021d GIT binary patch literal 613 zcmdPZ<>hkD&nrpIE71+9EJ(Fd$jnVlPu0&VNKNPEiqH+o%uOvWNz5&<QZUpr0ExI2 zCFZ6UC6)k{8XM>vBDDLY=B1ZpSSc78neuY+a{a&Y{{e%5mz$>>10xdy10xVJ{Qt)w z;FDOEY-XruXsKstV94<QHiI(*2MY+Wf&m*cU}tCN;NSp~Y;0f_2PY>dPyk4AadGkR zaC7tV@d*lw{6E6r!@vkM9|f>6q6@LH3!p1v5X2Dqe}F-d1LR(2MkNL&K?Y_)hX1!1 ic$gW193b1Cp_?JFzW)DBUM^iy0uLA{uz((EAq4>OG+|8u literal 0 HcmV?d00001 diff --git a/tests/ref/fate/mpjpeg-x-timestamp-demux b/tests/ref/fate/mpjpeg-x-timestamp-demux new file mode 100644 index 0000000000..efd559ae6d --- /dev/null +++ b/tests/ref/fate/mpjpeg-x-timestamp-demux @@ -0,0 +1,4 @@ +packet|codec_type=video|stream_index=0|pts=1000000|pts_time=1.000000|dts=1000000|dts_time=1.000000|duration=33333|duration_time=0.033333|size=225|pos=90|flags=K__|data_hash=CRC32:3f320fa7 +packet|codec_type=video|stream_index=0|pts=1500000|pts_time=1.500000|dts=1500000|dts_time=1.500000|duration=33333|duration_time=0.033333|size=225|pos=388|flags=K__|data_hash=CRC32:3f320fa7 +stream|index=0|codec_name=mjpeg|profile=192|codec_type=video|codec_tag_string=[0][0][0][0]|codec_tag=0x0000|width=1|height=1|coded_width=1|coded_height=1|has_b_frames=0|sample_aspect_ratio=1:1|display_aspect_ratio=1:1|pix_fmt=yuvj420p|level=-99|color_range=pc|color_space=bt470bg|color_transfer=unknown|color_primaries=unknown|chroma_location=center|field_order=unknown|id=N/A|r_frame_rate=1000000/1|avg_frame_rate=30/1|time_base=1/1000000|start_pts=1000000|start_time=1.000000|duration_ts=N/A|duration=N/A|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=8|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=2|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|disposition:multilayer=0 +format|filename=mpjpeg-x-timestamp.mjpeg|nb_streams=1|nb_programs=0|nb_stream_groups=0|format_name=mpjpeg|start_time=1.000000|duration=N/A|size=613|bit_rate=N/A|probe_score=100 -- 2.50.1 (Apple Git-155)
Please see updated v2: https://patchwork.ffmpeg.org/project/ffmpeg/patch/20251112124306.68975-1-v.s... I've addressed all comments. Thank you.
On 9. Nov 2025, at 02:38, Vladimir Sobolev <v.sobolev@gmail.com> wrote:
From 31d73c6774c1ea6d621db57f26439e297cc23c3d Mon Sep 17 00:00:00 2001 From: Vladimir Sobolev <v.sobolev@gmail.com> Date: Sun, 9 Nov 2025 02:28:13 +0200 Subject: [PATCH] avformat/mpjpegdec: add support for X-Timestamp and X-Framerate headers
Add support for parsing X-Timestamp and X-Framerate headers from HTTP multipart MJPEG streams. These headers allow servers to provide accurate timestamps and framerate information for each frame.
Changes: - Parse X-Timestamp header (in seconds) and set packet PTS/DTS - Parse X-Framerate/X-FrameRate header and update stream framerate - Maintain backward compatibility (defaults to 25 fps if not provided) - Add debug logging for parsed header values
This enables proper timestamp handling for MJPEG streams that provide timing information in HTTP headers, improving synchronization accuracy. --- libavformat/mpjpegdec.c | 69 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-)
diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c index 125b17585e..c90d7a2ad4 100644 --- a/libavformat/mpjpegdec.c +++ b/libavformat/mpjpegdec.c @@ -22,6 +22,9 @@ #include "libavutil/avstring.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/eval.h" +#include "libavutil/intfloat.h"
#include "avformat.h" #include "demux.h" @@ -34,6 +37,11 @@ typedef struct MPJPEGDemuxContext { char *searchstr; int searchstr_len; int strict_mime_boundary; + AVRational framerate; /* framerate from X-Framerate header */ + int64_t timestamp; /* timestamp from X-Timestamp header */ + int has_timestamp; /* flag indicating if timestamp was set */ + int framerate_set; /* flag indicating if framerate was set in header */ + int framerate_applied; /* flag indicating if framerate was applied to stream */ } MPJPEGDemuxContext;
static void trim_right(char *p) @@ -97,7 +105,8 @@ static int split_tag_value(char **tag, char **value, char *line) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx); + void *log_ctx, + MPJPEGDemuxContext *mpjpeg);
static int mpjpeg_read_close(AVFormatContext *s) { @@ -118,7 +127,7 @@ static int mpjpeg_read_probe(const AVProbeData *p)
ffio_init_read_context(&pb, p->buf, p->buf_size);
- ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0; + ret = (parse_multipart_header(&pb.pub, &size, "--", NULL, NULL) >= 0) ? AVPROBE_SCORE_MAX : 0;
return ret; } @@ -146,6 +155,12 @@ static int mpjpeg_read_header(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
+ /* Default framerate is 25 fps, will be updated from headers if available */ + MPJPEGDemuxContext *mpjpeg = s->priv_data; + mpjpeg->framerate = (AVRational){25, 1}; + mpjpeg->framerate_set = 0; + mpjpeg->framerate_applied = 0; + mpjpeg->has_timestamp = 0; avpriv_set_pts_info(st, 60, 1, 25);
avio_seek(s->pb, pos, SEEK_SET); @@ -167,7 +182,8 @@ static int parse_content_length(const char *value) static int parse_multipart_header(AVIOContext *pb, int* size, const char* expected_boundary, - void *log_ctx) + void *log_ctx, + MPJPEGDemuxContext *mpjpeg) { char line[128]; int found_content_type = 0; @@ -235,6 +251,33 @@ static int parse_multipart_header(AVIOContext *pb, av_log(log_ctx, AV_LOG_WARNING, "Invalid Content-Length value : %s\n", value); + } else if (mpjpeg && !av_strcasecmp(tag, "X-Timestamp")) { + double ts = av_strtod(value, NULL); + if (!isnan(ts) && isfinite(ts)) { + /* X-Timestamp is in seconds, convert to AV_TIME_BASE */ + mpjpeg->timestamp = (int64_t)(ts * AV_TIME_BASE); + mpjpeg->has_timestamp = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Timestamp: %s -> %"PRId64" (%.6f seconds)\n", + value, mpjpeg->timestamp, ts); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Timestamp value : %s\n", value); + } + } else if (mpjpeg && (!av_strcasecmp(tag, "X-Framerate") || !av_strcasecmp(tag, "X-FrameRate"))) { + AVRational fps = {0}; + if (av_parse_video_rate(&fps, value) >= 0 && fps.num > 0 && fps.den > 0) { + mpjpeg->framerate = fps; + mpjpeg->framerate_set = 1; + if (log_ctx) + av_log(log_ctx, AV_LOG_DEBUG, + "Parsed X-Framerate: %s -> %d/%d fps\n", + value, fps.num, fps.den); + } else if (log_ctx) { + av_log(log_ctx, AV_LOG_WARNING, + "Invalid X-Framerate value : %s\n", value); + } } }
@@ -311,10 +354,21 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); }
- ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s); + /* Reset timestamp flag for each packet */ + mpjpeg->has_timestamp = 0; + + ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s, mpjpeg); if (ret < 0) return ret;
+ /* Update framerate if it was set in header and hasn't been applied yet */ + if (mpjpeg->framerate_set && !mpjpeg->framerate_applied && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + st->avg_frame_rate = mpjpeg->framerate; + avpriv_set_pts_info(st, 60, mpjpeg->framerate.den, mpjpeg->framerate.num); + mpjpeg->framerate_applied = 1; + } + if (size > 0) { /* size has been provided to us in MIME header */ ret = av_get_packet(s->pb, pkt, size); @@ -353,6 +407,13 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) } }
+ /* Set timestamp from X-Timestamp header if available */ + if (ret >= 0 && mpjpeg->has_timestamp && s->nb_streams > 0) { + AVStream *st = s->streams[0]; + pkt->pts = av_rescale_q(mpjpeg->timestamp, AV_TIME_BASE_Q, st->time_base); + pkt->dts = pkt->pts; + } + return ret; }
participants (2)
-
Michael Niedermayer -
Vladimir Sobolev