lavd/decklink_dec: add support for teletext
[ffmpeg.git] / libavdevice / decklink_dec.cpp
index 6c5bc5d..9a721c9 100644 (file)
 #include <semaphore.h>
 
 extern "C" {
+#include "config.h"
 #include "libavformat/avformat.h"
 #include "libavformat/internal.h"
 #include "libavutil/imgutils.h"
+#if CONFIG_LIBZVBI
+#include <libzvbi.h>
+#endif
 }
 
 #include "decklink_common.h"
 #include "decklink_dec.h"
 
+#if CONFIG_LIBZVBI
+static uint8_t calc_parity_and_line_offset(int line)
+{
+    uint8_t ret = (line < 313) << 5;
+    if (line >= 7 && line <= 22)
+        ret += line;
+    if (line >= 320 && line <= 335)
+        ret += (line - 313);
+    return ret;
+}
+
+int teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt)
+{
+    vbi_bit_slicer slicer;
+
+    vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, VBI_PIXFMT_UYVY);
+
+    if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
+        return -1;
+
+    tgt[0] = 0x02; // data_unit_id
+    tgt[1] = 0x2c; // data_unit_length
+    tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
+    tgt[3] = 0xe4; // framing code
+
+    return 0;
+}
+#endif
+
 static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
 {
     memset(q, 0, sizeof(AVPacketQueue));
@@ -277,6 +310,50 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
         pkt.size         = videoFrame->GetRowBytes() *
                            videoFrame->GetHeight();
         //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
+
+#if CONFIG_LIBZVBI
+        if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) {
+            IDeckLinkVideoFrameAncillary *vanc;
+            AVPacket txt_pkt;
+            uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier
+            uint8_t *txt_buf = txt_buf0;
+
+            if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
+                int i;
+                int64_t line_mask = 1;
+                txt_buf[0] = 0x10;    // data_identifier - EBU_data
+                txt_buf++;
+                for (i = 6; i < 336; i++, line_mask <<= 1) {
+                    uint8_t *buf;
+                    if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
+                        if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0)
+                            txt_buf += 46;
+                    }
+                    if (i == 22)
+                        i = 317;
+                }
+                vanc->Release();
+                if (txt_buf - txt_buf0 > 1) {
+                    int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
+                    while (stuffing_units--) {
+                        memset(txt_buf, 0xff, 46);
+                        txt_buf[1] = 0x2c; // data_unit_length
+                        txt_buf += 46;
+                    }
+                    av_init_packet(&txt_pkt);
+                    txt_pkt.pts = pkt.pts;
+                    txt_pkt.dts = pkt.dts;
+                    txt_pkt.stream_index = ctx->teletext_st->index;
+                    txt_pkt.data = txt_buf0;
+                    txt_pkt.size = txt_buf - txt_buf0;
+                    if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
+                        ++ctx->dropped;
+                    }
+                }
+            }
+        }
+#endif
+
         c->frame_number++;
         if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
             ++ctx->dropped;
@@ -378,9 +455,17 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx)
         return AVERROR(ENOMEM);
     ctx->list_devices = cctx->list_devices;
     ctx->list_formats = cctx->list_formats;
+    ctx->teletext_lines = cctx->teletext_lines;
     ctx->preroll      = cctx->preroll;
     cctx->ctx = ctx;
 
+#if !CONFIG_LIBZVBI
+    if (ctx->teletext_lines) {
+        av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n");
+        return AVERROR(ENOSYS);
+    }
+#endif
+
     iter = CreateDeckLinkIteratorInstance();
     if (!iter) {
         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)
 
     ctx->video_st=st;
 
+    if (ctx->teletext_lines) {
+        st = avformat_new_stream(avctx, NULL);
+        if (!st) {
+            av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
+            goto error;
+        }
+        st->codec->codec_type  = AVMEDIA_TYPE_SUBTITLE;
+        st->codec->time_base.den      = ctx->bmd_tb_den;
+        st->codec->time_base.num      = ctx->bmd_tb_num;
+        st->codec->codec_id    = AV_CODEC_ID_DVB_TELETEXT;
+        avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
+        ctx->teletext_st = st;
+    }
+
     result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2);
 
     if (result != S_OK) {