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