2 * Blackmagic DeckLink output
3 * Copyright (c) 2013-2014 Ramiro Polla
5 * This file is part of FFmpeg.
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.
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.
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
22 #include <DeckLinkAPI.h>
25 #include <semaphore.h>
28 #include "libavformat/avformat.h"
29 #include "libavformat/internal.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/atomic.h"
34 #include "decklink_common.h"
35 #include "decklink_enc.h"
38 /* DeckLink callback class declaration */
39 class decklink_frame : public IDeckLinkVideoFrame
42 decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe) :
43 _ctx(ctx), _avframe(avframe), _refs(1) { }
45 virtual long STDMETHODCALLTYPE GetWidth (void) { return _avframe->width; }
46 virtual long STDMETHODCALLTYPE GetHeight (void) { return _avframe->height; }
47 virtual long STDMETHODCALLTYPE GetRowBytes (void) { return _avframe->linesize[0] < 0 ? -_avframe->linesize[0] : _avframe->linesize[0]; }
48 virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void) { return bmdFormat8BitYUV; }
49 virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void) { return _avframe->linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault; }
50 virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer)
52 if (_avframe->linesize[0] < 0)
53 *buffer = (void *)(_avframe->data[0] + _avframe->linesize[0] * (_avframe->height - 1));
55 *buffer = (void *)(_avframe->data[0]);
59 virtual HRESULT STDMETHODCALLTYPE GetTimecode (BMDTimecodeFormat format, IDeckLinkTimecode **timecode) { return S_FALSE; }
60 virtual HRESULT STDMETHODCALLTYPE GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary) { return S_FALSE; }
62 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
63 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return avpriv_atomic_int_add_and_fetch(&_refs, 1); }
64 virtual ULONG STDMETHODCALLTYPE Release(void)
66 int ret = avpriv_atomic_int_add_and_fetch(&_refs, -1);
68 av_frame_free(&_avframe);
74 struct decklink_ctx *_ctx;
81 class decklink_output_callback : public IDeckLinkVideoOutputCallback
84 virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame *_frame, BMDOutputFrameCompletionResult result)
86 decklink_frame *frame = static_cast<decklink_frame *>(_frame);
87 struct decklink_ctx *ctx = frame->_ctx;
88 AVFrame *avframe = frame->_avframe;
90 av_frame_unref(avframe);
92 sem_post(&ctx->semaphore);
96 virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped(void) { return S_OK; }
97 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
98 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
99 virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
102 static int decklink_setup_video(AVFormatContext *avctx, AVStream *st)
104 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
105 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
106 AVCodecParameters *c = st->codecpar;
109 av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
113 if (c->format != AV_PIX_FMT_UYVY422) {
114 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
115 " Only AV_PIX_FMT_UYVY422 is supported.\n");
118 if (ff_decklink_set_format(avctx, c->width, c->height,
119 st->time_base.num, st->time_base.den, c->field_order)) {
120 av_log(avctx, AV_LOG_ERROR, "Unsupported video size, framerate or field order!"
121 " Check available formats with -list_formats 1.\n");
124 if (ctx->dlo->EnableVideoOutput(ctx->bmd_mode,
125 bmdVideoOutputFlagDefault) != S_OK) {
126 av_log(avctx, AV_LOG_ERROR, "Could not enable video output!\n");
131 ctx->output_callback = new decklink_output_callback();
132 ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback);
134 /* Start video semaphore. */
135 ctx->frames_preroll = st->time_base.den * ctx->preroll;
136 if (st->time_base.den > 1000)
137 ctx->frames_preroll /= 1000;
139 /* Buffer twice as many frames as the preroll. */
140 ctx->frames_buffer = ctx->frames_preroll * 2;
141 ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
142 sem_init(&ctx->semaphore, 0, ctx->frames_buffer);
144 /* The device expects the framerate to be fixed. */
145 avpriv_set_pts_info(st, 64, st->time_base.num, st->time_base.den);
152 static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st)
154 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
155 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
156 AVCodecParameters *c = st->codecpar;
159 av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
162 if (c->sample_rate != 48000) {
163 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!"
164 " Only 48kHz is supported.\n");
167 if (c->channels != 2 && c->channels != 8) {
168 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!"
169 " Only stereo and 7.1 are supported.\n");
172 if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
173 bmdAudioSampleType16bitInteger,
175 bmdAudioOutputStreamTimestamped) != S_OK) {
176 av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n");
179 if (ctx->dlo->BeginAudioPreroll() != S_OK) {
180 av_log(avctx, AV_LOG_ERROR, "Could not begin audio preroll!\n");
184 /* The device expects the sample rate to be fixed. */
185 avpriv_set_pts_info(st, 64, 1, c->sample_rate);
186 ctx->channels = c->channels;
193 av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
195 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
196 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
198 if (ctx->playback_started) {
200 ctx->dlo->StopScheduledPlayback(ctx->last_pts * ctx->bmd_tb_num,
201 &actual, ctx->bmd_tb_den);
202 ctx->dlo->DisableVideoOutput();
204 ctx->dlo->DisableAudioOutput();
207 ff_decklink_cleanup(avctx);
209 if (ctx->output_callback)
210 delete ctx->output_callback;
212 sem_destroy(&ctx->semaphore);
214 av_freep(&cctx->ctx);
219 static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
221 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
222 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
223 AVFrame *avframe, *tmp = (AVFrame *)pkt->data;
224 decklink_frame *frame;
225 buffercount_type buffered;
228 if (tmp->format != AV_PIX_FMT_UYVY422 ||
229 tmp->width != ctx->bmd_width ||
230 tmp->height != ctx->bmd_height) {
231 av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format or dimension.\n");
232 return AVERROR(EINVAL);
234 avframe = av_frame_clone(tmp);
236 av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
240 frame = new decklink_frame(ctx, avframe);
242 av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
243 av_frame_free(&avframe);
247 /* Always keep at most one second of frames buffered. */
248 sem_wait(&ctx->semaphore);
250 /* Schedule frame for playback. */
251 hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame,
252 pkt->pts * ctx->bmd_tb_num,
253 ctx->bmd_tb_num, ctx->bmd_tb_den);
254 /* Pass ownership to DeckLink, or release on failure */
257 av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
258 " error %08x.\n", (uint32_t) hr);
262 ctx->dlo->GetBufferedVideoFrameCount(&buffered);
263 av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
264 if (pkt->pts > 2 && buffered <= 2)
265 av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
266 " Video may misbehave!\n");
268 /* Preroll video frames. */
269 if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
270 av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
271 if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
272 av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
275 av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
276 if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
277 av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
280 ctx->playback_started = 1;
286 static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
288 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
289 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
290 int sample_count = pkt->size / (ctx->channels << 1);
291 buffercount_type buffered;
293 ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
294 if (pkt->pts > 1 && !buffered)
295 av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
296 " Audio will misbehave!\n");
298 if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
299 bmdAudioSampleRate48kHz, NULL) != S_OK) {
300 av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
309 av_cold int ff_decklink_write_header(AVFormatContext *avctx)
311 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
312 struct decklink_ctx *ctx;
316 ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
318 return AVERROR(ENOMEM);
319 ctx->list_devices = cctx->list_devices;
320 ctx->list_formats = cctx->list_formats;
321 ctx->preroll = cctx->preroll;
324 /* List available devices. */
325 if (ctx->list_devices) {
326 ff_decklink_list_devices(avctx);
330 ret = ff_decklink_init_device(avctx, avctx->filename);
334 /* Get output device. */
335 if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
336 av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
342 /* List supported formats. */
343 if (ctx->list_formats) {
344 ff_decklink_list_formats(avctx);
351 for (n = 0; n < avctx->nb_streams; n++) {
352 AVStream *st = avctx->streams[n];
353 AVCodecParameters *c = st->codecpar;
354 if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
355 if (decklink_setup_audio(avctx, st))
357 } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
358 if (decklink_setup_video(avctx, st))
361 av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
369 ff_decklink_cleanup(avctx);
373 int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
375 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
376 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
377 AVStream *st = avctx->streams[pkt->stream_index];
379 ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
381 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
382 return decklink_write_video_packet(avctx, pkt);
383 else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
384 return decklink_write_audio_packet(avctx, pkt);