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