-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFmpegSDLPlayer.cpp
185 lines (165 loc) · 5.6 KB
/
FFmpegSDLPlayer.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
#include <stdio.h>
#include <intsafe.h>
#include <stdlib.h>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "SDL.h"
}
#define INBUF_SIZE 4096
using namespace std;
const char filePath[] = "a.h265"; // "640.mp4"; //
bool GetVideoIndex(AVFormatContext * pFormatCtx, int &i) {
int videoIndex = -1;
for (; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoIndex = i;
break;
}
}
return videoIndex;
}
static int DecodePacket(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame) {
int ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
fprintf(stderr, "sending a packet for decoding failed! \n");
return -1;
}
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
}
else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
return -1;
}
return 1;
}
void InitSDLObject(AVFrame * pFrame,
SDL_Window * &screen, SDL_Renderer* &sdlRenderer,
SDL_Texture* &sdlTexture, SDL_Rect &sdlRect) {
screen = SDL_CreateWindow("fd-player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, pFrame->width, pFrame->height, SDL_WINDOW_OPENGL);
sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,
pFrame->width, pFrame->height);
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = pFrame->width;
sdlRect.h = pFrame->height;
}
void ShowFrameInSDL(AVFrame *& pFrame,
SDL_Window * &screen, SDL_Texture*& sdlTexture,
SDL_Renderer*& sdlRenderer, SDL_Rect& sdlRect) {
if (sdlRenderer == nullptr) {
InitSDLObject(pFrame, screen, sdlRenderer, sdlTexture, sdlRect);
}
SDL_UpdateYUVTexture(sdlTexture, &sdlRect,
pFrame->data[0], pFrame->linesize[0],
pFrame->data[1], pFrame->linesize[1],
pFrame->data[2], pFrame->linesize[2]);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
SDL_RenderPresent(sdlRenderer);
SDL_Delay(40);
}
int main(int argc, char* argv[]) {
AVFormatContext *pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, filePath, NULL, NULL) != 0) {
fprintf(stderr, "open file %s failed! \n", filePath);
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
fprintf(stderr, "find stream info failed! \n");
return -1;
}
int i = 0;
int videoIndex = GetVideoIndex(pFormatCtx, i);
if (videoIndex < 0) {
fprintf(stderr, "get video stream %d failed! \n", i);
return -1;
}
AVCodecParameters *pCodecParam = pFormatCtx->streams[videoIndex]->codecpar;
AVCodec *pCodec = avcodec_find_decoder(pCodecParam->codec_id);
if (pCodec == NULL) {
fprintf(stderr, "[Err] find decoder of videoIndex %d failed! \n", i);
return -1;
}
AVCodecParserContext *parser = av_parser_init(pCodec->id);
if (!parser) {
fprintf(stderr, "parser init failed! \n");
return -1;
}
AVCodecContext *codecContext = avcodec_alloc_context3(pCodec);
if (!codecContext) {
fprintf(stderr, "alloc context failed! \n");
return -1;
}
if (avcodec_parameters_to_context(codecContext, pCodecParam) < 0) {
fprintf(stderr, "parameters to context failed! \n");
return -1;
}
if (avcodec_open2(codecContext, pCodec, NULL) < 0) {
fprintf(stderr, "open decoder failed! \n");
return -1;
}
AVFrame *pFrame = av_frame_alloc();
AVFrame * pFrameYUV = av_frame_alloc();
AVPacket* pkt = av_packet_alloc();
if (!pkt) {
fprintf(stderr, "alloc packet failed! \n");
}
av_dump_format(pFormatCtx, 0, filePath, 0);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
fprintf(stderr, "init sdl failed! \n");
return -1;
}
SDL_Window *screen = nullptr;
SDL_Renderer* sdlRenderer = nullptr;
SDL_Texture* sdlTexture = nullptr;
SDL_Rect sdlRect = { 0 };
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
uint8_t *data;
FILE* f = fopen(filePath, "rb");
if (!f) {
fprintf(stderr, "open file failed failed! \n");
return -1;
}
while (!feof(f)) {
int data_size = fread(inbuf, 1, INBUF_SIZE, f);
if (!data_size){
break;
}
data = inbuf;
while (data_size > 0) {
int ret = av_parser_parse2(parser, codecContext, &pkt->data, &pkt->size,
data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
fprintf(stderr, "av parse failed! \n");
return -1;
}
data += ret;
data_size -= ret;
if (pkt->size) {
if (pkt->stream_index == videoIndex) {
int retd = DecodePacket(codecContext, pkt, pFrame);
if (retd == 0) continue;
if (retd == -1) return -1;
ShowFrameInSDL(pFrame, screen, sdlTexture, sdlRenderer, sdlRect);
}
}
}
}
SDL_Quit();
av_parser_close(parser);
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_free_context(&codecContext);
av_packet_free(&pkt);
avformat_close_input(&pFormatCtx);
return 0;
}