You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
10KB

  1. /*
  2. * NewTek NDI output
  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. #include "libavformat/avformat.h"
  22. #include "libavformat/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libndi_newtek_common.h"
  26. struct NDIContext {
  27. const AVClass *cclass;
  28. /* Options */
  29. int reference_level;
  30. int clock_video, clock_audio;
  31. NDIlib_video_frame_t *video;
  32. NDIlib_audio_frame_interleaved_16s_t *audio;
  33. NDIlib_send_instance_t ndi_send;
  34. AVFrame *last_avframe;
  35. };
  36. static int ndi_write_trailer(AVFormatContext *avctx)
  37. {
  38. struct NDIContext *ctx = avctx->priv_data;
  39. if (ctx->ndi_send) {
  40. NDIlib_send_destroy(ctx->ndi_send);
  41. av_frame_free(&ctx->last_avframe);
  42. }
  43. av_freep(&ctx->video);
  44. av_freep(&ctx->audio);
  45. return 0;
  46. }
  47. static int ndi_write_video_packet(AVFormatContext *avctx, AVStream *st, AVPacket *pkt)
  48. {
  49. struct NDIContext *ctx = avctx->priv_data;
  50. AVFrame *avframe, *tmp = (AVFrame *)pkt->data;
  51. if (tmp->format != AV_PIX_FMT_UYVY422 && tmp->format != AV_PIX_FMT_BGRA &&
  52. tmp->format != AV_PIX_FMT_BGR0 && tmp->format != AV_PIX_FMT_RGBA &&
  53. tmp->format != AV_PIX_FMT_RGB0) {
  54. av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format.\n");
  55. return AVERROR(EINVAL);
  56. }
  57. if (tmp->linesize[0] < 0) {
  58. av_log(avctx, AV_LOG_ERROR, "Got a frame with negative linesize.\n");
  59. return AVERROR(EINVAL);
  60. }
  61. if (tmp->width != ctx->video->xres ||
  62. tmp->height != ctx->video->yres) {
  63. av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid dimension.\n");
  64. av_log(avctx, AV_LOG_ERROR, "tmp->width=%d, tmp->height=%d, ctx->video->xres=%d, ctx->video->yres=%d\n",
  65. tmp->width, tmp->height, ctx->video->xres, ctx->video->yres);
  66. return AVERROR(EINVAL);
  67. }
  68. avframe = av_frame_clone(tmp);
  69. if (!avframe)
  70. return AVERROR(ENOMEM);
  71. ctx->video->timecode = av_rescale_q(pkt->pts, st->time_base, NDI_TIME_BASE_Q);
  72. ctx->video->line_stride_in_bytes = avframe->linesize[0];
  73. ctx->video->p_data = (void *)(avframe->data[0]);
  74. av_log(avctx, AV_LOG_DEBUG, "%s: pkt->pts=%"PRId64", timecode=%"PRId64", st->time_base=%d/%d\n",
  75. __func__, pkt->pts, ctx->video->timecode, st->time_base.num, st->time_base.den);
  76. /* asynchronous for one frame, but will block if a second frame
  77. is given before the first one has been sent */
  78. NDIlib_send_send_video_async(ctx->ndi_send, ctx->video);
  79. av_frame_free(&ctx->last_avframe);
  80. ctx->last_avframe = avframe;
  81. return 0;
  82. }
  83. static int ndi_write_audio_packet(AVFormatContext *avctx, AVStream *st, AVPacket *pkt)
  84. {
  85. struct NDIContext *ctx = avctx->priv_data;
  86. ctx->audio->p_data = (short *)pkt->data;
  87. ctx->audio->timecode = av_rescale_q(pkt->pts, st->time_base, NDI_TIME_BASE_Q);
  88. ctx->audio->no_samples = pkt->size / (ctx->audio->no_channels << 1);
  89. av_log(avctx, AV_LOG_DEBUG, "%s: pkt->pts=%"PRId64", timecode=%"PRId64", st->time_base=%d/%d\n",
  90. __func__, pkt->pts, ctx->audio->timecode, st->time_base.num, st->time_base.den);
  91. NDIlib_util_send_send_audio_interleaved_16s(ctx->ndi_send, ctx->audio);
  92. return 0;
  93. }
  94. static int ndi_write_packet(AVFormatContext *avctx, AVPacket *pkt)
  95. {
  96. AVStream *st = avctx->streams[pkt->stream_index];
  97. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  98. return ndi_write_video_packet(avctx, st, pkt);
  99. else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  100. return ndi_write_audio_packet(avctx, st, pkt);
  101. return AVERROR_BUG;
  102. }
  103. static int ndi_setup_audio(AVFormatContext *avctx, AVStream *st)
  104. {
  105. struct NDIContext *ctx = avctx->priv_data;
  106. AVCodecParameters *c = st->codecpar;
  107. if (ctx->audio) {
  108. av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
  109. return AVERROR(EINVAL);
  110. }
  111. ctx->audio = av_mallocz(sizeof(NDIlib_audio_frame_interleaved_16s_t));
  112. if (!ctx->audio)
  113. return AVERROR(ENOMEM);
  114. ctx->audio->sample_rate = c->sample_rate;
  115. ctx->audio->no_channels = c->channels;
  116. ctx->audio->reference_level = ctx->reference_level;
  117. avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
  118. return 0;
  119. }
  120. static int ndi_setup_video(AVFormatContext *avctx, AVStream *st)
  121. {
  122. struct NDIContext *ctx = avctx->priv_data;
  123. AVCodecParameters *c = st->codecpar;
  124. if (ctx->video) {
  125. av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
  126. return AVERROR(EINVAL);
  127. }
  128. if (c->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
  129. av_log(avctx, AV_LOG_ERROR, "Unsupported codec format!"
  130. " Only AV_CODEC_ID_WRAPPED_AVFRAME is supported (-vcodec wrapped_avframe).\n");
  131. return AVERROR(EINVAL);
  132. }
  133. if (c->format != AV_PIX_FMT_UYVY422 && c->format != AV_PIX_FMT_BGRA &&
  134. c->format != AV_PIX_FMT_BGR0 && c->format != AV_PIX_FMT_RGBA &&
  135. c->format != AV_PIX_FMT_RGB0) {
  136. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
  137. " Only AV_PIX_FMT_UYVY422, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,"
  138. " AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB0 is supported.\n");
  139. return AVERROR(EINVAL);
  140. }
  141. if (c->field_order == AV_FIELD_BB || c->field_order == AV_FIELD_BT) {
  142. av_log(avctx, AV_LOG_ERROR, "Lower field-first disallowed");
  143. return AVERROR(EINVAL);
  144. }
  145. ctx->video = av_mallocz(sizeof(NDIlib_video_frame_t));
  146. if (!ctx->video)
  147. return AVERROR(ENOMEM);
  148. switch(c->format) {
  149. case AV_PIX_FMT_UYVY422:
  150. ctx->video->FourCC = NDIlib_FourCC_type_UYVY;
  151. break;
  152. case AV_PIX_FMT_BGRA:
  153. ctx->video->FourCC = NDIlib_FourCC_type_BGRA;
  154. break;
  155. case AV_PIX_FMT_BGR0:
  156. ctx->video->FourCC = NDIlib_FourCC_type_BGRX;
  157. break;
  158. case AV_PIX_FMT_RGBA:
  159. ctx->video->FourCC = NDIlib_FourCC_type_RGBA;
  160. break;
  161. case AV_PIX_FMT_RGB0:
  162. ctx->video->FourCC = NDIlib_FourCC_type_RGBX;
  163. break;
  164. }
  165. ctx->video->xres = c->width;
  166. ctx->video->yres = c->height;
  167. ctx->video->frame_rate_N = st->avg_frame_rate.num;
  168. ctx->video->frame_rate_D = st->avg_frame_rate.den;
  169. ctx->video->frame_format_type = c->field_order == AV_FIELD_PROGRESSIVE
  170. ? NDIlib_frame_format_type_progressive
  171. : NDIlib_frame_format_type_interleaved;
  172. if (st->sample_aspect_ratio.num) {
  173. AVRational display_aspect_ratio;
  174. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  175. st->codecpar->width * (int64_t)st->sample_aspect_ratio.num,
  176. st->codecpar->height * (int64_t)st->sample_aspect_ratio.den,
  177. 1024 * 1024);
  178. ctx->video->picture_aspect_ratio = av_q2d(display_aspect_ratio);
  179. }
  180. else
  181. ctx->video->picture_aspect_ratio = (double)st->codecpar->width/st->codecpar->height;
  182. avpriv_set_pts_info(st, 64, 1, NDI_TIME_BASE);
  183. return 0;
  184. }
  185. static int ndi_write_header(AVFormatContext *avctx)
  186. {
  187. int ret = 0;
  188. unsigned int n;
  189. struct NDIContext *ctx = avctx->priv_data;
  190. const NDIlib_send_create_t ndi_send_desc = { .p_ndi_name = avctx->url,
  191. .p_groups = NULL, .clock_video = ctx->clock_video, .clock_audio = ctx->clock_audio };
  192. if (!NDIlib_initialize()) {
  193. av_log(avctx, AV_LOG_ERROR, "NDIlib_initialize failed.\n");
  194. return AVERROR_EXTERNAL;
  195. }
  196. /* check if streams compatible */
  197. for (n = 0; n < avctx->nb_streams; n++) {
  198. AVStream *st = avctx->streams[n];
  199. AVCodecParameters *c = st->codecpar;
  200. if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
  201. if ((ret = ndi_setup_audio(avctx, st)))
  202. goto error;
  203. } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
  204. if ((ret = ndi_setup_video(avctx, st)))
  205. goto error;
  206. } else {
  207. av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
  208. ret = AVERROR(EINVAL);
  209. goto error;
  210. }
  211. }
  212. ctx->ndi_send = NDIlib_send_create(&ndi_send_desc);
  213. if (!ctx->ndi_send) {
  214. av_log(avctx, AV_LOG_ERROR, "Failed to create NDI output %s\n", avctx->url);
  215. ret = AVERROR_EXTERNAL;
  216. }
  217. error:
  218. return ret;
  219. }
  220. #define OFFSET(x) offsetof(struct NDIContext, x)
  221. static const AVOption options[] = {
  222. { "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},
  223. { "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 },
  224. { "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 },
  225. { NULL },
  226. };
  227. static const AVClass libndi_newtek_muxer_class = {
  228. .class_name = "NDI muxer",
  229. .item_name = av_default_item_name,
  230. .option = options,
  231. .version = LIBAVUTIL_VERSION_INT,
  232. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  233. };
  234. AVOutputFormat ff_libndi_newtek_muxer = {
  235. .name = "libndi_newtek",
  236. .long_name = NULL_IF_CONFIG_SMALL("Network Device Interface (NDI) output using NewTek library"),
  237. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  238. .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
  239. .subtitle_codec = AV_CODEC_ID_NONE,
  240. .flags = AVFMT_NOFILE,
  241. .priv_class = &libndi_newtek_muxer_class,
  242. .priv_data_size = sizeof(struct NDIContext),
  243. .write_header = ndi_write_header,
  244. .write_packet = ndi_write_packet,
  245. .write_trailer = ndi_write_trailer,
  246. };