Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dvbsubdec.c @ 0d02cacd

History | View | Annotate | Download (38.8 KB)

1 c6ec28b1 Michael Niedermayer
/*
2
 * DVB subtitle decoding for ffmpeg
3
 * Copyright (c) 2005 Ian Caulfield.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 c6ec28b1 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 c6ec28b1 Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 c6ec28b1 Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 c6ec28b1 Michael Niedermayer
 */
21
#include "avcodec.h"
22
#include "dsputil.h"
23
#include "bitstream.h"
24
25
//#define DEBUG
26
//#define DEBUG_PACKET_CONTENTS
27
//#define DEBUG_SAVE_IMAGES
28
29
#define DVBSUB_PAGE_SEGMENT     0x10
30
#define DVBSUB_REGION_SEGMENT   0x11
31
#define DVBSUB_CLUT_SEGMENT     0x12
32
#define DVBSUB_OBJECT_SEGMENT   0x13
33
#define DVBSUB_DISPLAY_SEGMENT  0x80
34
35 0c3c674c Michael Niedermayer
#define cm (ff_cropTbl + MAX_NEG_CROP)
36 c6ec28b1 Michael Niedermayer
37
#ifdef DEBUG_SAVE_IMAGES
38
#undef fprintf
39
#if 0
40
static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
41
                     uint32_t *rgba_palette)
42
{
43
    int x, y, v;
44
    FILE *f;
45
    char fname[40], fname2[40];
46
    char command[1024];
47 115329f1 Diego Biurrun

48 c6ec28b1 Michael Niedermayer
    snprintf(fname, 40, "%s.ppm", filename);
49

50
    f = fopen(fname, "w");
51
    if (!f) {
52
        perror(fname);
53
        exit(1);
54
    }
55
    fprintf(f, "P6\n"
56
            "%d %d\n"
57
            "%d\n",
58
            w, h, 255);
59
    for(y = 0; y < h; y++) {
60
        for(x = 0; x < w; x++) {
61
            v = rgba_palette[bitmap[y * w + x]];
62
            putc((v >> 16) & 0xff, f);
63
            putc((v >> 8) & 0xff, f);
64
            putc((v >> 0) & 0xff, f);
65
        }
66
    }
67
    fclose(f);
68 115329f1 Diego Biurrun

69

70 c6ec28b1 Michael Niedermayer
    snprintf(fname2, 40, "%s-a.pgm", filename);
71

72
    f = fopen(fname2, "w");
73
    if (!f) {
74
        perror(fname2);
75
        exit(1);
76
    }
77
    fprintf(f, "P5\n"
78
            "%d %d\n"
79
            "%d\n",
80
            w, h, 255);
81
    for(y = 0; y < h; y++) {
82
        for(x = 0; x < w; x++) {
83
            v = rgba_palette[bitmap[y * w + x]];
84
            putc((v >> 24) & 0xff, f);
85
        }
86
    }
87
    fclose(f);
88 115329f1 Diego Biurrun

89 c6ec28b1 Michael Niedermayer
    snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90
    system(command);
91 115329f1 Diego Biurrun

92 c6ec28b1 Michael Niedermayer
    snprintf(command, 1024, "rm %s %s", fname, fname2);
93
    system(command);
94
}
95
#endif
96
97
static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
98
{
99
    int x, y, v;
100
    FILE *f;
101
    char fname[40], fname2[40];
102
    char command[1024];
103 115329f1 Diego Biurrun
104 c6ec28b1 Michael Niedermayer
    snprintf(fname, 40, "%s.ppm", filename);
105
106
    f = fopen(fname, "w");
107
    if (!f) {
108
        perror(fname);
109
        exit(1);
110
    }
111
    fprintf(f, "P6\n"
112
            "%d %d\n"
113
            "%d\n",
114
            w, h, 255);
115
    for(y = 0; y < h; y++) {
116
        for(x = 0; x < w; x++) {
117
            v = bitmap[y * w + x];
118
            putc((v >> 16) & 0xff, f);
119
            putc((v >> 8) & 0xff, f);
120
            putc((v >> 0) & 0xff, f);
121
        }
122
    }
123
    fclose(f);
124 115329f1 Diego Biurrun
125
126 c6ec28b1 Michael Niedermayer
    snprintf(fname2, 40, "%s-a.pgm", filename);
127
128
    f = fopen(fname2, "w");
129
    if (!f) {
130
        perror(fname2);
131
        exit(1);
132
    }
133
    fprintf(f, "P5\n"
134
            "%d %d\n"
135
            "%d\n",
136
            w, h, 255);
137
    for(y = 0; y < h; y++) {
138
        for(x = 0; x < w; x++) {
139
            v = bitmap[y * w + x];
140
            putc((v >> 24) & 0xff, f);
141
        }
142
    }
143
    fclose(f);
144 115329f1 Diego Biurrun
145 c6ec28b1 Michael Niedermayer
    snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
146
    system(command);
147 115329f1 Diego Biurrun
148 c6ec28b1 Michael Niedermayer
    snprintf(command, 1024, "rm %s %s", fname, fname2);
149
    system(command);
150
}
151
#endif
152
153
#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
154
155
typedef struct DVBSubCLUT {
156
    int id;
157
158
    uint32_t clut4[4];
159
    uint32_t clut16[16];
160
    uint32_t clut256[256];
161 115329f1 Diego Biurrun
162 c6ec28b1 Michael Niedermayer
    struct DVBSubCLUT *next;
163
} DVBSubCLUT;
164
165
static DVBSubCLUT default_clut;
166
167
typedef struct DVBSubObjectDisplay {
168
    int object_id;
169
    int region_id;
170
171
    int x_pos;
172
    int y_pos;
173
174
    int fgcolour;
175
    int bgcolour;
176 115329f1 Diego Biurrun
177 c6ec28b1 Michael Niedermayer
    struct DVBSubObjectDisplay *region_list_next;
178 115329f1 Diego Biurrun
    struct DVBSubObjectDisplay *object_list_next;
179 c6ec28b1 Michael Niedermayer
} DVBSubObjectDisplay;
180
181
typedef struct DVBSubObject {
182
    int id;
183
184
    int type;
185 115329f1 Diego Biurrun
186
    DVBSubObjectDisplay *display_list;
187
188 c6ec28b1 Michael Niedermayer
    struct DVBSubObject *next;
189
} DVBSubObject;
190
191
typedef struct DVBSubRegionDisplay {
192
    int region_id;
193
194
    int x_pos;
195
    int y_pos;
196
197
    struct DVBSubRegionDisplay *next;
198
} DVBSubRegionDisplay;
199
200
typedef struct DVBSubRegion {
201
    int id;
202
203
    int width;
204
    int height;
205
    int depth;
206 115329f1 Diego Biurrun
207 c6ec28b1 Michael Niedermayer
    int clut;
208
    int bgcolour;
209 115329f1 Diego Biurrun
210 c6ec28b1 Michael Niedermayer
    uint8_t *pbuf;
211
    int buf_size;
212
213
    DVBSubObjectDisplay *display_list;
214 115329f1 Diego Biurrun
215 c6ec28b1 Michael Niedermayer
    struct DVBSubRegion *next;
216
} DVBSubRegion;
217
218
typedef struct DVBSubContext {
219
    int composition_id;
220
    int ancillary_id;
221
222
    int time_out;
223
    DVBSubRegion *region_list;
224
    DVBSubCLUT   *clut_list;
225
    DVBSubObject *object_list;
226 115329f1 Diego Biurrun
227 c6ec28b1 Michael Niedermayer
    int display_list_size;
228
    DVBSubRegionDisplay *display_list;
229
} DVBSubContext;
230
231
232
static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
233
{
234
    DVBSubObject *ptr = ctx->object_list;
235
236
    while (ptr != NULL && ptr->id != object_id) {
237
        ptr = ptr->next;
238
    }
239 115329f1 Diego Biurrun
240 c6ec28b1 Michael Niedermayer
    return ptr;
241
}
242
243
static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
244
{
245
    DVBSubCLUT *ptr = ctx->clut_list;
246
247
    while (ptr != NULL && ptr->id != clut_id) {
248
        ptr = ptr->next;
249
    }
250 115329f1 Diego Biurrun
251 c6ec28b1 Michael Niedermayer
    return ptr;
252
}
253
254
static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
255
{
256
    DVBSubRegion *ptr = ctx->region_list;
257
258
    while (ptr != NULL && ptr->id != region_id) {
259
        ptr = ptr->next;
260
    }
261 115329f1 Diego Biurrun
262 c6ec28b1 Michael Niedermayer
    return ptr;
263
}
264
265
static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
266
{
267
    DVBSubObject *object, *obj2, **obj2_ptr;
268
    DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
269
270
    while (region->display_list != NULL) {
271
        display = region->display_list;
272 115329f1 Diego Biurrun
273 c6ec28b1 Michael Niedermayer
        object = get_object(ctx, display->object_id);
274 115329f1 Diego Biurrun
275 c6ec28b1 Michael Niedermayer
        if (object != NULL) {
276
            obj_disp = object->display_list;
277
            obj_disp_ptr = &object->display_list;
278 115329f1 Diego Biurrun
279 c6ec28b1 Michael Niedermayer
            while (obj_disp != NULL && obj_disp != display) {
280
                obj_disp_ptr = &obj_disp->object_list_next;
281
                obj_disp = obj_disp->object_list_next;
282
            }
283 115329f1 Diego Biurrun
284 c6ec28b1 Michael Niedermayer
            if (obj_disp) {
285
                *obj_disp_ptr = obj_disp->object_list_next;
286 115329f1 Diego Biurrun
287 c6ec28b1 Michael Niedermayer
                if (object->display_list == NULL) {
288
                    obj2 = ctx->object_list;
289
                    obj2_ptr = &ctx->object_list;
290
291
                    while (obj2 != NULL && obj2 != object) {
292
                        obj2_ptr = &obj2->next;
293
                        obj2 = obj2->next;
294
                    }
295 115329f1 Diego Biurrun
296 c6ec28b1 Michael Niedermayer
                    *obj2_ptr = obj2->next;
297 115329f1 Diego Biurrun
298 c6ec28b1 Michael Niedermayer
                    av_free(obj2);
299
                }
300
            }
301
        }
302 115329f1 Diego Biurrun
303 c6ec28b1 Michael Niedermayer
        region->display_list = display->region_list_next;
304 115329f1 Diego Biurrun
305 c6ec28b1 Michael Niedermayer
        av_free(display);
306
    }
307 115329f1 Diego Biurrun
308 c6ec28b1 Michael Niedermayer
}
309
310
static void delete_state(DVBSubContext *ctx)
311
{
312
    DVBSubRegion *region;
313
    DVBSubCLUT *clut;
314 115329f1 Diego Biurrun
315 c6ec28b1 Michael Niedermayer
    while (ctx->region_list != NULL)
316
    {
317
        region = ctx->region_list;
318
319
        ctx->region_list = region->next;
320
321
        delete_region_display_list(ctx, region);
322
        if (region->pbuf != NULL)
323
            av_free(region->pbuf);
324
325
        av_free(region);
326
    }
327
328
    while (ctx->clut_list != NULL)
329
    {
330
        clut = ctx->clut_list;
331
332
        ctx->clut_list = clut->next;
333
334
        av_free(clut);
335
    }
336
337
    /* Should already be null */
338
    if (ctx->object_list != NULL)
339
        av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
340
}
341
342
static int dvbsub_init_decoder(AVCodecContext *avctx)
343
{
344
    int i, r, g, b, a = 0;
345
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
346
347
    memset(avctx->priv_data, 0, sizeof(DVBSubContext));
348 115329f1 Diego Biurrun
349 c6ec28b1 Michael Niedermayer
    ctx->composition_id = avctx->sub_id & 0xffff;
350
    ctx->ancillary_id = avctx->sub_id >> 16;
351
352
    default_clut.id = -1;
353
    default_clut.next = NULL;
354
355
    default_clut.clut4[0] = RGBA(  0,   0,   0,   0);
356
    default_clut.clut4[1] = RGBA(255, 255, 255, 255);
357
    default_clut.clut4[2] = RGBA(  0,   0,   0, 255);
358
    default_clut.clut4[3] = RGBA(127, 127, 127, 255);
359
360
    default_clut.clut16[0] = RGBA(  0,   0,   0,   0);
361
    for (i = 1; i < 16; i++) {
362
        if (i < 8) {
363
            r = (i & 1) ? 255 : 0;
364
            g = (i & 2) ? 255 : 0;
365
            b = (i & 4) ? 255 : 0;
366
        } else {
367
            r = (i & 1) ? 127 : 0;
368
            g = (i & 2) ? 127 : 0;
369
            b = (i & 4) ? 127 : 0;
370 115329f1 Diego Biurrun
        }
371 c6ec28b1 Michael Niedermayer
        default_clut.clut16[i] = RGBA(r, g, b, 255);
372
    }
373
374
    default_clut.clut256[0] = RGBA(  0,   0,   0,   0);
375
    for (i = 1; i < 256; i++) {
376
        if (i < 8) {
377
            r = (i & 1) ? 255 : 0;
378
            g = (i & 2) ? 255 : 0;
379
            b = (i & 4) ? 255 : 0;
380
            a = 63;
381
        } else {
382
            switch (i & 0x88) {
383
            case 0x00:
384
                r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
385
                g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
386
                b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
387
                a = 255;
388
                break;
389
            case 0x08:
390
                r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
391
                g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
392
                b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
393
                a = 127;
394
                break;
395
            case 0x80:
396
                r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
397
                g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
398
                b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
399
                a = 255;
400
                break;
401
            case 0x88:
402
                r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
403
                g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
404
                b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
405
                a = 255;
406
                break;
407
            }
408 115329f1 Diego Biurrun
        }
409 c6ec28b1 Michael Niedermayer
        default_clut.clut256[i] = RGBA(r, g, b, a);
410
    }
411
412
    return 0;
413
}
414
415
static int dvbsub_close_decoder(AVCodecContext *avctx)
416
{
417
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
418
    DVBSubRegionDisplay *display;
419
420
    delete_state(ctx);
421 115329f1 Diego Biurrun
422 c6ec28b1 Michael Niedermayer
    while (ctx->display_list != NULL)
423
    {
424
        display = ctx->display_list;
425
        ctx->display_list = display->next;
426 115329f1 Diego Biurrun
427 c6ec28b1 Michael Niedermayer
        av_free(display);
428
    }
429
430
    return 0;
431
}
432
433 115329f1 Diego Biurrun
static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
434
                                   uint8_t **srcbuf, int buf_size,
435 c6ec28b1 Michael Niedermayer
                                   int non_mod, uint8_t *map_table)
436
{
437
    GetBitContext gb;
438 115329f1 Diego Biurrun
439 c6ec28b1 Michael Niedermayer
    int bits;
440
    int run_length;
441
    int pixels_read = 0;
442 115329f1 Diego Biurrun
443 c6ec28b1 Michael Niedermayer
    init_get_bits(&gb, *srcbuf, buf_size << 8);
444 115329f1 Diego Biurrun
445 c6ec28b1 Michael Niedermayer
    while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
446
        bits = get_bits(&gb, 2);
447
448
        if (bits != 0) {
449
            if (non_mod != 1 || bits != 1) {
450
                if (map_table != NULL)
451
                    *destbuf++ = map_table[bits];
452
                else
453
                    *destbuf++ = bits;
454
            }
455
            pixels_read++;
456
        } else {
457
            bits = get_bits(&gb, 1);
458
            if (bits == 1) {
459
                run_length = get_bits(&gb, 3) + 3;
460
                bits = get_bits(&gb, 2);
461 115329f1 Diego Biurrun
462 c6ec28b1 Michael Niedermayer
                if (non_mod == 1 && bits == 1)
463
                    pixels_read += run_length;
464
                else {
465
                    if (map_table != NULL)
466
                        bits = map_table[bits];
467
                    while (run_length-- > 0 && pixels_read < dbuf_len) {
468
                        *destbuf++ = bits;
469
                        pixels_read++;
470
                    }
471
                }
472
            } else {
473
                bits = get_bits(&gb, 1);
474
                if (bits == 0) {
475
                    bits = get_bits(&gb, 2);
476
                    if (bits == 2) {
477
                        run_length = get_bits(&gb, 4) + 12;
478
                        bits = get_bits(&gb, 2);
479
480
                        if (non_mod == 1 && bits == 1)
481
                            pixels_read += run_length;
482
                        else {
483
                            if (map_table != NULL)
484
                                bits = map_table[bits];
485
                            while (run_length-- > 0 && pixels_read < dbuf_len) {
486
                                *destbuf++ = bits;
487
                                pixels_read++;
488
                            }
489
                        }
490
                    } else if (bits == 3) {
491
                        run_length = get_bits(&gb, 8) + 29;
492
                        bits = get_bits(&gb, 2);
493
494
                        if (non_mod == 1 && bits == 1)
495
                            pixels_read += run_length;
496
                        else {
497
                            if (map_table != NULL)
498
                                bits = map_table[bits];
499
                            while (run_length-- > 0 && pixels_read < dbuf_len) {
500
                                *destbuf++ = bits;
501
                                pixels_read++;
502
                            }
503
                        }
504
                    } else if (bits == 1) {
505
                        pixels_read += 2;
506
                        if (map_table != NULL)
507
                            bits = map_table[0];
508
                        else
509
                            bits = 0;
510
                        if (pixels_read <= dbuf_len) {
511
                            *destbuf++ = bits;
512
                            *destbuf++ = bits;
513
                        }
514
                    } else {
515
                        (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
516
                        return pixels_read;
517
                    }
518
                } else {
519
                    if (map_table != NULL)
520
                        bits = map_table[0];
521
                    else
522
                        bits = 0;
523
                    *destbuf++ = bits;
524
                    pixels_read++;
525
                }
526
            }
527
        }
528
    }
529 115329f1 Diego Biurrun
530 c6ec28b1 Michael Niedermayer
    if (get_bits(&gb, 6) != 0)
531
        av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
532
533
    (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
534
535
    return pixels_read;
536
}
537 115329f1 Diego Biurrun
538
static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
539
                                   uint8_t **srcbuf, int buf_size,
540 c6ec28b1 Michael Niedermayer
                                   int non_mod, uint8_t *map_table)
541
{
542
    GetBitContext gb;
543 115329f1 Diego Biurrun
544 c6ec28b1 Michael Niedermayer
    int bits;
545
    int run_length;
546
    int pixels_read = 0;
547 115329f1 Diego Biurrun
548
    init_get_bits(&gb, *srcbuf, buf_size << 8);
549
550 c6ec28b1 Michael Niedermayer
    while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
551
        bits = get_bits(&gb, 4);
552
553
        if (bits != 0) {
554
            if (non_mod != 1 || bits != 1) {
555
                if (map_table != NULL)
556
                    *destbuf++ = map_table[bits];
557
                else
558
                    *destbuf++ = bits;
559
            }
560
            pixels_read++;
561
        } else {
562
            bits = get_bits(&gb, 1);
563
            if (bits == 0) {
564
                run_length = get_bits(&gb, 3);
565 115329f1 Diego Biurrun
566 c6ec28b1 Michael Niedermayer
                if (run_length == 0) {
567
                    (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
568
                    return pixels_read;
569
                }
570 115329f1 Diego Biurrun
571 c6ec28b1 Michael Niedermayer
                run_length += 2;
572 115329f1 Diego Biurrun
573 c6ec28b1 Michael Niedermayer
                if (map_table != NULL)
574
                    bits = map_table[0];
575
                else
576
                    bits = 0;
577 115329f1 Diego Biurrun
578 c6ec28b1 Michael Niedermayer
                while (run_length-- > 0 && pixels_read < dbuf_len) {
579
                    *destbuf++ = bits;
580
                    pixels_read++;
581
                }
582
            } else {
583
                bits = get_bits(&gb, 1);
584
                if (bits == 0) {
585
                    run_length = get_bits(&gb, 2) + 4;
586
                    bits = get_bits(&gb, 4);
587
588
                    if (non_mod == 1 && bits == 1)
589
                        pixels_read += run_length;
590
                    else {
591
                        if (map_table != NULL)
592
                            bits = map_table[bits];
593
                        while (run_length-- > 0 && pixels_read < dbuf_len) {
594
                            *destbuf++ = bits;
595
                            pixels_read++;
596
                        }
597
                    }
598
                } else {
599
                    bits = get_bits(&gb, 2);
600
                    if (bits == 2) {
601
                        run_length = get_bits(&gb, 4) + 9;
602
                        bits = get_bits(&gb, 4);
603 115329f1 Diego Biurrun
604 c6ec28b1 Michael Niedermayer
                        if (non_mod == 1 && bits == 1)
605
                            pixels_read += run_length;
606
                        else {
607
                            if (map_table != NULL)
608
                                bits = map_table[bits];
609
                            while (run_length-- > 0 && pixels_read < dbuf_len) {
610
                                *destbuf++ = bits;
611
                                pixels_read++;
612
                            }
613
                        }
614
                    } else if (bits == 3) {
615
                        run_length = get_bits(&gb, 8) + 25;
616
                        bits = get_bits(&gb, 4);
617
618
                        if (non_mod == 1 && bits == 1)
619
                            pixels_read += run_length;
620
                        else {
621
                            if (map_table != NULL)
622
                                bits = map_table[bits];
623
                            while (run_length-- > 0 && pixels_read < dbuf_len) {
624
                                *destbuf++ = bits;
625
                                pixels_read++;
626
                            }
627
                        }
628
                    } else if (bits == 1) {
629
                        pixels_read += 2;
630
                        if (map_table != NULL)
631
                            bits = map_table[0];
632
                        else
633
                            bits = 0;
634
                        if (pixels_read <= dbuf_len) {
635
                            *destbuf++ = bits;
636
                            *destbuf++ = bits;
637
                        }
638
                    } else {
639
                        if (map_table != NULL)
640
                            bits = map_table[0];
641
                        else
642
                            bits = 0;
643
                        *destbuf++ = bits;
644
                        pixels_read ++;
645
                    }
646
                }
647
            }
648
        }
649
    }
650 115329f1 Diego Biurrun
651 c6ec28b1 Michael Niedermayer
    if (get_bits(&gb, 8) != 0)
652
        av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
653 115329f1 Diego Biurrun
654 c6ec28b1 Michael Niedermayer
    (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
655
656
    return pixels_read;
657
}
658 115329f1 Diego Biurrun
659
static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
660
                                    uint8_t **srcbuf, int buf_size,
661 c6ec28b1 Michael Niedermayer
                                    int non_mod, uint8_t *map_table)
662
{
663
    uint8_t *sbuf_end = (*srcbuf) + buf_size;
664
    int bits;
665
    int run_length;
666
    int pixels_read = 0;
667 115329f1 Diego Biurrun
668 c6ec28b1 Michael Niedermayer
    while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
669
        bits = *(*srcbuf)++;
670 115329f1 Diego Biurrun
671 c6ec28b1 Michael Niedermayer
        if (bits != 0) {
672
            if (non_mod != 1 || bits != 1) {
673
                if (map_table != NULL)
674
                    *destbuf++ = map_table[bits];
675
                else
676
                    *destbuf++ = bits;
677
            }
678
            pixels_read++;
679
        } else {
680
            bits = *(*srcbuf)++;
681
            run_length = bits & 0x7f;
682
            if ((bits & 0x80) == 0) {
683
                if (run_length == 0) {
684
                    return pixels_read;
685
                }
686 115329f1 Diego Biurrun
687 c6ec28b1 Michael Niedermayer
                if (map_table != NULL)
688
                    bits = map_table[0];
689
                else
690
                    bits = 0;
691
                while (run_length-- > 0 && pixels_read < dbuf_len) {
692
                    *destbuf++ = bits;
693
                    pixels_read++;
694
                }
695
            } else {
696
                bits = *(*srcbuf)++;
697
698
                if (non_mod == 1 && bits == 1)
699
                    pixels_read += run_length;
700
                if (map_table != NULL)
701
                    bits = map_table[bits];
702
                else while (run_length-- > 0 && pixels_read < dbuf_len) {
703
                    *destbuf++ = bits;
704
                    pixels_read++;
705
                }
706
            }
707
        }
708
    }
709 115329f1 Diego Biurrun
710 c6ec28b1 Michael Niedermayer
    if (*(*srcbuf)++ != 0)
711
        av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
712 115329f1 Diego Biurrun
713 c6ec28b1 Michael Niedermayer
    return pixels_read;
714
}
715 115329f1 Diego Biurrun
716 c6ec28b1 Michael Niedermayer
717
718
static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
719
                                          uint8_t *buf, int buf_size, int top_bottom, int non_mod)
720
{
721
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
722
723
    DVBSubRegion *region = get_region(ctx, display->region_id);
724
    uint8_t *buf_end = buf + buf_size;
725
    uint8_t *pbuf;
726
    int x_pos, y_pos;
727
    int i;
728 115329f1 Diego Biurrun
729 c6ec28b1 Michael Niedermayer
    uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
730
    uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
731 115329f1 Diego Biurrun
    uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
732 c6ec28b1 Michael Niedermayer
                         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
733
    uint8_t *map_table;
734 115329f1 Diego Biurrun
735 c6ec28b1 Michael Niedermayer
#ifdef DEBUG
736
    av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
737
                top_bottom ? "bottom" : "top");
738
#endif
739
740
#ifdef DEBUG_PACKET_CONTENTS
741
    for (i = 0; i < buf_size; i++)
742
    {
743
        if (i % 16 == 0)
744
            av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
745
746
        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
747
        if (i % 16 == 15)
748
            av_log(avctx, AV_LOG_INFO, "\n");
749
    }
750 115329f1 Diego Biurrun
751 c6ec28b1 Michael Niedermayer
    if (i % 16 != 0)
752
        av_log(avctx, AV_LOG_INFO, "\n");
753
754
#endif
755
756
    if (region == 0)
757
        return;
758 115329f1 Diego Biurrun
759 c6ec28b1 Michael Niedermayer
    pbuf = region->pbuf;
760 115329f1 Diego Biurrun
761 c6ec28b1 Michael Niedermayer
    x_pos = display->x_pos;
762
    y_pos = display->y_pos;
763 115329f1 Diego Biurrun
764 c6ec28b1 Michael Niedermayer
    if ((y_pos & 1) != top_bottom)
765
        y_pos++;
766
767
    while (buf < buf_end) {
768
        if (x_pos > region->width || y_pos > region->height) {
769
            av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
770
            return;
771
        }
772 115329f1 Diego Biurrun
773 c6ec28b1 Michael Niedermayer
        switch (*buf++) {
774
        case 0x10:
775
            if (region->depth == 8)
776
                map_table = map2to8;
777
            else if (region->depth == 4)
778
                map_table = map2to4;
779
            else
780
                map_table = NULL;
781 115329f1 Diego Biurrun
782
            x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
783
                                                region->width - x_pos, &buf, buf_size,
784 c6ec28b1 Michael Niedermayer
                                                non_mod, map_table);
785
            break;
786
        case 0x11:
787
            if (region->depth < 4) {
788
                av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
789
                return;
790
            }
791 115329f1 Diego Biurrun
792 c6ec28b1 Michael Niedermayer
            if (region->depth == 8)
793
                map_table = map4to8;
794
            else
795
                map_table = NULL;
796 115329f1 Diego Biurrun
797
            x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
798
                                                region->width - x_pos, &buf, buf_size,
799 c6ec28b1 Michael Niedermayer
                                                non_mod, map_table);
800
            break;
801
        case 0x12:
802
            if (region->depth < 8) {
803
                av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
804
                return;
805
            }
806 115329f1 Diego Biurrun
807
            x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
808
                                                region->width - x_pos, &buf, buf_size,
809 c6ec28b1 Michael Niedermayer
                                                non_mod, NULL);
810
            break;
811 115329f1 Diego Biurrun
812 c6ec28b1 Michael Niedermayer
        case 0x20:
813
            map2to4[0] = (*buf) >> 4;
814
            map2to4[1] = (*buf++) & 0xf;
815
            map2to4[2] = (*buf) >> 4;
816
            map2to4[3] = (*buf++) & 0xf;
817
            break;
818
        case 0x21:
819
            for (i = 0; i < 4; i++)
820
                map2to8[i] = *buf++;
821
            break;
822
        case 0x22:
823
            for (i = 0; i < 16; i++)
824
                map4to8[i] = *buf++;
825
            break;
826 115329f1 Diego Biurrun
827 c6ec28b1 Michael Niedermayer
        case 0xf0:
828
            x_pos = display->x_pos;
829
            y_pos += 2;
830
            break;
831
        default:
832
            av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
833
        }
834
    }
835 115329f1 Diego Biurrun
836 c6ec28b1 Michael Niedermayer
}
837
838
static void dvbsub_parse_object_segment(AVCodecContext *avctx,
839
                                        uint8_t *buf, int buf_size)
840
{
841
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
842 115329f1 Diego Biurrun
843 c6ec28b1 Michael Niedermayer
    uint8_t *buf_end = buf + buf_size;
844
    uint8_t *block;
845
    int object_id;
846
    DVBSubObject *object;
847
    DVBSubObjectDisplay *display;
848
    int top_field_len, bottom_field_len;
849 115329f1 Diego Biurrun
850 c6ec28b1 Michael Niedermayer
    int coding_method, non_modifying_colour;
851 115329f1 Diego Biurrun
852 fead30d4 Alex Beregszaszi
    object_id = AV_RB16(buf);
853 c6ec28b1 Michael Niedermayer
    buf += 2;
854 115329f1 Diego Biurrun
855 c6ec28b1 Michael Niedermayer
    object = get_object(ctx, object_id);
856
857 115329f1 Diego Biurrun
    if (!object)
858
        return;
859
860 c6ec28b1 Michael Niedermayer
    coding_method = ((*buf) >> 2) & 3;
861
    non_modifying_colour = ((*buf++) >> 1) & 1;
862 115329f1 Diego Biurrun
863 c6ec28b1 Michael Niedermayer
    if (coding_method == 0) {
864 fead30d4 Alex Beregszaszi
        top_field_len = AV_RB16(buf);
865 c6ec28b1 Michael Niedermayer
        buf += 2;
866 fead30d4 Alex Beregszaszi
        bottom_field_len = AV_RB16(buf);
867 c6ec28b1 Michael Niedermayer
        buf += 2;
868 115329f1 Diego Biurrun
869 c6ec28b1 Michael Niedermayer
        if (buf + top_field_len + bottom_field_len > buf_end) {
870
            av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
871
            return;
872 115329f1 Diego Biurrun
        }
873
874 c6ec28b1 Michael Niedermayer
        for (display = object->display_list; display != 0; display = display->object_list_next) {
875
            block = buf;
876
877
            dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
878
                                            non_modifying_colour);
879
880
            if (bottom_field_len > 0)
881
                block = buf + top_field_len;
882
            else
883
                bottom_field_len = top_field_len;
884
885
            dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
886
                                            non_modifying_colour);
887
        }
888 115329f1 Diego Biurrun
889 c6ec28b1 Michael Niedermayer
/*  } else if (coding_method == 1) {*/
890 115329f1 Diego Biurrun
891 c6ec28b1 Michael Niedermayer
    } else {
892
        av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
893
    }
894 115329f1 Diego Biurrun
895 c6ec28b1 Michael Niedermayer
}
896
897
#define SCALEBITS 10
898
#define ONE_HALF  (1 << (SCALEBITS - 1))
899
#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
900
901
#define YUV_TO_RGB1_CCIR(cb1, cr1)\
902
{\
903
    cb = (cb1) - 128;\
904
    cr = (cr1) - 128;\
905
    r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
906
    g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
907
            ONE_HALF;\
908
    b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
909
}
910
911
#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
912
{\
913
    y = ((y1) - 16) * FIX(255.0/219.0);\
914
    r = cm[(y + r_add) >> SCALEBITS];\
915
    g = cm[(y + g_add) >> SCALEBITS];\
916
    b = cm[(y + b_add) >> SCALEBITS];\
917
}
918
919
920
static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
921
                                        uint8_t *buf, int buf_size)
922
{
923
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
924 115329f1 Diego Biurrun
925 c6ec28b1 Michael Niedermayer
    uint8_t *buf_end = buf + buf_size;
926
    int clut_id;
927
    DVBSubCLUT *clut;
928
    int entry_id, depth , full_range;
929
    int y, cr, cb, alpha;
930
    int r, g, b, r_add, g_add, b_add;
931
932
#ifdef DEBUG_PACKET_CONTENTS
933
    int i;
934
935
    av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
936
937
    for (i=0; i < buf_size; i++)
938
    {
939
        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
940
        if (i % 16 == 15)
941
            av_log(avctx, AV_LOG_INFO, "\n");
942
    }
943 115329f1 Diego Biurrun
944 c6ec28b1 Michael Niedermayer
    if (i % 16 != 0)
945
        av_log(avctx, AV_LOG_INFO, "\n");
946
947
#endif
948
949
    clut_id = *buf++;
950
    buf += 1;
951 115329f1 Diego Biurrun
952 c6ec28b1 Michael Niedermayer
    clut = get_clut(ctx, clut_id);
953 115329f1 Diego Biurrun
954 c6ec28b1 Michael Niedermayer
    if (clut == NULL) {
955
        clut = av_malloc(sizeof(DVBSubCLUT));
956 115329f1 Diego Biurrun
957 c6ec28b1 Michael Niedermayer
        memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
958
959
        clut->id = clut_id;
960 115329f1 Diego Biurrun
961
        clut->next = ctx->clut_list;
962 c6ec28b1 Michael Niedermayer
        ctx->clut_list = clut;
963
    }
964 115329f1 Diego Biurrun
965 c6ec28b1 Michael Niedermayer
    while (buf + 4 < buf_end)
966
    {
967
        entry_id = *buf++;
968 115329f1 Diego Biurrun
969 c6ec28b1 Michael Niedermayer
        depth = (*buf) & 0xe0;
970 115329f1 Diego Biurrun
971 c6ec28b1 Michael Niedermayer
        if (depth == 0) {
972
            av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
973
            return;
974
        }
975 115329f1 Diego Biurrun
976 c6ec28b1 Michael Niedermayer
        full_range = (*buf++) & 1;
977 115329f1 Diego Biurrun
978 c6ec28b1 Michael Niedermayer
        if (full_range) {
979
            y = *buf++;
980
            cr = *buf++;
981
            cb = *buf++;
982
            alpha = *buf++;
983
        } else {
984
            y = buf[0] & 0xfc;
985
            cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
986
            cb = (buf[1] << 2) & 0xf0;
987
            alpha = (buf[1] << 6) & 0xc0;
988 115329f1 Diego Biurrun
989 c6ec28b1 Michael Niedermayer
            buf += 2;
990
        }
991 115329f1 Diego Biurrun
992 c6ec28b1 Michael Niedermayer
        if (y == 0)
993
            alpha = 0xff;
994 115329f1 Diego Biurrun
995 c6ec28b1 Michael Niedermayer
        YUV_TO_RGB1_CCIR(cb, cr);
996
        YUV_TO_RGB2_CCIR(r, g, b, y);
997 115329f1 Diego Biurrun
998 c6ec28b1 Michael Niedermayer
#ifdef DEBUG
999
        av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1000
#endif
1001 115329f1 Diego Biurrun
1002 c6ec28b1 Michael Niedermayer
        if (depth & 0x80)
1003
            clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1004
        if (depth & 0x40)
1005
            clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1006
        if (depth & 0x20)
1007
            clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1008
    }
1009
}
1010
1011
1012
static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1013
                                        uint8_t *buf, int buf_size)
1014
{
1015
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1016 115329f1 Diego Biurrun
1017 c6ec28b1 Michael Niedermayer
    uint8_t *buf_end = buf + buf_size;
1018
    int region_id, object_id;
1019
    DVBSubRegion *region;
1020
    DVBSubObject *object;
1021
    DVBSubObjectDisplay *display;
1022
    int fill;
1023 115329f1 Diego Biurrun
1024 c6ec28b1 Michael Niedermayer
    if (buf_size < 10)
1025
        return;
1026 115329f1 Diego Biurrun
1027 c6ec28b1 Michael Niedermayer
    region_id = *buf++;
1028 115329f1 Diego Biurrun
1029 c6ec28b1 Michael Niedermayer
    region = get_region(ctx, region_id);
1030 115329f1 Diego Biurrun
1031 c6ec28b1 Michael Niedermayer
    if (region == NULL)
1032
    {
1033
        region = av_mallocz(sizeof(DVBSubRegion));
1034 115329f1 Diego Biurrun
1035 c6ec28b1 Michael Niedermayer
        region->id = region_id;
1036 115329f1 Diego Biurrun
1037 c6ec28b1 Michael Niedermayer
        region->next = ctx->region_list;
1038
        ctx->region_list = region;
1039
    }
1040 115329f1 Diego Biurrun
1041 c6ec28b1 Michael Niedermayer
    fill = ((*buf++) >> 3) & 1;
1042 115329f1 Diego Biurrun
1043 fead30d4 Alex Beregszaszi
    region->width = AV_RB16(buf);
1044 c6ec28b1 Michael Niedermayer
    buf += 2;
1045 fead30d4 Alex Beregszaszi
    region->height = AV_RB16(buf);
1046 c6ec28b1 Michael Niedermayer
    buf += 2;
1047 115329f1 Diego Biurrun
1048 c6ec28b1 Michael Niedermayer
    if (region->width * region->height != region->buf_size) {
1049
        if (region->pbuf != 0)
1050
            av_free(region->pbuf);
1051 115329f1 Diego Biurrun
1052 c6ec28b1 Michael Niedermayer
        region->buf_size = region->width * region->height;
1053 115329f1 Diego Biurrun
1054 c6ec28b1 Michael Niedermayer
        region->pbuf = av_malloc(region->buf_size);
1055 115329f1 Diego Biurrun
1056 c6ec28b1 Michael Niedermayer
        fill = 1;
1057
    }
1058 115329f1 Diego Biurrun
1059 c6ec28b1 Michael Niedermayer
    region->depth = 1 << (((*buf++) >> 2) & 7);
1060
    region->clut = *buf++;
1061 115329f1 Diego Biurrun
1062 c6ec28b1 Michael Niedermayer
    if (region->depth == 8)
1063
        region->bgcolour = *buf++;
1064
    else {
1065
        buf += 1;
1066 115329f1 Diego Biurrun
1067 c6ec28b1 Michael Niedermayer
        if (region->depth == 4)
1068
            region->bgcolour = (((*buf++) >> 4) & 15);
1069
        else
1070
            region->bgcolour = (((*buf++) >> 2) & 3);
1071
    }
1072
1073
#ifdef DEBUG
1074
    av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1075
#endif
1076
1077
    if (fill) {
1078
        memset(region->pbuf, region->bgcolour, region->buf_size);
1079
#ifdef DEBUG
1080
        av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolour);
1081
#endif
1082
    }
1083
1084
    delete_region_display_list(ctx, region);
1085
1086
    while (buf + 5 < buf_end) {
1087 fead30d4 Alex Beregszaszi
        object_id = AV_RB16(buf);
1088 c6ec28b1 Michael Niedermayer
        buf += 2;
1089 115329f1 Diego Biurrun
1090 c6ec28b1 Michael Niedermayer
        object = get_object(ctx, object_id);
1091
1092
        if (object == NULL) {
1093
            object = av_mallocz(sizeof(DVBSubObject));
1094 115329f1 Diego Biurrun
1095 c6ec28b1 Michael Niedermayer
            object->id = object_id;
1096
            object->next = ctx->object_list;
1097
            ctx->object_list = object;
1098
        }
1099 115329f1 Diego Biurrun
1100 c6ec28b1 Michael Niedermayer
        object->type = (*buf) >> 6;
1101 115329f1 Diego Biurrun
1102 c6ec28b1 Michael Niedermayer
        display = av_mallocz(sizeof(DVBSubObjectDisplay));
1103 115329f1 Diego Biurrun
1104 c6ec28b1 Michael Niedermayer
        display->object_id = object_id;
1105
        display->region_id = region_id;
1106 115329f1 Diego Biurrun
1107 fead30d4 Alex Beregszaszi
        display->x_pos = AV_RB16(buf) & 0xfff;
1108 c6ec28b1 Michael Niedermayer
        buf += 2;
1109 fead30d4 Alex Beregszaszi
        display->y_pos = AV_RB16(buf) & 0xfff;
1110 c6ec28b1 Michael Niedermayer
        buf += 2;
1111 115329f1 Diego Biurrun
1112 c6ec28b1 Michael Niedermayer
        if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1113
            display->fgcolour = *buf++;
1114
            display->bgcolour = *buf++;
1115
        }
1116 115329f1 Diego Biurrun
1117 c6ec28b1 Michael Niedermayer
        display->region_list_next = region->display_list;
1118
        region->display_list = display;
1119 115329f1 Diego Biurrun
1120 c6ec28b1 Michael Niedermayer
        display->object_list_next = object->display_list;
1121
        object->display_list = display;
1122
    }
1123
}
1124
1125
static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1126
                                        uint8_t *buf, int buf_size)
1127
{
1128
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1129
    DVBSubRegionDisplay *display;
1130
    DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1131 115329f1 Diego Biurrun
1132 c6ec28b1 Michael Niedermayer
    uint8_t *buf_end = buf + buf_size;
1133
    int region_id;
1134
    int page_state;
1135 115329f1 Diego Biurrun
1136 c6ec28b1 Michael Niedermayer
    if (buf_size < 1)
1137
        return;
1138 115329f1 Diego Biurrun
1139 c6ec28b1 Michael Niedermayer
    ctx->time_out = *buf++;
1140
    page_state = ((*buf++) >> 2) & 3;
1141 115329f1 Diego Biurrun
1142 c6ec28b1 Michael Niedermayer
#ifdef DEBUG
1143
    av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1144
#endif
1145
1146
    if (page_state == 2)
1147
    {
1148
        delete_state(ctx);
1149
    }
1150 115329f1 Diego Biurrun
1151 c6ec28b1 Michael Niedermayer
    tmp_display_list = ctx->display_list;
1152
    ctx->display_list = NULL;
1153
    ctx->display_list_size = 0;
1154 115329f1 Diego Biurrun
1155 c6ec28b1 Michael Niedermayer
    while (buf + 5 < buf_end) {
1156
        region_id = *buf++;
1157
        buf += 1;
1158 115329f1 Diego Biurrun
1159 c6ec28b1 Michael Niedermayer
        display = tmp_display_list;
1160
        tmp_ptr = &tmp_display_list;
1161 115329f1 Diego Biurrun
1162 c6ec28b1 Michael Niedermayer
        while (display != NULL && display->region_id != region_id) {
1163
            tmp_ptr = &display->next;
1164
            display = display->next;
1165
        }
1166 115329f1 Diego Biurrun
1167 c6ec28b1 Michael Niedermayer
        if (display == NULL)
1168
            display = av_mallocz(sizeof(DVBSubRegionDisplay));
1169 115329f1 Diego Biurrun
1170 c6ec28b1 Michael Niedermayer
        display->region_id = region_id;
1171 115329f1 Diego Biurrun
1172 fead30d4 Alex Beregszaszi
        display->x_pos = AV_RB16(buf);
1173 c6ec28b1 Michael Niedermayer
        buf += 2;
1174 fead30d4 Alex Beregszaszi
        display->y_pos = AV_RB16(buf);
1175 c6ec28b1 Michael Niedermayer
        buf += 2;
1176 115329f1 Diego Biurrun
1177 c6ec28b1 Michael Niedermayer
        *tmp_ptr = display->next;
1178 115329f1 Diego Biurrun
1179 c6ec28b1 Michael Niedermayer
        display->next = ctx->display_list;
1180
        ctx->display_list = display;
1181
        ctx->display_list_size++;
1182 115329f1 Diego Biurrun
1183 c6ec28b1 Michael Niedermayer
#ifdef DEBUG
1184
        av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1185
#endif
1186
    }
1187 115329f1 Diego Biurrun
1188 c6ec28b1 Michael Niedermayer
    while (tmp_display_list != 0) {
1189
        display = tmp_display_list;
1190 115329f1 Diego Biurrun
1191 c6ec28b1 Michael Niedermayer
        tmp_display_list = display->next;
1192 115329f1 Diego Biurrun
1193 c6ec28b1 Michael Niedermayer
        av_free(display);
1194
    }
1195 115329f1 Diego Biurrun
1196 c6ec28b1 Michael Niedermayer
}
1197
1198
1199
#ifdef DEBUG_SAVE_IMAGES
1200
static void save_display_set(DVBSubContext *ctx)
1201
{
1202
    DVBSubRegion *region;
1203
    DVBSubRegionDisplay *display;
1204
    DVBSubCLUT *clut;
1205
    uint32_t *clut_table;
1206
    int x_pos, y_pos, width, height;
1207
    int x, y, y_off, x_off;
1208
    uint32_t *pbuf;
1209
    char filename[32];
1210
    static int fileno_index = 0;
1211
1212
    x_pos = -1;
1213
    y_pos = -1;
1214
    width = 0;
1215
    height = 0;
1216 115329f1 Diego Biurrun
1217 c6ec28b1 Michael Niedermayer
    for (display = ctx->display_list; display != NULL; display = display->next) {
1218
        region = get_region(ctx, display->region_id);
1219 115329f1 Diego Biurrun
1220 c6ec28b1 Michael Niedermayer
        if (x_pos == -1) {
1221
            x_pos = display->x_pos;
1222
            y_pos = display->y_pos;
1223
            width = region->width;
1224
            height = region->height;
1225
        } else {
1226
            if (display->x_pos < x_pos) {
1227
                width += (x_pos - display->x_pos);
1228
                x_pos = display->x_pos;
1229
            }
1230 115329f1 Diego Biurrun
1231 c6ec28b1 Michael Niedermayer
            if (display->y_pos < y_pos) {
1232
                height += (y_pos - display->y_pos);
1233
                y_pos = display->y_pos;
1234
            }
1235 115329f1 Diego Biurrun
1236 c6ec28b1 Michael Niedermayer
            if (display->x_pos + region->width > x_pos + width) {
1237
                width = display->x_pos + region->width - x_pos;
1238
            }
1239 115329f1 Diego Biurrun
1240 c6ec28b1 Michael Niedermayer
            if (display->y_pos + region->height > y_pos + height) {
1241
                height = display->y_pos + region->height - y_pos;
1242
            }
1243
        }
1244
    }
1245 115329f1 Diego Biurrun
1246 c6ec28b1 Michael Niedermayer
    if (x_pos >= 0) {
1247 115329f1 Diego Biurrun
1248 c6ec28b1 Michael Niedermayer
        pbuf = av_malloc(width * height * 4);
1249
1250
        for (display = ctx->display_list; display != NULL; display = display->next) {
1251
            region = get_region(ctx, display->region_id);
1252
1253
            x_off = display->x_pos - x_pos;
1254
            y_off = display->y_pos - y_pos;
1255
1256
            clut = get_clut(ctx, region->clut);
1257
1258
            if (clut == 0)
1259
                clut = &default_clut;
1260
1261
            switch (region->depth) {
1262
            case 2:
1263
                clut_table = clut->clut4;
1264
                break;
1265
            case 8:
1266
                clut_table = clut->clut256;
1267
                break;
1268
            case 4:
1269
            default:
1270
                clut_table = clut->clut16;
1271
                break;
1272
            }
1273 115329f1 Diego Biurrun
1274 c6ec28b1 Michael Niedermayer
            for (y = 0; y < region->height; y++) {
1275
                for (x = 0; x < region->width; x++) {
1276 115329f1 Diego Biurrun
                    pbuf[((y + y_off) * width) + x_off + x] =
1277 c6ec28b1 Michael Niedermayer
                        clut_table[region->pbuf[y * region->width + x]];
1278
                }
1279
            }
1280
1281 115329f1 Diego Biurrun
        }
1282 c6ec28b1 Michael Niedermayer
1283
        snprintf(filename, 32, "dvbs.%d", fileno_index);
1284
1285
        png_save2(filename, pbuf, width, height);
1286
1287
        av_free(pbuf);
1288
    }
1289 115329f1 Diego Biurrun
1290 c6ec28b1 Michael Niedermayer
    fileno_index++;
1291
}
1292
#endif
1293
1294 115329f1 Diego Biurrun
static int dvbsub_display_end_segment(AVCodecContext *avctx, uint8_t *buf,
1295 c6ec28b1 Michael Niedermayer
                                        int buf_size, AVSubtitle *sub)
1296
{
1297
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1298
1299
    DVBSubRegion *region;
1300
    DVBSubRegionDisplay *display;
1301
    AVSubtitleRect *rect;
1302
    DVBSubCLUT *clut;
1303
    uint32_t *clut_table;
1304
    int i;
1305 115329f1 Diego Biurrun
1306 c6ec28b1 Michael Niedermayer
    sub->rects = NULL;
1307
    sub->start_display_time = 0;
1308
    sub->end_display_time = ctx->time_out * 1000;
1309
    sub->format = 0;
1310
1311
    sub->num_rects = ctx->display_list_size;
1312 115329f1 Diego Biurrun
1313 bf01fb69 Ian Caulfield
    if (sub->num_rects > 0)
1314
        sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
1315 c6ec28b1 Michael Niedermayer
1316
    i = 0;
1317
1318
    for (display = ctx->display_list; display != NULL; display = display->next) {
1319
        region = get_region(ctx, display->region_id);
1320
        rect = &sub->rects[i];
1321 115329f1 Diego Biurrun
1322 c6ec28b1 Michael Niedermayer
        if (region == NULL)
1323
            continue;
1324 115329f1 Diego Biurrun
1325 c6ec28b1 Michael Niedermayer
        rect->x = display->x_pos;
1326
        rect->y = display->y_pos;
1327
        rect->w = region->width;
1328
        rect->h = region->height;
1329
        rect->nb_colors = 16;
1330
        rect->linesize = region->width;
1331
1332
        clut = get_clut(ctx, region->clut);
1333 115329f1 Diego Biurrun
1334 c6ec28b1 Michael Niedermayer
        if (clut == NULL)
1335
            clut = &default_clut;
1336 115329f1 Diego Biurrun
1337 c6ec28b1 Michael Niedermayer
        switch (region->depth) {
1338
        case 2:
1339
            clut_table = clut->clut4;
1340
            break;
1341
        case 8:
1342
            clut_table = clut->clut256;
1343
            break;
1344
        case 4:
1345
        default:
1346
            clut_table = clut->clut16;
1347
            break;
1348
        }
1349 115329f1 Diego Biurrun
1350 c6ec28b1 Michael Niedermayer
        rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
1351
        memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
1352 115329f1 Diego Biurrun
1353 c6ec28b1 Michael Niedermayer
        rect->bitmap = av_malloc(region->buf_size);
1354
        memcpy(rect->bitmap, region->pbuf, region->buf_size);
1355 115329f1 Diego Biurrun
1356 c6ec28b1 Michael Niedermayer
        i++;
1357
    }
1358 115329f1 Diego Biurrun
1359 c6ec28b1 Michael Niedermayer
    sub->num_rects = i;
1360 115329f1 Diego Biurrun
1361 c6ec28b1 Michael Niedermayer
#ifdef DEBUG_SAVE_IMAGES
1362
    save_display_set(ctx);
1363
#endif
1364 115329f1 Diego Biurrun
1365 c6ec28b1 Michael Niedermayer
    return 1;
1366
}
1367
1368
static int dvbsub_decode(AVCodecContext *avctx,
1369
                         void *data, int *data_size,
1370
                         uint8_t *buf, int buf_size)
1371
{
1372
    DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1373
    AVSubtitle *sub = (AVSubtitle*) data;
1374
    uint8_t *p, *p_end;
1375
    int segment_type;
1376
    int page_id;
1377
    int segment_length;
1378 115329f1 Diego Biurrun
1379 c6ec28b1 Michael Niedermayer
#ifdef DEBUG_PACKET_CONTENTS
1380
    int i;
1381
1382
    av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1383
1384
    for (i=0; i < buf_size; i++)
1385
    {
1386
        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1387
        if (i % 16 == 15)
1388
            av_log(avctx, AV_LOG_INFO, "\n");
1389
    }
1390 115329f1 Diego Biurrun
1391 c6ec28b1 Michael Niedermayer
    if (i % 16 != 0)
1392
        av_log(avctx, AV_LOG_INFO, "\n");
1393
1394
#endif
1395
1396
    if (buf_size <= 2)
1397
        return -1;
1398 115329f1 Diego Biurrun
1399 c6ec28b1 Michael Niedermayer
    p = buf;
1400
    p_end = buf + buf_size;
1401 115329f1 Diego Biurrun
1402 c6ec28b1 Michael Niedermayer
    while (p < p_end && *p == 0x0f)
1403
    {
1404
        p += 1;
1405
        segment_type = *p++;
1406 fead30d4 Alex Beregszaszi
        page_id = AV_RB16(p);
1407 c6ec28b1 Michael Niedermayer
        p += 2;
1408 fead30d4 Alex Beregszaszi
        segment_length = AV_RB16(p);
1409 c6ec28b1 Michael Niedermayer
        p += 2;
1410 115329f1 Diego Biurrun
1411 c6ec28b1 Michael Niedermayer
        if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1412
            switch (segment_type) {
1413
            case DVBSUB_PAGE_SEGMENT:
1414
                dvbsub_parse_page_segment(avctx, p, segment_length);
1415
                break;
1416
            case DVBSUB_REGION_SEGMENT:
1417
                dvbsub_parse_region_segment(avctx, p, segment_length);
1418
                break;
1419
            case DVBSUB_CLUT_SEGMENT:
1420
                dvbsub_parse_clut_segment(avctx, p, segment_length);
1421
                break;
1422
            case DVBSUB_OBJECT_SEGMENT:
1423
                dvbsub_parse_object_segment(avctx, p, segment_length);
1424
                break;
1425
            case DVBSUB_DISPLAY_SEGMENT:
1426
                *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1427
                break;
1428
            default:
1429
#ifdef DEBUG
1430 115329f1 Diego Biurrun
                av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1431 c6ec28b1 Michael Niedermayer
                        segment_type, page_id, segment_length);
1432
#endif
1433
                break;
1434
            }
1435
        }
1436
1437
        p += segment_length;
1438
    }
1439 115329f1 Diego Biurrun
1440 c6ec28b1 Michael Niedermayer
    if (p != p_end)
1441
    {
1442
#ifdef DEBUG
1443
        av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1444
#endif
1445
        return -1;
1446
    }
1447
1448 bf01fb69 Ian Caulfield
    return buf_size;
1449 c6ec28b1 Michael Niedermayer
}
1450
1451
1452
AVCodec dvbsub_decoder = {
1453
    "dvbsub",
1454
    CODEC_TYPE_SUBTITLE,
1455
    CODEC_ID_DVB_SUBTITLE,
1456
    sizeof(DVBSubContext),
1457
    dvbsub_init_decoder,
1458
    NULL,
1459
    dvbsub_close_decoder,
1460
    dvbsub_decode,
1461
};