如何在Android用FFmpeg+SDL2.0解码显示图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Android用FFmpeg+SDL2.0解码显示图像相关的知识,希望对你有一定的参考价值。

参考技术A 创建一个VideoPicture结构体用来保存解码出来的图像。

LOCAL_PATH := $(call my-dir)
###########################
#
# SDL shared library
#
###########################
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/src/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \
$(LOCAL_PATH)/src/atomic/SDL_atomic.c \
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
$(wildcard $(LOCAL_PATH)/src/core/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) \
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/filesystem/dummy/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/test/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
include $(BUILD_SHARED_LIBRARY)
###########################
#
# SDL static library
#
###########################
#LOCAL_MODULE := SDL2_static
#LOCAL_MODULE_FILENAME := libSDL2
#LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c
#LOCAL_LDLIBS :=
#LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
#include $(BUILD_STATIC_LIBRARY)

二、参考[原]如何在Android用FFmpeg解码图像, 在工程中新建一个ffmpeg文件夹,将与ffmpeg相关的头文件include进来。ffmpeg文件夹下的Android.mk内容:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := /path/to/build/ffmpeg/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)

三、新建player文件夹,用来编写解码与显示文件。player.c文件内容:
/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include <android/native_window_jni.h>
#include "SDL.h"
#include "SDL_thread.h"
#include "SDL_events.h"
#include "../include/logger.h"
#include "../ffmpeg/include/libavcodec/avcodec.h"
#include "../ffmpeg/include/libavformat/avformat.h"
#include "../ffmpeg/include/libavutil/pixfmt.h"
#include "../ffmpeg/include/libswscale/swscale.h"
int main(int argc, char *argv[])
char *file_path = argv[1];
LOGI("file_path:%s", file_path);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
uint8_t *out_buffer;
SDL_Texture *bmp = NULL;
SDL_Window *screen = NULL;
SDL_Rect rect;
SDL_Event event;
static struct SwsContext *img_convert_ctx;
int videoStream, i, numBytes;
int ret, got_picture;
av_register_all();
pFormatCtx = avformat_alloc_context();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
LOGE("Could not initialize SDL - %s. \n", SDL_GetError());
exit(1);

if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0)
LOGE("can't open the file. \n");
return -1;

if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
LOGE("Could't find stream infomation.\n");
return -1;

videoStream = 1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
videoStream = i;


LOGI("videoStream:%d", videoStream);
if (videoStream == -1)
LOGE("Didn't find a video stream.\n");
return -1;

pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
LOGE("Codec not found.\n");
return -1;

if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
LOGE("Could not open codec.\n");
return -1;

pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
//---------------------------init sdl---------------------------//
screen = SDL_CreateWindow("My Player Window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, pCodecCtx->width, pCodecCtx->height,
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(screen, -1, 0);
bmp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
//-------------------------------------------------------------//

img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
rect.x = 0;
rect.y = 0;
rect.w = pCodecCtx->width;
rect.h = pCodecCtx->height;
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
av_dump_format(pFormatCtx, 0, file_path, 0);
while (av_read_frame(pFormatCtx, packet) >= 0)
if (packet->stream_index == videoStream)
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
packet);
if (ret < 0)
LOGE("decode error.\n");
return -1;

LOGI("got_picture:%d", got_picture);
if (got_picture)
sws_scale(img_convert_ctx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data,
pFrameYUV->linesize);
////iPitch 计算yuv一行数据占的字节数
SDL_UpdateTexture(bmp, &rect, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmp, &rect, &rect);
SDL_RenderPresent(renderer);

SDL_Delay(50);

av_free_packet(packet);
SDL_PollEvent(&event);
switch (event.type)
case SDL_QUIT:
SDL_Quit();
exit(0);
break;
default:
break;


SDL_DestroyTexture(bmp);
av_free(out_buffer);
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;


四、编写player文件夹下面的Android makefile,内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := player
SDL_PATH := ../SDL
FFMPEG_PATH := ../ffmpeg
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(SDL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(FFMPEG_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c
LOCAL_SRC_FILES += player.c
LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_SHARED_LIBRARIES += ffmpeg
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)

五、修改SDLActivity.java文件内容,用来加载libffmpeg.so以及libplayer.so,以及修改用来解码的文件路径。

// Load the .so
static
System.loadLibrary("ffmpeg");
System.loadLibrary("SDL2");
//System.loadLibrary("SDL2_image");
//System.loadLibrary("SDL2_mixer");
//System.loadLibrary("SDL2_net");
//System.loadLibrary("SDL2_ttf");
System.loadLibrary("player");


/**
Simple nativeInit() runnable
*/
class SDLMain implements Runnable
@Override
public void run()
// Runs SDL_main()
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
SDLActivity.nativeInit("/sdcard/a.mp4");
//Log.v("SDL", "SDL thread terminated");

本回答被提问者和网友采纳

最简单的基于FFmpeg的移动端样例附件:SDL Android HelloWorld

=====================================================

最简单的基于FFmpeg的移动端样例系列文章列表:

最简单的基于FFmpeg的移动端样例:Android HelloWorld

最简单的基于FFmpeg的移动端样例:Android 视频解码器

最简单的基于FFmpeg的移动端样例:Android 视频解码器-单个库版

最简单的基于FFmpeg的移动端样例:Android 推流器

最简单的基于FFmpeg的移动端样例:Android 视频转码器

最简单的基于FFmpeg的移动端样例附件:Android 自带播放器

最简单的基于FFmpeg的移动端样例附件:SDL Android HelloWorld

最简单的基于FFmpeg的移动端样例:IOS HelloWorld

最简单的基于FFmpeg的移动端样例:IOS 视频解码器

最简单的基于FFmpeg的移动端样例:IOS 推流器

最简单的基于FFmpeg的移动端样例:IOS 视频转码器

最简单的基于FFmpeg的移动端样例附件:IOS自带播放器

最简单的基于FFmpeg的移动端样例:Windows Phone HelloWorld

=====================================================


本文记录一个安卓平台下SDL的样例。

该样例读取并显示了一张BMP图片。通过该样例能够了解SDL在安卓平台下的使用。本文中使用的SDL版本号为2.0。

技术分享图片


Android程序SDL2类库使用说明

简单记录一下Android程序使用SDL2类库的方法。这部分的信息能够參考SDL2源码文件夹中的README-android.txt文件。

SDL2使用的步骤能够分为下面步骤:


(1)配置Androidproject

a)?新建Androidproject

能够直接使用SDL2源码文件夹中的android-projectproject,也能够自己新建project。假设自己新建project,则须要拷贝android-projectproject中src文件夹下的SDLActivity.java文件。该文件里的SDLActivity定义比較长。眼下还没有研究过。

b)?拷贝SDL2源码
将SDL2源码文件夹中下列内容拷贝至Androidproject的jni文件夹下:
src文件夹
include文件夹
Android.mk

(2)编译C语言代码

a)?编写C语言代码

在Androidproject的jni文件夹下新建C语言文件并编写调用SDL2的代码。在这里须要注意C语言代码的主函数名称和命令行程序是一样的。依旧是“main(int argc, char *argv[])”。

b)?改动Android.mk文件

在Android.mk文件后面追加一段代码,用来编译一个libSDL2main.so的库。该库能够调用自己的C语言程序。在这里须要注意。“libSDL2main”这个名称是能够任意起的。可是要和SDLActivity中LoadLibrary()函数中的库名称相相应(默认名称应该是libmain.so)。代码内容例如以下所看到的。

#libSDL2main=======================================
# Lei Xiaohua
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2main
SDL_PATH := ./
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c 	$(SDL_PATH)/simplest_showbmp.c
LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)

PS:上述代码改动自SDL2源码文件夹中android-projectproject的jni\src文件夹下的Android.mk文件。



c)?编写Application.mk文件(可选)
Application.mk中的APP_ABI设定了编译后库文件支持的指令集。默认使用“armeabi”,这里使用默认值就能够了。


d)?执行ndk-build
编写完C语言代码和Android.mk之后就能够执行ndk-build命令进行编译了。

ndk-build命令执行成功后,会在根文件夹下的“libs/armeabi”文件夹中生成2个库文件:

libSDL2.so
libSDL2main.so
接下来就能够在Android手机或者虚拟机上对整个Androidproject进行測试了。

源码

项目的文件夹结构如图所看到的。Java源码位于src文件夹。而C代码位于jni文件夹。

技术分享图片

Android程序Java端代码位于src\org\libsdl\app\SDLActivity.java。

该Activity取自于SDL2源码文件夹中的android-projectproject。在这里不再记录。

jni文件夹中的src和include文件夹各自是SDL2的源码文件夹中的src文件夹和include文件夹。

C语言端源码位于jni/simplest_showbmp.c。例如以下所看到的。该源码读取了存储卡中的一张test.bmp文件,并显示出来。

/**
 * 移植SDL到安卓平台的HelloWorld程序
 * Simplest SDL Android Helloworld
 *
 * 雷霄骅 Lei Xiaohua
 * [email protected]
 * 中国传媒大学/数字电视技术
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 *
 *
 * 本程序是移植SDL到安卓平台的最简单程序。它能够读取并显示一张BMP图片。

* * This software is the simplest program transplant SDL2 to Android platform. * It shows a BMP file on the screen. */ #ifdef __ANDROID__ #include <jni.h> #include <android/log.h> #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , "(^_^)", __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , "(^_^)", __VA_ARGS__) #else #define LOGE(format, ...) printf("(>_<) " format "\n", ##__VA_ARGS__) #define LOGI(format, ...) printf("(^_^) " format "\n", ##__VA_ARGS__) #endif #include "SDL.h" #include "SDL_log.h" #include "SDL_main.h" int main(int argc, char *argv[]) { struct SDL_Window *window = NULL; struct SDL_Renderer *render = NULL; struct SDL_Surface *bmp = NULL; struct SDL_Texture *texture = NULL; char *filepath = "/storage/emulated/0/test.bmp"; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1) { LOGE("SDL_Init failed %s", SDL_GetError()); } window = SDL_CreateWindow("SDL HelloWorld!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); if (window == NULL) { LOGE("SDL_CreateWindow failed %s", SDL_GetError()); } render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (render == NULL) { LOGE("SDL_CreateRenderer failed %s", SDL_GetError()); } bmp = SDL_LoadBMP(filepath); if (bmp == NULL) { LOGE("SDL_LoadBMP failed: %s", SDL_GetError()); } texture = SDL_CreateTextureFromSurface(render, bmp); SDL_FreeSurface(bmp); SDL_RenderClear(render); SDL_RenderCopy(render, texture, NULL, NULL); SDL_RenderPresent(render); SDL_Delay(10000); SDL_DestroyTexture(texture); SDL_DestroyRenderer(render); SDL_DestroyWindow(window); //Quit SDL SDL_Quit(); return 0; }


Android.mk文件位于jni/Android.mk,例如以下所看到的。
LOCAL_PATH := $(call my-dir)

###########################
#
# SDL shared library
#
###########################

include $(CLEAR_VARS)

LOCAL_MODULE := SDL2

LOCAL_C_INCLUDES := $(LOCAL_PATH)/include

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)

LOCAL_SRC_FILES := 	$(subst $(LOCAL_PATH)/,, 	$(wildcard $(LOCAL_PATH)/src/*.c) 	$(wildcard $(LOCAL_PATH)/src/audio/*.c) 	$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) 	$(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) 	$(LOCAL_PATH)/src/atomic/SDL_atomic.c 	$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm 	$(wildcard $(LOCAL_PATH)/src/core/android/*.c) 	$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) 	$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) 	$(wildcard $(LOCAL_PATH)/src/events/*.c) 	$(wildcard $(LOCAL_PATH)/src/file/*.c) 	$(wildcard $(LOCAL_PATH)/src/haptic/*.c) 	$(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) 	$(wildcard $(LOCAL_PATH)/src/joystick/*.c) 	$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) 	$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) 	$(wildcard $(LOCAL_PATH)/src/power/*.c) 	$(wildcard $(LOCAL_PATH)/src/power/android/*.c) 	$(wildcard $(LOCAL_PATH)/src/filesystem/dummy/*.c) 	$(wildcard $(LOCAL_PATH)/src/render/*.c) 	$(wildcard $(LOCAL_PATH)/src/render/*/*.c) 	$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) 	$(wildcard $(LOCAL_PATH)/src/thread/*.c) 	$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) 	$(wildcard $(LOCAL_PATH)/src/timer/*.c) 	$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) 	$(wildcard $(LOCAL_PATH)/src/video/*.c) 	$(wildcard $(LOCAL_PATH)/src/video/android/*.c)     $(wildcard $(LOCAL_PATH)/src/test/*.c))

LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid

include $(BUILD_SHARED_LIBRARY)

###########################
#
# SDL static library
#
###########################

LOCAL_MODULE := SDL2_static

LOCAL_MODULE_FILENAME := libSDL2

LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c

LOCAL_LDLIBS := 
LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid

include $(BUILD_STATIC_LIBRARY)


#libSDL2main=======================================
# Lei Xiaohua
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2main
SDL_PATH := ./
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c 	$(SDL_PATH)/simplest_showbmp.c
LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)

执行结果

App在手机上执行后的结果例如以下图所看到的。能够看出App读取了test.bmp文件而且显示出来。

注意须要把BMP文件拷贝至存储卡相应的文件夹中。程序默认情况下会读取根文件夹下的“test.bmp”文件。


技术分享图片

下载


simplest ffmpeg mobile

项目主页

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile

开源中国:https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile

SourceForge:https://sourceforge.net/projects/simplestffmpegmobile/


CSDNproject下载地址:http://download.csdn.net/detail/leixiaohua1020/8924391


本解决方式包括了使用FFmpeg在移动端处理多媒体的各种样例:
[Android]
simplest_android_player: 基于安卓接口的视频播放器
simplest_ffmpeg_android_helloworld: 安卓平台下基于FFmpeg的HelloWorld程序
simplest_ffmpeg_android_decoder: 安卓平台下最简单的基于FFmpeg的视频解码器
simplest_ffmpeg_android_decoder_onelib: 安卓平台下最简单的基于FFmpeg的视频解码器-单库版
simplest_ffmpeg_android_streamer: 安卓平台下最简单的基于FFmpeg的推流器
simplest_ffmpeg_android_transcoder: 安卓平台下移植的FFmpeg命令行工具
simplest_sdl_android_helloworld: 移植SDL到安卓平台的最简单程序
[IOS]
simplest_ios_player: 基于IOS接口的视频播放器
simplest_ffmpeg_ios_helloworld: IOS平台下基于FFmpeg的HelloWorld程序
simplest_ffmpeg_ios_decoder: IOS平台下最简单的基于FFmpeg的视频解码器
simplest_ffmpeg_ios_streamer: IOS平台下最简单的基于FFmpeg的推流器
simplest_ffmpeg_ios_transcoder: IOS平台下移植的ffmpeg.c命令行工具
simplest_sdl_ios_helloworld: 移植SDL到IOS平台的最简单程序





以上是关于如何在Android用FFmpeg+SDL2.0解码显示图像的主要内容,如果未能解决你的问题,请参考以下文章

SDL_Overlay 的 SDL2.0 替代方案

如何利用批处理和ffmpeg视频解帧将多个mp4文件解成图片?

Android 音视频ffmpeg

如何在Android用FFmpeg解码图像

如何在android上使用ffmpeg录制视频?

最简单的基于FFMPEG的Helloworld程序