changeset 28468:b9da026ee222

change internal real video packetizing format to the more straight forward one see [MPlayer-dev-eng] [PATCH] cleanup/uniformize real video packetizing patch blessed by Roberto
author aurel
date Mon, 09 Feb 2009 22:00:57 +0000
parents 0450a90b81cb
children 75df3c533a73
files libmpcodecs/vd_ffmpeg.c libmpcodecs/vd_realvid.c libmpdemux/demux_mkv.c libmpdemux/demux_real.c
diffstat 4 files changed, 63 insertions(+), 172 deletions(-) [+]
line wrap: on
line diff
--- a/libmpcodecs/vd_ffmpeg.c	Mon Feb 09 20:31:35 2009 +0000
+++ b/libmpcodecs/vd_ffmpeg.c	Mon Feb 09 22:00:57 2009 +0000
@@ -755,28 +755,6 @@
 
     avctx->hurry_up=(flags&3)?((flags&2)?2:1):0;
 
-    if(sh->ds->demuxer->type != DEMUXER_TYPE_LAVF &&
-       sh->ds->demuxer->type != DEMUXER_TYPE_LAVF_PREFERRED)
-    if(   sh->format == mmioFOURCC('R', 'V', '1', '0')
-       || sh->format == mmioFOURCC('R', 'V', '1', '3')
-       || sh->format == mmioFOURCC('R', 'V', '2', '0')
-       || sh->format == mmioFOURCC('R', 'V', '3', '0')
-       || sh->format == mmioFOURCC('R', 'V', '4', '0'))
-    {
-        dp_hdr_t *hdr= (dp_hdr_t*)data;
-        uint32_t *offsets = (uint32_t*)(data + hdr->chunktab);
-        uint8_t *offstab = av_malloc((hdr->chunks+1) * 8);
-        uint8_t *buf = data;
-        int chunks = hdr->chunks;
-        int dlen = hdr->len;
-
-        buf[0] = chunks;
-        memcpy(offstab, offsets, (chunks + 1) * 8);
-        memmove(buf + 1 + (chunks + 1) * 8, data + sizeof(dp_hdr_t), dlen);
-        memcpy(buf + 1, offstab, (chunks + 1) * 8);
-        av_free(offstab);
-    }
-
     mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
            ((int *)data)[0], ((int *)data)[1], ((int *)data)[2], ((int *)data)[3]);
     ret = avcodec_decode_video(avctx, pic,
--- a/libmpcodecs/vd_realvid.c	Mon Feb 09 20:31:35 2009 +0000
+++ b/libmpcodecs/vd_realvid.c	Mon Feb 09 22:00:57 2009 +0000
@@ -9,6 +9,7 @@
 
 #include "mp_msg.h"
 #include "help_mp.h"
+#include "mpbswap.h"
 
 #include "vd_internal.h"
 #include "loader/wine/windef.h"
@@ -362,27 +363,23 @@
 	bufsz = 0;
 }
 
-// copypaste from demux_real.c - it should match to get it working!
-typedef struct dp_hdr_s {
-    uint32_t chunks;	// number of chunks
-    uint32_t timestamp; // timestamp from packet header
-    uint32_t len;	// length of actual data
-    uint32_t chunktab;	// offset to chunk offset array
-} dp_hdr_t;
-
 // decode a frame
 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
 	mp_image_t* mpi;
 	unsigned long result;
-	dp_hdr_t* dp_hdr=(dp_hdr_t*)data;
-	unsigned char* dp_data=((unsigned char*)data)+sizeof(dp_hdr_t);
-	uint32_t* extra=(uint32_t*)(((char*)data)+dp_hdr->chunktab);
+	uint8_t *buf = data;
+	int chunks = *buf++;
+	int extra_size = 8*(chunks+1);
+	uint32_t data_size = len-1-extra_size;
+	unsigned char* dp_data=buf+extra_size;
+	uint32_t* extra=(uint32_t*)buf;
+	int i;
 
 	unsigned int transform_out[5];
 	transform_in_t transform_in={
-		dp_hdr->len,	// length of the packet (sub-packets appended)
+		data_size,	// length of the packet (sub-packets appended)
 		0,		// unknown, seems to be unused
-		dp_hdr->chunks,	// number of sub-packets - 1
+		chunks,	// number of sub-packets - 1
 		extra,		// table of sub-packet offsets
 		0,		// unknown, seems to be unused
 		0,		// timestamp (should be unneded)
@@ -397,6 +394,9 @@
 	    if (!buffer) return 0;
 	}
 	
+	for (i=0; i<2*(chunks+1); i++)
+		extra[i] = le2me_32(extra[i]);
+
 #ifdef CONFIG_WIN32DLL
 	if (dll_type == 1)
 	    result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
--- a/libmpdemux/demux_mkv.c	Mon Feb 09 20:31:35 2009 +0000
+++ b/libmpdemux/demux_mkv.c	Mon Feb 09 22:00:57 2009 +0000
@@ -98,7 +98,8 @@
 
   /* stuff for realmedia */
   int realmedia;
-  int rv_kf_base, rv_kf_pts;
+  int64_t rv_kf_base;
+  int rv_kf_pts;
   float rv_pts;  /* previous video timestamp */
   float ra_pts;  /* previous audio timestamp */
 
@@ -2379,61 +2380,7 @@
   ds_add_packet(demuxer->sub, dp);
 }
 
-// Taken from demux_real.c. Thanks to the original developpers :)
-#define SKIP_BITS(n) buffer <<= n
-#define SHOW_BITS(n) ((buffer) >> (32 - (n)))
-
-static float real_fix_timestamp(mkv_track_t *track, unsigned char *s,
-                                int timestamp) {
-  float v_pts;
-  uint32_t buffer = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
-  int kf = timestamp;
-  int pict_type;
-  int orig_kf;
-
-  if (!strcmp(track->codec_id, MKV_V_REALV30) ||
-      !strcmp(track->codec_id, MKV_V_REALV40)) {
-
-    if (!strcmp(track->codec_id, MKV_V_REALV30)) {
-      SKIP_BITS(3);
-      pict_type = SHOW_BITS(2);
-      SKIP_BITS(2 + 7);
-    }else{
-      SKIP_BITS(1);
-      pict_type = SHOW_BITS(2);
-      SKIP_BITS(2 + 7 + 3);
-    }
-    kf = SHOW_BITS(13);         // kf= 2*SHOW_BITS(12);
-    orig_kf = kf;
-    if (pict_type <= 1) {
-      // I frame, sync timestamps:
-      track->rv_kf_base = timestamp - kf;
-      mp_msg(MSGT_DEMUX, MSGL_DBG2, "\nTS: base=%08X\n", track->rv_kf_base);
-      kf = timestamp;
-    } else {
-      // P/B frame, merge timestamps:
-      int tmp = timestamp - track->rv_kf_base;
-      kf |= tmp & (~0x1fff);    // combine with packet timestamp
-      if (kf < (tmp - 4096))    // workaround wrap-around problems
-        kf += 8192;
-      else if (kf > (tmp + 4096))
-        kf -= 8192;
-      kf += track->rv_kf_base;
-    }
-    if (pict_type != 3) {       // P || I  frame -> swap timestamps
-      int tmp = kf;
-      kf = track->rv_kf_pts;
-      track->rv_kf_pts = tmp;
-    }
-    mp_msg(MSGT_DEMUX, MSGL_DBG2, "\nTS: %08X -> %08X (%04X) %d %02X %02X %02X "
-           "%02X %5d\n", timestamp, kf, orig_kf, pict_type, s[0], s[1], s[2],
-           s[3], kf - (int)(1000.0 * track->rv_pts));
-  }
-  v_pts = kf * 0.001f;
-  track->rv_pts = v_pts;
-
-  return v_pts;
-}
+double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts);
 
 static void
 handle_realvideo (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
@@ -2442,35 +2389,9 @@
   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
   demux_packet_t *dp;
   uint32_t timestamp = mkv_d->last_pts * 1000;
-  uint32_t *hdr;
-  uint8_t chunks;
-  int isize;
-#ifdef WORDS_BIGENDIAN
-  uint8_t *p;
-  int i;
-#endif
-
-  chunks = *buffer++;
-  isize = --size - (chunks+1)*8;
-  dp = new_demux_packet (REALHEADER_SIZE + size);
-  memcpy (dp->buffer + REALHEADER_SIZE, buffer + (chunks+1)*8, isize);
-#ifdef WORDS_BIGENDIAN
-  p = (uint8_t *)(dp->buffer + REALHEADER_SIZE + isize);
-  for (i = 0; i<(chunks+1)*8; i+=4) {
-    p[i] = *((uint8_t *)buffer+i+3);
-    p[i+1] = *((uint8_t *)buffer+i+2);
-    p[i+2] = *((uint8_t *)buffer+i+1);
-    p[i+3] = *((uint8_t *)buffer+i);
-  }
-#else
-  memcpy (dp->buffer + REALHEADER_SIZE + isize, buffer, (chunks+1)*8);
-#endif
-
-  hdr = dp->buffer;
-  *hdr++ = chunks;                 // number of chunks
-  *hdr++ = timestamp;              // timestamp from packet header
-  *hdr++ = isize;                  // length of actual data
-  *hdr++ = REALHEADER_SIZE + isize;    // offset to chunk offset array
+
+  dp = new_demux_packet (size);
+  memcpy (dp->buffer, buffer, size);
 
   if (mkv_d->v_skip_to_keyframe)
     {
@@ -2479,8 +2400,9 @@
       track->rv_kf_pts = timestamp;
     }
   else
-    dp->pts = real_fix_timestamp (track, dp->buffer + REALHEADER_SIZE,
-                                  timestamp);
+    dp->pts = real_fix_timestamp (dp->buffer, timestamp,
+                                  ((sh_video_t*)demuxer->video->sh)->bih->biCompression,
+                                  &track->rv_kf_base, &track->rv_kf_pts, NULL);
   dp->pos = demuxer->filepos;
   dp->flags = block_bref ? 0 : 0x10;
 
--- a/libmpdemux/demux_real.c	Mon Feb 09 20:31:35 2009 +0000
+++ b/libmpdemux/demux_real.c	Mon Feb 09 22:00:57 2009 +0000
@@ -28,6 +28,7 @@
 #include "config.h"
 #include "mp_msg.h"
 #include "help_mp.h"
+#include "mpbswap.h"
 
 #include "stream/stream.h"
 #include "demuxer.h"
@@ -474,8 +475,9 @@
 #define SKIP_BITS(n) buffer<<=n
 #define SHOW_BITS(n) ((buffer)>>(32-(n)))
 
-static double real_fix_timestamp(real_priv_t* priv, unsigned char* s, unsigned int timestamp, double frametime, unsigned int format){
+double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts){
   double v_pts;
+  unsigned char *s = buf + 1 + (*buf+1)*8;
   uint32_t buffer= (s[0]<<24) + (s[1]<<16) + (s[2]<<8) + s[3];
   unsigned int kf=timestamp;
   int pict_type;
@@ -497,29 +499,29 @@
 //    if(pict_type==0)
     if(pict_type<=1){
       // I frame, sync timestamps:
-      priv->kf_base=(int64_t)timestamp-kf;
-      mp_msg(MSGT_DEMUX, MSGL_DBG2,"\nTS: base=%08"PRIX64"\n",priv->kf_base);
+      *kf_base=(int64_t)timestamp-kf;
+      mp_msg(MSGT_DEMUX, MSGL_DBG2,"\nTS: base=%08"PRIX64"\n",*kf_base);
       kf=timestamp;
     } else {
       // P/B frame, merge timestamps:
-      int64_t tmp=(int64_t)timestamp-priv->kf_base;
+      int64_t tmp=(int64_t)timestamp-*kf_base;
       kf|=tmp&(~0x1fff);	// combine with packet timestamp
       if(kf<tmp-4096) kf+=8192; else // workaround wrap-around problems
       if(kf>tmp+4096) kf-=8192;
-      kf+=priv->kf_base;
+      kf+=*kf_base;
     }
     if(pict_type != 3){ // P || I  frame -> swap timestamps
 	unsigned int tmp=kf;
-	kf=priv->kf_pts;
-	priv->kf_pts=tmp;
+	kf=*kf_pts;
+	*kf_pts=tmp;
 //	if(kf<=tmp) kf=0;
     }
-    mp_msg(MSGT_DEMUX, MSGL_DBG2,"\nTS: %08X -> %08X (%04X) %d %02X %02X %02X %02X %5u\n",timestamp,kf,orig_kf,pict_type,s[0],s[1],s[2],s[3],kf-(unsigned int)(1000.0*priv->v_pts));
+    mp_msg(MSGT_DEMUX, MSGL_DBG2,"\nTS: %08X -> %08X (%04X) %d %02X %02X %02X %02X %5u\n",timestamp,kf,orig_kf,pict_type,s[0],s[1],s[2],s[3],pts?kf-(unsigned int)(*pts*1000.0):0);
   }
 #endif
     v_pts=kf*0.001f;
-//    if(v_pts<priv->v_pts || !kf) v_pts=priv->v_pts+frametime;
-    priv->v_pts=v_pts;
+//    if(pts && (v_pts<*pts || !kf)) v_pts=*pts+frametime;
+    if(pts) *pts=v_pts;
     return v_pts;
 }
 
@@ -530,6 +532,29 @@
     uint32_t chunktab;	// offset to chunk offset array
 } dp_hdr_t;
 
+static void queue_video_packet(real_priv_t *priv, demux_stream_t *ds, demux_packet_t *dp)
+{
+    dp_hdr_t hdr = *(dp_hdr_t*)dp->buffer;
+    unsigned char *tmp = malloc(8*(1+hdr.chunks));
+    memcpy(tmp, dp->buffer+hdr.chunktab, 8*(1+hdr.chunks));
+    memmove(dp->buffer+1+8*(1+hdr.chunks), dp->buffer+sizeof(dp_hdr_t), hdr.len);
+    memcpy(dp->buffer+1, tmp, 8*(1+hdr.chunks));
+    *dp->buffer = (uint8_t)hdr.chunks;
+    free(tmp);
+
+    if(priv->video_after_seek){
+        priv->kf_base = 0;
+        priv->kf_pts = hdr.timestamp;
+        priv->video_after_seek = 0;
+    }
+    if(hdr.len >= 3)  /* this check may be useless */
+        dp->pts = real_fix_timestamp(dp->buffer, hdr.timestamp,
+                                     ((sh_video_t *)ds->sh)->format,
+                                     &priv->kf_base, &priv->kf_pts,
+                                     &priv->v_pts);
+    ds_add_packet(ds, dp);
+}
+
 // return value:
 //     0 = EOF or no stream found
 //     1 = successfully read a packet
@@ -915,16 +940,7 @@
 		    if(ds->asf_seq!=vpkg_seqnum){
 			// this fragment is for new packet, close the old one
 			mp_msg(MSGT_DEMUX,MSGL_DBG2, "closing probably incomplete packet, len: %d  \n",dp->len);
-			if(priv->video_after_seek){
-				priv->kf_base = 0;
-				priv->kf_pts = dp_hdr->timestamp;
-				dp->pts=
-				real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-				priv->video_after_seek = 0;
-			} else if (dp_hdr->len >= 3)
-			    dp->pts =
-			    real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-			ds_add_packet(ds,dp);
+			queue_video_packet(priv, ds, dp);
 			ds->asf_packet=NULL;
 		    } else {
 			// append data to it!
@@ -941,8 +957,8 @@
 			    dp_data=dp->buffer+sizeof(dp_hdr_t);
 			    extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
 			}
-			extra[2*dp_hdr->chunks+0]=1;
-			extra[2*dp_hdr->chunks+1]=dp_hdr->len;
+			extra[2*dp_hdr->chunks+0]=le2me_32(1);
+			extra[2*dp_hdr->chunks+1]=le2me_32(dp_hdr->len);
 			if(0x80==(vpkg_header&0xc0)){
 			    // last fragment!
 			    if(dp_hdr->len!=vpkg_length-vpkg_offset)
@@ -954,16 +970,7 @@
 			    len-=vpkg_offset;
  			    mp_dbg(MSGT_DEMUX,MSGL_DBG2, "fragment (%d bytes) appended, %d bytes left\n",vpkg_offset,len);
 			    // we know that this is the last fragment -> we can close the packet!
-			    if(priv->video_after_seek){
-				    priv->kf_base = 0;
-				    priv->kf_pts = dp_hdr->timestamp;
-				    dp->pts=
-				    real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-				    priv->video_after_seek = 0;
-			    } else if (dp_hdr->len >= 3)
-				dp->pts =
-				real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-			    ds_add_packet(ds,dp);
+			    queue_video_packet(priv, ds, dp);
 			    ds->asf_packet=NULL;
 			    // continue parsing
 			    continue;
@@ -991,7 +998,7 @@
 		dp_hdr->chunktab=sizeof(dp_hdr_t)+vpkg_length;
 		dp_data=dp->buffer+sizeof(dp_hdr_t);
 		extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
-		extra[0]=1; extra[1]=0; // offset of the first chunk
+		extra[0]=le2me_32(1); extra[1]=0; // offset of the first chunk
 		if(0x00==(vpkg_header&0xc0)){
 		    // first fragment:
 		    if (len > dp->len - sizeof(dp_hdr_t)) len = dp->len - sizeof(dp_hdr_t);
@@ -999,13 +1006,6 @@
 		    stream_read(demuxer->stream, dp_data, len);
 		    ds->asf_packet=dp;
 		    len=0;
-		    if(priv->video_after_seek){
-		        priv->kf_base = 0;
-		        priv->kf_pts = dp_hdr->timestamp;
-		        dp->pts=
-		        real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-		        priv->video_after_seek = 0;
-		    }
 		    break;
 		}
 		// whole packet (not fragmented):
@@ -1020,16 +1020,7 @@
 		}
 		dp_hdr->len=vpkg_length; len-=vpkg_length;
 		stream_read(demuxer->stream, dp_data, vpkg_length);
-		if(priv->video_after_seek){
-			priv->kf_base = 0;
-			priv->kf_pts = dp_hdr->timestamp;
-			dp->pts=
-			real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-			priv->video_after_seek = 0;
-		} else if (dp_hdr->len >= 3)
-		    dp->pts =
-		    real_fix_timestamp(priv,dp_data,dp_hdr->timestamp,sh_video->frametime,sh_video->format);
-		ds_add_packet(ds,dp);
+		queue_video_packet(priv, ds, dp);
 
 	    } // while(len>0)