1 /*
2 * video_out_pgm.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * MD5 code derived from linux kernel GPL implementation
7 *
8 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
9 * See http://libmpeg2.sourceforge.net/ for updates.
10 *
11 * mpeg2dec is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * mpeg2dec is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <inttypes.h>
32
33 #include "video_out.h"
34 #include "vo_internal.h"
35
36 typedef struct pgm_instance_s {
37 vo_instance_t vo;
38 int framenum;
39 int width;
40 int height;
41 int chroma_width;
42 int chroma_height;
43 char header[1024];
44 void (* writer) (struct pgm_instance_s *, uint8_t *, size_t);
45 FILE * file;
46 uint32_t md5_hash[4];
47 uint32_t md5_block[16];
48 uint32_t md5_bytes;
49 } pgm_instance_t;
50
file_writer(pgm_instance_t * instance,uint8_t * ptr,size_t size)51 static void file_writer (pgm_instance_t * instance, uint8_t * ptr, size_t size)
52 {
53 fwrite (ptr, size, 1, instance->file);
54 }
55
internal_draw_frame(pgm_instance_t * instance,uint8_t * const * buf)56 static void internal_draw_frame (pgm_instance_t * instance,
57 uint8_t * const * buf)
58 {
59 static uint8_t black[16384] = { 0 };
60 int i;
61
62 instance->writer (instance, (uint8_t *)instance->header,
63 strlen (instance->header));
64 for (i = 0; i < instance->height; i++) {
65 instance->writer (instance, buf[0] + i * instance->width,
66 instance->width);
67 instance->writer (instance, black,
68 2 * instance->chroma_width - instance->width);
69 }
70 for (i = 0; i < instance->chroma_height; i++) {
71 instance->writer (instance, buf[1] + i * instance->chroma_width,
72 instance->chroma_width);
73 instance->writer (instance, buf[2] + i * instance->chroma_width,
74 instance->chroma_width);
75 }
76 }
77
pgm_draw_frame(vo_instance_t * _instance,uint8_t * const * buf,void * id)78 static void pgm_draw_frame (vo_instance_t * _instance,
79 uint8_t * const * buf, void * id)
80 {
81 pgm_instance_t * instance = (pgm_instance_t *) _instance;
82 char filename[128];
83
84 sprintf (filename, "%d.pgm", instance->framenum++);
85 instance->file = fopen (filename, "wb");
86 if (instance->file == NULL)
87 return;
88 internal_draw_frame (instance, buf);
89 fclose (instance->file);
90 }
91
pgm_setup(vo_instance_t * _instance,unsigned int width,unsigned int height,unsigned int chroma_width,unsigned int chroma_height,vo_setup_result_t * result)92 static int pgm_setup (vo_instance_t * _instance, unsigned int width,
93 unsigned int height, unsigned int chroma_width,
94 unsigned int chroma_height, vo_setup_result_t * result)
95 {
96 pgm_instance_t * instance;
97
98 instance = (pgm_instance_t *) _instance;
99
100 /*
101 * Layout of the Y, U, and V buffers in our pgm image
102 *
103 * YY YY YY
104 * 420: YY 422: YY 444: YY
105 * UV UV UUVV
106 * UV UUVV
107 */
108 if (width > 2 * chroma_width)
109 return 1;
110
111 instance->width = width;
112 instance->height = height;
113 instance->chroma_width = chroma_width;
114 instance->chroma_height = chroma_height;
115 sprintf (instance->header, "P5\n%d %d\n255\n", 2 * chroma_width,
116 height + chroma_height);
117 result->convert = NULL;
118 return 0;
119 }
120
internal_open(void draw (vo_instance_t *,uint8_t * const *,void *),void writer (pgm_instance_t *,uint8_t *,size_t))121 static vo_instance_t * internal_open (void draw (vo_instance_t *,
122 uint8_t * const *, void *),
123 void writer (pgm_instance_t *,
124 uint8_t *, size_t))
125 {
126 pgm_instance_t * instance;
127
128 instance = (pgm_instance_t *) malloc (sizeof (pgm_instance_t));
129 if (instance == NULL)
130 return NULL;
131
132 instance->vo.setup = pgm_setup;
133 instance->vo.setup_fbuf = NULL;
134 instance->vo.set_fbuf = NULL;
135 instance->vo.start_fbuf = NULL;
136 instance->vo.draw = draw;
137 instance->vo.discard = NULL;
138 instance->vo.close = (void (*) (vo_instance_t *)) free;
139 instance->framenum = 0;
140 instance->writer = writer;
141 instance->file = stdout;
142
143 return (vo_instance_t *) instance;
144 }
145
vo_pgm_open(void)146 vo_instance_t * vo_pgm_open (void)
147 {
148 return internal_open (pgm_draw_frame, file_writer);
149 }
150
pgmpipe_draw_frame(vo_instance_t * _instance,uint8_t * const * buf,void * id)151 static void pgmpipe_draw_frame (vo_instance_t * _instance,
152 uint8_t * const * buf, void * id)
153 {
154 pgm_instance_t * instance = (pgm_instance_t *) _instance;
155 internal_draw_frame (instance, buf);
156 }
157
vo_pgmpipe_open(void)158 vo_instance_t * vo_pgmpipe_open (void)
159 {
160 return internal_open (pgmpipe_draw_frame, file_writer);
161 }
162
163 #define F1(x,y,z) (z ^ (x & (y ^ z)))
164 #define F2(x,y,z) F1 (z, x, y)
165 #define F3(x,y,z) (x ^ y ^ z)
166 #define F4(x,y,z) (y ^ (x | ~z))
167
168 #define MD5STEP(f,w,x,y,z,in,s) do { \
169 w += f (x, y, z) + in; w = ((w << s) | (w >> (32 - s))) + x; \
170 } while (0)
171
md5_transform(uint32_t * hash,uint32_t const * in)172 static void md5_transform (uint32_t * hash, uint32_t const * in)
173 {
174 uint32_t a, b, c, d;
175
176 a = hash[0];
177 b = hash[1];
178 c = hash[2];
179 d = hash[3];
180
181 MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7);
182 MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
183 MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17);
184 MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
185 MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
186 MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12);
187 MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17);
188 MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22);
189 MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7);
190 MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
191 MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
192 MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22);
193 MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7);
194 MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12);
195 MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17);
196 MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22);
197
198 MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5);
199 MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9);
200 MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14);
201 MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
202 MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5);
203 MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9);
204 MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
205 MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
206 MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
207 MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9);
208 MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
209 MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20);
210 MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
211 MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
212 MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14);
213 MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
214
215 MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4);
216 MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11);
217 MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
218 MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23);
219 MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4);
220 MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
221 MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
222 MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
223 MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
224 MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
225 MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
226 MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23);
227 MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
228 MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
229 MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
230 MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
231
232 MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6);
233 MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10);
234 MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15);
235 MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21);
236 MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6);
237 MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
238 MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15);
239 MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21);
240 MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
241 MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
242 MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15);
243 MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
244 MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6);
245 MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10);
246 MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
247 MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21);
248
249 hash[0] += a;
250 hash[1] += b;
251 hash[2] += c;
252 hash[3] += d;
253 }
254
swap(uint32_t x)255 static inline uint32_t swap (uint32_t x)
256 {
257 return (((x & 0xff) << 24) | ((x & 0xff00) << 8) |
258 ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24));
259 }
260
little_endian(uint32_t * buf,unsigned int words)261 static inline void little_endian (uint32_t * buf, unsigned int words)
262 {
263 #ifdef WORDS_BIGENDIAN
264 while (words--)
265 buf[words] = swap (buf[words]);
266 #endif
267 }
268
md5_writer(pgm_instance_t * instance,uint8_t * ptr,size_t size)269 static void md5_writer (pgm_instance_t * instance, uint8_t * ptr, size_t size)
270 {
271 const unsigned int offset = instance->md5_bytes & 0x3f;
272
273 instance->md5_bytes += size;
274
275 if (offset + size < 64) {
276 memcpy ((char *)instance->md5_block + offset, ptr, size);
277 return;
278 } else if (offset) {
279 const int avail = 64 - offset;
280 memcpy ((char *)instance->md5_block + offset, ptr, avail);
281 little_endian (instance->md5_block, 16);
282 md5_transform (instance->md5_hash, instance->md5_block);
283 ptr += avail;
284 size -= avail;
285 }
286
287 while (size >= 64) {
288 #ifndef ARCH_X86
289 memcpy (instance->md5_block, ptr, 64);
290 little_endian (instance->md5_block, 16);
291 md5_transform (instance->md5_hash, instance->md5_block);
292 #else
293 md5_transform (instance->md5_hash, (uint32_t *)ptr);
294 #endif
295 ptr += 64;
296 size -= 64;
297 }
298
299 memcpy (instance->md5_block, ptr, size);
300 }
301
md5_draw_frame(vo_instance_t * _instance,uint8_t * const * buf,void * id)302 static void md5_draw_frame (vo_instance_t * _instance,
303 uint8_t * const * buf, void * id)
304 {
305 pgm_instance_t * instance = (pgm_instance_t *) _instance;
306 unsigned int offset;
307 uint8_t * p;
308 int padding;
309
310 instance->md5_hash[0] = 0x67452301;
311 instance->md5_hash[1] = 0xefcdab89;
312 instance->md5_hash[2] = 0x98badcfe;
313 instance->md5_hash[3] = 0x10325476;
314 instance->md5_bytes = 0;
315
316 internal_draw_frame (instance, buf);
317
318 offset = instance->md5_bytes & 0x3f;
319 p = (uint8_t *)instance->md5_block + offset;
320 padding = 55 - offset;
321
322 *p++ = 0x80;
323 if (padding < 0) {
324 memset (p, 0, padding + 8);
325 little_endian (instance->md5_block, 16);
326 md5_transform (instance->md5_hash, instance->md5_block);
327 p = (uint8_t *)instance->md5_block;
328 padding = 56;
329 }
330
331 memset (p, 0, padding);
332 instance->md5_block[14] = instance->md5_bytes << 3;
333 instance->md5_block[15] = instance->md5_bytes >> 29;
334 little_endian (instance->md5_block, 14);
335 md5_transform (instance->md5_hash, instance->md5_block);
336
337 printf ("%08x%08x%08x%08x *%d.pgm\n", swap (instance->md5_hash[0]),
338 swap (instance->md5_hash[1]) , swap (instance->md5_hash[2]),
339 swap (instance->md5_hash[3]), instance->framenum++);
340 }
341
vo_md5_open(void)342 vo_instance_t * vo_md5_open (void)
343 {
344 return internal_open (md5_draw_frame, md5_writer);
345 }
346