| 112 | } |
| 113 | |
| 114 | static int xsub_encode(AVCodecContext *avctx, unsigned char *buf, |
| 115 | int bufsize, const AVSubtitle *h) |
| 116 | { |
| 117 | uint64_t startTime = h->pts / 1000; // FIXME: need better solution... |
| 118 | uint64_t endTime = startTime + h->end_display_time - h->start_display_time; |
| 119 | int start_tc[4], end_tc[4]; |
| 120 | uint8_t *hdr = buf + 27; // Point behind the timestamp |
| 121 | uint8_t *rlelenptr; |
| 122 | uint16_t width, height; |
| 123 | int i; |
| 124 | PutBitContext pb; |
| 125 | |
| 126 | if (bufsize < 27 + 7*2 + 4*3) { |
| 127 | av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n"); |
| 128 | return AVERROR_BUFFER_TOO_SMALL; |
| 129 | } |
| 130 | |
| 131 | // TODO: support multiple rects |
| 132 | if (h->num_rects != 1) |
| 133 | av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects); |
| 134 | |
| 135 | // TODO: render text-based subtitles into bitmaps |
| 136 | if (!h->rects[0]->data[0] || !h->rects[0]->data[1]) { |
| 137 | av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n"); |
| 138 | return AVERROR(EINVAL); |
| 139 | } |
| 140 | |
| 141 | // TODO: color reduction, similar to dvdsub encoder |
| 142 | if (h->rects[0]->nb_colors > 4) |
| 143 | av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors); |
| 144 | |
| 145 | // TODO: Palette swapping if color zero is not transparent |
| 146 | if (((uint32_t *)h->rects[0]->data[1])[0] & 0xff000000) |
| 147 | av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n"); |
| 148 | |
| 149 | if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) { |
| 150 | av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n"); |
| 151 | return AVERROR(EINVAL); |
| 152 | } |
| 153 | |
| 154 | snprintf(buf, 28, |
| 155 | "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]", |
| 156 | start_tc[3], start_tc[2], start_tc[1], start_tc[0], |
| 157 | end_tc[3], end_tc[2], end_tc[1], end_tc[0]); |
| 158 | |
| 159 | // Width and height must probably be multiples of 2. |
| 160 | // 2 pixels required on either side of subtitle. |
| 161 | // Possibly due to limitations of hardware renderers. |
| 162 | // TODO: check if the bitmap is already padded |
| 163 | width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2; |
| 164 | height = FFALIGN(h->rects[0]->h, 2); |
| 165 | |
| 166 | bytestream_put_le16(&hdr, width); |
| 167 | bytestream_put_le16(&hdr, height); |
| 168 | bytestream_put_le16(&hdr, h->rects[0]->x); |
| 169 | bytestream_put_le16(&hdr, h->rects[0]->y); |
| 170 | bytestream_put_le16(&hdr, h->rects[0]->x + width -1); |
| 171 | bytestream_put_le16(&hdr, h->rects[0]->y + height -1); |
nothing calls this directly
no test coverage detected