-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathyuvsubtitle.c
More file actions
627 lines (469 loc) · 13.9 KB
/
Copy pathyuvsubtitle.c
File metadata and controls
627 lines (469 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* generic.c
* Mark Heath <mjpeg0 at silicontrip.org>
* http://silicontrip.net/~mark/lavtools/
*
** <h3>YUV Subtitles</h3>
** <p> a subtitle rendering utility for yuv streams</p>
** <p> Reads an ascii subtitle file and renders a TTF in the image</p>
** <p> The subtitle file is in the format </p>
** <pre>
**10,100,Leah and the wookie must never again leave this city.
**100,170,That was never a condition of our arrangment.
**</pre>
**<h4>Arguments</h4>
**<pre>
**-f <fontfile> path to TTF file.
**-s <size> size to render the subtitle
**-y <y pos> vertical position on screen
**-c <colour> colour in the format y,u,v
**-u <filename> subtitle file filename
**</pre>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
gcc yuvdeinterlace.c -I/sw/include/mjpegtools -lmjpegutils
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <yuv4mpeg.h>
#include <mpegconsts.h>
#include "utilyuv.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
struct subtitle {
//in frame counts
int on;
int off;
// char *text;
unsigned char text[256]; // for testing
};
struct subhead {
int entries;
struct subtitle *subs;
};
#define VERSION "0.1"
static void print_usage()
{
fprintf (stderr,
"usage: yuvsubtitle\n"
"\t-f <fontfile> path to TTF file.\n"
"\t-s <size> size to render the subtitle\n"
"\t-y <y pos> vertical position on screen\n"
"\t-c <colour> colour in the format y,u,v\n"
"\t-u <filename> subtitle file filename\n"
);
}
// cheap and nasty UTF-8 to UTF-16 decoder
unsigned int decode_char ( int *p, unsigned char * text)
{
unsigned int sp;
sp = text[*p];
// mjpeg_debug ("encoded character %d",sp);
// try for a uri style special characters
if (sp == '%') {
char special[3];
strncpy(special,&text[*p+1],2);
special[2] = '\0';
*p+=2;
sscanf(special,"%x",&sp);
} else if ((sp & 224) == 192) {
if ((text[*p+1] & 192) == 128) {
// mjpeg_debug("decode UTF dual character");
sp = ((sp & 31) << 6) + (text[*p+1] & 63);
(*p)++;
}
} else if ((sp & 240) == 224) {
if ((text[*p+1] & 192) == 128 && (text[*p+2] & 192) == 128) {
// mjpeg_debug("decode UTF triple character");
sp = ((sp & 15) << 12) + ((text[*p+1] & 63) << 6) + (text[*p+2] & 63);
*p+=2;
}
} else if ((sp & 248) == 240) {
if ((text[*p+1] & 192) == 128 && (text[*p+2] & 192) == 128 && (text[*p+3] & 192) == 128) {
// mjpeg_debug("decode UTF quad character");
sp = ((sp & 7) << 18) + ((text[*p+1] & 63) << 12) + ((text[*p+2] & 63) << 6) + (text[*p+3] & 63);
*p+=3;
}
}
//mjpeg_debug ("decoded character %x",sp);
return sp;
}
void ftdims (int *px, int *py, int *lines, FT_Face face, unsigned char * text) {
FT_GlyphSlot slot = face->glyph; /* a small shortcut */
int pen_x,n,max,topmax=0,rowmax=0;
unsigned int sp;
pen_x = 0; max = 0;
for ( n = 0; n < strlen(text); n++ ) {
sp = decode_char(&n,text);
if (sp == 10) {
if (pen_x>max) max=pen_x;
pen_x = 0; //pen_y += rowmax + topmax;
(*lines)++;
} else if (sp >=32) {
FT_Load_Char( face, sp, FT_LOAD_RENDER );
/* ignore errors */
pen_x += slot->advance.x;
// pen_y += slot->advance.y; /* not useful for now */
if ( slot->bitmap_top > topmax) topmax=slot->bitmap_top;
if ( slot->bitmap.rows-slot->bitmap_top > rowmax) rowmax = slot->bitmap.rows-slot->bitmap_top;
}
}
if (max > pen_x) pen_x = max;
*px = pen_x;
*py = rowmax + topmax;
}
void draw_bitmap (FT_Bitmap* bitmap, int x, int y, uint8_t *m[3], y4m_stream_info_t *si, uint8_t yc, uint8_t uc, uint8_t vc )
{
FT_Int i, j, p, q;
uint8_t bri, piy,piu,piv;
FT_Int x_max;
FT_Int y_max;
int width,height,cx,cy;
width = y4m_si_get_plane_width(si,0);
height = y4m_si_get_plane_height(si,0);
x_max = x + bitmap->width;
y_max = y + bitmap->rows;
for ( i = x, p = 0; i < x_max; i++, p++ )
{
for ( j = y, q = 0; j < y_max; j++, q++ )
{
if ( i >= width || j >= height )
continue;
// configurable colour
bri = bitmap->buffer[q * bitmap->width + p];
piy = luma_mix(get_pixel (i,j,0,m,si),yc,bri);
cx = xchroma(i,si);
cy = ychroma(j,si);
piu = chroma_mix(get_pixel (cx,cy,1,m,si),uc,bri);
piv = chroma_mix(get_pixel (cx,cy,2,m,si),vc,bri);
set_pixel(piy,i,j,0,m,si);
set_pixel(piu,cx,cy,1,m,si);
set_pixel(piv,cx,cy,2,m,si);
}
}
}
static void filterframe (uint8_t *m[3], y4m_stream_info_t *si, FT_Face face, unsigned char * text,
int pen_y, int yc, int uc, int vc, int yadvance )
{
FT_GlyphSlot slot = face->glyph; /* a small shortcut */
int pen_x, n;
int twidth,theight,lines=0;
int cx,cy;
int width;
int sp;
mjpeg_debug ("text: %s\n",text);
// configurable start location.
width = y4m_si_get_plane_width(si,0);
ftdims(&twidth, &theight, &lines, face,text);
twidth = twidth >> 6;
pen_x = width / 2 - twidth / 2;
if (pen_x < 0) {
mjpeg_warn("text larger than screen width");
pen_x = 0;
}
mjpeg_debug ("dims: w: %d h: %d l: %d",twidth,theight,lines);
pen_y -= theight * lines;
for ( n = 0; n < strlen(text); n++ )
{
/* load glyph image into the slot (erase previous one) */
sp = decode_char(&n,text);
if (sp == 10) {
//TODO: get correct vertical spacing.
// FT_Load_Char( face, sp, FT_LOAD_RENDER );
pen_x = width / 2 - twidth / 2;
pen_y += theight+4; // test value since most of my subs tests are 24
} else {
FT_Load_Char( face, sp, FT_LOAD_RENDER );
for (sp=0;sp<3;sp++)
for (cx=-1; cx < 2; cx ++)
for (cy =-1; cy<2; cy++) {
draw_bitmap( &slot->bitmap,
pen_x + slot->bitmap_left + cx + sp,
pen_y - slot->bitmap_top + cy + sp,
m,si,16,128,128 );
}
/*
draw_bitmap( &slot->bitmap,
pen_x + slot->bitmap_left - 1,
pen_y - slot->bitmap_top - 1,
m,si,16,128,128 );
draw_bitmap( &slot->bitmap,
pen_x + slot->bitmap_left + 1,
pen_y - slot->bitmap_top - 1,
m,si,16,128,128 );
draw_bitmap( &slot->bitmap,
pen_x + slot->bitmap_left - 1,
pen_y - slot->bitmap_top + 1,
m,si,16,128,128 );
*/
draw_bitmap( &slot->bitmap,
pen_x + slot->bitmap_left,
pen_y - slot->bitmap_top,
m,si,yc,uc,vc );
/* increment pen position */
pen_x += slot->advance.x >> 6;
pen_y += slot->advance.y >> 6; /* not useful for now */
}
/* now, draw to our target surface */
}
}
unsigned char * get_sub (struct subhead s, int fc) {
int n;
// mjpeg_info("entries: %d ", s.entries);
for (n=0; n < s.entries; n++){
// mjpeg_info ("checking %d - %d",s.subs[n].on, s.subs[n].off);
if (s.subs[n].on <= fc && s.subs[n].off >= fc)
return s.subs[n].text;
}
return '\0';
}
static void filter( int fdIn, int fdOut , y4m_stream_info_t *inStrInfo, FT_Face face, struct subhead subs,
int pen_y, int yc, int uc, int vc, int yadvance )
{
y4m_frame_info_t in_frame ;
uint8_t *yuv_data[3] ;
int read_error_code ;
int write_error_code ;
int framecounter=0;
unsigned char *text;
// Allocate memory for the YUV channels
if (chromalloc(yuv_data,inStrInfo))
mjpeg_error_exit1 ("Could'nt allocate memory for the YUV4MPEG data!");
/* Initialize counters */
write_error_code = Y4M_OK ;
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn, inStrInfo,&in_frame,yuv_data );
framecounter++;
while( Y4M_ERR_EOF != read_error_code && write_error_code == Y4M_OK ) {
// do work
// mjpeg_info("frame: %d",framecounter);
if (read_error_code == Y4M_OK) {
text=get_sub(subs,framecounter);
if (text != '\0') {
filterframe(yuv_data,inStrInfo,face,text,pen_y,yc,uc,vc,yadvance);
}
write_error_code = y4m_write_frame( fdOut, inStrInfo, &in_frame, yuv_data );
}
y4m_fini_frame_info( &in_frame );
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn, inStrInfo,&in_frame,yuv_data );
framecounter++;
}
// Clean-up regardless an error happened or not
y4m_fini_frame_info( &in_frame );
free( yuv_data[0] );
free( yuv_data[1] );
free( yuv_data[2] );
if( read_error_code != Y4M_ERR_EOF )
mjpeg_error_exit1 ("Error reading from input stream!");
}
/*
void read_subs_t(struct subhead *s)
{
s->entries=5 ;
s->subs = malloc(sizeof(struct subtitle) * s->entries);
s->subs[0].on = 10;
s->subs[0].off = 100;
strcpy(s->subs[0].text,"Leah and the Wookie must never again leave this city.");
s->subs[1].on = 100;
s->subs[1].off = 170;
strcpy(s->subs[1].text,"That was never a condition of our arrangment.");
s->subs[2].on = 170;
s->subs[2].off = 220;
strcpy(s->subs[2].text,"Nor was giving Han to this bounty hunter.");
s->subs[3].on = 230;
s->subs[3].off = 350;
strcpy(s->subs[3].text,"I have altered the Deal. Pray I don't alter it any further.");
s->subs[4].on = 368;
s->subs[4].off = 434;
strcpy(s->subs[4].text,"This deal is getting worse all the time.");
}
*/
void edlcount (FILE *file, int *maxline, int *lines)
{
int c;
int count=0;
*maxline=0;
*lines=0;
flockfile(file); // for optimising the single character reads
while (!feof(file)){
c = getc_unlocked(file);
count++;
if (c==10) {
(*lines)++;
if (count > *maxline) {
*maxline = count;
count=0;
}
}
}
funlockfile(file);
rewind(file);
}
int read_subs (struct subhead *s, char *filename) {
FILE *fn;
char *line;
int maxline,lines,count=0,c;
char *p;
fn = fopen(filename,"r");
if (fn == NULL) {
mjpeg_error_exit1("Cannot open subtitle file");
}
edlcount(fn,&maxline,&lines);
s->entries=lines ;
s->subs = malloc(sizeof(struct subtitle) * s->entries);
line = (char *)malloc(maxline);
if (line == NULL) {
mjpeg_error("Error allocating line memory");
fclose (fn);
return -1;
}
while (fgets(line,maxline,fn) != NULL) {
sscanf(line,"%d,%d",&s->subs[count].on,&s->subs[count].off);
for (c=0; c < strlen(line); c++) {
if (line[c] == ',') {
p=&line[c+1];
}
}
strcpy(s->subs[count].text, p);
for (c=0; c < strlen(s->subs[count].text); c++) {
// mjpeg_debug("copying character: %d",s->subs[count].text[c]);
if (s->subs[count].text[c] < 32) {
s->subs[count].text[c]=0;
}
}
count++;
}
free (line);
}
// *************************************************************************************
// MAIN
// *************************************************************************************
int main (int argc, char *argv[])
{
int verbose = 1;
int fdIn = 0 ;
int fdOut = 1 ;
y4m_stream_info_t in_streaminfo ;
int height=16;
int c, pen_y;
const static char *legal_flags = "hv:f:s:y:c:u:";
FT_Library library;
FT_Face face;
struct subhead subs;
char * subname = NULL;
int yc,uc,vc;
if (FT_Init_FreeType(&library))
mjpeg_error_exit1("Cannot initialise the freetype library");
// defaults
pen_y = -1;
// default text colour
yc = 235;
uc = 128;
vc = 128;
while ((c = getopt (argc, argv, legal_flags)) != -1) {
switch (c) {
case 'v':
verbose = atoi (optarg);
if (verbose < 0 || verbose > 2)
mjpeg_error_exit1 ("Verbose level must be [0..2]");
break;
case 'f':
c = FT_New_Face( library,
optarg,
0,
&face );
if ( c == FT_Err_Unknown_File_Format )
{
mjpeg_error_exit1("Do not recognise the font file");
}
else if ( c )
{
mjpeg_error_exit1("Error reading the font file");
}
break;
case 's':
height = atoi(optarg);
break;
case 'y':
pen_y = atoi(optarg);
break;
case 'c':
sscanf (optarg,"%d,%d,%d",&yc,&uc,&vc);
break;
case 'u':
subname = malloc(strlen(optarg));
strcpy(subname,optarg);
break;
case 'h':
case '?':
print_usage (argv);
return 0 ;
break;
}
}
FT_Set_Pixel_Sizes( face, /* handle to face object */
0, /* pixel_width */
height ); /* pixel_height */
// mjpeg tools global initialisations
mjpeg_default_handler_verbosity (verbose);
// Initialize input streams
y4m_init_stream_info (&in_streaminfo);
// ***************************************************************
// Get video stream informations (size, framerate, interlacing, aspect ratio).
// The streaminfo structure is filled in
// ***************************************************************
// INPUT comes from stdin, we check for a correct file header
if (y4m_read_stream_header (fdIn, &in_streaminfo) != Y4M_OK)
mjpeg_error_exit1 ("Could'nt read YUV4MPEG header!");
// Information output
mjpeg_info ("yuvsubtitle (version " VERSION ") is a subtitle rendering utility for yuv streams");
mjpeg_info ("(C) Mark Heath <mjpeg0 at silicontrip.org>");
// mjpeg_info ("yuvcropdetect -h for help");
if (pen_y==-1) {
pen_y = y4m_si_get_plane_height(&in_streaminfo,0) - 48;
}
y4m_write_stream_header(fdOut,&in_streaminfo);
if (subname != NULL) {
// read the subtitle file
read_subs(&subs,subname);
filter(fdIn, fdOut, &in_streaminfo,face,subs,pen_y,yc,uc,vc,14 * height / 10);
// mjpeg_debug ("free subname");
free (subname);
// mjpeg_debug ("free subs");
free (subs.subs);
} else {
mjpeg_error_exit1("No subtitle filename specified");
}
/* in that function we do all the important work */
y4m_fini_stream_info (&in_streaminfo);
mjpeg_debug ("done face");
FT_Done_Face ( face );
mjpeg_debug ("done freetype");
FT_Done_FreeType( library );
return 0;
}
/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/