Mercurial > libavcodec.hg
annotate xsubdec.c @ 5489:dc54869af30b libavcodec
Colours except background should not be transparent
| author | reimar |
|---|---|
| date | Sun, 05 Aug 2007 12:11:24 +0000 |
| parents | 0f736b1402ca |
| children | 12d77ed34985 |
| rev | line source |
|---|---|
| 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 | |
| 5484 | 10 static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 }; |
| 11 static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 6, 10, 10, 1 }; | |
| 12 | |
| 13 static uint64_t parse_timecode(AVCodecContext *avctx, uint8_t *buf) { | |
| 14 int i; | |
| 15 int64_t ms = 0; | |
| 16 if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.') | |
| 17 return AV_NOPTS_VALUE; | |
| 18 for (i = 0; i < sizeof(tc_offsets); i++) { | |
| 19 uint8_t c = buf[tc_offsets[i]] - '0'; | |
| 20 if (c > 9) return AV_NOPTS_VALUE; | |
| 21 ms = (ms + c) * tc_muls[i]; | |
| 22 } | |
| 23 return ms; | |
| 24 } | |
| 25 | |
| 5483 | 26 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, |
| 27 uint8_t *buf, int buf_size) { | |
| 28 AVSubtitle *sub = data; | |
| 29 uint8_t *buf_end = buf + buf_size; | |
| 30 uint8_t *bitmap; | |
| 31 int w, h, x, y, rlelen, i; | |
| 32 GetBitContext gb; | |
| 33 | |
| 34 // check that at least header fits | |
| 35 if (buf_size < 27 + 7 * 2 + 4 * 3) { | |
| 36 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); | |
| 37 return -1; | |
| 38 } | |
| 39 | |
| 5484 | 40 // read start and end time |
| 41 if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') { | |
| 42 av_log(avctx, AV_LOG_ERROR, "invalid time code\n"); | |
| 43 return -1; | |
| 44 } | |
| 45 sub->start_display_time = parse_timecode(avctx, buf + 1); | |
| 46 sub->end_display_time = parse_timecode(avctx, buf + 14); | |
| 47 buf += 27; | |
| 48 | |
| 5483 | 49 // read header |
| 50 w = bytestream_get_le16(&buf); | |
| 51 h = bytestream_get_le16(&buf); | |
| 52 if (avcodec_check_dimensions(avctx, w, h) < 0) | |
| 53 return -1; | |
| 54 x = bytestream_get_le16(&buf); | |
| 55 y = bytestream_get_le16(&buf); | |
| 56 // skip bottom right position, it gives no new information | |
| 57 bytestream_get_le16(&buf); | |
| 58 bytestream_get_le16(&buf); | |
| 59 rlelen = bytestream_get_le16(&buf); | |
| 60 | |
| 61 // allocate sub and set values | |
| 62 if (!sub->rects) { | |
| 63 sub->rects = av_mallocz(sizeof(AVSubtitleRect)); | |
| 64 sub->num_rects = 1; | |
| 65 } | |
| 5485 | 66 av_freep(&sub->rects[0].bitmap); |
| 5483 | 67 sub->rects[0].x = x; sub->rects[0].y = y; |
| 68 sub->rects[0].w = w; sub->rects[0].h = h; | |
| 69 sub->rects[0].linesize = w; | |
| 70 sub->rects[0].bitmap = av_malloc(w * h); | |
| 71 sub->rects[0].nb_colors = 4; | |
| 72 sub->rects[0].rgba_palette = av_malloc(sub->rects[0].nb_colors * 4); | |
| 73 | |
| 74 // read palette | |
| 75 for (i = 0; i < sub->rects[0].nb_colors; i++) | |
| 76 sub->rects[0].rgba_palette[i] = bytestream_get_be24(&buf); | |
|
5489
dc54869af30b
Colours except background should not be transparent
reimar
parents:
5488
diff
changeset
|
77 // make all except background (first entry) non-transparent |
|
dc54869af30b
Colours except background should not be transparent
reimar
parents:
5488
diff
changeset
|
78 for (i = 1; i < sub->rects[0].nb_colors; i++) |
|
dc54869af30b
Colours except background should not be transparent
reimar
parents:
5488
diff
changeset
|
79 sub->rects[0].rgba_palette[i] |= 0xff000000; |
| 5483 | 80 |
| 81 // process RLE-compressed data | |
| 82 rlelen = FFMIN(rlelen, buf_end - buf); | |
| 83 init_get_bits(&gb, buf, rlelen * 8); | |
| 84 bitmap = sub->rects[0].bitmap; | |
| 85 for (y = 0; y < h; y++) { | |
| 86 for (x = 0; x < w; ) { | |
| 87 int log2 = ff_log2_tab[show_bits(&gb, 8)]; | |
|
5487
3039b660bf35
get rid of xsubdec array and calculate value instead
reimar
parents:
5486
diff
changeset
|
88 int run = get_bits(&gb, 14 - 4 * (log2 >> 1)); |
| 5483 | 89 int colour = get_bits(&gb, 2); |
| 90 run = FFMIN(run, w - x); | |
| 91 // run length 0 means till end of row | |
| 92 if (!run) run = w - x; | |
| 93 memset(bitmap, colour, run); | |
| 94 bitmap += run; | |
| 95 x += run; | |
| 96 } | |
| 97 align_get_bits(&gb); | |
| 98 } | |
| 99 *data_size = 1; | |
| 100 return buf_size; | |
| 101 } | |
| 102 | |
| 103 AVCodec xsub_decoder = { | |
| 104 "xsub", | |
| 105 CODEC_TYPE_SUBTITLE, | |
| 106 CODEC_ID_XSUB, | |
| 107 0, | |
| 108 decode_init, | |
| 109 NULL, | |
| 110 NULL, | |
| 111 decode_frame, | |
| 112 }; |
