annotate h263.c @ 200:6ab301aaa652 libavcodec

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