FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libndi_newtek_dec.c
Go to the documentation of this file.
1 /*
2  * Newtek NDI input
3  * Copyright (c) 2017 Maksym Veremeyenko
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavformat/avformat.h"
23 #include "libavformat/internal.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/imgutils.h"
26 
27 #include "libndi_newtek_common.h"
28 
29 struct NDIContext {
30  const AVClass *cclass;
31 
32  /* Options */
34  int64_t wait_sources;
36  char *extra_ips;
37 
38  /* Runtime */
39  NDIlib_recv_create_t *recv;
40  NDIlib_find_instance_t ndi_find;
41 
42  /* Streams */
44 };
45 
46 static int ndi_set_video_packet(AVFormatContext *avctx, NDIlib_video_frame_t *v, AVPacket *pkt)
47 {
48  int ret;
49  struct NDIContext *ctx = avctx->priv_data;
50 
51  ret = av_new_packet(pkt, v->yres * v->line_stride_in_bytes);
52  if (ret < 0)
53  return ret;
54 
55  pkt->dts = pkt->pts = av_rescale_q(v->timecode, NDI_TIME_BASE_Q, ctx->video_st->time_base);
56  pkt->duration = av_rescale_q(1, (AVRational){v->frame_rate_D, v->frame_rate_N}, ctx->video_st->time_base);
57 
58  av_log(avctx, AV_LOG_DEBUG, "%s: pkt->dts = pkt->pts = %"PRId64", duration=%"PRId64", timecode=%"PRId64"\n",
59  __func__, pkt->dts, pkt->duration, v->timecode);
60 
61  pkt->flags |= AV_PKT_FLAG_KEY;
62  pkt->stream_index = ctx->video_st->index;
63 
64  memcpy(pkt->data, v->p_data, pkt->size);
65 
66  return 0;
67 }
68 
69 static int ndi_set_audio_packet(AVFormatContext *avctx, NDIlib_audio_frame_t *a, AVPacket *pkt)
70 {
71  int ret;
72  struct NDIContext *ctx = avctx->priv_data;
73 
74  NDIlib_audio_frame_interleaved_16s_t dst;
75 
76  ret = av_new_packet(pkt, 2 * a->no_samples * a->no_channels);
77  if (ret < 0)
78  return ret;
79 
80  pkt->dts = pkt->pts = av_rescale_q(a->timecode, NDI_TIME_BASE_Q, ctx->audio_st->time_base);
81  pkt->duration = av_rescale_q(1, (AVRational){a->no_samples, a->sample_rate}, ctx->audio_st->time_base);
82 
83  av_log(avctx, AV_LOG_DEBUG, "%s: pkt->dts = pkt->pts = %"PRId64", duration=%"PRId64", timecode=%"PRId64"\n",
84  __func__, pkt->dts, pkt->duration, a->timecode);
85 
86  pkt->flags |= AV_PKT_FLAG_KEY;
87  pkt->stream_index = ctx->audio_st->index;
88 
89  dst.reference_level = 0;
90  dst.p_data = (short *)pkt->data;
91  NDIlib_util_audio_to_interleaved_16s(a, &dst);
92 
93  return 0;
94 }
95 
96 static int ndi_find_sources(AVFormatContext *avctx, const char *name, NDIlib_source_t *source_to_connect_to)
97 {
98  int j = AVERROR(ENODEV);
99  unsigned int n, i;
100  struct NDIContext *ctx = avctx->priv_data;
101  const NDIlib_source_t *ndi_srcs = NULL;
102  const NDIlib_find_create_t find_create_desc = { .show_local_sources = true,
103  .p_groups = NULL, .p_extra_ips = ctx->extra_ips };
104 
105  if (!ctx->ndi_find)
106  ctx->ndi_find = NDIlib_find_create2(&find_create_desc);
107  if (!ctx->ndi_find) {
108  av_log(avctx, AV_LOG_ERROR, "NDIlib_find_create failed.\n");
109  return AVERROR(EIO);
110  }
111 
112  while (1)
113  {
114  int f, t = ctx->wait_sources / 1000;
115  av_log(avctx, AV_LOG_DEBUG, "Waiting for sources %d miliseconds\n", t);
116  f = NDIlib_find_wait_for_sources(ctx->ndi_find, t);
117  av_log(avctx, AV_LOG_DEBUG, "NDIlib_find_wait_for_sources returns %d\n", f);
118  if (!f)
119  break;
120  };
121 
122  ndi_srcs = NDIlib_find_get_current_sources(ctx->ndi_find, &n);
123 
124  if (ctx->find_sources)
125  av_log(avctx, AV_LOG_INFO, "Found %d NDI sources:\n", n);
126 
127  for (i = 0; i < n; i++) {
128  if (ctx->find_sources)
129  av_log(avctx, AV_LOG_INFO, "\t'%s'\t'%s'\n", ndi_srcs[i].p_ndi_name, ndi_srcs[i].p_ip_address);
130 
131  if (!strcmp(name, ndi_srcs[i].p_ndi_name)) {
132  *source_to_connect_to = ndi_srcs[i];
133  j = i;
134  }
135  }
136 
137  return j;
138 }
139 
141 {
142  int ret;
143  NDIlib_recv_create_t recv_create_desc;
144  const NDIlib_tally_t tally_state = { .on_program = true, .on_preview = false };
145  struct NDIContext *ctx = avctx->priv_data;
146 
147  if (!NDIlib_initialize()) {
148  av_log(avctx, AV_LOG_ERROR, "NDIlib_initialize failed.\n");
149  return AVERROR_EXTERNAL;
150  }
151 
152  /* Find available sources. */
153  ret = ndi_find_sources(avctx, avctx->url, &recv_create_desc.source_to_connect_to);
154  if (ctx->find_sources) {
155  return AVERROR_EXIT;
156  }
157  if (ret < 0)
158  return ret;
159 
160  /* Create receiver description */
161  recv_create_desc.color_format = NDIlib_recv_color_format_e_UYVY_RGBA;
162  recv_create_desc.bandwidth = NDIlib_recv_bandwidth_highest;
163  recv_create_desc.allow_video_fields = ctx->allow_video_fields;
164 
165  /* Create the receiver */
166  ctx->recv = NDIlib_recv_create(&recv_create_desc);
167  if (!ctx->recv) {
168  av_log(avctx, AV_LOG_ERROR, "NDIlib_recv_create2 failed.\n");
169  return AVERROR(EIO);
170  }
171 
172  /* Set tally */
173  NDIlib_recv_set_tally(ctx->recv, &tally_state);
174 
175  avctx->ctx_flags |= AVFMTCTX_NOHEADER;
176 
177  return 0;
178 }
179 
180 static int ndi_create_video_stream(AVFormatContext *avctx, NDIlib_video_frame_t *v)
181 {
182  AVStream *st;
183  AVRational tmp;
184  struct NDIContext *ctx = avctx->priv_data;
185 
186  st = avformat_new_stream(avctx, NULL);
187  if (!st) {
188  av_log(avctx, AV_LOG_ERROR, "Cannot add video stream\n");
189  return AVERROR(ENOMEM);
190  }
191 
193  st->r_frame_rate = av_make_q(v->frame_rate_N, v->frame_rate_D);
194 
195  tmp = av_mul_q(av_d2q(v->picture_aspect_ratio, INT_MAX), (AVRational){v->yres, v->xres});
196  av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den, tmp.num, tmp.den, 1000);
197  st->codecpar->sample_aspect_ratio = st->sample_aspect_ratio;
198 
199  st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
200  st->codecpar->width = v->xres;
201  st->codecpar->height = v->yres;
202  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
203  st->codecpar->bit_rate = av_rescale(v->xres * v->yres * 16, v->frame_rate_N, v->frame_rate_D);
204  st->codecpar->field_order = v->frame_format_type == NDIlib_frame_format_type_progressive
206 
207  if (NDIlib_FourCC_type_UYVY == v->FourCC || NDIlib_FourCC_type_UYVA == v->FourCC) {
208  st->codecpar->format = AV_PIX_FMT_UYVY422;
209  st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
210  if (NDIlib_FourCC_type_UYVA == v->FourCC)
211  av_log(avctx, AV_LOG_WARNING, "Alpha channel ignored\n");
212  } else if (NDIlib_FourCC_type_BGRA == v->FourCC) {
213  st->codecpar->format = AV_PIX_FMT_BGRA;
214  st->codecpar->codec_tag = MKTAG('B', 'G', 'R', 'A');
215  } else if (NDIlib_FourCC_type_BGRX == v->FourCC) {
216  st->codecpar->format = AV_PIX_FMT_BGR0;
217  st->codecpar->codec_tag = MKTAG('B', 'G', 'R', '0');
218  } else if (NDIlib_FourCC_type_RGBA == v->FourCC) {
219  st->codecpar->format = AV_PIX_FMT_RGBA;
220  st->codecpar->codec_tag = MKTAG('R', 'G', 'B', 'A');
221  } else if (NDIlib_FourCC_type_RGBX == v->FourCC) {
222  st->codecpar->format = AV_PIX_FMT_RGB0;
223  st->codecpar->codec_tag = MKTAG('R', 'G', 'B', '0');
224  } else {
225  av_log(avctx, AV_LOG_ERROR, "Unsupported video stream format, v->FourCC=%d\n", v->FourCC);
226  return AVERROR(EINVAL);
227  }
228 
230 
231  ctx->video_st = st;
232 
233  return 0;
234 }
235 
236 static int ndi_create_audio_stream(AVFormatContext *avctx, NDIlib_audio_frame_t *a)
237 {
238  AVStream *st;
239  struct NDIContext *ctx = avctx->priv_data;
240 
241  st = avformat_new_stream(avctx, NULL);
242  if (!st) {
243  av_log(avctx, AV_LOG_ERROR, "Cannot add audio stream\n");
244  return AVERROR(ENOMEM);
245  }
246 
249  st->codecpar->sample_rate = a->sample_rate;
250  st->codecpar->channels = a->no_channels;
251 
253 
254  ctx->audio_st = st;
255 
256  return 0;
257 }
258 
260 {
261  int ret = 0;
262  struct NDIContext *ctx = avctx->priv_data;
263 
264  while (!ret) {
265  NDIlib_video_frame_t v;
266  NDIlib_audio_frame_t a;
267  NDIlib_metadata_frame_t m;
268  NDIlib_frame_type_e t;
269 
270  av_log(avctx, AV_LOG_DEBUG, "NDIlib_recv_capture...\n");
271  t = NDIlib_recv_capture(ctx->recv, &v, &a, &m, 40);
272  av_log(avctx, AV_LOG_DEBUG, "NDIlib_recv_capture=%d\n", t);
273 
274  if (t == NDIlib_frame_type_video) {
275  if (!ctx->video_st)
276  ret = ndi_create_video_stream(avctx, &v);
277  if (!ret)
278  ret = ndi_set_video_packet(avctx, &v, pkt);
279  NDIlib_recv_free_video(ctx->recv, &v);
280  break;
281  }
282  else if (t == NDIlib_frame_type_audio) {
283  if (!ctx->audio_st)
284  ret = ndi_create_audio_stream(avctx, &a);
285  if (!ret)
286  ret = ndi_set_audio_packet(avctx, &a, pkt);
287  NDIlib_recv_free_audio(ctx->recv, &a);
288  break;
289  }
290  else if (t == NDIlib_frame_type_metadata)
291  NDIlib_recv_free_metadata(ctx->recv, &m);
292  else if (t == NDIlib_frame_type_error){
293  av_log(avctx, AV_LOG_ERROR, "NDIlib_recv_capture failed with error\n");
294  ret = AVERROR(EIO);
295  }
296  };
297 
298  return ret;
299 }
300 
301 static int ndi_read_close(AVFormatContext *avctx)
302 {
303  struct NDIContext *ctx = (struct NDIContext *)avctx->priv_data;
304 
305  if (ctx->recv)
306  NDIlib_recv_destroy(ctx->recv);
307 
308  if (ctx->ndi_find)
309  NDIlib_find_destroy(ctx->ndi_find);
310 
311  return 0;
312 }
313 
314 #define OFFSET(x) offsetof(struct NDIContext, x)
315 #define DEC AV_OPT_FLAG_DECODING_PARAM
316 
317 static const AVOption options[] = {
318  { "find_sources", "Find available sources" , OFFSET(find_sources), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, DEC },
319  { "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 },
320  { "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 },
321  { "extra_ips", "List of comma separated ip addresses to scan for remote sources", OFFSET(extra_ips), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
322  { NULL },
323 };
324 
326  .class_name = "NDI demuxer",
327  .item_name = av_default_item_name,
328  .option = options,
329  .version = LIBAVUTIL_VERSION_INT,
331 };
332 
334  .name = "libndi_newtek",
335  .long_name = NULL_IF_CONFIG_SMALL("Network Device Interface (NDI) input using NewTek library"),
336  .flags = AVFMT_NOFILE,
337  .priv_class = &libndi_newtek_demuxer_class,
338  .priv_data_size = sizeof(struct NDIContext),
339  .read_header = ndi_read_header,
340  .read_packet = ndi_read_packet,
341  .read_close = ndi_read_close,
342 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
#define NULL
Definition: coverity.c:32
AVOption.
Definition: opt.h:246
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4882
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
static const AVOption options[]
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1400
static int ndi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
AVInputFormat ff_libndi_newtek_demuxer
Format I/O context.
Definition: avformat.h:1351
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static int ndi_read_header(AVFormatContext *avctx)
NDIlib_recv_create_t * recv
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1295
AVOptions.
#define NDI_TIME_BASE_Q
#define f(width, name)
Definition: cbs_vp9.c:255
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1463
AVStream * audio_st
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
uint8_t * data
Definition: avcodec.h:1445
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
char * url
input or output URL.
Definition: avformat.h:1447
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
static int ndi_create_video_stream(AVFormatContext *avctx, NDIlib_video_frame_t *v)
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
static int ndi_read_close(AVFormatContext *avctx)
NDIlib_find_instance_t ndi_find
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
static const AVClass libndi_newtek_demuxer_class
AVFormatContext * ctx
Definition: movenc.c:48
int n
Definition: avisynth_c.h:684
static int ndi_find_sources(AVFormatContext *avctx, const char *name, NDIlib_source_t *source_to_connect_to)
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
Stream structure.
Definition: avformat.h:874
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
Describe the class of an AVClass context structure.
Definition: log.h:67
static int ndi_create_audio_stream(AVFormatContext *avctx, NDIlib_audio_frame_t *a)
Rational number (pair of numerator and denominator).
Definition: rational.h:58
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
static int ndi_set_video_packet(AVFormatContext *avctx, NDIlib_video_frame_t *v, AVPacket *pkt)
static int ndi_set_audio_packet(AVFormatContext *avctx, NDIlib_audio_frame_t *a, AVPacket *pkt)
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
AVStream * video_st
int sample_rate
Audio only.
Definition: avcodec.h:4010
Main libavformat public API header.
#define DEC
if(ret< 0)
Definition: vf_mcdeint.c:279
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
int64_t wait_sources
const AVClass * cclass
void * priv_data
Format private data.
Definition: avformat.h:1379
int channels
Audio only.
Definition: avcodec.h:4006
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
#define OFFSET(x)
#define NDI_TIME_BASE
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:998
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1422
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
const char * name
Definition: opengl_enc.c:103
static uint8_t tmp[11]
Definition: aes_ctr.c:26