-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl_ffmpeg.c
183 lines (157 loc) · 5.83 KB
/
sdl_ffmpeg.c
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
#include <SDL2/SDL.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/packet.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
// cc sdl_ffmpeg.c -lavcodec -lavformat -lavutil -lswscale -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_image
int main(int argc, char* argv[]) {
if (argc < 2)
fprintf(stderr, "Usage: %s <video_file>\n", argv[0]), exit(1);
const char* filename = argv[1];
// Initialize FFmpeg
avformat_network_init();
// Open the video file
AVFormatContext* formatContext = NULL;
if (avformat_open_input(&formatContext, filename, NULL, NULL) != 0) {
fprintf(stderr, "Could not open file %s\n", filename);
exit(1);
}
// Retrieve stream information
if (avformat_find_stream_info(formatContext, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
// Find the first video stream
int videoStreamIndex = -1;
for (unsigned int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
fprintf(stderr, "Could not find a video stream\n");
exit(1);
}
// Get a pointer to the codec context for the video stream
AVCodecParameters* codecParameters = formatContext->streams[videoStreamIndex]->codecpar;
const AVCodec* codec = avcodec_find_decoder(codecParameters->codec_id);
if (codec == NULL) {
fprintf(stderr, "Unsupported codec!\n");
exit(1);
}
// Open codec
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) {
fprintf(stderr, "Could not copy codec context\n");
exit(1);
}
if (avcodec_open2(codecContext, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}
// Create SDL window
SDL_Window* window = SDL_CreateWindow(
"SDL Video Player",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
codecContext->width, codecContext->height,
SDL_WINDOW_OPENGL
);
if (!window) {
fprintf(stderr, "SDL: could not create window - exiting\n");
exit(1);
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture* texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING,
codecContext->width, codecContext->height
);
// Prepare to read frames
AVPacket* packet = av_packet_alloc();
AVFrame* frame = av_frame_alloc();
AVFrame* frameYUV = av_frame_alloc();
if (!frame || !frameYUV) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
if (AV_TIME_BASE != 1000000) {
fprintf(stderr, "AV_TIME_BASE != 1000000\n");
exit(1);
}
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, codecContext->width, codecContext->height, 32);
uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(frameYUV->data, frameYUV->linesize, buffer, AV_PIX_FMT_YUV420P, codecContext->width, codecContext->height, 1);
struct SwsContext* sws_ctx = sws_getContext(
codecContext->width,
codecContext->height,
codecContext->pix_fmt,
codecContext->width,
codecContext->height,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
AVRational time_base_q = {1, AV_TIME_BASE};
int64_t last_pts = 0;
// Main loop
while (av_read_frame(formatContext, packet) >= 0) {
if (packet->stream_index == videoStreamIndex) {
if (avcodec_send_packet(codecContext, packet) == 0) {
while (avcodec_receive_frame(codecContext, frame) == 0) {
// Calculate delay, considering the previous frame's delay
// Current frame time in microseconds
int64_t pts = frame->pts != AV_NOPTS_VALUE ? frame->pts : 0;
if (last_pts > 0) {
int64_t delay_microseconds = pts - last_pts;
if (delay_microseconds > 0) {
int64_t delay_miliseconds = delay_microseconds / 1000;
SDL_Delay(delay_miliseconds);
}
}
last_pts = pts;
sws_scale(
sws_ctx,
(uint8_t const* const*)frame->data,
frame->linesize,
0,
codecContext->height,
frameYUV->data,
frameYUV->linesize
);
SDL_UpdateYUVTexture(
texture,
NULL,
frameYUV->data[0], frameYUV->linesize[0],
frameYUV->data[1], frameYUV->linesize[1],
frameYUV->data[2], frameYUV->linesize[2]
);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
}
}
av_packet_unref(packet);
}
// Free resources
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
av_free(buffer);
av_frame_free(&frameYUV);
av_frame_free(&frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
return 0;
}