Mercurial > libavcodec.hg
annotate 8bps.c @ 2682:dfa4d2773d13 libavcodec
work with latest svn of x264
author | mru |
---|---|
date | Sat, 14 May 2005 21:02:49 +0000 |
parents | f67b63ed036d |
children | 1bf080e490db |
rev | line source |
---|---|
1608 | 1 /* |
2 * Quicktime Planar RGB (8BPS) Video Decoder | |
3 * Copyright (C) 2003 Roberto Togni | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file 8bps.c | |
23 * QT 8BPS Video Decoder by Roberto Togni <rtogni at bresciaonline dot it> | |
24 * For more information about the 8BPS format, visit: | |
25 * http://www.pcisys.net/~melanson/codecs/ | |
26 * | |
27 * Supports: PAL8 (RGB 8bpp, paletted) | |
28 * : BGR24 (RGB 24bpp) (can also output it as RGBA32) | |
29 * : RGBA32 (RGB 32bpp, 4th plane is probably alpha and it's ignored) | |
30 * | |
31 */ | |
32 | |
33 #include <stdio.h> | |
34 #include <stdlib.h> | |
35 | |
36 #include "common.h" | |
37 #include "avcodec.h" | |
38 | |
39 | |
40 const enum PixelFormat pixfmt_rgb24[] = {PIX_FMT_BGR24, PIX_FMT_RGBA32, -1}; | |
41 | |
42 /* | |
43 * Decoder context | |
44 */ | |
45 typedef struct EightBpsContext { | |
46 | |
47 AVCodecContext *avctx; | |
48 AVFrame pic; | |
49 | |
50 unsigned char planes; | |
51 unsigned char planemap[4]; | |
52 } EightBpsContext; | |
53 | |
54 | |
55 /* | |
56 * | |
57 * Decode a frame | |
58 * | |
59 */ | |
60 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
61 { | |
62 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data; | |
63 unsigned char *encoded = (unsigned char *)buf; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
64 unsigned char *pixptr, *pixptr_end; |
1608 | 65 unsigned int height = avctx->height; // Real image height |
66 unsigned int dlen, p, row; | |
67 unsigned char *lp, *dp; | |
68 unsigned char count; | |
69 unsigned int px_inc; | |
70 unsigned int planes = c->planes; | |
71 unsigned char *planemap = c->planemap; | |
72 | |
73 if(c->pic.data[0]) | |
74 avctx->release_buffer(avctx, &c->pic); | |
75 | |
76 c->pic.reference = 0; | |
77 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID; | |
78 if(avctx->get_buffer(avctx, &c->pic) < 0){ | |
79 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
80 return -1; | |
81 } | |
82 | |
83 /* Set data pointer after line lengths */ | |
84 dp = encoded + planes * (height << 1); | |
85 | |
86 /* Ignore alpha plane, don't know what to do with it */ | |
87 if (planes == 4) | |
88 planes--; | |
89 | |
90 px_inc = planes + (avctx->pix_fmt == PIX_FMT_RGBA32); | |
91 | |
92 for (p = 0; p < planes; p++) { | |
93 /* Lines length pointer for this plane */ | |
94 lp = encoded + p * (height << 1); | |
95 | |
96 /* Decode a plane */ | |
97 for(row = 0; row < height; row++) { | |
98 pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p]; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
99 pixptr_end = pixptr + c->pic.linesize[0]; |
1608 | 100 dlen = be2me_16(*(unsigned short *)(lp+row*2)); |
101 /* Decode a row of this plane */ | |
102 while(dlen > 0) { | |
103 if ((count = *dp++) <= 127) { | |
104 count++; | |
105 dlen -= count + 1; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
106 if (pixptr + count * px_inc > pixptr_end) |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
107 break; |
1608 | 108 while(count--) { |
109 *pixptr = *dp++; | |
110 pixptr += px_inc; | |
111 } | |
112 } else { | |
113 count = 257 - count; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
114 if (pixptr + count * px_inc > pixptr_end) |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
115 break; |
1608 | 116 while(count--) { |
117 *pixptr = *dp; | |
118 pixptr += px_inc; | |
119 } | |
120 dp++; | |
121 dlen -= 2; | |
122 } | |
123 } | |
124 } | |
125 } | |
126 | |
127 if (avctx->palctrl) { | |
128 memcpy (c->pic.data[1], avctx->palctrl->palette, AVPALETTE_SIZE); | |
129 if (avctx->palctrl->palette_changed) { | |
130 c->pic.palette_has_changed = 1; | |
131 avctx->palctrl->palette_changed = 0; | |
132 } else | |
133 c->pic.palette_has_changed = 0; | |
134 } | |
135 | |
136 *data_size = sizeof(AVFrame); | |
137 *(AVFrame*)data = c->pic; | |
138 | |
139 /* always report that the buffer was completely consumed */ | |
140 return buf_size; | |
141 } | |
142 | |
143 | |
144 /* | |
145 * | |
146 * Init 8BPS decoder | |
147 * | |
148 */ | |
149 static int decode_init(AVCodecContext *avctx) | |
150 { | |
151 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data; | |
152 | |
153 c->avctx = avctx; | |
154 avctx->has_b_frames = 0; | |
155 | |
156 c->pic.data[0] = NULL; | |
157 | |
2429
4b350cc506a7
Use avcodec_check_dimensions instead of custom hack
rtognimp
parents:
2418
diff
changeset
|
158 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
159 return 1; |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
160 } |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
1608
diff
changeset
|
161 |
1608 | 162 switch (avctx->bits_per_sample) { |
163 case 8: | |
164 avctx->pix_fmt = PIX_FMT_PAL8; | |
165 c->planes = 1; | |
166 c->planemap[0] = 0; // 1st plane is palette indexes | |
167 if (avctx->palctrl == NULL) { | |
168 av_log(avctx, AV_LOG_ERROR, "Error: PAL8 format but no palette from demuxer.\n"); | |
169 return -1; | |
170 } | |
171 break; | |
172 case 24: | |
173 avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24); | |
174 c->planes = 3; | |
175 c->planemap[0] = 2; // 1st plane is red | |
176 c->planemap[1] = 1; // 2nd plane is green | |
177 c->planemap[2] = 0; // 3rd plane is blue | |
178 break; | |
179 case 32: | |
180 avctx->pix_fmt = PIX_FMT_RGBA32; | |
181 c->planes = 4; | |
182 #ifdef WORDS_BIGENDIAN | |
183 c->planemap[0] = 1; // 1st plane is red | |
184 c->planemap[1] = 2; // 2nd plane is green | |
185 c->planemap[2] = 3; // 3rd plane is blue | |
186 c->planemap[3] = 0; // 4th plane is alpha??? | |
187 #else | |
188 c->planemap[0] = 2; // 1st plane is red | |
189 c->planemap[1] = 1; // 2nd plane is green | |
190 c->planemap[2] = 0; // 3rd plane is blue | |
191 c->planemap[3] = 3; // 4th plane is alpha??? | |
192 #endif | |
193 break; | |
194 default: | |
195 av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_sample); | |
196 return -1; | |
197 } | |
198 | |
199 return 0; | |
200 } | |
201 | |
202 | |
203 | |
204 | |
205 /* | |
206 * | |
207 * Uninit 8BPS decoder | |
208 * | |
209 */ | |
210 static int decode_end(AVCodecContext *avctx) | |
211 { | |
212 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data; | |
213 | |
214 if (c->pic.data[0]) | |
215 avctx->release_buffer(avctx, &c->pic); | |
216 | |
217 return 0; | |
218 } | |
219 | |
220 | |
221 | |
222 AVCodec eightbps_decoder = { | |
223 "8bps", | |
224 CODEC_TYPE_VIDEO, | |
225 CODEC_ID_8BPS, | |
226 sizeof(EightBpsContext), | |
227 decode_init, | |
228 NULL, | |
229 decode_end, | |
230 decode_frame, | |
231 CODEC_CAP_DR1, | |
232 }; |