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; }
void MP4Encoder::EncodeStart() { //1. 注册所有组件 av_register_all(); //2. 初始化输出码流的AVFormatContext avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, this->mp4Path); //3. 打开待输出的视频文件 if (avio_open(&pFormatCtx->pb, this->mp4Path, AVIO_FLAG_READ_WRITE)) { LOGE("open output file failed"); return; } //4. 初始化视频码流 pStream = avformat_new_stream(pFormatCtx, NULL); if (pStream == NULL) { LOGE("allocating output stream failed"); return; } //5. 寻找编码器并打开编码器 pCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG4); if (!pCodec) { LOGE("could not find encoder"); return; } //6. 分配编码器并设置参数 pCodecCtx = avcodec_alloc_context3(pCodec); pCodecCtx->codec_id = pCodec->id; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->width = height; pCodecCtx->height = width; pCodecCtx->time_base.num = 1; pCodecCtx->time_base.den = 25; pCodecCtx->bit_rate = 400000; pCodecCtx->gop_size = 12; //将AVCodecContext的成员复制到AVCodecParameters结构体 avcodec_parameters_from_context(pStream->codecpar, pCodecCtx); av_stream_set_r_frame_rate(pStream, {1, 25}); //7. 打开编码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { LOGE("open encoder fail!"); return; } //输出格式信息 av_dump_format(pFormatCtx, 0, this->mp4Path, 1); //初始化帧 pFrame = av_frame_alloc(); pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; pFrame->format = pCodecCtx->pix_fmt; int bufferSize = av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1); pFrameBuffer = (uint8_t *) av_malloc(bufferSize); av_image_fill_arrays(pFrame->data, pFrame->linesize, pFrameBuffer, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1); AVDictionary *opt = 0; //H.264 if (pCodecCtx->codec_id == AV_CODEC_ID_H264) { av_dict_set_int(&opt, "video_track_timescale", 25, 0); av_dict_set(&opt, "preset", "slow", 0); av_dict_set(&opt, "tune", "zerolatency", 0); } //8. 写文件头 avformat_write_header(pFormatCtx, &opt); //创建已编码帧 av_new_packet(&avPacket, bufferSize * 3); //标记正在转换 this->transform = true; }