Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
tests: Refactor rotozoom/videogen common code into a separate file.
tests: Mark some file-internal symbols as static.
build: Drop leftover .exp pattern from LIBSUFFIXES list.
vsrc_buffer: return EAGAIN if no frame is available.
WMAL: Shift output samples by the specified number of padding zeroes.
WMAL: Restore removed code in mclms_predict()
rtpdec_h264: Remove a useless ifdef
rtpdec_h264: Remove outdated/useless/incorrect comments
rtpdec_h264: Remove useless memory corruption checks
rtpdec_h264: Return proper error codes
rtpdec_h264: Check the available data length before reading
rtpdec_h264: Add input size checks
png: check bit depth for PAL8/Y400A pixel formats.
ea: check chunk_size for validity.
celp filters: Do not read earlier than the start of the 'out' vector.

Conflicts:
libavcodec/pngdec.c
libavfilter/src_buffer.c
tests/rotozoom.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/05/06 03:10:45
Showing 10 changed files
... ...
@@ -115,6 +115,6 @@ OBJDIRS := $(OBJDIRS) $(dir $(OBJS) $(HOSTOBJS) $(TESTOBJS))
115 115
 
116 116
 CLEANSUFFIXES     = *.d *.o *~ *.ho *.map *.ver *.gcno *.gcda
117 117
 DISTCLEANSUFFIXES = *.pc
118
-LIBSUFFIXES       = *.a *.lib *.so *.so.* *.dylib *.dll *.def *.dll.a *.exp
118
+LIBSUFFIXES       = *.a *.lib *.so *.so.* *.dylib *.dll *.def *.dll.a
119 119
 
120 120
 -include $(wildcard $(OBJS:.o=.d) $(TESTOBJS:.o=.d))
... ...
@@ -133,9 +133,8 @@ void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
133 133
         out2 -= val * old_out2;
134 134
         out3 -= val * old_out3;
135 135
 
136
-        old_out3 = out[-5];
137
-
138 136
         for (i = 5; i <= filter_length; i += 2) {
137
+            old_out3 = out[-i];
139 138
             val = filter_coeffs[i-1];
140 139
 
141 140
             out0 -= val * old_out3;
... ...
@@ -154,7 +153,6 @@ void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
154 154
 
155 155
             FFSWAP(float, old_out0, old_out2);
156 156
             old_out1 = old_out3;
157
-            old_out3 = out[-i-2];
158 157
         }
159 158
 
160 159
         tmp0 = out0;
... ...
@@ -501,7 +501,7 @@ static int decode_frame(AVCodecContext *avctx,
501 501
                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
502 502
                 } else if (s->bit_depth == 8 &&
503 503
                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
504
-                    avctx->pix_fmt = PIX_FMT_GRAY8A;
504
+                    avctx->pix_fmt = PIX_FMT_Y400A;
505 505
                 } else {
506 506
                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
507 507
                                                 "and color type %d\n",
... ...
@@ -658,9 +658,9 @@ static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
658 658
     int num_channels = s->num_channels;
659 659
 
660 660
     for (ich = 0; ich < num_channels; ich++) {
661
+        pred[ich] = 0;
661 662
         if (!s->is_channel_coded[ich])
662 663
             continue;
663
-        pred[ich] = 0;
664 664
         for (i = 0; i < order * num_channels; i++)
665 665
             pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
666 666
                          s->mclms_coeffs[i + order * num_channels * ich];
... ...
@@ -983,10 +983,10 @@ static int decode_subframe(WmallDecodeCtx *s)
983 983
 
984 984
         for (j = 0; j < subframe_len; j++) {
985 985
             if (s->bits_per_sample == 16) {
986
-                *s->samples_16[c] = (int16_t) s->channel_residues[c][j];
986
+                *s->samples_16[c] = (int16_t) s->channel_residues[c][j] << padding_zeroes;
987 987
                 s->samples_16[c] += s->num_channels;
988 988
             } else {
989
-                *s->samples_32[c] = s->channel_residues[c][j];
989
+                *s->samples_32[c] = s->channel_residues[c][j] << padding_zeroes;
990 990
                 s->samples_32[c] += s->num_channels;
991 991
             }
992 992
         }
... ...
@@ -495,12 +495,17 @@ static int ea_read_packet(AVFormatContext *s,
495 495
 
496 496
     while (!packet_read || partial_packet) {
497 497
         chunk_type = avio_rl32(pb);
498
-        chunk_size = (ea->big_endian ? avio_rb32(pb) : avio_rl32(pb)) - 8;
498
+        chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
499
+        if (chunk_size <= 8)
500
+            return AVERROR_INVALIDDATA;
501
+        chunk_size -= 8;
499 502
 
500 503
         switch (chunk_type) {
501 504
         /* audio data */
502 505
         case ISNh_TAG:
503 506
             /* header chunk also contains data; skip over the header portion*/
507
+            if (chunk_size < 32)
508
+                return AVERROR_INVALIDDATA;
504 509
             avio_skip(pb, 32);
505 510
             chunk_size -= 32;
506 511
         case ISNd_TAG:
... ...
@@ -30,10 +30,6 @@
30 30
  * Single Nal Unit Mode (0), or
31 31
  * Non-Interleaved Mode (1).  It currently does not support
32 32
  * Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24, FU-B packet types)
33
- *
34
- * @note TODO:
35
- * 1) RTCP sender reports for udp streams are required..
36
- *
37 33
  */
38 34
 
39 35
 #include "libavutil/base64.h"
... ...
@@ -49,26 +45,17 @@
49 49
 #include "rtpdec.h"
50 50
 #include "rtpdec_formats.h"
51 51
 
52
-/**
53
-    RTP/H264 specific private data.
54
-*/
55 52
 struct PayloadContext {
56
-    unsigned long cookie;       ///< sanity check, to make sure we get the pointer we're expecting.
57
-
58 53
     //sdp setup parameters
59
-    uint8_t profile_idc;        ///< from the sdp setup parameters.
60
-    uint8_t profile_iop;        ///< from the sdp setup parameters.
61
-    uint8_t level_idc;          ///< from the sdp setup parameters.
62
-    int packetization_mode;     ///< from the sdp setup parameters.
54
+    uint8_t profile_idc;
55
+    uint8_t profile_iop;
56
+    uint8_t level_idc;
57
+    int packetization_mode;
63 58
 #ifdef DEBUG
64 59
     int packet_types_received[32];
65 60
 #endif
66 61
 };
67 62
 
68
-#define MAGIC_COOKIE (0xdeadbeef)       ///< Cookie for the extradata; to verify we are what we think we are, and that we haven't been freed.
69
-#define DEAD_COOKIE (0xdeaddead)        ///< Cookie for the extradata; once it is freed.
70
-
71
-/* ---------------- private code */
72 63
 static int sdp_parse_fmtp_config_h264(AVStream * stream,
73 64
                                       PayloadContext * h264_data,
74 65
                                       char *attr, char *value)
... ...
@@ -104,7 +91,6 @@ static int sdp_parse_fmtp_config_h264(AVStream * stream,
104 104
             buffer[0] = value[4]; buffer[1] = value[5];
105 105
             level_idc = strtol(buffer, NULL, 16);
106 106
 
107
-            // set the parameters...
108 107
             av_log(codec, AV_LOG_DEBUG,
109 108
                    "RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
110 109
                    profile_idc, profile_iop, level_idc);
... ...
@@ -141,7 +127,6 @@ static int sdp_parse_fmtp_config_h264(AVStream * stream,
141 141
                 {
142 142
                     if(codec->extradata_size)
143 143
                     {
144
-                        // av_realloc?
145 144
                         memcpy(dest, codec->extradata, codec->extradata_size);
146 145
                         av_free(codec->extradata);
147 146
                     }
... ...
@@ -173,15 +158,19 @@ static int h264_handle_packet(AVFormatContext *ctx,
173 173
                               const uint8_t * buf,
174 174
                               int len, int flags)
175 175
 {
176
-    uint8_t nal = buf[0];
177
-    uint8_t type = (nal & 0x1f);
176
+    uint8_t nal;
177
+    uint8_t type;
178 178
     int result= 0;
179 179
     uint8_t start_sequence[] = { 0, 0, 0, 1 };
180 180
 
181
-#ifdef DEBUG
181
+    if (!len) {
182
+        av_log(ctx, AV_LOG_ERROR, "Empty H264 RTP packet\n");
183
+        return AVERROR_INVALIDDATA;
184
+    }
185
+    nal  = buf[0];
186
+    type = nal & 0x1f;
187
+
182 188
     assert(data);
183
-    assert(data->cookie == MAGIC_COOKIE);
184
-#endif
185 189
     assert(buf);
186 190
 
187 191
     if (type >= 1 && type <= 23)
... ...
@@ -211,8 +200,8 @@ static int h264_handle_packet(AVFormatContext *ctx,
211 211
                 const uint8_t *src= buf;
212 212
                 int src_len= len;
213 213
 
214
-                do {
215
-                    uint16_t nal_size = AV_RB16(src); // this going to be a problem if unaligned (can it be?)
214
+                while (src_len > 2) {
215
+                    uint16_t nal_size = AV_RB16(src);
216 216
 
217 217
                     // consume the length of the aggregate...
218 218
                     src += 2;
... ...
@@ -245,7 +234,7 @@ static int h264_handle_packet(AVFormatContext *ctx,
245 245
                     if (src_len < 0)
246 246
                         av_log(ctx, AV_LOG_ERROR,
247 247
                                "Consumed more bytes than we got! (%d)\n", src_len);
248
-                } while (src_len > 2);      // because there could be rtp padding..
248
+                }
249 249
 
250 250
                 if(pass==0) {
251 251
                     // now we know the total size of the packet (with the start sequences added)
... ...
@@ -265,16 +254,16 @@ static int h264_handle_packet(AVFormatContext *ctx,
265 265
         av_log(ctx, AV_LOG_ERROR,
266 266
                "Unhandled type (%d) (See RFC for implementation details\n",
267 267
                type);
268
-        result= -1;
268
+        result = AVERROR(ENOSYS);
269 269
         break;
270 270
 
271 271
     case 28:                   // FU-A (fragmented nal)
272 272
         buf++;
273 273
         len--;                  // skip the fu_indicator
274
-        {
274
+        if (len > 1) {
275 275
             // these are the same as above, we just redo them here for clarity...
276 276
             uint8_t fu_indicator = nal;
277
-            uint8_t fu_header = *buf;   // read the fu_header.
277
+            uint8_t fu_header = *buf;
278 278
             uint8_t start_bit = fu_header >> 7;
279 279
 //            uint8_t end_bit = (fu_header & 0x40) >> 6;
280 280
             uint8_t nal_type = (fu_header & 0x1f);
... ...
@@ -302,6 +291,9 @@ static int h264_handle_packet(AVFormatContext *ctx,
302 302
                 av_new_packet(pkt, len);
303 303
                 memcpy(pkt->data, buf, len);
304 304
             }
305
+        } else {
306
+            av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
307
+            result = AVERROR_INVALIDDATA;
305 308
         }
306 309
         break;
307 310
 
... ...
@@ -309,7 +301,7 @@ static int h264_handle_packet(AVFormatContext *ctx,
309 309
     case 31:                   // undefined
310 310
     default:
311 311
         av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)", type);
312
-        result= -1;
312
+        result = AVERROR_INVALIDDATA;
313 313
         break;
314 314
     }
315 315
 
... ...
@@ -318,18 +310,9 @@ static int h264_handle_packet(AVFormatContext *ctx,
318 318
     return result;
319 319
 }
320 320
 
321
-/* ---------------- public code */
322 321
 static PayloadContext *h264_new_context(void)
323 322
 {
324
-    PayloadContext *data =
325
-        av_mallocz(sizeof(PayloadContext) +
326
-                   FF_INPUT_BUFFER_PADDING_SIZE);
327
-
328
-    if (data) {
329
-        data->cookie = MAGIC_COOKIE;
330
-    }
331
-
332
-    return data;
323
+    return av_mallocz(sizeof(PayloadContext) + FF_INPUT_BUFFER_PADDING_SIZE);
333 324
 }
334 325
 
335 326
 static void h264_free_context(PayloadContext *data)
... ...
@@ -344,13 +327,6 @@ static void h264_free_context(PayloadContext *data)
344 344
     }
345 345
 #endif
346 346
 
347
-    assert(data);
348
-    assert(data->cookie == MAGIC_COOKIE);
349
-
350
-    // avoid stale pointers (assert)
351
-    data->cookie = DEAD_COOKIE;
352
-
353
-    // and clear out this...
354 347
     av_free(data);
355 348
 }
356 349
 
... ...
@@ -366,7 +342,6 @@ static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
366 366
 
367 367
     stream = s->streams[st_index];
368 368
     codec = stream->codec;
369
-    assert(h264_data->cookie == MAGIC_COOKIE);
370 369
 
371 370
     if (av_strstart(p, "framesize:", &p)) {
372 371
         char buf1[50];
... ...
@@ -392,12 +367,9 @@ static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
392 392
         // could use this if we wanted.
393 393
     }
394 394
 
395
-    return 0;                   // keep processing it the normal way...
395
+    return 0;
396 396
 }
397 397
 
398
-/**
399
-This is the structure for expanding on the dynamic rtp protocols (makes everything static. yay!)
400
-*/
401 398
 RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
402 399
     .enc_name         = "H264",
403 400
     .codec_type       = AVMEDIA_TYPE_VIDEO,
... ...
@@ -17,6 +17,9 @@ ffservertest: ffserver$(EXESUF) tests/vsynth1/00.pgm tests/data/asynth1.sw
17 17
 
18 18
 OBJDIRS += tests/data tests/vsynth1 tests/vsynth2
19 19
 
20
+# Required due to missing automatic dependency tracking for HOSTOBJS.
21
+tests/rotozoom.o tests/videogen.o: tests/utils.c
22
+
20 23
 tests/vsynth1/00.pgm: tests/videogen$(HOSTEXESUF) | tests/vsynth1
21 24
 	$(M)./$< 'tests/vsynth1/'
22 25
 
... ...
@@ -24,6 +24,8 @@
24 24
 #include <stdio.h>
25 25
 #include <inttypes.h>
26 26
 
27
+#include "utils.c"
28
+
27 29
 #define FIXP (1 << 16)
28 30
 #define MY_PI 205887 // (M_PI * FIX)
29 31
 
... ...
@@ -53,136 +55,12 @@ static int64_t int_sin(int64_t a)
53 53
     return a - int_pow(a, 3) / 6 + int_pow(a, 5) / 120 - int_pow(a, 7) / 5040;
54 54
 }
55 55
 
56
-#define SCALEBITS 8
57
-#define ONE_HALF  (1 << (SCALEBITS - 1))
58
-#define FIX(x)    ((int) ((x) * (1L << SCALEBITS) + 0.5))
59
-
60
-static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
61
-                             unsigned char *cr, const unsigned char *src,
62
-                             int width, int height)
63
-{
64
-    int wrap, wrap3, x, y;
65
-    int r, g, b, r1, g1, b1;
66
-    const unsigned char *p;
67
-
68
-    wrap  = width;
69
-    wrap3 = width * 3;
70
-    p     = src;
71
-    for (y = 0; y < height; y += 2) {
72
-        for (x = 0; x < width; x += 2) {
73
-            r       = p[0];
74
-            g       = p[1];
75
-            b       = p[2];
76
-            r1      = r;
77
-            g1      = g;
78
-            b1      = b;
79
-            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
80
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
81
-            r       = p[3];
82
-            g       = p[4];
83
-            b       = p[5];
84
-            r1     += r;
85
-            g1     += g;
86
-            b1     += b;
87
-            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
88
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
89
-            p      += wrap3;
90
-            lum    += wrap;
91
-
92
-            r       = p[0];
93
-            g       = p[1];
94
-            b       = p[2];
95
-            r1     += r;
96
-            g1     += g;
97
-            b1     += b;
98
-            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
99
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
100
-            r       = p[3];
101
-            g       = p[4];
102
-            b       = p[5];
103
-            r1     += r;
104
-            g1     += g;
105
-            b1     += b;
106
-            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
107
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
108
-
109
-            cb[0]   = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
110
-                       FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
111
-            cr[0]   = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
112
-                       FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
113
-
114
-            cb++;
115
-            cr++;
116
-            p   += -wrap3 + 2 * 3;
117
-            lum += -wrap  + 2;
118
-        }
119
-        p   += wrap3;
120
-        lum += wrap;
121
-    }
122
-}
123
-
124
-/* cif format */
125
-#define DEFAULT_WIDTH   352
126
-#define DEFAULT_HEIGHT  288
127
-#define DEFAULT_NB_PICT  50
128
-
129
-static void pgmyuv_save(const char *filename, int w, int h,
130
-                        unsigned char *rgb_tab)
131
-{
132
-    FILE *f;
133
-    int i, h2, w2;
134
-    unsigned char *cb, *cr;
135
-    unsigned char *lum_tab, *cb_tab, *cr_tab;
136
-
137
-    lum_tab = malloc(w * h);
138
-    cb_tab  = malloc(w * h / 4);
139
-    cr_tab  = malloc(w * h / 4);
140
-
141
-    rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
142
-
143
-    f = fopen(filename, "wb");
144
-    fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
145
-    fwrite(lum_tab, 1, w * h, f);
146
-    h2 = h / 2;
147
-    w2 = w / 2;
148
-    cb = cb_tab;
149
-    cr = cr_tab;
150
-    for (i = 0; i < h2; i++) {
151
-        fwrite(cb, 1, w2, f);
152
-        fwrite(cr, 1, w2, f);
153
-        cb += w2;
154
-        cr += w2;
155
-    }
156
-    fclose(f);
157
-
158
-    free(lum_tab);
159
-    free(cb_tab);
160
-    free(cr_tab);
161
-}
162
-
163
-unsigned char *rgb_tab;
164
-int width, height, wrap;
165
-
166
-static void put_pixel(int x, int y, int r, int g, int b)
167
-{
168
-    unsigned char *p;
169
-
170
-    if (x < 0 || x >= width ||
171
-        y < 0 || y >= height)
172
-        return;
173
-
174
-    p    = rgb_tab + y * wrap + x * 3;
175
-    p[0] = r;
176
-    p[1] = g;
177
-    p[2] = b;
178
-}
179
-
180
-unsigned char tab_r[256 * 256];
181
-unsigned char tab_g[256 * 256];
182
-unsigned char tab_b[256 * 256];
56
+static unsigned char tab_r[256 * 256];
57
+static unsigned char tab_g[256 * 256];
58
+static unsigned char tab_b[256 * 256];
183 59
 
184
-int h_cos[360];
185
-int h_sin[360];
60
+static int h_cos[360];
61
+static int h_sin[360];
186 62
 
187 63
 static int ipol(uint8_t *src, int x, int y)
188 64
 {
189 65
new file mode 100644
... ...
@@ -0,0 +1,146 @@
0
+/*
1
+ * copyright (c) Sebastien Bechet <s.bechet@av7.net>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <stdio.h>
21
+#include <stdlib.h>
22
+
23
+#define SCALEBITS 8
24
+#define ONE_HALF  (1 << (SCALEBITS - 1))
25
+#define FIX(x)    ((int) ((x) * (1L << SCALEBITS) + 0.5))
26
+
27
+static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
28
+                             unsigned char *cr, unsigned char *src,
29
+                             int width, int height)
30
+{
31
+    int wrap, wrap3, x, y;
32
+    int r, g, b, r1, g1, b1;
33
+    unsigned char *p;
34
+
35
+    wrap  = width;
36
+    wrap3 = width * 3;
37
+    p     = src;
38
+    for (y = 0; y < height; y += 2) {
39
+        for (x = 0; x < width; x += 2) {
40
+            r       = p[0];
41
+            g       = p[1];
42
+            b       = p[2];
43
+            r1      = r;
44
+            g1      = g;
45
+            b1      = b;
46
+            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
47
+                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
48
+            r       = p[3];
49
+            g       = p[4];
50
+            b       = p[5];
51
+            r1     += r;
52
+            g1     += g;
53
+            b1     += b;
54
+            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
55
+                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
56
+            p      += wrap3;
57
+            lum    += wrap;
58
+
59
+            r       = p[0];
60
+            g       = p[1];
61
+            b       = p[2];
62
+            r1     += r;
63
+            g1     += g;
64
+            b1     += b;
65
+            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
66
+                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
67
+            r       = p[3];
68
+            g       = p[4];
69
+            b       = p[5];
70
+            r1     += r;
71
+            g1     += g;
72
+            b1     += b;
73
+            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
74
+                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
75
+
76
+            cb[0]   = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
77
+                       FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
78
+            cr[0]   = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
79
+                       FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
80
+
81
+            cb++;
82
+            cr++;
83
+            p   += -wrap3 + 2 * 3;
84
+            lum += -wrap  + 2;
85
+        }
86
+        p   += wrap3;
87
+        lum += wrap;
88
+    }
89
+}
90
+
91
+/* cif format */
92
+#define DEFAULT_WIDTH   352
93
+#define DEFAULT_HEIGHT  288
94
+#define DEFAULT_NB_PICT  50
95
+
96
+static void pgmyuv_save(const char *filename, int w, int h,
97
+                        unsigned char *rgb_tab)
98
+{
99
+    FILE *f;
100
+    int i, h2, w2;
101
+    unsigned char *cb, *cr;
102
+    unsigned char *lum_tab, *cb_tab, *cr_tab;
103
+
104
+    lum_tab = malloc(w * h);
105
+    cb_tab  = malloc(w * h / 4);
106
+    cr_tab  = malloc(w * h / 4);
107
+
108
+    rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
109
+
110
+    f = fopen(filename, "wb");
111
+    fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
112
+    fwrite(lum_tab, 1, w * h, f);
113
+    h2 = h / 2;
114
+    w2 = w / 2;
115
+    cb = cb_tab;
116
+    cr = cr_tab;
117
+    for (i = 0; i < h2; i++) {
118
+        fwrite(cb, 1, w2, f);
119
+        fwrite(cr, 1, w2, f);
120
+        cb += w2;
121
+        cr += w2;
122
+    }
123
+    fclose(f);
124
+
125
+    free(lum_tab);
126
+    free(cb_tab);
127
+    free(cr_tab);
128
+}
129
+
130
+static unsigned char *rgb_tab;
131
+static int width, height, wrap;
132
+
133
+static void put_pixel(int x, int y, int r, int g, int b)
134
+{
135
+    unsigned char *p;
136
+
137
+    if (x < 0 || x >= width ||
138
+        y < 0 || y >= height)
139
+        return;
140
+
141
+    p    = rgb_tab + y * wrap + x * 3;
142
+    p[0] = r;
143
+    p[1] = g;
144
+    p[2] = b;
145
+}
... ...
@@ -25,134 +25,7 @@
25 25
 #include <stdint.h>
26 26
 #include <stdio.h>
27 27
 
28
-#define SCALEBITS 8
29
-#define ONE_HALF  (1 << (SCALEBITS - 1))
30
-#define FIX(x)    ((int) ((x) * (1L << SCALEBITS) + 0.5))
31
-
32
-static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
33
-                             uint8_t *src, int width, int height)
34
-{
35
-    int wrap, wrap3, x, y;
36
-    int r, g, b, r1, g1, b1;
37
-    uint8_t *p;
38
-
39
-    wrap  = width;
40
-    wrap3 = width * 3;
41
-    p     = src;
42
-    for (y = 0; y < height; y += 2) {
43
-        for (x = 0; x < width; x += 2) {
44
-            r       = p[0];
45
-            g       = p[1];
46
-            b       = p[2];
47
-            r1      = r;
48
-            g1      = g;
49
-            b1      = b;
50
-            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
51
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
52
-            r       = p[3];
53
-            g       = p[4];
54
-            b       = p[5];
55
-            r1     += r;
56
-            g1     += g;
57
-            b1     += b;
58
-            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
59
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
60
-            p      += wrap3;
61
-            lum    += wrap;
62
-
63
-            r       = p[0];
64
-            g       = p[1];
65
-            b       = p[2];
66
-            r1     += r;
67
-            g1     += g;
68
-            b1     += b;
69
-            lum[0]  = (FIX(0.29900) * r + FIX(0.58700) * g +
70
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
71
-            r       = p[3];
72
-            g       = p[4];
73
-            b       = p[5];
74
-            r1     += r;
75
-            g1     += g;
76
-            b1     += b;
77
-            lum[1]  = (FIX(0.29900) * r + FIX(0.58700) * g +
78
-                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
79
-
80
-            cb[0]   = 128 + ((- FIX(0.16874) * r1 -
81
-                                FIX(0.33126) * g1 +
82
-                                FIX(0.50000) * b1 +
83
-                              4 * ONE_HALF - 1)
84
-                             >> (SCALEBITS + 2));
85
-            cr[0]   = 128 + ((FIX(0.50000) * r1 -
86
-                              FIX(0.41869) * g1 -
87
-                              FIX(0.08131) * b1 +
88
-                              4 * ONE_HALF - 1)
89
-                             >> (SCALEBITS + 2));
90
-
91
-            cb++;
92
-            cr++;
93
-            p   += -wrap3 + 2 * 3;
94
-            lum += -wrap + 2;
95
-        }
96
-        p   += wrap3;
97
-        lum += wrap;
98
-    }
99
-}
100
-
101
-/* cif format */
102
-#define DEFAULT_WIDTH   352
103
-#define DEFAULT_HEIGHT  288
104
-#define DEFAULT_NB_PICT 50 /* 2 seconds */
105
-
106
-static void pgmyuv_save(const char *filename, int w, int h,
107
-                        unsigned char *rgb_tab)
108
-{
109
-    FILE *f;
110
-    int i, h2, w2;
111
-    unsigned char *cb, *cr;
112
-    unsigned char *lum_tab, *cb_tab, *cr_tab;
113
-
114
-    lum_tab = malloc(w * h);
115
-    cb_tab  = malloc((w * h) / 4);
116
-    cr_tab  = malloc((w * h) / 4);
117
-
118
-    rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
119
-
120
-    f = fopen(filename, "wb");
121
-    fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
122
-    fwrite(lum_tab, 1, w * h, f);
123
-    h2 = h / 2;
124
-    w2 = w / 2;
125
-    cb = cb_tab;
126
-    cr = cr_tab;
127
-    for (i = 0; i < h2; i++) {
128
-        fwrite(cb, 1, w2, f);
129
-        fwrite(cr, 1, w2, f);
130
-        cb += w2;
131
-        cr += w2;
132
-    }
133
-    fclose(f);
134
-
135
-    free(lum_tab);
136
-    free(cb_tab);
137
-    free(cr_tab);
138
-}
139
-
140
-unsigned char *rgb_tab;
141
-int width, height, wrap;
142
-
143
-static void put_pixel(int x, int y, int r, int g, int b)
144
-{
145
-    unsigned char *p;
146
-
147
-    if (x < 0 || x >= width ||
148
-        y < 0 || y >= height)
149
-        return;
150
-
151
-    p    = rgb_tab + y * wrap + x * 3;
152
-    p[0] = r;
153
-    p[1] = g;
154
-    p[2] = b;
155
-}
28
+#include "utils.c"
156 29
 
157 30
 static unsigned int myrnd(unsigned int *seed_ptr, int n)
158 31
 {
... ...
@@ -200,9 +73,9 @@ typedef struct VObj {
200 200
     int r, g, b;
201 201
 } VObj;
202 202
 
203
-VObj objs[NB_OBJS];
203
+static VObj objs[NB_OBJS];
204 204
 
205
-unsigned int seed = 1;
205
+static unsigned int seed = 1;
206 206
 
207 207
 static void gen_image(int num, int w, int h)
208 208
 {