/* build the comment header packet from the passed metadata */ int theora_encode_comment(theora_comment *tc, ogg_packet *op) { const char *vendor = theora_version_string(); const int vendor_length = strlen(vendor); oggpack_buffer *opb; #ifndef LIBOGG2 opb = _ogg_malloc(sizeof(oggpack_buffer)); oggpackB_writeinit(opb); #else opb = _ogg_malloc(oggpack_buffersize()); oggpackB_writeinit(opb, ogg_buffer_create()); #endif oggpackB_write(opb, 0x81, 8); _tp_writebuffer(opb, "theora", 6); _tp_writelsbint(opb, vendor_length); _tp_writebuffer(opb, vendor, vendor_length); _tp_writelsbint(opb, tc->comments); if(tc->comments){ int i; for(i=0;i<tc->comments;i++){ if(tc->user_comments[i]){ _tp_writelsbint(opb,tc->comment_lengths[i]); _tp_writebuffer(opb,tc->user_comments[i],tc->comment_lengths[i]); }else{ oggpackB_write(opb,0,32); } } } op->bytes=oggpack_bytes(opb); #ifndef LIBOGG2 /* So we're expecting the application will free this? */ op->packet=_ogg_malloc(oggpack_bytes(opb)); memcpy(op->packet, oggpack_get_buffer(opb), oggpack_bytes(opb)); oggpack_writeclear(opb); #else op->packet = oggpack_writebuffer(opb); /* When the application puts op->packet into a stream_state object, it becomes the property of libogg2's internal memory management. */ #endif _ogg_free(opb); op->b_o_s=0; op->e_o_s=0; op->packetno=0; op->granulepos=0; return (0); }
void InitPBInstance(PB_INSTANCE *pbi){ /* initialize whole structure to 0 */ memset(pbi, 0, sizeof(*pbi)); InitTmpBuffers(pbi); /* allocate memory for the oggpack_buffer */ #ifndef LIBOGG2 pbi->opb = _ogg_malloc(sizeof(oggpack_buffer)); #else pbi->opb = _ogg_malloc(oggpack_buffersize()); #endif /* variables needing initialization (not being set to 0) */ pbi->ModifierPointer[0] = &pbi->Modifier[0][255]; pbi->ModifierPointer[1] = &pbi->Modifier[1][255]; pbi->ModifierPointer[2] = &pbi->Modifier[2][255]; pbi->ModifierPointer[3] = &pbi->Modifier[3][255]; pbi->DecoderErrorCode = 0; pbi->KeyFrameType = DCT_KEY_FRAME; pbi->FramesHaveBeenSkipped = 0; }
int theora_decode_header(theora_info *ci, theora_comment *cc, ogg_packet *op){ long ret; oggpack_buffer *opb; if(!op)return OC_BADHEADER; #ifndef LIBOGG2 opb = _ogg_malloc(sizeof(oggpack_buffer)); oggpackB_readinit(opb,op->packet,op->bytes); #else opb = _ogg_malloc(oggpack_buffersize()); oggpackB_readinit(opb,op->packet); #endif { char id[6]; int typeflag; theora_read(opb,8,&ret); typeflag = ret; if(!(typeflag&0x80)) { free(opb); return(OC_NOTFORMAT); } _tp_readbuffer(opb,id,6); if(memcmp(id,"theora",6)) { free(opb); return(OC_NOTFORMAT); } switch(typeflag){ case 0x80: if(!op->b_o_s){ /* Not the initial packet */ free(opb); return(OC_BADHEADER); } if(ci->version_major!=0){ /* previously initialized info header */ free(opb); return OC_BADHEADER; } ret = _theora_unpack_info(ci,opb); free(opb); return(ret); case 0x81: if(ci->version_major==0){ /* um... we didn't get the initial header */ free(opb); return(OC_BADHEADER); } ret = _theora_unpack_comment(cc,opb); free(opb); return(ret); case 0x82: if(ci->version_major==0 || cc->vendor==NULL){ /* um... we didn't get the initial header or comments yet */ free(opb); return(OC_BADHEADER); } ret = _theora_unpack_tables(ci,opb); free(opb); return(ret); default: free(opb); if(ci->version_major==0 || cc->vendor==NULL || ((codec_setup_info *)ci->codec_setup)->HuffRoot[0]==NULL){ /* we haven't gotten the three required headers */ return(OC_BADHEADER); } /* ignore any trailing header packets for forward compatibility */ return(OC_NEWPACKET); } } /* I don't think it's possible to get this far, but better safe.. */ free(opb); return(OC_BADHEADER); }
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; }