|
25 | 25 | #include <semaphore.h> |
26 | 26 |
|
27 | 27 | extern "C" { |
| 28 | +#include "config.h" |
28 | 29 | #include "libavformat/avformat.h" |
29 | 30 | #include "libavformat/internal.h" |
30 | 31 | #include "libavutil/imgutils.h" |
| 32 | +#if CONFIG_LIBZVBI |
| 33 | +#include <libzvbi.h> |
| 34 | +#endif |
31 | 35 | } |
32 | 36 |
|
33 | 37 | #include "decklink_common.h" |
34 | 38 | #include "decklink_dec.h" |
35 | 39 |
|
| 40 | +#if CONFIG_LIBZVBI |
| 41 | +static uint8_t calc_parity_and_line_offset(int line) |
| 42 | +{ |
| 43 | + uint8_t ret = (line < 313) << 5; |
| 44 | + if (line >= 7 && line <= 22) |
| 45 | + ret += line; |
| 46 | + if (line >= 320 && line <= 335) |
| 47 | + ret += (line - 313); |
| 48 | + return ret; |
| 49 | +} |
| 50 | + |
| 51 | +int teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt) |
| 52 | +{ |
| 53 | + vbi_bit_slicer slicer; |
| 54 | + |
| 55 | + vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, VBI_PIXFMT_UYVY); |
| 56 | + |
| 57 | + if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE) |
| 58 | + return -1; |
| 59 | + |
| 60 | + tgt[0] = 0x02; // data_unit_id |
| 61 | + tgt[1] = 0x2c; // data_unit_length |
| 62 | + tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset |
| 63 | + tgt[3] = 0xe4; // framing code |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | +#endif |
| 68 | + |
36 | 69 | static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q) |
37 | 70 | { |
38 | 71 | memset(q, 0, sizeof(AVPacketQueue)); |
@@ -277,6 +310,50 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( |
277 | 310 | pkt.size = videoFrame->GetRowBytes() * |
278 | 311 | videoFrame->GetHeight(); |
279 | 312 | //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts); |
| 313 | + |
| 314 | +#if CONFIG_LIBZVBI |
| 315 | + if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) { |
| 316 | + IDeckLinkVideoFrameAncillary *vanc; |
| 317 | + AVPacket txt_pkt; |
| 318 | + uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier |
| 319 | + uint8_t *txt_buf = txt_buf0; |
| 320 | + |
| 321 | + if (videoFrame->GetAncillaryData(&vanc) == S_OK) { |
| 322 | + int i; |
| 323 | + int64_t line_mask = 1; |
| 324 | + txt_buf[0] = 0x10; // data_identifier - EBU_data |
| 325 | + txt_buf++; |
| 326 | + for (i = 6; i < 336; i++, line_mask <<= 1) { |
| 327 | + uint8_t *buf; |
| 328 | + if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) { |
| 329 | + if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0) |
| 330 | + txt_buf += 46; |
| 331 | + } |
| 332 | + if (i == 22) |
| 333 | + i = 317; |
| 334 | + } |
| 335 | + vanc->Release(); |
| 336 | + if (txt_buf - txt_buf0 > 1) { |
| 337 | + int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4; |
| 338 | + while (stuffing_units--) { |
| 339 | + memset(txt_buf, 0xff, 46); |
| 340 | + txt_buf[1] = 0x2c; // data_unit_length |
| 341 | + txt_buf += 46; |
| 342 | + } |
| 343 | + av_init_packet(&txt_pkt); |
| 344 | + txt_pkt.pts = pkt.pts; |
| 345 | + txt_pkt.dts = pkt.dts; |
| 346 | + txt_pkt.stream_index = ctx->teletext_st->index; |
| 347 | + txt_pkt.data = txt_buf0; |
| 348 | + txt_pkt.size = txt_buf - txt_buf0; |
| 349 | + if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) { |
| 350 | + ++ctx->dropped; |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + } |
| 355 | +#endif |
| 356 | + |
280 | 357 | c->frame_number++; |
281 | 358 | if (avpacket_queue_put(&ctx->queue, &pkt) < 0) { |
282 | 359 | ++ctx->dropped; |
@@ -378,9 +455,17 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) |
378 | 455 | return AVERROR(ENOMEM); |
379 | 456 | ctx->list_devices = cctx->list_devices; |
380 | 457 | ctx->list_formats = cctx->list_formats; |
| 458 | + ctx->teletext_lines = cctx->teletext_lines; |
381 | 459 | ctx->preroll = cctx->preroll; |
382 | 460 | cctx->ctx = ctx; |
383 | 461 |
|
| 462 | +#if !CONFIG_LIBZVBI |
| 463 | + if (ctx->teletext_lines) { |
| 464 | + av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n"); |
| 465 | + return AVERROR(ENOSYS); |
| 466 | + } |
| 467 | +#endif |
| 468 | + |
384 | 469 | iter = CreateDeckLinkIteratorInstance(); |
385 | 470 | if (!iter) { |
386 | 471 | av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n"); |
@@ -488,6 +573,20 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) |
488 | 573 |
|
489 | 574 | ctx->video_st=st; |
490 | 575 |
|
| 576 | + if (ctx->teletext_lines) { |
| 577 | + st = avformat_new_stream(avctx, NULL); |
| 578 | + if (!st) { |
| 579 | + av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n"); |
| 580 | + goto error; |
| 581 | + } |
| 582 | + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| 583 | + st->codec->time_base.den = ctx->bmd_tb_den; |
| 584 | + st->codec->time_base.num = ctx->bmd_tb_num; |
| 585 | + st->codec->codec_id = AV_CODEC_ID_DVB_TELETEXT; |
| 586 | + avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ |
| 587 | + ctx->teletext_st = st; |
| 588 | + } |
| 589 | + |
491 | 590 | result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2); |
492 | 591 |
|
493 | 592 | if (result != S_OK) { |
|
0 commit comments