annotate msmpeg4.c @ 422:aa4a1c6209bd libavcodec

* baling 8 seems to have the same speed
author kabi
date Thu, 23 May 2002 16:19:07 +0000
parents 5c8b3a717929
children 718a22dc121f
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.
411
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
18 *
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
19 * msmpeg4v2 stuff by Michael Niedermayer <michaelni@gmx.at>
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 */
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 347
diff changeset
21 #include "avcodec.h"
0
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
411
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
25
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 * You can also call this codec : MPEG4 with a twist !
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 * TODO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 * - (encoding) select best mv table (two choices)
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 * - (encoding) select best vlc/dc table
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
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
46 static UINT32 v2_dc_lum_table[512][2];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
47 static UINT32 v2_dc_chroma_table[512][2];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
48
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 int n, int coded);
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 static int msmpeg4_decode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 int *mx_ptr, int *my_ptr);
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
55 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
56 static void init_h263_dc_for_msmpeg4();
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
57
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
58
225
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
59 extern UINT32 inverse[256];
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
60
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 int intra_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 int frame_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 /* XXX: move it to mpegvideo.h */
986e461dc072 Initial revision
glantau
parents:
diff changeset
66
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 static int init_done = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
68
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 #include "msmpeg4data.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 #ifdef STATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
72
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 const char *st_names[ST_NB] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 "unknown",
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 "dc",
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 "intra_ac",
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 "inter_ac",
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 "intra_mb",
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 "inter_mb",
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 "mv",
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
82
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 int st_current_index = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 unsigned int st_bit_counts[ST_NB];
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 unsigned int st_out_bit_counts[ST_NB];
986e461dc072 Initial revision
glantau
parents:
diff changeset
86
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 #define set_stat(var) st_current_index = var;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 void print_stats(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 unsigned int total;
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 printf("Input:\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 total = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 for(i=0;i<ST_NB;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 total += st_bit_counts[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 if (total == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 total = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 for(i=0;i<ST_NB;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 st_names[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 (double)st_bit_counts[i] / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 (double)st_bit_counts[i] * 100.0 / total);
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 "total",
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 (double)total / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 100.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
110
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 printf("Output:\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 total = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 for(i=0;i<ST_NB;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 total += st_out_bit_counts[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 if (total == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 total = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 for(i=0;i<ST_NB;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 st_names[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 (double)st_out_bit_counts[i] / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 (double)st_out_bit_counts[i] * 100.0 / total);
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 printf("%-10s : %10.1f %5.1f%%\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 "total",
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 (double)total / 8.0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 100.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
128
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
130
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 #define set_stat(var)
986e461dc072 Initial revision
glantau
parents:
diff changeset
132
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
134
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 /* build the table which associate a (x,y) motion vector to a vlc */
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 static void init_mv_table(MVTable *tab)
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 int i, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 347
diff changeset
140 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 /* mark all entries as not used */
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 for(i=0;i<4096;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 tab->table_mv_index[i] = tab->n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
144
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 for(i=0;i<tab->n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 x = tab->table_mvx[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 y = tab->table_mvy[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 tab->table_mv_index[(x << 6) | y] = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
151
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 static void code012(PutBitContext *pb, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 if (n == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 put_bits(pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 put_bits(pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 put_bits(pb, 1, (n >= 2));
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
161
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 /* write MSMPEG4 V3 compatible frame header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 align_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
168
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 put_bits(&s->pb, 2, s->pict_type - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
170
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 put_bits(&s->pb, 5, s->qscale);
986e461dc072 Initial revision
glantau
parents:
diff changeset
172
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 s->rl_table_index = 2;
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
174 if(s->msmpeg4_version==2)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
175 s->rl_chroma_table_index = 2; /* only for I frame */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
176 else
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
177 s->rl_chroma_table_index = 1; /* only for I frame */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
178
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 s->dc_table_index = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 s->mv_table_index = 1; /* only if P frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 s->use_skip_mb_code = 1; /* only if P frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
986e461dc072 Initial revision
glantau
parents:
diff changeset
185
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
186 if(s->msmpeg4_version!=2){
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
187 code012(&s->pb, s->rl_chroma_table_index);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
188 code012(&s->pb, s->rl_table_index);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
189
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
190 put_bits(&s->pb, 1, s->dc_table_index);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
191 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 s->no_rounding = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 put_bits(&s->pb, 1, s->use_skip_mb_code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
195
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 s->rl_chroma_table_index = s->rl_table_index;
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
197 if(s->msmpeg4_version!=2){
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
198 code012(&s->pb, s->rl_table_index);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
199
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
200 put_bits(&s->pb, 1, s->dc_table_index);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
201
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
202 put_bits(&s->pb, 1, s->mv_table_index);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
203 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
204
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
205 if(s->flipflop_rounding){
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
206 s->no_rounding ^= 1;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
207 }else{
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
208 s->no_rounding = 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
209 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
211
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 if (!init_done) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 /* init various encoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 init_done = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 init_mv_table(&mv_tables[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 init_mv_table(&mv_tables[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 for(i=0;i<NB_RL_TABLES;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 init_rl(&rl_table[i]);
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
219
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
220 init_h263_dc_for_msmpeg4();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
222
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 intra_count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
228
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
229 void msmpeg4_encode_ext_header(MpegEncContext * s)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
230 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
231 s->flipflop_rounding=1;
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
232 s->bitrate= 910; // FIXME
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
233
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
234 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
235
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
236 put_bits(&s->pb, 11, s->bitrate);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
237
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
238 put_bits(&s->pb, 1, s->flipflop_rounding);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
239 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
240
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 /* predict coded block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 {
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
244 int xy, wrap, pred, a, b, c;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
245
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
246 xy = s->block_index[n];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
247 wrap = s->block_wrap[0];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
248
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 */
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
252 a = s->coded_block[xy - 1 ];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
253 b = s->coded_block[xy - 1 - wrap];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
254 c = s->coded_block[xy - wrap];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
255
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 if (b == c) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
261
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 /* store value */
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
263 *coded_block_ptr = &s->coded_block[xy];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
264
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
267
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 static void msmpeg4_encode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 int mx, int my)
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 /* modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 /* WARNING : you cannot reach all the MVs even with the modulo
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 encoding. This is a somewhat strange compromise they took !!! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
285
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 mx += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 my += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 if ((unsigned)mx >= 64 ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 (unsigned)my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
294
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 code = mv->table_mv_index[(mx << 6) | my];
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 mv->table_mv_bits[code],
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 mv->table_mv_code[code]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 /* escape : code litterally */
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 put_bits(&s->pb, 6, mx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 put_bits(&s->pb, 6, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
306
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 void msmpeg4_encode_mb(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 DCTELEM block[6][64],
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 int motion_x, int motion_y)
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 int cbp, coded_cbp, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 int pred_x, pred_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 UINT8 *coded_block;
986e461dc072 Initial revision
glantau
parents:
diff changeset
314
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 if (s->block_last_index[i] >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 cbp |= 1 << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 /* skip macroblock */
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 return;
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 if (s->use_skip_mb_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
330
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
331 if(s->msmpeg4_version==2){
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
332 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
333 v2_mb_type[cbp&3][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
334 v2_mb_type[cbp&3][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
335 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
336 else coded_cbp= cbp;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
337
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
338 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
339 cbpy_tab[coded_cbp>>2][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
340 cbpy_tab[coded_cbp>>2][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
341
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
342 h263_pred_motion(s, 0, &pred_x, &pred_y);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
343 msmpeg4v2_encode_motion(s, motion_x - pred_x);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
344 msmpeg4v2_encode_motion(s, motion_y - pred_y);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
345 }else{
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
346 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
347 table_mb_non_intra[cbp + 64][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
348 table_mb_non_intra[cbp + 64][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
349
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
350 /* motion vector */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
351 h263_pred_motion(s, 0, &pred_x, &pred_y);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
352 msmpeg4_encode_motion(s, motion_x - pred_x,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
353 motion_y - pred_y);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
354 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 coded_cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 int val, pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 val = (s->block_last_index[i] >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 if (i < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 /* predict value for close blocks only for luma */
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 pred = coded_block_pred(s, i, &coded_block);
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 *coded_block = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 coded_cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 if (coded_cbp)
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 printf("cbp=%x %x\n", cbp, coded_cbp);
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
375
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
376 if(s->msmpeg4_version==2){
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
377 if (s->pict_type == I_TYPE) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
378 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
379 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
380 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
381 if (s->use_skip_mb_code)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
382 put_bits(&s->pb, 1, 0); /* mb coded */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
383 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
384 v2_mb_type[(cbp&3) + 4][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
385 v2_mb_type[(cbp&3) + 4][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
386 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
387 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 put_bits(&s->pb,
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
389 cbpy_tab[cbp>>2][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
390 cbpy_tab[cbp>>2][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
391 }else{
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
392 if (s->pict_type == I_TYPE) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
393 set_stat(ST_INTRA_MB);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
394 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
395 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
396 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
397 if (s->use_skip_mb_code)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
398 put_bits(&s->pb, 1, 0); /* mb coded */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
399 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
400 table_mb_non_intra[cbp][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
401 table_mb_non_intra[cbp][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
402 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
403 set_stat(ST_INTRA_MB);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
404 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
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
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 msmpeg4_encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
412
411
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
413 /* old ffmpeg msmpeg4v3 mode */
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
414 void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 {
411
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
416 if (s->qscale < 5){
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
417 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
418 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
419 }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
420 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
421 s->c_dc_scale = (s->qscale + 13)>>1;
411
5c8b3a717929 workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents: 396
diff changeset
422 }else{
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
423 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
424 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
425 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
427
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 /* dir = 0: left, dir = 1: top prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 static int msmpeg4_pred_dc(MpegEncContext * s, int n,
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
430 INT16 **dc_val_ptr, int *dir_ptr)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 {
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
432 int a, b, c, wrap, pred, scale;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
433 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
434
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 /* find prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 scale = s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 scale = s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 }
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
441 wrap = s->block_wrap[n];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
442 dc_val= s->dc_val[0] + s->block_index[n];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
443
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 */
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
447 a = dc_val[ - 1];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
448 b = dc_val[ - 1 - wrap];
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
449 c = dc_val[ - wrap];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
450
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 /* XXX: the following solution consumes divisions, but it does not
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 necessitate to modify mpegvideo.c. The problem comes from the
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 fact they decided to store the quantized DC (which would lead
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 to problems if Q could vary !) */
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
455 #if defined ARCH_X86 && !defined PIC
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
456 asm volatile(
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
457 "movl %3, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
458 "shrl $1, %%eax \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
459 "addl %%eax, %2 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
460 "addl %%eax, %1 \n\t"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
461 "addl %0, %%eax \n\t"
225
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
462 "mull %4 \n\t"
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
463 "movl %%edx, %0 \n\t"
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
464 "movl %1, %%eax \n\t"
225
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
465 "mull %4 \n\t"
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
466 "movl %%edx, %1 \n\t"
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
467 "movl %2, %%eax \n\t"
225
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
468 "mull %4 \n\t"
ae145876789d use multiply instead of divides for DC prediction on X86
michaelni
parents: 221
diff changeset
469 "movl %%edx, %2 \n\t"
228
3c2bad70a196 workaround gcc 2.95.2 bug
michaelni
parents: 225
diff changeset
470 : "+b" (a), "+c" (b), "+D" (c)
3c2bad70a196 workaround gcc 2.95.2 bug
michaelni
parents: 225
diff changeset
471 : "g" (scale), "S" (inverse[scale])
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
472 : "%eax", "%edx"
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
473 );
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
474 #else
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
475 /* #elif defined (ARCH_ALPHA) */
214
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
476 /* Divisions are extremely costly on Alpha; optimize the most
221
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
477 common case. But they are costly everywhere...
fe243b4aec02 * temporal solution for shared lib compilation
kabi
parents: 214
diff changeset
478 */
214
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
479 if (scale == 8) {
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
480 a = (a + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
481 b = (b + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
482 c = (c + (8 >> 1)) / 8;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
483 } else {
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
484 a = (a + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
485 b = (b + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
486 c = (c + (scale >> 1)) / scale;
73df666cacc7 Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents: 208
diff changeset
487 }
204
fceb435fae6b (commit by michael)
arpi_esp
parents: 201
diff changeset
488 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 /* XXX: WARNING: they did not choose the same test as MPEG4. This
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 is very important ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 if (abs(a - b) <= abs(b - c)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 *dir_ptr = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 *dir_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
498
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 /* update predictor */
299
fb9e77674bd4 minor optimizations / simplifications
michaelni
parents: 251
diff changeset
500 *dc_val_ptr = &dc_val[0];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
503
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 #define DC_MAX 119
986e461dc072 Initial revision
glantau
parents:
diff changeset
505
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 int sign, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 int pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
510 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
511
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
513
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
520
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 /* do the prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 level -= pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
523
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
524 if(s->msmpeg4_version==2){
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
525 if (n < 4) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
526 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
527 v2_dc_lum_table[level+256][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
528 v2_dc_lum_table[level+256][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
529 }else{
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
530 put_bits(&s->pb,
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
531 v2_dc_chroma_table[level+256][1],
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
532 v2_dc_chroma_table[level+256][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
533 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
534 }else{
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
535 sign = 0;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
536 if (level < 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
537 level = -level;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
538 sign = 1;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
539 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
540 code = level;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
541 if (code > DC_MAX)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
542 code = DC_MAX;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
543
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
544 if (s->dc_table_index == 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
545 if (n < 4) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
546 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
547 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
548 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
549 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 } else {
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
551 if (n < 4) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
552 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
553 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
554 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
555 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 }
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
557
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
558 if (code == DC_MAX)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
559 put_bits(&s->pb, 8, level);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
560
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
561 if (level != 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
562 put_bits(&s->pb, 1, sign);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
563 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
566
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 /* Encoding of a block. Very similar to MPEG4 except for a different
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 escape coding (same as H263) and more vlc tables.
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 int level, run, last, i, j, last_index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 int last_non_zero, sign, slevel;
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 int code, run_diff, dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 const RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
576
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
578 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 rl = &rl_table[3 + s->rl_table_index];
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
591 if(s->msmpeg4_version==2)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
592 run_diff = 0;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
593 else
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
594 run_diff = 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
597
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 last_non_zero = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 for (; i <= last_index; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 run = i - last_non_zero - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 last = (i == last_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 slevel = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 code = get_rl_index(rl, last, run, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 int level1, run1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
617
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 level1 = level - rl->max_level[last][run];
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 if (level1 < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 goto esc2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 code = get_rl_index(rl, last, run, level1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 esc2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 if (level > MAX_LEVEL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 run1 = run - rl->max_run[last][level] - run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 if (run1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 code = get_rl_index(rl, last, run1, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 esc3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 /* third escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 put_bits(&s->pb, 1, last);
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 put_bits(&s->pb, 6, run);
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 put_bits(&s->pb, 8, slevel & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
657
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 /****************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 /* decoding stuff */
986e461dc072 Initial revision
glantau
parents:
diff changeset
660
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 static VLC mb_non_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 static VLC mb_intra_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 static VLC dc_lum_vlc[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 static VLC dc_chroma_vlc[2];
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
665 static VLC v2_dc_lum_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
666 static VLC v2_dc_chroma_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
667 static VLC cbpy_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
668 static VLC v2_intra_cbpc_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
669 static VLC v2_mb_type_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
670 static VLC v2_mv_vlc;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
671
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
672 /* this table is practically identical to the one from h263 except that its inverted */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
673 static void init_h263_dc_for_msmpeg4()
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
674 {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
675 static int inited=0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
676
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
677 if(!inited){
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
678 int level, uni_code, uni_len;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
679 inited=1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
680
309
32485ad4cc4e fixing msmpeg4v2 bugs (is bugfree now afaik)
michaelni
parents: 307
diff changeset
681 for(level=-256; level<256; level++){
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
682 int size, v, l;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
683 /* find number of bits */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
684 size = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
685 v = abs(level);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
686 while (v) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
687 v >>= 1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
688 size++;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
689 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
690
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
691 if (level < 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
692 l= (-level) ^ ((1 << size) - 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
693 else
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
694 l= level;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
695
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
696 /* luminance h263 */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
697 uni_code= DCtab_lum[size][0];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
698 uni_len = DCtab_lum[size][1];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
699 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
700
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
701 if (size > 0) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
702 uni_code<<=size; uni_code|=l;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
703 uni_len+=size;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
704 if (size > 8){
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
705 uni_code<<=1; uni_code|=1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
706 uni_len++;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
707 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
708 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
709 v2_dc_lum_table[level+256][0]= uni_code;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
710 v2_dc_lum_table[level+256][1]= uni_len;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
711
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
712 /* chrominance h263 */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
713 uni_code= DCtab_chrom[size][0];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
714 uni_len = DCtab_chrom[size][1];
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
715 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
716
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
717 if (size > 0) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
718 uni_code<<=size; uni_code|=l;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
719 uni_len+=size;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
720 if (size > 8){
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
721 uni_code<<=1; uni_code|=1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
722 uni_len++;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
723 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
724 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
725 v2_dc_chroma_table[level+256][0]= uni_code;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
726 v2_dc_chroma_table[level+256][1]= uni_len;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
727
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
728 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
729 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
730 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
731
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 /* init all vlc decoding tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 int msmpeg4_decode_init_vlc(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
737
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 for(i=0;i<NB_RL_TABLES;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 init_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 init_vlc_rl(&rl_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
742 for(i=0;i<2;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 mv = &mv_tables[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 init_vlc(&mv->vlc, 9, mv->n + 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 mv->table_mv_bits, 1, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 mv->table_mv_code, 2, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
748
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 init_vlc(&dc_lum_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 &table0_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 &table0_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 init_vlc(&dc_chroma_vlc[0], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 &table0_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 &table0_dc_chroma[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 init_vlc(&dc_lum_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
756 &table1_dc_lum[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 &table1_dc_lum[0][0], 8, 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 init_vlc(&dc_chroma_vlc[1], 9, 120,
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 &table1_dc_chroma[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 &table1_dc_chroma[0][0], 8, 4);
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
761
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
762 init_h263_dc_for_msmpeg4();
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
763 init_vlc(&v2_dc_lum_vlc, 9, 512,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
764 &v2_dc_lum_table[0][1], 8, 4,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
765 &v2_dc_lum_table[0][0], 8, 4);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
766 init_vlc(&v2_dc_chroma_vlc, 9, 512,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
767 &v2_dc_chroma_table[0][1], 8, 4,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
768 &v2_dc_chroma_table[0][0], 8, 4);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
769
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
770 init_vlc(&cbpy_vlc, 6, 16,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
771 &cbpy_tab[0][1], 2, 1,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
772 &cbpy_tab[0][0], 2, 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
773 init_vlc(&v2_intra_cbpc_vlc, 3, 4,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
774 &v2_intra_cbpc[0][1], 2, 1,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
775 &v2_intra_cbpc[0][0], 2, 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
776 init_vlc(&v2_mb_type_vlc, 5, 8,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
777 &v2_mb_type[0][1], 2, 1,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
778 &v2_mb_type[0][0], 2, 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
779 init_vlc(&v2_mv_vlc, 9, 33,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
780 &mvtab[0][1], 2, 1,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
781 &mvtab[0][0], 2, 1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
782
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 init_vlc(&mb_non_intra_vlc, 9, 128,
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 &table_mb_non_intra[0][1], 8, 4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 &table_mb_non_intra[0][0], 8, 4);
48
11ce5e672961 fixed mb_intra_vlc decoding table size
glantau
parents: 25
diff changeset
786 init_vlc(&mb_intra_vlc, 9, 64,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
787 &table_mb_intra[0][1], 4, 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 &table_mb_intra[0][0], 4, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
790 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
791
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 static int decode012(GetBitContext *gb)
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
794 int n;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
795 n = get_bits1(gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
796 if (n == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
797 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
798 else
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
799 return get_bits1(gb) + 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
800 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
801
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
802 int msmpeg4_decode_picture_header(MpegEncContext * s)
300
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
803 {
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
804 int code, code2;
300
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
805
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
806 #if 0
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
807 {
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
808 int i;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
809 for(i=0; i<s->gb.size*8; i++)
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
810 printf("%d", get_bits1(&s->gb));
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
811 // get_bits1(&s->gb);
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
812 printf("END\n");
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
813 return -1;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
814 }
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
815 #endif
300
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
816 s->pict_type = get_bits(&s->gb, 2) + 1;
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
817 if (s->pict_type != I_TYPE &&
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
818 s->pict_type != P_TYPE)
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
819 return -1;
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
820
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
821 s->qscale = get_bits(&s->gb, 5);
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
822
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
823 if (s->pict_type == I_TYPE) {
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
824 code = get_bits(&s->gb, 5);
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
825 /* 0x17: one slice, 0x18: two slices */
300
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
826 if (code < 0x17)
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
827 return -1;
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
828 s->slice_height = s->mb_height / (code - 0x16);
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
829
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
830 switch(s->msmpeg4_version){
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
831 case 2:
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
832 s->rl_chroma_table_index = 2;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
833 s->rl_table_index = 2;
300
d874359e58f1 msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents: 299
diff changeset
834
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
835 s->dc_table_index = 0; //not used
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
836 break;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
837 case 3:
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
838 s->rl_chroma_table_index = decode012(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
839 s->rl_table_index = decode012(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
840
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
841 s->dc_table_index = get_bits1(&s->gb);
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
842 break;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
843 case 4:
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
844 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */);
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
845 printf("%X\n", show_bits(&s->gb, 24));
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
846 code= get_bits(&s->gb, 2);
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
847 if(code==1){
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
848 code2= get_bits(&s->gb, 3);
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
849 if(code2==7) skip_bits(&s->gb, 1);
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
850 }
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
851 printf("%X\n", show_bits(&s->gb, 24));
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
852 s->rl_chroma_table_index = 2;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
853 s->rl_table_index = 2;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
854
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
855 s->dc_table_index = 0;
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
856 break;
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
857 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 s->no_rounding = 1;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
859 /* printf(" %d %d %d %d \n",
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
860 s->qscale,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
861 s->rl_chroma_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
862 s->rl_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
863 s->dc_table_index);*/
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 } else {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
865 s->use_skip_mb_code = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
866
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
867 if(s->msmpeg4_version==2){
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
868 s->rl_table_index = 2;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
869 s->rl_chroma_table_index = s->rl_table_index;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
870
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
871 s->dc_table_index = 0; //not used
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
872
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
873 s->mv_table_index = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
874 }else{
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
875 s->rl_table_index = decode012(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
876 s->rl_chroma_table_index = s->rl_table_index;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
877
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
878 s->dc_table_index = get_bits1(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
879
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
880 s->mv_table_index = get_bits1(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
881 }
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
882 /* printf(" %d %d %d %d %d \n",
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
883 s->use_skip_mb_code,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
884 s->rl_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
885 s->rl_chroma_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
886 s->dc_table_index,
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
887 s->mv_table_index);*/
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
888 if(s->flipflop_rounding){
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
889 s->no_rounding ^= 1;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
890 }else{
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
891 s->no_rounding = 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
892 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
893 // printf("%d", s->no_rounding);
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
894 //return -1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
895 }
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
896
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
897 #if 0
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
898 if(s->msmpeg4_version==2)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
899 {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
900 int i;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
901 for(i=0; i<s->gb.size*8; i++)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
902 // printf("%d", get_bits1(&s->gb));
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
903 get_bits1(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
904 printf("END\n");
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
905 return -1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
906 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
907 #endif
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
908
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 printf("*****frame %d:\n", frame_count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
911 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
913 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
914
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
915 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
916 {
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
917 /* the alt_bitstream reader could read over the end so we need to check it */
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
918 if(get_bits_count(&s->gb) + 16 < buf_size*8)
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
919 {
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
920 int fps;
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
921
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
922 fps= get_bits(&s->gb, 5);
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
923 s->bitrate= get_bits(&s->gb, 11);
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
924 s->flipflop_rounding= get_bits1(&s->gb);
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
925
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
926 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
927 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
928 else
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
929 {
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
930 s->flipflop_rounding= 0;
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
931 s->bitrate= 0;
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
932 }
251
75091bfc577b fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents: 246
diff changeset
933
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
934 return 0;
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
935 }
2eb04d6be309 (commit by michael)
arpi_esp
parents: 204
diff changeset
936
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
937 static inline void memsetw(short *tab, int val, int n)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
938 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
939 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
940 for(i=0;i<n;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
941 tab[i] = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
942 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
943
310
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
944 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
945 {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
946 int range, bit_size, sign, code, bits;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
947
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
948 if (val == 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
949 /* zero vector */
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
950 code = 0;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
951 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
952 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
953 bit_size = s->f_code - 1;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
954 range = 1 << bit_size;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
955 if (val <= -64)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
956 val += 64;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
957 else if (val >= 64)
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
958 val -= 64;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
959
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
960 if (val >= 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
961 sign = 0;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
962 } else {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
963 val = -val;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
964 sign = 1;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
965 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
966 val--;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
967 code = (val >> bit_size) + 1;
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
968 bits = val & (range - 1);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
969
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
970 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
971 if (bit_size > 0) {
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
972 put_bits(&s->pb, bit_size, bits);
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
973 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
974 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
975 }
49e73229a5e6 msmpeg4v2 encoding
michaelni
parents: 309
diff changeset
976
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
977 /* this is identical to h263 except that its range is multiplied by 2 */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
978 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
979 {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
980 int code, val, sign, shift;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
981
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
982 code = get_vlc(&s->gb, &v2_mv_vlc);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
983 if (code < 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
984 return 0xffff;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
985
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
986 if (code == 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
987 return pred;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
988 sign = get_bits1(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
989 shift = f_code - 1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
990 val = (code - 1) << shift;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
991 if (shift > 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
992 val |= get_bits(&s->gb, shift);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
993 val++;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
994 if (sign)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
995 val = -val;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
996 val += pred;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
997
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
998 if (val <= -64)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
999 val += 64;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1000 else if (val >= 64)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1001 val -= 64;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1002
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1003 return val;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1004 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1005
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1006
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1007 int msmpeg4v2_decode_mb(MpegEncContext *s,
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1008 DCTELEM block[6][64])
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1009 {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1010 int cbp, code, i;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1011 if (s->pict_type == P_TYPE) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1012 if (s->use_skip_mb_code) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1013 if (get_bits1(&s->gb)) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1014 /* skip mb */
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1015 s->mb_intra = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1016 for(i=0;i<6;i++)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1017 s->block_last_index[i] = -1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1018 s->mv_dir = MV_DIR_FORWARD;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1019 s->mv_type = MV_TYPE_16X16;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1020 s->mv[0][0][0] = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1021 s->mv[0][0][1] = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1022 s->mb_skiped = 1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1023 return 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1024 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1025 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1026
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1027 code = get_vlc(&s->gb, &v2_mb_type_vlc);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1028 s->mb_intra = code >>2;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1029
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1030 cbp = code & 0x3;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1031 } else {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1032 s->mb_intra = 1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1033 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1034 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1035
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1036 if (!s->mb_intra) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1037 int mx, my;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1038
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1039 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1040 if((cbp&3) != 3) cbp^= 0x3C;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1041
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1042 h263_pred_motion(s, 0, &mx, &my);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1043 mx= msmpeg4v2_decode_motion(s, mx, 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1044 my= msmpeg4v2_decode_motion(s, my, 1);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1045
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1046 s->mv_dir = MV_DIR_FORWARD;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1047 s->mv_type = MV_TYPE_16X16;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1048 s->mv[0][0][0] = mx;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1049 s->mv[0][0][1] = my;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1050 } else {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1051 s->ac_pred = get_bits1(&s->gb);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1052 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1053 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1054
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1055 for (i = 0; i < 6; i++) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1056 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1057 {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1058 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1059 return -1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1060 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1061 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1062 return 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1063 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1064
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1065 int msmpeg4_decode_mb(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1066 DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
1067 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 int cbp, code, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 UINT8 *coded_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1070
986e461dc072 Initial revision
glantau
parents:
diff changeset
1071 /* special slice handling */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1072 if (s->mb_x == 0) {
122
56e34b70ef5d * prevent crash in decoder
kabi
parents: 48
diff changeset
1073 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074 int wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1075 /* reset DC pred (set previous line to 1024) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1076 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1077 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1078 1024, 2 * s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1079 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1081 1024, s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1082 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1083 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
1084
cf37da86d990 fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents: 122
diff changeset
1085 /* 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
1086 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
1087 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
1088 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
1089 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
1090 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
1091 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
1092 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
1093 0, s->mb_width*16);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1094
986e461dc072 Initial revision
glantau
parents:
diff changeset
1095 s->first_slice_line = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1096 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1097 s->first_slice_line = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1098 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1099 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1100
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1101 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1102
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1103 if (s->pict_type == P_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1104 set_stat(ST_INTER_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1105 if (s->use_skip_mb_code) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1106 if (get_bits1(&s->gb)) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1107 /* skip mb */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1108 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1109 for(i=0;i<6;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1110 s->block_last_index[i] = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1111 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1112 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1113 s->mv[0][0][0] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1114 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
1115 s->mb_skiped = 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1116 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1117 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1118 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1119
986e461dc072 Initial revision
glantau
parents:
diff changeset
1120 code = get_vlc(&s->gb, &mb_non_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1121 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1122 return -1;
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1123 //s->mb_intra = (code & 0x40) ? 0 : 1;
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1124 s->mb_intra = (~code & 0x40) >> 6;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1125
986e461dc072 Initial revision
glantau
parents:
diff changeset
1126 cbp = code & 0x3f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1127 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1128 set_stat(ST_INTRA_MB);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1129 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1130 code = get_vlc(&s->gb, &mb_intra_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1131 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1132 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1133 /* predict coded block pattern */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1134 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1135 for(i=0;i<6;i++) {
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1136 int val = ((code >> (5 - i)) & 1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1137 if (i < 4) {
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1138 int pred = coded_block_pred(s, i, &coded_val);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1139 val = val ^ pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1140 *coded_val = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1141 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1142 cbp |= val << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1143 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1144 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1145
986e461dc072 Initial revision
glantau
parents:
diff changeset
1146 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1147 int mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1148 set_stat(ST_MV);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1149 h263_pred_motion(s, 0, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1150 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1151 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1152 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1153 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1154 s->mv[0][0][0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1155 s->mv[0][0][1] = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1156 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1157 set_stat(ST_INTRA_MB);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1158 s->ac_pred = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1159 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1160
986e461dc072 Initial revision
glantau
parents:
diff changeset
1161 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1162 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1163 {
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1164 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1165 // return -1;
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1166 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1167 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1168 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1169 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1170
986e461dc072 Initial revision
glantau
parents:
diff changeset
1171 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1172 int n, int coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1173 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1174 int code, level, i, j, last, run, run_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1175 int dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1176 RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1177 const UINT8 *scan_table;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1178 int qmul, qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1179
986e461dc072 Initial revision
glantau
parents:
diff changeset
1180 if (s->mb_intra) {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1181 qmul=1;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1182 qadd=0;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1183
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1184 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1185 set_stat(ST_DC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1186 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1187 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1188 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1189 block[0] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1190 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1191 rl = &rl_table[s->rl_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1192 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1193 rl = &rl_table[3 + s->rl_chroma_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1194 }
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1195
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1196 run_diff = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1197 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1198 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1199 goto not_coded;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1200 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1201 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1202 if (dc_pred_dir == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1203 scan_table = ff_alternate_vertical_scan; /* left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1204 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1205 scan_table = ff_alternate_horizontal_scan; /* top */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1206 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1207 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1208 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1209 set_stat(ST_INTRA_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1210 } else {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1211 qmul = s->qscale << 1;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1212 qadd = (s->qscale - 1) | 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1213 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1214 rl = &rl_table[3 + s->rl_table_index];
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1215
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1216 if(s->msmpeg4_version==2)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1217 run_diff = 0;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1218 else
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1219 run_diff = 1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1220
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1221 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1222 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1223 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1224 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1225 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1226 set_stat(ST_INTER_AC);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1227 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1228
986e461dc072 Initial revision
glantau
parents:
diff changeset
1229 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1230 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1231 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1232 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1233 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1234 /* escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1235 if (get_bits1(&s->gb) == 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1236 if (get_bits1(&s->gb) == 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1237 /* third escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1238 last = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1239 run = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1240 level = get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1241 level = (level << 24) >> 24; /* sign extend */
246
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1242 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
214ac77c0948 * simplified indexing
kabi
parents: 228
diff changeset
1243 if (level>0) level= level * qmul + qadd;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1244 else level= level * qmul - qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1245 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1246 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1247 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1248 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1249 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1250 run = rl->table_run[code];
201
91ed656e7339 (commit by michael)
arpi_esp
parents: 200
diff changeset
1251 level = rl->table_level[code];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1252 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1253 run += rl->max_run[last][level] + run_diff;
201
91ed656e7339 (commit by michael)
arpi_esp
parents: 200
diff changeset
1254 level= level * qmul + qadd;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1255 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1256 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1257 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1258 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1259 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1260 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1261 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1262 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1263 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1264 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1265 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1266 level += rl->max_level[last][run];
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1267 level= level * qmul + qadd;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1268 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1269 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1270 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1271 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1272 run = rl->table_run[code];
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 195
diff changeset
1273 level = rl->table_level[code] * qmul + qadd;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1274 last = code >= rl->last;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1275 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1276 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1277 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1278 i += run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1279 if (i >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1280 return -1;
311
ac677a84d5df wmv1 header parser (unfinished)
michaelni
parents: 310
diff changeset
1281 //printf("RL:%d %d %d ", run, level, last);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1282 j = scan_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1283 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1284 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1285 if (last)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1286 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1287 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1288 not_coded:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1289 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1290 mpeg4_pred_ac(s, block, n, dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1291 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1292 i = 64; /* XXX: not optimal */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1293 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1294 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1295 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1296
986e461dc072 Initial revision
glantau
parents:
diff changeset
1297 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1298 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1299
986e461dc072 Initial revision
glantau
parents:
diff changeset
1300 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1301 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1302 int level, pred;
25
2f603eb2f23d types fix
glantau
parents: 21
diff changeset
1303 INT16 *dc_val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1304
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1305 if(s->msmpeg4_version==2){
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1306 if (n < 4) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1307 level = get_vlc(&s->gb, &v2_dc_lum_vlc);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1308 } else {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1309 level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1310 }
309
32485ad4cc4e fixing msmpeg4v2 bugs (is bugfree now afaik)
michaelni
parents: 307
diff changeset
1311 if (level < 0)
307
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1312 return -1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1313 level-=256;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1314 }else{ //FIXME optimize use unified tables & index
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1315 if (n < 4) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1316 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1317 } else {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1318 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1319 }
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1320 if (level < 0)
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1321 return -1;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1322
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1323 if (level == DC_MAX) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1324 level = get_bits(&s->gb, 8);
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1325 if (get_bits1(&s->gb))
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1326 level = -level;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1327 } else if (level != 0) {
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1328 if (get_bits1(&s->gb))
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1329 level = -level;
764aeec1320e msmpeg4v2 decoding (no encoding yet)
michaelni
parents: 300
diff changeset
1330 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1331 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1332
986e461dc072 Initial revision
glantau
parents:
diff changeset
1333 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1334 level += pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1335
986e461dc072 Initial revision
glantau
parents:
diff changeset
1336 /* update predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1337 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1338 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1339 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1340 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1341 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1342
986e461dc072 Initial revision
glantau
parents:
diff changeset
1343 return level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1345
986e461dc072 Initial revision
glantau
parents:
diff changeset
1346 static int msmpeg4_decode_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1347 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1348 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1349 MVTable *mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1350 int code, mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1351
986e461dc072 Initial revision
glantau
parents:
diff changeset
1352 mv = &mv_tables[s->mv_table_index];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1353
986e461dc072 Initial revision
glantau
parents:
diff changeset
1354 code = get_vlc(&s->gb, &mv->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1355 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1356 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1357 if (code == mv->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1358 mx = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1359 my = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1360 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1361 mx = mv->table_mvx[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1362 my = mv->table_mvy[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1363 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1364
986e461dc072 Initial revision
glantau
parents:
diff changeset
1365 mx += *mx_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1366 my += *my_ptr - 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1367 /* WARNING : they do not do exactly modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1368 if (mx <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1369 mx += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1370 else if (mx >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1371 mx -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1372
986e461dc072 Initial revision
glantau
parents:
diff changeset
1373 if (my <= -64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1374 my += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1375 else if (my >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1376 my -= 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1377 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1378 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1379 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1380 }