]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/parser.c
Fix misspelled parameter names in Doxygen documentation.
[ffmpeg] / libavcodec / parser.c
index b0a6a2a1b079dec0bd083fe68f758eb4943dd566..864b5f2d2ae1e0cd10c030cc2727785a438b18b4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Audio and Video frame extraction
- * Copyright (c) 2003 Fabrice Bellard.
- * Copyright (c) 2003 Michael Niedermayer.
+ * Copyright (c) 2003 Fabrice Bellard
+ * Copyright (c) 2003 Michael Niedermayer
  *
  * This file is part of FFmpeg.
  *
@@ -22,7 +22,7 @@
 
 #include "parser.h"
 
-AVCodecParser *av_first_parser = NULL;
+static AVCodecParser *av_first_parser = NULL;
 
 AVCodecParser* av_parser_next(AVCodecParser *p){
     if(p) return p->next;
@@ -73,9 +73,38 @@ AVCodecParserContext *av_parser_init(int codec_id)
     }
     s->fetch_timestamp=1;
     s->pict_type = FF_I_TYPE;
+    s->key_frame = -1;
+    s->convergence_duration = AV_NOPTS_VALUE;
+    s->dts_sync_point       = INT_MIN;
+    s->dts_ref_dts_delta    = INT_MIN;
+    s->pts_dts_delta        = INT_MIN;
     return s;
 }
 
+void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){
+    int i;
+
+    s->dts= s->pts= AV_NOPTS_VALUE;
+    s->pos= -1;
+    s->offset= 0;
+    for(i = 0; i < AV_PARSER_PTS_NB; i++) {
+        if (   s->cur_offset + off >= s->cur_frame_offset[i]
+            && (s->frame_offset < s->cur_frame_offset[i] ||
+              (!s->frame_offset && !s->next_frame_offset)) // first field/frame
+            //check is disabled  because mpeg-ts doesnt send complete PES packets
+            && /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
+            s->dts= s->cur_frame_dts[i];
+            s->pts= s->cur_frame_pts[i];
+            s->pos= s->cur_frame_pos[i];
+            s->offset = s->next_frame_offset - s->cur_frame_offset[i];
+            if(remove)
+                s->cur_frame_offset[i]= INT64_MAX;
+            if(s->cur_offset + off < s->cur_frame_end[i])
+                break;
+        }
+    }
+}
+
 /**
  *
  * @param buf           input
@@ -99,6 +128,8 @@ AVCodecParserContext *av_parser_init(int codec_id)
  *          decode_frame(data, size);
  *   }
  * @endcode
+ *
+ * @deprecated Use av_parser_parse2() instead.
  */
 int av_parser_parse(AVCodecParserContext *s,
                     AVCodecContext *avctx,
@@ -106,30 +137,41 @@ int av_parser_parse(AVCodecParserContext *s,
                     const uint8_t *buf, int buf_size,
                     int64_t pts, int64_t dts)
 {
-    int index, i, k;
+    return av_parser_parse2(s, avctx, poutbuf, poutbuf_size, buf, buf_size, pts, dts, AV_NOPTS_VALUE);
+}
+
+int av_parser_parse2(AVCodecParserContext *s,
+                     AVCodecContext *avctx,
+                     uint8_t **poutbuf, int *poutbuf_size,
+                     const uint8_t *buf, int buf_size,
+                     int64_t pts, int64_t dts,
+                     int64_t pos)
+{
+    int index, i;
     uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
 
     if (buf_size == 0) {
         /* padding is always necessary even if EOF, so we add it here */
         memset(dummy_buf, 0, sizeof(dummy_buf));
         buf = dummy_buf;
-    } else {
+    } else if (s->cur_offset + buf_size !=
+               s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
         /* add a new packet descriptor */
-        k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
-        s->cur_frame_start_index = k;
-        s->cur_frame_offset[k] = s->cur_offset;
-        s->cur_frame_pts[k] = pts;
-        s->cur_frame_dts[k] = dts;
-
-        /* fill first PTS/DTS */
-        if (s->fetch_timestamp){
-            s->fetch_timestamp=0;
-            s->last_pts = pts;
-            s->last_dts = dts;
-            s->last_offset = 0;
-            s->cur_frame_pts[k] =
-            s->cur_frame_dts[k] = AV_NOPTS_VALUE;
-        }
+            i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
+            s->cur_frame_start_index = i;
+            s->cur_frame_offset[i] = s->cur_offset;
+            s->cur_frame_end[i] = s->cur_offset + buf_size;
+            s->cur_frame_pts[i] = pts;
+            s->cur_frame_dts[i] = dts;
+            s->cur_frame_pos[i] = pos;
+    }
+
+    if (s->fetch_timestamp){
+        s->fetch_timestamp=0;
+        s->last_pts = s->pts;
+        s->last_dts = s->dts;
+        s->last_pos = s->pos;
+        ff_fetch_timestamp(s, 0, 0);
     }
 
     /* WARNING: the returned index can be negative */
@@ -138,35 +180,11 @@ int av_parser_parse(AVCodecParserContext *s,
     /* update the file pointer */
     if (*poutbuf_size) {
         /* fill the data for the current frame */
-        s->frame_offset = s->last_frame_offset;
-        s->pts = s->last_pts;
-        s->dts = s->last_dts;
-        s->offset = s->last_offset;
+        s->frame_offset = s->next_frame_offset;
 
         /* offset of the next frame */
-        s->last_frame_offset = s->cur_offset + index;
-        /* find the packet in which the new frame starts. It
-           is tricky because of MPEG video start codes
-           which can begin in one packet and finish in
-           another packet. In the worst case, an MPEG
-           video start code could be in 4 different
-           packets. */
-        k = s->cur_frame_start_index;
-        for(i = 0; i < AV_PARSER_PTS_NB; i++) {
-            if (s->last_frame_offset >= s->cur_frame_offset[k])
-                break;
-            k = (k - 1) & (AV_PARSER_PTS_NB - 1);
-        }
-
-        s->last_pts = s->cur_frame_pts[k];
-        s->last_dts = s->cur_frame_dts[k];
-        s->last_offset = s->last_frame_offset - s->cur_frame_offset[k];
-
-        /* some parsers tell us the packet size even before seeing the first byte of the next packet,
-           so the next pts/dts is in the next chunk */
-        if(index == buf_size){
-            s->fetch_timestamp=1;
-        }
+        s->next_frame_offset = s->cur_offset + index;
+        s->fetch_timestamp=1;
     }
     if (index < 0)
         index = 0;
@@ -197,7 +215,7 @@ int av_parser_change(AVCodecParserContext *s,
     *poutbuf_size= buf_size;
     if(avctx->extradata){
         if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
-            /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
+            /*||(s->pict_type != FF_I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
             /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
             int size= buf_size + avctx->extradata_size;
             *poutbuf_size= size;
@@ -214,17 +232,19 @@ int av_parser_change(AVCodecParserContext *s,
 
 void av_parser_close(AVCodecParserContext *s)
 {
-    if (s->parser->parser_close)
-        s->parser->parser_close(s);
-    av_free(s->priv_data);
-    av_free(s);
+    if(s){
+        if (s->parser->parser_close)
+            s->parser->parser_close(s);
+        av_free(s->priv_data);
+        av_free(s);
+    }
 }
 
 /*****************************************************/
 
 /**
  * combines the (truncated) bitstream to a complete frame
- * @returns -1 if no complete frame could be created
+ * @return -1 if no complete frame could be created, AVERROR(ENOMEM) if there was a memory allocation error
  */
 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
 {
@@ -249,8 +269,11 @@ int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_s
 
     /* copy into buffer end return */
     if(next == END_NOT_FOUND){
-        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
+        void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
 
+        if(!new_buffer)
+            return AVERROR(ENOMEM);
+        pc->buffer = new_buffer;
         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
         pc->index += *buf_size;
         return -1;
@@ -261,8 +284,11 @@ int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_s
 
     /* append to buffer */
     if(pc->index){
-        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
+        void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
 
+        if(!new_buffer)
+            return AVERROR(ENOMEM);
+        pc->buffer = new_buffer;
         memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
         pc->index = 0;
         *buf= pc->buffer;
@@ -271,6 +297,7 @@ int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_s
     /* store overread bytes */
     for(;next < 0; next++){
         pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
+        pc->state64 = (pc->state64<<8) | pc->buffer[pc->last_index + next];
         pc->overread++;
     }
 
@@ -288,7 +315,7 @@ void ff_parse_close(AVCodecParserContext *s)
 {
     ParseContext *pc = s->priv_data;
 
-    av_free(pc->buffer);
+    av_freep(&pc->buffer);
 }
 
 void ff_parse1_close(AVCodecParserContext *s)