History

lavd/decklink_dec: add support for teletext

It uses the libzvbi slicer, therefore teletext capture requires libzvbi.

Marton Balint committed 10 years ago

Commit 995c7a61 parent 98e94df6 files · +116 −1Browse files

Showing the changes to libavdevice/decklink_dec.cpp (1 of 6 files). Show all

Unified · Split

libavdevice/decklink_dec.cpp+99−0
@@ -25,14 +25,47 @@ · 24 unmodified lines
2525 #include <semaphore.h>
2626
2727 extern "C" {
28+#include "config.h"
2829 #include "libavformat/avformat.h"
2930 #include "libavformat/internal.h"
3031 #include "libavutil/imgutils.h"
32+#if CONFIG_LIBZVBI
33+#include <libzvbi.h>
34+#endif
3135 }
3236
3337 #include "decklink_common.h"
3438 #include "decklink_dec.h"
3539
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+
3669 static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
3770 {
3871 memset(q, 0, sizeof(AVPacketQueue));
@@ -277,6 +310,50 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( · 238 unmodified lines
277310 pkt.size = videoFrame->GetRowBytes() *
278311 videoFrame->GetHeight();
279312 //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+
280357 c->frame_number++;
281358 if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
282359 ++ctx->dropped;
@@ -378,9 +455,17 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) · 95 unmodified lines
378455 return AVERROR(ENOMEM);
379456 ctx->list_devices = cctx->list_devices;
380457 ctx->list_formats = cctx->list_formats;
458+ ctx->teletext_lines = cctx->teletext_lines;
381459 ctx->preroll = cctx->preroll;
382460 cctx->ctx = ctx;
383461
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+
384469 iter = CreateDeckLinkIteratorInstance();
385470 if (!iter) {
386471 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) · 101 unmodified lines
488573
489574 ctx->video_st=st;
490575
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+
491590 result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2);
492591
493592 if (result != S_OK) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t