bool MemoryBuffer::copy(const MemoryBuffer& buf) { bool ret; if ((ret = resize(buf.size()))) { copyData(buf.dataPtr<uint8_t>(), buf.size()); } return ret; }
void assemble(const char *binfname, void *fin) { MemoryBuffer buffer; assemble_to_buffer(&buffer, fin); // Buffer compression logic. sp_file_hdr_t *header = (sp_file_hdr_t *)buffer.bytes(); size_t region_size = header->imagesize - header->dataoffs; size_t zbuf_max = compressBound(region_size); Bytef *zbuf = (Bytef *)malloc(zbuf_max); uLong new_disksize = zbuf_max; int err = compress2( zbuf, &new_disksize, (Bytef *)(buffer.bytes() + header->dataoffs), region_size, Z_BEST_COMPRESSION ); if (err != Z_OK) { free(zbuf); pc_printf("Unable to compress, error %d\n", err); pc_printf("Falling back to no compression.\n"); splat_to_binary(binfname, buffer.bytes(), buffer.size()); return; } header->disksize = new_disksize + header->dataoffs; header->compression = SmxConsts::FILE_COMPRESSION_GZ; buffer.rewind(header->dataoffs); buffer.write(zbuf, new_disksize); free(zbuf); splat_to_binary(binfname, buffer.bytes(), buffer.size()); }
DecoderStatus AacDecoder::process(Packet*packet){ FXlong fs = packet->stream_position; FXbool eos = packet->flags&FLAG_EOS; long unsigned int samplerate; FXuchar channels; NeAACDecFrameInfo frame; if (packet->flags&AAC_FLAG_CONFIG) { handle = NeAACDecOpen(); if (NeAACDecInit2(handle,packet->data(),packet->size(),&samplerate,&channels)<0){ packet->unref(); return DecoderError; } return DecoderOk; } else { buffer.append(packet->data(),packet->size()); packet->unref(); if (handle==NULL) { handle = NeAACDecOpen(); long n = NeAACDecInit(handle,buffer.data(),buffer.size(),&samplerate,&channels); if (n<0) return DecoderError; else if (n>0) buffer.readBytes(n); af.set(AP_FORMAT_S16,samplerate,channels); engine->output->post(new ConfigureEvent(af,Codec::AAC)); } } if (buffer.size()<FAAD_MIN_STREAMSIZE*2) { return DecoderOk; } do { if (out==NULL){ out = engine->decoder->get_output_packet(); if (out==NULL) return DecoderInterrupted; out->af = af; out->stream_position = fs; out->stream_length = packet->stream_length; } void * outbuffer = out->ptr(); NeAACDecDecode2(handle,&frame,buffer.data(),buffer.size(),&outbuffer,out->availableFrames()*out->af.framesize()); if (frame.bytesconsumed>0) { buffer.readBytes(frame.bytesconsumed); } if (frame.error > 0) { GM_DEBUG_PRINT("[aac] error %d (%ld): %s\n",frame.error,frame.bytesconsumed,faacDecGetErrorMessage(frame.error)); } if (frame.samples) { fs+=(frame.samples/frame.channels); out->wroteFrames((frame.samples/frame.channels)); if (out->availableFrames()==0) { engine->output->post(out); out=NULL; } } } while(buffer.size()>(2*FAAD_MIN_STREAMSIZE) && frame.bytesconsumed); if (eos) { if (out) { engine->output->post(out); out=NULL; } engine->output->post(new ControlEvent(End,packet->stream)); } return DecoderOk; }