struct SwsContext *getSwsContext(int SrcW, int SrcH, enum PixelFormat SrcFormat, int SrcColorSpace, int SrcColorRange, int DstW, int DstH, enum PixelFormat DstFormat, int DstColorSpace, int DstColorRange, int64_t Flags) { struct SwsContext *Context = sws_alloc_context(); // 0 = limited range, 1 = full range int SrcRange = SrcColorRange == AVCOL_RANGE_JPEG; int DstRange = DstColorRange == AVCOL_RANGE_JPEG; Flags |= SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP | SWS_ACCURATE_RND | SWS_BITEXACT; if (!Context) return 0; av_opt_set_int(Context, "sws_flags", Flags, 0); av_opt_set_int(Context, "srcw", SrcW, 0); av_opt_set_int(Context, "srch", SrcH, 0); av_opt_set_int(Context, "dstw", DstW, 0); av_opt_set_int(Context, "dsth", DstH, 0); av_opt_set_int(Context, "src_range", SrcRange, 0); av_opt_set_int(Context, "dst_range", DstRange, 0); av_opt_set_int(Context, "src_format", SrcFormat, 0); av_opt_set_int(Context, "dst_format", DstFormat, 0); sws_setColorspaceDetails(Context, sws_getCoefficients(SrcColorSpace), SrcRange, sws_getCoefficients(DstColorSpace), DstRange, 0, 1 << 16, 1 << 16); if (sws_init_context(Context, 0, 0) < 0) { sws_freeContext(Context); return 0; } return Context; }
void FFMS_AudioSource::SetOutputFormat(FFMS_ResampleOptions const& opt) { if (opt.SampleRate != AP.SampleRate) throw FFMS_Exception(FFMS_ERROR_RESAMPLING, FFMS_ERROR_UNSUPPORTED, "Sample rate changes are currently unsupported."); #ifndef FFMS_RESAMPLING_ENABLED if (opt.SampleFormat != AP.SampleFormat || opt.SampleRate != AP.SampleRate || opt.ChannelLayout != AP.ChannelLayout) throw FFMS_Exception(FFMS_ERROR_RESAMPLING, FFMS_ERROR_UNSUPPORTED, "FFMS was not built with resampling enabled. The only supported conversion is interleaving planar audio."); #endif #ifdef WITH_AVRESAMPLE if (opt.SampleFormat != AP.SampleFormat || opt.ChannelLayout != AP.ChannelLayout) throw FFMS_Exception(FFMS_ERROR_RESAMPLING, FFMS_ERROR_UNSUPPORTED, "FFMS was not built with FFMPEG resampling enabled."); #endif // Cache stores audio in the output format, so clear it and reopen the file Cache.clear(); PacketNumber = 0; ReopenFile(); FlushBuffers(CodecContext); BytesPerSample = av_get_bytes_per_sample(static_cast<AVSampleFormat>(opt.SampleFormat)) * av_get_channel_layout_nb_channels(opt.ChannelLayout); NeedsResample = opt.SampleFormat != (int)CodecContext->sample_fmt || opt.SampleRate != AP.SampleRate || opt.ChannelLayout != AP.ChannelLayout || opt.ForceResample; #ifdef FFMS_RESAMPLING_ENABLED if (!NeedsResample) return; FFResampleContext newContext; SetOptions(opt, newContext, resample_options); av_opt_set_int(newContext, "in_sample_rate", AP.SampleRate, 0); av_opt_set_int(newContext, "in_sample_fmt", CodecContext->sample_fmt, 0); av_opt_set_int(newContext, "in_channel_layout", AP.ChannelLayout, 0); av_opt_set_int(newContext, "out_sample_rate", opt.SampleRate, 0); #ifdef WITH_SWRESAMPLE av_opt_set_channel_layout(newContext, "out_channel_layout", opt.ChannelLayout, 0); av_opt_set_sample_fmt(newContext, "out_sample_fmt", (AVSampleFormat)opt.SampleFormat, 0); #endif if (ffms_open(newContext)) throw FFMS_Exception(FFMS_ERROR_RESAMPLING, FFMS_ERROR_UNKNOWN, "Could not open avresample context"); newContext.swap(ResampleContext); #endif }
value ffmpeg_stream_new_audio(value ctx, value audio_info_) { CAMLparam2(ctx, audio_info_); CAMLlocal1(stream); AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_AAC); stream = caml_alloc_tuple(StreamSize); int ret; Stream_aux_direct_val(stream) = caml_alloc_custom(&streamaux_ops, sizeof(struct StreamAux), 0, 1); Stream_aux_val(stream)->type = Val_int(STREAM_AUDIO); Stream_context_direct_val(stream) = ctx; Stream_aux_val(stream)->avstream = avformat_new_stream(Context_val(ctx)->fmtCtx, codec); Stream_aux_val(stream)->avstream->codec->codec_id = AV_CODEC_ID_AAC; Stream_aux_val(stream)->avstream->codec->sample_rate = Int_val(Field(audio_info_, 0)); Stream_aux_val(stream)->avstream->codec->channels = Int_val(Field(audio_info_, 1)); Stream_aux_val(stream)->avstream->codec->sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; Stream_aux_val(stream)->avstream->codec->channel_layout = AV_CH_LAYOUT_STEREO; //Stream_aux_val(stream)->avstream->codec->channels = av_get_channel_layout_nb_channels(Stream_aux_val(stream)->avstream->codec->channel_layout); if (Context_val(ctx)->fmtCtx->oformat->flags & AVFMT_GLOBALHEADER) { Stream_aux_val(stream)->avstream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } Stream_aux_val(stream)->avstream->time_base = (AVRational) {1, 10000}; AVDictionary* codecOpts = NULL; AVCodecContext* codecCtx = Stream_aux_val(stream)->avstream->codec; caml_enter_blocking_section(); ret = avcodec_open2(codecCtx, codec, &codecOpts); raise_and_leave_blocking_section_if_not(ret >= 0, ExnOpen, ret); caml_leave_blocking_section(); if (Stream_aux_val(stream)->avstream->codec->sample_fmt != AV_SAMPLE_FMT_S16) { Stream_aux_val(stream)->swrCtx = swr_alloc(); assert(Stream_aux_val(stream)->swrCtx); av_opt_set_int (Stream_aux_val(stream)->swrCtx, "in_channel_count", Stream_aux_val(stream)->avstream->codec->channels, 0); av_opt_set_int (Stream_aux_val(stream)->swrCtx, "in_sample_rate", Stream_aux_val(stream)->avstream->codec->sample_rate, 0); av_opt_set_sample_fmt(Stream_aux_val(stream)->swrCtx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_int (Stream_aux_val(stream)->swrCtx, "out_channel_count", Stream_aux_val(stream)->avstream->codec->channels, 0); av_opt_set_int (Stream_aux_val(stream)->swrCtx, "out_sample_rate", Stream_aux_val(stream)->avstream->codec->sample_rate, 0); av_opt_set_sample_fmt(Stream_aux_val(stream)->swrCtx, "out_sample_fmt", Stream_aux_val(stream)->avstream->codec->sample_fmt, 0); } CAMLreturn((value) stream); }
static int insert_trim(int64_t start_time, int64_t duration, AVFilterContext **last_filter, int *pad_idx, const char *filter_name) { AVFilterGraph *graph = (*last_filter)->graph; AVFilterContext *ctx; const AVFilter *trim; enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx); const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim"; int ret = 0; if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE) return 0; trim = avfilter_get_by_name(name); if (!trim) { av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit " "recording time.\n", name); return AVERROR_FILTER_NOT_FOUND; } ctx = avfilter_graph_alloc_filter(graph, trim, filter_name); if (!ctx) return AVERROR(ENOMEM); if (duration != INT64_MAX) { ret = av_opt_set_int(ctx, "durationi", duration, AV_OPT_SEARCH_CHILDREN); } if (ret >= 0 && start_time != AV_NOPTS_VALUE) { ret = av_opt_set_int(ctx, "starti", start_time, AV_OPT_SEARCH_CHILDREN); } if (ret < 0) { av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name); return ret; } ret = avfilter_init_str(ctx, NULL); if (ret < 0) return ret; ret = avfilter_link(*last_filter, *pad_idx, ctx, 0); if (ret < 0) return ret; *last_filter = ctx; *pad_idx = 0; return 0; }
static int ensure_resampler(AudioOutputFile *audio_output_file, AVCodecContext *audio_codec_ctx) { if (!audio_output_file->aresampler) { audio_output_file->aresampler = avresample_alloc_context(); if (!audio_output_file->aresampler) { GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("Cannot allocate the audio resampler. Aborting.\n")); return -1; } av_opt_set_int(audio_output_file->aresampler, "in_channel_layout", DC_AUDIO_CHANNEL_LAYOUT, 0); av_opt_set_int(audio_output_file->aresampler, "out_channel_layout", audio_codec_ctx->channel_layout, 0); av_opt_set_int(audio_output_file->aresampler, "in_sample_fmt", DC_AUDIO_SAMPLE_FORMAT, 0); av_opt_set_int(audio_output_file->aresampler, "out_sample_fmt", audio_codec_ctx->sample_fmt, 0); av_opt_set_int(audio_output_file->aresampler, "in_sample_rate", DC_AUDIO_SAMPLE_RATE, 0); av_opt_set_int(audio_output_file->aresampler, "out_sample_rate", audio_codec_ctx->sample_rate, 0); av_opt_set_int(audio_output_file->aresampler, "in_channels", DC_AUDIO_NUM_CHANNELS, 0); av_opt_set_int(audio_output_file->aresampler, "out_channels", audio_codec_ctx->channels, 0); if (avresample_open(audio_output_file->aresampler)) { GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("Could not open the audio resampler. Aborting.\n")); return -1; } } return 0; }
bool FFMPEGer::open_audio(AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg){ AVCodecContext *c; int nb_samples; int ret; AVDictionary *opt = NULL; c = ost->st->codec; /* open it */ av_dict_copy(&opt, opt_arg, 0); ret = avcodec_open2(c, codec, &opt); av_dict_free(&opt); if (ret < 0) { ALOGE("Could not open audio codec: %s", av_err2str(ret)); return false; } if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) nb_samples = 10000; else nb_samples = c->frame_size; ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout, c->sample_rate, nb_samples); ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout, c->sample_rate, nb_samples); /* create resampler context */ ost->swr_ctx = swr_alloc(); if (!ost->swr_ctx) { ALOGE("Could not allocate resampler context"); return false; } /* set options */ av_opt_set_int (ost->swr_ctx, "in_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "in_sample_rate", c->sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_int (ost->swr_ctx, "out_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0); /* initialize the resampling context */ ret = swr_init(ost->swr_ctx); if (ret < 0){ ALOGE("Failed to initialize the resampling context"); return false; } return true; }
void setOptionsToFFmpegObj(const QVariant& opt, void* obj) { if (!opt.isValid()) return; AVClass *c = obj ? *(AVClass**)obj : 0; if (c) qDebug() << QStringLiteral("%1.%2 options:").arg(QLatin1String(c->class_name)).arg(QLatin1String(c->item_name(obj))); else qDebug() << "options:"; if (opt.type() == QVariant::Map) { QVariantMap options(opt.toMap()); if (options.isEmpty()) return; QMapIterator<QString, QVariant> i(options); while (i.hasNext()) { i.next(); const QVariant::Type vt = i.value().type(); if (vt == QVariant::Map) continue; const QByteArray key(i.key().toUtf8()); qDebug("%s=>%s", i.key().toUtf8().constData(), i.value().toByteArray().constData()); if (vt == QVariant::Int || vt == QVariant::UInt || vt == QVariant::Bool) { // QVariant.toByteArray(): "true" or "false", can not recognized by avcodec av_opt_set_int(obj, key.constData(), i.value().toInt(), AV_OPT_SEARCH_CHILDREN); } else if (vt == QVariant::LongLong || vt == QVariant::ULongLong) { av_opt_set_int(obj, key.constData(), i.value().toLongLong(), AV_OPT_SEARCH_CHILDREN); } else if (vt == QVariant::Double) { av_opt_set_double(obj, key.constData(), i.value().toDouble(), AV_OPT_SEARCH_CHILDREN); } } return; } QVariantHash options(opt.toHash()); if (options.isEmpty()) return; QHashIterator<QString, QVariant> i(options); while (i.hasNext()) { i.next(); const QVariant::Type vt = i.value().type(); if (vt == QVariant::Hash) continue; const QByteArray key(i.key().toUtf8()); qDebug("%s=>%s", i.key().toUtf8().constData(), i.value().toByteArray().constData()); if (vt == QVariant::Int || vt == QVariant::UInt || vt == QVariant::Bool) { av_opt_set_int(obj, key.constData(), i.value().toInt(), AV_OPT_SEARCH_CHILDREN); } else if (vt == QVariant::LongLong || vt == QVariant::ULongLong) { av_opt_set_int(obj, key.constData(), i.value().toLongLong(), AV_OPT_SEARCH_CHILDREN); } } }
static int ensure_resampler(AudioInputFile *audio_input_file, int sample_rate, int num_channels, u64 channel_layout, enum AVSampleFormat sample_format) { if (!audio_input_file->aresampler) { audio_input_file->aresampler = avresample_alloc_context(); if (!audio_input_file->aresampler) { GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("Cannot allocate the audio resampler. Aborting.\n")); return -1; } av_opt_set_int(audio_input_file->aresampler, "in_channel_layout", channel_layout, 0); av_opt_set_int(audio_input_file->aresampler, "out_channel_layout", DC_AUDIO_CHANNEL_LAYOUT, 0); av_opt_set_int(audio_input_file->aresampler, "in_sample_fmt", sample_format, 0); av_opt_set_int(audio_input_file->aresampler, "out_sample_fmt", DC_AUDIO_SAMPLE_FORMAT, 0); av_opt_set_int(audio_input_file->aresampler, "in_sample_rate", sample_rate, 0); av_opt_set_int(audio_input_file->aresampler, "out_sample_rate", DC_AUDIO_SAMPLE_RATE, 0); av_opt_set_int(audio_input_file->aresampler, "in_channels", num_channels, 0); av_opt_set_int(audio_input_file->aresampler, "out_channels", DC_AUDIO_NUM_CHANNELS, 0); if (avresample_open(audio_input_file->aresampler)) { GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("Could not open the audio resampler. Aborting.\n")); return -1; } } return 0; }
static void OpenAudio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg, int sample_rate) { AVCodecContext *c = NULL; int nb_samples = 0; int ret = 0; AVDictionary *opt = NULL; c = ost->st->codec; // コーデックを初期化 av_dict_copy(&opt, opt_arg, 0); ret = avcodec_open2(c, codec, &opt); av_dict_free(&opt); if (ret < 0) { fprintf(stderr, "Could not open audio codec: %s\n", MakeErrorString(ret)); return; } if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) nb_samples = 10000; else nb_samples = c->frame_size; ost->frame = AllocAudioFrame(c->sample_fmt, c->channel_layout, c->sample_rate, nb_samples); ost->tmp_frame = AllocAudioFrame(AV_SAMPLE_FMT_S16, c->channel_layout, sample_rate, nb_samples/(c->sample_rate/sample_rate)); // サンプル変換部 ost->swr_ctx = swr_alloc(); if (!ost->swr_ctx) { fprintf(stderr, "Could not allocate resampler context\n"); return; } // 音声フォーマットの設定 av_opt_set_int (ost->swr_ctx, "in_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "in_sample_rate", sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_int (ost->swr_ctx, "out_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0); // サンプル変換部を初期化 if ((ret = swr_init(ost->swr_ctx)) < 0) { fprintf(stderr, "Failed to initialize the resampling context\n"); return; } }
/** * gst_ffmpegmux_setcaps * @pad: #GstPad * @caps: New caps. * * Set caps to pad. * * Returns: #TRUE on success. */ static gboolean gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps) { GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (gst_pad_get_parent (pad)); GstFFMpegMuxPad *collect_pad; AVStream *st; collect_pad = (GstFFMpegMuxPad *) gst_pad_get_element_private (pad); st = ffmpegmux->context->streams[collect_pad->padnum]; av_opt_set_int (ffmpegmux->context, "preload", ffmpegmux->preload, 0); ffmpegmux->context->max_delay = ffmpegmux->max_delay; /* for the format-specific guesses, we'll go to * our famous codec mapper */ if (gst_ffmpeg_caps_to_codecid (caps, st->codec) == AV_CODEC_ID_NONE) goto not_accepted; /* copy over the aspect ratios, ffmpeg expects the stream aspect to match the * codec aspect. */ st->sample_aspect_ratio = st->codec->sample_aspect_ratio; GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps); return TRUE; /* ERRORS */ not_accepted: { GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps); return FALSE; } }
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { AResampleContext *aresample = ctx->priv; int ret = 0; char *argd = av_strdup(args); aresample->next_pts = AV_NOPTS_VALUE; aresample->swr = swr_alloc(); if (!aresample->swr) return AVERROR(ENOMEM); if (args) { char *ptr=argd, *token; while(token = av_strtok(ptr, ":", &ptr)) { char *value; av_strtok(token, "=", &value); if(value) { if((ret=av_opt_set(aresample->swr, token, value, 0)) < 0) goto end; } else { int out_rate; if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0) goto end; if((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0) goto end; } } } end: av_free(argd); return ret; }
static int open_input_file(const char *filename) { int ret; AVCodec *dec; if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); return ret; } if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); return ret; } /* select the video stream */ ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n"); return ret; } video_stream_index = ret; dec_ctx = fmt_ctx->streams[video_stream_index]->codec; av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0); /* init the video decoder */ if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n"); return ret; } return 0; }
void VideoDecoderFFmpeg::setSkipLoopFilter(DiscardType value) { DPTR_D(VideoDecoderFFmpeg); d.skip_loop_filter = value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "skip_loop_filter", (int64_t)value, 0); }
static int ffdec_init(struct decoder_ctx *ctx, int hw) { int ret; AVCodecContext *avctx = ctx->avctx; AVCodec *dec = avcodec_find_decoder(avctx->codec_id); TRACE(ctx, "initialize context"); av_opt_set_int(avctx, "refcounted_frames", 1, 0); if (hw && avctx->codec_id == AV_CODEC_ID_H264) { AVCodec *codec = avcodec_find_decoder_by_name("h264_mediacodec"); if (!codec) return AVERROR_DECODER_NOT_FOUND; dec = codec; } TRACE(ctx, "codec open"); ret = avcodec_open2(avctx, dec, NULL); if (ret < 0) return ret; ctx->avctx = avctx; return ret; }
void VideoDecoderFFmpeg::setBugFlags(BugFlags value) { DPTR_D(VideoDecoderFFmpeg); d.bug = (int)value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "bug", (int64_t)value, 0); }
void VideoDecoderFFmpeg::setThreadFlags(ThreadFlags value) { DPTR_D(VideoDecoderFFmpeg); d.thread_type = (int)value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "thread_type", (int64_t)value, 0); }
void VideoDecoderFFmpeg::setMotionVectorVisFlags(MotionVectorVisFlags value) { DPTR_D(VideoDecoderFFmpeg); d.debug_mv = (int)value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "vismv", (int64_t)value, 0); }
void VideoDecoderFFmpeg::setSkipFrame(DiscardType value) { DPTR_D(VideoDecoderFFmpeg); d.skip_frame = (int)value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "skip_frame", (int64_t)value, 0); }
void VideoDecoderFFmpeg::setThreads(int value) { DPTR_D(VideoDecoderFFmpeg); d.threads = value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "threads", (int64_t)value, 0); }
void VideoDecoderFFmpeg::setStrict(StrictType value) { DPTR_D(VideoDecoderFFmpeg); d.strict = (int)value; if (d.codec_ctx) av_opt_set_int(d.codec_ctx, "strict", int64_t(value), 0); }
static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) { AVCodecContext *c; int nb_samples; int ret; AVDictionary *opt = NULL; c = ost->st->codec; /* open it */ av_dict_copy(&opt, opt_arg, 0); ret = avcodec_open2(c, codec, &opt); av_dict_free(&opt); if (ret < 0) { fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret)); exit(1); } /* init signal generator */ ost->t = 0; ost->tincr = 2 * M_PI * 110.0 / c->sample_rate; /* increment frequency by 110 Hz per second */ ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) nb_samples = 10000; else nb_samples = c->frame_size; ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout, c->sample_rate, nb_samples); ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout, c->sample_rate, nb_samples); /* create resampler context */ ost->swr_ctx = swr_alloc(); if (!ost->swr_ctx) { fprintf(stderr, "Could not allocate resampler context\n"); exit(1); } /* set options */ av_opt_set_int (ost->swr_ctx, "in_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "in_sample_rate", c->sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_int (ost->swr_ctx, "out_channel_count", c->channels, 0); av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0); av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0); /* initialize the resampling context */ if ((ret = swr_init(ost->swr_ctx)) < 0) { fprintf(stderr, "Failed to initialize the resampling context\n"); exit(1); } }
void Option::setBool( const bool value ) { int error = av_opt_set_int( _avContext, getName().c_str(), value, AV_OPT_SEARCH_CHILDREN ); std::ostringstream os; os << ( value ? "true" : "false" ); checkFFmpegSetOption( error, os.str() ); }
void Option::setInt( const int value ) { int error = av_opt_set_int( _avContext, getName().c_str(), value, AV_OPT_SEARCH_CHILDREN ); std::ostringstream os; os << value; checkFFmpegSetOption( error, os.str() ); }
int frame_puller_open_audio(frame_puller **o_fp, const char *path, int output_sample_rate) { *o_fp = NULL; int ret; frame_puller *fp; if ((ret = _frame_puller_new(&fp, path)) < 0) return ret; fp->type = FRAME_PULLER_AUDIO; if ((ret = _frame_puller_init(fp, AVMEDIA_TYPE_AUDIO)) < 0) return ret; fp->output_sample_rate = output_sample_rate > 0 ? output_sample_rate : fp->codec_ctx->sample_rate; fp->sample_scale_rate = (double)fp->output_sample_rate / (double)fp->codec_ctx->sample_rate; // Initialize the libswresample context for audio resampling. // > Create the buffer for the converted frame to store data fp->frame = av_frame_alloc(); fp->frame->format = AV_SAMPLE_FMT_S16P; fp->frame->channel_layout = fp->codec_ctx->channel_layout; fp->frame->sample_rate = fp->output_sample_rate; if ((fp->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) || !strcmp(fp->codec->name, "pcm_mulaw")) fp->frame->nb_samples = 4096; else fp->frame->nb_samples = fp->sample_scale_rate * fp->codec_ctx->frame_size; av_log(NULL, AV_LOG_INFO, "frame_puller: number of samples per frame = %d\n", fp->frame->nb_samples); if ((ret = av_frame_get_buffer(fp->frame, 0)) < 0) return ret; // > Create the SwrContext fp->libsw.swr_ctx = swr_alloc(); if (!fp->libsw.swr_ctx) { av_log(NULL, AV_LOG_ERROR, "frame_puller: Cannot initialize audio resampling library" "(possibly caused by insufficient memory)\n"); return AVERROR_UNKNOWN; } // > Provide options for the SwrContext av_opt_set_channel_layout(fp->libsw.swr_ctx, "in_channel_layout", fp->codec_ctx->channel_layout, 0); av_opt_set_channel_layout(fp->libsw.swr_ctx, "out_channel_layout", fp->codec_ctx->channel_layout, 0); av_opt_set_int(fp->libsw.swr_ctx, "in_sample_rate", fp->codec_ctx->sample_rate, 0); av_opt_set_int(fp->libsw.swr_ctx, "out_sample_rate", fp->output_sample_rate, 0); av_opt_set_sample_fmt(fp->libsw.swr_ctx, "in_sample_fmt", fp->codec_ctx->sample_fmt, 0); av_opt_set_sample_fmt(fp->libsw.swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0); // > Fully initialize the SwrContext if ((ret = swr_init(fp->libsw.swr_ctx)) < 0) return ret; // For use in @ref frame_puller_last_time. fp->frame->pts = -233333; *o_fp = fp; return 0; }
int update_resampler_configuration( AVAudioResampleContext *avr, uint64_t out_channel_layout, int out_sample_rate, enum AVSampleFormat out_sample_fmt, uint64_t in_channel_layout, int in_sample_rate, enum AVSampleFormat in_sample_fmt, int *input_planes, int *input_block_align ) { /* Reopen the resampler. */ avresample_close( avr ); av_opt_set_int( avr, "in_channel_layout", in_channel_layout, 0 ); av_opt_set_int( avr, "in_sample_fmt", in_sample_fmt, 0 ); av_opt_set_int( avr, "in_sample_rate", in_sample_rate, 0 ); av_opt_set_int( avr, "out_channel_layout", out_channel_layout, 0 ); av_opt_set_int( avr, "out_sample_fmt", out_sample_fmt, 0 ); av_opt_set_int( avr, "out_sample_rate", out_sample_rate, 0 ); av_opt_set_int( avr, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0 ); if( avresample_open( avr ) < 0 ) return -1; /* Set up the number of planes and the block alignment of input audio frame. */ int input_channels = av_get_channel_layout_nb_channels( in_channel_layout ); if( av_sample_fmt_is_planar( in_sample_fmt ) ) { *input_planes = input_channels; *input_block_align = av_get_bytes_per_sample( in_sample_fmt ); } else { *input_planes = 1; *input_block_align = av_get_bytes_per_sample( in_sample_fmt ) * input_channels; } return 0; }
static int config_props(AVFilterLink *link) { ASyncContext *s = link->src->priv; int ret; s->min_delta = s->min_delta_sec * link->sample_rate; link->time_base = (AVRational){1, link->sample_rate}; s->avr = avresample_alloc_context(); if (!s->avr) return AVERROR(ENOMEM); av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0); av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0); av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0); av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0); av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0); av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0); if (s->resample) av_opt_set_int(s->avr, "force_resampling", 1, 0); if ((ret = avresample_open(s->avr)) < 0) return ret; return 0; }
BOOL CVideoLivRecord::open_audio(AVStream *st, AVCodec* codec, AVDictionary* opt) { AVCodecContext * avcc = st->codec; int nb_samples; int ret; AVDictionary* opt_dst = NULL;//必须初始化为空。 av_dict_copy(&opt_dst, opt, 0); ret = avcodec_open2(avcc, codec, &opt_dst); av_dict_free(&opt_dst); if (ret < 0){ log("[CVideoLivRecord::open_audio] -- avcodec_open2() error"); return FALSE; } m_t = 0; m_tincr = 2 * M_PI * 110.0 / avcc->sample_rate; m_tincr2 = 2 * M_PI * 110.0 / avcc->sample_rate / avcc->sample_rate; if (avcc->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE){ nb_samples = 10000; } else { nb_samples = avcc->frame_size; } m_pAudioFrame = alloc_audio_frame(avcc->sample_fmt, avcc->channel_layout, avcc->sample_rate, nb_samples); m_pAudioBkFrame = alloc_audio_frame(AV_SAMPLE_FMT_S16, avcc->channel_layout, avcc->sample_rate, nb_samples); m_pAudioSwrctx = swr_alloc(); if (!m_pAudioSwrctx){ log("[CVideoLivRecord::open_audio] -- swr_alloc() error"); return FALSE; } av_opt_set_int (m_pAudioSwrctx, "in_channel_count", avcc->channels, 0); av_opt_set_int (m_pAudioSwrctx, "in_sample_rate", avcc->sample_rate, 0); av_opt_set_sample_fmt(m_pAudioSwrctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_int (m_pAudioSwrctx, "out_channel_count", avcc->channels, 0); av_opt_set_int (m_pAudioSwrctx, "out_sample_rate", avcc->sample_rate, 0); av_opt_set_sample_fmt(m_pAudioSwrctx, "out_sample_fmt", avcc->sample_fmt, 0); if (swr_init(m_pAudioSwrctx) < 0){ log("[CVideoLivRecord::open_audio] -- swr_init() error"); return FALSE; } return TRUE; }
void AudioResampleImpl::initResampler() { m_resampler.reset(swr_alloc(), [](SwrContext * context){ swr_free(&context); }); assert(m_from.isValid()); assert(m_to.isValid()); av_opt_set_int(m_resampler.get(), "in_channel_layout", av_get_default_channel_layout(m_from.channelCount()), 0); /// @todo Дополнить раскладкой AudioFormat av_opt_set_int(m_resampler.get(), "out_channel_layout", av_get_default_channel_layout(m_to.channelCount()), 0); av_opt_set_int(m_resampler.get(), "in_sample_rate", m_from.sampleRate(), 0); av_opt_set_int(m_resampler.get(), "out_sample_rate", m_to.sampleRate(), 0); av_opt_set_sample_fmt(m_resampler.get(), "in_sample_fmt", m_from.sampleFormat(), 0); av_opt_set_sample_fmt(m_resampler.get(), "out_sample_fmt", m_to.sampleFormat(), 0); /// Non planar int res = swr_init(m_resampler.get()); if (res != 0) throw FFException(res); }
STDMETHODIMP CBDDemuxer::OpenMVCExtensionDemuxer(int playItem) { int ret; MPLS_PL *pl = bd_get_title_mpls(m_pBD); if (!pl) return E_FAIL; const char *clip_id = pl->ext_sub_path[m_MVCExtensionSubPathIndex].sub_play_item[playItem].clip->clip_id; char *fileName = av_asprintf("%sBDMV\\STREAM\\%s.m2ts", m_cBDRootPath, clip_id); DbgLog((LOG_TRACE, 10, "CBDDemuxer::OpenMVCExtensionDemuxer(): Opening MVC extension stream at %s", fileName)); // Try to open the MVC stream AVInputFormat *format = av_find_input_format("mpegts"); ret = avformat_open_input(&m_MVCFormatContext, fileName, format, nullptr); if (ret < 0) { DbgLog((LOG_TRACE, 10, "-> Opening MVC demuxing context failed (%d)", ret)); goto fail; } av_opt_set_int(m_MVCFormatContext, "correct_ts_overflow", 0, 0); m_MVCFormatContext->flags |= AVFMT_FLAG_KEEP_SIDE_DATA; // Find the streams ret = avformat_find_stream_info(m_MVCFormatContext, nullptr); if (ret < 0) { DbgLog((LOG_TRACE, 10, "-> avformat_find_stream_info failed (%d)", ret)); goto fail; } // Find and select our MVC stream DbgLog((LOG_TRACE, 10, "-> MVC m2ts has %d streams", m_MVCFormatContext->nb_streams)); for (unsigned i = 0; i < m_MVCFormatContext->nb_streams; i++) { if (m_MVCFormatContext->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264_MVC && m_MVCFormatContext->streams[i]->codecpar->extradata_size > 0) { m_MVCStreamIndex = i; break; } else { m_MVCFormatContext->streams[i]->discard = AVDISCARD_ALL; } } if (m_MVCStreamIndex < 0) { DbgLog((LOG_TRACE, 10, "-> MVC Stream not found")); goto fail; } m_MVCExtensionClip = playItem; return S_OK; fail: CloseMVCExtensionDemuxer(); return E_FAIL; }
/** * Set the maximum HD rate. * If playing DTS-HD content, setting a HD rate of 0 will only use the DTS-Core * and the HD stream be stripped out before encoding * Input: rate = maximum HD rate in Hz */ bool SPDIFEncoder::SetMaxHDRate(int rate) { if (!m_oc) { return false; } av_opt_set_int(m_oc->priv_data, "dtshd_rate", rate, 0); return true; }