Libav
xsubenc.c
Go to the documentation of this file.
1 /*
2  * DivX (XSUB) subtitle encoder
3  * Copyright (c) 2005 DivX, Inc.
4  * Copyright (c) 2009 Bjorn Axelsson
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "put_bits.h"
26 
34 #define PADDING 0
35 #define PADDING_COLOR 0
36 
42 static void put_xsub_rle(PutBitContext *pb, int len, int color)
43 {
44  if (len <= 255)
45  put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
46  else
47  put_bits(pb, 14, 0);
48  put_bits(pb, 2, color);
49 }
50 
56 static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
57  int linesize, int w, int h)
58 {
59  int x0, x1, y, len, color = PADDING_COLOR;
60 
61  for (y = 0; y < h; y++) {
62  x0 = 0;
63  while (x0 < w) {
64  // Make sure we have enough room for at least one run and padding
65  if (pb->size_in_bits - put_bits_count(pb) < 7*8)
66  return -1;
67 
68  x1 = x0;
69  color = bitmap[x1++] & 3;
70  while (x1 < w && (bitmap[x1] & 3) == color)
71  x1++;
72  len = x1 - x0;
73  if (PADDING && x0 == 0) {
74  if (color == PADDING_COLOR) {
75  len += PADDING;
76  x0 -= PADDING;
77  } else
79  }
80 
81  // Run can't be longer than 255, unless it is the rest of a row
82  if (x1 == w && color == PADDING_COLOR) {
83  len += PADDING + (w&1);
84  } else
85  len = FFMIN(len, 255);
86  put_xsub_rle(pb, len, color);
87 
88  x0 += len;
89  }
90  if (color != PADDING_COLOR && (PADDING + (w&1)))
91  put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
92 
94 
95  bitmap += linesize;
96  }
97 
98  return 0;
99 }
100 
101 static int make_tc(uint64_t ms, int *tc)
102 {
103  static const int tc_divs[3] = { 1000, 60, 60 };
104  int i;
105  for (i=0; i<3; i++) {
106  tc[i] = ms % tc_divs[i];
107  ms /= tc_divs[i];
108  }
109  tc[3] = ms;
110  return ms > 99;
111 }
112 
113 static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
114  int bufsize, const AVSubtitle *h)
115 {
116  uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
117  uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
118  int start_tc[4], end_tc[4];
119  uint8_t *hdr = buf + 27; // Point behind the timestamp
120  uint8_t *rlelenptr;
121  uint16_t width, height;
122  int i;
123  PutBitContext pb;
124 
125  if (bufsize < 27 + 7*2 + 4*3) {
126  av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
127  return -1;
128  }
129 
130  // TODO: support multiple rects
131  if (h->num_rects > 1)
132  av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
133 
134  // TODO: render text-based subtitles into bitmaps
135  if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
136  av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
137  return -1;
138  }
139 
140  // TODO: color reduction, similar to dvdsub encoder
141  if (h->rects[0]->nb_colors > 4)
142  av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
143 
144  // TODO: Palette swapping if color zero is not transparent
145  if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
146  av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
147 
148  if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
149  av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
150  return -1;
151  }
152 
153  snprintf(buf, 28,
154  "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
155  start_tc[3], start_tc[2], start_tc[1], start_tc[0],
156  end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
157 
158  // Width and height must probably be multiples of 2.
159  // 2 pixels required on either side of subtitle.
160  // Possibly due to limitations of hardware renderers.
161  // TODO: check if the bitmap is already padded
162  width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
163  height = FFALIGN(h->rects[0]->h, 2);
164 
165  bytestream_put_le16(&hdr, width);
166  bytestream_put_le16(&hdr, height);
167  bytestream_put_le16(&hdr, h->rects[0]->x);
168  bytestream_put_le16(&hdr, h->rects[0]->y);
169  bytestream_put_le16(&hdr, h->rects[0]->x + width);
170  bytestream_put_le16(&hdr, h->rects[0]->y + height);
171 
172  rlelenptr = hdr; // Will store length of first field here later.
173  hdr+=2;
174 
175  // Palette
176  for (i=0; i<4; i++)
177  bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
178 
179  // Bitmap
180  // RLE buffer. Reserve 2 bytes for possible padding after the last row.
181  init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
182  if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
183  h->rects[0]->pict.linesize[0]*2,
184  h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
185  return -1;
186  bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
187 
188  if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
189  h->rects[0]->pict.linesize[0]*2,
190  h->rects[0]->w, h->rects[0]->h >> 1))
191  return -1;
192 
193  // Enforce total height to be be multiple of 2
194  if (h->rects[0]->h & 1) {
195  put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
197  }
198 
199  flush_put_bits(&pb);
200 
201  return hdr - buf + put_bits_count(&pb)/8;
202 }
203 
205 {
206  if (!avctx->codec_tag)
207  avctx->codec_tag = MKTAG('D','X','S','B');
208 
209  return 0;
210 }
211 
213  .name = "xsub",
214  .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
215  .type = AVMEDIA_TYPE_SUBTITLE,
216  .id = AV_CODEC_ID_XSUB,
217  .init = xsub_encoder_init,
218  .encode_sub = xsub_encode,
219 };
#define PADDING
Number of pixels to pad left and right.
Definition: xsubenc.c:34
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3028
static void put_xsub_rle(PutBitContext *pb, int len, int color)
Encode a single color run.
Definition: xsubenc.c:42
int x
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3059
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int nb_colors
number of colors in pict, undefined when pict is not set
Definition: avcodec.h:3063
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:45
unsigned num_rects
Definition: avcodec.h:3087
AVCodec.
Definition: avcodec.h:2796
#define FFALIGN(x, a)
Definition: common.h:62
AVSubtitleRect ** rects
Definition: avcodec.h:3088
int w
width of pict, undefined when pict is not set
Definition: avcodec.h:3061
static int make_tc(uint64_t ms, int *tc)
Definition: xsubenc.c:101
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3027
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:21
int h
height of pict, undefined when pict is not set
Definition: avcodec.h:3062
int size_in_bits
Definition: put_bits.h:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int y
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3060
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static int xsub_encode(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *h)
Definition: xsubenc.c:113
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap, int linesize, int w, int h)
Encode a 4-color bitmap with XSUB rle.
Definition: xsubenc.c:56
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
uint32_t end_display_time
Definition: avcodec.h:3086
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:3089
AVPicture pict
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:3069
#define FFMIN(a, b)
Definition: common.h:57
AVCodec ff_xsub_encoder
Definition: xsubenc.c:212
static int width
Definition: utils.c:156
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1050
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
#define PADDING_COLOR
Definition: xsubenc.c:35
int height
Definition: gxfenc.c:72
static av_cold int xsub_encoder_init(AVCodecContext *avctx)
Definition: xsubenc.c:204
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
uint32_t start_display_time
Definition: avcodec.h:3085
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static const uint8_t color[]
Definition: log.c:55
int len
#define MKTAG(a, b, c, d)
Definition: common.h:238
bitstream writer API