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