5483
|
1 #include "avcodec.h"
|
|
2 #include "bitstream.h"
|
|
3 #include "bytestream.h"
|
|
4
|
|
5 static int decode_init(AVCodecContext *avctx) {
|
|
6 avctx->pix_fmt = PIX_FMT_PAL8;
|
|
7 return 0;
|
|
8 }
|
|
9
|
|
10 static const uint8_t runbits[8] = { 2, 2, 6, 6, 10, 10, 14, 14 };
|
|
11
|
|
12 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
|
|
13 uint8_t *buf, int buf_size) {
|
|
14 AVSubtitle *sub = data;
|
|
15 uint8_t *buf_end = buf + buf_size;
|
|
16 uint8_t *bitmap;
|
|
17 int w, h, x, y, rlelen, i;
|
|
18 GetBitContext gb;
|
|
19
|
|
20 // check that at least header fits
|
|
21 if (buf_size < 27 + 7 * 2 + 4 * 3) {
|
|
22 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
|
|
23 return -1;
|
|
24 }
|
|
25
|
|
26 // read header
|
|
27 w = bytestream_get_le16(&buf);
|
|
28 h = bytestream_get_le16(&buf);
|
|
29 if (avcodec_check_dimensions(avctx, w, h) < 0)
|
|
30 return -1;
|
|
31 x = bytestream_get_le16(&buf);
|
|
32 y = bytestream_get_le16(&buf);
|
|
33 // skip bottom right position, it gives no new information
|
|
34 bytestream_get_le16(&buf);
|
|
35 bytestream_get_le16(&buf);
|
|
36 rlelen = bytestream_get_le16(&buf);
|
|
37
|
|
38 // allocate sub and set values
|
|
39 if (!sub->rects) {
|
|
40 sub->rects = av_mallocz(sizeof(AVSubtitleRect));
|
|
41 sub->num_rects = 1;
|
|
42 }
|
|
43 av_freep(sub->rects[0].bitmap);
|
|
44 sub->rects[0].x = x; sub->rects[0].y = y;
|
|
45 sub->rects[0].w = w; sub->rects[0].h = h;
|
|
46 sub->rects[0].linesize = w;
|
|
47 sub->rects[0].bitmap = av_malloc(w * h);
|
|
48 sub->rects[0].nb_colors = 4;
|
|
49 sub->rects[0].rgba_palette = av_malloc(sub->rects[0].nb_colors * 4);
|
|
50
|
|
51 // read palette
|
|
52 for (i = 0; i < sub->rects[0].nb_colors; i++)
|
|
53 sub->rects[0].rgba_palette[i] = bytestream_get_be24(&buf);
|
|
54
|
|
55 // process RLE-compressed data
|
|
56 rlelen = FFMIN(rlelen, buf_end - buf);
|
|
57 init_get_bits(&gb, buf, rlelen * 8);
|
|
58 bitmap = sub->rects[0].bitmap;
|
|
59 for (y = 0; y < h; y++) {
|
|
60 for (x = 0; x < w; ) {
|
|
61 int log2 = ff_log2_tab[show_bits(&gb, 8)];
|
|
62 int run = get_bits(&gb, runbits[log2]);
|
|
63 int colour = get_bits(&gb, 2);
|
|
64 run = FFMIN(run, w - x);
|
|
65 // run length 0 means till end of row
|
|
66 if (!run) run = w - x;
|
|
67 memset(bitmap, colour, run);
|
|
68 bitmap += run;
|
|
69 x += run;
|
|
70 }
|
|
71 align_get_bits(&gb);
|
|
72 }
|
|
73 *data_size = 1;
|
|
74 return buf_size;
|
|
75 }
|
|
76
|
|
77 AVCodec xsub_decoder = {
|
|
78 "xsub",
|
|
79 CODEC_TYPE_SUBTITLE,
|
|
80 CODEC_ID_XSUB,
|
|
81 0,
|
|
82 decode_init,
|
|
83 NULL,
|
|
84 NULL,
|
|
85 decode_frame,
|
|
86 };
|