Browse code

lavd: implement NewTek NDI input/output device support

Signed-off-by: Marton Balint <cus@passwd.hu>

Maksym Veremeyenko authored on 2017/08/18 22:34:06
Showing 10 changed files
... ...
@@ -34,6 +34,7 @@ version <next>:
34 34
 - floodfill video filter
35 35
 - pseudocolor video filter
36 36
 - raw G.726 muxer and demuxer, left- and right-justified
37
+- NewTek NDI input/output device
37 38
 
38 39
 version 3.3:
39 40
 - CrystalHD decoder moved to new decode API
... ...
@@ -277,6 +277,7 @@ External library support:
277 277
   --enable-libzvbi         enable teletext support via libzvbi [no]
278 278
   --disable-lzma           disable lzma [autodetect]
279 279
   --enable-decklink        enable Blackmagic DeckLink I/O support [no]
280
+  --enable-libndi_newtek   enable Newteck NDI I/O support [no]
280 281
   --enable-mediacodec      enable Android MediaCodec support [no]
281 282
   --enable-libmysofa       enable libmysofa, needed for sofalizer filter [no]
282 283
   --enable-openal          enable OpenAL 1.1 capture support [no]
... ...
@@ -1508,6 +1509,7 @@ EXTERNAL_LIBRARY_GPL_LIST="
1508 1508
 
1509 1509
 EXTERNAL_LIBRARY_NONFREE_LIST="
1510 1510
     decklink
1511
+    libndi_newtek
1511 1512
     libfdk_aac
1512 1513
     openssl
1513 1514
 "
... ...
@@ -3014,6 +3016,10 @@ decklink_indev_deps="decklink threads"
3014 3014
 decklink_indev_extralibs="-lstdc++"
3015 3015
 decklink_outdev_deps="decklink threads"
3016 3016
 decklink_outdev_extralibs="-lstdc++"
3017
+libndi_newtek_indev_deps="libndi_newtek"
3018
+libndi_newtek_indev_extralibs="-lndi"
3019
+libndi_newtek_outdev_deps="libndi_newtek"
3020
+libndi_newtek_outdev_extralibs="-lndi"
3017 3021
 dshow_indev_deps="IBaseFilter"
3018 3022
 dshow_indev_extralibs="-lpsapi -lole32 -lstrmiids -luuid -loleaut32 -lshlwapi"
3019 3023
 dv1394_indev_deps="dv1394"
... ...
@@ -5567,6 +5573,8 @@ avisynth_demuxer_extralibs='$ldl'
5567 5567
 cuda_extralibs='$ldl'
5568 5568
 decklink_outdev_extralibs="$decklink_outdev_extralibs $ldl"
5569 5569
 decklink_indev_extralibs="$decklink_indev_extralibs $ldl"
5570
+libndi_newtek_outdev_extralibs="$libndi_newtek_outdev_extralibs $ldl"
5571
+libndi_newtek_indev_extralibs="$libndi_newtek_indev_extralibs $ldl"
5570 5572
 frei0r_filter_extralibs='$ldl'
5571 5573
 frei0r_src_filter_extralibs='$ldl'
5572 5574
 ladspa_filter_extralibs='$ldl'
... ...
@@ -5825,6 +5833,7 @@ enabled coreimage_filter  && { check_header_objcc QuartzCore/CoreImage.h || disa
5825 5825
 enabled coreimagesrc_filter && { check_header_objcc QuartzCore/CoreImage.h || disable coreimagesrc_filter; }
5826 5826
 enabled decklink          && { { check_header DeckLinkAPI.h || die "ERROR: DeckLinkAPI.h header not found"; } &&
5827 5827
                                { check_cpp_condition DeckLinkAPIVersion.h "BLACKMAGIC_DECKLINK_API_VERSION >= 0x0a060100" || die "ERROR: Decklink API version must be >= 10.6.1."; } }
5828
+enabled libndi_newtek     && { check_header Processing.NDI.Lib.h || die "ERROR: Processing.NDI.Lib.h header not found"; }
5828 5829
 enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
5829 5830
 enabled gmp               && require gmp gmp.h mpz_export -lgmp
5830 5831
 enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
... ...
@@ -332,6 +332,54 @@ ffmpeg -channels 16 -format_code Hi50 -f decklink -i 'UltraStudio Mini Recorder'
332 332
 
333 333
 @end itemize
334 334
 
335
+@section libndi_newtek
336
+
337
+The libndi_newtek input device provides capture capabilities for using NDI (Network
338
+Device Interface, standard created by NewTek).
339
+
340
+Input filename is a NDI source name that could be found by sending -find_sources 1
341
+to command line - it has no specific syntax but human-readable formatted.
342
+
343
+To enable this input device, you need the NDI SDK and you
344
+need to configure with the appropriate @code{--extra-cflags}
345
+and @code{--extra-ldflags}.
346
+
347
+@subsection Options
348
+
349
+@table @option
350
+
351
+@item find_sources
352
+If set to @option{true}, print a list of found/available NDI sources and exit.
353
+Defaults to @option{false}.
354
+
355
+@item wait_sources
356
+Override time to wait until the number of online sources have changed.
357
+Defaults to @option{0.5}.
358
+
359
+@item allow_video_fields
360
+When this flag is @option{false}, all video that you receive will be progressive.
361
+Defaults to @option{true}.
362
+
363
+@end table
364
+
365
+@subsection Examples
366
+
367
+@itemize
368
+
369
+@item
370
+List input devices:
371
+@example
372
+ffmpeg -f libndi_newtek -find_sources 1 -i dummy
373
+@end example
374
+
375
+@item
376
+Restream to NDI:
377
+@example
378
+ffmpeg -f libndi_newtek -i "DEV-5.INTERNAL.M1STEREO.TV (NDI_SOURCE_NAME_1)" -f libndi_newtek -y NDI_SOURCE_NAME_2
379
+@end example
380
+
381
+@end itemize
382
+
335 383
 @section dshow
336 384
 
337 385
 Windows DirectShow input device.
... ...
@@ -182,6 +182,51 @@ ffmpeg -i test.avi -f decklink -pix_fmt uyvy422 -s 720x486 -r 24000/1001 'DeckLi
182 182
 
183 183
 @end itemize
184 184
 
185
+@section libndi_newtek
186
+
187
+The libndi_newtek output device provides playback capabilities for using NDI (Network
188
+Device Interface, standard created by NewTek).
189
+
190
+Output filename is a NDI name.
191
+
192
+To enable this output device, you need the NDI SDK and you
193
+need to configure with the appropriate @code{--extra-cflags}
194
+and @code{--extra-ldflags}.
195
+
196
+NDI uses uyvy422 pixel format natively, but also supports bgra, bgr0, rgba and
197
+rgb0.
198
+
199
+@subsection Options
200
+
201
+@table @option
202
+
203
+@item reference_level
204
+The audio reference level in dB. This specifies how many dB above the
205
+reference level (+4dBU) is the full range of 16 bit audio.
206
+Defaults to @option{0}.
207
+
208
+@item clock_video
209
+These specify whether video "clock" themselves.
210
+Defaults to @option{false}.
211
+
212
+@item clock_audio
213
+These specify whether audio "clock" themselves.
214
+Defaults to @option{false}.
215
+
216
+@end table
217
+
218
+@subsection Examples
219
+
220
+@itemize
221
+
222
+@item
223
+Play video clip:
224
+@example
225
+ffmpeg -i "udp://@@239.1.1.1:10480?fifo_size=1000000&overrun_nonfatal=1" -vf "scale=720:576,fps=fps=25,setdar=dar=16/9,format=pix_fmts=uyvy422" -f libndi_newtek NEW_NDI1
226
+@end example
227
+
228
+@end itemize
229
+
185 230
 @section fbdev
186 231
 
187 232
 Linux framebuffer output device.
... ...
@@ -19,6 +19,8 @@ OBJS-$(CONFIG_BKTR_INDEV)                += bktr.o
19 19
 OBJS-$(CONFIG_CACA_OUTDEV)               += caca.o
20 20
 OBJS-$(CONFIG_DECKLINK_OUTDEV)           += decklink_enc.o decklink_enc_c.o decklink_common.o
21 21
 OBJS-$(CONFIG_DECKLINK_INDEV)            += decklink_dec.o decklink_dec_c.o decklink_common.o
22
+OBJS-$(CONFIG_LIBNDI_NEWTEK_OUTDEV)      += libndi_newtek_enc.o
23
+OBJS-$(CONFIG_LIBNDI_NEWTEK_INDEV)       += libndi_newtek_dec.o
22 24
 OBJS-$(CONFIG_DSHOW_INDEV)               += dshow_crossbar.o dshow.o dshow_enummediatypes.o \
23 25
                                             dshow_enumpins.o dshow_filter.o \
24 26
                                             dshow_pin.o dshow_common.o
... ...
@@ -60,6 +62,8 @@ SLIBOBJS-$(HAVE_GNU_WINDRES)             += avdeviceres.o
60 60
 SKIPHEADERS                              += decklink_common.h
61 61
 SKIPHEADERS-$(CONFIG_DECKLINK)           += decklink_enc.h decklink_dec.h \
62 62
                                             decklink_common_c.h
63
+SKIPHEADERS-$(CONFIG_LIBNDI_NEWTEK_INDEV) += libndi_newtek_common.h
64
+SKIPHEADERS-$(CONFIG_LIBNDI_NEWTEK_OUTDEV) += libndi_newtek_common.h
63 65
 SKIPHEADERS-$(CONFIG_DSHOW_INDEV)        += dshow_capture.h
64 66
 SKIPHEADERS-$(CONFIG_FBDEV_INDEV)        += fbdev_common.h
65 67
 SKIPHEADERS-$(CONFIG_FBDEV_OUTDEV)       += fbdev_common.h
... ...
@@ -46,6 +46,7 @@ static void register_all(void)
46 46
     REGISTER_INDEV   (BKTR,             bktr);
47 47
     REGISTER_OUTDEV  (CACA,             caca);
48 48
     REGISTER_INOUTDEV(DECKLINK,         decklink);
49
+    REGISTER_INOUTDEV(LIBNDI_NEWTEK,    libndi_newtek);
49 50
     REGISTER_INDEV   (DSHOW,            dshow);
50 51
     REGISTER_INDEV   (DV1394,           dv1394);
51 52
     REGISTER_INOUTDEV(FBDEV,            fbdev);
52 53
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+/*
1
+ * NewTek NDI common code
2
+ * Copyright (c) 2017 Maksym Veremeyenko
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVDEVICE_LIBNDI_NEWTEK_COMMON_H
22
+#define AVDEVICE_LIBNDI_NEWTEK_COMMON_H
23
+
24
+#include <Processing.NDI.Lib.h>
25
+
26
+#define NDI_TIME_BASE 10000000
27
+#define NDI_TIME_BASE_Q (AVRational){1, NDI_TIME_BASE}
28
+
29
+#endif
0 30
new file mode 100644
... ...
@@ -0,0 +1,340 @@
0
+/*
1
+ * Newtek NDI input
2
+ * Copyright (c) 2017 Maksym Veremeyenko
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavformat/avformat.h"
22
+#include "libavformat/internal.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/imgutils.h"
25
+
26
+#include "libndi_newtek_common.h"
27
+
28
+struct NDIContext {
29
+    const AVClass *cclass;
30
+
31
+    /* Options */
32
+    int find_sources;
33
+    int64_t wait_sources;
34
+    int allow_video_fields;
35
+
36
+    /* Runtime */
37
+    NDIlib_recv_create_t *recv;
38
+    NDIlib_find_instance_t ndi_find;
39
+
40
+    /* Streams */
41
+    AVStream *video_st, *audio_st;
42
+};
43
+
44
+static int ndi_set_video_packet(AVFormatContext *avctx, NDIlib_video_frame_t *v, AVPacket *pkt)
45
+{
46
+    int ret;
47
+    struct NDIContext *ctx = avctx->priv_data;
48
+
49
+    ret = av_new_packet(pkt, v->yres * v->line_stride_in_bytes);
50
+    if (ret < 0)
51
+        return ret;
52
+
53
+    pkt->dts = pkt->pts = av_rescale_q(v->timecode, NDI_TIME_BASE_Q, ctx->video_st->time_base);
54
+    pkt->duration = av_rescale_q(1, (AVRational){v->frame_rate_D, v->frame_rate_N}, ctx->video_st->time_base);
55
+
56
+    av_log(avctx, AV_LOG_DEBUG, "%s: pkt->dts = pkt->pts = %"PRId64", duration=%"PRId64", timecode=%"PRId64"\n",
57
+        __func__, pkt->dts, pkt->duration, v->timecode);
58
+
59
+    pkt->flags         |= AV_PKT_FLAG_KEY;
60
+    pkt->stream_index   = ctx->video_st->index;
61
+
62
+    memcpy(pkt->data, v->p_data, pkt->size);
63
+
64
+    return 0;
65
+}
66
+
67
+static int ndi_set_audio_packet(AVFormatContext *avctx, NDIlib_audio_frame_t *a, AVPacket *pkt)
68
+{
69
+    int ret;
70
+    struct NDIContext *ctx = avctx->priv_data;
71
+
72
+    NDIlib_audio_frame_interleaved_16s_t dst;
73
+
74
+    ret = av_new_packet(pkt, 2 * a->no_samples * a->no_channels);
75
+    if (ret < 0)
76
+        return ret;
77
+
78
+    pkt->dts = pkt->pts = av_rescale_q(a->timecode, NDI_TIME_BASE_Q, ctx->audio_st->time_base);
79
+    pkt->duration = av_rescale_q(1, (AVRational){a->no_samples, a->sample_rate}, ctx->audio_st->time_base);
80
+
81
+    av_log(avctx, AV_LOG_DEBUG, "%s: pkt->dts = pkt->pts = %"PRId64", duration=%"PRId64", timecode=%"PRId64"\n",
82
+        __func__, pkt->dts, pkt->duration, a->timecode);
83
+
84
+    pkt->flags       |= AV_PKT_FLAG_KEY;
85
+    pkt->stream_index = ctx->audio_st->index;
86
+
87
+    dst.reference_level = 0;
88
+    dst.p_data = (short *)pkt->data;
89
+    NDIlib_util_audio_to_interleaved_16s(a, &dst);
90
+
91
+    return 0;
92
+}
93
+
94
+static int ndi_find_sources(AVFormatContext *avctx, const char *name, NDIlib_source_t *source_to_connect_to)
95
+{
96
+    int j = AVERROR(ENODEV);
97
+    unsigned int n, i;
98
+    struct NDIContext *ctx = avctx->priv_data;
99
+    const NDIlib_source_t *ndi_srcs = NULL;
100
+    const NDIlib_find_create_t find_create_desc = { .show_local_sources = true,
101
+        .p_groups = NULL, .p_extra_ips = NULL };
102
+
103
+    if (!ctx->ndi_find)
104
+        ctx->ndi_find = NDIlib_find_create2(&find_create_desc);
105
+    if (!ctx->ndi_find) {
106
+        av_log(avctx, AV_LOG_ERROR, "NDIlib_find_create failed.\n");
107
+        return AVERROR(EIO);
108
+    }
109
+
110
+    while (1)
111
+    {
112
+        int f, t = ctx->wait_sources / 1000;
113
+        av_log(avctx, AV_LOG_DEBUG, "Waiting for sources %d miliseconds\n", t);
114
+        f = NDIlib_find_wait_for_sources(ctx->ndi_find, t);
115
+        av_log(avctx, AV_LOG_DEBUG, "NDIlib_find_wait_for_sources returns %d\n", f);
116
+        if (!f)
117
+            break;
118
+    };
119
+
120
+    ndi_srcs = NDIlib_find_get_current_sources(ctx->ndi_find, &n);
121
+
122
+    if (ctx->find_sources)
123
+        av_log(avctx, AV_LOG_INFO, "Found %d NDI sources:\n", n);
124
+
125
+    for (i = 0; i < n; i++) {
126
+        if (ctx->find_sources)
127
+            av_log(avctx, AV_LOG_INFO, "\t'%s'\t'%s'\n", ndi_srcs[i].p_ndi_name, ndi_srcs[i].p_ip_address);
128
+
129
+        if (!strcmp(name, ndi_srcs[i].p_ndi_name)) {
130
+            *source_to_connect_to = ndi_srcs[i];
131
+            j = i;
132
+        }
133
+    }
134
+
135
+    return j;
136
+}
137
+
138
+static int ndi_read_header(AVFormatContext *avctx)
139
+{
140
+    int ret;
141
+    NDIlib_recv_create_t recv_create_desc;
142
+    const NDIlib_tally_t tally_state = { .on_program = true, .on_preview = false };
143
+    struct NDIContext *ctx = avctx->priv_data;
144
+
145
+    if (!NDIlib_initialize()) {
146
+        av_log(avctx, AV_LOG_ERROR, "NDIlib_initialize failed.\n");
147
+        return AVERROR_EXTERNAL;
148
+    }
149
+
150
+    /* Find available sources. */
151
+    ret = ndi_find_sources(avctx, avctx->filename, &recv_create_desc.source_to_connect_to);
152
+    if (ctx->find_sources) {
153
+        return AVERROR_EXIT;
154
+    }
155
+    if (ret < 0)
156
+        return ret;
157
+
158
+    /* Create receiver description */
159
+    recv_create_desc.color_format = NDIlib_recv_color_format_e_UYVY_RGBA;
160
+    recv_create_desc.bandwidth = NDIlib_recv_bandwidth_highest;
161
+    recv_create_desc.allow_video_fields = ctx->allow_video_fields;
162
+
163
+    /* Create the receiver */
164
+    ctx->recv = NDIlib_recv_create(&recv_create_desc);
165
+    if (!ctx->recv) {
166
+        av_log(avctx, AV_LOG_ERROR, "NDIlib_recv_create2 failed.\n");
167
+        return AVERROR(EIO);
168
+    }
169
+
170
+    /* Set tally */
171
+    NDIlib_recv_set_tally(ctx->recv, &tally_state);
172
+
173
+    avctx->ctx_flags |= AVFMTCTX_NOHEADER;
174
+
175
+    return 0;
176
+}
177
+
178
+static int ndi_create_video_stream(AVFormatContext *avctx, NDIlib_video_frame_t *v)
179
+{
180
+    AVStream *st;
181
+    AVRational tmp;
182
+    struct NDIContext *ctx = avctx->priv_data;
183
+
184
+    st = avformat_new_stream(avctx, NULL);
185
+    if (!st) {
186
+        av_log(avctx, AV_LOG_ERROR, "Cannot add video stream\n");
187
+        return AVERROR(ENOMEM);
188
+    }
189
+
190
+    st->time_base                   = NDI_TIME_BASE_Q;
191
+    av_stream_set_r_frame_rate(st, av_make_q(v->frame_rate_N, v->frame_rate_D));
192
+
193
+    tmp = av_mul_q(av_d2q(v->picture_aspect_ratio, INT_MAX), (AVRational){v->yres, v->xres});
194
+    av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den, tmp.num, tmp.den, 1000);
195
+    st->codecpar->sample_aspect_ratio = st->sample_aspect_ratio;
196
+
197
+    st->codecpar->codec_type        = AVMEDIA_TYPE_VIDEO;
198
+    st->codecpar->width             = v->xres;
199
+    st->codecpar->height            = v->yres;
200
+    st->codecpar->codec_id          = AV_CODEC_ID_RAWVIDEO;
201
+    st->codecpar->bit_rate          = av_rescale(v->xres * v->yres * 16, v->frame_rate_N, v->frame_rate_D);
202
+    st->codecpar->field_order       = v->frame_format_type == NDIlib_frame_format_type_progressive
203
+        ? AV_FIELD_PROGRESSIVE : AV_FIELD_TT;
204
+
205
+    if (NDIlib_FourCC_type_UYVY == v->FourCC || NDIlib_FourCC_type_UYVA == v->FourCC) {
206
+        st->codecpar->format        = AV_PIX_FMT_UYVY422;
207
+        st->codecpar->codec_tag     = MKTAG('U', 'Y', 'V', 'Y');
208
+        if (NDIlib_FourCC_type_UYVA == v->FourCC)
209
+            av_log(avctx, AV_LOG_WARNING, "Alpha channel ignored\n");
210
+    } else if (NDIlib_FourCC_type_BGRA == v->FourCC) {
211
+        st->codecpar->format        = AV_PIX_FMT_BGRA;
212
+        st->codecpar->codec_tag     = MKTAG('B', 'G', 'R', 'A');
213
+    } else if (NDIlib_FourCC_type_BGRX == v->FourCC) {
214
+        st->codecpar->format        = AV_PIX_FMT_BGR0;
215
+        st->codecpar->codec_tag     = MKTAG('B', 'G', 'R', '0');
216
+    } else if (NDIlib_FourCC_type_RGBA == v->FourCC) {
217
+        st->codecpar->format        = AV_PIX_FMT_RGBA;
218
+        st->codecpar->codec_tag     = MKTAG('R', 'G', 'B', 'A');
219
+    } else if (NDIlib_FourCC_type_RGBX == v->FourCC) {
220
+        st->codecpar->format        = AV_PIX_FMT_RGB0;
221
+        st->codecpar->codec_tag     = MKTAG('R', 'G', 'B', '0');
222
+    } else {
223
+        av_log(avctx, AV_LOG_ERROR, "Unsupported video stream format, v->FourCC=%d\n", v->FourCC);
224
+        return AVERROR(EINVAL);
225
+    }
226
+
227
+    avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
228
+
229
+    ctx->video_st = st;
230
+
231
+    return 0;
232
+}
233
+
234
+static int ndi_create_audio_stream(AVFormatContext *avctx, NDIlib_audio_frame_t *a)
235
+{
236
+    AVStream *st;
237
+    struct NDIContext *ctx = avctx->priv_data;
238
+
239
+    st = avformat_new_stream(avctx, NULL);
240
+    if (!st) {
241
+        av_log(avctx, AV_LOG_ERROR, "Cannot add audio stream\n");
242
+        return AVERROR(ENOMEM);
243
+    }
244
+
245
+    st->codecpar->codec_type        = AVMEDIA_TYPE_AUDIO;
246
+    st->codecpar->codec_id          = AV_CODEC_ID_PCM_S16LE;
247
+    st->codecpar->sample_rate       = a->sample_rate;
248
+    st->codecpar->channels          = a->no_channels;
249
+
250
+    avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
251
+
252
+    ctx->audio_st = st;
253
+
254
+    return 0;
255
+}
256
+
257
+static int ndi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
258
+{
259
+    int ret = 0;
260
+    struct NDIContext *ctx = avctx->priv_data;
261
+
262
+    while (!ret) {
263
+        NDIlib_video_frame_t v;
264
+        NDIlib_audio_frame_t a;
265
+        NDIlib_metadata_frame_t m;
266
+        NDIlib_frame_type_e t;
267
+
268
+        av_log(avctx, AV_LOG_DEBUG, "NDIlib_recv_capture...\n");
269
+        t = NDIlib_recv_capture(ctx->recv, &v, &a, &m, 40);
270
+        av_log(avctx, AV_LOG_DEBUG, "NDIlib_recv_capture=%d\n", t);
271
+
272
+        if (t == NDIlib_frame_type_video) {
273
+            if (!ctx->video_st)
274
+                ret = ndi_create_video_stream(avctx, &v);
275
+            if (!ret)
276
+                ret = ndi_set_video_packet(avctx, &v, pkt);
277
+            NDIlib_recv_free_video(ctx->recv, &v);
278
+            break;
279
+        }
280
+        else if (t == NDIlib_frame_type_audio) {
281
+            if (!ctx->audio_st)
282
+                ret = ndi_create_audio_stream(avctx, &a);
283
+            if (!ret)
284
+                ret = ndi_set_audio_packet(avctx, &a, pkt);
285
+            NDIlib_recv_free_audio(ctx->recv, &a);
286
+            break;
287
+        }
288
+        else if (t == NDIlib_frame_type_metadata)
289
+            NDIlib_recv_free_metadata(ctx->recv, &m);
290
+        else if (t == NDIlib_frame_type_error){
291
+            av_log(avctx, AV_LOG_ERROR, "NDIlib_recv_capture failed with error\n");
292
+            ret = AVERROR(EIO);
293
+        }
294
+    };
295
+
296
+    return ret;
297
+}
298
+
299
+static int ndi_read_close(AVFormatContext *avctx)
300
+{
301
+    struct NDIContext *ctx = (struct NDIContext *)avctx->priv_data;
302
+
303
+    if (ctx->recv)
304
+        NDIlib_recv_destroy(ctx->recv);
305
+
306
+    if (ctx->ndi_find)
307
+        NDIlib_find_destroy(ctx->ndi_find);
308
+
309
+    return 0;
310
+}
311
+
312
+#define OFFSET(x) offsetof(struct NDIContext, x)
313
+#define DEC AV_OPT_FLAG_DECODING_PARAM
314
+
315
+static const AVOption options[] = {
316
+    { "find_sources", "Find available sources"  , OFFSET(find_sources), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, DEC },
317
+    { "wait_sources", "Time to wait until the number of online sources have changed"  , OFFSET(wait_sources), AV_OPT_TYPE_DURATION, { .i64 = 1000000 }, 100000, 20000000, DEC },
318
+    { "allow_video_fields", "When this flag is FALSE, all video that you receive will be progressive"  , OFFSET(allow_video_fields), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, DEC },
319
+    { NULL },
320
+};
321
+
322
+static const AVClass libndi_newtek_demuxer_class = {
323
+    .class_name = "NDI demuxer",
324
+    .item_name  = av_default_item_name,
325
+    .option     = options,
326
+    .version    = LIBAVUTIL_VERSION_INT,
327
+    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
328
+};
329
+
330
+AVInputFormat ff_libndi_newtek_demuxer = {
331
+    .name           = "libndi_newtek",
332
+    .long_name      = NULL_IF_CONFIG_SMALL("Network Device Interface (NDI) input using NewTek library"),
333
+    .flags          = AVFMT_NOFILE,
334
+    .priv_class     = &libndi_newtek_demuxer_class,
335
+    .priv_data_size = sizeof(struct NDIContext),
336
+    .read_header   = ndi_read_header,
337
+    .read_packet   = ndi_read_packet,
338
+    .read_close    = ndi_read_close,
339
+};
0 340
new file mode 100644
... ...
@@ -0,0 +1,299 @@
0
+/*
1
+ * NewTek NDI output
2
+ * Copyright (c) 2017 Maksym Veremeyenko
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavformat/avformat.h"
22
+#include "libavformat/internal.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/imgutils.h"
25
+
26
+#include "libndi_newtek_common.h"
27
+
28
+struct NDIContext {
29
+    const AVClass *cclass;
30
+
31
+    /* Options */
32
+    int reference_level;
33
+    int clock_video, clock_audio;
34
+
35
+    NDIlib_video_frame_t *video;
36
+    NDIlib_audio_frame_interleaved_16s_t *audio;
37
+    NDIlib_send_instance_t ndi_send;
38
+    AVFrame *last_avframe;
39
+};
40
+
41
+static int ndi_write_trailer(AVFormatContext *avctx)
42
+{
43
+    struct NDIContext *ctx = avctx->priv_data;
44
+
45
+    if (ctx->ndi_send) {
46
+        NDIlib_send_destroy(ctx->ndi_send);
47
+        av_frame_free(&ctx->last_avframe);
48
+    }
49
+
50
+    av_freep(&ctx->video);
51
+    av_freep(&ctx->audio);
52
+
53
+    return 0;
54
+}
55
+
56
+static int ndi_write_video_packet(AVFormatContext *avctx, AVStream *st, AVPacket *pkt)
57
+{
58
+    struct NDIContext *ctx = avctx->priv_data;
59
+    AVFrame *avframe, *tmp = (AVFrame *)pkt->data;
60
+
61
+    if (tmp->format != AV_PIX_FMT_UYVY422 && tmp->format != AV_PIX_FMT_BGRA &&
62
+        tmp->format != AV_PIX_FMT_BGR0 && tmp->format != AV_PIX_FMT_RGBA &&
63
+        tmp->format != AV_PIX_FMT_RGB0) {
64
+        av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format.\n");
65
+        return AVERROR(EINVAL);
66
+    }
67
+
68
+    if (tmp->linesize[0] < 0) {
69
+        av_log(avctx, AV_LOG_ERROR, "Got a frame with negative linesize.\n");
70
+        return AVERROR(EINVAL);
71
+    }
72
+
73
+    if (tmp->width  != ctx->video->xres ||
74
+        tmp->height != ctx->video->yres) {
75
+        av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid dimension.\n");
76
+        av_log(avctx, AV_LOG_ERROR, "tmp->width=%d, tmp->height=%d, ctx->video->xres=%d, ctx->video->yres=%d\n",
77
+            tmp->width, tmp->height, ctx->video->xres, ctx->video->yres);
78
+        return AVERROR(EINVAL);
79
+    }
80
+
81
+    avframe = av_frame_clone(tmp);
82
+    if (!avframe)
83
+        return AVERROR(ENOMEM);
84
+
85
+    ctx->video->timecode = av_rescale_q(pkt->pts, st->time_base, NDI_TIME_BASE_Q);
86
+
87
+    ctx->video->line_stride_in_bytes = avframe->linesize[0];
88
+    ctx->video->p_data = (void *)(avframe->data[0]);
89
+
90
+    av_log(avctx, AV_LOG_DEBUG, "%s: pkt->pts=%"PRId64", timecode=%"PRId64", st->time_base=%d/%d\n",
91
+        __func__, pkt->pts, ctx->video->timecode, st->time_base.num, st->time_base.den);
92
+
93
+    /* asynchronous for one frame, but will block if a second frame
94
+        is given before the first one has been sent */
95
+    NDIlib_send_send_video_async(ctx->ndi_send, ctx->video);
96
+
97
+    av_frame_free(&ctx->last_avframe);
98
+    ctx->last_avframe = avframe;
99
+
100
+    return 0;
101
+}
102
+
103
+static int ndi_write_audio_packet(AVFormatContext *avctx, AVStream *st, AVPacket *pkt)
104
+{
105
+    struct NDIContext *ctx = avctx->priv_data;
106
+
107
+    ctx->audio->p_data = (short *)pkt->data;
108
+    ctx->audio->timecode = av_rescale_q(pkt->pts, st->time_base, NDI_TIME_BASE_Q);
109
+    ctx->audio->no_samples = pkt->size / (ctx->audio->no_channels << 1);
110
+
111
+    av_log(avctx, AV_LOG_DEBUG, "%s: pkt->pts=%"PRId64", timecode=%"PRId64", st->time_base=%d/%d\n",
112
+        __func__, pkt->pts, ctx->audio->timecode, st->time_base.num, st->time_base.den);
113
+
114
+    NDIlib_util_send_send_audio_interleaved_16s(ctx->ndi_send, ctx->audio);
115
+
116
+    return 0;
117
+}
118
+
119
+static int ndi_write_packet(AVFormatContext *avctx, AVPacket *pkt)
120
+{
121
+    AVStream *st = avctx->streams[pkt->stream_index];
122
+
123
+    if      (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
124
+        return ndi_write_video_packet(avctx, st, pkt);
125
+    else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
126
+        return ndi_write_audio_packet(avctx, st, pkt);
127
+
128
+    return AVERROR_BUG;
129
+}
130
+
131
+static int ndi_setup_audio(AVFormatContext *avctx, AVStream *st)
132
+{
133
+    struct NDIContext *ctx = avctx->priv_data;
134
+    AVCodecParameters *c = st->codecpar;
135
+
136
+    if (ctx->audio) {
137
+        av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
138
+        return AVERROR(EINVAL);
139
+    }
140
+
141
+    ctx->audio = av_mallocz(sizeof(NDIlib_audio_frame_interleaved_16s_t));
142
+    if (!ctx->audio)
143
+        return AVERROR(ENOMEM);
144
+
145
+    ctx->audio->sample_rate = c->sample_rate;
146
+    ctx->audio->no_channels = c->channels;
147
+    ctx->audio->reference_level = ctx->reference_level;
148
+
149
+    avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
150
+
151
+    return 0;
152
+}
153
+
154
+static int ndi_setup_video(AVFormatContext *avctx, AVStream *st)
155
+{
156
+    struct NDIContext *ctx = avctx->priv_data;
157
+    AVCodecParameters *c = st->codecpar;
158
+
159
+    if (ctx->video) {
160
+        av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
161
+        return AVERROR(EINVAL);
162
+    }
163
+
164
+    if (c->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
165
+        av_log(avctx, AV_LOG_ERROR, "Unsupported codec format!"
166
+               " Only AV_CODEC_ID_WRAPPED_AVFRAME is supported (-vcodec wrapped_avframe).\n");
167
+        return AVERROR(EINVAL);
168
+    }
169
+
170
+    if (c->format != AV_PIX_FMT_UYVY422 && c->format != AV_PIX_FMT_BGRA &&
171
+        c->format != AV_PIX_FMT_BGR0 && c->format != AV_PIX_FMT_RGBA &&
172
+        c->format != AV_PIX_FMT_RGB0) {
173
+        av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
174
+               " Only AV_PIX_FMT_UYVY422, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,"
175
+               " AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB0 is supported.\n");
176
+        return AVERROR(EINVAL);
177
+    }
178
+
179
+    if (c->field_order == AV_FIELD_BB || c->field_order == AV_FIELD_BT) {
180
+        av_log(avctx, AV_LOG_ERROR, "Lower field-first disallowed");
181
+        return AVERROR(EINVAL);
182
+    }
183
+
184
+    ctx->video = av_mallocz(sizeof(NDIlib_video_frame_t));
185
+    if (!ctx->video)
186
+        return AVERROR(ENOMEM);
187
+
188
+    switch(c->format) {
189
+        case AV_PIX_FMT_UYVY422:
190
+            ctx->video->FourCC = NDIlib_FourCC_type_UYVY;
191
+            break;
192
+        case AV_PIX_FMT_BGRA:
193
+            ctx->video->FourCC = NDIlib_FourCC_type_BGRA;
194
+            break;
195
+        case AV_PIX_FMT_BGR0:
196
+            ctx->video->FourCC = NDIlib_FourCC_type_BGRX;
197
+            break;
198
+        case AV_PIX_FMT_RGBA:
199
+            ctx->video->FourCC = NDIlib_FourCC_type_RGBA;
200
+            break;
201
+        case AV_PIX_FMT_RGB0:
202
+            ctx->video->FourCC = NDIlib_FourCC_type_RGBX;
203
+            break;
204
+    }
205
+
206
+    ctx->video->xres = c->width;
207
+    ctx->video->yres = c->height;
208
+    ctx->video->frame_rate_N = st->avg_frame_rate.num;
209
+    ctx->video->frame_rate_D = st->avg_frame_rate.den;
210
+    ctx->video->frame_format_type = c->field_order == AV_FIELD_PROGRESSIVE
211
+        ? NDIlib_frame_format_type_progressive
212
+        : NDIlib_frame_format_type_interleaved;
213
+
214
+    if (st->sample_aspect_ratio.num) {
215
+        AVRational display_aspect_ratio;
216
+        av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
217
+                  st->codecpar->width  * (int64_t)st->sample_aspect_ratio.num,
218
+                  st->codecpar->height * (int64_t)st->sample_aspect_ratio.den,
219
+                  1024 * 1024);
220
+        ctx->video->picture_aspect_ratio = av_q2d(display_aspect_ratio);
221
+    }
222
+    else
223
+        ctx->video->picture_aspect_ratio = (double)st->codecpar->width/st->codecpar->height;
224
+
225
+    avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
226
+
227
+    return 0;
228
+}
229
+
230
+static int ndi_write_header(AVFormatContext *avctx)
231
+{
232
+    int ret = 0;
233
+    unsigned int n;
234
+    struct NDIContext *ctx = avctx->priv_data;
235
+    const NDIlib_send_create_t ndi_send_desc = { .p_ndi_name = avctx->filename,
236
+        .p_groups = NULL, .clock_video = ctx->clock_video, .clock_audio = ctx->clock_audio };
237
+
238
+    if (!NDIlib_initialize()) {
239
+        av_log(avctx, AV_LOG_ERROR, "NDIlib_initialize failed.\n");
240
+        return AVERROR_EXTERNAL;
241
+    }
242
+
243
+    /* check if streams compatible */
244
+    for (n = 0; n < avctx->nb_streams; n++) {
245
+        AVStream *st = avctx->streams[n];
246
+        AVCodecParameters *c = st->codecpar;
247
+        if        (c->codec_type == AVMEDIA_TYPE_AUDIO) {
248
+            if ((ret = ndi_setup_audio(avctx, st)))
249
+                goto error;
250
+        } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
251
+            if ((ret = ndi_setup_video(avctx, st)))
252
+                goto error;
253
+        } else {
254
+            av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
255
+            ret = AVERROR(EINVAL);
256
+            goto error;
257
+        }
258
+    }
259
+
260
+    ctx->ndi_send = NDIlib_send_create(&ndi_send_desc);
261
+    if (!ctx->ndi_send) {
262
+        av_log(avctx, AV_LOG_ERROR, "Failed to create NDI output %s\n", avctx->filename);
263
+        ret = AVERROR_EXTERNAL;
264
+    }
265
+
266
+error:
267
+    return ret;
268
+}
269
+
270
+#define OFFSET(x) offsetof(struct NDIContext, x)
271
+static const AVOption options[] = {
272
+    { "reference_level", "The audio reference level in dB"  , OFFSET(reference_level), AV_OPT_TYPE_INT, { .i64 = 0 }, -20, 20, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM},
273
+    { "clock_video", "These specify whether video 'clock' themselves"  , OFFSET(clock_video), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
274
+    { "clock_audio", "These specify whether audio 'clock' themselves"  , OFFSET(clock_audio), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
275
+    { NULL },
276
+};
277
+
278
+static const AVClass libndi_newtek_muxer_class = {
279
+    .class_name = "NDI muxer",
280
+    .item_name  = av_default_item_name,
281
+    .option     = options,
282
+    .version    = LIBAVUTIL_VERSION_INT,
283
+    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
284
+};
285
+
286
+AVOutputFormat ff_libndi_newtek_muxer = {
287
+    .name           = "libndi_newtek",
288
+    .long_name      = NULL_IF_CONFIG_SMALL("Network Device Interface (NDI) output using NewTek library"),
289
+    .audio_codec    = AV_CODEC_ID_PCM_S16LE,
290
+    .video_codec    = AV_CODEC_ID_WRAPPED_AVFRAME,
291
+    .subtitle_codec = AV_CODEC_ID_NONE,
292
+    .flags          = AVFMT_NOFILE,
293
+    .priv_class     = &libndi_newtek_muxer_class,
294
+    .priv_data_size = sizeof(struct NDIContext),
295
+    .write_header   = ndi_write_header,
296
+    .write_packet   = ndi_write_packet,
297
+    .write_trailer  = ndi_write_trailer,
298
+};
... ...
@@ -28,8 +28,8 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVDEVICE_VERSION_MAJOR  57
31
-#define LIBAVDEVICE_VERSION_MINOR   7
32
-#define LIBAVDEVICE_VERSION_MICRO 101
31
+#define LIBAVDEVICE_VERSION_MINOR   8
32
+#define LIBAVDEVICE_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
35 35
                                                LIBAVDEVICE_VERSION_MINOR, \