Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.0 USB Camera 實戰(六):用 FFmpeg 實現 MPEG 編碼

編輯:關於 Android 程式設計

前面本來打算做 H.264 編碼,研究了兩天發現 FFmpeg 裡的 H.264 編碼似乎要依賴第三方庫 x264,於是就找了個更簡單的方案——做 MPEG 編碼。關於 FFmpeg 的移植,我在之前那篇 FFmpeg 解碼的文章裡已經給出了具體鏈接和用法說明,這裡重點講解如何通過 FFmpeg 把 YUV422 格式轉換成 RGB,以及如何把 YUV422 轉成 MPEG 格式。

YUV 轉 RGB

接前面幾篇的思路,獲取到 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;

/*
 * encoding 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 還比較多,沒怎麼認真去寫,如果原理和過程有問題,歡迎大家指出。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved