FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
decklink_dec.cpp
Go to the documentation of this file.
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 <atomic>
25 using std::atomic;
26 
27 /* Include internal.h first to avoid conflict between winsock.h (used by
28  * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
29 extern "C" {
30 #include "libavformat/internal.h"
31 }
32 
33 #include <DeckLinkAPI.h>
34 
35 extern "C" {
36 #include "config.h"
37 #include "libavformat/avformat.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/avutil.h"
40 #include "libavutil/common.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/time.h"
44 #include "libavutil/mathematics.h"
45 #include "libavutil/reverse.h"
46 #include "avdevice.h"
47 #if CONFIG_LIBZVBI
48 #include <libzvbi.h>
49 #endif
50 }
51 
52 #include "decklink_common.h"
53 #include "decklink_dec.h"
54 
55 #define MAX_WIDTH_VANC 1920
56 const BMDDisplayMode AUTODETECT_DEFAULT_MODE = bmdModeNTSC;
57 
58 typedef struct VANCLineNumber {
59  BMDDisplayMode mode;
63  int vanc_end;
65 
66 /* These VANC line numbers need not be very accurate. In any case
67  * GetBufferForVerticalBlankingLine() will return an error when invalid
68  * ancillary line number was requested. We just need to make sure that the
69  * entire VANC region is covered, while making sure we don't decode VANC of
70  * another source during switching*/
72  /* SD Modes */
73 
74  {bmdModeNTSC, 11, 19, 274, 282},
75  {bmdModeNTSC2398, 11, 19, 274, 282},
76  {bmdModePAL, 7, 22, 320, 335},
77  {bmdModeNTSCp, 11, -1, -1, 39},
78  {bmdModePALp, 7, -1, -1, 45},
79 
80  /* HD 1080 Modes */
81 
82  {bmdModeHD1080p2398, 8, -1, -1, 42},
83  {bmdModeHD1080p24, 8, -1, -1, 42},
84  {bmdModeHD1080p25, 8, -1, -1, 42},
85  {bmdModeHD1080p2997, 8, -1, -1, 42},
86  {bmdModeHD1080p30, 8, -1, -1, 42},
87  {bmdModeHD1080i50, 8, 20, 570, 585},
88  {bmdModeHD1080i5994, 8, 20, 570, 585},
89  {bmdModeHD1080i6000, 8, 20, 570, 585},
90  {bmdModeHD1080p50, 8, -1, -1, 42},
91  {bmdModeHD1080p5994, 8, -1, -1, 42},
92  {bmdModeHD1080p6000, 8, -1, -1, 42},
93 
94  /* HD 720 Modes */
95 
96  {bmdModeHD720p50, 8, -1, -1, 26},
97  {bmdModeHD720p5994, 8, -1, -1, 26},
98  {bmdModeHD720p60, 8, -1, -1, 26},
99 
100  /* For all other modes, for which we don't support VANC */
101  {bmdModeUnknown, 0, -1, -1, -1}
102 };
103 
104 class decklink_allocator : public IDeckLinkMemoryAllocator
105 {
106 public:
108  virtual ~decklink_allocator() { }
109 
110  // IDeckLinkMemoryAllocator methods
111  virtual HRESULT STDMETHODCALLTYPE AllocateBuffer(unsigned int bufferSize, void* *allocatedBuffer)
112  {
113  void *buf = av_malloc(bufferSize + AV_INPUT_BUFFER_PADDING_SIZE);
114  if (!buf)
115  return E_OUTOFMEMORY;
116  *allocatedBuffer = buf;
117  return S_OK;
118  }
119  virtual HRESULT STDMETHODCALLTYPE ReleaseBuffer(void* buffer)
120  {
121  av_free(buffer);
122  return S_OK;
123  }
124  virtual HRESULT STDMETHODCALLTYPE Commit() { return S_OK; }
125  virtual HRESULT STDMETHODCALLTYPE Decommit() { return S_OK; }
126 
127  // IUnknown methods
128  virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
129  virtual ULONG STDMETHODCALLTYPE AddRef(void) { return ++_refs; }
130  virtual ULONG STDMETHODCALLTYPE Release(void)
131  {
132  int ret = --_refs;
133  if (!ret)
134  delete this;
135  return ret;
136  }
137 
138 private:
139  std::atomic<int> _refs;
140 };
141 
142 extern "C" {
143 static void decklink_object_free(void *opaque, uint8_t *data)
144 {
145  IUnknown *obj = (class IUnknown *)opaque;
146  obj->Release();
147 }
148 }
149 
150 static int get_vanc_line_idx(BMDDisplayMode mode)
151 {
152  unsigned int i;
153  for (i = 0; i < FF_ARRAY_ELEMS(vanc_line_numbers); i++) {
154  if (mode == vanc_line_numbers[i].mode)
155  return i;
156  }
157  /* Return the VANC idx for Unknown mode */
158  return i - 1;
159 }
160 
161 static inline void clear_parity_bits(uint16_t *buf, int len) {
162  int i;
163  for (i = 0; i < len; i++)
164  buf[i] &= 0xff;
165 }
166 
167 static int check_vanc_parity_checksum(uint16_t *buf, int len, uint16_t checksum) {
168  int i;
169  uint16_t vanc_sum = 0;
170  for (i = 3; i < len - 1; i++) {
171  uint16_t v = buf[i];
172  int np = v >> 8;
173  int p = av_parity(v & 0xff);
174  if ((!!p ^ !!(v & 0x100)) || (np != 1 && np != 2)) {
175  // Parity check failed
176  return -1;
177  }
178  vanc_sum += v;
179  }
180  vanc_sum &= 0x1ff;
181  vanc_sum |= ((~vanc_sum & 0x100) << 1);
182  if (checksum != vanc_sum) {
183  // Checksum verification failed
184  return -1;
185  }
186  return 0;
187 }
188 
189 /* The 10-bit VANC data is packed in V210, we only need the luma component. */
190 static void extract_luma_from_v210(uint16_t *dst, const uint8_t *src, int width)
191 {
192  int i;
193  for (i = 0; i < width / 3; i++) {
194  *dst++ = (src[1] >> 2) + ((src[2] & 15) << 6);
195  *dst++ = src[4] + ((src[5] & 3) << 8);
196  *dst++ = (src[6] >> 4) + ((src[7] & 63) << 4);
197  src += 8;
198  }
199 }
200 
201 static void unpack_v210(uint16_t *dst, const uint8_t *src, int width)
202 {
203  int i;
204  for (i = 0; i < width * 2 / 3; i++) {
205  *dst++ = src[0] + ((src[1] & 3) << 8);
206  *dst++ = (src[1] >> 2) + ((src[2] & 15) << 6);
207  *dst++ = (src[2] >> 4) + ((src[3] & 63) << 4);
208  src += 4;
209  }
210 }
211 
213 {
214  uint8_t ret = (line < 313) << 5;
215  if (line >= 7 && line <= 22)
216  ret += line;
217  if (line >= 320 && line <= 335)
218  ret += (line - 313);
219  return ret;
220 }
221 
222 static void fill_data_unit_head(int line, uint8_t *tgt)
223 {
224  tgt[0] = 0x02; // data_unit_id
225  tgt[1] = 0x2c; // data_unit_length
226  tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
227  tgt[3] = 0xe4; // framing code
228 }
229 
230 #if CONFIG_LIBZVBI
231 static uint8_t* teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt, vbi_pixfmt fmt)
232 {
233  vbi_bit_slicer slicer;
234 
235  vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, fmt);
236 
237  if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
238  return tgt;
239 
240  fill_data_unit_head(line, tgt);
241 
242  return tgt + 46;
243 }
244 
245 static uint8_t* teletext_data_unit_from_vbi_data_10bit(int line, uint8_t *src, uint8_t *tgt)
246 {
247  uint8_t y[720];
248  uint8_t *py = y;
249  uint8_t *pend = y + 720;
250  /* The 10-bit VBI data is packed in V210, but libzvbi only supports 8-bit,
251  * so we extract the 8 MSBs of the luma component, that is enough for
252  * teletext bit slicing. */
253  while (py < pend) {
254  *py++ = (src[1] >> 4) + ((src[2] & 15) << 4);
255  *py++ = (src[4] >> 2) + ((src[5] & 3 ) << 6);
256  *py++ = (src[6] >> 6) + ((src[7] & 63) << 2);
257  src += 8;
258  }
259  return teletext_data_unit_from_vbi_data(line, y, tgt, VBI_PIXFMT_YUV420);
260 }
261 #endif
262 
263 static uint8_t* teletext_data_unit_from_op47_vbi_packet(int line, uint16_t *py, uint8_t *tgt)
264 {
265  int i;
266 
267  if (py[0] != 0x255 || py[1] != 0x255 || py[2] != 0x227)
268  return tgt;
269 
270  fill_data_unit_head(line, tgt);
271 
272  py += 3;
273  tgt += 4;
274 
275  for (i = 0; i < 42; i++)
276  *tgt++ = ff_reverse[py[i] & 255];
277 
278  return tgt;
279 }
280 
281 static int linemask_matches(int line, int64_t mask)
282 {
283  int shift = -1;
284  if (line >= 6 && line <= 22)
285  shift = line - 6;
286  if (line >= 318 && line <= 335)
287  shift = line - 318 + 17;
288  return shift >= 0 && ((1ULL << shift) & mask);
289 }
290 
291 static uint8_t* teletext_data_unit_from_op47_data(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines)
292 {
293  if (py < pend - 9) {
294  if (py[0] == 0x151 && py[1] == 0x115 && py[3] == 0x102) { // identifier, identifier, format code for WST teletext
295  uint16_t *descriptors = py + 4;
296  int i;
297  py += 9;
298  for (i = 0; i < 5 && py < pend - 45; i++, py += 45) {
299  int line = (descriptors[i] & 31) + (!(descriptors[i] & 128)) * 313;
300  if (line && linemask_matches(line, wanted_lines))
301  tgt = teletext_data_unit_from_op47_vbi_packet(line, py, tgt);
302  }
303  }
304  }
305  return tgt;
306 }
307 
308 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)
309 {
310  uint16_t did = py[0]; // data id
311  uint16_t sdid = py[1]; // secondary data id
312  uint16_t dc = py[2] & 255; // data count
313  py += 3;
314  pend = FFMIN(pend, py + dc);
315  if (did == 0x143 && sdid == 0x102) { // subtitle distribution packet
316  tgt = teletext_data_unit_from_op47_data(py, pend, tgt, wanted_lines);
317  } else if (allow_multipacket && did == 0x143 && sdid == 0x203) { // VANC multipacket
318  py += 2; // priority, line/field
319  while (py < pend - 3) {
320  tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
321  py += 4 + (py[2] & 255); // ndid, nsdid, ndc, line/field
322  }
323  }
324  return tgt;
325 }
326 
327 static uint8_t *vanc_to_cc(AVFormatContext *avctx, uint16_t *buf, size_t words,
328  unsigned &cc_count)
329 {
330  size_t i, len = (buf[5] & 0xff) + 6 + 1;
331  uint8_t cdp_sum, rate;
332  uint16_t hdr, ftr;
333  uint8_t *cc;
334  uint16_t *cdp = &buf[6]; // CDP follows
335  if (cdp[0] != 0x96 || cdp[1] != 0x69) {
336  av_log(avctx, AV_LOG_WARNING, "Invalid CDP header 0x%.2x 0x%.2x\n", cdp[0], cdp[1]);
337  return NULL;
338  }
339 
340  len -= 7; // remove VANC header and checksum
341 
342  if (cdp[2] != len) {
343  av_log(avctx, AV_LOG_WARNING, "CDP len %d != %zu\n", cdp[2], len);
344  return NULL;
345  }
346 
347  cdp_sum = 0;
348  for (i = 0; i < len - 1; i++)
349  cdp_sum += cdp[i];
350  cdp_sum = cdp_sum ? 256 - cdp_sum : 0;
351  if (cdp[len - 1] != cdp_sum) {
352  av_log(avctx, AV_LOG_WARNING, "CDP checksum invalid 0x%.4x != 0x%.4x\n", cdp_sum, cdp[len-1]);
353  return NULL;
354  }
355 
356  rate = cdp[3];
357  if (!(rate & 0x0f)) {
358  av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
359  return NULL;
360  }
361  rate >>= 4;
362  if (rate > 8) {
363  av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
364  return NULL;
365  }
366 
367  if (!(cdp[4] & 0x43)) /* ccdata_present | caption_service_active | reserved */ {
368  av_log(avctx, AV_LOG_WARNING, "CDP flags invalid (0x%.2x)\n", cdp[4]);
369  return NULL;
370  }
371 
372  hdr = (cdp[5] << 8) | cdp[6];
373  if (cdp[7] != 0x72) /* ccdata_id */ {
374  av_log(avctx, AV_LOG_WARNING, "Invalid ccdata_id 0x%.2x\n", cdp[7]);
375  return NULL;
376  }
377 
378  cc_count = cdp[8];
379  if (!(cc_count & 0xe0)) {
380  av_log(avctx, AV_LOG_WARNING, "Invalid cc_count 0x%.2x\n", cc_count);
381  return NULL;
382  }
383 
384  cc_count &= 0x1f;
385  if ((len - 13) < cc_count * 3) {
386  av_log(avctx, AV_LOG_WARNING, "Invalid cc_count %d (> %zu)\n", cc_count * 3, len - 13);
387  return NULL;
388  }
389 
390  if (cdp[len - 4] != 0x74) /* footer id */ {
391  av_log(avctx, AV_LOG_WARNING, "Invalid footer id 0x%.2x\n", cdp[len-4]);
392  return NULL;
393  }
394 
395  ftr = (cdp[len - 3] << 8) | cdp[len - 2];
396  if (ftr != hdr) {
397  av_log(avctx, AV_LOG_WARNING, "Header 0x%.4x != Footer 0x%.4x\n", hdr, ftr);
398  return NULL;
399  }
400 
401  cc = (uint8_t *)av_malloc(cc_count * 3);
402  if (cc == NULL) {
403  av_log(avctx, AV_LOG_WARNING, "CC - av_malloc failed for cc_count = %d\n", cc_count);
404  return NULL;
405  }
406 
407  for (size_t i = 0; i < cc_count; i++) {
408  cc[3*i + 0] = cdp[9 + 3*i+0] /* & 3 */;
409  cc[3*i + 1] = cdp[9 + 3*i+1];
410  cc[3*i + 2] = cdp[9 + 3*i+2];
411  }
412 
413  cc_count *= 3;
414  return cc;
415 }
416 
417 static uint8_t *get_metadata(AVFormatContext *avctx, uint16_t *buf, size_t width,
418  uint8_t *tgt, size_t tgt_size, AVPacket *pkt)
419 {
420  decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
421  uint16_t *max_buf = buf + width;
422 
423  while (buf < max_buf - 6) {
424  int len;
425  uint16_t did = buf[3] & 0xFF; // data id
426  uint16_t sdid = buf[4] & 0xFF; // secondary data id
427  /* Check for VANC header */
428  if (buf[0] != 0 || buf[1] != 0x3ff || buf[2] != 0x3ff) {
429  return tgt;
430  }
431 
432  len = (buf[5] & 0xff) + 6 + 1;
433  if (len > max_buf - buf) {
434  av_log(avctx, AV_LOG_WARNING, "Data Count (%d) > data left (%zu)\n",
435  len, max_buf - buf);
436  return tgt;
437  }
438 
439  if (did == 0x43 && (sdid == 0x02 || sdid == 0x03) && cctx->teletext_lines &&
440  width == 1920 && tgt_size >= 1920) {
441  if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
442  av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
443  goto skip_packet;
444  }
445  tgt = teletext_data_unit_from_ancillary_packet(buf + 3, buf + len, tgt, cctx->teletext_lines, 1);
446  } else if (did == 0x61 && sdid == 0x01) {
447  unsigned int data_len;
448  uint8_t *data;
449  if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
450  av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
451  goto skip_packet;
452  }
453  clear_parity_bits(buf, len);
454  data = vanc_to_cc(avctx, buf, width, data_len);
455  if (data) {
456  if (av_packet_add_side_data(pkt, AV_PKT_DATA_A53_CC, data, data_len) < 0)
457  av_free(data);
458  }
459  } else {
460  av_log(avctx, AV_LOG_DEBUG, "Unknown meta data DID = 0x%.2x SDID = 0x%.2x\n",
461  did, sdid);
462  }
463 skip_packet:
464  buf += len;
465  }
466 
467  return tgt;
468 }
469 
471 {
472  struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
473  memset(q, 0, sizeof(AVPacketQueue));
476  q->avctx = avctx;
477  q->max_q_size = ctx->queue_size;
478 }
479 
481 {
482  AVPacketList *pkt, *pkt1;
483 
485  for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
486  pkt1 = pkt->next;
487  av_packet_unref(&pkt->pkt);
488  av_freep(&pkt);
489  }
490  q->last_pkt = NULL;
491  q->first_pkt = NULL;
492  q->nb_packets = 0;
493  q->size = 0;
495 }
496 
498 {
502 }
503 
504 static unsigned long long avpacket_queue_size(AVPacketQueue *q)
505 {
506  unsigned long long size;
508  size = q->size;
510  return size;
511 }
512 
514 {
515  AVPacketList *pkt1;
516 
517  // Drop Packet if queue size is > maximum queue size
518  if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
519  av_packet_unref(pkt);
520  av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
521  return -1;
522  }
523  /* ensure the packet is reference counted */
524  if (av_packet_make_refcounted(pkt) < 0) {
525  av_packet_unref(pkt);
526  return -1;
527  }
528 
529  pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
530  if (!pkt1) {
531  av_packet_unref(pkt);
532  return -1;
533  }
534  av_packet_move_ref(&pkt1->pkt, pkt);
535  pkt1->next = NULL;
536 
538 
539  if (!q->last_pkt) {
540  q->first_pkt = pkt1;
541  } else {
542  q->last_pkt->next = pkt1;
543  }
544 
545  q->last_pkt = pkt1;
546  q->nb_packets++;
547  q->size += pkt1->pkt.size + sizeof(*pkt1);
548 
550 
552  return 0;
553 }
554 
556 {
557  AVPacketList *pkt1;
558  int ret;
559 
561 
562  for (;; ) {
563  pkt1 = q->first_pkt;
564  if (pkt1) {
565  q->first_pkt = pkt1->next;
566  if (!q->first_pkt) {
567  q->last_pkt = NULL;
568  }
569  q->nb_packets--;
570  q->size -= pkt1->pkt.size + sizeof(*pkt1);
571  *pkt = pkt1->pkt;
572  av_free(pkt1);
573  ret = 1;
574  break;
575  } else if (!block) {
576  ret = 0;
577  break;
578  } else {
579  pthread_cond_wait(&q->cond, &q->mutex);
580  }
581  }
583  return ret;
584 }
585 
586 class decklink_input_callback : public IDeckLinkInputCallback
587 {
588 public:
591 
592  virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
593  virtual ULONG STDMETHODCALLTYPE AddRef(void);
594  virtual ULONG STDMETHODCALLTYPE Release(void);
595  virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
596  virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
597 
598 private:
599  std::atomic<int> _refs;
602  int no_video;
605 };
606 
608 {
609  avctx = _avctx;
610  decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
611  ctx = (struct decklink_ctx *)cctx->ctx;
612  no_video = 0;
614 }
615 
617 {
618 }
619 
621 {
622  return ++_refs;
623 }
624 
626 {
627  int ret = --_refs;
628  if (!ret)
629  delete this;
630  return ret;
631 }
632 
633 static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
634  IDeckLinkAudioInputPacket *audioFrame,
635  int64_t wallclock,
636  int64_t abs_wallclock,
637  DecklinkPtsSource pts_src,
638  AVRational time_base, int64_t *initial_pts,
639  int copyts)
640 {
641  int64_t pts = AV_NOPTS_VALUE;
642  BMDTimeValue bmd_pts;
643  BMDTimeValue bmd_duration;
644  HRESULT res = E_INVALIDARG;
645  switch (pts_src) {
646  case PTS_SRC_AUDIO:
647  if (audioFrame)
648  res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
649  break;
650  case PTS_SRC_VIDEO:
651  if (videoFrame)
652  res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
653  break;
654  case PTS_SRC_REFERENCE:
655  if (videoFrame)
656  res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
657  break;
658  case PTS_SRC_WALLCLOCK:
659  /* fall through */
661  {
662  /* MSVC does not support compound literals like AV_TIME_BASE_Q
663  * in C++ code (compiler error C4576) */
664  AVRational timebase;
665  timebase.num = 1;
666  timebase.den = AV_TIME_BASE;
667  if (pts_src == PTS_SRC_WALLCLOCK)
668  pts = av_rescale_q(wallclock, timebase, time_base);
669  else
670  pts = av_rescale_q(abs_wallclock, timebase, time_base);
671  break;
672  }
673  }
674  if (res == S_OK)
675  pts = bmd_pts / time_base.num;
676 
677  if (!copyts) {
678  if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
679  *initial_pts = pts;
680  if (*initial_pts != AV_NOPTS_VALUE)
681  pts -= *initial_pts;
682  }
683 
684  return pts;
685 }
686 
688  IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
689 {
690  void *frameBytes;
691  void *audioFrameBytes;
692  BMDTimeValue frameTime;
693  BMDTimeValue frameDuration;
694  int64_t wallclock = 0, abs_wallclock = 0;
695  struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
696 
697  if (ctx->autodetect) {
698  if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
699  ctx->bmd_mode == bmdModeUnknown)
700  {
702  }
703  return S_OK;
704  }
705 
706  // Drop the frames till system's timestamp aligns with the configured value.
707  if (0 == ctx->frameCount && cctx->timestamp_align) {
708  AVRational remainder = av_make_q(av_gettime() % cctx->timestamp_align, 1000000);
709  AVRational frame_duration = av_inv_q(ctx->video_st->r_frame_rate);
710  if (av_cmp_q(remainder, frame_duration) > 0) {
711  ++ctx->dropped;
712  return S_OK;
713  }
714  }
715 
716  ctx->frameCount++;
718  wallclock = av_gettime_relative();
720  abs_wallclock = av_gettime();
721 
722  // Handle Video Frame
723  if (videoFrame) {
724  AVPacket pkt;
725  av_init_packet(&pkt);
726  if (ctx->frameCount % 25 == 0) {
727  unsigned long long qsize = avpacket_queue_size(&ctx->queue);
729  "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
730  ctx->frameCount,
731  videoFrame->GetRowBytes() * videoFrame->GetHeight(),
732  (double)qsize / 1024 / 1024);
733  }
734 
735  videoFrame->GetBytes(&frameBytes);
736  videoFrame->GetStreamTime(&frameTime, &frameDuration,
738 
739  if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
740  if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
741  unsigned bars[8] = {
742  0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
743  0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
744  int width = videoFrame->GetWidth();
745  int height = videoFrame->GetHeight();
746  unsigned *p = (unsigned *)frameBytes;
747 
748  for (int y = 0; y < height; y++) {
749  for (int x = 0; x < width; x += 2)
750  *p++ = bars[(x * 8) / width];
751  }
752  }
753 
754  if (!no_video) {
755  av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
756  "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
757  }
758  no_video = 1;
759  } else {
760  if (no_video) {
761  av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
762  "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
763  }
764  no_video = 0;
765 
766  // Handle Timecode (if requested)
767  if (ctx->tc_format) {
768  IDeckLinkTimecode *timecode;
769  if (videoFrame->GetTimecode(ctx->tc_format, &timecode) == S_OK) {
770  const char *tc = NULL;
771  DECKLINK_STR decklink_tc;
772  if (timecode->GetString(&decklink_tc) == S_OK) {
773  tc = DECKLINK_STRDUP(decklink_tc);
774  DECKLINK_FREE(decklink_tc);
775  }
776  timecode->Release();
777  if (tc) {
778  AVDictionary* metadata_dict = NULL;
779  int metadata_len;
780  uint8_t* packed_metadata;
781  if (av_dict_set(&metadata_dict, "timecode", tc, AV_DICT_DONT_STRDUP_VAL) >= 0) {
782  packed_metadata = av_packet_pack_dictionary(metadata_dict, &metadata_len);
783  av_dict_free(&metadata_dict);
784  if (packed_metadata) {
785  if (av_packet_add_side_data(&pkt, AV_PKT_DATA_STRINGS_METADATA, packed_metadata, metadata_len) < 0)
786  av_freep(&packed_metadata);
787  }
788  }
789  }
790  } else {
791  av_log(avctx, AV_LOG_DEBUG, "Unable to find timecode.\n");
792  }
793  }
794  }
795 
796  pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
797  pkt.dts = pkt.pts;
798 
799  pkt.duration = frameDuration;
800  //To be made sure it still applies
801  pkt.flags |= AV_PKT_FLAG_KEY;
802  pkt.stream_index = ctx->video_st->index;
803  pkt.data = (uint8_t *)frameBytes;
804  pkt.size = videoFrame->GetRowBytes() *
805  videoFrame->GetHeight();
806  //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
807 
808  if (!no_video) {
809  IDeckLinkVideoFrameAncillary *vanc;
810  AVPacket txt_pkt;
811  uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
812  uint8_t *txt_buf = txt_buf0;
813 
814  if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
815  int i;
816  int64_t line_mask = 1;
817  BMDPixelFormat vanc_format = vanc->GetPixelFormat();
818  txt_buf[0] = 0x10; // data_identifier - EBU_data
819  txt_buf++;
820 #if CONFIG_LIBZVBI
821  if (ctx->bmd_mode == bmdModePAL && ctx->teletext_lines &&
822  (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
823  av_assert0(videoFrame->GetWidth() == 720);
824  for (i = 6; i < 336; i++, line_mask <<= 1) {
825  uint8_t *buf;
826  if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
827  if (vanc_format == bmdFormat8BitYUV)
828  txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
829  else
830  txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
831  }
832  if (i == 22)
833  i = 317;
834  }
835  }
836 #endif
837  if (vanc_format == bmdFormat10BitYUV && videoFrame->GetWidth() <= MAX_WIDTH_VANC) {
838  int idx = get_vanc_line_idx(ctx->bmd_mode);
839  for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) {
840  uint8_t *buf;
841  if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
842  uint16_t vanc[MAX_WIDTH_VANC];
843  size_t vanc_size = videoFrame->GetWidth();
844  if (ctx->bmd_mode == bmdModeNTSC && videoFrame->GetWidth() * 2 <= MAX_WIDTH_VANC) {
845  vanc_size = vanc_size * 2;
846  unpack_v210(vanc, buf, videoFrame->GetWidth());
847  } else {
848  extract_luma_from_v210(vanc, buf, videoFrame->GetWidth());
849  }
850  txt_buf = get_metadata(avctx, vanc, vanc_size,
851  txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt);
852  }
853  if (i == vanc_line_numbers[idx].field0_vanc_end)
854  i = vanc_line_numbers[idx].field1_vanc_start - 1;
855  }
856  }
857  vanc->Release();
858  if (txt_buf - txt_buf0 > 1) {
859  int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
860  while (stuffing_units--) {
861  memset(txt_buf, 0xff, 46);
862  txt_buf[1] = 0x2c; // data_unit_length
863  txt_buf += 46;
864  }
865  av_init_packet(&txt_pkt);
866  txt_pkt.pts = pkt.pts;
867  txt_pkt.dts = pkt.dts;
868  txt_pkt.stream_index = ctx->teletext_st->index;
869  txt_pkt.data = txt_buf0;
870  txt_pkt.size = txt_buf - txt_buf0;
871  if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
872  ++ctx->dropped;
873  }
874  }
875  }
876  }
877 
878  pkt.buf = av_buffer_create(pkt.data, pkt.size, decklink_object_free, videoFrame, 0);
879  if (pkt.buf)
880  videoFrame->AddRef();
881 
882  if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
883  ++ctx->dropped;
884  }
885  }
886 
887  // Handle Audio Frame
888  if (audioFrame) {
889  AVPacket pkt;
890  BMDTimeValue audio_pts;
891  av_init_packet(&pkt);
892 
893  //hack among hacks
894  pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
895  audioFrame->GetBytes(&audioFrameBytes);
896  audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
897  pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
898  pkt.dts = pkt.pts;
899 
900  //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
901  pkt.flags |= AV_PKT_FLAG_KEY;
902  pkt.stream_index = ctx->audio_st->index;
903  pkt.data = (uint8_t *)audioFrameBytes;
904 
905  if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
906  ++ctx->dropped;
907  }
908  }
909 
910  return S_OK;
911 }
912 
914  BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
915  BMDDetectedVideoInputFormatFlags)
916 {
917  ctx->bmd_mode = mode->GetDisplayMode();
918  return S_OK;
919 }
920 
921 static int decklink_autodetect(struct decklink_cctx *cctx) {
922  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
923  DECKLINK_BOOL autodetect_supported = false;
924  int i;
925 
926  if (ctx->attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &autodetect_supported) != S_OK)
927  return -1;
928  if (autodetect_supported == false)
929  return -1;
930 
931  ctx->autodetect = 1;
932  ctx->bmd_mode = bmdModeUnknown;
933  if (ctx->dli->EnableVideoInput(AUTODETECT_DEFAULT_MODE,
934  bmdFormat8BitYUV,
935  bmdVideoInputEnableFormatDetection) != S_OK) {
936  return -1;
937  }
938 
939  if (ctx->dli->StartStreams() != S_OK) {
940  return -1;
941  }
942 
943  // 1 second timeout
944  for (i = 0; i < 10; i++) {
945  av_usleep(100000);
946  /* Sometimes VideoInputFrameArrived is called without the
947  * bmdFrameHasNoInputSource flag before VideoInputFormatChanged.
948  * So don't break for bmd_mode == AUTODETECT_DEFAULT_MODE. */
949  if (ctx->bmd_mode != bmdModeUnknown &&
951  break;
952  }
953 
954  ctx->dli->PauseStreams();
955  ctx->dli->FlushStreams();
956  ctx->autodetect = 0;
957  if (ctx->bmd_mode != bmdModeUnknown) {
958  cctx->format_code = (char *)av_mallocz(5);
959  if (!cctx->format_code)
960  return -1;
961  AV_WB32(cctx->format_code, ctx->bmd_mode);
962  return 0;
963  } else {
964  return -1;
965  }
966 
967 }
968 
969 extern "C" {
970 
972 {
973  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
974  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
975 
976  if (ctx->capture_started) {
977  ctx->dli->StopStreams();
978  ctx->dli->DisableVideoInput();
979  ctx->dli->DisableAudioInput();
980  }
981 
982  ff_decklink_cleanup(avctx);
983  avpacket_queue_end(&ctx->queue);
984 
985  av_freep(&cctx->ctx);
986 
987  return 0;
988 }
989 
991 {
992  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
993  struct decklink_ctx *ctx;
994  class decklink_allocator *allocator;
996  AVStream *st;
997  HRESULT result;
998  char fname[1024];
999  char *tmp;
1000  int mode_num = 0;
1001  int ret;
1002 
1003  ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
1004  if (!ctx)
1005  return AVERROR(ENOMEM);
1006  ctx->list_devices = cctx->list_devices;
1007  ctx->list_formats = cctx->list_formats;
1008  ctx->teletext_lines = cctx->teletext_lines;
1009  ctx->preroll = cctx->preroll;
1010  ctx->duplex_mode = cctx->duplex_mode;
1011  if (cctx->tc_format > 0 && (unsigned int)cctx->tc_format < FF_ARRAY_ELEMS(decklink_timecode_format_map))
1012  ctx->tc_format = decklink_timecode_format_map[cctx->tc_format];
1013  if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
1014  ctx->video_input = decklink_video_connection_map[cctx->video_input];
1015  if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
1016  ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
1017  ctx->audio_pts_source = cctx->audio_pts_source;
1018  ctx->video_pts_source = cctx->video_pts_source;
1019  ctx->draw_bars = cctx->draw_bars;
1020  ctx->audio_depth = cctx->audio_depth;
1021  cctx->ctx = ctx;
1022 
1023  /* Check audio channel option for valid values: 2, 8 or 16 */
1024  switch (cctx->audio_channels) {
1025  case 2:
1026  case 8:
1027  case 16:
1028  break;
1029  default:
1030  av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
1031  return AVERROR(EINVAL);
1032  }
1033 
1034  /* Check audio bit depth option for valid values: 16 or 32 */
1035  switch (cctx->audio_depth) {
1036  case 16:
1037  case 32:
1038  break;
1039  default:
1040  av_log(avctx, AV_LOG_ERROR, "Value for audio bit depth option must be either 16 or 32\n");
1041  return AVERROR(EINVAL);
1042  }
1043 
1044  /* List available devices. */
1045  if (ctx->list_devices) {
1046  ff_decklink_list_devices_legacy(avctx, 1, 0);
1047  return AVERROR_EXIT;
1048  }
1049 
1050  if (cctx->v210) {
1051  av_log(avctx, AV_LOG_WARNING, "The bm_v210 option is deprecated and will be removed. Please use the -raw_format yuv422p10.\n");
1052  cctx->raw_format = MKBETAG('v','2','1','0');
1053  }
1054 
1055  av_strlcpy(fname, avctx->url, sizeof(fname));
1056  tmp=strchr (fname, '@');
1057  if (tmp != NULL) {
1058  av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
1059  mode_num = atoi (tmp+1);
1060  *tmp = 0;
1061  }
1062 
1063  ret = ff_decklink_init_device(avctx, fname);
1064  if (ret < 0)
1065  return ret;
1066 
1067  /* Get input device. */
1068  if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
1069  av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
1070  avctx->url);
1071  ret = AVERROR(EIO);
1072  goto error;
1073  }
1074 
1075  /* List supported formats. */
1076  if (ctx->list_formats) {
1078  ret = AVERROR_EXIT;
1079  goto error;
1080  }
1081 
1082  if (ff_decklink_set_configs(avctx, DIRECTION_IN) < 0) {
1083  av_log(avctx, AV_LOG_ERROR, "Could not set input configuration\n");
1084  ret = AVERROR(EIO);
1085  goto error;
1086  }
1087 
1089  ret = (ctx->dli->SetCallback(input_callback) == S_OK ? 0 : AVERROR_EXTERNAL);
1090  input_callback->Release();
1091  if (ret < 0) {
1092  av_log(avctx, AV_LOG_ERROR, "Cannot set input callback\n");
1093  goto error;
1094  }
1095 
1096  allocator = new decklink_allocator();
1097  ret = (ctx->dli->SetVideoInputFrameMemoryAllocator(allocator) == S_OK ? 0 : AVERROR_EXTERNAL);
1098  allocator->Release();
1099  if (ret < 0) {
1100  av_log(avctx, AV_LOG_ERROR, "Cannot set custom memory allocator\n");
1101  goto error;
1102  }
1103 
1104  if (mode_num == 0 && !cctx->format_code) {
1105  if (decklink_autodetect(cctx) < 0) {
1106  av_log(avctx, AV_LOG_ERROR, "Cannot Autodetect input stream or No signal\n");
1107  ret = AVERROR(EIO);
1108  goto error;
1109  }
1110  av_log(avctx, AV_LOG_INFO, "Autodetected the input mode\n");
1111  }
1112  if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
1113  av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
1114  mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
1115  ret = AVERROR(EIO);
1116  goto error;
1117  }
1118 
1119 #if !CONFIG_LIBZVBI
1120  if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
1121  av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
1122  ret = AVERROR(ENOSYS);
1123  goto error;
1124  }
1125 #endif
1126 
1127  /* Setup streams. */
1128  st = avformat_new_stream(avctx, NULL);
1129  if (!st) {
1130  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1131  ret = AVERROR(ENOMEM);
1132  goto error;
1133  }
1134  st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1135  st->codecpar->codec_id = cctx->audio_depth == 32 ? AV_CODEC_ID_PCM_S32LE : AV_CODEC_ID_PCM_S16LE;
1136  st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
1137  st->codecpar->channels = cctx->audio_channels;
1138  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1139  ctx->audio_st=st;
1140 
1141  st = avformat_new_stream(avctx, NULL);
1142  if (!st) {
1143  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1144  ret = AVERROR(ENOMEM);
1145  goto error;
1146  }
1147  st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
1148  st->codecpar->width = ctx->bmd_width;
1149  st->codecpar->height = ctx->bmd_height;
1150 
1151  st->time_base.den = ctx->bmd_tb_den;
1152  st->time_base.num = ctx->bmd_tb_num;
1153  st->r_frame_rate = av_make_q(st->time_base.den, st->time_base.num);
1154 
1155  switch((BMDPixelFormat)cctx->raw_format) {
1156  case bmdFormat8BitYUV:
1157  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1158  st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
1159  st->codecpar->format = AV_PIX_FMT_UYVY422;
1160  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
1161  break;
1162  case bmdFormat10BitYUV:
1163  st->codecpar->codec_id = AV_CODEC_ID_V210;
1164  st->codecpar->codec_tag = MKTAG('V','2','1','0');
1165  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
1166  st->codecpar->bits_per_coded_sample = 10;
1167  break;
1168  case bmdFormat8BitARGB:
1169  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1170  st->codecpar->format = AV_PIX_FMT_0RGB;
1171  st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
1172  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1173  break;
1174  case bmdFormat8BitBGRA:
1175  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1176  st->codecpar->format = AV_PIX_FMT_BGR0;
1177  st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
1178  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1179  break;
1180  case bmdFormat10BitRGB:
1181  st->codecpar->codec_id = AV_CODEC_ID_R210;
1182  st->codecpar->codec_tag = MKTAG('R','2','1','0');
1183  st->codecpar->format = AV_PIX_FMT_RGB48LE;
1184  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 30, st->time_base.den, st->time_base.num);
1185  st->codecpar->bits_per_coded_sample = 10;
1186  break;
1187  default:
1188  av_log(avctx, AV_LOG_ERROR, "Raw Format %.4s not supported\n", (char*) &cctx->raw_format);
1189  ret = AVERROR(EINVAL);
1190  goto error;
1191  }
1192 
1193  switch (ctx->bmd_field_dominance) {
1194  case bmdUpperFieldFirst:
1195  st->codecpar->field_order = AV_FIELD_TT;
1196  break;
1197  case bmdLowerFieldFirst:
1198  st->codecpar->field_order = AV_FIELD_BB;
1199  break;
1200  case bmdProgressiveFrame:
1201  case bmdProgressiveSegmentedFrame:
1202  st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
1203  break;
1204  }
1205 
1206  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1207 
1208  ctx->video_st=st;
1209 
1210  if (ctx->teletext_lines) {
1211  st = avformat_new_stream(avctx, NULL);
1212  if (!st) {
1213  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1214  ret = AVERROR(ENOMEM);
1215  goto error;
1216  }
1217  st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
1218  st->time_base.den = ctx->bmd_tb_den;
1219  st->time_base.num = ctx->bmd_tb_num;
1220  st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
1221  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1222  ctx->teletext_st = st;
1223  }
1224 
1225  av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
1226  result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, cctx->audio_depth == 32 ? bmdAudioSampleType32bitInteger : bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
1227 
1228  if (result != S_OK) {
1229  av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
1230  ret = AVERROR(EIO);
1231  goto error;
1232  }
1233 
1234  result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
1235  (BMDPixelFormat) cctx->raw_format,
1236  bmdVideoInputFlagDefault);
1237 
1238  if (result != S_OK) {
1239  av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
1240  ret = AVERROR(EIO);
1241  goto error;
1242  }
1243 
1244  avpacket_queue_init (avctx, &ctx->queue);
1245 
1246  if (ctx->dli->StartStreams() != S_OK) {
1247  av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
1248  ret = AVERROR(EIO);
1249  goto error;
1250  }
1251 
1252  return 0;
1253 
1254 error:
1255  ff_decklink_cleanup(avctx);
1256  return ret;
1257 }
1258 
1260 {
1261  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
1262  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
1263 
1264  avpacket_queue_get(&ctx->queue, pkt, 1);
1265 
1266  if (ctx->tc_format && !(av_dict_get(ctx->video_st->metadata, "timecode", NULL, 0))) {
1267  int size;
1268  const uint8_t *side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
1269  if (side_metadata) {
1270  if (av_packet_unpack_dictionary(side_metadata, size, &ctx->video_st->metadata) < 0)
1271  av_log(avctx, AV_LOG_ERROR, "Unable to set timecode\n");
1272  }
1273  }
1274 
1275  return 0;
1276 }
1277 
1279 {
1280  return ff_decklink_list_devices(avctx, device_list, 1, 0);
1281 }
1282 
1283 } /* extern "C" */
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
#define NULL
Definition: coverity.c:32
static int shift(int a, int b)
Definition: sonic.c:82
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:108
#define S_OK
Definition: windows2linux.h:40
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:166
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
const char * fmt
Definition: avisynth_c.h:769
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4882
const uint8_t ff_reverse[256]
Definition: reverse.c:23
ATSC A53 Part 4 Closed Captions.
Definition: avcodec.h:1354
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
BMDDisplayMode mode
Convenience header that includes libavutil's core.
#define tc
Definition: regdef.h:69
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
static AVPacket pkt
pthread_cond_t cond
#define src
Definition: vp8dsp.c:254
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:140
Format I/O context.
Definition: avformat.h:1351
static int16_t block[64]
Definition: dct.c:115
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
Unpack a dictionary from side_data.
Definition: avpacket.c:523
pthread_mutex_t mutex
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:103
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt, or 0 if no associated fourCC code can be found.
Definition: raw.c:300
AVPacket pkt
Definition: avformat.h:2018
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1463
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
#define height
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint8_t * data
Definition: avcodec.h:1445
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:653
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:148
ptrdiff_t size
Definition: opengl_enc.c:101
#define E_NOINTERFACE
Definition: windows2linux.h:42
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
Main libavdevice API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
static const uint16_t mask[17]
Definition: lzw.c:38
AVPacketList * last_pkt
#define AVERROR(e)
Definition: error.h:43
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
#define FALSE
Definition: windows2linux.h:37
char * url
input or output URL.
Definition: avformat.h:1447
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1428
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
AVPacketList * first_pkt
unsigned long long size
#define width
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:661
AVFormatContext * ctx
Definition: movenc.c:48
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:100
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
static volatile int checksum
Definition: adler32.c:30
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:874
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
A list of zero terminated key/value strings.
Definition: avcodec.h:1284
void * LPVOID
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, int *size)
Pack a dictionary for use in side_data.
Definition: avpacket.c:488
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:200
void * buf
Definition: avisynth_c.h:690
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:295
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
int64_t max_q_size
AVFormatContext * avctx
List of devices.
Definition: avdevice.h:460
#define E_OUTOFMEMORY
Definition: windows2linux.h:45
static int64_t pts
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
DWORD HRESULT
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
#define av_parity
Definition: intmath.h:158
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
Main libavformat public API header.
struct AVPacketList * next
Definition: avformat.h:2019
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
uint32_t ULONG
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:129
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
Denominator.
Definition: rational.h:60
#define MKBETAG(a, b, c, d)
Definition: common.h:367
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
#define av_free(p)
int len
void * priv_data
Format private data.
Definition: avformat.h:1379
int channels
Audio only.
Definition: avcodec.h:4006
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
#define av_freep(p)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
#define MKTAG(a, b, c, d)
Definition: common.h:366
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:237
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:998
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1422
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
GLuint buffer
Definition: opengl_enc.c:102
static uint8_t tmp[11]
Definition: aes_ctr.c:26