栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

FFmpeg源代码简单分析-其他-libavdevice的gdigrab

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

FFmpeg源代码简单分析-其他-libavdevice的gdigrab

参考链接
  • FFmpeg源代码简单分析:libavdevice的gdigrab_雷霄骅的博客-CSDN博客_gdigrab

libavdevice的gdigrab
  • GDIGrab用于在Windows下屏幕录像(抓屏)
  • gdigrab的源代码位于libavdevicegdigrab.c。
  • 关键函数的调用关系图如下图所示。
  • 图中绿色背景的函数代表源代码中自己声明的函数,紫色背景的函数代表Win32的API函数。

 

ff_gdigrab_demuxer
  • 在FFmpeg中Device也被当做是一种Format,因为GDIGrab是一个输入设备,因此被当作一个AVInputFormat。
  • GDIGrab对应的AVInputFormat结构体如下所示。
const 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_class
  • ff_gdigrab_demuxer指定它的AVClass为一个名称为“gdigrab_class”的静态变量。
  • gdigrab_class的定义如下。
static const AVClass gdigrab_class = {
    .class_name = "GDIgrab indev",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
};
  • 从gdigrab_class的定义可以看出,它指定了一个名称为“options”的数组作为它的选项数组(赋值给AVClass的option变量)

options
  • 下面看一下这个options数组的定义,如下所示。
#define OFFSET(x) offsetof(struct gdigrab, x)
#define DEC AV_OPT_FLAG_DECODING_PARAM
static 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, INT_MAX, 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设备用到的各种变量,定义如下所示。
struct gdigrab {
    const AVClass *class;   

    int        frame_size;  
    int        header_size; 
    AVRational time_base;   
    int64_t    time_frame;  

    int        draw_mouse;  
    int        show_region; 
    AVRational framerate;   
    int        width;       
    int        height;      
    int        offset_x;    
    int        offset_y;    

    HWND       hwnd;        
    HDC        source_hdc;  
    HDC        dest_hdc;    
    BITMAPINFO bmi;         
    HBITMAP    hbmp;        
    void      *buffer;      
    RECT       clip_rect;   

    HWND       region_hwnd; 

    int cursor_error_printed;
};
gdigrab_read_header()
  • gdigrab_read_header()用于初始化gdigrab。
  • 函数的定义如下所示。
static int
gdigrab_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->url;
    const char *name     = NULL;
    AVStream   *st       = NULL;

    int bpp;
    int horzres;
    int vertres;
    int desktophorzres;
    int desktopvertres;
    RECT virtual_rect;
    RECT clip_rect;
    BITMAP bmp;
    int ret;

    if (!strncmp(filename, "title=", 6)) {
        wchar_t *name_w = NULL;
        name = filename + 6;

        if(utf8towchar(name, &name_w)) {
            ret = AVERROR(errno);
            goto error;
        }
        if(!name_w) {
            ret = AVERROR(EINVAL);
            goto error;
        }

        hwnd = FindWindowW(NULL, name_w);
        av_freep(&name_w);
        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;
        }
    } else if (!strcmp(filename, "desktop")) {
        hwnd = NULL;
    } else {
        av_log(s1, AV_LOG_ERROR,
               "Please use "desktop" or "title=" to specify your target.n");
        ret = AVERROR(EIO);
        goto error;
    }

    
    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);

    horzres = GetDeviceCaps(source_hdc, HORZRES);
    vertres = GetDeviceCaps(source_hdc, VERTRES);
    desktophorzres = GetDeviceCaps(source_hdc, DESKTOPHORZRES);
    desktopvertres = GetDeviceCaps(source_hdc, DESKTOPVERTRES);

    if (hwnd) {
        GetClientRect(hwnd, &virtual_rect);
        
        virtual_rect.left   = virtual_rect.left   * desktophorzres / horzres;
        virtual_rect.right  = virtual_rect.right  * desktophorzres / horzres;
        virtual_rect.top    = virtual_rect.top    * desktopvertres / vertres;
        virtual_rect.bottom = virtual_rect.bottom * desktopvertres / vertres;
    } else {
        
        virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
        virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
        virtual_rect.right = (virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN)) * desktophorzres / horzres;
        virtual_rect.bottom = (virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN)) * desktopvertres / vertres;
    }

    
    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;
    }


    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, abortingn");
        ret = AVERROR(EIO);
        goto error;
    }

    dest_hdc = CreateCompatibleDC(source_hdc);
    if (!dest_hdc) {
        WIN32_API_ERROR("Screen DC CreateCompatibleDC");
        ret = AVERROR(EIO);
        goto error;
    }

    
    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;
    }

    
    GetObject(hbmp, sizeof(BITMAP), &bmp);

    st = avformat_new_stream(s1, NULL);
    if (!st) {
        ret = AVERROR(ENOMEM);
        goto error;
    }
    avpriv_set_pts_info(st, 64, 1, 1000000); 

    gdigrab->frame_size  = bmp.bmWidthBytes * bmp.bmHeight * bmp.bmPlanes;
    gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
                           (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) ;
    gdigrab->time_base   = av_inv_q(gdigrab->framerate);
    gdigrab->time_frame  = av_gettime_relative() / 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->avg_frame_rate = av_inv_q(gdigrab->time_base);

    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id   = AV_CODEC_ID_BMP;
    st->codecpar->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()用于读取一帧抓屏数据。
  • 该函数的定义如下所示。
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;
 
    
    time_frame += INT64_C(1000000);
 
    
    if (gdigrab->show_region)
        gdigrab_region_wnd_update(s1, gdigrab);
 
    
    //延时
    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;
 
    
    //关键: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);
 
    
    //BMP文件头BITMAPFILEHEADER
    bfh.bfType = 0x4d42; 
    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。
  • 该函数的定义如下所示。
  • 从源代码可以看出,gdigrab_read_close ()完成了各种变量的清理工作。
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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号