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;
185 *end_handle =
av_mallocz(
sizeof(**end_handle));
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) *
195 if (!(*end_handle)->link_penalty) {
198 "couldn't allocate link_penalty\n");
215 int size = 0, mod_offset = (buf_size - 1) % 4, i, j;
218 for (i = 0; i < mod_offset; i++) {
219 if ((
AV_RB16(buf + i) & 0xFFFE) == 0xFFF8)
223 for (; i < buf_size - 1; i += 4) {
225 if (((x & ~(x + 0x01010101)) & 0x80808080)) {
226 for (j = 0; j < 4; j++) {
227 if ((
AV_RB16(buf + i + j) & 0xFFFE) == 0xFFF8)
238 int search_end,
size = 0, read_len,
temp;
244 read_len = search_end - search_start + 1;
247 search_start += read_len - 1;
250 if (search_start != search_end) {
253 wrap[0] = buf[read_len - 1];
254 read_len = search_end - search_start + 1;
260 if ((
AV_RB16(wrap) & 0xFFFE) == 0xFFF8) {
262 size =
FFMAX(size, temp);
268 size =
FFMAX(size, temp);
269 search_start += read_len - 1;
282 int log_level_offset)
285 if (child_fi->samplerate != header_fi->samplerate) {
288 "sample rate change detected in adjacent frames\n");
290 if (child_fi->bps != header_fi->bps) {
293 "bits per sample change detected in adjacent frames\n");
299 "blocking strategy change detected in adjacent frames\n");
301 if (child_fi->channels != header_fi->channels) {
304 "number of channels change detected in adjacent frames\n");
312 int log_level_offset)
315 int deduction, deduction_expected = 0, i;
321 (child_fi->frame_or_sample_num
324 int expected_frame_num, expected_sample_num;
330 while (curr != child) {
334 expected_frame_num++;
342 if (expected_frame_num == child_fi->frame_or_sample_num ||
343 expected_sample_num == child_fi->frame_or_sample_num)
344 deduction_expected = deduction ? 0 : 1;
348 "sample/frame number mismatch in adjacent frames\n");
352 if (deduction && !deduction_expected) {
357 int inverted_test = 0;
375 while (start->
next != child)
396 if (!crc ^ !inverted_test) {
399 "crc check failed from offset %i (frame %"PRId64
") to %i (frame %"PRId64
")\n",
401 child->
offset, child_fi->frame_or_sample_num);
436 child = header->
next;
449 header->
max_score = base_score + child_score;
466 for (curr = fpc->
headers; curr; curr = curr->
next) {
519 const uint8_t **poutbuf,
int *poutbuf_size,
541 *poutbuf_size = buf_size;
555 for (curr = fpc->
headers; curr != best_child; curr = temp) {
558 "dropping low score %i frame header from offset %i to %i\n",
570 for (curr = best_child->
next; curr; curr = curr->
next)
600 while ((buf && buf_size && read_end < buf + buf_size &&
602 || ((!buf || !buf_size) && !fpc->
end_padded)) {
606 if (!buf || !buf_size) {
614 read_end = read_end +
FFMIN(buf + buf_size - read_end,
627 if (buf && buf_size) {
629 read_end - read_start,
NULL);
638 start_offset =
FFMAX(0, start_offset);
641 if (nb_headers < 0) {
643 "find_new_headers couldn't allocate FLAC header\n");
650 if (buf && read_end < buf + buf_size) {
651 read_start = read_end;
673 read_start = read_end =
NULL;
677 for (curr = fpc->
headers; curr; curr = curr->
next) {
707 return buf_size ? read_end - buf : 0;
719 "couldn't allocate fifo_buf\n");