kateenc: Refactoring for readability and leak fixing

Instead of a massive if/else/if/else/if/else/...:
* Use a common cleanup path for allocated items just before leaving
  the function (which will be free-d only if we're not dealing with
  a delayed SPU).
* "goto" that cleanup path wherever needed

CID #1427096
CID #1427114
This commit is contained in:
Edward Hervey 2018-01-11 11:33:22 +01:00 committed by Edward Hervey
parent 568c552ac0
commit c755ad5032

View File

@ -780,6 +780,8 @@ gst_kate_enc_chain_spu (GstKateEnc * ke, GstBuffer * buf)
kate_palette *kpalette; kate_palette *kpalette;
GstFlowReturn rflow; GstFlowReturn rflow;
int ret = 0; int ret = 0;
GstClockTime start, stop;
kate_float t0, t1;
/* allocate region, bitmap, and palette, in case we have to delay encoding them */ /* allocate region, bitmap, and palette, in case we have to delay encoding them */
kregion = (kate_region *) g_malloc (sizeof (kate_region)); kregion = (kate_region *) g_malloc (sizeof (kate_region));
@ -810,22 +812,21 @@ gst_kate_enc_chain_spu (GstKateEnc * ke, GstBuffer * buf)
} }
} }
#endif #endif
g_free (kregion); goto beach;
g_free (kbitmap); }
g_free (kpalette);
} else if (G_UNLIKELY (kbitmap->width == 0 || kbitmap->height == 0)) { if (G_UNLIKELY (kbitmap->width == 0 || kbitmap->height == 0)) {
/* there are some DVDs (well, at least one) where some dimwits put in a wholly transparent full screen 720x576 SPU !!!!?! */ /* there are some DVDs (well, at least one) where some dimwits put in a wholly transparent full screen 720x576 SPU !!!!?! */
GST_WARNING_OBJECT (ke, "SPU is totally invisible - dimwits"); GST_WARNING_OBJECT (ke, "SPU is totally invisible - dimwits");
rflow = GST_FLOW_OK; rflow = GST_FLOW_OK;
} else { goto beach;
}
/* timestamp offsets are hidden in the SPU packets */ /* timestamp offsets are hidden in the SPU packets */
GstClockTime start = start = GST_BUFFER_TIMESTAMP (buf) + GST_KATE_STM_TO_GST (ke->show_time);
GST_BUFFER_TIMESTAMP (buf) + GST_KATE_STM_TO_GST (ke->show_time); stop = GST_BUFFER_TIMESTAMP (buf) + GST_KATE_STM_TO_GST (ke->hide_time);
GstClockTime stop = t0 = start / (double) GST_SECOND;
GST_BUFFER_TIMESTAMP (buf) + GST_KATE_STM_TO_GST (ke->hide_time); t1 = stop / (double) GST_SECOND;
kate_float t0 = start / (double) GST_SECOND;
kate_float t1 = stop / (double) GST_SECOND;
GST_DEBUG_OBJECT (ke, "buf ts %f, start/show %hu/%hu", GST_DEBUG_OBJECT (ke, "buf ts %f, start/show %hu/%hu",
GST_BUFFER_TIMESTAMP (buf) / (double) GST_SECOND, ke->show_time, GST_BUFFER_TIMESTAMP (buf) / (double) GST_SECOND, ke->show_time,
ke->hide_time); ke->hide_time);
@ -852,22 +853,23 @@ gst_kate_enc_chain_spu (GstKateEnc * ke, GstBuffer * buf)
if (G_UNLIKELY (ret < 0)) { if (G_UNLIKELY (ret < 0)) {
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL), GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Failed to set region: %s", gst_kate_util_get_error_message (ret))); ("Failed to set region: %s", gst_kate_util_get_error_message (ret)));
rflow = GST_FLOW_ERROR; goto error_return;
} else { }
ret = kate_encode_set_palette (&ke->k, kpalette); ret = kate_encode_set_palette (&ke->k, kpalette);
if (G_UNLIKELY (ret < 0)) { if (G_UNLIKELY (ret < 0)) {
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL), GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Failed to set palette: %s", ("Failed to set palette: %s", gst_kate_util_get_error_message (ret)));
gst_kate_util_get_error_message (ret))); goto error_return;
rflow = GST_FLOW_ERROR; }
} else {
ret = kate_encode_set_bitmap (&ke->k, kbitmap); ret = kate_encode_set_bitmap (&ke->k, kbitmap);
if (G_UNLIKELY (ret < 0)) { if (G_UNLIKELY (ret < 0)) {
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL), GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Failed to set bitmap: %s", ("Failed to set bitmap: %s", gst_kate_util_get_error_message (ret)));
gst_kate_util_get_error_message (ret))); goto error_return;
rflow = GST_FLOW_ERROR; }
} else {
/* Some SPUs have no hide time - so I'm going to delay the encoding of the packet /* Some SPUs have no hide time - so I'm going to delay the encoding of the packet
till either a suitable event happens, and the time of this event will be used till either a suitable event happens, and the time of this event will be used
as the end time of this SPU, which will then be encoded and sent off. Suitable as the end time of this SPU, which will then be encoded and sent off. Suitable
@ -883,23 +885,21 @@ gst_kate_enc_chain_spu (GstKateEnc * ke, GstBuffer * buf)
ke->delayed_palette = kpalette; ke->delayed_palette = kpalette;
ke->delayed_region = kregion; ke->delayed_region = kregion;
rflow = GST_FLOW_OK; rflow = GST_FLOW_OK;
} else { goto beach;
}
ret = kate_encode_text (&ke->k, t0, t1, "", 0, &kp); ret = kate_encode_text (&ke->k, t0, t1, "", 0, &kp);
if (G_UNLIKELY (ret < 0)) { if (G_UNLIKELY (ret < 0)) {
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL), GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Failed to encode empty text for SPU buffer: %s", ("Failed to encode empty text for SPU buffer: %s",
gst_kate_util_get_error_message (ret))); gst_kate_util_get_error_message (ret)));
rflow = GST_FLOW_ERROR; goto error_return;
} else {
rflow =
gst_kate_enc_chain_push_packet (ke, &kp, start,
stop - start + 1);
}
}
}
}
} }
rflow = gst_kate_enc_chain_push_packet (ke, &kp, start, stop - start + 1);
beach:
/* Cleanup data if we're not keeping it around */
if (!ke->delayed_spu) { if (!ke->delayed_spu) {
g_free (kpalette->colors); g_free (kpalette->colors);
g_free (kpalette); g_free (kpalette);
@ -907,9 +907,14 @@ gst_kate_enc_chain_spu (GstKateEnc * ke, GstBuffer * buf)
g_free (kbitmap); g_free (kbitmap);
g_free (kregion); g_free (kregion);
} }
}
return rflow; return rflow;
error_return:
{
rflow = GST_FLOW_ERROR;
goto beach;
}
} }
static GstFlowReturn static GstFlowReturn