Ticket #668: atsc_cc-r7892.patch
File atsc_cc-r7892.patch, 12.3 KB (added by , 20 years ago) |
---|
-
libs/libmythtv/avformatdecoder.cpp
158 158 frame_decoded(0), directrendering(false), drawband(false), bitrate(0), 159 159 gopset(false), seen_gop(false), seq_count(0), firstgoppos(0), 160 160 prevgoppos(0), gotvideo(false), lastvpts(0), lastapts(0), 161 lastccptsu(0), 161 162 using_null_videoout(use_null_videoout), video_codec_id(kCodec_NONE), 162 163 maxkeyframedist(-1), 164 ccd(new CCDecoder(this)), 163 165 // Audio 164 166 audioSamples(new short int[AVCODEC_MAX_AUDIO_FRAME_SIZE]), 165 167 audio_sample_size(-1), audio_sampling_rate(-1), audio_channels(-1), … … 173 175 174 176 bool debug = (bool)(print_verbose_messages & VB_LIBAV); 175 177 av_log_set_level((debug) ? AV_LOG_DEBUG : AV_LOG_ERROR); 178 179 save_cctc[0] = save_cctc[1] = 0; 176 180 } 177 181 178 182 AvFormatDecoder::~AvFormatDecoder() 179 183 { 180 184 CloseContext(); 185 delete ccd; 181 186 delete d; 182 187 if (audioSamples) 183 188 delete [] audioSamples; … … 308 313 309 314 lastapts = 0; 310 315 lastvpts = 0; 316 lastccptsu = 0; 317 save_cctc[0] = save_cctc[1] = 0; 311 318 312 319 av_read_frame_flush(ic); 313 320 … … 893 900 { 894 901 d->InitMPEG2(); 895 902 } 903 904 enc->decode_cc_dvd = decode_cc_dvd; 905 enc->decode_cc_atsc = decode_cc_atsc; 896 906 break; 897 907 } 898 908 case CODEC_TYPE_AUDIO: … … 1231 1241 cerr<<"render_slice_xvmc called with bad avctx or src"<<endl; 1232 1242 } 1233 1243 1244 void decode_cc_dvd(struct AVCodecContext *s, const uint8_t *buf, int buf_size) 1245 { 1246 // taken from xine-lib libspucc by Christian Vogler 1247 1248 AvFormatDecoder *nd = (AvFormatDecoder *)(s->opaque); 1249 unsigned long long utc = nd->lastccptsu; 1250 1251 const uint8_t *current = buf; 1252 int curbytes = 0; 1253 uint8_t data1, data2; 1254 uint8_t cc_code; 1255 int odd_offset = 1; 1256 1257 while (curbytes < buf_size) 1258 { 1259 int skip = 2; 1260 1261 cc_code = *current++; 1262 curbytes++; 1263 1264 if (buf_size - curbytes < 2) 1265 break; 1266 1267 data1 = *current; 1268 data2 = *(current + 1); 1269 1270 switch (cc_code) 1271 { 1272 case 0xfe: 1273 /* expect 2 byte encoding (perhaps CC3, CC4?) */ 1274 /* ignore for time being */ 1275 skip = 2; 1276 break; 1277 1278 case 0xff: 1279 { 1280 /* expect EIA-608 CC1/CC2 encoding */ 1281 int tc = utc / 1000; 1282 int data = (data2 << 8) | data1; 1283 nd->ccd->FormatCCField(tc, 0, data); 1284 utc += 33367; 1285 skip = 5; 1286 break; 1287 } 1288 1289 case 0x00: 1290 /* This seems to be just padding */ 1291 skip = 2; 1292 break; 1293 1294 case 0x01: 1295 odd_offset = data2 & 0x80; 1296 if (odd_offset) 1297 skip = 2; 1298 else 1299 skip = 5; 1300 break; 1301 1302 default: 1303 // rest is not needed? 1304 goto done; 1305 //skip = 2; 1306 //break; 1307 } 1308 current += skip; 1309 curbytes += skip; 1310 1311 } 1312 done: 1313 nd->lastccptsu = utc; 1314 } 1315 1316 void decode_cc_atsc(struct AVCodecContext *s, const uint8_t *buf, int buf_size) 1317 { 1318 AvFormatDecoder *nd = (AvFormatDecoder *)(s->opaque); 1319 unsigned long long utc = nd->lastccptsu; 1320 1321 const uint8_t *current = buf; 1322 int curbytes = 0; 1323 int curcount = 0; 1324 uint8_t data1, data2; 1325 uint8_t cc_count; 1326 uint8_t cc_code; 1327 int cc_state; 1328 1329 if (buf_size < 2) 1330 return; 1331 1332 // check process_cc_data_flag 1333 if (!(*current & 0x40)) 1334 return; 1335 cc_count = *current & 0x1f; 1336 current++; curbytes++; 1337 1338 // skip em_data 1339 current++; curbytes++; 1340 1341 cc_state = 0; 1342 while (curbytes < buf_size && curcount < cc_count) 1343 { 1344 cc_code = *current++; 1345 curbytes++; 1346 1347 if (buf_size - curbytes < 2) 1348 break; 1349 1350 data1 = *current++; 1351 data2 = *current++; 1352 curbytes += 2; 1353 curcount++; 1354 1355 // check cc_valid 1356 if (!(cc_code & 0x04)) 1357 continue; 1358 1359 cc_code &= 0x03; 1360 switch (cc_code) 1361 { 1362 case 0x00: 1363 case 0x01: 1364 // EIA-608 field-1/2 1365 int data = (data2 << 8) | data1; 1366 unsigned int tc; 1367 1368 if ((cc_state & cc_code) == cc_code) 1369 { 1370 // another field of same type -- assume 1371 // it's for the next frame 1372 utc += 33367; 1373 cc_state = 0; 1374 } 1375 cc_state |= cc_code; 1376 tc = utc / 1000; 1377 1378 // For some reason, one frame can be out of order. 1379 // We need to save the CC codes for at least one 1380 // frame so we can send the correct sequence to the 1381 // decoder. 1382 1383 if (nd->save_cctc[cc_code]) 1384 { 1385 if (nd->save_cctc[cc_code] < tc) 1386 { 1387 // send saved code to decoder 1388 nd->ccd->FormatCCField(nd->save_cctc[cc_code], 1389 cc_code, 1390 nd->save_ccdata[cc_code]); 1391 nd->save_cctc[cc_code] = 0; 1392 } 1393 else if ((nd->save_cctc[cc_code] - tc) > 1000) 1394 { 1395 // saved code is too far in the future; probably bad 1396 // - discard it 1397 nd->save_cctc[cc_code] = 0; 1398 } 1399 else 1400 { 1401 // send new code to decoder 1402 nd->ccd->FormatCCField(tc, cc_code, data); 1403 } 1404 } 1405 if (!nd->save_cctc[cc_code]) 1406 { 1407 // no saved code 1408 // - save new code since it may be out of order 1409 nd->save_cctc[cc_code] = tc; 1410 nd->save_ccdata[cc_code] = data; 1411 } 1412 break; 1413 case 0x02: 1414 // TODO: EIA-708 DTVCC packet data 1415 break; 1416 case 0x03: 1417 // TODO: EIA-708 DTVCC packet start 1418 break; 1419 } 1420 1421 } 1422 nd->lastccptsu = utc; 1423 } 1424 1234 1425 void AvFormatDecoder::HandleGopStart(AVPacket *pkt) 1235 1426 { 1236 1427 if (prevgoppos != 0 && keyframedist != 1) … … 1406 1597 1407 1598 gopset = false; 1408 1599 prevgoppos = 0; 1409 lastapts = lastvpts = 0;1600 lastapts = lastvpts = lastccptsu = 0; 1410 1601 } 1411 1602 1412 1603 seq_count++; … … 2023 2214 } 2024 2215 case CODEC_TYPE_VIDEO: 2025 2216 { 2217 if (firstloop && pts != (int64_t)AV_NOPTS_VALUE) 2218 lastccptsu = (long long)(av_q2d(curstream->time_base) * 2219 pkt->pts * 1000000); 2220 2026 2221 if (onlyvideo < 0) 2027 2222 { 2028 2223 framesPlayed++; … … 2221 2416 return len; // consume whole frame even if len > enc_len ? 2222 2417 } 2223 2418 2419 void AvFormatDecoder::AddTextData(unsigned char *buf, int len, 2420 long long timecode, char type) 2421 { 2422 m_parent->AddTextData((char *)buf, len, timecode, type); 2423 } 2424 -
libs/libmythtv/avformatdecoder.h
7 7 #include "programinfo.h" 8 8 #include "format.h" 9 9 #include "decoderbase.h" 10 #include "ccdecoder.h" 10 11 11 12 extern "C" { 12 13 #include "frame.h" … … 24 25 /// The AvFormatDecoder is used to decode non-NuppleVideo files. 25 26 /// It's used a a decoder of last resort after trying the NuppelDecoder 26 27 /// and IvtvDecoder (if "USING_IVTV" is defined). 27 class AvFormatDecoder : public DecoderBase 28 class AvFormatDecoder : public DecoderBase, public CCReader 28 29 { 29 30 friend void HandleStreamChange(void*); 30 31 public: … … 77 78 virtual bool setCurrentAudioTrack(int trackNo); 78 79 virtual QStringList listAudioTracks() const; 79 80 81 void AddTextData(unsigned char *buf, int len, long long timecode, char type); 82 80 83 virtual void incCurrentSubtitleTrack(); 81 84 virtual void decCurrentSubtitleTrack(); 82 85 virtual bool setCurrentSubtitleTrack(int trackNo); … … 106 109 friend void render_slice_xvmc(struct AVCodecContext *c, const AVFrame *src, 107 110 int offset[4], int y, int type, int height); 108 111 112 friend void decode_cc_dvd(struct AVCodecContext *c, const uint8_t *buf, int buf_size); 113 friend void decode_cc_atsc(struct AVCodecContext *c, const uint8_t *buf, int buf_size); 114 109 115 friend int open_avf(URLContext *h, const char *filename, int flags); 110 116 friend int read_avf(URLContext *h, uint8_t *buf, int buf_size); 111 117 friend int write_avf(URLContext *h, uint8_t *buf, int buf_size); … … 168 174 169 175 long long lastvpts; 170 176 long long lastapts; 177 long long lastccptsu; 178 unsigned int save_cctc[2]; 179 int save_ccdata[2]; 171 180 172 181 bool using_null_videoout; 173 182 MythCodecID video_codec_id; 174 183 175 184 int maxkeyframedist; 176 185 186 CCDecoder *ccd; 187 177 188 // Audio 178 189 short int *audioSamples; 179 190 int audio_sample_size; -
libs/libavcodec/mpeg12.c
2987 2987 return; 2988 2988 avctx->dtg_active_format = p[0] & 0x0f; 2989 2989 } 2990 } else if (len >= 3 && 2991 p[0] == 'C' && p[1] == 'C') { 2992 p += 2; 2993 len -= 2; 2994 avctx->decode_cc_dvd(avctx, p, len); 2995 } else if (len >= 6 && 2996 p[0] == 0x47 && p[1] == 0x41 && p[2] == 0x39 && p[3] == 0x34 && 2997 p[4] == 0x03) { 2998 p += 5; 2999 len -= 5; 3000 avctx->decode_cc_atsc(avctx, p, len); 2990 3001 } 3002 2991 3003 } 2992 3004 2993 3005 static void mpeg_decode_gop(AVCodecContext *avctx, -
libs/libavcodec/utils.c
423 423 return 0; 424 424 } 425 425 426 void avcodec_default_decode_cc_dvd(AVCodecContext *c, const uint8_t *buf, int buf_size){ 427 } 428 429 void avcodec_default_decode_cc_atsc(AVCodecContext *c, const uint8_t *buf, int buf_size){ 430 } 431 426 432 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){ 427 433 return fmt[0]; 428 434 } … … 758 764 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS; 759 765 s->palctrl = NULL; 760 766 s->reget_buffer= avcodec_default_reget_buffer; 767 768 s->decode_cc_dvd= avcodec_default_decode_cc_dvd; 769 s->decode_cc_atsc= avcodec_default_decode_cc_atsc; 761 770 } 762 771 763 772 /** -
libs/libavcodec/avcodec.h
1858 1858 */ 1859 1859 int xvmc_vld_hwslice; 1860 1860 1861 /** 1862 * Closed Caption decoder 1863 * - encoding: forbidden 1864 * - decoding: set by decoder 1865 */ 1866 void (*decode_cc_dvd)(struct AVCodecContext *c, const uint8_t *buf, int buf_size); 1867 void (*decode_cc_atsc)(struct AVCodecContext *c, const uint8_t *buf, int buf_size); 1868 1861 1869 } AVCodecContext; 1862 1870 1863 1871 /** … … 2261 2269 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count); 2262 2270 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count); 2263 2271 //FIXME func typedef 2272 void avcodec_default_decode_cc_dvd(AVCodecContext *c, const uint8_t *buf, int buf_size); 2273 void avcodec_default_decode_cc_atsc(AVCodecContext *c, const uint8_t *buf, int buf_size); 2264 2274 2265 2275 /** 2266 2276 * opens / inits the AVCodecContext.