+ assert(chunk_tv);
+ k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
+ if (fc->first_stream_chunk < 0) {
+ uint32_t largest = afh_get_largest_chunk_size(&mmd->afd.afhi)
+ + vsst->header_len;
+ uint8_t needed, want;
+
+ ret = num_slices(largest, fc, &needed);
+ if (ret < 0)
+ return ret;
+ if (needed > fc->fcp->data_slices_per_group)
+ PARA_WARNING_LOG("fec parms insufficient for this audio file\n");
+ want = PARA_MAX(needed, fc->fcp->data_slices_per_group);
+ if (want != k) {
+ fec_free(fc->parms);
+ fc->src_data = para_realloc(fc->src_data, want * sizeof(char *));
+ ret = fec_new(want, want + fc->fcp->slices_per_group
+ - fc->fcp->data_slices_per_group, &fc->parms);
+ if (ret < 0)
+ return ret;
+ k = want;
+ fc->num_extra_slices = 0;
+ if (k > fc->fcp->data_slices_per_group) {
+ fc->num_extra_slices = k - fc->fcp->data_slices_per_group;
+ PARA_NOTICE_LOG("using %d extra slices\n",
+ fc->num_extra_slices);
+ }
+ }
+ fc->stream_start = *now;
+ fc->first_stream_chunk = mmd->current_chunk;
+ g->first_chunk = mmd->current_chunk;
+ g->num = 0;
+ } else {
+ /* use duration of the previous group for the timing of this group */
+ set_slice_duration(fc, g);
+ g->first_chunk += g->num_chunks;
+ g->num++;
+ }
+ if (g->first_chunk >= mmd->afd.afhi.chunks_total)
+ return 0;
+ if (need_audio_header(fc, vsst)) {
+ ret = num_slices(vsst->header_len, fc, &g->num_header_slices);
+ if (ret < 0)
+ return ret;
+ } else
+ g->num_header_slices = 0;
+ afh_get_chunk(g->first_chunk, &mmd->afd.afhi, vsst->map, &start_buf,
+ &len);
+ data_slices = k - g->num_header_slices;
+ assert(data_slices);
+ max_data_size = slice_bytes * data_slices;
+ g->bytes = 0;
+ for (i = g->first_chunk; i < mmd->afd.afhi.chunks_total; i++) {
+ afh_get_chunk(i, &mmd->afd.afhi, vsst->map, &buf, &len);
+ if (g->bytes + len > max_data_size)
+ break;
+ g->bytes += len;
+ }
+ g->num_chunks = i - g->first_chunk;
+ assert(g->num_chunks);
+ fc->current_slice_num = 0;
+ if (g->num == 0)
+ set_slice_duration(fc, g);
+
+ /* setup header slices */
+ buf = vsst->header_buf;
+ for (i = 0; i < g->num_header_slices; i++) {
+ fc->src_data[i] = (const unsigned char *)buf;
+ buf += slice_bytes;
+ }
+
+ /* setup data slices */
+ buf = start_buf;
+ for (i = g->num_header_slices; i < k; i++) {
+ if (buf + slice_bytes > vsst->map + mmd->size)
+ /*
+ * Can not use the memory mapped audio file for this
+ * slice as it goes beyond the map. This slice will not
+ * be fully used.
+ */
+ break;
+ fc->src_data[i] = (const unsigned char *)buf;
+ buf += slice_bytes;
+ }
+ if (i < k) {
+ uint32_t payload_size = vsst->map + mmd->size - buf;
+ memcpy(fc->extra_src_buf, buf, payload_size);
+ fc->src_data[i] = fc->extra_src_buf;
+ i++;
+ /* use arbitrary data for all remaining slices */
+ buf = vsst->map;
+ for (; i < k; i++)
+ fc->src_data[i] = (const unsigned char *)buf;
+ }
+ PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), "
+ "%d header slices, %d data slices\n",
+ g->num, g->num_chunks, g->first_chunk,
+ g->first_chunk + g->num_chunks - 1,
+ g->num_header_slices, data_slices
+ );
+ /* set group start */
+ if (g->num != 0 && vsst->header_len != 0 && fc->first_stream_chunk == 0)
+ /* chunk #0 is the audio file header */
+ tv_scale(g->first_chunk - 1, chunk_tv, &tmp);
+ else
+ tv_scale(g->first_chunk - fc->first_stream_chunk,
+ chunk_tv, &tmp);
+ tv_add(&fc->stream_start, &tmp, &g->start);
+ return 1;
+}