-identify output: print media start time
[mplayer/glamo.git] / TOOLS / subrip.c
blobd582bf91b3bb42bb24e25463997394f4436dd57d
1 /*
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"
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include "libvo/video_out.h"
34 #include "vobsub.h"
35 #include "spudec.h"
37 /* XXX Kludge ahead, this MUST be the same as the definitions found in ../spudec.c */
38 typedef struct packet_t packet_t;
39 struct 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
46 even and odd lines */
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;
52 packet_t *next;
54 typedef struct {
55 packet_t *queue_head;
56 packet_t *queue_tail;
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];
67 unsigned int custom;
68 unsigned int now_pts;
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;
85 int spu_changed;
86 } spudec_handle_t;
88 int vobsub_id=0;
89 int sub_pos=0;
91 static spudec_handle_t *spudec;
92 static FILE *fsub = NULL;
93 static unsigned int sub_idx = 0;
95 static void
96 process_gocr_output(const char *const fname, unsigned int start, unsigned int end)
98 FILE *file;
99 int temp, h, m, s, ms;
100 int c, bol;
101 file = fopen(fname, "r");
102 if (file == NULL) {
103 perror("fopen failed");
104 return;
106 temp = start;
107 temp /= 90;
108 h = temp / 3600000;
109 temp %= 3600000;
110 m = temp / 60000;
111 temp %= 60000;
112 s = temp / 1000;
113 temp %= 1000;
114 ms = temp;
115 fprintf(fsub, "%d\n%02d:%02d:%02d,%03d --> ", ++sub_idx, h, m, s, ms);
116 temp = end;
117 temp /= 90;
118 h = temp / 3600000;
119 temp %= 3600000;
120 m = temp / 60000;
121 temp %= 60000;
122 s = temp / 1000;
123 temp %= 1000;
124 ms = temp;
125 fprintf(fsub, "%02d:%02d:%02d,%03d\n", h, m, s, ms);
126 bol = 1;
127 while ((c = getc(file)) != EOF) {
128 if (bol) {
129 if (!isspace(c)) {
130 putc(c, fsub);
131 bol=0;
134 else if (!bol) {
135 putc(c, fsub);
136 bol = c == '\n';
139 putc('\n', fsub);
140 fflush(fsub);
141 fclose(file);
144 static void
145 output_pgm(FILE *f, int w, int h, unsigned char *src, unsigned char *srca, int stride)
147 int x, y;
148 fprintf(f,
149 "P5\n"
150 "%d %d\n"
151 "255\n",
152 w, h);
153 for (y = 0; y < h; ++y) {
154 for (x = 0; x < w; ++x) {
155 int res;
156 if (srca[x])
157 res = src[x] * (256 - srca[x]);
158 else
159 res = 0;
160 res = (65535 - res) >> 8;
161 putc(res&0xff, f);
164 src += stride;
165 srca += stride;
167 putc('\n', f);
170 static void
171 draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
173 FILE *f;
174 char buf[128];
175 char cmd[512];
176 int cmdres;
177 const char *const tmpfname = tmpnam(NULL);
178 sprintf(buf, "subtitle-%d-%d.pgm", spudec->start_pts / 90, spudec->end_pts / 90);
179 f = fopen(buf, "w");
180 output_pgm(f, w, h, src, srca, stride);
181 fclose(f);
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);
185 if (cmdres < 0) {
186 perror("system failed");
187 exit(EXIT_FAILURE);
189 else if (cmdres) {
190 fprintf(stderr, GOCR_PROGRAM" returned %d\n", cmdres);
191 exit(cmdres);
193 process_gocr_output(tmpfname, spudec->start_pts, spudec->end_pts);
194 unlink(buf);
195 unlink(tmpfname);
199 main(int argc, char **argv)
201 const char *vobsubname, *subripname;
202 void *vobsub;
203 void *packet;
204 int packet_len;
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);