前面本來說是做h264編碼的 研究了兩天發現ffmpeg裡的h264編碼似乎是要信賴第三方庫x264 還是怎麼簡單怎麼來吧所以就整了個mpeg編碼 ffmpeg移植前面我有一篇ffmpeg解碼裡已經給了 具體連結在這http://blog.csdn.net/hclydao/article/details/18546757
怎麼使用那裡面也已經說了 這裡主要是通過ffmpeg將yuv422格式轉換成rgb 然後就是yuv422轉成mpeg格式 接前面幾篇 擷取到yuv422資料後 為了能顯示出來 所以先轉換成rgb565的資料 介面函數如下
/** yuv to rgb*/JNIEXPORT jint JNICALL Java_com_hclydao_webcam_Ffmpeg_yuvtorgb(JNIEnv * env, jclass obj,const jbyteArray yuvdata, jbyteArray rgbdata,const jint dwidth,const jint dheight){jbyte *ydata = (jbyte*)(*env)->GetByteArrayElements(env, yuvdata, 0);jbyte *rdata = (jbyte*)(*env)->GetByteArrayElements(env, rgbdata, 0);AVFrame * rpicture=NULL;AVFrame * ypicture=NULL;struct SwsContext *swsctx = NULL;rpicture=avcodec_alloc_frame();ypicture=avcodec_alloc_frame();avpicture_fill((AVPicture *) rpicture, (uint8_t *)rdata, PIX_FMT_RGB565,dwidth,dheight);avpicture_fill((AVPicture *) ypicture, (uint8_t *)ydata, AV_PIX_FMT_YUYV422,mwidth,mheight);swsctx = sws_getContext(mwidth,mheight, AV_PIX_FMT_YUYV422,dwidth, dheight,PIX_FMT_RGB565, SWS_BICUBIC, NULL, NULL, NULL);sws_scale(swsctx,(const uint8_t* const*)ypicture->data,ypicture->linesize,0,mheight,rpicture->data,rpicture->linesize);sws_freeContext(swsctx);av_free(rpicture);av_free(ypicture);(*env)->ReleaseByteArrayElements(env, yuvdata, ydata, 0);(*env)->ReleaseByteArrayElements(env, rgbdata, rdata, 0);return 0;}
然後就是mpeg編碼了 網上說ffmpeg只能將yuv420p的編碼 所以要先將yuv422轉成yuv420p後在進行編碼 相關介面函數如下
AVCodecContext *pCodecCtx= NULL;AVPacket avpkt;FILE * video_file;unsigned char *outbuf=NULL;unsigned char *yuv420buf=NULL;static int outsize=0;/** encording init*/JNIEXPORT jint JNICALL Java_com_hclydao_webcam_Ffmpeg_videoinit(JNIEnv * env, jclass obj,jbyteArray filename){LOGI("%s\n",__func__);AVCodec * pCodec=NULL;avcodec_register_all();pCodec=avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);if(pCodec == NULL) {LOGE("++++++++++++codec not found\n");return -1;} pCodecCtx=avcodec_alloc_context3(pCodec); if (pCodecCtx == NULL) { LOGE("++++++Could not allocate video codec context\n"); return -1; } /* put sample parameters */ pCodecCtx->bit_rate = 400000; /* resolution must be a multiple of two */ pCodecCtx->width = mwidth; pCodecCtx->height = mheight; /* frames per second */ pCodecCtx->time_base= (AVRational){1,25}; pCodecCtx->gop_size = 10; /* emit one intra frame every ten frames */ pCodecCtx->max_b_frames=1; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;//AV_PIX_FMT_YUYV422; /* open it */ if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { LOGE("+++++++Could not open codec\n"); return -1; }outsize = mwidth * mheight*2;outbuf = malloc(outsize*sizeof(char));yuv420buf = malloc(outsize*sizeof(char));jbyte *filedir = (jbyte*)(*env)->GetByteArrayElements(env, filename, 0); if ((video_file = fopen(filedir, "wb")) == NULL) { LOGE("++++++++++++open %s failed\n",filedir); return -1; }(*env)->ReleaseByteArrayElements(env, filename, filedir, 0);return 1;}JNIEXPORT jint JNICALL Java_com_hclydao_webcam_Ffmpeg_videostart(JNIEnv * env, jclass obj,jbyteArray yuvdata){int frameFinished=0,size=0;jbyte *ydata = (jbyte*)(*env)->GetByteArrayElements(env, yuvdata, 0);AVFrame * yuv420pframe=NULL;AVFrame * yuv422frame=NULL;struct SwsContext *swsctx = NULL;yuv420pframe=avcodec_alloc_frame();yuv422frame=avcodec_alloc_frame();avpicture_fill((AVPicture *) yuv420pframe, (uint8_t *)yuv420buf, AV_PIX_FMT_YUV420P,mwidth,mheight);avpicture_fill((AVPicture *) yuv422frame, (uint8_t *)ydata, AV_PIX_FMT_YUYV422,mwidth,mheight);swsctx = sws_getContext(mwidth,mheight, AV_PIX_FMT_YUYV422,mwidth, mheight,AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);sws_scale(swsctx,(const uint8_t* const*)yuv422frame->data,yuv422frame->linesize,0,mheight,yuv420pframe->data,yuv420pframe->linesize);av_init_packet(&avpkt);size = avcodec_encode_video2(pCodecCtx, &avpkt, yuv420pframe, &frameFinished); if (size < 0) { LOGE("+++++Error encoding frame\n"); return -1; }if(frameFinished)fwrite(avpkt.data,avpkt.size,1,video_file);av_free_packet(&avpkt);sws_freeContext(swsctx);av_free(yuv420pframe);av_free(yuv422frame);(*env)->ReleaseByteArrayElements(env, yuvdata, ydata, 0);}JNIEXPORT jint JNICALL Java_com_hclydao_webcam_Ffmpeg_videoclose(JNIEnv * env, jclass obj){fclose(video_file);avcodec_close(pCodecCtx);av_free(pCodecCtx);free(outbuf);}最後錄下來的視頻是可以用播放的 總感覺代碼好像哪裡寫的不對 bug總是有的 過程和原理搞清楚了其它就容易了
下面是我錄的 至此網路攝影機這塊暫時就這樣了
下載連結 請去我的資源裡找吧 我上傳了沒顯示出來 那個有20幾M的就是的了
這個bug很多 沒怎麼認真去寫 如果原理和過程有問題的希望大家指出