annotate dvdsubenc.c @ 11629:251ce43203f6 libavcodec

Factorize code in get_bit_rate().
author stefano
date Tue, 13 Apr 2010 23:24:25 +0000
parents 8a4984c5cacc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
1 /*
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
2 * DVD subtitle encoding for ffmpeg
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 8516
diff changeset
3 * Copyright (c) 2005 Wolfram Gloger
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
15 * Lesser General Public License for more details.
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
16 *
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
20 */
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
21 #include "avcodec.h"
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
22 #include "bytestream.h"
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
23
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
24 #undef NDEBUG
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
25 #include <assert.h>
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
26
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
27 // ncnt is the nibble counter
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
28 #define PUTNIBBLE(val)\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
29 do {\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
30 if (ncnt++ & 1)\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
31 *q++ = bitbuf | ((val) & 0x0f);\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
32 else\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
33 bitbuf = (val) << 4;\
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
34 } while(0)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
35
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
36 static void dvd_encode_rle(uint8_t **pq,
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
37 const uint8_t *bitmap, int linesize,
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
38 int w, int h,
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
39 const int cmap[256])
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
40 {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
41 uint8_t *q;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
42 unsigned int bitbuf = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
43 int ncnt;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
44 int x, y, len, color;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
45
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
46 q = *pq;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
47
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
48 for (y = 0; y < h; ++y) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
49 ncnt = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
50 for(x = 0; x < w; x += len) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
51 color = bitmap[x];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
52 for (len=1; x+len < w; ++len)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
53 if (bitmap[x+len] != color)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
54 break;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
55 color = cmap[color];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
56 assert(color < 4);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
57 if (len < 0x04) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
58 PUTNIBBLE((len << 2)|color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
59 } else if (len < 0x10) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
60 PUTNIBBLE(len >> 2);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
61 PUTNIBBLE((len << 2)|color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
62 } else if (len < 0x40) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
63 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
64 PUTNIBBLE(len >> 2);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
65 PUTNIBBLE((len << 2)|color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
66 } else if (x+len == w) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
67 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
68 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
69 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
70 PUTNIBBLE(color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
71 } else {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
72 if (len > 0xff)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
73 len = 0xff;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
74 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
75 PUTNIBBLE(len >> 6);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
76 PUTNIBBLE(len >> 2);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
77 PUTNIBBLE((len << 2)|color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
78 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
79 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
80 /* end of line */
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
81 if (ncnt & 1)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
82 PUTNIBBLE(0);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
83 bitmap += linesize;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
84 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
85
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
86 *pq = q;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
87 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
88
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
89 static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
90 const AVSubtitle *h)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
91 {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
92 uint8_t *q, *qq;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
93 int object_id;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
94 int offset1[20], offset2[20];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
95 int i, imax, color, alpha, rects = h->num_rects;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
96 unsigned long hmax;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
97 unsigned long hist[256];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
98 int cmap[256];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
99
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
100 if (rects == 0 || h->rects == NULL)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
101 return -1;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
102 if (rects > 20)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
103 rects = 20;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
104
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
105 // analyze bitmaps, compress to 4 colors
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
106 for (i=0; i<256; ++i) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
107 hist[i] = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
108 cmap[i] = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
109 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
110 for (object_id = 0; object_id < rects; object_id++)
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
111 for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
8516
315b302fcd1d Replace AVSubtitleRect.rgba_palette and bitmap by AVPicture.
michael
parents: 8512
diff changeset
112 color = h->rects[object_id]->pict.data[0][i];
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
113 // only count non-transparent pixels
8516
315b302fcd1d Replace AVSubtitleRect.rgba_palette and bitmap by AVPicture.
michael
parents: 8512
diff changeset
114 alpha = ((uint32_t*)h->rects[object_id]->pict.data[1])[color] >> 24;
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
115 hist[color] += alpha;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
116 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
117 for (color=3;; --color) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
118 hmax = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
119 imax = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
120 for (i=0; i<256; ++i)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
121 if (hist[i] > hmax) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
122 imax = i;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
123 hmax = hist[i];
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
124 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
125 if (hmax == 0)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
126 break;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
127 if (color == 0)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
128 color = 3;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
129 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
130 imax, hist[imax], color);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
131 cmap[imax] = color;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
132 hist[imax] = 0;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
133 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
134
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
135
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
136 // encode data block
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
137 q = outbuf + 4;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
138 for (object_id = 0; object_id < rects; object_id++) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
139 offset1[object_id] = q - outbuf;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
140 // worst case memory requirement: 1 nibble per pixel..
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
141 if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
142 + 17*rects + 21 > outbuf_size) {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
143 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
144 return -1;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
145 }
8516
315b302fcd1d Replace AVSubtitleRect.rgba_palette and bitmap by AVPicture.
michael
parents: 8512
diff changeset
146 dvd_encode_rle(&q, h->rects[object_id]->pict.data[0],
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
147 h->rects[object_id]->w*2,
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
148 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
149 cmap);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
150 offset2[object_id] = q - outbuf;
8516
315b302fcd1d Replace AVSubtitleRect.rgba_palette and bitmap by AVPicture.
michael
parents: 8512
diff changeset
151 dvd_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
152 h->rects[object_id]->w*2,
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
153 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
154 cmap);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
155 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
156
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
157 // set data packet size
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
158 qq = outbuf + 2;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
159 bytestream_put_be16(&qq, q - outbuf);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
160
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
161 // send start display command
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
162 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
163 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
164 *q++ = 0x03; // palette - 4 nibbles
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
165 *q++ = 0x03; *q++ = 0x7f;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
166 *q++ = 0x04; // alpha - 4 nibbles
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
167 *q++ = 0xf0; *q++ = 0x00;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
168 //*q++ = 0x0f; *q++ = 0xff;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
169
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
170 // XXX not sure if more than one rect can really be encoded..
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
171 // 12 bytes per rect
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
172 for (object_id = 0; object_id < rects; object_id++) {
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
173 int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
174 int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
175
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
176 *q++ = 0x05;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
177 // x1 x2 -> 6 nibbles
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
178 *q++ = h->rects[object_id]->x >> 4;
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
179 *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
180 *q++ = x2;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
181 // y1 y2 -> 6 nibbles
8512
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
182 *q++ = h->rects[object_id]->y >> 4;
aa45029f5cd7 Change AVSubtitle.rects to an array of pointers so ABI does not break
michael
parents: 7040
diff changeset
183 *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
184 *q++ = y2;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2962
diff changeset
185
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
186 *q++ = 0x06;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
187 // offset1, offset2
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
188 bytestream_put_be16(&q, offset1[object_id]);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
189 bytestream_put_be16(&q, offset2[object_id]);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
190 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
191 *q++ = 0x01; // start command
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
192 *q++ = 0xff; // terminating command
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
193
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
194 // send stop display command last
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
195 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
196 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
197 *q++ = 0x02; // set end
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
198 *q++ = 0xff; // terminating command
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
199
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
200 qq = outbuf;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4019
diff changeset
201 bytestream_put_be16(&qq, q - outbuf);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2962
diff changeset
202
2962
fde28cb7e3d5 use PRIxN, %zd, %td formats where needed
mru
parents: 2946
diff changeset
203 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
204 return q - outbuf;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
205 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
206
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
207 static int dvdsub_encode(AVCodecContext *avctx,
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
208 unsigned char *buf, int buf_size, void *data)
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
209 {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
210 //DVDSubtitleContext *s = avctx->priv_data;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
211 AVSubtitle *sub = data;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
212 int ret;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
213
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
214 ret = encode_dvd_subtitles(buf, buf_size, sub);
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
215 return ret;
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
216 }
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
217
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
218 AVCodec dvdsub_encoder = {
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
219 "dvdsub",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 8629
diff changeset
220 AVMEDIA_TYPE_SUBTITLE,
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
221 CODEC_ID_DVD_SUBTITLE,
4019
6d4ac21853d7 Remove empty structures,
gpoirier
parents: 3947
diff changeset
222 0,
5941
d030978bcd93 remove some empty close/init functions in avcodec
aurel
parents: 5920
diff changeset
223 NULL,
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
224 dvdsub_encode,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
225 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
2946
ac94d509884e dvbsub encoder, patch by Wolfram Gloger < wmglo AH dent POIS med POIS uni-muenchen POIS de >
gpoirier
parents:
diff changeset
226 };