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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
| #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstring>
#include <cstdio>
extern "C" {
#define __STDC_CONSTANT_MACROS
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
class ffmpeg_decoder {
typedef bool (*draw_callback_t)( uint8_t *, int, int, int );
draw_callback_t m_draw_cb;
bool m_b_is_valid;
AVFormatContext * m_p_fmt_ctx;
AVCodecContext * m_p_dec_ctx;
int m_i_video_stream;
AVFrame * m_p_frame_yuv;
AVFrame * m_p_frame_rgb;
int m_p_frame_rgb_size;
uint8_t * m_p_frame_rgb_data;
SwsContext * m_p_sws_ctx;
public:
ffmpeg_decoder( const char * url_filename, draw_callback_t draw_cb )
: m_draw_cb( draw_cb ), m_b_is_valid( false ), m_p_fmt_ctx( nullptr ), m_p_sws_ctx( nullptr ) {
av_register_all();
if ( avformat_open_input( &m_p_fmt_ctx, url_filename, NULL, NULL ) < 0 ) {
av_log( NULL, AV_LOG_ERROR, "avformat_open_input()\n" );
} else {
if ( avformat_find_stream_info( m_p_fmt_ctx, NULL ) < 0 ) {
av_log( NULL, AV_LOG_ERROR, "avformat_find_stream_info()\n" );
} else {
AVCodec * p_codec;
m_i_video_stream = av_find_best_stream( m_p_fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &p_codec, 0 );
if ( m_i_video_stream < 0 ) {
av_log( NULL, AV_LOG_ERROR, "av_find_best_stream()\n" );
} else {
m_p_dec_ctx = m_p_fmt_ctx->streams[m_i_video_stream]->codec;
if ( avcodec_open2( m_p_dec_ctx, p_codec, NULL ) < 0 ) {
av_log( NULL, AV_LOG_ERROR, "avcodec_open2()\n" );
} else {
m_p_frame_yuv = av_frame_alloc();
m_p_frame_rgb = av_frame_alloc();
m_p_frame_rgb_size = avpicture_get_size( PIX_FMT_RGB32, m_p_dec_ctx->width, m_p_dec_ctx->height );
m_p_frame_rgb_data = new uint8_t [m_p_frame_rgb_size];
avpicture_fill( (AVPicture *)m_p_frame_rgb, m_p_frame_rgb_data, PIX_FMT_RGB32, m_p_dec_ctx->width, m_p_dec_ctx->height );
m_b_is_valid = true;
return;
}
avcodec_close( m_p_dec_ctx );
}
}
avformat_close_input( &m_p_fmt_ctx );
}
}
virtual ~ffmpeg_decoder() {
if ( is_valid() ) {
delete [] m_p_frame_rgb_data;
av_frame_free( &m_p_frame_rgb );
av_frame_free( &m_p_frame_yuv );
avcodec_close( m_p_dec_ctx );
avformat_close_input( &m_p_fmt_ctx );
}
}
bool is_valid() const {
return m_b_is_valid;
}
// returns -1 if object is not valid
int get_width() const {
return is_valid() ? m_p_dec_ctx->width : -1;
}
// returns -1 if object is not valid
int get_height() const {
return is_valid() ? m_p_dec_ctx->height : -1;
}
/// returns: 1 if video frame decoded, -1 if not video frame decoded, 0 if no more frames to decode
int decode_frame() {
int status = 1;
AVPacket packet;
if ( av_read_frame( m_p_fmt_ctx, &packet ) < 0 ) {
status = 0;
} else {
// is video frame?
if ( packet.stream_index == m_i_video_stream ) {
av_frame_unref( m_p_frame_yuv );
int got_picture = 0;
if ( avcodec_decode_video2( m_p_dec_ctx, m_p_frame_yuv, &got_picture, &packet ) < 0 ) {
status = -1;
} else {
if ( got_picture ) {
m_p_sws_ctx = sws_getCachedContext( m_p_sws_ctx,
m_p_dec_ctx->width, m_p_dec_ctx->height, m_p_dec_ctx->pix_fmt,
m_p_dec_ctx->width, m_p_dec_ctx->height, PIX_FMT_RGB32, SWS_FAST_BILINEAR, NULL, NULL, NULL );
sws_scale( m_p_sws_ctx,
m_p_frame_yuv->data, m_p_frame_yuv->linesize, 0, m_p_dec_ctx->height,
m_p_frame_rgb->data, m_p_frame_rgb->linesize );
m_draw_cb( m_p_frame_rgb->data[0], m_p_frame_rgb->linesize[0], m_p_dec_ctx->width, m_p_dec_ctx->height );
}
}
} else {
status = -1;
}
av_free_packet( &packet );
}
return status;
}
};
static HWND g_hWnd;
static BITMAPINFO g_bi;
static int g_width;
static int g_height;
static uint8_t * g_p_dib;
static int g_i_delay = 18; // ms
static bool g_pause = false;
static bool my_draw_cb( uint8_t * p_src, int /*pitch*/, int width, int height ) {
g_width = width;
g_height = height;
g_p_dib = p_src;
InvalidateRect( g_hWnd, NULL, FALSE );
return true;
}
static LRESULT CALLBACK wnd_proc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch ( uMsg ) {
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint( hWnd, &ps );
SetDIBitsToDevice( ps.hdc, 0, 0, g_width, g_height, 0, 0, 0, g_height, g_p_dib, &g_bi, DIB_RGB_COLORS );
EndPaint( hWnd, &ps );
} break;
case WM_NCHITTEST:
return HTCAPTION;
case WM_KEYDOWN: {
switch ( wParam ) {
case VK_UP : --g_i_delay; if ( g_i_delay < 0 ) g_i_delay = 0; break;
case VK_DOWN : ++g_i_delay; if ( g_i_delay > 100 ) g_i_delay = 100; break;
case VK_SPACE : g_pause = !g_pause; break;
case VK_ESCAPE: PostQuitMessage( 0 ); break;
}
} break;
case WM_DESTROY: PostQuitMessage( 0 ); break;
default: return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return 0;
}
int main( int narg, char *args[] )
{
if ( narg != 2 ) return 1;
ffmpeg_decoder dec( args[1], my_draw_cb );
ZeroMemory( &g_bi, sizeof( BITMAPINFO ) );
g_bi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
g_bi.bmiHeader.biWidth = dec.get_width();
g_bi.bmiHeader.biHeight = -dec.get_height();
g_bi.bmiHeader.biPlanes = 1;
g_bi.bmiHeader.biBitCount = 32;
g_bi.bmiHeader.biCompression = BI_RGB;
const char * class_name = "ffmpeg_test_cls32";
HINSTANCE hInst = GetModuleHandle( NULL );
WNDCLASS wc;
ZeroMemory( &wc, sizeof( WNDCLASS ) );
wc.lpfnWndProc = wnd_proc;
wc.hInstance = hInst;
wc.lpszClassName = class_name;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
RegisterClass( &wc );
g_hWnd = CreateWindow( class_name, NULL, WS_POPUP | WS_VISIBLE,
(GetSystemMetrics( SM_CXSCREEN ) / 2) - (dec.get_width() / 2),
(GetSystemMetrics( SM_CYSCREEN ) / 2) - (dec.get_height() / 2),
dec.get_width(), dec.get_height(), NULL, NULL, hInst, NULL );
MSG msg;
for ( ; ; ) {
if ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) {
TranslateMessage( &msg );
if ( msg.message == WM_QUIT ) break;
DispatchMessage( &msg );
} else {
if ( !g_pause ) {
int r = dec.decode_frame();
if ( r == 1 && g_i_delay ) Sleep( g_i_delay );
} else {
Sleep( 100 );
}
}
}
} |