forked from YJBeetle/OscilloscopePlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
557 lines (509 loc) · 19.9 KB
/
decode.cpp
File metadata and controls
557 lines (509 loc) · 19.9 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "decode.h"
Decode::Decode(QObject *parent) : QThread(parent)
{
}
Decode::~Decode()
{
avcodec_free_context(&video_dec_ctx);
avcodec_free_context(&audio_dec_ctx);
avformat_close_input(&fmt_ctx);
av_frame_free(&frame);
// av_frame_free(&video_convert_frame);
if(video_edge) delete video_edge;
}
void Decode::set(int scaleX, int scaleY, int moveX, int moveY, int edge)
{
this->scaleX = scaleX;
this->scaleY = scaleY;
this->moveX = moveX;
this->moveY = moveY;
this->edge = edge;
refresh = true;
}
void Decode::setScaleX(int scaleX)
{
this->scaleXrefresh = scaleX;
refresh = true;
}
void Decode::setScaleY(int scaleY)
{
this->scaleYrefresh = scaleY;
refresh = true;
}
void Decode::setMoveX(int moveX)
{
this->moveXrefresh = moveX;
refresh = true;
}
void Decode::setMoveY(int moveY)
{
this->moveYrefresh = moveY;
refresh = true;
}
void Decode::setEdge(int edge)
{
this->edgeRefresh = edge;
refresh = true;
}
void Decode::run()
{
int ret = 0, got_frame;
/* 从文件中读取帧 */
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
AVPacket orig_pkt = pkt;
do {
ret = decode_packet(&got_frame);
if (ret < 0)
break;
pkt.data += ret;
pkt.size -= ret;
} while (pkt.size > 0);
av_packet_unref(&orig_pkt);
}
/* 刷新缓存的帧 */
pkt.data = nullptr;
pkt.size = 0;
do {
decode_packet(&got_frame);
} while (got_frame);
}
bool Decode::isReady()
{
return ready;
}
int Decode::open(QString filename)
{
/* 打开文件读取格式 */
if (avformat_open_input(&fmt_ctx, filename.toLatin1().data(), nullptr, nullptr) < 0)
{
return 1; //无法打开源文件
}
/* 显示输入文件信息(调试用) */
// av_dump_format(fmt_ctx, 0, filename.toLatin1().data(), 0);
/* 检索流信息 */
if (avformat_find_stream_info(fmt_ctx, nullptr) < 0)
{
return 2; //找不到流信息
}
/* 打开视频流 */
video_stream_idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (video_stream_idx >= 0)
{
video_stream = fmt_ctx->streams[video_stream_idx]; //视频流
/* 寻找视频流的解码器 */
AVCodec* video_dec = nullptr; //视频流的解码器
video_dec = avcodec_find_decoder(video_stream->codecpar->codec_id);
if (video_dec)
{
/* 为解码器分配编解码器上下文 */
video_dec_ctx = avcodec_alloc_context3(video_dec);
if (video_dec_ctx)
{
/* 将编解码器参数从输入流复制到输出编解码器上下文 */
if (avcodec_parameters_to_context(video_dec_ctx, video_stream->codecpar) >= 0)
{
/* 启动解码器 */
if (avcodec_open2(video_dec_ctx, video_dec, nullptr) >= 0)
{
/* 读取一些信息,宽高像素格式 */
video_width = video_dec_ctx->width;
video_height = video_dec_ctx->height;
video_pix_fmt = video_dec_ctx->pix_fmt;
//转换色彩
// video_convert_frame = av_frame_alloc(); //帧
// if (!video_convert_frame)
// return 8; //无法分配帧
// unsigned char* out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_BGRA, video_width, video_height, 1));
// if (!out_buffer)
// return 8; //无法分配
// av_image_fill_arrays(video_convert_frame->data, video_convert_frame->linesize, out_buffer, AV_PIX_FMT_BGRA, video_width, video_height, 1);
// video_convert_ctx = sws_getContext(video_width, video_height, video_pix_fmt,
// video_width, video_height, AV_PIX_FMT_BGRA,
// SWS_BICUBIC, NULL, NULL, NULL);
// if(!video_convert_ctx)
// {
// audio_stream_idx = -1;
// return 8; //无法设置视频色彩转换上下文
// }
//边缘检测
video_edge = new quint8[256 * 256];
}
else
return 7; //无法打开视频编解码器
}
else
return 6; //无法将视频编解码器参数复制到解码器上下文
}
else
return 5; //无法分配编解码器上下文
}
else
return 4; //找不到视频流的解码器
}
else
return 3; //找不到视频流
/* 打开音频流 */
audio_stream_idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
if (audio_stream_idx >= 0)
{
audio_stream = fmt_ctx->streams[audio_stream_idx]; //音频流
/* 寻找视频流的解码器 */
AVCodec* audio_dec = nullptr; //音频流的解码器
audio_dec = avcodec_find_decoder(audio_stream->codecpar->codec_id);
if (audio_dec)
{
/* 为解码器分配编解码器上下文 */
audio_dec_ctx = avcodec_alloc_context3(audio_dec);
;
if (audio_dec_ctx)
{
/* 将编解码器参数从输入流复制到输出编解码器上下文 */
if (avcodec_parameters_to_context(audio_dec_ctx, audio_stream->codecpar) >= 0)
{
/* 启动解码器 */
if (avcodec_open2(audio_dec_ctx, audio_dec, nullptr) >= 0)
{
/* 音频重采样 */ //采样为44100双声道16位整型
audio_convert_ctx = swr_alloc_set_opts(nullptr,
AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_U8, 44100,
int64_t(audio_dec_ctx->channel_layout), audio_dec_ctx->sample_fmt, audio_dec_ctx->sample_rate,
0, nullptr);
//audio_convert_ctx = swr_alloc_set_opts(nullptr, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, 44100, audio_stream->codec->channel_layout, audio_stream->codec->sample_fmt, audio_stream->codec->sample_rate, 0, NULL); //采样为44100双声道16位整型
if(!audio_convert_ctx)
{
audio_stream_idx = -1;
return 15; //无法设置重新采样上下文
}
swr_init(audio_convert_ctx);
}
else
{
audio_stream_idx = -1;
return 14; //无法打开音频编解码器
}
}
else
{
audio_stream_idx = -1;
return 13; //无法将音频编解码器参数复制到解码器上下文
}
}
else
{
audio_stream_idx = -1;
return 12; //无法分配编解码器上下文
}
}
else
{
audio_stream_idx = -1;
return 11; //找不到音频流的解码器
}
}
else
{
audio_stream_idx = -1;
return 10; //找不到音频流
}
frame = av_frame_alloc();
if (!frame)
return 16; //无法分配帧
/* 初始化数据包,data设置为nullptr,让demuxer填充它 */
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
ready = true; //已准备好状态
return 0;
}
AVRational Decode::fps()
{
if(video_stream && video_stream->avg_frame_rate.num && video_stream->avg_frame_rate.den)
return video_stream->avg_frame_rate;
else
{
AVRational ret;
ret.den = 1;
ret.num = 30;
return ret;
}
}
int Decode::width()
{
return video_width;
}
int Decode::height()
{
return video_height;
}
int Decode::decode_packet(int *got_frame)
{
int ret = 0;
int decoded = pkt.size;
*got_frame = 0;
//检查队列长度
auto fps = this->fps();
if(video.size() > 4) //缓存4f,如果队列中已有的超过4f则休息一帧的时间
QTest::qSleep(1000 * fps.den / fps.num);
if (pkt.stream_index == video_stream_idx) //包是视频包
{
/* 解码视频帧 */
ret = avcodec_send_packet(video_dec_ctx, &pkt);
if (ret < 0)
{
fprintf(stderr, "发送数据包进行解码时出错\n");
return ret;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(video_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return ret;
else if (ret < 0) {
fprintf(stderr, "解码时出错\n");
return ret;
}
//输出帧信息
//printf("video_frame n:%d coded_n:%d\n",
// video_frame_count++, frame->coded_picture_number);
//转换色彩
//sws_scale(video_convert_ctx,
// frame->data, frame->linesize, 0, video_height,
// video_convert_frame->data, video_convert_frame->linesize);
//检查刷新
if(refresh)
{
refresh = false;
scaleX = scaleXrefresh;
scaleY = scaleYrefresh;
moveX = moveXrefresh;
moveY = moveYrefresh;
edge = edgeRefresh;
}
//边缘检测
float temp1[9]={1,0,-1,1,0,-1,1,0,-1}; //模板数组
float temp2[9]={-1,-1,-1,0,0,0,1,1,1};
float result1; //用于暂存模板值
float result2;
int count = 0; //总点数计数
memset(video_edge, 0, 256 * 256);
{
int y = (1 - moveY - video_height / 2) * scaleY / 100 + 256 / 2;
if(y < 0)
y = 0;
int yMax = (video_height - 1 - moveY - video_height / 2) * scaleY / 100 + 256 / 2;
if(yMax > 256)
yMax = 256;
for (; y < yMax; y++)
{
int yy = video_height / 2 + (-256 / 2 + y) * 100 / scaleY + moveY;
{
int x = (1 - moveX - video_width / 2) * scaleX / 100 + 256 / 2;
if(x < 0)
x = 0;
int xMax = (video_width - 1 - moveX - video_width / 2) * scaleX / 100 + 256 / 2;
if(xMax > 256)
xMax = 256;
for(; x < xMax; x++)
{
int xx = video_width / 2 + (-256 / 2 + x) * 100 / scaleX + moveX;
result1 = 0;
result2 = 0;
for (int ty = 0; ty < 3; ty++)
{
for (int tx = 0; tx < 3; tx++)
{
int z = frame->data[0][(yy - 1 + ty) * frame->linesize[0] + xx - 1 + tx]; //这里我们假装视频是YUV,这里只用Y就是灰度了
result1 += z * temp1[ ty * 3 + tx];
result2 += z * temp2[ ty * 3 + tx];
}
}
result1 = abs(int(result1));
result2 = abs(int(result2));
if(result1 < result2)
result1 = result2;
if((result1 > edge)) //超过阈值则为有效点
{
video_edge[y * 256 + x] = 255;
count++;
}
}
}
}
}
//生成QImage
//QImage image(video_width, video_height, QImage::Format_ARGB32);
//for (int y = 0; y < video_height; y++)
// memcpy(image.scanLine(y), video_convert_frame->data[0] + y * video_convert_frame->linesize[0], video_width * 4);
QImage image(256, 256, QImage::Format_Grayscale8);
for (int y = 0; y < 256; y++)
{
int yy = video_height / 2 + (-256 / 2 + y) * 100 / scaleY + moveY;
if(yy < 0 || yy >= video_height)
memset(image.scanLine(y), 0, 256);
else
for(int x = 0; x < 256; x++)
{
int xx = video_width / 2 + (-256 / 2 + x) * 100 / scaleX + moveX;
if(xx < 0 || xx >= video_width)
image.scanLine(y)[x] = 0;
else
image.scanLine(y)[x] = frame->data[0][yy * frame->linesize[0] + xx]; //这里我们假装视频是YUV,这里只用Y就是灰度了
}
}
video.enqueue(image); //添加到队列尾部
QImage imageEdge(256, 256, QImage::Format_Grayscale8);
for (int y = 0; y < 256; y++)
memcpy(imageEdge.scanLine(y), video_edge + y * 256, 256);
videoEdge.enqueue(imageEdge); //添加到队列尾部
//计算路径点
QVector<Point> points(count);
int x = 0;
int y = 0;
for (int i = 0; i < count; i++)
{
int rMax;
rMax = (x > y) ? x : y;
rMax = (rMax > 256 - x) ? rMax : 256 - x;
rMax = (rMax > 256 - y) ? rMax : 256 - y;
for (int r = 0; r < rMax; r++)
{
int xMin = x - r;
int xMax = x + r;
int yMin = y - r;
int yMax = y + r;
bool xMinOut = false;
bool xMaxOut = false;
bool yMinOut = false;
bool yMaxOut = false;
if(xMin < 0)
{
xMin = 0;
xMinOut = true;
}
if(xMax >= 256)
{
xMax = 255;
xMaxOut = true;
}
if(yMin < 0)
{
yMin = 0;
yMinOut = true;
}
if(yMax >= 256)
{
yMax = 255;
yMaxOut = true;
}
//上边缘
if(!yMinOut)
{
for(int xx = xMin; xx < xMax; xx++)
{
if(video_edge[yMin * 256 + xx] == 255)
{
points[i].x = x = xx;
points[i].y = y = yMin;
video_edge[yMin * 256 + xx] = 254; //标记为已使用过了
goto find;
}
}
}
//下边缘
if(!yMaxOut)
{
for(int xx = xMin; xx < xMax; xx++)
{
if(video_edge[yMax * 256 + xx] == 255)
{
points[i].x = x = xx;
points[i].y = y = yMax;
video_edge[yMax * 256 + xx] = 254; //标记为已使用过了
goto find;
}
}
}
//左边缘
if(!xMinOut)
{
for(int yy = yMin; yy < yMax; yy++)
{
if(video_edge[yy * 256 + xMin] == 255)
{
points[i].x = x = xMin;
points[i].y = y = yy;
video_edge[yy * 256 + xMin] = 254; //标记为已使用过了
goto find;
}
}
}
//右边缘
if(!xMaxOut)
{
for(int yy = yMin; yy < yMax; yy++)
{
if(video_edge[yy * 256 + xMax] == 255)
{
points[i].x = x = xMax;
points[i].y = y = yy;
video_edge[yy * 256 + xMax] = 254; //标记为已使用过了
goto find;
}
}
}
}
find:
;;
}
this->points.enqueue(points);
}
}
else if (pkt.stream_index == audio_stream_idx) //是音频包
{
/* 解码音频帧 */
ret = avcodec_send_packet(audio_dec_ctx, &pkt);
if (ret < 0)
{
fprintf(stderr, "将数据包提交给解码器时出错\n");
return ret;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(audio_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return ret;
else if (ret < 0) {
fprintf(stderr, "解码时出错\n");
return ret;
}
//输出帧信息
//printf("audio_frame n:%d nb_samples:%d pts:%s\n",
// audio_frame_count++, frame->nb_samples,
// av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
int data_size = av_get_bytes_per_sample(audio_dec_ctx->sample_fmt);
if (data_size < 0) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "无法计算数据大小\n");
return -1;
}
// for (i = 0; i < frame->nb_samples; i++)
// for (ch = 0; ch < audio_dec_ctx->channels; ch++)
// fwrite(frame->data[ch] + data_size*i, 1, data_size, audio_dst_file);
// size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(AVSampleFormat(frame->format));
/* Write the raw audio data samples of the first plane. This works
* fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
* most audio decoders output planar audio, which uses a separate
* plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
* In other words, this code will write only the first audio channel
* in these cases.
* You should use libswresample or libavfilter to convert the frame
* to packed data. */
// fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
//memset(play_buf, sizeof(uint8_t), out_size);
// swr_convert(pSwrCtx, &play_buf , out_size, (const uint8_t**)frame->data, frame->nb_samples);
// out->write((char*)play_buf, out_size);
// QTest::qSleep( 20 );
}
}
return decoded;
}