getbit.c
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:4k
- /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
- /*
- * Disclaimer of Warranty
- *
- * These software programs are available to the user without any license fee or
- * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
- * any and all warranties, whether express, implied, or statuary, including any
- * implied warranties or merchantability or of fitness for a particular
- * purpose. In no event shall the copyright-holder be liable for any
- * incidental, punitive, or consequential damages of any kind whatsoever
- * arising from the use of these programs.
- *
- * This disclaimer of warranty extends to the user of these programs and user's
- * customers, employees, agents, transferees, successors, and assigns.
- *
- * The MPEG Software Simulation Group does not represent or warrant that the
- * programs furnished hereunder are free of infringement of any third-party
- * patents.
- *
- * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
- * are subject to royalty fees to patent holders. Many of these patents are
- * general enough such that they are unavoidable regardless of implementation
- * design.
- *
- */
- /*
- * All modifications (mpeg2decode -> DVD2AVI) are Copyright (C) Chia-chen Kuo - Jan 2001
- */
- #include "global.h"
- #include "getbit.h"
- __forceinline static unsigned int Get_Short(void);
- void Initialize_Buffer()
- {
- Rdptr = Rdbfr + BUFFER_SIZE;
- Rdmax = Rdptr;
- if (SystemStream_Flag)
- {
- if (Rdptr >= Rdmax)
- Next_Packet();
- CurrentBfr = *Rdptr++ << 24;
- if (Rdptr >= Rdmax)
- Next_Packet();
- CurrentBfr += *Rdptr++ << 16;
- if (Rdptr >= Rdmax)
- Next_Packet();
- CurrentBfr += *Rdptr++ << 8;
- if (Rdptr >= Rdmax)
- Next_Packet();
- CurrentBfr += *Rdptr++;
- Fill_Next();
- }
- else
- {
- Fill_Buffer();
- CurrentBfr = (*Rdptr << 24) + (*(Rdptr+1) << 16) + (*(Rdptr+2) << 8) + *(Rdptr+3);
- Rdptr += 4;
- Fill_Next();
- }
-
- BitsLeft = 32;
- }
- void Next_Packet(void)
- {
- static short volume;
- static int i, j, Packet_Length, Packet_Header_Length, size;
- static unsigned int code, AUDIO_ID, VOBCELL_Count, AC3_Track, MPA_Track;
- for (; Fault_Flag == 0;)
- {
- code = Get_Short();
- code = (code<<16) + Get_Short();
- // remove system layer byte stuffing
- while (!Fault_Flag && (code & 0xffffff00) != 0x00000100)
- code = (code<<8) + Get_Byte();
- if (Fault_Flag)
- {
- return;
- }
- switch (code)
- {
- case PACK_START_CODE:
- Rdptr += 8;
- VOBCELL_Count = 0;
- break;
- case PRIVATE_STREAM_2:
- Packet_Length = Get_Short();
- if (++VOBCELL_Count==2)
- {
- Rdptr += 25;
- VOB_ID = Get_Short();
- CELL_ID = Get_Short();
- Rdptr += Packet_Length - 29;
- }
- else
- Rdptr += Packet_Length;
- break;
- case VIDEO_ELEMENTARY_STREAM:
- Packet_Length = Get_Short();
- Rdmax = Rdptr + Packet_Length;
- code = Get_Byte();
- if ((code & 0xc0)==0x80)
- {
- code = Get_Byte();
- Packet_Header_Length = Get_Byte();
- if (code>=0x80 && !Rip_Flag)
- {
- code = Get_Byte();
- VideoPTS = (code & 0x0e) << 29;
- VideoPTS |= (Get_Short() & 0xfffe) << 14;
- VideoPTS |= (Get_Short()>>1) & 0x7fff;
- VideoPTS /= 90;
- Rdptr += Packet_Header_Length-5;
- }
- else
- Rdptr += Packet_Header_Length;
- Bitrate_Meter += Rdmax-Rdptr;
- return;
- }
- else
- Rdptr += Packet_Length-1;
- break;
- default:
- if (code>=SYSTEM_START_CODE)
- {
- Packet_Length = Get_Short();
- Rdptr += Packet_Length;
- }
- break;
- }
- }
- }
- unsigned int Get_Bits_All(unsigned int N)
- {
- N -= BitsLeft;
- Val = (CurrentBfr << (32 - BitsLeft)) >> (32 - BitsLeft);
- if (N)
- Val = (Val << N) + (NextBfr >> (32 - N));
- CurrentBfr = NextBfr;
- BitsLeft = 32 - N;
- Fill_Next();
- return Val;
- }
- void Flush_Buffer_All(unsigned int N)
- {
- CurrentBfr = NextBfr;
- BitsLeft = BitsLeft + 32 - N;
- Fill_Next();
- }
- void Fill_Buffer()
- {
- int readsize = BUFFER_SIZE;
- if (gIsEOS)
- {
- int available = GetAvailableInSmartCache();
- if (available == 0)
- {
- // Force to exit from decoding cycle
- Fault_Flag = ERROR_FLUSH;
- }
- if (available < BUFFER_SIZE)
- {
- readsize = available;
- }
- }
- Read = FetchDataFromSmartCache(Rdbfr, readsize);
- Rdptr = Rdbfr;
- Rdmax = Rdbfr + Read;
- Bitrate_Meter += Read;
- }
- static unsigned int Get_Short()
- {
- unsigned int i = Get_Byte();
- return (i<<8) | Get_Byte();
- }