43 #define FLAC_MAX_SEQUENTIAL_HEADERS 3
45 #define FLAC_MIN_HEADERS 10
47 #define FLAC_AVG_FRAME_SIZE 8192
50 #define FLAC_HEADER_BASE_SCORE 10
51 #define FLAC_HEADER_CHANGED_PENALTY 7
52 #define FLAC_HEADER_CRC_FAIL_PENALTY 50
53 #define FLAC_HEADER_NOT_PENALIZED_YET 100000
54 #define FLAC_HEADER_NOT_SCORED_YET -100000
57 #define MAX_FRAME_HEADER_SIZE 16
116 uint8_t** wrap_buf,
int* allocated_size)
124 if (f->
end - start >= len)
131 "couldn't reallocate wrap buffer of size %d", len);
136 int seg_len =
FFMIN(f->
end - start, len);
137 memcpy(tmp_buf, start, seg_len);
138 tmp_buf = (
uint8_t*)tmp_buf + seg_len;
180 while (*end_handle) {
181 end_handle = &(*end_handle)->
next;
188 "couldn't allocate FLACHeaderMarker\n");
191 (*end_handle)->fi =
fi;
192 (*end_handle)->offset =
offset;
193 (*end_handle)->link_penalty =
av_malloc(
sizeof(
int) *
208 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
211 for (i = 0; i < mod_offset; i++) {
212 if ((
AV_RB16(buf + i) & 0xFFFE) == 0xFFF8)
216 for (; i < buf_size - 1; i += 4) {
218 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
219 for (j = 0; j < 4; j++) {
220 if ((
AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
231 int search_end,
size = 0, read_len,
temp;
237 read_len = search_end - search_start + 1;
240 search_start += read_len - 1;
243 if (search_start != search_end) {
246 wrap[0] = buf[read_len - 1];
247 read_len = search_end - search_start + 1;
253 if ((
AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
255 size =
FFMAX(size, temp);
261 size =
FFMAX(size, temp);
262 search_start += read_len - 1;
275 int log_level_offset)
278 if (child_fi->samplerate != header_fi->samplerate) {
281 "sample rate change detected in adjacent frames\n");
283 if (child_fi->bps != header_fi->bps) {
286 "bits per sample change detected in adjacent frames\n");
292 "blocking strategy change detected in adjacent frames\n");
294 if (child_fi->channels != header_fi->channels) {
297 "number of channels change detected in adjacent frames\n");
305 int log_level_offset)
308 int deduction, deduction_expected = 0, i;
314 (child_fi->frame_or_sample_num
317 int expected_frame_num, expected_sample_num;
323 while (curr != child) {
327 expected_frame_num++;
335 if (expected_frame_num == child_fi->frame_or_sample_num ||
336 expected_sample_num == child_fi->frame_or_sample_num)
337 deduction_expected = deduction ? 0 : 1;
341 "sample/frame number mismatch in adjacent frames\n");
345 if (deduction && !deduction_expected) {
350 int inverted_test = 0;
368 while (start->
next != child)
389 if (!crc ^ !inverted_test) {
392 "crc check failed from offset %i (frame %"PRId64
") to %i (frame %"PRId64
")\n",
394 child->
offset, child_fi->frame_or_sample_num);
429 child = header->
next;
442 header->
max_score = base_score + child_score;
459 for (curr = fpc->
headers; curr; curr = curr->
next) {
504 const uint8_t **poutbuf,
int *poutbuf_size,
518 *poutbuf_size = buf_size;
532 for (curr = fpc->
headers; curr != best_child; curr = temp) {
535 "dropping low score %i frame header from offset %i to %i\n",
547 for (curr = best_child->
next; curr; curr = curr->
next)
577 while ((buf && buf_size && read_end < buf + buf_size &&
579 || ((!buf || !buf_size) && !fpc->
end_padded)) {
583 if (!buf || !buf_size) {
591 read_end = read_end +
FFMIN(buf + buf_size - read_end,
599 "couldn't reallocate buffer of size %td\n",
604 if (buf && buf_size) {
606 read_end - read_start, NULL);
615 start_offset =
FFMAX(0, start_offset);
618 if (nb_headers < 0) {
620 "find_new_headers couldn't allocate FLAC header\n");
627 if (buf && read_end < buf + buf_size) {
628 read_start = read_end;
650 read_start = read_end = NULL;
655 for (curr = fpc->
headers; curr; curr = curr->
next) {
685 return read_end -
buf;