typo fixes
[mplayer/greg.git] / TOOLS / subrip.c
blob3103e2a5a0b35f535ebe19cc6563bcc9ce413afb
1 /* -*- compile-command: "gcc -g -Wall -I.. -o subrip subrip.c ../vobsub.o ../spudec.o ../mp_msg.o ../unrarlib.o ../postproc/swscale.o ../postproc/rgb2rgb.o ../postproc/yuv2rgb.o ../libmpcodecs/img_format.o -lm" -*- */
2 /*
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"
9 */
11 /* Make sure this accesses the CVS version of JOCR/GOCR */
12 #define GOCR_PROGRAM "gocr"
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include "libvo/video_out.h"
21 #include "vobsub.h"
22 #include "spudec.h"
24 /* XXX Kludge ahead, this MUST be the same as the definitions found in ../spudec.c */
25 typedef struct packet_t packet_t;
26 struct packet_t {
27 unsigned char *packet;
28 unsigned int palette[4];
29 unsigned int alpha[4];
30 unsigned int control_start; /* index of start of control data */
31 unsigned int current_nibble[2]; /* next data nibble (4 bits) to be
32 processed (for RLE decoding) for
33 even and odd lines */
34 int deinterlace_oddness; /* 0 or 1, index into current_nibble */
35 unsigned int start_col, end_col;
36 unsigned int start_row, end_row;
37 unsigned int width, height, stride;
38 unsigned int start_pts, end_pts;
39 packet_t *next;
41 typedef struct {
42 packet_t *queue_head;
43 packet_t *queue_tail;
44 unsigned int global_palette[16];
45 unsigned int orig_frame_width, orig_frame_height;
46 unsigned char* packet;
47 size_t packet_reserve; /* size of the memory pointed to by packet */
48 unsigned int packet_offset; /* end of the currently assembled fragment */
49 unsigned int packet_size; /* size of the packet once all fragments are assembled */
50 unsigned int packet_pts; /* PTS for this packet */
51 unsigned int palette[4];
52 unsigned int alpha[4];
53 unsigned int cuspal[4];
54 unsigned int custom;
55 unsigned int now_pts;
56 unsigned int start_pts, end_pts;
57 unsigned int start_col, end_col;
58 unsigned int start_row, end_row;
59 unsigned int width, height, stride;
60 size_t image_size; /* Size of the image buffer */
61 unsigned char *image; /* Grayscale value */
62 unsigned char *aimage; /* Alpha value */
63 unsigned int scaled_frame_width, scaled_frame_height;
64 unsigned int scaled_start_col, scaled_start_row;
65 unsigned int scaled_width, scaled_height, scaled_stride;
66 size_t scaled_image_size;
67 unsigned char *scaled_image;
68 unsigned char *scaled_aimage;
69 int auto_palette; /* 1 if we lack a palette and must use an heuristic. */
70 int font_start_level; /* Darkest value used for the computed font */
71 vo_functions_t *hw_spu;
72 int spu_changed;
73 } spudec_handle_t;
75 int use_gui;
76 int gtkMessageBox;
77 int verbose=1;
78 int identify=0;
79 int vobsub_id=0;
80 int sub_pos=0;
82 static spudec_handle_t *spudec;
83 static FILE *fsub = NULL;
84 static unsigned int sub_idx = 0;
86 static void
87 process_gocr_output(const char *const fname, unsigned int start, unsigned int end)
89 FILE *file;
90 int temp, h, m, s, ms;
91 int c, bol;
92 file = fopen(fname, "r");
93 if (file == NULL) {
94 perror("fopen failed");
95 return;
97 temp = start;
98 temp /= 90;
99 h = temp / 3600000;
100 temp %= 3600000;
101 m = temp / 60000;
102 temp %= 60000;
103 s = temp / 1000;
104 temp %= 1000;
105 ms = temp;
106 fprintf(fsub, "%d\n%02d:%02d:%02d,%03d --> ", ++sub_idx, h, m, s, ms);
107 temp = end;
108 temp /= 90;
109 h = temp / 3600000;
110 temp %= 3600000;
111 m = temp / 60000;
112 temp %= 60000;
113 s = temp / 1000;
114 temp %= 1000;
115 ms = temp;
116 fprintf(fsub, "%02d:%02d:%02d,%03d\n", h, m, s, ms);
117 bol = 1;
118 while ((c = getc(file)) != EOF) {
119 if (bol) {
120 if (!isspace(c)) {
121 putc(c, fsub);
122 bol=0;
125 else if (!bol) {
126 putc(c, fsub);
127 bol = c == '\n';
130 putc('\n', fsub);
131 fflush(fsub);
132 fclose(file);
135 static void
136 output_pgm(FILE *f, int w, int h, unsigned char *src, unsigned char *srca, int stride)
138 int x, y;
139 fprintf(f,
140 "P5\n"
141 "%d %d\n"
142 "255\n",
143 w, h);
144 for (y = 0; y < h; ++y) {
145 for (x = 0; x < w; ++x) {
146 int res;
147 if (srca[x])
148 res = src[x] * (256 - srca[x]);
149 else
150 res = 0;
151 res = (65535 - res) >> 8;
152 putc(res&0xff, f);
155 src += stride;
156 srca += stride;
158 putc('\n', f);
161 static void
162 draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
164 FILE *f;
165 char buf[128];
166 char cmd[512];
167 int cmdres;
168 const char *const tmpfname = tmpnam(NULL);
169 sprintf(buf, "subtitle-%d-%d.pgm", spudec->start_pts / 90, spudec->end_pts / 90);
170 f = fopen(buf, "w");
171 output_pgm(f, w, h, src, srca, stride);
172 fclose(f);
173 /* see <URL:http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/subtitleripper/subtitleripper/src/README.gocr?rev=HEAD&content-type=text/vnd.viewcvs-markup> */
174 sprintf(cmd, GOCR_PROGRAM" -v 1 -s 7 -d 0 -m 130 -m 256 -m 32 -i %s -o %s", buf, tmpfname);
175 cmdres = system(cmd);
176 if (cmdres < 0) {
177 perror("system failed");
178 exit(EXIT_FAILURE);
180 else if (cmdres) {
181 fprintf(stderr, GOCR_PROGRAM" returned %d\n", cmdres);
182 exit(cmdres);
184 process_gocr_output(tmpfname, spudec->start_pts, spudec->end_pts);
185 unlink(buf);
186 unlink(tmpfname);
189 void
190 fast_memcpy(void *a, void *b, int s)
191 { //FIXME
192 memcpy(a, b, s);
196 main(int argc, char **argv)
198 const char *vobsubname, *subripname;
199 void *vobsub;
200 void *packet;
201 int packet_len;
202 #ifdef BAD
203 unsigned int prev_pts;
204 #endif
205 unsigned int pts100;
207 if (argc < 2 || 4 < argc) {
208 fprintf(stderr, "Usage: %s <vobsub basename> [<subid> [<output filename>] ]\n", argv[0]);
209 exit(EXIT_FAILURE);
211 vobsubname = argv[1];
212 subripname = NULL;
213 fsub = stdout;
214 if (argc >= 3)
215 vobsub_id = atoi(argv[2]);
216 if (argc >= 4) {
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);
231 if (vobsub)
232 vobsub_close(vobsub);
233 exit(EXIT_SUCCESS);