av_cold int ff_decklink_write_header(AVFormatContext *avctx) { struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data; struct decklink_ctx *ctx; unsigned int n; int ret; ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx)); if (!ctx) return AVERROR(ENOMEM); ctx->list_devices = cctx->list_devices; ctx->list_formats = cctx->list_formats; ctx->preroll = cctx->preroll; cctx->ctx = ctx; /* List available devices. */ if (ctx->list_devices) { ff_decklink_list_devices(avctx); return AVERROR_EXIT; } ret = ff_decklink_init_device(avctx, avctx->filename); if (ret < 0) return ret; /* Get output device. */ if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n", avctx->filename); ret = AVERROR(EIO); goto error; } /* List supported formats. */ if (ctx->list_formats) { ff_decklink_list_formats(avctx); ret = AVERROR_EXIT; goto error; } /* Setup streams. */ ret = AVERROR(EIO); for (n = 0; n < avctx->nb_streams; n++) { AVStream *st = avctx->streams[n]; AVCodecContext *c = st->codec; if (c->codec_type == AVMEDIA_TYPE_AUDIO) { if (decklink_setup_audio(avctx, st)) goto error; } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) { if (decklink_setup_video(avctx, st)) goto error; } else { av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n"); goto error; } } return 0; error: ff_decklink_cleanup(avctx); return ret; }
av_cold int ff_decklink_read_header(AVFormatContext *avctx) { struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; struct decklink_ctx *ctx; AVStream *st; HRESULT result; char fname[1024]; char *tmp; int mode_num = 0; int ret; ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx)); if (!ctx) return AVERROR(ENOMEM); ctx->list_devices = cctx->list_devices; ctx->list_formats = cctx->list_formats; ctx->teletext_lines = cctx->teletext_lines; ctx->preroll = cctx->preroll; ctx->duplex_mode = cctx->duplex_mode; if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map)) ctx->video_input = decklink_video_connection_map[cctx->video_input]; if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map)) ctx->audio_input = decklink_audio_connection_map[cctx->audio_input]; ctx->audio_pts_source = cctx->audio_pts_source; ctx->video_pts_source = cctx->video_pts_source; ctx->draw_bars = cctx->draw_bars; cctx->ctx = ctx; #if !CONFIG_LIBZVBI if (ctx->teletext_lines) { av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n"); return AVERROR(ENOSYS); } #endif /* Check audio channel option for valid values: 2, 8 or 16 */ switch (cctx->audio_channels) { case 2: case 8: case 16: break; default: av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n"); return AVERROR(EINVAL); } /* List available devices. */ if (ctx->list_devices) { ff_decklink_list_devices(avctx); return AVERROR_EXIT; } strcpy (fname, avctx->filename); tmp=strchr (fname, '@'); if (tmp != NULL) { mode_num = atoi (tmp+1); *tmp = 0; } ret = ff_decklink_init_device(avctx, fname); if (ret < 0) return ret; /* Get input device. */ if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n", avctx->filename); ret = AVERROR(EIO); goto error; } /* List supported formats. */ if (ctx->list_formats) { ff_decklink_list_formats(avctx, DIRECTION_IN); ret = AVERROR_EXIT; goto error; } if (mode_num > 0) { if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) { av_log(avctx, AV_LOG_ERROR, "Could not set mode %d for %s\n", mode_num, fname); ret = AVERROR(EIO); goto error; } } /* Setup streams. */ st = avformat_new_stream(avctx, NULL); if (!st) { av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n"); ret = AVERROR(ENOMEM); goto error; } st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; st->codecpar->sample_rate = bmdAudioSampleRate48kHz; st->codecpar->channels = cctx->audio_channels; avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ ctx->audio_st=st; st = avformat_new_stream(avctx, NULL); if (!st) { av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n"); ret = AVERROR(ENOMEM); goto error; } st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->width = ctx->bmd_width; st->codecpar->height = ctx->bmd_height; st->time_base.den = ctx->bmd_tb_den; st->time_base.num = ctx->bmd_tb_num; av_stream_set_r_frame_rate(st, av_make_q(st->time_base.den, st->time_base.num)); if (cctx->v210) { st->codecpar->codec_id = AV_CODEC_ID_V210; st->codecpar->codec_tag = MKTAG('V', '2', '1', '0'); st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3); } else { st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; st->codecpar->format = AV_PIX_FMT_UYVY422; st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y'); st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num); } avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ ctx->video_st=st; if (ctx->teletext_lines) { st = avformat_new_stream(avctx, NULL); if (!st) { av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n"); ret = AVERROR(ENOMEM); goto error; } st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; st->time_base.den = ctx->bmd_tb_den; st->time_base.num = ctx->bmd_tb_num; st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT; avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ ctx->teletext_st = st; } av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels); result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels); if (result != S_OK) { av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n"); ret = AVERROR(EIO); goto error; } result = ctx->dli->EnableVideoInput(ctx->bmd_mode, cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV, bmdVideoInputFlagDefault); if (result != S_OK) { av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n"); ret = AVERROR(EIO); goto error; } avpacket_queue_init (avctx, &ctx->queue); if (decklink_start_input (avctx) != S_OK) { av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n"); ret = AVERROR(EIO); goto error; } return 0; error: ff_decklink_cleanup(avctx); return ret; }