]> git.sesse.net Git - ffmpeg/blob - libavdevice/decklink_dec.cpp
Merge commit '38434a9ff5b9a1a048f32c1c7e2a9519cf12f8ba'
[ffmpeg] / libavdevice / decklink_dec.cpp
1 /*
2  * Blackmagic DeckLink input
3  * Copyright (c) 2013-2014 Luca Barbato, Deti Fliegl
4  * Copyright (c) 2014 Rafaël Carré
5  * Copyright (c) 2017 Akamai Technologies, Inc.
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /* Include internal.h first to avoid conflict between winsock.h (used by
25  * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
26 extern "C" {
27 #include "libavformat/internal.h"
28 }
29
30 #include <DeckLinkAPI.h>
31
32 extern "C" {
33 #include "config.h"
34 #include "libavformat/avformat.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/avutil.h"
37 #include "libavutil/common.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/time.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/reverse.h"
43 #include "avdevice.h"
44 #if CONFIG_LIBZVBI
45 #include <libzvbi.h>
46 #endif
47 }
48
49 #include "decklink_common.h"
50 #include "decklink_dec.h"
51
52 #define MAX_WIDTH_VANC 1920
53 const BMDDisplayMode AUTODETECT_DEFAULT_MODE = bmdModeNTSC;
54
55 typedef struct VANCLineNumber {
56     BMDDisplayMode mode;
57     int vanc_start;
58     int field0_vanc_end;
59     int field1_vanc_start;
60     int vanc_end;
61 } VANCLineNumber;
62
63 /* These VANC line numbers need not be very accurate. In any case
64  * GetBufferForVerticalBlankingLine() will return an error when invalid
65  * ancillary line number was requested. We just need to make sure that the
66  * entire VANC region is covered, while making sure we don't decode VANC of
67  * another source during switching*/
68 static VANCLineNumber vanc_line_numbers[] = {
69     /* SD Modes */
70
71     {bmdModeNTSC, 11, 19, 274, 282},
72     {bmdModeNTSC2398, 11, 19, 274, 282},
73     {bmdModePAL, 7, 22, 320, 335},
74     {bmdModeNTSCp, 11, -1, -1, 39},
75     {bmdModePALp, 7, -1, -1, 45},
76
77     /* HD 1080 Modes */
78
79     {bmdModeHD1080p2398, 8, -1, -1, 42},
80     {bmdModeHD1080p24, 8, -1, -1, 42},
81     {bmdModeHD1080p25, 8, -1, -1, 42},
82     {bmdModeHD1080p2997, 8, -1, -1, 42},
83     {bmdModeHD1080p30, 8, -1, -1, 42},
84     {bmdModeHD1080i50, 8, 20, 570, 585},
85     {bmdModeHD1080i5994, 8, 20, 570, 585},
86     {bmdModeHD1080i6000, 8, 20, 570, 585},
87     {bmdModeHD1080p50, 8, -1, -1, 42},
88     {bmdModeHD1080p5994, 8, -1, -1, 42},
89     {bmdModeHD1080p6000, 8, -1, -1, 42},
90
91      /* HD 720 Modes */
92
93     {bmdModeHD720p50, 8, -1, -1, 26},
94     {bmdModeHD720p5994, 8, -1, -1, 26},
95     {bmdModeHD720p60, 8, -1, -1, 26},
96
97     /* For all other modes, for which we don't support VANC */
98     {bmdModeUnknown, 0, -1, -1, -1}
99 };
100
101 static int get_vanc_line_idx(BMDDisplayMode mode)
102 {
103     unsigned int i;
104     for (i = 0; i < FF_ARRAY_ELEMS(vanc_line_numbers); i++) {
105         if (mode == vanc_line_numbers[i].mode)
106             return i;
107     }
108     /* Return the VANC idx for Unknown mode */
109     return i - 1;
110 }
111
112 static inline void clear_parity_bits(uint16_t *buf, int len) {
113     int i;
114     for (i = 0; i < len; i++)
115         buf[i] &= 0xff;
116 }
117
118 static int check_vanc_parity_checksum(uint16_t *buf, int len, uint16_t checksum) {
119     int i;
120     uint16_t vanc_sum = 0;
121     for (i = 3; i < len - 1; i++) {
122         uint16_t v = buf[i];
123         int np = v >> 8;
124         int p = av_parity(v & 0xff);
125         if ((!!p ^ !!(v & 0x100)) || (np != 1 && np != 2)) {
126             // Parity check failed
127             return -1;
128         }
129         vanc_sum += v;
130     }
131     vanc_sum &= 0x1ff;
132     vanc_sum |= ((~vanc_sum & 0x100) << 1);
133     if (checksum != vanc_sum) {
134         // Checksum verification failed
135         return -1;
136     }
137     return 0;
138 }
139
140 /* The 10-bit VANC data is packed in V210, we only need the luma component. */
141 static void extract_luma_from_v210(uint16_t *dst, const uint8_t *src, int width)
142 {
143     int i;
144     for (i = 0; i < width / 3; i++) {
145         *dst++ = (src[1] >> 2) + ((src[2] & 15) << 6);
146         *dst++ =  src[4]       + ((src[5] &  3) << 8);
147         *dst++ = (src[6] >> 4) + ((src[7] & 63) << 4);
148         src += 8;
149     }
150 }
151
152 static uint8_t calc_parity_and_line_offset(int line)
153 {
154     uint8_t ret = (line < 313) << 5;
155     if (line >= 7 && line <= 22)
156         ret += line;
157     if (line >= 320 && line <= 335)
158         ret += (line - 313);
159     return ret;
160 }
161
162 static void fill_data_unit_head(int line, uint8_t *tgt)
163 {
164     tgt[0] = 0x02; // data_unit_id
165     tgt[1] = 0x2c; // data_unit_length
166     tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
167     tgt[3] = 0xe4; // framing code
168 }
169
170 #if CONFIG_LIBZVBI
171 static uint8_t* teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt, vbi_pixfmt fmt)
172 {
173     vbi_bit_slicer slicer;
174
175     vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, fmt);
176
177     if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
178         return tgt;
179
180     fill_data_unit_head(line, tgt);
181
182     return tgt + 46;
183 }
184
185 static uint8_t* teletext_data_unit_from_vbi_data_10bit(int line, uint8_t *src, uint8_t *tgt)
186 {
187     uint8_t y[720];
188     uint8_t *py = y;
189     uint8_t *pend = y + 720;
190     /* The 10-bit VBI data is packed in V210, but libzvbi only supports 8-bit,
191      * so we extract the 8 MSBs of the luma component, that is enough for
192      * teletext bit slicing. */
193     while (py < pend) {
194         *py++ = (src[1] >> 4) + ((src[2] & 15) << 4);
195         *py++ = (src[4] >> 2) + ((src[5] & 3 ) << 6);
196         *py++ = (src[6] >> 6) + ((src[7] & 63) << 2);
197         src += 8;
198     }
199     return teletext_data_unit_from_vbi_data(line, y, tgt, VBI_PIXFMT_YUV420);
200 }
201 #endif
202
203 static uint8_t* teletext_data_unit_from_op47_vbi_packet(int line, uint16_t *py, uint8_t *tgt)
204 {
205     int i;
206
207     if (py[0] != 0x255 || py[1] != 0x255 || py[2] != 0x227)
208         return tgt;
209
210     fill_data_unit_head(line, tgt);
211
212     py += 3;
213     tgt += 4;
214
215     for (i = 0; i < 42; i++)
216        *tgt++ = ff_reverse[py[i] & 255];
217
218     return tgt;
219 }
220
221 static int linemask_matches(int line, int64_t mask)
222 {
223     int shift = -1;
224     if (line >= 6 && line <= 22)
225         shift = line - 6;
226     if (line >= 318 && line <= 335)
227         shift = line - 318 + 17;
228     return shift >= 0 && ((1ULL << shift) & mask);
229 }
230
231 static uint8_t* teletext_data_unit_from_op47_data(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines)
232 {
233     if (py < pend - 9) {
234         if (py[0] == 0x151 && py[1] == 0x115 && py[3] == 0x102) {       // identifier, identifier, format code for WST teletext
235             uint16_t *descriptors = py + 4;
236             int i;
237             py += 9;
238             for (i = 0; i < 5 && py < pend - 45; i++, py += 45) {
239                 int line = (descriptors[i] & 31) + (!(descriptors[i] & 128)) * 313;
240                 if (line && linemask_matches(line, wanted_lines))
241                     tgt = teletext_data_unit_from_op47_vbi_packet(line, py, tgt);
242             }
243         }
244     }
245     return tgt;
246 }
247
248 static uint8_t* teletext_data_unit_from_ancillary_packet(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines, int allow_multipacket)
249 {
250     uint16_t did = py[0];                                               // data id
251     uint16_t sdid = py[1];                                              // secondary data id
252     uint16_t dc = py[2] & 255;                                          // data count
253     py += 3;
254     pend = FFMIN(pend, py + dc);
255     if (did == 0x143 && sdid == 0x102) {                                // subtitle distribution packet
256         tgt = teletext_data_unit_from_op47_data(py, pend, tgt, wanted_lines);
257     } else if (allow_multipacket && did == 0x143 && sdid == 0x203) {    // VANC multipacket
258         py += 2;                                                        // priority, line/field
259         while (py < pend - 3) {
260             tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
261             py += 4 + (py[2] & 255);                                    // ndid, nsdid, ndc, line/field
262         }
263     }
264     return tgt;
265 }
266
267 static uint8_t *vanc_to_cc(AVFormatContext *avctx, uint16_t *buf, size_t words,
268                            unsigned &cc_count)
269 {
270     size_t i, len = (buf[5] & 0xff) + 6 + 1;
271     uint8_t cdp_sum, rate;
272     uint16_t hdr, ftr;
273     uint8_t *cc;
274     uint16_t *cdp = &buf[6]; // CDP follows
275     if (cdp[0] != 0x96 || cdp[1] != 0x69) {
276         av_log(avctx, AV_LOG_WARNING, "Invalid CDP header 0x%.2x 0x%.2x\n", cdp[0], cdp[1]);
277         return NULL;
278     }
279
280     len -= 7; // remove VANC header and checksum
281
282     if (cdp[2] != len) {
283         av_log(avctx, AV_LOG_WARNING, "CDP len %d != %zu\n", cdp[2], len);
284         return NULL;
285     }
286
287     cdp_sum = 0;
288     for (i = 0; i < len - 1; i++)
289         cdp_sum += cdp[i];
290     cdp_sum = cdp_sum ? 256 - cdp_sum : 0;
291     if (cdp[len - 1] != cdp_sum) {
292         av_log(avctx, AV_LOG_WARNING, "CDP checksum invalid 0x%.4x != 0x%.4x\n", cdp_sum, cdp[len-1]);
293         return NULL;
294     }
295
296     rate = cdp[3];
297     if (!(rate & 0x0f)) {
298         av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
299         return NULL;
300     }
301     rate >>= 4;
302     if (rate > 8) {
303         av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
304         return NULL;
305     }
306
307     if (!(cdp[4] & 0x43)) /* ccdata_present | caption_service_active | reserved */ {
308         av_log(avctx, AV_LOG_WARNING, "CDP flags invalid (0x%.2x)\n", cdp[4]);
309         return NULL;
310     }
311
312     hdr = (cdp[5] << 8) | cdp[6];
313     if (cdp[7] != 0x72) /* ccdata_id */ {
314         av_log(avctx, AV_LOG_WARNING, "Invalid ccdata_id 0x%.2x\n", cdp[7]);
315         return NULL;
316     }
317
318     cc_count = cdp[8];
319     if (!(cc_count & 0xe0)) {
320         av_log(avctx, AV_LOG_WARNING, "Invalid cc_count 0x%.2x\n", cc_count);
321         return NULL;
322     }
323
324     cc_count &= 0x1f;
325     if ((len - 13) < cc_count * 3) {
326         av_log(avctx, AV_LOG_WARNING, "Invalid cc_count %d (> %zu)\n", cc_count * 3, len - 13);
327         return NULL;
328     }
329
330     if (cdp[len - 4] != 0x74) /* footer id */ {
331         av_log(avctx, AV_LOG_WARNING, "Invalid footer id 0x%.2x\n", cdp[len-4]);
332         return NULL;
333     }
334
335     ftr = (cdp[len - 3] << 8) | cdp[len - 2];
336     if (ftr != hdr) {
337         av_log(avctx, AV_LOG_WARNING, "Header 0x%.4x != Footer 0x%.4x\n", hdr, ftr);
338         return NULL;
339     }
340
341     cc = (uint8_t *)av_malloc(cc_count * 3);
342     if (cc == NULL) {
343         av_log(avctx, AV_LOG_WARNING, "CC - av_malloc failed for cc_count = %d\n", cc_count);
344         return NULL;
345     }
346
347     for (size_t i = 0; i < cc_count; i++) {
348         cc[3*i + 0] = cdp[9 + 3*i+0] /* & 3 */;
349         cc[3*i + 1] = cdp[9 + 3*i+1];
350         cc[3*i + 2] = cdp[9 + 3*i+2];
351     }
352
353     cc_count *= 3;
354     return cc;
355 }
356
357 static uint8_t *get_metadata(AVFormatContext *avctx, uint16_t *buf, size_t width,
358                              uint8_t *tgt, size_t tgt_size, AVPacket *pkt)
359 {
360     decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
361     uint16_t *max_buf = buf + width;
362
363     while (buf < max_buf - 6) {
364         int len;
365         uint16_t did = buf[3] & 0xFF;                                  // data id
366         uint16_t sdid = buf[4] & 0xFF;                                 // secondary data id
367         /* Check for VANC header */
368         if (buf[0] != 0 || buf[1] != 0x3ff || buf[2] != 0x3ff) {
369             return tgt;
370         }
371
372         len = (buf[5] & 0xff) + 6 + 1;
373         if (len > max_buf - buf) {
374             av_log(avctx, AV_LOG_WARNING, "Data Count (%d) > data left (%zu)\n",
375                     len, max_buf - buf);
376             return tgt;
377         }
378
379         if (did == 0x43 && (sdid == 0x02 || sdid == 0x03) && cctx->teletext_lines &&
380             width == 1920 && tgt_size >= 1920) {
381             if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
382                 av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
383                 goto skip_packet;
384             }
385             tgt = teletext_data_unit_from_ancillary_packet(buf + 3, buf + len, tgt, cctx->teletext_lines, 1);
386         } else if (did == 0x61 && sdid == 0x01) {
387             unsigned int data_len;
388             uint8_t *data;
389             if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
390                 av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
391                 goto skip_packet;
392             }
393             clear_parity_bits(buf, len);
394             data = vanc_to_cc(avctx, buf, width, data_len);
395             if (data) {
396                 if (av_packet_add_side_data(pkt, AV_PKT_DATA_A53_CC, data, data_len) < 0)
397                     av_free(data);
398             }
399         } else {
400             av_log(avctx, AV_LOG_DEBUG, "Unknown meta data DID = 0x%.2x SDID = 0x%.2x\n",
401                     did, sdid);
402         }
403 skip_packet:
404         buf += len;
405     }
406
407     return tgt;
408 }
409
410 static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
411 {
412     struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
413     memset(q, 0, sizeof(AVPacketQueue));
414     pthread_mutex_init(&q->mutex, NULL);
415     pthread_cond_init(&q->cond, NULL);
416     q->avctx = avctx;
417     q->max_q_size = ctx->queue_size;
418 }
419
420 static void avpacket_queue_flush(AVPacketQueue *q)
421 {
422     AVPacketList *pkt, *pkt1;
423
424     pthread_mutex_lock(&q->mutex);
425     for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
426         pkt1 = pkt->next;
427         av_packet_unref(&pkt->pkt);
428         av_freep(&pkt);
429     }
430     q->last_pkt   = NULL;
431     q->first_pkt  = NULL;
432     q->nb_packets = 0;
433     q->size       = 0;
434     pthread_mutex_unlock(&q->mutex);
435 }
436
437 static void avpacket_queue_end(AVPacketQueue *q)
438 {
439     avpacket_queue_flush(q);
440     pthread_mutex_destroy(&q->mutex);
441     pthread_cond_destroy(&q->cond);
442 }
443
444 static unsigned long long avpacket_queue_size(AVPacketQueue *q)
445 {
446     unsigned long long size;
447     pthread_mutex_lock(&q->mutex);
448     size = q->size;
449     pthread_mutex_unlock(&q->mutex);
450     return size;
451 }
452
453 static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
454 {
455     AVPacketList *pkt1;
456     int ret;
457
458     // Drop Packet if queue size is > maximum queue size
459     if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
460         av_log(q->avctx, AV_LOG_WARNING,  "Decklink input buffer overrun!\n");
461         return -1;
462     }
463
464     pkt1 = (AVPacketList *)av_mallocz(sizeof(AVPacketList));
465     if (!pkt1) {
466         return -1;
467     }
468     ret = av_packet_ref(&pkt1->pkt, pkt);
469     av_packet_unref(pkt);
470     if (ret < 0) {
471         av_free(pkt1);
472         return -1;
473     }
474     pkt1->next = NULL;
475
476     pthread_mutex_lock(&q->mutex);
477
478     if (!q->last_pkt) {
479         q->first_pkt = pkt1;
480     } else {
481         q->last_pkt->next = pkt1;
482     }
483
484     q->last_pkt = pkt1;
485     q->nb_packets++;
486     q->size += pkt1->pkt.size + sizeof(*pkt1);
487
488     pthread_cond_signal(&q->cond);
489
490     pthread_mutex_unlock(&q->mutex);
491     return 0;
492 }
493
494 static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
495 {
496     AVPacketList *pkt1;
497     int ret;
498
499     pthread_mutex_lock(&q->mutex);
500
501     for (;; ) {
502         pkt1 = q->first_pkt;
503         if (pkt1) {
504             q->first_pkt = pkt1->next;
505             if (!q->first_pkt) {
506                 q->last_pkt = NULL;
507             }
508             q->nb_packets--;
509             q->size -= pkt1->pkt.size + sizeof(*pkt1);
510             *pkt     = pkt1->pkt;
511             av_free(pkt1);
512             ret = 1;
513             break;
514         } else if (!block) {
515             ret = 0;
516             break;
517         } else {
518             pthread_cond_wait(&q->cond, &q->mutex);
519         }
520     }
521     pthread_mutex_unlock(&q->mutex);
522     return ret;
523 }
524
525 class decklink_input_callback : public IDeckLinkInputCallback
526 {
527 public:
528         decklink_input_callback(AVFormatContext *_avctx);
529         ~decklink_input_callback();
530
531         virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
532         virtual ULONG STDMETHODCALLTYPE AddRef(void);
533         virtual ULONG STDMETHODCALLTYPE  Release(void);
534         virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
535         virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
536
537 private:
538         ULONG           m_refCount;
539         pthread_mutex_t m_mutex;
540         AVFormatContext *avctx;
541         decklink_ctx    *ctx;
542         int no_video;
543         int64_t initial_video_pts;
544         int64_t initial_audio_pts;
545 };
546
547 decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
548 {
549     avctx = _avctx;
550     decklink_cctx       *cctx = (struct decklink_cctx *)avctx->priv_data;
551     ctx = (struct decklink_ctx *)cctx->ctx;
552     no_video = 0;
553     initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
554     pthread_mutex_init(&m_mutex, NULL);
555 }
556
557 decklink_input_callback::~decklink_input_callback()
558 {
559     pthread_mutex_destroy(&m_mutex);
560 }
561
562 ULONG decklink_input_callback::AddRef(void)
563 {
564     pthread_mutex_lock(&m_mutex);
565     m_refCount++;
566     pthread_mutex_unlock(&m_mutex);
567
568     return (ULONG)m_refCount;
569 }
570
571 ULONG decklink_input_callback::Release(void)
572 {
573     pthread_mutex_lock(&m_mutex);
574     m_refCount--;
575     pthread_mutex_unlock(&m_mutex);
576
577     if (m_refCount == 0) {
578         delete this;
579         return 0;
580     }
581
582     return (ULONG)m_refCount;
583 }
584
585 static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
586                            IDeckLinkAudioInputPacket *audioFrame,
587                            int64_t wallclock,
588                            int64_t abs_wallclock,
589                            DecklinkPtsSource pts_src,
590                            AVRational time_base, int64_t *initial_pts,
591                            int copyts)
592 {
593     int64_t pts = AV_NOPTS_VALUE;
594     BMDTimeValue bmd_pts;
595     BMDTimeValue bmd_duration;
596     HRESULT res = E_INVALIDARG;
597     switch (pts_src) {
598         case PTS_SRC_AUDIO:
599             if (audioFrame)
600                 res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
601             break;
602         case PTS_SRC_VIDEO:
603             if (videoFrame)
604                 res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
605             break;
606         case PTS_SRC_REFERENCE:
607             if (videoFrame)
608                 res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
609             break;
610         case PTS_SRC_WALLCLOCK:
611             /* fall through */
612         case PTS_SRC_ABS_WALLCLOCK:
613         {
614             /* MSVC does not support compound literals like AV_TIME_BASE_Q
615              * in C++ code (compiler error C4576) */
616             AVRational timebase;
617             timebase.num = 1;
618             timebase.den = AV_TIME_BASE;
619             if (pts_src == PTS_SRC_WALLCLOCK)
620                 pts = av_rescale_q(wallclock, timebase, time_base);
621             else
622                 pts = av_rescale_q(abs_wallclock, timebase, time_base);
623             break;
624         }
625     }
626     if (res == S_OK)
627         pts = bmd_pts / time_base.num;
628
629     if (!copyts) {
630         if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
631             *initial_pts = pts;
632         if (*initial_pts != AV_NOPTS_VALUE)
633             pts -= *initial_pts;
634     }
635
636     return pts;
637 }
638
639 HRESULT decklink_input_callback::VideoInputFrameArrived(
640     IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
641 {
642     void *frameBytes;
643     void *audioFrameBytes;
644     BMDTimeValue frameTime;
645     BMDTimeValue frameDuration;
646     int64_t wallclock = 0, abs_wallclock = 0;
647     struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
648
649     if (ctx->autodetect) {
650         if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
651             ctx->bmd_mode == bmdModeUnknown)
652         {
653             ctx->bmd_mode = AUTODETECT_DEFAULT_MODE;
654         }
655         return S_OK;
656     }
657
658     ctx->frameCount++;
659     if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
660         wallclock = av_gettime_relative();
661     if (ctx->audio_pts_source == PTS_SRC_ABS_WALLCLOCK || ctx->video_pts_source == PTS_SRC_ABS_WALLCLOCK)
662         abs_wallclock = av_gettime();
663
664     // Handle Video Frame
665     if (videoFrame) {
666         AVPacket pkt;
667         av_init_packet(&pkt);
668         if (ctx->frameCount % 25 == 0) {
669             unsigned long long qsize = avpacket_queue_size(&ctx->queue);
670             av_log(avctx, AV_LOG_DEBUG,
671                     "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
672                     ctx->frameCount,
673                     videoFrame->GetRowBytes() * videoFrame->GetHeight(),
674                     (double)qsize / 1024 / 1024);
675         }
676
677         videoFrame->GetBytes(&frameBytes);
678         videoFrame->GetStreamTime(&frameTime, &frameDuration,
679                                   ctx->video_st->time_base.den);
680
681         if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
682             if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
683                 unsigned bars[8] = {
684                     0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
685                     0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
686                 int width  = videoFrame->GetWidth();
687                 int height = videoFrame->GetHeight();
688                 unsigned *p = (unsigned *)frameBytes;
689
690                 for (int y = 0; y < height; y++) {
691                     for (int x = 0; x < width; x += 2)
692                         *p++ = bars[(x * 8) / width];
693                 }
694             }
695
696             if (!no_video) {
697                 av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
698                         "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
699             }
700             no_video = 1;
701         } else {
702             if (no_video) {
703                 av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
704                         "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
705             }
706             no_video = 0;
707         }
708
709         pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
710         pkt.dts = pkt.pts;
711
712         pkt.duration = frameDuration;
713         //To be made sure it still applies
714         pkt.flags       |= AV_PKT_FLAG_KEY;
715         pkt.stream_index = ctx->video_st->index;
716         pkt.data         = (uint8_t *)frameBytes;
717         pkt.size         = videoFrame->GetRowBytes() *
718                            videoFrame->GetHeight();
719         //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
720
721         if (!no_video) {
722             IDeckLinkVideoFrameAncillary *vanc;
723             AVPacket txt_pkt;
724             uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
725             uint8_t *txt_buf = txt_buf0;
726
727             if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
728                 int i;
729                 int64_t line_mask = 1;
730                 BMDPixelFormat vanc_format = vanc->GetPixelFormat();
731                 txt_buf[0] = 0x10;    // data_identifier - EBU_data
732                 txt_buf++;
733 #if CONFIG_LIBZVBI
734                 if (ctx->bmd_mode == bmdModePAL && ctx->teletext_lines &&
735                     (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
736                     av_assert0(videoFrame->GetWidth() == 720);
737                     for (i = 6; i < 336; i++, line_mask <<= 1) {
738                         uint8_t *buf;
739                         if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
740                             if (vanc_format == bmdFormat8BitYUV)
741                                 txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
742                             else
743                                 txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
744                         }
745                         if (i == 22)
746                             i = 317;
747                     }
748                 }
749 #endif
750                 if (vanc_format == bmdFormat10BitYUV && videoFrame->GetWidth() <= MAX_WIDTH_VANC) {
751                     int idx = get_vanc_line_idx(ctx->bmd_mode);
752                     for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) {
753                         uint8_t *buf;
754                         if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
755                             uint16_t luma_vanc[MAX_WIDTH_VANC];
756                             extract_luma_from_v210(luma_vanc, buf, videoFrame->GetWidth());
757                             txt_buf = get_metadata(avctx, luma_vanc, videoFrame->GetWidth(),
758                                                    txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt);
759                         }
760                         if (i == vanc_line_numbers[idx].field0_vanc_end)
761                             i = vanc_line_numbers[idx].field1_vanc_start - 1;
762                     }
763                 }
764                 vanc->Release();
765                 if (txt_buf - txt_buf0 > 1) {
766                     int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
767                     while (stuffing_units--) {
768                         memset(txt_buf, 0xff, 46);
769                         txt_buf[1] = 0x2c; // data_unit_length
770                         txt_buf += 46;
771                     }
772                     av_init_packet(&txt_pkt);
773                     txt_pkt.pts = pkt.pts;
774                     txt_pkt.dts = pkt.dts;
775                     txt_pkt.stream_index = ctx->teletext_st->index;
776                     txt_pkt.data = txt_buf0;
777                     txt_pkt.size = txt_buf - txt_buf0;
778                     if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
779                         ++ctx->dropped;
780                     }
781                 }
782             }
783         }
784
785         if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
786             ++ctx->dropped;
787         }
788     }
789
790     // Handle Audio Frame
791     if (audioFrame) {
792         AVPacket pkt;
793         BMDTimeValue audio_pts;
794         av_init_packet(&pkt);
795
796         //hack among hacks
797         pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
798         audioFrame->GetBytes(&audioFrameBytes);
799         audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
800         pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
801         pkt.dts = pkt.pts;
802
803         //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
804         pkt.flags       |= AV_PKT_FLAG_KEY;
805         pkt.stream_index = ctx->audio_st->index;
806         pkt.data         = (uint8_t *)audioFrameBytes;
807
808         if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
809             ++ctx->dropped;
810         }
811     }
812
813     return S_OK;
814 }
815
816 HRESULT decklink_input_callback::VideoInputFormatChanged(
817     BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
818     BMDDetectedVideoInputFormatFlags)
819 {
820     ctx->bmd_mode = mode->GetDisplayMode();
821     return S_OK;
822 }
823
824 static int decklink_autodetect(struct decklink_cctx *cctx) {
825     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
826     DECKLINK_BOOL autodetect_supported = false;
827     int i;
828
829     if (ctx->attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &autodetect_supported) != S_OK)
830         return -1;
831     if (autodetect_supported == false)
832         return -1;
833
834     ctx->autodetect = 1;
835     ctx->bmd_mode  = bmdModeUnknown;
836     if (ctx->dli->EnableVideoInput(AUTODETECT_DEFAULT_MODE,
837                                    bmdFormat8BitYUV,
838                                    bmdVideoInputEnableFormatDetection) != S_OK) {
839         return -1;
840     }
841
842     if (ctx->dli->StartStreams() != S_OK) {
843         return -1;
844     }
845
846     // 1 second timeout
847     for (i = 0; i < 10; i++) {
848         av_usleep(100000);
849         /* Sometimes VideoInputFrameArrived is called without the
850          * bmdFrameHasNoInputSource flag before VideoInputFormatChanged.
851          * So don't break for bmd_mode == AUTODETECT_DEFAULT_MODE. */
852         if (ctx->bmd_mode != bmdModeUnknown &&
853             ctx->bmd_mode != AUTODETECT_DEFAULT_MODE)
854             break;
855     }
856
857     ctx->dli->PauseStreams();
858     ctx->dli->FlushStreams();
859     ctx->autodetect = 0;
860     if (ctx->bmd_mode != bmdModeUnknown) {
861         cctx->format_code = (char *)av_mallocz(5);
862         if (!cctx->format_code)
863             return -1;
864         AV_WB32(cctx->format_code, ctx->bmd_mode);
865         return 0;
866     } else {
867         return -1;
868     }
869
870 }
871
872 extern "C" {
873
874 av_cold int ff_decklink_read_close(AVFormatContext *avctx)
875 {
876     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
877     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
878
879     if (ctx->capture_started) {
880         ctx->dli->StopStreams();
881         ctx->dli->DisableVideoInput();
882         ctx->dli->DisableAudioInput();
883     }
884
885     ff_decklink_cleanup(avctx);
886     avpacket_queue_end(&ctx->queue);
887
888     av_freep(&cctx->ctx);
889
890     return 0;
891 }
892
893 av_cold int ff_decklink_read_header(AVFormatContext *avctx)
894 {
895     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
896     struct decklink_ctx *ctx;
897     AVStream *st;
898     HRESULT result;
899     char fname[1024];
900     char *tmp;
901     int mode_num = 0;
902     int ret;
903
904     ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
905     if (!ctx)
906         return AVERROR(ENOMEM);
907     ctx->list_devices = cctx->list_devices;
908     ctx->list_formats = cctx->list_formats;
909     ctx->teletext_lines = cctx->teletext_lines;
910     ctx->preroll      = cctx->preroll;
911     ctx->duplex_mode  = cctx->duplex_mode;
912     if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
913         ctx->video_input = decklink_video_connection_map[cctx->video_input];
914     if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
915         ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
916     ctx->audio_pts_source = cctx->audio_pts_source;
917     ctx->video_pts_source = cctx->video_pts_source;
918     ctx->draw_bars = cctx->draw_bars;
919     ctx->audio_depth = cctx->audio_depth;
920     cctx->ctx = ctx;
921
922     /* Check audio channel option for valid values: 2, 8 or 16 */
923     switch (cctx->audio_channels) {
924         case 2:
925         case 8:
926         case 16:
927             break;
928         default:
929             av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
930             return AVERROR(EINVAL);
931     }
932
933     /* Check audio bit depth option for valid values: 16 or 32 */
934     switch (cctx->audio_depth) {
935         case 16:
936         case 32:
937             break;
938         default:
939             av_log(avctx, AV_LOG_ERROR, "Value for audio bit depth option must be either 16 or 32\n");
940             return AVERROR(EINVAL);
941     }
942
943     /* List available devices. */
944     if (ctx->list_devices) {
945         ff_decklink_list_devices_legacy(avctx, 1, 0);
946         return AVERROR_EXIT;
947     }
948
949     if (cctx->v210) {
950         av_log(avctx, AV_LOG_WARNING, "The bm_v210 option is deprecated and will be removed. Please use the -raw_format yuv422p10.\n");
951         cctx->raw_format = MKBETAG('v','2','1','0');
952     }
953
954     av_strlcpy(fname, avctx->url, sizeof(fname));
955     tmp=strchr (fname, '@');
956     if (tmp != NULL) {
957         av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
958         mode_num = atoi (tmp+1);
959         *tmp = 0;
960     }
961
962     ret = ff_decklink_init_device(avctx, fname);
963     if (ret < 0)
964         return ret;
965
966     /* Get input device. */
967     if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
968         av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
969                avctx->url);
970         ret = AVERROR(EIO);
971         goto error;
972     }
973
974     /* List supported formats. */
975     if (ctx->list_formats) {
976         ff_decklink_list_formats(avctx, DIRECTION_IN);
977         ret = AVERROR_EXIT;
978         goto error;
979     }
980
981     if (ff_decklink_set_configs(avctx, DIRECTION_IN) < 0) {
982         av_log(avctx, AV_LOG_ERROR, "Could not set input configuration\n");
983         ret = AVERROR(EIO);
984         goto error;
985     }
986
987     ctx->input_callback = new decklink_input_callback(avctx);
988     ctx->dli->SetCallback(ctx->input_callback);
989
990     if (mode_num == 0 && !cctx->format_code) {
991         if (decklink_autodetect(cctx) < 0) {
992             av_log(avctx, AV_LOG_ERROR, "Cannot Autodetect input stream or No signal\n");
993             ret = AVERROR(EIO);
994             goto error;
995         }
996         av_log(avctx, AV_LOG_INFO, "Autodetected the input mode\n");
997     }
998     if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
999         av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
1000             mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
1001         ret = AVERROR(EIO);
1002         goto error;
1003     }
1004
1005 #if !CONFIG_LIBZVBI
1006     if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
1007         av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
1008         ret = AVERROR(ENOSYS);
1009         goto error;
1010     }
1011 #endif
1012
1013     /* Setup streams. */
1014     st = avformat_new_stream(avctx, NULL);
1015     if (!st) {
1016         av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1017         ret = AVERROR(ENOMEM);
1018         goto error;
1019     }
1020     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
1021     st->codecpar->codec_id    = cctx->audio_depth == 32 ? AV_CODEC_ID_PCM_S32LE : AV_CODEC_ID_PCM_S16LE;
1022     st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
1023     st->codecpar->channels    = cctx->audio_channels;
1024     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
1025     ctx->audio_st=st;
1026
1027     st = avformat_new_stream(avctx, NULL);
1028     if (!st) {
1029         av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1030         ret = AVERROR(ENOMEM);
1031         goto error;
1032     }
1033     st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
1034     st->codecpar->width       = ctx->bmd_width;
1035     st->codecpar->height      = ctx->bmd_height;
1036
1037     st->time_base.den      = ctx->bmd_tb_den;
1038     st->time_base.num      = ctx->bmd_tb_num;
1039     st->r_frame_rate       = av_make_q(st->time_base.den, st->time_base.num);
1040
1041     switch((BMDPixelFormat)cctx->raw_format) {
1042     case bmdFormat8BitYUV:
1043         st->codecpar->codec_id    = AV_CODEC_ID_RAWVIDEO;
1044         st->codecpar->codec_tag   = MKTAG('U', 'Y', 'V', 'Y');
1045         st->codecpar->format      = AV_PIX_FMT_UYVY422;
1046         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
1047         break;
1048     case bmdFormat10BitYUV:
1049         st->codecpar->codec_id    = AV_CODEC_ID_V210;
1050         st->codecpar->codec_tag   = MKTAG('V','2','1','0');
1051         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
1052         st->codecpar->bits_per_coded_sample = 10;
1053         break;
1054     case bmdFormat8BitARGB:
1055         st->codecpar->codec_id    = AV_CODEC_ID_RAWVIDEO;
1056         st->codecpar->codec_tag   = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);;
1057         st->codecpar->format      = AV_PIX_FMT_0RGB;
1058         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1059         break;
1060     case bmdFormat8BitBGRA:
1061         st->codecpar->codec_id    = AV_CODEC_ID_RAWVIDEO;
1062         st->codecpar->codec_tag   = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
1063         st->codecpar->format      = AV_PIX_FMT_BGR0;
1064         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1065         break;
1066     case bmdFormat10BitRGB:
1067         st->codecpar->codec_id    = AV_CODEC_ID_R210;
1068         st->codecpar->codec_tag   = MKTAG('R','2','1','0');
1069         st->codecpar->format      = AV_PIX_FMT_RGB48LE;
1070         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 30, st->time_base.den, st->time_base.num);
1071         st->codecpar->bits_per_coded_sample = 10;
1072         break;
1073     default:
1074         av_log(avctx, AV_LOG_ERROR, "Raw Format %.4s not supported\n", (char*) &cctx->raw_format);
1075         ret = AVERROR(EINVAL);
1076         goto error;
1077     }
1078
1079     switch (ctx->bmd_field_dominance) {
1080     case bmdUpperFieldFirst:
1081         st->codecpar->field_order = AV_FIELD_TT;
1082         break;
1083     case bmdLowerFieldFirst:
1084         st->codecpar->field_order = AV_FIELD_BB;
1085         break;
1086     case bmdProgressiveFrame:
1087     case bmdProgressiveSegmentedFrame:
1088         st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
1089         break;
1090     }
1091
1092     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
1093
1094     ctx->video_st=st;
1095
1096     if (ctx->teletext_lines) {
1097         st = avformat_new_stream(avctx, NULL);
1098         if (!st) {
1099             av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1100             ret = AVERROR(ENOMEM);
1101             goto error;
1102         }
1103         st->codecpar->codec_type  = AVMEDIA_TYPE_SUBTITLE;
1104         st->time_base.den         = ctx->bmd_tb_den;
1105         st->time_base.num         = ctx->bmd_tb_num;
1106         st->codecpar->codec_id    = AV_CODEC_ID_DVB_TELETEXT;
1107         avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
1108         ctx->teletext_st = st;
1109     }
1110
1111     av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
1112     result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, cctx->audio_depth == 32 ? bmdAudioSampleType32bitInteger : bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
1113
1114     if (result != S_OK) {
1115         av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
1116         ret = AVERROR(EIO);
1117         goto error;
1118     }
1119
1120     result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
1121                                         (BMDPixelFormat) cctx->raw_format,
1122                                         bmdVideoInputFlagDefault);
1123
1124     if (result != S_OK) {
1125         av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
1126         ret = AVERROR(EIO);
1127         goto error;
1128     }
1129
1130     avpacket_queue_init (avctx, &ctx->queue);
1131
1132     if (ctx->dli->StartStreams() != S_OK) {
1133         av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
1134         ret = AVERROR(EIO);
1135         goto error;
1136     }
1137
1138     return 0;
1139
1140 error:
1141     ff_decklink_cleanup(avctx);
1142     return ret;
1143 }
1144
1145 int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
1146 {
1147     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
1148     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
1149
1150     avpacket_queue_get(&ctx->queue, pkt, 1);
1151
1152     return 0;
1153 }
1154
1155 int ff_decklink_list_input_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list)
1156 {
1157     return ff_decklink_list_devices(avctx, device_list, 1, 0);
1158 }
1159
1160 } /* extern "C" */