static void esds_writeobjtype(BIT_STREAM_T *pStream, int objTypeIdx) { if(objTypeIdx >= 0x1f) { bits_write(pStream, 0x1f, 5); bits_write(pStream, objTypeIdx - 0x1f, 6); } else { bits_write(pStream, objTypeIdx, 5); } }
static void esds_writefreqidx(BIT_STREAM_T *pStream, int freqIdx) { if(freqIdx >= 0x0f) { bits_write(pStream, 0x0f, 4); bits_write(pStream, freqIdx - 0x0f, 24); } else { bits_write(pStream, freqIdx, 4); } }
int auds_write_pcm(auds_t *auds, __u8 *buffer, __u8 **data, int *size, int bsize, data_source_t *ds) { __u8 one[4]; int count=0; int bpos=0; __u8 *bp=buffer; int i,nodata=0; __s16 *resamp=NULL, *pr=NULL; int channels=2; if(auds) channels=auds->channels; bits_write(&bp,1,3,&bpos); // channel=1, stereo bits_write(&bp,0,4,&bpos); // unknown bits_write(&bp,0,8,&bpos); // unknown bits_write(&bp,0,4,&bpos); // unknown if(bsize!=4096) bits_write(&bp,1,1,&bpos); // hassize else bits_write(&bp,0,1,&bpos); // hassize bits_write(&bp,0,2,&bpos); // unused bits_write(&bp,1,1,&bpos); // is-not-compressed if(bsize!=4096){ bits_write(&bp,(bsize>>24)&0xff,8,&bpos); // size of data, integer, big endian bits_write(&bp,(bsize>>16)&0xff,8,&bpos); bits_write(&bp,(bsize>>8)&0xff,8,&bpos); bits_write(&bp,bsize&0xff,8,&bpos); }
bool field_set_value(field_t * field, void * value) { bool ret = false; if (field && value) { #ifdef USE_BITS switch (field->type) { case TYPE_BITS: ret = bits_write( field->value.bits.bits, field->value.bits.offset_in_bits, value, 0, field->value.bits.size_in_bits ); break; default: #endif memcpy(&field->value, value, field_get_size(field)); ret = true; #ifdef USE_BITS break; } #endif } return ret; }
field_t * field_create_bits(const char * key, const void * value, size_t offset_in_bits_in, size_t size_in_bits) { size_t num_bytes; field_t * field; if (!(field = malloc(sizeof(field_t)))) goto ERR_MALLOC; field->key = key; field->type = TYPE_BITS; num_bytes = (size_in_bits / 8) + (size_in_bits % 8 ? 1 : 0); if (!(field->value.bits.bits = malloc(num_bytes))) goto ERR_MALLOC2; field->value.bits.size_in_bits = size_in_bits; field->value.bits.offset_in_bits = (8 * num_bytes - size_in_bits); memset(field->value.bits.bits, 0, num_bytes); if (!bits_write(field->value.bits.bits, field->value.bits.offset_in_bits, value, offset_in_bits_in, size_in_bits)) { goto ERR_BITS_WRITE_BITS; } return field; ERR_MALLOC2: ERR_BITS_WRITE_BITS: free(field); ERR_MALLOC: return NULL; }
char * lzw_encode_run(lzw_dict *dict, char *seq, data *buffer) { int len; // Insert length 1 into dict lzw_dict_entry *curr = dict->head; // Find longest match in the dictionary while (curr != NULL) { if (strncmp(curr->seq, seq, curr->length) == 0) { len = curr->length + 1; break; } curr = curr->next; } // Exit if character at seq[0] not in initial dictionary. if (curr == NULL) { fprintf(stderr, "[FATAL] Character %c not in initial alphabet. Program will now terminate.", seq[0]); exit(-1); } // Add new entry to dict char *new_seq = malloc(len + 1); strncpy(new_seq, seq, len); new_seq[len] = '\0'; lzw_dict_add(dict, new_seq, -1); bits_write(buffer, curr->code, dict->bits); return seq + len - 1; }
data * lzw_encode(char *alphabet, char *code) { data *d = data_init(); lzw_dict *dict = lzw_dict_init(alphabet); while (*code != '\0') code = lzw_encode_run(dict, code, d); bits_write(d, 0, dict->bits); // Stop code = 0 lzw_dict_free(dict); return d; }
bool protocol_field_set(const protocol_field_t * protocol_field, uint8_t * segment, const field_t * field) { bool ret = true; uint8_t * segment_field = segment + protocol_field->offset; switch (protocol_field->type) { #ifdef USE_IPV4 case TYPE_IPV4: memcpy(segment_field, &field->value.ipv4, sizeof(ipv4_t)); break; #endif #ifdef USE_IPV6 case TYPE_IPV6: memcpy(segment_field, &field->value.ipv6, sizeof(ipv6_t)); break; #endif case TYPE_UINT8: *(uint8_t *) segment_field = field->value.int8; break; case TYPE_UINT16: *(uint16_t *) segment_field = htons(field->value.int16); break; case TYPE_UINT32: *(uint32_t *) segment_field = htonl(field->value.int32); break; #ifdef USE_BITS case TYPE_BITS: ret = bits_write( segment_field, protocol_field->offset_in_bits, &field->value.bits, 8 * sizeof(field->value.bits) - protocol_field->size_in_bits, protocol_field->size_in_bits ); break; #endif case TYPE_STRING: default: fprintf(stderr, "protocol_field_set: Type not supported"); ret = false; break; } return ret; }
static int h264_encodeSPSVUI_hrd(BIT_STREAM_T *pStream, H264_SPS_VUI_T *pVui) { int idx; bits_writeExpGolomb(pStream, pVui->hrd_cpb_count - 1); bits_write(pStream, pVui->hrd_bit_rate_scale, 4); bits_write(pStream, pVui->hrd_cpb_size_scale, 4); for(idx = 0; idx < pVui->hrd_cpb_count; idx++) { bits_writeExpGolomb(pStream, pVui->hrd_cpb[idx].hrd_bit_rate_val_min1); bits_writeExpGolomb(pStream, pVui->hrd_cpb[idx].hrd_cpb_size_val_min1); bits_write(pStream, pVui->hrd_cpb[idx].hrd_cbr_flag, 1); } bits_write(pStream, pVui->hrd_initial_cpb_removal_delay_len-1, 5); bits_write(pStream, pVui->hrd_cpb_removal_delay_len-1, 5); bits_write(pStream, pVui->hrd_dpb_output_delay_len-1, 5); bits_write(pStream, pVui->hrd_time_offset_len, 5); return H264_RC_OK; }
int esds_encodeDecoderSp(unsigned char *pBuf, unsigned int len, const ESDS_DECODER_CFG_T *pSp) { BIT_STREAM_T stream; if(!pBuf || len < 2) { return -1; } memset(&stream, 0, sizeof(stream)); memset(pBuf, 0, len > 8 ? 8 : len); stream.sz = len; stream.buf = pBuf; //0x12 0x10 esds_writeobjtype(&stream, pSp->objTypeIdx); esds_writefreqidx(&stream, pSp->freqIdx); bits_write(&stream, pSp->channelIdx, 4); if(pSp->frameDeltaHz == 960) { bits_write(&stream, 1, 1); } else { bits_write(&stream, 0, 1); } bits_write(&stream, pSp->dependsOnCoreCoderFlag, 1); bits_write(&stream, pSp->extFlag, 1); if(pSp->haveExtTypeIdx) { if(len < 6) { return -1; } bits_write(&stream, 0x2b7, 11); esds_writeobjtype(&stream, pSp->extObjTypeIdx); if(pSp->extObjTypeIdx == 5 && pSp->haveExtFreqIdx) { bits_write(&stream, 1, 1); esds_writefreqidx(&stream, pSp->extFreqIdx); } } BIT_STREAM_ENDOF_BYTE((&stream)); return stream.byteIdx; }
// TODO factorize with field_create field_t * field_create_bits(const char * key, const void * value, size_t offset_in_bits_in, size_t size_in_bits) { field_t * field; size_t offset_in_bits_out; if (!(field = malloc(sizeof(field_t)))) goto ERR_MALLOC; field->key = key; field->type = TYPE_BITS; memset(&field->value.bits, 0, sizeof(field->value.bits)); offset_in_bits_out = 8 * sizeof(field->value.bits) - size_in_bits; if (!bits_write(&field->value.bits, offset_in_bits_out, value, offset_in_bits_in, size_in_bits)) { goto ERR_BITS_WRITE_BITS; } return field; ERR_BITS_WRITE_BITS: free(field); ERR_MALLOC: return NULL; }
int h264_encodeSPS(BIT_STREAM_T *pStream, H264_NAL_SPS_T *pSps) { int idx; bits_write(pStream, pSps->profile_id, 8); bits_write(pStream, 0, 8); bits_write(pStream, pSps->level_id, 8); bits_writeExpGolomb(pStream, pSps->seq_param_set_id); if(pSps->profile_id >= H264_PROFILE_HIGH) { bits_writeExpGolomb(pStream, pSps->chroma_format_idc); if(pSps->chroma_format_idc == 3) { bits_write(pStream, pSps->residual_color_transform, 1); } bits_writeExpGolomb(pStream, pSps->bit_depth_luma - 8); bits_writeExpGolomb(pStream, pSps->bit_depth_chroma - 8); bits_write(pStream, pSps->qprime_y_zero_transform_bypass, 1); bits_write(pStream, pSps->scaling_matrix_present, 1); if(pSps->scaling_matrix_present) { LOG(X_ERROR("SPS scaling matrix encode not implemented for high profiles")); return H264_RC_ERR; } } bits_writeExpGolomb(pStream, pSps->log2_max_frame_num - 4); bits_writeExpGolomb(pStream, pSps->pic_order_cnt_type); if(pSps->pic_order_cnt_type == 0) { bits_writeExpGolomb(pStream, pSps->log2_max_pic_order_cnt_lsb - 4); } else if(pSps->pic_order_cnt_type == 1) { bits_write(pStream, pSps->pic_delta_pic_order, 1); bits_writeExpGolomb(pStream, pSps->pic_offset_non_ref_pic); bits_writeExpGolomb(pStream, pSps->pic_offset_top_to_bottom); bits_writeExpGolomb(pStream, pSps->pic_num_ref_frames_in_pic_order_cnt_cycle); for(idx = 0; idx < pSps->pic_num_ref_frames_in_pic_order_cnt_cycle; idx++) { bits_writeExpGolomb(pStream, pSps->pic_offset_for_ref_frame[idx]); } } else if(pSps->pic_order_cnt_type != 2) { LOG(X_ERROR("SPS contains invalid pic order count type:%d"), pSps->pic_order_cnt_type); return H264_RC_ERR; } bits_writeExpGolomb(pStream, pSps->num_ref_frames); bits_write(pStream, pSps->gaps_in_frame_num_val_allowed_flag, 1); bits_writeExpGolomb(pStream, pSps->pic_width_in_mbs - 1); bits_writeExpGolomb(pStream, pSps->pic_height_in_mbs - 1); //TODO: this may be set to 0 for interlaced frames bits_write(pStream, pSps->frame_mbs_only_flag, 1); if(pSps->frame_mbs_only_flag == 0) { bits_write(pStream, pSps->mb_adaptive_frame_field_flag, 1); } bits_write(pStream, pSps->direct_8x8_inference_flag, 1); bits_write(pStream, pSps->frame_cropping, 1); if(pSps->frame_cropping) { bits_writeExpGolomb(pStream, pSps->crop_left); bits_writeExpGolomb(pStream, pSps->crop_right); bits_writeExpGolomb(pStream, pSps->crop_top); bits_writeExpGolomb(pStream, pSps->crop_bottom); } // VUI parameters bits_write(pStream, pSps->vui.params_present_flag, 1); if(pSps->vui.params_present_flag) { bits_write(pStream, pSps->vui.aspect_ratio_info_present, 1); if(pSps->vui.aspect_ratio_info_present) { bits_write(pStream, pSps->vui.aspect_ratio_idc, 8); if(pSps->vui.aspect_ratio_idc == 255) { bits_write(pStream, pSps->vui.aspect_ratio_sar_width, 16); bits_write(pStream, pSps->vui.aspect_ratio_sar_height, 16); } } bits_write(pStream, pSps->vui.overscan_info_present, 1); if(pSps->vui.overscan_info_present) { bits_write(pStream, pSps->vui.overscan_appropriate, 1); } bits_write(pStream, pSps->vui.video_signal_type_present, 1); if(pSps->vui.video_signal_type_present) { bits_write(pStream, pSps->vui.video_format, 3); bits_write(pStream, pSps->vui.video_full_range, 1); bits_write(pStream, pSps->vui.color_desc_present, 1); if(pSps->vui.color_desc_present) { bits_write(pStream, pSps->vui.color_primaries, 8); bits_write(pStream, pSps->vui.color_transfer_characteristics, 8); bits_write(pStream, pSps->vui.color_matrix, 8); } } bits_write(pStream, pSps->vui.chroma_loc_info_present, 1); if(pSps->vui.chroma_loc_info_present) { bits_writeExpGolomb(pStream, pSps->vui.chroma_sample_top); bits_writeExpGolomb(pStream, pSps->vui.chroma_sample_bottom); } bits_write(pStream, pSps->vui.timing_info_present, 1); if(pSps->vui.timing_info_present) { bits_write(pStream, pSps->vui.timing_num_units_in_tick, 32); bits_write(pStream, pSps->vui.timing_timescale, 32); bits_write(pStream, pSps->vui.timing_fixed_frame_rate, 1); } bits_write(pStream, pSps->vui.nal_hrd_params, 1); if(pSps->vui.nal_hrd_params) { if(h264_encodeSPSVUI_hrd(pStream, &pSps->vui) != 0) { return H264_RC_ERR; } } bits_write(pStream, pSps->vui.vcl_hrd_params, 1); if(pSps->vui.vcl_hrd_params) { if(h264_encodeSPSVUI_hrd(pStream, &pSps->vui) != 0) { return H264_RC_ERR; } } if(pSps->vui.nal_hrd_params || pSps->vui.vcl_hrd_params) { bits_write(pStream, pSps->vui.low_delay_hrd_flag, 1); } bits_write(pStream, pSps->vui.pic_struct_present_flag, 1); bits_write(pStream, pSps->vui.bitstream_restriction, 1); if(pSps->vui.bitstream_restriction) { bits_write(pStream, pSps->vui.motion_vec_over_pic_boundary, 1); bits_writeExpGolomb(pStream, pSps->vui.max_bytes_per_pic_denom); bits_writeExpGolomb(pStream, pSps->vui.max_bits_per_pic_denom); bits_writeExpGolomb(pStream, pSps->vui.log2_max_mv_len_horiz); bits_writeExpGolomb(pStream, pSps->vui.log2_max_mv_len_vert); bits_writeExpGolomb(pStream, pSps->vui.num_reorder_frames); bits_writeExpGolomb(pStream, pSps->vui.max_dec_frame_buffering); } } // end of VUI H264_STREAM_ENDOF_BYTE(pStream); return H264_RC_OK; }
static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, int i_es_size, es_format_t *p_fmt, int i_stream_id, int i_private_id, vlc_bool_t b_mpeg2, vlc_bool_t b_data_alignment, int i_header_size ) { bits_buffer_t bits; int i_extra = 0; /* For PES_PRIVATE_STREAM_1 there is an extra header after the pes header */ /* i_private_id != -1 because TS use 0xbd without private_id */ if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 ) { i_extra = 1; if( ( i_private_id & 0xf0 ) == 0x80 ) { i_extra += 3; } } bits_initwrite( &bits, 50, p_hdr ); /* add start code */ bits_write( &bits, 24, 0x01 ); bits_write( &bits, 8, i_stream_id ); switch( i_stream_id ) { case PES_PROGRAM_STREAM_MAP: case PES_PADDING: case PES_PRIVATE_STREAM_2: case PES_ECM: case PES_EMM: case PES_PROGRAM_STREAM_DIRECTORY: case PES_DSMCC_STREAM: case PES_ITU_T_H222_1_TYPE_E_STREAM: /* add pes data size */ bits_write( &bits, 16, i_es_size ); bits_align( &bits ); return( bits.i_data ); default: /* arg, a little more difficult */ if( b_mpeg2 ) { int i_pts_dts; if( i_pts > 0 && i_dts > 0 && ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES && p_fmt->i_codec != VLC_FOURCC('m','p','g','v') ) ) ) { i_pts_dts = 0x03; if ( !i_header_size ) i_header_size = 0xa; } else if( i_pts > 0 ) { i_pts_dts = 0x02; if ( !i_header_size ) i_header_size = 0x5; } else { i_pts_dts = 0x00; if ( !i_header_size ) i_header_size = 0x0; } bits_write( &bits, 16, i_es_size + i_extra + 3 + i_header_size ); // size bits_write( &bits, 2, 0x02 ); // mpeg2 id bits_write( &bits, 2, 0x00 ); // pes scrambling control bits_write( &bits, 1, 0x00 ); // pes priority bits_write( &bits, 1, b_data_alignment ); // data alignement indicator bits_write( &bits, 1, 0x00 ); // copyright bits_write( &bits, 1, 0x00 ); // original or copy bits_write( &bits, 2, i_pts_dts ); // pts_dts flags bits_write( &bits, 1, 0x00 ); // escr flags bits_write( &bits, 1, 0x00 ); // es rate flag bits_write( &bits, 1, 0x00 ); // dsm trick mode flag bits_write( &bits, 1, 0x00 ); // additional copy info flag bits_write( &bits, 1, 0x00 ); // pes crc flag bits_write( &bits, 1, 0x00 ); // pes extention flags bits_write( &bits, 8, i_header_size ); // header size -> pts and dts /* write pts */ if( i_pts_dts & 0x02 ) { bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011' bits_write( &bits, 3, i_pts >> 30 ); bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_pts >> 15 ); bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_pts ); bits_write( &bits, 1, 0x01 ); // marker i_header_size -= 0x5; } /* write i_dts */ if( i_pts_dts & 0x01 ) { bits_write( &bits, 4, 0x01 ); // '0001' bits_write( &bits, 3, i_dts >> 30 ); bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_dts >> 15 ); bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_dts ); bits_write( &bits, 1, 0x01 ); // marker i_header_size -= 0x5; } while ( i_header_size ) { bits_write( &bits, 8, 0xff ); i_header_size--; } }
void huffman_encode_char(data *buffer, char c) { switch (c) { case 'H': bits_write(buffer, 0, 1); break; case 'G': bits_write(buffer, 4, 3); break; case 'C': bits_write(buffer, 20, 5); break; case 'A': bits_write(buffer, 42, 6); break; case '<': bits_write(buffer, 86, 7); break; case '9': bits_write(buffer, 174, 8); break; case ':': bits_write(buffer, 175, 8); break; case 'B': bits_write(buffer, 22, 5); break; case '>': bits_write(buffer, 92, 7); break; case '.': bits_write(buffer, 744, 10); break; case ')': bits_write(buffer, 2980, 12); break; case 'I': bits_write(buffer, 2981, 12); break; case '+': bits_write(buffer, 1491, 11); break; case '6': bits_write(buffer, 373, 9); break; case ';': bits_write(buffer, 187, 8); break; case '#': bits_write(buffer, 47, 6); break; case 'F': bits_write(buffer, 6, 3); break; case 'E': bits_write(buffer, 14, 4); break; case 'D': bits_write(buffer, 30, 5); break; case '2': bits_write(buffer, 992, 10); break; case ',': bits_write(buffer, 1986, 11); break; case '/': bits_write(buffer, 1987, 11); break; case '8': bits_write(buffer, 497, 9); break; case '5': bits_write(buffer, 498, 9); break; case '7': bits_write(buffer, 499, 9); break; case '?': bits_write(buffer, 125, 7); break; case '=': bits_write(buffer, 252, 8); break; case '3': bits_write(buffer, 1012, 10); break; case '(': bits_write(buffer, 4052, 12); break; case '*': bits_write(buffer, 4053, 12); break; case '%': bits_write(buffer, 16216, 14); break; case '&': bits_write(buffer, 16217, 14); break; case '\'': bits_write(buffer, 8109, 13); break; case '-': bits_write(buffer, 4055, 12); break; case '4': bits_write(buffer, 1014, 10); break; case '0': bits_write(buffer, 2030, 11); break; case '1': bits_write(buffer, 2031, 11); break; case '@': bits_write(buffer, 127, 7); break; } }