FFmpeg原始碼簡單分析:libavdevice的gdigrab,
本文記錄FFmpeg的libavdevice中GDIGrab組件的原始碼。GDIGrab用於在Windows下螢幕錄影(抓屏)。在ffmpeg.exe中使用可以參考文章:
FFmpeg擷取DirectShow裝置資料(網路攝影機,錄屏)
編程使用可以參考文章:
最簡單的基於FFmpeg的AVDevice例子(螢幕錄製)
gdigrab的原始碼位於libavdevice\gdigrab.c。關鍵函數的呼叫歷程圖如所示。圖中綠色背景的函數代表原始碼中自己聲明的函數,紫色背景的函數代表Win32的API函數。
ff_gdigrab_demuxer在FFmpeg中Device也被當做是一種Format,因為GDIGrab是一個輸入裝置,因此被當作一個AVInputFormat。GDIGrab對應的AVInputFormat結構體如下所示。
AVInputFormat ff_gdigrab_demuxer = { .name = "gdigrab", .long_name = NULL_IF_CONFIG_SMALL("GDI API Windows frame grabber"), .priv_data_size = sizeof(struct gdigrab), .read_header = gdigrab_read_header, .read_packet = gdigrab_read_packet, .read_close = gdigrab_read_close, .flags = AVFMT_NOFILE, .priv_class = &gdigrab_class,};
從該結構體可以看出:
裝置名稱是“gdigrab”;
裝置完整名稱是“GDI API Windows frame grabber”;
初始化函數指標read_header()指向gdigrab_read_header();
讀取資料函數指標read_packet()指向gdigrab_read_packet();
關閉函數指標read_close()指向gdigrab_read_close();
Flags設定為AVFMT_NOFILE;
AVClass指定為gdigrab_class。
下面分析一下這些資料。
gdigrab_classff_gdigrab_demuxer指定它的AVClass為一個名稱為“gdigrab_class”的靜態變數。有關AVClass的概念之前已經記錄過,在這裡不再重複。gdigrab_class的定義如下。
static const AVClass gdigrab_class = { .class_name = "GDIgrab indev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT,};
從gdigrab_class的定義可以看出,它指定了一個名稱為“options”的數組作為它的選項數組(賦值給AVClass的option變數)。
options下面看一下這個options數組的定義,如下所示。
#define OFFSET(x) offsetof(struct gdigrab, x)#define DEC AV_OPT_FLAG_DECODING_PARAMstatic const AVOption options[] = { { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC }, { "show_region", "draw border around capture area", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC }, { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC }, { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC }, { "offset_x", "capture area x offset", OFFSET(offset_x), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC }, { "offset_y", "capture area y offset", OFFSET(offset_y), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC }, { NULL },};
options數組中包含了該Device支援的選項。可以看出GDIGrab支援如下選項:
draw_mouse:畫出滑鼠指標。
show_region:划出抓屏地區的邊界。
framerate:抓屏幀率。
video_size:抓屏的大小。
offset_x:抓屏起始點x軸座標。
offset_y:抓屏起始點y軸座標。
從宏定義“#define OFFSET(x) offsetof(struct gdigrab, x)”中可以看出,這些選項都儲存在一個名稱為“gdigrab”的結構體中。
Gdigrab 上下文結構體Gdigrab上下文結構體中儲存了GDIGrab裝置用到的各種變數,定義如下所示。
/** * GDI Device Demuxer context */struct gdigrab { const AVClass *class; /**< Class for private options */ int frame_size; /**< Size in bytes of the frame pixel data */ int header_size; /**< Size in bytes of the DIB header */ AVRational time_base; /**< Time base */ int64_t time_frame; /**< Current time */ int draw_mouse; /**< Draw mouse cursor (private option) */ int show_region; /**< Draw border (private option) */ AVRational framerate; /**< Capture framerate (private option) */ int width; /**< Width of the grab frame (private option) */ int height; /**< Height of the grab frame (private option) */ int offset_x; /**< Capture x offset (private option) */ int offset_y; /**< Capture y offset (private option) */ HWND hwnd; /**< Handle of the window for the grab */ HDC source_hdc; /**< Source device context */ HDC dest_hdc; /**< Destination, source-compatible DC */ BITMAPINFO bmi; /**< Information describing DIB format */ HBITMAP hbmp; /**< Information on the bitmap captured */ void *buffer; /**< The buffer containing the bitmap image data */ RECT clip_rect; /**< The subarea of the screen or window to clip */ HWND region_hwnd; /**< Handle of the region border window */ int cursor_error_printed;};
gdigrab_read_header()gdigrab_read_header()用於初始化gdigrab。函數的定義如下所示。
/** * Initializes the gdi grab device demuxer (public device demuxer API). * * @param s1 Context from avformat core * @return AVERROR_IO error, 0 success */static intgdigrab_read_header(AVFormatContext *s1){ struct gdigrab *gdigrab = s1->priv_data; //視窗控制代碼 HWND hwnd; HDC source_hdc = NULL; HDC dest_hdc = NULL; BITMAPINFO bmi; HBITMAP hbmp = NULL; void *buffer = NULL; const char *filename = s1->filename; const char *name = NULL; AVStream *st = NULL; int bpp; RECT virtual_rect; //視窗的位置和大小 RECT clip_rect; BITMAP bmp; int ret; //filename為視窗名稱 if (!strncmp(filename, "title=", 6)) { name = filename + 6; //尋找視窗的控制代碼 hwnd = FindWindow(NULL, name); if (!hwnd) { av_log(s1, AV_LOG_ERROR, "Can't find window '%s', aborting.\n", name); ret = AVERROR(EIO); goto error; } if (gdigrab->show_region) { av_log(s1, AV_LOG_WARNING, "Can't show region when grabbing a window.\n"); gdigrab->show_region = 0; } //filename為desktop } else if (!strcmp(filename, "desktop")) { //視窗控制代碼為NULL hwnd = NULL; } else { av_log(s1, AV_LOG_ERROR, "Please use \"desktop\" or \"title=<windowname>\" to specify your target.\n"); ret = AVERROR(EIO); goto error; } if (hwnd) { GetClientRect(hwnd, &virtual_rect); } else { //視窗控制代碼為NULL,代表是全屏 virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN); virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN); virtual_rect.right = virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN); virtual_rect.bottom = virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN); } /* If no width or height set, use full screen/window area */ if (!gdigrab->width || !gdigrab->height) { clip_rect.left = virtual_rect.left; clip_rect.top = virtual_rect.top; clip_rect.right = virtual_rect.right; clip_rect.bottom = virtual_rect.bottom; } else { clip_rect.left = gdigrab->offset_x; clip_rect.top = gdigrab->offset_y; clip_rect.right = gdigrab->width + gdigrab->offset_x; clip_rect.bottom = gdigrab->height + gdigrab->offset_y; } if (clip_rect.left < virtual_rect.left || clip_rect.top < virtual_rect.top || clip_rect.right > virtual_rect.right || clip_rect.bottom > virtual_rect.bottom) { av_log(s1, AV_LOG_ERROR, "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)", clip_rect.left, clip_rect.top, clip_rect.right, clip_rect.bottom, virtual_rect.left, virtual_rect.top, virtual_rect.right, virtual_rect.bottom); ret = AVERROR(EIO); goto error; } /* This will get the device context for the selected window, or if * none, the primary screen */ //得到某個視窗控制代碼的DC source_hdc = GetDC(hwnd); if (!source_hdc) { WIN32_API_ERROR("Couldn't get window device context"); ret = AVERROR(EIO); goto error; } bpp = GetDeviceCaps(source_hdc, BITSPIXEL); if (name) { av_log(s1, AV_LOG_INFO, "Found window %s, capturing %lix%lix%i at (%li,%li)\n", name, clip_rect.right - clip_rect.left, clip_rect.bottom - clip_rect.top, bpp, clip_rect.left, clip_rect.top); } else { av_log(s1, AV_LOG_INFO, "Capturing whole desktop as %lix%lix%i at (%li,%li)\n", clip_rect.right - clip_rect.left, clip_rect.bottom - clip_rect.top, bpp, clip_rect.left, clip_rect.top); } if (clip_rect.right - clip_rect.left <= 0 || clip_rect.bottom - clip_rect.top <= 0 || bpp%8) { av_log(s1, AV_LOG_ERROR, "Invalid properties, aborting\n"); ret = AVERROR(EIO); goto error; } //建立一個與指定裝置相容的HDC dest_hdc = CreateCompatibleDC(source_hdc); if (!dest_hdc) { WIN32_API_ERROR("Screen DC CreateCompatibleDC"); ret = AVERROR(EIO); goto error; } /* Create a DIB and select it into the dest_hdc */ //BMP bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = clip_rect.right - clip_rect.left; bmi.bmiHeader.biHeight = -(clip_rect.bottom - clip_rect.top); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = bpp; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biSizeImage = 0; bmi.bmiHeader.biXPelsPerMeter = 0; bmi.bmiHeader.biYPelsPerMeter = 0; bmi.bmiHeader.biClrUsed = 0; bmi.bmiHeader.biClrImportant = 0; hbmp = CreateDIBSection(dest_hdc, &bmi, DIB_RGB_COLORS, &buffer, NULL, 0); if (!hbmp) { WIN32_API_ERROR("Creating DIB Section"); ret = AVERROR(EIO); goto error; } if (!SelectObject(dest_hdc, hbmp)) { WIN32_API_ERROR("SelectObject"); ret = AVERROR(EIO); goto error; } /* Get info from the bitmap */ GetObject(hbmp, sizeof(BITMAP), &bmp); //建立AVStream st = avformat_new_stream(s1, NULL); if (!st) { ret = AVERROR(ENOMEM); goto error; } avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ //儲存資訊到GDIGrab上下文結構體 gdigrab->frame_size = bmp.bmWidthBytes * bmp.bmHeight * bmp.bmPlanes; gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */; gdigrab->time_base = av_inv_q(gdigrab->framerate); gdigrab->time_frame = av_gettime() / av_q2d(gdigrab->time_base); gdigrab->hwnd = hwnd; gdigrab->source_hdc = source_hdc; gdigrab->dest_hdc = dest_hdc; gdigrab->hbmp = hbmp; gdigrab->bmi = bmi; gdigrab->buffer = buffer; gdigrab->clip_rect = clip_rect; gdigrab->cursor_error_printed = 0; if (gdigrab->show_region) { if (gdigrab_region_wnd_init(s1, gdigrab)) { ret = AVERROR(EIO); goto error; } } st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->codec->codec_id = AV_CODEC_ID_BMP; st->codec->time_base = gdigrab->time_base; st->codec->bit_rate = (gdigrab->header_size + gdigrab->frame_size) * 1/av_q2d(gdigrab->time_base) * 8; return 0;error://如果出錯了 if (source_hdc) ReleaseDC(hwnd, source_hdc); if (dest_hdc) DeleteDC(dest_hdc); if (hbmp) DeleteObject(hbmp); if (source_hdc) DeleteDC(source_hdc); return ret;}
從原始碼可以看出,gdigrab_read_header()的流程大致如下所示:
(1)確定視窗的控制代碼hwnd。如果指定了“title=”的話,調用FindWindow()擷取hwnd;如果指定了“desktop”,則設定hwnd為NULL。
(2)根據視窗的控制代碼hwnd確定抓屏的矩形地區。如果抓取指定視窗,則通過GetClientRect()函數;否則就抓取整個螢幕。
(3)調用GDI的API完成抓屏的一些初始化工作。包括:
a)通過GetDC()獲得某個視窗控制代碼的HDC(在這裡是source_hdc)。
b)通過CreateCompatibleDC()建立一個與指定裝置相容的HDC(在這裡是dest_hdc)
c)通過CreateDIBSection()建立HBITMAP
d)通過SelectObject()綁定HBITMAP和HDC(指的是dest_hdc)
(4)通過avformat_new_stream()建立一個AVStream。
(5)將初始化時候的一些參數儲存至GDIGrab的上下文結構體。
gdigrab_read_packet()gdigrab_read_packet()用於讀取一幀抓屏資料。該函數的定義如下所示。
/** * Grabs a frame from gdi (public device demuxer API). * * @param s1 Context from avformat core * @param pkt Packet holding the grabbed frame * @return frame size in bytes */static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt){ struct gdigrab *gdigrab = s1->priv_data; //讀取參數 HDC dest_hdc = gdigrab->dest_hdc; HDC source_hdc = gdigrab->source_hdc; RECT clip_rect = gdigrab->clip_rect; AVRational time_base = gdigrab->time_base; int64_t time_frame = gdigrab->time_frame; BITMAPFILEHEADER bfh; int file_size = gdigrab->header_size + gdigrab->frame_size; int64_t curtime, delay; /* Calculate the time of the next frame */ time_frame += INT64_C(1000000); /* Run Window message processing queue */ if (gdigrab->show_region) gdigrab_region_wnd_update(s1, gdigrab); /* wait based on the frame rate */ //延時 for (;;) { curtime = av_gettime(); delay = time_frame * av_q2d(time_base) - curtime; if (delay <= 0) { if (delay < INT64_C(-1000000) * av_q2d(time_base)) { time_frame += INT64_C(1000000); } break; } if (s1->flags & AVFMT_FLAG_NONBLOCK) { return AVERROR(EAGAIN); } else { av_usleep(delay); } } //建立一個AVPacket if (av_new_packet(pkt, file_size) < 0) return AVERROR(ENOMEM); pkt->pts = curtime; /* Blit screen grab */ //關鍵:BitBlt()完成抓屏功能 if (!BitBlt(dest_hdc, 0, 0, clip_rect.right - clip_rect.left, clip_rect.bottom - clip_rect.top, source_hdc, clip_rect.left, clip_rect.top, SRCCOPY | CAPTUREBLT)) { WIN32_API_ERROR("Failed to capture image"); return AVERROR(EIO); } //畫滑鼠指標? if (gdigrab->draw_mouse) paint_mouse_pointer(s1, gdigrab); /* Copy bits to packet data */ //BMP檔案頭BITMAPFILEHEADER bfh.bfType = 0x4d42; /* "BM" in little-endian */ bfh.bfSize = file_size; bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfOffBits = gdigrab->header_size; //往AVPacket中拷貝資料 //拷貝BITMAPFILEHEADER memcpy(pkt->data, &bfh, sizeof(bfh)); //拷貝BITMAPINFOHEADER memcpy(pkt->data + sizeof(bfh), &gdigrab->bmi.bmiHeader, sizeof(gdigrab->bmi.bmiHeader)); //不常見 if (gdigrab->bmi.bmiHeader.biBitCount <= 8) GetDIBColorTable(dest_hdc, 0, 1 << gdigrab->bmi.bmiHeader.biBitCount, (RGBQUAD *) (pkt->data + sizeof(bfh) + sizeof(gdigrab->bmi.bmiHeader))); //拷貝像素資料 memcpy(pkt->data + gdigrab->header_size, gdigrab->buffer, gdigrab->frame_size); gdigrab->time_frame = time_frame; return gdigrab->header_size + gdigrab->frame_size;}
從原始碼可以看出,gdigrab_read_packet()的流程大致如下所示:
(1)從GDIGrab上下文結構體讀取初始化時候設定的參數。
(2)根據幀率參數進行延時。
(3)通過av_new_packet()建立一個AVPacket。
(4)通過BitBlt()完成抓屏功能。
(5)如果需要畫滑鼠指標的話,調用paint_mouse_pointer(),這裡不做分析。
(6)按照順序拷貝以下3項內容至AVPacket的data指向的記憶體:
a)BITMAPFILEHEADER
b)BITMAPINFOHEADER
c)抓屏的到的像素資料
gdigrab_read_close()gdigrab_read_close()用於關閉gdigrab。該函數的定義如下所示。
/** * Closes gdi frame grabber (public device demuxer API). * * @param s1 Context from avformat core * @return 0 success, !0 failure */static int gdigrab_read_close(AVFormatContext *s1){ struct gdigrab *s = s1->priv_data; if (s->show_region) gdigrab_region_wnd_destroy(s1, s); if (s->source_hdc) ReleaseDC(s->hwnd, s->source_hdc); if (s->dest_hdc) DeleteDC(s->dest_hdc); if (s->hbmp) DeleteObject(s->hbmp); if (s->source_hdc) DeleteDC(s->source_hdc); return 0;}
從原始碼可以看出,gdigrab_read_close ()完成了各種變數的清理工作。
雷霄驊
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020