Mercurial > libavcodec.hg
annotate mpeg12.c @ 1165:f0c39139426d libavcodec
10l
author | michaelni |
---|---|
date | Wed, 02 Apr 2003 10:18:29 +0000 |
parents | 0e3c0c4a7b3d |
children | fea03d2c4946 |
rev | line source |
---|---|
0 | 1 /* |
1106 | 2 * MPEG1 codec / MPEG2 decoder |
429 | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
0 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file mpeg12.c | |
22 * MPEG1 codec / MPEG2 decoder. | |
23 */ | |
24 | |
76 | 25 //#define DEBUG |
0 | 26 #include "avcodec.h" |
27 #include "dsputil.h" | |
28 #include "mpegvideo.h" | |
29 | |
30 #include "mpeg12data.h" | |
31 | |
694 | 32 #if 1 |
33 #define PRINT_QP(a, b) {} | |
34 #else | |
35 #define PRINT_QP(a, b) printf(a, b) | |
36 #endif | |
37 | |
0 | 38 /* Start codes. */ |
39 #define SEQ_END_CODE 0x000001b7 | |
40 #define SEQ_START_CODE 0x000001b3 | |
41 #define GOP_START_CODE 0x000001b8 | |
42 #define PICTURE_START_CODE 0x00000100 | |
43 #define SLICE_MIN_START_CODE 0x00000101 | |
44 #define SLICE_MAX_START_CODE 0x000001af | |
45 #define EXT_START_CODE 0x000001b5 | |
46 #define USER_START_CODE 0x000001b2 | |
47 | |
552 | 48 #define DC_VLC_BITS 9 |
49 #define MV_VLC_BITS 9 | |
50 #define MBINCR_VLC_BITS 9 | |
51 #define MB_PAT_VLC_BITS 9 | |
52 #define MB_PTYPE_VLC_BITS 6 | |
53 #define MB_BTYPE_VLC_BITS 6 | |
54 #define TEX_VLC_BITS 9 | |
55 | |
0 | 56 static void mpeg1_encode_block(MpegEncContext *s, |
57 DCTELEM *block, | |
58 int component); | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
59 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added |
0 | 60 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); |
711 | 61 static inline int mpeg1_decode_block_inter(MpegEncContext *s, |
62 DCTELEM *block, | |
63 int n); | |
64 static inline int mpeg1_decode_block_intra(MpegEncContext *s, | |
0 | 65 DCTELEM *block, |
66 int n); | |
714 | 67 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
0 | 68 DCTELEM *block, |
69 int n); | |
714 | 70 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
0 | 71 DCTELEM *block, |
72 int n); | |
73 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); | |
74 | |
947 | 75 #ifdef CONFIG_ENCODERS |
1162 | 76 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; |
1064 | 77 static uint8_t fcode_tab[MAX_MV*2+1]; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
78 |
947 | 79 static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2]; |
80 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2]; | |
81 #endif | |
82 | |
552 | 83 static void init_2d_vlc_rl(RLTable *rl) |
84 { | |
620
a5aa53b6e648
warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michaelni
parents:
617
diff
changeset
|
85 int i; |
552 | 86 |
87 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, | |
88 &rl->table_vlc[0][1], 4, 2, | |
89 &rl->table_vlc[0][0], 4, 2); | |
90 | |
91 | |
92 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); | |
93 for(i=0; i<rl->vlc.table_size; i++){ | |
94 int code= rl->vlc.table[i][0]; | |
95 int len = rl->vlc.table[i][1]; | |
96 int level, run; | |
97 | |
98 if(len==0){ // illegal code | |
99 run= 65; | |
100 level= MAX_LEVEL; | |
101 }else if(len<0){ //more bits needed | |
102 run= 0; | |
103 level= code; | |
104 }else{ | |
105 if(code==rl->n){ //esc | |
106 run= 65; | |
107 level= 0; | |
108 }else if(code==rl->n+1){ //eob | |
711 | 109 run= 0; |
110 level= 127; | |
552 | 111 }else{ |
112 run= rl->table_run [code] + 1; | |
113 level= rl->table_level[code]; | |
114 } | |
115 } | |
116 rl->rl_vlc[0][i].len= len; | |
117 rl->rl_vlc[0][i].level= level; | |
118 rl->rl_vlc[0][i].run= run; | |
119 } | |
120 } | |
121 | |
947 | 122 static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){ |
123 int i; | |
124 | |
125 for(i=0; i<128; i++){ | |
126 int level= i-64; | |
127 int run; | |
128 for(run=0; run<64; run++){ | |
129 int len, bits, code; | |
130 | |
131 int alevel= ABS(level); | |
132 int sign= (level>>31)&1; | |
133 | |
134 if (alevel > rl->max_level[0][run]) | |
135 code= 111; /*rl->n*/ | |
136 else | |
137 code= rl->index_run[0][run] + alevel - 1; | |
138 | |
139 if (code < 111 /* rl->n */) { | |
140 /* store the vlc & sign at once */ | |
141 len= mpeg1_vlc[code][1]+1; | |
142 bits= (mpeg1_vlc[code][0]<<1) + sign; | |
143 } else { | |
144 len= mpeg1_vlc[111/*rl->n*/][1]+6; | |
145 bits= mpeg1_vlc[111/*rl->n*/][0]<<6; | |
146 | |
147 bits|= run; | |
148 if (alevel < 128) { | |
149 bits<<=8; len+=8; | |
150 bits|= level & 0xff; | |
151 } else { | |
152 bits<<=16; len+=16; | |
153 bits|= level & 0xff; | |
154 if (level < 0) { | |
155 bits|= 0x8001 + level + 255; | |
156 } else { | |
157 bits|= level & 0xffff; | |
158 } | |
159 } | |
160 } | |
161 | |
162 uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits; | |
163 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len; | |
164 } | |
165 } | |
166 } | |
552 | 167 |
0 | 168 static void put_header(MpegEncContext *s, int header) |
169 { | |
170 align_put_bits(&s->pb); | |
236 | 171 put_bits(&s->pb, 16, header>>16); |
172 put_bits(&s->pb, 16, header&0xFFFF); | |
0 | 173 } |
174 | |
175 /* put sequence header if needed */ | |
176 static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
177 { | |
178 unsigned int vbv_buffer_size; | |
1 | 179 unsigned int fps, v; |
918 | 180 int n, i; |
1064 | 181 uint64_t time_code; |
918 | 182 float best_aspect_error= 1E10; |
183 float aspect_ratio= s->avctx->aspect_ratio; | |
184 | |
185 if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA) | |
0 | 186 |
903 | 187 if (s->current_picture.key_frame) { |
0 | 188 /* mpeg1 header repeated every gop */ |
189 put_header(s, SEQ_START_CODE); | |
190 | |
191 /* search closest frame rate */ | |
192 { | |
193 int i, dmin, d; | |
194 s->frame_rate_index = 0; | |
195 dmin = 0x7fffffff; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
196 for(i=1;i<14;i++) { |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
197 if(s->avctx->strict_std_compliance >= 0 && i>=9) break; |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
198 |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
199 d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]); |
0 | 200 if (d < dmin) { |
201 dmin = d; | |
202 s->frame_rate_index = i; | |
203 } | |
204 } | |
205 } | |
206 | |
207 put_bits(&s->pb, 12, s->width); | |
208 put_bits(&s->pb, 12, s->height); | |
918 | 209 |
210 for(i=1; i<15; i++){ | |
211 float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio); | |
212 error= ABS(error); | |
213 | |
214 if(error < best_aspect_error){ | |
215 best_aspect_error= error; | |
216 s->aspect_ratio_info= i; | |
217 } | |
218 } | |
219 | |
220 put_bits(&s->pb, 4, s->aspect_ratio_info); | |
0 | 221 put_bits(&s->pb, 4, s->frame_rate_index); |
1138 | 222 v = (s->bit_rate + 399) / 400; |
0 | 223 if (v > 0x3ffff) |
224 v = 0x3ffff; | |
225 put_bits(&s->pb, 18, v); | |
226 put_bits(&s->pb, 1, 1); /* marker */ | |
639
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
227 |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
228 if(s->avctx->rc_buffer_size) |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
229 vbv_buffer_size = s->avctx->rc_buffer_size; |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
230 else |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
231 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */ |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
232 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; |
0 | 233 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); |
234 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ | |
235 put_bits(&s->pb, 1, 0); /* no custom intra matrix */ | |
236 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */ | |
237 | |
238 put_header(s, GOP_START_CODE); | |
239 put_bits(&s->pb, 1, 0); /* do drop frame */ | |
240 /* time code : we must convert from the real frame rate to a | |
241 fake mpeg frame rate in case of low frame rate */ | |
242 fps = frame_rate_tab[s->frame_rate_index]; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
243 time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE; |
0 | 244 s->gop_picture_number = s->fake_picture_number; |
1064 | 245 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); |
246 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); | |
0 | 247 put_bits(&s->pb, 1, 1); |
1064 | 248 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
249 put_bits(&s->pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE)); |
0 | 250 put_bits(&s->pb, 1, 1); /* closed gop */ |
251 put_bits(&s->pb, 1, 0); /* broken link */ | |
252 } | |
253 | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
254 if (s->avctx->frame_rate < (24 * s->avctx->frame_rate_base) && s->picture_number > 0) { |
0 | 255 /* insert empty P pictures to slow down to the desired |
256 frame rate. Each fake pictures takes about 20 bytes */ | |
257 fps = frame_rate_tab[s->frame_rate_index]; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
258 n = av_rescale((int64_t)s->picture_number * s->avctx->frame_rate_base, fps, s->avctx->frame_rate) / MPEG1_FRAME_RATE_BASE - 1; |
0 | 259 while (s->fake_picture_number < n) { |
260 mpeg1_skip_picture(s, s->fake_picture_number - | |
261 s->gop_picture_number); | |
262 s->fake_picture_number++; | |
263 } | |
264 | |
265 } | |
266 } | |
267 | |
1160 | 268 static inline void encode_mb_skip_run(MpegEncContext *s, int run){ |
269 while (run >= 33) { | |
270 put_bits(&s->pb, 11, 0x008); | |
271 run -= 33; | |
272 } | |
273 put_bits(&s->pb, mbAddrIncrTable[run][1], | |
274 mbAddrIncrTable[run][0]); | |
275 } | |
0 | 276 |
277 /* insert a fake P picture */ | |
278 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) | |
279 { | |
280 /* mpeg1 picture header */ | |
281 put_header(s, PICTURE_START_CODE); | |
282 /* temporal reference */ | |
283 put_bits(&s->pb, 10, pict_num & 0x3ff); | |
284 | |
285 put_bits(&s->pb, 3, P_TYPE); | |
286 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
287 | |
288 put_bits(&s->pb, 1, 1); /* integer coordinates */ | |
289 put_bits(&s->pb, 3, 1); /* forward_f_code */ | |
290 | |
291 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
292 | |
293 /* only one slice */ | |
294 put_header(s, SLICE_MIN_START_CODE); | |
295 put_bits(&s->pb, 5, 1); /* quantizer scale */ | |
296 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
297 | |
1160 | 298 encode_mb_skip_run(s, 0); |
0 | 299 |
300 /* empty macroblock */ | |
301 put_bits(&s->pb, 3, 1); /* motion only */ | |
302 | |
303 /* zero motion x & y */ | |
304 put_bits(&s->pb, 1, 1); | |
305 put_bits(&s->pb, 1, 1); | |
306 | |
307 /* output a number of empty slice */ | |
1160 | 308 encode_mb_skip_run(s, s->mb_width * s->mb_height - 2); |
0 | 309 |
310 /* empty macroblock */ | |
311 put_bits(&s->pb, 3, 1); /* motion only */ | |
312 | |
313 /* zero motion x & y */ | |
314 put_bits(&s->pb, 1, 1); | |
315 put_bits(&s->pb, 1, 1); | |
316 } | |
317 | |
498 | 318 static void common_init(MpegEncContext *s) |
319 { | |
320 s->y_dc_scale_table= | |
321 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
322 } | |
323 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
324 #ifdef CONFIG_ENCODERS |
1160 | 325 |
326 void ff_mpeg1_encode_slice_header(MpegEncContext *s){ | |
327 put_header(s, SLICE_MIN_START_CODE + s->mb_y); | |
328 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */ | |
329 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
330 } | |
331 | |
332 void ff_mpeg1_clean_buffers(MpegEncContext *s){ | |
333 s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
334 s->last_dc[1] = s->last_dc[0]; | |
335 s->last_dc[2] = s->last_dc[0]; | |
336 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
337 } | |
338 | |
0 | 339 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) |
340 { | |
341 mpeg1_encode_sequence_header(s); | |
342 | |
343 /* mpeg1 picture header */ | |
344 put_header(s, PICTURE_START_CODE); | |
345 /* temporal reference */ | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
346 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
347 // RAL: s->picture_number instead of s->fake_picture_number |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
348 put_bits(&s->pb, 10, (s->picture_number - |
0 | 349 s->gop_picture_number) & 0x3ff); |
276
1e2f9ef286d4
- Fix pts calculation on mpeg mux (A/V sync) - Thanks to Lennert Buytenhek
pulento
parents:
275
diff
changeset
|
350 s->fake_picture_number++; |
0 | 351 |
352 put_bits(&s->pb, 3, s->pict_type); | |
353 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
354 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
355 // RAL: Forward f_code also needed for B frames |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
356 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
0 | 357 put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
358 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
359 } | |
360 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
361 // RAL: Backward f_code necessary for B frames |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
362 if (s->pict_type == B_TYPE) { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
363 put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
364 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
365 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
366 |
0 | 367 put_bits(&s->pb, 1, 0); /* extra bit picture */ |
368 | |
1160 | 369 s->mb_y=0; |
370 ff_mpeg1_encode_slice_header(s); | |
0 | 371 } |
372 | |
373 void mpeg1_encode_mb(MpegEncContext *s, | |
374 DCTELEM block[6][64], | |
375 int motion_x, int motion_y) | |
376 { | |
1160 | 377 int i, cbp; |
378 const int mb_x = s->mb_x; | |
379 const int mb_y = s->mb_y; | |
380 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; | |
0 | 381 |
382 /* compute cbp */ | |
383 cbp = 0; | |
384 for(i=0;i<6;i++) { | |
385 if (s->block_last_index[i] >= 0) | |
386 cbp |= 1 << (5 - i); | |
387 } | |
388 | |
1160 | 389 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) && |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
390 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) || |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
391 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
392 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
1160 | 393 s->mb_skip_run++; |
694 | 394 s->qscale -= s->dquant; |
740 | 395 s->skip_count++; |
396 s->misc_bits++; | |
397 s->last_bits++; | |
0 | 398 } else { |
1160 | 399 if(first_mb){ |
400 assert(s->mb_skip_run == 0); | |
401 encode_mb_skip_run(s, s->mb_x); | |
402 }else{ | |
403 encode_mb_skip_run(s, s->mb_skip_run); | |
0 | 404 } |
405 | |
406 if (s->pict_type == I_TYPE) { | |
694 | 407 if(s->dquant && cbp){ |
408 put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */ | |
409 put_bits(&s->pb, 5, s->qscale); | |
410 }else{ | |
411 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ | |
412 s->qscale -= s->dquant; | |
413 } | |
740 | 414 s->misc_bits+= get_bits_diff(s); |
415 s->i_count++; | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
416 } else if (s->mb_intra) { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
417 if(s->dquant && cbp){ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
418 put_bits(&s->pb, 6, 0x01); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
419 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
420 }else{ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
421 put_bits(&s->pb, 5, 0x03); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
422 s->qscale -= s->dquant; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
423 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
424 s->misc_bits+= get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
425 s->i_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
426 s->last_mv[0][0][0] = |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
427 s->last_mv[0][0][1] = 0; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
428 } else if (s->pict_type == P_TYPE) { |
0 | 429 if (cbp != 0) { |
430 if (motion_x == 0 && motion_y == 0) { | |
694 | 431 if(s->dquant){ |
432 put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */ | |
433 put_bits(&s->pb, 5, s->qscale); | |
434 }else{ | |
435 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */ | |
436 } | |
740 | 437 s->misc_bits+= get_bits_diff(s); |
0 | 438 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
439 } else { | |
694 | 440 if(s->dquant){ |
441 put_bits(&s->pb, 5, 2); /* motion + cbp */ | |
442 put_bits(&s->pb, 5, s->qscale); | |
443 }else{ | |
444 put_bits(&s->pb, 1, 1); /* motion + cbp */ | |
445 } | |
740 | 446 s->misc_bits+= get_bits_diff(s); |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
447 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
448 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added |
740 | 449 s->mv_bits+= get_bits_diff(s); |
0 | 450 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
451 } | |
452 } else { | |
453 put_bits(&s->pb, 3, 1); /* motion only */ | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
454 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
455 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added |
694 | 456 s->qscale -= s->dquant; |
740 | 457 s->mv_bits+= get_bits_diff(s); |
0 | 458 } |
740 | 459 s->f_count++; |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
460 } else |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
461 { // RAL: All the following bloc added for B frames: |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
462 if (cbp != 0) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
463 { // With coded bloc pattern |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
464 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
465 { // Bi-directional motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
466 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
467 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
468 put_bits(&s->pb, 5, 2); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
469 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
470 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
471 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
472 put_bits(&s->pb, 2, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
473 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
474 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
475 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
476 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
477 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
478 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
479 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
480 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
481 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
482 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
483 else if (s->mv_dir == MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
484 { // Backward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
485 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
486 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
487 put_bits(&s->pb, 6, 2); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
488 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
489 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
490 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
491 put_bits(&s->pb, 3, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
492 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
493 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
494 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
495 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
496 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
497 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
498 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
499 else if (s->mv_dir == MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
500 { // Forward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
501 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
502 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
503 put_bits(&s->pb, 6, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
504 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
505 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
506 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
507 put_bits(&s->pb, 4, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
508 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
509 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
510 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
511 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
512 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
513 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
514 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
515 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
516 else |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
517 { // No coded bloc pattern |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
518 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
519 { // Bi-directional motion |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
520 put_bits(&s->pb, 2, 2); /* backward & forward motion */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
521 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
522 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
523 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
524 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
525 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
526 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
527 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
528 else if (s->mv_dir == MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
529 { // Backward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
530 put_bits(&s->pb, 3, 2); /* backward motion only */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
531 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
532 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
533 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
534 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
535 else if (s->mv_dir == MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
536 { // Forward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
537 put_bits(&s->pb, 4, 2); /* forward motion only */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
538 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
539 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
540 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
541 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
542 s->qscale -= s->dquant; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
543 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
544 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
545 // End of bloc from RAL |
0 | 546 } |
547 for(i=0;i<6;i++) { | |
548 if (cbp & (1 << (5 - i))) { | |
549 mpeg1_encode_block(s, block[i], i); | |
550 } | |
551 } | |
1160 | 552 s->mb_skip_run = 0; |
740 | 553 if(s->mb_intra) |
554 s->i_tex_bits+= get_bits_diff(s); | |
555 else | |
556 s->p_tex_bits+= get_bits_diff(s); | |
0 | 557 } |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
558 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
559 // RAL: By this: |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
560 if (s->mv_dir & MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
561 { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
562 s->last_mv[0][0][0]= s->mv[0][0][0]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
563 s->last_mv[0][0][1]= s->mv[0][0][1]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
564 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
565 if (s->mv_dir & MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
566 { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
567 s->last_mv[1][0][0]= s->mv[1][0][0]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
568 s->last_mv[1][0][1]= s->mv[1][0][1]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
569 } |
0 | 570 } |
571 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
572 // RAL: Parameter added: f_or_b_code |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
573 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) |
0 | 574 { |
575 int code, bit_size, l, m, bits, range, sign; | |
576 | |
577 if (val == 0) { | |
578 /* zero vector */ | |
579 code = 0; | |
580 put_bits(&s->pb, | |
581 mbMotionVectorTable[0][1], | |
582 mbMotionVectorTable[0][0]); | |
583 } else { | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
584 bit_size = f_or_b_code - 1; |
0 | 585 range = 1 << bit_size; |
586 /* modulo encoding */ | |
587 l = 16 * range; | |
588 m = 2 * l; | |
589 if (val < -l) { | |
590 val += m; | |
591 } else if (val >= l) { | |
592 val -= m; | |
593 } | |
594 | |
595 if (val >= 0) { | |
596 val--; | |
597 code = (val >> bit_size) + 1; | |
598 bits = val & (range - 1); | |
599 sign = 0; | |
600 } else { | |
601 val = -val; | |
602 val--; | |
603 code = (val >> bit_size) + 1; | |
604 bits = val & (range - 1); | |
605 sign = 1; | |
606 } | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
607 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
608 assert(code > 0 && code <= 16); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
609 |
0 | 610 put_bits(&s->pb, |
611 mbMotionVectorTable[code][1], | |
612 mbMotionVectorTable[code][0]); | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
613 |
0 | 614 put_bits(&s->pb, 1, sign); |
615 if (bit_size > 0) { | |
616 put_bits(&s->pb, bit_size, bits); | |
617 } | |
618 } | |
619 } | |
620 | |
498 | 621 void ff_mpeg1_encode_init(MpegEncContext *s) |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
622 { |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
623 static int done=0; |
498 | 624 |
625 common_init(s); | |
626 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
627 if(!done){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
628 int f_code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
629 int mv; |
498 | 630 int i; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
631 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
632 done=1; |
498 | 633 init_rl(&rl_mpeg1); |
947 | 634 |
498 | 635 for(i=0; i<64; i++) |
636 { | |
637 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i]; | |
638 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i]; | |
639 } | |
947 | 640 |
641 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len); | |
498 | 642 |
643 /* build unified dc encoding tables */ | |
644 for(i=-255; i<256; i++) | |
645 { | |
646 int adiff, index; | |
647 int bits, code; | |
648 int diff=i; | |
649 | |
650 adiff = ABS(diff); | |
651 if(diff<0) diff--; | |
652 index = vlc_dc_table[adiff]; | |
653 | |
654 bits= vlc_dc_lum_bits[index] + index; | |
655 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)); | |
656 mpeg1_lum_dc_uni[i+255]= bits + (code<<8); | |
657 | |
658 bits= vlc_dc_chroma_bits[index] + index; | |
659 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)); | |
660 mpeg1_chr_dc_uni[i+255]= bits + (code<<8); | |
661 } | |
662 | |
1162 | 663 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); |
664 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
665 for(f_code=1; f_code<=MAX_FCODE; f_code++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
666 for(mv=-MAX_MV; mv<=MAX_MV; mv++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
667 int len; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
668 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
669 if(mv==0) len= mbMotionVectorTable[0][1]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
670 else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
671 int val, bit_size, range, code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
672 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
673 bit_size = s->f_code - 1; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
674 range = 1 << bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
675 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
676 val=mv; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
677 if (val < 0) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
678 val = -val; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
679 val--; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
680 code = (val >> bit_size) + 1; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
681 if(code<17){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
682 len= mbMotionVectorTable[code][1] + 1 + bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
683 }else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
684 len= mbMotionVectorTable[16][1] + 2 + bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
685 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
686 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
687 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
688 mv_penalty[f_code][mv+MAX_MV]= len; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
689 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
690 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
691 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
692 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
693 for(f_code=MAX_FCODE; f_code>0; f_code--){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
694 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
695 fcode_tab[mv+MAX_MV]= f_code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
696 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
697 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
698 } |
936 | 699 s->me.mv_penalty= mv_penalty; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
700 s->fcode_tab= fcode_tab; |
344 | 701 s->min_qcoeff=-255; |
702 s->max_qcoeff= 255; | |
947 | 703 s->intra_ac_vlc_length= |
704 s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len; | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
705 } |
498 | 706 |
0 | 707 static inline void encode_dc(MpegEncContext *s, int diff, int component) |
708 { | |
709 if (component == 0) { | |
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
710 put_bits( |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
711 &s->pb, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
712 mpeg1_lum_dc_uni[diff+255]&0xFF, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
713 mpeg1_lum_dc_uni[diff+255]>>8); |
0 | 714 } else { |
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
715 put_bits( |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
716 &s->pb, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
717 mpeg1_chr_dc_uni[diff+255]&0xFF, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
718 mpeg1_chr_dc_uni[diff+255]>>8); |
0 | 719 } |
720 } | |
721 | |
722 static void mpeg1_encode_block(MpegEncContext *s, | |
723 DCTELEM *block, | |
724 int n) | |
725 { | |
726 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | |
727 int code, component; | |
236 | 728 // RLTable *rl = &rl_mpeg1; |
0 | 729 |
730 last_index = s->block_last_index[n]; | |
731 | |
732 /* DC coef */ | |
733 if (s->mb_intra) { | |
734 component = (n <= 3 ? 0 : n - 4 + 1); | |
735 dc = block[0]; /* overflow is impossible */ | |
736 diff = dc - s->last_dc[component]; | |
737 encode_dc(s, diff, component); | |
738 s->last_dc[component] = dc; | |
739 i = 1; | |
740 } else { | |
741 /* encode the first coefficient : needs to be done here because | |
742 it is handled slightly differently */ | |
743 level = block[0]; | |
744 if (abs(level) == 1) { | |
1064 | 745 code = ((uint32_t)level >> 31); /* the sign bit */ |
0 | 746 put_bits(&s->pb, 2, code | 0x02); |
747 i = 1; | |
748 } else { | |
749 i = 0; | |
750 last_non_zero = -1; | |
751 goto next_coef; | |
752 } | |
753 } | |
754 | |
755 /* now quantify & encode AC coefs */ | |
756 last_non_zero = i - 1; | |
236 | 757 |
0 | 758 for(;i<=last_index;i++) { |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
759 j = s->intra_scantable.permutated[i]; |
0 | 760 level = block[j]; |
761 next_coef: | |
762 #if 0 | |
763 if (level != 0) | |
764 dprintf("level[%d]=%d\n", i, level); | |
765 #endif | |
766 /* encode using VLC */ | |
767 if (level != 0) { | |
768 run = i - last_non_zero - 1; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
769 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
770 alevel= level; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
771 MASK_ABS(sign, alevel) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
772 sign&=1; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
773 |
236 | 774 // code = get_rl_index(rl, 0, run, alevel); |
947 | 775 if (alevel <= mpeg1_max_level[0][run]){ |
236 | 776 code= mpeg1_index_run[0][run] + alevel - 1; |
777 /* store the vlc & sign at once */ | |
778 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); | |
0 | 779 } else { |
236 | 780 /* escape seems to be pretty rare <5% so i dont optimize it */ |
781 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); | |
0 | 782 /* escape: only clip in this case */ |
783 put_bits(&s->pb, 6, run); | |
784 if (alevel < 128) { | |
785 put_bits(&s->pb, 8, level & 0xff); | |
786 } else { | |
787 if (level < 0) { | |
788 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
789 } else { | |
790 put_bits(&s->pb, 16, level & 0xffff); | |
791 } | |
792 } | |
793 } | |
794 last_non_zero = i; | |
795 } | |
796 } | |
797 /* end of block */ | |
798 put_bits(&s->pb, 2, 0x2); | |
799 } | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
800 #endif //CONFIG_ENCODERS |
0 | 801 |
802 /******************************************/ | |
803 /* decoding */ | |
804 | |
805 static VLC dc_lum_vlc; | |
806 static VLC dc_chroma_vlc; | |
807 static VLC mv_vlc; | |
808 static VLC mbincr_vlc; | |
809 static VLC mb_ptype_vlc; | |
810 static VLC mb_btype_vlc; | |
811 static VLC mb_pat_vlc; | |
812 | |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
813 static void init_vlcs(MpegEncContext *s) |
0 | 814 { |
815 static int done = 0; | |
816 | |
817 if (!done) { | |
494 | 818 done = 1; |
0 | 819 |
568 | 820 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, |
0 | 821 vlc_dc_lum_bits, 1, 1, |
822 vlc_dc_lum_code, 2, 2); | |
568 | 823 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12, |
0 | 824 vlc_dc_chroma_bits, 1, 1, |
825 vlc_dc_chroma_code, 2, 2); | |
545 | 826 init_vlc(&mv_vlc, MV_VLC_BITS, 17, |
0 | 827 &mbMotionVectorTable[0][1], 2, 1, |
828 &mbMotionVectorTable[0][0], 2, 1); | |
545 | 829 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35, |
0 | 830 &mbAddrIncrTable[0][1], 2, 1, |
831 &mbAddrIncrTable[0][0], 2, 1); | |
545 | 832 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63, |
0 | 833 &mbPatTable[0][1], 2, 1, |
834 &mbPatTable[0][0], 2, 1); | |
835 | |
545 | 836 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, |
0 | 837 &table_mb_ptype[0][1], 2, 1, |
838 &table_mb_ptype[0][0], 2, 1); | |
545 | 839 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, |
0 | 840 &table_mb_btype[0][1], 2, 1, |
841 &table_mb_btype[0][0], 2, 1); | |
842 init_rl(&rl_mpeg1); | |
843 init_rl(&rl_mpeg2); | |
552 | 844 |
845 init_2d_vlc_rl(&rl_mpeg1); | |
846 init_2d_vlc_rl(&rl_mpeg2); | |
0 | 847 } |
848 } | |
849 | |
850 static inline int get_dmv(MpegEncContext *s) | |
851 { | |
21 | 852 if(get_bits1(&s->gb)) |
853 return 1 - (get_bits1(&s->gb) << 1); | |
0 | 854 else |
855 return 0; | |
856 } | |
857 | |
54 | 858 static inline int get_qscale(MpegEncContext *s) |
859 { | |
860 int qscale; | |
861 if (s->mpeg2) { | |
862 if (s->q_scale_type) { | |
863 qscale = non_linear_qscale[get_bits(&s->gb, 5)]; | |
864 } else { | |
865 qscale = get_bits(&s->gb, 5) << 1; | |
866 } | |
867 } else { | |
868 /* for mpeg1, we use the generic unquant code */ | |
869 qscale = get_bits(&s->gb, 5); | |
870 } | |
871 return qscale; | |
872 } | |
873 | |
0 | 874 /* motion type (for mpeg2) */ |
875 #define MT_FIELD 1 | |
876 #define MT_FRAME 2 | |
877 #define MT_16X8 2 | |
878 #define MT_DMV 3 | |
879 | |
880 static int mpeg_decode_mb(MpegEncContext *s, | |
881 DCTELEM block[6][64]) | |
882 { | |
751 | 883 int i, j, k, cbp, val, mb_type, motion_type; |
0 | 884 |
885 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y); | |
886 | |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
887 assert(s->mb_skiped==0); |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
888 |
1160 | 889 if (s->mb_skip_run-- != 0) { |
0 | 890 /* skip mb */ |
891 s->mb_intra = 0; | |
892 for(i=0;i<6;i++) | |
893 s->block_last_index[i] = -1; | |
894 s->mv_type = MV_TYPE_16X16; | |
895 if (s->pict_type == P_TYPE) { | |
896 /* if P type, zero motion vector is implied */ | |
897 s->mv_dir = MV_DIR_FORWARD; | |
898 s->mv[0][0][0] = s->mv[0][0][1] = 0; | |
899 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0; | |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
900 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0; |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
901 s->mb_skiped = 1; |
0 | 902 } else { |
903 /* if B type, reuse previous vectors and directions */ | |
904 s->mv[0][0][0] = s->last_mv[0][0][0]; | |
905 s->mv[0][0][1] = s->last_mv[0][0][1]; | |
906 s->mv[1][0][0] = s->last_mv[1][0][0]; | |
907 s->mv[1][0][1] = s->last_mv[1][0][1]; | |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
908 |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
909 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
910 s->mb_skiped = 1; |
0 | 911 } |
930 | 912 |
0 | 913 return 0; |
914 } | |
915 | |
916 switch(s->pict_type) { | |
917 default: | |
918 case I_TYPE: | |
21 | 919 if (get_bits1(&s->gb) == 0) { |
920 if (get_bits1(&s->gb) == 0) | |
0 | 921 return -1; |
922 mb_type = MB_QUANT | MB_INTRA; | |
923 } else { | |
924 mb_type = MB_INTRA; | |
925 } | |
926 break; | |
927 case P_TYPE: | |
545 | 928 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1); |
568 | 929 if (mb_type < 0){ |
930 fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y); | |
0 | 931 return -1; |
568 | 932 } |
0 | 933 break; |
934 case B_TYPE: | |
545 | 935 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1); |
568 | 936 if (mb_type < 0){ |
937 fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y); | |
0 | 938 return -1; |
568 | 939 } |
0 | 940 break; |
941 } | |
942 dprintf("mb_type=%x\n", mb_type); | |
943 motion_type = 0; /* avoid warning */ | |
944 if (mb_type & (MB_FOR|MB_BACK)) { | |
945 /* get additionnal motion vector type */ | |
946 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) | |
947 motion_type = MT_FRAME; | |
948 else | |
949 motion_type = get_bits(&s->gb, 2); | |
950 } | |
951 /* compute dct type */ | |
952 if (s->picture_structure == PICT_FRAME && | |
953 !s->frame_pred_frame_dct && | |
954 (mb_type & (MB_PAT | MB_INTRA))) { | |
21 | 955 s->interlaced_dct = get_bits1(&s->gb); |
0 | 956 #ifdef DEBUG |
957 if (s->interlaced_dct) | |
958 printf("interlaced_dct\n"); | |
959 #endif | |
960 } else { | |
961 s->interlaced_dct = 0; /* frame based */ | |
962 } | |
963 | |
964 if (mb_type & MB_QUANT) { | |
54 | 965 s->qscale = get_qscale(s); |
0 | 966 } |
967 if (mb_type & MB_INTRA) { | |
968 if (s->concealment_motion_vectors) { | |
969 /* just parse them */ | |
970 if (s->picture_structure != PICT_FRAME) | |
21 | 971 skip_bits1(&s->gb); /* field select */ |
0 | 972 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0); |
973 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0); | |
974 } | |
975 s->mb_intra = 1; | |
976 cbp = 0x3f; | |
977 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | |
978 } else { | |
979 s->mb_intra = 0; | |
980 cbp = 0; | |
981 } | |
982 /* special case of implicit zero motion vector */ | |
983 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) { | |
984 s->mv_dir = MV_DIR_FORWARD; | |
985 s->mv_type = MV_TYPE_16X16; | |
986 s->last_mv[0][0][0] = 0; | |
987 s->last_mv[0][0][1] = 0; | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
988 s->last_mv[0][1][0] = 0; |
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
989 s->last_mv[0][1][1] = 0; |
0 | 990 s->mv[0][0][0] = 0; |
991 s->mv[0][0][1] = 0; | |
992 } else if (mb_type & (MB_FOR | MB_BACK)) { | |
993 /* motion vectors */ | |
994 s->mv_dir = 0; | |
995 for(i=0;i<2;i++) { | |
996 if (mb_type & (MB_FOR >> i)) { | |
997 s->mv_dir |= (MV_DIR_FORWARD >> i); | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
998 dprintf("motion_type=%d\n", motion_type); |
0 | 999 switch(motion_type) { |
1000 case MT_FRAME: /* or MT_16X8 */ | |
1001 if (s->picture_structure == PICT_FRAME) { | |
1002 /* MT_FRAME */ | |
1003 s->mv_type = MV_TYPE_16X16; | |
1004 for(k=0;k<2;k++) { | |
1005 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1006 s->last_mv[i][0][k]); | |
1007 s->last_mv[i][0][k] = val; | |
1008 s->last_mv[i][1][k] = val; | |
1009 /* full_pel: only for mpeg1 */ | |
1010 if (s->full_pel[i]) | |
1011 val = val << 1; | |
1012 s->mv[i][0][k] = val; | |
1013 dprintf("mv%d: %d\n", k, val); | |
1014 } | |
1015 } else { | |
1016 /* MT_16X8 */ | |
1017 s->mv_type = MV_TYPE_16X8; | |
1018 for(j=0;j<2;j++) { | |
21 | 1019 s->field_select[i][j] = get_bits1(&s->gb); |
0 | 1020 for(k=0;k<2;k++) { |
1021 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1022 s->last_mv[i][j][k]); | |
1023 s->last_mv[i][j][k] = val; | |
1024 s->mv[i][j][k] = val; | |
1025 } | |
1026 } | |
1027 } | |
1028 break; | |
1029 case MT_FIELD: | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1030 s->mv_type = MV_TYPE_FIELD; |
0 | 1031 if (s->picture_structure == PICT_FRAME) { |
1032 for(j=0;j<2;j++) { | |
21 | 1033 s->field_select[i][j] = get_bits1(&s->gb); |
0 | 1034 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0], |
1035 s->last_mv[i][j][0]); | |
1036 s->last_mv[i][j][0] = val; | |
1037 s->mv[i][j][0] = val; | |
1038 dprintf("fmx=%d\n", val); | |
1039 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
1040 s->last_mv[i][j][1] >> 1); | |
1041 s->last_mv[i][j][1] = val << 1; | |
1042 s->mv[i][j][1] = val; | |
1043 dprintf("fmy=%d\n", val); | |
1044 } | |
1045 } else { | |
21 | 1046 s->field_select[i][0] = get_bits1(&s->gb); |
0 | 1047 for(k=0;k<2;k++) { |
1048 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1049 s->last_mv[i][0][k]); | |
1050 s->last_mv[i][0][k] = val; | |
1051 s->last_mv[i][1][k] = val; | |
1052 s->mv[i][0][k] = val; | |
1053 } | |
1054 } | |
1055 break; | |
1056 case MT_DMV: | |
1057 { | |
1058 int dmx, dmy, mx, my, m; | |
1059 | |
1060 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], | |
1061 s->last_mv[i][0][0]); | |
1062 s->last_mv[i][0][0] = mx; | |
1063 s->last_mv[i][1][0] = mx; | |
1064 dmx = get_dmv(s); | |
1065 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
1066 s->last_mv[i][0][1] >> 1); | |
1067 dmy = get_dmv(s); | |
1068 s->mv_type = MV_TYPE_DMV; | |
1069 /* XXX: totally broken */ | |
1070 if (s->picture_structure == PICT_FRAME) { | |
1071 s->last_mv[i][0][1] = my << 1; | |
1072 s->last_mv[i][1][1] = my << 1; | |
1073 | |
1074 m = s->top_field_first ? 1 : 3; | |
1075 /* top -> top pred */ | |
1076 s->mv[i][0][0] = mx; | |
1077 s->mv[i][0][1] = my << 1; | |
1078 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
1079 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1; | |
1080 m = 4 - m; | |
1081 s->mv[i][2][0] = mx; | |
1082 s->mv[i][2][1] = my << 1; | |
1083 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
1084 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; | |
1085 } else { | |
1086 s->last_mv[i][0][1] = my; | |
1087 s->last_mv[i][1][1] = my; | |
1088 s->mv[i][0][0] = mx; | |
1089 s->mv[i][0][1] = my; | |
1090 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx; | |
1091 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 | |
1092 /* + 2 * cur_field */; | |
1093 } | |
1094 } | |
1095 break; | |
1096 } | |
1097 } | |
1098 } | |
1099 } | |
1100 | |
1101 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) { | |
21 | 1102 skip_bits1(&s->gb); /* marker */ |
0 | 1103 } |
1104 | |
1105 if (mb_type & MB_PAT) { | |
545 | 1106 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1); |
568 | 1107 if (cbp < 0){ |
1108 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y); | |
0 | 1109 return -1; |
568 | 1110 } |
0 | 1111 cbp++; |
1112 } | |
1113 dprintf("cbp=%x\n", cbp); | |
1114 | |
1115 if (s->mpeg2) { | |
1116 if (s->mb_intra) { | |
1117 for(i=0;i<6;i++) { | |
711 | 1118 if (mpeg2_decode_block_intra(s, block[i], i) < 0) |
1119 return -1; | |
0 | 1120 } |
1121 } else { | |
1122 for(i=0;i<6;i++) { | |
711 | 1123 if (cbp & 32) { |
0 | 1124 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) |
1125 return -1; | |
391 | 1126 } else { |
1127 s->block_last_index[i] = -1; | |
0 | 1128 } |
711 | 1129 cbp+=cbp; |
0 | 1130 } |
1131 } | |
1132 } else { | |
711 | 1133 if (s->mb_intra) { |
1134 for(i=0;i<6;i++) { | |
1135 if (mpeg1_decode_block_intra(s, block[i], i) < 0) | |
0 | 1136 return -1; |
711 | 1137 } |
1138 }else{ | |
1139 for(i=0;i<6;i++) { | |
1140 if (cbp & 32) { | |
1141 if (mpeg1_decode_block_inter(s, block[i], i) < 0) | |
1142 return -1; | |
1143 } else { | |
1144 s->block_last_index[i] = -1; | |
1145 } | |
1146 cbp+=cbp; | |
0 | 1147 } |
1148 } | |
1149 } | |
1150 return 0; | |
1151 } | |
1152 | |
1153 /* as h263, but only 17 codes */ | |
1154 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) | |
1155 { | |
1156 int code, sign, val, m, l, shift; | |
1157 | |
545 | 1158 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); |
0 | 1159 if (code < 0) { |
1160 return 0xffff; | |
1161 } | |
1162 if (code == 0) { | |
1163 return pred; | |
1164 } | |
21 | 1165 sign = get_bits1(&s->gb); |
0 | 1166 shift = fcode - 1; |
1167 val = (code - 1) << shift; | |
1168 if (shift > 0) | |
1169 val |= get_bits(&s->gb, shift); | |
1170 val++; | |
1171 if (sign) | |
1172 val = -val; | |
1173 val += pred; | |
1174 | |
1175 /* modulo decoding */ | |
1176 l = (1 << shift) * 16; | |
1177 m = 2 * l; | |
1178 if (val < -l) { | |
1179 val += m; | |
1180 } else if (val >= l) { | |
1181 val -= m; | |
1182 } | |
1183 return val; | |
1184 } | |
1185 | |
1186 static inline int decode_dc(MpegEncContext *s, int component) | |
1187 { | |
1188 int code, diff; | |
1189 | |
1190 if (component == 0) { | |
568 | 1191 code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2); |
0 | 1192 } else { |
568 | 1193 code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2); |
0 | 1194 } |
568 | 1195 if (code < 0){ |
1196 fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y); | |
0 | 1197 return 0xffff; |
568 | 1198 } |
0 | 1199 if (code == 0) { |
1200 diff = 0; | |
1201 } else { | |
1202 diff = get_bits(&s->gb, code); | |
1203 if ((diff & (1 << (code - 1))) == 0) | |
1204 diff = (-1 << code) | (diff + 1); | |
1205 } | |
1206 return diff; | |
1207 } | |
1208 | |
711 | 1209 static inline int mpeg1_decode_block_intra(MpegEncContext *s, |
0 | 1210 DCTELEM *block, |
1211 int n) | |
1212 { | |
1213 int level, dc, diff, i, j, run; | |
711 | 1214 int component; |
0 | 1215 RLTable *rl = &rl_mpeg1; |
1064 | 1216 uint8_t * const scantable= s->intra_scantable.permutated; |
1217 const uint16_t *quant_matrix= s->intra_matrix; | |
711 | 1218 const int qscale= s->qscale; |
0 | 1219 |
711 | 1220 /* DC coef */ |
1221 component = (n <= 3 ? 0 : n - 4 + 1); | |
1222 diff = decode_dc(s, component); | |
1223 if (diff >= 0xffff) | |
1224 return -1; | |
1225 dc = s->last_dc[component]; | |
1226 dc += diff; | |
1227 s->last_dc[component] = dc; | |
1228 block[0] = dc<<3; | |
1229 dprintf("dc=%d diff=%d\n", dc, diff); | |
1230 i = 0; | |
1231 { | |
1232 OPEN_READER(re, &s->gb); | |
1233 /* now quantify & encode AC coefs */ | |
1234 for(;;) { | |
1235 UPDATE_CACHE(re, &s->gb); | |
1236 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1237 | |
1238 if(level == 127){ | |
1239 break; | |
1240 } else if(level != 0) { | |
1241 i += run; | |
1242 j = scantable[i]; | |
1243 level= (level*qscale*quant_matrix[j])>>3; | |
1244 level= (level-1)|1; | |
1245 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1246 LAST_SKIP_BITS(re, &s->gb, 1); | |
1247 } else { | |
1248 /* escape */ | |
1249 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1250 UPDATE_CACHE(re, &s->gb); | |
1251 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); | |
1252 if (level == -128) { | |
1253 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); | |
1254 } else if (level == 0) { | |
1255 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); | |
1256 } | |
1257 i += run; | |
1258 j = scantable[i]; | |
1259 if(level<0){ | |
1260 level= -level; | |
1261 level= (level*qscale*quant_matrix[j])>>3; | |
1262 level= (level-1)|1; | |
1263 level= -level; | |
1264 }else{ | |
1265 level= (level*qscale*quant_matrix[j])>>3; | |
1266 level= (level-1)|1; | |
1267 } | |
1268 } | |
1269 if (i > 63){ | |
1270 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1271 return -1; | |
1272 } | |
1273 | |
1274 block[j] = level; | |
1275 } | |
1276 CLOSE_READER(re, &s->gb); | |
1277 } | |
1278 s->block_last_index[n] = i; | |
1279 return 0; | |
1280 } | |
1281 | |
1282 static inline int mpeg1_decode_block_inter(MpegEncContext *s, | |
1283 DCTELEM *block, | |
1284 int n) | |
1285 { | |
1286 int level, i, j, run; | |
1287 RLTable *rl = &rl_mpeg1; | |
1064 | 1288 uint8_t * const scantable= s->intra_scantable.permutated; |
1289 const uint16_t *quant_matrix= s->inter_matrix; | |
711 | 1290 const int qscale= s->qscale; |
1291 | |
1292 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1293 int v; |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1294 OPEN_READER(re, &s->gb); |
711 | 1295 i = -1; |
0 | 1296 /* special case for the first coef. no need to add a second vlc table */ |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1297 UPDATE_CACHE(re, &s->gb); |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1298 v= SHOW_UBITS(re, &s->gb, 2); |
0 | 1299 if (v & 2) { |
714 | 1300 LAST_SKIP_BITS(re, &s->gb, 2); |
711 | 1301 level= (3*qscale*quant_matrix[0])>>4; |
1302 level= (level-1)|1; | |
1303 if(v&1) | |
1304 level= -level; | |
714 | 1305 block[0] = level; |
711 | 1306 i++; |
0 | 1307 } |
714 | 1308 |
711 | 1309 /* now quantify & encode AC coefs */ |
1310 for(;;) { | |
1311 UPDATE_CACHE(re, &s->gb); | |
1312 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1313 | |
1314 if(level == 127){ | |
1315 break; | |
1316 } else if(level != 0) { | |
1317 i += run; | |
1318 j = scantable[i]; | |
1319 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1320 level= (level-1)|1; | |
1321 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1322 LAST_SKIP_BITS(re, &s->gb, 1); | |
1323 } else { | |
1324 /* escape */ | |
1325 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1326 UPDATE_CACHE(re, &s->gb); | |
1327 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); | |
1328 if (level == -128) { | |
1329 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); | |
1330 } else if (level == 0) { | |
1331 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); | |
1332 } | |
1333 i += run; | |
1334 j = scantable[i]; | |
1335 if(level<0){ | |
1336 level= -level; | |
1337 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1338 level= (level-1)|1; | |
1339 level= -level; | |
1340 }else{ | |
1341 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1342 level= (level-1)|1; | |
1343 } | |
1344 } | |
1345 if (i > 63){ | |
1346 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1347 return -1; | |
1348 } | |
0 | 1349 |
711 | 1350 block[j] = level; |
0 | 1351 } |
711 | 1352 CLOSE_READER(re, &s->gb); |
0 | 1353 } |
711 | 1354 s->block_last_index[n] = i; |
0 | 1355 return 0; |
1356 } | |
1357 | |
1358 /* Also does unquantization here, since I will never support mpeg2 | |
1359 encoding */ | |
714 | 1360 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
1361 DCTELEM *block, | |
1362 int n) | |
0 | 1363 { |
1364 int level, i, j, run; | |
1365 RLTable *rl = &rl_mpeg1; | |
1064 | 1366 uint8_t * const scantable= s->intra_scantable.permutated; |
1367 const uint16_t *quant_matrix; | |
714 | 1368 const int qscale= s->qscale; |
0 | 1369 int mismatch; |
1370 | |
1371 mismatch = 1; | |
1372 | |
1373 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1374 int v; |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1375 OPEN_READER(re, &s->gb); |
714 | 1376 i = -1; |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1377 if (n < 4) |
714 | 1378 quant_matrix = s->inter_matrix; |
0 | 1379 else |
714 | 1380 quant_matrix = s->chroma_inter_matrix; |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1381 |
0 | 1382 /* special case for the first coef. no need to add a second vlc table */ |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1383 UPDATE_CACHE(re, &s->gb); |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1384 v= SHOW_UBITS(re, &s->gb, 2); |
0 | 1385 if (v & 2) { |
714 | 1386 LAST_SKIP_BITS(re, &s->gb, 2); |
1387 level= (3*qscale*quant_matrix[0])>>5; | |
1388 if(v&1) | |
1389 level= -level; | |
1390 block[0] = level; | |
1391 mismatch ^= level; | |
1392 i++; | |
1393 } | |
1394 | |
1395 /* now quantify & encode AC coefs */ | |
1396 for(;;) { | |
1397 UPDATE_CACHE(re, &s->gb); | |
1398 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1399 | |
1400 if(level == 127){ | |
1401 break; | |
1402 } else if(level != 0) { | |
1403 i += run; | |
1404 j = scantable[i]; | |
1405 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1406 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1407 LAST_SKIP_BITS(re, &s->gb, 1); | |
1408 } else { | |
1409 /* escape */ | |
1410 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1411 UPDATE_CACHE(re, &s->gb); | |
1412 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); | |
1413 | |
1414 i += run; | |
1415 j = scantable[i]; | |
1416 if(level<0){ | |
1417 level= ((-level*2+1)*qscale*quant_matrix[j])>>5; | |
1418 level= -level; | |
1419 }else{ | |
1420 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1421 } | |
1422 } | |
1423 if (i > 63){ | |
1424 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1425 return -1; | |
1426 } | |
1427 | |
1428 mismatch ^= level; | |
1429 block[j] = level; | |
0 | 1430 } |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1431 CLOSE_READER(re, &s->gb); |
0 | 1432 } |
1433 block[63] ^= (mismatch & 1); | |
714 | 1434 |
0 | 1435 s->block_last_index[n] = i; |
1436 return 0; | |
1437 } | |
1438 | |
714 | 1439 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
1440 DCTELEM *block, | |
1441 int n) | |
0 | 1442 { |
1443 int level, dc, diff, i, j, run; | |
714 | 1444 int component; |
0 | 1445 RLTable *rl; |
1064 | 1446 uint8_t * const scantable= s->intra_scantable.permutated; |
1447 const uint16_t *quant_matrix; | |
714 | 1448 const int qscale= s->qscale; |
0 | 1449 int mismatch; |
1450 | |
1451 /* DC coef */ | |
714 | 1452 if (n < 4){ |
1453 quant_matrix = s->intra_matrix; | |
1454 component = 0; | |
1455 }else{ | |
1456 quant_matrix = s->chroma_intra_matrix; | |
1457 component = n - 3; | |
1458 } | |
0 | 1459 diff = decode_dc(s, component); |
1460 if (diff >= 0xffff) | |
1461 return -1; | |
1462 dc = s->last_dc[component]; | |
1463 dc += diff; | |
1464 s->last_dc[component] = dc; | |
1465 block[0] = dc << (3 - s->intra_dc_precision); | |
1466 dprintf("dc=%d\n", block[0]); | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
1467 mismatch = block[0] ^ 1; |
714 | 1468 i = 0; |
0 | 1469 if (s->intra_vlc_format) |
1470 rl = &rl_mpeg2; | |
1471 else | |
1472 rl = &rl_mpeg1; | |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1473 |
714 | 1474 { |
1475 OPEN_READER(re, &s->gb); | |
1476 /* now quantify & encode AC coefs */ | |
1477 for(;;) { | |
1478 UPDATE_CACHE(re, &s->gb); | |
1479 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1480 | |
1481 if(level == 127){ | |
1482 break; | |
1483 } else if(level != 0) { | |
1484 i += run; | |
1485 j = scantable[i]; | |
1486 level= (level*qscale*quant_matrix[j])>>4; | |
1487 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1488 LAST_SKIP_BITS(re, &s->gb, 1); | |
1489 } else { | |
1490 /* escape */ | |
1491 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1492 UPDATE_CACHE(re, &s->gb); | |
1493 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); | |
1494 i += run; | |
1495 j = scantable[i]; | |
1496 if(level<0){ | |
1497 level= (-level*qscale*quant_matrix[j])>>4; | |
1498 level= -level; | |
1499 }else{ | |
1500 level= (level*qscale*quant_matrix[j])>>4; | |
1501 } | |
1502 } | |
1503 if (i > 63){ | |
1504 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1505 return -1; | |
1506 } | |
1507 | |
1508 mismatch^= level; | |
1509 block[j] = level; | |
568 | 1510 } |
714 | 1511 CLOSE_READER(re, &s->gb); |
0 | 1512 } |
714 | 1513 block[63]^= mismatch&1; |
1514 | |
0 | 1515 s->block_last_index[n] = i; |
1516 return 0; | |
1517 } | |
1518 | |
1519 /* compressed picture size */ | |
1520 #define PICTURE_BUFFER_SIZE 100000 | |
1521 | |
1522 typedef struct Mpeg1Context { | |
1523 MpegEncContext mpeg_enc_ctx; | |
1064 | 1524 uint32_t header_state; |
0 | 1525 int start_code; /* current start code */ |
1064 | 1526 uint8_t buffer[PICTURE_BUFFER_SIZE]; |
1527 uint8_t *buf_ptr; | |
0 | 1528 int buffer_size; |
1529 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1530 int repeat_field; /* true if we must repeat the field */ |
0 | 1531 } Mpeg1Context; |
1532 | |
1533 static int mpeg_decode_init(AVCodecContext *avctx) | |
1534 { | |
1535 Mpeg1Context *s = avctx->priv_data; | |
498 | 1536 |
558 | 1537 s->mpeg_enc_ctx.flags= avctx->flags; |
498 | 1538 common_init(&s->mpeg_enc_ctx); |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
1539 init_vlcs(&s->mpeg_enc_ctx); |
0 | 1540 |
1541 s->header_state = 0xff; | |
1542 s->mpeg_enc_ctx_allocated = 0; | |
1543 s->buffer_size = PICTURE_BUFFER_SIZE; | |
1544 s->start_code = -1; | |
1545 s->buf_ptr = s->buffer; | |
1546 s->mpeg_enc_ctx.picture_number = 0; | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1547 s->repeat_field = 0; |
344 | 1548 s->mpeg_enc_ctx.codec_id= avctx->codec->id; |
0 | 1549 return 0; |
1550 } | |
1551 | |
1552 /* return the 8 bit start code value and update the search | |
1553 state. Return -1 if no start code found */ | |
1064 | 1554 static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end, |
1555 uint32_t *header_state) | |
0 | 1556 { |
1064 | 1557 uint8_t *buf_ptr; |
0 | 1558 unsigned int state, v; |
1559 int val; | |
1560 | |
1561 state = *header_state; | |
1562 buf_ptr = *pbuf_ptr; | |
1563 while (buf_ptr < buf_end) { | |
1564 v = *buf_ptr++; | |
1565 if (state == 0x000001) { | |
1566 state = ((state << 8) | v) & 0xffffff; | |
1567 val = state; | |
1568 goto found; | |
1569 } | |
1570 state = ((state << 8) | v) & 0xffffff; | |
1571 } | |
1572 val = -1; | |
1573 found: | |
1574 *pbuf_ptr = buf_ptr; | |
1575 *header_state = state; | |
1576 return val; | |
1577 } | |
1578 | |
1579 static int mpeg1_decode_picture(AVCodecContext *avctx, | |
1064 | 1580 uint8_t *buf, int buf_size) |
0 | 1581 { |
1582 Mpeg1Context *s1 = avctx->priv_data; | |
1583 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1584 int ref, f_code; | |
1585 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1586 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1587 |
1588 ref = get_bits(&s->gb, 10); /* temporal ref */ | |
1589 s->pict_type = get_bits(&s->gb, 3); | |
45
933cc4acab5c
fixed mpeg1 last block bug (mb stuffing code was not included in vlc table...)
glantau
parents:
38
diff
changeset
|
1590 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); |
872 | 1591 |
21 | 1592 skip_bits(&s->gb, 16); |
0 | 1593 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
21 | 1594 s->full_pel[0] = get_bits1(&s->gb); |
0 | 1595 f_code = get_bits(&s->gb, 3); |
1596 if (f_code == 0) | |
1597 return -1; | |
1598 s->mpeg_f_code[0][0] = f_code; | |
1599 s->mpeg_f_code[0][1] = f_code; | |
1600 } | |
1601 if (s->pict_type == B_TYPE) { | |
21 | 1602 s->full_pel[1] = get_bits1(&s->gb); |
0 | 1603 f_code = get_bits(&s->gb, 3); |
1604 if (f_code == 0) | |
1605 return -1; | |
1606 s->mpeg_f_code[1][0] = f_code; | |
1607 s->mpeg_f_code[1][1] = f_code; | |
1608 } | |
903 | 1609 s->current_picture.pict_type= s->pict_type; |
1610 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
911 | 1611 |
0 | 1612 s->y_dc_scale = 8; |
1613 s->c_dc_scale = 8; | |
1614 s->first_slice = 1; | |
1615 return 0; | |
1616 } | |
1617 | |
1618 static void mpeg_decode_sequence_extension(MpegEncContext *s) | |
1619 { | |
1620 int horiz_size_ext, vert_size_ext; | |
917 | 1621 int bit_rate_ext, vbv_buf_ext; |
0 | 1622 int frame_rate_ext_n, frame_rate_ext_d; |
917 | 1623 float aspect; |
0 | 1624 |
21 | 1625 skip_bits(&s->gb, 8); /* profil and level */ |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1626 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ |
21 | 1627 skip_bits(&s->gb, 2); /* chroma_format */ |
0 | 1628 horiz_size_ext = get_bits(&s->gb, 2); |
1629 vert_size_ext = get_bits(&s->gb, 2); | |
1630 s->width |= (horiz_size_ext << 12); | |
1631 s->height |= (vert_size_ext << 12); | |
1632 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ | |
1633 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; | |
21 | 1634 skip_bits1(&s->gb); /* marker */ |
0 | 1635 vbv_buf_ext = get_bits(&s->gb, 8); |
917 | 1636 s->low_delay = get_bits1(&s->gb); |
0 | 1637 frame_rate_ext_n = get_bits(&s->gb, 2); |
1638 frame_rate_ext_d = get_bits(&s->gb, 5); | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1639 av_reduce( |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1640 &s->avctx->frame_rate, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1641 &s->avctx->frame_rate_base, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1642 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1), |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1643 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1), |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1644 1<<30); |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1645 |
0 | 1646 dprintf("sequence extension\n"); |
1647 s->mpeg2 = 1; | |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
1648 s->avctx->sub_id = 2; /* indicates mpeg2 found */ |
917 | 1649 |
1650 aspect= mpeg2_aspect[s->aspect_ratio_info]; | |
1651 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height); | |
1652 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect; | |
0 | 1653 } |
1654 | |
1655 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | |
1656 { | |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1657 int i, v, j; |
0 | 1658 |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1659 dprintf("matrix extension\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1660 |
21 | 1661 if (get_bits1(&s->gb)) { |
0 | 1662 for(i=0;i<64;i++) { |
1663 v = get_bits(&s->gb, 8); | |
1092 | 1664 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1665 s->intra_matrix[j] = v; |
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1666 s->chroma_intra_matrix[j] = v; |
0 | 1667 } |
1668 } | |
21 | 1669 if (get_bits1(&s->gb)) { |
0 | 1670 for(i=0;i<64;i++) { |
1671 v = get_bits(&s->gb, 8); | |
1092 | 1672 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
344 | 1673 s->inter_matrix[j] = v; |
1674 s->chroma_inter_matrix[j] = v; | |
0 | 1675 } |
1676 } | |
21 | 1677 if (get_bits1(&s->gb)) { |
0 | 1678 for(i=0;i<64;i++) { |
1679 v = get_bits(&s->gb, 8); | |
1092 | 1680 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1681 s->chroma_intra_matrix[j] = v; |
0 | 1682 } |
1683 } | |
21 | 1684 if (get_bits1(&s->gb)) { |
0 | 1685 for(i=0;i<64;i++) { |
1686 v = get_bits(&s->gb, 8); | |
1092 | 1687 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
344 | 1688 s->chroma_inter_matrix[j] = v; |
0 | 1689 } |
1690 } | |
1691 } | |
1692 | |
1693 static void mpeg_decode_picture_coding_extension(MpegEncContext *s) | |
1694 { | |
1695 s->full_pel[0] = s->full_pel[1] = 0; | |
1696 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4); | |
1697 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4); | |
1698 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4); | |
1699 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4); | |
1700 s->intra_dc_precision = get_bits(&s->gb, 2); | |
1701 s->picture_structure = get_bits(&s->gb, 2); | |
21 | 1702 s->top_field_first = get_bits1(&s->gb); |
1703 s->frame_pred_frame_dct = get_bits1(&s->gb); | |
1704 s->concealment_motion_vectors = get_bits1(&s->gb); | |
1705 s->q_scale_type = get_bits1(&s->gb); | |
1706 s->intra_vlc_format = get_bits1(&s->gb); | |
1707 s->alternate_scan = get_bits1(&s->gb); | |
1708 s->repeat_first_field = get_bits1(&s->gb); | |
1709 s->chroma_420_type = get_bits1(&s->gb); | |
1710 s->progressive_frame = get_bits1(&s->gb); | |
715
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1711 |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1712 if(s->picture_structure == PICT_FRAME) |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1713 s->first_field=0; |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1714 else{ |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1715 s->first_field ^= 1; |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1716 memset(s->mbskip_table, 0, s->mb_width*s->mb_height); |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1717 } |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1718 |
715
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1719 if(s->alternate_scan){ |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1720 ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1721 ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1722 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1723 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1724 }else{ |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1725 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1726 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1727 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1728 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1729 } |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1730 |
0 | 1731 /* composite display not parsed */ |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1732 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1733 dprintf("picture_structure=%d\n", s->picture_structure); |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
1734 dprintf("top field first=%d\n", s->top_field_first); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
1735 dprintf("repeat first field=%d\n", s->repeat_first_field); |
0 | 1736 dprintf("conceal=%d\n", s->concealment_motion_vectors); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1737 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1738 dprintf("alternate_scan=%d\n", s->alternate_scan); |
0 | 1739 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct); |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1740 dprintf("progressive_frame=%d\n", s->progressive_frame); |
0 | 1741 } |
1742 | |
1743 static void mpeg_decode_extension(AVCodecContext *avctx, | |
1064 | 1744 uint8_t *buf, int buf_size) |
0 | 1745 { |
1746 Mpeg1Context *s1 = avctx->priv_data; | |
1747 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1748 int ext_type; | |
1749 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1750 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1751 |
1752 ext_type = get_bits(&s->gb, 4); | |
1753 switch(ext_type) { | |
1754 case 0x1: | |
1755 /* sequence ext */ | |
1756 mpeg_decode_sequence_extension(s); | |
1757 break; | |
1758 case 0x3: | |
1759 /* quant matrix extension */ | |
1760 mpeg_decode_quant_matrix_extension(s); | |
1761 break; | |
1762 case 0x8: | |
1763 /* picture extension */ | |
1764 mpeg_decode_picture_coding_extension(s); | |
1765 break; | |
1766 } | |
1767 } | |
1768 | |
826 | 1769 #define DECODE_SLICE_FATAL_ERROR -2 |
1770 #define DECODE_SLICE_ERROR -1 | |
1771 #define DECODE_SLICE_OK 0 | |
1772 #define DECODE_SLICE_EOP 1 | |
1773 | |
1774 /** | |
1775 * decodes a slice. | |
1776 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br> | |
1777 * DECODE_SLICE_ERROR if the slice is damaged<br> | |
1778 * DECODE_SLICE_OK if this slice is ok<br> | |
1779 * DECODE_SLICE_EOP if the end of the picture is reached | |
1780 */ | |
0 | 1781 static int mpeg_decode_slice(AVCodecContext *avctx, |
925 | 1782 AVFrame *pict, |
0 | 1783 int start_code, |
1064 | 1784 uint8_t *buf, int buf_size) |
0 | 1785 { |
1786 Mpeg1Context *s1 = avctx->priv_data; | |
1787 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1788 int ret; | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1789 const int field_pic= s->picture_structure != PICT_FRAME; |
0 | 1790 |
1791 start_code = (start_code - 1) & 0xff; | |
568 | 1792 if (start_code >= s->mb_height){ |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
1793 fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height); |
826 | 1794 return DECODE_SLICE_ERROR; |
568 | 1795 } |
1160 | 1796 |
1797 ff_mpeg1_clean_buffers(s); | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1798 |
0 | 1799 /* start frame decoding */ |
1138 | 1800 if (s->first_slice) { |
1801 if(s->first_field || s->picture_structure==PICT_FRAME){ | |
771
d4cc92144266
handle direct rendering buffer allocation failure
michaelni
parents:
751
diff
changeset
|
1802 if(MPV_frame_start(s, avctx) < 0) |
826 | 1803 return DECODE_SLICE_FATAL_ERROR; |
1085
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1804 /* first check if we must repeat the frame */ |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1805 s->current_picture.repeat_pict = 0; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1806 |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1807 if (s->repeat_first_field) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1808 if (s->progressive_sequence) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1809 if (s->top_field_first) |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1810 s->current_picture.repeat_pict = 4; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1811 else |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1812 s->current_picture.repeat_pict = 2; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1813 } else if (s->progressive_frame) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1814 s->current_picture.repeat_pict = 1; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1815 } |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1816 } |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1817 // printf("%d \n", s->current_picture.repeat_pict); |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1818 |
930 | 1819 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
1820 printf("qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n", |
930 | 1821 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1], |
1822 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), | |
1823 s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"", | |
1824 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors, | |
1825 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :""); | |
1826 } | |
1138 | 1827 }else{ //second field |
1828 int i; | |
1829 for(i=0; i<4; i++){ | |
1830 s->current_picture.data[i] = s->current_picture_ptr->data[i]; | |
1831 if(s->picture_structure == PICT_BOTTOM_FIELD){ | |
1832 s->current_picture.data[i] += s->current_picture_ptr->linesize[i]; | |
1833 } | |
1834 } | |
1835 } | |
0 | 1836 } |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1837 s->first_slice = 0; |
0 | 1838 |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1839 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1840 |
54 | 1841 s->qscale = get_qscale(s); |
0 | 1842 /* extra slice info */ |
21 | 1843 while (get_bits1(&s->gb) != 0) { |
1844 skip_bits(&s->gb, 8); | |
0 | 1845 } |
1846 | |
716 | 1847 s->mb_x=0; |
1848 for(;;) { | |
1849 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); | |
1850 if (code < 0) | |
1851 return -1; /* error = end of slice, but empty slice is bad or?*/ | |
1852 if (code >= 33) { | |
1853 if (code == 33) { | |
1854 s->mb_x += 33; | |
1855 } | |
1856 /* otherwise, stuffing, nothing to do */ | |
1857 } else { | |
1858 s->mb_x += code; | |
1859 break; | |
1860 } | |
1861 } | |
1160 | 1862 |
716 | 1863 s->mb_y = start_code; |
1160 | 1864 s->mb_skip_run= 0; |
716 | 1865 |
0 | 1866 for(;;) { |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
844
diff
changeset
|
1867 s->dsp.clear_blocks(s->block[0]); |
716 | 1868 |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1869 ret = mpeg_decode_mb(s, s->block); |
0 | 1870 dprintf("ret=%d\n", ret); |
1871 if (ret < 0) | |
1872 return -1; | |
1138 | 1873 |
716 | 1874 MPV_decode_mb(s, s->block); |
1875 | |
1876 if (++s->mb_x >= s->mb_width) { | |
1098
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1877 if(s->picture_structure==PICT_FRAME){ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1878 ff_draw_horiz_band(s, 16*s->mb_y, 16); |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1879 }else{ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1880 if(!s->first_field){ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1881 ff_draw_horiz_band(s, 32*s->mb_y, 32); |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1882 } |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1883 } |
716 | 1884 |
1885 s->mb_x = 0; | |
1886 s->mb_y++; | |
694 | 1887 PRINT_QP("%s", "\n"); |
716 | 1888 } |
694 | 1889 PRINT_QP("%2d", s->qscale); |
716 | 1890 |
1891 /* skip mb handling */ | |
1160 | 1892 if (s->mb_skip_run == -1) { |
716 | 1893 /* read again increment */ |
1160 | 1894 s->mb_skip_run = 0; |
716 | 1895 for(;;) { |
1896 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); | |
1897 if (code < 0) | |
1898 goto eos; /* error = end of slice */ | |
1899 if (code >= 33) { | |
1900 if (code == 33) { | |
1160 | 1901 s->mb_skip_run += 33; |
716 | 1902 } |
1903 /* otherwise, stuffing, nothing to do */ | |
1904 } else { | |
1160 | 1905 s->mb_skip_run += code; |
716 | 1906 break; |
1907 } | |
1908 } | |
1909 } | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1910 if(s->mb_y<<field_pic >= s->mb_height){ |
716 | 1911 fprintf(stderr, "slice too long\n"); |
826 | 1912 return DECODE_SLICE_ERROR; |
716 | 1913 } |
0 | 1914 } |
716 | 1915 eos: //end of slice |
1916 | |
305
1de85b419387
emms was missing, found by juanjo but he didnt commit it?!
michaelni
parents:
296
diff
changeset
|
1917 emms_c(); |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1918 //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number); |
0 | 1919 /* end of slice reached */ |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1920 if (s->mb_y<<field_pic == s->mb_height && !s->first_field) { |
0 | 1921 /* end of image */ |
903 | 1922 |
1923 if(s->mpeg2) | |
1924 s->qscale >>=1; | |
0 | 1925 |
1926 MPV_frame_end(s); | |
1927 | |
924 | 1928 if (s->pict_type == B_TYPE || s->low_delay) { |
925 | 1929 *pict= *(AVFrame*)&s->current_picture; |
0 | 1930 } else { |
903 | 1931 s->picture_number++; |
0 | 1932 /* latency of 1 frame for I and P frames */ |
1933 /* XXX: use another variable than picture_number */ | |
1138 | 1934 if (s->last_picture_ptr == NULL) { |
903 | 1935 return DECODE_SLICE_OK; |
0 | 1936 } else { |
925 | 1937 *pict= *(AVFrame*)&s->last_picture; |
0 | 1938 } |
1939 } | |
903 | 1940 return DECODE_SLICE_EOP; |
0 | 1941 } else { |
826 | 1942 return DECODE_SLICE_OK; |
0 | 1943 } |
1944 } | |
1945 | |
1946 static int mpeg1_decode_sequence(AVCodecContext *avctx, | |
1064 | 1947 uint8_t *buf, int buf_size) |
0 | 1948 { |
1949 Mpeg1Context *s1 = avctx->priv_data; | |
1950 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1951 int width, height, i, v, j; |
917 | 1952 float aspect; |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
1953 |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1954 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1955 |
1956 width = get_bits(&s->gb, 12); | |
1957 height = get_bits(&s->gb, 12); | |
917 | 1958 s->aspect_ratio_info= get_bits(&s->gb, 4); |
1959 if(!s->mpeg2){ | |
1960 aspect= mpeg1_aspect[s->aspect_ratio_info]; | |
1961 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height); | |
1962 } | |
1963 | |
0 | 1964 s->frame_rate_index = get_bits(&s->gb, 4); |
1965 if (s->frame_rate_index == 0) | |
1966 return -1; | |
1967 s->bit_rate = get_bits(&s->gb, 18) * 400; | |
21 | 1968 if (get_bits1(&s->gb) == 0) /* marker */ |
0 | 1969 return -1; |
1970 if (width <= 0 || height <= 0 || | |
1971 (width % 2) != 0 || (height % 2) != 0) | |
1972 return -1; | |
1973 if (width != s->width || | |
1974 height != s->height) { | |
1975 /* start new mpeg1 context decoding */ | |
1976 s->out_format = FMT_MPEG1; | |
1977 if (s1->mpeg_enc_ctx_allocated) { | |
1978 MPV_common_end(s); | |
1979 } | |
1980 s->width = width; | |
1981 s->height = height; | |
924 | 1982 avctx->has_b_frames= 1; |
69 | 1983 s->avctx = avctx; |
0 | 1984 avctx->width = width; |
1985 avctx->height = height; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1986 av_reduce( |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1987 &avctx->frame_rate, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1988 &avctx->frame_rate_base, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1989 frame_rate_tab[s->frame_rate_index], |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1990 MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1991 1<<30 |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1992 ); |
0 | 1993 avctx->bit_rate = s->bit_rate; |
1994 | |
1995 if (MPV_common_init(s) < 0) | |
1996 return -1; | |
1997 s1->mpeg_enc_ctx_allocated = 1; | |
1998 } | |
1999 | |
21 | 2000 skip_bits(&s->gb, 10); /* vbv_buffer_size */ |
2001 skip_bits(&s->gb, 1); | |
0 | 2002 |
2003 /* get matrix */ | |
21 | 2004 if (get_bits1(&s->gb)) { |
0 | 2005 for(i=0;i<64;i++) { |
2006 v = get_bits(&s->gb, 8); | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2007 j = s->intra_scantable.permutated[i]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
2008 s->intra_matrix[j] = v; |
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
2009 s->chroma_intra_matrix[j] = v; |
0 | 2010 } |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2011 #ifdef DEBUG |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2012 dprintf("intra matrix present\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2013 for(i=0;i<64;i++) |
710
97377ab86647
forgot zigzag_direct[] behind #ifdef DEBUG (found by Klaas-Pieter Vlieg <vlieg at eurescom dot de>)
michaelni
parents:
706
diff
changeset
|
2014 dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2015 printf("\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2016 #endif |
0 | 2017 } else { |
2018 for(i=0;i<64;i++) { | |
1092 | 2019 int j= s->dsp.idct_permutation[i]; |
533
3c07cf9595de
adding ff prefix to avoid global name conficts with xvid (patch by Marko Kreen <marko at l-t.ee>)
michaelni
parents:
520
diff
changeset
|
2020 v = ff_mpeg1_default_intra_matrix[i]; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2021 s->intra_matrix[j] = v; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2022 s->chroma_intra_matrix[j] = v; |
0 | 2023 } |
2024 } | |
21 | 2025 if (get_bits1(&s->gb)) { |
0 | 2026 for(i=0;i<64;i++) { |
2027 v = get_bits(&s->gb, 8); | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2028 j = s->intra_scantable.permutated[i]; |
344 | 2029 s->inter_matrix[j] = v; |
2030 s->chroma_inter_matrix[j] = v; | |
0 | 2031 } |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2032 #ifdef DEBUG |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2033 dprintf("non intra matrix present\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2034 for(i=0;i<64;i++) |
710
97377ab86647
forgot zigzag_direct[] behind #ifdef DEBUG (found by Klaas-Pieter Vlieg <vlieg at eurescom dot de>)
michaelni
parents:
706
diff
changeset
|
2035 dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2036 printf("\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2037 #endif |
0 | 2038 } else { |
2039 for(i=0;i<64;i++) { | |
1092 | 2040 int j= s->dsp.idct_permutation[i]; |
533
3c07cf9595de
adding ff prefix to avoid global name conficts with xvid (patch by Marko Kreen <marko at l-t.ee>)
michaelni
parents:
520
diff
changeset
|
2041 v = ff_mpeg1_default_non_intra_matrix[i]; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2042 s->inter_matrix[j] = v; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2043 s->chroma_inter_matrix[j] = v; |
0 | 2044 } |
2045 } | |
2046 | |
2047 /* we set mpeg2 parameters so that it emulates mpeg1 */ | |
2048 s->progressive_sequence = 1; | |
2049 s->progressive_frame = 1; | |
2050 s->picture_structure = PICT_FRAME; | |
2051 s->frame_pred_frame_dct = 1; | |
2052 s->mpeg2 = 0; | |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
2053 avctx->sub_id = 1; /* indicates mpeg1 */ |
0 | 2054 return 0; |
2055 } | |
2056 | |
1084 | 2057 static void mpeg_decode_user_data(AVCodecContext *avctx, |
2058 const uint8_t *buf, int buf_size) | |
2059 { | |
2060 const uint8_t *p; | |
2061 int len, flags; | |
2062 p = buf; | |
2063 len = buf_size; | |
2064 | |
2065 /* we parse the DTG active format information */ | |
2066 if (len >= 5 && | |
2067 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') { | |
2068 flags = p[4]; | |
2069 p += 5; | |
2070 len -= 5; | |
2071 if (flags & 0x80) { | |
2072 /* skip event id */ | |
2073 if (len < 2) | |
2074 return; | |
2075 p += 2; | |
2076 len -= 2; | |
2077 } | |
2078 if (flags & 0x40) { | |
2079 if (len < 1) | |
2080 return; | |
2081 avctx->dtg_active_format = p[0] & 0x0f; | |
2082 } | |
2083 } | |
2084 } | |
2085 | |
0 | 2086 /* handle buffering and image synchronisation */ |
2087 static int mpeg_decode_frame(AVCodecContext *avctx, | |
2088 void *data, int *data_size, | |
1064 | 2089 uint8_t *buf, int buf_size) |
0 | 2090 { |
2091 Mpeg1Context *s = avctx->priv_data; | |
1064 | 2092 uint8_t *buf_end, *buf_ptr, *buf_start; |
0 | 2093 int len, start_code_found, ret, code, start_code, input_size; |
925 | 2094 AVFrame *picture = data; |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2095 MpegEncContext *s2 = &s->mpeg_enc_ctx; |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2096 |
0 | 2097 dprintf("fill_buffer\n"); |
2098 | |
2099 *data_size = 0; | |
344 | 2100 |
0 | 2101 /* special case for last picture */ |
2102 if (buf_size == 0) { | |
2103 if (s2->picture_number > 0) { | |
925 | 2104 *picture= *(AVFrame*)&s2->next_picture; |
903 | 2105 |
925 | 2106 *data_size = sizeof(AVFrame); |
0 | 2107 } |
2108 return 0; | |
2109 } | |
2110 | |
2111 buf_ptr = buf; | |
2112 buf_end = buf + buf_size; | |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2113 |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2114 #if 0 |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2115 if (s->repeat_field % 2 == 1) { |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2116 s->repeat_field++; |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2117 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number, |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2118 // s2->picture_number, s->repeat_field); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2119 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) { |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2120 *data_size = sizeof(AVPicture); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2121 goto the_end; |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2122 } |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2123 } |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2124 #endif |
0 | 2125 while (buf_ptr < buf_end) { |
2126 buf_start = buf_ptr; | |
2127 /* find start next code */ | |
2128 code = find_start_code(&buf_ptr, buf_end, &s->header_state); | |
2129 if (code >= 0) { | |
2130 start_code_found = 1; | |
2131 } else { | |
2132 start_code_found = 0; | |
2133 } | |
2134 /* copy to buffer */ | |
2135 len = buf_ptr - buf_start; | |
2136 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
2137 /* data too big : flush */ | |
2138 s->buf_ptr = s->buffer; | |
2139 if (start_code_found) | |
2140 s->start_code = code; | |
2141 } else { | |
2142 memcpy(s->buf_ptr, buf_start, len); | |
2143 s->buf_ptr += len; | |
842
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
826
diff
changeset
|
2144 if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found) |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2145 && s->buf_ptr+4<s->buffer+s->buffer_size){ |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2146 start_code_found= 1; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2147 code= 0x1FF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2148 s->header_state=0xFF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2149 s->buf_ptr[0]=0; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2150 s->buf_ptr[1]=0; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2151 s->buf_ptr[2]=1; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2152 s->buf_ptr[3]=0xFF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2153 s->buf_ptr+=4; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2154 } |
0 | 2155 if (start_code_found) { |
2156 /* prepare data for next start code */ | |
2157 input_size = s->buf_ptr - s->buffer; | |
2158 start_code = s->start_code; | |
2159 s->buf_ptr = s->buffer; | |
2160 s->start_code = code; | |
2161 switch(start_code) { | |
2162 case SEQ_START_CODE: | |
2163 mpeg1_decode_sequence(avctx, s->buffer, | |
2164 input_size); | |
2165 break; | |
2166 | |
2167 case PICTURE_START_CODE: | |
2168 /* we have a complete image : we try to decompress it */ | |
2169 mpeg1_decode_picture(avctx, | |
2170 s->buffer, input_size); | |
2171 break; | |
2172 case EXT_START_CODE: | |
2173 mpeg_decode_extension(avctx, | |
2174 s->buffer, input_size); | |
2175 break; | |
1084 | 2176 case USER_START_CODE: |
2177 mpeg_decode_user_data(avctx, | |
2178 s->buffer, input_size); | |
2179 break; | |
0 | 2180 default: |
2181 if (start_code >= SLICE_MIN_START_CODE && | |
911 | 2182 start_code <= SLICE_MAX_START_CODE) { |
917 | 2183 |
2184 /* skip b frames if we dont have reference frames */ | |
1138 | 2185 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break; |
917 | 2186 /* skip b frames if we are in a hurry */ |
2187 if(avctx->hurry_up && s2->pict_type==B_TYPE) break; | |
2188 /* skip everything if we are in a hurry>=5 */ | |
2189 if(avctx->hurry_up>=5) break; | |
2190 | |
0 | 2191 ret = mpeg_decode_slice(avctx, picture, |
2192 start_code, s->buffer, input_size); | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
2193 |
826 | 2194 if (ret == DECODE_SLICE_EOP) { |
0 | 2195 *data_size = sizeof(AVPicture); |
2196 goto the_end; | |
825 | 2197 }else if(ret<0){ |
890
653d9419ea01
dont put flies in the stdout soup patch by (Agent Smith <asmith at wgz dot com>)
michaelni
parents:
885
diff
changeset
|
2198 fprintf(stderr,"Error while decoding slice\n"); |
826 | 2199 if(ret==DECODE_SLICE_FATAL_ERROR) return -1; |
0 | 2200 } |
2201 } | |
2202 break; | |
2203 } | |
2204 } | |
2205 } | |
2206 } | |
2207 the_end: | |
2208 return buf_ptr - buf; | |
2209 } | |
2210 | |
2211 static int mpeg_decode_end(AVCodecContext *avctx) | |
2212 { | |
2213 Mpeg1Context *s = avctx->priv_data; | |
2214 | |
2215 if (s->mpeg_enc_ctx_allocated) | |
2216 MPV_common_end(&s->mpeg_enc_ctx); | |
2217 return 0; | |
2218 } | |
2219 | |
2220 AVCodec mpeg_decoder = { | |
2221 "mpegvideo", | |
2222 CODEC_TYPE_VIDEO, | |
2223 CODEC_ID_MPEG1VIDEO, | |
2224 sizeof(Mpeg1Context), | |
2225 mpeg_decode_init, | |
2226 NULL, | |
2227 mpeg_decode_end, | |
2228 mpeg_decode_frame, | |
842
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
826
diff
changeset
|
2229 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, |
0 | 2230 }; |