1 Star 0 Fork 0

pedoc/FFmpeg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
compat
doc
ffbuild
fftools
libavcodec
libavdevice
libavfilter
aarch64
cuda
dnn
metal
opencl
tests
x86
.gitignore
Makefile
aeval.c
af_acontrast.c
af_acopy.c
af_acrossover.c
af_acrusher.c
af_adeclick.c
af_adecorrelate.c
af_adelay.c
af_adenorm.c
af_aderivative.c
af_adynamicequalizer.c
af_adynamicsmooth.c
af_aecho.c
af_aemphasis.c
af_aexciter.c
af_afade.c
af_afftdn.c
af_afftfilt.c
af_afir.c
af_afir.h
af_afirdsp.h
af_aformat.c
af_afreqshift.c
af_afwtdn.c
af_agate.c
af_aiir.c
af_alimiter.c
af_amerge.c
af_amix.c
af_amultiply.c
af_anequalizer.c
af_anlmdn.c
af_anlmdndsp.h
af_anlms.c
af_anull.c
af_apad.c
af_aphaser.c
af_apsyclip.c
af_apulsator.c
af_aresample.c
af_arnndn.c
af_asdr.c
af_asetnsamples.c
af_asetrate.c
af_ashowinfo.c
af_asoftclip.c
af_aspectralstats.c
af_asr.c
af_astats.c
af_asubboost.c
af_asupercut.c
af_atempo.c
af_atilt.c
af_axcorrelate.c
af_biquads.c
af_bs2b.c
af_channelmap.c
af_channelsplit.c
af_chorus.c
af_compand.c
af_compensationdelay.c
af_crossfeed.c
af_crystalizer.c
af_dcshift.c
af_deesser.c
af_dialoguenhance.c
af_drmeter.c
af_dynaudnorm.c
af_earwax.c
af_extrastereo.c
af_firequalizer.c
af_flanger.c
af_haas.c
af_hdcd.c
af_headphone.c
af_join.c
af_ladspa.c
af_loudnorm.c
af_lv2.c
af_mcompand.c
af_pan.c
af_replaygain.c
af_rubberband.c
af_sidechaincompress.c
af_silencedetect.c
af_silenceremove.c
af_sofalizer.c
af_speechnorm.c
af_stereotools.c
af_stereowiden.c
af_superequalizer.c
af_surround.c
af_tremolo.c
af_vibrato.c
af_virtualbass.c
af_volume.c
af_volume.h
af_volumedetect.c
afir_template.c
allfilters.c
asink_anullsink.c
asrc_afirsrc.c
asrc_anoisesrc.c
asrc_anullsrc.c
asrc_flite.c
asrc_hilbert.c
asrc_sinc.c
asrc_sine.c
atadenoise.h
audio.c
audio.h
avf_abitscope.c
avf_ahistogram.c
avf_aphasemeter.c
avf_avectorscope.c
avf_concat.c
avf_showcqt.c
avf_showcqt.h
avf_showfreqs.c
avf_showspatial.c
avf_showspectrum.c
avf_showvolume.c
avf_showwaves.c
avfilter.c
avfilter.h
avfiltergraph.c
avfilterres.rc
bbox.c
bbox.h
blend.h
blend_modes.c
boxblur.c
boxblur.h
bufferqueue.h
buffersink.c
buffersink.h
buffersrc.c
buffersrc.h
bwdif.h
colorchannelmixer_template.c
colorspace.c
colorspace.h
colorspacedsp.c
colorspacedsp.h
colorspacedsp_template.c
colorspacedsp_yuv2yuv_template.c
convolution.h
deshake.h
dnn_filter_common.c
dnn_filter_common.h
dnn_interface.h
drawutils.c
drawutils.h
ebur128.c
ebur128.h
edge_common.c
edge_common.h
f_bench.c
f_cue.c
f_drawgraph.c
f_ebur128.c
f_graphmonitor.c
f_interleave.c
f_latency.c
f_loop.c
f_metadata.c
f_perms.c
f_realtime.c
f_reverse.c
f_segment.c
f_select.c
f_sendcmd.c
f_sidedata.c
f_streamselect.c
f_zmq.c
fflcms2.c
fflcms2.h
fifo.c
file_open.c
filters.h
formats.c
formats.h
framepool.c
framepool.h
framequeue.c
framequeue.h
framerate.h
framesync.c
framesync.h
gblur.h
generate_wave_table.c
generate_wave_table.h
gradfun.h
graphdump.c
graphparser.c
hermite.h
hflip.h
internal.h
lavfutils.c
lavfutils.h
libavfilter.v
limiter.h
log2_tab.c
lswsutils.c
lswsutils.h
lut3d.h
maskedclamp.h
maskedmerge.h
median.h
median_template.c
motion_estimation.c
motion_estimation.h
opencl.c
opencl.h
opencl_source.h
phase_template.c
preserve_color.h
psnr.h
pthread.c
qp_table.c
qp_table.h
qsvvpp.c
qsvvpp.h
removegrain.h
scale_eval.c
scale_eval.h
scene_sad.c
scene_sad.h
setpts.c
settb.c
signature.h
signature_lookup.c
split.c
src_avsynctest.c
src_movie.c
ssim.h
stereo3d.h
thread.h
threshold.h
tinterlace.h
transform.c
transform.h
transpose.h
trim.c
unsharp.h
v360.h
vaapi_vpp.c
vaapi_vpp.h
vaf_spectrumsynth.c
version.c
version.h
version_major.h
vf_addroi.c
vf_alphamerge.c
vf_amplify.c
vf_aspect.c
vf_atadenoise.c
vf_avgblur.c
vf_avgblur_opencl.c
vf_avgblur_vulkan.c
vf_bbox.c
vf_bilateral.c
vf_bitplanenoise.c
vf_blackdetect.c
vf_blackframe.c
vf_blend.c
vf_blend_init.h
vf_blend_vulkan.c
vf_blockdetect.c
vf_blurdetect.c
vf_bm3d.c
vf_boxblur.c
vf_bwdif.c
vf_cas.c
vf_chromaber_vulkan.c
vf_chromakey.c
vf_chromakey_cuda.c
vf_chromakey_cuda.cu
vf_chromanr.c
vf_chromashift.c
vf_ciescope.c
vf_codecview.c
vf_colorbalance.c
vf_colorchannelmixer.c
vf_colorconstancy.c
vf_colorcontrast.c
vf_colorcorrect.c
vf_colorize.c
vf_colorkey.c
vf_colorkey_opencl.c
vf_colorlevels.c
vf_colormap.c
vf_colormatrix.c
vf_colorspace.c
vf_colortemperature.c
vf_convolution.c
vf_convolution_opencl.c
vf_convolve.c
vf_copy.c
vf_coreimage.m
vf_cover_rect.c
vf_crop.c
vf_cropdetect.c
vf_curves.c
vf_datascope.c
vf_dblur.c
vf_dctdnoiz.c
vf_deband.c
vf_deblock.c
vf_decimate.c
vf_dedot.c
vf_deflicker.c
vf_deinterlace_qsv.c
vf_deinterlace_vaapi.c
vf_dejudder.c
vf_delogo.c
vf_derain.c
vf_deshake.c
vf_deshake_opencl.c
vf_despill.c
vf_detelecine.c
vf_displace.c
vf_dnn_classify.c
vf_dnn_detect.c
vf_dnn_processing.c
vf_drawbox.c
vf_drawtext.c
vf_edgedetect.c
vf_elbg.c
vf_entropy.c
vf_epx.c
vf_eq.c
vf_eq.h
vf_estdif.c
vf_exposure.c
vf_extractplanes.c
vf_fade.c
vf_feedback.c
vf_fftdnoiz.c
vf_fftfilt.c
vf_field.c
vf_fieldhint.c
vf_fieldmatch.c
vf_fieldorder.c
vf_fillborders.c
vf_find_rect.c
vf_flip_vulkan.c
vf_floodfill.c
vf_format.c
vf_fps.c
vf_framepack.c
vf_framerate.c
vf_framestep.c
vf_freezedetect.c
vf_freezeframes.c
vf_frei0r.c
vf_fspp.c
vf_fspp.h
vf_gblur.c
vf_gblur_init.h
vf_gblur_vulkan.c
vf_geq.c
vf_gradfun.c
vf_grayworld.c
vf_guided.c
vf_hflip.c
vf_hflip_init.h
vf_histeq.c
vf_histogram.c
vf_hqdn3d.c
vf_hqdn3d.h
vf_hqx.c
vf_hsvkey.c
vf_hue.c
vf_huesaturation.c
vf_hwdownload.c
vf_hwmap.c
vf_hwupload.c
vf_hwupload_cuda.c
vf_hysteresis.c
vf_iccdetect.c
vf_iccgen.c
vf_identity.c
vf_idet.c
vf_idet.h
vf_il.c
vf_kerndeint.c
vf_lagfun.c
vf_lenscorrection.c
vf_lensfun.c
vf_libopencv.c
vf_libplacebo.c
vf_libvmaf.c
vf_limitdiff.c
vf_limiter.c
vf_lumakey.c
vf_lut.c
vf_lut2.c
vf_lut3d.c
vf_maskedclamp.c
vf_maskedmerge.c
vf_maskedminmax.c
vf_maskedthreshold.c
vf_maskfun.c
vf_mcdeint.c
vf_median.c
vf_mergeplanes.c
vf_mestimate.c
vf_midequalizer.c
vf_minterpolate.c
vf_misc_vaapi.c
vf_mix.c
vf_monochrome.c
vf_morpho.c
vf_mpdecimate.c
vf_multiply.c
vf_negate.c
vf_neighbor.c
vf_neighbor_opencl.c
vf_nlmeans.c
vf_nlmeans.h
vf_nlmeans_init.h
vf_nlmeans_opencl.c
vf_nnedi.c
vf_noise.c
vf_noise.h
vf_normalize.c
vf_null.c
vf_ocr.c
vf_overlay.c
vf_overlay.h
vf_overlay_cuda.c
vf_overlay_cuda.cu
vf_overlay_opencl.c
vf_overlay_qsv.c
vf_overlay_vaapi.c
vf_overlay_vulkan.c
vf_owdenoise.c
vf_pad.c
vf_pad_opencl.c
vf_palettegen.c
vf_paletteuse.c
vf_perspective.c
vf_phase.c
vf_photosensitivity.c
vf_pixdesctest.c
vf_pixelize.c
vf_pp.c
vf_pp7.c
vf_pp7.h
vf_premultiply.c
vf_procamp_vaapi.c
vf_program_opencl.c
vf_pseudocolor.c
vf_psnr.c
vf_pullup.c
vf_pullup.h
vf_qp.c
vf_random.c
vf_readeia608.c
vf_readvitc.c
vf_remap.c
vf_remap_opencl.c
vf_removegrain.c
vf_removelogo.c
vf_repeatfields.c
vf_rotate.c
vf_sab.c
vf_scale.c
vf_scale_cuda.c
vf_scale_cuda.cu
vf_scale_cuda.h
vf_scale_npp.c
vf_scale_qsv.c
vf_scale_vaapi.c
vf_scale_vulkan.c
vf_scdet.c
vf_scroll.c
vf_selectivecolor.c
vf_separatefields.c
vf_setparams.c
vf_sharpen_npp.c
vf_shear.c
vf_showinfo.c
vf_showpalette.c
vf_shuffleframes.c
vf_shufflepixels.c
vf_shuffleplanes.c
vf_signalstats.c
vf_signature.c
vf_siti.c
vf_smartblur.c
vf_spp.c
vf_spp.h
vf_sr.c
vf_ssim.c
vf_stack.c
vf_stereo3d.c
vf_subtitles.c
vf_super2xsai.c
vf_swaprect.c
vf_swapuv.c
vf_telecine.c
vf_threshold.c
vf_threshold_init.h
vf_thumbnail.c
vf_thumbnail_cuda.c
vf_thumbnail_cuda.cu
vf_tile.c
vf_tinterlace.c
vf_tmidequalizer.c
vf_tonemap.c
vf_tonemap_opencl.c
vf_tonemap_vaapi.c
vf_tpad.c
vf_transpose.c
vf_transpose_npp.c
vf_transpose_opencl.c
vf_transpose_vaapi.c
vf_transpose_vulkan.c
vf_unsharp.c
vf_unsharp_opencl.c
vf_untile.c
vf_uspp.c
vf_v360.c
vf_vaguedenoiser.c
vf_varblur.c
vf_vectorscope.c
vf_vflip.c
vf_vfrdet.c
vf_vibrance.c
vf_vidstabdetect.c
vf_vidstabtransform.c
vf_vif.c
vf_vignette.c
vf_vmafmotion.c
vf_vpp_qsv.c
vf_w3fdif.c
vf_waveform.c
vf_weave.c
vf_xbr.c
vf_xfade.c
vf_xfade_opencl.c
vf_xmedian.c
vf_yadif.c
vf_yadif_cuda.c
vf_yadif_cuda.cu
vf_yadif_videotoolbox.m
vf_yaepblur.c
vf_zoompan.c
vf_zscale.c
video.c
video.h
vidstabutils.c
vidstabutils.h
vmaf_motion.h
vsink_nullsink.c
vsrc_cellauto.c
vsrc_gradients.c
vsrc_life.c
vsrc_mandelbrot.c
vsrc_mptestsrc.c
vsrc_sierpinski.c
vsrc_testsrc.c
vulkan.c
vulkan.h
vulkan_filter.c
vulkan_filter.h
w3fdif.h
window_func.h
yadif.h
yadif_common.c
libavformat
libavutil
libpostproc
libswresample
libswscale
presets
tests
tools
.gitattributes
.gitignore
.mailmap
.travis.yml
CONTRIBUTING.md
COPYING.GPLv2
COPYING.GPLv3
COPYING.LGPLv2.1
COPYING.LGPLv3
CREDITS
Changelog
INSTALL.md
LICENSE.md
MAINTAINERS
Makefile
README.md
RELEASE
RELEASE_NOTES
configure
克隆/下载
vf_transpose_npp.c 15.60 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <nppi.h>
#include <stdio.h>
#include <string.h>
#include "libavutil/common.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_cuda_internal.h"
#include "libavutil/cuda_check.h"
#include "libavutil/internal.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
static const enum AVPixelFormat supported_formats[] = {
AV_PIX_FMT_YUV420P,
AV_PIX_FMT_YUV444P
};
enum TransposeStage {
STAGE_ROTATE,
STAGE_TRANSPOSE,
STAGE_NB
};
enum Transpose {
NPP_TRANSPOSE_CCLOCK_FLIP = 0,
NPP_TRANSPOSE_CLOCK = 1,
NPP_TRANSPOSE_CCLOCK = 2,
NPP_TRANSPOSE_CLOCK_FLIP = 3
};
enum Passthrough {
NPP_TRANSPOSE_PT_TYPE_NONE = 0,
NPP_TRANSPOSE_PT_TYPE_LANDSCAPE,
NPP_TRANSPOSE_PT_TYPE_PORTRAIT
};
typedef struct NPPTransposeStageContext {
int stage_needed;
enum AVPixelFormat in_fmt;
enum AVPixelFormat out_fmt;
struct {
int width;
int height;
} planes_in[3], planes_out[3];
AVBufferRef *frames_ctx;
AVFrame *frame;
} NPPTransposeStageContext;
typedef struct NPPTransposeContext {
const AVClass *class;
NPPTransposeStageContext stages[STAGE_NB];
AVFrame *tmp_frame;
int passthrough; ///< PassthroughType, landscape passthrough mode enabled
int dir; ///< TransposeDir
} NPPTransposeContext;
static int npptranspose_init(AVFilterContext *ctx)
{
NPPTransposeContext *s = ctx->priv;
int i;
for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
s->stages[i].frame = av_frame_alloc();
if (!s->stages[i].frame)
return AVERROR(ENOMEM);
}
s->tmp_frame = av_frame_alloc();
if (!s->tmp_frame)
return AVERROR(ENOMEM);
return 0;
}
static void npptranspose_uninit(AVFilterContext *ctx)
{
NPPTransposeContext *s = ctx->priv;
int i;
for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
av_frame_free(&s->stages[i].frame);
av_buffer_unref(&s->stages[i].frames_ctx);
}
av_frame_free(&s->tmp_frame);
}
static int init_stage(NPPTransposeStageContext *stage, AVBufferRef *device_ctx)
{
AVBufferRef *out_ref = NULL;
AVHWFramesContext *out_ctx;
int in_sw, in_sh, out_sw, out_sh;
int ret, i;
av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
if (!stage->planes_out[0].width) {
stage->planes_out[0].width = stage->planes_in[0].width;
stage->planes_out[0].height = stage->planes_in[0].height;
}
for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
}
out_ref = av_hwframe_ctx_alloc(device_ctx);
if (!out_ref)
return AVERROR(ENOMEM);
out_ctx = (AVHWFramesContext*)out_ref->data;
out_ctx->format = AV_PIX_FMT_CUDA;
out_ctx->sw_format = stage->out_fmt;
out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
ret = av_hwframe_ctx_init(out_ref);
if (ret < 0)
goto fail;
av_frame_unref(stage->frame);
ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
if (ret < 0)
goto fail;
stage->frame->width = stage->planes_out[0].width;
stage->frame->height = stage->planes_out[0].height;
av_buffer_unref(&stage->frames_ctx);
stage->frames_ctx = out_ref;
return 0;
fail:
av_buffer_unref(&out_ref);
return ret;
}
static int format_is_supported(enum AVPixelFormat fmt)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
if (supported_formats[i] == fmt)
return 1;
return 0;
}
static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
int out_width, int out_height)
{
NPPTransposeContext *s = ctx->priv;
AVHWFramesContext *in_frames_ctx;
enum AVPixelFormat format;
int i, ret, last_stage = -1;
int rot_width = out_width, rot_height = out_height;
/* check that we have a hw context */
if (!ctx->inputs[0]->hw_frames_ctx) {
av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
return AVERROR(EINVAL);
}
in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
format = in_frames_ctx->sw_format;
if (!format_is_supported(format)) {
av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
av_get_pix_fmt_name(format));
return AVERROR(ENOSYS);
}
if (s->dir != NPP_TRANSPOSE_CCLOCK_FLIP) {
s->stages[STAGE_ROTATE].stage_needed = 1;
}
if (s->dir == NPP_TRANSPOSE_CCLOCK_FLIP || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) {
s->stages[STAGE_TRANSPOSE].stage_needed = 1;
/* Rotating by 180° in case of clock_flip, or not at all for cclock_flip, so width/height unchanged by rotation */
rot_width = in_width;
rot_height = in_height;
}
s->stages[STAGE_ROTATE].in_fmt = format;
s->stages[STAGE_ROTATE].out_fmt = format;
s->stages[STAGE_ROTATE].planes_in[0].width = in_width;
s->stages[STAGE_ROTATE].planes_in[0].height = in_height;
s->stages[STAGE_ROTATE].planes_out[0].width = rot_width;
s->stages[STAGE_ROTATE].planes_out[0].height = rot_height;
s->stages[STAGE_TRANSPOSE].in_fmt = format;
s->stages[STAGE_TRANSPOSE].out_fmt = format;
s->stages[STAGE_TRANSPOSE].planes_in[0].width = rot_width;
s->stages[STAGE_TRANSPOSE].planes_in[0].height = rot_height;
s->stages[STAGE_TRANSPOSE].planes_out[0].width = out_width;
s->stages[STAGE_TRANSPOSE].planes_out[0].height = out_height;
/* init the hardware contexts */
for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
if (!s->stages[i].stage_needed)
continue;
ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
if (ret < 0)
return ret;
last_stage = i;
}
if (last_stage >= 0) {
ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
} else {
ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
s->passthrough = 1;
}
if (!ctx->outputs[0]->hw_frames_ctx)
return AVERROR(ENOMEM);
return 0;
}
static int npptranspose_config_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
NPPTransposeContext *s = ctx->priv;
int ret;
if ((inlink->w >= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_LANDSCAPE) ||
(inlink->w <= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_PORTRAIT))
{
if (inlink->hw_frames_ctx) {
outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
if (!outlink->hw_frames_ctx)
return AVERROR(ENOMEM);
}
av_log(ctx, AV_LOG_VERBOSE,
"w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
inlink->w, inlink->h, inlink->w, inlink->h);
return 0;
} else {
s->passthrough = NPP_TRANSPOSE_PT_TYPE_NONE;
}
outlink->w = inlink->h;
outlink->h = inlink->w;
outlink->sample_aspect_ratio = (AVRational){inlink->sample_aspect_ratio.den, inlink->sample_aspect_ratio.num};
ret = init_processing_chain(ctx, inlink->w, inlink->h, outlink->w, outlink->h);
if (ret < 0)
return ret;
av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -transpose-> w:%d h:%d\n",
inlink->w, inlink->h, outlink->w, outlink->h);
return 0;
}
static int npptranspose_rotate(AVFilterContext *ctx, NPPTransposeStageContext *stage,
AVFrame *out, AVFrame *in)
{
NPPTransposeContext *s = ctx->priv;
NppStatus err;
int i;
for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
int iw = stage->planes_in[i].width;
int ih = stage->planes_in[i].height;
int ow = stage->planes_out[i].width;
int oh = stage->planes_out[i].height;
// nppRotate uses 0,0 as the rotation point
// need to shift the image accordingly after rotation
// need to substract 1 to get the correct coordinates
double angle = s->dir == NPP_TRANSPOSE_CLOCK ? -90.0 : s->dir == NPP_TRANSPOSE_CCLOCK ? 90.0 : 180.0;
int shiftw = (s->dir == NPP_TRANSPOSE_CLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? ow - 1 : 0;
int shifth = (s->dir == NPP_TRANSPOSE_CCLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? oh - 1 : 0;
err = nppiRotate_8u_C1R(in->data[i], (NppiSize){ iw, ih },
in->linesize[i], (NppiRect){ 0, 0, iw, ih },
out->data[i], out->linesize[i],
(NppiRect){ 0, 0, ow, oh },
angle, shiftw, shifth, NPPI_INTER_NN);
if (err != NPP_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "NPP rotate error: %d\n", err);
return AVERROR_UNKNOWN;
}
}
return 0;
}
static int npptranspose_transpose(AVFilterContext *ctx, NPPTransposeStageContext *stage,
AVFrame *out, AVFrame *in)
{
NppStatus err;
int i;
for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
int iw = stage->planes_in[i].width;
int ih = stage->planes_in[i].height;
err = nppiTranspose_8u_C1R(in->data[i], in->linesize[i],
out->data[i], out->linesize[i],
(NppiSize){ iw, ih });
if (err != NPP_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "NPP transpose error: %d\n", err);
return AVERROR_UNKNOWN;
}
}
return 0;
}
static int (*const npptranspose_process[])(AVFilterContext *ctx, NPPTransposeStageContext *stage,
AVFrame *out, AVFrame *in) = {
[STAGE_ROTATE] = npptranspose_rotate,
[STAGE_TRANSPOSE] = npptranspose_transpose
};
static int npptranspose_filter(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
{
NPPTransposeContext *s = ctx->priv;
AVFrame *src = in;
int i, ret, last_stage = -1;
for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
if (!s->stages[i].stage_needed)
continue;
ret = npptranspose_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
if (ret < 0)
return ret;
src = s->stages[i].frame;
last_stage = i;
}
if (last_stage < 0)
return AVERROR_BUG;
ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
if (ret < 0)
return ret;
av_frame_move_ref(out, src);
av_frame_move_ref(src, s->tmp_frame);
ret = av_frame_copy_props(out, in);
if (ret < 0)
return ret;
return 0;
}
static int npptranspose_filter_frame(AVFilterLink *link, AVFrame *in)
{
AVFilterContext *ctx = link->dst;
NPPTransposeContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
AVFrame *out = NULL;
CUcontext dummy;
int ret = 0;
if (s->passthrough)
return ff_filter_frame(outlink, in);
out = av_frame_alloc();
if (!out) {
ret = AVERROR(ENOMEM);
goto fail;
}
ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(device_hwctx->cuda_ctx));
if (ret < 0)
goto fail;
ret = npptranspose_filter(ctx, out, in);
CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
if (ret < 0)
goto fail;
av_frame_free(&in);
return ff_filter_frame(outlink, out);
fail:
av_frame_free(&in);
av_frame_free(&out);
return ret;
}
#define OFFSET(x) offsetof(NPPTransposeContext, x)
#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
static const AVOption options[] = {
{ "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, "dir" },
{ "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 0, FLAGS, "dir" },
{ "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK }, 0, 0, FLAGS, "dir" },
{ "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK }, 0, 0, FLAGS, "dir" },
{ "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK_FLIP }, 0, 0, FLAGS, "dir" },
{ "passthrough", "do not apply transposition if the input matches the specified geometry", OFFSET(passthrough), AV_OPT_TYPE_INT, { .i64 = NPP_TRANSPOSE_PT_TYPE_NONE }, 0, 2, FLAGS, "passthrough" },
{ "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_NONE }, 0, 0, FLAGS, "passthrough" },
{ "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_LANDSCAPE }, 0, 0, FLAGS, "passthrough" },
{ "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_PORTRAIT }, 0, 0, FLAGS, "passthrough" },
{ NULL },
};
static const AVClass npptranspose_class = {
.class_name = "npptranspose",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
static const AVFilterPad npptranspose_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = npptranspose_filter_frame,
},
};
static const AVFilterPad npptranspose_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = npptranspose_config_props,
},
};
const AVFilter ff_vf_transpose_npp = {
.name = "transpose_npp",
.description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video transpose"),
.init = npptranspose_init,
.uninit = npptranspose_uninit,
.priv_size = sizeof(NPPTransposeContext),
.priv_class = &npptranspose_class,
FILTER_INPUTS(npptranspose_inputs),
FILTER_OUTPUTS(npptranspose_outputs),
FILTER_SINGLE_PIXFMT(AV_PIX_FMT_CUDA),
.flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pedoc/ffmpeg.git
git@gitee.com:pedoc/ffmpeg.git
pedoc
ffmpeg
FFmpeg
n5.1.2

搜索帮助