Merge commit '36ef5369ee9b336febc2c270f8718cec4476cb85'
[ffmpeg.git] / libavcodec / dvdsubenc.c
1 /*
2  * DVD subtitle encoding
3  * Copyright (c) 2005 Wolfram Gloger
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "libavutil/avassert.h"
24
25
26 // ncnt is the nibble counter
27 #define PUTNIBBLE(val)\
28 do {\
29     if (ncnt++ & 1)\
30         *q++ = bitbuf | ((val) & 0x0f);\
31     else\
32         bitbuf = (val) << 4;\
33 } while(0)
34
35 static void dvd_encode_rle(uint8_t **pq,
36                            const uint8_t *bitmap, int linesize,
37                            int w, int h,
38                            const int cmap[256])
39 {
40     uint8_t *q;
41     unsigned int bitbuf = 0;
42     int ncnt;
43     int x, y, len, color;
44
45     q = *pq;
46
47     for (y = 0; y < h; ++y) {
48         ncnt = 0;
49         for(x = 0; x < w; x += len) {
50             color = bitmap[x];
51             for (len=1; x+len < w; ++len)
52                 if (bitmap[x+len] != color)
53                     break;
54             color = cmap[color];
55             av_assert0(color < 4);
56             if (len < 0x04) {
57                 PUTNIBBLE((len << 2)|color);
58             } else if (len < 0x10) {
59                 PUTNIBBLE(len >> 2);
60                 PUTNIBBLE((len << 2)|color);
61             } else if (len < 0x40) {
62                 PUTNIBBLE(0);
63                 PUTNIBBLE(len >> 2);
64                 PUTNIBBLE((len << 2)|color);
65             } else if (x+len == w) {
66                 PUTNIBBLE(0);
67                 PUTNIBBLE(0);
68                 PUTNIBBLE(0);
69                 PUTNIBBLE(color);
70             } else {
71                 if (len > 0xff)
72                     len = 0xff;
73                 PUTNIBBLE(0);
74                 PUTNIBBLE(len >> 6);
75                 PUTNIBBLE(len >> 2);
76                 PUTNIBBLE((len << 2)|color);
77             }
78         }
79         /* end of line */
80         if (ncnt & 1)
81             PUTNIBBLE(0);
82         bitmap += linesize;
83     }
84
85     *pq = q;
86 }
87
88 static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
89                                 const AVSubtitle *h)
90 {
91     uint8_t *q, *qq;
92     int object_id;
93     int offset1[20], offset2[20];
94     int i, imax, color, alpha, rects = h->num_rects;
95     unsigned long hmax;
96     unsigned long hist[256];
97     int           cmap[256];
98
99     if (rects == 0 || h->rects == NULL)
100         return -1;
101     if (rects > 20)
102         rects = 20;
103
104     // analyze bitmaps, compress to 4 colors
105     for (i=0; i<256; ++i) {
106         hist[i] = 0;
107         cmap[i] = 0;
108     }
109     for (object_id = 0; object_id < rects; object_id++)
110         for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
111             color = h->rects[object_id]->pict.data[0][i];
112             // only count non-transparent pixels
113             alpha = ((uint32_t*)h->rects[object_id]->pict.data[1])[color] >> 24;
114             hist[color] += alpha;
115         }
116     for (color=3;; --color) {
117         hmax = 0;
118         imax = 0;
119         for (i=0; i<256; ++i)
120             if (hist[i] > hmax) {
121                 imax = i;
122                 hmax = hist[i];
123             }
124         if (hmax == 0)
125             break;
126         if (color == 0)
127             color = 3;
128         av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
129                imax, hist[imax], color);
130         cmap[imax] = color;
131         hist[imax] = 0;
132     }
133
134
135     // encode data block
136     q = outbuf + 4;
137     for (object_id = 0; object_id < rects; object_id++) {
138         offset1[object_id] = q - outbuf;
139         // worst case memory requirement: 1 nibble per pixel..
140         if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
141             + 17*rects + 21 > outbuf_size) {
142             av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
143             return -1;
144         }
145         dvd_encode_rle(&q, h->rects[object_id]->pict.data[0],
146                        h->rects[object_id]->w*2,
147                        h->rects[object_id]->w, h->rects[object_id]->h >> 1,
148                        cmap);
149         offset2[object_id] = q - outbuf;
150         dvd_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
151                        h->rects[object_id]->w*2,
152                        h->rects[object_id]->w, h->rects[object_id]->h >> 1,
153                        cmap);
154     }
155
156     // set data packet size
157     qq = outbuf + 2;
158     bytestream_put_be16(&qq, q - outbuf);
159
160     // send start display command
161     bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
162     bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
163     *q++ = 0x03; // palette - 4 nibbles
164     *q++ = 0x03; *q++ = 0x7f;
165     *q++ = 0x04; // alpha - 4 nibbles
166     *q++ = 0xf0; *q++ = 0x00;
167     //*q++ = 0x0f; *q++ = 0xff;
168
169     // XXX not sure if more than one rect can really be encoded..
170     // 12 bytes per rect
171     for (object_id = 0; object_id < rects; object_id++) {
172         int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
173         int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
174
175         *q++ = 0x05;
176         // x1 x2 -> 6 nibbles
177         *q++ = h->rects[object_id]->x >> 4;
178         *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
179         *q++ = x2;
180         // y1 y2 -> 6 nibbles
181         *q++ = h->rects[object_id]->y >> 4;
182         *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
183         *q++ = y2;
184
185         *q++ = 0x06;
186         // offset1, offset2
187         bytestream_put_be16(&q, offset1[object_id]);
188         bytestream_put_be16(&q, offset2[object_id]);
189     }
190     *q++ = 0x01; // start command
191     *q++ = 0xff; // terminating command
192
193     // send stop display command last
194     bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
195     bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
196     *q++ = 0x02; // set end
197     *q++ = 0xff; // terminating command
198
199     qq = outbuf;
200     bytestream_put_be16(&qq, q - outbuf);
201
202     av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
203     return q - outbuf;
204 }
205
206 static int dvdsub_encode(AVCodecContext *avctx,
207                          unsigned char *buf, int buf_size, void *data)
208 {
209     //DVDSubtitleContext *s = avctx->priv_data;
210     AVSubtitle *sub = data;
211     int ret;
212
213     ret = encode_dvd_subtitles(buf, buf_size, sub);
214     return ret;
215 }
216
217 AVCodec ff_dvdsub_encoder = {
218     .name           = "dvdsub",
219     .type           = AVMEDIA_TYPE_SUBTITLE,
220     .id             = AV_CODEC_ID_DVD_SUBTITLE,
221     .encode         = dvdsub_encode,
222     .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
223 };