avcodec/idctdsp: Transmit studio_profile to init instead of using AVCodecContext...
[ffmpeg.git] / libavcodec / h264_parser.c
index ce46c58..1a9840a 100644 (file)
 #include "libavutil/mem.h"
 #include "libavutil/pixfmt.h"
 
+#include "avcodec.h"
 #include "get_bits.h"
 #include "golomb.h"
 #include "h264.h"
 #include "h264_sei.h"
+#include "h264_ps.h"
 #include "h264data.h"
 #include "internal.h"
 #include "mpegutils.h"
@@ -58,6 +60,8 @@ typedef struct H264ParseContext {
     uint8_t parse_history[6];
     int parse_history_count;
     int parse_last_mb;
+    int64_t reference_dts;
+    int last_frame_num, last_picture_structure;
 } H264ParseContext;
 
 
@@ -104,14 +108,14 @@ static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
                 state >>= 1;           // 2->1, 1->0, 0->0
         } else if (state <= 5) {
             int nalu_type = buf[i] & 0x1F;
-            if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
-                nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
+            if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
+                nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
                 if (pc->frame_start_found) {
                     i++;
                     goto found;
                 }
-            } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
-                       nalu_type == NAL_IDR_SLICE) {
+            } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
+                       nalu_type == H264_NAL_IDR_SLICE) {
                 state += 8;
                 continue;
             }
@@ -198,7 +202,7 @@ static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
     if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
         (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
         ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
-                                  &pwt, logctx);
+                                  &pwt, p->picture_structure, logctx);
 
     if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
         int i;
@@ -226,26 +230,6 @@ static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
     return 0;
 }
 
-static inline int get_avc_nalsize(H264ParseContext *p, const uint8_t *buf,
-                                  int buf_size, int *buf_index, void *logctx)
-{
-    int i, nalsize = 0;
-
-    if (*buf_index >= buf_size - p->nal_length_size) {
-        // the end of the buffer is reached, refill it
-        return AVERROR(EAGAIN);
-    }
-
-    for (i = 0; i < p->nal_length_size; i++)
-        nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];
-    if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
-        av_log(logctx, AV_LOG_ERROR,
-               "AVC: nal size %d\n", nalsize);
-        return AVERROR_INVALIDDATA;
-    }
-    return nalsize;
-}
-
 /**
  * Parse NAL units of found picture and decode some basic information.
  *
@@ -259,6 +243,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                                   const uint8_t * const buf, int buf_size)
 {
     H264ParseContext *p = s->priv_data;
+    H2645RBSP rbsp = { NULL };
     H2645NAL nal = { NULL };
     int buf_index, next_avc;
     unsigned int pps_id;
@@ -274,11 +259,15 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
 
     ff_h264_sei_uninit(&p->sei);
-    p->sei.frame_packing.frame_packing_arrangement_cancel_flag = -1;
+    p->sei.frame_packing.arrangement_cancel_flag = -1;
 
     if (!buf_size)
         return 0;
 
+    av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size, buf_size);
+    if (!rbsp.rbsp_buffer)
+        return AVERROR(ENOMEM);
+
     buf_index     = 0;
     next_avc      = p->is_avc ? 0 : buf_size;
     for (;;) {
@@ -286,7 +275,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         int src_length, consumed, nalsize = 0;
 
         if (buf_index >= next_avc) {
-            nalsize = get_avc_nalsize(p, buf, buf_size, &buf_index, avctx);
+            nalsize = get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx);
             if (nalsize < 0)
                 break;
             next_avc = buf_index + nalsize;
@@ -301,10 +290,10 @@ static inline int parse_nal_units(AVCodecParserContext *s,
 
         state = buf[buf_index];
         switch (state & 0x1f) {
-        case NAL_SLICE:
-        case NAL_IDR_SLICE:
+        case H264_NAL_SLICE:
+        case H264_NAL_IDR_SLICE:
             // Do not walk the whole buffer just to decode slice header
-            if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
+            if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
                 /* IDR or disposable slice
                  * No need to decode many bytes because MMCOs shall not be present. */
                 if (src_length > 60)
@@ -316,7 +305,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             }
             break;
         }
-        consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
+        consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &rbsp, &nal, 1);
         if (consumed < 0)
             break;
 
@@ -330,17 +319,17 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         nal.type    = get_bits(&nal.gb, 5);
 
         switch (nal.type) {
-        case NAL_SPS:
+        case H264_NAL_SPS:
             ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
             break;
-        case NAL_PPS:
+        case H264_NAL_PPS:
             ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
                                                  nal.size_bits);
             break;
-        case NAL_SEI:
+        case H264_NAL_SEI:
             ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
             break;
-        case NAL_IDR_SLICE:
+        case H264_NAL_IDR_SLICE:
             s->key_frame = 1;
 
             p->poc.prev_frame_num        = 0;
@@ -348,7 +337,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             p->poc.prev_poc_msb          =
             p->poc.prev_poc_lsb          = 0;
         /* fall through */
-        case NAL_SLICE:
+        case H264_NAL_SLICE:
             get_ue_golomb_long(&nal.gb);  // skip first_mb_in_slice
             slice_type   = get_ue_golomb_31(&nal.gb);
             s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
@@ -438,7 +427,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                 }
             }
 
-            if (nal.type == NAL_IDR_SLICE)
+            if (nal.type == H264_NAL_IDR_SLICE)
                 get_ue_golomb_long(&nal.gb); /* idr_pic_id */
             if (sps->poc_type == 0) {
                 p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
@@ -460,14 +449,16 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             /* Decode POC of this picture.
              * The prev_ values needed for decoding POC of the next picture are not set here. */
             field_poc[0] = field_poc[1] = INT_MAX;
-            ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
+            ret = ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
                              &p->poc, p->picture_structure, nal.ref_idc);
+            if (ret < 0)
+                goto fail;
 
             /* Continue parsing to check if MMCO_RESET is present.
              * FIXME: MMCO_RESET could appear in non-first slice.
              *        Maybe, we should parse all undisposable non-IDR slice of this
              *        picture until encountering MMCO_RESET in a slice of it. */
-            if (nal.ref_idc && nal.type != NAL_IDR_SLICE) {
+            if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
                 got_reset = scan_mmco_reset(s, &nal.gb, avctx);
                 if (got_reset < 0)
                     goto fail;
@@ -487,25 +478,25 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                 }
             }
 
-            if (sps->pic_struct_present_flag) {
+            if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
                 switch (p->sei.picture_timing.pic_struct) {
-                case SEI_PIC_STRUCT_TOP_FIELD:
-                case SEI_PIC_STRUCT_BOTTOM_FIELD:
+                case H264_SEI_PIC_STRUCT_TOP_FIELD:
+                case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
                     s->repeat_pict = 0;
                     break;
-                case SEI_PIC_STRUCT_FRAME:
-                case SEI_PIC_STRUCT_TOP_BOTTOM:
-                case SEI_PIC_STRUCT_BOTTOM_TOP:
+                case H264_SEI_PIC_STRUCT_FRAME:
+                case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
+                case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
                     s->repeat_pict = 1;
                     break;
-                case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
-                case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
+                case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
+                case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
                     s->repeat_pict = 2;
                     break;
-                case SEI_PIC_STRUCT_FRAME_DOUBLING:
+                case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
                     s->repeat_pict = 3;
                     break;
-                case SEI_PIC_STRUCT_FRAME_TRIPLING:
+                case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
                     s->repeat_pict = 5;
                     break;
                 default:
@@ -518,14 +509,14 @@ static inline int parse_nal_units(AVCodecParserContext *s,
 
             if (p->picture_structure == PICT_FRAME) {
                 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
-                if (sps->pic_struct_present_flag) {
+                if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
                     switch (p->sei.picture_timing.pic_struct) {
-                    case SEI_PIC_STRUCT_TOP_BOTTOM:
-                    case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
+                    case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
+                    case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
                         s->field_order = AV_FIELD_TT;
                         break;
-                    case SEI_PIC_STRUCT_BOTTOM_TOP:
-                    case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
+                    case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
+                    case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
                         s->field_order = AV_FIELD_BB;
                         break;
                     default:
@@ -545,21 +536,33 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                     s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
                 else
                     s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
-                s->field_order = AV_FIELD_UNKNOWN;
+                if (p->poc.frame_num == p->last_frame_num &&
+                    p->last_picture_structure != AV_PICTURE_STRUCTURE_UNKNOWN &&
+                    p->last_picture_structure != AV_PICTURE_STRUCTURE_FRAME &&
+                    p->last_picture_structure != s->picture_structure) {
+                    if (p->last_picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD)
+                        s->field_order = AV_FIELD_TT;
+                    else
+                        s->field_order = AV_FIELD_BB;
+                } else {
+                    s->field_order = AV_FIELD_UNKNOWN;
+                }
+                p->last_picture_structure = s->picture_structure;
+                p->last_frame_num = p->poc.frame_num;
             }
 
-            av_freep(&nal.rbsp_buffer);
+            av_freep(&rbsp.rbsp_buffer);
             return 0; /* no need to evaluate the rest */
         }
     }
     if (q264) {
-        av_freep(&nal.rbsp_buffer);
+        av_freep(&rbsp.rbsp_buffer);
         return 0;
     }
     /* didn't find a picture! */
     av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
 fail:
-    av_freep(&nal.rbsp_buffer);
+    av_freep(&rbsp.rbsp_buffer);
     return -1;
 }
 
@@ -616,6 +619,26 @@ static int h264_parse(AVCodecParserContext *s,
         s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
     }
 
+    if (s->dts_sync_point >= 0) {
+        int64_t den = avctx->time_base.den * (int64_t)avctx->pkt_timebase.num;
+        if (den > 0) {
+            int64_t num = avctx->time_base.num * (int64_t)avctx->pkt_timebase.den;
+            if (s->dts != AV_NOPTS_VALUE) {
+                // got DTS from the stream, update reference timestamp
+                p->reference_dts = s->dts - av_rescale(s->dts_ref_dts_delta, num, den);
+            } else if (p->reference_dts != AV_NOPTS_VALUE) {
+                // compute DTS based on reference timestamp
+                s->dts = p->reference_dts + av_rescale(s->dts_ref_dts_delta, num, den);
+            }
+
+            if (p->reference_dts != AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE)
+                s->pts = s->dts + av_rescale(s->pts_dts_delta, num, den);
+
+            if (s->dts_sync_point > 0)
+                p->reference_dts = s->dts; // new reference
+        }
+    }
+
     *poutbuf      = buf;
     *poutbuf_size = buf_size;
     return next;
@@ -635,17 +658,17 @@ static int h264_split(AVCodecContext *avctx,
         if ((state & 0xFFFFFF00) != 0x100)
             break;
         nalu_type = state & 0x1F;
-        if (nalu_type == NAL_SPS) {
+        if (nalu_type == H264_NAL_SPS) {
             has_sps = 1;
-        } else if (nalu_type == NAL_PPS)
+        } else if (nalu_type == H264_NAL_PPS)
             has_pps = 1;
         /* else if (nalu_type == 0x01 ||
          *     nalu_type == 0x02 ||
          *     nalu_type == 0x05) {
          *  }
          */
-        else if ((nalu_type != NAL_SEI || has_pps) &&
-                  nalu_type != NAL_AUD && nalu_type != NAL_SPS_EXT &&
+        else if ((nalu_type != H264_NAL_SEI || has_pps) &&
+                  nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
                   nalu_type != 0x0f) {
             if (has_sps) {
                 while (ptr - 4 > buf && ptr[-5] == 0)
@@ -673,6 +696,8 @@ static av_cold int init(AVCodecParserContext *s)
 {
     H264ParseContext *p = s->priv_data;
 
+    p->reference_dts = AV_NOPTS_VALUE;
+    p->last_frame_num = INT_MAX;
     ff_h264dsp_init(&p->h264dsp, 8, 1);
     return 0;
 }