| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006-2007, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bdemux_ts.c $ |
|---|
| 11 | * $brcm_Revision: 2 $ |
|---|
| 12 | * $brcm_Date: 2/16/07 10:11a $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * MPEG-2 Demux library |
|---|
| 17 | * |
|---|
| 18 | * Revision History: |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log: /BSEAV/lib/utils/bdemux_ts.c $ |
|---|
| 21 | * |
|---|
| 22 | * 2 2/16/07 10:11a vsilyaev |
|---|
| 23 | * PR 27443: Optimized TS parser |
|---|
| 24 | * |
|---|
| 25 | * 1 10/16/06 11:35a vsilyaev |
|---|
| 26 | * PR 24956 PR 24844: Software MPEG-2 TS and PES demultiplexor |
|---|
| 27 | * |
|---|
| 28 | * |
|---|
| 29 | *******************************************************************************/ |
|---|
| 30 | #include "bstd.h" |
|---|
| 31 | #include "bdemux_ts.h" |
|---|
| 32 | #include "biobits.h" |
|---|
| 33 | BDBG_MODULE(bdemux_ts); |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | BDBG_OBJECT_ID(bdemux_ts); |
|---|
| 37 | void |
|---|
| 38 | bdemux_ts_init(bdemux_ts *ts, uint16_t pid) |
|---|
| 39 | { |
|---|
| 40 | BDBG_ASSERT(ts); |
|---|
| 41 | BDBG_OBJECT_INIT(ts, bdemux_ts); |
|---|
| 42 | ts->pid = pid; |
|---|
| 43 | ts->continuity_counter = 0; |
|---|
| 44 | ts->flags = 0; |
|---|
| 45 | |
|---|
| 46 | /* verify B_GET_BITS */ |
|---|
| 47 | BDBG_CASSERT(B_GET_BITS(0xDE,7,4)==0xD); |
|---|
| 48 | BDBG_CASSERT(B_GET_BITS(0xDE,3,0)==0xE); |
|---|
| 49 | BDBG_CASSERT(B_GET_BITS(0xDE,3,3)==0x1); |
|---|
| 50 | BDBG_CASSERT(B_GET_BITS(0xDE,1,1)==0x1); |
|---|
| 51 | BDBG_CASSERT(B_GET_BITS(0xDE,0,0)==0x0); |
|---|
| 52 | BDBG_CASSERT(B_GET_BITS(0xDE,6,0)==0x5E); |
|---|
| 53 | BDBG_CASSERT(!B_GET_BIT(0xDE,0)); |
|---|
| 54 | BDBG_CASSERT(B_GET_BIT(0xDE,1)); |
|---|
| 55 | BDBG_CASSERT(B_GET_BIT(0xDE,7)); |
|---|
| 56 | BDBG_CASSERT(!B_GET_BIT(0xDE,5)); |
|---|
| 57 | |
|---|
| 58 | BDBG_CASSERT(B_SET_BIT( _ , 1, 0) == 1); |
|---|
| 59 | BDBG_CASSERT(B_SET_BIT( _ , 2, 0) == 1); |
|---|
| 60 | BDBG_CASSERT(B_SET_BIT( _ , 1, 1) == 2); |
|---|
| 61 | BDBG_CASSERT(B_SET_BIT( _ , 1, 31) == (1u<<31)); |
|---|
| 62 | BDBG_CASSERT(B_SET_BITS( _ , 1, 0, 0) == 1); |
|---|
| 63 | BDBG_CASSERT(B_SET_BITS( _ , 1, 1, 0) == 1); |
|---|
| 64 | BDBG_CASSERT(B_SET_BITS( _ , 1, 1, 1) == 2); |
|---|
| 65 | BDBG_CASSERT(B_SET_BITS( _ , 0x55, 7, 1) == 0xAA); |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | #define B_NEXT_CONT_COUNTER(c) (((c)+1)&0x0F) |
|---|
| 70 | |
|---|
| 71 | int |
|---|
| 72 | bdemux_ts_feed(bdemux_ts *ts, const uint8_t *data, size_t len) |
|---|
| 73 | { |
|---|
| 74 | size_t left; |
|---|
| 75 | unsigned ts_continuity_counter; |
|---|
| 76 | |
|---|
| 77 | BDBG_OBJECT_ASSERT(ts, bdemux_ts); |
|---|
| 78 | BDBG_ASSERT(data); |
|---|
| 79 | BDBG_ASSERT(((unsigned long)data&3)==0); /* check that data is 32 bit alligned */ |
|---|
| 80 | BDBG_ASSERT(len>0); |
|---|
| 81 | BDBG_ASSERT((len%B_TS_PKT_LEN)==0); |
|---|
| 82 | |
|---|
| 83 | /* ISO/IEC 13818-1 */ |
|---|
| 84 | |
|---|
| 85 | ts_continuity_counter = ts->continuity_counter; |
|---|
| 86 | for(left=len;left>=B_TS_PKT_LEN;left-=B_TS_PKT_LEN,data+=B_TS_PKT_LEN) { |
|---|
| 87 | uint32_t word; |
|---|
| 88 | unsigned pid; |
|---|
| 89 | unsigned adaptation_field_control; |
|---|
| 90 | unsigned continuity_counter; |
|---|
| 91 | unsigned off; /* offset into the transport packet */ |
|---|
| 92 | unsigned flags = ts->flags; |
|---|
| 93 | |
|---|
| 94 | if (data[0]!=B_TS_SYNC) { |
|---|
| 95 | goto err_out_of_sync; |
|---|
| 96 | } |
|---|
| 97 | BDBG_ASSERT(data[0]==B_TS_SYNC); |
|---|
| 98 | |
|---|
| 99 | /* Table 2-3 -- ITU-T Rec. H.222.0 | ISO/IEC 13818 transport packet */ |
|---|
| 100 | word = B_TS_LOAD16(data, 1); |
|---|
| 101 | pid = B_GET_BITS(word, 12, 0); |
|---|
| 102 | if (pid!=ts->pid) { |
|---|
| 103 | BDBG_MSG(("bdemux_ts_feed: %#lx wrong pid %#x(%#x)", (unsigned long)ts, (unsigned)pid, (unsigned)ts->pid)); |
|---|
| 104 | goto wrong_pid; |
|---|
| 105 | } |
|---|
| 106 | if (B_GET_BIT(word, 15) /* transport_error_indicator */) { |
|---|
| 107 | goto ts_error; |
|---|
| 108 | } |
|---|
| 109 | if (B_GET_BIT(word, 14) /* transport_error_indicator */) { |
|---|
| 110 | flags |= BDEMUX_TS_PAYLOAD_UNIT_START; |
|---|
| 111 | } |
|---|
| 112 | word = B_TS_LOAD8(data, 3); |
|---|
| 113 | continuity_counter = B_GET_BITS(word, 3, 0); |
|---|
| 114 | adaptation_field_control = B_GET_BITS(word, 5, 4); |
|---|
| 115 | if (continuity_counter != ts_continuity_counter) { |
|---|
| 116 | goto cc_error; |
|---|
| 117 | } |
|---|
| 118 | after_cc: |
|---|
| 119 | off = 4; |
|---|
| 120 | if (adaptation_field_control==0x02 || adaptation_field_control==0x03) { |
|---|
| 121 | goto adaptation_payload; |
|---|
| 122 | } |
|---|
| 123 | after_adaptation_payload: |
|---|
| 124 | if (adaptation_field_control==0x01) { |
|---|
| 125 | #if 1 |
|---|
| 126 | bdemux_ts_action action = bdemux_ts_data(ts, flags, data+off, B_TS_PKT_LEN-off); |
|---|
| 127 | #else |
|---|
| 128 | bdemux_ts_action action = bdemux_ts_action_consume; |
|---|
| 129 | #endif |
|---|
| 130 | flags = 0; /* clear flags */ |
|---|
| 131 | if (action==bdemux_ts_action_hold) { |
|---|
| 132 | break; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | next_cc: |
|---|
| 136 | ts_continuity_counter = B_NEXT_CONT_COUNTER(continuity_counter); |
|---|
| 137 | done: |
|---|
| 138 | ts->flags = flags; /* save flags */ |
|---|
| 139 | wrong_pid: |
|---|
| 140 | ; |
|---|
| 141 | continue; |
|---|
| 142 | ts_error: |
|---|
| 143 | BDBG_WRN(("bdemux_ts_feed: %#lx transport_error_indicator", (unsigned long)ts)); |
|---|
| 144 | flags |= BDEMUX_TS_ERROR; |
|---|
| 145 | goto done; |
|---|
| 146 | cc_error: |
|---|
| 147 | if (!(adaptation_field_control==0x00 || adaptation_field_control==0x02)) { |
|---|
| 148 | if(B_NEXT_CONT_COUNTER(continuity_counter)==ts_continuity_counter) { |
|---|
| 149 | BDBG_WRN(("bdemux_ts_feed: %#lx continuity counter duplicate %#x(%#x)", (unsigned long)ts, (unsigned)continuity_counter, (unsigned)ts->continuity_counter)); |
|---|
| 150 | goto done; |
|---|
| 151 | } |
|---|
| 152 | BDBG_MSG(("bdemux_ts_feed: %#lx continuity counter error %#x(%#x)", (unsigned long)ts, (unsigned)continuity_counter, (unsigned)ts->continuity_counter)); |
|---|
| 153 | flags |= BDEMUX_TS_DISCONTINUITY; |
|---|
| 154 | } |
|---|
| 155 | goto after_cc; |
|---|
| 156 | adaptation_payload: |
|---|
| 157 | { |
|---|
| 158 | unsigned adaptation_field_length = B_TS_LOAD8(data, 4); |
|---|
| 159 | if (adaptation_field_length>0) { |
|---|
| 160 | word = B_TS_LOAD8(data, 5); |
|---|
| 161 | if (B_GET_BIT(word, 7) /* discontinuity_indicator */) { |
|---|
| 162 | flags |= BDEMUX_TS_MARKED_DISCONTINUITY; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | off = 5 + adaptation_field_length; |
|---|
| 166 | if (off>B_TS_PKT_LEN) { |
|---|
| 167 | BDBG_WRN(("bdemux_ts_feed: %#lx invalid adaptation_field_length=%u", (unsigned long)ts, adaptation_field_length)); |
|---|
| 168 | goto next_cc; |
|---|
| 169 | } |
|---|
| 170 | adaptation_field_control &= ~2; /* clear bit 1, e.g. 3 would become 1 */ |
|---|
| 171 | } |
|---|
| 172 | goto after_adaptation_payload; |
|---|
| 173 | } |
|---|
| 174 | ts->continuity_counter = ts_continuity_counter; |
|---|
| 175 | BDBG_ASSERT(len>=left); |
|---|
| 176 | return len-left; |
|---|
| 177 | err_out_of_sync: |
|---|
| 178 | BDBG_ERR(("out of_sync %02x:%02x:%02x:%02x(%#lx) at %u:%u", data[0], data[1], data[2], data[3], (unsigned long)data, len-left, len)); |
|---|
| 179 | ts->continuity_counter = ts_continuity_counter; |
|---|
| 180 | if(len==left) { |
|---|
| 181 | return -B_TS_PKT_LEN; |
|---|
| 182 | } |
|---|
| 183 | return -(len-left); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|