1.获得ffmpeg 总上下文,打开媒体注意:配置权限,授权文件读写权限
AVFormatContext *avFormatContext = avformat_alloc_context();
AVDictionary *opts = NULL;
av_dict_set(&opts, "timeout", "3000000", 0);
int ret = avformat_open_input(&avFormatContext, path_, NULL, &opts);
if (ret) {
// 配置权限,授权文件读写权限
LOGE("打开媒体失败:%s", av_err2str(ret));
return;
}
- 查找媒体中的流信息
ret = avformat_find_stream_info(avFormatContext, 0);
if (ret < 0) {
LOGE("查找媒体中的流信息失败:%s", av_err2str(ret));
//TODO 通过反射通知java
return;
}
avFormatContext->nb_streams 文件流中存在多个流,视频流或音频流
//3获取媒体流(音频或视频)
AVStream *stream = avFormatContext->streams[i];
//4获取编解码这段流的参数
AVCodecParameters *codecParameters = stream->codecpar;
//5 通过参数中的id(编解码的方式),来查找当前流的解码器
AVCodec *codec = avcodec_find_decoder(codecParameters->codec_id);
if (!codec) {
LOGE("查找当前流的解码器失败");
//TODO 反射通知java
return;
}
//6 创建解码器上下文
AVCodecContext *codecContext = avcodec_alloc_context3(codec);
//7 设置解码器上下文的参数
ret = avcodec_parameters_to_context(codecContext, codecParameters);
if (ret < 0) {
//TODO 反射通知java
LOGE("设置解码器上下文的参数失败:%s", av_err2str(ret));
return;
}
//8 打开解码器
ret = avcodec_open2(codecContext, codec, 0);
if (ret) {
//TODO 反射通知java
LOGE("打开解码器失败:%s", av_err2str(ret));
return;
}
判断视频流或音频流做相应处理
//判断流类型(音频还是视频?)
if (codecParameters->codec_type == AVMEDIA_TYPE_AUDIO) {
//音频
LOGE("音频流");
} else if (codecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {
LOGE("视频流");
videoDecode(aNativeWindow, avFormatContext, ret, video_stream_idx, i, codecContext);
}
视频解码
void
videoDecode(ANativeWindow *aNativeWindow, AVFormatContext *avFormatContext, int ret,
int video_stream_idx, int i, AVCodecContext *&codecContext) {
video_stream_idx = i;
//视频
AVPacket *packet = av_packet_alloc();
// 重视速度:fast_bilinear,point
//重视质量cubic,spline,lanczos
//缩写:
//重视速度:fast_bilinear,point
//重视质量gauss,bilinear
//重视锐度cubic,spline,lanczos
SwsContext *swsContext = sws_getContext(codecContext->width, codecContext->height,
codecContext->pix_fmt,
codecContext->width, codecContext->height,
AV_PIX_FMT_RGBA, SWS_BILINEAR, 0, 0, 0);
ANativeWindow_setBuffersGeometry(aNativeWindow, codecContext->width,
codecContext->height,
WINDOW_FORMAT_RGBA_8888);
ANativeWindow_Buffer outBuffer;
//从视频流中读取数据包
while (av_read_frame(avFormatContext, packet) == 0) {
if (packet->stream_index == video_stream_idx) {
//拿到了视频数据包(编码压缩了的),需要把数据包给解码器进行解码
ret = avcodec_send_packet(codecContext, packet);
if (ret) {
LOGE("avcodec_send_packet %d", ret);
break;
}
AVFrame *frame = av_frame_alloc();
int ret = avcodec_receive_frame(codecContext, frame);
LOGE("receive %d", ret);
if (ret == AVERROR(EAGAIN)) {
continue;
} else if (ret < 0) {
break;
}
//接收的容器
uint8_t *dst_data[4];
//每一行的地址
int dst_linesize[4];
av_image_alloc(dst_data, dst_linesize, codecContext->width,
codecContext->height,
AV_PIX_FMT_RGBA, 1);
sws_scale(swsContext, frame->data, frame->linesize, 0, frame->height, dst_data,
dst_linesize);
//AVFrame yum -->image-->dst_data-->渲染surfaceView
//锁定window
ANativeWindow_lock(aNativeWindow, &outBuffer, 0);
//渲染
uint8_t *firstWindow = static_cast(outBuffer.bits);
//输入源(rgb)
uint8_t *src_data = dst_data[0];
//拿一行有多少字节RGBA
int destStride = outBuffer.stride * 4;
int src_linesize = dst_linesize[0];
for (int i = 0; i < outBuffer.height; ++i) {
//内存拷贝
memcpy(firstWindow + i * destStride, src_data + i * src_linesize,
destStride);
}
//window解锁
ANativeWindow_unlockAndPost(aNativeWindow);
usleep(1000 * 16);
av_frame_free(&frame);
}
}
av_packet_free(&packet);
avcodec_free_context(&codecContext);
//内存释放
ANativeWindow_release(aNativeWindow);
}