annotate ffmpeg/libavcodec/idcinvideo.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * id Quake II CIN Video Decoder
yading@10 3 * Copyright (C) 2003 the ffmpeg project
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * id Quake II Cin Video Decoder by Dr. Tim Ferguson
yading@10 25 * For more information about the id CIN format, visit:
yading@10 26 * http://www.csse.monash.edu.au/~timf/
yading@10 27 *
yading@10 28 * This video decoder outputs PAL8 colorspace data. Interacting with this
yading@10 29 * decoder is a little involved. During initialization, the demuxer must
yading@10 30 * transmit the 65536-byte Huffman table(s) to the decoder via extradata.
yading@10 31 * Then, whenever a palette change is encountered while demuxing the file,
yading@10 32 * the demuxer must use the same extradata space to transmit an
yading@10 33 * AVPaletteControl structure.
yading@10 34 *
yading@10 35 * id CIN video is purely Huffman-coded, intraframe-only codec. It achieves
yading@10 36 * a little more compression by exploiting the fact that adjacent pixels
yading@10 37 * tend to be similar.
yading@10 38 *
yading@10 39 * Note that this decoder could use libavcodec's optimized VLC facilities
yading@10 40 * rather than naive, tree-based Huffman decoding. However, there are 256
yading@10 41 * Huffman tables. Plus, the VLC bit coding order is right -> left instead
yading@10 42 * or left -> right, so all of the bits would have to be reversed. Further,
yading@10 43 * the original Quake II implementation likely used a similar naive
yading@10 44 * decoding algorithm and it worked fine on much lower spec machines.
yading@10 45 */
yading@10 46
yading@10 47 #include <stdio.h>
yading@10 48 #include <stdlib.h>
yading@10 49 #include <string.h>
yading@10 50
yading@10 51 #include "avcodec.h"
yading@10 52 #include "internal.h"
yading@10 53 #include "libavutil/internal.h"
yading@10 54
yading@10 55 #define HUFFMAN_TABLE_SIZE 64 * 1024
yading@10 56 #define HUF_TOKENS 256
yading@10 57 #define PALETTE_COUNT 256
yading@10 58
yading@10 59 typedef struct
yading@10 60 {
yading@10 61 int count;
yading@10 62 unsigned char used;
yading@10 63 int children[2];
yading@10 64 } hnode;
yading@10 65
yading@10 66 typedef struct IdcinContext {
yading@10 67
yading@10 68 AVCodecContext *avctx;
yading@10 69
yading@10 70 const unsigned char *buf;
yading@10 71 int size;
yading@10 72
yading@10 73 hnode huff_nodes[256][HUF_TOKENS*2];
yading@10 74 int num_huff_nodes[256];
yading@10 75
yading@10 76 uint32_t pal[256];
yading@10 77 } IdcinContext;
yading@10 78
yading@10 79 /**
yading@10 80 * Find the lowest probability node in a Huffman table, and mark it as
yading@10 81 * being assigned to a higher probability.
yading@10 82 * @return the node index of the lowest unused node, or -1 if all nodes
yading@10 83 * are used.
yading@10 84 */
yading@10 85 static int huff_smallest_node(hnode *hnodes, int num_hnodes) {
yading@10 86 int i;
yading@10 87 int best, best_node;
yading@10 88
yading@10 89 best = 99999999;
yading@10 90 best_node = -1;
yading@10 91 for(i = 0; i < num_hnodes; i++) {
yading@10 92 if(hnodes[i].used)
yading@10 93 continue;
yading@10 94 if(!hnodes[i].count)
yading@10 95 continue;
yading@10 96 if(hnodes[i].count < best) {
yading@10 97 best = hnodes[i].count;
yading@10 98 best_node = i;
yading@10 99 }
yading@10 100 }
yading@10 101
yading@10 102 if(best_node == -1)
yading@10 103 return -1;
yading@10 104 hnodes[best_node].used = 1;
yading@10 105 return best_node;
yading@10 106 }
yading@10 107
yading@10 108 /*
yading@10 109 * Build the Huffman tree using the generated/loaded probabilities histogram.
yading@10 110 *
yading@10 111 * On completion:
yading@10 112 * huff_nodes[prev][i < HUF_TOKENS] - are the nodes at the base of the tree.
yading@10 113 * huff_nodes[prev][i >= HUF_TOKENS] - are used to construct the tree.
yading@10 114 * num_huff_nodes[prev] - contains the index to the root node of the tree.
yading@10 115 * That is: huff_nodes[prev][num_huff_nodes[prev]] is the root node.
yading@10 116 */
yading@10 117 static av_cold void huff_build_tree(IdcinContext *s, int prev) {
yading@10 118 hnode *node, *hnodes;
yading@10 119 int num_hnodes, i;
yading@10 120
yading@10 121 num_hnodes = HUF_TOKENS;
yading@10 122 hnodes = s->huff_nodes[prev];
yading@10 123 for(i = 0; i < HUF_TOKENS * 2; i++)
yading@10 124 hnodes[i].used = 0;
yading@10 125
yading@10 126 while (1) {
yading@10 127 node = &hnodes[num_hnodes]; /* next free node */
yading@10 128
yading@10 129 /* pick two lowest counts */
yading@10 130 node->children[0] = huff_smallest_node(hnodes, num_hnodes);
yading@10 131 if(node->children[0] == -1)
yading@10 132 break; /* reached the root node */
yading@10 133
yading@10 134 node->children[1] = huff_smallest_node(hnodes, num_hnodes);
yading@10 135 if(node->children[1] == -1)
yading@10 136 break; /* reached the root node */
yading@10 137
yading@10 138 /* combine nodes probability for new node */
yading@10 139 node->count = hnodes[node->children[0]].count +
yading@10 140 hnodes[node->children[1]].count;
yading@10 141 num_hnodes++;
yading@10 142 }
yading@10 143
yading@10 144 s->num_huff_nodes[prev] = num_hnodes - 1;
yading@10 145 }
yading@10 146
yading@10 147 static av_cold int idcin_decode_init(AVCodecContext *avctx)
yading@10 148 {
yading@10 149 IdcinContext *s = avctx->priv_data;
yading@10 150 int i, j, histogram_index = 0;
yading@10 151 unsigned char *histograms;
yading@10 152
yading@10 153 s->avctx = avctx;
yading@10 154 avctx->pix_fmt = AV_PIX_FMT_PAL8;
yading@10 155
yading@10 156 /* make sure the Huffman tables make it */
yading@10 157 if (s->avctx->extradata_size != HUFFMAN_TABLE_SIZE) {
yading@10 158 av_log(s->avctx, AV_LOG_ERROR, " id CIN video: expected extradata size of %d\n", HUFFMAN_TABLE_SIZE);
yading@10 159 return -1;
yading@10 160 }
yading@10 161
yading@10 162 /* build the 256 Huffman decode trees */
yading@10 163 histograms = (unsigned char *)s->avctx->extradata;
yading@10 164 for (i = 0; i < 256; i++) {
yading@10 165 for(j = 0; j < HUF_TOKENS; j++)
yading@10 166 s->huff_nodes[i][j].count = histograms[histogram_index++];
yading@10 167 huff_build_tree(s, i);
yading@10 168 }
yading@10 169
yading@10 170 return 0;
yading@10 171 }
yading@10 172
yading@10 173 static int idcin_decode_vlcs(IdcinContext *s, AVFrame *frame)
yading@10 174 {
yading@10 175 hnode *hnodes;
yading@10 176 long x, y;
yading@10 177 int prev;
yading@10 178 unsigned char v = 0;
yading@10 179 int bit_pos, node_num, dat_pos;
yading@10 180
yading@10 181 prev = bit_pos = dat_pos = 0;
yading@10 182 for (y = 0; y < (frame->linesize[0] * s->avctx->height);
yading@10 183 y += frame->linesize[0]) {
yading@10 184 for (x = y; x < y + s->avctx->width; x++) {
yading@10 185 node_num = s->num_huff_nodes[prev];
yading@10 186 hnodes = s->huff_nodes[prev];
yading@10 187
yading@10 188 while(node_num >= HUF_TOKENS) {
yading@10 189 if(!bit_pos) {
yading@10 190 if(dat_pos >= s->size) {
yading@10 191 av_log(s->avctx, AV_LOG_ERROR, "Huffman decode error.\n");
yading@10 192 return -1;
yading@10 193 }
yading@10 194 bit_pos = 8;
yading@10 195 v = s->buf[dat_pos++];
yading@10 196 }
yading@10 197
yading@10 198 node_num = hnodes[node_num].children[v & 0x01];
yading@10 199 v = v >> 1;
yading@10 200 bit_pos--;
yading@10 201 }
yading@10 202
yading@10 203 frame->data[0][x] = node_num;
yading@10 204 prev = node_num;
yading@10 205 }
yading@10 206 }
yading@10 207
yading@10 208 return 0;
yading@10 209 }
yading@10 210
yading@10 211 static int idcin_decode_frame(AVCodecContext *avctx,
yading@10 212 void *data, int *got_frame,
yading@10 213 AVPacket *avpkt)
yading@10 214 {
yading@10 215 const uint8_t *buf = avpkt->data;
yading@10 216 int buf_size = avpkt->size;
yading@10 217 IdcinContext *s = avctx->priv_data;
yading@10 218 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
yading@10 219 AVFrame *frame = data;
yading@10 220 int ret;
yading@10 221
yading@10 222 s->buf = buf;
yading@10 223 s->size = buf_size;
yading@10 224
yading@10 225 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 226 return ret;
yading@10 227
yading@10 228 if (idcin_decode_vlcs(s, frame))
yading@10 229 return AVERROR_INVALIDDATA;
yading@10 230
yading@10 231 if (pal) {
yading@10 232 frame->palette_has_changed = 1;
yading@10 233 memcpy(s->pal, pal, AVPALETTE_SIZE);
yading@10 234 }
yading@10 235 /* make the palette available on the way out */
yading@10 236 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
yading@10 237
yading@10 238 *got_frame = 1;
yading@10 239
yading@10 240 /* report that the buffer was completely consumed */
yading@10 241 return buf_size;
yading@10 242 }
yading@10 243
yading@10 244 AVCodec ff_idcin_decoder = {
yading@10 245 .name = "idcinvideo",
yading@10 246 .type = AVMEDIA_TYPE_VIDEO,
yading@10 247 .id = AV_CODEC_ID_IDCIN,
yading@10 248 .priv_data_size = sizeof(IdcinContext),
yading@10 249 .init = idcin_decode_init,
yading@10 250 .decode = idcin_decode_frame,
yading@10 251 .capabilities = CODEC_CAP_DR1,
yading@10 252 .long_name = NULL_IF_CONFIG_SMALL("id Quake II CIN video"),
yading@10 253 };