void copyAVFormatContext(AVFormatContext **dest, AVFormatContext **source){ int numStreams = (*source)->nb_streams; LOGI("copyAVFormatContext source has %d streams", numStreams); int i; for (i = 0; i < numStreams; i++) { // Get input stream AVStream *inputStream = (*source)->streams[i]; AVCodecContext *inputCodecContext = inputStream->codec; // Add new stream to output with codec from input stream //LOGI("Attempting to find encoder %s", avcodec_get_name(inputCodecContext->codec_id)); AVCodec *outputCodec = avcodec_find_encoder(inputCodecContext->codec_id); if(outputCodec == NULL){ LOGI("Unable to find encoder %s", avcodec_get_name(inputCodecContext->codec_id)); } AVStream *outputStream = avformat_new_stream(*dest, outputCodec); AVCodecContext *outputCodecContext = outputStream->codec; // Copy input stream's codecContext for output stream's codecContext avcodec_copy_context(outputCodecContext, inputCodecContext); outputCodecContext->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL; LOGI("copyAVFormatContext Copied stream %d with codec %s sample_fmt %s", i, avcodec_get_name(inputCodecContext->codec_id), av_get_sample_fmt_name(inputCodecContext->sample_fmt)); } }
Stream::Stream(AVFormatContext*& formatCtx, AVStream*& stream, DataSource& dataSource, std::shared_ptr<Timer> timer) : m_formatCtx(formatCtx), m_stream(stream), m_dataSource(dataSource), m_timer(timer), m_codec(nullptr), m_streamID(-1), m_packetList(), m_status(Stopped), m_readerMutex() { CHECK(stream, "Stream::Stream() - invalid stream argument"); CHECK(timer, "Inconcistency error: null timer"); int err = 0; m_stream = stream; m_streamID = stream->index; CHECK(m_stream, "Inconcistency error: null stream") CHECK(m_streamID >= 0, "Inconcistency error: invalid stream id"); // Get the decoder m_codec = avcodec_find_decoder(m_stream->codec->codec_id); CHECK(m_codec, "Stream() - no decoder for " + std::string(avcodec_get_name(m_stream->codec->codec_id)) + " codec"); // Load the codec err = avcodec_open2(m_stream->codec, m_codec, nullptr); CHECK0(err, "Stream() - unable to load decoder for codec " + std::string(avcodec_get_name(m_stream->codec->codec_id))); AVDictionaryEntry* entry = av_dict_get(m_stream->metadata, "language", nullptr, 0); if (entry) { m_language = entry->value; } }
int av_encode_init(AVCodecContext *c,enum AVCodecID codec_id, AVDictionary *opt_arg) { AVCodec * codec; AVDictionary *opt = NULL; int ret; codec = avcodec_find_encoder(codec_id); if (!codec) { av_log(NULL, AV_LOG_FATAL, "Could not find encoder '%s'\n", avcodec_get_name(codec_id)); return -1; } /* open it */ av_dict_copy(&opt, opt_arg, 0); ret = avcodec_open2(c, codec, &opt); av_dict_free(&opt); if (ret < 0) { char errmsg[ERROR_BUFFER_SIZE]; av_strerror(ret, errmsg, ERROR_BUFFER_SIZE); av_log(NULL, AV_LOG_FATAL, "Could not open codec: %s\n", errmsg); return -2; } return 0; }
/* Add an output stream. */ static void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id) { AVCodecContext *c; int i; /* find the encoder */ *codec = avcodec_find_encoder(codec_id); if (!(*codec)) { fprintf(stderr, "Could not find encoder for '%s'\n", avcodec_get_name(codec_id)); exit(1); } ost->st = avformat_new_stream(oc, *codec); if (!ost->st) { fprintf(stderr, "Could not allocate stream\n"); exit(1); } ost->st->id = oc->nb_streams-1; c = ost->st->codec; switch ((*codec)->type) { case AVMEDIA_TYPE_VIDEO: c->codec_id = codec_id; c->bit_rate = 400000; /* Resolution must be a multiple of two. */ c->width = 352; c->height = 288; /* timebase: This is the fundamental unit of time (in seconds) in terms * of which frame timestamps are represented. For fixed-fps content, * timebase should be 1/framerate and timestamp increments should be * identical to 1. */ ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE }; c->time_base = ost->st->time_base; c->gop_size = 12; /* emit one intra frame every twelve frames at most */ c->pix_fmt = STREAM_PIX_FMT; if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { /* just for testing, we also add B frames */ c->max_b_frames = 2; } if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { /* Needed to avoid using macroblocks in which some coeffs overflow. * This does not happen with normal video, it just happens here as * the motion of the chroma plane does not match the luma plane. */ c->mb_decision = 2; } break; default: break; } /* Some formats want stream headers to be separate. */ if (oc->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; }
/* Add an output stream. */ static AVStream *add_webcam_stream(struct liveStream *ctx, AVCodec **codec, enum AVCodecID codec_id) { AVCodecContext *c = NULL; AVStream *st = NULL; AVFormatContext *oc = ctx->oc; /* find the encoder */ *codec = avcodec_find_encoder(codec_id); if (!(*codec)) { av_log(NULL, AV_LOG_ERROR, "Could not find encoder for '%s'\n", avcodec_get_name(codec_id)); return NULL; } st = avformat_new_stream(oc, *codec); if (!st) { av_log(NULL, AV_LOG_ERROR, "Could not allocate stream\n"); return NULL; } st->id = oc->nb_streams - 1; c = st->codec; c->codec_id = codec_id; /* Resolution must be a multiple of two. */ c->width = STREAM_WIDTH; c->height = STREAM_HEIGHT; /* timebase: This is the fundamental unit of time (in seconds) in terms * of which frame timestamps are represented. For fixed-fps content, * timebase should be 1/framerate and timestamp increments should be * identical to 1. * Timebase = 1/frame rate therfore den = num */ st->avg_frame_rate.den = ctx->video_avg_frame_rate.den; st->avg_frame_rate.num = ctx->video_avg_frame_rate.num; st->time_base.den = ctx->video_avg_frame_rate.num; st->time_base.num = ctx->video_avg_frame_rate.den; c->time_base.den = ctx->video_avg_frame_rate.num; c->time_base.num = ctx->video_avg_frame_rate.den; c->gop_size = ctx->video_avg_frame_rate.num/ctx->video_avg_frame_rate.den; /* emit one intra frame every twelve frames at most */ c->pix_fmt = STREAM_PIX_FMT; if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { /* Needed to avoid using macroblocks in which some coeffs overflow. * This does not happen with normal video, it just happens here as * the motion of the chroma plane does not match the luma plane. */ c->mb_decision = 2; } /* Some formats want stream headers to be separate. */ if (oc->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
QString AVDecoder::codecName() const { DPTR_D(const AVDecoder); if (!d.codec_name.isEmpty()) return d.codec_name; if (d.codec_ctx) return avcodec_get_name(d.codec_ctx->codec_id); return ""; }
/* Add an output stream. */ static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id, StreamInfo *sInfo) { AVCodecContext *c; AVStream *st; /* find the encoder */ *codec = avcodec_find_encoder(codec_id); if (!(*codec)) { fprintf(stderr, "Could not find encoder for '%s'\n", avcodec_get_name(codec_id)); exit(1); } st = avformat_new_stream(oc, *codec); if (!st) { fprintf(stderr, "Could not allocate stream\n"); exit(1); } st->id = oc->nb_streams - 1; c = st->codec; switch ((*codec)->type) { case AVMEDIA_TYPE_VIDEO: avcodec_get_context_defaults3(c, *codec); c->codec_id = codec_id; c->bit_rate = sInfo->bitrate; /* Resolution must be a multiple of two. */ c->width = sInfo->width; c->height = sInfo->height; /* timebase: This is the fundamental unit of time (in seconds) in terms * of which frame timestamps are represented. For fixed-fps content, * timebase should be 1/framerate and timestamp increments should be * identical to 1. */ c->time_base.den = sInfo->frame_rate; c->time_base.num = 1; c->gop_size = 12; /* emit one intra frame every twelve frames at most */ c->pix_fmt = sInfo->pix_fmt; c->rc_min_rate = sInfo->bitrate-50; c->rc_max_rate = sInfo->bitrate+50; c->rc_buffer_size = sInfo->bitrate+60; if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { /* just for testing, we also add B frames */ //c->max_b_frames = 2; } if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { /* Needed to avoid using macroblocks in which some coeffs overflow. * This does not happen with normal video, it just happens here as * the motion of the chroma plane does not match the luma plane. */ c->mb_decision = 2; } break; default: break; } /* Some formats want stream headers to be separate. */ if (oc->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
bool AVDecoder::open() { DPTR_D(AVDecoder); // codec_ctx can't be null for none-ffmpeg based decoders because we may use it's properties in those decoders if (!d.codec_ctx) { qWarning("FFmpeg codec context not ready"); return false; } AVCodec *codec = 0; if (!d.codec_name.isEmpty()) { codec = avcodec_find_decoder_by_name(d.codec_name.toUtf8().constData()); if (!codec) { const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(d.codec_name.toUtf8().constData()); if (cd) { codec = avcodec_find_decoder(cd->id); } } } else { codec = avcodec_find_decoder(d.codec_ctx->codec_id); } if (!codec) { // TODO: can be null for none-ffmpeg based decoders QString es(tr("No codec could be found for '%1'")); if (d.codec_name.isEmpty()) { es = es.arg(QLatin1String(avcodec_get_name(d.codec_ctx->codec_id))); } else { es = es.arg(d.codec_name); } qWarning() << es; AVError::ErrorCode ec(AVError::CodecError); switch (d.codec_ctx->coder_type) { case AVMEDIA_TYPE_VIDEO: ec = AVError::VideoCodecNotFound; break; case AVMEDIA_TYPE_AUDIO: ec = AVError::AudioCodecNotFound; break; case AVMEDIA_TYPE_SUBTITLE: ec = AVError::SubtitleCodecNotFound; default: break; } emit error(AVError(ec, es)); return false; } // hwa extra init can be here if (!d.open()) { d.close(); return false; } // TODO: skip for none-ffmpeg based decoders d.applyOptionsForDict(); av_opt_set_int(d.codec_ctx, "refcounted_frames", d.enableFrameRef(), 0); // why dict may have no effect? // TODO: only open for ff decoders AV_ENSURE_OK(avcodec_open2(d.codec_ctx, codec, d.options.isEmpty() ? NULL : &d.dict), false); d.is_open = true; return true; }
void format_from_av(AVCodecContext *src, format_t *dst) { dst->codec = avcodec_get_name(src->codec_id); dst->samplerate = src->sample_rate; dst->bitspersample = av_get_bytes_per_sample(src->sample_fmt) * 8; dst->channels = src->channels; dst->extradata = src->extradata; dst->extradata_size = src->extradata_size; dst->frame_size = src->frame_size * src->channels * av_get_bytes_per_sample(src->sample_fmt); }
AVStream *EncoderFfmpegCore::addStream(AVFormatContext *formatctx, AVCodec **codec, enum AVCodecID codec_id) { #else AVStream *EncoderFfmpegCore::addStream(AVFormatContext *formatctx, AVCodec **codec, enum CodecID codec_id) { #endif AVCodecContext *l_SCodecCtx = NULL; AVStream *l_SStream = NULL; // find the encoder *codec = avcodec_find_encoder(codec_id); if (!(*codec)) { #ifdef avcodec_get_name fprintf(stderr, "Could not find encoder for '%s'\n", avcodec_get_name(codec_id)); #endif return NULL; } l_SStream = avformat_new_stream(formatctx, *codec); if (!l_SStream) { qDebug() << "Could not allocate stream"; return NULL; } l_SStream->id = formatctx->nb_streams-1; l_SCodecCtx = l_SStream->codec; m_pResample = new EncoderFfmpegResample(l_SCodecCtx); switch ((*codec)->type) { case AVMEDIA_TYPE_AUDIO: l_SStream->id = 1; l_SCodecCtx->sample_fmt = m_pEncoderAudioCodec->sample_fmts[0]; l_SCodecCtx->bit_rate = m_lBitrate; l_SCodecCtx->sample_rate = 44100; l_SCodecCtx->channels = 2; m_pResample->open(AV_SAMPLE_FMT_FLT, l_SCodecCtx->sample_fmt); break; default: break; } // Some formats want stream headers to be separate. if (formatctx->oformat->flags & AVFMT_GLOBALHEADER) l_SCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER; return l_SStream; }
bool AVDecoder::open() { DPTR_D(AVDecoder); if (!d.codec_ctx) { qWarning("FFmpeg codec context not ready"); return false; } AVCodec *codec = 0; if (!d.codec_name.isEmpty()) { codec = avcodec_find_decoder_by_name(d.codec_name.toUtf8().constData()); } else { codec = avcodec_find_decoder(d.codec_ctx->codec_id); } if (!codec) { QString es(tr("No codec could be found for '%1'")); if (d.codec_name.isEmpty()) { es = es.arg(avcodec_get_name(d.codec_ctx->codec_id)); } else { es = es.arg(d.codec_name); } qWarning() << es; AVError::ErrorCode ec(AVError::CodecError); switch (d.codec_ctx->coder_type) { case AVMEDIA_TYPE_VIDEO: ec = AVError::VideoCodecNotFound; break; case AVMEDIA_TYPE_AUDIO: ec = AVError::AudioCodecNotFound; break; case AVMEDIA_TYPE_SUBTITLE: ec = AVError::SubtitleCodecNotFound; default: break; } emit error(AVError(ec, es)); return false; } // hwa extra init can be here if (!d.open()) { d.close(); return false; } d.applyOptionsForDict(); int ret = avcodec_open2(d.codec_ctx, codec, d.options.isEmpty() ? NULL : &d.dict); if (ret < 0) { qWarning("open video codec failed: %s", av_err2str(ret)); return false; } d.is_open = true; return true; }
void plex_report_stream(const AVStream *st) { if (plexContext.progress_url && (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) && !(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) { char url[4096]; snprintf(url, sizeof(url), "%s/stream?index=%i&id=%i&codec=%s&type=%s", plexContext.progress_url, st->index, st->id, avcodec_get_name(st->codecpar->codec_id), av_get_media_type_string(st->codecpar->codec_type)); av_free(PMS_IssueHttpRequest(url, "PUT")); } }
bool VideoDecoderVDAPrivate::open() { qDebug("opening VDA module"); if (codec_ctx->codec_id != AV_CODEC_ID_H264) { qWarning("input codec (%s) isn't H264, canceling VDA decoding", avcodec_get_name(codec_ctx->codec_id)); return false; } #if 0 if (!codec_ctx->extradata || codec_ctx->extradata_size < 7) { qWarning("VDA requires extradata."); return false; } #endif return true; }
void set_codec(AVFormatContext *ic, int i) { const char *codec_type = av_get_media_type_string(ic->streams[i]->codec->codec_type); if (!codec_type) { return; } const char *codec_name = avcodec_get_name(ic->streams[i]->codec->codec_id); if (strcmp(codec_type, "audio") == 0) { av_dict_set(&ic->metadata, AUDIO_CODEC, codec_name, 0); } else if (codec_type && strcmp(codec_type, "video") == 0) { av_dict_set(&ic->metadata, VIDEO_CODEC, codec_name, 0); } }
IJKFF_Pipenode *ffpipenode_create_video_decoder_from_ffplay(FFPlayer *ffp) { IJKFF_Pipenode *node = ffpipenode_alloc(sizeof(IJKFF_Pipenode_Opaque)); if (!node) return node; IJKFF_Pipenode_Opaque *opaque = node->opaque; opaque->ffp = ffp; node->func_destroy = func_destroy; node->func_run_sync = func_run_sync; ffp_set_video_codec_info(ffp, AVCODEC_MODULE_NAME, avcodec_get_name(ffp->is->viddec.avctx->codec_id)); return node; }
static int lrc_write_header(AVFormatContext *s) { const AVDictionaryEntry *metadata_item; if(s->nb_streams != 1 || s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) { av_log(s, AV_LOG_ERROR, "LRC supports only a single subtitle stream.\n"); return AVERROR(EINVAL); } if(s->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP && s->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT) { av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n", avcodec_get_name(s->streams[0]->codecpar->codec_id)); return AVERROR(EINVAL); } avpriv_set_pts_info(s->streams[0], 64, 1, 100); ff_standardize_creation_time(s); ff_metadata_conv_ctx(s, ff_lrc_metadata_conv, NULL); if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests /* LRC provides a metadata slot for specifying encoder version * in addition to encoder name. We will store LIBAVFORMAT_VERSION * to it. */ av_dict_set(&s->metadata, "ve", AV_STRINGIFY(LIBAVFORMAT_VERSION), 0); } else { av_dict_set(&s->metadata, "ve", NULL, 0); } for(metadata_item = NULL; (metadata_item = av_dict_get(s->metadata, "", metadata_item, AV_DICT_IGNORE_SUFFIX));) { char *delim; if(!metadata_item->value[0]) { continue; } while((delim = strchr(metadata_item->value, '\n'))) { *delim = ' '; } while((delim = strchr(metadata_item->value, '\r'))) { *delim = ' '; } avio_printf(s->pb, "[%s:%s]\n", metadata_item->key, metadata_item->value); } avio_printf(s->pb, "\n"); return 0; }
const d3d_format_t* VideoDecoderD3DPrivate::getFormat(const AVCodecContext *avctx, const QVector<GUID> &guids, GUID* selected) const { foreach (const GUID& g, guids) { const dxva2_mode_t *mode = Dxva2FindMode(&g); if (mode) { qDebug("- '%s' is supported by hardware", mode->name); } else { qDebug("- Unknown GUID = %08X-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x", (unsigned)g.Data1, g.Data2, g.Data3 , g.Data4[0], g.Data4[1] , g.Data4[2], g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]); } } /* Try all supported mode by our priority */ const dxva2_mode_t *mode = dxva2_modes; for (; mode->name; ++mode) { if (!mode->codec || mode->codec != avctx->codec_id) { qDebug("codec does not match to %s: %s", avcodec_get_name(avctx->codec_id), avcodec_get_name((AVCodecID)mode->codec)); continue; } qDebug("D3D found codec: %s. Check runtime support for the codec.", mode->name); bool is_supported = false; //TODO: find_if foreach (const GUID& g, guids) { if (IsEqualGUID(*mode->guid, g)) { is_supported = true; break; } } if (is_supported) { qDebug("Check profile support: %s", AVDecoderPrivate::getProfileName(avctx)); is_supported = checkProfile(mode, avctx->profile); } if (!is_supported) continue; int dxfmt = fourccFor(mode->guid); if (!dxfmt) continue; if (selected) *selected = *mode->guid; return D3dFindFormat(dxfmt); } return NULL; }
static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) { int i; av_log(log_ctx, log_level, "filename:'%s' format:%s\n", slave->avf->filename, slave->avf->oformat->name); for (i = 0; i < slave->avf->nb_streams; i++) { AVStream *st = slave->avf->streams[i]; AVBSFContext *bsf = slave->bsfs[i]; const char *bsf_name; av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", i, avcodec_get_name(st->codecpar->codec_id), av_get_media_type_string(st->codecpar->codec_type)); bsf_name = bsf->filter->priv_class ? bsf->filter->priv_class->item_name(bsf) : bsf->filter->name; av_log(log_ctx, log_level, " bsfs: %s\n", bsf_name); } }
static int srt_write_header(AVFormatContext *avf) { if (avf->nb_streams != 1 || avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) { av_log(avf, AV_LOG_ERROR, "SRT supports only a single subtitles stream.\n"); return AVERROR(EINVAL); } if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT && avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP && avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) { av_log(avf, AV_LOG_ERROR, "Unsupported subtitles codec: %s\n", avcodec_get_name(avf->streams[0]->codec->codec_id)); return AVERROR(EINVAL); } avpriv_set_pts_info(avf->streams[0], 64, 1, 1000); return 0; }
AVStream* createVideoStream(AVFormatContext* pCtx, uint32_t fps, AVCodecID codecID, const std::string& filename, AVCodec*& pCodec) { // Get the encoder pCodec = avcodec_find_encoder(codecID); if(pCodec == nullptr) { error(filename, std::string("Can't find ") + avcodec_get_name(codecID) + " encoder."); return nullptr; } // create the video stream AVStream* pStream = avformat_new_stream(pCtx, nullptr); if(pStream == nullptr) { error(filename, "Failed to create video stream."); return nullptr; } pStream->id = pCtx->nb_streams - 1; pStream->time_base = {1, (int)fps}; return pStream; }
bool FFMPEG::initCodec(AVCodecID video_codec_id) { avcodec = avcodec_find_decoder(video_codec_id); if (!avcodec) { error = "Couldn't find decoder for "; error += avcodec_get_name(video_codec_id); return false; } avctx = avcodec_alloc_context3(avcodec); if (!avctx) { error = "Couldn't allocate AVCodecContext."; return false; } if (avcodec_open2(avctx, avcodec, nullptr) < 0) { error = "Couldn't open AVCodecContext."; return false; } return true; }
AVCodec* AVMuxer::Private::addStream(AVFormatContext* ctx, AVCodecID cid) { AVCodec* codec = avcodec_find_encoder(cid); if (!codec) { qWarning("Can not find encoder for %s", avcodec_get_name(cid)); return 0; } AVStream *s = avformat_new_stream(ctx, codec); if (!s) { qWarning("Can not allocate stream"); return 0; } // set by avformat if unset s->id = ctx->nb_streams - 1; AVCodecContext *c = s->codec; c->codec_id = cid; if (codec->type == AVMEDIA_TYPE_VIDEO) { if (venc) { s->time_base = kTB;//av_d2q(1.0/venc->frameRate(), venc->frameRate()*1001.0+2); c->bit_rate = venc->bitRate(); c->width = venc->width(); c->height = venc->height(); c->pix_fmt = (AVPixelFormat)VideoFormat::pixelFormatToFFmpeg(venc->pixelFormat()); // Using codec->time_base is deprecated, but needed for older lavf. c->time_base = s->time_base; } } /* Some formats want stream headers to be separate. */ if (ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; // expose avctx to encoder and set properties in encoder? // list codecs for a given format in ui if (codec->type == AVMEDIA_TYPE_AUDIO) audio_streams.push_back(s->id); else if (codec->type == AVMEDIA_TYPE_VIDEO) video_streams.push_back(s->id); else if (codec->type == AVMEDIA_TYPE_SUBTITLE) subtitle_streams.push_back(s->id); return codec; }
static int filter_packet(void *log_ctx, AVPacket *pkt, AVFormatContext *fmt_ctx, AVBitStreamFilterContext *bsf_ctx) { AVCodecContext *enc_ctx = fmt_ctx->streams[pkt->stream_index]->codec; int ret = 0; while (bsf_ctx) { AVPacket new_pkt = *pkt; ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, &new_pkt.data, &new_pkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY); if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { if ((ret = av_copy_packet(&new_pkt, pkt)) < 0) break; ret = 1; } if (ret > 0) { av_free_packet(pkt); new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, av_buffer_default_free, NULL, 0); if (!new_pkt.buf) break; } *pkt = new_pkt; bsf_ctx = bsf_ctx->next; } if (ret < 0) { av_log(log_ctx, AV_LOG_ERROR, "Failed to filter bitstream with filter %s for stream %d in file '%s' with codec %s\n", bsf_ctx->filter->name, pkt->stream_index, fmt_ctx->filename, avcodec_get_name(enc_ctx->codec_id)); } return ret; }
/* * ����Ƶ������ */ static int open_video(AVDecodeCtx *pec, AVCodecID video_codec_id, AVDictionary *opt_arg) { int ret; AVCodecContext *c = pec->_video_st->codec; AVDictionary *opt = NULL; AVCodec *codec; if(av_decode_init(c,video_codec_id,opt_arg)!=0){ av_log(NULL, AV_LOG_FATAL, "Could not init decoder '%s'\n", avcodec_get_name(video_codec_id)); return -1; } pec->_vctx.st = pec->_video_st; /* allocate and init a re-usable frame */ pec->_vctx.frame = alloc_picture(c->pix_fmt, c->width, c->height); if (!pec->_vctx.frame) { av_log(NULL, AV_LOG_FATAL, "Could not allocate video frame\n"); return -1; } return 0; }
const std::list<Demuxer::DecoderInfo>& Demuxer::getAvailableDecoders() { AVCodec* codec = nullptr; loadFFmpeg(); if (g_availableDecoders.empty()) { while (nullptr != (codec = av_codec_next(codec))) { DecoderInfo info = { avcodec_get_name(codec->id), codec->long_name, AVMediaTypeToMediaType(codec->type) }; g_availableDecoders.push_back(info); } } return g_availableDecoders; }
static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) { int i; av_log(log_ctx, log_level, "filename:'%s' format:%s\n", slave->avf->filename, slave->avf->oformat->name); for (i = 0; i < slave->avf->nb_streams; i++) { AVStream *st = slave->avf->streams[i]; AVBitStreamFilterContext *bsf = slave->bsfs[i]; av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", i, avcodec_get_name(st->codec->codec_id), av_get_media_type_string(st->codec->codec_type)); if (bsf) { av_log(log_ctx, log_level, " bsfs:"); while (bsf) { av_log(log_ctx, log_level, "%s%s", bsf->filter->name, bsf->next ? "," : ""); bsf = bsf->next; } } av_log(log_ctx, log_level, "\n"); } }
static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config) { int ret; AVCodec *codec = avcodec_find_encoder_by_name(codec_name); if (!codec || codec->type != ctx->codec_type) { report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors, "Invalid codec name: '%s'\n", codec_name); return 0; } if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) { if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0) return ret; ctx->codec = codec; } if (ctx->codec_id != codec->id) report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors, "Inconsistent configuration: trying to set '%s' " "codec option, but '%s' codec is used previously\n", codec_name, avcodec_get_name(ctx->codec_id)); return 0; }
int AudioEncode::open(AVCodecID codec_id, int flags) { mCodec = avcodec_find_encoder(codec_id); if (!mCodec) { fprintf(stderr, "Couldn't find encoder: %s\n", avcodec_get_name(codec_id)); return -1; } mCodecCtx = avcodec_alloc_context3(mCodec); if (!mCodecCtx) { fprintf(stderr, "allocate codec context failed.\n"); return -1; } mFlags |= flags; mChannelNum = av_get_channel_layout_nb_channels(mChannelLayout); mMaxSamplesCount = mCodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ? 10000 : mCodecCtx->frame_size; mFifo = av_audio_fifo_alloc(mSampleFmt, mChannelNum, mMaxSamplesCount); av_samples_alloc_array_and_samples(&mData, NULL, mChannelNum, mMaxSamplesCount, mSampleFmt, 0); return 0; }
AVStream *add_stream(AVStream *instream, enum AVCodecID codec_id, AVCodec **codecDecode) { AVStream *st; AVCodecContext *ctxDecode = NULL; AVCodecContext *decCtx = NULL; int i = 0, videoStream = 0; /* find the encoder */ (*codecDecode) = avcodec_find_encoder(codec_id); if (!(*codecDecode)) { fprintf(stderr, "Could not find encoder for '%s'\n", avcodec_get_name(codec_id)); exit(1); } st = avformat_new_stream(ofmt_ctx, *codecDecode); //add a new stream to media file. if (!st) { fprintf(stderr, "Could not allocate stream\n"); exit(1); } for (i = 0; i<(inFmtCtx)->nb_streams; i++) if ((inFmtCtx)->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } st->id = videoStream; // stream index ctxDecode = st->codec; //save the information to codeccontext instream = inFmtCtx->streams[videoStream]; decCtx = instream->codec; if ((*codecDecode)->type == AVMEDIA_TYPE_VIDEO) { ///* Resolution must be a multiple of two. */ ctxDecode->width = clip_width; ctxDecode->height = clip_height; //ctxDecode->coder_type = AVMEDIA_TYPE_VIDEO;// codec_type : VIDEO ctxDecode->coder_type = AVMEDIA_TYPE_VIDEO; ctxDecode->qmin = 20; // qmin=20 ctxDecode->qmax = 51; // qmax=51 //(*ctxEncode)->flags |= CODEC_FLAG_PSNR; ctxDecode->flags |= CODEC_FLAG_LOOP_FILTER; // flags=+loop filter ctxDecode->sample_aspect_ratio = ctxDecode->sample_aspect_ratio; ctxDecode->pix_fmt = PIX_FMT_YUV420P; //ctxDecode->profile = FF_PROFILE_H264_HIGH_422; //high profile } /* Some formats want stream headers to be separate. */ if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) ctxDecode->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_close(ctxDecode); return st; }
STDMETHODIMP CDecDXVA2::InitDecoder(AVCodecID codec, const CMediaType *pmt) { HRESULT hr = S_OK; DbgLog((LOG_TRACE, 10, L"CDecDXVA2::InitDecoder(): Initializing DXVA2 decoder")); DestroyDecoder(false); m_DisplayDelay = DXVA2_QUEUE_SURFACES; // Reduce display delay for DVD decoding for lower decode latency if (m_pCallback->GetDecodeFlags() & LAV_VIDEO_DEC_FLAG_DVD) m_DisplayDelay /= 2; // If we have a DXVA Decoder, check if its capable // If we don't have one yet, it may be handed to us later, and compat is checked at that point GUID input = GUID_NULL; if (m_pDXVADecoderService) { D3DFORMAT output; hr = FindVideoServiceConversion(codec, &input, &output); if (FAILED(hr)) { DbgLog((LOG_TRACE, 10, L"-> No decoder device available that can decode codec '%S' to NV12", avcodec_get_name(codec))); return E_FAIL; } } m_bFailHWDecode = FALSE; DbgLog((LOG_TRACE, 10, L"-> Creation of DXVA2 decoder successfull, initializing ffmpeg")); hr = CDecAvcodec::InitDecoder(codec, pmt); if (FAILED(hr)) { return hr; } if (((codec == AV_CODEC_ID_H264 || codec == AV_CODEC_ID_MPEG2VIDEO) && m_pAVCtx->pix_fmt != AV_PIX_FMT_YUV420P && m_pAVCtx->pix_fmt != AV_PIX_FMT_YUVJ420P && m_pAVCtx->pix_fmt != AV_PIX_FMT_DXVA2_VLD && m_pAVCtx->pix_fmt != AV_PIX_FMT_NONE) || (codec == AV_CODEC_ID_H264 && m_pAVCtx->profile != FF_PROFILE_UNKNOWN && !H264_CHECK_PROFILE(m_pAVCtx->profile))) { DbgLog((LOG_TRACE, 10, L"-> Incompatible profile detected, falling back to software decoding")); return E_FAIL; } m_dwSurfaceWidth = FFALIGN(m_pAVCtx->coded_width, 16); m_dwSurfaceHeight = FFALIGN(m_pAVCtx->coded_height, 16); if (FAILED(CheckHWCompatConditions(input))) { return E_FAIL; } return S_OK; }