2 * Use with CVS JOCR/GOCR.
4 * You will have to change 'vobsub_id' value if you want another subtitle than number 0.
6 * HINT: you can view the subtitle that is being decoded with "display subtitle-*.pgm"
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* Make sure this accesses the CVS version of JOCR/GOCR */
24 #define GOCR_PROGRAM "gocr"
31 #include <sys/types.h>
33 #include "libvo/video_out.h"
38 char *mplayer_version
;
40 /* XXX Kludge ahead, this MUST be the same as the definitions found in ../spudec.c */
41 typedef struct packet_t packet_t
;
43 unsigned char *packet
;
44 unsigned int palette
[4];
45 unsigned int alpha
[4];
46 unsigned int control_start
; /* index of start of control data */
47 unsigned int current_nibble
[2]; /* next data nibble (4 bits) to be
48 processed (for RLE decoding) for
50 int deinterlace_oddness
; /* 0 or 1, index into current_nibble */
51 unsigned int start_col
, end_col
;
52 unsigned int start_row
, end_row
;
53 unsigned int width
, height
, stride
;
54 unsigned int start_pts
, end_pts
;
60 unsigned int global_palette
[16];
61 unsigned int orig_frame_width
, orig_frame_height
;
62 unsigned char* packet
;
63 size_t packet_reserve
; /* size of the memory pointed to by packet */
64 unsigned int packet_offset
; /* end of the currently assembled fragment */
65 unsigned int packet_size
; /* size of the packet once all fragments are assembled */
66 unsigned int packet_pts
; /* PTS for this packet */
67 unsigned int palette
[4];
68 unsigned int alpha
[4];
69 unsigned int cuspal
[4];
72 unsigned int start_pts
, end_pts
;
73 unsigned int start_col
, end_col
;
74 unsigned int start_row
, end_row
;
75 unsigned int width
, height
, stride
;
76 size_t image_size
; /* Size of the image buffer */
77 unsigned char *image
; /* Grayscale value */
78 unsigned char *aimage
; /* Alpha value */
79 unsigned int scaled_frame_width
, scaled_frame_height
;
80 unsigned int scaled_start_col
, scaled_start_row
;
81 unsigned int scaled_width
, scaled_height
, scaled_stride
;
82 size_t scaled_image_size
;
83 unsigned char *scaled_image
;
84 unsigned char *scaled_aimage
;
85 int auto_palette
; /* 1 if we lack a palette and must use an heuristic. */
86 int font_start_level
; /* Darkest value used for the computed font */
87 const vo_functions_t
*hw_spu
;
94 static spudec_handle_t
*spudec
;
95 static FILE *fsub
= NULL
;
96 static unsigned int sub_idx
= 0;
99 process_gocr_output(const char *const fname
, unsigned int start
, unsigned int end
)
102 int temp
, h
, m
, s
, ms
;
104 file
= fopen(fname
, "r");
106 perror("fopen failed");
118 fprintf(fsub
, "%d\n%02d:%02d:%02d,%03d --> ", ++sub_idx
, h
, m
, s
, ms
);
128 fprintf(fsub
, "%02d:%02d:%02d,%03d\n", h
, m
, s
, ms
);
130 while ((c
= getc(file
)) != EOF
) {
148 output_pgm(FILE *f
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
156 for (y
= 0; y
< h
; ++y
) {
157 for (x
= 0; x
< w
; ++x
) {
160 res
= src
[x
] * (256 - srca
[x
]);
163 res
= (65535 - res
) >> 8;
174 draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
180 const char *const tmpfname
= tmpnam(NULL
);
181 sprintf(buf
, "subtitle-%d-%d.pgm", spudec
->start_pts
/ 90, spudec
->end_pts
/ 90);
183 output_pgm(f
, w
, h
, src
, srca
, stride
);
185 /* see <URL:http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/subtitleripper/subtitleripper/src/README.gocr?rev=HEAD&content-type=text/vnd.viewcvs-markup> */
186 sprintf(cmd
, GOCR_PROGRAM
" -v 1 -s 7 -d 0 -m 130 -m 256 -m 32 -i %s -o %s", buf
, tmpfname
);
187 cmdres
= system(cmd
);
189 perror("system failed");
193 fprintf(stderr
, GOCR_PROGRAM
" returned %d\n", cmdres
);
196 process_gocr_output(tmpfname
, spudec
->start_pts
, spudec
->end_pts
);
202 main(int argc
, char **argv
)
204 const char *vobsubname
, *subripname
;
210 if (argc
< 2 || 4 < argc
) {
211 fprintf(stderr
, "Usage: %s <vobsub basename> [<subid> [<output filename>] ]\n", argv
[0]);
214 vobsubname
= argv
[1];
218 vobsub_id
= atoi(argv
[2]);
220 subripname
= argv
[3];
221 fsub
= fopen(subripname
, "w");
224 vobsub
= vobsub_open(vobsubname
, NULL
, 0, &spudec
);
225 while ((packet_len
=vobsub_get_next_packet(vobsub
, &packet
, &pts100
)) >= 0) {
226 spudec_assemble(spudec
, packet
, packet_len
, pts100
);
227 if (spudec
->queue_head
) {
228 spudec_heartbeat(spudec
, spudec
->queue_head
->start_pts
);
229 if (spudec_changed(spudec
))
230 spudec_draw(spudec
, draw_alpha
);
235 vobsub_close(vobsub
);