comparison vp3.c @ 1238:6eab0df78d47 libavcodec

squashed a bunch of subtle array indexing bugs, fixed block mapping added more error checking, added (and enabled, for the time being) a keyframe-only mode
author tmmm
date Sat, 10 May 2003 21:46:17 +0000
parents d6e4784ffc16
children fe46bd7c64d4
comparison
equal deleted inserted replaced
1237:6ce5cde4e1d4 1238:6eab0df78d47
223 int last_quality_index; 223 int last_quality_index;
224 224
225 int superblock_count; 225 int superblock_count;
226 int superblock_width; 226 int superblock_width;
227 int superblock_height; 227 int superblock_height;
228 int y_superblock_width;
229 int y_superblock_height;
230 int c_superblock_width;
231 int c_superblock_height;
228 int u_superblock_start; 232 int u_superblock_start;
229 int v_superblock_start; 233 int v_superblock_start;
230 unsigned char *superblock_coding; 234 unsigned char *superblock_coding;
231 235
232 int macroblock_count; 236 int macroblock_count;
290 294
291 /* 295 /*
292 * This function sets up all of the various blocks mappings: 296 * This function sets up all of the various blocks mappings:
293 * superblocks <-> fragments, macroblocks <-> fragments, 297 * superblocks <-> fragments, macroblocks <-> fragments,
294 * superblocks <-> macroblocks 298 * superblocks <-> macroblocks
299 *
300 * Returns 0 is successful; returns 1 if *anything* went wrong.
295 */ 301 */
296 static void init_block_mapping(Vp3DecodeContext *s) 302 static int init_block_mapping(Vp3DecodeContext *s)
297 { 303 {
298 int i, j; 304 int i, j;
299 signed int hilbert_walk_y[16]; 305 signed int hilbert_walk_y[16];
300 signed int hilbert_walk_c[16]; 306 signed int hilbert_walk_c[16];
301 signed int hilbert_walk_mb[4]; 307 signed int hilbert_walk_mb[4];
385 if (i == 0) { 391 if (i == 0) {
386 392
387 /* start of Y superblocks */ 393 /* start of Y superblocks */
388 right_edge = s->fragment_width; 394 right_edge = s->fragment_width;
389 bottom_edge = s->fragment_height; 395 bottom_edge = s->fragment_height;
390 current_width = 0; 396 current_width = -1;
391 current_height = 0; 397 current_height = 0;
392 superblock_row_inc = 3 * s->fragment_width; 398 superblock_row_inc = 3 * s->fragment_width -
399 (s->y_superblock_width * 4 - s->fragment_width);
393 hilbert = hilbert_walk_y; 400 hilbert = hilbert_walk_y;
394 401
395 /* the first operation for this variable is to advance by 1 */ 402 /* the first operation for this variable is to advance by 1 */
396 current_fragment = -1; 403 current_fragment = -1;
397 404
398 } else if (i == s->u_superblock_start) { 405 } else if (i == s->u_superblock_start) {
399 406
400 /* start of U superblocks */ 407 /* start of U superblocks */
401 right_edge = s->fragment_width / 2; 408 right_edge = s->fragment_width / 2;
402 bottom_edge = s->fragment_height / 2; 409 bottom_edge = s->fragment_height / 2;
403 current_width = 0; 410 current_width = -1;
404 current_height = 0; 411 current_height = 0;
405 superblock_row_inc = 3 * (s->fragment_width / 2); 412 superblock_row_inc = 3 * (s->fragment_width / 2) -
413 (s->c_superblock_width * 4 - s->fragment_width / 2);
406 hilbert = hilbert_walk_c; 414 hilbert = hilbert_walk_c;
407 415
408 /* the first operation for this variable is to advance by 1 */ 416 /* the first operation for this variable is to advance by 1 */
409 current_fragment = s->u_fragment_start - 1; 417 current_fragment = s->u_fragment_start - 1;
410 418
411 } else if (i == s->v_superblock_start) { 419 } else if (i == s->v_superblock_start) {
412 420
413 /* start of V superblocks */ 421 /* start of V superblocks */
414 right_edge = s->fragment_width / 2; 422 right_edge = s->fragment_width / 2;
415 bottom_edge = s->fragment_height / 2; 423 bottom_edge = s->fragment_height / 2;
416 current_width = 0; 424 current_width = -1;
417 current_height = 0; 425 current_height = 0;
418 superblock_row_inc = 3 * (s->fragment_width / 2); 426 superblock_row_inc = 3 * (s->fragment_width / 2) -
427 (s->c_superblock_width * 4 - s->fragment_width / 2);
419 hilbert = hilbert_walk_c; 428 hilbert = hilbert_walk_c;
420 429
421 /* the first operation for this variable is to advance by 1 */ 430 /* the first operation for this variable is to advance by 1 */
422 current_fragment = s->v_fragment_start - 1; 431 current_fragment = s->v_fragment_start - 1;
423 432
424 } 433 }
425 434
426 if (current_width >= right_edge) { 435 if (current_width >= right_edge - 1) {
427 /* reset width and move to next superblock row */ 436 /* reset width and move to next superblock row */
428 current_width = 0; 437 current_width = -1;
429 current_height += 4; 438 current_height += 4;
430 439
431 /* fragment is now at the start of a new superblock row */ 440 /* fragment is now at the start of a new superblock row */
432 current_fragment += superblock_row_inc; 441 current_fragment += superblock_row_inc;
433 } 442 }
434 443
435 /* iterate through all 16 fragments in a superblock */ 444 /* iterate through all 16 fragments in a superblock */
436 for (j = 0; j < 16; j++) { 445 for (j = 0; j < 16; j++) {
437 current_fragment += hilbert[j]; 446 current_fragment += hilbert[j];
447 current_width += travel_width[j];
438 current_height += travel_height[j]; 448 current_height += travel_height[j];
439 449
440 /* check if the fragment is in bounds */ 450 /* check if the fragment is in bounds */
441 if ((current_width <= right_edge) && 451 if ((current_width < right_edge) &&
442 (current_height < bottom_edge)) { 452 (current_height < bottom_edge)) {
443 s->superblock_fragments[mapping_index] = current_fragment; 453 s->superblock_fragments[mapping_index] = current_fragment;
444 debug_init(" mapping fragment %d to superblock %d, position %d\n", 454 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
445 s->superblock_fragments[mapping_index], i, j); 455 s->superblock_fragments[mapping_index], i, j,
456 current_width, right_edge, current_height, bottom_edge);
446 } else { 457 } else {
447 s->superblock_fragments[mapping_index] = -1; 458 s->superblock_fragments[mapping_index] = -1;
448 debug_init(" superblock %d, position %d has no fragment\n", 459 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
449 i, j); 460 i, j,
461 current_width, right_edge, current_height, bottom_edge);
450 } 462 }
451 463
452 current_width += travel_width[j];
453 mapping_index++; 464 mapping_index++;
454 } 465 }
455 } 466 }
456 467
457 /* initialize the superblock <-> macroblock mapping; iterate through 468 /* initialize the superblock <-> macroblock mapping; iterate through
458 * all of the Y plane superblocks to build this mapping */ 469 * all of the Y plane superblocks to build this mapping */
459 right_edge = s->macroblock_width; 470 right_edge = s->macroblock_width;
460 bottom_edge = s->macroblock_height; 471 bottom_edge = s->macroblock_height;
461 current_width = 0; 472 current_width = -1;
462 current_height = 0; 473 current_height = 0;
463 superblock_row_inc = s->macroblock_width; 474 superblock_row_inc = s->macroblock_width -
475 (s->y_superblock_width * 2 - s->macroblock_width);;
464 hilbert = hilbert_walk_mb; 476 hilbert = hilbert_walk_mb;
465 mapping_index = 0; 477 mapping_index = 0;
466 current_macroblock = -1; 478 current_macroblock = -1;
467 for (i = 0; i < s->u_superblock_start; i++) { 479 for (i = 0; i < s->u_superblock_start; i++) {
468 480
476 } 488 }
477 489
478 /* iterate through each potential macroblock in the superblock */ 490 /* iterate through each potential macroblock in the superblock */
479 for (j = 0; j < 4; j++) { 491 for (j = 0; j < 4; j++) {
480 current_macroblock += hilbert_walk_mb[j]; 492 current_macroblock += hilbert_walk_mb[j];
493 current_width += travel_width_mb[j];
481 current_height += travel_height_mb[j]; 494 current_height += travel_height_mb[j];
482 495
483 /* check if the macroblock is in bounds */ 496 /* check if the macroblock is in bounds */
484 if ((current_width <= right_edge) && 497 if ((current_width < right_edge) &&
485 (current_height < bottom_edge)) { 498 (current_height < bottom_edge)) {
486 s->superblock_macroblocks[mapping_index] = current_macroblock; 499 s->superblock_macroblocks[mapping_index] = current_macroblock;
487 debug_init(" mapping macroblock %d to superblock %d, position %d\n", 500 debug_init(" mapping macroblock %d to superblock %d, position %d\n",
488 s->superblock_macroblocks[mapping_index], i, j); 501 s->superblock_macroblocks[mapping_index], i, j);
489 } else { 502 } else {
490 s->superblock_macroblocks[mapping_index] = -1; 503 s->superblock_macroblocks[mapping_index] = -1;
491 debug_init(" superblock %d, position %d has no macroblock\n", 504 debug_init(" superblock %d, position %d has no macroblock\n",
492 i, j); 505 i, j);
493 } 506 }
494 507
495 current_width += travel_width_mb[j];
496 mapping_index++; 508 mapping_index++;
497 } 509 }
498 } 510 }
499 511
500 /* initialize the macroblock <-> fragment mapping */ 512 /* initialize the macroblock <-> fragment mapping */
536 s->macroblock_fragments[mapping_index++] = -1; 548 s->macroblock_fragments[mapping_index++] = -1;
537 549
538 /* C planes */ 550 /* C planes */
539 c_fragment = s->u_fragment_start + 551 c_fragment = s->u_fragment_start +
540 (i * s->fragment_width / 4) + (j / 2); 552 (i * s->fragment_width / 4) + (j / 2);
541 s->all_fragments[c_fragment].macroblock = s->macroblock_count; 553 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
542 s->macroblock_fragments[mapping_index++] = c_fragment; 554 s->macroblock_fragments[mapping_index++] = c_fragment;
543 debug_init("%d ", c_fragment); 555 debug_init("%d ", c_fragment);
544 556
545 c_fragment = s->v_fragment_start + 557 c_fragment = s->v_fragment_start +
546 (i * s->fragment_width / 4) + (j / 2); 558 (i * s->fragment_width / 4) + (j / 2);
547 s->all_fragments[c_fragment].macroblock = s->macroblock_count; 559 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
548 s->macroblock_fragments[mapping_index++] = c_fragment; 560 s->macroblock_fragments[mapping_index++] = c_fragment;
549 debug_init("%d ", c_fragment); 561 debug_init("%d ", c_fragment);
550 562
551 debug_init("\n"); 563 debug_init("\n");
552 564
557 current_macroblock++; 569 current_macroblock++;
558 } 570 }
559 571
560 current_fragment += s->fragment_width; 572 current_fragment += s->fragment_width;
561 } 573 }
574
575 return 0; /* successful path out */
562 } 576 }
563 577
564 /* 578 /*
565 * This function unpacks a single token (which should be in the range 0..31) 579 * This function unpacks a single token (which should be in the range 0..31)
566 * and returns a zero run (number of zero coefficients in current DCT matrix 580 * and returns a zero run (number of zero coefficients in current DCT matrix
1104 1118
1105 /* 1119 /*
1106 * This function unpacks all of the superblock/macroblock/fragment coding 1120 * This function unpacks all of the superblock/macroblock/fragment coding
1107 * information from the bitstream. 1121 * information from the bitstream.
1108 */ 1122 */
1109 static void unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) 1123 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1110 { 1124 {
1111 int bit = 0; 1125 int bit = 0;
1112 int current_superblock = 0; 1126 int current_superblock = 0;
1113 int current_run = 0; 1127 int current_run = 0;
1114 int decode_fully_flags = 0; 1128 int decode_fully_flags = 0;
1210 /* iterate through all 16 fragments in a superblock */ 1224 /* iterate through all 16 fragments in a superblock */
1211 for (j = 0; j < 16; j++) { 1225 for (j = 0; j < 16; j++) {
1212 1226
1213 /* if the fragment is in bounds, check its coding status */ 1227 /* if the fragment is in bounds, check its coding status */
1214 current_fragment = s->superblock_fragments[i * 16 + j]; 1228 current_fragment = s->superblock_fragments[i * 16 + j];
1229 if (current_fragment >= s->fragment_count) {
1230 printf (" vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1231 current_fragment, s->fragment_count);
1232 return 1;
1233 }
1215 if (current_fragment != -1) { 1234 if (current_fragment != -1) {
1216 if (s->superblock_coding[i] == SB_NOT_CODED) { 1235 if (s->superblock_coding[i] == SB_NOT_CODED) {
1217 1236
1218 /* copy all the fragments from the prior frame */ 1237 /* copy all the fragments from the prior frame */
1219 s->all_fragments[current_fragment].coding_method = 1238 s->all_fragments[current_fragment].coding_method =
1284 s->coded_fragment_list_index, 1303 s->coded_fragment_list_index,
1285 s->first_coded_y_fragment, 1304 s->first_coded_y_fragment,
1286 s->last_coded_y_fragment, 1305 s->last_coded_y_fragment,
1287 s->first_coded_c_fragment, 1306 s->first_coded_c_fragment,
1288 s->last_coded_c_fragment); 1307 s->last_coded_c_fragment);
1308
1309 return 0;
1289 } 1310 }
1290 1311
1291 /* 1312 /*
1292 * This function unpacks all the coding mode data for individual macroblocks 1313 * This function unpacks all the coding mode data for individual macroblocks
1293 * from the bitstream. 1314 * from the bitstream.
1294 */ 1315 */
1295 static void unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) 1316 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1296 { 1317 {
1297 int i, j, k; 1318 int i, j, k;
1298 int scheme; 1319 int scheme;
1299 int current_macroblock; 1320 int current_macroblock;
1300 int current_fragment; 1321 int current_fragment;
1332 for (j = 0; j < 4; j++) { 1353 for (j = 0; j < 4; j++) {
1333 current_macroblock = s->superblock_macroblocks[i * 4 + j]; 1354 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1334 if ((current_macroblock == -1) || 1355 if ((current_macroblock == -1) ||
1335 (!s->macroblock_coded[current_macroblock])) 1356 (!s->macroblock_coded[current_macroblock]))
1336 continue; 1357 continue;
1358 if (current_macroblock >= s->macroblock_count) {
1359 printf (" vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1360 current_macroblock, s->macroblock_count);
1361 return 1;
1362 }
1337 1363
1338 /* mode 7 means get 3 bits for each coding mode */ 1364 /* mode 7 means get 3 bits for each coding mode */
1339 if (scheme == 7) 1365 if (scheme == 7)
1340 coding_mode = get_bits(gb, 3); 1366 coding_mode = get_bits(gb, 3);
1341 else 1367 else
1342 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)]; 1368 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1343 1369
1344 for (k = 0; k < 6; k++) { 1370 for (k = 0; k < 6; k++) {
1345 current_fragment = 1371 current_fragment =
1346 s->macroblock_fragments[current_macroblock * 6 + k]; 1372 s->macroblock_fragments[current_macroblock * 6 + k];
1373 if (current_fragment == -1)
1374 continue;
1375 if (current_fragment >= s->fragment_count) {
1376 printf (" vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1377 current_fragment, s->fragment_count);
1378 return 1;
1379 }
1347 if (s->all_fragments[current_fragment].coding_method != 1380 if (s->all_fragments[current_fragment].coding_method !=
1348 MODE_COPY) 1381 MODE_COPY)
1349 s->all_fragments[current_fragment].coding_method = 1382 s->all_fragments[current_fragment].coding_method =
1350 coding_mode; 1383 coding_mode;
1351 } 1384 }
1353 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n", 1386 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1354 s->macroblock_fragments[current_macroblock * 6], coding_mode); 1387 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1355 } 1388 }
1356 } 1389 }
1357 } 1390 }
1391
1392 return 0;
1358 } 1393 }
1359 1394
1360 /* 1395 /*
1361 * This function adjusts the components of a motion vector for the halfpel 1396 * This function adjusts the components of a motion vector for the halfpel
1362 * motion grid. c_plane indicates whether the vector applies to the U or V 1397 * motion grid. c_plane indicates whether the vector applies to the U or V
1408 1443
1409 /* 1444 /*
1410 * This function unpacks all the motion vectors for the individual 1445 * This function unpacks all the motion vectors for the individual
1411 * macroblocks from the bitstream. 1446 * macroblocks from the bitstream.
1412 */ 1447 */
1413 static void unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) 1448 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1414 { 1449 {
1415 int i, j, k; 1450 int i, j, k;
1416 int coding_mode; 1451 int coding_mode;
1417 int motion_x[6]; 1452 int motion_x[6];
1418 int motion_y[6]; 1453 int motion_y[6];
1446 for (j = 0; j < 4; j++) { 1481 for (j = 0; j < 4; j++) {
1447 current_macroblock = s->superblock_macroblocks[i * 4 + j]; 1482 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1448 if ((current_macroblock == -1) || 1483 if ((current_macroblock == -1) ||
1449 (!s->macroblock_coded[current_macroblock])) 1484 (!s->macroblock_coded[current_macroblock]))
1450 continue; 1485 continue;
1486 if (current_macroblock >= s->macroblock_count) {
1487 printf (" vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1488 current_macroblock, s->macroblock_count);
1489 return 1;
1490 }
1451 1491
1452 current_fragment = s->macroblock_fragments[current_macroblock * 6]; 1492 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1493 if (current_fragment >= s->fragment_count) {
1494 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1495 current_fragment, s->fragment_count);
1496 return 1;
1497 }
1453 switch (s->all_fragments[current_fragment].coding_method) { 1498 switch (s->all_fragments[current_fragment].coding_method) {
1454 1499
1455 case MODE_INTER_PLUS_MV: 1500 case MODE_INTER_PLUS_MV:
1456 case MODE_GOLDEN_MV: 1501 case MODE_GOLDEN_MV:
1457 /* all 6 fragments use the same motion vector */ 1502 /* all 6 fragments use the same motion vector */
1557 current_fragment, 1602 current_fragment,
1558 s->all_fragments[current_fragment].coding_method); 1603 s->all_fragments[current_fragment].coding_method);
1559 for (k = 0; k < 6; k++) { 1604 for (k = 0; k < 6; k++) {
1560 current_fragment = 1605 current_fragment =
1561 s->macroblock_fragments[current_macroblock * 6 + k]; 1606 s->macroblock_fragments[current_macroblock * 6 + k];
1607 if (current_fragment == -1)
1608 continue;
1609 if (current_fragment >= s->fragment_count) {
1610 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1611 current_fragment, s->fragment_count);
1612 return 1;
1613 }
1562 s->all_fragments[current_fragment].motion_halfpel_index = 1614 s->all_fragments[current_fragment].motion_halfpel_index =
1563 adjust_vector(&motion_x[k], &motion_y[k], 1615 adjust_vector(&motion_x[k], &motion_y[k],
1564 ((k == 4) || (k == 5))); 1616 ((k == 4) || (k == 5)));
1565 s->all_fragments[current_fragment].motion_x = motion_x[k]; 1617 s->all_fragments[current_fragment].motion_x = motion_x[k];
1566 s->all_fragments[current_fragment].motion_y = motion_y[k]; 1618 s->all_fragments[current_fragment].motion_y = motion_y[k];
1569 s->all_fragments[current_fragment].motion_halfpel_index); 1621 s->all_fragments[current_fragment].motion_halfpel_index);
1570 } 1622 }
1571 } 1623 }
1572 } 1624 }
1573 } 1625 }
1626
1627 return 0;
1574 } 1628 }
1575 1629
1576 /* 1630 /*
1577 * This function is called by unpack_dct_coeffs() to extract the VLCs from 1631 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1578 * the bitstream. The VLCs encode tokens which are used to unpack DCT 1632 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1630 1684
1631 /* 1685 /*
1632 * This function unpacks all of the DCT coefficient data from the 1686 * This function unpacks all of the DCT coefficient data from the
1633 * bitstream. 1687 * bitstream.
1634 */ 1688 */
1635 static void unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) 1689 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1636 { 1690 {
1637 int i; 1691 int i;
1638 int dc_y_table; 1692 int dc_y_table;
1639 int dc_c_table; 1693 int dc_c_table;
1640 int ac_y_table; 1694 int ac_y_table;
1714 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n", 1768 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1715 i, ac_c_table); 1769 i, ac_c_table);
1716 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i, 1770 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
1717 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run); 1771 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1718 } 1772 }
1773
1774 return 0;
1719 } 1775 }
1720 1776
1721 /* 1777 /*
1722 * This function reverses the DC prediction for each coded fragment in 1778 * This function reverses the DC prediction for each coded fragment in
1723 * the frame. Much of this function is adapted directly from the original 1779 * the frame. Much of this function is adapted directly from the original
2070 * to render the block */ 2126 * to render the block */
2071 if ((motion_source < upper_motion_limit) || 2127 if ((motion_source < upper_motion_limit) ||
2072 (motion_source > lower_motion_limit)) { 2128 (motion_source > lower_motion_limit)) {
2073 // printf (" vp3: help! motion source (%d) out of range (%d..%d)\n", 2129 // printf (" vp3: help! motion source (%d) out of range (%d..%d)\n",
2074 // motion_source, upper_motion_limit, lower_motion_limit); 2130 // motion_source, upper_motion_limit, lower_motion_limit);
2131 continue;
2075 } 2132 }
2076 2133
2077 /* first, take care of copying a block from either the 2134 /* first, take care of copying a block from either the
2078 * previous or the golden frame */ 2135 * previous or the golden frame */
2079 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || 2136 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2208 */ 2265 */
2209 static int vp3_decode_init(AVCodecContext *avctx) 2266 static int vp3_decode_init(AVCodecContext *avctx)
2210 { 2267 {
2211 Vp3DecodeContext *s = avctx->priv_data; 2268 Vp3DecodeContext *s = avctx->priv_data;
2212 int i; 2269 int i;
2270 int c_width;
2271 int c_height;
2272 int y_superblock_count;
2273 int c_superblock_count;
2213 2274
2214 s->avctx = avctx; 2275 s->avctx = avctx;
2215 s->width = avctx->width; 2276 s->width = avctx->width;
2216 s->height = avctx->height; 2277 s->height = avctx->height;
2217 avctx->pix_fmt = PIX_FMT_YUV420P; 2278 avctx->pix_fmt = PIX_FMT_YUV420P;
2220 2281
2221 /* initialize to an impossible value which will force a recalculation 2282 /* initialize to an impossible value which will force a recalculation
2222 * in the first frame decode */ 2283 * in the first frame decode */
2223 s->quality_index = -1; 2284 s->quality_index = -1;
2224 2285
2225 s->superblock_width = (s->width + 31) / 32; 2286 s->y_superblock_width = (s->width + 31) / 32;
2226 s->superblock_height = (s->height + 31) / 32; 2287 s->y_superblock_height = (s->height + 31) / 32;
2227 s->superblock_count = s->superblock_width * s->superblock_height * 3 / 2; 2288 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2228 s->u_superblock_start = s->superblock_width * s->superblock_height; 2289
2229 s->v_superblock_start = s->superblock_width * s->superblock_height * 5 / 4; 2290 /* work out the dimensions for the C planes */
2291 c_width = s->width / 2;
2292 c_height = s->height / 2;
2293 s->c_superblock_width = (c_width + 31) / 32;
2294 s->c_superblock_height = (c_height + 31) / 32;
2295 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2296
2297 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2298 s->u_superblock_start = y_superblock_count;
2299 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2230 s->superblock_coding = av_malloc(s->superblock_count); 2300 s->superblock_coding = av_malloc(s->superblock_count);
2231 2301
2232 s->macroblock_width = (s->width + 15) / 16; 2302 s->macroblock_width = (s->width + 15) / 16;
2233 s->macroblock_height = (s->height + 15) / 16; 2303 s->macroblock_height = (s->height + 15) / 16;
2234 s->macroblock_count = s->macroblock_width * s->macroblock_height; 2304 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2239 /* fragment count covers all 8x8 blocks for all 3 planes */ 2309 /* fragment count covers all 8x8 blocks for all 3 planes */
2240 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2; 2310 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2241 s->u_fragment_start = s->fragment_width * s->fragment_height; 2311 s->u_fragment_start = s->fragment_width * s->fragment_height;
2242 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4; 2312 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2243 2313
2244 debug_init(" width: %d x %d\n", s->width, s->height); 2314 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2245 debug_init(" superblocks: %d x %d, %d total\n", 2315 debug_init(" C plane: %d x %d\n", c_width, c_height);
2246 s->superblock_width, s->superblock_height, s->superblock_count); 2316 debug_init(" Y superblocks: %d x %d, %d total\n",
2317 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2318 debug_init(" C superblocks: %d x %d, %d total\n",
2319 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2320 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2321 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2247 debug_init(" macroblocks: %d x %d, %d total\n", 2322 debug_init(" macroblocks: %d x %d, %d total\n",
2248 s->macroblock_width, s->macroblock_height, s->macroblock_count); 2323 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2249 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n", 2324 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2250 s->fragment_count, 2325 s->fragment_count,
2251 s->fragment_width, 2326 s->fragment_width,
2331 2406
2332 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index); 2407 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
2333 counter++; 2408 counter++;
2334 2409
2335 if (s->keyframe) { 2410 if (s->keyframe) {
2336 /* release the previous golden frame and get a new one */ 2411 if ((s->golden_frame.data[0]) &&
2337 if (s->golden_frame.data[0]) 2412 (s->last_frame.data[0] == s->golden_frame.data[0]))
2338 avctx->release_buffer(avctx, &s->golden_frame); 2413 avctx->release_buffer(avctx, &s->golden_frame);
2339 2414 else if (s->last_frame.data[0])
2340 /* last frame, if allocated, is hereby invalidated */
2341 if (s->last_frame.data[0])
2342 avctx->release_buffer(avctx, &s->last_frame); 2415 avctx->release_buffer(avctx, &s->last_frame);
2343 2416
2344 s->golden_frame.reference = 0; 2417 s->golden_frame.reference = 0;
2345 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) { 2418 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2346 printf("vp3: get_buffer() failed\n"); 2419 printf("vp3: get_buffer() failed\n");
2372 } else 2445 } else
2373 debug_vp3("\n"); 2446 debug_vp3("\n");
2374 2447
2375 init_frame(s, &gb); 2448 init_frame(s, &gb);
2376 2449
2377 unpack_superblocks(s, &gb); 2450 #define KEYFRAMES_ONLY 1
2378 unpack_modes(s, &gb); 2451 #if KEYFRAMES_ONLY
2379 unpack_vectors(s, &gb); 2452 if (!s->keyframe) {
2380 unpack_dct_coeffs(s, &gb); 2453
2454 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2455 s->current_frame.linesize[0] * s->height);
2456 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2457 s->current_frame.linesize[1] * s->height / 2);
2458 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2459 s->current_frame.linesize[2] * s->height / 2);
2460
2461 } else {
2462 #endif
2463
2464 if (unpack_superblocks(s, &gb) ||
2465 unpack_modes(s, &gb) ||
2466 unpack_vectors(s, &gb) ||
2467 unpack_dct_coeffs(s, &gb)) {
2468
2469 printf(" vp3: could not decode frame\n");
2470 return -1;
2471 }
2381 2472
2382 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height); 2473 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2383 reverse_dc_prediction(s, s->u_fragment_start, 2474 reverse_dc_prediction(s, s->u_fragment_start,
2384 s->fragment_width / 2, s->fragment_height / 2); 2475 s->fragment_width / 2, s->fragment_height / 2);
2385 reverse_dc_prediction(s, s->v_fragment_start, 2476 reverse_dc_prediction(s, s->v_fragment_start,
2386 s->fragment_width / 2, s->fragment_height / 2); 2477 s->fragment_width / 2, s->fragment_height / 2);
2387 2478
2388 render_fragments(s, 0, s->width, s->height, 0); 2479 render_fragments(s, 0, s->width, s->height, 0);
2389 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1); 2480 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2390 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2); 2481 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2482
2483 #if KEYFRAMES_ONLY
2484 }
2485 #endif
2391 2486
2392 *data_size=sizeof(AVFrame); 2487 *data_size=sizeof(AVFrame);
2393 *(AVFrame*)data= s->current_frame; 2488 *(AVFrame*)data= s->current_frame;
2394 2489
2395 /* release the last frame, if it is allocated and if it is not the 2490 /* release the last frame, if it is allocated and if it is not the
2417 av_free(s->superblock_macroblocks); 2512 av_free(s->superblock_macroblocks);
2418 av_free(s->macroblock_fragments); 2513 av_free(s->macroblock_fragments);
2419 av_free(s->macroblock_coded); 2514 av_free(s->macroblock_coded);
2420 2515
2421 /* release all frames */ 2516 /* release all frames */
2422 avctx->release_buffer(avctx, &s->golden_frame); 2517 if (s->golden_frame.data[0])
2423 avctx->release_buffer(avctx, &s->last_frame); 2518 avctx->release_buffer(avctx, &s->golden_frame);
2424 avctx->release_buffer(avctx, &s->current_frame); 2519 if (s->last_frame.data[0])
2520 avctx->release_buffer(avctx, &s->last_frame);
2521 /* no need to release the current_frame since it will always be pointing
2522 * to the same frame as either the golden or last frame */
2425 2523
2426 return 0; 2524 return 0;
2427 } 2525 }
2428 2526
2429 AVCodec vp3_decoder = { 2527 AVCodec vp3_decoder = {