summaryrefslogtreecommitdiff
path: root/dxr3spudecoder.c
blob: 6d870bf231ac917293cf1d0711c493ec49360024 (plain)
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
628
629
630
631
632
633
/*
 * dxr3spudecoder.c
 *
 * Copyright (C) 2004 Christian Gmeiner
 *
 * Orginal:
 *    Copyright (C) 2001.2002 Andreas Schultz <aschultz@warp10.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <assert.h>
#include <math.h>

#include "dxr3spudecoder.h"
#include "dxr3interface.h"
#include "dxr3tools.h"

// ==================================
#define CMD_SPU_MENU            0x00
#define CMD_SPU_SHOW            0x01
#define CMD_SPU_HIDE            0x02
#define CMD_SPU_SET_PALETTE     0x03
#define CMD_SPU_SET_ALPHA       0x04
#define CMD_SPU_SET_SIZE        0x05
#define CMD_SPU_SET_PXD_OFFSET  0x06
#define CMD_SPU_CHG_COLCON      0x07
#define CMD_SPU_EOF             0xff

#define spuU32(i) ((spu[i] << 8) + spu[i+1])


/*
 * cDxr3Spubitmap:
 *
 * this is a bitmap of the full screen and two palettes
 * the normal palette for the background and the highlight palette
 *
 * Inputs:
 *  - a SPU rle encoded image on creation, which will be decoded into
 *    the full screen indexed bitmap
 *
 * Output:
 *  - a minimal sized cDxr3SpuBitmap a given palette, the indexed bitmap
 *    will be scanned to get the smallest possible resulting bitmap considering
 *    transparencies
 */

// ==================================
void cDxr3SpuPalette::setPalette(const uint32_t * pal)
{
    for (int i = 0; i < 16; i++)
	palette[i] = Tools::YUV2Rgb(pal[i]);
}

// ==================================
#define setMin(a, b) if (a > b) a = b
#define setMax(a, b) if (a < b) a = b

#define spuXres 720
#define spuYres 576

#define revRect(r1, r2) { r1.x1 = r2.x2; r1.y1 = r2.y2; r1.x2 = r2.x1; r1.y2 = r2.y1; }

// ==================================
cDxr3SpuBitmap::cDxr3SpuBitmap(sDxr3SpuRect size, uint8_t * fodd,
			       uint8_t * eodd, uint8_t * feven,
			       uint8_t * eeven)
{
    if (size.x1 < 0 || size.y1 < 0 || size.x2 >= spuXres || size.y2 >= spuYres)
	throw;

    bmpsize = size;
    revRect(minsize[0], size);
    revRect(minsize[1], size);
    revRect(minsize[2], size);
    revRect(minsize[3], size);

    if (!(bmp = new uint8_t[spuXres * spuYres * sizeof(uint8_t)]))
	throw;

    memset(bmp, 0, spuXres * spuYres * sizeof(uint8_t));
    putFieldData(0, fodd, eodd);
    putFieldData(1, feven, eeven);
}

// ==================================
cDxr3SpuBitmap::~cDxr3SpuBitmap()
{
    delete[]bmp;
}

// ==================================
cBitmap *cDxr3SpuBitmap::getBitmap(const aDxr3SpuPalDescr paldescr,
				   const cDxr3SpuPalette & pal,
				   sDxr3SpuRect & size) const
{
    int h = size.height();
    int w = size.width();

    if (size.y1 + h >= spuYres)
    {
	h = spuYres - size.y1 - 1;
    }
    if (size.x1 + w >= spuXres)
    {
	w = spuXres - size.x1 - 1;
    }

    if (w & 0x03)
    {
	w += 4 - (w & 0x03);
    }

    cBitmap *ret = new cBitmap(w, h, 2);

    // set the palette
    for (int i = 0; i < 4; i++)
    {
	uint32_t color = pal.getColor(paldescr[i].index, paldescr[i].trans);
	ret->SetColor(i, (tColor) color);
    }

    // set the content
    for (int yp = 0; yp < h; yp++)
    {
	for (int xp = 0; xp < w; xp++)
	{
	    uint8_t idx = bmp[(size.y1 + yp) * spuXres + size.x1 + xp];
	    ret->SetIndex(xp, yp, idx);
	}
    }
    return ret;
}

// ==================================
// find the minimum non-transparent area
bool cDxr3SpuBitmap::getMinSize(const aDxr3SpuPalDescr paldescr,
				sDxr3SpuRect & size) const
{
    bool ret = false;
    for (int i = 0; i < 4; i++)
    {
	if (paldescr[i].trans != 0)
	{
	    if (!ret)
	    {
		size = minsize[i];
	    }
	    else
	    {
		setMin(size.x1, minsize[i].x1);
		setMin(size.y1, minsize[i].y1);
		setMax(size.x2, minsize[i].x2);
		setMax(size.y2, minsize[i].y2);
	    }
	    ret = true;
	}
    }
    dsyslog("dxr3: getminsize: (%d,%d) x (%d,%d)",
	    size.x1, size.y1, size.x2, size.y2);
    if (size.x1 > size.x2 || size.y1 > size.y2)
	return false;
    return ret;
}

// ==================================
void cDxr3SpuBitmap::putPixel(int xp, int yp, int len, uint8_t colorid)
{
    memset(bmp + spuXres * yp + xp, colorid, len);
    setMin(minsize[colorid].x1, xp);
    setMin(minsize[colorid].y1, yp);
    setMax(minsize[colorid].x2, xp + len - 1);
    setMax(minsize[colorid].y2, yp + len - 1);
}

// ==================================
static uint8_t getBits(uint8_t * &data, uint8_t & bitf)
{
    uint8_t ret = *data;
    if (bitf)
    {
	ret >>= 4;
    }
    else
    {
	data++;
    }

    bitf ^= 1;

    return (ret & 0xf);
}

// ==================================
void cDxr3SpuBitmap::putFieldData(int field, uint8_t * data, uint8_t * endp)
{
    int xp = bmpsize.x1;
    int yp = bmpsize.y1 + field;
    uint8_t bitf = 1;

    while (data < endp)
    {
	uint16_t vlc = getBits(data, bitf);
	if (vlc < 0x0004)
	{
	    vlc = (vlc << 4) | getBits(data, bitf);
	    if (vlc < 0x0010)
	    {
		vlc = (vlc << 4) | getBits(data, bitf);
		if (vlc < 0x0040)
		{
		    vlc = (vlc << 4) | getBits(data, bitf);
		}
	    }
	}

	uint8_t color = vlc & 0x03;
	int len = vlc >> 2;

	// if len == 0 -> end sequence - fill to end of line
	len = len ? len : bmpsize.x2 - xp + 1;
	putPixel(xp, yp, len, color);
	xp += len;

	if (xp > bmpsize.x2)
	{
	    // nextLine
	    if (!bitf)
		data++;
	    bitf = 1;
	    xp = bmpsize.x1;
	    yp += 2;
	    if (yp > bmpsize.y2)
		return;
	}
    }
}

// ==================================
// ! constructor
cDxr3SpuDecoder::cDxr3SpuDecoder()
{
    clean = true;
    scaleMode = eSpuNormal;
    spu = NULL;
    osd = NULL;
    spubmp = NULL;
    allowedShow = true;
}

// ==================================
cDxr3SpuDecoder::~cDxr3SpuDecoder()
{
    delete spubmp;
    delete spu;
    delete osd;
}

// ==================================
// ! send spu data to dxr3
void cDxr3SpuDecoder::processSPU(uint32_t pts, uint8_t * buf, bool AllowedShow)
{
    setTime(pts);
    dsyslog("dxr3: spudec push: pts=%d", pts);

    delete spubmp;
    spubmp = NULL;
    delete[]spu;
    spu = buf;
    spupts = pts;

    DCSQ_offset = cmdOffs();
    prev_DCSQ_offset = 0;

    clean = true;
    allowedShow = AllowedShow;
}

// ==================================
// ! get scalemode
cSpuDecoder::eScaleMode cDxr3SpuDecoder::getScaleMode(void)
{
    return scaleMode;
}

// ==================================
// ! set scalemode
void cDxr3SpuDecoder::setScaleMode(cSpuDecoder::eScaleMode ScaleMode)
{
    scaleMode = ScaleMode;
}

// ==================================
// ! send palette to dxr3
void cDxr3SpuDecoder::setPalette(uint32_t * pal)
{
    palette.setPalette(pal);
}

// ==================================
// ! send highlight to dxr3
void cDxr3SpuDecoder::setHighlight(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t palette)
{
    aDxr3SpuPalDescr pld;
    for (int i = 0; i < 4; i++)
    {
	pld[i].index = 0xf & (palette >> (16 + 4 * i));
	pld[i].trans = 0xf & (palette >> (4 * i));
    }

    bool ne = hlpsize.x1 != sx || hlpsize.y1 != sy ||
	hlpsize.x2 != ex || hlpsize.y2 != ey ||
	pld[0] != hlpDescr[0] || pld[1] != hlpDescr[1] ||
	pld[2] != hlpDescr[2] || pld[3] != hlpDescr[3];

    if (ne)
    {
	dsyslog("dxr3: spu highlight: %d, %d, %d, %d", sx, sy, ex, ey);

	hlpsize.x1 = sx;
	hlpsize.y1 = sy;
	hlpsize.x2 = ex;
	hlpsize.y2 = ey;
	memcpy(hlpDescr, pld, sizeof(aDxr3SpuPalDescr));
	highlight = true;
	clean = false;
    }
}

// ==================================
// ! clear highlight
void cDxr3SpuDecoder::clearHighlight()
{
    clean &= !highlight;
    highlight = false;
    hlpsize.x1 = -1;
    hlpsize.y1 = -1;
    hlpsize.x2 = -1;
    hlpsize.y2 = -1;
}

// ==================================
int cDxr3SpuDecoder::ScaleYcoord(int value)
{
    if (scaleMode == eSpuLetterBox)
    {
	int offset =
	    cDevice::PrimaryDevice()->GetVideoSystem() == vsPAL ? 72 : 60;
	return lround((value * 3.0) / 4.0) + offset;
    }
    return value;
}

// ==================================
int cDxr3SpuDecoder::ScaleYres(int value)
{
    if (scaleMode == eSpuLetterBox)
    {
	return lround((value * 3.0) / 4.0);
    }
    return value;
}

// ==================================
void cDxr3SpuDecoder::DrawBmp(sDxr3SpuRect & size, cBitmap * bmp)
{
    int x2 = size.x2;
    while ((x2 - size.x1 + 1) & 0x03)
	x2++;
    tArea Area = { size.x1, size.y1, x2, size.y2, 2 };
    osd->SetAreas(&Area, 1);
    if (x2 > size.x2)
	osd->DrawRectangle(size.x2 + 1, size.y1, x2, size.y2, clrTransparent);
    osd->DrawBitmap(size.x1, size.y1, *bmp);
    delete bmp;
}

// ==================================
// ! draw nav, subtitles, ...
void cDxr3SpuDecoder::Draw()
{
    Hide();

    if (!spubmp)
    {
	return;
    }

    cBitmap *fg = NULL;
    cBitmap *bg = NULL;
    sDxr3SpuRect bgsize;
    sDxr3SpuRect hlsize;

    hlsize.x1 = hlpsize.x1;
    hlsize.y1 = ScaleYcoord(hlpsize.y1);
    hlsize.x2 = hlpsize.x2;
    hlsize.y2 = ScaleYcoord(hlpsize.y2);

    if (highlight)
    {
	fg = spubmp->getBitmap(hlpDescr, palette, hlsize);
    }

    if (spubmp->getMinSize(palDescr, bgsize))
    {
	bg = spubmp->getBitmap(palDescr, palette, bgsize);
	if (scaleMode == eSpuLetterBox)
	{
	    // the coordinates have to be modified for letterbox
	    int y1 = ScaleYres(bgsize.y1) + bgsize.height();
	    bgsize.y2 = y1 + bgsize.height();
	    bgsize.y1 = y1;
	}
    }

    if (bg || fg)
    {
	if (osd == NULL)
	    if ((osd = cOsdProvider::NewOsd(0, 0)) == NULL)
	    {
		esyslog("dxr3: could not instantiate new OSD");
		return;
	    }

	if (fg)
	{
	    DrawBmp(hlsize, fg);
	}

	if (bg)
	{
	    DrawBmp(bgsize, bg);
	}

	osd->Flush();
    }

    clean = true;
}

// ==================================
// ! hide nav, subtitles, ...
void cDxr3SpuDecoder::Hide()
{
    delete osd;
    osd = NULL;
}

// ==================================
// ! clear highlight and osd
void cDxr3SpuDecoder::Empty()
{
    Hide();

    delete spubmp;
    spubmp = NULL;

    delete[]spu;
    spu = NULL;

    clearHighlight();
    clean = true;
}

// ==================================
// ! set pts
int cDxr3SpuDecoder::setTime(uint32_t pts)
{
    if (!spu)
    {
	return 0;
    }

    if (spu && !clean)
    {
	Draw();
    }

    while (DCSQ_offset != prev_DCSQ_offset)
    {
	// Display Control Sequences
	int i = DCSQ_offset;
	state = spNONE;

	uint32_t exec_time = spupts + spuU32(i) * 1024;
	if ((pts != 0) && (exec_time > pts))
	{
	    return 0;
	}

	if (pts != 0)
	{
	    uint16_t feven = 0;
	    uint16_t fodd = 0;

	    i += 2;

	    prev_DCSQ_offset = DCSQ_offset;
	    DCSQ_offset = spuU32(i);
	    i += 2;

	    while (spu[i] != CMD_SPU_EOF)
	    {
		// Command Sequence
		switch (spu[i])
		{
		case CMD_SPU_SHOW:
		    // show subpicture
		    dsyslog("dxr3: spu show");
		    state = spSHOW;
		    i++;
		    break;

		case CMD_SPU_HIDE:
		    // hide subpicture
		    dsyslog("dxr3: spu hide");
		    state = spHIDE;
		    i++;
		    break;

		case CMD_SPU_SET_PALETTE:
		    // CLUT
		    palDescr[0].index = spu[i + 2] & 0xf;
		    palDescr[1].index = spu[i + 2] >> 4;
		    palDescr[2].index = spu[i + 1] & 0xf;
		    palDescr[3].index = spu[i + 1] >> 4;
		    i += 3;
		    break;

		case CMD_SPU_SET_ALPHA:
		    // transparency palette
		    palDescr[0].trans = spu[i + 2] & 0xf;
		    palDescr[1].trans = spu[i + 2] >> 4;
		    palDescr[2].trans = spu[i + 1] & 0xf;
		    palDescr[3].trans = spu[i + 1] >> 4;
		    i += 3;
		    break;

		case CMD_SPU_SET_SIZE:
		    // image coordinates
		    size.x1 = (spu[i + 1] << 4) | (spu[i + 2] >> 4);
		    size.x2 = ((spu[i + 2] & 0x0f) << 8) | spu[i + 3];

		    size.y1 = (spu[i + 4] << 4) | (spu[i + 5] >> 4);
		    size.y2 = ((spu[i + 5] & 0x0f) << 8) | spu[i + 6];

		    dsyslog("dxr3: spu size (%d,%d) x (%d,%d)",
			    size.x1, size.y1, size.x2, size.y2);
		    i += 7;
		    break;

		case CMD_SPU_SET_PXD_OFFSET:
		    // image 1 / image 2 offsets
		    fodd = spuU32(i + 1);
		    feven = spuU32(i + 3);

		    dsyslog("dxr3: spu offset: odd=%d, even=%d", fodd, feven);
		    i += 5;
		    break;

		case CMD_SPU_CHG_COLCON:
		{
		    int size = spuU32(i + 1);
		    i += 1 + size;
		}
		break;

		case CMD_SPU_MENU:
		    dsyslog("dxr3: spu menu");
		    state = spMENU;
		    i++;
		    break;

		default:
		    esyslog("dxr3: invalid sequence in control header (%.2x)",
			    spu[i]);
		    assert(0);
		    i++;
		    break;
		}
	    }
	    if (fodd != 0 && feven != 0)
	    {
		delete spubmp;
		spubmp = new cDxr3SpuBitmap(size, spu + fodd, spu + feven,
					    spu + feven, spu + cmdOffs());
	    }
	}
	else if (!clean)
	{
	    state = spSHOW;
	}

	if ((state == spSHOW && allowedShow) || state == spMENU)
	{
	    Draw();
	}

	if (state == spHIDE)
	{
	    Hide();
	}

	if (pts == 0)
	{
	    return 0;
	}
    }

    return 1;
}

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// c-basic-offset: 4
// indent-tabs-mode: nil
// End: