#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include <stdio.h>
#include <string.h>
#include <jni.h>
#include <android/log.h> typedef enum
{
FALSE = 0, TRUE = 1,
} C_BOOL; typedef unsigned char uint8_t; #define IN_BUFFER_SIZE 2048
#define TARGET_VIDEO_TYPE AV_PIX_FMT_RGB565 #define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
#define LOGD(format, ...) __android_log_print(ANDROID_LOG_DEBUG, "(-_-)", format, ##__VA_ARGS__) static C_BOOL __DecodeH264Video(FILE* fp_in, FILE* fp_out); JNIEXPORT jboolean JNICALL Java_com_zhoulee_h264decoder_MainActivity_DecodeH264Video(JNIEnv *env, jobject obj)
{
char filepath_in[] = "/data/video/bxjg_352x288.h264";
FILE *fp_in = fopen(filepath_in, "rb");
if (NULL == fp_in)
{
LOGE("open input h264 video file failed, filename [%s]", filepath_in);
return (jboolean) FALSE;
} char filepath_out[] = "/data/video/bxjg_352x288.rgb565";
FILE *fp_out = fopen(filepath_out, "wb");
if (NULL == fp_out)
{
LOGE("open output rgb video file failed, filename [%s]", filepath_out);
return (jboolean) FALSE;
} LOGD("open input and output file success"); if (TRUE == __DecodeH264Video(fp_in, fp_out))
{
LOGD("decode h264 video success");
}
else
{
LOGE("decode h264 video failed");
return (jboolean) FALSE;
} fclose(fp_in);
fclose(fp_out); return (jboolean) TRUE;
} typedef struct
{
AVCodec *pCodec;
AVCodecContext *pCodecCtx;
AVCodecParserContext *pCodecParserCtx;
AVFrame *pFrame;
AVFrame *pRGBFrame;
struct SwsContext *img_convert_ctx;
uint8_t *out_buffer;
uint8_t *cur_ptr;
AVPacket packet;
int cur_size;
int first_time;
int got_picture;
int dst_bpp; //bytes of pixel of destination format
} H264DecodeHandler; JNIEXPORT jint JNICALL Java_com_zhoulee_h264decoder_MainActivity_InitH264Decoder(JNIEnv *env, jobject obj)
{
return 0;
} int InitH264Decoder()
{
H264DecodeHandler *handle = (H264DecodeHandler*) malloc(sizeof(H264DecodeHandler));
if (NULL == handle)
{
LOGE("malloc H264DecodeHandler failed");
return 0;
} avcodec_register_all();
handle->pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (NULL == handle->pCodec)
{
LOGE("avcodec_find_decoder failed");
return 0;
} handle->pCodecCtx = avcodec_alloc_context3(handle->pCodec);
if (NULL == handle->pCodecCtx)
{
LOGE("avcodec_alloc_context3 failed");
return 0;
} handle->pCodecParserCtx = av_parser_init(AV_CODEC_ID_H264);
if (NULL == handle->pCodecParserCtx)
{
LOGE("av_parser_init failed");
return 0;
} if (avcodec_open2(handle->pCodecCtx, handle->pCodec, NULL) < 0)
{
LOGE("avcodec_open2 failed");
return 0;
} handle->pFrame = av_frame_alloc();
if (NULL == handle->pFrame)
{
LOGE("av_frame_alloc failed");
return 0;
} av_init_packet(&(handle->packet)); handle->cur_size = 0;
handle->got_picture = 0;
handle->first_time = 1;
handle->dst_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(TARGET_VIDEO_TYPE)) / 8; LOGD("bytes of pixel of destination format is [%d]", handle->dst_bpp); return (int) handle;
} void ReleaseH264Decoder(int decodeHandle)
{
H264DecodeHandler* handle = (H264DecodeHandler*) decodeHandle;
sws_freeContext(handle->img_convert_ctx);
av_frame_free(&(handle->pRGBFrame));
av_parser_close(handle->pCodecParserCtx);
av_frame_free(&(handle->pRGBFrame));
avcodec_close(handle->pCodecCtx);
av_free(handle->pCodecCtx);
free(handle);
} JNIEXPORT jint JNICALL Java_com_zhoulee_h264decoder_MainActivity_ReleaseH264Decoder(JNIEnv *env, jobject obj,
jint handle)
{ } typedef enum
{
DecFailed = 0, DecSuccess = 1, DecNeedMoreData = 2,
} DECODERESULT; DECODERESULT DecodeOneFrame(int decodeHandle)
{
H264DecodeHandler* handle = (H264DecodeHandler*) decodeHandle; int parse_len = av_parser_parse2(handle->pCodecParserCtx, handle->pCodecCtx, &(handle->packet.data),
&(handle->packet.size), handle->cur_ptr, handle->cur_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE); LOGD("current size is [%d], parse size is [%d], handle->packet.size [%d]",
handle->cur_size, parse_len, handle->packet.size); handle->cur_ptr += parse_len;
handle->cur_size -= parse_len; if (0 == handle->packet.size)
{
LOGD("handle->packet.size is 0, need read more data");
return DecNeedMoreData;
} int decode_ret = avcodec_decode_video2(handle->pCodecCtx, handle->pFrame, &(handle->got_picture),
&(handle->packet)); if (decode_ret < 0)
{
LOGE("avcodec_decode_video2 failed");
return DecFailed;
} if (handle->got_picture)
{
if (handle->first_time)
{
LOGD("CodecCtx->codec->long_name [%s]", handle->pCodecCtx->codec->long_name);
LOGD("CodecCtx->width [%d], CodecCtx->height [%d]", handle->pCodecCtx->width, handle->pCodecCtx->height); handle->img_convert_ctx = sws_getContext(handle->pCodecCtx->width, handle->pCodecCtx->height,
handle->pCodecCtx->pix_fmt, handle->pCodecCtx->width, handle->pCodecCtx->height, TARGET_VIDEO_TYPE,
SWS_BICUBIC, NULL, NULL, NULL); handle->pRGBFrame = av_frame_alloc(); handle->out_buffer = (uint8_t *) av_malloc(
avpicture_get_size(TARGET_VIDEO_TYPE, handle->pCodecCtx->width, handle->pCodecCtx->height)); avpicture_fill((AVPicture *) handle->pRGBFrame, handle->out_buffer, TARGET_VIDEO_TYPE,
handle->pCodecCtx->width, handle->pCodecCtx->height); handle->first_time = 0;
} sws_scale(handle->img_convert_ctx, (const uint8_t* const *) handle->pFrame->data, handle->pFrame->linesize, 0,
handle->pCodecCtx->height, handle->pRGBFrame->data, handle->pRGBFrame->linesize);
} return DecSuccess;
} DECODERESULT DecodeH264ToRGB(int decodeHandle, uint8_t* in_buffer, int in_len, uint8_t** out_buffer, int* out_len)
{
H264DecodeHandler* handle = (H264DecodeHandler*) decodeHandle; if(handle->cur_size == 0)
{
handle->cur_ptr = in_buffer;
handle->cur_size = in_len;
} DECODERESULT decode_result = DecodeOneFrame((int) handle); if (DecSuccess == decode_result)
{
*out_buffer = handle->pRGBFrame->data[0];
*out_len = handle->pCodecCtx->width * handle->pCodecCtx->height * handle->dst_bpp;
} return decode_result;
} C_BOOL __DecodeH264Video(FILE* fp_in, FILE* fp_out)
{
H264DecodeHandler* handle = (H264DecodeHandler*) InitH264Decoder();
if (handle == NULL)
{
LOGE("init h264 decoder failed");
return FALSE;
} uint8_t in_buffer[IN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
memset(in_buffer, 0, sizeof(in_buffer)); C_BOOL need_read_from_file = TRUE;
int read_size = 0;
int frame_index = 0;
uint8_t* out_buffer = NULL;
int out_len = 0;
while (TRUE)
{
if (need_read_from_file == TRUE)
{
if ((read_size = fread(in_buffer, 1, IN_BUFFER_SIZE, fp_in)) == 0)
{
LOGD("read all data from file, end!!!");
break;
}
LOGD("read from file size is [%d]", read_size);
} DECODERESULT decode_result = DecodeH264ToRGB((int) handle, in_buffer, read_size, &out_buffer, &out_len); if (DecFailed == decode_result)
{
LOGE("decode one frame failed");
break;
} if (DecSuccess == decode_result)
{
fwrite(out_buffer, 1, out_len, fp_out);
++frame_index;
LOGD("succeed to decode one frame, frame index [%d]", frame_index);
} if (DecNeedMoreData == decode_result)
{
need_read_from_file = TRUE;
}
else
{
need_read_from_file = FALSE;
}
} ReleaseH264Decoder((int) handle); return TRUE;
}

H264转成RGB24格式-2016.01.21的更多相关文章

  1. 详解H264视频格式-2016.01.28

    专业名词解释 VCL(Video Coding Layer)视频编码层 NAL(Network Abstraction Layer)网络提取层 SPS(Sequence Parameter Set) ...

  2. H264编码 封装成MP4格式 视频流 RTP封包

    H264编码 封装成MP4格式 视频流 RTP封包         分类:             多媒体编程              2013-02-20 21:31     3067人阅读    ...

  3. MySQL Binlog Mixed模式记录成Row格式

    背景: 一个简单的主从结构,主的binlog format是Mixed模式,在执行一条简单的导入语句时,通过mysqlbinlog导出发现记录的Binlog全部变成了Row的格式(明明设置的是Mixe ...

  4. [官方软件] Easy Sysprep v4.3.29.602 【系统封装部署利器】(2016.01.22)--skyfree大神

    [官方软件] Easy Sysprep v4.3.29.602 [系统封装部署利器](2016.01.22) Skyfree 发表于 2016-1-22 13:55:55 https://www.it ...

  5. FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM

    FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...

  6. 将jpg压缩成webp格式的图片

    cwebp名称 cwebp -压缩图像文件为的WebP文件概要 cwebp [选项] INPUT_FILE -o output_file.webp描述 cwebp压缩使用的WebP格式的图像.输入格式 ...

  7. 关于springmvc下服务器文件打包成zip格式下载功能

    关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储   版权声明:本文为博主 ...

  8. CAFFE学习笔记(四)将自己的jpg数据转成lmdb格式

    1 引言 1-1 以example_mnist为例,如何加载属于自己的测试集? 首先抛出一个问题:在example_mnist这个例子中,测试集是人家给好了的.那么如果我们想自己试着手写几个数字然后验 ...

  9. [转] 将DOS格式文本文件转换成UNIX格式

    点击此处阅读原文 用途说明 dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文 ...

随机推荐

  1. hive查询语句

    一. 为什么hive是数据仓库 hive局限于hdfs, 不能进行记录级别的增删改 hive底层的mapreduce启动耗时很长, 无法做到传统数据库的秒查, 只适合离线分析 hive不支持事务, 无 ...

  2. C#之线程和并发

    建议大家对C#撑握的不错的时候,可以去看一些开源项目.走技术这条路,就要耐得住寂寞(群里双休日说要让群主找妹子进群的人必须反思),练好内功.不撑握C#高级知识点,别想看懂优秀的开源项目,更别指望吸收其 ...

  3. DG_Oracle DataGuard作用和概念(概念)

    2014-06-03 Created By BaoXinjian  

  4. POJ-3461 Oulipo(KMP,模式串在主串中出现次数)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

  5. UCOS-消息队列(学习笔记)

    消息队列的核心是一个消息的指针数组,UCOS系统初始化时根据OS_CONFI.h中的最大队列个数定义这么多个消息队列(队列的结构)并将他们串联成空的链表,创建消息队列时从空链表中抽出一个并用指针数组的 ...

  6. Laravel5.0 CSRFチェックを無効化(修改后可以像5.1以上那样从CSRF保护中排除指定URL)

    Laravel5では全てのPOSTに勝手にCSRFチェックが付いてきます.便利と言えば便利ですが.Laravel外からのPOSTを受け取りたいときなど大迷惑です. CSRFチェックを排除する方法が何故 ...

  7. this web application instance has been stopped already解决办法

    重启tomcat的时候出错 Illegal access: this web application instance has been stopped already.  Could not loa ...

  8. linux下批量修改文件名之rename

    最近因为突然用到需匹配更换文件名,发现rename命令真是 简单好用,和sed语法及vim 替换很相似. 1. 更改文件名后缀 rename 's/\.txt/\.html/' * 2.增加文件名后缀 ...

  9. 衔接UI线程和管理后台工作线程的类(多线程、异步调用)

    一个不错的UI多线程操作类 http://www.cnblogs.com/net66/archive/2005/08/03/206132.html

  10. 【转】group by多个字段理解

    来源:http://uule.iteye.com/blog/1569262 首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使 ...