changeset 19243:73443b22df17

final cleanup: implemented send_mpeg_pes_packet() and send_mpeg_lpcm_packet() using new send_mpeg_pes_packet_ll()
author nicodvb
date Sat, 29 Jul 2006 18:39:07 +0000
parents 8298ac25902e
children 5e5d323b1934
files libmpdemux/mpeg_packetizer.c
diffstat 1 files changed, 29 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/mpeg_packetizer.c	Sat Jul 29 18:24:27 2006 +0000
+++ b/libmpdemux/mpeg_packetizer.c	Sat Jul 29 18:39:07 2006 +0000
@@ -45,9 +45,10 @@
 };
 
 /* Send MPEG <type> PES packet */
-int
-send_mpeg_pes_packet (unsigned char *data, int len, int id, uint64_t pts,
-                      int type, int my_write (unsigned char *data, int len))
+static int
+send_mpeg_pes_packet_ll(unsigned char *data, int len, int id, uint64_t pts,
+                      int type, unsigned char *header, int header_len,
+                      int align4, int my_write (unsigned char *data, int len))
 {
   int ptslen = (pts ? 5 : 0);
   int n = 0;
@@ -71,11 +72,13 @@
         hdr = 3;
     else
         hdr = (ptslen ? 0 : 1);
-    if (6 + hdr + ptslen + payload_size > PES_MAX_SIZE)
-      payload_size = PES_MAX_SIZE - 6 - hdr - ptslen;
+    if (6 + hdr + ptslen + payload_size + header_len > PES_MAX_SIZE)
+      payload_size = PES_MAX_SIZE - 6 - hdr - ptslen - header_len;
+    if(align4)
+      payload_size &= ~3;
 
     /* construct PES header: packetize */
-    plen = payload_size + hdr + ptslen;
+    plen = payload_size + hdr + ptslen + header_len;
     pes_header[4] = plen >> 8;
     pes_header[5] = plen & 255;
     idx = 6;
@@ -114,6 +117,12 @@
       else
         pes_header[idx++] = 0x0f;
     }
+
+    if(header_len)
+    {
+        memcpy(&pes_header[idx], header, header_len);
+        idx += header_len;
+    }
     
     my_write (pes_header, idx);
     n = my_write (data, payload_size);
@@ -121,14 +130,24 @@
     len -= n;
     data += n;
     ptslen = 0; /* store PTS only once, at first packet! */
+    if(align4 && len < 4)
+      break;
   }
 
   return n;
 }
 
+int
+send_mpeg_pes_packet (unsigned char *data, int len, int id, uint64_t pts,
+                      int type, int my_write (unsigned char *data, int len))
+{
+    send_mpeg_pes_packet_ll(data, len, id, pts, type, NULL, 0, 0, my_write);
+}
+
+
 /* Send MPEG <type> PS packet */
 int
-send_mpeg_ps_packet (unsigned char *data, int len, int id, uint64_t pts, int type,
+send_mpeg_ps_packet(unsigned char *data, int len, int id, uint64_t pts, int type,
                       int my_write (unsigned char *data, int len))
 {
   if(type == 2)
@@ -140,97 +159,10 @@
 
 /* Send MPEG 2 LPCM packet */
 int
-send_mpeg_lpcm_packet (unsigned char* data, int len,
+send_mpeg_lpcm_packet(unsigned char* data, int len,
                        int id, uint64_t pts, int freq_id,
                        int my_write (unsigned char *data, int len))
 {
-  int ptslen = pts ? 5 : 0;
-  int n = 0;
-
-  mp_msg (MSGT_HEADER, MSGL_DBG2,
-          "MPEG LPCM packet: 0x%x => %lu   \n", id, pts);
-  memset (pes_header, '\0', PES_MAX_SIZE);
-  
-  /* startcode */
-  pes_header[0] = 0;
-  pes_header[1] = 0;
-  pes_header[2] = 1;
-  pes_header[3] = 0xBD; /* stream id */
-    
-  while (len >= 4)
-  {
-    int payload_size = PES_MAX_SIZE - 6 - 20; /* max possible data len */
-    if( payload_size > len)
-      payload_size = len;
-
-    payload_size &= ~3; /* align! */
-
-    /* packetsize */
-    pes_header[4] = (payload_size + 3 + ptslen + 7) >> 8;
-    pes_header[5] = (payload_size + 3 + ptslen + 7) & 255;
-
-    /* stuffing */
-    /* TTCCxxxx  CC=css TT=type: 1=STD 0=mpeg1 2=vob */
-    pes_header[6] = 0x81;
-	      
-    /* FFxxxxxx   FF=pts flags=2 vs 0 */
-    pes_header[7] = ptslen ? 0x80 : 0;
-
-    /* header length */
-    pes_header[8] = ptslen;
-	      
-    if (ptslen)
-    {
-      int x;
-
-      /* presentation time stamp */
-      x = (0x02 << 4) | (((pts >> 30) & 0x07) << 1) | 1;
-      pes_header[9] = x;
-      
-      x = ((((pts >> 15) & 0x7fff) << 1) | 1);
-      pes_header[10] = x >> 8;
-      pes_header[11] = x &255;
-      
-      x = (((pts & 0x7fff) << 1) | 1);
-      pes_header[12] = x >> 8;
-      pes_header[13] = x & 255;
-    }
-	      
-    /* ============ LPCM header: (7 bytes) ================= */
-    /* Info by mocm@convergence.de */
-    
-    /* ID */
-    pes_header[ptslen + 9] = id;
-
-    /* number of frames */
-    pes_header[ptslen + 10] = 0x07;
-
-    /* first acces unit pointer, i.e. start of audio frame */
-    pes_header[ptslen + 11] = 0x00;
-    pes_header[ptslen + 12] = 0x04;
-
-    /* audio emphasis on-off                                  1 bit */
-    /* audio mute on-off                                      1 bit */
-    /* reserved                                               1 bit */
-    /* audio frame number                                     5 bit */
-    pes_header[ptslen + 13] = 0x0C;
-
-    /* quantization word length                               2 bit */
-    /* audio sampling frequency (48khz = 0, 96khz = 1)        2 bit */
-    /* reserved                                               1 bit */
-    /* number of audio channels - 1 (e.g. stereo = 1)         3 bit */
-    pes_header[ptslen + 14] = 1 | (freq_id << 4);
-
-    /* dynamic range control (0x80 if off) */
-    pes_header [ptslen + 15] = 0x80;
-
-    memcpy (&pes_header[6 + 3 + ptslen + 7], data, payload_size);
-    n += my_write (pes_header, 6 + 3 + ptslen + 7 + payload_size);
-
-    len -= payload_size;
-    data += payload_size;
-    ptslen = 0; /* store PTS only once, at first packet! */
-  }
-
-  return n;
+    unsigned char header[7] = {0xA0, 0x07, 0x00, 0x04, 0x0C, 1 | (freq_id << 4), 0x80};
+    send_mpeg_pes_packet_ll(data, len, 0xBD, pts, 2, header, sizeof(header), 1, my_write);
 }