+ if (get_bits(gb, 1)) {
+ int token;
+ if (s->entries >= 32) { /* overflow */
+ av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
+ return -1;
+ }
+ token = get_bits(gb, 5);
+ //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
+ s->huffman_table[s->hti][token][0] = s->hbits;
+ s->huffman_table[s->hti][token][1] = s->huff_code_size;
+ s->entries++;
+ }
+ else {
+ if (s->huff_code_size >= 32) {/* overflow */
+ av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
+ return -1;
+ }
+ s->huff_code_size++;
+ s->hbits <<= 1;
+ read_huffman_tree(avctx, gb);
+ s->hbits |= 1;
+ read_huffman_tree(avctx, gb);
+ s->hbits >>= 1;
+ s->huff_code_size--;
+ }
+ return 0;
+}
+
+static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
+{
+ Vp3DecodeContext *s = avctx->priv_data;
+
+ s->theora = get_bits_long(gb, 24);
+ av_log(avctx, AV_LOG_INFO, "Theora bitstream version %X\n", s->theora);
+
+ /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
+ /* but previous versions have the image flipped relative to vp3 */
+ if (s->theora < 0x030200)
+ {
+ s->flipped_image = 1;
+ av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
+ }
+
+ s->width = get_bits(gb, 16) << 4;
+ s->height = get_bits(gb, 16) << 4;
+
+ if(avcodec_check_dimensions(avctx, s->width, s->height)){
+ av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
+ s->width= s->height= 0;
+ return -1;
+ }
+
+ if (s->theora >= 0x030400)
+ {
+ skip_bits(gb, 32); /* total number of superblocks in a frame */
+ // fixme, the next field is 36bits long
+ skip_bits(gb, 32); /* total number of blocks in a frame */
+ skip_bits(gb, 4); /* total number of blocks in a frame */
+ skip_bits(gb, 32); /* total number of macroblocks in a frame */
+
+ skip_bits(gb, 24); /* frame width */
+ skip_bits(gb, 24); /* frame height */
+ }
+ else
+ {
+ skip_bits(gb, 24); /* frame width */
+ skip_bits(gb, 24); /* frame height */
+ }
+
+ if (s->theora >= 0x030200) {
+ skip_bits(gb, 8); /* offset x */
+ skip_bits(gb, 8); /* offset y */
+ }
+
+ skip_bits(gb, 32); /* fps numerator */
+ skip_bits(gb, 32); /* fps denumerator */
+ skip_bits(gb, 24); /* aspect numerator */
+ skip_bits(gb, 24); /* aspect denumerator */
+
+ if (s->theora < 0x030200)
+ skip_bits(gb, 5); /* keyframe frequency force */
+ skip_bits(gb, 8); /* colorspace */
+ if (s->theora >= 0x030400)
+ skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
+ skip_bits(gb, 24); /* bitrate */
+
+ skip_bits(gb, 6); /* quality hint */
+
+ if (s->theora >= 0x030200)
+ {
+ skip_bits(gb, 5); /* keyframe frequency force */
+
+ if (s->theora < 0x030400)
+ skip_bits(gb, 5); /* spare bits */
+ }
+
+// align_get_bits(gb);
+
+ avctx->width = s->width;
+ avctx->height = s->height;
+
+ return 0;
+}
+
+static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
+{
+ Vp3DecodeContext *s = avctx->priv_data;
+ int i, n, matrices, inter, plane;
+
+ if (s->theora >= 0x030200) {
+ n = get_bits(gb, 3);
+ /* loop filter limit values table */
+ for (i = 0; i < 64; i++)
+ s->filter_limit_values[i] = get_bits(gb, n);
+ }
+
+ if (s->theora >= 0x030200)
+ n = get_bits(gb, 4) + 1;
+ else
+ n = 16;
+ /* quality threshold table */
+ for (i = 0; i < 64; i++)
+ s->coded_ac_scale_factor[i] = get_bits(gb, n);
+
+ if (s->theora >= 0x030200)
+ n = get_bits(gb, 4) + 1;
+ else
+ n = 16;
+ /* dc scale factor table */
+ for (i = 0; i < 64; i++)
+ s->coded_dc_scale_factor[i] = get_bits(gb, n);
+
+ if (s->theora >= 0x030200)
+ matrices = get_bits(gb, 9) + 1;
+ else
+ matrices = 3;
+
+ if(matrices > 384){
+ av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
+ return -1;
+ }
+
+ for(n=0; n<matrices; n++){
+ for (i = 0; i < 64; i++)
+ s->base_matrix[n][i]= get_bits(gb, 8);
+ }
+
+ for (inter = 0; inter <= 1; inter++) {
+ for (plane = 0; plane <= 2; plane++) {
+ int newqr= 1;
+ if (inter || plane > 0)
+ newqr = get_bits(gb, 1);
+ if (!newqr) {
+ int qtj, plj;
+ if(inter && get_bits(gb, 1)){
+ qtj = 0;
+ plj = plane;
+ }else{
+ qtj= (3*inter + plane - 1) / 3;
+ plj= (plane + 2) % 3;
+ }
+ s->qr_count[inter][plane]= s->qr_count[qtj][plj];
+ memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
+ memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
+ } else {
+ int qri= 0;
+ int qi = 0;
+
+ for(;;){
+ i= get_bits(gb, av_log2(matrices-1)+1);
+ if(i>= matrices){
+ av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
+ return -1;
+ }
+ s->qr_base[inter][plane][qri]= i;
+ if(qi >= 63)
+ break;
+ i = get_bits(gb, av_log2(63-qi)+1) + 1;
+ s->qr_size[inter][plane][qri++]= i;
+ qi += i;
+ }
+
+ if (qi > 63) {
+ av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
+ return -1;
+ }
+ s->qr_count[inter][plane]= qri;
+ }
+ }
+ }
+
+ /* Huffman tables */
+ for (s->hti = 0; s->hti < 80; s->hti++) {
+ s->entries = 0;
+ s->huff_code_size = 1;
+ if (!get_bits(gb, 1)) {
+ s->hbits = 0;
+ read_huffman_tree(avctx, gb);
+ s->hbits = 1;
+ read_huffman_tree(avctx, gb);
+ }
+ }
+
+ s->theora_tables = 1;
+
+ return 0;
+}
+
+static int theora_decode_init(AVCodecContext *avctx)
+{
+ Vp3DecodeContext *s = avctx->priv_data;
+ GetBitContext gb;
+ int ptype;
+ uint8_t *p= avctx->extradata;
+ int op_bytes, i;
+
+ s->theora = 1;
+
+ if (!avctx->extradata_size)
+ {
+ av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
+ return -1;
+ }
+
+ for(i=0;i<3;i++) {
+ op_bytes = *(p++)<<8;
+ op_bytes += *(p++);
+
+ init_get_bits(&gb, p, op_bytes);
+ p += op_bytes;
+
+ ptype = get_bits(&gb, 8);
+ debug_vp3("Theora headerpacket type: %x\n", ptype);
+
+ if (!(ptype & 0x80))
+ {
+ av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
+// return -1;
+ }
+
+ // FIXME: check for this aswell
+ skip_bits(&gb, 6*8); /* "theora" */
+
+ switch(ptype)
+ {
+ case 0x80:
+ theora_decode_header(avctx, &gb);
+ break;
+ case 0x81:
+// FIXME: is this needed? it breaks sometimes
+// theora_decode_comments(avctx, gb);
+ break;
+ case 0x82:
+ theora_decode_tables(avctx, &gb);
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
+ break;
+ }
+ if(8*op_bytes != get_bits_count(&gb))
+ av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*op_bytes - get_bits_count(&gb), ptype);
+ if (s->theora < 0x030200)
+ break;
+ }
+
+ vp3_decode_init(avctx);