From 963a56274b2df9573674cd624226028064d840c0 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 12 Sep 2011 20:41:19 +0200 Subject: [PATCH] Refactoring --- src/input/r_mpeg_ts.cpp | 193 +++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 94 deletions(-) diff --git a/src/input/r_mpeg_ts.cpp b/src/input/r_mpeg_ts.cpp index 4a773dee7..143980608 100644 --- a/src/input/r_mpeg_ts.cpp +++ b/src/input/r_mpeg_ts.cpp @@ -736,36 +736,38 @@ mpeg_ts_reader_c::parse_packet(int id, unsigned char *buf) { unsigned char payload_size = m_detected_packet_size - (payload - (unsigned char *)hdr) - 4; + mpeg_ts_track_ptr &track = tracks[tidx]; + if (hdr->payload_unit_start_indicator) { - if (tracks[tidx]->type == PAT_TYPE || tracks[tidx]->type == PMT_TYPE) { + if (track->type == PAT_TYPE || track->type == PMT_TYPE) { if ((1 + *payload) > payload_size) return false; table_data = (mpeg_ts_pat_t *)(payload + 1 + *payload); payload_size -= 1 + *payload; - tracks[tidx]->payload_size = SECTION_LENGTH(table_data) + 3; + track->payload_size = SECTION_LENGTH(table_data) + 3; payload = (unsigned char *)table_data; } else { pes_data = (mpeg_ts_pes_header_t *)payload; - tracks[tidx]->payload_size = ((uint16_t) (pes_data->PES_packet_length_msb) << 8) | (pes_data->PES_packet_length_lsb); + track->payload_size = ((uint16_t) (pes_data->PES_packet_length_msb) << 8) | (pes_data->PES_packet_length_lsb); #if 1 - if (tracks[tidx]->payload_size > 0) - tracks[tidx]->payload_size = tracks[tidx]->payload_size - 3 - pes_data->PES_header_data_length; + if (track->payload_size > 0) + track->payload_size = track->payload_size - 3 - pes_data->PES_header_data_length; #else - tracks[tidx]->payload_size = 0; + track->payload_size = 0; #endif - if ( (tracks[tidx]->payload_size == 0) - && (tracks[tidx]->payload->get_size() != 0) + if ( (track->payload_size == 0) + && (track->payload->get_size() != 0) && (input_status == INPUT_READ)) { - tracks[tidx]->payload_size = tracks[tidx]->payload->get_size(); - mxverb(3, boost::format("mpeg_ts: Table/PES completed (%1%) for PID %2%\n") % tracks[tidx]->payload_size % table_pid); + track->payload_size = track->payload->get_size(); + mxverb(3, boost::format("mpeg_ts: Table/PES completed (%1%) for PID %2%\n") % track->payload_size % table_pid); send_to_packetizer(tidx); } mxverb(4, boost::format(" PES info: stream_id = %1%\n") % (int)pes_data->stream_id); - mxverb(4, boost::format(" PES info: PES_packet_length = %1%\n") % tracks[tidx]->payload_size); + mxverb(4, boost::format(" PES info: PES_packet_length = %1%\n") % track->payload_size); //mxverb(3, boost::format("scrambling = %d, priority = %d, alignment = %d, copyright = %d, copy = %d\n", pes_hdr->PES_scrambling_control, pes_hdr->PES_priority, pes_hdr->data_alignment_indicator, pes_hdr->copyright, pes_hdr->original_or_copy); mxverb(4, boost::format(" PES info: PTS_DTS = %1% ESCR = %2% ES_rate = %3%\n") % (int)pes_data->PTS_DTS % (int)pes_data->ESCR % (int)pes_data->ES_rate); mxverb(4, boost::format(" PES info: DSM_trick_mode = %1%, add_copy = %2%, CRC = %3%, ext = %4%\n") % (int)pes_data->DSM_trick_mode % (int)pes_data->additional_copy_info % (int)pes_data->PES_CRC % (int)pes_data->PES_extension); @@ -780,76 +782,78 @@ mpeg_ts_reader_c::parse_packet(int id, unsigned char *buf) { m_global_timecode_offset = PTS; } - if (PTS == tracks[tidx]->timecode) { + if (PTS == track->timecode) { mxverb(3, boost::format(" Adding PES with same PTS as previous !!\n")); - tracks[tidx]->payload->add(payload, payload_size); + track->payload->add(payload, payload_size); return true; - } else if ((0 != tracks[tidx]->payload->get_size()) && (INPUT_READ == input_status)) + } else if ((0 != track->payload->get_size()) && (INPUT_READ == input_status)) send_to_packetizer(tidx); - tracks[tidx]->timecode = PTS; + track->timecode = PTS; - mxverb(3, boost::format(" PTS found: %1%\n") % tracks[tidx]->timecode); + mxverb(3, boost::format(" PTS found: %1%\n") % track->timecode); } payload = &pes_data->PES_header_data_length + pes_data->PES_header_data_length + 1; payload_size = ((unsigned char *)hdr + m_detected_packet_size - 4) - (unsigned char *) payload; // this condition is for ES probing when there is still not enough data for detection - if (tracks[tidx]->payload_size == 0 && tracks[tidx]->payload->get_size() != 0) - tracks[tidx]->data_ready = true; + if (track->payload_size == 0 && track->payload->get_size() != 0) + track->data_ready = true; } - tracks[tidx]->continuity_counter = CONTINUITY_COUNTER(hdr); + track->continuity_counter = CONTINUITY_COUNTER(hdr); - if (tracks[tidx]->payload_size != 0 && (payload_size + tracks[tidx]->payload->get_size()) > tracks[tidx]->payload_size) - payload_size = tracks[tidx]->payload_size - tracks[tidx]->payload->get_size(); + if (track->payload_size != 0 && (payload_size + track->payload->get_size()) > track->payload_size) + payload_size = track->payload_size - track->payload->get_size(); //mxverb(3, boost::format("mpeg_ts: Payload size: %1% - PID: %2%\n") % (int)payload_size % table_pid); } else { /* Check continuity counter */ - if (tracks[tidx]->payload->get_size() != 0) { + if (track->payload->get_size() != 0) { if (!adf_discontinuity_indicator) { - tracks[tidx]->continuity_counter++; - tracks[tidx]->continuity_counter %= 16; - if (CONTINUITY_COUNTER(hdr) != tracks[tidx]->continuity_counter) { - //delete tracks[tidx].table; + track->continuity_counter++; + track->continuity_counter %= 16; + if (CONTINUITY_COUNTER(hdr) != track->continuity_counter) { + //delete track.table; mxverb(3, boost::format("mpeg_ts: Continuity error on PID: %1%. Continue anyway...\n") % table_pid); - tracks[tidx]->continuity_counter = CONTINUITY_COUNTER(hdr); - //tracks[tidx]->payload->remove(tracks[tidx]->payload->get_size()); - //tracks[tidx]->processed = false; - //tracks[tidx]->data_ready = false; - //tracks[tidx]->payload_size = 0; + track->continuity_counter = CONTINUITY_COUNTER(hdr); + //track->payload->remove(track->payload->get_size()); + //track->processed = false; + //track->data_ready = false; + //track->payload_size = 0; //return false; } } else - tracks[tidx]->continuity_counter = CONTINUITY_COUNTER(hdr); + track->continuity_counter = CONTINUITY_COUNTER(hdr); } else return false; - if (tracks[tidx]->payload_size != 0 && payload_size > tracks[tidx]->payload_size - tracks[tidx]->payload->get_size()) - payload_size = tracks[tidx]->payload_size - tracks[tidx]->payload->get_size(); + if (track->payload_size != 0 && payload_size > track->payload_size - track->payload->get_size()) + payload_size = track->payload_size - track->payload->get_size(); } if ((buf + m_detected_packet_size) < (payload + payload_size)) payload_size = buf + m_detected_packet_size - payload; - - tracks[tidx]->payload->add(payload, payload_size); - //mxverb(3, boost::format("mpeg_ts: ---------> Written %1% bytes for PID %2%\n") % payload_size % table_pid); - - if (tracks[tidx]->payload->get_size() == 0) + if (0 == payload_size) return false; - if (tracks[tidx]->payload->get_size() == tracks[tidx]->payload_size) - tracks[tidx]->data_ready = true; + track->payload->add(payload, payload_size); + //mxverb(3, boost::format("mpeg_ts: ---------> Written %1% bytes for PID %2%\n") % payload_size % table_pid); - /*if (tracks[tidx]->payload->get_size() == tracks[tidx]->payload_size || - (input_status == INPUT_PROBE && tracks[tidx]->payload_size == 0 && tracks[tidx]->type == ES_VIDEO_TYPE) ) {*/ - if (!tracks[tidx]->data_ready) + if (track->payload->get_size() == 0) + return false; + + if (track->payload->get_size() == track->payload_size) + track->data_ready = true; + + /*if (track->payload->get_size() == track->payload_size || + (input_status == INPUT_PROBE && track->payload_size == 0 && track->type == ES_VIDEO_TYPE) ) {*/ + if (!track->data_ready) return true; - mxverb(3, boost::format("mpeg_ts: Table/PES completed (%1%) for PID %2%\n") % tracks[tidx]->payload->get_size() % table_pid); + mxverb(3, boost::format("mpeg_ts: Table/PES completed (%1%) for PID %2%\n") % track->payload->get_size() % table_pid); if (input_status != INPUT_PROBE) { if (input_status == INPUT_READ) // PES completed, set track to quicly send it to the rightpacketizer @@ -857,34 +861,34 @@ mpeg_ts_reader_c::parse_packet(int id, unsigned char *buf) { return true; } - if (tracks[tidx]->type == PAT_TYPE) - ret = parse_pat(tracks[tidx]->payload->get_buffer()); + if (track->type == PAT_TYPE) + ret = parse_pat(track->payload->get_buffer()); - else if (tracks[tidx]->type == PMT_TYPE) - ret = parse_pmt(tracks[tidx]->payload->get_buffer()); + else if (track->type == PMT_TYPE) + ret = parse_pmt(track->payload->get_buffer()); - else if (tracks[tidx]->type == ES_VIDEO_TYPE) { - if ((FOURCC('M', 'P', 'G', '1') == tracks[tidx]->fourcc) || (FOURCC('M', 'P', 'G', '2') == tracks[tidx]->fourcc)) - ret = new_stream_v_mpeg_1_2(tracks[tidx]->payload->get_buffer(), tracks[tidx]->payload->get_size(), tracks[tidx]); - else if (FOURCC('A', 'V', 'C', '1') == tracks[tidx]->fourcc) - ret = new_stream_v_avc(tracks[tidx]->payload->get_buffer(), tracks[tidx]->payload->get_size(), tracks[tidx]); + else if (track->type == ES_VIDEO_TYPE) { + if ((FOURCC('M', 'P', 'G', '1') == track->fourcc) || (FOURCC('M', 'P', 'G', '2') == track->fourcc)) + ret = new_stream_v_mpeg_1_2(track->payload->get_buffer(), track->payload->get_size(), track); + else if (FOURCC('A', 'V', 'C', '1') == track->fourcc) + ret = new_stream_v_avc(track->payload->get_buffer(), track->payload->get_size(), track); - } else if (tracks[tidx]->type == ES_AUDIO_TYPE) { - if (FOURCC('M', 'P', '2', ' ') == tracks[tidx]->fourcc) - ret = new_stream_a_mpeg(tracks[tidx]->payload->get_buffer(), tracks[tidx]->payload->get_size(), tracks[tidx]); - else if (FOURCC('A', 'C', '3', ' ') == tracks[tidx]->fourcc) - ret = new_stream_a_ac3(tracks[tidx]->payload->get_buffer(), tracks[tidx]->payload->get_size(), tracks[tidx]); - else if (FOURCC('T', 'R', 'H', 'D') == tracks[tidx]->fourcc) - ret = new_stream_a_truehd(tracks[tidx]->payload->get_buffer(), tracks[tidx]->payload->get_size(), tracks[tidx]); + } else if (track->type == ES_AUDIO_TYPE) { + if (FOURCC('M', 'P', '2', ' ') == track->fourcc) + ret = new_stream_a_mpeg(track->payload->get_buffer(), track->payload->get_size(), track); + else if (FOURCC('A', 'C', '3', ' ') == track->fourcc) + ret = new_stream_a_ac3(track->payload->get_buffer(), track->payload->get_size(), track); + else if (FOURCC('T', 'R', 'H', 'D') == track->fourcc) + ret = new_stream_a_truehd(track->payload->get_buffer(), track->payload->get_size(), track); - } else if (tracks[tidx]->type == ES_SUBT_TYPE) + } else if (track->type == ES_SUBT_TYPE) ret = 0; if (ret == 0) { - if (tracks[tidx]->type == PAT_TYPE || tracks[tidx]->type == PMT_TYPE) + if (track->type == PAT_TYPE || track->type == PMT_TYPE) tracks.erase(tracks.begin() + tidx); else { - tracks[tidx]->processed = true; + track->processed = true; es_to_process--; mxverb(3, boost::format("mpeg_ts: ES to process: %1%\n") % es_to_process); } @@ -892,14 +896,14 @@ mpeg_ts_reader_c::parse_packet(int id, unsigned char *buf) { } else if (ret == FILE_STATUS_MOREDATA) { mxverb(3, boost::format("mpeg_ts: Need more data to probe ES\n")); - tracks[tidx]->processed = false; - tracks[tidx]->data_ready = false; + track->processed = false; + track->data_ready = false; } else { mxverb(3, boost::format("mpeg_ts: Failed to parse packet. Reset and retry\n")); - tracks[tidx]->payload->remove(tracks[tidx]->payload->get_size()); - tracks[tidx]->processed = false; - tracks[tidx]->payload_size = 0; + track->payload->remove(track->payload->get_size()); + track->processed = false; + track->payload_size = 0; return false; } @@ -908,21 +912,18 @@ mpeg_ts_reader_c::parse_packet(int id, unsigned char *buf) { void mpeg_ts_reader_c::create_packetizer(int64_t id) { - char type = 'v'; - if (tracks[id]->type == ES_AUDIO_TYPE) - type = 'a'; - else if (tracks[id]->type == ES_SUBT_TYPE) - type = 's'; - if ((0 > id) || (tracks.size() <= static_cast(id))) return; - if (0 == tracks[id]->ptzr) - return; - if (!demuxing_requested(type, id)) + + mpeg_ts_track_ptr &track = tracks[id]; + char type = ES_AUDIO_TYPE == track->type ? 'a' + : ES_SUBT_TYPE == track->type ? 's' + : 'v'; + + if ((0 == track->ptzr) || !demuxing_requested(type, id)) return; m_ti.m_id = id; - mpeg_ts_track_ptr &track = tracks[id]; if (ES_AUDIO_TYPE == track->type) { if ( (FOURCC('M', 'P', '1', ' ') == track->fourcc) || (FOURCC('M', 'P', '2', ' ') == track->fourcc) @@ -1036,28 +1037,32 @@ mpeg_ts_reader_c::send_to_packetizer(int tid) { if (tid == -1) return -1; + mpeg_ts_track_ptr &track = tracks[tid]; + //if (tid == 0) - // m_file->write(tracks[tid]->payload->get_buffer(), tracks[tid]->payload_size); - if (tracks[tid]->timecode < m_global_timecode_offset) - tracks[tid]->timecode = 0; + // m_file->write(track->payload->get_buffer(), track->payload_size); + if (track->timecode < m_global_timecode_offset) + track->timecode = 0; else - tracks[tid]->timecode = (uint64_t)(tracks[tid]->timecode - m_global_timecode_offset) * 100000ll / 9; + track->timecode = (uint64_t)(track->timecode - m_global_timecode_offset) * 100000ll / 9; // WARNING WARNING WARNING - comment this to use source audio PTSs !!! - if ((tracks[tid]->type == ES_AUDIO_TYPE) && m_dont_use_audio_pts) - tracks[tid]->timecode = -1; + if ((track->type == ES_AUDIO_TYPE) && m_dont_use_audio_pts) + track->timecode = -1; - mxverb(3, boost::format("mpeg_ts: PTS in nanoseconds: %1%\n") % tracks[tid]->timecode); + mxverb(3, boost::format("mpeg_ts: PTS in nanoseconds: %1%\n") % track->timecode); - if (tracks[tid]->ptzr != -1) { - packet_t *new_packet = new packet_t(clone_memory(tracks[tid]->payload->get_buffer(), tracks[tid]->payload->get_size()), tracks[tid]->timecode); - PTZR(tracks[tid]->ptzr)->process(new_packet); + if (track->ptzr != -1) { + packet_t *new_packet = new packet_t(clone_memory(track->payload->get_buffer(), track->payload->get_size()), track->timecode); + PTZR(track->ptzr)->process(new_packet); } - //mxverb(3, boost::format("mpeg_ts: packet processed... (%1% bytes)\n") % tracks[tid]->payload->get_size()); - tracks[tid]->payload->remove(tracks[tid]->payload->get_size()); - tracks[tid]->processed = false; - tracks[tid]->data_ready = false; - tracks[tid]->payload_size = 0; + + //mxverb(3, boost::format("mpeg_ts: packet processed... (%1% bytes)\n") % track->payload->get_size()); + track->payload->remove(track->payload->get_size()); + track->processed = false; + track->data_ready = false; + track->payload_size = 0; + return 0; }