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
00054 uint32_t pal[256];
00055 } EightBpsContext;
00056
00057
00058
00059
00060
00061
00062
00063 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00064 {
00065 const uint8_t *buf = avpkt->data;
00066 int buf_size = avpkt->size;
00067 EightBpsContext * const c = avctx->priv_data;
00068 const unsigned char *encoded = buf;
00069 unsigned char *pixptr, *pixptr_end;
00070 unsigned int height = avctx->height;
00071 unsigned int dlen, p, row;
00072 const unsigned char *lp, *dp;
00073 unsigned char count;
00074 unsigned int planes = c->planes;
00075 unsigned char *planemap = c->planemap;
00076
00077 if(c->pic.data[0])
00078 avctx->release_buffer(avctx, &c->pic);
00079
00080 c->pic.reference = 0;
00081 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00082 if(avctx->get_buffer(avctx, &c->pic) < 0){
00083 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00084 return -1;
00085 }
00086
00087
00088 dp = encoded + planes * (height << 1);
00089
00090 for (p = 0; p < planes; p++) {
00091
00092 lp = encoded + p * (height << 1);
00093
00094
00095 for(row = 0; row < height; row++) {
00096 pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
00097 pixptr_end = pixptr + c->pic.linesize[0];
00098 dlen = av_be2ne16(*(const unsigned short *)(lp+row*2));
00099
00100 while(dlen > 0) {
00101 if(dp + 1 >= buf+buf_size) return -1;
00102 if ((count = *dp++) <= 127) {
00103 count++;
00104 dlen -= count + 1;
00105 if (pixptr + count * planes > pixptr_end)
00106 break;
00107 if(dp + count > buf+buf_size) return -1;
00108 while(count--) {
00109 *pixptr = *dp++;
00110 pixptr += planes;
00111 }
00112 } else {
00113 count = 257 - count;
00114 if (pixptr + count * planes > pixptr_end)
00115 break;
00116 while(count--) {
00117 *pixptr = *dp;
00118 pixptr += planes;
00119 }
00120 dp++;
00121 dlen -= 2;
00122 }
00123 }
00124 }
00125 }
00126
00127 if (avctx->bits_per_coded_sample <= 8) {
00128 const uint8_t *pal = av_packet_get_side_data(avpkt,
00129 AV_PKT_DATA_PALETTE,
00130 NULL);
00131 if (pal) {
00132 c->pic.palette_has_changed = 1;
00133 memcpy(c->pal, pal, AVPALETTE_SIZE);
00134 }
00135
00136 memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
00137 }
00138
00139 *data_size = sizeof(AVFrame);
00140 *(AVFrame*)data = c->pic;
00141
00142
00143 return buf_size;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152 static av_cold int decode_init(AVCodecContext *avctx)
00153 {
00154 EightBpsContext * const c = avctx->priv_data;
00155
00156 c->avctx = avctx;
00157
00158 avcodec_get_frame_defaults(&c->pic);
00159 c->pic.data[0] = NULL;
00160
00161 switch (avctx->bits_per_coded_sample) {
00162 case 8:
00163 avctx->pix_fmt = PIX_FMT_PAL8;
00164 c->planes = 1;
00165 c->planemap[0] = 0;
00166 break;
00167 case 24:
00168 avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
00169 c->planes = 3;
00170 c->planemap[0] = 2;
00171 c->planemap[1] = 1;
00172 c->planemap[2] = 0;
00173 break;
00174 case 32:
00175 avctx->pix_fmt = PIX_FMT_RGB32;
00176 c->planes = 4;
00177 #if HAVE_BIGENDIAN
00178 c->planemap[0] = 1;
00179 c->planemap[1] = 2;
00180 c->planemap[2] = 3;
00181 c->planemap[3] = 0;
00182 #else
00183 c->planemap[0] = 2;
00184 c->planemap[1] = 1;
00185 c->planemap[2] = 0;
00186 c->planemap[3] = 3;
00187 #endif
00188 break;
00189 default:
00190 av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_coded_sample);
00191 return -1;
00192 }
00193
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 static av_cold int decode_end(AVCodecContext *avctx)
00206 {
00207 EightBpsContext * const c = avctx->priv_data;
00208
00209 if (c->pic.data[0])
00210 avctx->release_buffer(avctx, &c->pic);
00211
00212 return 0;
00213 }
00214
00215
00216
00217 AVCodec ff_eightbps_decoder = {
00218 .name = "8bps",
00219 .type = AVMEDIA_TYPE_VIDEO,
00220 .id = CODEC_ID_8BPS,
00221 .priv_data_size = sizeof(EightBpsContext),
00222 .init = decode_init,
00223 .close = decode_end,
00224 .decode = decode_frame,
00225 .capabilities = CODEC_CAP_DR1,
00226 .long_name = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
00227 };