void program_map_section_free(program_map_section_t *pms) { if (pms == NULL) return; if (pms->descriptors != NULL) { vqarray_foreach(pms->descriptors, descriptor_free); vqarray_free(pms->descriptors); } if (pms->es_info) { vqarray_foreach(pms->es_info, (vqarray_functor_t) es_info_free); vqarray_free(pms->es_info); } free(pms); }
void es_info_free(elementary_stream_info_t *es) { if (es == NULL) return; if (es->descriptors != NULL) { vqarray_foreach(es->descriptors, descriptor_free); vqarray_free(es->descriptors); } free(es); }
void conditional_access_section_free(conditional_access_section_t *cas) { if (cas == NULL) return; if (cas->descriptors != NULL) { vqarray_foreach(cas->descriptors, (vqarray_functor_t)descriptor_free); vqarray_free(cas->descriptors); } free(cas); }
void mpeg2ts_stream_free(mpeg2ts_stream_t *m2s) { if (m2s == NULL) return; if (m2s->programs != NULL) { vqarray_foreach(m2s->programs, mpeg2ts_program_free); vqarray_free(m2s->programs); } if (m2s->pat != NULL) { program_association_section_free(m2s->pat); } free(m2s); }
void mpeg2ts_program_free(mpeg2ts_program_t *m2p) { if (m2p == NULL) return; if (m2p->pids != NULL) { vqarray_foreach(m2p->pids, pid_info_free); vqarray_free(m2p->pids); } if (m2p->pmt != NULL) { program_map_section_free(m2p->pmt); } free(m2p); }
int program_association_section_read(program_association_section_t *pas, uint8_t *buf, size_t buf_len, uint32_t payload_unit_start_indicator, psi_table_buffer_t *patBuffer) { vqarray_t *programs; int num_programs = 0; if (pas == NULL || buf == NULL) { SAFE_REPORT_TS_ERR(-1); return 0; } bs_t *b = NULL; if (!payload_unit_start_indicator && patBuffer->buffer == NULL) { // this TS packet is not start of table, and we have no cached table data LOG_WARN ("program_association_section_read: payload_unit_start_indicator not set and no cached data"); return 0; } if (payload_unit_start_indicator) { uint8_t payloadStartPtr = buf[0]; buf += (payloadStartPtr + 1); buf_len -= (payloadStartPtr + 1); LOG_DEBUG_ARGS ("program_association_section_read: payloadStartPtr = %d", payloadStartPtr); } // check for pat spanning multiple TS packets if (patBuffer->buffer != NULL) { LOG_DEBUG_ARGS ("program_association_section_read: patBuffer detected: patBufferAllocSz = %d, patBufferUsedSz = %d", patBuffer->bufferAllocSz, patBuffer->bufferUsedSz); size_t numBytesToCopy = buf_len; if (buf_len > (patBuffer->bufferAllocSz - patBuffer->bufferUsedSz)) { numBytesToCopy = patBuffer->bufferAllocSz - patBuffer->bufferUsedSz; } LOG_DEBUG_ARGS ("program_association_section_read: copying %d bytes to patBuffer", numBytesToCopy); memcpy (patBuffer->buffer + patBuffer->bufferUsedSz, buf, numBytesToCopy); patBuffer->bufferUsedSz += numBytesToCopy; if (patBuffer->bufferUsedSz < patBuffer->bufferAllocSz) { LOG_DEBUG ("program_association_section_read: patBuffer not yet full -- returning"); return 0; } b = bs_new(patBuffer->buffer, patBuffer->bufferUsedSz); } else { b = bs_new(buf, buf_len); } pas->table_id = bs_read_u8(b); if (pas->table_id != program_association_section) { LOG_ERROR_ARGS("Table ID in PAT is 0x%02X instead of expected 0x%02X", pas->table_id, program_association_section); reportAddErrorLogArgs("Table ID in PAT is 0x%02X instead of expected 0x%02X", pas->table_id, program_association_section); SAFE_REPORT_TS_ERR(-30); resetPSITableBuffer(patBuffer); bs_free (b); return 0; } // read byte 0 pas->section_syntax_indicator = bs_read_u1(b); if (!pas->section_syntax_indicator) { LOG_ERROR("section_syntax_indicator not set in PAT"); reportAddErrorLog("section_syntax_indicator not set in PAT"); SAFE_REPORT_TS_ERR(-31); resetPSITableBuffer(patBuffer); bs_free (b); return 0; } bs_skip_u(b, 3); // TODO read the zero bit, check it to be zero pas->section_length = bs_read_u(b, 12); if (pas->section_length > MAX_SECTION_LEN) { LOG_ERROR_ARGS("PAT section length is 0x%02X, larger than maximum allowed 0x%02X", pas->section_length, MAX_SECTION_LEN); reportAddErrorLogArgs("PAT section length is 0x%02X, larger than maximum allowed 0x%02X", pas->section_length, MAX_SECTION_LEN); SAFE_REPORT_TS_ERR(-32); resetPSITableBuffer(patBuffer); bs_free (b); return 0; } if (pas->section_length > bs_bytes_left(b)) { LOG_DEBUG ("program_association_section_read: Detected section spans more than one TS packet -- allocating buffer"); if (patBuffer->buffer != NULL) { // should never get here LOG_ERROR ("program_association_section_read: unexpected patBufffer"); reportAddErrorLog ("program_association_section_read: unexpected patBufffer"); resetPSITableBuffer(patBuffer); } patBuffer->bufferAllocSz = pas->section_length + 3; patBuffer->buffer = (uint8_t *)calloc (pas->section_length + 3, 1); memcpy (patBuffer->buffer, buf, buf_len); patBuffer->bufferUsedSz = buf_len; bs_free (b); return 0; } // read bytes 1,2 pas->transport_stream_id = bs_read_u16(b); // read bytes 3,4 bs_skip_u(b, 2); pas->version_number = bs_read_u(b, 5); pas->current_next_indicator = bs_read_u1(b); if (!pas->current_next_indicator) LOG_WARN("This PAT is not yet applicable/n"); // read byte 5 pas->section_number = bs_read_u8(b); pas->last_section_number = bs_read_u8(b); if (pas->section_number != 0 || pas->last_section_number != 0) LOG_WARN("Multi-section PAT is not supported yet/n"); // read bytes 6,7 num_programs = (pas->section_length - 5 - 4) / 4; // Programs listed in the PAT // explanation: section_length gives us the length from the end of section_length // we used 5 bytes for the mandatory section fields, and will use another 4 bytes for CRC // the remaining bytes contain program information, which is 4 bytes per iteration // It's much shorter in C :-) // Read the program loop, but ignore the NIT PID "program" programs = vqarray_new(); for (uint32_t i = 0; i < num_programs; i++) { program_info_t *prog = malloc(sizeof(program_info_t)); prog->program_number = bs_read_u16(b); if (prog->program_number == 0) { // Skip the NIT PID program (not a real program) free(prog); bs_skip_u(b, 16); continue; } bs_skip_u(b, 3); prog->program_map_PID = bs_read_u(b, 13); vqarray_add(programs, (vqarray_elem_t*)prog); } // This is our true number of programs pas->_num_programs = vqarray_length(programs); if (pas->_num_programs > 1) LOG_WARN_ARGS("%zd programs found, but only SPTS is fully supported. Patches are welcome.", pas->_num_programs); // Copy form our vqarray into the native array pas->programs = malloc(pas->_num_programs * sizeof(program_info_t)); for (uint32_t i = 0; i < pas->_num_programs; i++) { program_info_t* prog = (program_info_t*)vqarray_pop(programs); pas->programs[i] = *prog; free(prog); } vqarray_free(programs); pas->CRC_32 = bs_read_u32(b); // check CRC crc_t pas_crc = crc_init(); pas_crc = crc_update(pas_crc, buf, bs_pos(b) - 4); pas_crc = crc_finalize(pas_crc); if (pas_crc != pas->CRC_32) { LOG_ERROR_ARGS("PAT CRC_32 specified as 0x%08X, but calculated as 0x%08X", pas->CRC_32, pas_crc); reportAddErrorLogArgs("PAT CRC_32 specified as 0x%08X, but calculated as 0x%08X", pas->CRC_32, pas_crc); SAFE_REPORT_TS_ERR(-33); resetPSITableBuffer(patBuffer); bs_free (b); return 0; } else { // LOG_DEBUG("PAT CRC_32 checked successfully"); // don't enable unless you want to see this every ~100ms } bs_free(b); resetPSITableBuffer(patBuffer); return 1; }