annotate xsubenc.c @ 10875:6a6a1c2d8745 libavcodec

AAC: simplify some calculations in decode_spectrum_and_dequant() Simplify cur_band_type, group_len, and coef/offset calculations. This makes the code easier to read and slightly faster.
author mru
date Wed, 13 Jan 2010 16:46:32 +0000
parents 8e1fbf9b0858
children 8a4984c5cacc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9867
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
1 /*
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
2 * DivX (XSUB) subtitle encoder
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
3 * Copyright (c) 2005 DivX, Inc.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
4 * Copyright (c) 2009 Bjorn Axelsson
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
5 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
6 * This file is part of FFmpeg.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
7 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
12 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
16 * Lesser General Public License for more details.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
17 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
21 */
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
22
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
23 #include "avcodec.h"
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
24 #include "bytestream.h"
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
25 #include "put_bits.h"
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
26
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
27 /**
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
28 * Number of pixels to pad left and right.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
29 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
30 * The official encoder pads the subtitles with two pixels on either side,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
31 * but until we find out why, we won't do it (we will pad to have width
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
32 * divisible by 2 though).
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
33 */
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
34 #define PADDING 0
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
35 #define PADDING_COLOR 0
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
36
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
37 /**
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
38 * Encodes a single color run. At most 16 bits will be used.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
39 * \param len length of the run, values > 255 mean "until end of line", may not be < 0.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
40 * \param color color to encode, only the lowest two bits are used and all others must be 0.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
41 */
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
42 static void put_xsub_rle(PutBitContext *pb, int len, int color)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
43 {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
44 if (len <= 255)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
45 put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
46 else
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
47 put_bits(pb, 14, 0);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
48 put_bits(pb, 2, color);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
49 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
50
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
51 /**
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
52 * Encodes a 4-color bitmap with XSUB rle.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
53 *
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
54 * The encoded bitmap may be wider than the source bitmap due to padding.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
55 */
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
56 static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
57 int linesize, int w, int h)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
58 {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
59 int x0, x1, y, len, color = PADDING_COLOR;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
60
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
61 for (y = 0; y < h; y++) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
62 x0 = 0;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
63 while (x0 < w) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
64 // Make sure we have enough room for at least one run and padding
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
65 if (pb->size_in_bits - put_bits_count(pb) < 7*8)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
66 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
67
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
68 x1 = x0;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
69 color = bitmap[x1++] & 3;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
70 while (x1 < w && (bitmap[x1] & 3) == color)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
71 x1++;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
72 len = x1 - x0;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
73 if (PADDING && x0 == 0) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
74 if (color == PADDING_COLOR) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
75 len += PADDING;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
76 x0 -= PADDING;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
77 } else
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
78 put_xsub_rle(pb, PADDING, PADDING_COLOR);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
79 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
80
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
81 // Run can't be longer than 255, unless it is the rest of a row
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
82 if (x1 == w && color == PADDING_COLOR) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
83 len += PADDING + (w&1);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
84 } else
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
85 len = FFMIN(len, 255);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
86 put_xsub_rle(pb, len, color);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
87
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
88 x0 += len;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
89 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
90 if (color != PADDING_COLOR && (PADDING + (w&1)))
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
91 put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
92
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
93 align_put_bits(pb);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
94
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
95 bitmap += linesize;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
96 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
97
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
98 return 0;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
99 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
100
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
101 static int make_tc(uint64_t ms, int *tc)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
102 {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
103 static const int tc_divs[3] = { 1000, 60, 60 };
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
104 int i;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
105 for (i=0; i<3; i++) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
106 tc[i] = ms % tc_divs[i];
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
107 ms /= tc_divs[i];
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
108 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
109 tc[3] = ms;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
110 return ms > 99;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
111 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
112
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
113 static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
114 int bufsize, void *data)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
115 {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
116 AVSubtitle *h = data;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
117 uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
118 uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
119 int start_tc[4], end_tc[4];
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
120 uint8_t *hdr = buf + 27; // Point behind the timestamp
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
121 uint8_t *rlelenptr;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
122 uint16_t width, height;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
123 int i;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
124 PutBitContext pb;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
125
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
126 if (bufsize < 27 + 7*2 + 4*3) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
127 av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
128 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
129 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
130
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
131 // TODO: support multiple rects
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
132 if (h->num_rects > 1)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
133 av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
134
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
135 // TODO: render text-based subtitles into bitmaps
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
136 if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
137 av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
138 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
139 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
140
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
141 // TODO: color reduction, similar to dvdsub encoder
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
142 if (h->rects[0]->nb_colors > 4)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
143 av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
144
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
145 // TODO: Palette swapping if color zero is not transparent
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
146 if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
147 av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
148
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
149 if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
150 av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
151 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
152 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
153
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
154 snprintf(buf, 28,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
155 "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
156 start_tc[3], start_tc[2], start_tc[1], start_tc[0],
10091
8e1fbf9b0858 100l typo, xsub encoder printed end time hours where minutes should have been.
reimar
parents: 9867
diff changeset
157 end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
9867
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
158
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
159 // Width and height must probably be multiples of 2.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
160 // 2 pixels required on either side of subtitle.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
161 // Possibly due to limitations of hardware renderers.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
162 // TODO: check if the bitmap is already padded
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
163 width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
164 height = FFALIGN(h->rects[0]->h, 2);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
165
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
166 bytestream_put_le16(&hdr, width);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
167 bytestream_put_le16(&hdr, height);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
168 bytestream_put_le16(&hdr, h->rects[0]->x);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
169 bytestream_put_le16(&hdr, h->rects[0]->y);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
170 bytestream_put_le16(&hdr, h->rects[0]->x + width);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
171 bytestream_put_le16(&hdr, h->rects[0]->y + height);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
172
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
173 rlelenptr = hdr; // Will store length of first field here later.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
174 hdr+=2;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
175
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
176 // Palette
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
177 for (i=0; i<4; i++)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
178 bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
179
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
180 // Bitmap
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
181 // RLE buffer. Reserve 2 bytes for possible padding after the last row.
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
182 init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
183 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
184 h->rects[0]->pict.linesize[0]*2,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
185 h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
186 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
187 bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
188
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
189 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
190 h->rects[0]->pict.linesize[0]*2,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
191 h->rects[0]->w, h->rects[0]->h >> 1))
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
192 return -1;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
193
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
194 // Enforce total height to be be multiple of 2
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
195 if (h->rects[0]->h & 1) {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
196 put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
197 align_put_bits(&pb);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
198 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
199
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
200 flush_put_bits(&pb);
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
201
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
202 return hdr - buf + put_bits_count(&pb)/8;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
203 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
204
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
205 static av_cold int xsub_encoder_init(AVCodecContext *avctx)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
206 {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
207 if (!avctx->codec_tag)
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
208 avctx->codec_tag = MKTAG('D','X','S','B');
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
209
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
210 return 0;
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
211 }
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
212
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
213 AVCodec xsub_encoder = {
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
214 "xsub",
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
215 CODEC_TYPE_SUBTITLE,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
216 CODEC_ID_XSUB,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
217 0,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
218 xsub_encoder_init,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
219 xsub_encode,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
220 NULL,
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
221 .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
4ccec8d051fa Add support for encoding XSUB subtitles. Muxing support is still missing.
reimar
parents:
diff changeset
222 };