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"
37 /* XXX Kludge ahead, this MUST be the same as the definitions found in ../spudec.c */
38 typedef struct packet_t packet_t
;
40 unsigned char *packet
;
41 unsigned int palette
[4];
42 unsigned int alpha
[4];
43 unsigned int control_start
; /* index of start of control data */
44 unsigned int current_nibble
[2]; /* next data nibble (4 bits) to be
45 processed (for RLE decoding) for
47 int deinterlace_oddness
; /* 0 or 1, index into current_nibble */
48 unsigned int start_col
, end_col
;
49 unsigned int start_row
, end_row
;
50 unsigned int width
, height
, stride
;
51 unsigned int start_pts
, end_pts
;
57 unsigned int global_palette
[16];
58 unsigned int orig_frame_width
, orig_frame_height
;
59 unsigned char* packet
;
60 size_t packet_reserve
; /* size of the memory pointed to by packet */
61 unsigned int packet_offset
; /* end of the currently assembled fragment */
62 unsigned int packet_size
; /* size of the packet once all fragments are assembled */
63 unsigned int packet_pts
; /* PTS for this packet */
64 unsigned int palette
[4];
65 unsigned int alpha
[4];
66 unsigned int cuspal
[4];
69 unsigned int start_pts
, end_pts
;
70 unsigned int start_col
, end_col
;
71 unsigned int start_row
, end_row
;
72 unsigned int width
, height
, stride
;
73 size_t image_size
; /* Size of the image buffer */
74 unsigned char *image
; /* Grayscale value */
75 unsigned char *aimage
; /* Alpha value */
76 unsigned int scaled_frame_width
, scaled_frame_height
;
77 unsigned int scaled_start_col
, scaled_start_row
;
78 unsigned int scaled_width
, scaled_height
, scaled_stride
;
79 size_t scaled_image_size
;
80 unsigned char *scaled_image
;
81 unsigned char *scaled_aimage
;
82 int auto_palette
; /* 1 if we lack a palette and must use an heuristic. */
83 int font_start_level
; /* Darkest value used for the computed font */
84 const vo_functions_t
*hw_spu
;
91 static spudec_handle_t
*spudec
;
92 static FILE *fsub
= NULL
;
93 static unsigned int sub_idx
= 0;
96 process_gocr_output(const char *const fname
, unsigned int start
, unsigned int end
)
99 int temp
, h
, m
, s
, ms
;
101 file
= fopen(fname
, "r");
103 perror("fopen failed");
115 fprintf(fsub
, "%d\n%02d:%02d:%02d,%03d --> ", ++sub_idx
, h
, m
, s
, ms
);
125 fprintf(fsub
, "%02d:%02d:%02d,%03d\n", h
, m
, s
, ms
);
127 while ((c
= getc(file
)) != EOF
) {
145 output_pgm(FILE *f
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
153 for (y
= 0; y
< h
; ++y
) {
154 for (x
= 0; x
< w
; ++x
) {
157 res
= src
[x
] * (256 - srca
[x
]);
160 res
= (65535 - res
) >> 8;
171 draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
177 const char *const tmpfname
= tmpnam(NULL
);
178 sprintf(buf
, "subtitle-%d-%d.pgm", spudec
->start_pts
/ 90, spudec
->end_pts
/ 90);
180 output_pgm(f
, w
, h
, src
, srca
, stride
);
182 /* see <URL:http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/subtitleripper/subtitleripper/src/README.gocr?rev=HEAD&content-type=text/vnd.viewcvs-markup> */
183 sprintf(cmd
, GOCR_PROGRAM
" -v 1 -s 7 -d 0 -m 130 -m 256 -m 32 -i %s -o %s", buf
, tmpfname
);
184 cmdres
= system(cmd
);
186 perror("system failed");
190 fprintf(stderr
, GOCR_PROGRAM
" returned %d\n", cmdres
);
193 process_gocr_output(tmpfname
, spudec
->start_pts
, spudec
->end_pts
);
199 main(int argc
, char **argv
)
201 const char *vobsubname
, *subripname
;
207 if (argc
< 2 || 4 < argc
) {
208 fprintf(stderr
, "Usage: %s <vobsub basename> [<subid> [<output filename>] ]\n", argv
[0]);
211 vobsubname
= argv
[1];
215 vobsub_id
= atoi(argv
[2]);
217 subripname
= argv
[3];
218 fsub
= fopen(subripname
, "w");
221 vobsub
= vobsub_open(vobsubname
, NULL
, 0, &spudec
);
222 while ((packet_len
=vobsub_get_next_packet(vobsub
, &packet
, &pts100
)) >= 0) {
223 spudec_assemble(spudec
, packet
, packet_len
, pts100
);
224 if (spudec
->queue_head
) {
225 spudec_heartbeat(spudec
, spudec
->queue_head
->start_pts
);
226 if (spudec_changed(spudec
))
227 spudec_draw(spudec
, draw_alpha
);
232 vobsub_close(vobsub
);