1+ //
2+ // Created by frank on 2018/2/1.
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+
14+ #define TAG "MediaPlayer"
15+ #define LOGI (FORMAT ,...) __android_log_print(ANDROID_LOG_INFO, TAG, FORMAT, ##__VA_ARGS__);
16+ #define LOGE (FORMAT ,...) __android_log_print(ANDROID_LOG_ERROR, TAG, FORMAT, ##__VA_ARGS__);
17+
18+ //播放倍率
19+ float play_rate = 1 ;
20+ //视频总时长
21+ long duration = 0 ;
22+
23+ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_VideoPlayer_play
24+ (JNIEnv * env , jclass clazz , jstring filePath , jobject surface ){
25+
26+ const char * file_name = (* env )-> GetStringUTFChars (env , filePath , JNI_FALSE );
27+ //注册所有组件
28+ av_register_all ();
29+ //分配上下文
30+ AVFormatContext * pFormatCtx = avformat_alloc_context ();
31+ //打开视频文件
32+ if (avformat_open_input (& pFormatCtx , file_name , NULL , NULL )!= 0 ) {
33+ LOGE ("Couldn't open file:%s\n" , file_name );
34+ return -1 ;
35+ }
36+ //检索多媒体流信息
37+ if (avformat_find_stream_info (pFormatCtx , NULL )< 0 ) {
38+ LOGE ("Couldn't find stream information." );
39+ return -1 ;
40+ }
41+ //寻找视频流的第一帧
42+ int videoStream = -1 , i ;
43+ for (i = 0 ; i < pFormatCtx -> nb_streams ; i ++ ) {
44+ if (pFormatCtx -> streams [i ]-> codec -> codec_type == AVMEDIA_TYPE_VIDEO
45+ && videoStream < 0 ) {
46+ videoStream = i ;
47+ }
48+ }
49+ if (videoStream == -1 ) {
50+ LOGE ("couldn't find a video stream." );
51+ return -1 ;
52+ }
53+
54+ //获取视频总时长
55+ if (pFormatCtx -> duration != AV_NOPTS_VALUE ) {
56+ duration = (long ) (pFormatCtx -> duration / AV_TIME_BASE );
57+ LOGE ("duration=%d" , duration );
58+ }
59+
60+ //获取codec上下文指针
61+ AVCodecContext * pCodecCtx = pFormatCtx -> streams [videoStream ]-> codec ;
62+ //寻找视频流的解码器
63+ AVCodec * pCodec = avcodec_find_decoder (pCodecCtx -> codec_id );
64+ if (pCodec == NULL ) {
65+ LOGE ("couldn't find Codec." );
66+ return -1 ;
67+ }
68+ if (avcodec_open2 (pCodecCtx , pCodec , NULL ) < 0 ) {
69+ LOGE ("Couldn't open codec." );
70+ return -1 ;
71+ }
72+ // 获取native window
73+ ANativeWindow * nativeWindow = ANativeWindow_fromSurface (env , surface );
74+ // 获取视频宽高
75+ int videoWidth = pCodecCtx -> width ;
76+ int videoHeight = pCodecCtx -> height ;
77+ // 设置native window的buffer大小,可自动拉伸
78+ ANativeWindow_setBuffersGeometry (nativeWindow , videoWidth , videoHeight , WINDOW_FORMAT_RGBA_8888 );
79+ ANativeWindow_Buffer windowBuffer ;
80+ if (avcodec_open2 (pCodecCtx , pCodec , NULL )< 0 ) {
81+ LOGE ("Couldn't open codec." );
82+ return -1 ;
83+ }
84+ //申请内存
85+ AVFrame * pFrame = av_frame_alloc ();
86+ AVFrame * pFrameRGBA = av_frame_alloc ();
87+ if (pFrameRGBA == NULL || pFrame == NULL ) {
88+ LOGE ("Couldn't allocate video frame." );
89+ return -1 ;
90+ }
91+ // buffer中数据用于渲染,且格式为RGBA
92+ int numBytes = av_image_get_buffer_size (AV_PIX_FMT_RGBA , pCodecCtx -> width , pCodecCtx -> height , 1 );
93+
94+ uint8_t * buffer = (uint8_t * )av_malloc (numBytes * sizeof (uint8_t ));
95+ av_image_fill_arrays (pFrameRGBA -> data , pFrameRGBA -> linesize , buffer , AV_PIX_FMT_RGBA ,
96+ pCodecCtx -> width , pCodecCtx -> height , 1 );
97+
98+ // 由于解码出来的帧格式不是RGBA的,在渲染之前需要进行格式转换
99+ struct SwsContext * sws_ctx = sws_getContext (pCodecCtx -> width ,
100+ pCodecCtx -> height ,
101+ pCodecCtx -> pix_fmt ,
102+ pCodecCtx -> width ,
103+ pCodecCtx -> height ,
104+ AV_PIX_FMT_RGBA ,
105+ SWS_BILINEAR ,
106+ NULL ,
107+ NULL ,
108+ NULL );
109+
110+ int frameFinished ;
111+ AVPacket packet ;
112+
113+ while (av_read_frame (pFormatCtx , & packet )>=0 ) {
114+ //判断是否为视频流
115+ if (packet .stream_index == videoStream ) {
116+ //对该帧进行解码
117+ avcodec_decode_video2 (pCodecCtx , pFrame , & frameFinished , & packet );
118+ if (frameFinished ) {
119+ // lock native window
120+ ANativeWindow_lock (nativeWindow , & windowBuffer , 0 );
121+ // 格式转换
122+ sws_scale (sws_ctx , (uint8_t const * const * )pFrame -> data ,
123+ pFrame -> linesize , 0 , pCodecCtx -> height ,
124+ pFrameRGBA -> data , pFrameRGBA -> linesize );
125+ // 获取stride
126+ uint8_t * dst = windowBuffer .bits ;
127+ int dstStride = windowBuffer .stride * 4 ;
128+ uint8_t * src = pFrameRGBA -> data [0 ];
129+ int srcStride = pFrameRGBA -> linesize [0 ];
130+ // 由于window的stride和帧的stride不同,因此需要逐行复制
131+ int h ;
132+ for (h = 0 ; h < videoHeight ; h ++ ) {
133+ memcpy (dst + h * dstStride , src + h * srcStride , (size_t ) srcStride );
134+ }
135+ ANativeWindow_unlockAndPost (nativeWindow );
136+ }
137+ //延迟等待
138+ usleep ((unsigned long ) (1000 * 40 * play_rate ));
139+ }
140+ av_packet_unref (& packet );
141+ }
142+ //释放内存以及关闭文件
143+ av_free (buffer );
144+ av_free (pFrameRGBA );
145+ av_free (pFrame );
146+ avcodec_close (pCodecCtx );
147+ avformat_close_input (& pFormatCtx );
148+ return 0 ;
149+ }
150+
151+ //设置播放速率
152+ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_VideoPlayer_setPlayRate
153+ (JNIEnv * env , jclass clazz , jfloat playRate ){
154+ play_rate = playRate ;
155+ }
156+
157+ //获取视频总时长
158+ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_VideoPlayer_getDuration
159+ (JNIEnv * env , jclass clazz ){
160+ return duration ;
161+ }
0 commit comments