-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswscale.cc
119 lines (92 loc) · 3.08 KB
/
swscale.cc
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
/*
* copyright (c) 2017 老衲不出家
*
* 2017-08-11
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern "C"
{
#include "libswscale/swscale.h"
#include "libavutil/pixfmt.h"
}
const char *srcFileName = "ds_480x272.yuv";
const char *dstFileName = "ds_720x576.yuv";
int main()
{
// 設定原始 YUV 的長寬
const int in_width = 480;
const int in_height = 272;
// 設定目的 YUV 的長寬
const int out_width = 720;
const int out_height = 576;
const int read_size = in_width * in_height * 3 / 2;
const int write_size = out_width * out_height * 3 / 2;
struct SwsContext *img_convert_ctx = nullptr;
uint8_t *inbuf[4];
uint8_t *outbuf[4];
int inlinesize[4] = { in_width, in_width / 2, in_width / 2, 0 };
int outlinesize[4] = { out_width, out_width / 2, out_width / 2, 0 };
uint8_t *ptr_src_yuv_buf = nullptr;
uint8_t *ptr_dst_yuv_buf = nullptr;
ptr_src_yuv_buf = new uint8_t[in_width * in_height * 3/2];
ptr_dst_yuv_buf = new uint8_t[out_width * out_height * 3 /2];
FILE *fin = fopen(srcFileName, "rb");
FILE *fout = fopen(dstFileName, "wb");
if (fin == NULL) {
fprintf(stderr, "open input file %s error.\n", srcFileName);
return -1;
}
if (fout == NULL) {
fprintf(stderr, "open output file %s error.\n", dstFileName);
return -1;
}
inbuf[0] = (uint8_t *)malloc(in_width*in_height);
inbuf[1] = (uint8_t *)malloc(in_width*in_height >> 2);
inbuf[2] = (uint8_t *)malloc(in_width*in_height >> 2);
inbuf[3] = NULL;
outbuf[0] = (uint8_t *)malloc(out_width*out_height);
outbuf[1] = (uint8_t *)malloc(out_width*out_height >> 2);
outbuf[2] = (uint8_t *)malloc(out_width*out_height >> 2);
outbuf[3] = NULL;
// ********* Initialize software scaling *********
// ********* sws_getContext **********************
img_convert_ctx = sws_getContext(in_width, in_height, AV_PIX_FMT_YUV420P,
out_width, out_height, AV_PIX_FMT_YUV420P, SWS_POINT, nullptr, nullptr, nullptr);
if (img_convert_ctx == NULL) {
fprintf(stderr, "Cannot initialize the conversion context!\n");
return -1;
}
int32_t in_y_size = in_width*in_height;
int32_t out_y_size;
bool bExit = false;
while (!bExit) {
if ((fread(ptr_src_yuv_buf, 1, read_size, fin) < 0) || (feof(fin))) {
bExit = true;
break;
}
memcpy(inbuf[0], ptr_src_yuv_buf, in_y_size);
memcpy(inbuf[1], ptr_src_yuv_buf + in_y_size, in_y_size/4);
memcpy(inbuf[2], ptr_src_yuv_buf + in_y_size*5/4, in_y_size / 4);
// ********* 主要的 function ******
// ********* sws_scale ************
sws_scale(img_convert_ctx, inbuf, inlinesize,
0, in_height, outbuf, outlinesize);
memcpy(ptr_dst_yuv_buf, outbuf[0], out_width*out_height);
memcpy(ptr_dst_yuv_buf + out_width*out_height, outbuf[1], out_width*out_height >> 2);
memcpy(ptr_dst_yuv_buf + (out_width*out_height * 5 >> 2), outbuf[2], out_width*out_height >> 2);
fwrite(ptr_dst_yuv_buf, 1, write_size, fout);
}
// ********* 結束的 function *******
// ********* sws_freeContext *******
sws_freeContext(img_convert_ctx);
fclose(fin);
fclose(fout);
delete[] ptr_src_yuv_buf;
ptr_src_yuv_buf = nullptr;
delete[] ptr_dst_yuv_buf;
ptr_dst_yuv_buf = nullptr;
return 0;
}