wg <[email protected]> added the comment:

Just now noticed this issue.  I hit the same problem last year and fixed it;
here is what I wrote to Ian Caulfield to explain the patch:

--------------------------------------------------
Analysis has shown that in this case the

    if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new
packet */

check gets "stuck" and is never true (even though more correct dvbsub
packets keep arriving).  I then looked at other parsers and _none_ of
them use the pts values for detecting packet start; the last_pts and
fetch_timestamp fields are even marked "private" in the parser header
file, so I suppose they shouldn't really be used at all in the parser.

So I rewrote that part of the parser detecting the start code and then
parsing segments as before, but without any pts touching.  Now
everything works fine with all samples that I have.  The parser is now
also slightly smaller and more efficient, as it doesn't keep
re-examining the complete packet for segments.

Please take a look -- the diff is long; but it's probably better to
look at it as a rewrite.

____________________________________________________
FFmpeg issue tracker <[email protected]>
<https://roundup.ffmpeg.org/roundup/ffmpeg/issue536>
____________________________________________________
--- trunk/libavcodec/dvbsub_parser.c	2008-06-02 21:18:47.000000000 +0200
+++ ffmpeg-wg/libavcodec/dvbsub_parser.c	2008-12-24 17:10:58.000000000 +0100
@@ -33,7 +33,7 @@
 /* parser definition */
 typedef struct DVBSubParseContext {
     uint8_t *packet_buf;
-    int packet_start;
+    int cursor;
     int packet_index;
     int in_packet;
 } DVBSubParseContext;
@@ -52,8 +52,6 @@
                         const uint8_t *buf, int buf_size)
 {
     DVBSubParseContext *pc = s->priv_data;
-    uint8_t *p, *p_end;
-    int len, buf_pos = 0;
 
 #ifdef DEBUG
     av_log(avctx, AV_LOG_INFO, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n",
@@ -78,106 +76,60 @@
     *poutbuf = NULL;
     *poutbuf_size = 0;
 
-    s->fetch_timestamp = 1;
-
-    if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new packet */
-    {
-        if (pc->packet_index != pc->packet_start)
-        {
-#ifdef DEBUG
-            av_log(avctx, AV_LOG_INFO, "Discarding %d bytes\n",
-                pc->packet_index - pc->packet_start);
-#endif
-        }
-
-        pc->packet_start = 0;
-        pc->packet_index = 0;
-
+    if (!pc->in_packet) {
         if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
 #ifdef DEBUG
             av_log(avctx, AV_LOG_INFO, "Bad packet header\n");
 #endif
-            return -1;
+            return buf_size; /* discard data */
         }
-
-        buf_pos = 2;
-
+        if (buf_size-2 > PARSE_BUF_SIZE)
+            return -1;
+        memcpy(pc->packet_buf, buf+2, buf_size-2);
+        pc->cursor = 0;
+        pc->packet_index = buf_size-2;
         pc->in_packet = 1;
     } else {
-        if (pc->packet_start != 0)
-        {
-            if (pc->packet_index != pc->packet_start)
-            {
-                memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
-                            pc->packet_index - pc->packet_start);
-
-                pc->packet_index -= pc->packet_start;
-                pc->packet_start = 0;
-            } else {
-                pc->packet_start = 0;
-                pc->packet_index = 0;
-            }
-        }
+        if (buf_size + pc->packet_index > PARSE_BUF_SIZE)
+            return -1;
+        memcpy(pc->packet_buf + pc->packet_index, buf, buf_size);
+        pc->packet_index += buf_size;
     }
 
-    if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
-        return -1;
-
-/* if not currently in a packet, discard data */
-    if (pc->in_packet == 0)
-        return buf_size;
-
-    memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
-    pc->packet_index += buf_size - buf_pos;
-
-    p = pc->packet_buf;
-    p_end = pc->packet_buf + pc->packet_index;
-
-    while (p < p_end)
-    {
-        if (*p == 0x0f)
-        {
-            if (p + 6 <= p_end)
-            {
-                len = AV_RB16(p + 4);
-
-                if (p + len + 6 <= p_end)
-                {
-                    *poutbuf_size += len + 6;
+    /* parse newly obtained segments */
+    while (pc->cursor < pc->packet_index) {
+        if (pc->packet_buf[pc->cursor] == 0x0f) {
+            if (pc->cursor + 6 <= pc->packet_index) {
+                int len = AV_RB16(pc->packet_buf + pc->cursor + 4);
 
-                    p += len + 6;
+                if (pc->cursor + len + 6 <= pc->packet_index) {
+                    pc->cursor += len + 6;
                 } else
                     break;
             } else
                 break;
-        } else if (*p == 0xff) {
-            if (p + 1 < p_end)
-            {
+        } else if (pc->packet_buf[pc->cursor] == 0xff) {
+            if (pc->cursor + 1 < pc->packet_index) {
 #ifdef DEBUG
                 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
 #endif
             }
-            pc->packet_index = p - pc->packet_buf;
+            *poutbuf = pc->packet_buf;
+            *poutbuf_size = pc->cursor;
+            pc->packet_index = 0;
             pc->in_packet = 0;
+#ifdef DEBUG
+            av_log(avctx, AV_LOG_INFO, "parse packet sz:%d\n", *poutbuf_size);
+#endif
             break;
         } else {
             av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
 
-            pc->packet_index = p - pc->packet_buf;
             pc->in_packet = 0;
             break;
         }
     }
 
-    if (*poutbuf_size > 0)
-    {
-        *poutbuf = pc->packet_buf;
-        pc->packet_start = *poutbuf_size;
-    }
-
-    if (s->pts == AV_NOPTS_VALUE)
-        s->pts = s->last_pts;
-
     return buf_size;
 }
 

Reply via email to