[FFmpeg-cvslog] yuv4mpeg: add rough duration estimate and seeking.

Ronald S. Bultje git at videolan.org
Sat Jul 11 14:47:06 CEST 2015


ffmpeg | branch: master | Ronald S. Bultje <rsbultje at gmail.com> | Fri Jul 10 21:15:35 2015 -0400| [733c5d889b9887babbae85a5435e8f0d39473c51] | committer: Michael Niedermayer

yuv4mpeg: add rough duration estimate and seeking.

Reviewed-by: Paul B Mahol <onemda at gmail.com>
Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=733c5d889b9887babbae85a5435e8f0d39473c51
---

 libavformat/yuv4mpeg.h       |    1 +
 libavformat/yuv4mpegdec.c    |   26 ++++++++++----
 tests/ref/seek/lavf-yuv4mpeg |   78 ++++++++++++++++++++++++++++--------------
 3 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/libavformat/yuv4mpeg.h b/libavformat/yuv4mpeg.h
index 750f498..eba7337 100644
--- a/libavformat/yuv4mpeg.h
+++ b/libavformat/yuv4mpeg.h
@@ -23,5 +23,6 @@
 
 #define Y4M_MAGIC "YUV4MPEG2"
 #define Y4M_FRAME_MAGIC "FRAME"
+#define Y4M_FRAME_MAGIC_LEN 6
 
 #endif /* AVFORMAT_YUV4MPEG_H */
diff --git a/libavformat/yuv4mpegdec.c b/libavformat/yuv4mpegdec.c
index 7613c3c..4ebdb78 100644
--- a/libavformat/yuv4mpegdec.c
+++ b/libavformat/yuv4mpegdec.c
@@ -256,6 +256,12 @@ static int yuv4_read_header(AVFormatContext *s)
     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
     st->codec->chroma_sample_location = chroma_sample_location;
     st->codec->field_order            = field_order;
+    s->packet_size = avpicture_get_size(st->codec->pix_fmt, width, height) + Y4M_FRAME_MAGIC_LEN;
+    if ((int) s->packet_size < 0)
+        return s->packet_size;
+    s->internal->data_offset = avio_tell(pb);
+
+    st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
 
     return 0;
 }
@@ -264,7 +270,7 @@ static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int i;
     char header[MAX_FRAME_HEADER+1];
-    int packet_size, width, height, ret;
+    int width, height, ret, off = avio_tell(s->pb);
     AVStream *st = s->streams[0];
 
     for (i = 0; i < MAX_FRAME_HEADER; i++) {
@@ -287,17 +293,22 @@ static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
     width  = st->codec->width;
     height = st->codec->height;
 
-    packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
-    if (packet_size < 0)
-        return packet_size;
-
-    ret = av_get_packet(s->pb, pkt, packet_size);
+    ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
     if (ret < 0)
         return ret;
-    else if (ret != packet_size)
+    else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN)
         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
 
     pkt->stream_index = 0;
+    pkt->pts = (off - s->internal->data_offset) / s->packet_size;
+    pkt->duration = 1;
+    return 0;
+}
+
+static int yuv4_read_seek(AVFormatContext *s, int stream_index,
+                          int64_t pts, int flags)
+{
+    avio_seek(s->pb, pts * s->packet_size + s->internal->data_offset, SEEK_SET);
     return 0;
 }
 
@@ -316,5 +327,6 @@ AVInputFormat ff_yuv4mpegpipe_demuxer = {
     .read_probe     = yuv4_probe,
     .read_header    = yuv4_read_header,
     .read_packet    = yuv4_read_packet,
+    .read_seek      = yuv4_read_seek,
     .extensions     = "y4m",
 };
diff --git a/tests/ref/seek/lavf-yuv4mpeg b/tests/ref/seek/lavf-yuv4mpeg
index 81c1de9..60c3036 100644
--- a/tests/ref/seek/lavf-yuv4mpeg
+++ b/tests/ref/seek/lavf-yuv4mpeg
@@ -1,27 +1,53 @@
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:     64 size:152064
-ret:-1         st:-1 flags:0  ts:-1.000000
-ret:-1         st:-1 flags:1  ts: 1.894167
-ret:-1         st: 0 flags:0  ts: 0.800000
-ret:-1         st: 0 flags:1  ts:-0.320000
-ret:-1         st:-1 flags:0  ts: 2.576668
-ret:-1         st:-1 flags:1  ts: 1.470835
-ret:-1         st: 0 flags:0  ts: 0.360000
-ret:-1         st: 0 flags:1  ts:-0.760000
-ret:-1         st:-1 flags:0  ts: 2.153336
-ret:-1         st:-1 flags:1  ts: 1.047503
-ret:-1         st: 0 flags:0  ts:-0.040000
-ret:-1         st: 0 flags:1  ts: 2.840000
-ret:-1         st:-1 flags:0  ts: 1.730004
-ret:-1         st:-1 flags:1  ts: 0.624171
-ret:-1         st: 0 flags:0  ts:-0.480000
-ret:-1         st: 0 flags:1  ts: 2.400000
-ret:-1         st:-1 flags:0  ts: 1.306672
-ret:-1         st:-1 flags:1  ts: 0.200839
-ret:-1         st: 0 flags:0  ts:-0.920000
-ret:-1         st: 0 flags:1  ts: 2.000000
-ret:-1         st:-1 flags:0  ts: 0.883340
-ret:-1         st:-1 flags:1  ts:-0.222493
-ret:-1         st: 0 flags:0  ts: 2.680000
-ret:-1         st: 0 flags:1  ts: 1.560000
-ret:-1         st:-1 flags:0  ts: 0.460008
-ret:-1         st:-1 flags:1  ts:-0.645825
+ret: 0         st:-1 flags:0  ts:-1.000000
+ret: 0         st: 0 flags:1 dts: 0.040000 pts: 0.040000 pos: 152134 size:152064
+ret: 0         st:-1 flags:1  ts: 1.894167
+ret:-EOF
+ret: 0         st: 0 flags:0  ts: 0.800000
+ret: 0         st: 0 flags:1 dts: 0.800000 pts: 0.800000 pos:3041464 size:152064
+ret: 0         st: 0 flags:1  ts:-0.320000
+ret: 0         st: 0 flags:1 dts: 0.840000 pts: 0.840000 pos:3193534 size:152064
+ret: 0         st:-1 flags:0  ts: 2.576668
+ret:-EOF
+ret: 0         st:-1 flags:1  ts: 1.470835
+ret:-EOF
+ret: 0         st: 0 flags:0  ts: 0.360000
+ret: 0         st: 0 flags:1 dts: 0.360000 pts: 0.360000 pos:1368694 size:152064
+ret: 0         st: 0 flags:1  ts:-0.760000
+ret: 0         st: 0 flags:1 dts: 0.400000 pts: 0.400000 pos:1520764 size:152064
+ret: 0         st:-1 flags:0  ts: 2.153336
+ret:-EOF
+ret: 0         st:-1 flags:1  ts: 1.047503
+ret:-EOF
+ret: 0         st: 0 flags:0  ts:-0.040000
+ret:-EOF
+ret: 0         st: 0 flags:1  ts: 2.840000
+ret:-EOF
+ret: 0         st:-1 flags:0  ts: 1.730004
+ret:-EOF
+ret: 0         st:-1 flags:1  ts: 0.624171
+ret: 0         st: 0 flags:1 dts: 0.640000 pts: 0.640000 pos:2433184 size:152064
+ret: 0         st: 0 flags:0  ts:-0.480000
+ret: 0         st: 0 flags:1 dts: 0.680000 pts: 0.680000 pos:2585254 size:152064
+ret: 0         st: 0 flags:1  ts: 2.400000
+ret:-EOF
+ret: 0         st:-1 flags:0  ts: 1.306672
+ret:-EOF
+ret: 0         st:-1 flags:1  ts: 0.200839
+ret: 0         st: 0 flags:1 dts: 0.200000 pts: 0.200000 pos: 760414 size:152064
+ret: 0         st: 0 flags:0  ts:-0.920000
+ret: 0         st: 0 flags:1 dts: 0.240000 pts: 0.240000 pos: 912484 size:152064
+ret: 0         st: 0 flags:1  ts: 2.000000
+ret:-EOF
+ret: 0         st:-1 flags:0  ts: 0.883340
+ret: 0         st: 0 flags:1 dts: 0.880000 pts: 0.880000 pos:3345604 size:152064
+ret: 0         st:-1 flags:1  ts:-0.222493
+ret: 0         st: 0 flags:1 dts: 0.920000 pts: 0.920000 pos:3497674 size:152064
+ret: 0         st: 0 flags:0  ts: 2.680000
+ret:-EOF
+ret: 0         st: 0 flags:1  ts: 1.560000
+ret:-EOF
+ret: 0         st:-1 flags:0  ts: 0.460008
+ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos:1824904 size:152064
+ret: 0         st:-1 flags:1  ts:-0.645825
+ret: 0         st: 0 flags:1 dts: 0.520000 pts: 0.520000 pos:1976974 size:152064



More information about the ffmpeg-cvslog mailing list