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