-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
263 lines (232 loc) · 6.99 KB
/
Copy pathmain.cpp
File metadata and controls
263 lines (232 loc) · 6.99 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <iostream>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "SDL/SDL.h"
};
#else
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL/SDL.h>
#ifdef __cplusplus
};
#endif
#endif
#include "PrintAvDetails.h"
//Full Screen
#define SHOW_FULLSCREEN 0
//Output YUV420P
#define OUTPUT_YUV420P 0
//Refresh
#define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
int thread_exit = 0;
//Thread
int sfp_refresh_thread(void *opaque)
{
SDL_Event event;
while (thread_exit == 0) {
event.type = SFM_REFRESH_EVENT;
SDL_PushEvent(&event);
//Wait 40 ms
SDL_Delay(40);
}
return 0;
}
SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread, NULL);
bool avDecode(AVCodecContext *pCodecCtx,
AVFrame * pFrame,
AVFrame * pFrameYUV,
SDL_Overlay *bmp,
SwsContext *swsCtx,
AVPacket *packet,
SDL_Rect &rect)
{
// Decode
if(0 != avcodec_send_packet(pCodecCtx, packet))
{
return false;
}
if(0 != avcodec_receive_frame(pCodecCtx, pFrame))
{
return false;
}
SDL_LockYUVOverlay(bmp);
pFrameYUV->data[0] = bmp->pixels[0];
pFrameYUV->data[1] = bmp->pixels[2];
pFrameYUV->data[2] = bmp->pixels[1];
pFrameYUV->linesize[0] = bmp->pitches[0];
pFrameYUV->linesize[1] = bmp->pitches[2];
pFrameYUV->linesize[2] = bmp->pitches[1];
sws_scale(swsCtx,
(const uint8_t* const*)pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
pFrameYUV->data,
pFrameYUV->linesize);
#if OUTPUT_YUV420P
int y_size = pCodecCtx->width*pCodecCtx->height;
fwrite(pFrameYUV->data[0],1,y_size, fpYuv); //Y
fwrite(pFrameYUV->data[1],1,y_size/4, fpYuv); //U
fwrite(pFrameYUV->data[2],1,y_size/4, fpYuv); //V
#endif
SDL_UnlockYUVOverlay(bmp);
SDL_DisplayYUVOverlay(bmp, &rect);
return true;
}
int main(int argc, char* argv[])
{
const char *filepath = "../test.h265";
// 初始化
av_register_all(); // 注册所有的编解码器
avformat_network_init();
// avformat环境
AVFormatContext *pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
printf("Couldn't open input stream.\n");
return -1;
}
if(avformat_find_stream_info(pFormatCtx, NULL)<0) {
printf("Couldn't find stream information.\n");
return -1;
}
int videoIndex = -1;
for(int i = 0; i < pFormatCtx->nb_streams; ++i) {
if(pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoIndex = i;
break;
}
}
if(videoIndex == -1) {
printf("Didn't find a video stream.\n");
return -1;
}
// avcodec环境
AVCodecContext *pCodecCtx = avcodec_alloc_context3(NULL);
if (pCodecCtx == NULL)
{
printf("Could not allocate AVCodecContext\n");
return -1;
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);
// 获取avcodec[解码器]
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
}
// 帧
AVFrame * pFrame = av_frame_alloc();
AVFrame * pFrameYUV = av_frame_alloc();
// SDL Start ----------------------------
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf( "Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}
#if SHOW_FULLSCREEN
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
int screen_w = vi->current_w;
int screen_h = vi->current_h;
SDL_Surface *screen = SDL_SetVideoMode(screen_w, screen_h, 0,SDL_FULLSCREEN);
#else
int screen_w = pCodecCtx->width;
int screen_h = pCodecCtx->height;
SDL_Surface *screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
#endif
if(!screen) {
printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
return -1;
}
SDL_Overlay *bmp = SDL_CreateYUVOverlay(pCodecCtx->width,
pCodecCtx->height,
SDL_YV12_OVERLAY,
screen);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = screen_w;
rect.h = screen_h;
// SDL End------------------------
//Output Information-----------------------------
printf("------------- File Information ------------------\n");
av_dump_format(pFormatCtx, 0, filepath, 0);
PrintAvDetails(pFormatCtx, pCodecCtx, pCodec, videoIndex);
printf("-------------------------------------------------\n");
#if OUTPUT_YUV420P
FILE *fpYuv = fopen("output.yuv","wb+");
#endif
SDL_WM_SetCaption("Simplest FFmpeg Player", NULL);
SwsContext *swsCtx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_YUV420P,
SWS_BICUBIC,
NULL,
NULL,
NULL);
//------------------------------
//Event Loop
SDL_Event event;
AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
for (;;) {
// Wait
SDL_WaitEvent(&event);
if(SFM_REFRESH_EVENT == event.type)
{
//------------------------------
if(0 == av_read_frame(pFormatCtx, packet))
{
if(packet->stream_index == videoIndex)
{
if(!avDecode(pCodecCtx,
pFrame,
pFrameYUV,
bmp,
swsCtx,
packet,
rect))
{
printf("avdecode error\n");
break;
}
}
av_packet_unref(packet);
}
else
{
//Exit Thread
thread_exit = 1;
break;
}
}
}
// release
#if OUTPUT_YUV420P
fclose(fpYuv);
#endif
sws_freeContext(swsCtx);
SDL_Quit();
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}