decklink_enc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Blackmagic DeckLink output
  3. * Copyright (c) 2013-2014 Ramiro Polla
  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 <DeckLinkAPI.h>
  22. #include <pthread.h>
  23. #include <semaphore.h>
  24. extern "C" {
  25. #include "libavformat/avformat.h"
  26. #include "libavformat/internal.h"
  27. #include "libavutil/imgutils.h"
  28. }
  29. #include "decklink_common.h"
  30. #include "decklink_enc.h"
  31. /* DeckLink callback class declaration */
  32. class decklink_frame : public IDeckLinkVideoFrame
  33. {
  34. public:
  35. decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe, long width,
  36. long height, void *buffer) :
  37. _ctx(ctx), _avframe(avframe), _width(width),
  38. _height(height), _buffer(buffer), _refs(0) { }
  39. virtual long STDMETHODCALLTYPE GetWidth (void) { return _width; }
  40. virtual long STDMETHODCALLTYPE GetHeight (void) { return _height; }
  41. virtual long STDMETHODCALLTYPE GetRowBytes (void) { return _width<<1; }
  42. virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void) { return bmdFormat8BitYUV; }
  43. virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void) { return bmdVideoOutputFlagDefault; }
  44. virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer) { *buffer = _buffer; return S_OK; }
  45. virtual HRESULT STDMETHODCALLTYPE GetTimecode (BMDTimecodeFormat format, IDeckLinkTimecode **timecode) { return S_FALSE; }
  46. virtual HRESULT STDMETHODCALLTYPE GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary) { return S_FALSE; }
  47. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  48. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return ++_refs; }
  49. virtual ULONG STDMETHODCALLTYPE Release(void) { if (!--_refs) delete this; return _refs; }
  50. struct decklink_ctx *_ctx;
  51. AVFrame *_avframe;
  52. private:
  53. long _width;
  54. long _height;
  55. void *_buffer;
  56. int _refs;
  57. };
  58. class decklink_output_callback : public IDeckLinkVideoOutputCallback
  59. {
  60. public:
  61. virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame *_frame, BMDOutputFrameCompletionResult result)
  62. {
  63. decklink_frame *frame = static_cast<decklink_frame *>(_frame);
  64. struct decklink_ctx *ctx = frame->_ctx;
  65. AVFrame *avframe = frame->_avframe;
  66. av_frame_free(&avframe);
  67. sem_post(&ctx->semaphore);
  68. return S_OK;
  69. }
  70. virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped(void) { return S_OK; }
  71. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  72. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
  73. virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
  74. };
  75. static int decklink_setup_video(AVFormatContext *avctx, AVStream *st)
  76. {
  77. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  78. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  79. AVCodecContext *c = st->codec;
  80. if (ctx->video) {
  81. av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
  82. return -1;
  83. }
  84. if (c->pix_fmt != AV_PIX_FMT_UYVY422) {
  85. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
  86. " Only AV_PIX_FMT_UYVY422 is supported.\n");
  87. return -1;
  88. }
  89. if (ff_decklink_set_format(avctx, c->width, c->height,
  90. c->time_base.num, c->time_base.den)) {
  91. av_log(avctx, AV_LOG_ERROR, "Unsupported video size or framerate!"
  92. " Check available formats with -list_formats 1.\n");
  93. return -1;
  94. }
  95. if (ctx->dlo->EnableVideoOutput(ctx->bmd_mode,
  96. bmdVideoOutputFlagDefault) != S_OK) {
  97. av_log(avctx, AV_LOG_ERROR, "Could not enable video output!\n");
  98. return -1;
  99. }
  100. /* Set callback. */
  101. ctx->output_callback = new decklink_output_callback();
  102. ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback);
  103. /* Start video semaphore. */
  104. ctx->frames_preroll = c->time_base.den * ctx->preroll;
  105. if (c->time_base.den > 1000)
  106. ctx->frames_preroll /= 1000;
  107. /* Buffer twice as many frames as the preroll. */
  108. ctx->frames_buffer = ctx->frames_preroll * 2;
  109. ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
  110. sem_init(&ctx->semaphore, 0, ctx->frames_buffer);
  111. /* The device expects the framerate to be fixed. */
  112. avpriv_set_pts_info(st, 64, c->time_base.num, c->time_base.den);
  113. ctx->video = 1;
  114. return 0;
  115. }
  116. static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st)
  117. {
  118. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  119. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  120. AVCodecContext *c = st->codec;
  121. if (ctx->audio) {
  122. av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
  123. return -1;
  124. }
  125. if (c->sample_rate != 48000) {
  126. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!"
  127. " Only 48kHz is supported.\n");
  128. return -1;
  129. }
  130. if (c->channels != 2 && c->channels != 8) {
  131. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!"
  132. " Only stereo and 7.1 are supported.\n");
  133. return -1;
  134. }
  135. if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
  136. bmdAudioSampleType16bitInteger,
  137. c->channels,
  138. bmdAudioOutputStreamTimestamped) != S_OK) {
  139. av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n");
  140. return -1;
  141. }
  142. if (ctx->dlo->BeginAudioPreroll() != S_OK) {
  143. av_log(avctx, AV_LOG_ERROR, "Could not begin audio preroll!\n");
  144. return -1;
  145. }
  146. /* The device expects the sample rate to be fixed. */
  147. avpriv_set_pts_info(st, 64, 1, c->sample_rate);
  148. ctx->channels = c->channels;
  149. ctx->audio = 1;
  150. return 0;
  151. }
  152. av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
  153. {
  154. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  155. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  156. if (ctx->playback_started) {
  157. BMDTimeValue actual;
  158. ctx->dlo->StopScheduledPlayback(ctx->last_pts * ctx->bmd_tb_num,
  159. &actual, ctx->bmd_tb_den);
  160. ctx->dlo->DisableVideoOutput();
  161. if (ctx->audio)
  162. ctx->dlo->DisableAudioOutput();
  163. }
  164. ff_decklink_cleanup(avctx);
  165. if (ctx->output_callback)
  166. delete ctx->output_callback;
  167. sem_destroy(&ctx->semaphore);
  168. av_freep(&cctx->ctx);
  169. return 0;
  170. }
  171. static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
  172. {
  173. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  174. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  175. AVPicture *avpicture = (AVPicture *) pkt->data;
  176. AVFrame *avframe, *tmp;
  177. decklink_frame *frame;
  178. buffercount_type buffered;
  179. HRESULT hr;
  180. /* HACK while av_uncoded_frame() isn't implemented */
  181. int ret;
  182. tmp = av_frame_alloc();
  183. if (!tmp)
  184. return AVERROR(ENOMEM);
  185. tmp->format = AV_PIX_FMT_UYVY422;
  186. tmp->width = ctx->bmd_width;
  187. tmp->height = ctx->bmd_height;
  188. ret = av_frame_get_buffer(tmp, 32);
  189. if (ret < 0) {
  190. av_frame_free(&tmp);
  191. return ret;
  192. }
  193. av_image_copy(tmp->data, tmp->linesize, (const uint8_t **) avpicture->data,
  194. avpicture->linesize, (AVPixelFormat) tmp->format, tmp->width,
  195. tmp->height);
  196. avframe = av_frame_clone(tmp);
  197. av_frame_free(&tmp);
  198. if (!avframe) {
  199. av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
  200. return AVERROR(EIO);
  201. }
  202. /* end HACK */
  203. frame = new decklink_frame(ctx, avframe, ctx->bmd_width, ctx->bmd_height,
  204. (void *) avframe->data[0]);
  205. if (!frame) {
  206. av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
  207. return AVERROR(EIO);
  208. }
  209. /* Always keep at most one second of frames buffered. */
  210. sem_wait(&ctx->semaphore);
  211. /* Schedule frame for playback. */
  212. hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame,
  213. pkt->pts * ctx->bmd_tb_num,
  214. ctx->bmd_tb_num, ctx->bmd_tb_den);
  215. if (hr != S_OK) {
  216. av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
  217. " error %08x.\n", (uint32_t) hr);
  218. frame->Release();
  219. return AVERROR(EIO);
  220. }
  221. ctx->dlo->GetBufferedVideoFrameCount(&buffered);
  222. av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
  223. if (pkt->pts > 2 && buffered <= 2)
  224. av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
  225. " Video may misbehave!\n");
  226. /* Preroll video frames. */
  227. if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
  228. av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
  229. if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
  230. av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
  231. return AVERROR(EIO);
  232. }
  233. av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
  234. if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
  235. av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
  236. return AVERROR(EIO);
  237. }
  238. ctx->playback_started = 1;
  239. }
  240. return 0;
  241. }
  242. static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
  243. {
  244. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  245. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  246. int sample_count = pkt->size / (ctx->channels << 1);
  247. buffercount_type buffered;
  248. ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
  249. if (pkt->pts > 1 && !buffered)
  250. av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
  251. " Audio will misbehave!\n");
  252. if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
  253. bmdAudioSampleRate48kHz, NULL) != S_OK) {
  254. av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
  255. return AVERROR(EIO);
  256. }
  257. return 0;
  258. }
  259. extern "C" {
  260. av_cold int ff_decklink_write_header(AVFormatContext *avctx)
  261. {
  262. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  263. struct decklink_ctx *ctx;
  264. unsigned int n;
  265. int ret;
  266. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  267. if (!ctx)
  268. return AVERROR(ENOMEM);
  269. ctx->list_devices = cctx->list_devices;
  270. ctx->list_formats = cctx->list_formats;
  271. ctx->preroll = cctx->preroll;
  272. cctx->ctx = ctx;
  273. /* List available devices. */
  274. if (ctx->list_devices) {
  275. ff_decklink_list_devices(avctx);
  276. return AVERROR_EXIT;
  277. }
  278. ret = ff_decklink_init_device(avctx, avctx->filename);
  279. if (ret < 0)
  280. return ret;
  281. /* Get output device. */
  282. if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
  283. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  284. avctx->filename);
  285. ret = AVERROR(EIO);
  286. goto error;
  287. }
  288. /* List supported formats. */
  289. if (ctx->list_formats) {
  290. ff_decklink_list_formats(avctx);
  291. ret = AVERROR_EXIT;
  292. goto error;
  293. }
  294. /* Setup streams. */
  295. ret = AVERROR(EIO);
  296. for (n = 0; n < avctx->nb_streams; n++) {
  297. AVStream *st = avctx->streams[n];
  298. AVCodecContext *c = st->codec;
  299. if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
  300. if (decklink_setup_audio(avctx, st))
  301. goto error;
  302. } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
  303. if (decklink_setup_video(avctx, st))
  304. goto error;
  305. } else {
  306. av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
  307. goto error;
  308. }
  309. }
  310. return 0;
  311. error:
  312. ff_decklink_cleanup(avctx);
  313. return ret;
  314. }
  315. int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
  316. {
  317. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  318. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  319. AVStream *st = avctx->streams[pkt->stream_index];
  320. ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
  321. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  322. return decklink_write_video_packet(avctx, pkt);
  323. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  324. return decklink_write_audio_packet(avctx, pkt);
  325. return AVERROR(EIO);
  326. }
  327. } /* extern "C" */