Android之集成FFmpeg到SDL项目

// FFmpeg 下载
// 本教程使用 FFmpeg 4.2.2 "Ada" 下进行 ...
http://ffmpeg.org/download.html#releases

// 拷贝库文件(Android之NDK编译FFmpeg教程)
// 拷贝 ffmpeg-4.2.2/include 至 jni/ffmpeg 
// 拷贝 arm64-v8、armeabi-v7a、... 至 jni/ffmpeg/libs
// 修改 app\jni\src\Android.mk

LOCAL_PATH := $(call my-dir)

FFMPEG_PATH := ../FFMPEG

include $(CLEAR_VARS)
LOCAL_MODULE :=  libavutil
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libavutil.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=  libswresample
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libswresample.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=  libswscale
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libswscale.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavcodec
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libavcodec.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavformat
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libavformat.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavfilter
LOCAL_SRC_FILES := ../ffmpeg/libs/$(TARGET_ARCH_ABI)/libavfilter.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := main
SDL_PATH := ../SDL
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := YourSourceHere.c
LOCAL_SHARED_LIBRARIES := SDL2 SDL2_image
LOCAL_SHARED_LIBRARIES += libavcodec libavfilter libavformat libavutil libswresample libswscale
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)
// 修改 app\jni\src\YourSourceHere.c 

#include <android/log.h>
#include "SDL.h"
#include "SDL_image.h"

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

#define LOG_TAG "xplay"

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG, __VA_ARGS__)

int main(int argc, char *argv[]) {
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Event event;
    //配置一个图像缩放的效果,linear效果更平滑,也叫抗锯齿
    //SDL_setenv(SDL_HINT_RENDER_SCALE_QUALITY,"linear",0);

    // FFMPEG ...
    LOGD("%s", av_version_info());

    // 初始化SDL
    IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    return 1;
    // 创建一个窗口
    window = SDL_CreateWindow("SDL_RenderClear" , SDL_WINDOWPOS_CENTERED,
                  SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_SHOWN);
    // 创建一个渲染器
    renderer = SDL_CreateRenderer(window, -1, 0);
    // 创建一个Surface,加载刚才下载的图片
    SDL_Surface *jpg = IMG_Load("a1.jpg");
//    SDL_Surface *bmp = SDL_LoadBMP("test.bmp");
//    //设置图片中的白色为透明色
//    SDL_SetColorKey(bmp, SDL_TRUE, 0xffffff);
    // 创建一个Texture
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, jpg);
    //清除所有事件
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
    //进入主循环
    while  (1) {
    Uint32 a = SDL_GetTicks();
    if  (SDL_PollEvent(&event)) {
        if  (event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
        break;
    }
    //使用红色填充背景
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    SDL_RenderClear(renderer);
    // 将纹理布置到渲染器
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    // 刷新屏幕
    SDL_RenderPresent(renderer);
    Uint32 b = SDL_GetTicks();
    LOGD(">>> %d", b - a);
    }
    // 释放Surface
    SDL_FreeSurface(jpg);
    //  释放Texture
    SDL_DestroyTexture(texture);
    //释放渲染器
    SDL_DestroyRenderer(renderer);
    //释放窗口
    SDL_DestroyWindow(window);
    //延时
    //SDL_Delay(8000);
    //退出
    SDL_Quit();
    return  0;
}