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