OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 *height = h; | 613 *height = h; |
614 } | 614 } |
615 | 615 |
616 static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | 616 static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { |
617 cm->display_width = cm->width; | 617 cm->display_width = cm->width; |
618 cm->display_height = cm->height; | 618 cm->display_height = cm->height; |
619 if (vp9_rb_read_bit(rb)) | 619 if (vp9_rb_read_bit(rb)) |
620 vp9_read_frame_size(rb, &cm->display_width, &cm->display_height); | 620 vp9_read_frame_size(rb, &cm->display_width, &cm->display_height); |
621 } | 621 } |
622 | 622 |
623 static void apply_frame_size(VP9_COMMON *cm, int width, int height) { | 623 static void resize_context_buffers(VP9_COMMON *cm, int width, int height) { |
| 624 #if CONFIG_SIZE_LIMIT |
| 625 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT) |
| 626 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
| 627 "Width and height beyond allowed size."); |
| 628 #endif |
624 if (cm->width != width || cm->height != height) { | 629 if (cm->width != width || cm->height != height) { |
625 // Change in frame size. | 630 // Change in frame size (assumption: color format does not change). |
626 // TODO(agrange) Don't test width/height, check overall size. | 631 if (cm->width == 0 || cm->height == 0 || |
627 if (width > cm->width || height > cm->height) { | 632 width * height > cm->width * cm->height) { |
628 // Rescale frame buffers only if they're not big enough already. | 633 if (vp9_alloc_context_buffers(cm, width, height)) |
629 if (vp9_resize_frame_buffers(cm, width, height)) | |
630 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, | 634 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
631 "Failed to allocate frame buffers"); | 635 "Failed to allocate frame buffers"); |
| 636 } else { |
| 637 vp9_set_mb_mi(cm, width, height); |
632 } | 638 } |
633 | 639 vp9_init_context_buffers(cm); |
634 cm->width = width; | 640 cm->width = width; |
635 cm->height = height; | 641 cm->height = height; |
| 642 } |
| 643 } |
636 | 644 |
637 vp9_update_frame_size(cm); | 645 static void setup_frame_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { |
638 } | 646 int width, height; |
| 647 vp9_read_frame_size(rb, &width, &height); |
| 648 resize_context_buffers(cm, width, height); |
| 649 setup_display_size(cm, rb); |
639 | 650 |
640 if (vp9_realloc_frame_buffer( | 651 if (vp9_realloc_frame_buffer( |
641 get_frame_new_buffer(cm), cm->width, cm->height, | 652 get_frame_new_buffer(cm), cm->width, cm->height, |
642 cm->subsampling_x, cm->subsampling_y, VP9_DEC_BORDER_IN_PIXELS, | 653 cm->subsampling_x, cm->subsampling_y, VP9_DEC_BORDER_IN_PIXELS, |
643 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb, | 654 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb, |
644 cm->cb_priv)) { | 655 cm->cb_priv)) { |
645 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, | 656 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
646 "Failed to allocate frame buffer"); | 657 "Failed to allocate frame buffer"); |
647 } | 658 } |
648 } | 659 } |
649 | 660 |
650 static void setup_frame_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | |
651 int width, height; | |
652 vp9_read_frame_size(rb, &width, &height); | |
653 apply_frame_size(cm, width, height); | |
654 setup_display_size(cm, rb); | |
655 } | |
656 | |
657 static void setup_frame_size_with_refs(VP9_COMMON *cm, | 661 static void setup_frame_size_with_refs(VP9_COMMON *cm, |
658 struct vp9_read_bit_buffer *rb) { | 662 struct vp9_read_bit_buffer *rb) { |
659 int width, height; | 663 int width, height; |
660 int found = 0, i; | 664 int found = 0, i; |
661 for (i = 0; i < REFS_PER_FRAME; ++i) { | 665 for (i = 0; i < REFS_PER_FRAME; ++i) { |
662 if (vp9_rb_read_bit(rb)) { | 666 if (vp9_rb_read_bit(rb)) { |
663 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf; | 667 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf; |
664 width = buf->y_crop_width; | 668 width = buf->y_crop_width; |
665 height = buf->y_crop_height; | 669 height = buf->y_crop_height; |
666 found = 1; | 670 found = 1; |
667 break; | 671 break; |
668 } | 672 } |
669 } | 673 } |
670 | 674 |
671 if (!found) | 675 if (!found) |
672 vp9_read_frame_size(rb, &width, &height); | 676 vp9_read_frame_size(rb, &width, &height); |
673 | 677 |
674 // Check that each of the frames that this frame references has valid | 678 // Check that each of the frames that this frame references has valid |
675 // dimensions. | 679 // dimensions. |
676 for (i = 0; i < REFS_PER_FRAME; ++i) { | 680 for (i = 0; i < REFS_PER_FRAME; ++i) { |
677 RefBuffer *const ref_frame = &cm->frame_refs[i]; | 681 RefBuffer *const ref_frame = &cm->frame_refs[i]; |
678 const int ref_width = ref_frame->buf->y_width; | 682 if (!valid_ref_frame_size(ref_frame->buf->y_width, ref_frame->buf->y_height, |
679 const int ref_height = ref_frame->buf->y_height; | 683 width, height)) |
680 | |
681 if (!valid_ref_frame_size(ref_width, ref_height, width, height)) | |
682 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | 684 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
683 "Referenced frame has invalid size"); | 685 "Referenced frame has invalid size"); |
684 } | 686 } |
685 | 687 |
686 apply_frame_size(cm, width, height); | 688 resize_context_buffers(cm, width, height); |
687 setup_display_size(cm, rb); | 689 setup_display_size(cm, rb); |
| 690 |
| 691 if (vp9_realloc_frame_buffer( |
| 692 get_frame_new_buffer(cm), cm->width, cm->height, |
| 693 cm->subsampling_x, cm->subsampling_y, VP9_DEC_BORDER_IN_PIXELS, |
| 694 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb, |
| 695 cm->cb_priv)) { |
| 696 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
| 697 "Failed to allocate frame buffer"); |
| 698 } |
688 } | 699 } |
689 | 700 |
690 static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | 701 static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { |
691 int min_log2_tile_cols, max_log2_tile_cols, max_ones; | 702 int min_log2_tile_cols, max_log2_tile_cols, max_ones; |
692 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); | 703 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
693 | 704 |
694 // columns | 705 // columns |
695 max_ones = max_log2_tile_cols - min_log2_tile_cols; | 706 max_ones = max_log2_tile_cols - min_log2_tile_cols; |
696 cm->log2_tile_cols = min_log2_tile_cols; | 707 cm->log2_tile_cols = min_log2_tile_cols; |
697 while (max_ones-- && vp9_rb_read_bit(rb)) | 708 while (max_ones-- && vp9_rb_read_bit(rb)) |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 VP9_COMMON *const cm = (VP9_COMMON *)data; | 1069 VP9_COMMON *const cm = (VP9_COMMON *)data; |
1059 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet"); | 1070 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet"); |
1060 } | 1071 } |
1061 | 1072 |
1062 int vp9_read_sync_code(struct vp9_read_bit_buffer *const rb) { | 1073 int vp9_read_sync_code(struct vp9_read_bit_buffer *const rb) { |
1063 return vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 && | 1074 return vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 && |
1064 vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 && | 1075 vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 && |
1065 vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2; | 1076 vp9_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2; |
1066 } | 1077 } |
1067 | 1078 |
1068 static BITSTREAM_PROFILE read_profile(struct vp9_read_bit_buffer *rb) { | 1079 BITSTREAM_PROFILE vp9_read_profile(struct vp9_read_bit_buffer *rb) { |
1069 int profile = vp9_rb_read_bit(rb); | 1080 int profile = vp9_rb_read_bit(rb); |
1070 profile |= vp9_rb_read_bit(rb) << 1; | 1081 profile |= vp9_rb_read_bit(rb) << 1; |
| 1082 if (profile > 2) |
| 1083 profile += vp9_rb_read_bit(rb); |
1071 return (BITSTREAM_PROFILE) profile; | 1084 return (BITSTREAM_PROFILE) profile; |
1072 } | 1085 } |
1073 | 1086 |
1074 static size_t read_uncompressed_header(VP9Decoder *pbi, | 1087 static size_t read_uncompressed_header(VP9Decoder *pbi, |
1075 struct vp9_read_bit_buffer *rb) { | 1088 struct vp9_read_bit_buffer *rb) { |
1076 VP9_COMMON *const cm = &pbi->common; | 1089 VP9_COMMON *const cm = &pbi->common; |
1077 size_t sz; | 1090 size_t sz; |
1078 int i; | 1091 int i; |
1079 | 1092 |
1080 cm->last_frame_type = cm->frame_type; | 1093 cm->last_frame_type = cm->frame_type; |
1081 | 1094 |
1082 if (vp9_rb_read_literal(rb, 2) != VP9_FRAME_MARKER) | 1095 if (vp9_rb_read_literal(rb, 2) != VP9_FRAME_MARKER) |
1083 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | 1096 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
1084 "Invalid frame marker"); | 1097 "Invalid frame marker"); |
1085 | 1098 |
1086 cm->profile = read_profile(rb); | 1099 cm->profile = vp9_read_profile(rb); |
1087 if (cm->profile >= MAX_PROFILES) | 1100 if (cm->profile >= MAX_PROFILES) |
1088 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | 1101 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
1089 "Unsupported bitstream profile"); | 1102 "Unsupported bitstream profile"); |
1090 | 1103 |
1091 cm->show_existing_frame = vp9_rb_read_bit(rb); | 1104 cm->show_existing_frame = vp9_rb_read_bit(rb); |
1092 if (cm->show_existing_frame) { | 1105 if (cm->show_existing_frame) { |
1093 // Show an existing frame directly. | 1106 // Show an existing frame directly. |
1094 const int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)]; | 1107 const int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)]; |
1095 | 1108 |
1096 if (frame_to_show < 0 || cm->frame_bufs[frame_to_show].ref_count < 1) | 1109 if (frame_to_show < 0 || cm->frame_bufs[frame_to_show].ref_count < 1) |
(...skipping 14 matching lines...) Expand all Loading... |
1111 | 1124 |
1112 if (cm->frame_type == KEY_FRAME) { | 1125 if (cm->frame_type == KEY_FRAME) { |
1113 if (!vp9_read_sync_code(rb)) | 1126 if (!vp9_read_sync_code(rb)) |
1114 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | 1127 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
1115 "Invalid frame sync code"); | 1128 "Invalid frame sync code"); |
1116 if (cm->profile > PROFILE_1) | 1129 if (cm->profile > PROFILE_1) |
1117 cm->bit_depth = vp9_rb_read_bit(rb) ? BITS_12 : BITS_10; | 1130 cm->bit_depth = vp9_rb_read_bit(rb) ? BITS_12 : BITS_10; |
1118 cm->color_space = (COLOR_SPACE)vp9_rb_read_literal(rb, 3); | 1131 cm->color_space = (COLOR_SPACE)vp9_rb_read_literal(rb, 3); |
1119 if (cm->color_space != SRGB) { | 1132 if (cm->color_space != SRGB) { |
1120 vp9_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range | 1133 vp9_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range |
1121 if (cm->profile >= PROFILE_1) { | 1134 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
1122 cm->subsampling_x = vp9_rb_read_bit(rb); | 1135 cm->subsampling_x = vp9_rb_read_bit(rb); |
1123 cm->subsampling_y = vp9_rb_read_bit(rb); | 1136 cm->subsampling_y = vp9_rb_read_bit(rb); |
1124 vp9_rb_read_bit(rb); // has extra plane | 1137 vp9_rb_read_bit(rb); // has extra plane |
1125 } else { | 1138 } else { |
1126 cm->subsampling_y = cm->subsampling_x = 1; | 1139 cm->subsampling_y = cm->subsampling_x = 1; |
1127 } | 1140 } |
1128 } else { | 1141 } else { |
1129 if (cm->profile >= PROFILE_1) { | 1142 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
1130 cm->subsampling_y = cm->subsampling_x = 0; | 1143 cm->subsampling_y = cm->subsampling_x = 0; |
1131 vp9_rb_read_bit(rb); // has extra plane | 1144 vp9_rb_read_bit(rb); // has extra plane |
1132 } else { | 1145 } else { |
1133 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | 1146 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
1134 "RGB not supported in profile 0"); | 1147 "4:4:4 color not supported in profile 0"); |
1135 } | 1148 } |
1136 } | 1149 } |
1137 | 1150 |
1138 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1; | 1151 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1; |
1139 | 1152 |
1140 for (i = 0; i < REFS_PER_FRAME; ++i) { | 1153 for (i = 0; i < REFS_PER_FRAME; ++i) { |
1141 cm->frame_refs[i].idx = cm->new_fb_idx; | 1154 cm->frame_refs[i].idx = -1; |
1142 cm->frame_refs[i].buf = get_frame_new_buffer(cm); | 1155 cm->frame_refs[i].buf = NULL; |
1143 } | 1156 } |
1144 | 1157 |
1145 setup_frame_size(cm, rb); | 1158 setup_frame_size(cm, rb); |
1146 } else { | 1159 } else { |
1147 cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb); | 1160 cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb); |
1148 | 1161 |
1149 cm->reset_frame_context = cm->error_resilient_mode ? | 1162 cm->reset_frame_context = cm->error_resilient_mode ? |
1150 0 : vp9_rb_read_literal(rb, 2); | 1163 0 : vp9_rb_read_literal(rb, 2); |
1151 | 1164 |
1152 if (cm->intra_only) { | 1165 if (cm->intra_only) { |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1413 debug_check_frame_counts(cm); | 1426 debug_check_frame_counts(cm); |
1414 } | 1427 } |
1415 } else { | 1428 } else { |
1416 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | 1429 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1417 "Decode failed. Frame data is corrupted."); | 1430 "Decode failed. Frame data is corrupted."); |
1418 } | 1431 } |
1419 | 1432 |
1420 if (cm->refresh_frame_context) | 1433 if (cm->refresh_frame_context) |
1421 cm->frame_contexts[cm->frame_context_idx] = cm->fc; | 1434 cm->frame_contexts[cm->frame_context_idx] = cm->fc; |
1422 } | 1435 } |
OLD | NEW |