109 | | switch (user_identifier) { |
110 | | case 0x44544731: // "DTG1" - AFD_data |
111 | | if (size < 1) |
112 | | return -1; |
113 | | skip_bits(&h->gb, 1); |
114 | | if (get_bits(&h->gb, 1)) { |
115 | | skip_bits(&h->gb, 6); |
116 | | if (size < 2) |
117 | | return -1; |
118 | | skip_bits(&h->gb, 4); |
119 | | dtg_active_format = get_bits(&h->gb, 4); |
120 | | h->avctx->dtg_active_format = dtg_active_format; |
121 | | } else { |
122 | | skip_bits(&h->gb, 6); |
| 105 | /*Ignore first 3 bytes |
| 106 | p[0] = 0xB5 ITU-T Country Code |
| 107 | p[1-2] = 0x0031 ITU-T Provider code */ |
| 108 | p += 3; //ignore 0xB50031 |
| 109 | buf_end = p + buf_size-3; |
| 110 | |
| 111 | /*we parse the DTG active format information */ |
| 112 | if (buf_end - p >= 5 && |
| 113 | p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') { |
| 114 | int flags = p[4]; |
| 115 | p += 5; |
| 116 | if (flags & 0x80) { //skip event id |
| 117 | p += 2; |
| 118 | } |
| 119 | if (flags & 0x40) { |
| 120 | if (buf_end - p < 1) |
| 121 | return; |
| 122 | s->avctx->dtg_active_format = p[0] & 0x0f; |
| 123 | } |
| 124 | } else if (buf_end - p >= 6 && |
| 125 | p[0] == 0x43 && p[1] == 0x43 && p[2] == 0x01 && p[3] == 0xf8 && |
| 126 | p[4] == 0x9e) { |
| 127 | #undef fprintf |
| 128 | int atsc_cnt_loc = s->tmp_atsc_cc_len; |
| 129 | uint8_t real_count = 0; |
| 130 | unsigned int i; |
| 131 | |
| 132 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&real_count); |
| 133 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data |
| 134 | |
| 135 | for (i=5; i < (buf_end - p - 2) && |
| 136 | (s->tmp_atsc_cc_len + 3) < ATSC_CC_BUF_SIZE; i++) |
| 137 | { |
| 138 | if ((p[i]&0xfe) == 0xfe) // CC1&CC2 || CC3&CC4 |
| 139 | { |
| 140 | uint8_t type = (p[i] & 0x01) ^ 0x01; |
| 141 | uint8_t cc_data_1 = p[++i]; |
| 142 | uint8_t cc_data_2 = p[++i]; |
| 143 | uint8_t valid = 1; |
| 144 | uint8_t cc608_hdr = 0xf8 | (valid ? 0x04 : 0x00) | type; |
| 145 | real_count++; |
| 146 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr; |
| 147 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc_data_1; |
| 148 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc_data_2; |
| 149 | continue; |
| 172 | p += 5; |
| 173 | |
| 174 | if ((cclen <= buf_end - p) && ((cclen + blen) < ATSC_CC_BUF_SIZE)) { |
| 175 | uint8_t *dst = s->tmp_atsc_cc_buf + s->tmp_atsc_cc_len; |
| 176 | memcpy(dst, p, cclen); |
| 177 | s->tmp_atsc_cc_len += cclen; |
| 178 | } |
| 179 | } |
| 180 | else if (user_data_type_code == 0x04) { |
| 181 | // additional CEA-608 data, as per SCTE 21 |
| 182 | } |
| 183 | else if (user_data_type_code == 0x05) { |
| 184 | // luma PAM data, as per SCTE 21 |
| 185 | } |
| 186 | else if (user_data_type_code == 0x06) { |
| 187 | // bar data (letterboxing info) |
| 188 | } |
| 189 | } else if (buf_end - p >= 3 && p[0] == 0x03 && ((p[1]&0x7f) == 0x01)) { |
| 190 | // SCTE 20 encoding of CEA-608 |
| 191 | unsigned int cc_count = p[2]>>3; |
| 192 | unsigned int cc_bits = cc_count * 26; |
| 193 | unsigned int cc_bytes = (cc_bits + 7 - 3) / 8; |
| 194 | if (buf_end - p >= (2+cc_bytes) && (s->tmp_scte_cc_len + 2 + 3*cc_count) < SCTE_CC_BUF_SIZE) { |
| 195 | int scte_cnt_loc = s->tmp_scte_cc_len; |
| 196 | uint8_t real_count = 0, marker = 1, i; |
| 197 | GetBitContext gb; |
| 198 | init_get_bits(&gb, p+2, (buf_end-p-2) * sizeof(uint8_t)); |
| 199 | get_bits(&gb, 5); // swallow cc_count |
| 200 | s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = 0x40 | (0x1f&cc_count); |
| 201 | s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = 0x00; // em_data |
| 202 | for (i = 0; i < cc_count; i++) { |
| 203 | uint8_t valid, cc608_hdr; |
| 204 | uint8_t priority = get_bits(&gb, 2); |
| 205 | uint8_t field_no = get_bits(&gb, 2); |
| 206 | uint8_t line_offset = get_bits(&gb, 5); |
| 207 | uint8_t cc_data_1 = av_reverse[get_bits(&gb, 8)]; |
| 208 | uint8_t cc_data_2 = av_reverse[get_bits(&gb, 8)]; |
| 209 | uint8_t type = (1 == field_no) ? 0x00 : 0x01; |
| 210 | (void) priority; // we use all the data, don't need priority |
| 211 | marker &= get_bits(&gb, 1); |
| 212 | // dump if marker bit missing |
| 213 | valid = marker; |
| 214 | // ignore forbidden and repeated (3:2 pulldown) field numbers |
| 215 | valid = valid && (1 == field_no || 2 == field_no); |
| 216 | // ignore content not in line 21 |
| 217 | valid = valid && (11 == line_offset); |
| 218 | if (!valid) |
| 219 | continue; |
| 220 | cc608_hdr = 0xf8 | (valid ? 0x04 : 0x00) | type; |
| 221 | real_count++; |
| 222 | s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc608_hdr; |
| 223 | s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc_data_1; |
| 224 | s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc_data_2; |
| 225 | } |
| 226 | if (!real_count) |
| 227 | { |
| 228 | s->tmp_scte_cc_len = scte_cnt_loc; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | s->tmp_scte_cc_buf[scte_cnt_loc] = 0x40 | (0x1f&real_count); |
| 233 | s->tmp_scte_cc_len = scte_cnt_loc + 2 + 3 * real_count; |
| 234 | } |
| 235 | } |
| 236 | } else if (buf_end - p >= 11 && |
| 237 | p[0] == 0x05 && p[1] == 0x02) { |
| 238 | /* parse EIA-608 captions embedded in a DVB stream. */ |
| 239 | uint8_t dvb_cc_type = p[7]; |
| 240 | p += 8; |
| 241 | |
| 242 | /* Predictive frame tag, but MythTV reorders predictive |
| 243 | * frames for us along with the CC data, so we ignore it. |
| 244 | */ |
| 245 | if (dvb_cc_type == 0x05) { |
| 246 | dvb_cc_type = p[6]; |
| 247 | p += 7; |
| 248 | } |
| 249 | |
| 250 | if (dvb_cc_type == 0x02) { /* 2-byte caption, can be repeated */ |
| 251 | int type = 0x00; // line 21 field 1 == 0x00, field 2 == 0x01 |
| 252 | uint8_t cc608_hdr = 0xf8 | 0x04/*valid*/ | type; |
| 253 | uint8_t hi = p[1] & 0xFF; |
| 254 | uint8_t lo = p[2] & 0xFF; |
| 255 | |
| 256 | dvb_cc_type = p[3]; |
| 257 | |
| 258 | if ((2 <= buf_end - p) && ((3 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) { |
| 259 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&1/*cc_count*/); |
| 260 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data |
| 261 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr; |
| 262 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = hi; |
| 263 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = lo; |
| 264 | |
| 265 | /* Only repeat characters when the next type flag |
| 266 | * is 0x04 and the characters are repeatable (i.e., less than |
| 267 | * 32 with the parity stripped). |
| 268 | */ |
| 269 | if (dvb_cc_type == 0x04 && (hi & 0x7f) < 32) { |
| 270 | if ((2 <= buf_end - p) && ((3 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) { |
| 271 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&1/*cc_count*/); |
| 272 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data |
| 273 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr; |
| 274 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = hi; |
| 275 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = lo; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | p += 6; |
| 281 | } else if (dvb_cc_type == 0x04) { /* 4-byte caption, not repeated */ |
| 282 | if ((4 <= buf_end - p) && |
| 283 | ((6 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) { |
| 284 | int type = 0x00; // line 21 field 1 == 0x00, field 2 == 0x01 |
| 285 | uint8_t cc608_hdr = 0xf8 | 0x04/*valid*/ | type; |
| 286 | |
| 287 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&2/*cc_count*/); |
| 288 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data |
| 289 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr; |
| 290 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[1] & 0xFF; |
| 291 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[2] & 0xFF; |
| 292 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr; |
| 293 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[3] & 0xFF; |
| 294 | s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[4] & 0xFF; |
| 295 | } |
| 296 | |
| 297 | p += 9; |
| 298 | } |
| 299 | } |
| 300 | // For other CEA-608 embedding options see: |
| 301 | /* SCTE 21 */ |
| 302 | /* ETSI EN 301 775 */ |