decklink_enc.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. if (ctx->dlo)
  165. ctx->dlo->Release();
  166. if (ctx->dl)
  167. ctx->dl->Release();
  168. if (ctx->output_callback)
  169. delete ctx->output_callback;
  170. sem_destroy(&ctx->semaphore);
  171. av_freep(&cctx->ctx);
  172. return 0;
  173. }
  174. static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
  175. {
  176. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  177. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  178. AVPicture *avpicture = (AVPicture *) pkt->data;
  179. AVFrame *avframe, *tmp;
  180. decklink_frame *frame;
  181. buffercount_type buffered;
  182. HRESULT hr;
  183. /* HACK while av_uncoded_frame() isn't implemented */
  184. int ret;
  185. tmp = av_frame_alloc();
  186. if (!tmp)
  187. return AVERROR(ENOMEM);
  188. tmp->format = AV_PIX_FMT_UYVY422;
  189. tmp->width = ctx->bmd_width;
  190. tmp->height = ctx->bmd_height;
  191. ret = av_frame_get_buffer(tmp, 32);
  192. if (ret < 0) {
  193. av_frame_free(&tmp);
  194. return ret;
  195. }
  196. av_image_copy(tmp->data, tmp->linesize, (const uint8_t **) avpicture->data,
  197. avpicture->linesize, (AVPixelFormat) tmp->format, tmp->width,
  198. tmp->height);
  199. avframe = av_frame_clone(tmp);
  200. av_frame_free(&tmp);
  201. if (!avframe) {
  202. av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
  203. return AVERROR(EIO);
  204. }
  205. /* end HACK */
  206. frame = new decklink_frame(ctx, avframe, ctx->bmd_width, ctx->bmd_height,
  207. (void *) avframe->data[0]);
  208. if (!frame) {
  209. av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
  210. return AVERROR(EIO);
  211. }
  212. /* Always keep at most one second of frames buffered. */
  213. sem_wait(&ctx->semaphore);
  214. /* Schedule frame for playback. */
  215. hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame,
  216. pkt->pts * ctx->bmd_tb_num,
  217. ctx->bmd_tb_num, ctx->bmd_tb_den);
  218. if (hr != S_OK) {
  219. av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
  220. " error %08x.\n", (uint32_t) hr);
  221. frame->Release();
  222. return AVERROR(EIO);
  223. }
  224. ctx->dlo->GetBufferedVideoFrameCount(&buffered);
  225. av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
  226. if (pkt->pts > 2 && buffered <= 2)
  227. av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
  228. " Video may misbehave!\n");
  229. /* Preroll video frames. */
  230. if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
  231. av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
  232. if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
  233. av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
  234. return AVERROR(EIO);
  235. }
  236. av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
  237. if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
  238. av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
  239. return AVERROR(EIO);
  240. }
  241. ctx->playback_started = 1;
  242. }
  243. return 0;
  244. }
  245. static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
  246. {
  247. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  248. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  249. int sample_count = pkt->size / (ctx->channels << 1);
  250. buffercount_type buffered;
  251. ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
  252. if (pkt->pts > 1 && !buffered)
  253. av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
  254. " Audio will misbehave!\n");
  255. if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
  256. bmdAudioSampleRate48kHz, NULL) != S_OK) {
  257. av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
  258. return AVERROR(EIO);
  259. }
  260. return 0;
  261. }
  262. extern "C" {
  263. av_cold int ff_decklink_write_header(AVFormatContext *avctx)
  264. {
  265. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  266. struct decklink_ctx *ctx;
  267. IDeckLinkDisplayModeIterator *itermode;
  268. IDeckLinkIterator *iter;
  269. IDeckLink *dl = NULL;
  270. unsigned int n;
  271. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  272. if (!ctx)
  273. return AVERROR(ENOMEM);
  274. ctx->list_devices = cctx->list_devices;
  275. ctx->list_formats = cctx->list_formats;
  276. ctx->preroll = cctx->preroll;
  277. cctx->ctx = ctx;
  278. iter = CreateDeckLinkIteratorInstance();
  279. if (!iter) {
  280. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  281. return AVERROR(EIO);
  282. }
  283. /* List available devices. */
  284. if (ctx->list_devices) {
  285. ff_decklink_list_devices(avctx);
  286. return AVERROR_EXIT;
  287. }
  288. /* Open device. */
  289. while (iter->Next(&dl) == S_OK) {
  290. const char *displayName;
  291. ff_decklink_get_display_name(dl, &displayName);
  292. if (!strcmp(avctx->filename, displayName)) {
  293. av_free((void *) displayName);
  294. ctx->dl = dl;
  295. break;
  296. }
  297. av_free((void *) displayName);
  298. dl->Release();
  299. }
  300. iter->Release();
  301. if (!ctx->dl) {
  302. av_log(avctx, AV_LOG_ERROR, "Could not open '%s'\n", avctx->filename);
  303. return AVERROR(EIO);
  304. }
  305. /* Get output device. */
  306. if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
  307. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  308. avctx->filename);
  309. ctx->dl->Release();
  310. return AVERROR(EIO);
  311. }
  312. /* List supported formats. */
  313. if (ctx->list_formats) {
  314. ff_decklink_list_formats(avctx);
  315. ctx->dlo->Release();
  316. ctx->dl->Release();
  317. return AVERROR_EXIT;
  318. }
  319. if (ctx->dlo->GetDisplayModeIterator(&itermode) != S_OK) {
  320. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  321. ctx->dl->Release();
  322. return AVERROR(EIO);
  323. }
  324. /* Setup streams. */
  325. for (n = 0; n < avctx->nb_streams; n++) {
  326. AVStream *st = avctx->streams[n];
  327. AVCodecContext *c = st->codec;
  328. if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
  329. if (decklink_setup_audio(avctx, st))
  330. goto error;
  331. } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
  332. if (decklink_setup_video(avctx, st))
  333. goto error;
  334. } else {
  335. av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
  336. goto error;
  337. }
  338. }
  339. itermode->Release();
  340. return 0;
  341. error:
  342. ctx->dlo->Release();
  343. ctx->dl->Release();
  344. return AVERROR(EIO);
  345. }
  346. int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
  347. {
  348. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  349. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  350. AVStream *st = avctx->streams[pkt->stream_index];
  351. ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
  352. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  353. return decklink_write_video_packet(avctx, pkt);
  354. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  355. return decklink_write_audio_packet(avctx, pkt);
  356. return AVERROR(EIO);
  357. }
  358. } /* extern "C" */