static void dec_process(MSFilter *f){ DecState *s=(DecState*)f->data; mblk_t *im,*om; int nbytes; float samples[BLOCKL_MAX],tmp; int i; while ((im=ms_queue_get(f->inputs[0]))!=NULL){ nbytes=msgdsize(im); if (nbytes<=0) return; if (nbytes%38!=0 && nbytes%50!=0) return; if (nbytes%38==0 && s->nbytes!=NO_OF_BYTES_20MS) { /* not yet configured, or misconfigured */ s->ms_per_frame=20; s->nbytes=NO_OF_BYTES_20MS; s->nsamples=BLOCKL_20MS; initDecode(&s->ilbc_dec,s->ms_per_frame,0); } else if (nbytes%50==0 && s->nbytes!=NO_OF_BYTES_30MS) { /* not yet configured, or misconfigured */ s->ms_per_frame=30; s->nbytes=NO_OF_BYTES_30MS; s->nsamples=BLOCKL_30MS; initDecode(&s->ilbc_dec,s->ms_per_frame,0); } if (s->nbytes>0 && nbytes>=s->nbytes){ int frame_per_packet = nbytes/s->nbytes; int k; for (k=0;k<frame_per_packet;k++) { om=allocb(s->nsamples*2,0); iLBC_decode(samples,(uint8_t*)im->b_rptr+(k*s->nbytes),&s->ilbc_dec,1); for (i=0;i<s->nsamples;i++,om->b_wptr+=2){ tmp=samples[i]; //ÐÞÕý±¬ÆÆÒô if (tmp<MIN_SAMPLE) tmp=MIN_SAMPLE; else if (tmp>MAX_SAMPLE) tmp=MAX_SAMPLE; *((int16_t*)om->b_wptr)= tmp; } ms_queue_put(f->outputs[0],om); } }else{ ms_warning("bad iLBC frame !"); } freemsg(im); } }
IlbcToPcmFilter::IlbcToPcmFilter() { /* Initialize iLBC decoder states */ memset(&dec30, 0, sizeof(dec30)); memset(&dec20, 0, sizeof(dec20)); initDecode(&dec30, 30, 0); initDecode(&dec20, 20, 0); this->s_ilbclog = Logger::getLogger("iLBC"); #if 0 LOG4CXX_INFO(this->s_ilbclog, "Initialized new iLBC decoder"); #endif }
static void decodeForFlash(void * self, AS3_Val args) { AS3_Val progress; AS3_Val src, dest; int len, srcLen, yieldTicks; short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX]; AS3_ArrayValue(args, "AS3ValType, AS3ValType, AS3ValType, IntType, IntType", &progress, &src, &dest, &srcLen, &yieldTicks); iLBC_Dec_Inst_t Dec_Inst; initDecode(&Dec_Inst, 30, 1);//30ms mode int i = 0; int loops = srcLen / Dec_Inst.no_of_bytes; resetPositionByteArray(src); while(AS3_ByteArray_readBytes(encoded_data, src, Dec_Inst.no_of_bytes) == Dec_Inst.no_of_bytes){ len = decode(&Dec_Inst, decoded_data, encoded_data, 1);//1 for no packet loss AS3_ByteArray_writeBytes(dest, decoded_data, len * sizeof(short)); /* write output file */ if(i % yieldTicks == 0){ AS3_CallT(progress, NULL, "IntType", (int)((float)i / loops * 100)); flyield();//yield to main process } i++; } resetPositionByteArray(src); resetPositionByteArray(dest); // Don't remove progess 100 call here, else complete won't be called! AS3_CallT(progress, NULL, "IntType", 100); }
static void set_decoder_mode(struct audec_state *st, int mode) { if (st->mode == mode) return; info("ilbc: set iLBC decoder mode %dms\n", mode); st->mode = mode; switch (mode) { case 20: st->nsamp = BLOCKL_20MS; break; case 30: st->nsamp = BLOCKL_30MS; break; default: warning("ilbc: unknown decoder mode %d\n", mode); return; } st->nsamp = initDecode(&st->dec, mode, USE_ENHANCER); }
static int ilbctolin_new(struct ast_trans_pvt *pvt) { struct ilbc_coder_pvt *tmp = pvt->pvt; initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER); return 0; }
static int tdav_codec_ilbc_open(tmedia_codec_t* self) { tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self; initEncode(&ilbc->encoder, TDAV_ILBC_MODE); initDecode(&ilbc->decoder, TDAV_ILBC_MODE, tsk_true/* Enhancer */); return 0; }
static struct ast_translator_pvt *ilbctolin_new(void) { struct ilbc_coder_pvt *tmp; tmp = malloc(sizeof(struct ilbc_coder_pvt)); if (tmp) { /* Shut valgrind up */ memset(&tmp->dec, 0, sizeof(tmp->dec)); initDecode(&tmp->dec, USE_ILBC_ENHANCER); tmp->tail = 0; localusecnt++; } return tmp; }
long iLBC_create(const char* format_parameters, amci_codec_fmt_info_t* format_description) { iLBC_Codec_Inst_t* codec_inst; int mode; char* mbegin; char* msep; char modeb[8]; if ((!format_parameters)||(!*format_parameters)||(!(mbegin=strstr(format_parameters, "mode")))) { mode = 30; // default to 30 ms mode if no parameter given. } else { msep=mbegin; while (*msep!='=' && *msep!='\0') msep++; msep++; mbegin=msep; while (*msep!='=' && *msep!='\0') msep++; if ((msep-mbegin)>8) { DBG("Error in fmtp line >>'%s<<.\n", format_parameters); mode=30; } else { memcpy(modeb, mbegin, msep-mbegin); modeb[msep-mbegin]='\0'; if ((!(mode=atoi(modeb))) || (mode != 30 && mode!= 20)) { DBG("Error in fmtp line >>'%s<<.\n", format_parameters); mode=30; } } } format_description[0].id = AMCI_FMT_FRAME_LENGTH ; format_description[0].value = mode; format_description[1].id = AMCI_FMT_FRAME_SIZE; format_description[1].value = mode==30 ? 240 : 160; format_description[2].id = AMCI_FMT_ENCODED_FRAME_SIZE; format_description[2].value = mode==30 ? 50 : 38; format_description[3].id = 0; if (format_parameters) { DBG("ilbc with format parameters : '%s', mode=%d.\n", format_parameters, mode); } codec_inst = (iLBC_Codec_Inst_t*)malloc(sizeof(iLBC_Codec_Inst_t)); codec_inst->mode = mode; if (!codec_inst) return -1; initEncode(&codec_inst->iLBC_Enc_Inst, mode); initDecode(&codec_inst->iLBC_Dec_Inst, mode, 0 /* 1=use_enhancer */); return (long)codec_inst; }
static int codec_decoder(const struct PluginCodec_Definition * codec, void * context, const void * from, unsigned * fromLen, void * to, unsigned * toLen, unsigned int * flag) { int i; #ifndef OPAL_SYSTEM_ILBC float block[BLOCKL_MAX]; short * sampleBuffer = (short *)to; #endif struct iLBC_Dec_Inst_t_ * decoder = (struct iLBC_Dec_Inst_t_ *)context; // If packet not have integral number of frames for this mode if ((*fromLen % decoder->no_of_bytes) != 0) { // Then switch to the other mode initDecode(context, decoder->mode == 20 ? 30 : 20, 0); if ((*fromLen % decoder->no_of_bytes) != 0) return 0; // Still wrong, what are they sending us? } /* do actual decoding of block */ #ifdef OPAL_SYSTEM_ILBC iLBC_decode((short *)to, (WebRtc_UWord16 *)from, decoder, 1); #else iLBC_decode(block, (unsigned char *)from, decoder, 1); /* convert to short */ for (i = 0; i < decoder->blockl; i++) { float tmp = block[i]; if (tmp < -32767) tmp = -32767; else if (tmp > 32767) tmp = 32767; sampleBuffer[i] = (short)tmp; } #endif if (*toLen < decoder->blockl*2U) return 0; // set output lengths *toLen = decoder->blockl*2; *fromLen = decoder->no_of_bytes; return 1; }
static int set_codec_options(const struct PluginCodec_Definition * defn, void * context, const char * name, void * parm, unsigned * parmLen) { const char * const * option; if (context == NULL || parm == NULL || parmLen == NULL || *parmLen != sizeof(const char **)) return 0; for (option = (const char * const *)parm; *option != NULL; option += 2) { if (STRCMPI(option[0], PreferredModeStr) == 0) { unsigned mode = get_mode(option[1]); if (defn->destFormat[0] == 'L') initDecode(context, mode, 0); else initEncode(context, mode); } } return 1; }
/* * Open codec. */ static pj_status_t ilbc_codec_open(pjmedia_codec *codec, pjmedia_codec_param *attr ) { struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)codec; pj_status_t status; unsigned i, dec_fmtp_mode = 0, enc_fmtp_mode = 0; pj_assert(ilbc_codec != NULL); pj_assert(ilbc_codec->enc_ready == PJ_FALSE && ilbc_codec->dec_ready == PJ_FALSE); /* Get decoder mode */ for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) { if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, &STR_MODE) == 0) { dec_fmtp_mode = (unsigned) pj_strtoul(&attr->setting.dec_fmtp.param[i].val); break; } } /* Decoder mode must be set */ PJ_ASSERT_RETURN(dec_fmtp_mode == 20 || dec_fmtp_mode == 30, PJMEDIA_CODEC_EINMODE); /* Get encoder mode */ for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) { if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name, &STR_MODE) == 0) { enc_fmtp_mode = (unsigned) pj_strtoul(&attr->setting.enc_fmtp.param[i].val); break; } } /* The enc mode must be set in the attribute * (from the mode parameter in fmtp attribute in the SDP * received from remote) */ if (enc_fmtp_mode == 0) enc_fmtp_mode = dec_fmtp_mode; PJ_ASSERT_RETURN(enc_fmtp_mode==20 || enc_fmtp_mode==30, PJMEDIA_CODEC_EINMODE); /* Update enc_ptime in the param */ if (enc_fmtp_mode != dec_fmtp_mode) { attr->info.enc_ptime = (pj_uint16_t)enc_fmtp_mode; } else { attr->info.enc_ptime = 0; } /* Create enc */ ilbc_codec->enc_frame_size = initEncode(&ilbc_codec->enc, enc_fmtp_mode); ilbc_codec->enc_samples_per_frame = CLOCK_RATE * enc_fmtp_mode / 1000; ilbc_codec->enc_ready = PJ_TRUE; /* Create decoder */ ilbc_codec->dec_samples_per_frame = initDecode(&ilbc_codec->dec, dec_fmtp_mode, attr->setting.penh); if (dec_fmtp_mode == 20) ilbc_codec->dec_frame_size = 38; else if (dec_fmtp_mode == 30) ilbc_codec->dec_frame_size = 50; else { pj_assert(!"Invalid iLBC mode"); ilbc_codec->dec_frame_size = ilbc_codec->enc_frame_size; } ilbc_codec->dec_ready = PJ_TRUE; /* Save plc flags */ ilbc_codec->plc_enabled = (attr->setting.plc != 0); /* Create silence detector. */ ilbc_codec->vad_enabled = (attr->setting.vad != 0); status = pjmedia_silence_det_create(ilbc_codec->pool, CLOCK_RATE, ilbc_codec->enc_samples_per_frame, &ilbc_codec->vad); if (status != PJ_SUCCESS) return status; /* Init last_tx (not necessary because of zalloc, but better * be safe in case someone remove zalloc later. */ pj_set_timestamp32(&ilbc_codec->last_tx, 0, 0); PJ_LOG(5,(ilbc_codec->obj_name, "iLBC codec opened, encoder mode=%d, decoder mode=%d", enc_fmtp_mode, dec_fmtp_mode)); return PJ_SUCCESS; }
int main(int argc, char* argv[]) { /* Runtime statistics */ float starttime; float runtime; float outtime; FILE *ifileid,*efileid,*ofileid, *cfileid; short data[BLOCKL_MAX]; short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX]; int len; short pli, mode; int blockcount = 0; int packetlosscount = 0; /* Create structs */ iLBC_Enc_Inst_t Enc_Inst; iLBC_Dec_Inst_t Dec_Inst; /* get arguments and open files */ if ((argc!=5) && (argc!=6)) { fprintf(stderr, "\n*-----------------------------------------------*\n"); fprintf(stderr, " %s <20,30> input encoded decoded (channel)\n\n", argv[0]); fprintf(stderr, " mode : Frame size for the encoding/decoding\n"); fprintf(stderr, " 20 - 20 ms\n"); fprintf(stderr, " 30 - 30 ms\n"); fprintf(stderr, " input : Speech for encoder (16-bit pcm file)\n"); fprintf(stderr, " encoded : Encoded bit stream\n"); fprintf(stderr, " decoded : Decoded speech (16-bit pcm file)\n"); fprintf(stderr, " channel : Packet loss pattern, optional (16-bit)\n"); fprintf(stderr, " 1 - Packet received correctly\n"); fprintf(stderr, " 0 - Packet Lost\n"); fprintf(stderr, "*-----------------------------------------------*\n\n"); exit(1); } mode=atoi(argv[1]); if (mode != 20 && mode != 30) { fprintf(stderr,"Wrong mode %s, must be 20, or 30\n", argv[1]); exit(2); } ifileid=fopen(argv[2],"rb"); if ( (efileid=fopen(argv[3],(ifileid?"wb":"rb"))) == NULL) { fprintf(stderr, "Cannot open encoded file %s\n", argv[3]); exit(1);} if ( (ofileid=fopen(argv[4],"wb")) == NULL) { fprintf(stderr, "Cannot open decoded file %s\n", argv[4]); exit(1);} if (argc==6) { if( (cfileid=fopen(argv[5],"rb")) == NULL) { fprintf(stderr, "Cannot open channel file %s\n", argv[5]); exit(1); } } else { cfileid=NULL; } /* print info */ fprintf(stderr, "\n"); fprintf(stderr, "*---------------------------------------------------*\n"); fprintf(stderr, "* *\n"); fprintf(stderr, "* iLBC test program *\n"); fprintf(stderr, "* *\n"); fprintf(stderr, "* *\n"); fprintf(stderr, "*---------------------------------------------------*\n"); fprintf(stderr,"\nMode : %2d ms\n", mode); fprintf(stderr,"Input file : %s\n", argv[2]); fprintf(stderr,"Encoded file : %s\n", argv[3]); fprintf(stderr,"Output file : %s\n", argv[4]); if (argc==6) { fprintf(stderr,"Channel file : %s\n", argv[5]); } fprintf(stderr,"\n"); /* Initialization */ initEncode(&Enc_Inst, mode); initDecode(&Dec_Inst, mode, 1); /* Runtime statistics */ starttime=clock()/(float)CLOCKS_PER_SEC; /* loop over input blocks */ if(ifileid) { while (fread(data,sizeof(short),Enc_Inst.blockl,ifileid)== Enc_Inst.blockl) { blockcount++; /* encoding */ fprintf(stderr, "--- Encoding block %i --- ",blockcount); len=encode(&Enc_Inst, encoded_data, data); fprintf(stderr, "\r"); /* write byte file */ fwrite(encoded_data, sizeof(unsigned char), len, efileid); /* get channel data if provided */ if (argc==6) { if (fread(&pli, sizeof(short), 1, cfileid)) { if ((pli!=0)&&(pli!=1)) { fprintf(stderr, "Error in channel file\n"); exit(0); } if (pli==0) { /* Packet loss -> remove info from frame */ memset(encoded_data, 0, sizeof(short)*ILBCNOOFWORDS_MAX); packetlosscount++; } } else { fprintf(stderr, "Error. Channel file too short\n"); exit(0); } } else { pli=1; } /* decoding */ fprintf(stderr, "--- Decoding block %i --- ",blockcount); len=decode(&Dec_Inst, decoded_data, encoded_data, pli); fprintf(stderr, "\r"); /* write output file */ fwrite(decoded_data,sizeof(short),len,ofileid); } } else { fseek(efileid,0,SEEK_END); int inlen = ftell(efileid); int inpos=0; char *input = malloc(inlen); fseek(efileid,0,SEEK_SET); fread(input,1,inlen,efileid); while (inpos < inlen) { blockcount++; /* get channel data if provided */ if (argc==6) { if (fread(&pli, sizeof(short), 1, cfileid)) { if ((pli!=0)&&(pli!=1)) { fprintf(stderr, "Error in channel file\n"); exit(0); } if (pli==0) { /* Packet loss -> remove info from frame */ memset(encoded_data, 0, sizeof(short)*ILBCNOOFWORDS_MAX); packetlosscount++; } } else { fprintf(stderr, "Error. Channel file too short\n"); exit(0); } } else { pli=1; } /* decoding */ fprintf(stderr, "--- Decoding block %i --- ",blockcount); len=decode(&Dec_Inst, decoded_data, &input[inpos], pli); fprintf(stderr, "\r"); /* write output file */ fwrite(decoded_data,sizeof(short),len,ofileid); inpos += Dec_Inst.no_of_bytes; } } /* Runtime statistics */ runtime = (float)(clock()/(float)CLOCKS_PER_SEC-starttime); outtime = (float)((float)blockcount*(float)mode/1000.0); printf("\n\nLength of speech file: %.1f s\n", outtime); printf("Packet loss : %.1f%%\n", 100.0*(float)packetlosscount/(float)blockcount); printf("Time to run iLBC :"); printf(" %.1f s (%.1f %% of realtime)\n\n", runtime, (100*runtime/outtime)); /* close files */ if(ifileid) fclose(ifileid); fclose(efileid); fclose(ofileid); if (argc==6) { fclose(cfileid); } return(0); }
BOOL LLImageJ2CKDU::initDecode(LLImageJ2C &base, LLImageRaw &raw_image, int discard_level, int* region) { return initDecode(base,raw_image,0.0f,MODE_FAST,0,4,discard_level,region); }
// Returns TRUE to mean done, whether successful or not. BOOL LLImageJ2CKDU::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count) { ECodeStreamMode mode = MODE_FAST; LLTimer decode_timer; if (!mCodeStreamp) { if (!initDecode(base, raw_image, decode_time, mode, first_channel, max_channel_count)) { // Initializing the J2C decode failed, bail out. cleanupCodeStream(); return TRUE; // done } } // These can probably be grabbed from what's saved in the class. kdu_dims dims; mCodeStreamp->get_dims(0,dims); // Now we are ready to walk through the tiles processing them one-by-one. kdu_byte *buffer = raw_image.getData(); while (mTPosp->y < mTileIndicesp->size.y) { while (mTPosp->x < mTileIndicesp->size.x) { try { if (!mDecodeState) { kdu_tile tile = mCodeStreamp->open_tile(*(mTPosp)+mTileIndicesp->pos); // Find the region of the buffer occupied by this // tile. Note that we have no control over // sub-sampling factors which might have been used // during compression and so it can happen that tiles // (at the image component level) actually have // different dimensions. For this reason, we cannot // figure out the buffer region occupied by a tile // directly from the tile indices. Instead, we query // the highest resolution of the first tile-component // concerning its location and size on the canvas -- // the `dims' object already holds the location and // size of the entire image component on the same // canvas coordinate system. Comparing the two tells // us where the current tile is in the buffer. S32 channels = base.getComponents() - first_channel; if (channels > max_channel_count) { channels = max_channel_count; } kdu_resolution res = tile.access_component(0).access_resolution(); kdu_dims tile_dims; res.get_dims(tile_dims); kdu_coords offset = tile_dims.pos - dims.pos; int row_gap = channels*dims.size.x; // inter-row separation kdu_byte *buf = buffer + offset.y*row_gap + offset.x*channels; mDecodeState = new LLKDUDecodeState(tile, buf, row_gap); } // Do the actual processing F32 remaining_time = decode_time - decode_timer.getElapsedTimeF32(); // This is where we do the actual decode. If we run out of time, return false. if (mDecodeState->processTileDecode(remaining_time, (decode_time > 0.0f))) { delete mDecodeState; mDecodeState = NULL; } else { // Not finished decoding yet. // setLastError("Ran out of time while decoding"); return FALSE; } } catch (const char* msg) { base.setLastError(ll_safe_string(msg)); base.decodeFailed(); cleanupCodeStream(); return TRUE; // done } catch (...) { base.setLastError( "Unknown J2C error" ); base.decodeFailed(); cleanupCodeStream(); return TRUE; // done } mTPosp->x++; } mTPosp->y++; mTPosp->x = 0; } cleanupCodeStream(); return TRUE; }
static void dec_process(MSFilter *f){ DecState *s=(DecState*)f->data; mblk_t *im,*om; int nbytes; float samples[BLOCKL_MAX]={0}; int i; while ((im=ms_queue_get(f->inputs[0]))!=NULL){ nbytes=msgdsize(im); if (nbytes==0 || (nbytes%38!=0 && nbytes%50!=0)){ freemsg(im); continue; } if (nbytes%38==0 && s->nbytes!=NO_OF_BYTES_20MS) { /* not yet configured, or misconfigured */ s->ms_per_frame=20; s->nbytes=NO_OF_BYTES_20MS; s->nsamples=BLOCKL_20MS; s->ready=TRUE; initDecode(&s->ilbc_dec,s->ms_per_frame,s->postfilter); } else if (nbytes%50==0 && s->nbytes!=NO_OF_BYTES_30MS) { /* not yet configured, or misconfigured */ s->ms_per_frame=30; s->nbytes=NO_OF_BYTES_30MS; s->nsamples=BLOCKL_30MS; s->ready=TRUE; initDecode(&s->ilbc_dec,s->ms_per_frame,s->postfilter); } if (s->nbytes>0 && nbytes>=s->nbytes){ int frame_per_packet = nbytes/s->nbytes; int k; int plctime; for (k=0;k<frame_per_packet;k++) { om=allocb(s->nsamples*2,0); iLBC_decode(samples,(uint8_t*)im->b_rptr+(k*s->nbytes),&s->ilbc_dec,1); for (i=0;i<s->nsamples;i++,om->b_wptr+=2){ *((int16_t*)om->b_wptr)=samples[i]; } mblk_meta_copy(im,om); ms_queue_put(f->outputs[0],om); } if (s->plcctx){ plctime=ms_concealer_inc_sample_time(s->plcctx,f->ticker->time,frame_per_packet*s->ms_per_frame,1); if (plctime>0){ ms_warning("ilbc: did plc during %i ms",plctime); } } }else{ ms_warning("bad iLBC frame !"); } freemsg(im); } if (s->plcctx && s->ready && ms_concealer_context_is_concealement_required(s->plcctx,f->ticker->time)){ om=allocb(s->nsamples*2,0); iLBC_decode(samples,(uint8_t*)NULL,&s->ilbc_dec,0 /*PLC*/); for (i=0;i<s->nsamples;i++,om->b_wptr+=2){ *((int16_t*)om->b_wptr)=samples[i]; } mblk_set_plc_flag(om,TRUE); ms_queue_put(f->outputs[0],om); ms_concealer_inc_sample_time(s->plcctx,f->ticker->time,s->ms_per_frame,0); } }
//////////////////////////////////////////////////// // handle a new packet on the stream void EQPacketStream::handlePacket(EQUDPIPPacketFormat& packet) { emit numPacket(++m_packetCount, (int)m_streamid); if (!packet.isARQ()) // process packets that don't have an arq sequence { // we only handle packets with opcodes if (packet.payloadLength() < 2) return; // process the packets payload immediately processPayload(packet.payload(), packet.payloadLength()); } else if (packet.isARQ()) // process ARQ sequences { uint16_t arqSeq = packet.arq(); emit seqReceive(arqSeq, (int)m_streamid); /* this conditions should only be met once per zone/world, New Sequence */ if (packet.isSEQStart() && !packet.isClosingLo() && (m_session_tracking_enabled < 2)) { #ifdef PACKET_PROCESS_DIAG seqDebug("EQPacket: SEQStart found, setting arq seq, %04x stream %s", arqSeq, EQStreamStr[m_streamid]); #endif initDecode(); // hey, a SEQStart, use it's packet to set ARQ m_arqSeqExp = arqSeq; m_arqSeqFound = true; emit seqExpect(m_arqSeqExp, (int)m_streamid); if ((m_streamid == zone2client) && m_session_tracking_enabled) { m_session_tracking_enabled = 2; emit lockOnClient(packet.getSourcePort(), packet.getDestPort()); // notify that the client port has been latched emit sessionTrackingChanged(m_session_tracking_enabled); } } else if (!m_arqSeqFound && m_session_tracking_enabled == 0 && !packet.isClosingHi() && !packet.isClosingLo() && (m_streamid == zone2client)) { #ifdef PACKET_PROCESS_DIAG seqDebug("SEQ: new sequence found, setting arq seq, %04x stream %s", arqSeq, EQStreamStr[m_streamid]); #endif m_arqSeqExp = arqSeq; m_arqSeqFound = true; emit seqExpect(m_arqSeqExp, (int)m_streamid); } // is this the currently expected sequence, if so, do something with it. if (m_arqSeqExp == arqSeq) { m_arqSeqExp = arqSeq + 1; emit seqExpect(m_arqSeqExp, (int)m_streamid); #ifdef PACKET_PROCESS_DIAG seqDebug("SEQ: Found next arq in data stream %s, incrementing arq seq, %04x", EQStreamStr[m_streamid], arqSeq); #endif if (!packet.isASQ() && !packet.isFragment() && !packet.isClosingHi()) { // seems to be a sort of ping from client to server, has ARQ // but no ASQ, Flags look like 0x0201 (network byte order) #ifdef PACKET_PROCESS_DIAG seqDebug("SEQ: ARQ without ASQ from stream %s arq 0x%04x", EQStreamStr[m_streamid], arqSeq); #endif } // since the servers do not care about client closing sequences, we won't either // Hey clients have rights too, or not! else if (packet.isClosingHi() && packet.isClosingLo() && (m_streamid == zone2client)) { if (m_session_tracking_enabled) m_session_tracking_enabled = 1; emit closing(); return; } // if the packet is a fragment do appropriate processing else if (packet.isFragment()) processFragment(packet); else if (packet.payloadLength() >= 2) // has to have an opcode processPayload(packet.payload(), packet.payloadLength()); } // it's a packet from the future, add it to the cache else if ( ( (arqSeq > m_arqSeqExp) && (arqSeq < (uint32_t(m_arqSeqExp + arqSeqWrapCutoff))) ) || (arqSeq < (int32_t(m_arqSeqExp) - arqSeqWrapCutoff)) ) { #ifdef PACKET_PROCESS_DIAG seqDebug("SEQ: out of order arq %04x stream %s, sending to cache, %04d", arqSeq, EQStreamStr[m_streamid], m_cache.size()); #endif setCache(arqSeq, packet); } // if the cache isn't empty, then check for the expected ARQ sequence if (!m_cache.empty()) processCache(); } /* end ARQ processing */ }
/* * Open codec. */ static pj_status_t ilbc_codec_open(pjmedia_codec *codec, pjmedia_codec_param *attr ) { struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)codec; pj_status_t status; unsigned i; pj_uint16_t dec_fmtp_mode = DEFAULT_MODE, enc_fmtp_mode = DEFAULT_MODE; #if defined(PJMEDIA_ILBC_CODEC_USE_COREAUDIO)&& PJMEDIA_ILBC_CODEC_USE_COREAUDIO AudioStreamBasicDescription srcFormat, dstFormat; UInt32 size; srcFormat.mSampleRate = attr->info.clock_rate; srcFormat.mFormatID = kAudioFormatLinearPCM; srcFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; srcFormat.mBitsPerChannel = attr->info.pcm_bits_per_sample; srcFormat.mChannelsPerFrame = attr->info.channel_cnt; srcFormat.mBytesPerFrame = srcFormat.mChannelsPerFrame * srcFormat.mBitsPerChannel >> 3; srcFormat.mFramesPerPacket = 1; srcFormat.mBytesPerPacket = srcFormat.mBytesPerFrame * srcFormat.mFramesPerPacket; memset(&dstFormat, 0, sizeof(dstFormat)); dstFormat.mSampleRate = attr->info.clock_rate; dstFormat.mFormatID = kAudioFormatiLBC; dstFormat.mChannelsPerFrame = attr->info.channel_cnt; #endif pj_assert(ilbc_codec != NULL); pj_assert(ilbc_codec->enc_ready == PJ_FALSE && ilbc_codec->dec_ready == PJ_FALSE); /* Get decoder mode */ for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) { if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, &STR_MODE) == 0) { dec_fmtp_mode = (pj_uint16_t) pj_strtoul(&attr->setting.dec_fmtp.param[i].val); break; } } /* Decoder mode must be set */ PJ_ASSERT_RETURN(dec_fmtp_mode == 20 || dec_fmtp_mode == 30, PJMEDIA_CODEC_EINMODE); /* Get encoder mode */ for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) { if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name, &STR_MODE) == 0) { enc_fmtp_mode = (pj_uint16_t) pj_strtoul(&attr->setting.enc_fmtp.param[i].val); break; } } PJ_ASSERT_RETURN(enc_fmtp_mode==20 || enc_fmtp_mode==30, PJMEDIA_CODEC_EINMODE); /* Both sides of a bi-directional session MUST use the same "mode" value. * In this point, possible values are only 20 or 30, so when encoder and * decoder modes are not same, just use the default mode, it is 30. */ if (enc_fmtp_mode != dec_fmtp_mode) { enc_fmtp_mode = dec_fmtp_mode = DEFAULT_MODE; PJ_LOG(4,(ilbc_codec->obj_name, "Normalized iLBC encoder and decoder modes to %d", DEFAULT_MODE)); } /* Update some attributes based on negotiated mode. */ attr->info.avg_bps = (dec_fmtp_mode == 30? 13333 : 15200); attr->info.frm_ptime = dec_fmtp_mode; /* Create encoder */ #if defined(PJMEDIA_ILBC_CODEC_USE_COREAUDIO)&& PJMEDIA_ILBC_CODEC_USE_COREAUDIO dstFormat.mFramesPerPacket = CLOCK_RATE * enc_fmtp_mode / 1000; dstFormat.mBytesPerPacket = (enc_fmtp_mode == 20? 38 : 50); /* Use AudioFormat API to fill out the rest of the description */ size = sizeof(dstFormat); AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &dstFormat); if (AudioConverterNew(&srcFormat, &dstFormat, &ilbc_codec->enc) != noErr) return PJMEDIA_CODEC_EFAILED; ilbc_codec->enc_frame_size = (enc_fmtp_mode == 20? 38 : 50); #else ilbc_codec->enc_frame_size = initEncode(&ilbc_codec->enc, enc_fmtp_mode); #endif ilbc_codec->enc_samples_per_frame = CLOCK_RATE * enc_fmtp_mode / 1000; ilbc_codec->enc_ready = PJ_TRUE; /* Create decoder */ #if defined(PJMEDIA_ILBC_CODEC_USE_COREAUDIO)&& PJMEDIA_ILBC_CODEC_USE_COREAUDIO if (AudioConverterNew(&dstFormat, &srcFormat, &ilbc_codec->dec) != noErr) return PJMEDIA_CODEC_EFAILED; ilbc_codec->dec_samples_per_frame = CLOCK_RATE * dec_fmtp_mode / 1000; #else ilbc_codec->dec_samples_per_frame = initDecode(&ilbc_codec->dec, dec_fmtp_mode, attr->setting.penh); #endif ilbc_codec->dec_frame_size = (dec_fmtp_mode == 20? 38 : 50); ilbc_codec->dec_ready = PJ_TRUE; /* Save plc flags */ ilbc_codec->plc_enabled = (attr->setting.plc != 0); /* Create silence detector. */ ilbc_codec->vad_enabled = (attr->setting.vad != 0); status = pjmedia_silence_det_create(ilbc_codec->pool, CLOCK_RATE, ilbc_codec->enc_samples_per_frame, &ilbc_codec->vad); if (status != PJ_SUCCESS) return status; /* Init last_tx (not necessary because of zalloc, but better * be safe in case someone remove zalloc later. */ pj_set_timestamp32(&ilbc_codec->last_tx, 0, 0); PJ_LOG(4,(ilbc_codec->obj_name, "iLBC codec opened, mode=%d", dec_fmtp_mode)); return PJ_SUCCESS; }
static void * create_decoder(const struct PluginCodec_Definition * codec) { struct iLBC_Dec_Inst_t_ * context = (struct iLBC_Dec_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Dec_Inst_t_)); initDecode(context, codec->bitsPerSec != BITRATE_30MS ? 20 : 30, 0); return context; }
BOOL InWorldzJ2C::initDecode(ImageBaseForKDU& base, ImageBaseForKDU& raw_image, S32 discard_level, S32* region) { return initDecode(base,raw_image,0.0f,MODE_FAST,0,4,discard_level,region); }
// Returns TRUE to mean done, whether successful or not. BOOL InWorldzJ2C::decodeImpl(ImageBaseForKDU* base, ImageBaseForKDU* raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count) { if (!base || !raw_image) { // do nothing return TRUE; } if ((base->getData() == NULL) || (raw_image->getData() == NULL)) { base->mLastErrorMsg = "We've been sent bad data"; return TRUE; } ECodeStreamMode mode = MODE_FAST; DecodeTimer decode_timer; decode_timer.start(); if (!mCodeStreamp) { if (!initDecode(*base, *raw_image, decode_time, mode, first_channel, max_channel_count)) { // Initializing the J2C decode failed, bail out. cleanupCodeStream(); return TRUE; // done } } // These can probably be grabbed from what's saved in the class. kdu_dims dims; mCodeStreamp->get_dims(0, dims); if (dims.size.x <=0 || dims.size.y <= 0) { std::cout << "bad sizes in stream, not decoding!" << std::endl; return TRUE; } // figure out why this is happening -- MC /*if (dims.pos.get_x() < 0) { dims.pos.set_x(0); } if (dims.pos.get_y() < 0) { dims.pos.set_y(0); }*/ // Now we are ready to walk through the tiles processing them one-by-one. kdu_byte* buffer = raw_image->getData(); if (!buffer) { // shouldn't ever happen! cleanupCodeStream(); return TRUE; } while ((mTPosp->y < mTileIndicesp->size.y) && mTPosp->y >= 0) { while ((mTPosp->x < mTileIndicesp->size.x) && mTPosp->x >= 0) { try { if (!mDecodeState) { kdu_tile tile = mCodeStreamp->open_tile(*(mTPosp) + mTileIndicesp->pos); // Find the region of the buffer occupied by this // tile. Note that we have no control over // sub-sampling factors which might have been used // during compression and so it can happen that tiles // (at the image component level) actually have // different dimensions. For this reason, we cannot // figure out the buffer region occupied by a tile // directly from the tile indices. Instead, we query // the highest resolution of the first tile-component // concerning its location and size on the canvas -- // the `dims' object already holds the location and // size of the entire image component on the same // canvas coordinate system. Comparing the two tells // us where the current tile is in the buffer. S32 channels = base->getComponents() - first_channel; if (channels > max_channel_count) { channels = max_channel_count; } kdu_resolution res = tile.access_component(0).access_resolution(); kdu_dims tile_dims; res.get_dims(tile_dims); kdu_coords offset = tile_dims.pos - dims.pos; S32 row_gap = channels * dims.size.x; // inter-row separation kdu_byte* buf = buffer + offset.y*row_gap + offset.x * channels; mDecodeState = new LLKDUDecodeState(tile, buf, row_gap); } // Do the actual processing F32 remaining_time = decode_time - decode_timer.getElapsedTime(); // This is where we do the actual decode. If we run out of time, return false. if (mDecodeState->processTileDecode(remaining_time, (decode_time > 0.0f))) { delete mDecodeState; mDecodeState = NULL; //cleanupCodeStream(); ??? base->mDecodeSuccessful = true; //return TRUE; } else { // Not finished decoding yet. // setLastError("Ran out of time while decoding"); base->mDecodeSuccessful = false; //cleanupCodeStream(); ????? return FALSE; } } catch (const char* msg) { if (msg) { base->mLastErrorMsg = msg; } base->mDecodeSuccessful = false; cleanupCodeStream(); return TRUE; // done } catch (...) { base->mLastErrorMsg = "Unknown J2C error"; base->mDecodeSuccessful = false; cleanupCodeStream(); return TRUE; // done } mTPosp->x++; } mTPosp->y++; mTPosp->x = 0; } cleanupCodeStream(); return TRUE; }
static tsk_size_t tdav_codec_ilbc_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr) { int blocks, i, k, block_size; float dtmp; tsk_size_t out_size; tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self; if(!self || !in_data || !in_size || !out_data){ TSK_DEBUG_ERROR("Invalid parameter"); return 0; } if((in_size % NO_OF_BYTES_20MS) == 0){ /* Using 20ms mode */ blocks = (in_size/NO_OF_BYTES_20MS); out_size = (BLOCKL_20MS * blocks) * sizeof(short); block_size = out_size/blocks; if(ilbc->decoder.mode != 20){ initDecode(&ilbc->decoder, 20, tsk_true/* Enhancer */); } } else if((in_size % NO_OF_BYTES_30MS) == 0){ /* Using 30ms mode */ blocks = (in_size/NO_OF_BYTES_30MS); out_size = (BLOCKL_30MS * blocks) * sizeof(short); block_size = out_size/blocks; if(ilbc->decoder.mode != 30){ initDecode(&ilbc->decoder, 30, tsk_true/* Enhancer */); } } else{ TSK_DEBUG_ERROR("Invalid iLBC mode"); return 0; } /* allocate new buffer if needed */ if(*out_max_size<out_size){ if(!(*out_data = tsk_realloc(*out_data, out_size))){ TSK_DEBUG_ERROR("Failed to allocate new buffer"); *out_max_size = 0; return 0; } *out_max_size = out_size; } for(i = 0; i<blocks; i++){ iLBC_decode(ilbc->decblock, &((uint8_t*)in_data)[i*block_size], &ilbc->decoder, 1/* Normal */); /* convert to short */ for(k=0; k<ilbc->decoder.blockl; k++){ dtmp=ilbc->decblock[k]; if(dtmp<MIN_SAMPLE){ dtmp = MIN_SAMPLE; } else if(dtmp>MAX_SAMPLE){ dtmp = MAX_SAMPLE; } ((short*)*out_data)[(i*block_size) + k] = ((short) dtmp); } } return out_size; }