From 00b457adb855b0b25ac7cde4a9cb8c7b53fd0511 Mon Sep 17 00:00:00 2001
From: liuyuxin <liuyuxin@xiaomi.com>
Date: Wed, 22 Apr 2015 16:56:42 +0800
Subject: [PATCH] re-fetch PTS/DTS if fetch failed

IQiYi has special PES packets:
|<-------PES#1------->|<-----PES#2--->|
| AUD | P frame | PPS | AUD | P frame |

This kind of packet confused H264 parser, because  AUD ,PPS, SPS
and SEI are all considered as frame boundaries. (Details are in
libavcode/h264_parser.cpp) So, parser finds two frames in PES#1,
the second frame is supposed to fetch PTS/DTS of PES#2. Actually,
it doesn't. The second frame has only PPS(no P frame data), which
cause ff_fetch_timestamp() failed and outputbuf was NULL.
To hanlde this case, we need to re-fetch PTS/DTS.

Signed-off-by: liuyuxin <luckliuyuxin@163.com>
---
 libavcodec/parser.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libavcodec/parser.c b/libavcodec/parser.c
index b06a959..e62407b 100644
--- a/libavcodec/parser.c
+++ b/libavcodec/parser.c
@@ -163,6 +163,11 @@ int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
         s->last_dts        = s->dts;
         s->last_pos        = s->pos;
         ff_fetch_timestamp(s, 0, 0, 0);
+        if (s->dts == AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE
+                && s->pos == -1 && !s->offset) {
+            //fetch failed, re-fetch next time
+            s->fetch_timestamp = 1;
+        }
     }
     /* WARNING: the returned index can be negative */
     index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
-- 
1.9.1

