Merge commit '5c2fb561d94fc51d76ab21d6f7cc5b6cc3aa599c'
authorJames Almer <jamrial@gmail.com>
Mon, 1 Aug 2016 18:11:05 +0000 (15:11 -0300)
committerJames Almer <jamrial@gmail.com>
Mon, 1 Aug 2016 18:11:05 +0000 (15:11 -0300)
* commit '5c2fb561d94fc51d76ab21d6f7cc5b6cc3aa599c':
  h264: add H264_ prefix to the NAL unit types

Conflicts:
libavcodec/h264_parse.c
libavcodec/h264_parser.c
libavcodec/h264_slice.c
libavcodec/h264dec.c

Merged-by: James Almer <jamrial@gmail.com>
1  2 
libavcodec/h264.h
libavcodec/h264_parse.c
libavcodec/h264_parser.c
libavcodec/h264_refs.c
libavcodec/h264_slice.c
libavcodec/h264dec.c
libavcodec/omx.c
libavcodec/vaapi_encode_h264.c
libavformat/mxfenc.c

Simple merge
@@@ -346,8 -330,8 +346,8 @@@ static int decode_extradata_ps(const ui
      for (i = 0; i < pkt.nb_nals; i++) {
          H2645NAL *nal = &pkt.nals[i];
          switch (nal->type) {
-         case NAL_SPS:
+         case H264_NAL_SPS:
 -            ret = ff_h264_decode_seq_parameter_set(&nal->gb, logctx, ps);
 +            ret = ff_h264_decode_seq_parameter_set(&nal->gb, logctx, ps, 0);
              if (ret < 0)
                  goto fail;
              break;
@@@ -112,29 -89,20 +112,29 @@@ static int h264_find_frame_end(H264Pars
                      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;
 +            }
 +            state = 7;
 +        } else {
 +            p->parse_history[p->parse_history_count++] = buf[i];
 +            if (p->parse_history_count > 5) {
 +                unsigned int mb, last_mb = p->parse_last_mb;
 +                GetBitContext gb;
 +
 +                init_get_bits(&gb, p->parse_history, 8*p->parse_history_count);
 +                p->parse_history_count = 0;
 +                mb= get_ue_golomb_long(&gb);
 +                p->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 {
 -            // first_mb_in_slice is 0, probably the first nal of a new slice
 -            if (buf[i] & 0x80)
 -                goto found;
 -            state = 7;
          }
      }
      pc->state = state;
@@@ -281,32 -225,19 +281,32 @@@ static inline int parse_nal_units(AVCod
      if (!buf_size)
          return 0;
  
 +    buf_index     = 0;
 +    next_avc      = p->is_avc ? 0 : buf_size;
      for (;;) {
          const SPS *sps;
 -        int src_length, consumed;
 -        buf = avpriv_find_start_code(buf, buf_end, &state);
 -        if (buf >= buf_end)
 -            break;
 -        --buf;
 -        src_length = buf_end - buf;
 +        int src_length, consumed, nalsize = 0;
 +
 +        if (buf_index >= next_avc) {
 +            nalsize = get_avc_nalsize(p, buf, buf_size, &buf_index, avctx);
 +            if (nalsize < 0)
 +                break;
 +            next_avc = buf_index + nalsize;
 +        } else {
 +            buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
 +            if (buf_index >= buf_size)
 +                break;
 +            if (buf_index >= next_avc)
 +                continue;
 +        }
 +        src_length = next_avc - buf_index;
 +
 +        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)
          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);
 +            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;
              p->poc.prev_poc_msb          =
              p->poc.prev_poc_lsb          = 0;
          /* fall through */
-         case NAL_SLICE:
+         case H264_NAL_SLICE:
 -            get_ue_golomb(&nal.gb);  // skip first_mb_in_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];
              if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
                  }
              }
  
-             if (nal.type == NAL_IDR_SLICE)
+             if (nal.type == H264_NAL_IDR_SLICE)
 -                get_ue_golomb(&nal.gb); /* idr_pic_id */
 +                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);
  
@@@ -626,37 -534,31 +626,37 @@@ static int h264_parse(AVCodecParserCont
  static int h264_split(AVCodecContext *avctx,
                        const uint8_t *buf, int buf_size)
  {
 -    int i;
      uint32_t state = -1;
      int has_sps    = 0;
 +    int has_pps    = 0;
 +    const uint8_t *ptr = buf, *end = buf + buf_size;
 +    int nalu_type;
  
 -    for (i = 0; i <= buf_size; i++) {
 -        if ((state & 0xFFFFFF1F) == 0x107)
 +    while (ptr < end) {
 +        ptr = avpriv_find_start_code(ptr, end, &state);
 +        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)
 -        /*  if((state&0xFFFFFF1F) == 0x101 ||
 -         *     (state&0xFFFFFF1F) == 0x102 ||
 -         *     (state&0xFFFFFF1F) == 0x105) {
++        } 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 &&
 -        if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
 -            (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
 -            (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
 -            (state & 0xFFFFFF1F) != 0x10f) {
++        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 (i > 4 && buf[i - 5] == 0)
 -                    i--;
 -                return i - 4;
 +                while (ptr - 4 > buf && ptr[-5] == 0)
 +                    ptr--;
 +                return ptr - 4 - buf;
              }
          }
 -        if (i < buf_size)
 -            state = (state << 8) | buf[i];
      }
 +
      return 0;
  }
  
Simple merge
@@@ -1605,8 -1427,8 +1605,8 @@@ static int h264_slice_header_parse(cons
          sl->max_pic_num  = 1 << (sps->log2_max_frame_num + 1);
      }
  
-     if (nal->type == NAL_IDR_SLICE)
+     if (nal->type == H264_NAL_IDR_SLICE)
 -        get_ue_golomb(&sl->gb); /* idr_pic_id */
 +        get_ue_golomb_long(&sl->gb); /* idr_pic_id */
  
      if (sps->poc_type == 0) {
          sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
@@@ -1735,62 -1556,26 +1735,62 @@@ int ff_h264_decode_slice_header(H264Con
      if (ret < 0)
          return ret;
  
 -    if (!h->setup_finished) {
 -        if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
 -            if (h->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
 -                ff_h264_field_end(h, sl, 1);
 -            }
 +    if (sl->first_mb_addr == 0 || !h->current_slice) {
 +        if (h->setup_finished) {
 +            av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
 +            return AVERROR_INVALIDDATA;
 +        }
 +    }
  
 -            h->current_slice = 0;
 -            if (!h->first_field) {
 -                if (h->cur_pic_ptr && !h->droppable) {
 -                    ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
 -                                              h->picture_structure == PICT_BOTTOM_FIELD);
 +    if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
 +        if (h->current_slice) {
 +            if (h->max_contexts > 1) {
 +                if (!h->single_decode_warning) {
 +                    av_log(h->avctx, AV_LOG_WARNING, "Cannot decode multiple access units as slice threads\n");
 +                    h->single_decode_warning = 1;
                  }
-             } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type  == NAL_IDR_SLICE) {
 +                h->max_contexts = 1;
 +                return SLICE_SINGLETHREAD;
 +            }
 +
 +            if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) {
 +                ret = ff_h264_field_end(h, h->slice_ctx, 1);
 +                h->current_slice = 0;
 +                if (ret < 0)
 +                    return ret;
++            } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type  == H264_NAL_IDR_SLICE) {
 +                av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n");
 +                ret = ff_h264_field_end(h, h->slice_ctx, 1);
 +                h->current_slice = 0;
 +                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
 +                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
                  h->cur_pic_ptr = NULL;
 +                if (ret < 0)
 +                    return ret;
 +            } else
 +                return AVERROR_INVALIDDATA;
 +        }
 +
 +        if (!h->first_field) {
 +            if (h->cur_pic_ptr && !h->droppable) {
 +                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
 +                                          h->picture_structure == PICT_BOTTOM_FIELD);
              }
 +            h->cur_pic_ptr = NULL;
          }
 +    }
  
 -        if (h->current_slice == 0) {
 -            ret = h264_field_start(h, sl, nal);
 -            if (ret < 0)
 -                return ret;
 +    if (!h->current_slice)
 +        av_assert0(sl == h->slice_ctx);
 +
 +    if (h->current_slice == 0 && !h->first_field) {
 +        if (
 +            (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
 +            (h->avctx->skip_frame >= AVDISCARD_BIDIR  && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
 +            (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
-             (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
++            (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
 +            h->avctx->skip_frame >= AVDISCARD_ALL) {
 +            return SLICE_SKIPED;
          }
      }
  
  
      if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
          (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
-          h->nal_unit_type != NAL_IDR_SLICE) ||
++         h->nal_unit_type != H264_NAL_IDR_SLICE) ||
 +        (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
           sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
          (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR  &&
           sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
@@@ -674,22 -639,16 +674,22 @@@ static int get_last_needed_nal(H264Cont
           * which splits NALs strangely if so, when frame threading we
           * can't start the next thread until we've read all of them */
          switch (nal->type) {
-         case NAL_SPS:
-         case NAL_PPS:
+         case H264_NAL_SPS:
+         case H264_NAL_PPS:
              nals_needed = i;
              break;
-         case NAL_DPA:
-         case NAL_IDR_SLICE:
-         case NAL_SLICE:
+         case H264_NAL_DPA:
+         case H264_NAL_IDR_SLICE:
+         case H264_NAL_SLICE:
 -            init_get_bits(&gb, nal->data + 1, (nal->size - 1) * 8);
 -            if (!get_ue_golomb(&gb))
 +            ret = init_get_bits8(&gb, nal->data + 1, (nal->size - 1));
 +            if (ret < 0)
 +                return ret;
 +            if (!get_ue_golomb_long(&gb) ||  // first_mb_in_slice
 +                !first_slice ||
 +                first_slice != nal->type)
                  nals_needed = i;
 +            if (!first_slice)
 +                first_slice = nal->type;
          }
      }
  
@@@ -768,76 -686,46 +768,76 @@@ static int decode_nal_units(H264Contex
          int err;
  
          if (avctx->skip_frame >= AVDISCARD_NONREF &&
-             nal->ref_idc == 0 && nal->type != NAL_SEI)
+             nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
              continue;
  
 +again:
          // FIXME these should stop being context-global variables
          h->nal_ref_idc   = nal->ref_idc;
          h->nal_unit_type = nal->type;
  
          err = 0;
          switch (nal->type) {
-         case NAL_IDR_SLICE:
+         case H264_NAL_IDR_SLICE:
 +            if ((nal->data[1] & 0xFC) == 0x98) {
 +                av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
 +                h->next_outputed_poc = INT_MIN;
 +                ret = -1;
 +                goto end;
 +            }
-             if (nal->type != NAL_IDR_SLICE) {
+             if (nal->type != H264_NAL_IDR_SLICE) {
                  av_log(h->avctx, AV_LOG_ERROR,
                         "Invalid mix of idr and non-idr slices\n");
                  ret = -1;
                  goto end;
              }
 -            idr(h); // FIXME ensure we don't lose some frames if there is reordering
 +            if(!idr_cleared) {
 +                if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
 +                    av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
 +                    ret = AVERROR_INVALIDDATA;
 +                    goto end;
 +                }
 +                idr(h); // FIXME ensure we don't lose some frames if there is reordering
 +            }
 +            idr_cleared = 1;
 +            h->has_recovery_point = 1;
-         case NAL_SLICE:
+         case H264_NAL_SLICE:
              sl->gb = nal->gb;
  
              if ((err = ff_h264_decode_slice_header(h, sl, nal)))
                  break;
  
 -            if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
 -                h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &
 -                                    ((1 << h->ps.sps->log2_max_frame_num) - 1);
 +            if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
 +                const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
 +
 +                if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
 +                    h->valid_recovery_point = 1;
 +
 +                if (   h->recovery_frame < 0
 +                    || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
 +                    h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
 +
 +                    if (!h->valid_recovery_point)
 +                        h->recovery_frame = h->poc.frame_num;
 +                }
              }
  
-             h->cur_pic_ptr->f->key_frame |= (nal->type == NAL_IDR_SLICE);
 -            h->cur_pic_ptr->f->key_frame |=
 -                (nal->type == H264_NAL_IDR_SLICE) || (h->sei.recovery_point.recovery_frame_cnt >= 0);
++            h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);
  
-             if (nal->type == NAL_IDR_SLICE ||
 -            if (nal->type == H264_NAL_IDR_SLICE || h->recovery_frame == h->poc.frame_num) {
++            if (nal->type == H264_NAL_IDR_SLICE ||
 +                (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {
                  h->recovery_frame         = -1;
                  h->cur_pic_ptr->recovered = 1;
              }
              // If we have an IDR, all frames after it in decoded order are
              // "recovered".
-             if (nal->type == NAL_IDR_SLICE)
+             if (nal->type == H264_NAL_IDR_SLICE)
                  h->frame_recovered |= FRAME_RECOVERED_IDR;
 +#if 1
 +            h->cur_pic_ptr->recovered |= h->frame_recovered;
 +#else
              h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
 +#endif
  
              if (h->current_slice == 1) {
                  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))
                      context_count++;
              }
              break;
-         case NAL_DPA:
-         case NAL_DPB:
-         case NAL_DPC:
+         case H264_NAL_DPA:
+         case H264_NAL_DPB:
+         case H264_NAL_DPC:
              avpriv_request_sample(avctx, "data partitioning");
 -            ret = AVERROR(ENOSYS);
 -            goto end;
              break;
-         case NAL_SEI:
+         case H264_NAL_SEI:
              ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
 +            h->has_recovery_point = h->has_recovery_point || h->sei.recovery_point.recovery_frame_cnt != -1;
 +            if (avctx->debug & FF_DEBUG_GREEN_MD)
 +                debug_green_metadata(&h->sei.green_metadata, h->avctx);
 +#if FF_API_AFD
 +FF_DISABLE_DEPRECATION_WARNINGS
 +            h->avctx->dtg_active_format = h->sei.afd.active_format_description;
 +FF_ENABLE_DEPRECATION_WARNINGS
 +#endif /* FF_API_AFD */
              if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
                  goto end;
              break;
-         case NAL_SPS: {
 -        case H264_NAL_SPS:
 -            ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);
 -            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
 -                goto end;
++        case H264_NAL_SPS: {
 +            GetBitContext tmp_gb = nal->gb;
 +            if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
 +                break;
 +            av_log(h->avctx, AV_LOG_DEBUG,
 +                   "SPS decoding failure, trying again with the complete NAL\n");
 +            init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
 +            if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
 +                break;
 +            ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
              break;
-         case NAL_PPS:
 +        }
+         case H264_NAL_PPS:
              ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
                                                         nal->size_bits);
              if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
@@@ -1157,8 -912,8 +1157,8 @@@ static int h264_decode_frame(AVCodecCon
      if (buf_index < 0)
          return AVERROR_INVALIDDATA;
  
-     if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
+     if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
 -        buf_size = 0;
 +        av_assert0(buf_index <= buf_size);
          goto out;
      }
  
Simple merge
Simple merge
@@@ -1656,255 -1280,6 +1656,255 @@@ static int mxf_write_partition(AVFormat
      return 0;
  }
  
-         case NAL_SPS:
 +static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st,
 +AVPacket *pkt)
 +{
 +    MXFContext *mxf = s->priv_data;
 +    MXFStreamContext *sc = st->priv_data;
 +    int i, cid;
 +    uint8_t* header_cid;
 +    int frame_size = 0;
 +
 +    if (mxf->header_written)
 +        return 1;
 +
 +    if (pkt->size < 43)
 +        return -1;
 +
 +    header_cid = pkt->data + 0x28;
 +    cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
 +
 +    if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0)
 +        return -1;
 +    if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
 +        return AVERROR_INVALIDDATA;
 +
 +    switch (cid) {
 +    case 1235:
 +        sc->index = 24;
 +        sc->component_depth = 10;
 +        break;
 +    case 1237:
 +        sc->index = 25;
 +        break;
 +    case 1238:
 +        sc->index = 26;
 +        break;
 +    case 1241:
 +        sc->index = 27;
 +        sc->component_depth = 10;
 +        break;
 +    case 1242:
 +        sc->index = 28;
 +        break;
 +    case 1243:
 +        sc->index = 29;
 +        break;
 +    case 1250:
 +        sc->index = 30;
 +        sc->component_depth = 10;
 +        break;
 +    case 1251:
 +        sc->index = 31;
 +        break;
 +    case 1252:
 +        sc->index = 32;
 +        break;
 +    case 1253:
 +        sc->index = 33;
 +        break;
 +    default:
 +        return -1;
 +    }
 +
 +    sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
 +    sc->aspect_ratio = (AVRational){ 16, 9 };
 +
 +    if(s->oformat == &ff_mxf_opatom_muxer){
 +        mxf->edit_unit_byte_count = frame_size;
 +        return 1;
 +    }
 +
 +    mxf->edit_unit_byte_count = KAG_SIZE;
 +    for (i = 0; i < s->nb_streams; i++) {
 +        AVStream *st = s->streams[i];
 +        MXFStreamContext *sc = st->priv_data;
 +        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
 +            mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
 +            mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
 +        } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
 +            mxf->edit_unit_byte_count += 16 + 4 + frame_size;
 +            mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
 +        }
 +    }
 +
 +    return 1;
 +}
 +
 +static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 +{
 +    MXFContext *mxf = s->priv_data;
 +    MXFStreamContext *sc = st->priv_data;
 +    uint8_t *vs_pack, *vsc_pack;
 +    int i, ul_index, frame_size, stype, pal;
 +
 +    if (mxf->header_written)
 +        return 1;
 +
 +    // Check for minimal frame size
 +    if (pkt->size < 120000)
 +        return -1;
 +
 +    vs_pack  = pkt->data + 80*5 + 48;
 +    vsc_pack = pkt->data + 80*5 + 53;
 +    stype    = vs_pack[3] & 0x1f;
 +    pal      = (vs_pack[3] >> 5) & 0x1;
 +
 +    if ((vs_pack[2] & 0x07) == 0x02)
 +        sc->aspect_ratio = (AVRational){ 16, 9 };
 +    else
 +        sc->aspect_ratio = (AVRational){ 4, 3 };
 +
 +    sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
 +    // TODO: fix dv encoder to set proper FF/FS value in VSC pack
 +    // and set field dominance accordingly
 +    // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
 +
 +    switch (stype) {
 +    case 0x18: // DV100 720p
 +        ul_index = 6 + pal;
 +        frame_size = pal ? 288000 : 240000;
 +        if (sc->interlaced) {
 +            av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
 +            sc->interlaced = 0;
 +        }
 +        break;
 +    case 0x14: // DV100 1080i
 +        ul_index = 4 + pal;
 +        frame_size = pal ? 576000 : 480000;
 +        break;
 +    case 0x04: // DV50
 +        ul_index = 2 + pal;
 +        frame_size = pal ? 288000 : 240000;
 +        break;
 +    default: // DV25
 +        ul_index = 0 + pal;
 +        frame_size = pal ? 144000 : 120000;
 +    }
 +
 +    sc->index = ul_index + 16;
 +    sc->codec_ul =  &mxf_essence_container_uls[sc->index].codec_ul;
 +
 +    if(s->oformat == &ff_mxf_opatom_muxer) {
 +        mxf->edit_unit_byte_count = frame_size;
 +        return 1;
 +    }
 +
 +    mxf->edit_unit_byte_count = KAG_SIZE;
 +    for (i = 0; i < s->nb_streams; i++) {
 +        AVStream *st = s->streams[i];
 +        MXFStreamContext *sc = st->priv_data;
 +        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
 +            mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
 +            mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
 +        } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
 +            mxf->edit_unit_byte_count += 16 + 4 + frame_size;
 +            mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
 +        }
 +    }
 +
 +    return 1;
 +}
 +
 +static const struct {
 +    UID uid;
 +    int frame_size;
 +    int profile;
 +    uint8_t interlaced;
 +} mxf_h264_codec_uls[] = {
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 },      0, 110, 0 }, // AVC High 10 Intra
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960,   0, 1 }, // AVC Intra 50 1080i60
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088,   0, 1 }, // AVC Intra 50 1080i50
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960,   0, 0 }, // AVC Intra 50 1080p30
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088,   0, 0 }, // AVC Intra 50 1080p25
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736,   0, 0 }, // AVC Intra 50 720p60
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800,   0, 0 }, // AVC Intra 50 720p50
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 },      0, 122, 0 }, // AVC High 422 Intra
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576,   0, 1 }, // AVC Intra 100 1080i60
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832,   0, 1 }, // AVC Intra 100 1080i50
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576,   0, 0 }, // AVC Intra 100 1080p30
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832,   0, 0 }, // AVC Intra 100 1080p25
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544,   0, 0 }, // AVC Intra 100 720p60
 +    {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672,   0, 0 }, // AVC Intra 100 720p50
 +};
 +
 +static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
 +                                AVPacket *pkt, MXFIndexEntry *e)
 +{
 +    MXFContext *mxf = s->priv_data;
 +    MXFStreamContext *sc = st->priv_data;
 +    static const int mxf_h264_num_codec_uls = sizeof(mxf_h264_codec_uls) / sizeof(mxf_h264_codec_uls[0]);
 +    const uint8_t *buf = pkt->data;
 +    const uint8_t *buf_end = pkt->data + pkt->size;
 +    uint32_t state = -1;
 +    int extra_size = 512; // support AVC Intra files without SPS/PPS header
 +    int i, frame_size;
 +    uint8_t uid_found;
 +
 +    if (pkt->size > extra_size)
 +        buf_end -= pkt->size - extra_size; // no need to parse beyond SPS/PPS header
 +
 +    for (;;) {
 +        buf = avpriv_find_start_code(buf, buf_end, &state);
 +        if (buf >= buf_end)
 +            break;
 +        --buf;
 +        switch (state & 0x1f) {
-         case NAL_PPS:
++        case H264_NAL_SPS:
 +            st->codecpar->profile = buf[1];
 +            e->flags |= 0x40;
 +            break;
++        case H264_NAL_PPS:
 +            if (e->flags & 0x40) { // sequence header present
 +                e->flags |= 0x80; // random access
 +                extra_size = 0;
 +                buf = buf_end;
 +            }
 +            break;
 +        default:
 +            break;
 +        }
 +    }
 +
 +    if (mxf->header_written)
 +        return 1;
 +
 +    sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
 +    sc->component_depth = 10; // AVC Intra is always 10 Bit
 +    sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
 +    if (sc->interlaced)
 +        sc->field_dominance = 1; // top field first is mandatory for AVC Intra
 +
 +    uid_found = 0;
 +    frame_size = pkt->size + extra_size;
 +    for (i = 0; i < mxf_h264_num_codec_uls; i++) {
 +        if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
 +            sc->codec_ul = &mxf_h264_codec_uls[i].uid;
 +            return 1;
 +        } else if (st->codecpar->profile == mxf_h264_codec_uls[i].profile) {
 +            sc->codec_ul = &mxf_h264_codec_uls[i].uid;
 +            uid_found = 1;
 +        }
 +    }
 +
 +    if (!uid_found) {
 +        av_log(s, AV_LOG_ERROR, "AVC Intra 50/100 supported only\n");
 +        return 0;
 +    }
 +
 +    return 1;
 +}
 +
  static const UID mxf_mpeg2_codec_uls[] = {
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP