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