Merge commit 'd584533cf38141172e20bae5436629ee17c8ce50'
authorHendrik Leppkes <h.leppkes@gmail.com>
Thu, 29 Oct 2015 13:06:11 +0000 (14:06 +0100)
committerHendrik Leppkes <h.leppkes@gmail.com>
Thu, 29 Oct 2015 13:06:11 +0000 (14:06 +0100)
* commit 'd584533cf38141172e20bae5436629ee17c8ce50':
  avformat: Rework add_to_pktbuf

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
1  2 
libavformat/utils.c

@@@ -357,16 -205,27 +357,27 @@@ static int init_input(AVFormatContext *
          return ret;
      if (s->iformat)
          return 0;
 -    return av_probe_input_buffer(s->pb, &s->iformat, filename,
 -                                 s, 0, s->probesize);
 +    return av_probe_input_buffer2(s->pb, &s->iformat, filename,
 +                                 s, 0, s->format_probesize);
  }
  
- static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
-                                AVPacketList **plast_pktl)
+ static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
+                          AVPacketList **plast_pktl, int ref)
  {
      AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
+     int ret;
      if (!pktl)
-         return NULL;
+         return AVERROR(ENOMEM);
+     if (ref) {
+         if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
+             av_free(pktl);
+             return ret;
+         }
+     } else {
+         pktl->pkt = *pkt;
+     }
  
      if (*packet_buffer)
          (*plast_pktl)->next = pktl;
  
      /* Add the packet in the buffered packet list. */
      *plast_pktl = pktl;
-     pktl->pkt   = *pkt;
-     return &pktl->pkt;
+     return 0;
  }
  
 -static int queue_attached_pictures(AVFormatContext *s)
 +int avformat_queue_attached_pictures(AVFormatContext *s)
  {
-     int i;
+     int i, ret;
      for (i = 0; i < s->nb_streams; i++)
          if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
              s->streams[i]->discard < AVDISCARD_ALL) {
-             AVPacket copy = s->streams[i]->attached_pic;
-             if (copy.size <= 0) {
++            if (s->streams[i]->attached_pic.size <= 0) {
 +                av_log(s, AV_LOG_WARNING,
 +                    "Attached picture on stream %d has invalid size, "
 +                    "ignoring\n", i);
 +                continue;
 +            }
-             copy.buf = av_buffer_ref(copy.buf);
-             if (!copy.buf)
-                 return AVERROR(ENOMEM);
  
-             add_to_pktbuf(&s->internal->raw_packet_buffer, &copy,
-                           &s->internal->raw_packet_buffer_end);
+             ret = add_to_pktbuf(&s->internal->raw_packet_buffer,
+                                 &s->streams[i]->attached_pic,
+                                 &s->internal->raw_packet_buffer_end, 1);
+             if (ret < 0)
+                 return ret;
          }
      return 0;
  }
@@@ -700,37 -439,31 +709,39 @@@ int ff_read_packet(AVFormatContext *s, 
              continue;
          }
  
 +        if (pkt->stream_index >= (unsigned)s->nb_streams) {
 +            av_log(s, AV_LOG_ERROR, "Invalid stream index %d\n", pkt->stream_index);
 +            continue;
 +        }
 +
          st = s->streams[pkt->stream_index];
  
 -        switch (st->codec->codec_type) {
 -        case AVMEDIA_TYPE_VIDEO:
 -            if (s->video_codec_id)
 -                st->codec->codec_id = s->video_codec_id;
 -            break;
 -        case AVMEDIA_TYPE_AUDIO:
 -            if (s->audio_codec_id)
 -                st->codec->codec_id = s->audio_codec_id;
 -            break;
 -        case AVMEDIA_TYPE_SUBTITLE:
 -            if (s->subtitle_codec_id)
 -                st->codec->codec_id = s->subtitle_codec_id;
 -            break;
 +        if (update_wrap_reference(s, st, pkt->stream_index, pkt) && st->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
 +            // correct first time stamps to negative values
 +            if (!is_relative(st->first_dts))
 +                st->first_dts = wrap_timestamp(st, st->first_dts);
 +            if (!is_relative(st->start_time))
 +                st->start_time = wrap_timestamp(st, st->start_time);
 +            if (!is_relative(st->cur_dts))
 +                st->cur_dts = wrap_timestamp(st, st->cur_dts);
          }
  
 -        if (!pktl && (st->codec->codec_id != AV_CODEC_ID_PROBE ||
 -                      !st->probe_packets))
 +        pkt->dts = wrap_timestamp(st, pkt->dts);
 +        pkt->pts = wrap_timestamp(st, pkt->pts);
 +
 +        force_codec_ids(s, st);
 +
 +        /* TODO: audio: time filter; video: frame reordering (pts != dts) */
 +        if (s->use_wallclock_as_timestamps)
 +            pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
 +
 +        if (!pktl && st->request_probe <= 0)
              return ret;
  
-         add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
-                       &s->internal->raw_packet_buffer_end);
+         err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
+                             &s->internal->raw_packet_buffer_end, 0);
+         if (err)
+             return err;
          s->internal->raw_packet_buffer_remaining_size -= pkt->size;
  
          if ((err = probe_codec(s, st, pkt)) < 0)
@@@ -1269,21 -839,19 +1280,15 @@@ static int parse_packet(AVFormatContex
               st->parser->pict_type == AV_PICTURE_TYPE_I))
              out_pkt.flags |= AV_PKT_FLAG_KEY;
  
 -        compute_pkt_fields(s, st, st->parser, &out_pkt);
 +        if (st->parser->key_frame == -1 && st->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
 +            out_pkt.flags |= AV_PKT_FLAG_KEY;
  
 -        if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
 -            out_pkt.flags & AV_PKT_FLAG_KEY) {
 -            ff_reduce_index(s, st->index);
 -            av_add_index_entry(st, st->parser->frame_offset, out_pkt.dts,
 -                               0, 0, AVINDEX_KEYFRAME);
 -        }
 +        compute_pkt_fields(s, st, st->parser, &out_pkt, next_dts, next_pts);
  
-         if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
-             out_pkt.buf = pkt->buf;
-             pkt->buf    = NULL;
-         }
-         if ((ret = av_dup_packet(&out_pkt)) < 0)
-             goto fail;
-         if (!add_to_pktbuf(&s->internal->parse_queue, &out_pkt, &s->internal->parse_queue_end)) {
+         if ((ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
+                                  &s->internal->parse_queue_end,
+                                  1))) {
              av_packet_unref(&out_pkt);
-             ret = AVERROR(ENOMEM);
              goto fail;
          }
      }
@@@ -1553,25 -1027,11 +1558,26 @@@ int av_read_frame(AVFormatContext *s, A
                  return ret;
          }
  
-         if (av_dup_packet(add_to_pktbuf(&s->internal->packet_buffer, pkt,
-                                         &s->internal->packet_buffer_end)) < 0)
-             return AVERROR(ENOMEM);
+         ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
+                             &s->internal->packet_buffer_end, 1);
+         if (ret < 0)
+             return ret;
      }
 +
 +return_packet:
 +
 +    st = s->streams[pkt->stream_index];
 +    if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
 +        ff_reduce_index(s, st->index);
 +        av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
 +    }
 +
 +    if (is_relative(pkt->dts))
 +        pkt->dts -= RELATIVE_TS_BASE;
 +    if (is_relative(pkt->pts))
 +        pkt->pts -= RELATIVE_TS_BASE;
 +
 +    return ret;
  }
  
  /* XXX: suppress the packet queue */