OSDN Git Service

Merge commit '373fd76b4dbd9aa03ed28e502f33f2ca8c1ce19a'
[android-x86/external-ffmpeg.git] / libavdevice / decklink_dec.cpp
1 /*
2  * Blackmagic DeckLink input
3  * Copyright (c) 2013-2014 Luca Barbato, Deti Fliegl
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <DeckLinkAPI.h>
23
24 extern "C" {
25 #include "config.h"
26 #include "libavformat/avformat.h"
27 #include "libavformat/internal.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/common.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/time.h"
32 #include "libavutil/mathematics.h"
33 #if CONFIG_LIBZVBI
34 #include <libzvbi.h>
35 #endif
36 }
37
38 #include "decklink_common.h"
39 #include "decklink_dec.h"
40
41 #if CONFIG_LIBZVBI
42 static uint8_t calc_parity_and_line_offset(int line)
43 {
44     uint8_t ret = (line < 313) << 5;
45     if (line >= 7 && line <= 22)
46         ret += line;
47     if (line >= 320 && line <= 335)
48         ret += (line - 313);
49     return ret;
50 }
51
52 int teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt)
53 {
54     vbi_bit_slicer slicer;
55
56     vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, VBI_PIXFMT_UYVY);
57
58     if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
59         return -1;
60
61     tgt[0] = 0x02; // data_unit_id
62     tgt[1] = 0x2c; // data_unit_length
63     tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
64     tgt[3] = 0xe4; // framing code
65
66     return 0;
67 }
68 #endif
69
70 static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
71 {
72     memset(q, 0, sizeof(AVPacketQueue));
73     pthread_mutex_init(&q->mutex, NULL);
74     pthread_cond_init(&q->cond, NULL);
75     q->avctx = avctx;
76 }
77
78 static void avpacket_queue_flush(AVPacketQueue *q)
79 {
80     AVPacketList *pkt, *pkt1;
81
82     pthread_mutex_lock(&q->mutex);
83     for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
84         pkt1 = pkt->next;
85         av_packet_unref(&pkt->pkt);
86         av_freep(&pkt);
87     }
88     q->last_pkt   = NULL;
89     q->first_pkt  = NULL;
90     q->nb_packets = 0;
91     q->size       = 0;
92     pthread_mutex_unlock(&q->mutex);
93 }
94
95 static void avpacket_queue_end(AVPacketQueue *q)
96 {
97     avpacket_queue_flush(q);
98     pthread_mutex_destroy(&q->mutex);
99     pthread_cond_destroy(&q->cond);
100 }
101
102 static unsigned long long avpacket_queue_size(AVPacketQueue *q)
103 {
104     unsigned long long size;
105     pthread_mutex_lock(&q->mutex);
106     size = q->size;
107     pthread_mutex_unlock(&q->mutex);
108     return size;
109 }
110
111 static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
112 {
113     AVPacketList *pkt1;
114
115     // Drop Packet if queue size is > 1GB
116     if (avpacket_queue_size(q) >  1024 * 1024 * 1024 ) {
117         av_log(q->avctx, AV_LOG_WARNING,  "Decklink input buffer overrun!\n");
118         return -1;
119     }
120     /* duplicate the packet */
121     if (av_dup_packet(pkt) < 0) {
122         return -1;
123     }
124
125     pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
126     if (!pkt1) {
127         return -1;
128     }
129     pkt1->pkt  = *pkt;
130     pkt1->next = NULL;
131
132     pthread_mutex_lock(&q->mutex);
133
134     if (!q->last_pkt) {
135         q->first_pkt = pkt1;
136     } else {
137         q->last_pkt->next = pkt1;
138     }
139
140     q->last_pkt = pkt1;
141     q->nb_packets++;
142     q->size += pkt1->pkt.size + sizeof(*pkt1);
143
144     pthread_cond_signal(&q->cond);
145
146     pthread_mutex_unlock(&q->mutex);
147     return 0;
148 }
149
150 static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
151 {
152     AVPacketList *pkt1;
153     int ret;
154
155     pthread_mutex_lock(&q->mutex);
156
157     for (;; ) {
158         pkt1 = q->first_pkt;
159         if (pkt1) {
160             q->first_pkt = pkt1->next;
161             if (!q->first_pkt) {
162                 q->last_pkt = NULL;
163             }
164             q->nb_packets--;
165             q->size -= pkt1->pkt.size + sizeof(*pkt1);
166             *pkt     = pkt1->pkt;
167             av_free(pkt1);
168             ret = 1;
169             break;
170         } else if (!block) {
171             ret = 0;
172             break;
173         } else {
174             pthread_cond_wait(&q->cond, &q->mutex);
175         }
176     }
177     pthread_mutex_unlock(&q->mutex);
178     return ret;
179 }
180
181 class decklink_input_callback : public IDeckLinkInputCallback
182 {
183 public:
184         decklink_input_callback(AVFormatContext *_avctx);
185         ~decklink_input_callback();
186
187         virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
188         virtual ULONG STDMETHODCALLTYPE AddRef(void);
189         virtual ULONG STDMETHODCALLTYPE  Release(void);
190         virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
191         virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
192
193 private:
194         ULONG           m_refCount;
195         pthread_mutex_t m_mutex;
196         AVFormatContext *avctx;
197         decklink_ctx    *ctx;
198         int no_video;
199         int64_t initial_video_pts;
200         int64_t initial_audio_pts;
201 };
202
203 decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
204 {
205     avctx = _avctx;
206     decklink_cctx       *cctx = (struct decklink_cctx *)avctx->priv_data;
207     ctx = (struct decklink_ctx *)cctx->ctx;
208     no_video = 0;
209     initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
210     pthread_mutex_init(&m_mutex, NULL);
211 }
212
213 decklink_input_callback::~decklink_input_callback()
214 {
215     pthread_mutex_destroy(&m_mutex);
216 }
217
218 ULONG decklink_input_callback::AddRef(void)
219 {
220     pthread_mutex_lock(&m_mutex);
221     m_refCount++;
222     pthread_mutex_unlock(&m_mutex);
223
224     return (ULONG)m_refCount;
225 }
226
227 ULONG decklink_input_callback::Release(void)
228 {
229     pthread_mutex_lock(&m_mutex);
230     m_refCount--;
231     pthread_mutex_unlock(&m_mutex);
232
233     if (m_refCount == 0) {
234         delete this;
235         return 0;
236     }
237
238     return (ULONG)m_refCount;
239 }
240
241 static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
242                            IDeckLinkAudioInputPacket *audioFrame,
243                            int64_t wallclock,
244                            DecklinkPtsSource pts_src,
245                            AVRational time_base, int64_t *initial_pts)
246 {
247     int64_t pts = AV_NOPTS_VALUE;
248     BMDTimeValue bmd_pts;
249     BMDTimeValue bmd_duration;
250     HRESULT res = E_INVALIDARG;
251     switch (pts_src) {
252         case PTS_SRC_AUDIO:
253             if (audioFrame)
254                 res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
255             break;
256         case PTS_SRC_VIDEO:
257             if (videoFrame)
258                 res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
259             break;
260         case PTS_SRC_REFERENCE:
261             if (videoFrame)
262                 res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
263             break;
264         case PTS_SRC_WALLCLOCK:
265             pts = av_rescale_q(wallclock, AV_TIME_BASE_Q, time_base);
266             break;
267     }
268     if (res == S_OK)
269         pts = bmd_pts / time_base.num;
270
271     if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
272         *initial_pts = pts;
273     if (*initial_pts != AV_NOPTS_VALUE)
274         pts -= *initial_pts;
275
276     return pts;
277 }
278
279 HRESULT decklink_input_callback::VideoInputFrameArrived(
280     IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
281 {
282     void *frameBytes;
283     void *audioFrameBytes;
284     BMDTimeValue frameTime;
285     BMDTimeValue frameDuration;
286     int64_t wallclock = 0;
287
288     ctx->frameCount++;
289     if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
290         wallclock = av_gettime_relative();
291
292     // Handle Video Frame
293     if (videoFrame) {
294         AVPacket pkt;
295         av_init_packet(&pkt);
296         if (ctx->frameCount % 25 == 0) {
297             unsigned long long qsize = avpacket_queue_size(&ctx->queue);
298             av_log(avctx, AV_LOG_DEBUG,
299                     "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
300                     ctx->frameCount,
301                     videoFrame->GetRowBytes() * videoFrame->GetHeight(),
302                     (double)qsize / 1024 / 1024);
303         }
304
305         videoFrame->GetBytes(&frameBytes);
306         videoFrame->GetStreamTime(&frameTime, &frameDuration,
307                                   ctx->video_st->time_base.den);
308
309         if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
310             if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
311                 unsigned bars[8] = {
312                     0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
313                     0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
314                 int width  = videoFrame->GetWidth();
315                 int height = videoFrame->GetHeight();
316                 unsigned *p = (unsigned *)frameBytes;
317
318                 for (int y = 0; y < height; y++) {
319                     for (int x = 0; x < width; x += 2)
320                         *p++ = bars[(x * 8) / width];
321                 }
322             }
323
324             if (!no_video) {
325                 av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
326                         "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
327             }
328             no_video = 1;
329         } else {
330             if (no_video) {
331                 av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
332                         "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
333             }
334             no_video = 0;
335         }
336
337         pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
338         pkt.dts = pkt.pts;
339
340         pkt.duration = frameDuration;
341         //To be made sure it still applies
342         pkt.flags       |= AV_PKT_FLAG_KEY;
343         pkt.stream_index = ctx->video_st->index;
344         pkt.data         = (uint8_t *)frameBytes;
345         pkt.size         = videoFrame->GetRowBytes() *
346                            videoFrame->GetHeight();
347         //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
348
349 #if CONFIG_LIBZVBI
350         if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) {
351             IDeckLinkVideoFrameAncillary *vanc;
352             AVPacket txt_pkt;
353             uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier
354             uint8_t *txt_buf = txt_buf0;
355
356             if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
357                 int i;
358                 int64_t line_mask = 1;
359                 txt_buf[0] = 0x10;    // data_identifier - EBU_data
360                 txt_buf++;
361                 for (i = 6; i < 336; i++, line_mask <<= 1) {
362                     uint8_t *buf;
363                     if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
364                         if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0)
365                             txt_buf += 46;
366                     }
367                     if (i == 22)
368                         i = 317;
369                 }
370                 vanc->Release();
371                 if (txt_buf - txt_buf0 > 1) {
372                     int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
373                     while (stuffing_units--) {
374                         memset(txt_buf, 0xff, 46);
375                         txt_buf[1] = 0x2c; // data_unit_length
376                         txt_buf += 46;
377                     }
378                     av_init_packet(&txt_pkt);
379                     txt_pkt.pts = pkt.pts;
380                     txt_pkt.dts = pkt.dts;
381                     txt_pkt.stream_index = ctx->teletext_st->index;
382                     txt_pkt.data = txt_buf0;
383                     txt_pkt.size = txt_buf - txt_buf0;
384                     if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
385                         ++ctx->dropped;
386                     }
387                 }
388             }
389         }
390 #endif
391
392         if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
393             ++ctx->dropped;
394         }
395     }
396
397     // Handle Audio Frame
398     if (audioFrame) {
399         AVPacket pkt;
400         BMDTimeValue audio_pts;
401         av_init_packet(&pkt);
402
403         //hack among hacks
404         pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
405         audioFrame->GetBytes(&audioFrameBytes);
406         audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
407         pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
408         pkt.dts = pkt.pts;
409
410         //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
411         pkt.flags       |= AV_PKT_FLAG_KEY;
412         pkt.stream_index = ctx->audio_st->index;
413         pkt.data         = (uint8_t *)audioFrameBytes;
414
415         if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
416             ++ctx->dropped;
417         }
418     }
419
420     return S_OK;
421 }
422
423 HRESULT decklink_input_callback::VideoInputFormatChanged(
424     BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
425     BMDDetectedVideoInputFormatFlags)
426 {
427     return S_OK;
428 }
429
430 static HRESULT decklink_start_input(AVFormatContext *avctx)
431 {
432     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
433     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
434
435     ctx->input_callback = new decklink_input_callback(avctx);
436     ctx->dli->SetCallback(ctx->input_callback);
437     return ctx->dli->StartStreams();
438 }
439
440 extern "C" {
441
442 av_cold int ff_decklink_read_close(AVFormatContext *avctx)
443 {
444     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
445     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
446
447     if (ctx->capture_started) {
448         ctx->dli->StopStreams();
449         ctx->dli->DisableVideoInput();
450         ctx->dli->DisableAudioInput();
451     }
452
453     ff_decklink_cleanup(avctx);
454     avpacket_queue_end(&ctx->queue);
455
456     av_freep(&cctx->ctx);
457
458     return 0;
459 }
460
461 av_cold int ff_decklink_read_header(AVFormatContext *avctx)
462 {
463     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
464     struct decklink_ctx *ctx;
465     AVStream *st;
466     HRESULT result;
467     char fname[1024];
468     char *tmp;
469     int mode_num = 0;
470     int ret;
471
472     ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
473     if (!ctx)
474         return AVERROR(ENOMEM);
475     ctx->list_devices = cctx->list_devices;
476     ctx->list_formats = cctx->list_formats;
477     ctx->teletext_lines = cctx->teletext_lines;
478     ctx->preroll      = cctx->preroll;
479     ctx->duplex_mode  = cctx->duplex_mode;
480     if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
481         ctx->video_input = decklink_video_connection_map[cctx->video_input];
482     if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
483         ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
484     ctx->audio_pts_source = cctx->audio_pts_source;
485     ctx->video_pts_source = cctx->video_pts_source;
486     ctx->draw_bars = cctx->draw_bars;
487     cctx->ctx = ctx;
488
489 #if !CONFIG_LIBZVBI
490     if (ctx->teletext_lines) {
491         av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n");
492         return AVERROR(ENOSYS);
493     }
494 #endif
495
496     /* Check audio channel option for valid values: 2, 8 or 16 */
497     switch (cctx->audio_channels) {
498         case 2:
499         case 8:
500         case 16:
501             break;
502         default:
503             av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
504             return AVERROR(EINVAL);
505     }
506
507     /* List available devices. */
508     if (ctx->list_devices) {
509         ff_decklink_list_devices(avctx);
510         return AVERROR_EXIT;
511     }
512
513     strcpy (fname, avctx->filename);
514     tmp=strchr (fname, '@');
515     if (tmp != NULL) {
516         av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
517         mode_num = atoi (tmp+1);
518         *tmp = 0;
519     }
520
521     ret = ff_decklink_init_device(avctx, fname);
522     if (ret < 0)
523         return ret;
524
525     /* Get input device. */
526     if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
527         av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
528                avctx->filename);
529         ret = AVERROR(EIO);
530         goto error;
531     }
532
533     /* List supported formats. */
534     if (ctx->list_formats) {
535         ff_decklink_list_formats(avctx, DIRECTION_IN);
536         ret = AVERROR_EXIT;
537         goto error;
538     }
539
540     if (mode_num > 0 || cctx->format_code) {
541         if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
542             av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
543                 mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
544             ret = AVERROR(EIO);
545             goto error;
546         }
547     }
548
549     /* Setup streams. */
550     st = avformat_new_stream(avctx, NULL);
551     if (!st) {
552         av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
553         ret = AVERROR(ENOMEM);
554         goto error;
555     }
556     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
557     st->codecpar->codec_id    = AV_CODEC_ID_PCM_S16LE;
558     st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
559     st->codecpar->channels    = cctx->audio_channels;
560     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
561     ctx->audio_st=st;
562
563     st = avformat_new_stream(avctx, NULL);
564     if (!st) {
565         av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
566         ret = AVERROR(ENOMEM);
567         goto error;
568     }
569     st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
570     st->codecpar->width       = ctx->bmd_width;
571     st->codecpar->height      = ctx->bmd_height;
572
573     st->time_base.den      = ctx->bmd_tb_den;
574     st->time_base.num      = ctx->bmd_tb_num;
575     av_stream_set_r_frame_rate(st, av_make_q(st->time_base.den, st->time_base.num));
576
577     if (cctx->v210) {
578         st->codecpar->codec_id    = AV_CODEC_ID_V210;
579         st->codecpar->codec_tag   = MKTAG('V', '2', '1', '0');
580         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
581     } else {
582         st->codecpar->codec_id    = AV_CODEC_ID_RAWVIDEO;
583         st->codecpar->format      = AV_PIX_FMT_UYVY422;
584         st->codecpar->codec_tag   = MKTAG('U', 'Y', 'V', 'Y');
585         st->codecpar->bit_rate    = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
586     }
587
588     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
589
590     ctx->video_st=st;
591
592     if (ctx->teletext_lines) {
593         st = avformat_new_stream(avctx, NULL);
594         if (!st) {
595             av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
596             ret = AVERROR(ENOMEM);
597             goto error;
598         }
599         st->codecpar->codec_type  = AVMEDIA_TYPE_SUBTITLE;
600         st->time_base.den         = ctx->bmd_tb_den;
601         st->time_base.num         = ctx->bmd_tb_num;
602         st->codecpar->codec_id    = AV_CODEC_ID_DVB_TELETEXT;
603         avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
604         ctx->teletext_st = st;
605     }
606
607     av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
608     result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
609
610     if (result != S_OK) {
611         av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
612         ret = AVERROR(EIO);
613         goto error;
614     }
615
616     result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
617                                         cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV,
618                                         bmdVideoInputFlagDefault);
619
620     if (result != S_OK) {
621         av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
622         ret = AVERROR(EIO);
623         goto error;
624     }
625
626     avpacket_queue_init (avctx, &ctx->queue);
627
628     if (decklink_start_input (avctx) != S_OK) {
629         av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
630         ret = AVERROR(EIO);
631         goto error;
632     }
633
634     return 0;
635
636 error:
637     ff_decklink_cleanup(avctx);
638     return ret;
639 }
640
641 int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
642 {
643     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
644     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
645     AVFrame *frame = ctx->video_st->codec->coded_frame;
646
647     avpacket_queue_get(&ctx->queue, pkt, 1);
648     if (frame && (ctx->bmd_field_dominance == bmdUpperFieldFirst || ctx->bmd_field_dominance == bmdLowerFieldFirst)) {
649         frame->interlaced_frame = 1;
650         if (ctx->bmd_field_dominance == bmdUpperFieldFirst) {
651             frame->top_field_first = 1;
652         }
653     }
654
655     return 0;
656 }
657
658 } /* extern "C" */