2121#include "ffmpeg_jni_define.h"
2222
2323#define TAG "VideoFilter"
24+ #define MAX_AUDIO_FRAME_SIZE (48000 * 4)
2425
25- AVFormatContext * pFormatCtx ;
26- AVCodecContext * pCodecCtx ;
27- ANativeWindow * nativeWindow ;
26+ AVFormatContext * format_ctx ;
27+ AVCodecContext * audio_codec_ctx ;
28+ AVCodecContext * video_codec_ctx ;
2829
29- int again ;
30- int release ;
3130uint8_t * buffer ;
32- AVFrame * pFrame ;
33- AVFrame * pFrameRGBA ;
34- struct SwsContext * sws_ctx ;
35- int video_stream_index = -1 ;
36-
37- int pos ;
38- int got_frame ;
39- int out_channel_nb ;
31+ AVFrame * frame_src ;
32+ AVFrame * frame_rgb ;
4033uint8_t * out_buffer ;
41- jobject audio_track ;
42- bool playAudio = true;
34+ enum AVSampleFormat out_sample_fmt ;
35+
36+ ANativeWindow * native_window ;
37+
38+ struct SwsContext * sws_ctx ;
4339SwrContext * audio_swr_ctx ;
40+
41+ int video_stream_index = -1 ;
4442int audio_stream_index = -1 ;
45- AVCodecContext * audioCodecCtx ;
46- jmethodID audio_track_write_mid ;
47- enum AVSampleFormat out_sample_fmt ;
4843
44+ int pos = 0 ;
45+ int again = 0 ;
46+ int release = 0 ;
47+ int playAudio = 0 ;
48+ int out_channel_nb = 0 ;
4949
50- #define MAX_AUDIO_FRAME_SIZE (48000 * 4)
50+ jobject audio_track ;
51+ jmethodID audio_track_write_mid ;
5152
5253const char * filters [] = {"lutyuv='u=128:v=128'" ,
5354 "hue='h=60:s=-3'" ,
@@ -130,19 +131,19 @@ int init_filters(const char *filters_descr, AVRational time_base, AVCodecContext
130131//init player
131132int open_input (JNIEnv * env , const char * file_name , jobject surface ) {
132133 LOGI (TAG , "open file:%s\n" , file_name );
133- pFormatCtx = avformat_alloc_context ();
134- if (avformat_open_input (& pFormatCtx , file_name , NULL , NULL ) != 0 ) {
134+ format_ctx = avformat_alloc_context ();
135+ if (avformat_open_input (& format_ctx , file_name , NULL , NULL ) != 0 ) {
135136 LOGE (TAG , "Couldn't open file:%s\n" , file_name );
136137 return -1 ;
137138 }
138- if (avformat_find_stream_info (pFormatCtx , NULL ) < 0 ) {
139+ if (avformat_find_stream_info (format_ctx , NULL ) < 0 ) {
139140 LOGE (TAG , "Couldn't find stream information." );
140141 return -1 ;
141142 }
142143
143144 int i ;
144- for (i = 0 ; i < pFormatCtx -> nb_streams ; i ++ ) {
145- if (pFormatCtx -> streams [i ]-> codecpar -> codec_type == AVMEDIA_TYPE_VIDEO ) {
145+ for (i = 0 ; i < format_ctx -> nb_streams ; i ++ ) {
146+ if (format_ctx -> streams [i ]-> codecpar -> codec_type == AVMEDIA_TYPE_VIDEO ) {
146147 video_stream_index = i ;
147148 break ;
148149 }
@@ -152,43 +153,43 @@ int open_input(JNIEnv *env, const char *file_name, jobject surface) {
152153 return -1 ;
153154 }
154155
155- AVCodecParameters * videoCodec = pFormatCtx -> streams [video_stream_index ]-> codecpar ;
156+ AVCodecParameters * videoCodec = format_ctx -> streams [video_stream_index ]-> codecpar ;
156157 AVCodec * pCodec = avcodec_find_decoder (videoCodec -> codec_id );
157158 if (pCodec == NULL ) {
158159 LOGE (TAG , "couldn't find Codec." );
159160 return -1 ;
160161 }
161- pCodecCtx = avcodec_alloc_context3 (pCodec );
162- avcodec_parameters_to_context (pCodecCtx , videoCodec );
163- if (avcodec_open2 (pCodecCtx , pCodec , NULL ) < 0 ) {
162+ video_codec_ctx = avcodec_alloc_context3 (pCodec );
163+ avcodec_parameters_to_context (video_codec_ctx , videoCodec );
164+ if (avcodec_open2 (video_codec_ctx , pCodec , NULL ) < 0 ) {
164165 LOGE (TAG , "Couldn't open codec." );
165166 return -1 ;
166167 }
167168
168- nativeWindow = ANativeWindow_fromSurface (env , surface );
169- if (!nativeWindow ) {
169+ native_window = ANativeWindow_fromSurface (env , surface );
170+ if (!native_window ) {
170171 LOGE (TAG , "nativeWindow is null..." );
171172 return -1 ;
172173 }
173- ANativeWindow_setBuffersGeometry (nativeWindow , pCodecCtx -> width , pCodecCtx -> height ,
174+ ANativeWindow_setBuffersGeometry (native_window , video_codec_ctx -> width , video_codec_ctx -> height ,
174175 WINDOW_FORMAT_RGBA_8888 );
175- pFrame = av_frame_alloc ();
176- pFrameRGBA = av_frame_alloc ();
177- if (pFrameRGBA == NULL || pFrame == NULL ) {
176+ frame_src = av_frame_alloc ();
177+ frame_rgb = av_frame_alloc ();
178+ if (frame_rgb == NULL || frame_src == NULL ) {
178179 LOGE (TAG , "Couldn't allocate video frame." );
179180 return -1 ;
180181 }
181- int numBytes = av_image_get_buffer_size (AV_PIX_FMT_RGBA , pCodecCtx -> width , pCodecCtx -> height ,
182+ int numBytes = av_image_get_buffer_size (AV_PIX_FMT_RGBA , video_codec_ctx -> width , video_codec_ctx -> height ,
182183 1 );
183184
184185 buffer = (uint8_t * ) av_malloc (numBytes * sizeof (uint8_t ));
185- av_image_fill_arrays (pFrameRGBA -> data , pFrameRGBA -> linesize , buffer , AV_PIX_FMT_RGBA ,
186- pCodecCtx -> width , pCodecCtx -> height , 1 );
187- sws_ctx = sws_getContext (pCodecCtx -> width ,
188- pCodecCtx -> height ,
189- pCodecCtx -> pix_fmt ,
190- pCodecCtx -> width ,
191- pCodecCtx -> height ,
186+ av_image_fill_arrays (frame_rgb -> data , frame_rgb -> linesize , buffer , AV_PIX_FMT_RGBA ,
187+ video_codec_ctx -> width , video_codec_ctx -> height , 1 );
188+ sws_ctx = sws_getContext (video_codec_ctx -> width ,
189+ video_codec_ctx -> height ,
190+ video_codec_ctx -> pix_fmt ,
191+ video_codec_ctx -> width ,
192+ video_codec_ctx -> height ,
192193 AV_PIX_FMT_RGBA ,
193194 SWS_BILINEAR ,
194195 NULL ,
@@ -199,34 +200,33 @@ int open_input(JNIEnv *env, const char *file_name, jobject surface) {
199200}
200201
201202int init_audio (JNIEnv * env , jclass jthiz ) {
202- int i ;
203- for (i = 0 ; i < pFormatCtx -> nb_streams ; i ++ ) {
204- if (pFormatCtx -> streams [i ]-> codecpar -> codec_type == AVMEDIA_TYPE_AUDIO ) {
203+ for (int i = 0 ; i < format_ctx -> nb_streams ; i ++ ) {
204+ if (format_ctx -> streams [i ]-> codecpar -> codec_type == AVMEDIA_TYPE_AUDIO ) {
205205 audio_stream_index = i ;
206206 break ;
207207 }
208208 }
209209
210- AVCodecParameters * audioCodec = pFormatCtx -> streams [audio_stream_index ]-> codecpar ;
210+ AVCodecParameters * audioCodec = format_ctx -> streams [audio_stream_index ]-> codecpar ;
211211 AVCodec * codec = avcodec_find_decoder (audioCodec -> codec_id );
212212 if (codec == NULL ) {
213213 LOGE (TAG , "could not find audio decoder" );
214214 return -1 ;
215215 }
216- audioCodecCtx = avcodec_alloc_context3 (codec );
217- avcodec_parameters_to_context (audioCodecCtx , audioCodec );
218- if (avcodec_open2 (audioCodecCtx , codec , NULL ) < 0 ) {
216+ audio_codec_ctx = avcodec_alloc_context3 (codec );
217+ avcodec_parameters_to_context (audio_codec_ctx , audioCodec );
218+ if (avcodec_open2 (audio_codec_ctx , codec , NULL ) < 0 ) {
219219 LOGE (TAG , "could not open audio decoder" );
220220 return -1 ;
221221 }
222222
223- audio_swr_ctx = swr_alloc ();
224- enum AVSampleFormat in_sample_fmt = audioCodecCtx -> sample_fmt ;
225- out_sample_fmt = AV_SAMPLE_FMT_S16 ;
226- int in_sample_rate = audioCodecCtx -> sample_rate ;
227- int out_sample_rate = in_sample_rate ;
228- uint64_t in_ch_layout = audioCodecCtx -> channel_layout ;
223+ audio_swr_ctx = swr_alloc ();
224+ out_sample_fmt = AV_SAMPLE_FMT_S16 ;
225+ int in_sample_rate = audio_codec_ctx -> sample_rate ;
226+ int out_sample_rate = in_sample_rate ;
227+ uint64_t in_ch_layout = audio_codec_ctx -> channel_layout ;
229228 uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO ;
229+ enum AVSampleFormat in_sample_fmt = audio_codec_ctx -> sample_fmt ;
230230
231231 swr_alloc_set_opts (audio_swr_ctx ,
232232 (int64_t )out_ch_layout , out_sample_fmt , out_sample_rate ,
@@ -258,23 +258,20 @@ int init_audio(JNIEnv *env, jclass jthiz) {
258258}
259259
260260int play_audio (JNIEnv * env , AVPacket * packet , AVFrame * frame ) {
261- int ret = avcodec_send_packet (audioCodecCtx , packet );
261+ int ret = avcodec_send_packet (audio_codec_ctx , packet );
262262 if (ret < 0 )
263263 return ret ;
264264
265265 while (ret >= 0 ) {
266- ret = avcodec_receive_frame (audioCodecCtx , frame );
266+ ret = avcodec_receive_frame (audio_codec_ctx , frame );
267267 if (ret == AVERROR_EOF || ret == AVERROR (EAGAIN ))
268268 break ;
269269 else if (ret < 0 ) {
270270 LOGE (TAG , "decode error=%s" , av_err2str (ret ));
271271 break ;
272272 }
273273 }
274- if (ret < 0 ) {
275- return ret ;
276- }
277- if (got_frame > 0 ) {
274+ if (ret >= 0 ) {
278275 swr_convert (audio_swr_ctx , & out_buffer , MAX_AUDIO_FRAME_SIZE ,
279276 (const uint8_t * * ) frame -> data , frame -> nb_samples );
280277 int out_buffer_size = av_samples_get_buffer_size (NULL , out_channel_nb ,
@@ -289,11 +286,11 @@ int play_audio(JNIEnv *env, AVPacket *packet, AVFrame *frame) {
289286 (* env )-> DeleteLocalRef (env , audio_sample_array );
290287 usleep (1000 );//1000 * 16
291288 }
292- return 0 ;
289+ return ret ;
293290}
294291
295292int render_video (AVFilterContext * buffersrc_ctx , AVFilterContext * buffersink_ctx , AVFrame * filter_frame ) {
296- int ret = av_buffersrc_add_frame_flags (buffersrc_ctx , pFrame ,
293+ int ret = av_buffersrc_add_frame_flags (buffersrc_ctx , frame_src ,
297294 AV_BUFFERSRC_FLAG_KEEP_REF );
298295 if (ret < 0 ) {
299296 LOGE (TAG , "Error while feeding the filter_graph\n" );
@@ -304,20 +301,19 @@ int render_video(AVFilterContext *buffersrc_ctx, AVFilterContext *buffersink_ctx
304301 if (ret >= 0 ) {
305302 // lock native window
306303 ANativeWindow_Buffer windowBuffer ;
307- ANativeWindow_lock (nativeWindow , & windowBuffer , NULL );
304+ ANativeWindow_lock (native_window , & windowBuffer , NULL );
308305 // convert
309306 sws_scale (sws_ctx , (uint8_t const * const * ) filter_frame -> data ,
310- filter_frame -> linesize , 0 , pCodecCtx -> height ,
311- pFrameRGBA -> data , pFrameRGBA -> linesize );
312- uint8_t * dst = windowBuffer .bits ;
307+ filter_frame -> linesize , 0 , video_codec_ctx -> height ,
308+ frame_rgb -> data , frame_rgb -> linesize );
309+ uint8_t * dst = windowBuffer .bits ;
313310 int dstStride = windowBuffer .stride * 4 ;
314- uint8_t * src = pFrameRGBA -> data [0 ];
315- int srcStride = pFrameRGBA -> linesize [0 ];
316- int h ;
317- for (h = 0 ; h < pCodecCtx -> height ; h ++ ) {
311+ uint8_t * src = frame_rgb -> data [0 ];
312+ int srcStride = frame_rgb -> linesize [0 ];
313+ for (int h = 0 ; h < video_codec_ctx -> height ; h ++ ) {
318314 memcpy (dst + h * dstStride , src + h * srcStride , (size_t ) srcStride );
319315 }
320- ANativeWindow_unlockAndPost (nativeWindow );
316+ ANativeWindow_unlockAndPost (native_window );
321317 }
322318 av_frame_unref (filter_frame );
323319 return ret ;
@@ -329,7 +325,7 @@ VIDEO_PLAYER_FUNC(jint, filter, jstring filePath, jobject surface, jint position
329325 AVFilterGraph * filter_graph ;
330326 AVFilterContext * buffersrc_ctx ;
331327 AVFilterContext * buffersink_ctx ;
332- AVPacket * packet = av_packet_alloc ();
328+ AVPacket * packet = av_packet_alloc ();
333329 AVFrame * filter_frame = av_frame_alloc ();
334330 const char * file_name = (* env )-> GetStringUTFChars (env , filePath , JNI_FALSE );
335331
@@ -345,19 +341,19 @@ VIDEO_PLAYER_FUNC(jint, filter, jstring filePath, jobject surface, jint position
345341 }
346342
347343 //init filter
348- AVRational time_base = pFormatCtx -> streams [video_stream_index ]-> time_base ;
349- if ((ret = init_filters (filters [pos ], time_base , pCodecCtx ,
344+ AVRational time_base = format_ctx -> streams [video_stream_index ]-> time_base ;
345+ if ((ret = init_filters (filters [pos ], time_base , video_codec_ctx ,
350346 & filter_graph , & buffersrc_ctx , & buffersink_ctx )) < 0 ) {
351347 LOGE (TAG , "init_filter error, ret=%d\n" , ret );
352348 goto end ;
353349 }
354350
355- while (av_read_frame (pFormatCtx , packet ) >= 0 && !release ) {
351+ while (av_read_frame (format_ctx , packet ) >= 0 && !release ) {
356352 //switch filter
357353 if (again ) {
358354 again = 0 ;
359355 avfilter_graph_free (& filter_graph );
360- if ((ret = init_filters (filters [pos ], time_base , pCodecCtx ,
356+ if ((ret = init_filters (filters [pos ], time_base , video_codec_ctx ,
361357 & filter_graph , & buffersrc_ctx , & buffersink_ctx )) < 0 ) {
362358 LOGE (TAG , "init_filter error, ret=%d\n" , ret );
363359 goto end ;
@@ -366,12 +362,12 @@ VIDEO_PLAYER_FUNC(jint, filter, jstring filePath, jobject surface, jint position
366362 }
367363 //is video stream or not
368364 if (packet -> stream_index == video_stream_index ) {
369- ret = avcodec_send_packet (pCodecCtx , packet );
365+ ret = avcodec_send_packet (video_codec_ctx , packet );
370366 if (ret < 0 )
371367 goto end ;
372368
373369 while (ret >= 0 ) {
374- ret = avcodec_receive_frame (pCodecCtx , pFrame );
370+ ret = avcodec_receive_frame (video_codec_ctx , frame_src );
375371 if (ret == AVERROR_EOF || ret == AVERROR (EAGAIN ))
376372 break ;
377373 else if (ret < 0 ) {
@@ -386,7 +382,7 @@ VIDEO_PLAYER_FUNC(jint, filter, jstring filePath, jobject surface, jint position
386382 }
387383 } else if (packet -> stream_index == audio_stream_index ) {//audio stream
388384 if (playAudio ) {
389- play_audio (env , packet , pFrame );
385+ play_audio (env , packet , frame_src );
390386 }
391387 }
392388 av_packet_unref (packet );
@@ -397,17 +393,17 @@ VIDEO_PLAYER_FUNC(jint, filter, jstring filePath, jobject surface, jint position
397393 sws_freeContext (sws_ctx );
398394 swr_free (& audio_swr_ctx );
399395 avfilter_graph_free (& filter_graph );
400- // avcodec_free_context(&pCodecCtx );
401- // avcodec_free_context(&audioCodecCtx );
402- avformat_close_input (& pFormatCtx );
403- av_frame_free (& pFrameRGBA );
396+ // avcodec_free_context(&video_codec_ctx );
397+ // avcodec_free_context(&audio_codec_ctx );
398+ avformat_close_input (& format_ctx );
399+ av_frame_free (& frame_rgb );
404400 av_frame_free (& filter_frame );
405- av_frame_free (& pFrame );
401+ av_frame_free (& frame_src );
406402 av_packet_free (& packet );
407403
408404 audio_track = NULL ;
409405 audio_track_write_mid = NULL ;
410- ANativeWindow_release (nativeWindow );
406+ ANativeWindow_release (native_window );
411407 (* env )-> ReleaseStringUTFChars (env , filePath , file_name );
412408 again = 0 ;
413409 release = 0 ;
0 commit comments