forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_render.cpp
188 lines (152 loc) · 4.39 KB
/
sdl_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (C) 2012 microcai <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define UINT64_C(c) c ## ULL
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
#include <avplay.h>
#include <SDL/SDL.h>
#include "sdl_render.h"
#ifdef __cplusplus
extern "C" {
#endif
EXPORT_API int sdl_init_video(void *ctx, int w, int h, int pix_fmt)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = NULL;
vo->priv = (void *) (sdl = new sdl_render);
return sdl->init_render(vo->user_data, w, h, pix_fmt) ? 0 : -1;
}
EXPORT_API int sdl_render_one_frame(void *ctx, AVFrame * data,
int pix_fmt, double pts)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = (sdl_render *) vo->priv;
return sdl->render_one_frame(data, pix_fmt) ? 0 : -1;
}
EXPORT_API void sdl_re_size(void *ctx, int width, int height)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = (sdl_render *) vo->priv;
sdl->re_size(width, height);
}
EXPORT_API void sdl_aspect_ratio(void *ctx, int srcw, int srch,
int enable_aspect)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = (sdl_render *) vo->priv;
sdl->aspect_ratio(srcw, srch, enable_aspect);
}
EXPORT_API int sdl_use_overlay(void *ctx)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = (sdl_render *) vo->priv;
return sdl->use_overlay()? 0 : -1;
}
EXPORT_API void sdl_destory_render(void *ctx)
{
vo_context *vo = (vo_context *) ctx;
sdl_render *sdl = (sdl_render *) vo->priv;
if (sdl) {
sdl->destory_render();
delete sdl;
vo->priv = NULL;
}
}
#ifdef __cplusplus
}
#endif
bool
sdl_render::render_one_frame(AVFrame * data, int pix_fmt)
{
boost::mutex::scoped_lock l(renderlock);
SDL_Rect rect;
AVPicture pict = { {0} };
uint8_t **px = m_yuv->pixels;
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]
};
m_swsctx = sws_getCachedContext(m_swsctx, m_image_width, m_image_height,
(PixelFormat)m_pix_fmt, sfc->w, sfc->h, PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
SDL_LockYUVOverlay(m_yuv);
pict.data[0] = m_yuv->pixels[0];
pict.data[1] = m_yuv->pixels[2];
pict.data[2] = m_yuv->pixels[1];
pict.linesize[0] = m_yuv->pitches[0];
pict.linesize[1] = m_yuv->pitches[2];
pict.linesize[2] = m_yuv->pitches[1];
sws_scale(m_swsctx, pixels, linesize, 0, m_image_height, pict.data,
pict.linesize);
SDL_UnlockYUVOverlay(m_yuv);
rect.x = 0;
rect.y = 0;
rect.w = sfc->w;
rect.h = sfc->h;
SDL_DisplayYUVOverlay(m_yuv, &rect);
m_yuv->pixels = px;
return true;
}
void
sdl_render::re_size(int w, int h)
{
boost::mutex::scoped_lock l(renderlock);
//FIXME
//resize 有办法传递全屏信息就好了
int fw, fh;
SDL_Rect** mode = SDL_ListModes(NULL,SDL_FULLSCREEN);
fw = mode[0]->w ; fh = mode[0]->h;
bool fs=false;
if ( w == fw && h == fh )
fs = true;
sfc = SDL_SetVideoMode(w, h, 32, fs? SDL_FULLSCREEN : SDL_RESIZABLE);
SDL_FreeYUVOverlay(this->m_yuv);
this->m_yuv = SDL_CreateYUVOverlay(w, h, SDL_YV12_OVERLAY, sfc);
}
bool
sdl_render::init_render(void *ctx, int w, int h, int pix_fmt)
{
m_swsctx = NULL;
m_pix_fmt = pix_fmt;
if (!SDL_WasInit(SDL_INIT_VIDEO))
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_SetVideoMode(w, h, 32, SDL_RESIZABLE);
m_image_height = h;
m_image_width = w;
re_size(w, h);
return m_yuv;
}
void
sdl_render::aspect_ratio(int srcw, int srch, bool enable_aspect)
{
}
void
sdl_render::destory_render()
{
}
bool
sdl_render::use_overlay()
{
return true;
}