forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soft_render.cpp
150 lines (119 loc) · 3.31 KB
/
soft_render.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "internal.h"
// 使用gdi+渲染.
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
#include "soft_render.h"
soft_render::soft_render()
: m_hwnd(NULL)
, m_hdc(NULL)
, m_image_width(0)
, m_image_height(0)
, m_keep_aspect(false)
, m_window_aspect(0.0f)
, m_framebuffer(NULL)
, m_swsctx(NULL)
{
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
soft_render::~soft_render()
{
GdiplusShutdown(m_gdiplusToken);
}
bool soft_render::init_render(void* ctx, int w, int h, int pix_fmt)
{
m_hwnd = (HWND)ctx;
m_image_width = w;
m_image_height = h;
m_window_aspect = (float)w / (float)h;
// 获得dc.
m_hdc = GetDC(m_hwnd);
if (!m_hdc)
{
printf("GetDC failed.\n");
return false;
}
m_swsctx = sws_getContext(m_image_width, m_image_height, PIX_FMT_YUV420P, m_image_width, m_image_height,
PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
m_framebuffer = (uint8_t*)malloc(w * h * sizeof(uint32_t));
return true;
}
bool soft_render::render_one_frame(AVFrame* data, int pix_fmt)
{
Graphics Gs(m_hdc);
uint8_t* pixels[3] = { data->data[0],
data->data[1],
data->data[2] };
int linesize[3] = { data->linesize[0],
data->linesize[1],
data->linesize[2] };
AVFrame* pic = avcodec_alloc_frame();
avpicture_fill((AVPicture*)pic, m_framebuffer, PIX_FMT_BGR24, m_image_width, m_image_height);
sws_scale(m_swsctx, pixels, linesize, 0, m_image_height, pic->data, pic->linesize);
Bitmap bmp(m_image_width, m_image_height, pic->linesize[0], PixelFormat24bppRGB, pic->data[0]);
RECT rect_client = { 0 };
GetClientRect(m_hwnd, &rect_client);
int width = rect_client.right - rect_client.left;
int height = rect_client.bottom - rect_client.top;
if (m_keep_aspect)
{
GetClientRect(m_hwnd, &rect_client);
int win_width = width = rect_client.right - rect_client.left;
int win_height = height = rect_client.bottom - rect_client.top;
int tmpheight = ((float)width / m_window_aspect);
tmpheight += tmpheight % 2;
if(tmpheight > height)
{
width = ((float)height * m_window_aspect);
width += width % 2;
}
else
{
height = tmpheight;
}
// 居中对齐.
rect_client.left += ((win_width - width) / 2);
rect_client.top += ((win_height - height) / 2);
rect_client.bottom -= ((win_height - height) / 2);
rect_client.right -= ((win_width - width) / 2);
}
Gs.DrawImage(&bmp, rect_client.left, rect_client.top,
rect_client.right - rect_client.left, rect_client.bottom - rect_client.top);
av_free(pic);
return true;
}
void soft_render::re_size(int width, int height)
{
}
void soft_render::aspect_ratio(int srcw, int srch, bool enable_aspect)
{
if (enable_aspect)
{
m_keep_aspect = true;
m_window_aspect = (float)srcw / (float)srch;
}
else
{
m_keep_aspect = false;
}
}
void soft_render::destory_render()
{
if (m_hwnd && m_hdc)
{
ReleaseDC(m_hwnd, m_hdc);
m_hwnd = NULL;
m_hdc = NULL;
}
if (m_framebuffer)
{
free(m_framebuffer);
m_framebuffer = NULL;
}
if (m_swsctx)
{
sws_freeContext(m_swsctx);
m_swsctx = NULL;
}
}