?? video.cpp
字號:
/* * video.c -- * * This file contains C code that implements the video decoder model. * *//* * Copyright (c) 1995 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. *//* * Portions of this software Copyright (c) 1995 Brown University. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement * is hereby granted, provided that the above copyright notice and the * following two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. *//* We use FULL_COLOR_DITHER code for SMPEG */#define DISABLE_DITHER/* Correct bad motion information */#define LOOSE_MPEG/* If hardware accelerated, prevent use of dither code */#ifdef USE_ATI#ifndef DISABLE_DITHER#define DISABLE_DITHER#endif#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include "decoders.h"#include "video.h"#include "util.h"#include "proto.h"#ifdef USE_ATI#include "vhar128.h"#endif/* Declarations of functions. */#ifndef USE_ATIstatic void ReconIMBlock( VidStream*, int bnum );static void ReconPMBlock( VidStream*, int bnum, int recon_right_for, int recon_down_for, int zflag );static void ReconBMBlock( VidStream*, int bnum, int recon_right_back, int recon_down_back, int zflag );static void ReconBiMBlock( VidStream*, int bnum, int recon_right_for, int recon_down_for, int recon_right_back, int recon_down_back, int zflag );static void ReconSkippedBlock( unsigned char *source, unsigned char *dest, int row, int col, int row_size, int right, int down, int right_half, int down_half, int width );#endif /* USE_ATI */static void DoPictureDisplay( VidStream* );static int ParseSeqHead( VidStream* );static int ParseGOP( VidStream* );static int ParsePicture( VidStream*, TimeStamp );static int ParseSlice( VidStream* );static int ParseMacroBlock( VidStream* );static void ProcessSkippedPFrameMBlocks( VidStream* );static void ProcessSkippedBFrameMBlocks( VidStream* );/* Changes to make the code reentrant: de-globalized: totNumFrames, realTimeStart, matched_depth, ditherType, curBits, ReconPMBlock statics, first, [lc]max[xy], ditherFlags, vid_stream, Parse_done, seekValue, ReadPack static, sys_layer, bitOffset, bitLength, bitBuffer, curVidStream, X globals to xinfo (window, et al) use vid_stream->film_has_ended instead of FilmState lookup tables only initialized once, global as possible (default_intra_matrix, zigzag, zigzag_direct, scan) get rid of setjmp, long jmp Additional changes: if DISABLE_DITHER defined then do not compile dithering code -lsh@cs.brown.edu (Loring Holden) *//* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */#define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))/* Set up array for fast conversion from zig zag order to row/column coordinates.*/const int zigzag[64][2] ={ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 }, { 2, 0 }, { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 }, { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 }, { 5, 0 }, { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 }, { 0, 5 }, { 0, 6 }, { 1, 5 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 5, 1 }, { 6, 0 }, { 7, 0 }, { 6, 1 }, { 5, 2 }, { 4, 3 }, { 3, 4 }, { 2, 5 }, { 1, 6 }, { 0, 7 }, { 1, 7 }, { 2, 6 }, { 3, 5 }, { 4, 4 }, { 5, 3 }, { 6, 2 }, { 7, 1 }, { 7, 2 }, { 6, 3 }, { 5, 4 }, { 4, 5 }, { 3, 6 }, { 2, 7 }, { 3, 7 }, { 4, 6 }, { 5, 5 }, { 6, 4 }, { 7, 3 }, { 7, 4 }, { 6, 5 }, { 5, 6 }, { 4, 7 }, { 5, 7 }, { 6, 6 }, { 7, 5 }, { 7, 6 }, { 6, 7 }, { 7, 7 }};/* Set up array for fast conversion from row/column coordinates to zig zag order.*/const int scan[8][8] ={ { 0, 1, 5, 6, 14, 15, 27, 28 }, { 2, 4, 7, 13, 16, 26, 29, 42 }, { 3, 8, 12, 17, 25, 30, 41, 43 }, { 9, 11, 18, 24, 31, 40, 44, 53 }, { 10, 19, 23, 32, 39, 45, 52, 54 }, { 20, 22, 33, 38, 46, 51, 55, 60 }, { 21, 34, 37, 47, 50, 56, 59, 61 }, { 35, 36, 48, 49, 57, 58, 62, 63 }};/* Max lum, chrom indices for illegal block checking. */#ifdef USE_CROP_TABLE/* * We use a lookup table to make sure values stay in the 0..255 range. * Since this is cropping (ie, x = (x < 0)?0:(x>255)?255:x; ), wee call this * table the "crop table". * MAX_NEG_CROP is the maximum neg/pos value we can handle. */#define MAX_NEG_CROP 2048#define NUM_CROP_ENTRIES (2048+2*MAX_NEG_CROP)static unsigned char cropTbl[NUM_CROP_ENTRIES];#define crop(x) cm[x]#elsestatic inline unsigned char crop(int x){ if(x<=0) return 0; if(x>=255) return 255; return x;}#endif /* USE_CROP_TABLE *//* The following accounts for time and size spent in various parsing acitivites if ANALYSIS has been defined.*/#ifdef ANALYSISunsigned int bitCount = 0;/* #define SHOWMB_FLAG *//* #define SHOWEACHFLAG */typedef struct { int frametype; unsigned int totsize; unsigned int number; unsigned int i_mbsize; unsigned int p_mbsize; unsigned int b_mbsize; unsigned int bi_mbsize; unsigned int i_mbnum; unsigned int p_mbnum; unsigned int b_mbnum; unsigned int bi_mbnum; unsigned int i_mbcbp[64]; unsigned int p_mbcbp[64]; unsigned int b_mbcbp[64]; unsigned int bi_mbcbp[64]; unsigned int i_mbcoeff[64]; unsigned int p_mbcoeff[64]; unsigned int b_mbcoeff[64]; unsigned int bi_mbcoeff[64]; double tottime;} Statval;Statval stat_a[4];unsigned int pictureSizeCount;unsigned int mbSizeCount;unsigned int *mbCBPPtr, *mbCoeffPtr, *mbSizePtr;unsigned int cacheHit[8][8];unsigned int cacheMiss[8][8];static voidinit_stat_struct(astat) Statval *astat;{ int j; astat->frametype = 0; astat->totsize = 0; astat->number = 0; astat->i_mbsize = 0; astat->p_mbsize = 0; astat->b_mbsize = 0; astat->bi_mbsize = 0; astat->i_mbnum = 0; astat->p_mbnum = 0; astat->b_mbnum = 0; astat->bi_mbnum = 0; for (j = 0; j < 64; j++) { astat->i_mbcbp[j] = 0; astat->p_mbcbp[j] = 0; astat->b_mbcbp[j] = 0; astat->bi_mbcbp[j] = 0; astat->i_mbcoeff[j] = 0; astat->p_mbcoeff[j] = 0; astat->b_mbcoeff[j] = 0; astat->bi_mbcoeff[j] = 0; } astat->tottime = 0.0;}voidinit_stats(){ int i, j; for (i = 0; i < 4; i++) { init_stat_struct(&(stat_a[i])); stat_a[i].frametype = i; } for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { cacheHit[i][j] = 0; cacheMiss[i][j] = 0; } } bitCount = 0;}static voidPrintOneStat(){ int i; printf("\n"); switch (stat_a[0].frametype) { case I_TYPE: printf("I FRAME\n"); break; case P_TYPE: printf("P FRAME\n"); break; case B_TYPE: printf("B FRAME\n"); break; } printf("Size: %d bytes + %d bits\n", stat_a[0].totsize / 8, stat_a[0].totsize % 8); if (stat_a[0].i_mbnum > 0) { printf("\tI Macro Block Stats:\n"); printf("\t%d I Macroblocks\n", stat_a[0].i_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[0].i_mbsize / (8 * stat_a[0].i_mbnum), (stat_a[0].i_mbsize * stat_a[0].i_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].i_mbcbp[i], stat_a[0].i_mbcbp[i + 1], stat_a[0].i_mbcbp[i + 2], stat_a[0].i_mbcbp[i + 3], stat_a[0].i_mbcbp[i + 4], stat_a[0].i_mbcbp[i + 5], stat_a[0].i_mbcbp[i + 6], stat_a[0].i_mbcbp[i + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].i_mbcoeff[i], stat_a[0].i_mbcoeff[i + 1], stat_a[0].i_mbcoeff[i + 2], stat_a[0].i_mbcoeff[i + 3], stat_a[0].i_mbcoeff[i + 4], stat_a[0].i_mbcoeff[i + 5], stat_a[0].i_mbcoeff[i + 6], stat_a[0].i_mbcoeff[i + 7]); } } if (stat_a[0].p_mbnum > 0) { printf("\tP Macro Block Stats:\n"); printf("\t%d P Macroblocks\n", stat_a[0].p_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[0].p_mbsize / (8 * stat_a[0].p_mbnum), (stat_a[0].p_mbsize / stat_a[0].p_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].p_mbcbp[i], stat_a[0].p_mbcbp[i + 1], stat_a[0].p_mbcbp[i + 2], stat_a[0].p_mbcbp[i + 3], stat_a[0].p_mbcbp[i + 4], stat_a[0].p_mbcbp[i + 5], stat_a[0].p_mbcbp[i + 6], stat_a[0].p_mbcbp[i + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].p_mbcoeff[i], stat_a[0].p_mbcoeff[i + 1], stat_a[0].p_mbcoeff[i + 2], stat_a[0].p_mbcoeff[i + 3], stat_a[0].p_mbcoeff[i + 4], stat_a[0].p_mbcoeff[i + 5], stat_a[0].p_mbcoeff[i + 6], stat_a[0].p_mbcoeff[i + 7]); } } if (stat_a[0].b_mbnum > 0) { printf("\tB Macro Block Stats:\n"); printf("\t%d B Macroblocks\n", stat_a[0].b_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[0].b_mbsize / (8 * stat_a[0].b_mbnum), (stat_a[0].b_mbsize / stat_a[0].b_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].b_mbcbp[i], stat_a[0].b_mbcbp[i + 1], stat_a[0].b_mbcbp[i + 2], stat_a[0].b_mbcbp[i + 3], stat_a[0].b_mbcbp[i + 4], stat_a[0].b_mbcbp[i + 5], stat_a[0].b_mbcbp[i + 6], stat_a[0].b_mbcbp[i + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].b_mbcoeff[i], stat_a[0].b_mbcoeff[i + 1], stat_a[0].b_mbcoeff[i + 2], stat_a[0].b_mbcoeff[i + 3], stat_a[0].b_mbcoeff[i + 4], stat_a[0].b_mbcoeff[i + 5], stat_a[0].b_mbcoeff[i + 6], stat_a[0].b_mbcoeff[i + 7]); } } if (stat_a[0].bi_mbnum > 0) { printf("\tBi Macro Block Stats:\n"); printf("\t%d Bi Macroblocks\n", stat_a[0].bi_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[0].bi_mbsize / (8 * stat_a[0].bi_mbnum), (stat_a[0].bi_mbsize * stat_a[0].bi_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].bi_mbcbp[i], stat_a[0].bi_mbcbp[i + 1], stat_a[0].bi_mbcbp[i + 2], stat_a[0].bi_mbcbp[i + 3], stat_a[0].bi_mbcbp[i + 4], stat_a[0].bi_mbcbp[i + 5], stat_a[0].bi_mbcbp[i + 6], stat_a[0].bi_mbcbp[i + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (i = 0; i < 64; i += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].bi_mbcoeff[i], stat_a[0].bi_mbcoeff[i + 1], stat_a[0].bi_mbcoeff[i + 2], stat_a[0].bi_mbcoeff[i + 3], stat_a[0].bi_mbcoeff[i + 4], stat_a[0].bi_mbcoeff[i + 5], stat_a[0].bi_mbcoeff[i + 6], stat_a[0].bi_mbcoeff[i + 7]); } } printf("\nTime to Decode: %g secs.\n", stat_a[0].tottime); printf("****************\n");}voidPrintAllStats(vid_stream)VidStream *vid_stream;{ int i, j; unsigned int supertot, supernum; double supertime; printf("\n"); printf("General Info: \n"); printf("Width: %d\nHeight: %d\n", vid_stream->mb_width * 16, vid_stream->mb_height * 16); for (i = 1; i < 4; i++) { if (stat_a[i].number == 0) continue; switch (i) { case 1: printf("I FRAMES\n"); break; case 2: printf("P FRAMES\n"); break; case 3: printf("B FRAMES\n"); break; } printf("Number: %d\n", stat_a[i].number); printf("Avg. Size: %d bytes + %d bits\n", stat_a[i].totsize / (8 * stat_a[i].number), (stat_a[i].totsize / stat_a[i].number) % 8); if (stat_a[i].i_mbnum > 0) { printf("\tI Macro Block Stats:\n"); printf("\t%d I Macroblocks\n", stat_a[i].i_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[i].i_mbsize / (8 * stat_a[i].i_mbnum), (stat_a[i].i_mbsize / stat_a[i].i_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (j = 0; j < 64; j += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].i_mbcbp[j], stat_a[i].i_mbcbp[j + 1], stat_a[i].i_mbcbp[j + 2], stat_a[i].i_mbcbp[j + 3], stat_a[i].i_mbcbp[j + 4], stat_a[i].i_mbcbp[j + 5], stat_a[i].i_mbcbp[j + 6], stat_a[i].i_mbcbp[j + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (j = 0; j < 64; j += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].i_mbcoeff[j], stat_a[i].i_mbcoeff[j + 1], stat_a[i].i_mbcoeff[j + 2], stat_a[i].i_mbcoeff[j + 3], stat_a[i].i_mbcoeff[j + 4], stat_a[i].i_mbcoeff[j + 5], stat_a[i].i_mbcoeff[j + 6], stat_a[i].i_mbcoeff[j + 7]); } } if (stat_a[i].p_mbnum > 0) { printf("\tP Macro Block Stats:\n"); printf("\t%d P Macroblocks\n", stat_a[i].p_mbnum); printf("\tAvg. Size: %d bytes + %d bits\n", stat_a[i].p_mbsize / (8 * stat_a[i].p_mbnum), (stat_a[i].p_mbsize / stat_a[i].p_mbnum) % 8); printf("\t\tCoded Block Pattern Histogram:\n"); for (j = 0; j < 64; j += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].p_mbcbp[j], stat_a[i].p_mbcbp[j + 1], stat_a[i].p_mbcbp[j + 2], stat_a[i].p_mbcbp[j + 3], stat_a[i].p_mbcbp[j + 4], stat_a[i].p_mbcbp[j + 5], stat_a[i].p_mbcbp[j + 6], stat_a[i].p_mbcbp[j + 7]); } printf("\n\t\tNumber of Coefficients/Block Histogram:\n"); for (j = 0; j < 64; j += 8) { printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].p_mbcoeff[j], stat_a[i].p_mbcoeff[j + 1], stat_a[i].p_mbcoeff[j + 2], stat_a[i].p_mbcoeff[j + 3], stat_a[i].p_mbcoeff[j + 4], stat_a[i].p_mbcoeff[j + 5], stat_a[i].p_mbcoeff[j + 6],
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -