changeset 306:e7a8d4dd8e14 libavformat

seek support for PCM formats
author bellard
date Mon, 10 Nov 2003 18:41:45 +0000
parents ef53bff8bf23
children 5552d3761ec0
files au.c raw.c
diffstat 2 files changed, 58 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/au.c	Mon Nov 10 18:40:14 2003 +0000
+++ b/au.c	Mon Nov 10 18:41:45 2003 +0000
@@ -114,7 +114,7 @@
 
 /* au input */
 static int au_read_header(AVFormatContext *s,
-                           AVFormatParameters *ap)
+                          AVFormatParameters *ap)
 {
     int size;
     unsigned int tag;
@@ -187,6 +187,7 @@
     au_read_header,
     au_read_packet,
     au_read_close,
+    pcm_read_seek,
 };
 
 #ifdef CONFIG_ENCODERS
--- a/raw.c	Mon Nov 10 18:40:14 2003 +0000
+++ b/raw.c	Mon Nov 10 18:41:45 2003 +0000
@@ -107,6 +107,48 @@
     return 0;
 }
 
+int pcm_read_seek(AVFormatContext *s, 
+                  int stream_index, int64_t timestamp)
+{
+    AVStream *st;
+    int block_align, byte_rate;
+    int64_t pos;
+
+    st = s->streams[0];
+    switch(st->codec.codec_id) {
+    case CODEC_ID_PCM_S16LE:
+    case CODEC_ID_PCM_S16BE:
+    case CODEC_ID_PCM_U16LE:
+    case CODEC_ID_PCM_U16BE:
+        block_align = 2 * st->codec.channels;
+        byte_rate = block_align * st->codec.sample_rate;
+        break;
+    case CODEC_ID_PCM_S8:
+    case CODEC_ID_PCM_U8:
+    case CODEC_ID_PCM_MULAW:
+    case CODEC_ID_PCM_ALAW:
+        block_align = st->codec.channels;
+        byte_rate = block_align * st->codec.sample_rate;
+        break;
+    default:
+        block_align = st->codec.block_align;
+        byte_rate = st->codec.bit_rate / 8;
+        break;
+    }
+    
+    if (block_align <= 0 || byte_rate <= 0)
+        return -1;
+
+    /* compute the position by aligning it to block_align */
+    pos = (timestamp * byte_rate) / AV_TIME_BASE;
+    pos = (pos / block_align) * block_align;
+
+    /* recompute exact position */
+    st->cur_dts = (pos * AV_TIME_BASE) / byte_rate;
+    url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
+    return 0;
+}
+
 /* ac3 read */
 static int ac3_read_header(AVFormatContext *s,
                            AVFormatParameters *ap)
@@ -119,6 +161,7 @@
 
     st->codec.codec_type = CODEC_TYPE_AUDIO;
     st->codec.codec_id = CODEC_ID_AC3;
+    st->need_parsing = 1;
     /* the parameters will be extracted from the compressed bitstream */
     return 0;
 }
@@ -135,10 +178,13 @@
 
     st->codec.codec_type = CODEC_TYPE_VIDEO;
     st->codec.codec_id = s->iformat->value;
+    st->need_parsing = 1;
+
     /* for mjpeg, specify frame rate */
     /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
-    if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
-        if (ap) {
+    if (st->codec.codec_id == CODEC_ID_MJPEG || 
+        st->codec.codec_id == CODEC_ID_MPEG4) {
+        if (ap && ap->frame_rate) {
             st->codec.frame_rate      = ap->frame_rate;
             st->codec.frame_rate_base = ap->frame_rate_base;
         } else {
@@ -356,9 +402,8 @@
 #endif //CONFIG_ENCODERS
 
 /* pcm formats */
-#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
 
-#define PCMDEF(name, long_name, ext, codec) \
+#define PCMINPUTDEF(name, long_name, ext, codec) \
 AVInputFormat pcm_ ## name ## _iformat = {\
     #name,\
     long_name,\
@@ -367,24 +412,20 @@
     raw_read_header,\
     raw_read_packet,\
     raw_read_close,\
+    pcm_read_seek,\
     .extensions = ext,\
     .value = codec,\
 };
 
+#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
+
+#define PCMDEF(name, long_name, ext, codec) \
+    PCMINPUTDEF(name, long_name, ext, codec)
+
 #else
 
 #define PCMDEF(name, long_name, ext, codec) \
-AVInputFormat pcm_ ## name ## _iformat = {\
-    #name,\
-    long_name,\
-    0,\
-    NULL,\
-    raw_read_header,\
-    raw_read_packet,\
-    raw_read_close,\
-    .extensions = ext,\
-    .value = codec,\
-};\
+    PCMINPUTDEF(name, long_name, ext, codec)\
 \
 AVOutputFormat pcm_ ## name ## _oformat = {\
     #name,\