Mercurial > mplayer.hg
annotate sub/spudec.c @ 37195:ac6c37d85d65 default tip
configure: Fix initialization of variable def_local_aligned_32
It contiained the #define of HAVE_LOCAL_ALIGNED_16 instead
of HAVE_LOCAL_ALIGNED_32.
author | al |
---|---|
date | Sun, 28 Sep 2014 18:38:41 +0000 |
parents | cad608ae7295 |
children |
rev | line source |
---|---|
32456 | 1 /* |
2 * Skeleton of function spudec_process_controll() is from xine sources. | |
3 * Further works: | |
4 * LGB,... (yeah, try to improve it and insert your name here! ;-) | |
5 * | |
6 * Kim Minh Kaplan | |
7 * implement fragments reassembly, RLE decoding. | |
8 * read brightness from the IFO. | |
9 * | |
10 * For information on SPU format see <URL:http://sam.zoy.org/doc/dvd/subtitles/> | |
11 * and <URL:http://members.aol.com/mpucoder/DVD/spu.html> | |
12 * | |
13 * This file is part of MPlayer. | |
14 * | |
15 * MPlayer is free software; you can redistribute it and/or modify | |
16 * it under the terms of the GNU General Public License as published by | |
17 * the Free Software Foundation; either version 2 of the License, or | |
18 * (at your option) any later version. | |
19 * | |
20 * MPlayer is distributed in the hope that it will be useful, | |
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 * GNU General Public License for more details. | |
24 * | |
25 * You should have received a copy of the GNU General Public License along | |
26 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
28 */ | |
29 | |
30 #include "config.h" | |
31 #include "mp_msg.h" | |
32 | |
33 #include <errno.h> | |
34 #include <limits.h> | |
35 #include <stdio.h> | |
36 #include <stdlib.h> | |
37 #include <unistd.h> | |
38 #include <string.h> | |
35903 | 39 #include <strings.h> |
32456 | 40 #include <math.h> |
32467 | 41 #include "sub.h" |
32456 | 42 #include "libvo/video_out.h" |
32464
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32459
diff
changeset
|
43 #include "spudec.h" |
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32459
diff
changeset
|
44 #include "vobsub.h" |
32456 | 45 #include "libavutil/avutil.h" |
35712
d206960484fe
Add a number of missing libavutil header #includes.
diego
parents:
35253
diff
changeset
|
46 #include "libavutil/common.h" |
32456 | 47 #include "libavutil/intreadwrite.h" |
48 #include "libswscale/swscale.h" | |
49 | |
50 /* Valid values for spu_aamode: | |
51 0: none (fastest, most ugly) | |
52 1: approximate | |
53 2: full (slowest) | |
54 3: bilinear (similiar to vobsub, fast and not too bad) | |
55 4: uses swscaler gaussian (this is the only one that looks good) | |
56 */ | |
57 | |
58 int spu_aamode = 3; | |
59 int spu_alignment = -1; | |
60 float spu_gaussvar = 1.0; | |
61 | |
34784 | 62 typedef struct spu_packet_t packet_t; |
63 struct spu_packet_t { | |
32456 | 64 int is_decoded; |
65 unsigned char *packet; | |
66 int data_len; | |
67 unsigned int palette[4]; | |
68 unsigned int alpha[4]; | |
69 unsigned int control_start; /* index of start of control data */ | |
70 unsigned int current_nibble[2]; /* next data nibble (4 bits) to be | |
71 processed (for RLE decoding) for | |
72 even and odd lines */ | |
73 int deinterlace_oddness; /* 0 or 1, index into current_nibble */ | |
74 unsigned int start_col; | |
75 unsigned int start_row; | |
76 unsigned int width, height, stride; | |
77 unsigned int start_pts, end_pts; | |
78 packet_t *next; | |
79 }; | |
80 | |
81 struct palette_crop_cache { | |
82 int valid; | |
83 uint32_t palette; | |
84 int sx, sy, ex, ey; | |
85 int result; | |
86 }; | |
87 | |
88 typedef struct { | |
89 packet_t *queue_head; | |
90 packet_t *queue_tail; | |
91 unsigned int global_palette[16]; | |
92 unsigned int orig_frame_width, orig_frame_height; | |
93 unsigned char* packet; | |
94 size_t packet_reserve; /* size of the memory pointed to by packet */ | |
95 unsigned int packet_offset; /* end of the currently assembled fragment */ | |
96 unsigned int packet_size; /* size of the packet once all fragments are assembled */ | |
97 int packet_pts; /* PTS for this packet */ | |
98 unsigned int palette[4]; | |
99 unsigned int alpha[4]; | |
100 unsigned int cuspal[4]; | |
101 unsigned int custom; | |
102 unsigned int now_pts; | |
103 unsigned int start_pts, end_pts; | |
104 unsigned int start_col; | |
105 unsigned int start_row; | |
106 unsigned int width, height, stride; | |
107 size_t image_size; /* Size of the image buffer */ | |
108 unsigned char *image; /* Grayscale value */ | |
109 unsigned char *aimage; /* Alpha value */ | |
110 unsigned int pal_start_col, pal_start_row; | |
111 unsigned int pal_width, pal_height; | |
112 unsigned char *pal_image; /* palette entry value */ | |
113 unsigned int scaled_frame_width, scaled_frame_height; | |
114 unsigned int scaled_start_col, scaled_start_row; | |
115 unsigned int scaled_width, scaled_height, scaled_stride; | |
116 size_t scaled_image_size; | |
117 unsigned char *scaled_image; | |
118 unsigned char *scaled_aimage; | |
119 int auto_palette; /* 1 if we lack a palette and must use an heuristic. */ | |
120 int font_start_level; /* Darkest value used for the computed font */ | |
121 const vo_functions_t *hw_spu; | |
122 int spu_changed; | |
123 unsigned int forced_subs_only; /* flag: 0=display all subtitle, !0 display only forced subtitles */ | |
124 unsigned int is_forced_sub; /* true if current subtitle is a forced subtitle */ | |
125 | |
126 struct palette_crop_cache palette_crop_cache; | |
127 } spudec_handle_t; | |
128 | |
129 static void spudec_queue_packet(spudec_handle_t *this, packet_t *packet) | |
130 { | |
131 if (this->queue_head == NULL) | |
132 this->queue_head = packet; | |
133 else | |
134 this->queue_tail->next = packet; | |
135 this->queue_tail = packet; | |
136 } | |
137 | |
138 static packet_t *spudec_dequeue_packet(spudec_handle_t *this) | |
139 { | |
140 packet_t *retval = this->queue_head; | |
141 | |
142 this->queue_head = retval->next; | |
143 if (this->queue_head == NULL) | |
144 this->queue_tail = NULL; | |
145 | |
146 return retval; | |
147 } | |
148 | |
149 static void spudec_free_packet(packet_t *packet) | |
150 { | |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
151 free(packet->packet); |
32456 | 152 free(packet); |
153 } | |
154 | |
155 static inline unsigned int get_be16(const unsigned char *p) | |
156 { | |
157 return (p[0] << 8) + p[1]; | |
158 } | |
159 | |
160 static inline unsigned int get_be24(const unsigned char *p) | |
161 { | |
162 return (get_be16(p) << 8) + p[2]; | |
163 } | |
164 | |
165 static void next_line(packet_t *packet) | |
166 { | |
167 if (packet->current_nibble[packet->deinterlace_oddness] % 2) | |
168 packet->current_nibble[packet->deinterlace_oddness]++; | |
169 packet->deinterlace_oddness = (packet->deinterlace_oddness + 1) % 2; | |
170 } | |
171 | |
172 static inline unsigned char get_nibble(packet_t *packet) | |
173 { | |
174 unsigned char nib; | |
175 unsigned int *nibblep = packet->current_nibble + packet->deinterlace_oddness; | |
176 if (*nibblep / 2 >= packet->control_start) { | |
177 mp_msg(MSGT_SPUDEC,MSGL_WARN, "SPUdec: ERROR: get_nibble past end of packet\n"); | |
178 return 0; | |
179 } | |
180 nib = packet->packet[*nibblep / 2]; | |
181 if (*nibblep % 2) | |
182 nib &= 0xf; | |
183 else | |
184 nib >>= 4; | |
185 ++*nibblep; | |
186 return nib; | |
187 } | |
188 | |
189 /* Cut the sub to visible part */ | |
190 static inline void spudec_cut_image(spudec_handle_t *this) | |
191 { | |
192 unsigned int fy, ly; | |
193 unsigned int first_y, last_y; | |
194 | |
195 if (this->stride == 0 || this->height == 0) { | |
196 return; | |
197 } | |
198 | |
199 for (fy = 0; fy < this->image_size && !this->aimage[fy]; fy++); | |
200 for (ly = this->stride * this->height-1; ly && !this->aimage[ly]; ly--); | |
201 first_y = fy / this->stride; | |
202 last_y = ly / this->stride; | |
203 //printf("first_y: %d, last_y: %d\n", first_y, last_y); | |
204 this->start_row += first_y; | |
205 | |
206 // Some subtitles trigger this condition | |
207 if (last_y + 1 > first_y ) { | |
208 this->height = last_y - first_y +1; | |
209 } else { | |
210 this->height = 0; | |
211 return; | |
212 } | |
213 | |
214 // printf("new h %d new start %d (sz %d st %d)---\n\n", this->height, this->start_row, this->image_size, this->stride); | |
215 | |
216 if (first_y > 0) { | |
217 memmove(this->image, this->image + this->stride * first_y, this->stride * this->height); | |
218 memmove(this->aimage, this->aimage + this->stride * first_y, this->stride * this->height); | |
219 } | |
220 } | |
221 | |
222 | |
223 static int spudec_alloc_image(spudec_handle_t *this, int stride, int height) | |
224 { | |
225 if (this->width > stride) // just a safeguard | |
226 this->width = stride; | |
227 this->stride = stride; | |
228 this->height = height; | |
229 if (this->image_size < this->stride * this->height) { | |
230 if (this->image != NULL) { | |
231 free(this->image); | |
35209 | 232 this->image = NULL; |
32456 | 233 free(this->pal_image); |
35209 | 234 this->pal_image = NULL; |
32456 | 235 this->image_size = 0; |
236 this->pal_width = this->pal_height = 0; | |
237 } | |
238 this->image = malloc(2 * this->stride * this->height); | |
239 if (this->image) { | |
240 this->image_size = this->stride * this->height; | |
241 this->aimage = this->image + this->image_size; | |
242 // use stride here as well to simplify reallocation checks | |
243 this->pal_image = malloc(this->stride * this->height); | |
244 } | |
245 } | |
246 return this->image != NULL; | |
247 } | |
248 | |
249 /** | |
250 * \param pal palette in MPlayer-style gray-alpha values, i.e. | |
251 * alpha == 0 means transparent, 1 fully opaque, | |
252 * gray value <= 256 - alpha. | |
253 */ | |
254 static void pal2gray_alpha(const uint16_t *pal, | |
255 const uint8_t *src, int src_stride, | |
256 uint8_t *dst, uint8_t *dsta, | |
37172 | 257 int dst_stride, int w, int h, int skip_stride) |
32456 | 258 { |
259 int x, y; | |
260 for (y = 0; y < h; y++) { | |
261 for (x = 0; x < w; x++) { | |
262 uint16_t pixel = pal[src[x]]; | |
263 *dst++ = pixel; | |
264 *dsta++ = pixel >> 8; | |
265 } | |
37172 | 266 if (skip_stride) { |
267 dst += dst_stride - w; | |
268 dsta += dst_stride - w; | |
269 } else for (; x < dst_stride; x++) | |
32456 | 270 *dsta++ = *dst++ = 0; |
271 src += src_stride; | |
272 } | |
273 } | |
274 | |
275 static int apply_palette_crop(spudec_handle_t *this, | |
276 unsigned crop_x, unsigned crop_y, | |
277 unsigned crop_w, unsigned crop_h) | |
278 { | |
279 int i; | |
280 uint8_t *src; | |
281 uint16_t pal[4]; | |
282 unsigned stride = (crop_w + 7) & ~7; | |
34778
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
283 int ret = 1; |
32456 | 284 if (crop_x > this->pal_width || crop_y > this->pal_height || |
285 crop_w > this->pal_width - crop_x || crop_h > this->pal_width - crop_y || | |
286 crop_w > 0x8000 || crop_h > 0x8000 || | |
287 stride * crop_h > this->image_size) { | |
34778
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
288 // this might be an actual error or just signal that |
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
289 // the highlight should be removed. |
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
290 this->width = 0; |
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
291 this->height = 0; |
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
292 ret = 0; |
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
293 goto out; |
32456 | 294 } |
295 for (i = 0; i < 4; ++i) { | |
296 int color; | |
297 int alpha = this->alpha[i]; | |
298 // extend 4 -> 8 bit | |
299 alpha |= alpha << 4; | |
300 if (this->custom && (this->cuspal[i] >> 31) != 0) | |
301 alpha = 0; | |
302 color = this->custom ? this->cuspal[i] : | |
303 this->global_palette[this->palette[i]]; | |
304 color = (color >> 16) & 0xff; | |
305 // convert to MPlayer-style gray/alpha palette | |
306 color = FFMIN(color, alpha); | |
307 pal[i] = (-alpha << 8) | color; | |
308 } | |
309 src = this->pal_image + crop_y * this->pal_width + crop_x; | |
310 pal2gray_alpha(pal, src, this->pal_width, | |
311 this->image, this->aimage, stride, | |
37172 | 312 crop_w, crop_h, 0); |
32456 | 313 this->width = crop_w; |
314 this->height = crop_h; | |
315 this->stride = stride; | |
316 this->start_col = this->pal_start_col + crop_x; | |
317 this->start_row = this->pal_start_row + crop_y; | |
318 spudec_cut_image(this); | |
319 | |
34778
48310248f892
Ensure that the highlight disappears when switching
reimar
parents:
33143
diff
changeset
|
320 out: |
32456 | 321 // reset scaled image |
322 this->scaled_frame_width = 0; | |
323 this->scaled_frame_height = 0; | |
324 this->palette_crop_cache.valid = 0; | |
36767
18428293fb7f
spudec: fix spudec_apply_palette_crop return value.
reimar
parents:
35903
diff
changeset
|
325 return ret; |
32456 | 326 } |
327 | |
328 int spudec_apply_palette_crop(void *this, uint32_t palette, | |
329 int sx, int sy, int ex, int ey) | |
330 { | |
331 spudec_handle_t *spu = this; | |
332 struct palette_crop_cache *c = &spu->palette_crop_cache; | |
333 if (c->valid && c->palette == palette && | |
334 c->sx == sx && c->sy == sy && c->ex == ex && c->ey == ey) | |
335 return c->result; | |
336 spu->palette[0] = (palette >> 28) & 0xf; | |
337 spu->palette[1] = (palette >> 24) & 0xf; | |
338 spu->palette[2] = (palette >> 20) & 0xf; | |
339 spu->palette[3] = (palette >> 16) & 0xf; | |
340 spu->alpha[0] = (palette >> 12) & 0xf; | |
341 spu->alpha[1] = (palette >> 8) & 0xf; | |
342 spu->alpha[2] = (palette >> 4) & 0xf; | |
343 spu->alpha[3] = palette & 0xf; | |
344 spu->spu_changed = 1; | |
345 c->result = apply_palette_crop(spu, | |
346 sx - spu->pal_start_col, sy - spu->pal_start_row, | |
347 ex - sx, ey - sy); | |
348 c->palette = palette; | |
349 c->sx = sx; c->sy = sy; | |
350 c->ex = ex; c->ey = ey; | |
351 c->valid = 1; | |
352 return c->result; | |
353 } | |
354 | |
355 static void spudec_process_data(spudec_handle_t *this, packet_t *packet) | |
356 { | |
357 unsigned int i, x, y; | |
358 uint8_t *dst; | |
359 | |
360 if (!spudec_alloc_image(this, packet->stride, packet->height)) | |
361 return; | |
362 | |
363 this->pal_start_col = packet->start_col; | |
364 this->pal_start_row = packet->start_row; | |
365 this->pal_height = packet->height; | |
366 this->pal_width = packet->width; | |
367 this->stride = packet->stride; | |
368 memcpy(this->palette, packet->palette, sizeof(this->palette)); | |
369 memcpy(this->alpha, packet->alpha, sizeof(this->alpha)); | |
370 | |
371 i = packet->current_nibble[1]; | |
372 x = 0; | |
373 y = 0; | |
374 dst = this->pal_image; | |
375 while (packet->current_nibble[0] < i | |
376 && packet->current_nibble[1] / 2 < packet->control_start | |
377 && y < this->pal_height) { | |
378 unsigned int len, color; | |
379 unsigned int rle = 0; | |
380 rle = get_nibble(packet); | |
381 if (rle < 0x04) { | |
382 if (rle == 0) { | |
383 rle = (rle << 4) | get_nibble(packet); | |
384 if (rle < 0x04) | |
385 rle = (rle << 4) | get_nibble(packet); | |
386 } | |
387 rle = (rle << 4) | get_nibble(packet); | |
388 } | |
389 color = 3 - (rle & 0x3); | |
390 len = rle >> 2; | |
391 x += len; | |
392 if (len == 0 || x >= this->pal_width) { | |
393 len += this->pal_width - x; | |
394 next_line(packet); | |
395 x = 0; | |
396 ++y; | |
397 } | |
398 memset(dst, color, len); | |
399 dst += len; | |
400 } | |
401 apply_palette_crop(this, 0, 0, this->pal_width, this->pal_height); | |
402 } | |
403 | |
404 | |
405 /* | |
406 This function tries to create a usable palette. | |
407 It determines how many non-transparent colors are used, and assigns different | |
408 gray scale values to each color. | |
409 I tested it with four streams and even got something readable. Half of the | |
410 times I got black characters with white around and half the reverse. | |
411 */ | |
412 static void compute_palette(spudec_handle_t *this, packet_t *packet) | |
413 { | |
414 int used[16],i,cused,start,step,color; | |
415 | |
416 memset(used, 0, sizeof(used)); | |
417 for (i=0; i<4; i++) | |
418 if (packet->alpha[i]) /* !Transparent? */ | |
419 used[packet->palette[i]] = 1; | |
420 for (cused=0, i=0; i<16; i++) | |
421 if (used[i]) cused++; | |
422 if (!cused) return; | |
423 if (cused == 1) { | |
424 start = 0x80; | |
425 step = 0; | |
426 } else { | |
427 start = this->font_start_level; | |
428 step = (0xF0-this->font_start_level)/(cused-1); | |
429 } | |
430 memset(used, 0, sizeof(used)); | |
431 for (i=0; i<4; i++) { | |
432 color = packet->palette[i]; | |
433 if (packet->alpha[i] && !used[color]) { /* not assigned? */ | |
434 used[color] = 1; | |
435 this->global_palette[color] = start<<16; | |
436 start += step; | |
437 } | |
438 } | |
439 } | |
440 | |
441 static void spudec_process_control(spudec_handle_t *this, int pts100) | |
442 { | |
443 int a,b,c,d; /* Temporary vars */ | |
444 unsigned int date, type; | |
445 unsigned int off; | |
446 unsigned int start_off = 0; | |
447 unsigned int next_off; | |
448 unsigned int start_pts = 0; | |
449 unsigned int end_pts = 0; | |
450 unsigned int current_nibble[2] = {0, 0}; | |
451 unsigned int control_start; | |
452 unsigned int display = 0; | |
453 unsigned int start_col = 0; | |
454 unsigned int end_col = 0; | |
455 unsigned int start_row = 0; | |
456 unsigned int end_row = 0; | |
457 unsigned int width = 0; | |
458 unsigned int height = 0; | |
459 unsigned int stride = 0; | |
460 | |
461 control_start = get_be16(this->packet + 2); | |
462 next_off = control_start; | |
463 while (start_off != next_off) { | |
464 start_off = next_off; | |
465 date = get_be16(this->packet + start_off) * 1024; | |
466 next_off = get_be16(this->packet + start_off + 2); | |
467 mp_msg(MSGT_SPUDEC,MSGL_DBG2, "date=%d\n", date); | |
468 off = start_off + 4; | |
469 for (type = this->packet[off++]; type != 0xff; type = this->packet[off++]) { | |
470 mp_msg(MSGT_SPUDEC,MSGL_DBG2, "cmd=%d ",type); | |
471 switch(type) { | |
472 case 0x00: | |
473 /* Menu ID, 1 byte */ | |
474 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Menu ID\n"); | |
475 /* shouldn't a Menu ID type force display start? */ | |
476 start_pts = pts100 < 0 && -pts100 >= date ? 0 : pts100 + date; | |
477 end_pts = UINT_MAX; | |
478 display = 1; | |
479 this->is_forced_sub=~0; // current subtitle is forced | |
480 break; | |
481 case 0x01: | |
482 /* Start display */ | |
483 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Start display!\n"); | |
484 start_pts = pts100 < 0 && -pts100 >= date ? 0 : pts100 + date; | |
485 end_pts = UINT_MAX; | |
486 display = 1; | |
487 this->is_forced_sub=0; | |
488 break; | |
489 case 0x02: | |
490 /* Stop display */ | |
491 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Stop display!\n"); | |
492 end_pts = pts100 < 0 && -pts100 >= date ? 0 : pts100 + date; | |
493 break; | |
494 case 0x03: | |
495 /* Palette */ | |
496 this->palette[0] = this->packet[off] >> 4; | |
497 this->palette[1] = this->packet[off] & 0xf; | |
498 this->palette[2] = this->packet[off + 1] >> 4; | |
499 this->palette[3] = this->packet[off + 1] & 0xf; | |
500 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Palette %d, %d, %d, %d\n", | |
501 this->palette[0], this->palette[1], this->palette[2], this->palette[3]); | |
502 off+=2; | |
503 break; | |
504 case 0x04: | |
505 /* Alpha */ | |
506 a = this->packet[off] >> 4; | |
507 b = this->packet[off] & 0xf; | |
508 c = this->packet[off + 1] >> 4; | |
509 d = this->packet[off + 1] & 0xf; | |
510 // Note: some DVDs change these values to create a fade-in/fade-out effect | |
511 // We can not handle this, so just keep the highest value during the display time. | |
512 if (display) { | |
513 a = FFMAX(a, this->alpha[0]); | |
514 b = FFMAX(b, this->alpha[1]); | |
515 c = FFMAX(c, this->alpha[2]); | |
516 d = FFMAX(d, this->alpha[3]); | |
517 } | |
518 this->alpha[0] = a; | |
519 this->alpha[1] = b; | |
520 this->alpha[2] = c; | |
521 this->alpha[3] = d; | |
522 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Alpha %d, %d, %d, %d\n", | |
523 this->alpha[0], this->alpha[1], this->alpha[2], this->alpha[3]); | |
524 off+=2; | |
525 break; | |
526 case 0x05: | |
527 /* Co-ords */ | |
528 a = get_be24(this->packet + off); | |
529 b = get_be24(this->packet + off + 3); | |
530 start_col = a >> 12; | |
531 end_col = a & 0xfff; | |
532 width = (end_col < start_col) ? 0 : end_col - start_col + 1; | |
533 stride = (width + 7) & ~7; /* Kludge: draw_alpha needs width multiple of 8 */ | |
534 start_row = b >> 12; | |
535 end_row = b & 0xfff; | |
536 height = (end_row < start_row) ? 0 : end_row - start_row /* + 1 */; | |
537 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Coords col: %d - %d row: %d - %d (%dx%d)\n", | |
538 start_col, end_col, start_row, end_row, | |
539 width, height); | |
540 off+=6; | |
541 break; | |
542 case 0x06: | |
543 /* Graphic lines */ | |
544 current_nibble[0] = 2 * get_be16(this->packet + off); | |
545 current_nibble[1] = 2 * get_be16(this->packet + off + 2); | |
546 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"Graphic offset 1: %d offset 2: %d\n", | |
547 current_nibble[0] / 2, current_nibble[1] / 2); | |
548 off+=4; | |
549 break; | |
550 default: | |
551 mp_msg(MSGT_SPUDEC,MSGL_WARN,"spudec: Error determining control type 0x%02x. Skipping %d bytes.\n", | |
552 type, next_off - off); | |
553 goto next_control; | |
554 } | |
555 } | |
556 next_control: | |
557 if (!display) | |
558 continue; | |
559 if (end_pts == UINT_MAX && start_off != next_off) { | |
560 end_pts = get_be16(this->packet + next_off) * 1024; | |
561 end_pts = 1 - pts100 >= end_pts ? 0 : pts100 + end_pts - 1; | |
562 } | |
563 if (end_pts > 0) { | |
564 packet_t *packet = calloc(1, sizeof(packet_t)); | |
565 int i; | |
566 packet->start_pts = start_pts; | |
567 packet->end_pts = end_pts; | |
568 packet->current_nibble[0] = current_nibble[0]; | |
569 packet->current_nibble[1] = current_nibble[1]; | |
570 packet->start_row = start_row; | |
571 packet->start_col = start_col; | |
572 packet->width = width; | |
573 packet->height = height; | |
574 packet->stride = stride; | |
575 packet->control_start = control_start; | |
576 for (i=0; i<4; i++) { | |
577 packet->alpha[i] = this->alpha[i]; | |
578 packet->palette[i] = this->palette[i]; | |
579 } | |
580 packet->packet = malloc(this->packet_size); | |
581 memcpy(packet->packet, this->packet, this->packet_size); | |
582 spudec_queue_packet(this, packet); | |
583 } | |
584 } | |
585 } | |
586 | |
587 static void spudec_decode(spudec_handle_t *this, int pts100) | |
588 { | |
589 if (!this->hw_spu) | |
590 spudec_process_control(this, pts100); | |
591 else if (pts100 >= 0) { | |
592 static vo_mpegpes_t packet = { NULL, 0, 0x20, 0 }; | |
593 static vo_mpegpes_t *pkg=&packet; | |
594 packet.data = this->packet; | |
595 packet.size = this->packet_size; | |
596 packet.timestamp = pts100; | |
597 this->hw_spu->draw_frame((uint8_t**)&pkg); | |
598 } | |
599 } | |
600 | |
601 int spudec_changed(void * this) | |
602 { | |
603 spudec_handle_t * spu = this; | |
604 return spu->spu_changed || spu->now_pts > spu->end_pts; | |
605 } | |
606 | |
607 void spudec_assemble(void *this, unsigned char *packet, unsigned int len, int pts100) | |
608 { | |
609 spudec_handle_t *spu = this; | |
610 // spudec_heartbeat(this, pts100); | |
611 if (len < 2) { | |
612 mp_msg(MSGT_SPUDEC,MSGL_WARN,"SPUasm: packet too short\n"); | |
613 return; | |
614 } | |
615 spu->packet_pts = pts100; | |
616 if (spu->packet_offset == 0) { | |
617 unsigned int len2 = get_be16(packet); | |
618 // Start new fragment | |
619 if (spu->packet_reserve < len2) { | |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
620 free(spu->packet); |
32456 | 621 spu->packet = malloc(len2); |
622 spu->packet_reserve = spu->packet != NULL ? len2 : 0; | |
623 } | |
624 if (spu->packet != NULL) { | |
625 spu->packet_size = len2; | |
626 if (len > len2) { | |
627 mp_msg(MSGT_SPUDEC,MSGL_WARN,"SPUasm: invalid frag len / len2: %d / %d \n", len, len2); | |
628 return; | |
629 } | |
630 memcpy(spu->packet, packet, len); | |
631 spu->packet_offset = len; | |
632 spu->packet_pts = pts100; | |
633 } | |
634 } else { | |
635 // Continue current fragment | |
636 if (spu->packet_size < spu->packet_offset + len){ | |
637 mp_msg(MSGT_SPUDEC,MSGL_WARN,"SPUasm: invalid fragment\n"); | |
638 spu->packet_size = spu->packet_offset = 0; | |
639 return; | |
640 } else { | |
641 memcpy(spu->packet + spu->packet_offset, packet, len); | |
642 spu->packet_offset += len; | |
643 } | |
644 } | |
645 #if 1 | |
646 // check if we have a complete packet (unfortunatelly packet_size is bad | |
647 // for some disks) | |
648 // [cb] packet_size is padded to be even -> may be one byte too long | |
649 if ((spu->packet_offset == spu->packet_size) || | |
650 ((spu->packet_offset + 1) == spu->packet_size)){ | |
651 unsigned int x=0,y; | |
652 while(x+4<=spu->packet_offset){ | |
653 y=get_be16(spu->packet+x+2); // next control pointer | |
654 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"SPUtest: x=%d y=%d off=%d size=%d\n",x,y,spu->packet_offset,spu->packet_size); | |
655 if(x>=4 && x==y){ // if it points to self - we're done! | |
656 // we got it! | |
657 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"SPUgot: off=%d size=%d \n",spu->packet_offset,spu->packet_size); | |
658 spudec_decode(spu, pts100); | |
659 spu->packet_offset = 0; | |
660 break; | |
661 } | |
662 if(y<=x || y>=spu->packet_size){ // invalid? | |
663 mp_msg(MSGT_SPUDEC,MSGL_WARN,"SPUtest: broken packet!!!!! y=%d < x=%d\n",y,x); | |
664 spu->packet_size = spu->packet_offset = 0; | |
665 break; | |
666 } | |
667 x=y; | |
668 } | |
669 // [cb] packet is done; start new packet | |
670 spu->packet_offset = 0; | |
671 } | |
672 #else | |
673 if (spu->packet_offset == spu->packet_size) { | |
674 spudec_decode(spu, pts100); | |
675 spu->packet_offset = 0; | |
676 } | |
677 #endif | |
678 } | |
679 | |
680 void spudec_reset(void *this) // called after seek | |
681 { | |
682 spudec_handle_t *spu = this; | |
683 while (spu->queue_head) | |
684 spudec_free_packet(spudec_dequeue_packet(spu)); | |
685 spu->now_pts = 0; | |
686 spu->end_pts = 0; | |
687 spu->packet_size = spu->packet_offset = 0; | |
688 } | |
689 | |
690 void spudec_heartbeat(void *this, unsigned int pts100) | |
691 { | |
692 spudec_handle_t *spu = this; | |
693 spu->now_pts = pts100; | |
694 | |
695 // TODO: detect and handle broken timestamps (e.g. due to wrapping) | |
696 while (spu->queue_head != NULL && pts100 >= spu->queue_head->start_pts) { | |
697 packet_t *packet = spudec_dequeue_packet(spu); | |
698 spu->start_pts = packet->start_pts; | |
699 spu->end_pts = packet->end_pts; | |
700 if (packet->is_decoded) { | |
701 free(spu->image); | |
702 spu->image_size = packet->data_len; | |
703 spu->image = packet->packet; | |
704 spu->aimage = packet->packet + packet->stride * packet->height; | |
705 packet->packet = NULL; | |
706 spu->width = packet->width; | |
707 spu->height = packet->height; | |
708 spu->stride = packet->stride; | |
709 spu->start_col = packet->start_col; | |
710 spu->start_row = packet->start_row; | |
711 | |
712 // reset scaled image | |
713 spu->scaled_frame_width = 0; | |
714 spu->scaled_frame_height = 0; | |
715 } else { | |
716 if (spu->auto_palette) | |
717 compute_palette(spu, packet); | |
718 spudec_process_data(spu, packet); | |
719 } | |
720 spudec_free_packet(packet); | |
721 spu->spu_changed = 1; | |
722 } | |
723 } | |
724 | |
725 int spudec_visible(void *this){ | |
726 spudec_handle_t *spu = this; | |
727 int ret=(spu->start_pts <= spu->now_pts && | |
728 spu->now_pts < spu->end_pts && | |
729 spu->height > 0); | |
730 // printf("spu visible: %d \n",ret); | |
731 return ret; | |
732 } | |
733 | |
734 void spudec_set_forced_subs_only(void * const this, const unsigned int flag) | |
735 { | |
736 if(this){ | |
737 ((spudec_handle_t *)this)->forced_subs_only=flag; | |
738 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"SPU: Display only forced subs now %s\n", flag ? "enabled": "disabled"); | |
739 } | |
740 } | |
741 | |
742 void spudec_draw(void *this, void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)) | |
743 { | |
744 spudec_handle_t *spu = this; | |
745 if (spudec_visible(spu)) | |
746 { | |
747 draw_alpha(spu->start_col, spu->start_row, spu->width, spu->height, | |
748 spu->image, spu->aimage, spu->stride); | |
749 spu->spu_changed = 0; | |
750 } | |
751 } | |
752 | |
35031 | 753 static void validate_dimensions(spudec_handle_t *spu, unsigned dxs, unsigned dys) |
754 { | |
755 if (spu->orig_frame_width == 0 || spu->orig_frame_height == 0) { | |
756 spu->width = FFMIN(spu->width, dxs); | |
757 spu->height = FFMIN(spu->height, dys); | |
758 spu->start_col = FFMIN(spu->start_col, dxs - spu->width); | |
759 spu->start_row = FFMIN(spu->start_row, dys - spu->height); | |
760 return; | |
761 } | |
762 spu->orig_frame_width = FFMAX(spu->orig_frame_width, spu->start_col + spu->width); | |
763 spu->orig_frame_height = FFMAX(spu->orig_frame_height, spu->start_row + spu->height); | |
764 } | |
765 | |
32456 | 766 /* calc the bbox for spudec subs */ |
767 void spudec_calc_bbox(void *me, unsigned int dxs, unsigned int dys, unsigned int* bbox) | |
768 { | |
769 spudec_handle_t *spu = me; | |
35031 | 770 validate_dimensions(spu, dxs, dys); |
32456 | 771 if (spu->orig_frame_width == 0 || spu->orig_frame_height == 0 |
772 || (spu->orig_frame_width == dxs && spu->orig_frame_height == dys)) { | |
773 // unscaled | |
774 bbox[0] = spu->start_col; | |
775 bbox[1] = spu->start_col + spu->width; | |
776 bbox[2] = spu->start_row; | |
777 bbox[3] = spu->start_row + spu->height; | |
778 } | |
779 else { | |
780 // scaled | |
781 unsigned int scalex = 0x100 * dxs / spu->orig_frame_width; | |
782 unsigned int scaley = 0x100 * dys / spu->orig_frame_height; | |
783 bbox[0] = spu->start_col * scalex / 0x100; | |
784 bbox[1] = spu->start_col * scalex / 0x100 + spu->width * scalex / 0x100; | |
785 switch (spu_alignment) { | |
786 case 0: | |
787 bbox[3] = dys*sub_pos/100 + spu->height * scaley / 0x100; | |
788 if (bbox[3] > dys) bbox[3] = dys; | |
789 bbox[2] = bbox[3] - spu->height * scaley / 0x100; | |
790 break; | |
791 case 1: | |
792 if (sub_pos < 50) { | |
793 bbox[2] = dys*sub_pos/100 - spu->height * scaley / 0x200; | |
794 bbox[3] = bbox[2] + spu->height; | |
795 } else { | |
796 bbox[3] = dys*sub_pos/100 + spu->height * scaley / 0x200; | |
797 if (bbox[3] > dys) bbox[3] = dys; | |
798 bbox[2] = bbox[3] - spu->height * scaley / 0x100; | |
799 } | |
800 break; | |
801 case 2: | |
802 bbox[2] = dys*sub_pos/100 - spu->height * scaley / 0x100; | |
803 bbox[3] = bbox[2] + spu->height; | |
804 break; | |
805 default: /* -1 */ | |
806 bbox[2] = spu->start_row * scaley / 0x100; | |
807 bbox[3] = spu->start_row * scaley / 0x100 + spu->height * scaley / 0x100; | |
808 break; | |
809 } | |
810 } | |
811 } | |
812 /* transform mplayer's alpha value into an opacity value that is linear */ | |
813 static inline int canon_alpha(int alpha) | |
814 { | |
815 return (uint8_t)-alpha; | |
816 } | |
817 | |
818 typedef struct { | |
819 unsigned position; | |
820 unsigned left_up; | |
821 unsigned right_down; | |
822 }scale_pixel; | |
823 | |
824 | |
825 static void scale_table(unsigned int start_src, unsigned int start_tar, unsigned int end_src, unsigned int end_tar, scale_pixel * table) | |
826 { | |
827 unsigned int t; | |
828 unsigned int delta_src = end_src - start_src; | |
829 unsigned int delta_tar = end_tar - start_tar; | |
830 int src = 0; | |
831 int src_step; | |
832 if (delta_src == 0 || delta_tar == 0) { | |
833 return; | |
834 } | |
835 src_step = (delta_src << 16) / delta_tar >>1; | |
836 for (t = 0; t<=delta_tar; src += (src_step << 1), t++){ | |
837 table[t].position= FFMIN(src >> 16, end_src - 1); | |
838 table[t].right_down = src & 0xffff; | |
839 table[t].left_up = 0x10000 - table[t].right_down; | |
840 } | |
841 } | |
842 | |
843 /* bilinear scale, similar to vobsub's code */ | |
844 static void scale_image(int x, int y, scale_pixel* table_x, scale_pixel* table_y, spudec_handle_t * spu) | |
845 { | |
846 int alpha[4]; | |
847 int color[4]; | |
848 unsigned int scale[4]; | |
849 int base = table_y[y].position * spu->stride + table_x[x].position; | |
850 int scaled = y * spu->scaled_stride + x; | |
851 alpha[0] = canon_alpha(spu->aimage[base]); | |
852 alpha[1] = canon_alpha(spu->aimage[base + 1]); | |
853 alpha[2] = canon_alpha(spu->aimage[base + spu->stride]); | |
854 alpha[3] = canon_alpha(spu->aimage[base + spu->stride + 1]); | |
855 color[0] = spu->image[base]; | |
856 color[1] = spu->image[base + 1]; | |
857 color[2] = spu->image[base + spu->stride]; | |
858 color[3] = spu->image[base + spu->stride + 1]; | |
859 scale[0] = (table_x[x].left_up * table_y[y].left_up >> 16) * alpha[0]; | |
860 if (table_y[y].left_up == 0x10000) // necessary to avoid overflow-case | |
861 scale[0] = table_x[x].left_up * alpha[0]; | |
862 scale[1] = (table_x[x].right_down * table_y[y].left_up >>16) * alpha[1]; | |
863 scale[2] = (table_x[x].left_up * table_y[y].right_down >> 16) * alpha[2]; | |
864 scale[3] = (table_x[x].right_down * table_y[y].right_down >> 16) * alpha[3]; | |
865 spu->scaled_image[scaled] = (color[0] * scale[0] + color[1] * scale[1] + color[2] * scale[2] + color[3] * scale[3])>>24; | |
866 spu->scaled_aimage[scaled] = (scale[0] + scale[1] + scale[2] + scale[3]) >> 16; | |
867 if (spu->scaled_aimage[scaled]){ | |
868 // ensure that MPlayer's simplified alpha-blending can not overflow | |
869 spu->scaled_image[scaled] = FFMIN(spu->scaled_image[scaled], spu->scaled_aimage[scaled]); | |
870 // convert to MPlayer-style alpha | |
871 spu->scaled_aimage[scaled] = -spu->scaled_aimage[scaled]; | |
872 } | |
873 } | |
874 | |
875 static void sws_spu_image(unsigned char *d1, unsigned char *d2, int dw, int dh, | |
876 int ds, const unsigned char* s1, unsigned char* s2, | |
877 int sw, int sh, int ss) | |
878 { | |
879 struct SwsContext *ctx; | |
880 static SwsFilter filter; | |
881 static int firsttime = 1; | |
882 static float oldvar; | |
883 int i; | |
884 | |
885 if (!firsttime && oldvar != spu_gaussvar) sws_freeVec(filter.lumH); | |
886 if (firsttime) { | |
887 filter.lumH = filter.lumV = | |
888 filter.chrH = filter.chrV = sws_getGaussianVec(spu_gaussvar, 3.0); | |
889 sws_normalizeVec(filter.lumH, 1.0); | |
890 firsttime = 0; | |
891 oldvar = spu_gaussvar; | |
892 } | |
893 | |
894 ctx=sws_getContext(sw, sh, PIX_FMT_GRAY8, dw, dh, PIX_FMT_GRAY8, SWS_GAUSS, &filter, NULL, NULL); | |
895 sws_scale(ctx,&s1,&ss,0,sh,&d1,&ds); | |
34786 | 896 for (i=ss*sh-1; i>=0; i--) s2[i] = -s2[i]; |
32456 | 897 sws_scale(ctx,&s2,&ss,0,sh,&d2,&ds); |
34786 | 898 for (i=ds*dh-1; i>=0; i--) d2[i] = -d2[i]; |
32456 | 899 sws_freeContext(ctx); |
900 } | |
901 | |
902 void spudec_draw_scaled(void *me, unsigned int dxs, unsigned int dys, void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)) | |
903 { | |
904 spudec_handle_t *spu = me; | |
905 scale_pixel *table_x; | |
906 scale_pixel *table_y; | |
907 | |
908 if (spudec_visible(spu)) { | |
909 | |
910 // check if only forced subtitles are requested | |
911 if( (spu->forced_subs_only) && !(spu->is_forced_sub) ){ | |
912 return; | |
913 } | |
914 | |
35031 | 915 validate_dimensions(spu, dxs, dys); |
32456 | 916 if (!(spu_aamode&16) && (spu->orig_frame_width == 0 || spu->orig_frame_height == 0 |
917 || (spu->orig_frame_width == dxs && spu->orig_frame_height == dys))) { | |
918 spudec_draw(spu, draw_alpha); | |
919 } | |
920 else { | |
921 if (spu->scaled_frame_width != dxs || spu->scaled_frame_height != dys) { /* Resizing is needed */ | |
922 /* scaled_x = scalex * x / 0x100 | |
923 scaled_y = scaley * y / 0x100 | |
924 order of operations is important because of rounding. */ | |
925 unsigned int scalex = 0x100 * dxs / spu->orig_frame_width; | |
926 unsigned int scaley = 0x100 * dys / spu->orig_frame_height; | |
927 spu->scaled_start_col = spu->start_col * scalex / 0x100; | |
928 spu->scaled_start_row = spu->start_row * scaley / 0x100; | |
929 spu->scaled_width = spu->width * scalex / 0x100; | |
930 spu->scaled_height = spu->height * scaley / 0x100; | |
931 /* Kludge: draw_alpha needs width multiple of 8 */ | |
932 spu->scaled_stride = (spu->scaled_width + 7) & ~7; | |
933 if (spu->scaled_image_size < spu->scaled_stride * spu->scaled_height) { | |
934 if (spu->scaled_image) { | |
935 free(spu->scaled_image); | |
936 spu->scaled_image_size = 0; | |
937 } | |
938 spu->scaled_image = malloc(2 * spu->scaled_stride * spu->scaled_height); | |
939 if (spu->scaled_image) { | |
940 spu->scaled_image_size = spu->scaled_stride * spu->scaled_height; | |
941 spu->scaled_aimage = spu->scaled_image + spu->scaled_image_size; | |
942 } | |
943 } | |
944 if (spu->scaled_image) { | |
945 unsigned int x, y; | |
33143
1cad23803d94
Fix artefacts at right border of scaled bitmap subtitles by clearing the
reimar
parents:
32511
diff
changeset
|
946 // needs to be 0-initialized because draw_alpha draws always a |
1cad23803d94
Fix artefacts at right border of scaled bitmap subtitles by clearing the
reimar
parents:
32511
diff
changeset
|
947 // multiple of 8 pixels. TODO: optimize |
1cad23803d94
Fix artefacts at right border of scaled bitmap subtitles by clearing the
reimar
parents:
32511
diff
changeset
|
948 if (spu->scaled_width & 7) |
1cad23803d94
Fix artefacts at right border of scaled bitmap subtitles by clearing the
reimar
parents:
32511
diff
changeset
|
949 memset(spu->scaled_image, 0, 2 * spu->scaled_image_size); |
32456 | 950 if (spu->scaled_width <= 1 || spu->scaled_height <= 1) { |
951 goto nothing_to_do; | |
952 } | |
953 switch(spu_aamode&15) { | |
954 case 4: | |
955 sws_spu_image(spu->scaled_image, spu->scaled_aimage, | |
956 spu->scaled_width, spu->scaled_height, spu->scaled_stride, | |
957 spu->image, spu->aimage, spu->width, spu->height, spu->stride); | |
958 break; | |
959 case 3: | |
960 table_x = calloc(spu->scaled_width, sizeof(scale_pixel)); | |
961 table_y = calloc(spu->scaled_height, sizeof(scale_pixel)); | |
962 if (!table_x || !table_y) { | |
963 mp_msg(MSGT_SPUDEC, MSGL_FATAL, "Fatal: spudec_draw_scaled: calloc failed\n"); | |
35253
48db1b241757
Try to do something sensible when malloc fails instead
reimar
parents:
35231
diff
changeset
|
964 free(table_x); |
48db1b241757
Try to do something sensible when malloc fails instead
reimar
parents:
35231
diff
changeset
|
965 table_x = NULL; |
48db1b241757
Try to do something sensible when malloc fails instead
reimar
parents:
35231
diff
changeset
|
966 free(table_y); |
48db1b241757
Try to do something sensible when malloc fails instead
reimar
parents:
35231
diff
changeset
|
967 table_y = NULL; |
48db1b241757
Try to do something sensible when malloc fails instead
reimar
parents:
35231
diff
changeset
|
968 break; |
32456 | 969 } |
970 scale_table(0, 0, spu->width - 1, spu->scaled_width - 1, table_x); | |
971 scale_table(0, 0, spu->height - 1, spu->scaled_height - 1, table_y); | |
972 for (y = 0; y < spu->scaled_height; y++) | |
973 for (x = 0; x < spu->scaled_width; x++) | |
974 scale_image(x, y, table_x, table_y, spu); | |
975 free(table_x); | |
976 free(table_y); | |
977 break; | |
978 case 0: | |
979 /* no antialiasing */ | |
980 for (y = 0; y < spu->scaled_height; ++y) { | |
981 int unscaled_y = y * 0x100 / scaley; | |
982 int strides = spu->stride * unscaled_y; | |
983 int scaled_strides = spu->scaled_stride * y; | |
984 for (x = 0; x < spu->scaled_width; ++x) { | |
985 int unscaled_x = x * 0x100 / scalex; | |
986 spu->scaled_image[scaled_strides + x] = spu->image[strides + unscaled_x]; | |
987 spu->scaled_aimage[scaled_strides + x] = spu->aimage[strides + unscaled_x]; | |
988 } | |
989 } | |
990 break; | |
991 case 1: | |
992 { | |
993 /* Intermediate antialiasing. */ | |
994 for (y = 0; y < spu->scaled_height; ++y) { | |
995 const unsigned int unscaled_top = y * spu->orig_frame_height / dys; | |
996 unsigned int unscaled_bottom = (y + 1) * spu->orig_frame_height / dys; | |
997 if (unscaled_bottom >= spu->height) | |
998 unscaled_bottom = spu->height - 1; | |
999 for (x = 0; x < spu->scaled_width; ++x) { | |
1000 const unsigned int unscaled_left = x * spu->orig_frame_width / dxs; | |
1001 unsigned int unscaled_right = (x + 1) * spu->orig_frame_width / dxs; | |
1002 unsigned int color = 0; | |
1003 unsigned int alpha = 0; | |
1004 unsigned int walkx, walky; | |
1005 unsigned int base, tmp; | |
1006 if (unscaled_right >= spu->width) | |
1007 unscaled_right = spu->width - 1; | |
1008 for (walky = unscaled_top; walky <= unscaled_bottom; ++walky) | |
1009 for (walkx = unscaled_left; walkx <= unscaled_right; ++walkx) { | |
1010 base = walky * spu->stride + walkx; | |
1011 tmp = canon_alpha(spu->aimage[base]); | |
1012 alpha += tmp; | |
1013 color += tmp * spu->image[base]; | |
1014 } | |
1015 base = y * spu->scaled_stride + x; | |
1016 spu->scaled_image[base] = alpha ? color / alpha : 0; | |
1017 spu->scaled_aimage[base] = | |
1018 alpha * (1 + unscaled_bottom - unscaled_top) * (1 + unscaled_right - unscaled_left); | |
1019 /* spu->scaled_aimage[base] = | |
1020 alpha * dxs * dys / spu->orig_frame_width / spu->orig_frame_height; */ | |
1021 if (spu->scaled_aimage[base]) { | |
1022 spu->scaled_aimage[base] = 256 - spu->scaled_aimage[base]; | |
1023 if (spu->scaled_aimage[base] + spu->scaled_image[base] > 255) | |
1024 spu->scaled_image[base] = 256 - spu->scaled_aimage[base]; | |
1025 } | |
1026 } | |
1027 } | |
1028 } | |
1029 break; | |
1030 case 2: | |
1031 { | |
1032 /* Best antialiasing. Very slow. */ | |
1033 /* Any pixel (x, y) represents pixels from the original | |
1034 rectangular region comprised between the columns | |
1035 unscaled_y and unscaled_y + 0x100 / scaley and the rows | |
1036 unscaled_x and unscaled_x + 0x100 / scalex | |
1037 | |
1038 The original rectangular region that the scaled pixel | |
1039 represents is cut in 9 rectangular areas like this: | |
1040 | |
1041 +---+-----------------+---+ | |
1042 | 1 | 2 | 3 | | |
1043 +---+-----------------+---+ | |
1044 | | | | | |
1045 | 4 | 5 | 6 | | |
1046 | | | | | |
1047 +---+-----------------+---+ | |
1048 | 7 | 8 | 9 | | |
1049 +---+-----------------+---+ | |
1050 | |
1051 The width of the left column is at most one pixel and | |
1052 it is never null and its right column is at a pixel | |
1053 boundary. The height of the top row is at most one | |
1054 pixel it is never null and its bottom row is at a | |
1055 pixel boundary. The width and height of region 5 are | |
1056 integral values. The width of the right column is | |
1057 what remains and is less than one pixel. The height | |
1058 of the bottom row is what remains and is less than | |
1059 one pixel. | |
1060 | |
1061 The row above 1, 2, 3 is unscaled_y. The row between | |
1062 1, 2, 3 and 4, 5, 6 is top_low_row. The row between 4, | |
1063 5, 6 and 7, 8, 9 is (unsigned int)unscaled_y_bottom. | |
1064 The row beneath 7, 8, 9 is unscaled_y_bottom. | |
1065 | |
1066 The column left of 1, 4, 7 is unscaled_x. The column | |
1067 between 1, 4, 7 and 2, 5, 8 is left_right_column. The | |
1068 column between 2, 5, 8 and 3, 6, 9 is (unsigned | |
1069 int)unscaled_x_right. The column right of 3, 6, 9 is | |
1070 unscaled_x_right. */ | |
1071 const double inv_scalex = (double) 0x100 / scalex; | |
1072 const double inv_scaley = (double) 0x100 / scaley; | |
1073 for (y = 0; y < spu->scaled_height; ++y) { | |
1074 const double unscaled_y = y * inv_scaley; | |
1075 const double unscaled_y_bottom = unscaled_y + inv_scaley; | |
1076 const unsigned int top_low_row = FFMIN(unscaled_y_bottom, unscaled_y + 1.0); | |
1077 const double top = top_low_row - unscaled_y; | |
1078 const unsigned int height = unscaled_y_bottom > top_low_row | |
1079 ? (unsigned int) unscaled_y_bottom - top_low_row | |
1080 : 0; | |
1081 const double bottom = unscaled_y_bottom > top_low_row | |
1082 ? unscaled_y_bottom - floor(unscaled_y_bottom) | |
1083 : 0.0; | |
1084 for (x = 0; x < spu->scaled_width; ++x) { | |
1085 const double unscaled_x = x * inv_scalex; | |
1086 const double unscaled_x_right = unscaled_x + inv_scalex; | |
1087 const unsigned int left_right_column = FFMIN(unscaled_x_right, unscaled_x + 1.0); | |
1088 const double left = left_right_column - unscaled_x; | |
1089 const unsigned int width = unscaled_x_right > left_right_column | |
1090 ? (unsigned int) unscaled_x_right - left_right_column | |
1091 : 0; | |
1092 const double right = unscaled_x_right > left_right_column | |
1093 ? unscaled_x_right - floor(unscaled_x_right) | |
1094 : 0.0; | |
1095 double color = 0.0; | |
1096 double alpha = 0.0; | |
1097 double tmp; | |
1098 unsigned int base; | |
1099 /* Now use these informations to compute a good alpha, | |
1100 and lightness. The sum is on each of the 9 | |
1101 region's surface and alpha and lightness. | |
1102 | |
1103 transformed alpha = sum(surface * alpha) / sum(surface) | |
1104 transformed color = sum(surface * alpha * color) / sum(surface * alpha) | |
1105 */ | |
1106 /* 1: top left part */ | |
1107 base = spu->stride * (unsigned int) unscaled_y; | |
1108 tmp = left * top * canon_alpha(spu->aimage[base + (unsigned int) unscaled_x]); | |
1109 alpha += tmp; | |
1110 color += tmp * spu->image[base + (unsigned int) unscaled_x]; | |
1111 /* 2: top center part */ | |
1112 if (width > 0) { | |
1113 unsigned int walkx; | |
1114 for (walkx = left_right_column; walkx < (unsigned int) unscaled_x_right; ++walkx) { | |
1115 base = spu->stride * (unsigned int) unscaled_y + walkx; | |
1116 tmp = /* 1.0 * */ top * canon_alpha(spu->aimage[base]); | |
1117 alpha += tmp; | |
1118 color += tmp * spu->image[base]; | |
1119 } | |
1120 } | |
1121 /* 3: top right part */ | |
1122 if (right > 0.0) { | |
1123 base = spu->stride * (unsigned int) unscaled_y + (unsigned int) unscaled_x_right; | |
1124 tmp = right * top * canon_alpha(spu->aimage[base]); | |
1125 alpha += tmp; | |
1126 color += tmp * spu->image[base]; | |
1127 } | |
1128 /* 4: center left part */ | |
1129 if (height > 0) { | |
1130 unsigned int walky; | |
1131 for (walky = top_low_row; walky < (unsigned int) unscaled_y_bottom; ++walky) { | |
1132 base = spu->stride * walky + (unsigned int) unscaled_x; | |
1133 tmp = left /* * 1.0 */ * canon_alpha(spu->aimage[base]); | |
1134 alpha += tmp; | |
1135 color += tmp * spu->image[base]; | |
1136 } | |
1137 } | |
1138 /* 5: center part */ | |
1139 if (width > 0 && height > 0) { | |
1140 unsigned int walky; | |
1141 for (walky = top_low_row; walky < (unsigned int) unscaled_y_bottom; ++walky) { | |
1142 unsigned int walkx; | |
1143 base = spu->stride * walky; | |
1144 for (walkx = left_right_column; walkx < (unsigned int) unscaled_x_right; ++walkx) { | |
1145 tmp = /* 1.0 * 1.0 * */ canon_alpha(spu->aimage[base + walkx]); | |
1146 alpha += tmp; | |
1147 color += tmp * spu->image[base + walkx]; | |
1148 } | |
1149 } | |
1150 } | |
1151 /* 6: center right part */ | |
1152 if (right > 0.0 && height > 0) { | |
1153 unsigned int walky; | |
1154 for (walky = top_low_row; walky < (unsigned int) unscaled_y_bottom; ++walky) { | |
1155 base = spu->stride * walky + (unsigned int) unscaled_x_right; | |
1156 tmp = right /* * 1.0 */ * canon_alpha(spu->aimage[base]); | |
1157 alpha += tmp; | |
1158 color += tmp * spu->image[base]; | |
1159 } | |
1160 } | |
1161 /* 7: bottom left part */ | |
1162 if (bottom > 0.0) { | |
1163 base = spu->stride * (unsigned int) unscaled_y_bottom + (unsigned int) unscaled_x; | |
1164 tmp = left * bottom * canon_alpha(spu->aimage[base]); | |
1165 alpha += tmp; | |
1166 color += tmp * spu->image[base]; | |
1167 } | |
1168 /* 8: bottom center part */ | |
1169 if (width > 0 && bottom > 0.0) { | |
1170 unsigned int walkx; | |
1171 base = spu->stride * (unsigned int) unscaled_y_bottom; | |
1172 for (walkx = left_right_column; walkx < (unsigned int) unscaled_x_right; ++walkx) { | |
1173 tmp = /* 1.0 * */ bottom * canon_alpha(spu->aimage[base + walkx]); | |
1174 alpha += tmp; | |
1175 color += tmp * spu->image[base + walkx]; | |
1176 } | |
1177 } | |
1178 /* 9: bottom right part */ | |
1179 if (right > 0.0 && bottom > 0.0) { | |
1180 base = spu->stride * (unsigned int) unscaled_y_bottom + (unsigned int) unscaled_x_right; | |
1181 tmp = right * bottom * canon_alpha(spu->aimage[base]); | |
1182 alpha += tmp; | |
1183 color += tmp * spu->image[base]; | |
1184 } | |
1185 /* Finally mix these transparency and brightness information suitably */ | |
1186 base = spu->scaled_stride * y + x; | |
1187 spu->scaled_image[base] = alpha > 0 ? color / alpha : 0; | |
1188 spu->scaled_aimage[base] = alpha * scalex * scaley / 0x10000; | |
1189 if (spu->scaled_aimage[base]) { | |
1190 spu->scaled_aimage[base] = 256 - spu->scaled_aimage[base]; | |
1191 if (spu->scaled_aimage[base] + spu->scaled_image[base] > 255) | |
1192 spu->scaled_image[base] = 256 - spu->scaled_aimage[base]; | |
1193 } | |
1194 } | |
1195 } | |
1196 } | |
1197 } | |
1198 nothing_to_do: | |
1199 /* Kludge: draw_alpha needs width multiple of 8. */ | |
1200 if (spu->scaled_width < spu->scaled_stride) | |
1201 for (y = 0; y < spu->scaled_height; ++y) { | |
1202 memset(spu->scaled_aimage + y * spu->scaled_stride + spu->scaled_width, 0, | |
1203 spu->scaled_stride - spu->scaled_width); | |
1204 } | |
1205 spu->scaled_frame_width = dxs; | |
1206 spu->scaled_frame_height = dys; | |
1207 } | |
1208 } | |
1209 if (spu->scaled_image){ | |
1210 switch (spu_alignment) { | |
1211 case 0: | |
1212 spu->scaled_start_row = dys*sub_pos/100; | |
1213 if (spu->scaled_start_row + spu->scaled_height > dys) | |
1214 spu->scaled_start_row = dys - spu->scaled_height; | |
1215 break; | |
1216 case 1: | |
1217 spu->scaled_start_row = dys*sub_pos/100 - spu->scaled_height/2; | |
1218 if (sub_pos >= 50 && spu->scaled_start_row + spu->scaled_height > dys) | |
1219 spu->scaled_start_row = dys - spu->scaled_height; | |
1220 break; | |
1221 case 2: | |
1222 spu->scaled_start_row = dys*sub_pos/100 - spu->scaled_height; | |
1223 break; | |
1224 } | |
1225 draw_alpha(spu->scaled_start_col, spu->scaled_start_row, spu->scaled_width, spu->scaled_height, | |
1226 spu->scaled_image, spu->scaled_aimage, spu->scaled_stride); | |
1227 spu->spu_changed = 0; | |
1228 } | |
1229 } | |
1230 } | |
1231 else | |
1232 { | |
1233 mp_msg(MSGT_SPUDEC,MSGL_DBG2,"SPU not displayed: start_pts=%d end_pts=%d now_pts=%d\n", | |
1234 spu->start_pts, spu->end_pts, spu->now_pts); | |
1235 } | |
1236 } | |
1237 | |
1238 void spudec_update_palette(void * this, unsigned int *palette) | |
1239 { | |
1240 spudec_handle_t *spu = this; | |
1241 if (spu && palette) { | |
1242 memcpy(spu->global_palette, palette, sizeof(spu->global_palette)); | |
1243 if(spu->hw_spu) | |
1244 spu->hw_spu->control(VOCTRL_SET_SPU_PALETTE,spu->global_palette); | |
1245 } | |
1246 } | |
1247 | |
1248 void spudec_set_font_factor(void * this, double factor) | |
1249 { | |
1250 spudec_handle_t *spu = this; | |
1251 spu->font_start_level = (int)(0xF0-(0xE0*factor)); | |
1252 } | |
1253 | |
1254 static void spudec_parse_extradata(spudec_handle_t *this, | |
1255 uint8_t *extradata, int extradata_len) | |
1256 { | |
1257 uint8_t *buffer, *ptr; | |
1258 unsigned int *pal = this->global_palette, *cuspal = this->cuspal; | |
1259 unsigned int tridx; | |
1260 int i; | |
1261 | |
1262 if (extradata_len == 16*4) { | |
1263 for (i=0; i<16; i++) | |
1264 pal[i] = AV_RB32(extradata + i*4); | |
1265 this->auto_palette = 0; | |
1266 return; | |
1267 } | |
1268 | |
1269 if (!(ptr = buffer = malloc(extradata_len+1))) | |
1270 return; | |
1271 memcpy(buffer, extradata, extradata_len); | |
1272 buffer[extradata_len] = 0; | |
1273 | |
1274 do { | |
1275 if (*ptr == '#') | |
1276 continue; | |
1277 if (!strncmp(ptr, "size: ", 6)) | |
1278 sscanf(ptr + 6, "%dx%d", &this->orig_frame_width, &this->orig_frame_height); | |
1279 if (!strncmp(ptr, "palette: ", 9) && | |
1280 sscanf(ptr + 9, "%x, %x, %x, %x, %x, %x, %x, %x, " | |
1281 "%x, %x, %x, %x, %x, %x, %x, %x", | |
1282 &pal[ 0], &pal[ 1], &pal[ 2], &pal[ 3], | |
1283 &pal[ 4], &pal[ 5], &pal[ 6], &pal[ 7], | |
1284 &pal[ 8], &pal[ 9], &pal[10], &pal[11], | |
1285 &pal[12], &pal[13], &pal[14], &pal[15]) == 16) { | |
1286 for (i=0; i<16; i++) | |
1287 pal[i] = vobsub_palette_to_yuv(pal[i]); | |
1288 this->auto_palette = 0; | |
1289 } | |
1290 if (!strncasecmp(ptr, "forced subs: on", 15)) | |
1291 this->forced_subs_only = 1; | |
1292 if (!strncmp(ptr, "custom colors: ON, tridx: ", 26) && | |
1293 sscanf(ptr + 26, "%x, colors: %x, %x, %x, %x", | |
1294 &tridx, cuspal+0, cuspal+1, cuspal+2, cuspal+3) == 5) { | |
1295 for (i=0; i<4; i++) { | |
1296 cuspal[i] = vobsub_rgb_to_yuv(cuspal[i]); | |
1297 if (tridx & (1 << (12-4*i))) | |
1298 cuspal[i] |= 1 << 31; | |
1299 } | |
1300 this->custom = 1; | |
1301 } | |
1302 } while ((ptr=strchr(ptr,'\n')) && *++ptr); | |
1303 | |
1304 free(buffer); | |
1305 } | |
1306 | |
1307 void *spudec_new_scaled(unsigned int *palette, unsigned int frame_width, unsigned int frame_height, uint8_t *extradata, int extradata_len) | |
1308 { | |
1309 spudec_handle_t *this = calloc(1, sizeof(spudec_handle_t)); | |
1310 if (this){ | |
1311 this->orig_frame_height = frame_height; | |
1312 this->orig_frame_width = frame_width; | |
1313 // set up palette: | |
1314 if (palette) | |
1315 memcpy(this->global_palette, palette, sizeof(this->global_palette)); | |
1316 else | |
1317 this->auto_palette = 1; | |
1318 if (extradata) | |
1319 spudec_parse_extradata(this, extradata, extradata_len); | |
1320 /* XXX Although the video frame is some size, the SPU frame is | |
1321 always maximum size i.e. 720 wide and 576 or 480 high */ | |
1322 // For HD files in MKV the VobSub resolution can be higher though, | |
1323 // see largeres_vobsub.mkv | |
1324 if (this->orig_frame_width <= 720 && this->orig_frame_height <= 576) { | |
1325 this->orig_frame_width = 720; | |
1326 if (this->orig_frame_height == 480 || this->orig_frame_height == 240) | |
1327 this->orig_frame_height = 480; | |
1328 else | |
1329 this->orig_frame_height = 576; | |
1330 } | |
1331 } | |
1332 else | |
1333 mp_msg(MSGT_SPUDEC,MSGL_FATAL, "FATAL: spudec_init: calloc"); | |
1334 return this; | |
1335 } | |
1336 | |
1337 void *spudec_new(unsigned int *palette) | |
1338 { | |
1339 return spudec_new_scaled(palette, 0, 0, NULL, 0); | |
1340 } | |
1341 | |
1342 void spudec_free(void *this) | |
1343 { | |
1344 spudec_handle_t *spu = this; | |
1345 if (spu) { | |
1346 while (spu->queue_head) | |
1347 spudec_free_packet(spudec_dequeue_packet(spu)); | |
1348 free(spu->packet); | |
1349 spu->packet = NULL; | |
1350 free(spu->scaled_image); | |
1351 spu->scaled_image = NULL; | |
1352 free(spu->image); | |
1353 spu->image = NULL; | |
1354 spu->aimage = NULL; | |
1355 free(spu->pal_image); | |
1356 spu->pal_image = NULL; | |
1357 spu->image_size = 0; | |
1358 spu->pal_width = spu->pal_height = 0; | |
1359 free(spu); | |
1360 } | |
1361 } | |
1362 | |
1363 void spudec_set_hw_spu(void *this, const vo_functions_t *hw_spu) | |
1364 { | |
1365 spudec_handle_t *spu = this; | |
1366 if (!spu) | |
1367 return; | |
1368 spu->hw_spu = hw_spu; | |
1369 hw_spu->control(VOCTRL_SET_SPU_PALETTE,spu->global_palette); | |
1370 } | |
1371 | |
1372 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly | |
1373 | |
34784 | 1374 packet_t *spudec_packet_create(int x, int y, int w, int h) |
32456 | 1375 { |
1376 packet_t *packet; | |
1377 int stride = (w + 7) & ~7; | |
1378 if ((unsigned)w >= 0x8000 || (unsigned)h > 0x4000) | |
34784 | 1379 return NULL; |
32456 | 1380 packet = calloc(1, sizeof(packet_t)); |
1381 packet->is_decoded = 1; | |
1382 packet->width = w; | |
1383 packet->height = h; | |
1384 packet->stride = stride; | |
1385 packet->start_col = x; | |
1386 packet->start_row = y; | |
1387 packet->data_len = 2 * stride * h; | |
1388 if (packet->data_len) { // size 0 is a special "clear" packet | |
34784 | 1389 packet->packet = malloc(packet->data_len); |
1390 if (!packet->packet) { | |
1391 free(packet); | |
1392 packet = NULL; | |
1393 } | |
32456 | 1394 } |
34784 | 1395 return packet; |
1396 } | |
1397 | |
1398 void spudec_packet_clear(packet_t *packet) | |
1399 { | |
1400 /* clear alpha and value, as value is premultiplied */ | |
1401 memset(packet->packet, 0, packet->data_len); | |
1402 } | |
1403 | |
1404 void spudec_packet_fill(packet_t *packet, | |
1405 const uint8_t *pal_img, int pal_stride, | |
1406 const void *palette, | |
1407 int x, int y, int w, int h) | |
1408 { | |
1409 const uint32_t *pal = palette; | |
1410 uint8_t *img = packet->packet + x + y * packet->stride; | |
1411 uint8_t *aimg = img + packet->stride * packet->height; | |
1412 int i; | |
1413 uint16_t g8a8_pal[256]; | |
1414 | |
1415 for (i = 0; i < 256; i++) { | |
1416 uint32_t pixel = pal[i]; | |
1417 int alpha = pixel >> 24; | |
1418 int gray = (((pixel & 0x000000ff) >> 0) + | |
1419 ((pixel & 0x0000ff00) >> 7) + | |
1420 ((pixel & 0x00ff0000) >> 16)) >> 2; | |
1421 gray = FFMIN(gray, alpha); | |
1422 g8a8_pal[i] = (-alpha << 8) | gray; | |
1423 } | |
1424 pal2gray_alpha(g8a8_pal, pal_img, pal_stride, | |
37172 | 1425 img, aimg, packet->stride, w, h, 1); |
34784 | 1426 } |
1427 | |
1428 void spudec_packet_send(void *spu, packet_t *packet, double pts, double endpts) | |
1429 { | |
32456 | 1430 packet->start_pts = 0; |
1431 packet->end_pts = 0x7fffffff; | |
37161
6d654ac9201e
Better handling of out-of-range subtitle timestamps.
reimar
parents:
36767
diff
changeset
|
1432 if (pts != MP_NOPTS_VALUE && pts < 0xffffffffu / 90000) |
32456 | 1433 packet->start_pts = pts * 90000; |
37161
6d654ac9201e
Better handling of out-of-range subtitle timestamps.
reimar
parents:
36767
diff
changeset
|
1434 if (endpts != MP_NOPTS_VALUE && endpts < 0xffffffffu / 90000) |
32456 | 1435 packet->end_pts = endpts * 90000; |
1436 spudec_queue_packet(spu, packet); | |
1437 } | |
34784 | 1438 |
1439 /** | |
1440 * palette must contain at least 256 32-bit entries, otherwise crashes | |
1441 * are possible | |
1442 */ | |
1443 void spudec_set_paletted(void *spu, const uint8_t *pal_img, int pal_stride, | |
1444 const void *palette, | |
1445 int x, int y, int w, int h, | |
1446 double pts, double endpts) | |
1447 { | |
1448 packet_t *packet = spudec_packet_create(x, y, w, h); | |
1449 if (!packet) | |
1450 return; | |
1451 if (packet->data_len) // size 0 is a special "clear" packet | |
1452 spudec_packet_fill(packet, pal_img, pal_stride, palette, 0, 0, w, h); | |
1453 spudec_packet_send(spu, packet, pts, endpts); | |
1454 } |