-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg.cpp
337 lines (279 loc) · 10.7 KB
/
ffmpeg.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "ffmpeg.h"
#include <QProcess>
#include <QDebug>
extern "C" {
#include <3rdparty/FFMPEG/include/libavcodec/avcodec.h>
#include <3rdparty/FFMPEG/include/libavutil/imgutils.h>
#include <3rdparty/FFMPEG/include/libavutil/opt.h>
}
//For FFmpeg gerekli mi dene
//#ifndef INT64_C
//#define INT64_C(c) (c ## LL)
//#define UINT64_C(c) (c ## ULL)
//#endif
//#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
//#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
//#define AVFMT_RAWPICTURE 0x0020
//For FFmpeg gerekli mi dene
//This code should be updated for latest FFmpeg version
FFmpeg::FFmpeg(){
qInfo("FFMPEG CONSTRUCTOR");
ifmt_ctx = NULL;
ofmt_ctx = NULL;
avdic = NULL;
ofmt = NULL;
}
FFmpeg::~FFmpeg(){
qInfo("FFMPEG DESTRUCTOR");
av_dict_free(&avdic);
avformat_close_input(&ifmt_ctx);
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
}
void FFmpeg::runCommandLine(const QString command){
//Thread must be used. If not, gui wait the end of the process
QProcess FFMPEG;
QString process = "ffmpeg";
QStringList paramList = command.split(QRegExp("\\s+"), QString::SkipEmptyParts);
paramList.removeFirst();
FFMPEG.start(process,paramList);
if ( !(FFMPEG.waitForFinished()) )
qDebug() << "Conversion failed:" << FFMPEG.errorString();
else
qDebug() << "Conversion output:" << FFMPEG.readAll();
}
/*
int FFmpeg::saveVideoFromStream(){
in_filename = "rtp://233.233.233.233:6666";
//date format should be defined
QString date = QDateTime::currentDateTime().toString();
QByteArray ba = date.toLocal8Bit();
out_filename = ba.data();
//out_filename = "output.ts";
av_register_all();
avformat_network_init();
char option_key[] = "rtsp_transport";
char option_value[] = "tcp";
av_dict_set(&avdic, option_key, option_value, 0);
char option_key2[] = "max_delay";
char option_value2[] = "5000000";
av_dict_set(&avdic, option_key2, option_value2, 0);
AVPacket pkt;
int video_index = -1;
int frame_index = 0;
int i;
//Open the input stream
int ret;
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, &avdic)) < 0)
{
std::cout << "Could not open input file." << '\n';
goto end;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0)
{
std::cout << "Failed to retrieve input stream information" << '\n';
goto end;
}
//nb_streams represent several streams
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
//Video streaming
video_index = i;
std::cout << "get videostream." << '\n';
break;
}
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
//Open the output stream
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx)
{
printf("Could not create output context\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ofmt = ofmt_ctx->oformat;
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream* in_stream = ifmt_ctx->streams[i];
AVStream* out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream)
{
printf("Failed allocating output stream.\n");
ret = AVERROR_UNKNOWN;
goto end;
}
//Copy the encoding information of the output stream to the input stream
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0)
{
printf("Failed to copy context from input to output stream codec context\n");
goto end;
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
av_dump_format(ofmt_ctx, 0, out_filename, 1);
if (!(ofmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
printf("Could not open output URL '%s'", out_filename);
goto end;
}
}
//Write file header to output file
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0)
{
printf("Error occured when opening output URL\n");
goto end;
}
//Continuous access to data packets in the while loop, regardless of audio and video, is stored in the file
while (1)
{
AVStream* in_stream, * out_stream;
//Get a packet from the input stream
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
//copy packet
//Conversion of PTS/DTS Timing
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
//printf("pts %d dts %d base %d\n",pkt.pts,pkt.dts, in_stream->time_base);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
//Not all packet s in this while loop are video frames. Record when you receive a video frame
if (pkt.stream_index == video_index)
{
printf("Receive %8d video frames from input URL\n", frame_index);
frame_index++;
}
//Write the package data to a file.
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0)
{
if (ret == -22) {
continue;
}
else {
printf("Error muxing packet.error code %d\n", ret);
break;
}
}
//Av_free_packet(&pkt); // This sentence has been replaced by av_packet_unref in the new version.
av_packet_unref(&pkt);
}
//Write the end of the file
av_write_trailer(ofmt_ctx);
end:
av_dict_free(&avdic);
avformat_close_input(&ifmt_ctx);
//Close input
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF)
{
std::cout << "Error occured." << '\n';
return -1;
}
return 0;
}
*/
//This method is appropriate for last version of FFmpeg
void FFmpeg::encode(AVCodecContext* enc_ctx, AVFrame* frame, AVPacket* pkt,FILE* outfile)
{
int ret;
if (frame)
printf("Send frame %3\n", frame->pts);
ret = avcodec_send_frame(enc_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
printf("Write packet %3 (size=%5d)\n", pkt->pts, pkt->size);
fwrite(pkt->data, 1, pkt->size, outfile);
av_packet_unref(pkt);
}
}
//This method is appropriate last version of FFmpeg
void FFmpeg::saveFrame(unsigned char* buf, int wrap, int xsize, int ysize, char* filename)
{
FILE* f;
int i;
f = fopen(filename, "w");
// writing the minimal required header for a pgm file format
// portable graymap format -> https://en.wikipedia.org/wiki/Netpbm_format#PGM_example
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
// writing line by line
for (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
void FFmpeg::logging(const char* fmt, ...)
{
va_list args;
fprintf(stderr, "LOG: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
int FFmpeg::decode_packet(AVPacket* pPacket, AVCodecContext* pCodecContext, AVFrame* pFrame)
{
// Supply raw packet data as input to a decoder
// https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga58bc4bf1e0ac59e27362597e467efff3
int response = avcodec_send_packet(pCodecContext, pPacket);
if (response < 0) {
//logging("Error while sending a packet to the decoder: %s", av_err2str(response));
logging("Error while sending a packet to the decoder:");
return response;
}
while (response >= 0)
{
// Return decoded output data (into a frame) from a decoder
// https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
response = avcodec_receive_frame(pCodecContext, pFrame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
break;
}
else if (response < 0) {
//logging("Error while receiving a frame from the decoder: %s", av_err2str(response));
logging("Error while receiving a frame from the decoder:");
return response;
}
if (response >= 0) {
logging(
"Frame %d (type=%c, size=%d bytes) pts %d key_frame %d [DTS %d]",
pCodecContext->frame_number,
av_get_picture_type_char(pFrame->pict_type),
pFrame->pkt_size,
pFrame->pts,
pFrame->key_frame,
pFrame->coded_picture_number
);
//char frame_filename[1024];
//snprintf(frame_filename, sizeof(frame_filename), "%s-%d.pgm", "frame", pCodecContext->frame_number);
// save a grayscale frame into a .pgm file
//saveFrame(pFrame->data[0], pFrame->linesize[0], pFrame->width, pFrame->height, frame_filename);
}
}
return 0;
}