return ret;
}
+/**
+ * Remove the first sample from the sample queue.
+ */
+static void sample_queue_pop(HintSampleQueue *queue)
+{
+ if (queue->len <= 0)
+ return;
+ if (queue->samples[0].own_data)
+ av_free(queue->samples[0].data);
+ queue->len--;
+ memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
+}
+
+/**
+ * Empty the sample queue, releasing all memory.
+ */
+static void sample_queue_free(HintSampleQueue *queue)
+{
+ int i;
+ for (i = 0; i < queue->len; i++)
+ if (queue->samples[i].own_data)
+ av_free(queue->samples[i].data);
+ av_freep(&queue->samples);
+ queue->len = 0;
+ queue->size = 0;
+}
+
+/**
+ * Add a reference to the sample data to the sample queue. The data is
+ * not copied. sample_queue_retain should be called before pkt->data
+ * is reused/freed.
+ */
+static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample)
+{
+ /* No need to keep track of smaller samples, since describing them
+ * with immediates is more efficient. */
+ if (pkt->size <= 14)
+ return;
+ if (!queue->samples || queue->len >= queue->size) {
+ HintSample* samples;
+ queue->size += 10;
+ samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size);
+ if (!samples)
+ return;
+ queue->samples = samples;
+ }
+ queue->samples[queue->len].data = pkt->data;
+ queue->samples[queue->len].size = pkt->size;
+ queue->samples[queue->len].sample_number = sample;
+ queue->samples[queue->len].offset = 0;
+ queue->samples[queue->len].own_data = 0;
+ queue->len++;
+}
+
+/**
+ * Make local copies of all referenced sample data in the queue.
+ */
+static void sample_queue_retain(HintSampleQueue *queue)
+{
+ int i;
+ for (i = 0; i < queue->len; ) {
+ HintSample *sample = &queue->samples[i];
+ if (!sample->own_data) {
+ uint8_t* ptr = av_malloc(sample->size);
+ if (!ptr) {
+ /* Unable to allocate memory for this one, remove it */
+ memmove(queue->samples + i, queue->samples + i + 1,
+ sizeof(HintSample)*(queue->len - i - 1));
+ queue->len--;
+ continue;
+ }
+ memcpy(ptr, sample->data, sample->size);
+ sample->data = ptr;
+ sample->own_data = 1;
+ }
+ i++;
+ }
+}
+
+/**
+ * Find matches of needle[n_pos ->] within haystack. If a sufficiently
+ * large match is found, matching bytes before n_pos are included
+ * in the match, too (within the limits of the arrays).
+ *
+ * @param haystack buffer that may contain parts of needle
+ * @param h_len length of the haystack buffer
+ * @param needle buffer containing source data that have been used to
+ * construct haystack
+ * @param n_pos start position in needle used for looking for matches
+ * @param n_len length of the needle buffer
+ * @param match_h_offset_ptr offset of the first matching byte within haystack
+ * @param match_n_offset_ptr offset of the first matching byte within needle
+ * @param match_len_ptr length of the matched segment
+ * @return 0 if a match was found, < 0 if no match was found
+ */
+static int match_segments(const uint8_t *haystack, int h_len,
+ const uint8_t *needle, int n_pos, int n_len,
+ int *match_h_offset_ptr, int *match_n_offset_ptr,
+ int *match_len_ptr)
+{
+ int h_pos;
+ for (h_pos = 0; h_pos < h_len; h_pos++) {
+ int match_len = 0;
+ int match_h_pos, match_n_pos;
+
+ /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
+ while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
+ needle[n_pos + match_len] == haystack[h_pos + match_len])
+ match_len++;
+ if (match_len <= 8)
+ continue;
+
+ /* If a sufficiently large match was found, try to expand
+ * the matched segment backwards. */
+ match_h_pos = h_pos;
+ match_n_pos = n_pos;
+ while (match_n_pos > 0 && match_h_pos > 0 &&
+ needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
+ match_n_pos--;
+ match_h_pos--;
+ match_len++;
+ }
+ if (match_len <= 14)
+ continue;
+ *match_h_offset_ptr = match_h_pos;
+ *match_n_offset_ptr = match_n_pos;
+ *match_len_ptr = match_len;
+ return 0;
+ }
+ return -1;
+}
+
+/**
+ * Look for segments in samples in the sample queue matching the data
+ * in ptr. Samples not matching are removed from the queue. If a match
+ * is found, the next time it will look for matches starting from the
+ * end of the previous matched segment.
+ *
+ * @param data data to find matches for in the sample queue
+ * @param len length of the data buffer
+ * @param queue samples used for looking for matching segments
+ * @param pos the offset in data of the matched segment
+ * @param match_sample the number of the sample that contained the match
+ * @param match_offset the offset of the matched segment within the sample
+ * @param match_len the length of the matched segment
+ * @return 0 if a match was found, < 0 if no match was found
+ */
+static int find_sample_match(const uint8_t *data, int len,
+ HintSampleQueue *queue, int *pos,
+ int *match_sample, int *match_offset,
+ int *match_len)
+{
+ while (queue->len > 0) {
+ HintSample *sample = &queue->samples[0];
+ /* If looking for matches in a new sample, skip the first 5 bytes,
+ * since they often may be modified/removed in the output packet. */
+ if (sample->offset == 0 && sample->size > 5)
+ sample->offset = 5;
+
+ if (match_segments(data, len, sample->data, sample->offset,
+ sample->size, pos, match_offset, match_len) == 0) {
+ *match_sample = sample->sample_number;
+ /* Next time, look for matches at this offset, with a little
+ * margin to this match. */
+ sample->offset = *match_offset + *match_len + 5;
+ if (sample->offset + 10 >= sample->size)
+ sample_queue_pop(queue); /* Not enough useful data left */
+ return 0;
+ }
+
+ if (sample->offset < 10 && sample->size > 20) {
+ /* No match found from the start of the sample,
+ * try from the middle of the sample instead. */
+ sample->offset = sample->size/2;
+ } else {
+ /* No match for this sample, remove it */
+ sample_queue_pop(queue);
+ }
+ }
+ return -1;
+}
+
static void output_immediate(const uint8_t *data, int size,
ByteIOContext *out, int *entries)
{
}
}
+static void output_match(ByteIOContext *out, int match_sample,
+ int match_offset, int match_len, int *entries)
+{
+ put_byte(out, 2); /* sample constructor */
+ put_byte(out, 0); /* track reference */
+ put_be16(out, match_len);
+ put_be32(out, match_sample);
+ put_be32(out, match_offset);
+ put_be16(out, 1); /* bytes per block */
+ put_be16(out, 1); /* samples per block */
+ (*entries)++;
+}
+
static void describe_payload(const uint8_t *data, int size,
- ByteIOContext *out, int *entries)
+ ByteIOContext *out, int *entries,
+ HintSampleQueue *queue)
{
/* Describe the payload using different constructors */
+ while (size > 0) {
+ int match_sample, match_offset, match_len, pos;
+ if (find_sample_match(data, size, queue, &pos, &match_sample,
+ &match_offset, &match_len) < 0)
+ break;
+ output_immediate(data, pos, out, entries);
+ data += pos;
+ size -= pos;
+ output_match(out, match_sample, match_offset, match_len, entries);
+ data += match_len;
+ size -= match_len;
+ }
output_immediate(data, size, out, entries);
}
entries = 0;
/* Write one or more constructors describing the payload data */
- describe_payload(data, packet_len, out, &entries);
+ describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
data += packet_len;
size -= packet_len;
if (!rtp_ctx->pb)
return AVERROR(ENOMEM);
+ sample_queue_push(&trk->sample_queue, pkt, sample);
+
/* Feed the packet to the RTP muxer */
local_pkt = *pkt;
local_pkt.stream_index = 0;
ff_mov_write_packet(s, &hint_pkt);
done:
av_free(buf);
+ sample_queue_retain(&trk->sample_queue);
return ret;
}
uint8_t *ptr;
av_freep(&track->enc);
+ sample_queue_free(&track->sample_queue);
if (!rtp_ctx)
return;
if (rtp_ctx->pb) {