home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Video / systems.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  4.7 KB  |  204 lines

  1. /* systems.c, systems-specific routines                                 */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. #include "config.h"
  34. #include "global.h"
  35.  
  36.  
  37. #define Show_Bits(N) ( ld->Bfr >> (32-N) )
  38.  
  39. /* initialize buffer, call once before first getbits or showbits */
  40.  
  41. /* parse system layer, ignore everything we don't need */
  42. void Next_Packet()
  43. {
  44.   unsigned int code;
  45.   int l;
  46.  
  47.   for(;;)
  48.   {
  49.     code = Get_Long();
  50.  
  51.     /* remove system layer byte stuffing */
  52.     while ((code & 0xffffff00) != 0x100)
  53.       code = (code<<8) | Get_Byte();
  54.  
  55.     switch(code)
  56.     {
  57.     case PACK_START_CODE: /* pack header */
  58.       /* skip pack header (system_clock_reference and mux_rate) */
  59.       ld->Rdptr += 8;
  60.       break;
  61.     case VIDEO_ELEMENTARY_STREAM:   
  62.       code = Get_Word();             /* packet_length */
  63.       ld->Rdmax = ld->Rdptr + code;
  64.  
  65.       code = Get_Byte();
  66.  
  67.       if((code>>6)==0x02)
  68.       {
  69.         ld->Rdptr++;
  70.         code=Get_Byte();  /* parse PES_header_data_length */
  71.         ld->Rdptr+=code;    /* advance pointer by PES_header_data_length */
  72.  
  73.         return;
  74.       }
  75.       else if(code==0xff)
  76.       {
  77.         /* parse MPEG-1 packet header */
  78.         while((code=Get_Byte())== 0xFF);
  79.       }
  80.        
  81.       /* stuffing bytes */
  82.       if(code>=0x40)
  83.       {
  84.         if(code>=0x80)
  85.         {
  86.           fprintf(stderr,"Error in packet header\n");
  87.           return;
  88.         }
  89.         /* skip STD_buffer_scale */
  90.         ld->Rdptr++;
  91.         code = Get_Byte();
  92.       }
  93.  
  94.       if(code>=0x30)
  95.       {
  96.         if(code>=0x40)
  97.         {
  98.           fprintf(stderr,"Error in packet header\n");
  99.           return;
  100.         }
  101.         /* skip presentation and decoding time stamps */
  102.         ld->Rdptr += 9;
  103.       }
  104.       else if(code>=0x20)
  105.       {
  106.         /* skip presentation time stamps */
  107.         ld->Rdptr += 4;
  108.       }
  109.       else if(code!=0x0f)
  110.       {
  111.         fprintf(stderr,"Error in packet header\n");
  112.         return;
  113.       }
  114.       return;
  115.     case ISO_END_CODE: /* end */
  116.       /* simulate a buffer full of sequence end codes */
  117.       l = 0;
  118.       while (l<VIDEO_BUFFER_SIZE)
  119.       {
  120.         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>24;
  121.         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>16;
  122.         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>8;
  123.         ld->Rdbfr[l++] = SEQUENCE_END_CODE&0xff;
  124.       }
  125.       ld->Rdptr = ld->Rdbfr;
  126.       ld->Rdmax = ld->Rdbfr + VIDEO_BUFFER_SIZE;
  127.       return;
  128.     default:
  129.       if(code>=SYSTEM_START_CODE)
  130.       {
  131.         /* skip system headers and non-video packets*/
  132.         code = Get_Word();
  133.         ld->Rdptr += code;
  134.       }
  135.       else
  136.       {
  137.         fprintf(stderr,"Unexpected startcode %08x in system layer\n",code);
  138.         return;
  139.       }
  140.       break;
  141.     }
  142.   }
  143. }
  144.  
  145.  
  146.  
  147. void Flush_Buffer32()
  148. {
  149.   int Incnt;
  150.  
  151.   ld->Bfr = 0;
  152.  
  153.   Incnt = ld->Incnt;
  154.   Incnt -= 32;
  155.  
  156.   if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  157.   {
  158.     while (Incnt <= 24)
  159.     {
  160.       if (ld->Rdptr >= ld->Rdmax)
  161.         Next_Packet();
  162.       ld->Bfr |= Get_Byte() << (24 - Incnt);
  163.       Incnt += 8;
  164.     }
  165.   }
  166.   else
  167.   {
  168.     while (Incnt <= 24)
  169.     {
  170.       if (ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  171.         Fill_Buffer();
  172.       ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  173.       Incnt += 8;
  174.     }
  175.   }
  176.   ld->Incnt = Incnt;
  177.  
  178. #ifdef VERIFY 
  179.   ld->Bitcnt += 32;
  180. #endif /* VERIFY */
  181. }
  182.  
  183.  
  184. unsigned int Get_Bits32()
  185. {
  186.   unsigned int l;
  187.  
  188.   l = Show_Bits(32);
  189.   Flush_Buffer32();
  190.  
  191.   return l;
  192. }
  193.  
  194.  
  195. int Get_Long()
  196. {
  197.   int i;
  198.  
  199.   i = Get_Word();
  200.   return (i<<16) | Get_Word();
  201. }
  202.  
  203.  
  204.