#include <stdlib.h>
#include <string.h>
#include "state.h"
#include "enquant.h"
#include "huffenc.h"
static void oc_pack_octets(oggpack_buffer *_opb,const char *_buf,int _len){
int i;
for(i=0;i<_len;i++)oggpackB_write(_opb,_buf[i],8);
}
int oc_state_flushheader(oc_theora_state *_state,int *_packet_state,
oggpack_buffer *_opb,const th_quant_info *_qinfo,
const th_huff_code _codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS],
const char *_vendor,th_comment *_tc,ogg_packet *_op){
unsigned char *packet;
int b_o_s;
if(_op==NULL)return TH_EFAULT;
switch(*_packet_state){
case OC_PACKET_INFO_HDR:{
if(_state==NULL)return TH_EFAULT;
oggpackB_reset(_opb);
oggpackB_write(_opb,0x80,8);
oc_pack_octets(_opb,"theora",6);
oggpackB_write(_opb,TH_VERSION_MAJOR,8);
oggpackB_write(_opb,TH_VERSION_MINOR,8);
oggpackB_write(_opb,TH_VERSION_SUB,8);
oggpackB_write(_opb,_state->info.frame_width>>4,16);
oggpackB_write(_opb,_state->info.frame_height>>4,16);
oggpackB_write(_opb,_state->info.pic_width,24);
oggpackB_write(_opb,_state->info.pic_height,24);
oggpackB_write(_opb,_state->info.pic_x,8);
oggpackB_write(_opb,_state->info.pic_y,8);
oggpackB_write(_opb,_state->info.fps_numerator,32);
oggpackB_write(_opb,_state->info.fps_denominator,32);
oggpackB_write(_opb,_state->info.aspect_numerator,24);
oggpackB_write(_opb,_state->info.aspect_denominator,24);
oggpackB_write(_opb,_state->info.colorspace,8);
oggpackB_write(_opb,_state->info.target_bitrate,24);
oggpackB_write(_opb,_state->info.quality,6);
oggpackB_write(_opb,_state->info.keyframe_granule_shift,5);
oggpackB_write(_opb,_state->info.pixel_fmt,2);
oggpackB_write(_opb,0,3);
b_o_s=1;
}break;
case OC_PACKET_COMMENT_HDR:{
int vendor_len;
int i;
if(_tc==NULL)return TH_EFAULT;
vendor_len=strlen(_vendor);
oggpackB_reset(_opb);
oggpackB_write(_opb,0x81,8);
oc_pack_octets(_opb,"theora",6);
oggpack_write(_opb,vendor_len,32);
oc_pack_octets(_opb,_vendor,vendor_len);
oggpack_write(_opb,_tc->comments,32);
for(i=0;i<_tc->comments;i++){
if(_tc->user_comments[i]!=NULL){
oggpack_write(_opb,_tc->comment_lengths[i],32);
oc_pack_octets(_opb,_tc->user_comments[i],_tc->comment_lengths[i]);
}
else oggpack_write(_opb,0,32);
}
b_o_s=0;
}break;
case OC_PACKET_SETUP_HDR:{
int ret;
oggpackB_reset(_opb);
oggpackB_write(_opb,0x82,8);
oc_pack_octets(_opb,"theora",6);
oc_quant_params_pack(_opb,_qinfo);
ret=oc_huff_codes_pack(_opb,_codes);
if(ret<0)return ret;
b_o_s=0;
}break;
default:return 0;
}
packet=oggpackB_get_buffer(_opb);
if(packet==NULL)return TH_EFAULT;
_op->packet=packet;
_op->bytes=oggpackB_bytes(_opb);
_op->b_o_s=b_o_s;
_op->e_o_s=0;
_op->granulepos=0;
_op->packetno=*_packet_state+3;
return ++(*_packet_state)+3;
}