BOOL LoadFrameHeader(PB_INSTANCE *pbi) { UINT8 VersionByte0; // Must be 0 for VP30b and later UINT8 DctQMask; UINT8 SpareBits; // Spare cfg bits UINT8 Unused; BOOL RetVal = TRUE; // Is the frame and inter frame or a key frame pbi->FrameType = bitread1(&pbi->br); // unused bit Unused = bitread1(&pbi->br); // Quality (Q) index DctQMask = (UINT8)bitread( &pbi->br, 6 ); // If the frame was a base frame then read the frame dimensions and build a bitmap structure. if ( (pbi->FrameType == BASE_FRAME) ) { // Read the frame dimensions bytes (0,0 indicates vp31 or later) VersionByte0 = (UINT8)bitread( &pbi->br, 8 ); pbi->Vp3VersionNo = (UINT8)bitread( &pbi->br, 5 ); if(pbi->Vp3VersionNo > CURRENT_DECODE_VERSION) { RetVal = FALSE; return RetVal; } // Initialise version specific quantiser values InitQTables( pbi ); // Read the type / coding method for the key frame. pbi->KeyFrameType = (UINT8)bitread( &pbi->br, 1 ); SpareBits = (UINT8)bitread( &pbi->br, 2 ); // Select huffman tables SelectHuffmanSet( pbi ); } // Set this frame quality value from Q Index pbi->ThisFrameQualityValue = pbi->QThreshTable[DctQMask]; return RetVal; }
int theora_encode_init(theora_state *th, theora_info *c){ int i; CP_INSTANCE *cpi; memset(th, 0, sizeof(*th)); /*Currently only the 4:2:0 format is supported.*/ if(c->pixelformat!=OC_PF_420)return OC_IMPL; th->internal_encode=cpi=_ogg_calloc(1,sizeof(*cpi)); dsp_static_init (&cpi->dsp); memcpy (&cpi->pb.dsp, &cpi->dsp, sizeof(DspFunctions)); c->version_major=VERSION_MAJOR; c->version_minor=VERSION_MINOR; c->version_subminor=VERSION_SUB; InitTmpBuffers(&cpi->pb); InitPPInstance(&cpi->pp, &cpi->dsp); /* Initialise Configuration structure to legal values */ if(c->quality>63)c->quality=63; if(c->quality<0)c->quality=32; if(c->target_bitrate<0)c->target_bitrate=0; /* we clamp target_bitrate to 24 bits after setting up the encoder */ cpi->Configuration.BaseQ = c->quality; cpi->Configuration.FirstFrameQ = c->quality; cpi->Configuration.MaxQ = c->quality; cpi->Configuration.ActiveMaxQ = c->quality; cpi->MVChangeFactor = 14; cpi->FourMvChangeFactor = 8; cpi->MinImprovementForNewMV = 25; cpi->ExhaustiveSearchThresh = 2500; cpi->MinImprovementForFourMV = 100; cpi->FourMVThreshold = 10000; cpi->BitRateCapFactor = 1.50; cpi->InterTripOutThresh = 5000; cpi->MVEnabled = 1; cpi->InterCodeCount = 127; cpi->BpbCorrectionFactor = 1.0; cpi->GoldenFrameEnabled = 1; cpi->InterPrediction = 1; cpi->MotionCompensation = 1; cpi->ThreshMapThreshold = 5; cpi->MaxConsDroppedFrames = 1; /* Set encoder flags. */ /* if not AutoKeyframing cpi->ForceKeyFrameEvery = is frequency */ if(!c->keyframe_auto_p) c->keyframe_frequency_force = c->keyframe_frequency; /* Set the frame rate variables. */ if ( c->fps_numerator < 1 ) c->fps_numerator = 1; if ( c->fps_denominator < 1 ) c->fps_denominator = 1; /* don't go too nuts on keyframe spacing; impose a high limit to make certain the granulepos encoding strategy works */ if(c->keyframe_frequency_force>32768)c->keyframe_frequency_force=32768; if(c->keyframe_mindistance>32768)c->keyframe_mindistance=32768; if(c->keyframe_mindistance>c->keyframe_frequency_force) c->keyframe_mindistance=c->keyframe_frequency_force; cpi->pb.keyframe_granule_shift=_ilog(c->keyframe_frequency_force-1); /* clamp the target_bitrate to a maximum of 24 bits so we get a more meaningful value when we write this out in the header. */ if(c->target_bitrate>(1<<24)-1)c->target_bitrate=(1<<24)-1; /* copy in config */ memcpy(&cpi->pb.info,c,sizeof(*c)); th->i=&cpi->pb.info; th->granulepos=-1; /* Set up default values for QTargetModifier[Q_TABLE_SIZE] table */ for ( i = 0; i < Q_TABLE_SIZE; i++ ) cpi->QTargetModifier[i] = 1.0; /* Set up an encode buffer */ #ifndef LIBOGG2 cpi->oggbuffer = _ogg_malloc(sizeof(oggpack_buffer)); oggpackB_writeinit(cpi->oggbuffer); #else cpi->oggbuffer = _ogg_malloc(oggpack_buffersize()); cpi->oggbufferstate = ogg_buffer_create(); oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate); #endif /* Set data rate related variables. */ cpi->Configuration.TargetBandwidth = (c->target_bitrate) / 8; cpi->Configuration.OutputFrameRate = (double)( c->fps_numerator / c->fps_denominator ); cpi->frame_target_rate = cpi->Configuration.TargetBandwidth / cpi->Configuration.OutputFrameRate; /* Set key frame data rate target; this is nominal keyframe size */ cpi->Configuration.KeyFrameDataTarget = (c->keyframe_data_target_bitrate * c->fps_denominator / c->fps_numerator ) / 8; /* Note the height and width in the pre-processor control structure. */ cpi->ScanConfig.VideoFrameHeight = cpi->pb.info.height; cpi->ScanConfig.VideoFrameWidth = cpi->pb.info.width; InitFrameDetails(&cpi->pb); InitFilterTables(&cpi->pb); EInitFragmentInfo(cpi); EInitFrameInfo(cpi); /* Set up pre-processor config pointers. */ cpi->ScanConfig.Yuv0ptr = cpi->yuv0ptr; cpi->ScanConfig.Yuv1ptr = cpi->yuv1ptr; cpi->ScanConfig.SrfWorkSpcPtr = cpi->ConvDestBuffer; cpi->ScanConfig.disp_fragments = cpi->pb.display_fragments; cpi->ScanConfig.RegionIndex = cpi->pb.pixel_index_table; /* Initialise the pre-processor module. */ ScanYUVInit(&cpi->pp, &(cpi->ScanConfig)); /* Initialise Motion compensation */ InitMotionCompensation(cpi); /* Initialise the compression process. */ /* We always start at frame 1 */ cpi->CurrentFrame = 1; /* Reset the rate targeting correction factor. */ cpi->BpbCorrectionFactor = 1.0; cpi->TotalByteCount = 0; cpi->TotalMotionScore = 0; /* Up regulation variables. */ cpi->FinalPassLastPos = 0; /* Used to regulate a final unrestricted pass. */ cpi->LastEndSB = 0; /* Where we were in the loop last time. */ cpi->ResidueLastEndSB = 0; /* Where we were in the residue update loop last time. */ InitHuffmanSet(&cpi->pb); /* This makes sure encoder version specific tables are initialised */ InitQTables(&cpi->pb); /* Indicate that the next frame to be compressed is the first in the current clip. */ cpi->ThisIsFirstFrame = 1; cpi->readyflag = 1; return 0; }