From 7ca43932e5d37035d5876988678d051168f8d371 Mon Sep 17 00:00:00 2001 From: Gwenole Beauchesne Date: Wed, 2 Jan 2013 14:18:31 +0100 Subject: [PATCH] mpeg2: optimize scan for the second start code. Optimize scan for the second start code, on the next parse() call so that to avoid scanning again earlier bytes where we didn't find any start code. --- gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c b/gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c index 7ecd9d19c3..1624dd8fbb 100644 --- a/gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c +++ b/gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c @@ -1160,13 +1160,14 @@ gst_vaapi_decoder_mpeg2_parse(GstVaapiDecoder *base_decoder, { GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(base_decoder); + GstVaapiParserState * const ps = GST_VAAPI_PARSER_STATE(base_decoder); GstVaapiDecoderUnitMpeg2 *unit; GstVaapiDecoderStatus status; GstMpegVideoPacket *packet; const guchar *buf; guint32 start_code; guint size, buf_size, flags; - gint ofs; + gint ofs, ofs2; status = ensure_decoder(decoder); if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) @@ -1179,18 +1180,28 @@ gst_vaapi_decoder_mpeg2_parse(GstVaapiDecoder *base_decoder, ofs = scan_for_start_code(adapter, 0, size, &start_code); if (ofs < 0) return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA; - gst_adapter_flush(adapter, ofs); - size -= ofs; - ofs = G_UNLIKELY(size < 8) ? -1 : - scan_for_start_code(adapter, 4, size - 4, NULL); + if (ofs > 0) { + gst_adapter_flush(adapter, ofs); + size -= ofs; + } + + ofs2 = ps->input_offset2 - ofs - 4; + if (ofs2 < 4) + ofs2 = 4; + + ofs = G_UNLIKELY(size < ofs2 + 4) ? -1 : + scan_for_start_code(adapter, ofs2, size - ofs2, NULL); if (ofs < 0) { // Assume the whole packet is present if end-of-stream - if (!at_eos) + if (!at_eos) { + ps->input_offset2 = size; return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA; + } ofs = size; } buf_size = ofs; + ps->input_offset2 = 0; buf = gst_adapter_peek(adapter, buf_size); if (!buf)