]> git.sesse.net Git - ffmpeg/blob - libavdevice/decklink_enc.cpp
avformat/argo_brp: remove an allocation
[ffmpeg] / libavdevice / decklink_enc.cpp
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
22 #include <atomic>
23 using std::atomic;
24
25 /* Include internal.h first to avoid conflict between winsock.h (used by
26  * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
27 extern "C" {
28 #include "libavformat/internal.h"
29 }
30
31 #include <DeckLinkAPI.h>
32
33 extern "C" {
34 #include "libavformat/avformat.h"
35 #include "libavutil/imgutils.h"
36 #include "avdevice.h"
37 }
38
39 #include "decklink_common.h"
40 #include "decklink_enc.h"
41 #if CONFIG_LIBKLVANC
42 #include "libklvanc/vanc.h"
43 #include "libklvanc/vanc-lines.h"
44 #include "libklvanc/pixels.h"
45 #endif
46
47 /* DeckLink callback class declaration */
48 class decklink_frame : public IDeckLinkVideoFrame
49 {
50 public:
51     decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe, AVCodecID codec_id, int height, int width) :
52         _ctx(ctx), _avframe(avframe), _avpacket(NULL), _codec_id(codec_id), _ancillary(NULL), _height(height), _width(width),  _refs(1) { }
53     decklink_frame(struct decklink_ctx *ctx, AVPacket *avpacket, AVCodecID codec_id, int height, int width) :
54         _ctx(ctx), _avframe(NULL), _avpacket(avpacket), _codec_id(codec_id), _ancillary(NULL), _height(height), _width(width), _refs(1) { }
55     virtual long           STDMETHODCALLTYPE GetWidth      (void)          { return _width; }
56     virtual long           STDMETHODCALLTYPE GetHeight     (void)          { return _height; }
57     virtual long           STDMETHODCALLTYPE GetRowBytes   (void)
58     {
59       if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
60           return _avframe->linesize[0] < 0 ? -_avframe->linesize[0] : _avframe->linesize[0];
61       else
62           return ((GetWidth() + 47) / 48) * 128;
63     }
64     virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void)
65     {
66         if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
67             return bmdFormat8BitYUV;
68         else
69             return bmdFormat10BitYUV;
70     }
71     virtual BMDFrameFlags  STDMETHODCALLTYPE GetFlags      (void)
72     {
73        if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
74            return _avframe->linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault;
75        else
76            return bmdFrameFlagDefault;
77     }
78
79     virtual HRESULT        STDMETHODCALLTYPE GetBytes      (void **buffer)
80     {
81         if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
82             if (_avframe->linesize[0] < 0)
83                 *buffer = (void *)(_avframe->data[0] + _avframe->linesize[0] * (_avframe->height - 1));
84             else
85                 *buffer = (void *)(_avframe->data[0]);
86         } else {
87             *buffer = (void *)(_avpacket->data);
88         }
89         return S_OK;
90     }
91
92     virtual HRESULT STDMETHODCALLTYPE GetTimecode     (BMDTimecodeFormat format, IDeckLinkTimecode **timecode) { return S_FALSE; }
93     virtual HRESULT STDMETHODCALLTYPE GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary)
94     {
95         *ancillary = _ancillary;
96         if (_ancillary) {
97             _ancillary->AddRef();
98             return S_OK;
99         } else {
100             return S_FALSE;
101         }
102     }
103     virtual HRESULT STDMETHODCALLTYPE SetAncillaryData(IDeckLinkVideoFrameAncillary *ancillary)
104     {
105         if (_ancillary)
106             _ancillary->Release();
107         _ancillary = ancillary;
108         _ancillary->AddRef();
109         return S_OK;
110     }
111     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
112     virtual ULONG   STDMETHODCALLTYPE AddRef(void)                            { return ++_refs; }
113     virtual ULONG   STDMETHODCALLTYPE Release(void)
114     {
115         int ret = --_refs;
116         if (!ret) {
117             av_frame_free(&_avframe);
118             av_packet_free(&_avpacket);
119             if (_ancillary)
120                 _ancillary->Release();
121             delete this;
122         }
123         return ret;
124     }
125
126     struct decklink_ctx *_ctx;
127     AVFrame *_avframe;
128     AVPacket *_avpacket;
129     AVCodecID _codec_id;
130     IDeckLinkVideoFrameAncillary *_ancillary;
131     int _height;
132     int _width;
133
134 private:
135     std::atomic<int>  _refs;
136 };
137
138 class decklink_output_callback : public IDeckLinkVideoOutputCallback
139 {
140 public:
141     virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame *_frame, BMDOutputFrameCompletionResult result)
142     {
143         decklink_frame *frame = static_cast<decklink_frame *>(_frame);
144         struct decklink_ctx *ctx = frame->_ctx;
145
146         if (frame->_avframe)
147             av_frame_unref(frame->_avframe);
148         if (frame->_avpacket)
149             av_packet_unref(frame->_avpacket);
150
151         pthread_mutex_lock(&ctx->mutex);
152         ctx->frames_buffer_available_spots++;
153         pthread_cond_broadcast(&ctx->cond);
154         pthread_mutex_unlock(&ctx->mutex);
155
156         return S_OK;
157     }
158     virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped(void)       { return S_OK; }
159     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
160     virtual ULONG   STDMETHODCALLTYPE AddRef(void)                            { return 1; }
161     virtual ULONG   STDMETHODCALLTYPE Release(void)                           { return 1; }
162 };
163
164 static int decklink_setup_video(AVFormatContext *avctx, AVStream *st)
165 {
166     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
167     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
168     AVCodecParameters *c = st->codecpar;
169
170     if (ctx->video) {
171         av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
172         return -1;
173     }
174
175     if (c->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
176         if (c->format != AV_PIX_FMT_UYVY422) {
177             av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
178                    " Only AV_PIX_FMT_UYVY422 is supported.\n");
179             return -1;
180         }
181         ctx->raw_format = bmdFormat8BitYUV;
182     } else if (c->codec_id != AV_CODEC_ID_V210) {
183         av_log(avctx, AV_LOG_ERROR, "Unsupported codec type!"
184                " Only V210 and wrapped frame with AV_PIX_FMT_UYVY422 are supported.\n");
185         return -1;
186     } else {
187         ctx->raw_format = bmdFormat10BitYUV;
188     }
189
190     if (ff_decklink_set_configs(avctx, DIRECTION_OUT) < 0) {
191         av_log(avctx, AV_LOG_ERROR, "Could not set output configuration\n");
192         return -1;
193     }
194     if (ff_decklink_set_format(avctx, c->width, c->height,
195                             st->time_base.num, st->time_base.den, c->field_order)) {
196         av_log(avctx, AV_LOG_ERROR, "Unsupported video size, framerate or field order!"
197                " Check available formats with -list_formats 1.\n");
198         return -1;
199     }
200     if (ctx->supports_vanc && ctx->dlo->EnableVideoOutput(ctx->bmd_mode, bmdVideoOutputVANC) != S_OK) {
201         av_log(avctx, AV_LOG_WARNING, "Could not enable video output with VANC! Trying without...\n");
202         ctx->supports_vanc = 0;
203     }
204     if (!ctx->supports_vanc && ctx->dlo->EnableVideoOutput(ctx->bmd_mode, bmdVideoOutputFlagDefault) != S_OK) {
205         av_log(avctx, AV_LOG_ERROR, "Could not enable video output!\n");
206         return -1;
207     }
208
209     /* Set callback. */
210     ctx->output_callback = new decklink_output_callback();
211     ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback);
212
213     ctx->frames_preroll = st->time_base.den * ctx->preroll;
214     if (st->time_base.den > 1000)
215         ctx->frames_preroll /= 1000;
216
217     /* Buffer twice as many frames as the preroll. */
218     ctx->frames_buffer = ctx->frames_preroll * 2;
219     ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
220     pthread_mutex_init(&ctx->mutex, NULL);
221     pthread_cond_init(&ctx->cond, NULL);
222     ctx->frames_buffer_available_spots = ctx->frames_buffer;
223
224     av_log(avctx, AV_LOG_DEBUG, "output: %s, preroll: %d, frames buffer size: %d\n",
225            avctx->url, ctx->frames_preroll, ctx->frames_buffer);
226
227     /* The device expects the framerate to be fixed. */
228     avpriv_set_pts_info(st, 64, st->time_base.num, st->time_base.den);
229
230     ctx->video = 1;
231
232     return 0;
233 }
234
235 static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st)
236 {
237     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
238     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
239     AVCodecParameters *c = st->codecpar;
240
241     if (ctx->audio) {
242         av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
243         return -1;
244     }
245     if (c->sample_rate != 48000) {
246         av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!"
247                " Only 48kHz is supported.\n");
248         return -1;
249     }
250     if (c->channels != 2 && c->channels != 8 && c->channels != 16) {
251         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!"
252                " Only 2, 8 or 16 channels are supported.\n");
253         return -1;
254     }
255     if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
256                                     bmdAudioSampleType16bitInteger,
257                                     c->channels,
258                                     bmdAudioOutputStreamTimestamped) != S_OK) {
259         av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n");
260         return -1;
261     }
262     if (ctx->dlo->BeginAudioPreroll() != S_OK) {
263         av_log(avctx, AV_LOG_ERROR, "Could not begin audio preroll!\n");
264         return -1;
265     }
266
267     /* The device expects the sample rate to be fixed. */
268     avpriv_set_pts_info(st, 64, 1, c->sample_rate);
269     ctx->channels = c->channels;
270
271     ctx->audio = 1;
272
273     return 0;
274 }
275
276 av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
277 {
278     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
279     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
280
281     if (ctx->playback_started) {
282         BMDTimeValue actual;
283         ctx->dlo->StopScheduledPlayback(ctx->last_pts * ctx->bmd_tb_num,
284                                         &actual, ctx->bmd_tb_den);
285         ctx->dlo->DisableVideoOutput();
286         if (ctx->audio)
287             ctx->dlo->DisableAudioOutput();
288     }
289
290     ff_decklink_cleanup(avctx);
291
292     if (ctx->output_callback)
293         delete ctx->output_callback;
294
295     pthread_mutex_destroy(&ctx->mutex);
296     pthread_cond_destroy(&ctx->cond);
297
298 #if CONFIG_LIBKLVANC
299     klvanc_context_destroy(ctx->vanc_ctx);
300 #endif
301
302     av_freep(&cctx->ctx);
303
304     return 0;
305 }
306
307 #if CONFIG_LIBKLVANC
308 static void construct_cc(AVFormatContext *avctx, struct decklink_ctx *ctx,
309                          AVPacket *pkt, struct klvanc_line_set_s *vanc_lines)
310 {
311     struct klvanc_packet_eia_708b_s *cdp;
312     uint16_t *cdp_words;
313     uint16_t len;
314     uint8_t cc_count;
315     int size, ret, i;
316
317     const uint8_t *data = av_packet_get_side_data(pkt, AV_PKT_DATA_A53_CC, &size);
318     if (!data)
319         return;
320
321     cc_count = size / 3;
322
323     ret = klvanc_create_eia708_cdp(&cdp);
324     if (ret)
325         return;
326
327     ret = klvanc_set_framerate_EIA_708B(cdp, ctx->bmd_tb_num, ctx->bmd_tb_den);
328     if (ret) {
329         av_log(avctx, AV_LOG_ERROR, "Invalid framerate specified: %lld/%lld\n",
330                ctx->bmd_tb_num, ctx->bmd_tb_den);
331         klvanc_destroy_eia708_cdp(cdp);
332         return;
333     }
334
335     if (cc_count > KLVANC_MAX_CC_COUNT) {
336         av_log(avctx, AV_LOG_ERROR, "Illegal cc_count received: %d\n", cc_count);
337         cc_count = KLVANC_MAX_CC_COUNT;
338     }
339
340     /* CC data */
341     cdp->header.ccdata_present = 1;
342     cdp->header.caption_service_active = 1;
343     cdp->ccdata.cc_count = cc_count;
344     for (i = 0; i < cc_count; i++) {
345         if (data [3*i] & 0x04)
346             cdp->ccdata.cc[i].cc_valid = 1;
347         cdp->ccdata.cc[i].cc_type = data[3*i] & 0x03;
348         cdp->ccdata.cc[i].cc_data[0] = data[3*i+1];
349         cdp->ccdata.cc[i].cc_data[1] = data[3*i+2];
350     }
351
352     klvanc_finalize_EIA_708B(cdp, ctx->cdp_sequence_num++);
353     ret = klvanc_convert_EIA_708B_to_words(cdp, &cdp_words, &len);
354     klvanc_destroy_eia708_cdp(cdp);
355     if (ret != 0) {
356         av_log(avctx, AV_LOG_ERROR, "Failed converting 708 packet to words\n");
357         return;
358     }
359
360     ret = klvanc_line_insert(ctx->vanc_ctx, vanc_lines, cdp_words, len, 11, 0);
361     free(cdp_words);
362     if (ret != 0) {
363         av_log(avctx, AV_LOG_ERROR, "VANC line insertion failed\n");
364         return;
365     }
366 }
367
368 static int decklink_construct_vanc(AVFormatContext *avctx, struct decklink_ctx *ctx,
369                                    AVPacket *pkt, decklink_frame *frame)
370 {
371     struct klvanc_line_set_s vanc_lines = { 0 };
372     int ret = 0, i;
373
374     if (!ctx->supports_vanc)
375         return 0;
376
377     construct_cc(avctx, ctx, pkt, &vanc_lines);
378
379     IDeckLinkVideoFrameAncillary *vanc;
380     int result = ctx->dlo->CreateAncillaryData(bmdFormat10BitYUV, &vanc);
381     if (result != S_OK) {
382         av_log(avctx, AV_LOG_ERROR, "Failed to create vanc\n");
383         ret = AVERROR(EIO);
384         goto done;
385     }
386
387     /* Now that we've got all the VANC lines in a nice orderly manner, generate the
388        final VANC sections for the Decklink output */
389     for (i = 0; i < vanc_lines.num_lines; i++) {
390         struct klvanc_line_s *line = vanc_lines.lines[i];
391         int real_line;
392         void *buf;
393
394         if (!line)
395             break;
396
397         /* FIXME: include hack for certain Decklink cards which mis-represent
398            line numbers for pSF frames */
399         real_line = line->line_number;
400
401         result = vanc->GetBufferForVerticalBlankingLine(real_line, &buf);
402         if (result != S_OK) {
403             av_log(avctx, AV_LOG_ERROR, "Failed to get VANC line %d: %d", real_line, result);
404             continue;
405         }
406
407         /* Generate the full line taking into account all VANC packets on that line */
408         result = klvanc_generate_vanc_line_v210(ctx->vanc_ctx, line, (uint8_t *) buf,
409                                                 ctx->bmd_width);
410         if (result) {
411             av_log(avctx, AV_LOG_ERROR, "Failed to generate VANC line\n");
412             continue;
413         }
414     }
415
416     result = frame->SetAncillaryData(vanc);
417     vanc->Release();
418     if (result != S_OK) {
419         av_log(avctx, AV_LOG_ERROR, "Failed to set vanc: %d", result);
420         ret = AVERROR(EIO);
421     }
422
423 done:
424     for (i = 0; i < vanc_lines.num_lines; i++)
425         klvanc_line_free(vanc_lines.lines[i]);
426
427     return ret;
428 }
429 #endif
430
431 static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
432 {
433     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
434     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
435     AVStream *st = avctx->streams[pkt->stream_index];
436     AVFrame *avframe = NULL, *tmp = (AVFrame *)pkt->data;
437     AVPacket *avpacket = NULL;
438     decklink_frame *frame;
439     buffercount_type buffered;
440     HRESULT hr;
441
442     if (st->codecpar->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
443         if (tmp->format != AV_PIX_FMT_UYVY422 ||
444             tmp->width  != ctx->bmd_width ||
445             tmp->height != ctx->bmd_height) {
446             av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format or dimension.\n");
447             return AVERROR(EINVAL);
448         }
449
450         avframe = av_frame_clone(tmp);
451         if (!avframe) {
452             av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
453             return AVERROR(EIO);
454         }
455
456         frame = new decklink_frame(ctx, avframe, st->codecpar->codec_id, avframe->height, avframe->width);
457     } else {
458         avpacket = av_packet_clone(pkt);
459         if (!avpacket) {
460             av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
461             return AVERROR(EIO);
462         }
463
464         frame = new decklink_frame(ctx, avpacket, st->codecpar->codec_id, ctx->bmd_height, ctx->bmd_width);
465
466 #if CONFIG_LIBKLVANC
467         if (decklink_construct_vanc(avctx, ctx, pkt, frame))
468             av_log(avctx, AV_LOG_ERROR, "Failed to construct VANC\n");
469 #endif
470     }
471
472     if (!frame) {
473         av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
474         av_frame_free(&avframe);
475         av_packet_free(&avpacket);
476         return AVERROR(EIO);
477     }
478
479     /* Always keep at most one second of frames buffered. */
480     pthread_mutex_lock(&ctx->mutex);
481     while (ctx->frames_buffer_available_spots == 0) {
482         pthread_cond_wait(&ctx->cond, &ctx->mutex);
483     }
484     ctx->frames_buffer_available_spots--;
485     pthread_mutex_unlock(&ctx->mutex);
486
487     /* Schedule frame for playback. */
488     hr = ctx->dlo->ScheduleVideoFrame((class IDeckLinkVideoFrame *) frame,
489                                       pkt->pts * ctx->bmd_tb_num,
490                                       ctx->bmd_tb_num, ctx->bmd_tb_den);
491     /* Pass ownership to DeckLink, or release on failure */
492     frame->Release();
493     if (hr != S_OK) {
494         av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
495                 " error %08x.\n", (uint32_t) hr);
496         return AVERROR(EIO);
497     }
498
499     ctx->dlo->GetBufferedVideoFrameCount(&buffered);
500     av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
501     if (pkt->pts > 2 && buffered <= 2)
502         av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
503                " Video may misbehave!\n");
504
505     /* Preroll video frames. */
506     if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
507         av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
508         if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
509             av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
510             return AVERROR(EIO);
511         }
512         av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
513         if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
514             av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
515             return AVERROR(EIO);
516         }
517         ctx->playback_started = 1;
518     }
519
520     return 0;
521 }
522
523 static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
524 {
525     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
526     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
527     int sample_count = pkt->size / (ctx->channels << 1);
528     buffercount_type buffered;
529
530     ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
531     if (pkt->pts > 1 && !buffered)
532         av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
533                " Audio will misbehave!\n");
534
535     if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
536                                        bmdAudioSampleRate48kHz, NULL) != S_OK) {
537         av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
538         return AVERROR(EIO);
539     }
540
541     return 0;
542 }
543
544 extern "C" {
545
546 av_cold int ff_decklink_write_header(AVFormatContext *avctx)
547 {
548     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
549     struct decklink_ctx *ctx;
550     unsigned int n;
551     int ret;
552
553     ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
554     if (!ctx)
555         return AVERROR(ENOMEM);
556     ctx->list_devices = cctx->list_devices;
557     ctx->list_formats = cctx->list_formats;
558     ctx->preroll      = cctx->preroll;
559     ctx->duplex_mode  = cctx->duplex_mode;
560     cctx->ctx = ctx;
561 #if CONFIG_LIBKLVANC
562     if (klvanc_context_create(&ctx->vanc_ctx) < 0) {
563         av_log(avctx, AV_LOG_ERROR, "Cannot create VANC library context\n");
564         return AVERROR(ENOMEM);
565     }
566     ctx->supports_vanc = 1;
567 #endif
568
569     /* List available devices and exit. */
570     if (ctx->list_devices) {
571         av_log(avctx, AV_LOG_WARNING, "The -list_devices option is deprecated and will be removed. Please use ffmpeg -sinks decklink instead.\n");
572         ff_decklink_list_devices_legacy(avctx, 0, 1);
573         return AVERROR_EXIT;
574     }
575
576     ret = ff_decklink_init_device(avctx, avctx->url);
577     if (ret < 0)
578         return ret;
579
580     /* Get output device. */
581     if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
582         av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
583                avctx->url);
584         ret = AVERROR(EIO);
585         goto error;
586     }
587
588     /* List supported formats. */
589     if (ctx->list_formats) {
590         ff_decklink_list_formats(avctx);
591         ret = AVERROR_EXIT;
592         goto error;
593     }
594
595     /* Setup streams. */
596     ret = AVERROR(EIO);
597     for (n = 0; n < avctx->nb_streams; n++) {
598         AVStream *st = avctx->streams[n];
599         AVCodecParameters *c = st->codecpar;
600         if        (c->codec_type == AVMEDIA_TYPE_AUDIO) {
601             if (decklink_setup_audio(avctx, st))
602                 goto error;
603         } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
604             if (decklink_setup_video(avctx, st))
605                 goto error;
606         } else {
607             av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
608             goto error;
609         }
610     }
611
612     return 0;
613
614 error:
615     ff_decklink_cleanup(avctx);
616     return ret;
617 }
618
619 int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
620 {
621     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
622     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
623     AVStream *st = avctx->streams[pkt->stream_index];
624
625     ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
626
627     if      (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
628         return decklink_write_video_packet(avctx, pkt);
629     else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
630         return decklink_write_audio_packet(avctx, pkt);
631
632     return AVERROR(EIO);
633 }
634
635 int ff_decklink_list_output_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list)
636 {
637     return ff_decklink_list_devices(avctx, device_list, 0, 1);
638 }
639
640 } /* extern "C" */