annotate h263.c @ 157:bc12fd7e6153 libavcodec

temp denoiser changes: (a-b)^2 instead of |a-b| and MMX2/3DNOW version
author michael
date Wed, 14 Nov 2001 02:46:58 +0000
parents 3c3449bce692
children 7ce36cf13055
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 * H263/MPEG4 backend for ffmpeg encoder and decoder
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000,2001 Gerard Lantau.
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
4 * H263+ support.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * Copyright (c) 2001 Juan J. Sierralta P.
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include "common.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "h263data.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #include "mpeg4data.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
27
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 int n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 static void h263_encode_motion(MpegEncContext * s, int val);
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
31 static void h263p_encode_umotion(MpegEncContext * s, int val);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 int n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 static int h263_decode_motion(MpegEncContext * s, int pred);
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
35 static int h263p_decode_umotion(MpegEncContext * s, int pred);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 int n, int coded);
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 int n, int coded);
986e461dc072 Initial revision
glantau
parents:
diff changeset
40
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 int h263_get_picture_format(int width, int height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 int format;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 if (width == 128 && height == 96)
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 format = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 else if (width == 176 && height == 144)
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 format = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 else if (width == 352 && height == 288)
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 format = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 else if (width == 704 && height == 576)
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 format = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 else if (width == 1408 && height == 1152)
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 format = 5;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 format = 7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 return format;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
59
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 {
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
62 int format;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
63
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 align_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 put_bits(&s->pb, 22, 0x20);
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) /
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 s->frame_rate) & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
68
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 put_bits(&s->pb, 1, 1); /* marker */
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 put_bits(&s->pb, 1, 0); /* h263 id */
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 put_bits(&s->pb, 1, 0); /* split screen off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 put_bits(&s->pb, 1, 0); /* camera off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 put_bits(&s->pb, 1, 0); /* freeze picture release off */
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
74
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
75 format = h263_get_picture_format(s->width, s->height);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 if (!s->h263_plus) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 /* H.263v1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 put_bits(&s->pb, 3, format);
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 of H.263v1 UMV implies to check the predicted MV after
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 calculation of the current MB to see if we're on the limits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 put_bits(&s->pb, 1, 0); /* SAC: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 put_bits(&s->pb, 1, 0); /* not PB frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 put_bits(&s->pb, 5, s->qscale);
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 /* H.263v2 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 /* H.263 Plus PTYPE */
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 put_bits(&s->pb, 3, 7);
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
94 if (format == 7)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
95 put_bits(&s->pb,3,6); /* Custom Source Format */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
96 else
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
97 put_bits(&s->pb, 3, format);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
98
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 put_bits(&s->pb,1,0); /* Custom PCF: off */
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
100 s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
101 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 put_bits(&s->pb,1,0); /* SAC: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 put_bits(&s->pb,1,0); /* Advanced Intra Coding: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 put_bits(&s->pb,1,0); /* Deblocking Filter: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 put_bits(&s->pb,1,0); /* Slice Structured: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 put_bits(&s->pb,1,0); /* Modified Quantization: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 put_bits(&s->pb,3,0); /* Reserved */
986e461dc072 Initial revision
glantau
parents:
diff changeset
113
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
986e461dc072 Initial revision
glantau
parents:
diff changeset
115
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 put_bits(&s->pb,1,0); /* Rounding Type */
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 put_bits(&s->pb,2,0); /* Reserved */
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
121
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 /* This should be here if PLUSPTYPE */
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
986e461dc072 Initial revision
glantau
parents:
diff changeset
124
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
125 if (format == 7) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
126 /* Custom Picture Format (CPFMT) */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
127
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
128 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
129 put_bits(&s->pb,9,(s->width >> 2) - 1);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
130 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
131 put_bits(&s->pb,9,(s->height >> 2));
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
132 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
133
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
135 if (s->umvplus)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 put_bits(&s->pb, 5, s->qscale);
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
139
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 put_bits(&s->pb, 1, 0); /* no PEI */
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
142
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 void h263_encode_mb(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 DCTELEM block[6][64],
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 int motion_x, int motion_y)
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 int cbpc, cbpy, i, cbp, pred_x, pred_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
150 if (!s->mb_intra) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
151 /* compute cbp */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
152 cbp = 0;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
153 for (i = 0; i < 6; i++) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
154 if (s->block_last_index[i] >= 0)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
155 cbp |= 1 << (5 - i);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
156 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
157 if ((cbp | motion_x | motion_y) == 0) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
158 /* skip macroblock */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
159 put_bits(&s->pb, 1, 1);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
160 return;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
161 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
162 put_bits(&s->pb, 1, 0); /* mb coded */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
163 cbpc = cbp & 3;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
164 put_bits(&s->pb,
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
165 inter_MCBPC_bits[cbpc],
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
166 inter_MCBPC_code[cbpc]);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
167 cbpy = cbp >> 2;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
168 cbpy ^= 0xf;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
169 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
170
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
171 /* motion vectors: 16x16 mode only now */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
172 h263_pred_motion(s, 0, &pred_x, &pred_y);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
173
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
174 if (!s->umvplus) {
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
175 h263_encode_motion(s, motion_x - pred_x);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
176 h263_encode_motion(s, motion_y - pred_y);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
177 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
178 else {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
179 h263p_encode_umotion(s, motion_x - pred_x);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
180 h263p_encode_umotion(s, motion_y - pred_y);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
181 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
182 /* To prevent Start Code emulation */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
183 put_bits(&s->pb,1,1);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
184 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
185 } else {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 /* compute cbp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 cbp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 if (s->block_last_index[i] >= 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 cbp |= 1 << (5 - i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
192
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 cbpc = cbp & 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 intra_MCBPC_bits[cbpc],
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 intra_MCBPC_code[cbpc]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 put_bits(&s->pb, 1, 0); /* mb coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 put_bits(&s->pb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 inter_MCBPC_bits[cbpc + 4],
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 inter_MCBPC_code[cbpc + 4]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 /* XXX: currently, we do not try to use ac prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 put_bits(&s->pb, 1, 0); /* no ac prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 cbpy = cbp >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][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 /* encode each block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 mpeg4_encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 h263_encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
223
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 static inline int mid_pred(int a, int b, int c)
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 int vmin, vmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 vmin = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 if (b < vmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 vmin = b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 if (c < vmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 vmin = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
232
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 vmax = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 if (b > vmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 vmax = b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 if (c > vmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 vmax = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
238
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 return a + b + c - vmin - vmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
241
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 INT16 *h263_pred_motion(MpegEncContext * s, int block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 int *px, int *py)
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 int x, y, wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 INT16 *A, *B, *C, *mot_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 x = 2 * s->mb_x + 1 + (block & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 y = 2 * s->mb_y + 1 + ((block >> 1) & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
251
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 mot_val = s->motion_val[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
253
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 /* special case for first line */
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
255 if (y == 1 || s->first_slice_line || s->first_gob_line) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 A = s->motion_val[(x-1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 *px = A[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 *py = A[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 switch(block) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 A = s->motion_val[(x-1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 B = s->motion_val[(x) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 C = s->motion_val[(x+2) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 A = s->motion_val[(x-1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 B = s->motion_val[(x) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 C = s->motion_val[(x+1) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 A = s->motion_val[(x-1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 B = s->motion_val[(x-1) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 C = s->motion_val[(x) + (y-1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 *px = mid_pred(A[0], B[0], C[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 *py = mid_pred(A[1], B[1], C[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 return mot_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
284
986e461dc072 Initial revision
glantau
parents:
diff changeset
285
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 static void h263_encode_motion(MpegEncContext * s, int val)
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 int range, l, m, bit_size, sign, code, bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
289
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 if (val == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 /* zero vector */
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 code = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 bit_size = s->f_code - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 range = 1 << bit_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 /* modulo encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 l = range * 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 m = 2 * l;
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 if (val < -l) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 val += m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 } else if (val >= l) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 val -= m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
305
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 if (val >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 val--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 code = (val >> bit_size) + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 bits = val & (range - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 val = -val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 val--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 code = (val >> bit_size) + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 bits = val & (range - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
318
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 if (bit_size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 put_bits(&s->pb, bit_size, bits);
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
325
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
326 /* Encode MV differences on H.263+ with Unrestricted MV mode */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
327 static void h263p_encode_umotion(MpegEncContext * s, int val)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
328 {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
329 short sval = 0;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
330 short i = 0;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
331 short n_bits = 0;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
332 short temp_val;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
333 int code = 0;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
334 int tcode;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
335
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
336 if ( val == 0)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
337 put_bits(&s->pb, 1, 1);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
338 else if (val == 1)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
339 put_bits(&s->pb, 3, 0);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
340 else if (val == -1)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
341 put_bits(&s->pb, 3, 2);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
342 else {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
343
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
344 sval = ((val < 0) ? (short)(-val):(short)val);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
345 temp_val = sval;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
346
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
347 while (temp_val != 0) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
348 temp_val = temp_val >> 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
349 n_bits++;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
350 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
351
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
352 i = n_bits - 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
353 while (i > 0) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
354 tcode = (sval & (1 << (i-1))) >> (i-1);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
355 tcode = (tcode << 1) | 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
356 code = (code << 2) | tcode;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
357 i--;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
358 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
359 code = ((code << 1) | (val < 0)) << 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
360 put_bits(&s->pb, (2*n_bits)+1, code);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
361 //printf("\nVal = %d\tCode = %d", sval, code);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
362 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
363 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
364
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 void h263_encode_init_vlc(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 static int done = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
368
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 if (!done) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 done = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 init_rl(&rl_inter);
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 init_rl(&rl_intra);
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
375
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 int code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 RLTable *rl = &rl_inter;
986e461dc072 Initial revision
glantau
parents:
diff changeset
381
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 level = block[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 /* 255 cannot be represented, so we clamp */
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 if (level > 254) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 level = 254;
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 block[0] = 254;
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 if (level == 128)
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 put_bits(&s->pb, 8, 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
392 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 put_bits(&s->pb, 8, level & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
398
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 last_non_zero = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 for (; i <= last_index; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 run = i - last_non_zero - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 last = (i == last_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 slevel = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 code = get_rl_index(rl, last, run, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 put_bits(&s->pb, 1, last);
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 put_bits(&s->pb, 6, run);
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 put_bits(&s->pb, 8, slevel & 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
421 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
427
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 /***************************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
429
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 /* write mpeg4 VOP header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 align_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
434
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 put_bits(&s->pb, 32, 0x1B6); /* vop header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 /* XXX: time base + 1 not always correct */
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
440
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 put_bits(&s->pb, 1, 1); /* marker */
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 put_bits(&s->pb, 4, 1); /* XXX: correct time increment */
986e461dc072 Initial revision
glantau
parents:
diff changeset
443 put_bits(&s->pb, 1, 1); /* marker */
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 put_bits(&s->pb, 1, 1); /* vop coded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 if (s->pict_type == P_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 s->no_rounding = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
449 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
986e461dc072 Initial revision
glantau
parents:
diff changeset
450
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 put_bits(&s->pb, 5, s->qscale);
986e461dc072 Initial revision
glantau
parents:
diff changeset
452
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 if (s->pict_type != I_TYPE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 // printf("****frame %d\n", picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
456 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
457
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 void h263_dc_scale(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 int quant;
986e461dc072 Initial revision
glantau
parents:
diff changeset
461
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 quant = s->qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 /* luminance */
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 if (quant < 5)
986e461dc072 Initial revision
glantau
parents:
diff changeset
465 s->y_dc_scale = 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 else if (quant > 4 && quant < 9)
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 s->y_dc_scale = (2 * quant);
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 else if (quant > 8 && quant < 25)
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 s->y_dc_scale = (quant + 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 s->y_dc_scale = (2 * quant - 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
472 /* chrominance */
986e461dc072 Initial revision
glantau
parents:
diff changeset
473 if (quant < 5)
986e461dc072 Initial revision
glantau
parents:
diff changeset
474 s->c_dc_scale = 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
475 else if (quant > 4 && quant < 25)
986e461dc072 Initial revision
glantau
parents:
diff changeset
476 s->c_dc_scale = ((quant + 13) / 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
477 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
478 s->c_dc_scale = (quant - 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
480
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 static int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 int a, b, c, x, y, wrap, pred, scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 UINT16 *dc_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
485
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 /* find prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 dc_val = s->dc_val[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 scale = s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 x = s->mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 y = s->mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 dc_val = s->dc_val[n - 4 + 1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 scale = s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
500
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 /* B C
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 * A X
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 a = dc_val[(x - 1) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 b = dc_val[(x - 1) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 c = dc_val[(x) + (y - 1) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
507
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 if (abs(a - b) < abs(b - c)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 pred = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 *dir_ptr = 1; /* top */
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 pred = a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 *dir_ptr = 0; /* left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 /* we assume pred is positive */
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 pred = (pred + (scale >> 1)) / scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
517
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 /* prepare address for prediction update */
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
986e461dc072 Initial revision
glantau
parents:
diff changeset
520
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 return pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
523
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 int dir)
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 int x, y, wrap, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 INT16 *ac_val, *ac_val1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
529
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 /* find prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 x = 2 * s->mb_x + 1 + (n & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 wrap = s->mb_width * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 ac_val = s->ac_val[0][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 x = s->mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 y = s->mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 ac_val = s->ac_val[n - 4 + 1][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 ac_val += ((y) * wrap + (x)) * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 ac_val1 = ac_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 if (dir == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 /* left prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 ac_val -= 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 for(i=1;i<8;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 block[i*8] += ac_val[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 /* top prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 ac_val -= 16 * wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 for(i=1;i<8;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 block[i] += ac_val[i + 8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 /* left copy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 for(i=1;i<8;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 ac_val1[i] = block[i * 8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 /* top copy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 for(i=1;i<8;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 ac_val1[8 + i] = block[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
566
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 int size, v, pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 UINT16 *dc_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
571
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
578
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 /* do the prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 level -= pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 /* find number of bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 v = abs(level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 while (v) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 v >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 size++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
588
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 /* luminance */
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 /* chrominance */
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
596
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 /* encode remaining bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 if (size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 level = (-level) ^ ((1 << size) - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 put_bits(&s->pb, size, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 if (size > 8)
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
606
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 int code, dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 const RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
612
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 /* mpeg4 based DC predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 mpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 rl = &rl_intra;
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 rl = &rl_inter;
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
622
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 last_non_zero = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 for (; i <= last_index; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 run = i - last_non_zero - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 last = (i == last_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 sign = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 slevel = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 sign = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 code = get_rl_index(rl, last, run, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 int level1, run1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 level1 = level - rl->max_level[last][run];
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 if (level1 < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 goto esc2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 code = get_rl_index(rl, last, run, level1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 esc2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 if (level > MAX_LEVEL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 run1 = run - rl->max_run[last][level] - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 if (run1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 goto esc3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 code = get_rl_index(rl, last, run1, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 esc3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
657 /* third escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 put_bits(&s->pb, 1, last);
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 put_bits(&s->pb, 6, run);
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 put_bits(&s->pb, 12, slevel & 0xfff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 put_bits(&s->pb, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 put_bits(&s->pb, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 put_bits(&s->pb, 1, sign);
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
679 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
683
986e461dc072 Initial revision
glantau
parents:
diff changeset
684
986e461dc072 Initial revision
glantau
parents:
diff changeset
685
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 /***********************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
687 /* decoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
688
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 static VLC intra_MCBPC_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 static VLC inter_MCBPC_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 static VLC cbpy_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 static VLC mv_vlc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 static VLC dc_lum, dc_chrom;
986e461dc072 Initial revision
glantau
parents:
diff changeset
694
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 void init_rl(RLTable *rl)
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 UINT8 index_run[MAX_RUN+1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 int last, run, level, start, end, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
700
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 /* compute max_level[], max_run[] and index_run[] */
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 for(last=0;last<2;last++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 if (last == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 start = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 end = rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 start = rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 end = rl->n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
710
986e461dc072 Initial revision
glantau
parents:
diff changeset
711 memset(max_level, 0, MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
712 memset(max_run, 0, MAX_LEVEL + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 memset(index_run, rl->n, MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
714 for(i=start;i<end;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
715 run = rl->table_run[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
716 level = rl->table_level[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 if (index_run[run] == rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 index_run[run] = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 if (level > max_level[run])
986e461dc072 Initial revision
glantau
parents:
diff changeset
720 max_level[run] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
721 if (run > max_run[level])
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 max_run[level] = run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
724 rl->max_level[last] = malloc(MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 rl->max_run[last] = malloc(MAX_LEVEL + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 rl->index_run[last] = malloc(MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
732
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 void init_vlc_rl(RLTable *rl)
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 init_vlc(&rl->vlc, 9, rl->n + 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 &rl->table_vlc[0][1], 4, 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 &rl->table_vlc[0][0], 4, 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
739
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 /* init vlcs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
741
986e461dc072 Initial revision
glantau
parents:
diff changeset
742 /* XXX: find a better solution to handle static init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 void h263_decode_init_vlc(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 static int done = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
746
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 if (!done) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 done = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
749
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 init_vlc(&intra_MCBPC_vlc, 6, 8,
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 intra_MCBPC_bits, 1, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 intra_MCBPC_code, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 init_vlc(&inter_MCBPC_vlc, 9, 20,
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 inter_MCBPC_bits, 1, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 inter_MCBPC_code, 1, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
756 init_vlc(&cbpy_vlc, 6, 16,
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 &cbpy_tab[0][1], 2, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 &cbpy_tab[0][0], 2, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 init_vlc(&mv_vlc, 9, 33,
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 &mvtab[0][1], 2, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 &mvtab[0][0], 2, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
762 init_rl(&rl_inter);
986e461dc072 Initial revision
glantau
parents:
diff changeset
763 init_rl(&rl_intra);
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 init_vlc_rl(&rl_inter);
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 init_vlc_rl(&rl_intra);
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 init_vlc(&dc_lum, 9, 13,
986e461dc072 Initial revision
glantau
parents:
diff changeset
767 &DCtab_lum[0][1], 2, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 &DCtab_lum[0][0], 2, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
769 init_vlc(&dc_chrom, 9, 13,
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 &DCtab_chrom[0][1], 2, 1,
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 &DCtab_chrom[0][0], 2, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
774
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 int h263_decode_mb(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
776 DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
777 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
778 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
779 unsigned int val;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
780 INT16 *mot_val;
110
e00e5d93457c Added Juergen Keil fix on quant_tab[4] type.
pulento
parents: 79
diff changeset
781 static INT8 quant_tab[4] = { -1, -2, 1, 2 };
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
782 unsigned int gfid;
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
783
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
784 /* Check for GOB Start Code */
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
785 if (s->mb_x == 0) {
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
786 val = show_bits(&s->gb, 16);
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
787 if (val == 0) {
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
788 /* We have a GBSC probably with GSTUFF */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
789 skip_bits(&s->gb, 16); /* Drop the zeros */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
790 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
791 #ifdef DEBUG
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
792 fprintf(stderr,"\nGOB Start Code at MB %d\n",
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
793 (s->mb_y * s->mb_width) + s->mb_x);
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
794 #endif
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
795 s->gob_number = get_bits(&s->gb, 5); /* GN */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
796 gfid = get_bits(&s->gb, 2); /* GFID */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
797 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
798 #ifdef DEBUG
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
799 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", gn, gfid, s->qscale);
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
800 #endif
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
801 }
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
802 }
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
803 /* FIXME: In the future H.263+ will have intra prediction */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
804 /* and we are gonna need another way to detect MPEG4 */
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
805 if (!s->h263_pred) {
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
806 if (s->mb_y == s->gob_number)
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
807 s->first_gob_line = 1;
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
808 else
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
809 s->first_gob_line = 0;
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
810 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
811 if (s->pict_type == P_TYPE) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
812 if (get_bits1(&s->gb)) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
813 /* skip mb */
986e461dc072 Initial revision
glantau
parents:
diff changeset
814 s->mb_intra = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
815 for(i=0;i<6;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
816 s->block_last_index[i] = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
817 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
818 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 s->mv[0][0][0] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
820 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
821 s->mb_skiped = 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
823 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
824 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
825 //fprintf(stderr, "\tCBPC: %d", cbpc);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
826 if (cbpc < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
827 return -1;
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
828
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 dquant = cbpc & 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 s->mb_intra = ((cbpc & 4) != 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
832 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
833 if (cbpc < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 dquant = cbpc & 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
837 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
838
986e461dc072 Initial revision
glantau
parents:
diff changeset
839 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
840 cbpy = get_vlc(&s->gb, &cbpy_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
841 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
842 if (dquant) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
843 s->qscale += quant_tab[get_bits(&s->gb, 2)];
986e461dc072 Initial revision
glantau
parents:
diff changeset
844 if (s->qscale < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
845 s->qscale = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
846 else if (s->qscale > 31)
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 s->qscale = 31;
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
849 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
850 if ((cbpc & 16) == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
851 /* 16x16 motion prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
852 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
853 h263_pred_motion(s, 0, &pred_x, &pred_y);
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
854 if (s->umvplus_dec)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
855 mx = h263p_decode_umotion(s, pred_x);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
856 else
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
857 mx = h263_decode_motion(s, pred_x);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 if (mx >= 0xffff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
859 return -1;
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
860
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
861 if (s->umvplus_dec)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
862 my = h263p_decode_umotion(s, pred_y);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
863 else
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
864 my = h263_decode_motion(s, pred_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 if (my >= 0xffff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
867 s->mv[0][0][0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 s->mv[0][0][1] = my;
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
869 /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
870 fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
871 fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
872 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
873 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
874
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
875 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
876 s->mv_type = MV_TYPE_8X8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
877 for(i=0;i<4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
878 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
879 if (s->umvplus_dec)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
880 mx = h263p_decode_umotion(s, pred_x);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
881 else
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
882 mx = h263_decode_motion(s, pred_x);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
883 if (mx >= 0xffff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
884 return -1;
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
885
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
886 if (s->umvplus_dec)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
887 my = h263p_decode_umotion(s, pred_y);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
888 else
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
889 my = h263_decode_motion(s, pred_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
890 if (my >= 0xffff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
891 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
892 s->mv[0][i][0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
893 s->mv[0][i][1] = my;
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
894 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
895 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
896 mot_val[0] = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
897 mot_val[1] = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
898 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
899 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
900 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
901 s->ac_pred = 0;
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
902 if (s->h263_pred) {
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
903 s->ac_pred = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
904 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
905 cbpy = get_vlc(&s->gb, &cbpy_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
906 cbp = (cbpc & 3) | (cbpy << 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
907 if (dquant) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
908 s->qscale += quant_tab[get_bits(&s->gb, 2)];
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 if (s->qscale < 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 s->qscale = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
911 else if (s->qscale > 31)
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 s->qscale = 31;
986e461dc072 Initial revision
glantau
parents:
diff changeset
913 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
914 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
915
986e461dc072 Initial revision
glantau
parents:
diff changeset
916 /* decode each block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
917 if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
918 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
919 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
920 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
921 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
922 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
923 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
924 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
925 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
926 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
927 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
928 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
929 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
930
986e461dc072 Initial revision
glantau
parents:
diff changeset
931 static int h263_decode_motion(MpegEncContext * s, int pred)
986e461dc072 Initial revision
glantau
parents:
diff changeset
932 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
933 int code, val, sign, shift, l, m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
934
986e461dc072 Initial revision
glantau
parents:
diff changeset
935 code = get_vlc(&s->gb, &mv_vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
936 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
937 return 0xffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
938
986e461dc072 Initial revision
glantau
parents:
diff changeset
939 if (code == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
940 return pred;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
941 sign = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
942 shift = s->f_code - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
943 val = (code - 1) << shift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
944 if (shift > 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
945 val |= get_bits(&s->gb, shift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
946 val++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
947 if (sign)
986e461dc072 Initial revision
glantau
parents:
diff changeset
948 val = -val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
949 val += pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
950
986e461dc072 Initial revision
glantau
parents:
diff changeset
951 /* modulo decoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
952 if (!s->h263_long_vectors) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
953 l = (1 << (s->f_code - 1)) * 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
954 m = 2 * l;
986e461dc072 Initial revision
glantau
parents:
diff changeset
955 if (val < -l) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
956 val += m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
957 } else if (val >= l) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
958 val -= m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
959 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
960 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
961 /* horrible h263 long vector mode */
986e461dc072 Initial revision
glantau
parents:
diff changeset
962 if (pred < -31 && val < -63)
986e461dc072 Initial revision
glantau
parents:
diff changeset
963 val += 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
964 if (pred > 32 && val > 63)
986e461dc072 Initial revision
glantau
parents:
diff changeset
965 val -= 64;
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
966
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
967 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
968 return val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
969 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
970
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
971 /* Decodes RVLC of H.263+ UMV */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
972 static int h263p_decode_umotion(MpegEncContext * s, int pred)
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
973 {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
974 int code = 0, sign;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
975
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
976 if (get_bits1(&s->gb)) /* Motion difference = 0 */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
977 return pred;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
978
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
979 code = 2 + get_bits1(&s->gb);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
980
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
981 while (get_bits1(&s->gb))
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
982 {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
983 code <<= 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
984 code += get_bits1(&s->gb);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
985 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
986 sign = code & 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
987 code >>= 1;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
988
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
989 code = (sign) ? (pred - code) : (pred + code);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
990 #ifdef DEBUG
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
991 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
992 #endif
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
993 return code;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
994
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
995 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
996
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
997 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
998 int n, int coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
999 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1000 int code, level, i, j, last, run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1001 RLTable *rl = &rl_inter;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1002
986e461dc072 Initial revision
glantau
parents:
diff changeset
1003 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1004 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1005 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1006 int component, diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1007 component = (n <= 3 ? 0 : n - 4 + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1008 level = s->last_dc[component];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1009 if (s->rv10_first_dc_coded[component]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1010 diff = rv_decode_dc(s, n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1011 if (diff == 0xffff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1012 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1013 level += diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1014 level = level & 0xff; /* handle wrap round */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1015 s->last_dc[component] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1016 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1017 s->rv10_first_dc_coded[component] = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1018 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1019 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1020 level = get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1021 if (level == 255)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1022 level = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1023 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1024 block[0] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1025 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1026 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1027 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1028 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1029 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1030 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1031 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1032 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1033
986e461dc072 Initial revision
glantau
parents:
diff changeset
1034 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1035 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1036 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1037 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039 /* escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1040 last = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 run = get_bits(&s->gb, 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1042 level = (INT8)get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1043 if (s->h263_rv10 && level == -128) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1044 /* XXX: should patch encoder too */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1045 level = get_bits(&s->gb, 12);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1046 level = (level << 20) >> 20;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1047 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1048 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1049 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1050 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1051 last = code >= rl->last;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1052 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1053 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1054 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1055 i += run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1056 if (i >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1059 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1060 if (last)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1061 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064 s->block_last_index[n] = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1065 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1066 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1067
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1070 int level, pred, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1071 UINT16 *dc_val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1072
986e461dc072 Initial revision
glantau
parents:
diff changeset
1073 if (n < 4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074 code = get_vlc(&s->gb, &dc_lum);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1075 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1076 code = get_vlc(&s->gb, &dc_chrom);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1077 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1078 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1079 if (code == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080 level = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1081 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1082 level = get_bits(&s->gb, code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1083 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
986e461dc072 Initial revision
glantau
parents:
diff changeset
1084 level = - (level ^ ((1 << code) - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1085 if (code > 8)
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1086 skip_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1087 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1088
986e461dc072 Initial revision
glantau
parents:
diff changeset
1089 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1090 level += pred;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1091 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1092 level = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1093 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1094 *dc_val = level * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1095 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1096 *dc_val = level * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1097 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1098 return level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1099 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1100
986e461dc072 Initial revision
glantau
parents:
diff changeset
1101 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1102 int n, int coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1103 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1104 int code, level, i, j, last, run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1105 int dc_pred_dir;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1106 RLTable *rl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1107 const UINT8 *scan_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1108
986e461dc072 Initial revision
glantau
parents:
diff changeset
1109 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1110 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1111 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1112 if (level < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1113 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1114 block[0] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1115 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1116 if (!coded)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1117 goto not_coded;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1118 rl = &rl_intra;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1119 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1120 if (dc_pred_dir == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1121 scan_table = ff_alternate_vertical_scan; /* left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1122 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1123 scan_table = ff_alternate_horizontal_scan; /* top */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1124 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1125 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1126 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1127 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1128 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1129 if (!coded) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1130 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1131 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1132 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1133 rl = &rl_inter;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1134 scan_table = zigzag_direct;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1135 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1136
986e461dc072 Initial revision
glantau
parents:
diff changeset
1137 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1138 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1139 if (code < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1140 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1141 if (code == rl->n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1142 /* escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1143 if (get_bits1(&s->gb) != 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1144 if (get_bits1(&s->gb) != 0) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1145 /* third escape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1146 last = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1147 run = get_bits(&s->gb, 6);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1148 get_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1149 level = get_bits(&s->gb, 12);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1150 level = (level << 20) >> 20; /* sign extend */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1151 skip_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1152 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1153 /* second escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1154 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1155 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1156 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1157 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1158 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1159 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1160 run += rl->max_run[last][level] + 1;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1161 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1162 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1163 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1164 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1165 /* first escape */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1166 code = get_vlc(&s->gb, &rl->vlc);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1167 if (code < 0 || code >= rl->n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1168 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1169 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1170 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1171 last = code >= rl->last;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1172 level += rl->max_level[last][run];
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1173 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1174 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1175 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1176 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1177 run = rl->table_run[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1178 level = rl->table_level[code];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1179 last = code >= rl->last;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1180 if (get_bits1(&s->gb))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1181 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1182 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1183 i += run;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1184 if (i >= 64)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1185 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1186 j = scan_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1187 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1188 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1189 if (last)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1190 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1191 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1192 not_coded:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1193 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1194 mpeg4_pred_ac(s, block, n, dc_pred_dir);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1195 if (s->ac_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1196 i = 64; /* XXX: not optimal */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1197 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1198 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1199 s->block_last_index[n] = i - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1200 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1201 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1202
986e461dc072 Initial revision
glantau
parents:
diff changeset
1203 /* most is hardcoded. should extend to handle all h263 streams */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1204 int h263_decode_picture_header(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1205 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1206 int format, width, height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1207
986e461dc072 Initial revision
glantau
parents:
diff changeset
1208 /* picture header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1209 if (get_bits(&s->gb, 22) != 0x20)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1210 return -1;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1211 skip_bits(&s->gb, 8); /* picture timestamp */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1212
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1213 if (get_bits1(&s->gb) != 1)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1214 return -1; /* marker */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1215 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1216 return -1; /* h263 id */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1217 skip_bits1(&s->gb); /* split screen off */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1218 skip_bits1(&s->gb); /* camera off */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1219 skip_bits1(&s->gb); /* freeze picture release off */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1220
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
1221 /* Reset GOB number */
154
f914f710b8d0 - Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents: 144
diff changeset
1222 s->gob_number = 0;
155
3c3449bce692 - Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents: 154
diff changeset
1223
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1224 format = get_bits(&s->gb, 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1225
986e461dc072 Initial revision
glantau
parents:
diff changeset
1226 if (format != 7) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1227 s->h263_plus = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1228 /* H.263v1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1229 width = h263_format[format][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1230 height = h263_format[format][1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1231 if (!width)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1232 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1233
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1234 s->pict_type = I_TYPE + get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1235
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1236 s->unrestricted_mv = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1237 s->h263_long_vectors = s->unrestricted_mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1238
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1239 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1240 return -1; /* SAC: off */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1241 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1242 return -1; /* advanced prediction mode: off */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1243 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1244 return -1; /* not PB frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1245
986e461dc072 Initial revision
glantau
parents:
diff changeset
1246 s->qscale = get_bits(&s->gb, 5);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1247 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1248 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1249 s->h263_plus = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1250 /* H.263v2 */
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1251 /* OPPTYPE */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1252
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1253 if (get_bits(&s->gb, 3) != 1) /* Update Full Extended PTYPE */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1254 return -1;
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1255 format = get_bits(&s->gb, 3);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1256
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1257 skip_bits(&s->gb,1); /* Custom PCF */
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
1258 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1259 skip_bits(&s->gb, 10);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1260 skip_bits(&s->gb, 3); /* Reserved */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1261
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1262 /* MPPTYPE */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1263 s->pict_type = get_bits(&s->gb, 3) + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1264 if (s->pict_type != I_TYPE &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
1265 s->pict_type != P_TYPE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1266 return -1;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1267 skip_bits(&s->gb, 7);
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1268
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1269 /* Get the picture dimensions */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1270 if (format == 6) {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1271 /* Custom Picture Format (CPFMT) */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1272 skip_bits(&s->gb, 4); /* aspect ratio */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1273 width = (get_bits(&s->gb, 9) + 1) * 4;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1274 skip_bits1(&s->gb);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1275 height = get_bits(&s->gb, 9) * 4;
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1276 #ifdef DEBUG
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1277 fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height);
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1278 #endif
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1279 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1280 else {
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1281 width = h263_format[format][0];
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1282 height = h263_format[format][1];
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1283 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1284
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1285 if ((width == 0) || (height == 0))
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1286 return -1;
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1287
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
1288 if (s->umvplus_dec) {
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1289 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1290 }
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 70
diff changeset
1291
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1292 s->qscale = get_bits(&s->gb, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1293 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1294 /* PEI */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1295 while (get_bits1(&s->gb) != 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1296 skip_bits(&s->gb, 8);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1297 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1298 s->f_code = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1299 s->width = width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1300 s->height = height;
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
1301
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1302 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1303 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1304
986e461dc072 Initial revision
glantau
parents:
diff changeset
1305 /* decode mpeg4 VOP header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1306 int mpeg4_decode_picture_header(MpegEncContext * s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1307 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1308 int time_incr, startcode, state, v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1309
986e461dc072 Initial revision
glantau
parents:
diff changeset
1310 redo:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1311 /* search next start code */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1312 align_get_bits(&s->gb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1313 state = 0xff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1314 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1315 v = get_bits(&s->gb, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1316 if (state == 0x000001) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1317 state = ((state << 8) | v) & 0xffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1318 startcode = state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1319 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1320 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1321 state = ((state << 8) | v) & 0xffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1322 /* XXX: really detect end of frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1323 if (state == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1324 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1325 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1326
986e461dc072 Initial revision
glantau
parents:
diff changeset
1327 if (startcode == 0x120) {
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1328 int time_increment_resolution, width, height, vo_ver_id;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1329
986e461dc072 Initial revision
glantau
parents:
diff changeset
1330 /* vol header */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1331 skip_bits(&s->gb, 1); /* random access */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1332 skip_bits(&s->gb, 8); /* vo_type */
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1333 if (get_bits1(&s->gb) != 0) { /* is_ol_id */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1334 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1335 skip_bits(&s->gb, 3); /* vo_priority */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1336 } else {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1337 vo_ver_id = 1;
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1338 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1339
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1340 skip_bits(&s->gb, 4); /* aspect_ratio_info */
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1341 skip_bits1(&s->gb); /* vol control parameter */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1342 s->shape = get_bits(&s->gb, 2); /* vol shape */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1343 skip_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1344
986e461dc072 Initial revision
glantau
parents:
diff changeset
1345 time_increment_resolution = get_bits(&s->gb, 16);
70
c7767df463f4 log2 to av_log2
glantau
parents: 63
diff changeset
1346 s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1;
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1347 if (s->time_increment_bits < 1)
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1348 s->time_increment_bits = 1;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1349 skip_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1350
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1351 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1352 skip_bits(&s->gb, s->time_increment_bits);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1353 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1354
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1355 if (s->shape != 2) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1356 if (s->shape == 0) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1357 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1358 width = get_bits(&s->gb, 13);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1359 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1360 height = get_bits(&s->gb, 13);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1361 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1362 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1363
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1364 skip_bits1(&s->gb); /* interlaced */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1365 skip_bits1(&s->gb); /* OBMC */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1366 if (vo_ver_id == 1) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1367 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1368 } else {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1369 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1370 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1371 if (get_bits1(&s->gb) == 1) { /* not_8_bit */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1372 s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1373 skip_bits(&s->gb, 4); /* bits_per_pixel */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1374 } else {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1375 s->quant_precision = 5;
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1376 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1377
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1378 skip_bits1(&s->gb); /* vol_quant_type */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1379 skip_bits1(&s->gb); /* vol_quarter_pixel */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1380 skip_bits1(&s->gb); /* complexity_estimation_disabled */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1381 skip_bits1(&s->gb); /* resync_marker_disabled */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1382 skip_bits1(&s->gb); /* data_partioning_enabled */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1383 if (get_bits1(&s->gb) != 0) { /* scalability */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1384 printf("bad scalability!!!\n");
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1385 return -1;
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1386 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1387 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1388 goto redo;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1389 } else if (startcode != 0x1b6) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1390 goto redo;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1391 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1392
986e461dc072 Initial revision
glantau
parents:
diff changeset
1393 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1394 if (s->pict_type != I_TYPE &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
1395 s->pict_type != P_TYPE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1396 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1397
986e461dc072 Initial revision
glantau
parents:
diff changeset
1398 /* XXX: parse time base */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1399 time_incr = 0;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1400 while (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1401 time_incr++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1402
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1403 skip_bits1(&s->gb); /* marker */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1404 skip_bits(&s->gb, s->time_increment_bits);
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1405 skip_bits1(&s->gb); /* marker */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1406 /* vop coded */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1407 if (get_bits1(&s->gb) != 1)
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1408 goto redo;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1409
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1410 if (s->shape != 2 && s->pict_type == P_TYPE) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1411 /* rounding type for motion estimation */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1412 s->no_rounding = get_bits1(&s->gb);
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1413 } else {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1414 s->no_rounding = 0;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1415 }
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1416
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1417 if (s->shape != 0) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1418 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1419 int width, height, hor_spat_ref, ver_spat_ref;
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1420
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1421 width = get_bits(&s->gb, 13);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1422 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1423 height = get_bits(&s->gb, 13);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1424 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1425 hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1426 skip_bits1(&s->gb); /* marker */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1427 ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1428 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1429 skip_bits1(&s->gb); /* change_CR_disable */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1430
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1431 if (get_bits1(&s->gb) != 0) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1432 skip_bits(&s->gb, 8); /* constant_alpha_value */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1433 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1434 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1435
63
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1436 if (s->shape != 2) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1437 skip_bits(&s->gb, 3); /* intra dc VLC threshold */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1438
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1439 /* note: we do not use quant_precision to avoid problem if no
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1440 MPEG4 vol header as it is found on some old opendivx
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1441 movies */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1442 s->qscale = get_bits(&s->gb, 5);
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1443
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1444 if (s->pict_type != I_TYPE) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1445 s->f_code = get_bits(&s->gb, 3); /* fcode_for */
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1446 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1447 if (s->shape && (s->pict_type != I_TYPE)) {
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1448 skip_bits1(&s->gb); // vop shape coding type
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1449 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1450 }
8e2d8dbf89a5 better vol header parsing for mpeg4
glantau
parents: 21
diff changeset
1451 return 0;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1452 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1453
986e461dc072 Initial revision
glantau
parents:
diff changeset
1454 /* don't understand why they choose a different header ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1455 int intel_h263_decode_picture_header(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1456 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1457 int format;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1458
986e461dc072 Initial revision
glantau
parents:
diff changeset
1459 /* picture header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1460 if (get_bits(&s->gb, 22) != 0x20)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1461 return -1;
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1462 skip_bits(&s->gb, 8); /* picture timestamp */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1463
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1464 if (get_bits1(&s->gb) != 1)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1465 return -1; /* marker */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1466 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1467 return -1; /* h263 id */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1468 skip_bits1(&s->gb); /* split screen off */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1469 skip_bits1(&s->gb); /* camera off */
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1470 skip_bits1(&s->gb); /* freeze picture release off */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1471
986e461dc072 Initial revision
glantau
parents:
diff changeset
1472 format = get_bits(&s->gb, 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1473 if (format != 7)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1474 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1475
986e461dc072 Initial revision
glantau
parents:
diff changeset
1476 s->h263_plus = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1477
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1478 s->pict_type = I_TYPE + get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1479
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1480 s->unrestricted_mv = get_bits1(&s->gb);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1481 s->h263_long_vectors = s->unrestricted_mv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1482
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1483 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1484 return -1; /* SAC: off */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1485 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1486 return -1; /* advanced prediction mode: off */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1487 if (get_bits1(&s->gb) != 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1488 return -1; /* not PB frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1489
986e461dc072 Initial revision
glantau
parents:
diff changeset
1490 /* skip unknown header garbage */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1491 skip_bits(&s->gb, 41);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1492
986e461dc072 Initial revision
glantau
parents:
diff changeset
1493 s->qscale = get_bits(&s->gb, 5);
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1494 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1495
986e461dc072 Initial revision
glantau
parents:
diff changeset
1496 /* PEI */
21
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1497 while (get_bits1(&s->gb) != 0) {
20e680e7a490 get_bits() specialization, gives 4\speedup
arpi_esp
parents: 7
diff changeset
1498 skip_bits(&s->gb, 8);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1499 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1500 s->f_code = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1501 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1502 }
144
cb5dabd00ba2 - Bug fix on inter MCBPC table for inter+q.
pulento
parents: 110
diff changeset
1503