android 集成ffmpeg 編碼攝像頭數(shù)據(jù) mpegts格式 mpegvideo編碼 出錯(cuò)
第一步 初始化
Java_com_deerlive_jni_ffmpeg_FFmpegHandle_initVideo(JNIEnv *env, jobject instance,
jstring url_) {
const char *out_path = env->GetStringUTFChars(url_, 0);
logd(out_path);
//計(jì)算yuv數(shù)據(jù)的長度
yuv_width = width;
yuv_height = height;
y_length = width * height;
uv_length = width * height / 4;
av_register_all();
//推流就需要初始化網(wǎng)絡(luò)協(xié)議
avformat_network_init();
//初始化AVFormatContext
avformat_alloc_output_context2(&ofmt_ctx, NULL, "mpegts", out_path);
if(!ofmt_ctx) {
loge("Could not create output context\n");
return -1;
}
//尋找編碼器,這里用的就是x264的那個(gè)編碼器了
pCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
if(!pCodec) {
loge("Can not find encoder!\n");
return -1;
}
//初始化編碼器的context
pCodecCtx = avcodec_alloc_context3(pCodec);
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; //指定編碼格式
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = fps;
pCodecCtx->bit_rate = 800000;
pCodecCtx->gop_size = 300;
if(ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;
pCodecCtx->max_b_frames = 0;
AVDictionary *dicParams = NULL;
av_dict_set(&dicParams, "preset", "ultrafast", 0);
av_dict_set(&dicParams, "tune", "zerolatency", 0);
//打開編碼器
if(avcodec_open2(pCodecCtx, pCodec, &dicParams) < 0) {
loge("Failed to open encoder!\n");
return -1;
}
//新建輸出流
video_st = avformat_new_stream(ofmt_ctx, pCodec);
if(!video_st) {
loge("Failed allocation output stream\n");
return -1;
}
video_st->time_base.num = 1;
video_st->time_base.den = fps;
//復(fù)制一份編碼器的配置給輸出流
avcodec_parameters_from_context(video_st->codecpar, pCodecCtx);
//打開輸出流
int ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_WRITE);
if(ret < 0) {
loge("Could not open output URL %s");
return -1;
}
// ret = avformat_write_header(ofmt_ctx, NULL);
// if(ret < 0) {
// loge("Error occurred when open output URL\n");
// return -1;
// }
pFrameYUV = av_frame_alloc();
uint8_t *out_buffer = (uint8_t *) av_malloc(av_image_get_buffer_size(pCodecCtx->pix_fmt, width, height, 1));
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, pCodecCtx->pix_fmt, width, height, 1);
return 0;
}
第二步 獲取攝像頭原始數(shù)據(jù) 開始編碼 (回調(diào)形式)
extern "C"
JNIEXPORT jint JNICALL
Java_com_deerlive_jni_ffmpeg_FFmpegHandle_onFrameCallback(JNIEnv *env, jobject instance,
jbyteArray buffer_) {
startTime = av_gettime();
jbyte *in = env->GetByteArrayElements(buffer_, NULL);
int ret = 0;
//初始化一個(gè)幀的數(shù)據(jù)結(jié)構(gòu),用于編碼用
//指定AV_PIX_FMT_YUV420P這種格式的
//安卓攝像頭數(shù)據(jù)為NV21格式,此處將其轉(zhuǎn)換為YUV420P格式
////N21 0~width * height是Y分量, width*height~ width*height*3/2是VU交替存儲(chǔ)
//復(fù)制Y分量的數(shù)據(jù)
memcpy(pFrameYUV->data[0], in, y_length); //Y
for (int i = 0; i < uv_length; i++) {
//將v數(shù)據(jù)存到第三個(gè)平面
*(pFrameYUV->data[2] + i) = *(in + y_length + i * 2);
//將U數(shù)據(jù)存到第二個(gè)平面
*(pFrameYUV->data[1] + i) = *(in + y_length + i * 2 + 1);
}
pFrameYUV->format = pCodecCtx->pix_fmt;
pFrameYUV->width = yuv_width;
pFrameYUV->height = yuv_height;
pFrameYUV->pts = count;
//pFrameYUV->pts = (1.0 / 30) * 90 * count; ////////////////////////////
//例如對(duì)于H.264來說。1個(gè)AVPacket的data通常對(duì)應(yīng)一個(gè)NAL
//初始化AVPacket
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
// __android_log_print(ANDROID_LOG_WARN, "eric", "編碼前時(shí)間:%lld",
// (long long) ((av_gettime() - startTime) / 1000));
//開始編碼YUV數(shù)據(jù)
/* send the frame to the encoder */
ret = avcodec_send_frame(pCodecCtx, pFrameYUV);
if (ret < 0) {
logi("Error sending a frame for encoding\n");
}
ret = avcodec_receive_packet(pCodecCtx, &enc_pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
logi("Error during encodg\n");
else if (ret < 0) {
logi("Error during encoding\n");
}
//av_frame_free(&pFrameYUV);
if (ret != 0 || enc_pkt.size <= 0) {
loge("avcodec_receive_packet error");
return -2;
}
enc_pkt.stream_index = video_st->index;
AVRational time_base = ofmt_ctx->streams[0]->time_base;
AVRational r_frame_rate1 = pCodecCtx->framerate;
AVRational time_base_q = {1, AV_TIME_BASE};
int64_t calc_duration = (double)(AV_TIME_BASE) * (1 / av_q2d(r_frame_rate1));
//enc_pkt.pts = count * (video_st->time_base.den) / ((video_st->time_base.num) * fps);
enc_pkt.pts = av_rescale_q(count * calc_duration, time_base_q, time_base);
enc_pkt.dts = enc_pkt.pts;
//enc_pkt.duration = (video_st->time_base.den) / ((video_st->time_base.num) * fps);
enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base);
enc_pkt.pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret != 0) {
loge("av_interleaved_write_frame failed");
}
count++;
env->ReleaseByteArrayElements(buffer_, in, 0);
return 0;
}
報(bào)錯(cuò)信息 :
avcodec_send_frame send error
avcodec_receive_packet error
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。