編輯:關於 Android 程式設計
在 Android 上做影片播放,若使用 FFmpeg 解碼 H.264,解出來的原生格式是 YUV(通常是 YUV420P)。要把它畫到螢幕上,最佳路徑是透過 OpenGL ES 2.0 把 YUV 直接當紋理上傳,由 GPU 在 Shader 中完成色彩轉換——這是最快也最省電的做法。
但如果你還不打算投入 OpenGL ES,就必須先在 CPU 上把 YUV 轉成 RGB,再用 Canvas 或 Bitmap 繪製。本文比較兩條可行的轉換路徑。
FFmpeg 提供了 libswscale 模組專門處理影像縮放與色彩空間轉換,介面簡潔:
AVCodecContext *codec_ctx;
uint8_t *fill_buffer;
struct SwsContext *img_convert_ctx;
AVFrame *frame; // 解碼後的 YUV 資料
AVFrame *frame_rgb; // 轉換後的 RGB565 輸出
if (frame) {
int numBytes = avpicture_get_size(PIX_FMT_RGB565,
codec_ctx->width, codec_ctx->height);
fill_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) frame_rgb, fill_buffer,
PIX_FMT_RGB565, codec_ctx->width, codec_ctx->height);
img_convert_ctx = sws_getContext(
codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt,
codec_ctx->width, codec_ctx->height, PIX_FMT_RGB565,
SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(img_convert_ctx,
frame->data, frame->linesize, 0, codec_ctx->height,
frame_rgb->data, frame_rgb->linesize);
}
優點是通用性極強,支援任意 YUV 子採樣格式轉任意 RGB 排列;缺點是速度較慢,且 sws_getContext() 每次呼叫都要重建轉換情境,成本不低——實務上應該把 img_convert_ctx 提到迴圈外,只在解析度改變時重建一次。
社群中有一份廣為使用的 YUV→RGB 最佳化程式碼(網址 https://wss.co.uk/pinknoise/yuv2rgb/),同時提供 C 與 ARM 組語兩種實作。在 ARM 裝置上,組語版本的轉換耗時約 10 ms 等級,明顯優於通用的 sws_scale。
下載後解壓,會看到涵蓋各種格式组合的檔案:
yuv2rgb.h yuv2rgb16tab.c yuv2rgb555.s yuv420rgb565.s yuv420rgb565c.c yuv420rgb888.s yuv420rgb8888.s yuv422rgb565.s yuv422rgb565c.c yuv422rgb888.s yuv422rgb8888.s yuv444rgb565.s yuv444rgb565c.c yuv444rgb888.s yuv444rgb8888.s
檔名清楚地說明了「輸入格式 → 輸出格式」以及「.s 為組語、c 為純 C」的對應關係。若你需要的是 YUV420 轉 RGB565,就取 yuv420rgb565.s。
把 yuv420rgb565.s、yuv2rgb.h 與 yuv2rgb16tab.c 三個檔案放進專案。注意查找表 yuv2rgb16tab.c 是組語版本運算所必需的。
#include "yuv2rgb.h"
#include "yuv2rgb16tab.c"
extern const uint32_t yuv2rgb565_table[];
extern void yuv420_2_rgb565(
uint8_t *dst_ptr, // RGB 輸出緩衝
const uint8_t *y_ptr, // Y 平面
const uint8_t *u_ptr, // U 平面
const uint8_t *v_ptr, // V 平面
int32_t width, // 影像寬度
int32_t height, // 影像高度
int32_t y_span, // frame->linesize[0]
int32_t uv_span, // frame->linesize[1]
int32_t dst_span, // codec_ctx->width << 1
const uint32_t *tables,// 上面的 yuv2rgb565_table
int32_t dither); // 抖動模式:0 / 1 / 2 / 3
參數中的 span(又稱 stride)是各平面的行跨距。實務上必須傳入 FFmpeg 提供的 linesize 而不能假設等於寬度,因為對齊 padding 會讓兩者不一致——這是影像處理最常見的 bug 來源之一。
第一次呼叫時先配置 RGB 輸出緩衝,之後每幀重複使用:
if (first == 0) {
int numBytes = avpicture_get_size(PIX_FMT_RGB565,
codec_ctx->width, codec_ctx->height);
uint8_t *fill_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill(&frame_rgb, fill_buffer, PIX_FMT_RGB565,
codec_ctx->width, codec_ctx->height);
first = 1;
}
yuv420_2_rgb565(
frame_rgb.data[0],
frame->data[0], frame->data[1], frame->data[2],
codec_ctx->width, codec_ctx->height,
frame->linesize[0],
frame->linesize[1],
codec_ctx->width << 1,
yuv2rgb565_table,
3);
dst_span 傳 width << 1 是因為 RGB565 每個像素佔 2 個位元組。
若 Java 端傳入一個 byte[] 作為輸出緩衝,用 JNI 直接寫入可省掉一次額外複製:
uint8_t *buffer = (uint8_t *)(*env)->GetByteArrayElements(env, output, JNI_FALSE); memcpy(buffer, frame_rgb.data[0], codec_ctx->width * codec_ctx->height * 2); (*env)->ReleaseByteArrayElements(env, output, (jbyte *)buffer, 0);
第三個參數傳 0 表示「把修改同步回 Java 陣列並釋放」。若確定沒有修改,可傳 JNI_ABORT 省下一次回寫;但這裡我們確實寫入了資料,必須傳 0。
SwsContext 快取起來重用。最後提醒:這份最佳化程式碼是 ARMv5/v6 時代的產物,在現代的 ARMv7/v8 與 NEON 指令集上,效能表現與當年已有差距。若在今日的專案中採用,建議先自行做基準測試,並考慮以 NEON intrinsics 重寫關鍵迴圈,或直接評估改用 MediaCodec 硬解 + Surface 輸出的標準路徑。
Android 開發中的適配:先搞清楚要適配什麼,再談怎麼適配
Android 的碎片化問題已經被談到爛了。與其再抱怨一次,不如把「適配什麼」與「怎麼適配」做一次有條理的整理。 三類被適配的對象 第一類:作業系統版本 從 Androi
高德地圖定位失敗怎麼辦?分平台排查重點整理
高德地圖提供全國地圖瀏覽、地點搜尋與路線規劃,還能順便看商家的團購優惠。但一旦定位失敗,這些功能幾乎都跟著失效。以下依平台整理排查重點。 iPhone 使用者 到「
Android 多線程:HandlerThread 完全詳解
Java 與 Android 在線程處理上大同小異,但 HandlerThread 是 Android 特有的一個類,它在設計思想上有不少值得學習的地方。本文從特點、使用
Android 開發筆記(一百二十二):循環器視圖 RecyclerView
RecyclerView 是 Android 在 support-v7 函式庫中推出的控制項,中文譯名為「循環器視圖」。它的功能相當強大,一套元件就能分別實現 ListV