jpegparse: refactor renegotation

Instead of comparing caps, just signal when renegotation is needed.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5838>
This commit is contained in:
Víctor Manuel Jáquez Leal 2023-12-19 13:16:02 +01:00
parent 03d51244c0
commit 497890f678
2 changed files with 50 additions and 36 deletions

View file

@ -318,35 +318,37 @@ static gboolean
gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg) gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg)
{ {
GstJpegFrameHdr hdr = { 0, }; GstJpegFrameHdr hdr = { 0, };
guint colorspace;
guint sampling;
if (!gst_jpeg_segment_parse_frame_header (seg, &hdr)) { if (!gst_jpeg_segment_parse_frame_header (seg, &hdr)) {
return FALSE; return FALSE;
} }
parse->colorspace = GST_JPEG_COLORSPACE_NONE; colorspace = GST_JPEG_COLORSPACE_NONE;
parse->sampling = GST_JPEG_SAMPLING_NONE; sampling = GST_JPEG_SAMPLING_NONE;
switch (hdr.num_components) { switch (hdr.num_components) {
case 1: case 1:
parse->colorspace = GST_JPEG_COLORSPACE_GRAY; colorspace = GST_JPEG_COLORSPACE_GRAY;
parse->sampling = GST_JPEG_SAMPLING_GRAYSCALE; sampling = GST_JPEG_SAMPLING_GRAYSCALE;
break; break;
case 3: case 3:
if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_JFIF)) { if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_JFIF)) {
parse->colorspace = GST_JPEG_COLORSPACE_YUV; colorspace = GST_JPEG_COLORSPACE_YUV;
parse->sampling = yuv_sampling (&hdr); sampling = yuv_sampling (&hdr);
} else { } else {
if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_ADOBE)) { if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_ADOBE)) {
if (parse->adobe_transform == 0) { if (parse->adobe_transform == 0) {
parse->colorspace = GST_JPEG_COLORSPACE_RGB; colorspace = GST_JPEG_COLORSPACE_RGB;
parse->sampling = GST_JPEG_SAMPLING_RGB; sampling = GST_JPEG_SAMPLING_RGB;
} else if (parse->adobe_transform == 1) { } else if (parse->adobe_transform == 1) {
parse->colorspace = GST_JPEG_COLORSPACE_YUV;; colorspace = GST_JPEG_COLORSPACE_YUV;;
parse->sampling = yuv_sampling (&hdr); sampling = yuv_sampling (&hdr);
} else { } else {
GST_DEBUG_OBJECT (parse, "Unknown Adobe color transform code"); GST_DEBUG_OBJECT (parse, "Unknown Adobe color transform code");
parse->colorspace = GST_JPEG_COLORSPACE_YUV;; colorspace = GST_JPEG_COLORSPACE_YUV;;
parse->sampling = yuv_sampling (&hdr); sampling = yuv_sampling (&hdr);
} }
} else { } else {
int cid0, cid1, cid2; int cid0, cid1, cid2;
@ -356,15 +358,15 @@ gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg)
cid2 = hdr.components[2].identifier; cid2 = hdr.components[2].identifier;
if (cid0 == 1 && cid1 == 2 && cid2 == 3) { if (cid0 == 1 && cid1 == 2 && cid2 == 3) {
parse->colorspace = GST_JPEG_COLORSPACE_YUV; colorspace = GST_JPEG_COLORSPACE_YUV;
parse->sampling = yuv_sampling (&hdr); sampling = yuv_sampling (&hdr);
} else if (cid0 == 'R' && cid1 == 'G' && cid2 == 'B') { } else if (cid0 == 'R' && cid1 == 'G' && cid2 == 'B') {
parse->colorspace = GST_JPEG_COLORSPACE_RGB; colorspace = GST_JPEG_COLORSPACE_RGB;
parse->sampling = GST_JPEG_SAMPLING_RGB; sampling = GST_JPEG_SAMPLING_RGB;
} else { } else {
GST_DEBUG_OBJECT (parse, "Unrecognized component IDs"); GST_DEBUG_OBJECT (parse, "Unrecognized component IDs");
parse->colorspace = GST_JPEG_COLORSPACE_YUV; colorspace = GST_JPEG_COLORSPACE_YUV;
parse->sampling = yuv_sampling (&hdr); sampling = yuv_sampling (&hdr);
} }
} }
} }
@ -372,15 +374,15 @@ gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg)
case 4: case 4:
if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_ADOBE)) { if (valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_ADOBE)) {
if (parse->adobe_transform == 0) { if (parse->adobe_transform == 0) {
parse->colorspace = GST_JPEG_COLORSPACE_CMYK; colorspace = GST_JPEG_COLORSPACE_CMYK;
} else if (parse->adobe_transform == 2) { } else if (parse->adobe_transform == 2) {
parse->colorspace = GST_JPEG_COLORSPACE_YCCK; colorspace = GST_JPEG_COLORSPACE_YCCK;
} else { } else {
GST_DEBUG_OBJECT (parse, "Unknown Adobe color transform code"); GST_DEBUG_OBJECT (parse, "Unknown Adobe color transform code");
parse->colorspace = GST_JPEG_COLORSPACE_YCCK; colorspace = GST_JPEG_COLORSPACE_YCCK;
} }
} else { } else {
parse->colorspace = GST_JPEG_COLORSPACE_CMYK; colorspace = GST_JPEG_COLORSPACE_CMYK;
} }
break; break;
default: default:
@ -388,9 +390,12 @@ gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg)
break; break;
} }
if (hdr.width != parse->width || hdr.height != parse->height) { if (hdr.width != parse->width || hdr.height != parse->height
|| colorspace != parse->colorspace || sampling != parse->sampling) {
parse->width = hdr.width; parse->width = hdr.width;
parse->height = hdr.height; parse->height = hdr.height;
parse->colorspace = colorspace;
parse->sampling = sampling;
if (parse->first_picture && !parse->multiscope) { if (parse->first_picture && !parse->multiscope) {
if (parse->orig_height > 0 if (parse->orig_height > 0
@ -404,6 +409,7 @@ gst_jpeg_parse_sof (GstJpegParse * parse, GstJpegSegment * seg)
} }
parse->first_picture = FALSE; parse->first_picture = FALSE;
parse->renegotiate = TRUE;
} }
GST_INFO_OBJECT (parse, "SOF [%dx%d] %d comp - %s", parse->width, GST_INFO_OBJECT (parse, "SOF [%dx%d] %d comp - %s", parse->width,
@ -465,8 +471,11 @@ gst_jpeg_parse_app0 (GstJpegParse * parse, GstJpegSegment * seg)
if (unit == 0) { if (unit == 0) {
/* no units, X and Y specify the pixel aspect ratio */ /* no units, X and Y specify the pixel aspect ratio */
parse->x_density = xd; if (parse->x_density != xd || parse->y_density != yd) {
parse->y_density = yd; parse->renegotiate = TRUE;
parse->x_density = xd;
parse->y_density = yd;
}
} else if (unit == 1 || unit == 2) { } else if (unit == 1 || unit == 2) {
/* tag pixel per inches */ /* tag pixel per inches */
double hppi = xd, vppi = yd; double hppi = xd, vppi = yd;
@ -682,10 +691,7 @@ gst_jpeg_parse_reset (GstJpegParse * parse)
{ {
parse->last_offset = 0; parse->last_offset = 0;
parse->state = 0; parse->state = 0;
parse->sof = -1;
parse->adobe_transform = 0; parse->adobe_transform = 0;
parse->x_density = 0;
parse->y_density = 0;
parse->field = 0; parse->field = 0;
if (parse->tags) { if (parse->tags) {
@ -704,8 +710,12 @@ static gboolean
gst_jpeg_parse_set_new_caps (GstJpegParse * parse) gst_jpeg_parse_set_new_caps (GstJpegParse * parse)
{ {
GstCaps *caps; GstCaps *caps;
GstEvent *event;
gboolean res; gboolean res;
if (!parse->renegotiate)
return TRUE;
caps = gst_caps_new_simple ("image/jpeg", "parsed", G_TYPE_BOOLEAN, TRUE, caps = gst_caps_new_simple ("image/jpeg", "parsed", G_TYPE_BOOLEAN, TRUE,
NULL); NULL);
@ -752,21 +762,17 @@ gst_jpeg_parse_set_new_caps (GstJpegParse * parse)
NULL); NULL);
} }
if (parse->prev_caps && gst_caps_is_equal_fixed (caps, parse->prev_caps)) { parse->renegotiate = FALSE;
gst_caps_unref (caps);
return TRUE;
}
GST_DEBUG_OBJECT (parse, GST_DEBUG_OBJECT (parse,
"setting downstream caps on %s:%s to %" GST_PTR_FORMAT, "setting downstream caps on %s:%s to %" GST_PTR_FORMAT,
GST_DEBUG_PAD_NAME (GST_BASE_PARSE_SRC_PAD (parse)), caps); GST_DEBUG_PAD_NAME (GST_BASE_PARSE_SRC_PAD (parse)), caps);
res = gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
gst_caps_replace (&parse->prev_caps, caps); event = gst_event_new_caps (caps);
res = gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (parse), event);
gst_caps_unref (caps); gst_caps_unref (caps);
return res; return res;
} }
static GstFlowReturn static GstFlowReturn
@ -932,8 +938,14 @@ gst_jpeg_parse_handle_frame (GstBaseParse * bparse, GstBaseParseFrame * frame,
marker <= GST_JPEG_MARKER_SOF_MAX) { marker <= GST_JPEG_MARKER_SOF_MAX) {
if (!valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_SOF) if (!valid_state (parse->state, GST_JPEG_PARSER_STATE_GOT_SOF)
&& gst_jpeg_parse_sof (parse, &seg)) { && gst_jpeg_parse_sof (parse, &seg)) {
gint8 sof;
parse->state |= GST_JPEG_PARSER_STATE_GOT_SOF; parse->state |= GST_JPEG_PARSER_STATE_GOT_SOF;
parse->sof = marker - 0xc0; sof = marker - 0xc0;
if (parse->sof != sof) {
parse->sof = sof;
parse->renegotiate = TRUE;
}
} else { } else {
GST_ELEMENT_ERROR (parse, STREAM, FORMAT, GST_ELEMENT_ERROR (parse, STREAM, FORMAT,
("Invalid data"), ("Duplicated or bad SOF marker")); ("Invalid data"), ("Duplicated or bad SOF marker"));
@ -980,6 +992,7 @@ gst_jpeg_parse_start (GstBaseParse * bparse)
parse->framerate_denominator = 1; parse->framerate_denominator = 1;
parse->first_picture = TRUE; parse->first_picture = TRUE;
parse->renegotiate = TRUE;
parse->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE; parse->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
parse->field_order = GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST; parse->field_order = GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;

View file

@ -54,6 +54,7 @@ struct _GstJpegParse {
gboolean first_picture; gboolean first_picture;
gboolean multiscope; gboolean multiscope;
gboolean avid; gboolean avid;
gboolean renegotiate;
gint8 sof; gint8 sof;
gint8 adobe_transform; gint8 adobe_transform;