FFmpeg
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "codec_internal.h"
26 #include "put_bits.h"
27 
28 /**
29  * Number of pixels to pad left and right.
30  *
31  * The official encoder pads the subtitles with two pixels on either side,
32  * but until we find out why, we won't do it (we will pad to have width
33  * divisible by 2 though).
34  */
35 #define PADDING 0
36 #define PADDING_COLOR 0
37 
38 /**
39  * Encode a single color run. At most 16 bits will be used.
40  * @param len length of the run, values > 255 mean "until end of line", may not be < 0.
41  * @param color color to encode, only the lowest two bits are used and all others must be 0.
42  */
43 static void put_xsub_rle(PutBitContext *pb, int len, int color)
44 {
45  if (len <= 255)
46  put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
47  else
48  put_bits(pb, 14, 0);
49  put_bits(pb, 2, color);
50 }
51 
52 /**
53  * Encode a 4-color bitmap with XSUB rle.
54  *
55  * The encoded bitmap may be wider than the source bitmap due to padding.
56  */
57 static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
58  int linesize, int w, int h)
59 {
60  int x0, x1, y, len, color = PADDING_COLOR;
61 
62  for (y = 0; y < h; y++) {
63  x0 = 0;
64  while (x0 < w) {
65  // Make sure we have enough room for at least one run and padding
66  if (put_bytes_left(pb, 1) < 7)
68 
69  x1 = x0;
70  color = bitmap[x1++] & 3;
71  while (x1 < w && (bitmap[x1] & 3) == color)
72  x1++;
73  len = x1 - x0;
74  if (PADDING && x0 == 0) {
75  if (color == PADDING_COLOR) {
76  len += PADDING;
77  x0 -= PADDING;
78  } else
80  }
81 
82  // Run can't be longer than 255, unless it is the rest of a row
83  if (x1 == w && color == PADDING_COLOR) {
84  len += PADDING + (w&1);
85  } else
86  len = FFMIN(len, 255);
87  put_xsub_rle(pb, len, color);
88 
89  x0 += len;
90  }
91  if (color != PADDING_COLOR && (PADDING + (w&1)))
92  put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
93 
94  align_put_bits(pb);
95 
96  bitmap += linesize;
97  }
98 
99  return 0;
100 }
101 
102 static int make_tc(uint64_t ms, int *tc)
103 {
104  static const int tc_divs[3] = { 1000, 60, 60 };
105  int i;
106  for (i=0; i<3; i++) {
107  tc[i] = ms % tc_divs[i];
108  ms /= tc_divs[i];
109  }
110  tc[3] = ms;
111  return ms > 99;
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");
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);
172 
173  rlelenptr = hdr; // Will store length of first field here later.
174  hdr+=2;
175 
176  // Palette
177  for (i=0; i<4; i++)
178  bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->data[1])[i]);
179 
180  // Bitmap
181  // RLE buffer. Reserve 2 bytes for possible padding after the last row.
182  init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
183  if (xsub_encode_rle(&pb, h->rects[0]->data[0],
184  h->rects[0]->linesize[0] * 2,
185  h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
187  bytestream_put_le16(&rlelenptr, put_bytes_count(&pb, 0)); // Length of first field
188 
189  if (xsub_encode_rle(&pb, h->rects[0]->data[0] + h->rects[0]->linesize[0],
190  h->rects[0]->linesize[0] * 2,
191  h->rects[0]->w, h->rects[0]->h >> 1))
193 
194  // Enforce total height to be a multiple of 2
195  if (h->rects[0]->h & 1) {
196  put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
197  }
198 
199  flush_put_bits(&pb);
200 
201  return hdr - buf + put_bytes_output(&pb);
202 }
203 
205 {
206  if (!avctx->codec_tag)
207  avctx->codec_tag = MKTAG('D','X','S','B');
208 
209  avctx->bits_per_coded_sample = 4;
210 
211  return 0;
212 }
213 
215  .p.name = "xsub",
216  .p.long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
217  .p.type = AVMEDIA_TYPE_SUBTITLE,
218  .p.id = AV_CODEC_ID_XSUB,
219  .init = xsub_encoder_init,
221  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
222 };
AVSubtitle
Definition: avcodec.h:2305
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
align_put_bits
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: put_bits.h:413
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:89
color
Definition: vf_paletteuse.c:600
PADDING
#define PADDING
Number of pixels to pad left and right.
Definition: xsubenc.c:35
put_xsub_rle
static void put_xsub_rle(PutBitContext *pb, int len, int color)
Encode a single color run.
Definition: xsubenc.c:43
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:221
w
uint8_t w
Definition: llviddspenc.c:38
put_bytes_count
static int put_bytes_count(const PutBitContext *s, int round_up)
Definition: put_bits.h:100
ff_xsub_encoder
const FFCodec ff_xsub_encoder
Definition: xsubenc.c:214
FFCodec
Definition: codec_internal.h:112
make_tc
static int make_tc(uint64_t ms, int *tc)
Definition: xsubenc.c:102
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
put_bytes_left
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition: put_bits.h:135
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
width
#define width
xsub_encode
static int xsub_encode(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *h)
Definition: xsubenc.c:114
PutBitContext
Definition: put_bits.h:50
FF_CODEC_ENCODE_SUB_CB
#define FF_CODEC_ENCODE_SUB_CB(func)
Definition: codec_internal.h:266
ff_log2_tab
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
xsub_encode_rle
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:57
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
height
#define height
PADDING_COLOR
#define PADDING_COLOR
Definition: xsubenc.c:36
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
len
int len
Definition: vorbis_enc_data.h:426
xsub_encoder_init
static av_cold int xsub_encoder_init(AVCodecContext *avctx)
Definition: xsubenc.c:204
AV_CODEC_ID_XSUB
@ AV_CODEC_ID_XSUB
Definition: codec_id.h:530
avcodec.h
AVCodecContext
main external API structure.
Definition: avcodec.h:389
tc
#define tc
Definition: regdef.h:69
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:414
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2038
put_bits.h
snprintf
#define snprintf
Definition: snprintf.h:34