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