1 /* -*- compile-command: "gcc -g -Wall -I.. -o subrip subrip.c ../vobsub.o ../spudec.o ../mp_msg.o ../unrarlib.o ../libswscale/swscale.o ../libswscale/rgb2rgb.o ../libswscale/yuv2rgb.o ../libmpcodecs/img_format.o -lm" -*- */
3 * Use with CVS JOCR/GOCR.
5 * You will have to change 'vobsub_id' value if you want another subtitle than number 0.
7 * HINT: you can view the subtitle that is being decoded with "display subtitle-*.pgm"
11 /* Make sure this accesses the CVS version of JOCR/GOCR */
12 #define GOCR_PROGRAM "gocr"
19 #include <sys/types.h>
21 #include "libvo/video_out.h"
25 void guiMessageBox(int level
, char * str
) {};
27 /* XXX Kludge ahead, this MUST be the same as the definitions found in ../spudec.c */
28 typedef struct packet_t packet_t
;
30 unsigned char *packet
;
31 unsigned int palette
[4];
32 unsigned int alpha
[4];
33 unsigned int control_start
; /* index of start of control data */
34 unsigned int current_nibble
[2]; /* next data nibble (4 bits) to be
35 processed (for RLE decoding) for
37 int deinterlace_oddness
; /* 0 or 1, index into current_nibble */
38 unsigned int start_col
, end_col
;
39 unsigned int start_row
, end_row
;
40 unsigned int width
, height
, stride
;
41 unsigned int start_pts
, end_pts
;
47 unsigned int global_palette
[16];
48 unsigned int orig_frame_width
, orig_frame_height
;
49 unsigned char* packet
;
50 size_t packet_reserve
; /* size of the memory pointed to by packet */
51 unsigned int packet_offset
; /* end of the currently assembled fragment */
52 unsigned int packet_size
; /* size of the packet once all fragments are assembled */
53 unsigned int packet_pts
; /* PTS for this packet */
54 unsigned int palette
[4];
55 unsigned int alpha
[4];
56 unsigned int cuspal
[4];
59 unsigned int start_pts
, end_pts
;
60 unsigned int start_col
, end_col
;
61 unsigned int start_row
, end_row
;
62 unsigned int width
, height
, stride
;
63 size_t image_size
; /* Size of the image buffer */
64 unsigned char *image
; /* Grayscale value */
65 unsigned char *aimage
; /* Alpha value */
66 unsigned int scaled_frame_width
, scaled_frame_height
;
67 unsigned int scaled_start_col
, scaled_start_row
;
68 unsigned int scaled_width
, scaled_height
, scaled_stride
;
69 size_t scaled_image_size
;
70 unsigned char *scaled_image
;
71 unsigned char *scaled_aimage
;
72 int auto_palette
; /* 1 if we lack a palette and must use an heuristic. */
73 int font_start_level
; /* Darkest value used for the computed font */
74 vo_functions_t
*hw_spu
;
84 static spudec_handle_t
*spudec
;
85 static FILE *fsub
= NULL
;
86 static unsigned int sub_idx
= 0;
89 process_gocr_output(const char *const fname
, unsigned int start
, unsigned int end
)
92 int temp
, h
, m
, s
, ms
;
94 file
= fopen(fname
, "r");
96 perror("fopen failed");
108 fprintf(fsub
, "%d\n%02d:%02d:%02d,%03d --> ", ++sub_idx
, h
, m
, s
, ms
);
118 fprintf(fsub
, "%02d:%02d:%02d,%03d\n", h
, m
, s
, ms
);
120 while ((c
= getc(file
)) != EOF
) {
138 output_pgm(FILE *f
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
146 for (y
= 0; y
< h
; ++y
) {
147 for (x
= 0; x
< w
; ++x
) {
150 res
= src
[x
] * (256 - srca
[x
]);
153 res
= (65535 - res
) >> 8;
164 draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
, unsigned char *srca
, int stride
)
170 const char *const tmpfname
= tmpnam(NULL
);
171 sprintf(buf
, "subtitle-%d-%d.pgm", spudec
->start_pts
/ 90, spudec
->end_pts
/ 90);
173 output_pgm(f
, w
, h
, src
, srca
, stride
);
175 /* see <URL:http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/subtitleripper/subtitleripper/src/README.gocr?rev=HEAD&content-type=text/vnd.viewcvs-markup> */
176 sprintf(cmd
, GOCR_PROGRAM
" -v 1 -s 7 -d 0 -m 130 -m 256 -m 32 -i %s -o %s", buf
, tmpfname
);
177 cmdres
= system(cmd
);
179 perror("system failed");
183 fprintf(stderr
, GOCR_PROGRAM
" returned %d\n", cmdres
);
186 process_gocr_output(tmpfname
, spudec
->start_pts
, spudec
->end_pts
);
192 fast_memcpy(void *a
, void *b
, int s
)
198 main(int argc
, char **argv
)
200 const char *vobsubname
, *subripname
;
206 if (argc
< 2 || 4 < argc
) {
207 fprintf(stderr
, "Usage: %s <vobsub basename> [<subid> [<output filename>] ]\n", argv
[0]);
210 vobsubname
= argv
[1];
214 vobsub_id
= atoi(argv
[2]);
216 subripname
= argv
[3];
217 fsub
= fopen(subripname
, "w");
220 vobsub
= vobsub_open(vobsubname
, NULL
, 0, &spudec
);
221 while ((packet_len
=vobsub_get_next_packet(vobsub
, &packet
, &pts100
)) >= 0) {
222 spudec_assemble(spudec
, packet
, packet_len
, pts100
);
223 if (spudec
->queue_head
) {
224 spudec_heartbeat(spudec
, spudec
->queue_head
->start_pts
);
225 if (spudec_changed(spudec
))
226 spudec_draw(spudec
, draw_alpha
);
231 vobsub_close(vobsub
);