--- dvbcut/dvbcut/src/streamdata.cpp	2007-05-04 12:29:49.000000000 +0200
+++ dvbcut-wg/src/streamdata.cpp	2007-05-27 14:41:15.000000000 +0200
@@ -37,28 +37,28 @@
       && (header[2] & 0x0c) != 0x0c;	/* sampling rate code */
 }
 
-static int mpaframe(const void *data, int &pos, int len)
+// return frame size in bytes, or 0 for no valid frame
+static int mpaframe(const void *data, int &pos, int len, int &duration)
   {
   const unsigned char *d=(const unsigned char *)data;
-  while ((pos+2)<len && !is_mpa(&d[pos]))
-    ++pos;
-  if (pos+2>=len)
-    return 0;
-
-  int layer=(d[pos+1]>>1)&0x03;
-  int samples=(layer==4-1)?384:1152;
-  int samplingrate=mpegaudio_rates[(d[pos+2]>>2)&0x03];
-  int bitratecode=(d[pos+2]>>4)&0x0f;
-
-  int skipbytes=(mpegaudio_bitrate[layer][bitratecode]*125)*samples/samplingrate;
-  if (skipbytes)
-    pos+=skipbytes;
-  else
-    pos+=3;
-
-  while ((pos+2)<len && !is_mpa(&d[pos]))
-    ++pos;
-  return samples*90000/samplingrate; // Duration of MPEG audio frame in 90000Hz units
+
+  for (; pos+2<len; ++pos) {
+    if (!is_mpa(&d[pos]))
+      continue;
+
+    int layer=(d[pos+1]>>1)&0x03;
+    int samples=(layer==4-1)?384:1152;
+    int samplingrate=mpegaudio_rates[(d[pos+2]>>2)&0x03];
+    int bitratecode=(d[pos+2]>>4)&0x0f;
+
+    int skipbytes=(mpegaudio_bitrate[layer][bitratecode]*125)*samples/samplingrate;
+    if (skipbytes>0 && pos+skipbytes+2<len && is_mpa(&d[pos+skipbytes])) {
+      //fprintf(stderr, "mpaframe: samples=%d pos=%d len=%d\n", samples, pos,len);
+      duration = samples*90000/samplingrate; // Duration of MPEG audio frame in 90000Hz units
+      return skipbytes;
+    }
+  }
+  return 0;
   }
 
 //
@@ -101,19 +101,23 @@
 
 static int ac3_frameduration[4]={270000*1536/480,270000*1536/441,270000*1536/320,270000*1536/480};
 
-static int ac3frame(const void *data, int &pos, int len)
+// return frame size in bytes, or 0 for no valid frame
+static int ac3frame(const void *data, int &pos, int len, int &duration)
   {
   const unsigned char *d=(const unsigned char *)data;
-  while ((pos+2)<len && (*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
-    ++pos;
-  if (pos+2>=len)
-    return 0;
-
-  pos+=ac3_framelength[d[pos+4]];
-
-  while ((pos+2)<len && (*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
-    ++pos;
-  return ac3_frameduration[(d[pos+4]>>6)&3]/300; // Duration of AC3 audio frame in 90000Hz units
+
+  for(; pos+4<len; ++pos) {
+  if ((*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
+    continue;
+
+  int framelength = ac3_framelength[d[pos+4]];
+
+  if ((pos+framelength+1)<len && (*(uint16_t*)(d+pos+framelength)==AC3_SYNCWORD)) {
+    duration = ac3_frameduration[(d[pos+4]>>6)&3]/300; // Duration of AC3 audio frame in 90000Hz units
+    return framelength;
+  }
+  }
+  return 0;
   }
 
 void streamdata::audio_addpts(uint32_t startbufferpos, bool onepacket)
@@ -171,18 +175,13 @@
     int pos=0;
     int len=inbytes()-(bufferposition-getoffset());
     int ptsplus;
-    if (type==streamtype::ac3audio)
-      ptsplus=ac3frame(getdata(bufferposition),pos,len);
-    else
-      ptsplus=mpaframe(getdata(bufferposition),pos,len);
-    if (pos+2>=len)
+    int framesize = (type==streamtype::ac3audio) ?
+      ac3frame(getdata(bufferposition),pos,len,ptsplus) :
+      mpaframe(getdata(bufferposition),pos,len,ptsplus);
+    if (framesize==0)
       break;
-    if (ptsplus==0) {
-      needsync=true;
-      continue;
-      }
 
-    bufferposition+=pos;
+    bufferposition+=pos+framesize;
     pts+=ptsplus;
 
     if (nx==items.end() || bufferposition<nx->bufferposition) {
