1+ //
2+ // Created by frank on 2018/6/4.
3+ //
4+ #include "libavcodec/avcodec.h"
5+ #include "libavformat/avformat.h"
6+ #include "libswscale/swscale.h"
7+ #include <android/native_window.h>
8+ #include <android/native_window_jni.h>
9+ #include <stdio.h>
10+ #include <unistd.h>
11+ #include <libavutil/imgutils.h>
12+ #include <android/log.h>
13+ #include <libavfilter/buffersink.h>
14+ #include <libavfilter/buffersrc.h>
15+ #include <libavfilter/avfiltergraph.h>
16+ #include <libavutil/opt.h>
17+
18+ #define TAG "VideoFilter"
19+ #define LOGI (FORMAT ,...) __android_log_print(ANDROID_LOG_INFO, TAG, FORMAT, ##__VA_ARGS__);
20+ #define LOGE (FORMAT ,...) __android_log_print(ANDROID_LOG_ERROR, TAG, FORMAT, ##__VA_ARGS__);
21+
22+ AVFilterContext * buffersink_ctx ;
23+
24+ AVFilterContext * buffersrc_ctx ;
25+
26+ AVFilterGraph * filter_graph ;
27+
28+ int video_stream_index = -1 ;
29+
30+ AVFormatContext * pFormatCtx ;
31+
32+ AVCodecContext * pCodecCtx ;
33+
34+ ANativeWindow_Buffer windowBuffer ;
35+
36+ ANativeWindow * nativeWindow ;
37+
38+ AVFrame * pFrame ;
39+
40+ AVFrame * pFrameRGBA ;
41+
42+ uint8_t * buffer ;
43+
44+ struct SwsContext * sws_ctx ;
45+
46+ /**
47+ * colorbalance:
48+ rs/gs/bs
49+ Adjust red, green and blue shadows (darkest pixels).
50+ rm/gm/bm
51+ Adjust red, green and blue midtones (medium pixels).
52+ rh/gh/bh
53+ Adjust red, green and blue highlights (brightest pixels).
54+ */
55+
56+ /**
57+ * Draw text
58+ * Draw a text string or text from a specified file on top of a video, using the libfreetype library.
59+ * To enable compilation of this filter, you need to configure FFmpeg with --enable-libfreetype.
60+ * To enable default font fallback and the font option you need to configure FFmpeg with --enable-libfontconfig.
61+ * To enable the text_shaping option, you need to configure FFmpeg with --enable-libfribidi.
62+ * Video center: drawtext="fontsize=30:fontfile=FreeSerif.ttf:text='hello world':x=(w-text_w)/2:y=(h-text_h)/2"
63+ */
64+
65+ //const char *filter_descr = "lutyuv='u=128:v=128'";//黑白
66+ //const char *filter_descr = "hue='h=60:s=-3'";//hue滤镜-->Set the hue to 60 degrees and the saturation to -3
67+ //const char *filter_descr = "vflip";//上下反序
68+ //const char *filter_descr = "hflip";//左右反序
69+ //const char *filter_descr = "rotate=90";//旋转90°
70+ //const char *filter_descr = "colorbalance=bs=0.3";//添加蓝色背景
71+ //const char *filter_descr = "drawbox=x=100:y=100:w=100:h=100:color=pink@0.5'";//绘制矩形
72+ //const char *filter_descr = "drawgrid=w=iw/3:h=ih/3:t=2:c=white@0.5";//九宫格分割
73+ //const char *filter_descr = "edgedetect=low=0.1:high=0.4";//边缘检测
74+ //const char *filter_descr = "fftfilt=dc_Y=0:weight_Y='exp(-4 * ((Y+X)/(W+H)))'";//fft模糊
75+ //const char *filter_descr = "lutrgb='r=0:g=0'";//去掉红色、绿色分量,只保留蓝色
76+ //const char *filter_descr = "noise=alls=20:allf=t+u";//添加噪声
77+ //const char *filter_descr = "pad=640:480:40:0:violet";//pad填充颜色
78+ //const char *filter_descr = "vignette='PI/4+random(1)*PI/50':eval=frame";//闪烁装饰-->Make a flickering vignetting
79+
80+ //const char *filter_descr = "gblur=sigma=0.5:steps=1:planes=1:sigmaV=1";//高斯模糊
81+ //const char *filter_descr = "drawtext=fontfile='app/src/main/cpp/arial.ttf':fontcolor=green:fontsize=30:text='Hello world'";//绘制文字
82+ //const char *filter_descr = "movie=my_logo.png[wm];[in][wm]overlay=5:5[out]";//添加图片水印
83+
84+ //初始化滤波器
85+ int init_filters (const char * filters_descr ) {
86+ char args [512 ];
87+ int ret = 0 ;
88+ AVFilter * buffersrc = avfilter_get_by_name ("buffer" );
89+ AVFilter * buffersink = avfilter_get_by_name ("buffersink" );
90+ AVFilterInOut * outputs = avfilter_inout_alloc ();
91+ AVFilterInOut * inputs = avfilter_inout_alloc ();
92+ AVRational time_base = pFormatCtx -> streams [video_stream_index ]-> time_base ;
93+ enum AVPixelFormat pix_fmts [] = { AV_PIX_FMT_YUV420P , AV_PIX_FMT_NONE };
94+
95+ filter_graph = avfilter_graph_alloc ();
96+ if (!outputs || !inputs || !filter_graph ) {
97+ ret = AVERROR (ENOMEM );
98+ goto end ;
99+ }
100+
101+ /* buffer video source: the decoded frames from the decoder will be inserted here. */
102+ snprintf (args , sizeof (args ),
103+ "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d" ,
104+ pCodecCtx -> width , pCodecCtx -> height , pCodecCtx -> pix_fmt ,
105+ time_base .num , time_base .den ,
106+ pCodecCtx -> sample_aspect_ratio .num , pCodecCtx -> sample_aspect_ratio .den );
107+
108+ ret = avfilter_graph_create_filter (& buffersrc_ctx , buffersrc , "in" ,
109+ args , NULL , filter_graph );
110+ if (ret < 0 ) {
111+ LOGE ("Cannot create buffer source\n" );
112+ goto end ;
113+ }
114+
115+ /* buffer video sink: to terminate the filter chain. */
116+ ret = avfilter_graph_create_filter (& buffersink_ctx , buffersink , "out" ,
117+ NULL , NULL , filter_graph );
118+ if (ret < 0 ) {
119+ LOGE ("Cannot create buffer sink\n" );
120+ goto end ;
121+ }
122+
123+ ret = av_opt_set_int_list (buffersink_ctx , "pix_fmts" , pix_fmts ,
124+ AV_PIX_FMT_NONE , AV_OPT_SEARCH_CHILDREN );
125+ if (ret < 0 ) {
126+ LOGE ("Cannot set output pixel format\n" );
127+ goto end ;
128+ }
129+
130+ /*
131+ * The buffer source output must be connected to the input pad of
132+ * the first filter described by filters_descr; since the first
133+ * filter input label is not specified, it is set to "in" by
134+ * default.
135+ */
136+ outputs -> name = av_strdup ("in" );
137+ outputs -> filter_ctx = buffersrc_ctx ;
138+ outputs -> pad_idx = 0 ;
139+ outputs -> next = NULL ;
140+
141+ /*
142+ * The buffer sink input must be connected to the output pad of
143+ * the last filter described by filters_descr; since the last
144+ * filter output label is not specified, it is set to "out" by
145+ * default.
146+ */
147+ inputs -> name = av_strdup ("out" );
148+ inputs -> filter_ctx = buffersink_ctx ;
149+ inputs -> pad_idx = 0 ;
150+ inputs -> next = NULL ;
151+
152+ if ((ret = avfilter_graph_parse_ptr (filter_graph , filters_descr ,
153+ & inputs , & outputs , NULL )) < 0 )
154+ goto end ;
155+
156+ if ((ret = avfilter_graph_config (filter_graph , NULL )) < 0 )
157+ goto end ;
158+
159+ end :
160+ avfilter_inout_free (& inputs );
161+ avfilter_inout_free (& outputs );
162+
163+ return ret ;
164+ }
165+
166+ int open_input (JNIEnv * env , const char * file_name , jobject surface ){
167+ LOGI ("open file:%s\n" , file_name );
168+ //注册所有组件
169+ av_register_all ();
170+ //注册滤波器
171+ avfilter_register_all ();
172+ //分配上下文
173+ pFormatCtx = avformat_alloc_context ();
174+ //打开视频文件
175+ if (avformat_open_input (& pFormatCtx , file_name , NULL , NULL )!= 0 ) {
176+ LOGE ("Couldn't open file:%s\n" , file_name );
177+ return -1 ;
178+ }
179+ //检索多媒体流信息
180+ if (avformat_find_stream_info (pFormatCtx , NULL )< 0 ) {
181+ LOGE ("Couldn't find stream information." );
182+ return -1 ;
183+ }
184+ //寻找视频流的第一帧
185+ int i ;
186+ for (i = 0 ; i < pFormatCtx -> nb_streams ; i ++ ) {
187+ if (pFormatCtx -> streams [i ]-> codec -> codec_type == AVMEDIA_TYPE_VIDEO
188+ && video_stream_index < 0 ) {
189+ video_stream_index = i ;
190+ }
191+ }
192+ if (video_stream_index == -1 ) {
193+ LOGE ("couldn't find a video stream." );
194+ return -1 ;
195+ }
196+
197+ //获取codec上下文指针
198+ pCodecCtx = pFormatCtx -> streams [video_stream_index ]-> codec ;
199+ //寻找视频流的解码器
200+ AVCodec * pCodec = avcodec_find_decoder (pCodecCtx -> codec_id );
201+ if (pCodec == NULL ) {
202+ LOGE ("couldn't find Codec." );
203+ return -1 ;
204+ }
205+ if (avcodec_open2 (pCodecCtx , pCodec , NULL ) < 0 ) {
206+ LOGE ("Couldn't open codec." );
207+ return -1 ;
208+ }
209+ // 获取native window
210+ nativeWindow = ANativeWindow_fromSurface (env , surface );
211+
212+ // 设置native window的buffer大小,可自动拉伸
213+ ANativeWindow_setBuffersGeometry (nativeWindow , pCodecCtx -> width , pCodecCtx -> height , WINDOW_FORMAT_RGBA_8888 );
214+ //申请内存
215+ pFrame = av_frame_alloc ();
216+ pFrameRGBA = av_frame_alloc ();
217+ if (pFrameRGBA == NULL || pFrame == NULL ) {
218+ LOGE ("Couldn't allocate video frame." );
219+ return -1 ;
220+ }
221+ // buffer中数据用于渲染,且格式为RGBA
222+ int numBytes = av_image_get_buffer_size (AV_PIX_FMT_RGBA , pCodecCtx -> width , pCodecCtx -> height , 1 );
223+
224+ buffer = (uint8_t * )av_malloc (numBytes * sizeof (uint8_t ));
225+ av_image_fill_arrays (pFrameRGBA -> data , pFrameRGBA -> linesize , buffer , AV_PIX_FMT_RGBA ,
226+ pCodecCtx -> width , pCodecCtx -> height , 1 );
227+ // 由于解码出来的帧格式不是RGBA的,在渲染之前需要进行格式转换
228+ sws_ctx = sws_getContext (pCodecCtx -> width ,
229+ pCodecCtx -> height ,
230+ pCodecCtx -> pix_fmt ,
231+ pCodecCtx -> width ,
232+ pCodecCtx -> height ,
233+ AV_PIX_FMT_RGBA ,
234+ SWS_BILINEAR ,
235+ NULL ,
236+ NULL ,
237+ NULL );
238+
239+ return 0 ;
240+ }
241+
242+ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_VideoPlayer_filter
243+ (JNIEnv * env , jclass clazz , jstring filePath , jobject surface , jstring filterDescr ){
244+
245+ int ret ;
246+ const char * file_name = (* env )-> GetStringUTFChars (env , filePath , JNI_FALSE );
247+ const char * filter_descr = (* env )-> GetStringUTFChars (env , filterDescr , JNI_FALSE );
248+ //打开输入文件
249+ if ((ret = open_input (env , file_name , surface ) < 0 )){
250+ LOGE ("Couldn't allocate video frame." );
251+ goto end ;
252+ }
253+ //注册滤波器
254+ avfilter_register_all ();
255+
256+ AVFrame * filter_frame = av_frame_alloc ();
257+ if (filter_frame == NULL ) {
258+ LOGE ("Couldn't allocate video frame." );
259+ ret = -1 ;
260+ goto end ;
261+ }
262+
263+ //初始化滤波器
264+ if ((ret = init_filters (filter_descr )) < 0 ){
265+ LOGE ("init_filter error, ret=%d\n" , ret );
266+ goto end ;
267+ }
268+
269+ int frameFinished ;
270+ AVPacket packet ;
271+
272+ while (av_read_frame (pFormatCtx , & packet )>=0 ) {
273+ //判断是否为视频流
274+ if (packet .stream_index == video_stream_index ) {
275+ //对该帧进行解码
276+ avcodec_decode_video2 (pCodecCtx , pFrame , & frameFinished , & packet );
277+
278+ if (frameFinished ) {
279+ //把解码后视频帧添加到filter_graph
280+ if (av_buffersrc_add_frame_flags (buffersrc_ctx , pFrame , AV_BUFFERSRC_FLAG_KEEP_REF ) < 0 ) {
281+ LOGE ("Error while feeding the filter_graph\n" );
282+ break ;
283+ }
284+ //把滤波后的视频帧从filter graph取出来
285+ ret = av_buffersink_get_frame (buffersink_ctx , filter_frame );
286+ if (ret >= 0 ){
287+ // lock native window
288+ ANativeWindow_lock (nativeWindow , & windowBuffer , 0 );
289+ // 格式转换
290+ sws_scale (sws_ctx , (uint8_t const * const * )filter_frame -> data ,
291+ filter_frame -> linesize , 0 , pCodecCtx -> height ,
292+ pFrameRGBA -> data , pFrameRGBA -> linesize );
293+ // 获取stride
294+ uint8_t * dst = windowBuffer .bits ;
295+ int dstStride = windowBuffer .stride * 4 ;
296+ uint8_t * src = pFrameRGBA -> data [0 ];
297+ int srcStride = pFrameRGBA -> linesize [0 ];
298+ // 由于window的stride和帧的stride不同,因此需要逐行复制
299+ int h ;
300+ for (h = 0 ; h < pCodecCtx -> height ; h ++ ) {
301+ memcpy (dst + h * dstStride , src + h * srcStride , (size_t ) srcStride );
302+ }
303+ ANativeWindow_unlockAndPost (nativeWindow );
304+ }
305+ av_frame_unref (filter_frame );
306+ }
307+ av_frame_unref (pFrame );
308+ //延迟等待
309+ usleep ((unsigned long ) (1000 * 40 ));
310+ }
311+ av_packet_unref (& packet );
312+ }
313+ end :
314+ //释放内存以及关闭文件
315+ av_free (buffer );
316+ av_free (pFrameRGBA );
317+ av_free (filter_frame );
318+ av_free (pFrame );
319+ avcodec_close (pCodecCtx );
320+ avformat_close_input (& pFormatCtx );
321+ avfilter_graph_free (& filter_graph );
322+ (* env )-> ReleaseStringUTFChars (env , filePath , file_name );
323+ (* env )-> ReleaseStringUTFChars (env , filterDescr , filter_descr );
324+ return ret ;
325+ }
0 commit comments