/* write RV 1.0 compatible frame header */ void rv10_encode_picture_header(MpegEncContext *s, int picture_number) { int full_frame= 0; align_put_bits(&s->pb); put_bits(&s->pb, 1, 1); /* marker */ put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE)); put_bits(&s->pb, 1, 0); /* not PB frame */ put_bits(&s->pb, 5, s->qscale); if (s->pict_type == FF_I_TYPE) { /* specific MPEG like DC coding not used */ } /* if multiple packets per frame are sent, the position at which to display the macro blocks is coded here */ if(!full_frame){ put_bits(&s->pb, 6, 0); /* mb_x */ put_bits(&s->pb, 6, 0); /* mb_y */ put_bits(&s->pb, 12, s->mb_width * s->mb_height); } put_bits(&s->pb, 3, 0); /* ignored */ }
int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb) { int five_bit_ch, four_bit_ch, comment_size, bits; int offset = put_bits_count(pb); copy_bits(pb, gb, 10); //Tag, Object Type, Frequency five_bit_ch = copy_bits(pb, gb, 4); //Front five_bit_ch += copy_bits(pb, gb, 4); //Side five_bit_ch += copy_bits(pb, gb, 4); //Back four_bit_ch = copy_bits(pb, gb, 2); //LFE four_bit_ch += copy_bits(pb, gb, 3); //Data five_bit_ch += copy_bits(pb, gb, 4); //Coupling if (copy_bits(pb, gb, 1)) //Mono Mixdown copy_bits(pb, gb, 4); if (copy_bits(pb, gb, 1)) //Stereo Mixdown copy_bits(pb, gb, 4); if (copy_bits(pb, gb, 1)) //Matrix Mixdown copy_bits(pb, gb, 3); for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16) copy_bits(pb, gb, 16); if (bits) copy_bits(pb, gb, bits); align_put_bits(pb); align_get_bits(gb); comment_size = copy_bits(pb, gb, 8); for (; comment_size > 0; comment_size--) copy_bits(pb, gb, 8); return put_bits_count(pb) - offset; }
size_t xau_pcm8m_encode( const sint8* pcm, size_t samples, void* buf, size_t bufsize ) { // check preconditions xau_codec_t codec; init_put_bits( &codec.pb, (uint8*)buf, bufsize ); rice_init( codec.rice, PCM8M_K1, PCM8M_K2 ); ppfilter_init( &codec.flt, PCM8M_SH ); const sint8* p = pcm; for( size_t n = 0; n != samples; ++n ) { // a) skip sample diffing part as it seems hurting performance a lot on 8bit mode // *p -= prev / 2 // b) apply polyphase filter sint32 tmp = ppfilter_encode( &codec.flt, *p++ ); // c) skip median predictor part - also hurts performance on 8bps // *p -= PREDICTOR1( last, 4 ); // d) encode the 'unsignificated' code rice_enc( &codec.pb, codec.rice, xbs_sign_pack( tmp ) ); } align_put_bits( &codec.pb ); uint32 written = put_bits_count( &codec.pb ) / 8; // check for overruns return written; }
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ ASV1Context * const a = avctx->priv_data; AVFrame *pict = data; AVFrame * const p= (AVFrame*)&a->picture; int size; int mb_x, mb_y; init_put_bits(&a->pb, buf, buf_size); *p = *pict; p->pict_type= I_TYPE; p->key_frame= 1; for(mb_y=0; mb_y<a->mb_height2; mb_y++){ for(mb_x=0; mb_x<a->mb_width2; mb_x++){ dct_get(a, mb_x, mb_y); encode_mb(a, a->block); } } if(a->mb_width2 != a->mb_width){ mb_x= a->mb_width2; for(mb_y=0; mb_y<a->mb_height2; mb_y++){ dct_get(a, mb_x, mb_y); encode_mb(a, a->block); } } if(a->mb_height2 != a->mb_height){ mb_y= a->mb_height2; for(mb_x=0; mb_x<a->mb_width; mb_x++){ dct_get(a, mb_x, mb_y); encode_mb(a, a->block); } } emms_c(); align_put_bits(&a->pb); while(put_bits_count(&a->pb)&31) put_bits(&a->pb, 8, 0); size= put_bits_count(&a->pb)/32; if(avctx->codec_id == CODEC_ID_ASV1) a->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); else{ int i; for(i=0; i<4*size; i++) buf[i]= reverse[ buf[i] ]; } return size*4; }
void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number) { int format; align_put_bits(&s->pb); put_bits(&s->pb, 17, 1); put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */ put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->time_base.num) / //FIXME use timestamp s->avctx->time_base.den) & 0xff); /* TemporalReference */ if (s->width == 352 && s->height == 288) format = 2; else if (s->width == 176 && s->height == 144) format = 3; else if (s->width == 128 && s->height == 96) format = 4; else if (s->width == 320 && s->height == 240) format = 5; else if (s->width == 160 && s->height == 120) format = 6; else if (s->width <= 255 && s->height <= 255) format = 0; /* use 1 byte width & height */ else format = 1; /* use 2 bytes width & height */ put_bits(&s->pb, 3, format); /* PictureSize */ if (format == 0) { put_bits(&s->pb, 8, s->width); put_bits(&s->pb, 8, s->height); } else if (format == 1) { put_bits(&s->pb, 16, s->width); put_bits(&s->pb, 16, s->height); } put_bits(&s->pb, 2, s->pict_type == FF_P_TYPE); /* PictureType */ put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */ put_bits(&s->pb, 5, s->qscale); /* Quantizer */ put_bits(&s->pb, 1, 0); /* ExtraInformation */ if(s->h263_aic) { s->y_dc_scale_table= s->c_dc_scale_table= ff_aic_dc_scale_table; } else { s->y_dc_scale_table= s->c_dc_scale_table= ff_mpeg1_dc_scale_table; } }
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){ init_put_bits(&s->pb, buf, buf_size); if (s->use_bit_reservoir) { assert(0);//FIXME not implemented }else{ if(encode_block(s, src_coefs, total_gain) < 0) return INT_MAX; } align_put_bits(&s->pb); return put_bits_count(&s->pb)/8 - s->block_align; }
/** * Write some auxiliary information about the created AAC file. */ static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name) { int i, namelen, padbits; namelen = strlen(name) + 2; put_bits(&s->pb, 3, TYPE_FIL); put_bits(&s->pb, 4, FFMIN(namelen, 15)); if(namelen >= 15) put_bits(&s->pb, 8, namelen - 16); put_bits(&s->pb, 4, 0); //extension type - filler padbits = 8 - (put_bits_count(&s->pb) & 7); align_put_bits(&s->pb); for(i = 0; i < namelen - 2; i++) put_bits(&s->pb, 8, name[i]); put_bits(&s->pb, 12 - padbits, 0); }
/* pad the end of the output stream with zeros */ void flush_put_bits(PutBitContext *s) { #ifdef ALT_BITSTREAM_WRITER align_put_bits(s); #else s->bit_buf<<= s->bit_left; while (s->bit_left < 32) { /* XXX: should test end of buffer */ *s->buf_ptr++=s->bit_buf >> 24; s->bit_buf<<=8; s->bit_left+=8; } s->bit_left=32; s->bit_buf=0; #endif }
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ CLJRContext * const a = avctx->priv_data; AVFrame *pict = data; AVFrame * const p= (AVFrame*)&a->picture; int size; *p = *pict; p->pict_type= FF_I_TYPE; p->key_frame= 1; emms_c(); align_put_bits(&a->pb); while(get_bit_count(&a->pb)&31) put_bits(&a->pb, 8, 0); size= get_bit_count(&a->pb)/32; return size*4; }
void h263_encode_picture_header(MpegEncContext * s, int picture_number) { int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref; int best_clock_code=1; int best_divisor=60; int best_error= INT_MAX; if(s->h263_plus){ for(i=0; i<2; i++){ int div, error; div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den); div= av_clip(div, 1, 127); error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div); if(error < best_error){ best_error= error; best_divisor= div; best_clock_code= i; } } } s->custom_pcf= best_clock_code!=1 || best_divisor!=60; coded_frame_rate= 1800000; coded_frame_rate_base= (1000+best_clock_code)*best_divisor; align_put_bits(&s->pb); /* Update the pointer to last GOB */ s->ptr_lastgob = put_bits_ptr(&s->pb); put_bits(&s->pb, 22, 0x20); /* PSC */ temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp (coded_frame_rate_base * (int64_t)s->avctx->time_base.den); put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */ put_bits(&s->pb, 1, 1); /* marker */ put_bits(&s->pb, 1, 0); /* h263 id */ put_bits(&s->pb, 1, 0); /* split screen off */ put_bits(&s->pb, 1, 0); /* camera off */ put_bits(&s->pb, 1, 0); /* freeze picture release off */ format = ff_match_2uint16(h263_format, FF_ARRAY_ELEMS(h263_format), s->width, s->height); if (!s->h263_plus) { /* H.263v1 */ put_bits(&s->pb, 3, format); put_bits(&s->pb, 1, (s->pict_type == AV_PICTURE_TYPE_P)); /* By now UMV IS DISABLED ON H.263v1, since the restrictions of H.263v1 UMV implies to check the predicted MV after calculation of the current MB to see if we're on the limits */ put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */ put_bits(&s->pb, 1, 0); /* SAC: off */ put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */ put_bits(&s->pb, 1, 0); /* only I/P frames, no PB frame */ put_bits(&s->pb, 5, s->qscale); put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ } else { int ufep=1; /* H.263v2 */ /* H.263 Plus PTYPE */ put_bits(&s->pb, 3, 7); put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */ if (format == 8) put_bits(&s->pb,3,6); /* Custom Source Format */ else put_bits(&s->pb, 3, format); put_bits(&s->pb,1, s->custom_pcf); put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */ put_bits(&s->pb,1,0); /* SAC: off */ put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */ put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */ put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */ put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */ put_bits(&s->pb,1,0); /* Reference Picture Selection: off */ put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */ put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */ put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */ put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ put_bits(&s->pb,3,0); /* Reserved */ put_bits(&s->pb, 3, s->pict_type == AV_PICTURE_TYPE_P); put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */ put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */ put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */ put_bits(&s->pb,2,0); /* Reserved */ put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ /* This should be here if PLUSPTYPE */ put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ if (format == 8) { /* Custom Picture Format (CPFMT) */ s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio); put_bits(&s->pb,4,s->aspect_ratio_info); put_bits(&s->pb,9,(s->width >> 2) - 1); put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ put_bits(&s->pb,9,(s->height >> 2)); if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){ put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num); put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den); } } if(s->custom_pcf){ if(ufep){ put_bits(&s->pb, 1, best_clock_code); put_bits(&s->pb, 7, best_divisor); } put_sbits(&s->pb, 2, temp_ref>>8); } /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ if (s->umvplus) // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */ //FIXME check actual requested range put_bits(&s->pb,2,1); /* unlimited */ if(s->h263_slice_structured) put_bits(&s->pb,2,0); /* no weird submodes */ put_bits(&s->pb, 5, s->qscale); }
static void put_header(MpegEncContext *s, int header) { align_put_bits(&s->pb); put_bits(&s->pb, 16, header>>16); put_sbits(&s->pb, 16, header); }
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){ int v, bsize, ch, coef_nb_bits, parse_exponents; float mdct_norm; int nb_coefs[MAX_CHANNELS]; static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20}; //FIXME remove duplication relative to decoder if (s->use_variable_block_len) { assert(0); //FIXME not implemented }else{ /* fixed block len */ s->next_block_len_bits = s->frame_len_bits; s->prev_block_len_bits = s->frame_len_bits; s->block_len_bits = s->frame_len_bits; } s->block_len = 1 << s->block_len_bits; // assert((s->block_pos + s->block_len) <= s->frame_len); bsize = s->frame_len_bits - s->block_len_bits; //FIXME factor v = s->coefs_end[bsize] - s->coefs_start; for(ch = 0; ch < s->nb_channels; ch++) nb_coefs[ch] = v; { int n4 = s->block_len / 2; mdct_norm = 1.0 / (float)n4; if (s->version == 1) { mdct_norm *= sqrt(n4); } } if (s->nb_channels == 2) { put_bits(&s->pb, 1, s->ms_stereo= 1); } for(ch = 0; ch < s->nb_channels; ch++) { s->channel_coded[ch] = 1; //FIXME only set channel_coded when needed, instead of always if (s->channel_coded[ch]) { init_exp(s, ch, fixed_exp); } } for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { WMACoef *coefs1; float *coefs, *exponents, mult; int i, n; coefs1 = s->coefs1[ch]; exponents = s->exponents[ch]; mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; mult *= mdct_norm; coefs = src_coefs[ch]; if (s->use_noise_coding && 0) { assert(0); //FIXME not implemented } else { coefs += s->coefs_start; n = nb_coefs[ch]; for(i = 0;i < n; i++){ double t= *coefs++ / (exponents[i] * mult); if(t<-32768 || t>32767) return -1; coefs1[i] = lrint(t); } } } } v = 0; for(ch = 0; ch < s->nb_channels; ch++) { int a = s->channel_coded[ch]; put_bits(&s->pb, 1, a); v |= a; } if (!v) return 1; for(v= total_gain-1; v>=127; v-= 127) put_bits(&s->pb, 7, 127); put_bits(&s->pb, 7, v); coef_nb_bits= ff_wma_total_gain_to_bits(total_gain); if (s->use_noise_coding) { for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { int i, n; n = s->exponent_high_sizes[bsize]; for(i=0;i<n;i++) { put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0); if (0) nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; } } } } parse_exponents = 1; if (s->block_len_bits != s->frame_len_bits) { put_bits(&s->pb, 1, parse_exponents); } if (parse_exponents) { for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { if (s->use_exp_vlc) { encode_exp_vlc(s, ch, fixed_exp); } else { assert(0); //FIXME not implemented // encode_exp_lsp(s, ch); } } } } else { assert(0); //FIXME not implemented } for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { int run, tindex; WMACoef *ptr, *eptr; tindex = (ch == 1 && s->ms_stereo); ptr = &s->coefs1[ch][0]; eptr = ptr + nb_coefs[ch]; run=0; for(;ptr < eptr; ptr++){ if(*ptr){ int level= *ptr; int abs_level= FFABS(level); int code= 0; if(abs_level <= s->coef_vlcs[tindex]->max_level){ if(run < s->coef_vlcs[tindex]->levels[abs_level-1]) code= run + s->int_table[tindex][abs_level-1]; } assert(code < s->coef_vlcs[tindex]->n); put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]); if(code == 0){ if(1<<coef_nb_bits <= abs_level) return -1; //Workaround minor rounding differences for the regression tests, FIXME we should find and replace the problematic float by fixpoint for reg tests if(abs_level == 0x71B && (s->avctx->flags & CODEC_FLAG_BITEXACT)) abs_level=0x71A; put_bits(&s->pb, coef_nb_bits, abs_level); put_bits(&s->pb, s->frame_len_bits, run); } put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere run=0; }else{ run++; } } if(run) put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]); } if (s->version == 1 && s->nb_channels >= 2) { align_put_bits(&s->pb); } } return 0; }