annotate msmpeg4.c @ 222:4f83fb0c957a libavcodec

workaround gcc bug "m" (0) avoiding useage of ebx
author arpi_esp
date Mon, 28 Jan 2002 19:34:16 +0000
parents fe243b4aec02
children ae145876789d
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);
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
188
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
189 if(s->flipflop_rounding){
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
190 s->no_rounding ^= 1;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
191 }else{
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
192 s->no_rounding = 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
193 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
195
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 if (!init_done) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 /* init various encoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 init_done = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 init_mv_table(&mv_tables[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 init_mv_table(&mv_tables[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 for(i=0;i<NB_RL_TABLES;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 init_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
204
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 intra_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
210
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
211 void msmpeg4_encode_ext_header(MpegEncContext * s)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
212 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
213 if(s->pict_type == P_TYPE)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
214 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
215 return; // P-Frames dont seem to have them and not even a 0 bit
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
216 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
217 else
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
218 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
219 s->flipflop_rounding=1;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
220 s->bitrate= 910;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
221
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
222 put_bits(&s->pb, 1, 1); // ext header indicator
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
223
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
224 put_bits(&s->pb, 4, 7); // ?
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
225
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
226 put_bits(&s->pb, 11, s->bitrate);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
227
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
228 put_bits(&s->pb, 1, s->flipflop_rounding);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
229 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
230 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
231
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 /* predict coded block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 int x, y, wrap, pred, a, b, c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
236
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
240
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 a = s->coded_block[(x - 1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 b = s->coded_block[(x - 1) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 c = s->coded_block[(x) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
247
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 if (b == c) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
253
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 /* store value */
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 *coded_block_ptr = &s->coded_block[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
256
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
259
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 static void msmpeg4_encode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 int mx, int my)
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 /* modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 /* WARNING : you cannot reach all the MVs even with the modulo
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 encoding. This is a somewhat strange compromise they took !!! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
277
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 mx += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 my += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 if ((unsigned)mx >= 64 ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 (unsigned)my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
286
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 code = mv->table_mv_index[(mx << 6) | my];
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 mv->table_mv_bits[code],
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 mv->table_mv_code[code]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 /* escape : code litterally */
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 put_bits(&s->pb, 6, mx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 put_bits(&s->pb, 6, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
298
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 void msmpeg4_encode_mb(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 DCTELEM block[6][64],
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 int motion_x, int motion_y)
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 int cbp, coded_cbp, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 int pred_x, pred_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 UINT8 *coded_block;
986e461dc072 Initial revision
glantau
parents:
diff changeset
306
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 if (s->block_last_index[i] >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 cbp |= 1 << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 /* skip macroblock */
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 return;
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 if (s->use_skip_mb_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
322
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 table_mb_non_intra[cbp + 64][1],
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 table_mb_non_intra[cbp + 64][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
326
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 /* motion vector */
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 h263_pred_motion(s, 0, &pred_x, &pred_y);
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 msmpeg4_encode_motion(s, motion_x - pred_x,
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 motion_y - pred_y);
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 coded_cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 int val, pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 val = (s->block_last_index[i] >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 if (i < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 /* predict value for close blocks only for luma */
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 pred = coded_block_pred(s, i, &coded_block);
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 *coded_block = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 coded_cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 if (coded_cbp)
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 printf("cbp=%x %x\n", cbp, coded_cbp);
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
351
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 if (s->use_skip_mb_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 table_mb_non_intra[cbp][1],
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 table_mb_non_intra[cbp][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
366
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 msmpeg4_encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
371
986e461dc072 Initial revision
glantau
parents:
diff changeset
372
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 /* strongly inspirated from MPEG4, but not exactly the same ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 void msmpeg4_dc_scale(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 {
195
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
376 if (s->qscale < 5){
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
377 s->y_dc_scale = 8;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
378 s->c_dc_scale = 8;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
379 // s->c_dc_scale = (s->qscale + 13)>>1;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
380 }else if (s->qscale < 9){
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
381 s->y_dc_scale = 2 * s->qscale;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
382 s->c_dc_scale = (s->qscale + 13)>>1;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
383 }else{
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
384 s->y_dc_scale = s->qscale + 8;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
385 s->c_dc_scale = (s->qscale + 13)>>1;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
386 }
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
387 // this differs for quant >24 from mpeg4
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
388
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
389 // if(s->qscale==13) s->c_dc_scale=14;
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
390
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
391 // if(s->qscale>=6)
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
392 // printf("%d", s->qscale);
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
393
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
394 /* s->c_dc_scale values (found by Michael Nidermayer)
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
395 qscale=2 -> 8 (yes iam sure about that)
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
396 qscale=3 -> 8
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
397 qscale=4 -> 8
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
398 qscale=5 -> 9
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
399 qscale=6 -> 9
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
400 qscale=7 -> 10
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
401 qscale=8 -> 10
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
402 qscale=9 -> 11
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
403 qscale=10-> 11
92f726205082 s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 186
diff changeset
404 */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
406
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 /* dir = 0: left, dir = 1: top prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 static int msmpeg4_pred_dc(MpegEncContext * s, int n,
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
409 INT16 **dc_val_ptr, int *dir_ptr)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 int a, b, c, x, y, wrap, pred, scale;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
412 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
413
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 /* find prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 dc_val = s->dc_val[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 scale = s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
421 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 x = s->mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 y = s->mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 dc_val = s->dc_val[n - 4 + 1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 scale = s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
428
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 a = dc_val[(x - 1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 b = dc_val[(x - 1) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 c = dc_val[(x) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
435
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 /* XXX: the following solution consumes divisions, but it does not
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 necessitate to modify mpegvideo.c. The problem comes from the
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 fact they decided to store the quantized DC (which would lead
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 to problems if Q could vary !) */
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
440 #if defined ARCH_X86 && !defined PIC
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
441 /* using 16bit divisions as they are large enough and 2x as fast */
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
442 asm volatile(
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
443 "movl %3, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
444 "shrl $1, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
445 "addl %%eax, %2 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
446 "addl %%eax, %1 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
447 "addl %0, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
448 "xorl %%edx, %%edx \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
449 "divw %w3 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
450 "movzwl %%ax, %0 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
451 "movl %1, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
452 "xorl %%edx, %%edx \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
453 "divw %w3 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
454 "movzwl %%ax, %1 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
455 "movl %2, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
456 "xorl %%edx, %%edx \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
457 "divw %w3 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
458 "movzwl %%ax, %2 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
459 : "+r" (a), "+r" (b), "+r" (c)
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
460 : "r" (scale)
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
461 : "%eax", "%edx"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
462 );
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
463 #else
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
464 /* #elif defined (ARCH_ALPHA) */
214
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
465 /* Divisions are extremely costly on Alpha; optimize the most
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
466 common case. But they are costly everywhere...
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
467 */
214
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
468 if (scale == 8) {
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
469 a = (a + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
470 b = (b + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
471 c = (c + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
472 } else {
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
473 a = (a + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
474 b = (b + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
475 c = (c + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
476 }
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
477 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
478 /* XXX: WARNING: they did not choose the same test as MPEG4. This
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 is very important ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 if (abs(a - b) <= abs(b - c)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 *dir_ptr = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 *dir_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
487
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
492
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 #define DC_MAX 119
986e461dc072 Initial revision
glantau
parents:
diff changeset
494
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 int sign, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 int pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
499 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
500
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
502
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
509
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 /* do the prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 level -= pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
512
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
518
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 code = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
520 if (code > DC_MAX)
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 code = DC_MAX;
986e461dc072 Initial revision
glantau
parents:
diff changeset
522
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 if (s->dc_table_index == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
536
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 if (code == DC_MAX)
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 put_bits(&s->pb, 8, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
539
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 if (level != 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
544
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 /* Encoding of a block. Very similar to MPEG4 except for a different
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 escape coding (same as H263) and more vlc tables.
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 int level, run, last, i, j, last_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 int last_non_zero, sign, slevel;
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 int code, run_diff, dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 const RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
554
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 rl = &rl_table[3 + s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 run_diff = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
572
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 last_non_zero = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 for (; i <= last_index; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
578 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 run = i - last_non_zero - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 last = (i == last_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 slevel = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 code = get_rl_index(rl, last, run, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 int level1, run1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
592
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 level1 = level - rl->max_level[last][run];
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 if (level1 < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 goto esc2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 code = get_rl_index(rl, last, run, level1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 esc2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 if (level > MAX_LEVEL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 run1 = run - rl->max_run[last][level] - run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 if (run1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 code = get_rl_index(rl, last, run1, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 esc3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 /* third escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 put_bits(&s->pb, 1, last);
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 put_bits(&s->pb, 6, run);
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 put_bits(&s->pb, 8, slevel & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
632
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 /****************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 /* decoding stuff */
986e461dc072 Initial revision
glantau
parents:
diff changeset
635
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 static VLC mb_non_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 static VLC mb_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 static VLC dc_lum_vlc[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 static VLC dc_chroma_vlc[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
640
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 /* init all vlc decoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 int msmpeg4_decode_init_vlc(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
646
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 for(i=0;i<NB_RL_TABLES;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 init_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 init_vlc_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 for(i=0;i<2;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 mv = &mv_tables[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 init_vlc(&mv->vlc, 9, mv->n + 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 mv->table_mv_bits, 1, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 mv->table_mv_code, 2, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
657
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 init_vlc(&dc_lum_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 &table0_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 &table0_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 init_vlc(&dc_chroma_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 &table0_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 &table0_dc_chroma[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 init_vlc(&dc_lum_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 &table1_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 &table1_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 init_vlc(&dc_chroma_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 &table1_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 &table1_dc_chroma[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
670
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 init_vlc(&mb_non_intra_vlc, 9, 128,
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 &table_mb_non_intra[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 &table_mb_non_intra[0][0], 8, 4);
48
11ce5e672961 fixed mb_intra_vlc decoding table size
glantau
parents: 25
diff changeset
674 init_vlc(&mb_intra_vlc, 9, 64,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 &table_mb_intra[0][1], 4, 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 &table_mb_intra[0][0], 4, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
679
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 static int decode012(GetBitContext *gb)
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 int n;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
683 n = get_bits1(gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 if (n == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 else
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
687 return get_bits1(gb) + 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
688 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
689
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 int msmpeg4_decode_picture_header(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
693
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 s->pict_type = get_bits(&s->gb, 2) + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 if (s->pict_type != I_TYPE &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 s->pict_type != P_TYPE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
698
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 s->qscale = get_bits(&s->gb, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
700
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 code = get_bits(&s->gb, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 /* 0x17: one slice, 0x18: three slices */
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 /* XXX: implement it */
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
705 //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 if (code < 0x17)
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 s->slice_height = s->mb_height / (code - 0x16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 s->rl_chroma_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 s->rl_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
711
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
712 s->dc_table_index = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 s->no_rounding = 1;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
714 /* printf(" %d %d %d %d \n",
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
715 s->qscale,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
716 s->rl_chroma_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
717 s->rl_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
718 s->dc_table_index);*/
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 } else {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
720 s->use_skip_mb_code = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
721
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 s->rl_table_index = decode012(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 s->rl_chroma_table_index = s->rl_table_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
724
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
725 s->dc_table_index = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
726
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
727 s->mv_table_index = get_bits1(&s->gb);
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
728 /* printf(" %d %d %d %d %d \n",
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
729 s->use_skip_mb_code,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
730 s->rl_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
731 s->rl_chroma_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
732 s->dc_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
733 s->mv_table_index);*/
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
734 if(s->flipflop_rounding){
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
735 s->no_rounding ^= 1;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
736 }else{
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
737 s->no_rounding = 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
738 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
739 // printf("%d", s->no_rounding);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 }
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
741
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
742
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
748
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
749 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
750 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
751 int firstBit=0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
752
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
753 /* the alt_bitstream reader could read over the end so we need to check it */
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
754 if(get_bits_count(&s->gb) < buf_size*8) firstBit= get_bits1(&s->gb);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
755
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
756 if(s->pict_type == P_TYPE)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
757 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
758 if(firstBit) return -1; // havnt seen ext headers in P-Frames yet ;)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
759 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
760 else
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
761 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
762 int unk;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
763 if(!firstBit) // no header found
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
764 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
765 s->flipflop_rounding= 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
766 s->bitrate= 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
767 return 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
768 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
769
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
770 unk= get_bits(&s->gb, 4);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
771 s->bitrate= get_bits(&s->gb, 11);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
772
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
773 // printf("%2d %4d ;; %1X\n", unk,s->bitrate, unk);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
774
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
775 s->flipflop_rounding= get_bits1(&s->gb);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
776 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
777
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
778 return 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
779 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
780
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
781 void memsetw(short *tab, int val, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
782 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 for(i=0;i<n;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 tab[i] = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
786 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
787
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 int msmpeg4_decode_mb(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
790 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 int cbp, code, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 int pred, val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 UINT8 *coded_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
794
986e461dc072 Initial revision
glantau
parents:
diff changeset
795 /* special slice handling */
986e461dc072 Initial revision
glantau
parents:
diff changeset
796 if (s->mb_x == 0) {
122
56e34b70ef5d * prevent crash in decoder
kabi
parents: 48
diff changeset
797 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
798 int wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
799 /* reset DC pred (set previous line to 1024) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
800 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
801 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
802 1024, 2 * s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
803 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
804 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
805 1024, s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
806 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
807 1024, s->mb_width);
186
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
808
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
809 /* reset AC pred (set previous line to 0) */
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
810 wrap = s->mb_width * 2 + 2;
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
811 memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
812 0, 2 * s->mb_width*16);
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
813 wrap = s->mb_width + 2;
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
814 memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
815 0, s->mb_width*16);
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
816 memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
817 0, s->mb_width*16);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
818
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 s->first_slice_line = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
820 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
821 s->first_slice_line = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
823 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
824
986e461dc072 Initial revision
glantau
parents:
diff changeset
825 if (s->pict_type == P_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
826 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
827 if (s->use_skip_mb_code) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
828 if (get_bits1(&s->gb)) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 /* skip mb */
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 for(i=0;i<6;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
832 s->block_last_index[i] = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
833 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 s->mv[0][0][0] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 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
837 s->mb_skiped = 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
838 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
839 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
840 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
841
986e461dc072 Initial revision
glantau
parents:
diff changeset
842 code = get_vlc(&s->gb, &mb_non_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
843 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
844 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
845 if (code & 0x40)
986e461dc072 Initial revision
glantau
parents:
diff changeset
846 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
849
986e461dc072 Initial revision
glantau
parents:
diff changeset
850 cbp = code & 0x3f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
851 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
852 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
853 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
854 code = get_vlc(&s->gb, &mb_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
855 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
856 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
857 /* predict coded block pattern */
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
859 for(i=0;i<6;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 val = ((code >> (5 - i)) & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
861 if (i < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
862 pred = coded_block_pred(s, i, &coded_val);
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 *coded_val = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
867 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
869
986e461dc072 Initial revision
glantau
parents:
diff changeset
870 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
871 int mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
872 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
873 h263_pred_motion(s, 0, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
874 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
875 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
876 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
877 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
878 s->mv[0][0][0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
879 s->mv[0][0][1] = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
880 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
881 set_stat(ST_INTRA_MB);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
882 s->ac_pred = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
883 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
884
986e461dc072 Initial revision
glantau
parents:
diff changeset
885 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
886 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
887 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
888 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
889 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
890 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
891
986e461dc072 Initial revision
glantau
parents:
diff changeset
892 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
893 int n, int coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
894 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
895 int code, level, i, j, last, run, run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
896 int dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
897 RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
898 const UINT8 *scan_table;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
899 int qmul, qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
900
986e461dc072 Initial revision
glantau
parents:
diff changeset
901 if (s->mb_intra) {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
902 qmul=1;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
903 qadd=0;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
904
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
905 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
906 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
907 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
908 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 block[0] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
911 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
913 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
914 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
915 }
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
916
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
917 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
918 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
919 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
920 goto not_coded;
986e461dc072 Initial revision
glantau
parents:
diff changeset
921 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
922 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
923 if (dc_pred_dir == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
924 scan_table = ff_alternate_vertical_scan; /* left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
925 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
926 scan_table = ff_alternate_horizontal_scan; /* top */
986e461dc072 Initial revision
glantau
parents:
diff changeset
927 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
928 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
929 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
930 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
931 } else {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
932 qmul = s->qscale << 1;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
933 qadd = (s->qscale - 1) | 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
934 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
935 rl = &rl_table[3 + s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
936 run_diff = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
937 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
938 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
939 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
940 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
941 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
942 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
943 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
944
986e461dc072 Initial revision
glantau
parents:
diff changeset
945 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
946 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
947 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
948 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
949 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
950 /* escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
951 if (get_bits1(&s->gb) == 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
952 if (get_bits1(&s->gb) == 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
953 /* third escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
954 last = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
955 run = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
956 level = get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
957 level = (level << 24) >> 24; /* sign extend */
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
958 if(level>0) level= level * qmul + qadd;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
959 else level= level * qmul - qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
960 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
961 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
962 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
963 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
964 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
965 run = rl->table_run[code];
201
91ed656e7339 (commit by michael)
arpi_esp
parents: 200
diff changeset
966 level = rl->table_level[code];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
967 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
968 run += rl->max_run[last][level] + run_diff;
201
91ed656e7339 (commit by michael)
arpi_esp
parents: 200
diff changeset
969 level= level * qmul + qadd;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
970 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
971 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
972 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
973 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
974 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
975 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
976 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
977 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
978 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
979 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
980 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
981 level += rl->max_level[last][run];
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
982 level= level * qmul + qadd;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
983 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
984 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
985 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
986 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
987 run = rl->table_run[code];
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
988 level = rl->table_level[code] * qmul + qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
989 last = code >= rl->last;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
990 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
991 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
992 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
993 i += run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
994 if (i >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
995 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
996 j = scan_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
997 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
998 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
999 if (last)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1000 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1001 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1002 not_coded:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1003 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1004 mpeg4_pred_ac(s, block, n, dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1005 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1006 i = 64; /* XXX: not optimal */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1007 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1008 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1009 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1010
986e461dc072 Initial revision
glantau
parents:
diff changeset
1011 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1012 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1013
986e461dc072 Initial revision
glantau
parents:
diff changeset
1014 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1015 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1016 int level, pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
1017 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1018
986e461dc072 Initial revision
glantau
parents:
diff changeset
1019 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1020 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1021 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1022 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1023 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1024 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1025 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1026
986e461dc072 Initial revision
glantau
parents:
diff changeset
1027 if (level == DC_MAX) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1028 level = get_bits(&s->gb, 8);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1029 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1030 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1031 } else if (level != 0) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1032 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1033 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1034 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1035
986e461dc072 Initial revision
glantau
parents:
diff changeset
1036 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1037 level += pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1040 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1042 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1043 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1044 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1045
986e461dc072 Initial revision
glantau
parents:
diff changeset
1046 return level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1047 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1048
986e461dc072 Initial revision
glantau
parents:
diff changeset
1049 static int msmpeg4_decode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1050 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1051 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1052 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1053 int code, mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1054
986e461dc072 Initial revision
glantau
parents:
diff changeset
1055 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1056
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 code = get_vlc(&s->gb, &mv->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1059 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1060 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1061 mx = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 my = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064 mx = mv->table_mvx[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1065 my = mv->table_mvy[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1066 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1067
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 mx += *mx_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 my += *my_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1070 /* WARNING : they do not do exactly modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1071 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1072 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1073 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1075
986e461dc072 Initial revision
glantau
parents:
diff changeset
1076 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1077 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1078 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1079 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1081 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1082 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1083 }