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