📄 mpegplayer.java
字号:
Y = 512 + data[offset++]; YCbCr = frameBuffer[address]; frameBuffer[address++] = (clip[((YCbCr >> 0) & 0xFF) + Y ] << 0) + (clip[((YCbCr >> 8) & 0xFF) + Cb] << 8) + (clip[((YCbCr >> 16) & 0xFF) + Cr] << 16); Y = 512 + data[offset++]; YCbCr = frameBuffer[address]; frameBuffer[address++] = (clip[((YCbCr >> 0) & 0xFF) + Y ] << 0) + (clip[((YCbCr >> 8) & 0xFF) + Cb] << 8) + (clip[((YCbCr >> 16) & 0xFF) + Cr] << 16); offset += 16-2; address += width-2; Y = 512 + data[offset++]; YCbCr = frameBuffer[address]; frameBuffer[address++] = (clip[((YCbCr >> 0) & 0xFF) + Y ] << 0) + (clip[((YCbCr >> 8) & 0xFF) + Cb] << 8) + (clip[((YCbCr >> 16) & 0xFF) + Cr] << 16); Y = 512 + data[offset++]; YCbCr = frameBuffer[address]; frameBuffer[address++] = (clip[((YCbCr >> 0) & 0xFF) + Y ] << 0) + (clip[((YCbCr >> 8) & 0xFF) + Cb] << 8) + (clip[((YCbCr >> 16) & 0xFF) + Cr] << 16); offset -= 16; address -= width; index++; } offset += 16; address += width+width-16; } } private static int data[] = new int[256]; private static int clip[] = new int[1024]; static { for (int i = 0; i < 1024; i++) { clip[i] = Math.min(Math.max(i - 512, 0), 255); } }}/** * The MPEG-1 video stream decoder according to ISO 11172-2. */class MPEGVideoStream { /** * MPEG-1 video frame rate table */ private static final int frameRateTable[] = { 30, 24, 24, 25, 30, 30, 50, 60, 60, 12, 30, 30, 30, 30, 30, 30 }; /** * MPEG-1 video stream frame rate (frames per second) */ private int frameRate; /** * MPEG-1 video stream bit rate (bits per second) */ private int bitRate; /** * MPEG-1 video stream VBV buffer size (16 kbit steps) */ private int bufferSize; /** * MPEG-1 video stream time record (in frames) */ private int hour, minute, second, frame; /** * MPEG-1 video stream current picture */ private Picture picture; /** * MPEG-1 video stream boolean fields */ private boolean constrained, dropped, closed, broken; /** * The last MPEG-1 picture frame parsed */ private int frameBuffer[]; /** * The underlaying VLC input stream */ private VLCInputStream stream; /** * Initializes the MPEG-1 video input stream object */ public MPEGVideoStream(InputStream inputStream) throws IOException { /* set ups the VLC input stream */ stream = new VLCInputStream(inputStream); /* set ups the picture decoder */ picture = new Picture(); /* reset rates and buffer size */ frameRate = 0; bitRate = 0; bufferSize = 0; /* reset time record */ hour = 0; minute = 0; second = 0; frame = 0; /* reset boolean fields */ constrained = false; dropped = false; closed = false; broken = false; /* reset last frame buffer */ frameBuffer = getFrame(); } /** * Returns the MPEG-1 video stream frame rate */ public int getFrameRate() { return frameRate; } /** * Changes the MPEG-1 video stream frame rate */ public void setFrameRate(int rate) { frameRate = rate; } /** * Returns the MPEG-1 video stream bit rate */ public int getBitRate() { return bitRate; } /** * Changes the MPEG-1 video stream bit rate */ public void setBitRate(int rate) { bitRate = rate; } /** * Returns the MPEG-1 video stream VBV buffer size */ public int getBufferSize() { return bufferSize; } /** * Changes the MPEG-1 video stream VBV buffer size */ public void setBufferSize(int size) { bufferSize = size; } /** * Returns the MPEG-1 video stream time record */ public long getTime() { return frame + getFrameRate() * (second + 60L * minute + 3600L * hour); } /** * Changes the MPEG-1 video stream time record */ public void setTime(int hour, int minute, int second, int frame) { this.hour = hour; this.minute = minute; this.second = second; this.frame = frame; } /** * Returns true if the video parameters are constrained */ public boolean isConstrained() { return constrained; } /** * Enables or disables the video parameters constrains */ public void setConstrained(boolean constrained) { this.constrained = constrained; } /** * Returns true if the group of pictures drops frames */ public boolean isDropped() { return dropped; } /** * Changes the dropped flag of the group of pictures */ public void setDropped(boolean dropped) { this.dropped = dropped; } /** * Returns true if the group of pictures is closed */ public boolean isClosed() { return closed; } /** * Changes the closed flag of the group of pictures */ public void setClosed(boolean closed) { this.closed = closed; } /** * Returns true if there is a broken link */ public boolean isBroken() { return broken; } /** * Changes the broken flag of the group of pictures */ public void setBroken(boolean broken) { this.broken = broken; } /** * Returns the MPEG-1 video picture dimensions */ public int getWidth() { return picture.getWidth(); } public int getHeight() { return picture.getHeight(); } public int getStride() { return picture.getStride(); } /** * Parses the next frame of the MPEG-1 video stream */ public int[] getFrame() throws IOException { while (stream.showCode() != BitInputStream.SEQ_END_CODE) { switch (stream.getCode()) { case BitInputStream.SEQ_START_CODE: getSequenceHeader(stream); break; case BitInputStream.GOP_START_CODE: getGroupPictures(stream); break; case BitInputStream.PIC_START_CODE: return getPictureFrame(stream); case BitInputStream.USER_START_CODE: case BitInputStream.EXT_START_CODE: break; default: // throw new IOException("Unknown MPEG-1 video layer start code"); break; } } if (frameBuffer != picture.getLastFrame()) { frameBuffer = picture.getLastFrame(); return frameBuffer; } return null; } /** * Parses the sequence header from the MPEG-1 video stream */ private void getSequenceHeader(VLCInputStream stream) throws IOException { /* read picture dimensions in pixels */ int width = stream.getBits(12); int height = stream.getBits(12); int aspectRatio = stream.getBits(4); /* changes the MPEG-1 picture dimension */ if (picture.getWidth() == 0 && picture.getHeight() == 0) picture.setSize(width, height); /* read picture and bit rates */ setFrameRate(frameRateTable[stream.getBits(4)]); setBitRate(400 * stream.getBits(18)); stream.getBits(1); /* read VBV buffer size */ setBufferSize(stream.getBits(10)); /* read constrained parameters flag */ setConstrained(stream.getBits(1) != 0); /* read quantization matrix for intra coded blocks */ int intraMatrix[] = picture.getMacroblock().getIntraMatrix(); if (stream.getBits(1) != 0) { for (int i = 0; i < 64; i++) intraMatrix[i] = stream.getBits(8); } /* read quantization matrix for inter coded blocks */ int interMatrix[] = picture.getMacroblock().getInterMatrix(); if (stream.getBits(1) != 0) { for (int i = 0; i < 64; i++) interMatrix[i] = stream.getBits(8); } } /** * Parses group of pictures header from the MPEG-1 video stream */ private void getGroupPictures(VLCInputStream stream) throws IOException { /* read the drop frame flag */ setDropped(stream.getBits(1) != 0); /* read the time record */ int hour = stream.getBits(5); int minute = stream.getBits(6); int marker = stream.getBits(1); int second = stream.getBits(6); int frame = stream.getBits(6); setTime(hour, minute, second, frame); /* read closed and broken link flags */ setClosed(stream.getBits(1) != 0); setBroken(stream.getBits(1) != 0); } /** * Parses the next picture from the MPEG-1 video stream */ private int[] getPictureFrame(VLCInputStream stream) throws IOException { return picture.getFrame(stream); }}/** * The MPEG-1 video stream decoder applet that is intended to run * embedded inside of a Web page or another application. */public class MPEGPlayer extends Applet implements Runnable { /** * The MPEG-1 video input stream */ private MPEGVideoStream stream; /** * The picture frame buffer */ private int pixels[], width, height, stride; /** * The memory image color model */ private DirectColorModel model = null; /** * The memory image source */ private MemoryImageSource source = null; /** * The memory image object */ private Image image = null; /** * The applet's execution thread */ private Thread kicker = null; /** * The video stream location */ private URL url = null; /** * The repeat boolean parameter */ private boolean repeat = true; /** * Applet information */ public String getAppletInfo() { return "MPEGPlayer 0.9 (15 Apr 1998), Carlos Hasan (chasan@dcc.uchile.cl)"; } /** * Parameters information */ public String[][] getParameterInfo() { String info[][] = { { "source", "URL", "MPEG-1 video stream location" }, { "repeat", "boolean", "repeat the video sequence" } }; return info; } /** * Applet initialization */ public void init() { try { if (getParameter("source") != null) url = new URL(getDocumentBase(), getParameter("source")); if (getParameter("repeat") != null) repeat = getParameter("repeat").equalsIgnoreCase("true"); } catch (MalformedURLException exception) { showStatus("MPEG Exception: " + exception); } } /** * Start the execution of the applet */ public void start() { if (kicker == null && url != null) { kicker = new Thread(this); kicker.start(); } showStatus(getAppletInfo()); } /** * Stop the execution of the applet */ public void stop() { if (kicker != null && kicker.isAlive()) { kicker.stop(); } kicker = null; } /** * The applet main execution code */ public void run() { int frame[]; long time; try { do { stream = new MPEGVideoStream(url.openStream()); width = stream.getWidth(); height = stream.getHeight(); stride = stream.getStride(); resize(width, height); pixels = new int[stride * height]; model = new DirectColorModel(24, 0x0000ff, 0x00ff00, 0xff0000); time = System.currentTimeMillis(); while ((frame = readFrame()) != null) { drawFrame(frame, width, height, stride); source = new MemoryImageSource(width, height, model,pixels,0,stride); image = createImage(source); paint(getGraphics()); time += 1000L / stream.getFrameRate(); try { Thread.sleep(Math.max(time - System.currentTimeMillis(), 0)); } catch (InterruptedException exception) { } image.flush(); } } while (repeat); } catch (IOException exception) { showStatus("MPEG I/O Exception: " + exception); } } /** * Paint the current frame */ public void paint(Graphics graphics) { if (image != null) graphics.drawImage(image, 0, 0, null); } /** * Reads the next MPEG-1 video frame */ private int[] readFrame() { while (true) { try { return stream.getFrame(); } catch (Exception exception) { showStatus("MPEG Exception: " + exception); } } } /** * Draws the current MPEG-1 video frame */ private void drawFrame(int frame[], int width, int height, int stride) { int offset = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int YCbCr = frame[offset]; int Y = 512 + ((YCbCr >> 0) & 0xff); int Cb = cbtable[(YCbCr >> 8) & 0xff]; int Cr = crtable[(YCbCr >> 16) & 0xff]; pixels[offset++] = (clip[Y + (Cr >> 16)] << 0) + (clip[Y + (((Cb + Cr) << 16) >> 16)] << 8) + (clip[Y + (Cb >> 16)] << 16); } offset += stride - width; } } /** * Color conversion lookup tables */ private static int clip[], cbtable[], crtable[]; static { clip = new int[1024]; cbtable = new int[256]; crtable = new int[256]; for (int i = 0; i < 1024; i++) { clip[i] = Math.min(Math.max(i - 512, 0), 255); } for (int i = 0; i < 256; i++) { int level = i - 128; cbtable[i] = (((int)(1.77200 * level)) << 16) - ((int)(0.34414 * level)); crtable[i] = (((int)(1.40200 * level)) << 16) - ((int)(0.71414 * level)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -