Mercurial > libavcodec.hg
annotate h263.c @ 265:4e9e728021d8 libavcodec
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
some minor optimizations
last non zero index cant be 64 bugfix
author | michaelni |
---|---|
date | Fri, 15 Mar 2002 04:10:53 +0000 |
parents | 28c5c62b1c4c |
children | 252444e5259b |
rev | line source |
---|---|
0 | 1 /* |
2 * H263/MPEG4 backend for ffmpeg encoder and decoder | |
3 * Copyright (c) 2000,2001 Gerard Lantau. | |
78 | 4 * H263+ support. |
0 | 5 * Copyright (c) 2001 Juan J. Sierralta P. |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 */ | |
21 #include "common.h" | |
22 #include "dsputil.h" | |
23 #include "avcodec.h" | |
24 #include "mpegvideo.h" | |
25 #include "h263data.h" | |
26 #include "mpeg4data.h" | |
27 | |
255 | 28 //rounded divison & shift |
29 #define RDIV(a,b) ((a) > 0 ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b)) | |
30 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
31 #define ABS(a) (((a)>=0)?(a):(-(a))) |
255 | 32 |
0 | 33 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, |
34 int n); | |
35 static void h263_encode_motion(MpegEncContext * s, int val); | |
78 | 36 static void h263p_encode_umotion(MpegEncContext * s, int val); |
0 | 37 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
38 int n, int dc, UINT8 *scan_table); |
262 | 39 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); |
78 | 40 static int h263p_decode_umotion(MpegEncContext * s, int pred); |
0 | 41 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, |
42 int n, int coded); | |
43 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
44 int n, int coded); | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
45 static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
46 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
47 int dir); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
48 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
49 extern UINT32 inverse[256]; |
0 | 50 |
51 int h263_get_picture_format(int width, int height) | |
52 { | |
53 int format; | |
54 | |
55 if (width == 128 && height == 96) | |
56 format = 1; | |
57 else if (width == 176 && height == 144) | |
58 format = 2; | |
59 else if (width == 352 && height == 288) | |
60 format = 3; | |
61 else if (width == 704 && height == 576) | |
62 format = 4; | |
63 else if (width == 1408 && height == 1152) | |
64 format = 5; | |
65 else | |
66 format = 7; | |
67 return format; | |
68 } | |
69 | |
70 void h263_encode_picture_header(MpegEncContext * s, int picture_number) | |
71 { | |
78 | 72 int format; |
0 | 73 |
74 align_put_bits(&s->pb); | |
231 | 75 |
76 /* Update the pointer to last GOB */ | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
77 s->ptr_lastgob = pbBufPtr(&s->pb); |
231 | 78 s->gob_number = 0; |
79 | |
80 put_bits(&s->pb, 22, 0x20); /* PSC */ | |
241 | 81 put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) / |
0 | 82 s->frame_rate) & 0xff); |
83 | |
84 put_bits(&s->pb, 1, 1); /* marker */ | |
85 put_bits(&s->pb, 1, 0); /* h263 id */ | |
86 put_bits(&s->pb, 1, 0); /* split screen off */ | |
87 put_bits(&s->pb, 1, 0); /* camera off */ | |
88 put_bits(&s->pb, 1, 0); /* freeze picture release off */ | |
78 | 89 |
90 format = h263_get_picture_format(s->width, s->height); | |
0 | 91 if (!s->h263_plus) { |
92 /* H.263v1 */ | |
93 put_bits(&s->pb, 3, format); | |
94 put_bits(&s->pb, 1, (s->pict_type == P_TYPE)); | |
95 /* By now UMV IS DISABLED ON H.263v1, since the restrictions | |
96 of H.263v1 UMV implies to check the predicted MV after | |
97 calculation of the current MB to see if we're on the limits */ | |
98 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */ | |
99 put_bits(&s->pb, 1, 0); /* SAC: off */ | |
100 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */ | |
101 put_bits(&s->pb, 1, 0); /* not PB frame */ | |
102 put_bits(&s->pb, 5, s->qscale); | |
103 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
104 } else { | |
105 /* H.263v2 */ | |
106 /* H.263 Plus PTYPE */ | |
107 put_bits(&s->pb, 3, 7); | |
108 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */ | |
78 | 109 if (format == 7) |
110 put_bits(&s->pb,3,6); /* Custom Source Format */ | |
111 else | |
112 put_bits(&s->pb, 3, format); | |
113 | |
0 | 114 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
|
115 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
|
116 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */ |
0 | 117 put_bits(&s->pb,1,0); /* SAC: off */ |
118 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */ | |
119 put_bits(&s->pb,1,0); /* Advanced Intra Coding: off */ | |
120 put_bits(&s->pb,1,0); /* Deblocking Filter: off */ | |
121 put_bits(&s->pb,1,0); /* Slice Structured: off */ | |
122 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */ | |
123 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */ | |
124 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */ | |
125 put_bits(&s->pb,1,0); /* Modified Quantization: off */ | |
126 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
127 put_bits(&s->pb,3,0); /* Reserved */ | |
128 | |
129 put_bits(&s->pb, 3, s->pict_type == P_TYPE); | |
130 | |
131 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */ | |
132 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */ | |
133 put_bits(&s->pb,1,0); /* Rounding Type */ | |
134 put_bits(&s->pb,2,0); /* Reserved */ | |
135 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
136 | |
137 /* This should be here if PLUSPTYPE */ | |
138 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
139 | |
78 | 140 if (format == 7) { |
141 /* Custom Picture Format (CPFMT) */ | |
0 | 142 |
78 | 143 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */ |
144 put_bits(&s->pb,9,(s->width >> 2) - 1); | |
145 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
146 put_bits(&s->pb,9,(s->height >> 2)); | |
147 } | |
148 | |
0 | 149 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
150 if (s->umvplus) |
0 | 151 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */ |
152 put_bits(&s->pb, 5, s->qscale); | |
153 } | |
154 | |
155 put_bits(&s->pb, 1, 0); /* no PEI */ | |
156 } | |
157 | |
162 | 158 int h263_encode_gob_header(MpegEncContext * s, int mb_line) |
159 { | |
160 int pdif=0; | |
161 | |
162 /* Check to see if we need to put a new GBSC */ | |
163 /* for RTP packetization */ | |
164 if (s->rtp_mode) { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
165 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
162 | 166 if (pdif >= s->rtp_payload_size) { |
167 /* Bad luck, packet must be cut before */ | |
168 align_put_bits(&s->pb); | |
231 | 169 flush_put_bits(&s->pb); |
170 /* Call the RTP callback to send the last GOB */ | |
171 if (s->rtp_callback) { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
172 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 173 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number); |
174 } | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
175 s->ptr_lastgob = pbBufPtr(&s->pb); |
162 | 176 put_bits(&s->pb, 17, 1); /* GBSC */ |
231 | 177 s->gob_number = mb_line / s->gob_index; |
162 | 178 put_bits(&s->pb, 5, s->gob_number); /* GN */ |
231 | 179 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */ |
162 | 180 put_bits(&s->pb, 5, s->qscale); /* GQUANT */ |
231 | 181 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif); |
162 | 182 return pdif; |
183 } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) { | |
184 /* Cut the packet before we can't */ | |
185 align_put_bits(&s->pb); | |
231 | 186 flush_put_bits(&s->pb); |
187 /* Call the RTP callback to send the last GOB */ | |
188 if (s->rtp_callback) { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
189 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 190 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number); |
191 } | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
192 s->ptr_lastgob = pbBufPtr(&s->pb); |
162 | 193 put_bits(&s->pb, 17, 1); /* GBSC */ |
231 | 194 s->gob_number = mb_line / s->gob_index; |
162 | 195 put_bits(&s->pb, 5, s->gob_number); /* GN */ |
231 | 196 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */ |
162 | 197 put_bits(&s->pb, 5, s->qscale); /* GQUANT */ |
231 | 198 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif); |
162 | 199 return pdif; |
200 } | |
201 } | |
202 return 0; | |
203 } | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
204 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
205 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6]) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
206 { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
207 int score0=0, score1=0; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
208 int i, n; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
209 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
210 for(n=0; n<6; n++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
211 int x, y, wrap; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
212 INT16 *ac_val, *ac_val1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
213 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
214 if (n < 4) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
215 x = 2 * s->mb_x + 1 + (n & 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
216 y = 2 * s->mb_y + 1 + (n >> 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
217 wrap = s->mb_width * 2 + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
218 ac_val = s->ac_val[0][0]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
219 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
220 x = s->mb_x + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
221 y = s->mb_y + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
222 wrap = s->mb_width + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
223 ac_val = s->ac_val[n - 4 + 1][0]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
224 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
225 ac_val += ((y) * wrap + (x)) * 16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
226 ac_val1= ac_val; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
227 if(dir[n]){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
228 ac_val-= wrap*16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
229 for(i=1; i<8; i++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
230 const int level= block[n][block_permute_op(i )]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
231 score0+= ABS(level); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
232 score1+= ABS(level - ac_val[i+8]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
233 ac_val1[i ]= block[n][block_permute_op(i<<3)]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
234 ac_val1[i+8]= level; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
235 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
236 }else{ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
237 ac_val-= 16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
238 for(i=1; i<8; i++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
239 const int level= block[n][block_permute_op(i<<3)]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
240 score0+= ABS(level); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
241 score1+= ABS(level - ac_val[i]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
242 ac_val1[i ]= level; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
243 ac_val1[i+8]= block[n][block_permute_op(i )]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
244 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
245 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
246 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
247 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
248 return score0 > score1 ? 1 : 0; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
249 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
250 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
251 void mpeg4_encode_mb(MpegEncContext * s, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
252 DCTELEM block[6][64], |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
253 int motion_x, int motion_y) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
254 { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
255 int cbpc, cbpy, i, cbp, pred_x, pred_y; |
162 | 256 |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
257 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
258 if (!s->mb_intra) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
259 /* compute cbp */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
260 cbp = 0; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
261 for (i = 0; i < 6; i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
262 if (s->block_last_index[i] >= 0) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
263 cbp |= 1 << (5 - i); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
264 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
265 if ((cbp | motion_x | motion_y) == 0) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
266 /* skip macroblock */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
267 put_bits(&s->pb, 1, 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
268 return; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
269 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
270 put_bits(&s->pb, 1, 0); /* mb coded */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
271 cbpc = cbp & 3; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
272 put_bits(&s->pb, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
273 inter_MCBPC_bits[cbpc], |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
274 inter_MCBPC_code[cbpc]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
275 cbpy = cbp >> 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
276 cbpy ^= 0xf; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
277 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
278 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
279 /* motion vectors: 16x16 mode only now */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
280 h263_pred_motion(s, 0, &pred_x, &pred_y); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
281 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
282 h263_encode_motion(s, motion_x - pred_x); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
283 h263_encode_motion(s, motion_y - pred_y); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
284 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
285 /* encode each block */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
286 for (i = 0; i < 6; i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
287 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
288 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
289 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
290 int dc_diff[6]; //dc values with the dc prediction subtracted |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
291 int dir[6]; //prediction direction |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
292 int zigzag_last_index[6]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
293 UINT8 *scan_table[6]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
294 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
295 for(i=0; i<6; i++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
296 const int level= block[i][0]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
297 UINT16 *dc_ptr; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
298 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
299 dc_diff[i]= level - mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
300 if (i < 4) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
301 *dc_ptr = level * s->y_dc_scale; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
302 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
303 *dc_ptr = level * s->c_dc_scale; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
304 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
305 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
306 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
307 s->ac_pred= decide_ac_pred(s, block, dir); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
308 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
309 if(s->ac_pred){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
310 for(i=0; i<6; i++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
311 UINT8 *st; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
312 int last_index; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
313 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
314 mpeg4_inv_pred_ac(s, block[i], i, dir[i]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
315 if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
316 else st = ff_alternate_horizontal_scan; /* top */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
317 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
318 for(last_index=63; last_index>=0; last_index--) //FIXME optimize |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
319 if(block[i][st[last_index]]) break; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
320 zigzag_last_index[i]= s->block_last_index[i]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
321 s->block_last_index[i]= last_index; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
322 scan_table[i]= st; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
323 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
324 }else{ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
325 for(i=0; i<6; i++) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
326 scan_table[i]= zigzag_direct; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
327 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
328 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
329 /* compute cbp */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
330 cbp = 0; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
331 for (i = 0; i < 6; i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
332 if (s->block_last_index[i] >= 1) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
333 cbp |= 1 << (5 - i); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
334 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
335 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
336 cbpc = cbp & 3; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
337 if (s->pict_type == I_TYPE) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
338 put_bits(&s->pb, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
339 intra_MCBPC_bits[cbpc], |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
340 intra_MCBPC_code[cbpc]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
341 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
342 put_bits(&s->pb, 1, 0); /* mb coded */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
343 put_bits(&s->pb, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
344 inter_MCBPC_bits[cbpc + 4], |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
345 inter_MCBPC_code[cbpc + 4]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
346 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
347 put_bits(&s->pb, 1, s->ac_pred); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
348 cbpy = cbp >> 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
349 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
350 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
351 /* encode each block */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
352 for (i = 0; i < 6; i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
353 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i]); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
354 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
355 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
356 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
357 if(s->ac_pred){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
358 for(i=0; i<6; i++){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
359 int j; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
360 int x, y, wrap; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
361 INT16 *ac_val; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
362 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
363 if (i < 4) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
364 x = 2 * s->mb_x + 1 + (i & 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
365 y = 2 * s->mb_y + 1 + (i >> 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
366 wrap = s->mb_width * 2 + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
367 ac_val = s->ac_val[0][0]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
368 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
369 x = s->mb_x + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
370 y = s->mb_y + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
371 wrap = s->mb_width + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
372 ac_val = s->ac_val[i - 4 + 1][0]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
373 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
374 ac_val += ((y) * wrap + (x)) * 16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
375 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
376 if(dir[i]){ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
377 for(j=1; j<8; j++) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
378 block[i][block_permute_op(j )]= ac_val[j+8]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
379 }else{ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
380 for(j=1; j<8; j++) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
381 block[i][block_permute_op(j<<3)]= ac_val[j ]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
382 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
383 s->block_last_index[i]= zigzag_last_index[i]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
384 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
385 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
386 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
387 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
388 |
0 | 389 void h263_encode_mb(MpegEncContext * s, |
390 DCTELEM block[6][64], | |
391 int motion_x, int motion_y) | |
392 { | |
393 int cbpc, cbpy, i, cbp, pred_x, pred_y; | |
162 | 394 |
0 | 395 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); |
78 | 396 if (!s->mb_intra) { |
397 /* compute cbp */ | |
398 cbp = 0; | |
399 for (i = 0; i < 6; i++) { | |
400 if (s->block_last_index[i] >= 0) | |
401 cbp |= 1 << (5 - i); | |
402 } | |
403 if ((cbp | motion_x | motion_y) == 0) { | |
404 /* skip macroblock */ | |
405 put_bits(&s->pb, 1, 1); | |
406 return; | |
407 } | |
408 put_bits(&s->pb, 1, 0); /* mb coded */ | |
409 cbpc = cbp & 3; | |
410 put_bits(&s->pb, | |
411 inter_MCBPC_bits[cbpc], | |
412 inter_MCBPC_code[cbpc]); | |
413 cbpy = cbp >> 2; | |
414 cbpy ^= 0xf; | |
415 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); | |
0 | 416 |
78 | 417 /* motion vectors: 16x16 mode only now */ |
418 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
419 | |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
420 if (!s->umvplus) { |
78 | 421 h263_encode_motion(s, motion_x - pred_x); |
422 h263_encode_motion(s, motion_y - pred_y); | |
423 } | |
424 else { | |
425 h263p_encode_umotion(s, motion_x - pred_x); | |
426 h263p_encode_umotion(s, motion_y - pred_y); | |
427 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) | |
428 /* To prevent Start Code emulation */ | |
429 put_bits(&s->pb,1,1); | |
430 } | |
431 } else { | |
0 | 432 /* compute cbp */ |
433 cbp = 0; | |
434 for (i = 0; i < 6; i++) { | |
435 if (s->block_last_index[i] >= 1) | |
436 cbp |= 1 << (5 - i); | |
437 } | |
438 | |
439 cbpc = cbp & 3; | |
440 if (s->pict_type == I_TYPE) { | |
441 put_bits(&s->pb, | |
442 intra_MCBPC_bits[cbpc], | |
443 intra_MCBPC_code[cbpc]); | |
444 } else { | |
445 put_bits(&s->pb, 1, 0); /* mb coded */ | |
446 put_bits(&s->pb, | |
447 inter_MCBPC_bits[cbpc + 4], | |
448 inter_MCBPC_code[cbpc + 4]); | |
449 } | |
450 if (s->h263_pred) { | |
451 /* XXX: currently, we do not try to use ac prediction */ | |
452 put_bits(&s->pb, 1, 0); /* no ac prediction */ | |
453 } | |
454 cbpy = cbp >> 2; | |
455 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); | |
456 } | |
457 | |
458 /* encode each block */ | |
459 if (s->h263_pred) { | |
460 for (i = 0; i < 6; i++) { | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
461 // mpeg4_encode_block(s, block[i], i); |
0 | 462 } |
463 } else { | |
464 for (i = 0; i < 6; i++) { | |
465 h263_encode_block(s, block[i], i); | |
466 } | |
467 } | |
468 } | |
469 | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
470 void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n) |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
471 { |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
472 int x, y, wrap, a, c, pred_dc, scale, i; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
473 INT16 *dc_val, *ac_val, *ac_val1; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
474 |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
475 /* find prediction */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
476 if (n < 4) { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
477 x = 2 * s->mb_x + 1 + (n & 1); |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
478 y = 2 * s->mb_y + 1 + ((n & 2) >> 1); |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
479 wrap = s->mb_width * 2 + 2; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
480 dc_val = s->dc_val[0]; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
481 ac_val = s->ac_val[0][0]; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
482 scale = s->y_dc_scale; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
483 } else { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
484 x = s->mb_x + 1; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
485 y = s->mb_y + 1; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
486 wrap = s->mb_width + 2; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
487 dc_val = s->dc_val[n - 4 + 1]; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
488 ac_val = s->ac_val[n - 4 + 1][0]; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
489 scale = s->c_dc_scale; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
490 } |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
491 |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
492 ac_val += ((y) * wrap + (x)) * 16; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
493 ac_val1 = ac_val; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
494 |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
495 /* B C |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
496 * A X |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
497 */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
498 a = dc_val[(x - 1) + (y) * wrap]; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
499 c = dc_val[(x) + (y - 1) * wrap]; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
500 |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
501 pred_dc = 1024; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
502 if (s->ac_pred) { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
503 if (s->h263_aic_dir) { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
504 /* left prediction */ |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
505 if (a != 1024) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
506 ac_val -= 16; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
507 for(i=1;i<8;i++) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
508 block[block_permute_op(i*8)] += ac_val[i]; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
509 } |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
510 pred_dc = a; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
511 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
512 } else { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
513 /* top prediction */ |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
514 if (c != 1024) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
515 ac_val -= 16 * wrap; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
516 for(i=1;i<8;i++) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
517 block[block_permute_op(i)] += ac_val[i + 8]; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
518 } |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
519 pred_dc = c; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
520 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
521 } |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
522 } else { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
523 /* just DC prediction */ |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
524 if (a != 1024 && c != 1024) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
525 pred_dc = (a + c) >> 1; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
526 else if (a != 1024) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
527 pred_dc = a; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
528 else |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
529 pred_dc = c; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
530 } |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
531 |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
532 /* we assume pred is positive */ |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
533 block[0]=block[0]*scale + pred_dc; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
534 |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
535 if (block[0] < 0) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
536 block[0] = 0; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
537 else if (!(block[0] & 1)) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
538 block[0]++; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
539 |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
540 /* Update AC/DC tables */ |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
541 dc_val[(x) + (y) * wrap] = block[0]; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
542 |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
543 /* left copy */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
544 for(i=1;i<8;i++) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
545 ac_val1[i] = block[block_permute_op(i * 8)]; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
546 /* top copy */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
547 for(i=1;i<8;i++) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
548 ac_val1[8 + i] = block[block_permute_op(i)]; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
549 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
550 |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
551 |
0 | 552 static inline int mid_pred(int a, int b, int c) |
553 { | |
554 int vmin, vmax; | |
245 | 555 vmax = vmin = a; |
0 | 556 if (b < vmin) |
557 vmin = b; | |
245 | 558 else |
559 vmax = b; | |
560 | |
0 | 561 if (c < vmin) |
562 vmin = c; | |
245 | 563 else if (c > vmax) |
0 | 564 vmax = c; |
565 | |
566 return a + b + c - vmin - vmax; | |
567 } | |
568 | |
569 INT16 *h263_pred_motion(MpegEncContext * s, int block, | |
570 int *px, int *py) | |
571 { | |
245 | 572 int xy, y, wrap; |
0 | 573 INT16 *A, *B, *C, *mot_val; |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
574 static const int off[4]= {2, 1, 1, -1}; |
0 | 575 |
576 wrap = 2 * s->mb_width + 2; | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
577 y = xy = 2 * s->mb_y + 1 + (block >> 1); // y |
245 | 578 xy *= wrap; // y * wrap |
579 xy += 2 * s->mb_x + 1 + (block & 1); // x + y * wrap | |
0 | 580 |
245 | 581 mot_val = s->motion_val[xy]; |
0 | 582 |
583 /* 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
|
584 if (y == 1 || s->first_slice_line || s->first_gob_line) { |
245 | 585 A = s->motion_val[xy - 1]; |
0 | 586 *px = A[0]; |
587 *py = A[1]; | |
588 } else { | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
589 A = s->motion_val[xy - 1]; |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
590 B = s->motion_val[xy - wrap]; |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
591 C = s->motion_val[xy + off[block] - wrap]; |
0 | 592 *px = mid_pred(A[0], B[0], C[0]); |
593 *py = mid_pred(A[1], B[1], C[1]); | |
594 } | |
595 return mot_val; | |
596 } | |
597 | |
598 static void h263_encode_motion(MpegEncContext * s, int val) | |
599 { | |
600 int range, l, m, bit_size, sign, code, bits; | |
601 | |
602 if (val == 0) { | |
603 /* zero vector */ | |
604 code = 0; | |
605 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
606 } else { | |
607 bit_size = s->f_code - 1; | |
608 range = 1 << bit_size; | |
609 /* modulo encoding */ | |
610 l = range * 32; | |
611 m = 2 * l; | |
612 if (val < -l) { | |
613 val += m; | |
614 } else if (val >= l) { | |
615 val -= m; | |
616 } | |
617 | |
618 if (val >= 0) { | |
619 val--; | |
620 code = (val >> bit_size) + 1; | |
621 bits = val & (range - 1); | |
622 sign = 0; | |
623 } else { | |
624 val = -val; | |
625 val--; | |
626 code = (val >> bit_size) + 1; | |
627 bits = val & (range - 1); | |
628 sign = 1; | |
629 } | |
630 | |
631 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
632 if (bit_size > 0) { | |
633 put_bits(&s->pb, bit_size, bits); | |
634 } | |
635 } | |
636 } | |
637 | |
78 | 638 /* Encode MV differences on H.263+ with Unrestricted MV mode */ |
639 static void h263p_encode_umotion(MpegEncContext * s, int val) | |
640 { | |
641 short sval = 0; | |
642 short i = 0; | |
643 short n_bits = 0; | |
644 short temp_val; | |
645 int code = 0; | |
646 int tcode; | |
647 | |
648 if ( val == 0) | |
649 put_bits(&s->pb, 1, 1); | |
650 else if (val == 1) | |
651 put_bits(&s->pb, 3, 0); | |
652 else if (val == -1) | |
653 put_bits(&s->pb, 3, 2); | |
654 else { | |
655 | |
656 sval = ((val < 0) ? (short)(-val):(short)val); | |
657 temp_val = sval; | |
658 | |
659 while (temp_val != 0) { | |
660 temp_val = temp_val >> 1; | |
661 n_bits++; | |
662 } | |
663 | |
664 i = n_bits - 1; | |
665 while (i > 0) { | |
666 tcode = (sval & (1 << (i-1))) >> (i-1); | |
667 tcode = (tcode << 1) | 1; | |
668 code = (code << 2) | tcode; | |
669 i--; | |
670 } | |
671 code = ((code << 1) | (val < 0)) << 1; | |
672 put_bits(&s->pb, (2*n_bits)+1, code); | |
673 //printf("\nVal = %d\tCode = %d", sval, code); | |
674 } | |
675 } | |
676 | |
0 | 677 void h263_encode_init_vlc(MpegEncContext *s) |
678 { | |
679 static int done = 0; | |
680 | |
681 if (!done) { | |
682 done = 1; | |
683 init_rl(&rl_inter); | |
684 init_rl(&rl_intra); | |
685 } | |
686 } | |
687 | |
688 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
689 { | |
690 int level, run, last, i, j, last_index, last_non_zero, sign, slevel; | |
691 int code; | |
692 RLTable *rl = &rl_inter; | |
693 | |
694 if (s->mb_intra) { | |
231 | 695 /* DC coef */ |
696 level = block[0]; | |
0 | 697 /* 255 cannot be represented, so we clamp */ |
698 if (level > 254) { | |
699 level = 254; | |
700 block[0] = 254; | |
701 } | |
231 | 702 /* 0 cannot be represented also */ |
703 else if (!level) { | |
704 level = 1; | |
705 block[0] = 1; | |
706 } | |
707 if (level == 128) | |
708 put_bits(&s->pb, 8, 0xff); | |
709 else | |
710 put_bits(&s->pb, 8, level & 0xff); | |
711 i = 1; | |
0 | 712 } else { |
231 | 713 i = 0; |
0 | 714 } |
715 | |
716 /* AC coefs */ | |
717 last_index = s->block_last_index[n]; | |
718 last_non_zero = i - 1; | |
719 for (; i <= last_index; i++) { | |
720 j = zigzag_direct[i]; | |
721 level = block[j]; | |
722 if (level) { | |
723 run = i - last_non_zero - 1; | |
724 last = (i == last_index); | |
725 sign = 0; | |
726 slevel = level; | |
727 if (level < 0) { | |
728 sign = 1; | |
729 level = -level; | |
730 } | |
731 code = get_rl_index(rl, last, run, level); | |
732 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
733 if (code == rl->n) { | |
734 put_bits(&s->pb, 1, last); | |
735 put_bits(&s->pb, 6, run); | |
736 put_bits(&s->pb, 8, slevel & 0xff); | |
737 } else { | |
738 put_bits(&s->pb, 1, sign); | |
739 } | |
740 last_non_zero = i; | |
741 } | |
742 } | |
743 } | |
744 | |
745 /***************************************************/ | |
746 | |
262 | 747 static void mpeg4_stuffing(PutBitContext * pbc) |
748 { | |
749 int length; | |
750 put_bits(pbc, 1, 0); | |
751 length= (-get_bit_count(pbc))&7; | |
752 put_bits(pbc, length, (1<<length)-1); | |
753 } | |
754 | |
755 static void put_string(PutBitContext * pbc, char *s) | |
756 { | |
757 while(*s){ | |
758 put_bits(pbc, 8, *s); | |
759 s++; | |
760 } | |
761 put_bits(pbc, 8, 0); | |
762 } | |
763 | |
263 | 764 static void mpeg4_encode_vol_header(MpegEncContext * s) |
0 | 765 { |
263 | 766 int vo_ver_id=1; //must be 2 if we want GMC or q-pel |
767 | |
768 if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb); | |
769 put_bits(&s->pb, 16, 0); | |
770 put_bits(&s->pb, 16, 0x100); /* video obj */ | |
771 put_bits(&s->pb, 16, 0); | |
772 put_bits(&s->pb, 16, 0x120); /* video obj layer */ | |
773 | |
774 put_bits(&s->pb, 1, 0); /* random access vol */ | |
775 put_bits(&s->pb, 8, 1); /* video obj type indication= simple obj */ | |
776 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */ | |
777 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */ | |
778 put_bits(&s->pb, 3, 1); /* is obj layer priority */ | |
779 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */ //FIXME real aspect | |
780 put_bits(&s->pb, 1, 0); /* vol control parameters= no */ | |
781 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */ | |
782 put_bits(&s->pb, 1, 1); /* marker bit */ | |
783 put_bits(&s->pb, 16, s->time_increment_resolution=30000); | |
784 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; | |
785 if (s->time_increment_bits < 1) | |
786 s->time_increment_bits = 1; | |
787 put_bits(&s->pb, 1, 1); /* marker bit */ | |
788 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */ | |
789 put_bits(&s->pb, 1, 1); /* marker bit */ | |
790 put_bits(&s->pb, 13, s->width); /* vol width */ | |
791 put_bits(&s->pb, 1, 1); /* marker bit */ | |
792 put_bits(&s->pb, 13, s->height); /* vol height */ | |
793 put_bits(&s->pb, 1, 1); /* marker bit */ | |
794 put_bits(&s->pb, 1, 0); /* interlace */ | |
795 put_bits(&s->pb, 1, 1); /* obmc disable */ | |
796 if (vo_ver_id == 1) { | |
797 put_bits(&s->pb, 1, s->vol_sprite_usage=0); /* sprite enable */ | |
798 }else{ /* vo_ver_id == 2 */ | |
799 put_bits(&s->pb, 2, s->vol_sprite_usage=0); /* sprite enable */ | |
800 } | |
801 put_bits(&s->pb, 1, 0); /* not 8 bit */ | |
802 put_bits(&s->pb, 1, 0); /* quant type= h263 style*/ | |
803 if (vo_ver_id != 1) | |
804 put_bits(&s->pb, 1, s->quarter_sample=0); | |
805 put_bits(&s->pb, 1, 1); /* complexity estimation disable */ | |
806 put_bits(&s->pb, 1, 1); /* resync marker disable */ | |
807 put_bits(&s->pb, 1, 0); /* data partitioned */ | |
808 if (vo_ver_id != 1){ | |
809 put_bits(&s->pb, 1, 0); /* newpred */ | |
810 put_bits(&s->pb, 1, 0); /* reduced res vop */ | |
811 } | |
812 put_bits(&s->pb, 1, 0); /* scalability */ | |
813 | |
262 | 814 mpeg4_stuffing(&s->pb); |
815 put_bits(&s->pb, 16, 0); | |
816 put_bits(&s->pb, 16, 0x1B2); /* user_data */ | |
817 put_string(&s->pb, "ffmpeg"); //FIXME append some version ... | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
818 |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
819 s->no_rounding = 0; |
263 | 820 } |
821 | |
822 /* write mpeg4 VOP header */ | |
823 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number) | |
824 { | |
825 if(s->pict_type==I_TYPE) mpeg4_encode_vol_header(s); | |
826 | |
827 if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb); | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
828 put_bits(&s->pb, 16, 0); /* vop header */ |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
829 put_bits(&s->pb, 16, 0x1B6); /* vop header */ |
0 | 830 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */ |
831 /* XXX: time base + 1 not always correct */ | |
832 put_bits(&s->pb, 1, 1); | |
833 put_bits(&s->pb, 1, 0); | |
834 | |
835 put_bits(&s->pb, 1, 1); /* marker */ | |
263 | 836 put_bits(&s->pb, s->time_increment_bits, 1); /* XXX: correct time increment */ |
0 | 837 put_bits(&s->pb, 1, 1); /* marker */ |
838 put_bits(&s->pb, 1, 1); /* vop coded */ | |
263 | 839 if ( s->pict_type == P_TYPE |
840 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) { | |
841 s->no_rounding ^= 1; | |
0 | 842 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */ |
843 } | |
844 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */ | |
263 | 845 //FIXME sprite stuff |
0 | 846 |
847 put_bits(&s->pb, 5, s->qscale); | |
848 | |
849 if (s->pict_type != I_TYPE) | |
850 put_bits(&s->pb, 3, s->f_code); /* fcode_for */ | |
263 | 851 if (s->pict_type == B_TYPE) |
852 put_bits(&s->pb, 3, s->b_code); /* fcode_back */ | |
0 | 853 // printf("****frame %d\n", picture_number); |
854 } | |
855 | |
856 void h263_dc_scale(MpegEncContext * s) | |
857 { | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
858 #if 1 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
859 const static UINT8 y_tab[32]={ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
860 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
861 0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,36,38,40,42,44,46 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
862 }; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
863 const static UINT8 c_tab[32]={ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
864 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
865 0, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,20,21,22,23,24,25 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
866 }; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
867 s->y_dc_scale = y_tab[s->qscale]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
868 s->c_dc_scale = c_tab[s->qscale]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
869 #else |
0 | 870 int quant; |
871 quant = s->qscale; | |
872 /* luminance */ | |
873 if (quant < 5) | |
874 s->y_dc_scale = 8; | |
875 else if (quant > 4 && quant < 9) | |
876 s->y_dc_scale = (2 * quant); | |
877 else if (quant > 8 && quant < 25) | |
878 s->y_dc_scale = (quant + 8); | |
879 else | |
880 s->y_dc_scale = (2 * quant - 16); | |
881 /* chrominance */ | |
882 if (quant < 5) | |
883 s->c_dc_scale = 8; | |
884 else if (quant > 4 && quant < 25) | |
885 s->c_dc_scale = ((quant + 13) / 2); | |
886 else | |
887 s->c_dc_scale = (quant - 6); | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
888 #endif |
0 | 889 } |
890 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
891 static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr) |
0 | 892 { |
245 | 893 int a, b, c, xy, wrap, pred, scale; |
0 | 894 UINT16 *dc_val; |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
895 int dummy; |
0 | 896 |
897 /* find prediction */ | |
898 if (n < 4) { | |
899 wrap = s->mb_width * 2 + 2; | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
900 xy = 2 * s->mb_y + 1 + (n >> 1); |
245 | 901 xy *= wrap; |
902 xy += 2 * s->mb_x + 1 + (n & 1); | |
0 | 903 dc_val = s->dc_val[0]; |
904 scale = s->y_dc_scale; | |
905 } else { | |
906 wrap = s->mb_width + 2; | |
245 | 907 xy = s->mb_y + 1; |
908 xy *= wrap; | |
909 xy += s->mb_x + 1; | |
0 | 910 dc_val = s->dc_val[n - 4 + 1]; |
911 scale = s->c_dc_scale; | |
912 } | |
913 | |
914 /* B C | |
915 * A X | |
916 */ | |
245 | 917 a = dc_val[xy - 1]; |
918 b = dc_val[xy - 1 - wrap]; | |
919 c = dc_val[xy - wrap]; | |
0 | 920 |
921 if (abs(a - b) < abs(b - c)) { | |
922 pred = c; | |
923 *dir_ptr = 1; /* top */ | |
924 } else { | |
925 pred = a; | |
926 *dir_ptr = 0; /* left */ | |
927 } | |
928 /* we assume pred is positive */ | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
929 #ifdef ARCH_X86 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
930 asm volatile ( |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
931 "xorl %%edx, %%edx \n\t" |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
932 "mul %%ecx \n\t" |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
933 : "=d" (pred), "=a"(dummy) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
934 : "a" (pred + (scale >> 1)), "c" (inverse[scale]) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
935 ); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
936 #else |
0 | 937 pred = (pred + (scale >> 1)) / scale; |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
938 #endif |
0 | 939 |
940 /* prepare address for prediction update */ | |
245 | 941 *dc_val_ptr = &dc_val[xy]; |
0 | 942 |
943 return pred; | |
944 } | |
945 | |
174
ac5075a55488
new IDCT code by Michael Niedermayer (michaelni@gmx.at) - #define SIMPLE_IDCT to enable
arpi_esp
parents:
162
diff
changeset
|
946 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n, |
0 | 947 int dir) |
948 { | |
949 int x, y, wrap, i; | |
950 INT16 *ac_val, *ac_val1; | |
951 | |
952 /* find prediction */ | |
953 if (n < 4) { | |
954 x = 2 * s->mb_x + 1 + (n & 1); | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
955 y = 2 * s->mb_y + 1 + (n >> 1); |
0 | 956 wrap = s->mb_width * 2 + 2; |
957 ac_val = s->ac_val[0][0]; | |
958 } else { | |
959 x = s->mb_x + 1; | |
960 y = s->mb_y + 1; | |
961 wrap = s->mb_width + 2; | |
962 ac_val = s->ac_val[n - 4 + 1][0]; | |
963 } | |
964 ac_val += ((y) * wrap + (x)) * 16; | |
965 ac_val1 = ac_val; | |
966 if (s->ac_pred) { | |
967 if (dir == 0) { | |
968 /* left prediction */ | |
969 ac_val -= 16; | |
970 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
|
971 block[block_permute_op(i*8)] += ac_val[i]; |
0 | 972 } |
973 } else { | |
974 /* top prediction */ | |
975 ac_val -= 16 * wrap; | |
976 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
|
977 block[block_permute_op(i)] += ac_val[i + 8]; |
0 | 978 } |
979 } | |
980 } | |
981 /* left copy */ | |
982 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
|
983 ac_val1[i] = block[block_permute_op(i * 8)]; |
0 | 984 /* top copy */ |
985 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
|
986 ac_val1[8 + i] = block[block_permute_op(i)]; |
0 | 987 } |
988 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
989 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n, |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
990 int dir) |
0 | 991 { |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
992 int x, y, wrap, i; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
993 INT16 *ac_val; |
0 | 994 |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
995 /* find prediction */ |
0 | 996 if (n < 4) { |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
997 x = 2 * s->mb_x + 1 + (n & 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
998 y = 2 * s->mb_y + 1 + (n >> 1); |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
999 wrap = s->mb_width * 2 + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1000 ac_val = s->ac_val[0][0]; |
0 | 1001 } else { |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1002 x = s->mb_x + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1003 y = s->mb_y + 1; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1004 wrap = s->mb_width + 2; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1005 ac_val = s->ac_val[n - 4 + 1][0]; |
0 | 1006 } |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1007 ac_val += ((y) * wrap + (x)) * 16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1008 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1009 if (dir == 0) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1010 /* left prediction */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1011 ac_val -= 16; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1012 for(i=1;i<8;i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1013 block[block_permute_op(i*8)] -= ac_val[i]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1014 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1015 } else { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1016 /* top prediction */ |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1017 ac_val -= 16 * wrap; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1018 for(i=1;i<8;i++) { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1019 block[block_permute_op(i)] -= ac_val[i + 8]; |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1020 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1021 } |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1022 } |
0 | 1023 |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1024 |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1025 static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1026 { |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1027 int size, v; |
0 | 1028 /* find number of bits */ |
1029 size = 0; | |
1030 v = abs(level); | |
1031 while (v) { | |
1032 v >>= 1; | |
1033 size++; | |
1034 } | |
1035 | |
1036 if (n < 4) { | |
1037 /* luminance */ | |
1038 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]); | |
1039 } else { | |
1040 /* chrominance */ | |
1041 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]); | |
1042 } | |
1043 | |
1044 /* encode remaining bits */ | |
1045 if (size > 0) { | |
1046 if (level < 0) | |
1047 level = (-level) ^ ((1 << size) - 1); | |
1048 put_bits(&s->pb, size, level); | |
1049 if (size > 8) | |
1050 put_bits(&s->pb, 1, 1); | |
1051 } | |
1052 } | |
1053 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1054 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, UINT8 *scan_table) |
0 | 1055 { |
1056 int level, run, last, i, j, last_index, last_non_zero, sign, slevel; | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1057 int code; |
0 | 1058 const RLTable *rl; |
1059 | |
1060 if (s->mb_intra) { | |
1061 /* mpeg4 based DC predictor */ | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1062 mpeg4_encode_dc(s, intra_dc, n); |
0 | 1063 i = 1; |
1064 rl = &rl_intra; | |
1065 } else { | |
1066 i = 0; | |
1067 rl = &rl_inter; | |
1068 } | |
1069 | |
1070 /* AC coefs */ | |
1071 last_index = s->block_last_index[n]; | |
1072 last_non_zero = i - 1; | |
1073 for (; i <= last_index; i++) { | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1074 j = scan_table[i]; |
0 | 1075 level = block[j]; |
1076 if (level) { | |
1077 run = i - last_non_zero - 1; | |
1078 last = (i == last_index); | |
1079 sign = 0; | |
1080 slevel = level; | |
1081 if (level < 0) { | |
1082 sign = 1; | |
1083 level = -level; | |
1084 } | |
1085 code = get_rl_index(rl, last, run, level); | |
1086 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1087 if (code == rl->n) { | |
1088 int level1, run1; | |
1089 level1 = level - rl->max_level[last][run]; | |
1090 if (level1 < 1) | |
1091 goto esc2; | |
1092 code = get_rl_index(rl, last, run, level1); | |
1093 if (code == rl->n) { | |
1094 esc2: | |
1095 put_bits(&s->pb, 1, 1); | |
1096 if (level > MAX_LEVEL) | |
1097 goto esc3; | |
1098 run1 = run - rl->max_run[last][level] - 1; | |
1099 if (run1 < 0) | |
1100 goto esc3; | |
1101 code = get_rl_index(rl, last, run1, level); | |
1102 if (code == rl->n) { | |
1103 esc3: | |
1104 /* third escape */ | |
1105 put_bits(&s->pb, 1, 1); | |
1106 put_bits(&s->pb, 1, last); | |
1107 put_bits(&s->pb, 6, run); | |
1108 put_bits(&s->pb, 1, 1); | |
1109 put_bits(&s->pb, 12, slevel & 0xfff); | |
1110 put_bits(&s->pb, 1, 1); | |
1111 } else { | |
1112 /* second escape */ | |
1113 put_bits(&s->pb, 1, 0); | |
1114 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1115 put_bits(&s->pb, 1, sign); | |
1116 } | |
1117 } else { | |
1118 /* first escape */ | |
1119 put_bits(&s->pb, 1, 0); | |
1120 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1121 put_bits(&s->pb, 1, sign); | |
1122 } | |
1123 } else { | |
1124 put_bits(&s->pb, 1, sign); | |
1125 } | |
1126 last_non_zero = i; | |
1127 } | |
1128 } | |
1129 } | |
1130 | |
1131 | |
1132 | |
1133 /***********************************************/ | |
1134 /* decoding */ | |
1135 | |
1136 static VLC intra_MCBPC_vlc; | |
1137 static VLC inter_MCBPC_vlc; | |
1138 static VLC cbpy_vlc; | |
1139 static VLC mv_vlc; | |
1140 static VLC dc_lum, dc_chrom; | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1141 static VLC sprite_trajectory; |
262 | 1142 static VLC mb_type_b_vlc; |
0 | 1143 |
1144 void init_rl(RLTable *rl) | |
1145 { | |
1146 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1]; | |
1147 UINT8 index_run[MAX_RUN+1]; | |
1148 int last, run, level, start, end, i; | |
1149 | |
1150 /* compute max_level[], max_run[] and index_run[] */ | |
1151 for(last=0;last<2;last++) { | |
1152 if (last == 0) { | |
1153 start = 0; | |
1154 end = rl->last; | |
1155 } else { | |
1156 start = rl->last; | |
1157 end = rl->n; | |
1158 } | |
1159 | |
1160 memset(max_level, 0, MAX_RUN + 1); | |
1161 memset(max_run, 0, MAX_LEVEL + 1); | |
1162 memset(index_run, rl->n, MAX_RUN + 1); | |
1163 for(i=start;i<end;i++) { | |
1164 run = rl->table_run[i]; | |
1165 level = rl->table_level[i]; | |
1166 if (index_run[run] == rl->n) | |
1167 index_run[run] = i; | |
1168 if (level > max_level[run]) | |
1169 max_level[run] = level; | |
1170 if (run > max_run[level]) | |
1171 max_run[level] = run; | |
1172 } | |
1173 rl->max_level[last] = malloc(MAX_RUN + 1); | |
1174 memcpy(rl->max_level[last], max_level, MAX_RUN + 1); | |
1175 rl->max_run[last] = malloc(MAX_LEVEL + 1); | |
1176 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1); | |
1177 rl->index_run[last] = malloc(MAX_RUN + 1); | |
1178 memcpy(rl->index_run[last], index_run, MAX_RUN + 1); | |
1179 } | |
1180 } | |
1181 | |
1182 void init_vlc_rl(RLTable *rl) | |
1183 { | |
1184 init_vlc(&rl->vlc, 9, rl->n + 1, | |
1185 &rl->table_vlc[0][1], 4, 2, | |
1186 &rl->table_vlc[0][0], 4, 2); | |
1187 } | |
1188 | |
1189 /* init vlcs */ | |
1190 | |
1191 /* XXX: find a better solution to handle static init */ | |
1192 void h263_decode_init_vlc(MpegEncContext *s) | |
1193 { | |
1194 static int done = 0; | |
1195 | |
1196 if (!done) { | |
1197 done = 1; | |
1198 | |
1199 init_vlc(&intra_MCBPC_vlc, 6, 8, | |
1200 intra_MCBPC_bits, 1, 1, | |
1201 intra_MCBPC_code, 1, 1); | |
161 | 1202 init_vlc(&inter_MCBPC_vlc, 9, 25, |
0 | 1203 inter_MCBPC_bits, 1, 1, |
1204 inter_MCBPC_code, 1, 1); | |
1205 init_vlc(&cbpy_vlc, 6, 16, | |
1206 &cbpy_tab[0][1], 2, 1, | |
1207 &cbpy_tab[0][0], 2, 1); | |
1208 init_vlc(&mv_vlc, 9, 33, | |
1209 &mvtab[0][1], 2, 1, | |
1210 &mvtab[0][0], 2, 1); | |
1211 init_rl(&rl_inter); | |
1212 init_rl(&rl_intra); | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1213 init_rl(&rl_intra_aic); |
0 | 1214 init_vlc_rl(&rl_inter); |
1215 init_vlc_rl(&rl_intra); | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1216 init_vlc_rl(&rl_intra_aic); |
0 | 1217 init_vlc(&dc_lum, 9, 13, |
1218 &DCtab_lum[0][1], 2, 1, | |
1219 &DCtab_lum[0][0], 2, 1); | |
1220 init_vlc(&dc_chrom, 9, 13, | |
1221 &DCtab_chrom[0][1], 2, 1, | |
1222 &DCtab_chrom[0][0], 2, 1); | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1223 init_vlc(&sprite_trajectory, 9, 15, |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1224 &sprite_trajectory_tab[0][1], 4, 2, |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1225 &sprite_trajectory_tab[0][0], 4, 2); |
262 | 1226 init_vlc(&mb_type_b_vlc, 4, 4, |
1227 &mb_type_b_tab[0][1], 2, 1, | |
1228 &mb_type_b_tab[0][0], 2, 1); | |
0 | 1229 } |
1230 } | |
1231 | |
162 | 1232 int h263_decode_gob_header(MpegEncContext *s) |
1233 { | |
1234 unsigned int val, gfid; | |
1235 | |
1236 /* Check for GOB Start Code */ | |
1237 val = show_bits(&s->gb, 16); | |
1238 if (val == 0) { | |
1239 /* We have a GBSC probably with GSTUFF */ | |
1240 skip_bits(&s->gb, 16); /* Drop the zeros */ | |
1241 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */ | |
1242 #ifdef DEBUG | |
1243 fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
1244 #endif | |
1245 s->gob_number = get_bits(&s->gb, 5); /* GN */ | |
1246 gfid = get_bits(&s->gb, 2); /* GFID */ | |
1247 s->qscale = get_bits(&s->gb, 5); /* GQUANT */ | |
1248 #ifdef DEBUG | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
231
diff
changeset
|
1249 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale); |
162 | 1250 #endif |
1251 return 1; | |
1252 } | |
1253 return 0; | |
1254 | |
1255 } | |
1256 | |
0 | 1257 int h263_decode_mb(MpegEncContext *s, |
1258 DCTELEM block[6][64]) | |
1259 { | |
1260 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | |
1261 INT16 *mot_val; | |
110 | 1262 static INT8 quant_tab[4] = { -1, -2, 1, 2 }; |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
1263 |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1264 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) { |
21 | 1265 if (get_bits1(&s->gb)) { |
0 | 1266 /* skip mb */ |
1267 s->mb_intra = 0; | |
1268 for(i=0;i<6;i++) | |
1269 s->block_last_index[i] = -1; | |
1270 s->mv_dir = MV_DIR_FORWARD; | |
1271 s->mv_type = MV_TYPE_16X16; | |
255 | 1272 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){ |
1273 const int a= s->sprite_warping_accuracy; | |
1274 // int l = (1 << (s->f_code - 1)) * 32; | |
1275 | |
1276 s->mcsel=1; | |
1277 s->mv[0][0][0] = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample); | |
1278 s->mv[0][0][1] = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample); | |
1279 /* if (s->mv[0][0][0] < -l) s->mv[0][0][0]= -l; | |
1280 else if (s->mv[0][0][0] >= l) s->mv[0][0][0]= l-1; | |
1281 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l; | |
1282 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/ | |
1283 | |
1284 s->mb_skiped = 0; | |
1285 }else{ | |
1286 s->mcsel=0; | |
1287 s->mv[0][0][0] = 0; | |
1288 s->mv[0][0][1] = 0; | |
1289 s->mb_skiped = 1; | |
1290 } | |
0 | 1291 return 0; |
1292 } | |
1293 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc); | |
144 | 1294 //fprintf(stderr, "\tCBPC: %d", cbpc); |
0 | 1295 if (cbpc < 0) |
1296 return -1; | |
161 | 1297 if (cbpc > 20) |
1298 cbpc+=3; | |
1299 else if (cbpc == 20) | |
1300 fprintf(stderr, "Stuffing !"); | |
144 | 1301 |
0 | 1302 dquant = cbpc & 8; |
1303 s->mb_intra = ((cbpc & 4) != 0); | |
262 | 1304 if (s->mb_intra) goto intra; |
1305 | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1306 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1307 s->mcsel= get_bits1(&s->gb); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1308 else s->mcsel= 0; |
0 | 1309 cbpy = get_vlc(&s->gb, &cbpy_vlc); |
1310 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2); | |
1311 if (dquant) { | |
1312 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | |
1313 if (s->qscale < 1) | |
1314 s->qscale = 1; | |
1315 else if (s->qscale > 31) | |
1316 s->qscale = 31; | |
1317 } | |
1318 s->mv_dir = MV_DIR_FORWARD; | |
1319 if ((cbpc & 16) == 0) { | |
1320 /* 16x16 motion prediction */ | |
1321 s->mv_type = MV_TYPE_16X16; | |
1322 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
|
1323 if (s->umvplus_dec) |
78 | 1324 mx = h263p_decode_umotion(s, pred_x); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1325 else if(!s->mcsel) |
262 | 1326 mx = h263_decode_motion(s, pred_x, s->f_code); |
255 | 1327 else { |
1328 const int a= s->sprite_warping_accuracy; | |
1329 // int l = (1 << (s->f_code - 1)) * 32; | |
1330 mx= RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample); | |
1331 // if (mx < -l) mx= -l; | |
1332 // else if (mx >= l) mx= l-1; | |
1333 } | |
0 | 1334 if (mx >= 0xffff) |
1335 return -1; | |
78 | 1336 |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
1337 if (s->umvplus_dec) |
78 | 1338 my = h263p_decode_umotion(s, pred_y); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1339 else if(!s->mcsel) |
262 | 1340 my = h263_decode_motion(s, pred_y, s->f_code); |
255 | 1341 else{ |
1342 const int a= s->sprite_warping_accuracy; | |
1343 // int l = (1 << (s->f_code - 1)) * 32; | |
1344 my= RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample); | |
1345 // if (my < -l) my= -l; | |
1346 // else if (my >= l) my= l-1; | |
1347 } | |
0 | 1348 if (my >= 0xffff) |
1349 return -1; | |
1350 s->mv[0][0][0] = mx; | |
1351 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
|
1352 /*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
|
1353 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
|
1354 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
|
1355 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1) |
78 | 1356 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ |
1357 | |
0 | 1358 } else { |
1359 s->mv_type = MV_TYPE_8X8; | |
1360 for(i=0;i<4;i++) { | |
1361 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
|
1362 if (s->umvplus_dec) |
78 | 1363 mx = h263p_decode_umotion(s, pred_x); |
1364 else | |
262 | 1365 mx = h263_decode_motion(s, pred_x, s->f_code); |
0 | 1366 if (mx >= 0xffff) |
1367 return -1; | |
78 | 1368 |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
1369 if (s->umvplus_dec) |
78 | 1370 my = h263p_decode_umotion(s, pred_y); |
1371 else | |
262 | 1372 my = h263_decode_motion(s, pred_y, s->f_code); |
0 | 1373 if (my >= 0xffff) |
1374 return -1; | |
1375 s->mv[0][i][0] = mx; | |
1376 s->mv[0][i][1] = my; | |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
1377 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1) |
78 | 1378 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ |
0 | 1379 mot_val[0] = mx; |
1380 mot_val[1] = my; | |
1381 } | |
1382 } | |
262 | 1383 } else if(s->pict_type==B_TYPE) { |
1384 int modb1; // first bit of modb | |
1385 int modb2; // second bit of modb | |
1386 int mb_type; | |
1387 int time_pp; | |
1388 int time_pb; | |
1389 int xy; | |
1390 | |
1391 s->mb_intra = 0; //B-frames never contain intra blocks | |
1392 s->mcsel=0; // ... true gmc blocks | |
1393 | |
1394 if(s->mb_x==0){ | |
1395 s->last_mv[0][0][0]= | |
1396 s->last_mv[0][0][1]= | |
1397 s->last_mv[1][0][0]= | |
1398 s->last_mv[1][0][1]= 0; | |
1399 } | |
1400 | |
1401 /* if we skipped it in the future P Frame than skip it now too */ | |
1402 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC | |
1403 | |
1404 if(s->mb_skiped){ | |
1405 /* skip mb */ | |
1406 for(i=0;i<6;i++) | |
1407 s->block_last_index[i] = -1; | |
1408 | |
1409 s->mv_dir = MV_DIR_FORWARD; | |
1410 s->mv_type = MV_TYPE_16X16; | |
1411 s->mv[0][0][0] = 0; | |
1412 s->mv[0][0][1] = 0; | |
1413 s->mv[1][0][0] = 0; | |
1414 s->mv[1][0][1] = 0; | |
1415 s->last_mv[0][0][0]= | |
1416 s->last_mv[0][0][1]= | |
1417 s->last_mv[1][0][0]= | |
1418 s->last_mv[1][0][1]= 0; | |
1419 s->mb_skiped = 1; | |
1420 return 0; | |
1421 } | |
1422 | |
1423 modb1= get_bits1(&s->gb); | |
1424 if(modb1==0){ | |
1425 modb2= get_bits1(&s->gb); | |
1426 mb_type= get_vlc(&s->gb, &mb_type_b_vlc); | |
1427 if(modb2==0) cbp= get_bits(&s->gb, 6); | |
1428 else cbp=0; | |
1429 if (mb_type && cbp) { | |
1430 if(get_bits1(&s->gb)){ | |
1431 s->qscale +=get_bits1(&s->gb)*4 - 2; | |
1432 if (s->qscale < 1) | |
1433 s->qscale = 1; | |
1434 else if (s->qscale > 31) | |
1435 s->qscale = 31; | |
1436 } | |
1437 } | |
1438 }else{ | |
1439 mb_type=4; //like 0 but no vectors coded | |
1440 cbp=0; | |
1441 } | |
1442 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here | |
1443 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my | |
1444 switch(mb_type) | |
1445 { | |
1446 case 0: | |
1447 mx = h263_decode_motion(s, 0, 1); | |
1448 my = h263_decode_motion(s, 0, 1); | |
1449 case 4: | |
1450 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
1451 xy= (2*s->mb_width + 2)*(2*s->mb_y + 1) + 2*s->mb_x + 1; | |
1452 time_pp= s->last_non_b_time[0] - s->last_non_b_time[1]; | |
1453 time_pb= s->time - s->last_non_b_time[1]; | |
1454 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb); | |
1455 //FIXME 4MV | |
1456 //FIXME avoid divides | |
1457 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; | |
1458 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my; | |
1459 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0] | |
1460 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx; | |
1461 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] | |
1462 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my; | |
1463 /* s->mv[0][0][0] = | |
1464 s->mv[0][0][1] = | |
1465 s->mv[1][0][0] = | |
1466 s->mv[1][0][1] = 1000;*/ | |
1467 break; | |
1468 case 1: | |
1469 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
1470 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
1471 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
1472 s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
1473 s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
1474 | |
1475 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
1476 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
1477 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
1478 s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
1479 break; | |
1480 case 2: | |
1481 s->mv_dir = MV_DIR_BACKWARD; | |
1482 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
1483 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
1484 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
1485 s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
1486 break; | |
1487 case 3: | |
1488 s->mv_dir = MV_DIR_FORWARD; | |
1489 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
1490 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
1491 s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
1492 s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
1493 break; | |
1494 default: return -1; | |
1495 } | |
1496 } else { /* I-Frame */ | |
1497 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc); | |
1498 if (cbpc < 0) | |
1499 return -1; | |
1500 dquant = cbpc & 4; | |
1501 s->mb_intra = 1; | |
1502 intra: | |
0 | 1503 s->ac_pred = 0; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1504 if (s->h263_pred || s->h263_aic) { |
21 | 1505 s->ac_pred = get_bits1(&s->gb); |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1506 if (s->ac_pred && s->h263_aic) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1507 s->h263_aic_dir = get_bits1(&s->gb); |
0 | 1508 } |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1509 if (s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1510 s->y_dc_scale = 2 * s->qscale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1511 s->c_dc_scale = 2 * s->qscale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1512 } |
0 | 1513 cbpy = get_vlc(&s->gb, &cbpy_vlc); |
1514 cbp = (cbpc & 3) | (cbpy << 2); | |
1515 if (dquant) { | |
1516 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | |
1517 if (s->qscale < 1) | |
1518 s->qscale = 1; | |
1519 else if (s->qscale > 31) | |
1520 s->qscale = 31; | |
1521 } | |
1522 } | |
1523 | |
1524 /* decode each block */ | |
1525 if (s->h263_pred) { | |
1526 for (i = 0; i < 6; i++) { | |
1527 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
1528 return -1; | |
1529 } | |
1530 } else { | |
1531 for (i = 0; i < 6; i++) { | |
1532 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
1533 return -1; | |
1534 } | |
1535 } | |
1536 return 0; | |
1537 } | |
1538 | |
262 | 1539 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) |
0 | 1540 { |
1541 int code, val, sign, shift, l, m; | |
1542 | |
1543 code = get_vlc(&s->gb, &mv_vlc); | |
1544 if (code < 0) | |
1545 return 0xffff; | |
1546 | |
1547 if (code == 0) | |
1548 return pred; | |
21 | 1549 sign = get_bits1(&s->gb); |
262 | 1550 shift = f_code - 1; |
0 | 1551 val = (code - 1) << shift; |
1552 if (shift > 0) | |
1553 val |= get_bits(&s->gb, shift); | |
1554 val++; | |
1555 if (sign) | |
1556 val = -val; | |
1557 val += pred; | |
1558 | |
1559 /* modulo decoding */ | |
1560 if (!s->h263_long_vectors) { | |
262 | 1561 l = (1 << (f_code - 1)) * 32; |
0 | 1562 m = 2 * l; |
1563 if (val < -l) { | |
1564 val += m; | |
1565 } else if (val >= l) { | |
1566 val -= m; | |
1567 } | |
1568 } else { | |
1569 /* horrible h263 long vector mode */ | |
1570 if (pred < -31 && val < -63) | |
1571 val += 64; | |
1572 if (pred > 32 && val > 63) | |
1573 val -= 64; | |
154
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
1574 |
0 | 1575 } |
1576 return val; | |
1577 } | |
1578 | |
78 | 1579 /* Decodes RVLC of H.263+ UMV */ |
1580 static int h263p_decode_umotion(MpegEncContext * s, int pred) | |
1581 { | |
1582 int code = 0, sign; | |
1583 | |
1584 if (get_bits1(&s->gb)) /* Motion difference = 0 */ | |
1585 return pred; | |
1586 | |
1587 code = 2 + get_bits1(&s->gb); | |
1588 | |
1589 while (get_bits1(&s->gb)) | |
1590 { | |
1591 code <<= 1; | |
1592 code += get_bits1(&s->gb); | |
1593 } | |
1594 sign = code & 1; | |
1595 code >>= 1; | |
1596 | |
1597 code = (sign) ? (pred - code) : (pred + code); | |
1598 #ifdef DEBUG | |
1599 fprintf(stderr,"H.263+ UMV Motion = %d\n", code); | |
1600 #endif | |
1601 return code; | |
1602 | |
1603 } | |
1604 | |
0 | 1605 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, |
1606 int n, int coded) | |
1607 { | |
1608 int code, level, i, j, last, run; | |
1609 RLTable *rl = &rl_inter; | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1610 const UINT8 *scan_table; |
0 | 1611 |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1612 scan_table = zigzag_direct; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1613 if (s->h263_aic && s->mb_intra) { |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1614 rl = &rl_intra_aic; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1615 i = 0; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1616 if (s->ac_pred) { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1617 if (s->h263_aic_dir) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1618 scan_table = ff_alternate_vertical_scan; /* left */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1619 else |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1620 scan_table = ff_alternate_horizontal_scan; /* top */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1621 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1622 } else if (s->mb_intra) { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1623 /* DC coef */ |
0 | 1624 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) { |
1625 int component, diff; | |
1626 component = (n <= 3 ? 0 : n - 4 + 1); | |
1627 level = s->last_dc[component]; | |
1628 if (s->rv10_first_dc_coded[component]) { | |
1629 diff = rv_decode_dc(s, n); | |
1630 if (diff == 0xffff) | |
1631 return -1; | |
1632 level += diff; | |
1633 level = level & 0xff; /* handle wrap round */ | |
1634 s->last_dc[component] = level; | |
1635 } else { | |
1636 s->rv10_first_dc_coded[component] = 1; | |
1637 } | |
1638 } else { | |
1639 level = get_bits(&s->gb, 8); | |
1640 if (level == 255) | |
1641 level = 128; | |
1642 } | |
1643 block[0] = level; | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1644 i = 1; |
0 | 1645 } else { |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1646 i = 0; |
0 | 1647 } |
1648 if (!coded) { | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1649 if (s->mb_intra && s->h263_aic) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1650 goto not_coded; |
0 | 1651 s->block_last_index[n] = i - 1; |
1652 return 0; | |
1653 } | |
1654 | |
1655 for(;;) { | |
1656 code = get_vlc(&s->gb, &rl->vlc); | |
1657 if (code < 0) | |
1658 return -1; | |
1659 if (code == rl->n) { | |
1660 /* escape */ | |
21 | 1661 last = get_bits1(&s->gb); |
0 | 1662 run = get_bits(&s->gb, 6); |
1663 level = (INT8)get_bits(&s->gb, 8); | |
1664 if (s->h263_rv10 && level == -128) { | |
1665 /* XXX: should patch encoder too */ | |
1666 level = get_bits(&s->gb, 12); | |
1667 level = (level << 20) >> 20; | |
1668 } | |
1669 } else { | |
1670 run = rl->table_run[code]; | |
1671 level = rl->table_level[code]; | |
1672 last = code >= rl->last; | |
21 | 1673 if (get_bits1(&s->gb)) |
0 | 1674 level = -level; |
1675 } | |
1676 i += run; | |
1677 if (i >= 64) | |
1678 return -1; | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1679 j = scan_table[i]; |
0 | 1680 block[j] = level; |
1681 if (last) | |
1682 break; | |
1683 i++; | |
1684 } | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1685 not_coded: |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1686 if (s->mb_intra && s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1687 h263_pred_acdc(s, block, n); |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
264
diff
changeset
|
1688 i = 63; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1689 } |
0 | 1690 s->block_last_index[n] = i; |
1691 return 0; | |
1692 } | |
1693 | |
1694 static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1695 { | |
1696 int level, pred, code; | |
1697 UINT16 *dc_val; | |
1698 | |
1699 if (n < 4) | |
1700 code = get_vlc(&s->gb, &dc_lum); | |
1701 else | |
1702 code = get_vlc(&s->gb, &dc_chrom); | |
1703 if (code < 0) | |
1704 return -1; | |
1705 if (code == 0) { | |
1706 level = 0; | |
1707 } else { | |
1708 level = get_bits(&s->gb, code); | |
1709 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/ | |
1710 level = - (level ^ ((1 << code) - 1)); | |
1711 if (code > 8) | |
21 | 1712 skip_bits1(&s->gb); /* marker */ |
0 | 1713 } |
1714 | |
1715 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1716 level += pred; | |
1717 if (level < 0) | |
1718 level = 0; | |
1719 if (n < 4) { | |
1720 *dc_val = level * s->y_dc_scale; | |
1721 } else { | |
1722 *dc_val = level * s->c_dc_scale; | |
1723 } | |
1724 return level; | |
1725 } | |
1726 | |
1727 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
1728 int n, int coded) | |
1729 { | |
1730 int code, level, i, j, last, run; | |
1731 int dc_pred_dir; | |
1732 RLTable *rl; | |
1733 const UINT8 *scan_table; | |
1734 | |
1735 if (s->mb_intra) { | |
1736 /* DC coef */ | |
1737 level = mpeg4_decode_dc(s, n, &dc_pred_dir); | |
1738 if (level < 0) | |
1739 return -1; | |
1740 block[0] = level; | |
1741 i = 1; | |
1742 if (!coded) | |
1743 goto not_coded; | |
1744 rl = &rl_intra; | |
1745 if (s->ac_pred) { | |
1746 if (dc_pred_dir == 0) | |
1747 scan_table = ff_alternate_vertical_scan; /* left */ | |
1748 else | |
1749 scan_table = ff_alternate_horizontal_scan; /* top */ | |
1750 } else { | |
1751 scan_table = zigzag_direct; | |
1752 } | |
1753 } else { | |
1754 i = 0; | |
1755 if (!coded) { | |
1756 s->block_last_index[n] = i - 1; | |
1757 return 0; | |
1758 } | |
1759 rl = &rl_inter; | |
1760 scan_table = zigzag_direct; | |
1761 } | |
1762 | |
1763 for(;;) { | |
1764 code = get_vlc(&s->gb, &rl->vlc); | |
1765 if (code < 0) | |
1766 return -1; | |
1767 if (code == rl->n) { | |
1768 /* escape */ | |
21 | 1769 if (get_bits1(&s->gb) != 0) { |
1770 if (get_bits1(&s->gb) != 0) { | |
0 | 1771 /* third escape */ |
21 | 1772 last = get_bits1(&s->gb); |
0 | 1773 run = get_bits(&s->gb, 6); |
21 | 1774 get_bits1(&s->gb); /* marker */ |
0 | 1775 level = get_bits(&s->gb, 12); |
1776 level = (level << 20) >> 20; /* sign extend */ | |
21 | 1777 skip_bits1(&s->gb); /* marker */ |
0 | 1778 } else { |
1779 /* second escape */ | |
1780 code = get_vlc(&s->gb, &rl->vlc); | |
1781 if (code < 0 || code >= rl->n) | |
1782 return -1; | |
1783 run = rl->table_run[code]; | |
1784 level = rl->table_level[code]; | |
1785 last = code >= rl->last; | |
1786 run += rl->max_run[last][level] + 1; | |
21 | 1787 if (get_bits1(&s->gb)) |
0 | 1788 level = -level; |
1789 } | |
1790 } else { | |
1791 /* first escape */ | |
1792 code = get_vlc(&s->gb, &rl->vlc); | |
1793 if (code < 0 || code >= rl->n) | |
1794 return -1; | |
1795 run = rl->table_run[code]; | |
1796 level = rl->table_level[code]; | |
1797 last = code >= rl->last; | |
1798 level += rl->max_level[last][run]; | |
21 | 1799 if (get_bits1(&s->gb)) |
0 | 1800 level = -level; |
1801 } | |
1802 } else { | |
1803 run = rl->table_run[code]; | |
1804 level = rl->table_level[code]; | |
1805 last = code >= rl->last; | |
21 | 1806 if (get_bits1(&s->gb)) |
0 | 1807 level = -level; |
1808 } | |
1809 i += run; | |
1810 if (i >= 64) | |
1811 return -1; | |
1812 j = scan_table[i]; | |
1813 block[j] = level; | |
1814 i++; | |
1815 if (last) | |
1816 break; | |
1817 } | |
1818 not_coded: | |
1819 if (s->mb_intra) { | |
1820 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
1821 if (s->ac_pred) { | |
1822 i = 64; /* XXX: not optimal */ | |
1823 } | |
1824 } | |
1825 s->block_last_index[n] = i - 1; | |
1826 return 0; | |
1827 } | |
1828 | |
1829 /* most is hardcoded. should extend to handle all h263 streams */ | |
1830 int h263_decode_picture_header(MpegEncContext *s) | |
1831 { | |
1832 int format, width, height; | |
1833 | |
1834 /* picture header */ | |
1835 if (get_bits(&s->gb, 22) != 0x20) | |
1836 return -1; | |
231 | 1837 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */ |
1838 | |
21 | 1839 if (get_bits1(&s->gb) != 1) |
0 | 1840 return -1; /* marker */ |
21 | 1841 if (get_bits1(&s->gb) != 0) |
0 | 1842 return -1; /* h263 id */ |
21 | 1843 skip_bits1(&s->gb); /* split screen off */ |
1844 skip_bits1(&s->gb); /* camera off */ | |
1845 skip_bits1(&s->gb); /* freeze picture release off */ | |
0 | 1846 |
155
3c3449bce692
- Bug fix on MV prediction for MPEG4 caused by new H.263 GOB code.
pulento
parents:
154
diff
changeset
|
1847 /* Reset GOB number */ |
154
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
1848 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
|
1849 |
0 | 1850 format = get_bits(&s->gb, 3); |
1851 | |
161 | 1852 if (format != 7 && format != 6) { |
0 | 1853 s->h263_plus = 0; |
1854 /* H.263v1 */ | |
1855 width = h263_format[format][0]; | |
1856 height = h263_format[format][1]; | |
1857 if (!width) | |
1858 return -1; | |
161 | 1859 |
1860 s->width = width; | |
1861 s->height = height; | |
21 | 1862 s->pict_type = I_TYPE + get_bits1(&s->gb); |
0 | 1863 |
21 | 1864 s->unrestricted_mv = get_bits1(&s->gb); |
0 | 1865 s->h263_long_vectors = s->unrestricted_mv; |
1866 | |
21 | 1867 if (get_bits1(&s->gb) != 0) |
0 | 1868 return -1; /* SAC: off */ |
161 | 1869 if (get_bits1(&s->gb) != 0) { |
1870 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */ | |
1871 } | |
1872 | |
21 | 1873 if (get_bits1(&s->gb) != 0) |
0 | 1874 return -1; /* not PB frame */ |
1875 | |
1876 s->qscale = get_bits(&s->gb, 5); | |
21 | 1877 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ |
0 | 1878 } else { |
161 | 1879 int ufep; |
1880 | |
0 | 1881 /* H.263v2 */ |
161 | 1882 s->h263_plus = 1; |
1883 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */ | |
1884 | |
1885 if (ufep == 1) { | |
1886 /* OPPTYPE */ | |
1887 format = get_bits(&s->gb, 3); | |
1888 skip_bits(&s->gb,1); /* Custom PCF */ | |
1889 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */ | |
1890 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */ | |
1891 if (get_bits1(&s->gb) != 0) { | |
1892 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */ | |
1893 } | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1894 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */ |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1895 s->h263_aic = 1; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1896 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
245
diff
changeset
|
1897 skip_bits(&s->gb, 7); |
161 | 1898 skip_bits(&s->gb, 3); /* Reserved */ |
1899 } else if (ufep != 0) | |
0 | 1900 return -1; |
161 | 1901 |
78 | 1902 /* MPPTYPE */ |
0 | 1903 s->pict_type = get_bits(&s->gb, 3) + 1; |
1904 if (s->pict_type != I_TYPE && | |
1905 s->pict_type != P_TYPE) | |
1906 return -1; | |
250
3449316664b5
- Bug fix on RTYPE (rounding type) not being honoured by H.263+ decoder.
pulento
parents:
249
diff
changeset
|
1907 skip_bits(&s->gb, 2); |
3449316664b5
- Bug fix on RTYPE (rounding type) not being honoured by H.263+ decoder.
pulento
parents:
249
diff
changeset
|
1908 s->no_rounding = get_bits1(&s->gb); |
3449316664b5
- Bug fix on RTYPE (rounding type) not being honoured by H.263+ decoder.
pulento
parents:
249
diff
changeset
|
1909 //fprintf(stderr, "\nRTYPE: %d", s->no_rounding); |
3449316664b5
- Bug fix on RTYPE (rounding type) not being honoured by H.263+ decoder.
pulento
parents:
249
diff
changeset
|
1910 skip_bits(&s->gb, 4); |
78 | 1911 |
1912 /* Get the picture dimensions */ | |
161 | 1913 if (ufep) { |
1914 if (format == 6) { | |
1915 /* Custom Picture Format (CPFMT) */ | |
1916 skip_bits(&s->gb, 4); /* aspect ratio */ | |
1917 width = (get_bits(&s->gb, 9) + 1) * 4; | |
1918 skip_bits1(&s->gb); | |
1919 height = get_bits(&s->gb, 9) * 4; | |
78 | 1920 #ifdef DEBUG |
161 | 1921 fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height); |
78 | 1922 #endif |
161 | 1923 } |
1924 else { | |
1925 width = h263_format[format][0]; | |
1926 height = h263_format[format][1]; | |
1927 } | |
1928 if ((width == 0) || (height == 0)) | |
1929 return -1; | |
1930 s->width = width; | |
1931 s->height = height; | |
1932 if (s->umvplus_dec) { | |
1933 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ | |
1934 } | |
78 | 1935 } |
1936 | |
0 | 1937 s->qscale = get_bits(&s->gb, 5); |
1938 } | |
1939 /* PEI */ | |
21 | 1940 while (get_bits1(&s->gb) != 0) { |
1941 skip_bits(&s->gb, 8); | |
0 | 1942 } |
1943 s->f_code = 1; | |
1944 return 0; | |
1945 } | |
1946 | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1947 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1948 { |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1949 int i; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1950 int a= 2<<s->sprite_warping_accuracy; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1951 int rho= 3-s->sprite_warping_accuracy; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1952 int r=16/a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1953 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1954 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}}; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1955 int sprite_ref[4][2]; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1956 int virtual_ref[2][2]; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1957 int w2, h2; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1958 int alpha=0, beta=0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1959 int w= s->width; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1960 int h= s->height; |
255 | 1961 //printf("SP %d\n", s->sprite_warping_accuracy); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1962 for(i=0; i<s->num_sprite_warping_points; i++){ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1963 int length; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1964 int x=0, y=0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1965 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1966 length= get_vlc(&s->gb, &sprite_trajectory); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1967 if(length){ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1968 x= get_bits(&s->gb, length); |
255 | 1969 //printf("lx %d %d\n", length, x); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1970 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1971 x = - (x ^ ((1 << length) - 1)); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1972 } |
255 | 1973 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */ |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1974 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1975 length= get_vlc(&s->gb, &sprite_trajectory); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1976 if(length){ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1977 y=get_bits(&s->gb, length); |
255 | 1978 //printf("ly %d %d\n", length, y); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1979 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1980 y = - (y ^ ((1 << length) - 1)); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1981 } |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1982 skip_bits1(&s->gb); /* marker bit */ |
255 | 1983 //printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1984 //if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); |
255 | 1985 //x=y=0; |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1986 d[i][0]= x; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1987 d[i][1]= y; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1988 } |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1989 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1990 while((1<<alpha)<w) alpha++; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1991 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h' |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1992 w2= 1<<alpha; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1993 h2= 1<<beta; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1994 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
1995 // Note, the 4th point isnt used for GMC |
262 | 1996 if(s->divx_version==500 && s->divx_build==413){ |
1997 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0]; | |
1998 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1]; | |
1999 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0]; | |
2000 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1]; | |
2001 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0]; | |
2002 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1]; | |
2003 } else { | |
2004 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]); | |
2005 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]); | |
2006 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]); | |
2007 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]); | |
2008 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]); | |
2009 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]); | |
2010 } | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2011 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2012 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2013 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2014 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2015 // perhaps it should be reordered to be more readable ... |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2016 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2017 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2018 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2) |
255 | 2019 + RDIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2020 virtual_ref[0][1]= 16*vop_ref[0][1] |
255 | 2021 + RDIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2022 virtual_ref[1][0]= 16*vop_ref[0][0] |
255 | 2023 + RDIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2024 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2) |
255 | 2025 + RDIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h); |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2026 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2027 switch(s->num_sprite_warping_points) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2028 { |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2029 case 0: |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2030 s->sprite_offset[0][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2031 s->sprite_offset[0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2032 s->sprite_offset[1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2033 s->sprite_offset[1][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2034 s->sprite_delta[0][0][0]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2035 s->sprite_delta[0][0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2036 s->sprite_delta[0][1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2037 s->sprite_delta[0][1][1]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2038 s->sprite_delta[1][0][0]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2039 s->sprite_delta[1][0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2040 s->sprite_delta[1][1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2041 s->sprite_delta[1][1][1]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2042 s->sprite_shift[0][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2043 s->sprite_shift[0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2044 s->sprite_shift[1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2045 s->sprite_shift[1][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2046 break; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2047 case 1: //GMC only |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2048 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0]; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2049 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1]; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2050 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2051 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2052 s->sprite_delta[0][0][0]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2053 s->sprite_delta[0][0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2054 s->sprite_delta[0][1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2055 s->sprite_delta[0][1][1]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2056 s->sprite_delta[1][0][0]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2057 s->sprite_delta[1][0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2058 s->sprite_delta[1][1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2059 s->sprite_delta[1][1][1]= a; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2060 s->sprite_shift[0][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2061 s->sprite_shift[0][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2062 s->sprite_shift[1][0]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2063 s->sprite_shift[1][1]= 0; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2064 break; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2065 case 2: |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2066 case 3: //FIXME |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2067 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho)) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2068 + ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0]) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2069 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2070 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho)) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2071 + ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0]) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2072 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2073 s->sprite_offset[1][0]= ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2074 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2075 +2*w2*r*sprite_ref[0][0] - 16*w2); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2076 s->sprite_offset[1][1]= ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2077 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1) |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2078 +2*w2*r*sprite_ref[0][1] - 16*w2); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2079 s->sprite_delta[0][0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2080 s->sprite_delta[0][0][1]= ( r*sprite_ref[0][1] - virtual_ref[0][1]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2081 s->sprite_delta[0][1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2082 s->sprite_delta[0][1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2083 s->sprite_delta[1][0][0]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2084 s->sprite_delta[1][0][1]= 4*( r*sprite_ref[0][1] - virtual_ref[0][1]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2085 s->sprite_delta[1][1][0]= 4*(-r*sprite_ref[0][1] + virtual_ref[0][1]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2086 s->sprite_delta[1][1][1]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2087 s->sprite_shift[0][0]= alpha+rho; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2088 s->sprite_shift[0][1]= alpha+rho; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2089 s->sprite_shift[1][0]= alpha+rho+2; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2090 s->sprite_shift[1][1]= alpha+rho+2; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2091 break; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2092 // case 3: |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2093 break; |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2094 } |
255 | 2095 /*printf("%d %d\n", s->sprite_delta[0][0][0], a<<s->sprite_shift[0][0]); |
2096 printf("%d %d\n", s->sprite_delta[0][0][1], 0); | |
2097 printf("%d %d\n", s->sprite_delta[0][1][0], 0); | |
2098 printf("%d %d\n", s->sprite_delta[0][1][1], a<<s->sprite_shift[0][1]); | |
2099 printf("%d %d\n", s->sprite_delta[1][0][0], a<<s->sprite_shift[1][0]); | |
2100 printf("%d %d\n", s->sprite_delta[1][0][1], 0); | |
2101 printf("%d %d\n", s->sprite_delta[1][1][0], 0); | |
2102 printf("%d %d\n", s->sprite_delta[1][1][1], a<<s->sprite_shift[1][1]);*/ | |
2103 /* try to simplify the situation */ | |
2104 if( s->sprite_delta[0][0][0] == a<<s->sprite_shift[0][0] | |
2105 && s->sprite_delta[0][0][1] == 0 | |
2106 && s->sprite_delta[0][1][0] == 0 | |
2107 && s->sprite_delta[0][1][1] == a<<s->sprite_shift[0][1] | |
2108 && s->sprite_delta[1][0][0] == a<<s->sprite_shift[1][0] | |
2109 && s->sprite_delta[1][0][1] == 0 | |
2110 && s->sprite_delta[1][1][0] == 0 | |
2111 && s->sprite_delta[1][1][1] == a<<s->sprite_shift[1][1]) | |
2112 { | |
2113 s->sprite_offset[0][0]>>=s->sprite_shift[0][0]; | |
2114 s->sprite_offset[0][1]>>=s->sprite_shift[0][1]; | |
2115 s->sprite_offset[1][0]>>=s->sprite_shift[1][0]; | |
2116 s->sprite_offset[1][1]>>=s->sprite_shift[1][1]; | |
2117 s->sprite_delta[0][0][0]= a; | |
2118 s->sprite_delta[0][0][1]= 0; | |
2119 s->sprite_delta[0][1][0]= 0; | |
2120 s->sprite_delta[0][1][1]= a; | |
2121 s->sprite_delta[1][0][0]= a; | |
2122 s->sprite_delta[1][0][1]= 0; | |
2123 s->sprite_delta[1][1][0]= 0; | |
2124 s->sprite_delta[1][1][1]= a; | |
2125 s->sprite_shift[0][0]= 0; | |
2126 s->sprite_shift[0][1]= 0; | |
2127 s->sprite_shift[1][0]= 0; | |
2128 s->sprite_shift[1][1]= 0; | |
2129 s->real_sprite_warping_points=1; | |
2130 } | |
2131 else | |
2132 s->real_sprite_warping_points= s->num_sprite_warping_points; | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2133 |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2134 } |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2135 |
0 | 2136 /* decode mpeg4 VOP header */ |
2137 int mpeg4_decode_picture_header(MpegEncContext * s) | |
2138 { | |
2139 int time_incr, startcode, state, v; | |
2140 | |
2141 redo: | |
2142 /* search next start code */ | |
2143 align_get_bits(&s->gb); | |
2144 state = 0xff; | |
2145 for(;;) { | |
2146 v = get_bits(&s->gb, 8); | |
2147 if (state == 0x000001) { | |
2148 state = ((state << 8) | v) & 0xffffff; | |
2149 startcode = state; | |
2150 break; | |
2151 } | |
2152 state = ((state << 8) | v) & 0xffffff; | |
2153 /* XXX: really detect end of frame */ | |
263 | 2154 if (state == 0){ |
2155 printf("illegal zero code found\n"); | |
0 | 2156 return -1; |
263 | 2157 } |
0 | 2158 } |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2159 //printf("startcode %X %d\n", startcode, get_bits_count(&s->gb)); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2160 if (startcode == 0x120) { // Video Object Layer |
262 | 2161 int width, height, vo_ver_id; |
0 | 2162 |
2163 /* vol header */ | |
21 | 2164 skip_bits(&s->gb, 1); /* random access */ |
2165 skip_bits(&s->gb, 8); /* vo_type */ | |
63 | 2166 if (get_bits1(&s->gb) != 0) { /* is_ol_id */ |
2167 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */ | |
2168 skip_bits(&s->gb, 3); /* vo_priority */ | |
2169 } else { | |
2170 vo_ver_id = 1; | |
2171 } | |
0 | 2172 |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2173 s->aspect_ratio_info= get_bits(&s->gb, 4); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2174 if(s->aspect_ratio_info == EXTENDET_PAR){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2175 skip_bits(&s->gb, 8); //par_width |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2176 skip_bits(&s->gb, 8); // par_height |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2177 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2178 if(get_bits1(&s->gb)){ /* vol control parameter */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2179 printf("vol control parameter not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2180 return -1; |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2181 } |
63 | 2182 s->shape = get_bits(&s->gb, 2); /* vol shape */ |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2183 if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n"); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2184 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2185 printf("Gray shape not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2186 skip_bits(&s->gb, 4); //video_object_layer_shape_extension |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2187 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2188 |
21 | 2189 skip_bits1(&s->gb); /* marker */ |
0 | 2190 |
262 | 2191 s->time_increment_resolution = get_bits(&s->gb, 16); |
2192 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; | |
63 | 2193 if (s->time_increment_bits < 1) |
2194 s->time_increment_bits = 1; | |
21 | 2195 skip_bits1(&s->gb); /* marker */ |
0 | 2196 |
63 | 2197 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */ |
2198 skip_bits(&s->gb, s->time_increment_bits); | |
2199 } | |
0 | 2200 |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2201 if (s->shape != BIN_ONLY_SHAPE) { |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2202 if (s->shape == RECT_SHAPE) { |
63 | 2203 skip_bits1(&s->gb); /* marker */ |
2204 width = get_bits(&s->gb, 13); | |
2205 skip_bits1(&s->gb); /* marker */ | |
2206 height = get_bits(&s->gb, 13); | |
2207 skip_bits1(&s->gb); /* marker */ | |
2208 } | |
2209 | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2210 if(get_bits1(&s->gb)) printf("interlaced not supported\n"); /* interlaced */ |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2211 if(!get_bits1(&s->gb)) printf("OBMC not supported\n"); /* OBMC Disable */ |
63 | 2212 if (vo_ver_id == 1) { |
2213 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */ | |
2214 } else { | |
2215 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */ | |
2216 } | |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2217 if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n"); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2218 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2219 if(s->vol_sprite_usage==STATIC_SPRITE){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2220 s->sprite_width = get_bits(&s->gb, 13); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2221 skip_bits1(&s->gb); /* marker */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2222 s->sprite_height= get_bits(&s->gb, 13); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2223 skip_bits1(&s->gb); /* marker */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2224 s->sprite_left = get_bits(&s->gb, 13); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2225 skip_bits1(&s->gb); /* marker */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2226 s->sprite_top = get_bits(&s->gb, 13); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2227 skip_bits1(&s->gb); /* marker */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2228 } |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2229 s->num_sprite_warping_points= get_bits(&s->gb, 6); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2230 s->sprite_warping_accuracy = get_bits(&s->gb, 2); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2231 s->sprite_brightness_change= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2232 if(s->vol_sprite_usage==STATIC_SPRITE) |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2233 s->low_latency_sprite= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2234 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2235 // FIXME sadct disable bit if verid!=1 && shape not rect |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2236 |
63 | 2237 if (get_bits1(&s->gb) == 1) { /* not_8_bit */ |
2238 s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */ | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2239 if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */ |
63 | 2240 } else { |
2241 s->quant_precision = 5; | |
2242 } | |
2243 | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2244 // FIXME a bunch of grayscale shape things |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2245 if(get_bits1(&s->gb)) printf("Quant-Type not supported\n"); /* vol_quant_type */ //FIXME |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2246 if(vo_ver_id != 1) |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2247 s->quarter_sample= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2248 else s->quarter_sample=0; |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2249 |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2250 if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n"); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2251 #if 0 |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2252 if(get_bits1(&s->gb)) printf("resync disable\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2253 #else |
63 | 2254 skip_bits1(&s->gb); /* resync_marker_disabled */ |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2255 #endif |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2256 s->data_partioning= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2257 if(s->data_partioning){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2258 printf("data partitioning not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2259 skip_bits1(&s->gb); // reversible vlc |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2260 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2261 |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2262 if(vo_ver_id != 1) { |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2263 s->new_pred= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2264 if(s->new_pred){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2265 printf("new pred not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2266 skip_bits(&s->gb, 2); /* requested upstream message type */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2267 skip_bits1(&s->gb); /* newpred segment type */ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2268 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2269 s->reduced_res_vop= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2270 if(s->reduced_res_vop) printf("reduced resolution VOP not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2271 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2272 else{ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2273 s->new_pred=0; |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2274 s->reduced_res_vop= 0; |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2275 } |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2276 |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2277 s->scalability= get_bits1(&s->gb); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2278 if (s->scalability) { |
63 | 2279 printf("bad scalability!!!\n"); |
2280 return -1; | |
2281 } | |
2282 } | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2283 //printf("end Data %X %d\n", show_bits(&s->gb, 32), get_bits_count(&s->gb)&0x7); |
0 | 2284 goto redo; |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2285 } else if (startcode == 0x1b2) { //userdata |
255 | 2286 char buf[256]; |
2287 int i; | |
2288 int e; | |
2289 int ver, build; | |
2290 | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2291 //printf("user Data %X\n", show_bits(&s->gb, 32)); |
255 | 2292 buf[0]= show_bits(&s->gb, 8); |
2293 for(i=1; i<256; i++){ | |
2294 buf[i]= show_bits(&s->gb, 16)&0xFF; | |
2295 if(buf[i]==0) break; | |
2296 skip_bits(&s->gb, 8); | |
2297 } | |
2298 buf[255]=0; | |
2299 e=sscanf(buf, "DivX%dBuild%d", &ver, &build); | |
2300 if(e==2){ | |
2301 s->divx_version= ver; | |
2302 s->divx_build= build; | |
2303 if(s->picture_number==0){ | |
2304 printf("This file was encoded with DivX%d Build%d\n", ver, build); | |
2305 if(ver==500 && build==413){ //most likely all version are indeed totally buggy but i dunno for sure ... | |
2306 printf("WARNING: this version of DivX is not MPEG4 compatible, trying to workaround these bugs...\n"); | |
2307 }else{ | |
2308 printf("hmm, i havnt seen that version of divx yet, lets assume they fixed these bugs ...\n" | |
2309 "using mpeg4 decoder, if it fails contact the developers (of ffmpeg)\n"); | |
2310 } | |
2311 } | |
2312 } | |
2313 //printf("User Data: %s\n", buf); | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2314 goto redo; |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2315 } else if (startcode != 0x1b6) { //VOP |
0 | 2316 goto redo; |
2317 } | |
2318 | |
2319 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */ | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2320 //printf("pic: %d\n", s->pict_type); |
262 | 2321 time_incr=0; |
21 | 2322 while (get_bits1(&s->gb) != 0) |
0 | 2323 time_incr++; |
2324 | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2325 check_marker(&s->gb, "before time_increment"); |
262 | 2326 s->time_increment= get_bits(&s->gb, s->time_increment_bits); |
2327 if(s->pict_type!=B_TYPE){ | |
2328 s->time_base+= time_incr; | |
2329 s->last_non_b_time[1]= s->last_non_b_time[0]; | |
2330 s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment; | |
2331 }else{ | |
2332 s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution; | |
2333 s->time+= s->time_increment; | |
2334 } | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2335 |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2336 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){ |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2337 printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n"); |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2338 for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){ |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2339 if(get_bits1(&s->gb)) break; |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2340 } |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2341 printf("my guess is %d bits ;)\n",s->time_increment_bits); |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2342 } |
0 | 2343 /* vop coded */ |
21 | 2344 if (get_bits1(&s->gb) != 1) |
63 | 2345 goto redo; |
262 | 2346 //printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base, |
2347 //s->time, s->last_non_b_time[0], s->last_non_b_time[1]); | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2348 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2349 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) { |
0 | 2350 /* rounding type for motion estimation */ |
21 | 2351 s->no_rounding = get_bits1(&s->gb); |
63 | 2352 } else { |
2353 s->no_rounding = 0; | |
0 | 2354 } |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2355 //FIXME reduced res stuff |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2356 |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2357 if (s->shape != RECT_SHAPE) { |
63 | 2358 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) { |
2359 int width, height, hor_spat_ref, ver_spat_ref; | |
2360 | |
2361 width = get_bits(&s->gb, 13); | |
2362 skip_bits1(&s->gb); /* marker */ | |
2363 height = get_bits(&s->gb, 13); | |
2364 skip_bits1(&s->gb); /* marker */ | |
2365 hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */ | |
2366 skip_bits1(&s->gb); /* marker */ | |
2367 ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */ | |
2368 } | |
2369 skip_bits1(&s->gb); /* change_CR_disable */ | |
2370 | |
2371 if (get_bits1(&s->gb) != 0) { | |
2372 skip_bits(&s->gb, 8); /* constant_alpha_value */ | |
2373 } | |
2374 } | |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2375 //FIXME complexity estimation stuff |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2376 |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2377 if (s->shape != BIN_ONLY_SHAPE) { |
63 | 2378 skip_bits(&s->gb, 3); /* intra dc VLC threshold */ |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2379 //FIXME interlaced specific bits |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2380 } |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2381 |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2382 if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){ |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2383 if(s->num_sprite_warping_points){ |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2384 mpeg4_decode_sprite_trajectory(s); |
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2385 } |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2386 if(s->sprite_brightness_change) printf("sprite_brightness_change not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2387 if(s->vol_sprite_usage==STATIC_SPRITE) printf("static sprite not supported\n"); |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2388 } |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2389 |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2390 if (s->shape != BIN_ONLY_SHAPE) { |
63 | 2391 /* note: we do not use quant_precision to avoid problem if no |
2392 MPEG4 vol header as it is found on some old opendivx | |
2393 movies */ | |
2394 s->qscale = get_bits(&s->gb, 5); | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2395 if(s->qscale==0){ |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2396 printf("Error, header damaged or not MPEG4 header (qscale=0)\n"); |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2397 return -1; // makes no sense to continue, as there is nothing left from the image then |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2398 } |
63 | 2399 |
2400 if (s->pict_type != I_TYPE) { | |
2401 s->f_code = get_bits(&s->gb, 3); /* fcode_for */ | |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2402 if(s->f_code==0){ |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2403 printf("Error, header damaged or not MPEG4 header (f_code=0)\n"); |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2404 return -1; // makes no sense to continue, as the MV decoding will break very quickly |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
263
diff
changeset
|
2405 } |
63 | 2406 } |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2407 if (s->pict_type == B_TYPE) { |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2408 s->b_code = get_bits(&s->gb, 3); |
262 | 2409 //printf("b-code %d\n", s->b_code); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2410 } |
254
b4fed8b24e3a
gmc bitstream decoding support (the real motion compensation isnt implemnted yet)
michaelni
parents:
253
diff
changeset
|
2411 //printf("quant:%d fcode:%d\n", s->qscale, s->f_code); |
253
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2412 if(!s->scalability){ |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2413 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) { |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2414 skip_bits1(&s->gb); // vop shape coding type |
4448dd55d415
parsing more of the mpeg4 header & print some "not supported" stuff
michaelni
parents:
250
diff
changeset
|
2415 } |
63 | 2416 } |
2417 } | |
255 | 2418 s->picture_number++; // better than pic number==0 allways ;) |
63 | 2419 return 0; |
0 | 2420 } |
2421 | |
2422 /* don't understand why they choose a different header ! */ | |
2423 int intel_h263_decode_picture_header(MpegEncContext *s) | |
2424 { | |
2425 int format; | |
2426 | |
2427 /* picture header */ | |
2428 if (get_bits(&s->gb, 22) != 0x20) | |
2429 return -1; | |
21 | 2430 skip_bits(&s->gb, 8); /* picture timestamp */ |
0 | 2431 |
21 | 2432 if (get_bits1(&s->gb) != 1) |
0 | 2433 return -1; /* marker */ |
21 | 2434 if (get_bits1(&s->gb) != 0) |
0 | 2435 return -1; /* h263 id */ |
21 | 2436 skip_bits1(&s->gb); /* split screen off */ |
2437 skip_bits1(&s->gb); /* camera off */ | |
2438 skip_bits1(&s->gb); /* freeze picture release off */ | |
0 | 2439 |
2440 format = get_bits(&s->gb, 3); | |
2441 if (format != 7) | |
2442 return -1; | |
2443 | |
2444 s->h263_plus = 0; | |
2445 | |
21 | 2446 s->pict_type = I_TYPE + get_bits1(&s->gb); |
0 | 2447 |
21 | 2448 s->unrestricted_mv = get_bits1(&s->gb); |
0 | 2449 s->h263_long_vectors = s->unrestricted_mv; |
2450 | |
21 | 2451 if (get_bits1(&s->gb) != 0) |
0 | 2452 return -1; /* SAC: off */ |
21 | 2453 if (get_bits1(&s->gb) != 0) |
0 | 2454 return -1; /* advanced prediction mode: off */ |
21 | 2455 if (get_bits1(&s->gb) != 0) |
0 | 2456 return -1; /* not PB frame */ |
2457 | |
2458 /* skip unknown header garbage */ | |
21 | 2459 skip_bits(&s->gb, 41); |
0 | 2460 |
2461 s->qscale = get_bits(&s->gb, 5); | |
21 | 2462 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ |
0 | 2463 |
2464 /* PEI */ | |
21 | 2465 while (get_bits1(&s->gb) != 0) { |
2466 skip_bits(&s->gb, 8); | |
0 | 2467 } |
2468 s->f_code = 1; | |
2469 return 0; | |
2470 } | |
144 | 2471 |