]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264_parser.c
Fix incorrect increment in sgidec.c
[ffmpeg] / libavcodec / h264_parser.c
index 614dad35ebac1b21e902213addf1aaf3e8aa8318..c9dea81a4913870d07a4feaf62f420cba1c07c80 100644 (file)
@@ -25,6 +25,8 @@
  * @author Michael Niedermayer <michaelni@gmx.at>
  */
 
+#define UNCHECKED_BITSTREAM_READER 1
+
 #include "parser.h"
 #include "h264data.h"
 #include "golomb.h"
@@ -93,17 +95,28 @@ static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_si
                     goto found;
                 }
             }else if(v==1 || v==2 || v==5){
+                state+=8;
+                continue;
+            }
+            state= 7;
+        }else{
+            h->parse_history[h->parse_history_count++]= buf[i];
+            if(h->parse_history_count>3){
+                unsigned int mb, last_mb= h->parse_last_mb;
+                GetBitContext gb;
+
+                init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
+                h->parse_history_count=0;
+                mb= get_ue_golomb_long(&gb);
+                last_mb= h->parse_last_mb;
+                h->parse_last_mb= mb;
                 if(pc->frame_start_found){
-                    state+=8;
-                    continue;
+                    if(mb <= last_mb)
+                        goto found;
                 }else
                     pc->frame_start_found = 1;
+                state= 7;
             }
-            state= 7;
-        }else{
-            if(buf[i] & 0x80)
-                goto found;
-            state= 7;
         }
     }
     pc->state= state;
@@ -116,7 +129,7 @@ found:
     pc->frame_start_found= 0;
     if(h->is_avc)
         return next_avc;
-    return i-(state&5);
+    return i-(state&5) - 3*(state>7);
 }
 
 /**
@@ -137,6 +150,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     unsigned int slice_type;
     int state = -1;
     const uint8_t *ptr;
+    int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
 
     /* set some sane default values */
     s->pict_type = AV_PICTURE_TYPE_I;
@@ -185,7 +199,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             s->key_frame = 1;
             /* fall through */
         case NAL_SLICE:
-            get_ue_golomb(&h->s.gb);  // skip first_mb_in_slice
+            get_ue_golomb_long(&h->s.gb);  // skip first_mb_in_slice
             slice_type = get_ue_golomb_31(&h->s.gb);
             s->pict_type = golomb_to_pict_type[slice_type % 5];
             if (h->sei_recovery_frame_cnt >= 0) {
@@ -255,6 +269,8 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         }
         buf += consumed;
     }
+    if (q264)
+        return 0;
     /* didn't find a picture! */
     av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
     return -1;
@@ -360,14 +376,15 @@ static int init(AVCodecParserContext *s)
 {
     H264Context *h = s->priv_data;
     h->thread_context[0] = h;
+    h->s.slice_context_count = 1;
     return 0;
 }
 
 AVCodecParser ff_h264_parser = {
-    { CODEC_ID_H264 },
-    sizeof(H264Context),
-    init,
-    h264_parse,
-    close,
-    h264_split,
+    .codec_ids      = { CODEC_ID_H264 },
+    .priv_data_size = sizeof(H264Context),
+    .parser_init    = init,
+    .parser_parse   = h264_parse,
+    .parser_close   = close,
+    .split          = h264_split,
 };