annotate msmpeg4.c @ 48:11ce5e672961 libavcodec

fixed mb_intra_vlc decoding table size
author glantau
date Sat, 11 Aug 2001 18:56:40 +0000
parents 2f603eb2f23d
children 56e34b70ef5d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * MSMPEG4 backend for ffmpeg encoder and decoder
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include "common.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
24
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 * You can also call this codec : MPEG4 with a twist !
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 * TODO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 * - (encoding) select best mv table (two choices)
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 * - (encoding) select best vlc/dc table
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 * - (decoding) handle slice indication
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 //#define DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
34
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 /* motion vector table */
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 typedef struct MVTable {
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 int n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 const UINT16 *table_mv_code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 const UINT8 *table_mv_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 const UINT8 *table_mvx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 const UINT8 *table_mvy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 VLC vlc; /* decoding: vlc */
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 } MVTable;
986e461dc072 Initial revision
glantau
parents:
diff changeset
45
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 int n, int coded);
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 static int msmpeg4_decode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 int *mx_ptr, int *my_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
52
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 int intra_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 int frame_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 /* XXX: move it to mpegvideo.h */
986e461dc072 Initial revision
glantau
parents:
diff changeset
58
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 static int init_done = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
60
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 #include "msmpeg4data.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
62
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 #ifdef STATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
64
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 const char *st_names[ST_NB] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 "unknown",
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 "dc",
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 "intra_ac",
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 "inter_ac",
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 "intra_mb",
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 "inter_mb",
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 "mv",
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
74
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 int st_current_index = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 unsigned int st_bit_counts[ST_NB];
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 unsigned int st_out_bit_counts[ST_NB];
986e461dc072 Initial revision
glantau
parents:
diff changeset
78
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 #define set_stat(var) st_current_index = var;
986e461dc072 Initial revision
glantau
parents:
diff changeset
80
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 void print_stats(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 unsigned int total;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 printf("Input:\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 total = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 for(i=0;i<ST_NB;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 total += st_bit_counts[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 if (total == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 total = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 for(i=0;i<ST_NB;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 st_names[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 (double)st_bit_counts[i] / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 (double)st_bit_counts[i] * 100.0 / total);
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 "total",
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 (double)total / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 100.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
102
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 printf("Output:\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 total = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 for(i=0;i<ST_NB;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 total += st_out_bit_counts[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 if (total == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 total = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 for(i=0;i<ST_NB;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 st_names[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 (double)st_out_bit_counts[i] / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 (double)st_out_bit_counts[i] * 100.0 / total);
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 "total",
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 (double)total / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 100.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
120
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
122
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 #define set_stat(var)
986e461dc072 Initial revision
glantau
parents:
diff changeset
124
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
126
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 /* build the table which associate a (x,y) motion vector to a vlc */
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 static void init_mv_table(MVTable *tab)
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 int i, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
131
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 /* mark all entries as not used */
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 for(i=0;i<4096;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 tab->table_mv_index[i] = tab->n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
136
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 for(i=0;i<tab->n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 x = tab->table_mvx[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 y = tab->table_mvy[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 tab->table_mv_index[(x << 6) | y] = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
143
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 static void code012(PutBitContext *pb, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 if (n == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 put_bits(pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 put_bits(pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 put_bits(pb, 1, (n >= 2));
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
153
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 /* write MSMPEG4 V3 compatible frame header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 align_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
160
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 put_bits(&s->pb, 2, s->pict_type - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
162
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 put_bits(&s->pb, 5, s->qscale);
986e461dc072 Initial revision
glantau
parents:
diff changeset
164
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 s->rl_table_index = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 s->rl_chroma_table_index = 1; /* only for I frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 s->dc_table_index = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 s->mv_table_index = 1; /* only if P frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 s->use_skip_mb_code = 1; /* only if P frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
170
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
986e461dc072 Initial revision
glantau
parents:
diff changeset
173
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 code012(&s->pb, s->rl_chroma_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 code012(&s->pb, s->rl_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
176
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 put_bits(&s->pb, 1, s->dc_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 s->no_rounding = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 put_bits(&s->pb, 1, s->use_skip_mb_code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
181
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 s->rl_chroma_table_index = s->rl_table_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 code012(&s->pb, s->rl_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
184
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 put_bits(&s->pb, 1, s->dc_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
186
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 put_bits(&s->pb, 1, s->mv_table_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 s->no_rounding ^= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
190
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 if (!init_done) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 /* init various encoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 init_done = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 init_mv_table(&mv_tables[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 init_mv_table(&mv_tables[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 for(i=0;i<NB_RL_TABLES;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 init_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
199
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 intra_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
205
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 /* predict coded block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 int x, y, wrap, pred, a, b, c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
210
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
214
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 a = s->coded_block[(x - 1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 b = s->coded_block[(x - 1) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 c = s->coded_block[(x) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
221
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 if (b == c) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 /* store value */
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 *coded_block_ptr = &s->coded_block[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
230
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
233
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 static void msmpeg4_encode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 int mx, int my)
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
239
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 /* modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 /* WARNING : you cannot reach all the MVs even with the modulo
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 encoding. This is a somewhat strange compromise they took !!! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
251
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 mx += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 my += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 if ((unsigned)mx >= 64 ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 (unsigned)my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
260
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 code = mv->table_mv_index[(mx << 6) | my];
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 mv->table_mv_bits[code],
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 mv->table_mv_code[code]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 /* escape : code litterally */
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 put_bits(&s->pb, 6, mx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 put_bits(&s->pb, 6, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
272
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 void msmpeg4_encode_mb(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 DCTELEM block[6][64],
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 int motion_x, int motion_y)
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 int cbp, coded_cbp, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 int pred_x, pred_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 UINT8 *coded_block;
986e461dc072 Initial revision
glantau
parents:
diff changeset
280
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 if (s->block_last_index[i] >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 cbp |= 1 << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 /* skip macroblock */
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 return;
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 if (s->use_skip_mb_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
296
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 table_mb_non_intra[cbp + 64][1],
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 table_mb_non_intra[cbp + 64][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
300
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 /* motion vector */
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 h263_pred_motion(s, 0, &pred_x, &pred_y);
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 msmpeg4_encode_motion(s, motion_x - pred_x,
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 motion_y - pred_y);
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 coded_cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 int val, pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 val = (s->block_last_index[i] >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 if (i < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 /* predict value for close blocks only for luma */
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 pred = coded_block_pred(s, i, &coded_block);
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 *coded_block = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 coded_cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 if (coded_cbp)
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 printf("cbp=%x %x\n", cbp, coded_cbp);
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
325
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 if (s->use_skip_mb_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 table_mb_non_intra[cbp][1],
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 table_mb_non_intra[cbp][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
340
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 msmpeg4_encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
345
986e461dc072 Initial revision
glantau
parents:
diff changeset
346
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 /* strongly inspirated from MPEG4, but not exactly the same ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 void msmpeg4_dc_scale(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 int scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
351
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 if (s->qscale < 5)
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 scale = 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 else if (s->qscale < 9)
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 scale = 2 * s->qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 scale = s->qscale + 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 s->y_dc_scale = scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 s->c_dc_scale = (s->qscale + 13) / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
361
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 /* dir = 0: left, dir = 1: top prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 static int msmpeg4_pred_dc(MpegEncContext * s, int n,
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
364 INT16 **dc_val_ptr, int *dir_ptr)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 int a, b, c, x, y, wrap, pred, scale;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
367 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
368
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 /* find prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 dc_val = s->dc_val[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 scale = s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 x = s->mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 y = s->mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 dc_val = s->dc_val[n - 4 + 1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 scale = s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
383
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 a = dc_val[(x - 1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 b = dc_val[(x - 1) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 c = dc_val[(x) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
390
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 /* XXX: the following solution consumes divisions, but it does not
986e461dc072 Initial revision
glantau
parents:
diff changeset
392 necessitate to modify mpegvideo.c. The problem comes from the
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 fact they decided to store the quantized DC (which would lead
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 to problems if Q could vary !) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 a = (a + (scale >> 1)) / scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 b = (b + (scale >> 1)) / scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 c = (c + (scale >> 1)) / scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
398
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 /* XXX: WARNING: they did not choose the same test as MPEG4. This
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 is very important ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 if (abs(a - b) <= abs(b - c)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 *dir_ptr = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 *dir_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
408
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
413
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 #define DC_MAX 119
986e461dc072 Initial revision
glantau
parents:
diff changeset
415
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 int sign, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 int pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
420 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
421
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
423
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
430
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 /* do the prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 level -= pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
433
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
439
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 code = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 if (code > DC_MAX)
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 code = DC_MAX;
986e461dc072 Initial revision
glantau
parents:
diff changeset
443
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 if (s->dc_table_index == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
449 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
456 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
457
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 if (code == DC_MAX)
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 put_bits(&s->pb, 8, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
460
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 if (level != 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
465
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 /* Encoding of a block. Very similar to MPEG4 except for a different
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 escape coding (same as H263) and more vlc tables.
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 int level, run, last, i, j, last_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
472 int last_non_zero, sign, slevel;
986e461dc072 Initial revision
glantau
parents:
diff changeset
473 int code, run_diff, dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
474 const RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
475
986e461dc072 Initial revision
glantau
parents:
diff changeset
476 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
477 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
478 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 rl = &rl_table[3 + s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 run_diff = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
493
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 last_non_zero = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 for (; i <= last_index; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
500 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 run = i - last_non_zero - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 last = (i == last_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 slevel = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 code = get_rl_index(rl, last, run, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 int level1, run1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
513
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 level1 = level - rl->max_level[last][run];
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 if (level1 < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 goto esc2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 code = get_rl_index(rl, last, run, level1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 esc2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
520 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 if (level > MAX_LEVEL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 run1 = run - rl->max_run[last][level] - run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 if (run1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 code = get_rl_index(rl, last, run1, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 esc3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 /* third escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 put_bits(&s->pb, 1, last);
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 put_bits(&s->pb, 6, run);
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 put_bits(&s->pb, 8, slevel & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
553
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 /****************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 /* decoding stuff */
986e461dc072 Initial revision
glantau
parents:
diff changeset
556
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 static VLC mb_non_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 static VLC mb_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 static VLC dc_lum_vlc[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 static VLC dc_chroma_vlc[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
561
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 /* init all vlc decoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 int msmpeg4_decode_init_vlc(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
567
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 for(i=0;i<NB_RL_TABLES;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 init_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 init_vlc_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 for(i=0;i<2;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 mv = &mv_tables[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 init_vlc(&mv->vlc, 9, mv->n + 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 mv->table_mv_bits, 1, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 mv->table_mv_code, 2, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
578
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 init_vlc(&dc_lum_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 &table0_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 &table0_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 init_vlc(&dc_chroma_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 &table0_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 &table0_dc_chroma[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 init_vlc(&dc_lum_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 &table1_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 &table1_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 init_vlc(&dc_chroma_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 &table1_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 &table1_dc_chroma[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
591
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 init_vlc(&mb_non_intra_vlc, 9, 128,
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 &table_mb_non_intra[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 &table_mb_non_intra[0][0], 8, 4);
48
11ce5e672961 fixed mb_intra_vlc decoding table size
glantau
parents: 25
diff changeset
595 init_vlc(&mb_intra_vlc, 9, 64,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 &table_mb_intra[0][1], 4, 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 &table_mb_intra[0][0], 4, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
600
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 static int decode012(GetBitContext *gb)
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 int n;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
604 n = get_bits1(gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 if (n == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 else
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
608 return get_bits1(gb) + 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
610
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 int msmpeg4_decode_picture_header(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
614
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 s->pict_type = get_bits(&s->gb, 2) + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 if (s->pict_type != I_TYPE &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 s->pict_type != P_TYPE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
619
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 s->qscale = get_bits(&s->gb, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
621
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 code = get_bits(&s->gb, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 /* 0x17: one slice, 0x18: three slices */
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 /* XXX: implement it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 if (code < 0x17)
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 s->slice_height = s->mb_height / (code - 0x16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 s->rl_chroma_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 s->rl_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
631
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
632 s->dc_table_index = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 s->no_rounding = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 } else {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
635 s->use_skip_mb_code = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
636
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 s->rl_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 s->rl_chroma_table_index = s->rl_table_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
639
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
640 s->dc_table_index = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
641
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
642 s->mv_table_index = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 s->no_rounding ^= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
650
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 void memsetw(short *tab, int val, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 for(i=0;i<n;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 tab[i] = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
657
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 int msmpeg4_decode_mb(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 int cbp, code, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 int pred, val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 UINT8 *coded_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
664
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 /* special slice handling */
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 if (s->mb_x == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 if ((s->mb_y % s->slice_height) == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 int wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 /* reset DC pred (set previous line to 1024) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 1024, 2 * s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 1024, s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 1024, s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
678
986e461dc072 Initial revision
glantau
parents:
diff changeset
679 s->first_slice_line = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 s->first_slice_line = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
684
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 if (s->pict_type == P_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
687 if (s->use_skip_mb_code) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
688 if (get_bits1(&s->gb)) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 /* skip mb */
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 for(i=0;i<6;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 s->block_last_index[i] = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 s->mv[0][0][0] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 s->mv[0][0][1] = 0;
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
697 s->mb_skiped = 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
700 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
701
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 code = get_vlc(&s->gb, &mb_non_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 if (code & 0x40)
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
709
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 cbp = code & 0x3f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
711 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
712 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
714 code = get_vlc(&s->gb, &mb_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
715 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
716 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 /* predict coded block pattern */
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 for(i=0;i<6;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
720 val = ((code >> (5 - i)) & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
721 if (i < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 pred = coded_block_pred(s, i, &coded_val);
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
724 *coded_val = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
729
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 int mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 h263_pred_motion(s, 0, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 s->mv[0][0][0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 s->mv[0][0][1] = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 set_stat(ST_INTRA_MB);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
742 s->ac_pred = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
744
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
751
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 int n, int coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 int code, level, i, j, last, run, run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
756 int dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 const UINT8 *scan_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
759
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
762 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
763 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 block[0] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
767 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
769 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 goto not_coded;
986e461dc072 Initial revision
glantau
parents:
diff changeset
776 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
777 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
778 if (dc_pred_dir == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
779 scan_table = ff_alternate_vertical_scan; /* left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
780 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
781 scan_table = ff_alternate_horizontal_scan; /* top */
986e461dc072 Initial revision
glantau
parents:
diff changeset
782 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
786 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
787 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 rl = &rl_table[3 + s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 run_diff = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
790 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
794 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
795 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
796 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
797
986e461dc072 Initial revision
glantau
parents:
diff changeset
798 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
799 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
800 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
801 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
802 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
803 /* escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
804 if (get_bits1(&s->gb) == 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
805 if (get_bits1(&s->gb) == 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
806 /* third escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
807 last = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
808 run = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
809 level = get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
810 level = (level << 24) >> 24; /* sign extend */
986e461dc072 Initial revision
glantau
parents:
diff changeset
811 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
812 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
813 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
814 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
815 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
816 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
817 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
818 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 run += rl->max_run[last][level] + run_diff;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
820 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
821 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
823 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
824 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
825 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
826 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
827 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
828 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 level += rl->max_level[last][run];
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
832 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
833 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
837 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
838 last = code >= rl->last;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
839 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
840 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
841 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
842 i += run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
843 if (i >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
844 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
845 j = scan_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
846 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 if (last)
986e461dc072 Initial revision
glantau
parents:
diff changeset
849 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
850 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
851 not_coded:
986e461dc072 Initial revision
glantau
parents:
diff changeset
852 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
853 mpeg4_pred_ac(s, block, n, dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
854 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
855 i = 64; /* XXX: not optimal */
986e461dc072 Initial revision
glantau
parents:
diff changeset
856 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
857 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
859
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
861 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
862
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 int level, pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
866 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
867
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
869 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
870 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
871 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
872 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
873 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
874 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
875
986e461dc072 Initial revision
glantau
parents:
diff changeset
876 if (level == DC_MAX) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
877 level = get_bits(&s->gb, 8);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
878 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
879 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
880 } else if (level != 0) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
881 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
882 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
883 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
884
986e461dc072 Initial revision
glantau
parents:
diff changeset
885 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
886 level += pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
887
986e461dc072 Initial revision
glantau
parents:
diff changeset
888 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
889 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
890 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
891 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
892 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
893 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
894
986e461dc072 Initial revision
glantau
parents:
diff changeset
895 return level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
896 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
897
986e461dc072 Initial revision
glantau
parents:
diff changeset
898 static int msmpeg4_decode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
899 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
900 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
901 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
902 int code, mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
903
986e461dc072 Initial revision
glantau
parents:
diff changeset
904 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
905
986e461dc072 Initial revision
glantau
parents:
diff changeset
906 code = get_vlc(&s->gb, &mv->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
907 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
908 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 mx = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
911 my = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
913 mx = mv->table_mvx[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
914 my = mv->table_mvy[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
915 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
916
986e461dc072 Initial revision
glantau
parents:
diff changeset
917 mx += *mx_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
918 my += *my_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
919 /* WARNING : they do not do exactly modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
920 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
921 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
922 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
923 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
924
986e461dc072 Initial revision
glantau
parents:
diff changeset
925 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
926 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
927 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
928 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
929 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
930 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
931 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
932 }