00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035
00036 #include "common.h"
00037 #include "avcodec.h"
00038
00039
00040 const enum PixelFormat pixfmt_rgb24[] = {PIX_FMT_BGR24, PIX_FMT_RGBA32, -1};
00041
00042
00043
00044
00045 typedef struct EightBpsContext {
00046
00047 AVCodecContext *avctx;
00048 AVFrame pic;
00049
00050 unsigned char planes;
00051 unsigned char planemap[4];
00052 } EightBpsContext;
00053
00054
00055
00056
00057
00058
00059
00060 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
00061 {
00062 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
00063 unsigned char *encoded = (unsigned char *)buf;
00064 unsigned char *pixptr, *pixptr_end;
00065 unsigned int height = avctx->height;
00066 unsigned int dlen, p, row;
00067 unsigned char *lp, *dp;
00068 unsigned char count;
00069 unsigned int px_inc;
00070 unsigned int planes = c->planes;
00071 unsigned char *planemap = c->planemap;
00072
00073 if(c->pic.data[0])
00074 avctx->release_buffer(avctx, &c->pic);
00075
00076 c->pic.reference = 0;
00077 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00078 if(avctx->get_buffer(avctx, &c->pic) < 0){
00079 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00080 return -1;
00081 }
00082
00083
00084 dp = encoded + planes * (height << 1);
00085
00086
00087 if (planes == 4)
00088 planes--;
00089
00090 px_inc = planes + (avctx->pix_fmt == PIX_FMT_RGBA32);
00091
00092 for (p = 0; p < planes; p++) {
00093
00094 lp = encoded + p * (height << 1);
00095
00096
00097 for(row = 0; row < height; row++) {
00098 pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
00099 pixptr_end = pixptr + c->pic.linesize[0];
00100 dlen = be2me_16(*(unsigned short *)(lp+row*2));
00101
00102 while(dlen > 0) {
00103 if(dp + 1 >= buf+buf_size) return -1;
00104 if ((count = *dp++) <= 127) {
00105 count++;
00106 dlen -= count + 1;
00107 if (pixptr + count * px_inc > pixptr_end)
00108 break;
00109 if(dp + count > buf+buf_size) return -1;
00110 while(count--) {
00111 *pixptr = *dp++;
00112 pixptr += px_inc;
00113 }
00114 } else {
00115 count = 257 - count;
00116 if (pixptr + count * px_inc > pixptr_end)
00117 break;
00118 while(count--) {
00119 *pixptr = *dp;
00120 pixptr += px_inc;
00121 }
00122 dp++;
00123 dlen -= 2;
00124 }
00125 }
00126 }
00127 }
00128
00129 if (avctx->palctrl) {
00130 memcpy (c->pic.data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
00131 if (avctx->palctrl->palette_changed) {
00132 c->pic.palette_has_changed = 1;
00133 avctx->palctrl->palette_changed = 0;
00134 } else
00135 c->pic.palette_has_changed = 0;
00136 }
00137
00138 *data_size = sizeof(AVFrame);
00139 *(AVFrame*)data = c->pic;
00140
00141
00142 return buf_size;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 static int decode_init(AVCodecContext *avctx)
00152 {
00153 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
00154
00155 c->avctx = avctx;
00156 avctx->has_b_frames = 0;
00157
00158 c->pic.data[0] = NULL;
00159
00160 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00161 return 1;
00162 }
00163
00164 switch (avctx->bits_per_sample) {
00165 case 8:
00166 avctx->pix_fmt = PIX_FMT_PAL8;
00167 c->planes = 1;
00168 c->planemap[0] = 0;
00169 if (avctx->palctrl == NULL) {
00170 av_log(avctx, AV_LOG_ERROR, "Error: PAL8 format but no palette from demuxer.\n");
00171 return -1;
00172 }
00173 break;
00174 case 24:
00175 avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
00176 c->planes = 3;
00177 c->planemap[0] = 2;
00178 c->planemap[1] = 1;
00179 c->planemap[2] = 0;
00180 break;
00181 case 32:
00182 avctx->pix_fmt = PIX_FMT_RGBA32;
00183 c->planes = 4;
00184 #ifdef WORDS_BIGENDIAN
00185 c->planemap[0] = 1;
00186 c->planemap[1] = 2;
00187 c->planemap[2] = 3;
00188 c->planemap[3] = 0;
00189 #else
00190 c->planemap[0] = 2;
00191 c->planemap[1] = 1;
00192 c->planemap[2] = 0;
00193 c->planemap[3] = 3;
00194 #endif
00195 break;
00196 default:
00197 av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_sample);
00198 return -1;
00199 }
00200
00201 return 0;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 static int decode_end(AVCodecContext *avctx)
00213 {
00214 EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
00215
00216 if (c->pic.data[0])
00217 avctx->release_buffer(avctx, &c->pic);
00218
00219 return 0;
00220 }
00221
00222
00223
00224 AVCodec eightbps_decoder = {
00225 "8bps",
00226 CODEC_TYPE_VIDEO,
00227 CODEC_ID_8BPS,
00228 sizeof(EightBpsContext),
00229 decode_init,
00230 NULL,
00231 decode_end,
00232 decode_frame,
00233 CODEC_CAP_DR1,
00234 };