Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the futur

Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code

/* check that the timestamps are set */

if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {

av_log(s, AV_LOG_ERROR,"Timestamps are unset in a packet for stream %d\n", st->index);

return AVERROR(EINVAL);

}

 

​
//FIXME merge with compute_pkt_fields
 static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 {
  int delay = FFMAX(st->codecpar->video_delay, st->internal->avctx->max_b_frames > 0);
  int num, den, i;
  int frame_size;
 
  if (!s->internal->missing_ts_warning &&
  !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC) || (st->disposition & AV_DISPOSITION_TIMED_THUMBNAILS)) &&
  (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
  av_log(s, AV_LOG_WARNING,
  "Timestamps are unset in a packet for stream %d. "
  "This is deprecated and will stop working in the future. "
  "Fix your code to set the timestamps properly\n", st->index);
  s->internal->missing_ts_warning = 1;
  }
 
  if (s->debug & FF_FDEBUG_TS)
  av_log(s, AV_LOG_TRACE, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
  av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
 
  if (pkt->duration < 0 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
  pkt->duration, pkt->stream_index);
  pkt->duration = 0;
  }
 
  /* duration field */
  if (pkt->duration == 0) {
  ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
  if (den && num) {
  pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  }
  }
 
  if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  pkt->pts = pkt->dts;
 
  //XXX/FIXME this is a temporary hack until all encoders output pts
  if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
  static int warned;
  if (!warned) {
  av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
  warned = 1;
  }
  pkt->dts =
 // pkt->pts= st->cur_dts;
  pkt->pts = st->internal->priv_pts->val;
  }
 
  //calculate dts from pts
  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  st->pts_buffer[0] = pkt->pts;
  for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
 
  pkt->dts = st->pts_buffer[0];
  }
 
  if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
  st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
  st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  av_log(s, AV_LOG_ERROR,
  "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
  st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
  return AVERROR(EINVAL);
  }
  if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  av_log(s, AV_LOG_ERROR,
  "pts (%s) < dts (%s) in stream %d\n",
  av_ts2str(pkt->pts), av_ts2str(pkt->dts),
  st->index);
  return AVERROR(EINVAL);
  }
 
  if (s->debug & FF_FDEBUG_TS)
  av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%s dts2:%s\n",
  av_ts2str(pkt->pts), av_ts2str(pkt->dts));
 
  st->cur_dts = pkt->dts;
  st->internal->priv_pts->val = pkt->dts;
 
  /* update pts */
  switch (st->codecpar->codec_type) {
  case AVMEDIA_TYPE_AUDIO:
  frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
  ((AVFrame *)pkt->data)->nb_samples :
  av_get_audio_frame_duration(st->codec, pkt->size);
 
  /* HACK/FIXME, we skip the initial 0 size packets as they are most
  * likely equal to the encoder delay, but it would be better if we
  * had the real timestamps from the encoder */
  if (frame_size >= 0 && (pkt->size || st->internal->priv_pts->num != st->internal->priv_pts->den >> 1 || st->internal->priv_pts->val)) {
  frac_add(st->internal->priv_pts, (int64_t)st->time_base.den * frame_size);
  }
  break;
  case AVMEDIA_TYPE_VIDEO:
  frac_add(st->internal->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
  break;
  }
  return 0;
 }
​

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Hardware Video Encoding on iPhone — RTSP Server example On iOS, the only way to use hardware acceleration when encoding video is to use AVAssetWriter, and that means writing the compressed video to file. If you want to stream that video over the network, for example, it needs to be read back out of the file. I’ve written an example application that demonstrates how to do this, as part of an RTSP server that streams H264 video from the iPhone or iPad camera to remote clients. The end-to-end latency, measured using a low-latency DirectShow client, is under a second. Latency with VLC and QuickTime playback is a few seconds, since these clients buffer somewhat more data at the client side. The whole example app is available in source form here under an attribution license. It’s a very basic app, but is fully functional. Build and run the app on an iPhone or iPad, then use Quicktime Player or VLC to play back the URL that is displayed in the app. Details, Details When the compressed video data is written to a MOV or MP4 file, it is written to an mdat atom and indexed in the moov atom. However, the moov atom is not written out until the file is closed, and without that index, the data in mdat is not easily accessible. There are no boundary markers or sub-atoms, just raw elementary stream. Moreover, the data in the mdat cannot be extracted or used without the data from the moov atom (specifically the lengthSize and SPS and PPS param sets). My example code takes the following approach to this problem: Only video is written using the AVAssetWriter instance, or it would be impossible to distinguish video from audio in the mdat atom. Initially, I create two AVAssetWriter instances. The first frame is written to both, and then one instance is closed. Once the moov atom has been written to that file, I parse the file and assume that the parameters apply to both instances, since the initial conditions were the same. Once I have the parameters, I use a dispatch_source object to trigger reads from the file whenever new data is written. The body of the mdat chunk consists of H264 NALUs, each preceded by a length field. Although the length of the mdat chunk is not known, we can safely assume that it will continue to the end of the file (until we finish the output file and the moov is added). For RTP delivery of the data, we group the NALUs into frames by parsing the NALU headers. Since there are no AUDs marking the frame boundaries, this requires looking at several different elements of the NALU header. Timestamps arrive with the uncompressed frames from the camera and are stored in a FIFO. These timestamps are applied to the compressed frames in the same order. Fortunately, the AVAssetWriter live encoder does not require re-ordering of frames. When the file gets too large, a new instance of AVAssetWriter is used, so that the old temporary file can be deleted. Transition code must then wait for the old instance to be closed so that the remaining NALUs can be read from the mdat atom without reading past the end of that atom into the subsequent metadata. Finally, the new file is opened and timestamps are adjusted. The resulting compressed output is seamless. A little experimentation suggests that we are able to read compressed frames from file about 500ms or so after they are captured, and these frames then arrive around 200ms after that at the client app. Rotation For modern graphics hardware, it is very straightforward to rotate an image when displaying it, and this is the method used by AVFoundation to handle rotation of the camera. The buffers are captured, encoded and written to file in landscape orientation. If the device is rotated to portrait mode, a transform matrix is written out to the file to indicate that the video should be rotated for playback. At the same time, the preview layer is also rotated to match the device orientation. This is efficient and works in most cases. However, there isn’t a way to pass this transform matrix to an RTP client, so the view on a remote player will not match the preview on the device if it is rotated away from the base camera orientation. The solution is to rotate the pixel buffers after receiving them from the capture output and before delivering them to the encoder. There is a cost to this processing, and this example code does not include this extra step.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI视觉网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值