00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050
00051 #include "avcodec.h"
00052 #include "internal.h"
00053 #include "libavutil/internal.h"
00054
00055 #define HUFFMAN_TABLE_SIZE 64 * 1024
00056 #define HUF_TOKENS 256
00057 #define PALETTE_COUNT 256
00058
00059 typedef struct
00060 {
00061 int count;
00062 unsigned char used;
00063 int children[2];
00064 } hnode;
00065
00066 typedef struct IdcinContext {
00067
00068 AVCodecContext *avctx;
00069 AVFrame frame;
00070
00071 const unsigned char *buf;
00072 int size;
00073
00074 hnode huff_nodes[256][HUF_TOKENS*2];
00075 int num_huff_nodes[256];
00076
00077 uint32_t pal[256];
00078 } IdcinContext;
00079
00086 static int huff_smallest_node(hnode *hnodes, int num_hnodes) {
00087 int i;
00088 int best, best_node;
00089
00090 best = 99999999;
00091 best_node = -1;
00092 for(i = 0; i < num_hnodes; i++) {
00093 if(hnodes[i].used)
00094 continue;
00095 if(!hnodes[i].count)
00096 continue;
00097 if(hnodes[i].count < best) {
00098 best = hnodes[i].count;
00099 best_node = i;
00100 }
00101 }
00102
00103 if(best_node == -1)
00104 return -1;
00105 hnodes[best_node].used = 1;
00106 return best_node;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 static av_cold void huff_build_tree(IdcinContext *s, int prev) {
00119 hnode *node, *hnodes;
00120 int num_hnodes, i;
00121
00122 num_hnodes = HUF_TOKENS;
00123 hnodes = s->huff_nodes[prev];
00124 for(i = 0; i < HUF_TOKENS * 2; i++)
00125 hnodes[i].used = 0;
00126
00127 while (1) {
00128 node = &hnodes[num_hnodes];
00129
00130
00131 node->children[0] = huff_smallest_node(hnodes, num_hnodes);
00132 if(node->children[0] == -1)
00133 break;
00134
00135 node->children[1] = huff_smallest_node(hnodes, num_hnodes);
00136 if(node->children[1] == -1)
00137 break;
00138
00139
00140 node->count = hnodes[node->children[0]].count +
00141 hnodes[node->children[1]].count;
00142 num_hnodes++;
00143 }
00144
00145 s->num_huff_nodes[prev] = num_hnodes - 1;
00146 }
00147
00148 static av_cold int idcin_decode_init(AVCodecContext *avctx)
00149 {
00150 IdcinContext *s = avctx->priv_data;
00151 int i, j, histogram_index = 0;
00152 unsigned char *histograms;
00153
00154 s->avctx = avctx;
00155 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00156
00157
00158 if (s->avctx->extradata_size != HUFFMAN_TABLE_SIZE) {
00159 av_log(s->avctx, AV_LOG_ERROR, " id CIN video: expected extradata size of %d\n", HUFFMAN_TABLE_SIZE);
00160 return -1;
00161 }
00162
00163
00164 histograms = (unsigned char *)s->avctx->extradata;
00165 for (i = 0; i < 256; i++) {
00166 for(j = 0; j < HUF_TOKENS; j++)
00167 s->huff_nodes[i][j].count = histograms[histogram_index++];
00168 huff_build_tree(s, i);
00169 }
00170
00171 avcodec_get_frame_defaults(&s->frame);
00172 s->frame.data[0] = NULL;
00173
00174 return 0;
00175 }
00176
00177 static int idcin_decode_vlcs(IdcinContext *s)
00178 {
00179 hnode *hnodes;
00180 long x, y;
00181 int prev;
00182 unsigned char v = 0;
00183 int bit_pos, node_num, dat_pos;
00184
00185 prev = bit_pos = dat_pos = 0;
00186 for (y = 0; y < (s->frame.linesize[0] * s->avctx->height);
00187 y += s->frame.linesize[0]) {
00188 for (x = y; x < y + s->avctx->width; x++) {
00189 node_num = s->num_huff_nodes[prev];
00190 hnodes = s->huff_nodes[prev];
00191
00192 while(node_num >= HUF_TOKENS) {
00193 if(!bit_pos) {
00194 if(dat_pos >= s->size) {
00195 av_log(s->avctx, AV_LOG_ERROR, "Huffman decode error.\n");
00196 return -1;
00197 }
00198 bit_pos = 8;
00199 v = s->buf[dat_pos++];
00200 }
00201
00202 node_num = hnodes[node_num].children[v & 0x01];
00203 v = v >> 1;
00204 bit_pos--;
00205 }
00206
00207 s->frame.data[0][x] = node_num;
00208 prev = node_num;
00209 }
00210 }
00211
00212 return 0;
00213 }
00214
00215 static int idcin_decode_frame(AVCodecContext *avctx,
00216 void *data, int *got_frame,
00217 AVPacket *avpkt)
00218 {
00219 const uint8_t *buf = avpkt->data;
00220 int buf_size = avpkt->size;
00221 IdcinContext *s = avctx->priv_data;
00222 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00223 int ret;
00224
00225 s->buf = buf;
00226 s->size = buf_size;
00227
00228 if (s->frame.data[0])
00229 avctx->release_buffer(avctx, &s->frame);
00230
00231 if ((ret = ff_get_buffer(avctx, &s->frame))) {
00232 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00233 return ret;
00234 }
00235
00236 if (idcin_decode_vlcs(s))
00237 return AVERROR_INVALIDDATA;
00238
00239 if (pal) {
00240 s->frame.palette_has_changed = 1;
00241 memcpy(s->pal, pal, AVPALETTE_SIZE);
00242 }
00243
00244 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00245
00246 *got_frame = 1;
00247 *(AVFrame*)data = s->frame;
00248
00249
00250 return buf_size;
00251 }
00252
00253 static av_cold int idcin_decode_end(AVCodecContext *avctx)
00254 {
00255 IdcinContext *s = avctx->priv_data;
00256
00257 if (s->frame.data[0])
00258 avctx->release_buffer(avctx, &s->frame);
00259
00260 return 0;
00261 }
00262
00263 AVCodec ff_idcin_decoder = {
00264 .name = "idcinvideo",
00265 .type = AVMEDIA_TYPE_VIDEO,
00266 .id = AV_CODEC_ID_IDCIN,
00267 .priv_data_size = sizeof(IdcinContext),
00268 .init = idcin_decode_init,
00269 .close = idcin_decode_end,
00270 .decode = idcin_decode_frame,
00271 .capabilities = CODEC_CAP_DR1,
00272 .long_name = NULL_IF_CONFIG_SMALL("id Quake II CIN video"),
00273 };