Mercurial > libavcodec.hg
annotate mpeg12.c @ 275:7ebb3f9aaf3b libavcodec
- Added video coding statistics for ffmpeg. Needs more work.
- Fixed ME range for H.263+ UMV (32 pix).
| author | pulento |
|---|---|
| date | Tue, 19 Mar 2002 06:30:41 +0000 |
| parents | e10840e4f773 |
| children | 1e2f9ef286d4 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * MPEG1 encoder / MPEG2 decoder | |
| 3 * Copyright (c) 2000,2001 Gerard Lantau. | |
| 4 * | |
| 5 * This program is free software; you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License as published by | |
| 7 * the Free Software Foundation; either version 2 of the License, or | |
| 8 * (at your option) any later version. | |
| 9 * | |
| 10 * This program is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 * GNU General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU General Public License | |
| 16 * along with this program; if not, write to the Free Software | |
| 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 18 */ | |
| 76 | 19 //#define DEBUG |
| 0 | 20 #include "avcodec.h" |
| 21 #include "dsputil.h" | |
| 22 #include "mpegvideo.h" | |
| 23 | |
| 24 #include "mpeg12data.h" | |
| 25 | |
| 26 /* Start codes. */ | |
| 27 #define SEQ_END_CODE 0x000001b7 | |
| 28 #define SEQ_START_CODE 0x000001b3 | |
| 29 #define GOP_START_CODE 0x000001b8 | |
| 30 #define PICTURE_START_CODE 0x00000100 | |
| 31 #define SLICE_MIN_START_CODE 0x00000101 | |
| 32 #define SLICE_MAX_START_CODE 0x000001af | |
| 33 #define EXT_START_CODE 0x000001b5 | |
| 34 #define USER_START_CODE 0x000001b2 | |
| 35 | |
|
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
36 #define ABS(a) ((a)<0 ? -(a) : (a)) |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
37 |
| 0 | 38 static void mpeg1_encode_block(MpegEncContext *s, |
| 39 DCTELEM *block, | |
| 40 int component); | |
| 41 static void mpeg1_encode_motion(MpegEncContext *s, int val); | |
| 42 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); | |
| 43 static int mpeg1_decode_block(MpegEncContext *s, | |
| 44 DCTELEM *block, | |
| 45 int n); | |
| 46 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | |
| 47 DCTELEM *block, | |
| 48 int n); | |
| 49 static int mpeg2_decode_block_intra(MpegEncContext *s, | |
| 50 DCTELEM *block, | |
| 51 int n); | |
| 52 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); | |
| 53 | |
| 54 static void put_header(MpegEncContext *s, int header) | |
| 55 { | |
| 56 align_put_bits(&s->pb); | |
| 236 | 57 put_bits(&s->pb, 16, header>>16); |
| 58 put_bits(&s->pb, 16, header&0xFFFF); | |
| 0 | 59 } |
| 60 | |
| 61 /* put sequence header if needed */ | |
| 62 static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
| 63 { | |
| 64 unsigned int vbv_buffer_size; | |
| 1 | 65 unsigned int fps, v; |
| 66 int n; | |
| 0 | 67 UINT64 time_code; |
| 68 | |
| 69 if ((s->picture_number % s->gop_size) == 0) { | |
| 70 /* mpeg1 header repeated every gop */ | |
| 71 put_header(s, SEQ_START_CODE); | |
| 72 | |
| 73 /* search closest frame rate */ | |
| 74 { | |
| 75 int i, dmin, d; | |
| 76 s->frame_rate_index = 0; | |
| 77 dmin = 0x7fffffff; | |
| 78 for(i=1;i<9;i++) { | |
| 79 d = abs(s->frame_rate - frame_rate_tab[i]); | |
| 80 if (d < dmin) { | |
| 81 dmin = d; | |
| 82 s->frame_rate_index = i; | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 put_bits(&s->pb, 12, s->width); | |
| 88 put_bits(&s->pb, 12, s->height); | |
| 89 put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */ | |
| 90 put_bits(&s->pb, 4, s->frame_rate_index); | |
| 91 v = s->bit_rate / 400; | |
| 92 if (v > 0x3ffff) | |
| 93 v = 0x3ffff; | |
| 94 put_bits(&s->pb, 18, v); | |
| 95 put_bits(&s->pb, 1, 1); /* marker */ | |
| 96 /* vbv buffer size: slightly greater than an I frame. We add | |
| 97 some margin just in case */ | |
| 98 vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8); | |
| 99 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); | |
| 100 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ | |
| 101 put_bits(&s->pb, 1, 0); /* no custom intra matrix */ | |
| 102 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */ | |
| 103 | |
| 104 put_header(s, GOP_START_CODE); | |
| 105 put_bits(&s->pb, 1, 0); /* do drop frame */ | |
| 106 /* time code : we must convert from the real frame rate to a | |
| 107 fake mpeg frame rate in case of low frame rate */ | |
| 108 fps = frame_rate_tab[s->frame_rate_index]; | |
| 241 | 109 time_code = (INT64)s->fake_picture_number * FRAME_RATE_BASE; |
| 0 | 110 s->gop_picture_number = s->fake_picture_number; |
| 64 | 111 put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24)); |
| 112 put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60)); | |
| 0 | 113 put_bits(&s->pb, 1, 1); |
| 64 | 114 put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60)); |
| 115 put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE)); | |
| 0 | 116 put_bits(&s->pb, 1, 1); /* closed gop */ |
| 117 put_bits(&s->pb, 1, 0); /* broken link */ | |
| 118 } | |
| 119 | |
| 120 if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) { | |
| 121 /* insert empty P pictures to slow down to the desired | |
| 122 frame rate. Each fake pictures takes about 20 bytes */ | |
| 123 fps = frame_rate_tab[s->frame_rate_index]; | |
| 241 | 124 n = (((INT64)s->picture_number * fps) / s->frame_rate) - 1; |
| 0 | 125 while (s->fake_picture_number < n) { |
| 126 mpeg1_skip_picture(s, s->fake_picture_number - | |
| 127 s->gop_picture_number); | |
| 128 s->fake_picture_number++; | |
| 129 } | |
| 130 | |
| 131 } | |
| 132 s->fake_picture_number++; | |
| 133 } | |
| 134 | |
| 135 | |
| 136 /* insert a fake P picture */ | |
| 137 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) | |
| 138 { | |
| 139 unsigned int mb_incr; | |
| 140 | |
| 141 /* mpeg1 picture header */ | |
| 142 put_header(s, PICTURE_START_CODE); | |
| 143 /* temporal reference */ | |
| 144 put_bits(&s->pb, 10, pict_num & 0x3ff); | |
| 145 | |
| 146 put_bits(&s->pb, 3, P_TYPE); | |
| 147 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
| 148 | |
| 149 put_bits(&s->pb, 1, 1); /* integer coordinates */ | |
| 150 put_bits(&s->pb, 3, 1); /* forward_f_code */ | |
| 151 | |
| 152 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
| 153 | |
| 154 /* only one slice */ | |
| 155 put_header(s, SLICE_MIN_START_CODE); | |
| 156 put_bits(&s->pb, 5, 1); /* quantizer scale */ | |
| 157 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
| 158 | |
| 159 mb_incr = 1; | |
| 160 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 161 mbAddrIncrTable[mb_incr - 1][0]); | |
| 162 | |
| 163 /* empty macroblock */ | |
| 164 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 165 | |
| 166 /* zero motion x & y */ | |
| 167 put_bits(&s->pb, 1, 1); | |
| 168 put_bits(&s->pb, 1, 1); | |
| 169 | |
| 170 /* output a number of empty slice */ | |
| 171 mb_incr = s->mb_width * s->mb_height - 1; | |
| 172 while (mb_incr > 33) { | |
| 173 put_bits(&s->pb, 11, 0x008); | |
| 174 mb_incr -= 33; | |
| 175 } | |
| 176 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 177 mbAddrIncrTable[mb_incr - 1][0]); | |
| 178 | |
| 179 /* empty macroblock */ | |
| 180 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 181 | |
| 182 /* zero motion x & y */ | |
| 183 put_bits(&s->pb, 1, 1); | |
| 184 put_bits(&s->pb, 1, 1); | |
| 185 } | |
| 186 | |
| 187 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) | |
| 188 { | |
| 236 | 189 static int done=0; |
| 0 | 190 |
| 191 if (!done) { | |
| 236 | 192 int i; |
| 0 | 193 done = 1; |
| 194 init_rl(&rl_mpeg1); | |
| 236 | 195 |
| 196 for(i=0; i<64; i++) | |
| 197 { | |
| 198 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i]; | |
| 199 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i]; | |
| 200 } | |
|
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
201 |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
202 /* build unified dc encoding tables */ |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
203 for(i=-255; i<256; i++) |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
204 { |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
205 int adiff, index; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
206 int bits, code; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
207 int diff=i; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
208 |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
209 adiff = ABS(diff); |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
210 if(diff<0) diff--; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
211 index = vlc_dc_table[adiff]; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
212 |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
213 bits= vlc_dc_lum_bits[index] + index; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
214 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)); |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
215 mpeg1_lum_dc_uni[i+255]= bits + (code<<8); |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
216 |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
217 bits= vlc_dc_chroma_bits[index] + index; |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
218 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)); |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
219 mpeg1_chr_dc_uni[i+255]= bits + (code<<8); |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
220 } |
| 0 | 221 } |
| 222 mpeg1_encode_sequence_header(s); | |
| 223 | |
| 224 /* mpeg1 picture header */ | |
| 225 put_header(s, PICTURE_START_CODE); | |
| 226 /* temporal reference */ | |
| 227 put_bits(&s->pb, 10, (s->fake_picture_number - | |
| 228 s->gop_picture_number) & 0x3ff); | |
| 229 | |
| 230 put_bits(&s->pb, 3, s->pict_type); | |
| 231 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
| 232 | |
| 233 if (s->pict_type == P_TYPE) { | |
| 234 put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
| 235 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
| 236 } | |
| 237 | |
| 238 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
| 239 | |
| 240 /* only one slice */ | |
| 241 put_header(s, SLICE_MIN_START_CODE); | |
| 242 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */ | |
| 243 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
| 244 } | |
| 245 | |
| 246 void mpeg1_encode_mb(MpegEncContext *s, | |
| 247 DCTELEM block[6][64], | |
| 248 int motion_x, int motion_y) | |
| 249 { | |
| 250 int mb_incr, i, cbp, mb_x, mb_y; | |
| 251 | |
| 252 mb_x = s->mb_x; | |
| 253 mb_y = s->mb_y; | |
| 254 | |
| 255 /* compute cbp */ | |
| 256 cbp = 0; | |
| 257 for(i=0;i<6;i++) { | |
| 258 if (s->block_last_index[i] >= 0) | |
| 259 cbp |= 1 << (5 - i); | |
| 260 } | |
| 261 | |
| 262 /* skip macroblock, except if first or last macroblock of a slice */ | |
| 263 if ((cbp | motion_x | motion_y) == 0 && | |
| 264 (!((mb_x | mb_y) == 0 || | |
| 265 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) { | |
| 266 s->mb_incr++; | |
| 267 } else { | |
| 268 /* output mb incr */ | |
| 269 mb_incr = s->mb_incr; | |
| 270 | |
| 271 while (mb_incr > 33) { | |
| 272 put_bits(&s->pb, 11, 0x008); | |
| 273 mb_incr -= 33; | |
| 274 } | |
| 275 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 276 mbAddrIncrTable[mb_incr - 1][0]); | |
| 277 | |
| 278 if (s->pict_type == I_TYPE) { | |
| 279 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ | |
| 280 } else { | |
| 281 if (s->mb_intra) { | |
| 282 put_bits(&s->pb, 5, 0x03); | |
| 283 } else { | |
| 284 if (cbp != 0) { | |
| 285 if (motion_x == 0 && motion_y == 0) { | |
| 286 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */ | |
| 287 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); | |
| 288 } else { | |
| 289 put_bits(&s->pb, 1, 1); /* motion + cbp */ | |
| 290 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); | |
| 291 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); | |
| 292 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); | |
| 293 } | |
| 294 } else { | |
| 295 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 296 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); | |
| 297 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); | |
| 298 } | |
| 299 } | |
| 300 } | |
| 301 for(i=0;i<6;i++) { | |
| 302 if (cbp & (1 << (5 - i))) { | |
| 303 mpeg1_encode_block(s, block[i], i); | |
| 304 } | |
| 305 } | |
| 306 s->mb_incr = 1; | |
| 307 } | |
| 308 s->last_mv[0][0][0] = motion_x; | |
| 309 s->last_mv[0][0][1] = motion_y; | |
| 310 } | |
| 311 | |
| 312 static void mpeg1_encode_motion(MpegEncContext *s, int val) | |
| 313 { | |
| 314 int code, bit_size, l, m, bits, range, sign; | |
| 315 | |
| 316 if (val == 0) { | |
| 317 /* zero vector */ | |
| 318 code = 0; | |
| 319 put_bits(&s->pb, | |
| 320 mbMotionVectorTable[0][1], | |
| 321 mbMotionVectorTable[0][0]); | |
| 322 } else { | |
| 323 bit_size = s->f_code - 1; | |
| 324 range = 1 << bit_size; | |
| 325 /* modulo encoding */ | |
| 326 l = 16 * range; | |
| 327 m = 2 * l; | |
| 328 if (val < -l) { | |
| 329 val += m; | |
| 330 } else if (val >= l) { | |
| 331 val -= m; | |
| 332 } | |
| 333 | |
| 334 if (val >= 0) { | |
| 335 val--; | |
| 336 code = (val >> bit_size) + 1; | |
| 337 bits = val & (range - 1); | |
| 338 sign = 0; | |
| 339 } else { | |
| 340 val = -val; | |
| 341 val--; | |
| 342 code = (val >> bit_size) + 1; | |
| 343 bits = val & (range - 1); | |
| 344 sign = 1; | |
| 345 } | |
| 346 put_bits(&s->pb, | |
| 347 mbMotionVectorTable[code][1], | |
| 348 mbMotionVectorTable[code][0]); | |
| 349 put_bits(&s->pb, 1, sign); | |
| 350 if (bit_size > 0) { | |
| 351 put_bits(&s->pb, bit_size, bits); | |
| 352 } | |
| 353 } | |
| 354 } | |
| 355 | |
| 356 static inline void encode_dc(MpegEncContext *s, int diff, int component) | |
| 357 { | |
| 358 if (component == 0) { | |
|
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
359 put_bits( |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
360 &s->pb, |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
361 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
|
362 mpeg1_lum_dc_uni[diff+255]>>8); |
| 0 | 363 } else { |
|
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
364 put_bits( |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
365 &s->pb, |
|
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
366 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
|
367 mpeg1_chr_dc_uni[diff+255]>>8); |
| 0 | 368 } |
| 369 } | |
| 370 | |
| 371 static void mpeg1_encode_block(MpegEncContext *s, | |
| 372 DCTELEM *block, | |
| 373 int n) | |
| 374 { | |
| 375 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | |
| 376 int code, component; | |
| 236 | 377 // RLTable *rl = &rl_mpeg1; |
| 0 | 378 |
| 379 last_index = s->block_last_index[n]; | |
| 380 | |
| 381 /* DC coef */ | |
| 382 if (s->mb_intra) { | |
| 383 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 384 dc = block[0]; /* overflow is impossible */ | |
| 385 diff = dc - s->last_dc[component]; | |
| 386 encode_dc(s, diff, component); | |
| 387 s->last_dc[component] = dc; | |
| 388 i = 1; | |
| 389 } else { | |
| 390 /* encode the first coefficient : needs to be done here because | |
| 391 it is handled slightly differently */ | |
| 392 level = block[0]; | |
| 393 if (abs(level) == 1) { | |
| 394 code = ((UINT32)level >> 31); /* the sign bit */ | |
| 395 put_bits(&s->pb, 2, code | 0x02); | |
| 396 i = 1; | |
| 397 } else { | |
| 398 i = 0; | |
| 399 last_non_zero = -1; | |
| 400 goto next_coef; | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 /* now quantify & encode AC coefs */ | |
| 405 last_non_zero = i - 1; | |
| 236 | 406 |
| 0 | 407 for(;i<=last_index;i++) { |
| 408 j = zigzag_direct[i]; | |
| 409 level = block[j]; | |
| 410 next_coef: | |
| 411 #if 0 | |
| 412 if (level != 0) | |
| 413 dprintf("level[%d]=%d\n", i, level); | |
| 414 #endif | |
| 415 /* encode using VLC */ | |
| 416 if (level != 0) { | |
| 417 run = i - last_non_zero - 1; | |
| 236 | 418 #ifdef ARCH_X86 |
| 419 asm volatile( | |
| 420 "movl %2, %1 \n\t" | |
| 421 "movl %1, %0 \n\t" | |
| 422 "addl %1, %1 \n\t" | |
| 423 "sbbl %1, %1 \n\t" | |
| 424 "xorl %1, %0 \n\t" | |
| 425 "subl %1, %0 \n\t" | |
| 426 "andl $1, %1 \n\t" | |
| 427 : "=&r" (alevel), "=&r" (sign) | |
| 428 : "g" (level) | |
| 429 ); | |
| 430 #else | |
| 0 | 431 sign = 0; |
| 432 alevel = level; | |
| 433 if (alevel < 0) { | |
| 434 sign = 1; | |
| 435 alevel = -alevel; | |
| 436 } | |
| 236 | 437 #endif |
| 438 // code = get_rl_index(rl, 0, run, alevel); | |
| 439 if (alevel > mpeg1_max_level[0][run]) | |
| 440 code= 111; /*rl->n*/ | |
| 441 else | |
| 442 code= mpeg1_index_run[0][run] + alevel - 1; | |
| 443 | |
| 444 if (code < 111 /* rl->n */) { | |
| 445 /* store the vlc & sign at once */ | |
| 446 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); | |
| 0 | 447 } else { |
| 236 | 448 /* escape seems to be pretty rare <5% so i dont optimize it */ |
| 449 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); | |
| 0 | 450 /* escape: only clip in this case */ |
| 451 put_bits(&s->pb, 6, run); | |
| 452 if (alevel < 128) { | |
| 453 put_bits(&s->pb, 8, level & 0xff); | |
| 454 } else { | |
| 455 if (level < 0) { | |
| 456 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
| 457 } else { | |
| 458 put_bits(&s->pb, 16, level & 0xffff); | |
| 459 } | |
| 460 } | |
| 461 } | |
| 462 last_non_zero = i; | |
| 463 } | |
| 464 } | |
| 465 /* end of block */ | |
| 466 put_bits(&s->pb, 2, 0x2); | |
| 467 } | |
| 468 | |
| 469 /******************************************/ | |
| 470 /* decoding */ | |
| 471 | |
| 472 static VLC dc_lum_vlc; | |
| 473 static VLC dc_chroma_vlc; | |
| 474 static VLC mv_vlc; | |
| 475 static VLC mbincr_vlc; | |
| 476 static VLC mb_ptype_vlc; | |
| 477 static VLC mb_btype_vlc; | |
| 478 static VLC mb_pat_vlc; | |
| 479 | |
| 480 void mpeg1_init_vlc(MpegEncContext *s) | |
| 481 { | |
| 482 static int done = 0; | |
| 483 | |
| 484 if (!done) { | |
| 485 | |
| 486 init_vlc(&dc_lum_vlc, 9, 12, | |
| 487 vlc_dc_lum_bits, 1, 1, | |
| 488 vlc_dc_lum_code, 2, 2); | |
| 489 init_vlc(&dc_chroma_vlc, 9, 12, | |
| 490 vlc_dc_chroma_bits, 1, 1, | |
| 491 vlc_dc_chroma_code, 2, 2); | |
| 492 init_vlc(&mv_vlc, 9, 17, | |
| 493 &mbMotionVectorTable[0][1], 2, 1, | |
| 494 &mbMotionVectorTable[0][0], 2, 1); | |
|
45
933cc4acab5c
fixed mpeg1 last block bug (mb stuffing code was not included in vlc table...)
glantau
parents:
38
diff
changeset
|
495 init_vlc(&mbincr_vlc, 9, 35, |
| 0 | 496 &mbAddrIncrTable[0][1], 2, 1, |
| 497 &mbAddrIncrTable[0][0], 2, 1); | |
| 498 init_vlc(&mb_pat_vlc, 9, 63, | |
| 499 &mbPatTable[0][1], 2, 1, | |
| 500 &mbPatTable[0][0], 2, 1); | |
| 501 | |
| 502 init_vlc(&mb_ptype_vlc, 6, 32, | |
| 503 &table_mb_ptype[0][1], 2, 1, | |
| 504 &table_mb_ptype[0][0], 2, 1); | |
| 505 init_vlc(&mb_btype_vlc, 6, 32, | |
| 506 &table_mb_btype[0][1], 2, 1, | |
| 507 &table_mb_btype[0][0], 2, 1); | |
| 508 init_rl(&rl_mpeg1); | |
| 509 init_rl(&rl_mpeg2); | |
| 510 /* cannot use generic init because we must add the EOB code */ | |
| 511 init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, | |
| 512 &rl_mpeg1.table_vlc[0][1], 4, 2, | |
| 513 &rl_mpeg1.table_vlc[0][0], 4, 2); | |
| 514 init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, | |
| 515 &rl_mpeg2.table_vlc[0][1], 4, 2, | |
| 516 &rl_mpeg2.table_vlc[0][0], 4, 2); | |
| 517 } | |
| 518 } | |
| 519 | |
| 520 static inline int get_dmv(MpegEncContext *s) | |
| 521 { | |
| 21 | 522 if(get_bits1(&s->gb)) |
| 523 return 1 - (get_bits1(&s->gb) << 1); | |
| 0 | 524 else |
| 525 return 0; | |
| 526 } | |
| 527 | |
| 54 | 528 static inline int get_qscale(MpegEncContext *s) |
| 529 { | |
| 530 int qscale; | |
| 531 if (s->mpeg2) { | |
| 532 if (s->q_scale_type) { | |
| 533 qscale = non_linear_qscale[get_bits(&s->gb, 5)]; | |
| 534 } else { | |
| 535 qscale = get_bits(&s->gb, 5) << 1; | |
| 536 } | |
| 537 } else { | |
| 538 /* for mpeg1, we use the generic unquant code */ | |
| 539 qscale = get_bits(&s->gb, 5); | |
| 540 } | |
| 541 return qscale; | |
| 542 } | |
| 543 | |
| 0 | 544 /* motion type (for mpeg2) */ |
| 545 #define MT_FIELD 1 | |
| 546 #define MT_FRAME 2 | |
| 547 #define MT_16X8 2 | |
| 548 #define MT_DMV 3 | |
| 549 | |
| 550 static int mpeg_decode_mb(MpegEncContext *s, | |
| 551 DCTELEM block[6][64]) | |
| 552 { | |
| 553 int i, j, k, cbp, val, code, mb_type, motion_type; | |
| 554 | |
| 555 /* skip mb handling */ | |
| 556 if (s->mb_incr == 0) { | |
| 557 /* read again increment */ | |
| 558 s->mb_incr = 1; | |
| 559 for(;;) { | |
| 560 code = get_vlc(&s->gb, &mbincr_vlc); | |
| 561 if (code < 0) | |
| 562 return 1; /* error = end of slice */ | |
| 563 if (code >= 33) { | |
| 564 if (code == 33) { | |
| 565 s->mb_incr += 33; | |
| 566 } | |
| 567 /* otherwise, stuffing, nothing to do */ | |
| 568 } else { | |
| 569 s->mb_incr += code; | |
| 570 break; | |
| 571 } | |
| 572 } | |
| 573 } | |
| 574 if (++s->mb_x >= s->mb_width) { | |
| 575 s->mb_x = 0; | |
| 576 if (s->mb_y >= (s->mb_height - 1)) | |
| 577 return -1; | |
| 578 s->mb_y++; | |
| 579 } | |
| 580 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y); | |
| 581 | |
| 582 if (--s->mb_incr != 0) { | |
| 583 /* skip mb */ | |
| 584 s->mb_intra = 0; | |
| 585 for(i=0;i<6;i++) | |
| 586 s->block_last_index[i] = -1; | |
| 587 s->mv_type = MV_TYPE_16X16; | |
| 588 if (s->pict_type == P_TYPE) { | |
| 589 /* if P type, zero motion vector is implied */ | |
| 590 s->mv_dir = MV_DIR_FORWARD; | |
| 591 s->mv[0][0][0] = s->mv[0][0][1] = 0; | |
| 592 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
|
593 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0; |
| 0 | 594 } else { |
| 595 /* if B type, reuse previous vectors and directions */ | |
| 596 s->mv[0][0][0] = s->last_mv[0][0][0]; | |
| 597 s->mv[0][0][1] = s->last_mv[0][0][1]; | |
| 598 s->mv[1][0][0] = s->last_mv[1][0][0]; | |
| 599 s->mv[1][0][1] = s->last_mv[1][0][1]; | |
| 600 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
1
diff
changeset
|
601 s->mb_skiped = 1; |
| 0 | 602 return 0; |
| 603 } | |
| 604 | |
| 605 switch(s->pict_type) { | |
| 606 default: | |
| 607 case I_TYPE: | |
| 21 | 608 if (get_bits1(&s->gb) == 0) { |
| 609 if (get_bits1(&s->gb) == 0) | |
| 0 | 610 return -1; |
| 611 mb_type = MB_QUANT | MB_INTRA; | |
| 612 } else { | |
| 613 mb_type = MB_INTRA; | |
| 614 } | |
| 615 break; | |
| 616 case P_TYPE: | |
| 617 mb_type = get_vlc(&s->gb, &mb_ptype_vlc); | |
| 618 if (mb_type < 0) | |
| 619 return -1; | |
| 620 break; | |
| 621 case B_TYPE: | |
| 622 mb_type = get_vlc(&s->gb, &mb_btype_vlc); | |
| 623 if (mb_type < 0) | |
| 624 return -1; | |
| 625 break; | |
| 626 } | |
| 627 dprintf("mb_type=%x\n", mb_type); | |
| 628 motion_type = 0; /* avoid warning */ | |
| 629 if (mb_type & (MB_FOR|MB_BACK)) { | |
| 630 /* get additionnal motion vector type */ | |
| 631 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) | |
| 632 motion_type = MT_FRAME; | |
| 633 else | |
| 634 motion_type = get_bits(&s->gb, 2); | |
| 635 } | |
| 636 /* compute dct type */ | |
| 637 if (s->picture_structure == PICT_FRAME && | |
| 638 !s->frame_pred_frame_dct && | |
| 639 (mb_type & (MB_PAT | MB_INTRA))) { | |
| 21 | 640 s->interlaced_dct = get_bits1(&s->gb); |
| 0 | 641 #ifdef DEBUG |
| 642 if (s->interlaced_dct) | |
| 643 printf("interlaced_dct\n"); | |
| 644 #endif | |
| 645 } else { | |
| 646 s->interlaced_dct = 0; /* frame based */ | |
| 647 } | |
| 648 | |
| 649 if (mb_type & MB_QUANT) { | |
| 54 | 650 s->qscale = get_qscale(s); |
| 0 | 651 } |
| 652 if (mb_type & MB_INTRA) { | |
| 653 if (s->concealment_motion_vectors) { | |
| 654 /* just parse them */ | |
| 655 if (s->picture_structure != PICT_FRAME) | |
| 21 | 656 skip_bits1(&s->gb); /* field select */ |
| 0 | 657 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0); |
| 658 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0); | |
| 659 } | |
| 660 s->mb_intra = 1; | |
| 661 cbp = 0x3f; | |
| 662 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | |
| 663 } else { | |
| 664 s->mb_intra = 0; | |
| 665 cbp = 0; | |
| 666 } | |
| 667 /* special case of implicit zero motion vector */ | |
| 668 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) { | |
| 669 s->mv_dir = MV_DIR_FORWARD; | |
| 670 s->mv_type = MV_TYPE_16X16; | |
| 671 s->last_mv[0][0][0] = 0; | |
| 672 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
|
673 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
|
674 s->last_mv[0][1][1] = 0; |
| 0 | 675 s->mv[0][0][0] = 0; |
| 676 s->mv[0][0][1] = 0; | |
| 677 } else if (mb_type & (MB_FOR | MB_BACK)) { | |
| 678 /* motion vectors */ | |
| 679 s->mv_dir = 0; | |
| 680 for(i=0;i<2;i++) { | |
| 681 if (mb_type & (MB_FOR >> i)) { | |
| 682 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
|
683 dprintf("motion_type=%d\n", motion_type); |
| 0 | 684 switch(motion_type) { |
| 685 case MT_FRAME: /* or MT_16X8 */ | |
| 686 if (s->picture_structure == PICT_FRAME) { | |
| 687 /* MT_FRAME */ | |
| 688 s->mv_type = MV_TYPE_16X16; | |
| 689 for(k=0;k<2;k++) { | |
| 690 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 691 s->last_mv[i][0][k]); | |
| 692 s->last_mv[i][0][k] = val; | |
| 693 s->last_mv[i][1][k] = val; | |
| 694 /* full_pel: only for mpeg1 */ | |
| 695 if (s->full_pel[i]) | |
| 696 val = val << 1; | |
| 697 s->mv[i][0][k] = val; | |
| 698 dprintf("mv%d: %d\n", k, val); | |
| 699 } | |
| 700 } else { | |
| 701 /* MT_16X8 */ | |
| 702 s->mv_type = MV_TYPE_16X8; | |
| 703 for(j=0;j<2;j++) { | |
| 21 | 704 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 705 for(k=0;k<2;k++) { |
| 706 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 707 s->last_mv[i][j][k]); | |
| 708 s->last_mv[i][j][k] = val; | |
| 709 s->mv[i][j][k] = val; | |
| 710 } | |
| 711 } | |
| 712 } | |
| 713 break; | |
| 714 case MT_FIELD: | |
| 715 if (s->picture_structure == PICT_FRAME) { | |
| 716 s->mv_type = MV_TYPE_FIELD; | |
| 717 for(j=0;j<2;j++) { | |
| 21 | 718 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 719 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0], |
| 720 s->last_mv[i][j][0]); | |
| 721 s->last_mv[i][j][0] = val; | |
| 722 s->mv[i][j][0] = val; | |
| 723 dprintf("fmx=%d\n", val); | |
| 724 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 725 s->last_mv[i][j][1] >> 1); | |
| 726 s->last_mv[i][j][1] = val << 1; | |
| 727 s->mv[i][j][1] = val; | |
| 728 dprintf("fmy=%d\n", val); | |
| 729 } | |
| 730 } else { | |
| 731 s->mv_type = MV_TYPE_16X16; | |
| 21 | 732 s->field_select[i][0] = get_bits1(&s->gb); |
| 0 | 733 for(k=0;k<2;k++) { |
| 734 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 735 s->last_mv[i][0][k]); | |
| 736 s->last_mv[i][0][k] = val; | |
| 737 s->last_mv[i][1][k] = val; | |
| 738 s->mv[i][0][k] = val; | |
| 739 } | |
| 740 } | |
| 741 break; | |
| 742 case MT_DMV: | |
| 743 { | |
| 744 int dmx, dmy, mx, my, m; | |
| 745 | |
| 746 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], | |
| 747 s->last_mv[i][0][0]); | |
| 748 s->last_mv[i][0][0] = mx; | |
| 749 s->last_mv[i][1][0] = mx; | |
| 750 dmx = get_dmv(s); | |
| 751 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 752 s->last_mv[i][0][1] >> 1); | |
| 753 dmy = get_dmv(s); | |
| 754 s->mv_type = MV_TYPE_DMV; | |
| 755 /* XXX: totally broken */ | |
| 756 if (s->picture_structure == PICT_FRAME) { | |
| 757 s->last_mv[i][0][1] = my << 1; | |
| 758 s->last_mv[i][1][1] = my << 1; | |
| 759 | |
| 760 m = s->top_field_first ? 1 : 3; | |
| 761 /* top -> top pred */ | |
| 762 s->mv[i][0][0] = mx; | |
| 763 s->mv[i][0][1] = my << 1; | |
| 764 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 765 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1; | |
| 766 m = 4 - m; | |
| 767 s->mv[i][2][0] = mx; | |
| 768 s->mv[i][2][1] = my << 1; | |
| 769 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 770 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; | |
| 771 } else { | |
| 772 s->last_mv[i][0][1] = my; | |
| 773 s->last_mv[i][1][1] = my; | |
| 774 s->mv[i][0][0] = mx; | |
| 775 s->mv[i][0][1] = my; | |
| 776 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx; | |
| 777 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 | |
| 778 /* + 2 * cur_field */; | |
| 779 } | |
| 780 } | |
| 781 break; | |
| 782 } | |
| 783 } | |
| 784 } | |
| 785 } | |
| 786 | |
| 787 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) { | |
| 21 | 788 skip_bits1(&s->gb); /* marker */ |
| 0 | 789 } |
| 790 | |
| 791 if (mb_type & MB_PAT) { | |
| 792 cbp = get_vlc(&s->gb, &mb_pat_vlc); | |
| 793 if (cbp < 0) | |
| 794 return -1; | |
| 795 cbp++; | |
| 796 } | |
| 797 dprintf("cbp=%x\n", cbp); | |
| 798 | |
| 799 if (s->mpeg2) { | |
| 800 if (s->mb_intra) { | |
| 801 for(i=0;i<6;i++) { | |
| 802 if (cbp & (1 << (5 - i))) { | |
| 803 if (mpeg2_decode_block_intra(s, block[i], i) < 0) | |
| 804 return -1; | |
| 805 } | |
| 806 } | |
| 807 } else { | |
| 808 for(i=0;i<6;i++) { | |
| 809 if (cbp & (1 << (5 - i))) { | |
| 810 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) | |
| 811 return -1; | |
| 812 } | |
| 813 } | |
| 814 } | |
| 815 } else { | |
| 816 for(i=0;i<6;i++) { | |
| 817 if (cbp & (1 << (5 - i))) { | |
| 818 if (mpeg1_decode_block(s, block[i], i) < 0) | |
| 819 return -1; | |
| 820 } | |
| 821 } | |
| 822 } | |
| 823 return 0; | |
| 824 } | |
| 825 | |
| 826 /* as h263, but only 17 codes */ | |
| 827 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) | |
| 828 { | |
| 829 int code, sign, val, m, l, shift; | |
| 830 | |
| 831 code = get_vlc(&s->gb, &mv_vlc); | |
| 832 if (code < 0) { | |
| 833 return 0xffff; | |
| 834 } | |
| 835 if (code == 0) { | |
| 836 return pred; | |
| 837 } | |
| 21 | 838 sign = get_bits1(&s->gb); |
| 0 | 839 shift = fcode - 1; |
| 840 val = (code - 1) << shift; | |
| 841 if (shift > 0) | |
| 842 val |= get_bits(&s->gb, shift); | |
| 843 val++; | |
| 844 if (sign) | |
| 845 val = -val; | |
| 846 val += pred; | |
| 847 | |
| 848 /* modulo decoding */ | |
| 849 l = (1 << shift) * 16; | |
| 850 m = 2 * l; | |
| 851 if (val < -l) { | |
| 852 val += m; | |
| 853 } else if (val >= l) { | |
| 854 val -= m; | |
| 855 } | |
| 856 return val; | |
| 857 } | |
| 858 | |
| 859 static inline int decode_dc(MpegEncContext *s, int component) | |
| 860 { | |
| 861 int code, diff; | |
| 862 | |
| 863 if (component == 0) { | |
| 864 code = get_vlc(&s->gb, &dc_lum_vlc); | |
| 865 } else { | |
| 866 code = get_vlc(&s->gb, &dc_chroma_vlc); | |
| 867 } | |
| 868 if (code < 0) | |
| 869 return 0xffff; | |
| 870 if (code == 0) { | |
| 871 diff = 0; | |
| 872 } else { | |
| 873 diff = get_bits(&s->gb, code); | |
| 874 if ((diff & (1 << (code - 1))) == 0) | |
| 875 diff = (-1 << code) | (diff + 1); | |
| 876 } | |
| 877 return diff; | |
| 878 } | |
| 879 | |
| 880 static int mpeg1_decode_block(MpegEncContext *s, | |
| 881 DCTELEM *block, | |
| 882 int n) | |
| 883 { | |
| 884 int level, dc, diff, i, j, run; | |
| 885 int code, component; | |
| 886 RLTable *rl = &rl_mpeg1; | |
| 887 | |
| 888 if (s->mb_intra) { | |
| 889 /* DC coef */ | |
| 890 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 891 diff = decode_dc(s, component); | |
| 892 if (diff >= 0xffff) | |
| 893 return -1; | |
| 894 dc = s->last_dc[component]; | |
| 895 dc += diff; | |
| 896 s->last_dc[component] = dc; | |
| 897 block[0] = dc; | |
| 898 dprintf("dc=%d diff=%d\n", dc, diff); | |
| 899 i = 1; | |
| 900 } else { | |
| 901 int bit_cnt, v; | |
| 902 UINT32 bit_buf; | |
| 903 UINT8 *buf_ptr; | |
| 904 i = 0; | |
| 905 /* special case for the first coef. no need to add a second vlc table */ | |
| 906 SAVE_BITS(&s->gb); | |
| 907 SHOW_BITS(&s->gb, v, 2); | |
| 908 if (v & 2) { | |
| 909 run = 0; | |
| 910 level = 1 - ((v & 1) << 1); | |
| 911 FLUSH_BITS(2); | |
| 912 RESTORE_BITS(&s->gb); | |
| 913 goto add_coef; | |
| 914 } | |
| 915 RESTORE_BITS(&s->gb); | |
| 916 } | |
| 917 | |
| 918 /* now quantify & encode AC coefs */ | |
| 919 for(;;) { | |
| 920 code = get_vlc(&s->gb, &rl->vlc); | |
| 921 if (code < 0) { | |
| 922 return -1; | |
| 923 } | |
| 924 if (code == 112) { | |
| 925 break; | |
| 926 } else if (code == 111) { | |
| 927 /* escape */ | |
| 928 run = get_bits(&s->gb, 6); | |
| 929 level = get_bits(&s->gb, 8); | |
| 930 level = (level << 24) >> 24; | |
| 931 if (level == -128) { | |
| 932 level = get_bits(&s->gb, 8) - 256; | |
| 933 } else if (level == 0) { | |
| 934 level = get_bits(&s->gb, 8); | |
| 935 } | |
| 936 } else { | |
| 937 run = rl->table_run[code]; | |
| 938 level = rl->table_level[code]; | |
| 21 | 939 if (get_bits1(&s->gb)) |
| 0 | 940 level = -level; |
| 941 } | |
| 942 i += run; | |
| 943 if (i >= 64) | |
| 944 return -1; | |
| 945 add_coef: | |
| 946 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 947 j = zigzag_direct[i]; | |
| 948 block[j] = level; | |
| 949 i++; | |
| 950 } | |
|
240
e3e0094cb5c9
block_last_index was too large (in mpeg1 decoding)
michaelni
parents:
237
diff
changeset
|
951 s->block_last_index[n] = i-1; |
| 0 | 952 return 0; |
| 953 } | |
| 954 | |
| 955 /* Also does unquantization here, since I will never support mpeg2 | |
| 956 encoding */ | |
| 957 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | |
| 958 DCTELEM *block, | |
| 959 int n) | |
| 960 { | |
| 961 int level, i, j, run; | |
| 962 int code; | |
| 963 RLTable *rl = &rl_mpeg1; | |
| 964 const UINT8 *scan_table; | |
| 965 const UINT16 *matrix; | |
| 966 int mismatch; | |
| 967 | |
| 968 if (s->alternate_scan) | |
| 969 scan_table = ff_alternate_vertical_scan; | |
| 970 else | |
| 971 scan_table = zigzag_direct; | |
| 972 mismatch = 1; | |
| 973 | |
| 974 { | |
| 975 int bit_cnt, v; | |
| 976 UINT32 bit_buf; | |
| 977 UINT8 *buf_ptr; | |
| 978 i = 0; | |
| 979 if (n < 4) | |
| 980 matrix = s->non_intra_matrix; | |
| 981 else | |
| 982 matrix = s->chroma_non_intra_matrix; | |
| 983 | |
| 984 /* special case for the first coef. no need to add a second vlc table */ | |
| 985 SAVE_BITS(&s->gb); | |
| 986 SHOW_BITS(&s->gb, v, 2); | |
| 987 if (v & 2) { | |
| 988 run = 0; | |
| 989 level = 1 - ((v & 1) << 1); | |
| 990 FLUSH_BITS(2); | |
| 991 RESTORE_BITS(&s->gb); | |
| 992 goto add_coef; | |
| 993 } | |
| 994 RESTORE_BITS(&s->gb); | |
| 995 } | |
| 996 | |
| 997 /* now quantify & encode AC coefs */ | |
| 998 for(;;) { | |
| 999 code = get_vlc(&s->gb, &rl->vlc); | |
| 1000 if (code < 0) | |
| 1001 return -1; | |
| 1002 if (code == 112) { | |
| 1003 break; | |
| 1004 } else if (code == 111) { | |
| 1005 /* escape */ | |
| 1006 run = get_bits(&s->gb, 6); | |
| 1007 level = get_bits(&s->gb, 12); | |
| 1008 level = (level << 20) >> 20; | |
| 1009 } else { | |
| 1010 run = rl->table_run[code]; | |
| 1011 level = rl->table_level[code]; | |
| 21 | 1012 if (get_bits1(&s->gb)) |
| 0 | 1013 level = -level; |
| 1014 } | |
| 1015 i += run; | |
| 1016 if (i >= 64) | |
| 1017 return -1; | |
| 1018 add_coef: | |
| 1019 j = scan_table[i]; | |
| 1020 dprintf("%d: run=%d level=%d\n", n, run, level); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1021 /* XXX: optimize */ |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1022 if (level > 0) { |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1023 level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1024 } else { |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1025 level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1026 level = -level; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1027 } |
| 0 | 1028 /* XXX: is it really necessary to saturate since the encoder |
| 1029 knows whats going on ? */ | |
| 1030 mismatch ^= level; | |
| 1031 block[j] = level; | |
| 1032 i++; | |
| 1033 } | |
| 1034 block[63] ^= (mismatch & 1); | |
| 1035 s->block_last_index[n] = i; | |
| 1036 return 0; | |
| 1037 } | |
| 1038 | |
| 1039 static int mpeg2_decode_block_intra(MpegEncContext *s, | |
| 1040 DCTELEM *block, | |
| 1041 int n) | |
| 1042 { | |
| 1043 int level, dc, diff, i, j, run; | |
| 1044 int code, component; | |
| 1045 RLTable *rl; | |
| 1046 const UINT8 *scan_table; | |
| 1047 const UINT16 *matrix; | |
| 1048 int mismatch; | |
| 1049 | |
| 1050 if (s->alternate_scan) | |
| 1051 scan_table = ff_alternate_vertical_scan; | |
| 1052 else | |
| 1053 scan_table = zigzag_direct; | |
| 1054 | |
| 1055 /* DC coef */ | |
| 1056 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 1057 diff = decode_dc(s, component); | |
| 1058 if (diff >= 0xffff) | |
| 1059 return -1; | |
| 1060 dc = s->last_dc[component]; | |
| 1061 dc += diff; | |
| 1062 s->last_dc[component] = dc; | |
| 1063 block[0] = dc << (3 - s->intra_dc_precision); | |
| 1064 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
|
1065 mismatch = block[0] ^ 1; |
| 0 | 1066 i = 1; |
| 1067 if (s->intra_vlc_format) | |
| 1068 rl = &rl_mpeg2; | |
| 1069 else | |
| 1070 rl = &rl_mpeg1; | |
| 1071 if (n < 4) | |
| 1072 matrix = s->intra_matrix; | |
| 1073 else | |
| 1074 matrix = s->chroma_intra_matrix; | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1075 |
| 0 | 1076 /* now quantify & encode AC coefs */ |
| 1077 for(;;) { | |
| 1078 code = get_vlc(&s->gb, &rl->vlc); | |
| 1079 if (code < 0) | |
| 1080 return -1; | |
| 1081 if (code == 112) { | |
| 1082 break; | |
| 1083 } else if (code == 111) { | |
| 1084 /* escape */ | |
| 1085 run = get_bits(&s->gb, 6); | |
| 1086 level = get_bits(&s->gb, 12); | |
| 1087 level = (level << 20) >> 20; | |
| 1088 } else { | |
| 1089 run = rl->table_run[code]; | |
| 1090 level = rl->table_level[code]; | |
| 21 | 1091 if (get_bits1(&s->gb)) |
| 0 | 1092 level = -level; |
| 1093 } | |
| 1094 i += run; | |
| 1095 if (i >= 64) | |
| 1096 return -1; | |
| 1097 j = scan_table[i]; | |
| 1098 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 1099 level = (level * s->qscale * matrix[j]) / 16; | |
| 1100 /* XXX: is it really necessary to saturate since the encoder | |
| 1101 knows whats going on ? */ | |
| 1102 mismatch ^= level; | |
| 1103 block[j] = level; | |
| 1104 i++; | |
| 1105 } | |
| 1106 block[63] ^= (mismatch & 1); | |
| 1107 s->block_last_index[n] = i; | |
| 1108 return 0; | |
| 1109 } | |
| 1110 | |
| 1111 /* compressed picture size */ | |
| 1112 #define PICTURE_BUFFER_SIZE 100000 | |
| 1113 | |
| 1114 typedef struct Mpeg1Context { | |
| 1115 MpegEncContext mpeg_enc_ctx; | |
| 1116 UINT32 header_state; | |
| 1117 int start_code; /* current start code */ | |
| 1118 UINT8 buffer[PICTURE_BUFFER_SIZE]; | |
| 1119 UINT8 *buf_ptr; | |
| 1120 int buffer_size; | |
| 1121 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
|
1122 int repeat_field; /* true if we must repeat the field */ |
| 0 | 1123 } Mpeg1Context; |
| 1124 | |
| 1125 static int mpeg_decode_init(AVCodecContext *avctx) | |
| 1126 { | |
| 1127 Mpeg1Context *s = avctx->priv_data; | |
| 1128 | |
| 1129 s->header_state = 0xff; | |
| 1130 s->mpeg_enc_ctx_allocated = 0; | |
| 1131 s->buffer_size = PICTURE_BUFFER_SIZE; | |
| 1132 s->start_code = -1; | |
| 1133 s->buf_ptr = s->buffer; | |
| 1134 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
|
1135 s->repeat_field = 0; |
| 0 | 1136 return 0; |
| 1137 } | |
| 1138 | |
| 1139 /* return the 8 bit start code value and update the search | |
| 1140 state. Return -1 if no start code found */ | |
| 1141 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end, | |
| 1142 UINT32 *header_state) | |
| 1143 { | |
| 1144 UINT8 *buf_ptr; | |
| 1145 unsigned int state, v; | |
| 1146 int val; | |
| 1147 | |
| 1148 state = *header_state; | |
| 1149 buf_ptr = *pbuf_ptr; | |
| 1150 while (buf_ptr < buf_end) { | |
| 1151 v = *buf_ptr++; | |
| 1152 if (state == 0x000001) { | |
| 1153 state = ((state << 8) | v) & 0xffffff; | |
| 1154 val = state; | |
| 1155 goto found; | |
| 1156 } | |
| 1157 state = ((state << 8) | v) & 0xffffff; | |
| 1158 } | |
| 1159 val = -1; | |
| 1160 found: | |
| 1161 *pbuf_ptr = buf_ptr; | |
| 1162 *header_state = state; | |
| 1163 return val; | |
| 1164 } | |
| 1165 | |
| 1166 static int mpeg1_decode_picture(AVCodecContext *avctx, | |
| 1167 UINT8 *buf, int buf_size) | |
| 1168 { | |
| 1169 Mpeg1Context *s1 = avctx->priv_data; | |
| 1170 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1171 int ref, f_code; | |
| 1172 | |
| 1173 init_get_bits(&s->gb, buf, buf_size); | |
| 1174 | |
| 1175 ref = get_bits(&s->gb, 10); /* temporal ref */ | |
| 1176 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
|
1177 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); |
| 21 | 1178 skip_bits(&s->gb, 16); |
| 0 | 1179 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
| 21 | 1180 s->full_pel[0] = get_bits1(&s->gb); |
| 0 | 1181 f_code = get_bits(&s->gb, 3); |
| 1182 if (f_code == 0) | |
| 1183 return -1; | |
| 1184 s->mpeg_f_code[0][0] = f_code; | |
| 1185 s->mpeg_f_code[0][1] = f_code; | |
| 1186 } | |
| 1187 if (s->pict_type == B_TYPE) { | |
| 21 | 1188 s->full_pel[1] = get_bits1(&s->gb); |
| 0 | 1189 f_code = get_bits(&s->gb, 3); |
| 1190 if (f_code == 0) | |
| 1191 return -1; | |
| 1192 s->mpeg_f_code[1][0] = f_code; | |
| 1193 s->mpeg_f_code[1][1] = f_code; | |
| 1194 } | |
| 1195 s->y_dc_scale = 8; | |
| 1196 s->c_dc_scale = 8; | |
| 1197 s->first_slice = 1; | |
| 1198 return 0; | |
| 1199 } | |
| 1200 | |
| 1201 static void mpeg_decode_sequence_extension(MpegEncContext *s) | |
| 1202 { | |
| 1203 int horiz_size_ext, vert_size_ext; | |
| 1204 int bit_rate_ext, vbv_buf_ext, low_delay; | |
| 1205 int frame_rate_ext_n, frame_rate_ext_d; | |
| 1206 | |
| 21 | 1207 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
|
1208 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ |
| 21 | 1209 skip_bits(&s->gb, 2); /* chroma_format */ |
| 0 | 1210 horiz_size_ext = get_bits(&s->gb, 2); |
| 1211 vert_size_ext = get_bits(&s->gb, 2); | |
| 1212 s->width |= (horiz_size_ext << 12); | |
| 1213 s->height |= (vert_size_ext << 12); | |
| 1214 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ | |
| 1215 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; | |
| 21 | 1216 skip_bits1(&s->gb); /* marker */ |
| 0 | 1217 vbv_buf_ext = get_bits(&s->gb, 8); |
| 21 | 1218 low_delay = get_bits1(&s->gb); |
| 0 | 1219 frame_rate_ext_n = get_bits(&s->gb, 2); |
| 1220 frame_rate_ext_d = get_bits(&s->gb, 5); | |
| 1221 if (frame_rate_ext_d >= 1) | |
| 1222 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d; | |
| 1223 dprintf("sequence extension\n"); | |
| 1224 s->mpeg2 = 1; | |
| 1225 } | |
| 1226 | |
| 1227 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | |
| 1228 { | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1229 int i, v, j; |
| 0 | 1230 |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1231 dprintf("matrix extension\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1232 |
| 21 | 1233 if (get_bits1(&s->gb)) { |
| 0 | 1234 for(i=0;i<64;i++) { |
| 1235 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1236 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1237 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1238 s->chroma_intra_matrix[j] = v; |
| 0 | 1239 } |
| 1240 } | |
| 21 | 1241 if (get_bits1(&s->gb)) { |
| 0 | 1242 for(i=0;i<64;i++) { |
| 1243 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1244 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1245 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1246 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1247 } |
| 1248 } | |
| 21 | 1249 if (get_bits1(&s->gb)) { |
| 0 | 1250 for(i=0;i<64;i++) { |
| 1251 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1252 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1253 s->chroma_intra_matrix[j] = v; |
| 0 | 1254 } |
| 1255 } | |
| 21 | 1256 if (get_bits1(&s->gb)) { |
| 0 | 1257 for(i=0;i<64;i++) { |
| 1258 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1259 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1260 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1261 } |
| 1262 } | |
| 1263 } | |
| 1264 | |
| 1265 static void mpeg_decode_picture_coding_extension(MpegEncContext *s) | |
| 1266 { | |
| 1267 s->full_pel[0] = s->full_pel[1] = 0; | |
| 1268 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4); | |
| 1269 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4); | |
| 1270 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4); | |
| 1271 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4); | |
| 1272 s->intra_dc_precision = get_bits(&s->gb, 2); | |
| 1273 s->picture_structure = get_bits(&s->gb, 2); | |
| 21 | 1274 s->top_field_first = get_bits1(&s->gb); |
| 1275 s->frame_pred_frame_dct = get_bits1(&s->gb); | |
| 1276 s->concealment_motion_vectors = get_bits1(&s->gb); | |
| 1277 s->q_scale_type = get_bits1(&s->gb); | |
| 1278 s->intra_vlc_format = get_bits1(&s->gb); | |
| 1279 s->alternate_scan = get_bits1(&s->gb); | |
| 1280 s->repeat_first_field = get_bits1(&s->gb); | |
| 1281 s->chroma_420_type = get_bits1(&s->gb); | |
| 1282 s->progressive_frame = get_bits1(&s->gb); | |
| 0 | 1283 /* composite display not parsed */ |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1284 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
|
1285 dprintf("picture_structure=%d\n", s->picture_structure); |
| 0 | 1286 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
|
1287 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
|
1288 dprintf("alternate_scan=%d\n", s->alternate_scan); |
| 0 | 1289 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
|
1290 dprintf("progressive_frame=%d\n", s->progressive_frame); |
| 0 | 1291 } |
| 1292 | |
| 1293 static void mpeg_decode_extension(AVCodecContext *avctx, | |
| 1294 UINT8 *buf, int buf_size) | |
| 1295 { | |
| 1296 Mpeg1Context *s1 = avctx->priv_data; | |
| 1297 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1298 int ext_type; | |
| 1299 | |
| 1300 init_get_bits(&s->gb, buf, buf_size); | |
| 1301 | |
| 1302 ext_type = get_bits(&s->gb, 4); | |
| 1303 switch(ext_type) { | |
| 1304 case 0x1: | |
| 1305 /* sequence ext */ | |
| 1306 mpeg_decode_sequence_extension(s); | |
| 1307 break; | |
| 1308 case 0x3: | |
| 1309 /* quant matrix extension */ | |
| 1310 mpeg_decode_quant_matrix_extension(s); | |
| 1311 break; | |
| 1312 case 0x8: | |
| 1313 /* picture extension */ | |
| 1314 mpeg_decode_picture_coding_extension(s); | |
| 1315 break; | |
| 1316 } | |
| 1317 } | |
| 1318 | |
| 1319 /* return 1 if end of frame */ | |
| 1320 static int mpeg_decode_slice(AVCodecContext *avctx, | |
| 1321 AVPicture *pict, | |
| 1322 int start_code, | |
| 1323 UINT8 *buf, int buf_size) | |
| 1324 { | |
| 1325 Mpeg1Context *s1 = avctx->priv_data; | |
| 1326 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1327 int ret; | |
| 1328 | |
| 1329 start_code = (start_code - 1) & 0xff; | |
| 1330 if (start_code >= s->mb_height) | |
| 1331 return -1; | |
| 1332 s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
| 1333 s->last_dc[1] = s->last_dc[0]; | |
| 1334 s->last_dc[2] = s->last_dc[0]; | |
| 1335 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
| 1336 s->mb_x = -1; | |
| 1337 s->mb_y = start_code; | |
| 1338 s->mb_incr = 0; | |
| 1339 | |
| 1340 /* start frame decoding */ | |
| 1341 if (s->first_slice) { | |
| 1342 s->first_slice = 0; | |
| 1343 MPV_frame_start(s); | |
| 1344 } | |
| 1345 | |
| 1346 init_get_bits(&s->gb, buf, buf_size); | |
| 1347 | |
| 54 | 1348 s->qscale = get_qscale(s); |
| 0 | 1349 /* extra slice info */ |
| 21 | 1350 while (get_bits1(&s->gb) != 0) { |
| 1351 skip_bits(&s->gb, 8); | |
| 0 | 1352 } |
| 1353 | |
| 1354 for(;;) { | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1355 memset(s->block, 0, sizeof(s->block)); |
|
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1356 ret = mpeg_decode_mb(s, s->block); |
| 0 | 1357 dprintf("ret=%d\n", ret); |
| 1358 if (ret < 0) | |
| 1359 return -1; | |
| 1360 if (ret == 1) | |
| 1361 break; | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1362 MPV_decode_mb(s, s->block); |
| 0 | 1363 } |
| 1364 | |
| 1365 /* end of slice reached */ | |
| 1366 if (s->mb_x == (s->mb_width - 1) && | |
| 1367 s->mb_y == (s->mb_height - 1)) { | |
| 1368 /* end of image */ | |
| 1369 UINT8 **picture; | |
| 1370 | |
| 1371 MPV_frame_end(s); | |
| 1372 | |
| 1373 /* XXX: incorrect reported qscale for mpeg2 */ | |
| 1374 if (s->pict_type == B_TYPE) { | |
| 1375 picture = s->current_picture; | |
| 1376 avctx->quality = s->qscale; | |
| 1377 } else { | |
| 1378 /* latency of 1 frame for I and P frames */ | |
| 1379 /* XXX: use another variable than picture_number */ | |
| 1380 if (s->picture_number == 0) { | |
| 1381 picture = NULL; | |
| 1382 } else { | |
| 1383 picture = s->last_picture; | |
| 1384 avctx->quality = s->last_qscale; | |
| 1385 } | |
| 1386 s->last_qscale = s->qscale; | |
| 1387 s->picture_number++; | |
| 1388 } | |
| 1389 if (picture) { | |
| 1390 pict->data[0] = picture[0]; | |
| 1391 pict->data[1] = picture[1]; | |
| 1392 pict->data[2] = picture[2]; | |
| 1393 pict->linesize[0] = s->linesize; | |
| 1394 pict->linesize[1] = s->linesize / 2; | |
| 1395 pict->linesize[2] = s->linesize / 2; | |
| 1396 return 1; | |
| 1397 } else { | |
| 1398 return 0; | |
| 1399 } | |
| 1400 } else { | |
| 1401 return 0; | |
| 1402 } | |
| 1403 } | |
| 1404 | |
| 1405 static int mpeg1_decode_sequence(AVCodecContext *avctx, | |
| 1406 UINT8 *buf, int buf_size) | |
| 1407 { | |
| 1408 Mpeg1Context *s1 = avctx->priv_data; | |
| 1409 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1410 int width, height, i, v, j; |
| 0 | 1411 |
| 1412 init_get_bits(&s->gb, buf, buf_size); | |
| 1413 | |
| 1414 width = get_bits(&s->gb, 12); | |
| 1415 height = get_bits(&s->gb, 12); | |
| 21 | 1416 skip_bits(&s->gb, 4); |
| 0 | 1417 s->frame_rate_index = get_bits(&s->gb, 4); |
| 1418 if (s->frame_rate_index == 0) | |
| 1419 return -1; | |
| 1420 s->bit_rate = get_bits(&s->gb, 18) * 400; | |
| 21 | 1421 if (get_bits1(&s->gb) == 0) /* marker */ |
| 0 | 1422 return -1; |
| 1423 if (width <= 0 || height <= 0 || | |
| 1424 (width % 2) != 0 || (height % 2) != 0) | |
| 1425 return -1; | |
| 1426 if (width != s->width || | |
| 1427 height != s->height) { | |
| 1428 /* start new mpeg1 context decoding */ | |
| 1429 s->out_format = FMT_MPEG1; | |
| 1430 if (s1->mpeg_enc_ctx_allocated) { | |
| 1431 MPV_common_end(s); | |
| 1432 } | |
| 1433 s->width = width; | |
| 1434 s->height = height; | |
| 1435 s->has_b_frames = 1; | |
| 69 | 1436 s->avctx = avctx; |
| 0 | 1437 avctx->width = width; |
| 1438 avctx->height = height; | |
| 1439 avctx->frame_rate = frame_rate_tab[s->frame_rate_index]; | |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1440 s->frame_rate = avctx->frame_rate; |
| 0 | 1441 avctx->bit_rate = s->bit_rate; |
| 1442 | |
| 1443 if (MPV_common_init(s) < 0) | |
| 1444 return -1; | |
| 1445 mpeg1_init_vlc(s); | |
| 1446 s1->mpeg_enc_ctx_allocated = 1; | |
| 1447 } | |
| 1448 | |
| 21 | 1449 skip_bits(&s->gb, 10); /* vbv_buffer_size */ |
| 1450 skip_bits(&s->gb, 1); | |
| 0 | 1451 |
| 1452 /* get matrix */ | |
| 21 | 1453 if (get_bits1(&s->gb)) { |
| 0 | 1454 for(i=0;i<64;i++) { |
| 1455 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1456 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1457 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1458 s->chroma_intra_matrix[j] = v; |
| 0 | 1459 } |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1460 #ifdef DEBUG |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1461 dprintf("intra matrix present\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1462 for(i=0;i<64;i++) |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1463 dprintf(" %d", s->intra_matrix[zigzag_direct[i]]); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1464 printf("\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1465 #endif |
| 0 | 1466 } else { |
| 1467 for(i=0;i<64;i++) { | |
| 1468 v = default_intra_matrix[i]; | |
| 1469 s->intra_matrix[i] = v; | |
| 1470 s->chroma_intra_matrix[i] = v; | |
| 1471 } | |
| 1472 } | |
| 21 | 1473 if (get_bits1(&s->gb)) { |
| 0 | 1474 for(i=0;i<64;i++) { |
| 1475 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1476 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1477 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1478 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1479 } |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1480 #ifdef DEBUG |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1481 dprintf("non intra matrix present\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1482 for(i=0;i<64;i++) |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1483 dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1484 printf("\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1485 #endif |
| 0 | 1486 } else { |
| 1487 for(i=0;i<64;i++) { | |
| 1488 v = default_non_intra_matrix[i]; | |
| 1489 s->non_intra_matrix[i] = v; | |
| 1490 s->chroma_non_intra_matrix[i] = v; | |
| 1491 } | |
| 1492 } | |
| 1493 | |
| 1494 /* we set mpeg2 parameters so that it emulates mpeg1 */ | |
| 1495 s->progressive_sequence = 1; | |
| 1496 s->progressive_frame = 1; | |
| 1497 s->picture_structure = PICT_FRAME; | |
| 1498 s->frame_pred_frame_dct = 1; | |
| 1499 s->mpeg2 = 0; | |
| 1500 return 0; | |
| 1501 } | |
| 1502 | |
| 1503 /* handle buffering and image synchronisation */ | |
| 1504 static int mpeg_decode_frame(AVCodecContext *avctx, | |
| 1505 void *data, int *data_size, | |
| 1506 UINT8 *buf, int buf_size) | |
| 1507 { | |
| 1508 Mpeg1Context *s = avctx->priv_data; | |
| 1509 UINT8 *buf_end, *buf_ptr, *buf_start; | |
| 1510 int len, start_code_found, ret, code, start_code, input_size; | |
| 1511 AVPicture *picture = data; | |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1512 MpegEncContext *s2 = &s->mpeg_enc_ctx; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1513 |
| 0 | 1514 dprintf("fill_buffer\n"); |
| 1515 | |
| 1516 *data_size = 0; | |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1517 |
| 0 | 1518 /* special case for last picture */ |
| 1519 if (buf_size == 0) { | |
| 1520 if (s2->picture_number > 0) { | |
| 1521 picture->data[0] = s2->next_picture[0]; | |
| 1522 picture->data[1] = s2->next_picture[1]; | |
| 1523 picture->data[2] = s2->next_picture[2]; | |
| 1524 picture->linesize[0] = s2->linesize; | |
| 1525 picture->linesize[1] = s2->linesize / 2; | |
| 1526 picture->linesize[2] = s2->linesize / 2; | |
| 1527 *data_size = sizeof(AVPicture); | |
| 1528 } | |
| 1529 return 0; | |
| 1530 } | |
| 1531 | |
| 1532 buf_ptr = buf; | |
| 1533 buf_end = buf + buf_size; | |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1534 |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1535 if (s->repeat_field % 2 == 1) { |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1536 s->repeat_field++; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1537 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number, |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1538 // s2->picture_number, s->repeat_field); |
|
275
7ebb3f9aaf3b
- Added video coding statistics for ffmpeg. Needs more work.
pulento
parents:
267
diff
changeset
|
1539 *data_size = sizeof(AVPicture); |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1540 goto the_end; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1541 } |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1542 |
| 0 | 1543 while (buf_ptr < buf_end) { |
| 1544 buf_start = buf_ptr; | |
| 1545 /* find start next code */ | |
| 1546 code = find_start_code(&buf_ptr, buf_end, &s->header_state); | |
| 1547 if (code >= 0) { | |
| 1548 start_code_found = 1; | |
| 1549 } else { | |
| 1550 start_code_found = 0; | |
| 1551 } | |
| 1552 /* copy to buffer */ | |
| 1553 len = buf_ptr - buf_start; | |
| 1554 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
| 1555 /* data too big : flush */ | |
| 1556 s->buf_ptr = s->buffer; | |
| 1557 if (start_code_found) | |
| 1558 s->start_code = code; | |
| 1559 } else { | |
| 1560 memcpy(s->buf_ptr, buf_start, len); | |
| 1561 s->buf_ptr += len; | |
| 1562 | |
| 1563 if (start_code_found) { | |
| 1564 /* prepare data for next start code */ | |
| 1565 input_size = s->buf_ptr - s->buffer; | |
| 1566 start_code = s->start_code; | |
| 1567 s->buf_ptr = s->buffer; | |
| 1568 s->start_code = code; | |
| 1569 switch(start_code) { | |
| 1570 case SEQ_START_CODE: | |
| 1571 mpeg1_decode_sequence(avctx, s->buffer, | |
| 1572 input_size); | |
| 1573 break; | |
| 1574 | |
| 1575 case PICTURE_START_CODE: | |
| 1576 /* we have a complete image : we try to decompress it */ | |
| 1577 mpeg1_decode_picture(avctx, | |
| 1578 s->buffer, input_size); | |
| 1579 break; | |
| 1580 case EXT_START_CODE: | |
| 1581 mpeg_decode_extension(avctx, | |
| 1582 s->buffer, input_size); | |
| 1583 break; | |
| 1584 default: | |
| 1585 if (start_code >= SLICE_MIN_START_CODE && | |
| 1586 start_code <= SLICE_MAX_START_CODE) { | |
| 1587 ret = mpeg_decode_slice(avctx, picture, | |
| 1588 start_code, s->buffer, input_size); | |
| 1589 if (ret == 1) { | |
| 1590 /* got a picture: exit */ | |
|
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1591 /* first check if we must repeat the frame */ |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1592 if (s2->progressive_frame && s2->repeat_first_field) { |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1593 //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number); |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1594 s2->repeat_first_field = 0; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1595 s2->progressive_frame = 0; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1596 if (++s->repeat_field > 2) |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1597 s->repeat_field = 0; |
|
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1598 } |
| 0 | 1599 *data_size = sizeof(AVPicture); |
| 1600 goto the_end; | |
| 1601 } | |
| 1602 } | |
| 1603 break; | |
| 1604 } | |
| 1605 } | |
| 1606 } | |
| 1607 } | |
| 1608 the_end: | |
| 1609 return buf_ptr - buf; | |
| 1610 } | |
| 1611 | |
| 1612 static int mpeg_decode_end(AVCodecContext *avctx) | |
| 1613 { | |
| 1614 Mpeg1Context *s = avctx->priv_data; | |
| 1615 | |
| 1616 if (s->mpeg_enc_ctx_allocated) | |
| 1617 MPV_common_end(&s->mpeg_enc_ctx); | |
| 1618 return 0; | |
| 1619 } | |
| 1620 | |
| 1621 AVCodec mpeg_decoder = { | |
| 1622 "mpegvideo", | |
| 1623 CODEC_TYPE_VIDEO, | |
| 1624 CODEC_ID_MPEG1VIDEO, | |
| 1625 sizeof(Mpeg1Context), | |
| 1626 mpeg_decode_init, | |
| 1627 NULL, | |
| 1628 mpeg_decode_end, | |
| 1629 mpeg_decode_frame, | |
| 1630 }; |
