AVRational Codec::GetSupportedFrameRate(const AVRational& frame_rate) const { AVRational fps(frame_rate); while (get()->id == CODEC_ID_MPEG4 && fps.num > (1<<16)-1) { fps.num /= 2; fps.den /= 2; } if (!get()->supported_framerates) return fps; return get()->supported_framerates[av_find_nearest_q_idx(fps, get()->supported_framerates)]; }
const MXFSamplesPerFrame *ff_mxf_get_samples_per_frame(AVFormatContext *s, AVRational time_base) { int idx = av_find_nearest_q_idx(time_base, mxf_time_base); AVRational diff = av_sub_q(time_base, mxf_time_base[idx]); diff.num = abs(diff.num); if (av_cmp_q(diff, (AVRational){1, 1000}) >= 0) return NULL; if (av_cmp_q(time_base, mxf_time_base[idx])) av_log(s, AV_LOG_WARNING, "%d/%d input time base matched %d/%d container time base\n", time_base.num, time_base.den, mxf_spf[idx].time_base.num, mxf_spf[idx].time_base.den); return &mxf_spf[idx]; }
int encode_lavc_alloc_stream(struct encode_lavc_context *ctx, enum AVMediaType mt, AVStream **stream_out, AVCodecContext **codec_out) { AVDictionaryEntry *de; char **p; *stream_out = NULL; *codec_out = NULL; CHECK_FAIL(ctx, -1); if (ctx->header_written) return -1; if (ctx->avc->nb_streams == 0) { // if this stream isn't stream #0, allocate a dummy stream first for // the next call to use if (mt == AVMEDIA_TYPE_VIDEO && ctx->audio_first) { MP_INFO(ctx, "vo-lavc: preallocated audio stream for later use\n"); ctx->ast = avformat_new_stream( ctx->avc, NULL); // this one is AVMEDIA_TYPE_UNKNOWN for now } if (mt == AVMEDIA_TYPE_AUDIO && ctx->video_first) { MP_INFO(ctx, "ao-lavc: preallocated video stream for later use\n"); ctx->vst = avformat_new_stream( ctx->avc, NULL); // this one is AVMEDIA_TYPE_UNKNOWN for now } } // already have a stream of that type (this cannot really happen)? switch (mt) { case AVMEDIA_TYPE_VIDEO: if (ctx->vcc != NULL) return -1; if (ctx->vst == NULL) ctx->vst = avformat_new_stream(ctx->avc, NULL); break; case AVMEDIA_TYPE_AUDIO: if (ctx->acc != NULL) return -1; if (ctx->ast == NULL) ctx->ast = avformat_new_stream(ctx->avc, NULL); break; default: encode_lavc_fail(ctx, "requested invalid stream type\n"); return -1; } if (ctx->timebase.den == 0) { AVRational r; if (ctx->options->fps > 0) r = av_d2q(ctx->options->fps, ctx->options->fps * 1001 + 2); else if (ctx->options->autofps && ctx->vo_fps > 0) { r = av_d2q(ctx->vo_fps, ctx->vo_fps * 1001 + 2); MP_INFO(ctx, "option --ofps not specified " "but --oautofps is active, using guess of %u/%u\n", (unsigned)r.num, (unsigned)r.den); } else { // we want to handle: // 1/25 // 1001/24000 // 1001/30000 // for this we would need 120000fps... // however, mpeg-4 only allows 16bit values // so let's take 1001/30000 out r.num = 24000; r.den = 1; MP_INFO(ctx, "option --ofps not specified " "and fps could not be inferred, using guess of %u/%u\n", (unsigned)r.num, (unsigned)r.den); } if (ctx->vc && ctx->vc->supported_framerates) r = ctx->vc->supported_framerates[av_find_nearest_q_idx(r, ctx->vc->supported_framerates)]; ctx->timebase.num = r.den; ctx->timebase.den = r.num; } switch (mt) { case AVMEDIA_TYPE_VIDEO: if (!ctx->vc) { if (ctx->avc->oformat->video_codec != AV_CODEC_ID_NONE || ctx->options->vcodec) { encode_lavc_fail(ctx, "vo-lavc: encoder not found\n"); } return -1; } ctx->vcc = avcodec_alloc_context3(ctx->vc); // Using codec->time_base is deprecated, but needed for older lavf. ctx->vst->time_base = ctx->timebase; ctx->vcc->time_base = ctx->timebase; ctx->voptions = NULL; if (ctx->options->vopts) for (p = ctx->options->vopts; *p; ++p) if (!set_to_avdictionary(ctx, &ctx->voptions, NULL, *p)) MP_WARN(ctx, "vo-lavc: could not set option %s\n", *p); de = av_dict_get(ctx->voptions, "global_quality", NULL, 0); if (de) set_to_avdictionary(ctx, &ctx->voptions, "flags", "+qscale"); if (ctx->avc->oformat->flags & AVFMT_GLOBALHEADER) set_to_avdictionary(ctx, &ctx->voptions, "flags", "+global_header"); encode_2pass_prepare(ctx, &ctx->voptions, ctx->vst, ctx->vcc, &ctx->twopass_bytebuffer_v, "vo-lavc"); *stream_out = ctx->vst; *codec_out = ctx->vcc; return 0; case AVMEDIA_TYPE_AUDIO: if (!ctx->ac) { if (ctx->avc->oformat->audio_codec != AV_CODEC_ID_NONE || ctx->options->acodec) { encode_lavc_fail(ctx, "ao-lavc: encoder not found\n"); } return -1; } ctx->acc = avcodec_alloc_context3(ctx->ac); // Using codec->time_base is deprecated, but needed for older lavf. ctx->ast->time_base = ctx->timebase; ctx->acc->time_base = ctx->timebase; ctx->aoptions = 0; if (ctx->options->aopts) for (p = ctx->options->aopts; *p; ++p) if (!set_to_avdictionary(ctx, &ctx->aoptions, NULL, *p)) MP_WARN(ctx, "ao-lavc: could not set option %s\n", *p); de = av_dict_get(ctx->aoptions, "global_quality", NULL, 0); if (de) set_to_avdictionary(ctx, &ctx->aoptions, "flags", "+qscale"); if (ctx->avc->oformat->flags & AVFMT_GLOBALHEADER) set_to_avdictionary(ctx, &ctx->aoptions, "flags", "+global_header"); encode_2pass_prepare(ctx, &ctx->aoptions, ctx->ast, ctx->acc, &ctx->twopass_bytebuffer_a, "ao-lavc"); *stream_out = ctx->ast; *codec_out = ctx->acc; return 0; } // Unreachable. return -1; }