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 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;
60  int vanc_end;
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*/
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 void unpack_v210(uint16_t *dst, const uint8_t *src, int width)
153 {
154  int i;
155  for (i = 0; i < width * 2 / 3; i++) {
156  *dst++ = src[0] + ((src[1] & 3) << 8);
157  *dst++ = (src[1] >> 2) + ((src[2] & 15) << 6);
158  *dst++ = (src[2] >> 4) + ((src[3] & 63) << 4);
159  src += 4;
160  }
161 }
162 
164 {
165  uint8_t ret = (line < 313) << 5;
166  if (line >= 7 && line <= 22)
167  ret += line;
168  if (line >= 320 && line <= 335)
169  ret += (line - 313);
170  return ret;
171 }
172 
173 static void fill_data_unit_head(int line, uint8_t *tgt)
174 {
175  tgt[0] = 0x02; // data_unit_id
176  tgt[1] = 0x2c; // data_unit_length
177  tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
178  tgt[3] = 0xe4; // framing code
179 }
180 
181 #if CONFIG_LIBZVBI
182 static uint8_t* teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt, vbi_pixfmt fmt)
183 {
184  vbi_bit_slicer slicer;
185 
186  vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, fmt);
187 
188  if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
189  return tgt;
190 
191  fill_data_unit_head(line, tgt);
192 
193  return tgt + 46;
194 }
195 
196 static uint8_t* teletext_data_unit_from_vbi_data_10bit(int line, uint8_t *src, uint8_t *tgt)
197 {
198  uint8_t y[720];
199  uint8_t *py = y;
200  uint8_t *pend = y + 720;
201  /* The 10-bit VBI data is packed in V210, but libzvbi only supports 8-bit,
202  * so we extract the 8 MSBs of the luma component, that is enough for
203  * teletext bit slicing. */
204  while (py < pend) {
205  *py++ = (src[1] >> 4) + ((src[2] & 15) << 4);
206  *py++ = (src[4] >> 2) + ((src[5] & 3 ) << 6);
207  *py++ = (src[6] >> 6) + ((src[7] & 63) << 2);
208  src += 8;
209  }
210  return teletext_data_unit_from_vbi_data(line, y, tgt, VBI_PIXFMT_YUV420);
211 }
212 #endif
213 
214 static uint8_t* teletext_data_unit_from_op47_vbi_packet(int line, uint16_t *py, uint8_t *tgt)
215 {
216  int i;
217 
218  if (py[0] != 0x255 || py[1] != 0x255 || py[2] != 0x227)
219  return tgt;
220 
221  fill_data_unit_head(line, tgt);
222 
223  py += 3;
224  tgt += 4;
225 
226  for (i = 0; i < 42; i++)
227  *tgt++ = ff_reverse[py[i] & 255];
228 
229  return tgt;
230 }
231 
232 static int linemask_matches(int line, int64_t mask)
233 {
234  int shift = -1;
235  if (line >= 6 && line <= 22)
236  shift = line - 6;
237  if (line >= 318 && line <= 335)
238  shift = line - 318 + 17;
239  return shift >= 0 && ((1ULL << shift) & mask);
240 }
241 
242 static uint8_t* teletext_data_unit_from_op47_data(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines)
243 {
244  if (py < pend - 9) {
245  if (py[0] == 0x151 && py[1] == 0x115 && py[3] == 0x102) { // identifier, identifier, format code for WST teletext
246  uint16_t *descriptors = py + 4;
247  int i;
248  py += 9;
249  for (i = 0; i < 5 && py < pend - 45; i++, py += 45) {
250  int line = (descriptors[i] & 31) + (!(descriptors[i] & 128)) * 313;
251  if (line && linemask_matches(line, wanted_lines))
252  tgt = teletext_data_unit_from_op47_vbi_packet(line, py, tgt);
253  }
254  }
255  }
256  return tgt;
257 }
258 
259 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)
260 {
261  uint16_t did = py[0]; // data id
262  uint16_t sdid = py[1]; // secondary data id
263  uint16_t dc = py[2] & 255; // data count
264  py += 3;
265  pend = FFMIN(pend, py + dc);
266  if (did == 0x143 && sdid == 0x102) { // subtitle distribution packet
267  tgt = teletext_data_unit_from_op47_data(py, pend, tgt, wanted_lines);
268  } else if (allow_multipacket && did == 0x143 && sdid == 0x203) { // VANC multipacket
269  py += 2; // priority, line/field
270  while (py < pend - 3) {
271  tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
272  py += 4 + (py[2] & 255); // ndid, nsdid, ndc, line/field
273  }
274  }
275  return tgt;
276 }
277 
278 static uint8_t *vanc_to_cc(AVFormatContext *avctx, uint16_t *buf, size_t words,
279  unsigned &cc_count)
280 {
281  size_t i, len = (buf[5] & 0xff) + 6 + 1;
282  uint8_t cdp_sum, rate;
283  uint16_t hdr, ftr;
284  uint8_t *cc;
285  uint16_t *cdp = &buf[6]; // CDP follows
286  if (cdp[0] != 0x96 || cdp[1] != 0x69) {
287  av_log(avctx, AV_LOG_WARNING, "Invalid CDP header 0x%.2x 0x%.2x\n", cdp[0], cdp[1]);
288  return NULL;
289  }
290 
291  len -= 7; // remove VANC header and checksum
292 
293  if (cdp[2] != len) {
294  av_log(avctx, AV_LOG_WARNING, "CDP len %d != %zu\n", cdp[2], len);
295  return NULL;
296  }
297 
298  cdp_sum = 0;
299  for (i = 0; i < len - 1; i++)
300  cdp_sum += cdp[i];
301  cdp_sum = cdp_sum ? 256 - cdp_sum : 0;
302  if (cdp[len - 1] != cdp_sum) {
303  av_log(avctx, AV_LOG_WARNING, "CDP checksum invalid 0x%.4x != 0x%.4x\n", cdp_sum, cdp[len-1]);
304  return NULL;
305  }
306 
307  rate = cdp[3];
308  if (!(rate & 0x0f)) {
309  av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
310  return NULL;
311  }
312  rate >>= 4;
313  if (rate > 8) {
314  av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
315  return NULL;
316  }
317 
318  if (!(cdp[4] & 0x43)) /* ccdata_present | caption_service_active | reserved */ {
319  av_log(avctx, AV_LOG_WARNING, "CDP flags invalid (0x%.2x)\n", cdp[4]);
320  return NULL;
321  }
322 
323  hdr = (cdp[5] << 8) | cdp[6];
324  if (cdp[7] != 0x72) /* ccdata_id */ {
325  av_log(avctx, AV_LOG_WARNING, "Invalid ccdata_id 0x%.2x\n", cdp[7]);
326  return NULL;
327  }
328 
329  cc_count = cdp[8];
330  if (!(cc_count & 0xe0)) {
331  av_log(avctx, AV_LOG_WARNING, "Invalid cc_count 0x%.2x\n", cc_count);
332  return NULL;
333  }
334 
335  cc_count &= 0x1f;
336  if ((len - 13) < cc_count * 3) {
337  av_log(avctx, AV_LOG_WARNING, "Invalid cc_count %d (> %zu)\n", cc_count * 3, len - 13);
338  return NULL;
339  }
340 
341  if (cdp[len - 4] != 0x74) /* footer id */ {
342  av_log(avctx, AV_LOG_WARNING, "Invalid footer id 0x%.2x\n", cdp[len-4]);
343  return NULL;
344  }
345 
346  ftr = (cdp[len - 3] << 8) | cdp[len - 2];
347  if (ftr != hdr) {
348  av_log(avctx, AV_LOG_WARNING, "Header 0x%.4x != Footer 0x%.4x\n", hdr, ftr);
349  return NULL;
350  }
351 
352  cc = (uint8_t *)av_malloc(cc_count * 3);
353  if (cc == NULL) {
354  av_log(avctx, AV_LOG_WARNING, "CC - av_malloc failed for cc_count = %d\n", cc_count);
355  return NULL;
356  }
357 
358  for (size_t i = 0; i < cc_count; i++) {
359  cc[3*i + 0] = cdp[9 + 3*i+0] /* & 3 */;
360  cc[3*i + 1] = cdp[9 + 3*i+1];
361  cc[3*i + 2] = cdp[9 + 3*i+2];
362  }
363 
364  cc_count *= 3;
365  return cc;
366 }
367 
368 static uint8_t *get_metadata(AVFormatContext *avctx, uint16_t *buf, size_t width,
369  uint8_t *tgt, size_t tgt_size, AVPacket *pkt)
370 {
371  decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
372  uint16_t *max_buf = buf + width;
373 
374  while (buf < max_buf - 6) {
375  int len;
376  uint16_t did = buf[3] & 0xFF; // data id
377  uint16_t sdid = buf[4] & 0xFF; // secondary data id
378  /* Check for VANC header */
379  if (buf[0] != 0 || buf[1] != 0x3ff || buf[2] != 0x3ff) {
380  return tgt;
381  }
382 
383  len = (buf[5] & 0xff) + 6 + 1;
384  if (len > max_buf - buf) {
385  av_log(avctx, AV_LOG_WARNING, "Data Count (%d) > data left (%zu)\n",
386  len, max_buf - buf);
387  return tgt;
388  }
389 
390  if (did == 0x43 && (sdid == 0x02 || sdid == 0x03) && cctx->teletext_lines &&
391  width == 1920 && tgt_size >= 1920) {
392  if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
393  av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
394  goto skip_packet;
395  }
396  tgt = teletext_data_unit_from_ancillary_packet(buf + 3, buf + len, tgt, cctx->teletext_lines, 1);
397  } else if (did == 0x61 && sdid == 0x01) {
398  unsigned int data_len;
399  uint8_t *data;
400  if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
401  av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
402  goto skip_packet;
403  }
404  clear_parity_bits(buf, len);
405  data = vanc_to_cc(avctx, buf, width, data_len);
406  if (data) {
407  if (av_packet_add_side_data(pkt, AV_PKT_DATA_A53_CC, data, data_len) < 0)
408  av_free(data);
409  }
410  } else {
411  av_log(avctx, AV_LOG_DEBUG, "Unknown meta data DID = 0x%.2x SDID = 0x%.2x\n",
412  did, sdid);
413  }
414 skip_packet:
415  buf += len;
416  }
417 
418  return tgt;
419 }
420 
422 {
423  struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
424  memset(q, 0, sizeof(AVPacketQueue));
427  q->avctx = avctx;
428  q->max_q_size = ctx->queue_size;
429 }
430 
432 {
433  AVPacketList *pkt, *pkt1;
434 
436  for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
437  pkt1 = pkt->next;
438  av_packet_unref(&pkt->pkt);
439  av_freep(&pkt);
440  }
441  q->last_pkt = NULL;
442  q->first_pkt = NULL;
443  q->nb_packets = 0;
444  q->size = 0;
446 }
447 
449 {
453 }
454 
455 static unsigned long long avpacket_queue_size(AVPacketQueue *q)
456 {
457  unsigned long long size;
459  size = q->size;
461  return size;
462 }
463 
465 {
466  AVPacketList *pkt1;
467 
468  // Drop Packet if queue size is > maximum queue size
469  if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
470  av_packet_unref(pkt);
471  av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
472  return -1;
473  }
474  /* ensure the packet is reference counted */
475  if (av_packet_make_refcounted(pkt) < 0) {
476  av_packet_unref(pkt);
477  return -1;
478  }
479 
480  pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
481  if (!pkt1) {
482  av_packet_unref(pkt);
483  return -1;
484  }
485  av_packet_move_ref(&pkt1->pkt, pkt);
486  pkt1->next = NULL;
487 
489 
490  if (!q->last_pkt) {
491  q->first_pkt = pkt1;
492  } else {
493  q->last_pkt->next = pkt1;
494  }
495 
496  q->last_pkt = pkt1;
497  q->nb_packets++;
498  q->size += pkt1->pkt.size + sizeof(*pkt1);
499 
501 
503  return 0;
504 }
505 
507 {
508  AVPacketList *pkt1;
509  int ret;
510 
512 
513  for (;; ) {
514  pkt1 = q->first_pkt;
515  if (pkt1) {
516  q->first_pkt = pkt1->next;
517  if (!q->first_pkt) {
518  q->last_pkt = NULL;
519  }
520  q->nb_packets--;
521  q->size -= pkt1->pkt.size + sizeof(*pkt1);
522  *pkt = pkt1->pkt;
523  av_free(pkt1);
524  ret = 1;
525  break;
526  } else if (!block) {
527  ret = 0;
528  break;
529  } else {
530  pthread_cond_wait(&q->cond, &q->mutex);
531  }
532  }
534  return ret;
535 }
536 
537 class decklink_input_callback : public IDeckLinkInputCallback
538 {
539 public:
542 
543  virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
544  virtual ULONG STDMETHODCALLTYPE AddRef(void);
545  virtual ULONG STDMETHODCALLTYPE Release(void);
546  virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
547  virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
548 
549 private:
554  int no_video;
557 };
558 
560 {
561  avctx = _avctx;
562  decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
563  ctx = (struct decklink_ctx *)cctx->ctx;
564  no_video = 0;
567 }
568 
570 {
572 }
573 
575 {
577  m_refCount++;
579 
580  return (ULONG)m_refCount;
581 }
582 
584 {
586  m_refCount--;
588 
589  if (m_refCount == 0) {
590  delete this;
591  return 0;
592  }
593 
594  return (ULONG)m_refCount;
595 }
596 
597 static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
598  IDeckLinkAudioInputPacket *audioFrame,
599  int64_t wallclock,
600  int64_t abs_wallclock,
601  DecklinkPtsSource pts_src,
602  AVRational time_base, int64_t *initial_pts,
603  int copyts)
604 {
605  int64_t pts = AV_NOPTS_VALUE;
606  BMDTimeValue bmd_pts;
607  BMDTimeValue bmd_duration;
608  HRESULT res = E_INVALIDARG;
609  switch (pts_src) {
610  case PTS_SRC_AUDIO:
611  if (audioFrame)
612  res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
613  break;
614  case PTS_SRC_VIDEO:
615  if (videoFrame)
616  res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
617  break;
618  case PTS_SRC_REFERENCE:
619  if (videoFrame)
620  res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
621  break;
622  case PTS_SRC_WALLCLOCK:
623  /* fall through */
625  {
626  /* MSVC does not support compound literals like AV_TIME_BASE_Q
627  * in C++ code (compiler error C4576) */
628  AVRational timebase;
629  timebase.num = 1;
630  timebase.den = AV_TIME_BASE;
631  if (pts_src == PTS_SRC_WALLCLOCK)
632  pts = av_rescale_q(wallclock, timebase, time_base);
633  else
634  pts = av_rescale_q(abs_wallclock, timebase, time_base);
635  break;
636  }
637  }
638  if (res == S_OK)
639  pts = bmd_pts / time_base.num;
640 
641  if (!copyts) {
642  if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
643  *initial_pts = pts;
644  if (*initial_pts != AV_NOPTS_VALUE)
645  pts -= *initial_pts;
646  }
647 
648  return pts;
649 }
650 
652  IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
653 {
654  void *frameBytes;
655  void *audioFrameBytes;
656  BMDTimeValue frameTime;
657  BMDTimeValue frameDuration;
658  int64_t wallclock = 0, abs_wallclock = 0;
659  struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
660 
661  if (ctx->autodetect) {
662  if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
663  ctx->bmd_mode == bmdModeUnknown)
664  {
666  }
667  return S_OK;
668  }
669 
670  ctx->frameCount++;
672  wallclock = av_gettime_relative();
674  abs_wallclock = av_gettime();
675 
676  // Handle Video Frame
677  if (videoFrame) {
678  AVPacket pkt;
679  av_init_packet(&pkt);
680  if (ctx->frameCount % 25 == 0) {
681  unsigned long long qsize = avpacket_queue_size(&ctx->queue);
683  "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
684  ctx->frameCount,
685  videoFrame->GetRowBytes() * videoFrame->GetHeight(),
686  (double)qsize / 1024 / 1024);
687  }
688 
689  videoFrame->GetBytes(&frameBytes);
690  videoFrame->GetStreamTime(&frameTime, &frameDuration,
692 
693  if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
694  if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
695  unsigned bars[8] = {
696  0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
697  0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
698  int width = videoFrame->GetWidth();
699  int height = videoFrame->GetHeight();
700  unsigned *p = (unsigned *)frameBytes;
701 
702  for (int y = 0; y < height; y++) {
703  for (int x = 0; x < width; x += 2)
704  *p++ = bars[(x * 8) / width];
705  }
706  }
707 
708  if (!no_video) {
709  av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
710  "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
711  }
712  no_video = 1;
713  } else {
714  if (no_video) {
715  av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
716  "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
717  }
718  no_video = 0;
719  }
720 
721  pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
722  pkt.dts = pkt.pts;
723 
724  pkt.duration = frameDuration;
725  //To be made sure it still applies
726  pkt.flags |= AV_PKT_FLAG_KEY;
727  pkt.stream_index = ctx->video_st->index;
728  pkt.data = (uint8_t *)frameBytes;
729  pkt.size = videoFrame->GetRowBytes() *
730  videoFrame->GetHeight();
731  //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
732 
733  if (!no_video) {
734  IDeckLinkVideoFrameAncillary *vanc;
735  AVPacket txt_pkt;
736  uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
737  uint8_t *txt_buf = txt_buf0;
738 
739  if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
740  int i;
741  int64_t line_mask = 1;
742  BMDPixelFormat vanc_format = vanc->GetPixelFormat();
743  txt_buf[0] = 0x10; // data_identifier - EBU_data
744  txt_buf++;
745 #if CONFIG_LIBZVBI
746  if (ctx->bmd_mode == bmdModePAL && ctx->teletext_lines &&
747  (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
748  av_assert0(videoFrame->GetWidth() == 720);
749  for (i = 6; i < 336; i++, line_mask <<= 1) {
750  uint8_t *buf;
751  if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
752  if (vanc_format == bmdFormat8BitYUV)
753  txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
754  else
755  txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
756  }
757  if (i == 22)
758  i = 317;
759  }
760  }
761 #endif
762  if (vanc_format == bmdFormat10BitYUV && videoFrame->GetWidth() <= MAX_WIDTH_VANC) {
763  int idx = get_vanc_line_idx(ctx->bmd_mode);
764  for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) {
765  uint8_t *buf;
766  if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
767  uint16_t vanc[MAX_WIDTH_VANC];
768  size_t vanc_size = videoFrame->GetWidth();
769  if (ctx->bmd_mode == bmdModeNTSC && videoFrame->GetWidth() * 2 <= MAX_WIDTH_VANC) {
770  vanc_size = vanc_size * 2;
771  unpack_v210(vanc, buf, videoFrame->GetWidth());
772  } else {
773  extract_luma_from_v210(vanc, buf, videoFrame->GetWidth());
774  }
775  txt_buf = get_metadata(avctx, vanc, vanc_size,
776  txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt);
777  }
778  if (i == vanc_line_numbers[idx].field0_vanc_end)
779  i = vanc_line_numbers[idx].field1_vanc_start - 1;
780  }
781  }
782  vanc->Release();
783  if (txt_buf - txt_buf0 > 1) {
784  int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
785  while (stuffing_units--) {
786  memset(txt_buf, 0xff, 46);
787  txt_buf[1] = 0x2c; // data_unit_length
788  txt_buf += 46;
789  }
790  av_init_packet(&txt_pkt);
791  txt_pkt.pts = pkt.pts;
792  txt_pkt.dts = pkt.dts;
793  txt_pkt.stream_index = ctx->teletext_st->index;
794  txt_pkt.data = txt_buf0;
795  txt_pkt.size = txt_buf - txt_buf0;
796  if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
797  ++ctx->dropped;
798  }
799  }
800  }
801  }
802 
803  if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
804  ++ctx->dropped;
805  }
806  }
807 
808  // Handle Audio Frame
809  if (audioFrame) {
810  AVPacket pkt;
811  BMDTimeValue audio_pts;
812  av_init_packet(&pkt);
813 
814  //hack among hacks
815  pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
816  audioFrame->GetBytes(&audioFrameBytes);
817  audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
818  pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
819  pkt.dts = pkt.pts;
820 
821  //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
822  pkt.flags |= AV_PKT_FLAG_KEY;
823  pkt.stream_index = ctx->audio_st->index;
824  pkt.data = (uint8_t *)audioFrameBytes;
825 
826  if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
827  ++ctx->dropped;
828  }
829  }
830 
831  return S_OK;
832 }
833 
835  BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
836  BMDDetectedVideoInputFormatFlags)
837 {
838  ctx->bmd_mode = mode->GetDisplayMode();
839  return S_OK;
840 }
841 
842 static int decklink_autodetect(struct decklink_cctx *cctx) {
843  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
844  DECKLINK_BOOL autodetect_supported = false;
845  int i;
846 
847  if (ctx->attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &autodetect_supported) != S_OK)
848  return -1;
849  if (autodetect_supported == false)
850  return -1;
851 
852  ctx->autodetect = 1;
853  ctx->bmd_mode = bmdModeUnknown;
854  if (ctx->dli->EnableVideoInput(AUTODETECT_DEFAULT_MODE,
855  bmdFormat8BitYUV,
856  bmdVideoInputEnableFormatDetection) != S_OK) {
857  return -1;
858  }
859 
860  if (ctx->dli->StartStreams() != S_OK) {
861  return -1;
862  }
863 
864  // 1 second timeout
865  for (i = 0; i < 10; i++) {
866  av_usleep(100000);
867  /* Sometimes VideoInputFrameArrived is called without the
868  * bmdFrameHasNoInputSource flag before VideoInputFormatChanged.
869  * So don't break for bmd_mode == AUTODETECT_DEFAULT_MODE. */
870  if (ctx->bmd_mode != bmdModeUnknown &&
872  break;
873  }
874 
875  ctx->dli->PauseStreams();
876  ctx->dli->FlushStreams();
877  ctx->autodetect = 0;
878  if (ctx->bmd_mode != bmdModeUnknown) {
879  cctx->format_code = (char *)av_mallocz(5);
880  if (!cctx->format_code)
881  return -1;
882  AV_WB32(cctx->format_code, ctx->bmd_mode);
883  return 0;
884  } else {
885  return -1;
886  }
887 
888 }
889 
890 extern "C" {
891 
893 {
894  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
895  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
896 
897  if (ctx->capture_started) {
898  ctx->dli->StopStreams();
899  ctx->dli->DisableVideoInput();
900  ctx->dli->DisableAudioInput();
901  }
902 
903  ff_decklink_cleanup(avctx);
904  avpacket_queue_end(&ctx->queue);
905 
906  av_freep(&cctx->ctx);
907 
908  return 0;
909 }
910 
912 {
913  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
914  struct decklink_ctx *ctx;
915  AVStream *st;
916  HRESULT result;
917  char fname[1024];
918  char *tmp;
919  int mode_num = 0;
920  int ret;
921 
922  ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
923  if (!ctx)
924  return AVERROR(ENOMEM);
925  ctx->list_devices = cctx->list_devices;
926  ctx->list_formats = cctx->list_formats;
927  ctx->teletext_lines = cctx->teletext_lines;
928  ctx->preroll = cctx->preroll;
929  ctx->duplex_mode = cctx->duplex_mode;
930  if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
931  ctx->video_input = decklink_video_connection_map[cctx->video_input];
932  if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
933  ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
934  ctx->audio_pts_source = cctx->audio_pts_source;
935  ctx->video_pts_source = cctx->video_pts_source;
936  ctx->draw_bars = cctx->draw_bars;
937  ctx->audio_depth = cctx->audio_depth;
938  cctx->ctx = ctx;
939 
940  /* Check audio channel option for valid values: 2, 8 or 16 */
941  switch (cctx->audio_channels) {
942  case 2:
943  case 8:
944  case 16:
945  break;
946  default:
947  av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
948  return AVERROR(EINVAL);
949  }
950 
951  /* Check audio bit depth option for valid values: 16 or 32 */
952  switch (cctx->audio_depth) {
953  case 16:
954  case 32:
955  break;
956  default:
957  av_log(avctx, AV_LOG_ERROR, "Value for audio bit depth option must be either 16 or 32\n");
958  return AVERROR(EINVAL);
959  }
960 
961  /* List available devices. */
962  if (ctx->list_devices) {
963  ff_decklink_list_devices_legacy(avctx, 1, 0);
964  return AVERROR_EXIT;
965  }
966 
967  if (cctx->v210) {
968  av_log(avctx, AV_LOG_WARNING, "The bm_v210 option is deprecated and will be removed. Please use the -raw_format yuv422p10.\n");
969  cctx->raw_format = MKBETAG('v','2','1','0');
970  }
971 
972  av_strlcpy(fname, avctx->url, sizeof(fname));
973  tmp=strchr (fname, '@');
974  if (tmp != NULL) {
975  av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
976  mode_num = atoi (tmp+1);
977  *tmp = 0;
978  }
979 
980  ret = ff_decklink_init_device(avctx, fname);
981  if (ret < 0)
982  return ret;
983 
984  /* Get input device. */
985  if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
986  av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
987  avctx->url);
988  ret = AVERROR(EIO);
989  goto error;
990  }
991 
992  /* List supported formats. */
993  if (ctx->list_formats) {
995  ret = AVERROR_EXIT;
996  goto error;
997  }
998 
999  if (ff_decklink_set_configs(avctx, DIRECTION_IN) < 0) {
1000  av_log(avctx, AV_LOG_ERROR, "Could not set input configuration\n");
1001  ret = AVERROR(EIO);
1002  goto error;
1003  }
1004 
1005  ctx->input_callback = new decklink_input_callback(avctx);
1006  ctx->dli->SetCallback(ctx->input_callback);
1007 
1008  if (mode_num == 0 && !cctx->format_code) {
1009  if (decklink_autodetect(cctx) < 0) {
1010  av_log(avctx, AV_LOG_ERROR, "Cannot Autodetect input stream or No signal\n");
1011  ret = AVERROR(EIO);
1012  goto error;
1013  }
1014  av_log(avctx, AV_LOG_INFO, "Autodetected the input mode\n");
1015  }
1016  if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
1017  av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
1018  mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
1019  ret = AVERROR(EIO);
1020  goto error;
1021  }
1022 
1023 #if !CONFIG_LIBZVBI
1024  if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
1025  av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
1026  ret = AVERROR(ENOSYS);
1027  goto error;
1028  }
1029 #endif
1030 
1031  /* Setup streams. */
1032  st = avformat_new_stream(avctx, NULL);
1033  if (!st) {
1034  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1035  ret = AVERROR(ENOMEM);
1036  goto error;
1037  }
1038  st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1039  st->codecpar->codec_id = cctx->audio_depth == 32 ? AV_CODEC_ID_PCM_S32LE : AV_CODEC_ID_PCM_S16LE;
1040  st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
1041  st->codecpar->channels = cctx->audio_channels;
1042  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1043  ctx->audio_st=st;
1044 
1045  st = avformat_new_stream(avctx, NULL);
1046  if (!st) {
1047  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1048  ret = AVERROR(ENOMEM);
1049  goto error;
1050  }
1051  st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
1052  st->codecpar->width = ctx->bmd_width;
1053  st->codecpar->height = ctx->bmd_height;
1054 
1055  st->time_base.den = ctx->bmd_tb_den;
1056  st->time_base.num = ctx->bmd_tb_num;
1057  st->r_frame_rate = av_make_q(st->time_base.den, st->time_base.num);
1058 
1059  switch((BMDPixelFormat)cctx->raw_format) {
1060  case bmdFormat8BitYUV:
1061  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1062  st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
1063  st->codecpar->format = AV_PIX_FMT_UYVY422;
1064  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
1065  break;
1066  case bmdFormat10BitYUV:
1067  st->codecpar->codec_id = AV_CODEC_ID_V210;
1068  st->codecpar->codec_tag = MKTAG('V','2','1','0');
1069  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
1070  st->codecpar->bits_per_coded_sample = 10;
1071  break;
1072  case bmdFormat8BitARGB:
1073  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1074  st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
1075  st->codecpar->format = AV_PIX_FMT_0RGB;
1076  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1077  break;
1078  case bmdFormat8BitBGRA:
1079  st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
1080  st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
1081  st->codecpar->format = AV_PIX_FMT_BGR0;
1082  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
1083  break;
1084  case bmdFormat10BitRGB:
1085  st->codecpar->codec_id = AV_CODEC_ID_R210;
1086  st->codecpar->codec_tag = MKTAG('R','2','1','0');
1087  st->codecpar->format = AV_PIX_FMT_RGB48LE;
1088  st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 30, st->time_base.den, st->time_base.num);
1089  st->codecpar->bits_per_coded_sample = 10;
1090  break;
1091  default:
1092  av_log(avctx, AV_LOG_ERROR, "Raw Format %.4s not supported\n", (char*) &cctx->raw_format);
1093  ret = AVERROR(EINVAL);
1094  goto error;
1095  }
1096 
1097  switch (ctx->bmd_field_dominance) {
1098  case bmdUpperFieldFirst:
1099  st->codecpar->field_order = AV_FIELD_TT;
1100  break;
1101  case bmdLowerFieldFirst:
1102  st->codecpar->field_order = AV_FIELD_BB;
1103  break;
1104  case bmdProgressiveFrame:
1105  case bmdProgressiveSegmentedFrame:
1106  st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
1107  break;
1108  }
1109 
1110  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1111 
1112  ctx->video_st=st;
1113 
1114  if (ctx->teletext_lines) {
1115  st = avformat_new_stream(avctx, NULL);
1116  if (!st) {
1117  av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
1118  ret = AVERROR(ENOMEM);
1119  goto error;
1120  }
1121  st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
1122  st->time_base.den = ctx->bmd_tb_den;
1123  st->time_base.num = ctx->bmd_tb_num;
1124  st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
1125  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
1126  ctx->teletext_st = st;
1127  }
1128 
1129  av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
1130  result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, cctx->audio_depth == 32 ? bmdAudioSampleType32bitInteger : bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
1131 
1132  if (result != S_OK) {
1133  av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
1134  ret = AVERROR(EIO);
1135  goto error;
1136  }
1137 
1138  result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
1139  (BMDPixelFormat) cctx->raw_format,
1140  bmdVideoInputFlagDefault);
1141 
1142  if (result != S_OK) {
1143  av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
1144  ret = AVERROR(EIO);
1145  goto error;
1146  }
1147 
1148  avpacket_queue_init (avctx, &ctx->queue);
1149 
1150  if (ctx->dli->StartStreams() != S_OK) {
1151  av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
1152  ret = AVERROR(EIO);
1153  goto error;
1154  }
1155 
1156  return 0;
1157 
1158 error:
1159  ff_decklink_cleanup(avctx);
1160  return ret;
1161 }
1162 
1164 {
1165  struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
1166  struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
1167 
1168  avpacket_queue_get(&ctx->queue, pkt, 1);
1169 
1170  return 0;
1171 }
1172 
1174 {
1175  return ff_decklink_list_devices(avctx, device_list, 1, 0);
1176 }
1177 
1178 } /* extern "C" */
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:77
#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:4820
const uint8_t ff_reverse[256]
Definition: reverse.c:23
ATSC A53 Part 4 Closed Captions.
Definition: avcodec.h:1345
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:874
int size
Definition: avcodec.h:1431
BMDDisplayMode mode
Convenience header that includes libavutil's core.
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:1342
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)
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:99
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:298
AVPacket pkt
Definition: avformat.h:2000
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
#define height
uint8_t * data
Definition: avcodec.h:1430
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:647
#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:1462
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
static const uint16_t mask[17]
Definition: lzw.c:38
AVPacketList * last_pkt
#define AVERROR(e)
Definition: error.h:43
#define FALSE
Definition: windows2linux.h:37
char * url
input or output URL.
Definition: avformat.h:1438
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
Definition: graph2dot.c:48
uint16_t width
Definition: gdv.c:47
simple assert() macros that are a bit more flexible than ISO C assert().
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:1436
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
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:655
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:873
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
void * LPVOID
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
void * buf
Definition: avisynth_c.h:690
#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:236
int64_t max_q_size
AVFormatContext * avctx
List of devices.
Definition: avdevice.h:460
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
#define av_parity
Definition: intmath.h:158
Main libavformat public API header.
_fmutex pthread_mutex_t
Definition: os2threads.h:49
struct AVPacketList * next
Definition: avformat.h:2001
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_free(p)
int len
void * priv_data
Format private data.
Definition: avformat.h:1370
int channels
Audio only.
Definition: avcodec.h:3990
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:1429
#define av_freep(p)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
#define MKTAG(a, b, c, d)
Definition: common.h:366
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:233
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1407
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:1423
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static uint8_t tmp[11]
Definition: aes_ctr.c:26