summaryrefslogtreecommitdiff
path: root/legacy/ethumb/src/plugins/emotion/emotion.c
blob: 8695b67eedeb8cfc6f5c541266222a2ea5a684df (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "Ethumb.h"
#include "Ethumb_Plugin.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <Eina.h>
#include <Eet.h>
#include <Ecore_File.h>
#include <Evas.h>
#include <Edje.h>
#include <Edje_Edit.h>
#include <Emotion.h>

struct _emotion_plugin
{
   unsigned int fps;
   double ptotal, len, pi;
   double total_time, tmp_time;
   unsigned int pcount;
   unsigned int frnum;
   Eina_Bool first;
   Eet_File *ef;
   Evas_Object *video;
   Ethumb *e;
   int w, h;
};

static void
_resize_movie(struct _emotion_plugin *_plugin)
{
   Ethumb *e = _plugin->e;
   double ratio;
   int w, h;
   int fx, fy, fw, fh;

   ratio = emotion_object_ratio_get(_plugin->video);
   ethumb_calculate_aspect_from_ratio(e, ratio, &w, &h);
   ethumb_calculate_fill_from_ratio(e, ratio, &fx, &fy, &fw, &fh);

   _plugin->w = w;
   _plugin->h = h;

   ethumb_plugin_image_resize(e, _plugin->w, _plugin->h);

   evas_object_resize(_plugin->video, fw, fh);
   evas_object_move(_plugin->video, fx, fy);
   emotion_object_audio_mute_set(_plugin->video, 1);
}

static void
_frame_resized_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
{
   _resize_movie(data);
}

static void
_video_stopped_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
{
   struct _emotion_plugin *_plugin = data;

   _plugin->pi = 0;
   _plugin->ptotal = 0;
   _plugin->first = EINA_FALSE;
   _plugin->total_time = _plugin->tmp_time;
}

static void
_video_pos_set(struct _emotion_plugin *_plugin)
{
   double pos;
   double interval;

   pos = ethumb_video_start_get(_plugin->e);
   interval = ethumb_video_interval_get(_plugin->e);
   _plugin->len = emotion_object_play_length_get(_plugin->video);

   if (_plugin->len > 0)
     _plugin->first = EINA_TRUE;

   if (pos <=0 || pos >= 1)
     _plugin->pi = 0.1 * _plugin->len + _plugin->pcount *
	_plugin->len * interval;
   else
     _plugin->pi = pos * _plugin->len + _plugin->pcount *
	_plugin->len * interval;

   emotion_object_position_set(_plugin->video, _plugin->pi);
}

static int
_setup_thumbnail(struct _emotion_plugin *_plugin)
{
   char buf[4096];
   Evas *evas;
   Evas_Object *edje;
   unsigned int i;
   const char *thumb_path;

   ethumb_thumb_path_get(_plugin->e, &thumb_path, NULL);
   evas = ethumb_evas_get(_plugin->e);

   if (!edje_file_group_exists(thumb_path, "movie/thumb"))
     {
	fprintf(stderr, "ERROR: no group 'movie/thumb' found.\n");
	goto exit_error;
     }

   edje = edje_object_add(evas);
   edje_object_file_set(edje, thumb_path, "movie/thumb");
   if (!edje_object_part_exists(edje, "image"))
     {
	fprintf(stderr, "ERROR: no 'image' part found.\n");
	evas_object_del(edje);
	goto exit_error;
     }
   if (!edje_edit_program_exist(edje, "animate"))
     {
	fprintf(stderr, "ERROR: no 'animate' program found.\n");
	evas_object_del(edje);
	goto exit_error;
     }

   for (i = 0; i < _plugin->frnum; i++)
     {
	snprintf(buf, sizeof(buf), "images/%d", i);
	edje_edit_image_data_add(edje, buf, i);
	if (i == 0)
	  edje_edit_state_image_set(edje, "image", "default 0.00", buf);
	else
	  edje_edit_state_tween_add(edje, "image", "default 0.00", buf);
     }

   edje_edit_program_transition_time_set(edje, "animate",
					 _plugin->total_time);
   edje_edit_program_transition_time_set(edje, "animate_loop",
					 _plugin->total_time);
   edje_edit_group_min_w_set(edje, _plugin->w);
   edje_edit_group_max_w_set(edje, _plugin->w);
   edje_edit_group_min_h_set(edje, _plugin->h);
   edje_edit_group_max_h_set(edje, _plugin->h);
   edje_edit_save(edje);

   evas_object_del(edje);

   return 1;

exit_error:
   return 0;
}

static void
_finish_thumb_generation(struct _emotion_plugin *_plugin, int success)
{
   int r = 0;
   evas_object_smart_callback_del(_plugin->video, "frame_resize",
				  _frame_resized_cb);
   emotion_object_play_set(_plugin->video, 0);
   evas_object_del(_plugin->video);
   if (_plugin->ef)
     eet_close(_plugin->ef);

   if (success)
     r = _setup_thumbnail(_plugin);

   free(_plugin);
   ethumb_finished_callback_call(_plugin->e, r);
}

static int
_frame_grab_single(void *data)
{
   struct _emotion_plugin *_plugin = data;
   Ethumb *e = _plugin->e;
   double p;

   if (_plugin->len <= 0)
     {
	_video_pos_set(_plugin);
	return EINA_TRUE;
     }

   p = emotion_object_position_get(_plugin->video);
   if (p < _plugin->pi)
     return EINA_TRUE;

   ethumb_image_save(e);

   evas_object_smart_callback_del(_plugin->video, "frame_resize",
				  _frame_resized_cb);
   emotion_object_play_set(_plugin->video, 0);
   evas_object_del(_plugin->video);
   free(_plugin);
   ethumb_finished_callback_call(e, 1);

   return EINA_FALSE;
}

static int
_frame_grab(void *data)
{
   struct _emotion_plugin *_plugin = data;
   Ethumb *e = _plugin->e;
   char buf[4096];
   const void *pixels;
   double p;

   if (_plugin->len <= 0)
     {
	_video_pos_set(_plugin);
	return EINA_TRUE;
     }

   p = emotion_object_position_get(_plugin->video);
   if (p < _plugin->pi)
     return EINA_TRUE;

   if (_plugin->first)
     {
	_plugin->pi = p;
	_plugin->first = EINA_FALSE;
     }

   if (p > _plugin->pi + _plugin->ptotal)
     {
	_plugin->total_time += _plugin->tmp_time;
	if (_plugin->pcount >= ethumb_video_ntimes_get(e))
	  {
	     _finish_thumb_generation(_plugin, EINA_TRUE);
	     return EINA_FALSE;
	  }
	else
	  {
	     _plugin->pcount++;
	     _video_pos_set(_plugin);
	     return EINA_TRUE;
	  }
     }

   _plugin->tmp_time = p - _plugin->pi;

   if (_plugin->ef)
     {
	Ecore_Evas *ee = ethumb_ecore_evas_get(e);
	int quality, compress;

	quality = ethumb_thumb_quality_get(e);
	compress = ethumb_thumb_compress_get(e);

	pixels = ecore_evas_buffer_pixels_get(ee);
	snprintf(buf, sizeof(buf), "images/%d", _plugin->frnum);
	eet_data_image_write(_plugin->ef, buf, pixels, _plugin->w, _plugin->h,
			     0, compress, quality, quality);
	_plugin->frnum++;
     }

   return EINA_TRUE;
}

static void
_generate_animated_thumb(struct _emotion_plugin *_plugin)
{
   const char *thumb_path;
   char *thumb_dir;
   char buf[4096];
   Ethumb *e = _plugin->e;

   snprintf(buf, sizeof(buf), "%s/data/emotion_template.edj", PLUGINSDIR);
   ethumb_thumb_path_get(e, &thumb_path, NULL);
   thumb_dir = ecore_file_dir_get(thumb_path);
   ecore_file_mkpath(thumb_dir);
   free(thumb_dir);
   ecore_file_cp(buf, thumb_path);
   _plugin->ef = eet_open(thumb_path, EET_FILE_MODE_READ_WRITE);
   if (!_plugin->ef)
     {
	fprintf(stderr, "ERROR: could not open '%s'\n", thumb_path);
	_finish_thumb_generation(_plugin, 0);
     }

   ecore_timer_add(1.0 / ethumb_video_fps_get(e), _frame_grab, _plugin);
}

static void
_generate_thumb(Ethumb *e)
{
   Evas_Object *o;
   int r;
   const char *file;
   float start;
   Ethumb_Thumb_Format f;
   struct _emotion_plugin *_plugin = calloc(sizeof(struct _emotion_plugin), 1);

   o = emotion_object_add(ethumb_evas_get(e));
   r = emotion_object_init(o, "xine");
   if (!r)
     {
	fprintf(stderr, "ERROR: could not start emotion using gstreamer"
		" plugin.\n");
	evas_object_del(o);
	ethumb_finished_callback_call(e, 0);
	free(_plugin);
	return;
     }

   _plugin->video = o;

   ethumb_file_get(e, &file, NULL);
   start = ethumb_video_start_get(e);
   f = ethumb_thumb_format_get(e);

   emotion_object_file_set(o, file);

   _plugin->video = o;
   _plugin->e = e;

   _plugin->ptotal = ethumb_video_time_get(e) / ethumb_video_ntimes_get(e);
   _plugin->pcount = 1;

   _resize_movie(_plugin);
   evas_object_smart_callback_add(o, "frame_resize",
				  _frame_resized_cb, _plugin);
   evas_object_smart_callback_add(o, "decode_stop",
				  _video_stopped_cb, _plugin);

   if (f == ETHUMB_THUMB_EET)
     {
	_generate_animated_thumb(_plugin);
     }
   else
     {
	ecore_timer_add(0.1, _frame_grab_single, _plugin);
     }

   _video_pos_set(_plugin);
   emotion_object_play_set(o, 1);
   evas_object_show(o);
}

EAPI Ethumb_Plugin *
ethumb_plugin_get(void)
{
   static const char *extensions[] = { "avi", "mp4", "ogv", "mov", NULL };
   static Ethumb_Plugin plugin =
     {
	extensions,
	_generate_thumb,
     };

   return &plugin;
}

static Eina_Bool
_module_init(void)
{
   return EINA_TRUE;
}

static void
_module_shutdown(void)
{
}

EINA_MODULE_INIT(_module_init);
EINA_MODULE_SHUTDOWN(_module_shutdown);