6053
|
1 /*
|
|
2 * vo_gif89a.c Generate gif89a output in file out.gif
|
|
3 *
|
|
4 * Originally based on vo_png.c
|
|
5 *
|
|
6 * Stolen (C) 2002 by GifWhore <joey@yunamusic.com>
|
|
7 *
|
|
8 */
|
|
9
|
|
10 #include <stdio.h>
|
|
11 #include <stdlib.h>
|
|
12 #include <string.h>
|
|
13 #include <errno.h>
|
|
14
|
|
15 #include "config.h"
|
|
16 #include "video_out.h"
|
|
17 #include "video_out_internal.h"
|
|
18
|
|
19 #include "../postproc/rgb2rgb.h"
|
|
20
|
|
21 #include <gif_lib.h>
|
|
22
|
|
23 #define GIFWHORE_version 0.90
|
|
24
|
|
25 LIBVO_EXTERN (gif89a)
|
|
26
|
|
27 static vo_info_t vo_info =
|
|
28 {
|
|
29 "GIF89a (out.gif)",
|
|
30 "gif89a",
|
|
31 "GifWhore <joey@yunamusic.com>",
|
|
32 ""
|
|
33 };
|
|
34
|
|
35 extern int verbose;
|
|
36 extern int vo_config_count;
|
|
37
|
|
38 static int image_width;
|
|
39 static int image_height;
|
|
40 static int image_format;
|
|
41 static uint8_t *image_data=NULL;
|
|
42 static unsigned int scale_srcW = 0, scale_srcH = 0;
|
|
43
|
|
44 static int reverse_map = 0;
|
|
45 static unsigned char framenum = 0;
|
|
46 static int gif_frameskip;
|
|
47 static int gif_framedelay;
|
|
48 static int target_fps = 0;
|
|
49
|
|
50 GifFileType *newgif=NULL;
|
|
51
|
|
52 /*
|
|
53 * TODO
|
|
54 * OSD!!!
|
|
55 */
|
|
56
|
|
57 static uint32_t config
|
|
58 (uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
|
|
59 uint32_t fullscreen, char *title, uint32_t format, const vo_tune_info_t *info) {
|
|
60 char filename[] = "out.gif";
|
|
61 ColorMapObject *Cmap;
|
|
62 char LB[] = {
|
|
63 'N','E','T','S',
|
|
64 'C','A','P','E',
|
|
65 '2','.','0' };
|
|
66 char LB2[] = { 1, 0x00, 0x00 };
|
|
67
|
|
68 if (target_fps == 0) target_fps = 5;
|
|
69 gif_frameskip = (vo_fps + 0.25) / target_fps;
|
|
70 gif_framedelay = 100 / target_fps;
|
|
71
|
|
72 if ((width != d_width) || (height != d_height)) {
|
|
73 image_width = (d_width + 7) & ~7;
|
|
74 image_height = d_height;
|
|
75 scale_srcW = width;
|
|
76 scale_srcH = height;
|
|
77 SwScale_Init();
|
|
78 } else {
|
|
79 image_width = width;
|
|
80 image_height = height;
|
|
81 }
|
|
82 image_format = format;
|
|
83
|
|
84 Cmap = MakeMapObject(256, NULL);
|
|
85
|
|
86 switch(format) {
|
|
87 case IMGFMT_BGR32:
|
|
88 case IMGFMT_BGR24:
|
|
89 reverse_map = 1;
|
|
90 break;
|
|
91 case IMGFMT_RGB32:
|
|
92 case IMGFMT_RGB24:
|
|
93 break;
|
|
94 case IMGFMT_IYUV:
|
|
95 case IMGFMT_I420:
|
|
96 case IMGFMT_YV12:
|
|
97 reverse_map = 1;
|
|
98 yuv2rgb_init(24, MODE_RGB);
|
|
99 image_data = malloc(image_width*image_height*3);
|
|
100 break;
|
|
101 default:
|
|
102 return 1;
|
|
103 }
|
|
104
|
|
105 if (vo_config_count > 0)
|
|
106 return 0;
|
|
107
|
|
108 EGifSetGifVersion("89a");
|
|
109 newgif = EGifOpenFileName(filename, 0);
|
|
110 if (newgif == NULL)
|
|
111 {
|
|
112 fprintf(stderr, "error opening file for output.\n");
|
|
113 return(1);
|
|
114 }
|
|
115 EGifPutScreenDesc(newgif, image_width, image_height, 256, 0, Cmap);
|
|
116 EGifPutExtensionFirst(newgif, 0xFF, 11, LB);
|
|
117 EGifPutExtensionLast(newgif, 0, 3, LB2);
|
|
118
|
|
119 return 0;
|
|
120 }
|
|
121
|
|
122 static const vo_info_t* get_info(void)
|
|
123 {
|
|
124 return &vo_info;
|
|
125 }
|
|
126
|
|
127 static uint32_t draw_frame(uint8_t * src[])
|
|
128 {
|
|
129 uint8_t *use_data;
|
|
130 ColorMapObject *Cmap;
|
|
131 uint8_t Colors[256 * 3];
|
|
132 int z;
|
|
133 char CB[] = { (char)(gif_framedelay >> 8), (char)(gif_framedelay & 0xff), 0, 0};
|
|
134
|
|
135 if ((framenum++ % gif_frameskip)) return(0);
|
|
136
|
|
137 if ((image_format == IMGFMT_BGR32) || (image_format == IMGFMT_RGB32))
|
|
138 {
|
|
139 rgb32to24(src[0], image_data, image_width * image_height * 4);
|
|
140 src[0] = image_data;
|
|
141 }
|
|
142
|
|
143 Cmap = MakeMapObject(256, NULL);
|
|
144 use_data = (uint8_t *)malloc(image_width * image_height);
|
|
145 if (gif_reduce(image_width, image_height, src[0], use_data, Colors)) return(0);
|
|
146
|
|
147 if (reverse_map)
|
|
148 {
|
|
149 for (z = 0; z < 256; z++) {
|
|
150 Cmap->Colors[z].Blue = Colors[(z * 3) + 0];
|
|
151 Cmap->Colors[z].Green = Colors[(z * 3) + 1];
|
|
152 Cmap->Colors[z].Red = Colors[(z * 3) + 2];
|
|
153 }
|
|
154 }
|
|
155 else
|
|
156 {
|
|
157 for (z = 0; z < 256; z++) {
|
|
158 Cmap->Colors[z].Red = Colors[(z * 3) + 0];
|
|
159 Cmap->Colors[z].Green = Colors[(z * 3) + 1];
|
|
160 Cmap->Colors[z].Blue = Colors[(z * 3) + 2];
|
|
161 }
|
|
162 }
|
|
163
|
|
164 EGifPutExtension(newgif, 0xF9, 0x04, CB);
|
|
165 EGifPutImageDesc(newgif, 0, 0, image_width, image_height, 0, Cmap);
|
|
166 EGifPutLine(newgif, use_data, image_width * image_height);
|
|
167 FreeMapObject(Cmap);
|
|
168 free(use_data);
|
|
169
|
|
170 return (0);
|
|
171 }
|
|
172
|
|
173 static void draw_osd(void)
|
|
174 {
|
|
175 }
|
|
176
|
|
177 static void flip_page (void)
|
|
178 {
|
|
179 uint8_t *use_data;
|
|
180 ColorMapObject *Cmap;
|
|
181 uint8_t Colors[256 * 3];
|
|
182 int z;
|
|
183 char CB[] = { (char)(gif_framedelay >> 8), (char)(gif_framedelay & 0xff), 0, 0};
|
|
184
|
|
185 if ((image_format == IMGFMT_YV12) || (image_format == IMGFMT_IYUV) || (image_format == IMGFMT_I420)) {
|
|
186
|
|
187 if ((framenum++ % gif_frameskip)) return;
|
|
188
|
|
189 Cmap = MakeMapObject(256, NULL);
|
|
190 use_data = (uint8_t *)malloc(image_width * image_height);
|
|
191 if (gif_reduce(image_width, image_height, image_data, use_data, Colors)) return;
|
|
192
|
|
193 if (reverse_map)
|
|
194 {
|
|
195 for (z = 0; z < 256; z++) {
|
|
196 Cmap->Colors[z].Blue = Colors[(z * 3) + 0];
|
|
197 Cmap->Colors[z].Green = Colors[(z * 3) + 1];
|
|
198 Cmap->Colors[z].Red = Colors[(z * 3) + 2];
|
|
199 }
|
|
200 }
|
|
201 else
|
|
202 {
|
|
203 for (z = 0; z < 256; z++) {
|
|
204 Cmap->Colors[z].Red = Colors[(z * 3) + 0];
|
|
205 Cmap->Colors[z].Green = Colors[(z * 3) + 1];
|
|
206 Cmap->Colors[z].Blue = Colors[(z * 3) + 2];
|
|
207 }
|
|
208 }
|
|
209
|
|
210 EGifPutExtension(newgif, 0xF9, 0x04, CB);
|
|
211 EGifPutImageDesc(newgif, 0, 0, image_width, image_height, 0, Cmap);
|
|
212 EGifPutLine(newgif, use_data, image_width * image_height);
|
|
213 FreeMapObject(Cmap);
|
|
214 free(use_data);
|
|
215 }
|
|
216 }
|
|
217
|
|
218 static uint32_t draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
|
|
219 {
|
|
220 /* hack: swap planes for I420 ;) -- alex */
|
|
221 if ((image_format == IMGFMT_IYUV) || (image_format == IMGFMT_I420))
|
|
222 {
|
|
223 uint8_t *src_i420[3];
|
|
224
|
|
225 src_i420[0] = src[0];
|
|
226 src_i420[1] = src[2];
|
|
227 src_i420[2] = src[1];
|
|
228 src = src_i420;
|
|
229 }
|
|
230
|
|
231 if (scale_srcW) {
|
|
232 uint8_t *dst[3] = {image_data, NULL, NULL};
|
|
233 SwScale_YV12slice(src,stride,y,h,
|
|
234 dst, image_width*3, 24,
|
|
235 scale_srcW, scale_srcH, image_width, image_height);
|
|
236 } else {
|
|
237 uint8_t *dst = image_data + (image_width * y + x) * 3;
|
|
238 yuv2rgb(dst,src[0],src[1],src[2],w,h,image_width*3,stride[0],stride[1]);
|
|
239 }
|
|
240 return 0;
|
|
241 }
|
|
242
|
|
243 static uint32_t
|
|
244 query_format(uint32_t format)
|
|
245 {
|
|
246 switch(format){
|
|
247 case IMGFMT_IYUV:
|
|
248 case IMGFMT_I420:
|
|
249 case IMGFMT_YV12:
|
|
250 case IMGFMT_RGB|32:
|
|
251 case IMGFMT_BGR|32:
|
|
252 case IMGFMT_RGB|24:
|
|
253 case IMGFMT_BGR|24:
|
|
254 return 1 | VFCAP_SWSCALE | VFCAP_TIMER | VFCAP_ACCEPT_STRIDE;
|
|
255 }
|
|
256 return 0;
|
|
257 }
|
|
258
|
|
259 static void
|
|
260 uninit(void)
|
|
261 {
|
|
262 char temp[256];
|
|
263
|
|
264 if (image_data) { free(image_data); image_data=NULL; }
|
|
265
|
|
266 if (vo_config_count > 0) {
|
|
267 sprintf(temp, "gifwhore v%2.2f (c) %s\r\n",
|
|
268 GIFWHORE_version, "joey@yunamusic.com");
|
|
269 EGifPutComment(newgif, temp);
|
|
270 EGifCloseFile(newgif);
|
|
271 }
|
|
272 }
|
|
273
|
|
274
|
|
275 static void check_events(void)
|
|
276 {
|
|
277 }
|
|
278
|
|
279 int gif_reduce(int width, int height,
|
|
280 unsigned char *source,
|
|
281 unsigned char *destination,
|
|
282 unsigned char *palette)
|
|
283 {
|
|
284 GifColorType cmap[256];
|
|
285 unsigned char Ra[width * height];
|
|
286 unsigned char Ga[width * height];
|
|
287 unsigned char Ba[width * height];
|
|
288 unsigned char *R, *G, *B;
|
|
289 int Size = 256;
|
|
290 int i;
|
|
291
|
|
292 R = Ra; G = Ga; B = Ba;
|
|
293 for (i = 0; i < width * height; i++)
|
|
294 {
|
|
295 *R++ = *source++;
|
|
296 *G++ = *source++;
|
|
297 *B++ = *source++;
|
|
298 }
|
|
299
|
|
300 R = Ra; G = Ga; B = Ba;
|
|
301 if (QuantizeBuffer(width, height, &Size,
|
|
302 R, G, B,
|
|
303 destination, cmap) == GIF_ERROR)
|
|
304 {
|
|
305 fprintf(stderr, "vo_gif89a: Quantize failed!\n");
|
|
306 return(-1);
|
|
307 }
|
|
308
|
|
309 for (i = 0; i < Size; i++)
|
|
310 {
|
|
311 *palette++ = cmap[i].Red;
|
|
312 *palette++ = cmap[i].Green;
|
|
313 *palette++ = cmap[i].Blue;
|
|
314 }
|
|
315
|
|
316 return(0);
|
|
317 }
|
|
318
|
|
319 static uint32_t preinit(const char *arg)
|
|
320 {
|
|
321 int i = 0;
|
|
322 if (arg) i = atoi(arg);
|
|
323 if (i > vo_fps) i = vo_fps;
|
|
324 if (i < 1) i = 5;
|
|
325 target_fps = i;
|
|
326 return 0;
|
|
327 }
|
|
328
|
|
329 static uint32_t control(uint32_t request, void *data, ...)
|
|
330 {
|
|
331 switch (request) {
|
|
332 case VOCTRL_QUERY_FORMAT:
|
|
333 return query_format(*((uint32_t*)data));
|
|
334 }
|
|
335 return VO_NOTIMPL;
|
|
336 }
|
|
337
|