Mercurial > mplayer.hg
annotate sub/sub_cc.c @ 34764:9e81d9667789
Add Spanish entries to desktop file.
Translation by Juan A. Javierre, jjavierre telefonica net.
author | ib |
---|---|
date | Wed, 11 Apr 2012 18:44:02 +0000 |
parents | 02a03e00341e |
children | dd77d5a292df |
rev | line source |
---|---|
32458 | 1 /* |
2 * decoder for Closed Captions | |
3 * | |
4 * This decoder relies on MPlayer's OSD to display subtitles. | |
5 * Be warned that decoding is somewhat preliminary, though it basically works. | |
6 * | |
7 * Most notably, only the text information is decoded as of now, discarding | |
8 * color, background and position info (see source below). | |
9 * | |
10 * uses source from the xine closed captions decoder | |
11 * | |
12 * Copyright (C) 2002 Matteo Giani | |
13 * | |
14 * This file is part of MPlayer. | |
15 * | |
16 * MPlayer is free software; you can redistribute it and/or modify | |
17 * it under the terms of the GNU General Public License as published by | |
18 * the Free Software Foundation; either version 2 of the License, or | |
19 * (at your option) any later version. | |
20 * | |
21 * MPlayer is distributed in the hope that it will be useful, | |
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 * GNU General Public License for more details. | |
25 * | |
26 * You should have received a copy of the GNU General Public License along | |
27 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
29 */ | |
30 | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 | |
35 #include "config.h" | |
34756 | 36 #include "mp_msg.h" |
32464
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32458
diff
changeset
|
37 #include "sub_cc.h" |
32458 | 38 |
32464
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32458
diff
changeset
|
39 #include "subreader.h" |
32458 | 40 |
41 #include "libvo/video_out.h" | |
32467 | 42 #include "sub.h" |
32458 | 43 |
34756 | 44 #include "libavutil/avutil.h" |
45 | |
32458 | 46 |
47 #define CC_MAX_LINE_LENGTH 64 | |
48 | |
49 static char chartbl[128]; | |
50 | |
51 static subtitle buf1,buf2; | |
52 static subtitle *fb,*bb; | |
53 | |
54 static unsigned int cursor_pos=0; | |
55 | |
56 static int initialized=0; | |
57 | |
58 #define CC_ROLLON 1 | |
59 #define CC_ROLLUP 2 | |
60 | |
61 static int cc_mode=CC_ROLLON; | |
62 static int cc_lines=4; ///< number of visible rows in CC roll-up mode, not used in CC roll-on mode | |
63 | |
64 static void build_char_table(void) | |
65 { | |
66 int i; | |
67 /* first the normal ASCII codes */ | |
68 for (i = 0; i < 128; i++) | |
69 chartbl[i] = (char) i; | |
70 /* now the special codes */ | |
34702 | 71 chartbl[0x2a] = 0xe1; /* Latin Small Letter A with acute */ |
72 chartbl[0x5c] = 0xe9; /* Latin Small Letter E with acute */ | |
73 chartbl[0x5e] = 0xed; /* Latin Small Letter I with acute */ | |
74 chartbl[0x5f] = 0xf3; /* Latin Small Letter O with acute */ | |
75 chartbl[0x60] = 0xfa; /* Latin Small Letter U with acute */ | |
76 chartbl[0x7b] = 0xe7; /* Latin Small Letter C with cedilla */ | |
77 chartbl[0x7c] = 0xf7; /* Division sign */ | |
78 chartbl[0x7d] = 0xd1; /* Latin Capital letter N with tilde */ | |
79 chartbl[0x7e] = 0xf1; /* Latin Small Letter N with tilde */ | |
80 chartbl[0x7f] = 0xa4; /* Currency sign FIXME: this should be a solid block */ | |
32458 | 81 } |
82 | |
83 static void clear_buffer(subtitle *buf) | |
84 { | |
85 int i; | |
86 buf->lines=0; | |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
87 for (i = 0; i < SUB_MAX_TEXT; i++) { |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
88 free(buf->text[i]); |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
89 buf->text[i] = NULL; |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
90 } |
32458 | 91 } |
92 | |
93 | |
94 /** | |
95 \brief scroll buffer one line up | |
96 \param buf buffer to scroll | |
97 */ | |
98 static void scroll_buffer(subtitle* buf) | |
99 { | |
100 int i; | |
101 | |
102 while(buf->lines > cc_lines) | |
103 { | |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32467
diff
changeset
|
104 free(buf->text[0]); |
32458 | 105 |
32512 | 106 for(i = 0; i < buf->lines - 1; i++) buf->text[i] = buf->text[i+1]; |
32458 | 107 |
108 buf->text[buf->lines-1] = NULL; | |
109 buf->lines--; | |
110 } | |
111 } | |
112 | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
113 static int channel; |
32458 | 114 |
115 void subcc_init(void) | |
116 { | |
117 int i; | |
118 //printf("subcc_init(): initing...\n"); | |
119 build_char_table(); | |
120 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;} | |
121 buf1.lines=buf2.lines=0; | |
122 fb=&buf1; | |
123 bb=&buf2; | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
124 channel = -1; |
32458 | 125 |
126 initialized=1; | |
127 } | |
128 | |
34761 | 129 void subcc_reset(void) |
130 { | |
131 if (!initialized) | |
132 return; | |
133 clear_buffer(&buf1); | |
134 clear_buffer(&buf2); | |
135 } | |
32458 | 136 |
137 static void display_buffer(subtitle *buf) | |
138 { | |
139 vo_sub = buf; | |
140 vo_osd_changed(OSDTYPE_SUBTITLE); | |
141 } | |
142 | |
143 | |
144 static void append_char(char c) | |
145 { | |
146 if(!bb->lines) {bb->lines++; cursor_pos=0;} | |
147 if(bb->text[bb->lines - 1]==NULL) | |
148 { | |
32513 | 149 bb->text[bb->lines - 1] = calloc(1, CC_MAX_LINE_LENGTH); |
32458 | 150 cursor_pos=0; |
151 } | |
152 | |
153 if(c=='\n') | |
154 { | |
155 if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT) | |
156 { | |
157 bb->lines++;cursor_pos=0; | |
158 if(cc_mode==CC_ROLLUP){ //Carriage return - scroll buffer one line up | |
159 bb->text[bb->lines - 1]=calloc(1, CC_MAX_LINE_LENGTH); | |
160 scroll_buffer(bb); | |
161 } | |
162 } | |
163 } | |
164 else | |
165 { | |
166 if(cursor_pos==CC_MAX_LINE_LENGTH-1) | |
167 { | |
168 fprintf(stderr,"CC: append_char() reached CC_MAX_LINE_LENGTH!\n"); | |
169 return; | |
170 } | |
171 bb->text[bb->lines - 1][cursor_pos++]=c; | |
172 } | |
173 //In CC roll-up mode data should be shown immediately | |
174 if(cc_mode==CC_ROLLUP) display_buffer(bb); | |
175 } | |
176 | |
177 | |
178 static void swap_buffers(void) | |
179 { | |
180 subtitle *foo; | |
181 foo=fb; | |
182 fb=bb; | |
183 bb=foo; | |
184 } | |
185 | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
186 static int selected_channel(void) |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
187 { |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
188 return subcc_enabled - 1; |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
189 } |
32458 | 190 |
191 static void cc_decode_EIA608(unsigned short int data) | |
192 { | |
193 | |
194 static unsigned short int lastcode=0x0000; | |
32566 | 195 uint8_t c1 = data & 0x7f; |
196 uint8_t c2 = (data >> 8) & 0x7f; | |
32458 | 197 |
198 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */ | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
199 if (channel != (selected_channel() & 1)) |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
200 return; |
32458 | 201 append_char(chartbl[c1]); |
202 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/ | |
203 append_char(chartbl[c2]); | |
204 } | |
205 else if (c1 & 0x10) // control code / special char | |
206 { | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
207 channel = (c1 & 0x08) >> 3; |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
208 if (channel != (selected_channel() & 1)) |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
209 return; |
32458 | 210 c1&=~0x08; |
211 if(data!=lastcode) | |
212 { | |
213 if(c2 & 0x40) { /*PAC, Preamble Address Code */ | |
214 append_char('\n'); /*FIXME properly interpret PACs*/ | |
215 } | |
216 else | |
217 switch(c1) | |
218 { | |
219 case 0x10: break; // ext attribute | |
220 case 0x11: | |
221 if((c2 & 0x30)==0x30) | |
222 { | |
223 //printf("[debug]:Special char (ignored)\n"); | |
224 /*cc_decode_special_char()*/; | |
225 } | |
226 else if (c2 & 0x20) | |
227 { | |
228 //printf("[debug]: midrow_attr (ignored)\n"); | |
229 /*cc_decode_midrow_attr()*/; | |
230 } | |
231 break; | |
232 case 0x14: | |
233 switch(c2) | |
234 { | |
235 case 0x00: //CC roll-on mode | |
236 cc_mode=CC_ROLLON; | |
237 break; | |
238 case 0x25: //CC roll-up, 2 rows | |
239 case 0x26: //CC roll-up, 3 rows | |
240 case 0x27: //CC roll-up, 4 rows | |
241 cc_lines=c2-0x23; | |
242 cc_mode=CC_ROLLUP; | |
243 break; | |
244 case 0x2C: display_buffer(NULL); //EDM | |
245 clear_buffer(fb); break; | |
246 case 0x2d: append_char('\n'); //carriage return | |
247 break; | |
248 case 0x2e: clear_buffer(bb); //ENM | |
249 break; | |
250 case 0x2f: swap_buffers(); //Swap buffers | |
251 display_buffer(fb); | |
252 clear_buffer(bb); | |
253 break; | |
254 } | |
255 break; | |
256 case 0x17: | |
257 if( c2>=0x21 && c2<=0x23) //TAB | |
258 { | |
259 break; | |
260 } | |
261 } | |
262 } | |
263 } | |
264 lastcode=data; | |
265 } | |
266 | |
32566 | 267 static void subcc_decode(const uint8_t *inputbuffer, unsigned int inputlength) |
32458 | 268 { |
269 /* The first number may denote a channel number. I don't have the | |
270 * EIA-708 standard, so it is hard to say. | |
271 * From what I could figure out so far, the general format seems to be: | |
272 * | |
273 * repeat | |
274 * | |
275 * 0xfe starts 2 byte sequence of unknown purpose. It might denote | |
32522 | 276 * field #2 in line 21 of the VBI. |
277 * Treating it identical of 0xff fixes | |
278 * http://samples.mplayerhq.hu/MPEG-VOB/ClosedCaptions/Starship_Troopers.vob | |
32458 | 279 * |
280 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI. | |
281 * Followed by a 3-code triplet that starts either with 0xff or | |
282 * 0xfe. In either case, the following triplet needs to be ignored | |
283 * for line 21, field 1. | |
284 * | |
285 * 0x00 is padding, followed by 2 more 0x00. | |
286 * | |
287 * 0x01 always seems to appear at the beginning, always seems to | |
288 * be followed by 0xf8, 8-bit number. | |
289 * The lower 7 bits of this 8-bit number seem to denote the | |
290 * number of code triplets that follow. | |
291 * The most significant bit denotes whether the Line 21 field 1 | |
292 * captioning information is at odd or even triplet offsets from this | |
293 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets. | |
294 * | |
295 * Most captions are encoded with odd offsets, so this is what we | |
296 * will assume. | |
297 * | |
298 * until end of packet | |
299 */ | |
32566 | 300 const uint8_t *current = inputbuffer; |
32458 | 301 unsigned int curbytes = 0; |
32566 | 302 uint8_t data1, data2; |
303 uint8_t cc_code; | |
32458 | 304 int odd_offset = 1; |
305 | |
306 while (curbytes < inputlength) { | |
32512 | 307 cc_code = current[0]; |
32458 | 308 |
309 if (inputlength - curbytes < 2) { | |
310 #ifdef LOG_DEBUG | |
311 fprintf(stderr, "Not enough data for 2-byte CC encoding\n"); | |
312 #endif | |
313 break; | |
314 } | |
315 | |
32512 | 316 data1 = current[1]; |
317 data2 = current[2]; | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
318 current += 3; curbytes += 3; |
32458 | 319 |
320 switch (cc_code) { | |
321 case 0xfe: | |
322 case 0xff: | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
323 odd_offset ^= 1; |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
324 if (odd_offset != selected_channel() >> 1) |
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
325 break; |
32458 | 326 /* expect EIA-608 CC1/CC2 encoding */ |
327 // FIXME check parity! | |
328 // Parity check omitted assuming we are reading from a DVD and therefore | |
329 // we should encounter no "transmission errors". | |
330 cc_decode_EIA608(data1 | (data2 << 8)); | |
331 break; | |
332 | |
333 case 0x00: | |
334 /* This seems to be just padding */ | |
335 break; | |
336 | |
337 case 0x01: | |
32520
5e062dc4a04d
Add code to allow selecting the Close Captioning channel.
reimar
parents:
32513
diff
changeset
|
338 odd_offset = data2 >> 7; |
32458 | 339 break; |
340 | |
341 default: | |
342 //#ifdef LOG_DEBUG | |
343 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code); | |
344 //#endif | |
345 break; | |
346 } | |
347 } | |
348 } | |
349 | |
34756 | 350 static const uint8_t mov_cc_signature_1[] = {0, 0, 0, 0xa, 'c', 'd', 'a', 't'}; |
351 static const uint8_t mov_cc_signature_2[] = {0, 0, 0, 0xa, 'c', 'd', 't', '2'}; | |
352 /** | |
353 * MOV uses a vastly more verbose representation for EIA 608 CC data than DVDs. | |
354 * This function handles that case. | |
355 */ | |
356 static void mov_subcc_decode(const uint8_t *data, unsigned len) | |
357 { | |
358 while (len >= 10) { | |
359 int channel = -1; | |
360 if (memcmp(data, mov_cc_signature_1, sizeof(mov_cc_signature_1)) == 0) { | |
361 channel = 0; | |
362 } else if (memcmp(data, mov_cc_signature_2, sizeof(mov_cc_signature_2)) == 0) { | |
363 channel = 1; | |
364 } else { | |
365 mp_msg(MSGT_OSD, MSGL_V, "Unknown MOV 608 CC formatting\n"); | |
366 data++; | |
367 len--; | |
368 continue; | |
369 } | |
370 if (channel == selected_channel() >> 1) | |
371 cc_decode_EIA608(data[8] | (data[9] << 8)); | |
372 data += 10; | |
373 len -= 10; | |
374 } | |
375 } | |
32458 | 376 |
32566 | 377 void subcc_process_data(const uint8_t *inputdata, unsigned int len) |
32458 | 378 { |
34756 | 379 int mov_mode = len >= 10 && |
380 memcmp(inputdata, mov_cc_signature_1, sizeof(mov_cc_signature_1)) == 0; | |
32458 | 381 if(!subcc_enabled) return; |
382 if(!initialized) subcc_init(); | |
383 | |
34756 | 384 if (mov_mode) { |
385 mov_subcc_decode(inputdata, len); | |
386 return; | |
387 } | |
32458 | 388 subcc_decode(inputdata, len); |
389 } | |
34756 | 390 |
391 /** | |
34759 | 392 * This processes CC captions in the format as found in ATSC broadcasts. |
34756 | 393 * Like DVD CC it is stored inside the MPEG-frame userdata, but with two |
394 * differences: | |
395 * 1) It starts with "GA" instead of "CC" | |
396 * 2) It _must_ be reordered in the way the decoder reorders the video frames | |
397 * The latter makes things difficult and is the reason why there is no support | |
398 * for this yet beyond this function. | |
399 */ | |
400 void subcc_process_eia708(const uint8_t *data, int len) | |
401 { | |
402 int cc_count; | |
403 if (!subcc_enabled) | |
404 return; | |
405 if (!initialized) | |
406 subcc_init(); | |
407 if (len <= 5) | |
408 return; | |
409 if (data[0] != '9' || data[1] != '4' || data[2] != 3) { | |
410 mp_msg(MSGT_OSD, MSGL_ERR, "Unknown ATSC CC type " | |
411 "0x%"PRIx8" 0x%"PRIx8" 0x%"PRIx8"\n", | |
412 data[0], data[1], data[2]); | |
413 return; | |
414 } | |
415 // process_cc_data_flag | |
416 if (!(data[3] & 0x40)) | |
417 return; | |
418 cc_count = data[3] & 0x1f; | |
419 data += 5; | |
420 len -= 5; | |
421 cc_count = FFMIN(cc_count, len / 3); | |
422 while (cc_count--) { | |
423 // EAI-608 data | |
424 if ((data[0] & 0xfe) == 0xfc && (data[0] & 1) == selected_channel() >> 1) | |
425 cc_decode_EIA608(data[1] | (data[2] << 8)); | |
426 data += 3; | |
427 } | |
428 } |