unsigned Cartridge::find_header(const uint8_t *data, unsigned size) const { unsigned score_lo = score_header(data, size, 0x007fc0); unsigned score_hi = score_header(data, size, 0x00ffc0); unsigned score_ex = score_header(data, size, 0x40ffc0); if(score_ex) score_ex += 4; //favor ExHiROM on images > 32mbits if(score_lo >= score_hi && score_lo >= score_ex) { return 0x007fc0; } else if(score_hi >= score_ex) { return 0x00ffc0; } else { return 0x40ffc0; } }
unsigned SuperFamicomCartridge::find_header(linput_t *li) { unsigned score_lo = score_header(li, 0x007fc0); unsigned score_hi = score_header(li, 0x00ffc0); unsigned score_ex = score_header(li, 0x40ffc0); if(score_ex) score_ex += 4; //favor ExHiROM on images > 32mbits if(score_lo >= score_hi && score_lo >= score_ex) { return 0x007fc0; } else if(score_hi >= score_ex) { return 0x00ffc0; } else { return 0x40ffc0; } }
/** * Score a header. * * Give FLAC_HEADER_BASE_SCORE points to a frame for existing. * If it has children, (subsequent frames of which the preceding CRC footer * validates against this one,) then take the maximum score of the children, * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to * bps, sample rate, channels, but not decorrelation mode, or blocksize, * because it can change often. **/ static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header) { FLACHeaderMarker *child; int dist = 0; int child_score; if (header->max_score != FLAC_HEADER_NOT_SCORED_YET) return header->max_score; header->max_score = FLAC_HEADER_BASE_SCORE; /* Check and compute the children's scores. */ child = header->next; for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) { /* Look at the child's frame header info and penalize suspicious changes between the headers. */ if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) { header->link_penalty[dist] = check_header_mismatch(fpc, header, child, AV_LOG_DEBUG); } child_score = score_header(fpc, child) - header->link_penalty[dist]; if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) { /* Keep the child because the frame scoring is dynamic. */ header->best_child = child; header->max_score = FLAC_HEADER_BASE_SCORE + child_score; } child = child->next; } return header->max_score; }
static enum ssnes_mapper_type find_mapper_type(const uint8_t *data, size_t size) { // One 32KiB bank, has to be LoROM. if (size < 0x10000) return SSNES_MAPPER_LOROM; unsigned score_lo = score_header(data, size, 0x7fc0); unsigned score_hi = score_header(data, size, 0xffc0); unsigned index = (score_lo > score_hi) ? 0x7fc0 : 0xffc0; if (index == 0xffc0) return SSNES_MAPPER_HIROM; else return SSNES_MAPPER_LOROM; }
static void score_sequences(FLACParseContext *fpc) { FLACHeaderMarker *curr; int best_score = FLAC_HEADER_NOT_SCORED_YET; /* First pass to clear all old scores. */ for (curr = fpc->headers; curr; curr = curr->next) curr->max_score = FLAC_HEADER_NOT_SCORED_YET; /* Do a second pass to score them all. */ for (curr = fpc->headers; curr; curr = curr->next) { if (score_header(fpc, curr) > best_score) { fpc->best_header = curr; best_score = curr->max_score; } } }
/** * Score a header. * * Give FLAC_HEADER_BASE_SCORE points to a frame for existing. * If it has children, (subsequent frames of which the preceding CRC footer * validates against this one,) then take the maximum score of the children, * with a penalty of FLAC_HEADER_CHANGED_PENALTY applied for each change to * bps, sample rate, channels, but not decorrelation mode, or blocksize, * because it can change often. **/ static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header) { FLACHeaderMarker *child; int dist = 0; int child_score; int base_score = FLAC_HEADER_BASE_SCORE; if (header->max_score != FLAC_HEADER_NOT_SCORED_YET) return header->max_score; /* Modify the base score with changes from the last output header */ if (fpc->last_fi_valid) { /* Silence the log since this will be repeated if selected */ base_score -= check_header_fi_mismatch(fpc, &fpc->last_fi, &header->fi, AV_LOG_DEBUG); } header->max_score = base_score; /* Check and compute the children's scores. */ child = header->next; for (dist = 0; dist < FLAC_MAX_SEQUENTIAL_HEADERS && child; dist++) { /* Look at the child's frame header info and penalize suspicious changes between the headers. */ if (header->link_penalty[dist] == FLAC_HEADER_NOT_PENALIZED_YET) { header->link_penalty[dist] = check_header_mismatch(fpc, header, child, AV_LOG_DEBUG); } child_score = score_header(fpc, child) - header->link_penalty[dist]; if (FLAC_HEADER_BASE_SCORE + child_score > header->max_score) { /* Keep the child because the frame scoring is dynamic. */ header->best_child = child; header->max_score = base_score + child_score; } child = child->next; } return header->max_score; }