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 #include <string.h>
00037
00038 #include "libavutil/internal.h"
00039 #include "libavutil/intreadwrite.h"
00040 #include "avcodec.h"
00041 #include "internal.h"
00042
00043
00044 static const enum AVPixelFormat pixfmt_rgb24[] = {
00045 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE };
00046
00047
00048
00049
00050 typedef struct EightBpsContext {
00051 AVCodecContext *avctx;
00052 AVFrame pic;
00053
00054 unsigned char planes;
00055 unsigned char planemap[4];
00056
00057 uint32_t pal[256];
00058 } EightBpsContext;
00059
00060
00061
00062
00063
00064
00065
00066 static int decode_frame(AVCodecContext *avctx, void *data,
00067 int *got_frame, AVPacket *avpkt)
00068 {
00069 const uint8_t *buf = avpkt->data;
00070 int buf_size = avpkt->size;
00071 EightBpsContext * const c = avctx->priv_data;
00072 const unsigned char *encoded = buf;
00073 unsigned char *pixptr, *pixptr_end;
00074 unsigned int height = avctx->height;
00075 unsigned int dlen, p, row;
00076 const unsigned char *lp, *dp;
00077 unsigned char count;
00078 unsigned int planes = c->planes;
00079 unsigned char *planemap = c->planemap;
00080
00081 if (c->pic.data[0])
00082 avctx->release_buffer(avctx, &c->pic);
00083
00084 c->pic.reference = 0;
00085 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00086 if (ff_get_buffer(avctx, &c->pic) < 0){
00087 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00088 return -1;
00089 }
00090
00091
00092 dp = encoded + planes * (height << 1);
00093
00094 for (p = 0; p < planes; p++) {
00095
00096 lp = encoded + p * (height << 1);
00097
00098
00099 for (row = 0; row < height; row++) {
00100 pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
00101 pixptr_end = pixptr + c->pic.linesize[0];
00102 if(lp - encoded + row*2 + 1 >= buf_size)
00103 return -1;
00104 dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2));
00105
00106 while (dlen > 0) {
00107 if (dp + 1 >= buf + buf_size)
00108 return -1;
00109 if ((count = *dp++) <= 127) {
00110 count++;
00111 dlen -= count + 1;
00112 if (pixptr + count * planes > pixptr_end)
00113 break;
00114 if (dp + count > buf + buf_size)
00115 return -1;
00116 while (count--) {
00117 *pixptr = *dp++;
00118 pixptr += planes;
00119 }
00120 } else {
00121 count = 257 - count;
00122 if (pixptr + count * planes > pixptr_end)
00123 break;
00124 while (count--) {
00125 *pixptr = *dp;
00126 pixptr += planes;
00127 }
00128 dp++;
00129 dlen -= 2;
00130 }
00131 }
00132 }
00133 }
00134
00135 if (avctx->bits_per_coded_sample <= 8) {
00136 const uint8_t *pal = av_packet_get_side_data(avpkt,
00137 AV_PKT_DATA_PALETTE,
00138 NULL);
00139 if (pal) {
00140 c->pic.palette_has_changed = 1;
00141 memcpy(c->pal, pal, AVPALETTE_SIZE);
00142 }
00143
00144 memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
00145 }
00146
00147 *got_frame = 1;
00148 *(AVFrame*)data = c->pic;
00149
00150
00151 return buf_size;
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 static av_cold int decode_init(AVCodecContext *avctx)
00161 {
00162 EightBpsContext * const c = avctx->priv_data;
00163
00164 c->avctx = avctx;
00165 c->pic.data[0] = NULL;
00166
00167 avcodec_get_frame_defaults(&c->pic);
00168 switch (avctx->bits_per_coded_sample) {
00169 case 8:
00170 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00171 c->planes = 1;
00172 c->planemap[0] = 0;
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 = AV_PIX_FMT_RGB32;
00183 c->planes = 4;
00184 #if HAVE_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",
00198 avctx->bits_per_coded_sample);
00199 return -1;
00200 }
00201
00202 return 0;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 static av_cold int decode_end(AVCodecContext *avctx)
00214 {
00215 EightBpsContext * const c = avctx->priv_data;
00216
00217 if (c->pic.data[0])
00218 avctx->release_buffer(avctx, &c->pic);
00219
00220 return 0;
00221 }
00222
00223
00224
00225 AVCodec ff_eightbps_decoder = {
00226 .name = "8bps",
00227 .type = AVMEDIA_TYPE_VIDEO,
00228 .id = AV_CODEC_ID_8BPS,
00229 .priv_data_size = sizeof(EightBpsContext),
00230 .init = decode_init,
00231 .close = decode_end,
00232 .decode = decode_frame,
00233 .capabilities = CODEC_CAP_DR1,
00234 .long_name = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
00235 };