Ticket #4881: scaling_r.c

File scaling_r.c, 2.9 KB (added by rxt, 11 years ago)
Line 
1#include <libavutil/imgutils.h>
2#include <libswscale/swscale.h>
3
4//#define NO_SLICES
5
6/* Taken from scaling_video.c example */
7static void fill_yuv_image(uint8_t *data[4], int linesize[4],
8 int width, int height, int frame_index)
9{
10 int x, y;
11
12 /* Y */
13 for (y = 0; y < height; y++)
14 for (x = 0; x < width; x++)
15 data[0][y * linesize[0] + x] = x + y + frame_index * 3;
16
17 /* Cb and Cr */
18 for (y = 0; y < height / 2; y++) {
19 for (x = 0; x < width / 2; x++) {
20 data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
21 data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
22 }
23 }
24}
25
26int main()
27{
28 uint8_t *src_data[4], *dst_data[4];
29 int src_linesize[4], dst_linesize[4];
30 int src_w, src_h, dst_w, dst_h;
31 struct SwsContext *sws_ctx;
32 FILE *dst_file;
33 int i, ii;
34
35 src_w = 720;
36 src_h = 480;
37 dst_w = 720;
38 dst_h = 540;
39
40 dst_file = fopen("qqq", "wb");
41
42 sws_ctx = sws_getContext(src_w, src_h, AV_PIX_FMT_YUV420P,
43 dst_w, dst_h, AV_PIX_FMT_RGB24,
44 SWS_PRINT_INFO|SWS_BICUBIC, NULL, NULL, NULL);
45
46 src_linesize[0] = 768;//FFALIGN(src_w,16);
47 src_data[0] = av_malloc(src_linesize[0]*src_h+16);
48 src_linesize[1] = src_linesize[2] = 768/2;//FFALIGN(src_w/2,16);
49 src_data[1] = av_malloc(src_linesize[1]*src_h+16);
50 src_data[2] = av_malloc(src_linesize[2]*src_h+16);
51 dst_linesize[0] = 2160;//FFALIGN(dst_w*3,16);
52 dst_data[0] = av_malloc(dst_linesize[0]*dst_h+16);
53
54 src_data[3] = NULL;
55 src_linesize[3] = 0;
56 dst_data[1] = dst_data[2] = dst_data[3] = NULL;
57 dst_linesize[1] = dst_linesize[2] = dst_linesize[3] = 0;
58
59 for (ii = 0; ii < 100; ii++) {
60 /* generate synthetic video */
61 fill_yuv_image(src_data, src_linesize, src_w, src_h, ii);
62
63#ifdef NO_SLICES
64 /* Single pass */
65 sws_scale(sws_ctx, (const uint8_t * const*)src_data,
66 src_linesize, 0, src_h, dst_data, dst_linesize);
67#else
68 /* Slices */
69 for (i = 0; i < src_h; i+=16) {
70 // Copy data to the beginninig of the buffer to simulate slice decoding
71 memcpy(src_data[0], src_data[0]+src_linesize[0]*i, src_linesize[0]*16);
72 memcpy(src_data[1], src_data[1]+src_linesize[1]*i/2, src_linesize[1]*8);
73 memcpy(src_data[2], src_data[2]+src_linesize[2]*i/2, src_linesize[2]*8);
74 // Do slice scaling to destination buffer
75 sws_scale(sws_ctx, (const uint8_t * const*)src_data,
76 src_linesize, i, 16, dst_data, dst_linesize);
77 }
78#endif
79 /* write scaled image to file */
80 fwrite(dst_data[0], 1, dst_w*dst_h*3, dst_file);
81 }
82
83 fclose(dst_file);
84 av_freep(&src_data[0]);
85 av_freep(&src_data[1]);
86 av_freep(&src_data[2]);
87 av_freep(&dst_data[0]);
88 sws_freeContext(sws_ctx);
89 return 0;
90}