void xtr_oggkate_c::handle_frame(xtr_frame_t &f) { ogg_packet op; op.b_o_s = 0; op.e_o_s = (f.frame->get_size() == 1) && (f.frame->get_buffer()[0] == 0x7f); op.packetno = m_packetno; op.packet = f.frame->get_buffer(); op.bytes = f.frame->get_size(); /* we encode the backlink in the granulepos */ float f_timecode = f.timecode / 1000000000.0; int64_t g_backlink = 0; if (op.bytes >= static_cast<long>(1 + 3 * sizeof(int64_t))) g_backlink = get_uint64_le(op.packet + 1 + 2 * sizeof(int64_t)); float f_backlink = g_backlink * (float)m_kate_id_header.gden / m_kate_id_header.gnum; float f_base = f_timecode - f_backlink; float f_offset = f_timecode - f_base; int64_t g_base = (int64_t)(f_base * m_kate_id_header.gnum / m_kate_id_header.gden); int64_t g_offset = (int64_t)(f_offset * m_kate_id_header.gnum / m_kate_id_header.gden); op.granulepos = (g_base << m_kate_id_header.kfgshift) | g_offset; ogg_stream_packetin(&m_os, &op); flush_pages(); /* Kate is a data packet per page */ ++m_packetno; }
file_status_e ivf_reader_c::read(generic_packetizer_c *, bool) { size_t remaining_bytes = m_size - m_in->getFilePointer(); ivf::frame_header_t header; if ((sizeof(ivf::frame_header_t) > remaining_bytes) || (m_in->read(&header, sizeof(ivf::frame_header_t)) != sizeof(ivf::frame_header_t))) return flush_packetizers(); remaining_bytes -= sizeof(ivf::frame_header_t); uint32_t frame_size = get_uint32_le(&header.frame_size); if (remaining_bytes < frame_size) { m_in->setFilePointer(0, seek_end); return flush_packetizers(); } memory_cptr buffer = memory_c::alloc(frame_size); if (m_in->read(buffer->get_buffer(), frame_size) < frame_size) { m_in->setFilePointer(0, seek_end); return flush_packetizers(); } int64_t timestamp = get_uint64_le(&header.timestamp) * 1000000000ull * m_frame_rate_den / m_frame_rate_num; mxverb(3, boost::format("r_ivf.cpp: key %5% header.ts %1% num %2% den %3% res %4%\n") % get_uint64_le(&header.timestamp) % m_frame_rate_num % m_frame_rate_den % timestamp % ivf::is_keyframe(buffer)); PTZR0->process(new packet_t(buffer, timestamp)); return FILE_STATUS_MOREDATA; }
uint64_t mm_io_c::read_uint64_le() { unsigned char buffer[8]; if (read(buffer, 8) != 8) throw mtx::mm_io::end_of_file_x(); return get_uint64_le(buffer); }
static bool set_msadpcm_coeffs(unsigned char *buf) { int i; int num; uint64_t size; buf += 16; /* skip 'fmt ' GUID */ size = get_uint64_le(buf); if (size < 50) { DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%d < 50\n", (int)size); return false; } /* get nNumCoef */ buf += 28; num = buf[0] | (buf[1] << 8); /* * In many case, nNumCoef is 7. * Depending upon the encoder, as for this value there is a possibility of * increasing more. * If you found the file where this value exceeds 7, please report. */ if (num != MSADPCM_NUM_COEFF) { DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num); return false; } /* get aCoeffs */ buf += 2; for (i = 0; i < MSADPCM_NUM_COEFF; i++) { format.coeffs[i][0] = buf[0] | (SE(buf[1]) << 8); format.coeffs[i][1] = buf[2] | (SE(buf[3]) << 8); buf += 4; } return true; }
void xtr_oggkate_c::handle_frame(memory_cptr &frame, KaxBlockAdditions *, int64_t timecode, int64_t, int64_t, int64_t, bool, bool, bool) { m_content_decoder.reverse(frame, CONTENT_ENCODING_SCOPE_BLOCK); ogg_packet op; op.b_o_s = 0; op.e_o_s = (frame->get_size() == 1) && (frame->get_buffer()[0] == 0x7f); op.packetno = m_packetno; op.packet = frame->get_buffer(); op.bytes = frame->get_size(); /* we encode the backlink in the granulepos */ float f_timecode = timecode / 1000000000.0; int64_t g_backlink = 0; if (op.bytes >= static_cast<long>(1 + 3 * sizeof(int64_t))) g_backlink = get_uint64_le(op.packet + 1 + 2 * sizeof(int64_t)); float f_backlink = g_backlink * (float)m_kate_id_header.gden / m_kate_id_header.gnum; float f_base = f_timecode - f_backlink; float f_offset = f_timecode - f_base; int64_t g_base = (int64_t)(f_base * m_kate_id_header.gnum / m_kate_id_header.gden); int64_t g_offset = (int64_t)(f_offset * m_kate_id_header.gnum / m_kate_id_header.gden); op.granulepos = (g_base << m_kate_id_header.kfgshift) | g_offset; ogg_stream_packetin(&m_os, &op); flush_pages(); /* Kate is a data packet per page */ ++m_packetno; }
/* this is the codec entry point */ enum codec_status codec_main(void) { int status; uint32_t decodedsamples; size_t n; int bufcount; int endofstream; unsigned char *buf; uint8_t *wavbuf; off_t firstblockposn; /* position of the first block in file */ const struct pcm_codec *codec; uint64_t size; /* Generic codec initialisation */ ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1); next_track: status = CODEC_OK; if (codec_init()) { DEBUGF("codec_init() error\n"); status = CODEC_ERROR; goto exit; } if (codec_wait_taginfo() != 0) goto done; codec_set_replaygain(ci->id3); /* Need to save offset for later use (cleared indirectly by advance_buffer) */ bytesdone = ci->id3->offset; /* get RIFF chunk header */ buf = ci->request_buffer(&n, 40); if (n < 40) { DEBUGF("request_buffer error\n"); status = CODEC_ERROR; goto done; } if ((memcmp(buf , WAVE64_GUID_RIFF, 16) != 0) || (memcmp(buf+24, WAVE64_GUID_WAVE, 16) != 0)) { status = CODEC_ERROR; goto done; } /* advance to first WAVE chunk */ ci->advance_buffer(40); firstblockposn = 40; ci->memset(&format, 0, sizeof(struct pcm_format)); format.is_signed = true; format.is_little_endian = true; decodedsamples = 0; codec = 0; /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */ while (true) { /* get WAVE chunk header */ buf = ci->request_buffer(&n, 1024); if (n < 8) { DEBUGF("data chunk request_buffer error\n"); /* no more chunks, 'data' chunk must not have been found */ status = CODEC_ERROR; goto done; } /* chunkSize */ size = get_uint64_le(buf+16) - 24; if (memcmp(buf, WAVE64_GUID_FMT, 16) == 0) { if (size < 16) { DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%d < 16\n", (int)size); status = CODEC_ERROR; goto done; } /* wFormatTag */ format.formattag=buf[24]|(buf[25]<<8); /* wChannels */ format.channels=buf[26]|(buf[27]<<8); /* skipping dwSamplesPerSec */ /* skipping dwAvgBytesPerSec */ /* wBlockAlign */ format.blockalign=buf[36]|(buf[37]<<8); /* wBitsPerSample */ format.bitspersample=buf[38]|(buf[39]<<8); if (format.formattag != WAVE_FORMAT_PCM) { if (size < 18) { /* this is not a fatal error with some formats, * we'll see later if we can't decode it */ DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) " "doesn't have ext. fmt descr (chunksize=%d<18).\n", (unsigned int)format.formattag, (int)size); } else { if (format.formattag != WAVE_FORMAT_EXTENSIBLE) format.samplesperblock = buf[42]|(buf[43]<<8); else { format.size = buf[40]|(buf[41]<<8); if (format.size < 22) { DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is " "missing extension\n"); status = CODEC_ERROR; goto done; } /* wValidBitsPerSample */ format.bitspersample = buf[42]|(buf[43]<<8); /* skipping dwChannelMask (4bytes) */ /* SubFormat (only get the first two bytes) */ format.formattag = buf[48]|(buf[49]<<8); } } } /* msadpcm specific */ if (format.formattag == WAVE_FORMAT_ADPCM) { if (!set_msadpcm_coeffs(buf)) { status = CODEC_ERROR; goto done; } } /* get codec */ codec = get_wave_codec(format.formattag); if (!codec) { DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n", (unsigned int) format.formattag); status = CODEC_ERROR; goto done; } /* riff 8bit linear pcm is unsigned */ if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8) format.is_signed = false; /* check format, and calculate chunk size */ if (!codec->set_format(&format)) { status = CODEC_ERROR; goto done; } } else if (memcmp(buf, WAVE64_GUID_DATA, 16) == 0) { format.numbytes = size; /* advance to start of data */ ci->advance_buffer(24); firstblockposn += 24; break; } else if (memcmp(buf, WAVE64_GUID_FACT, 16) == 0) { /* skip 'fact' chunk */ } else { DEBUGF("unknown Wave64 chunk: " "'%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'\n", buf[0], buf[1], buf[ 2], buf[ 3], buf[ 4], buf[ 5], buf[ 6], buf[ 7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); } /* go to next chunk (8byte bound) */ size += 24 + ((1 + ~size) & 0x07); ci->advance_buffer(size); firstblockposn += size; } if (!codec) { DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n"); status = CODEC_ERROR; goto done; } /* common format check */ if (format.channels == 0) { DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n"); status = CODEC_ERROR; goto done; } if (format.samplesperblock == 0) { DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n"); status = CODEC_ERROR; goto done; } if (format.blockalign == 0) { DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n"); status = CODEC_ERROR; goto done; } if (format.numbytes == 0) { DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n"); status = CODEC_ERROR; goto done; } /* check chunksize */ if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels > PCM_SAMPLE_SIZE) format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign; if (format.chunksize == 0) { DEBUGF("CODEC_ERROR: chunksize is 0\n"); status = CODEC_ERROR; goto done; } ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); if (format.channels == 2) { ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); } else if (format.channels == 1) { ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); } else { DEBUGF("CODEC_ERROR: more than 2 channels\n"); status = CODEC_ERROR; goto done; } /* make sure we're at the correct offset */ if (bytesdone > (uint32_t) firstblockposn) { /* Round down to previous block */ struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn, PCM_SEEK_POS, &read_buffer); if (newpos->pos > format.numbytes) goto done; if (ci->seek_buffer(firstblockposn + newpos->pos)) { bytesdone = newpos->pos; decodedsamples = newpos->samples; } ci->seek_complete(); } else { /* already where we need to be */ bytesdone = 0; } /* The main decoder loop */ endofstream = 0; while (!endofstream) { ci->yield(); if (ci->stop_codec || ci->new_track) { break; } if (ci->seek_time) { struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, &read_buffer); if (newpos->pos > format.numbytes) break; if (ci->seek_buffer(firstblockposn + newpos->pos)) { bytesdone = newpos->pos; decodedsamples = newpos->samples; } ci->seek_complete(); } wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); if (n == 0) break; /* End of stream */ if (bytesdone + n > format.numbytes) { n = format.numbytes - bytesdone; endofstream = 1; } status = codec->decode(wavbuf, n, samples, &bufcount); if (status == CODEC_ERROR) { DEBUGF("codec error\n"); goto done; } ci->pcmbuf_insert(samples, NULL, bufcount); ci->advance_buffer(n); bytesdone += n; decodedsamples += bufcount; if (bytesdone >= format.numbytes) endofstream = 1; ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); } status = CODEC_OK; done: if (ci->request_next_track()) goto next_track; exit: return status; }