2 * MOV, 3GP, MP4 muxer RTP hinting
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
25 #include "rtpenc_chain.h"
26 #include "avio_internal.h"
29 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
31 MOVMuxContext *mov = s->priv_data;
32 MOVTrack *track = &mov->tracks[index];
33 MOVTrack *src_track = &mov->tracks[src_index];
34 AVStream *src_st = s->streams[src_index];
35 int ret = AVERROR(ENOMEM);
37 track->tag = MKTAG('r','t','p',' ');
38 track->src_track = src_index;
40 track->enc = avcodec_alloc_context3(NULL);
43 track->enc->codec_type = AVMEDIA_TYPE_DATA;
44 track->enc->codec_tag = track->tag;
46 ret = ff_rtp_chain_mux_open(&track->rtp_ctx, s, src_st, NULL,
47 RTP_MAX_PACKET_SIZE, src_index);
51 /* Copy the RTP AVStream timebase back to the hint AVStream */
52 track->timescale = track->rtp_ctx->streams[0]->time_base.den;
54 /* Mark the hinted track that packets written to it should be
55 * sent to this track for hinting. */
56 src_track->hint_track = index;
59 av_log(s, AV_LOG_WARNING,
60 "Unable to initialize hinting of stream %d\n", src_index);
61 av_freep(&track->enc);
62 /* Set a default timescale, to avoid crashes in av_dump_format */
63 track->timescale = 90000;
68 * Remove the first sample from the sample queue.
70 static void sample_queue_pop(HintSampleQueue *queue)
74 if (queue->samples[0].own_data)
75 av_free(queue->samples[0].data);
77 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
81 * Empty the sample queue, releasing all memory.
83 static void sample_queue_free(HintSampleQueue *queue)
86 for (i = 0; i < queue->len; i++)
87 if (queue->samples[i].own_data)
88 av_free(queue->samples[i].data);
89 av_freep(&queue->samples);
95 * Add a reference to the sample data to the sample queue. The data is
96 * not copied. sample_queue_retain should be called before pkt->data
99 static void sample_queue_push(HintSampleQueue *queue, uint8_t *data, int size,
102 /* No need to keep track of smaller samples, since describing them
103 * with immediates is more efficient. */
106 if (!queue->samples || queue->len >= queue->size) {
108 if (av_reallocp(&queue->samples, sizeof(*queue->samples) * queue->size) < 0) {
109 queue->len = queue->size = 0;
113 queue->samples[queue->len].data = data;
114 queue->samples[queue->len].size = size;
115 queue->samples[queue->len].sample_number = sample;
116 queue->samples[queue->len].offset = 0;
117 queue->samples[queue->len].own_data = 0;
122 * Make local copies of all referenced sample data in the queue.
124 static void sample_queue_retain(HintSampleQueue *queue)
127 for (i = 0; i < queue->len; ) {
128 HintSample *sample = &queue->samples[i];
129 if (!sample->own_data) {
130 uint8_t *ptr = av_malloc(sample->size);
132 /* Unable to allocate memory for this one, remove it */
133 memmove(queue->samples + i, queue->samples + i + 1,
134 sizeof(HintSample)*(queue->len - i - 1));
138 memcpy(ptr, sample->data, sample->size);
140 sample->own_data = 1;
147 * Find matches of needle[n_pos ->] within haystack. If a sufficiently
148 * large match is found, matching bytes before n_pos are included
149 * in the match, too (within the limits of the arrays).
151 * @param haystack buffer that may contain parts of needle
152 * @param h_len length of the haystack buffer
153 * @param needle buffer containing source data that have been used to
155 * @param n_pos start position in needle used for looking for matches
156 * @param n_len length of the needle buffer
157 * @param match_h_offset_ptr offset of the first matching byte within haystack
158 * @param match_n_offset_ptr offset of the first matching byte within needle
159 * @param match_len_ptr length of the matched segment
160 * @return 0 if a match was found, < 0 if no match was found
162 static int match_segments(const uint8_t *haystack, int h_len,
163 const uint8_t *needle, int n_pos, int n_len,
164 int *match_h_offset_ptr, int *match_n_offset_ptr,
168 for (h_pos = 0; h_pos < h_len; h_pos++) {
170 int match_h_pos, match_n_pos;
172 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
173 while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
174 needle[n_pos + match_len] == haystack[h_pos + match_len])
179 /* If a sufficiently large match was found, try to expand
180 * the matched segment backwards. */
183 while (match_n_pos > 0 && match_h_pos > 0 &&
184 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
191 *match_h_offset_ptr = match_h_pos;
192 *match_n_offset_ptr = match_n_pos;
193 *match_len_ptr = match_len;
200 * Look for segments in samples in the sample queue matching the data
201 * in ptr. Samples not matching are removed from the queue. If a match
202 * is found, the next time it will look for matches starting from the
203 * end of the previous matched segment.
205 * @param data data to find matches for in the sample queue
206 * @param len length of the data buffer
207 * @param queue samples used for looking for matching segments
208 * @param pos the offset in data of the matched segment
209 * @param match_sample the number of the sample that contained the match
210 * @param match_offset the offset of the matched segment within the sample
211 * @param match_len the length of the matched segment
212 * @return 0 if a match was found, < 0 if no match was found
214 static int find_sample_match(const uint8_t *data, int len,
215 HintSampleQueue *queue, int *pos,
216 int *match_sample, int *match_offset,
219 while (queue->len > 0) {
220 HintSample *sample = &queue->samples[0];
221 /* If looking for matches in a new sample, skip the first 5 bytes,
222 * since they often may be modified/removed in the output packet. */
223 if (sample->offset == 0 && sample->size > 5)
226 if (match_segments(data, len, sample->data, sample->offset,
227 sample->size, pos, match_offset, match_len) == 0) {
228 *match_sample = sample->sample_number;
229 /* Next time, look for matches at this offset, with a little
230 * margin to this match. */
231 sample->offset = *match_offset + *match_len + 5;
232 if (sample->offset + 10 >= sample->size)
233 sample_queue_pop(queue); /* Not enough useful data left */
237 if (sample->offset < 10 && sample->size > 20) {
238 /* No match found from the start of the sample,
239 * try from the middle of the sample instead. */
240 sample->offset = sample->size/2;
242 /* No match for this sample, remove it */
243 sample_queue_pop(queue);
249 static void output_immediate(const uint8_t *data, int size,
250 AVIOContext *out, int *entries)
256 avio_w8(out, 1); /* immediate constructor */
257 avio_w8(out, len); /* amount of valid data */
258 avio_write(out, data, len);
262 for (; len < 14; len++)
269 static void output_match(AVIOContext *out, int match_sample,
270 int match_offset, int match_len, int *entries)
272 avio_w8(out, 2); /* sample constructor */
273 avio_w8(out, 0); /* track reference */
274 avio_wb16(out, match_len);
275 avio_wb32(out, match_sample);
276 avio_wb32(out, match_offset);
277 avio_wb16(out, 1); /* bytes per block */
278 avio_wb16(out, 1); /* samples per block */
282 static void describe_payload(const uint8_t *data, int size,
283 AVIOContext *out, int *entries,
284 HintSampleQueue *queue)
286 /* Describe the payload using different constructors */
288 int match_sample, match_offset, match_len, pos;
289 if (find_sample_match(data, size, queue, &pos, &match_sample,
290 &match_offset, &match_len) < 0)
292 output_immediate(data, pos, out, entries);
295 output_match(out, match_sample, match_offset, match_len, entries);
299 output_immediate(data, size, out, entries);
303 * Write an RTP hint (that may contain one or more RTP packets)
304 * for the packets in data. data contains one or more packets with a
307 * @param out buffer where the hints are written
308 * @param data buffer containing RTP packets
309 * @param size the size of the data buffer
310 * @param trk the MOVTrack for the hint track
311 * @param dts pointer where the timestamp for the written RTP hint is stored
312 * @return the number of RTP packets in the written hint
314 static int write_hint_packets(AVIOContext *out, const uint8_t *data,
315 int size, MOVTrack *trk, int64_t *dts)
318 int64_t count_pos, entries_pos;
319 int count = 0, entries;
321 count_pos = avio_tell(out);
322 /* RTPsample header */
323 avio_wb16(out, 0); /* packet count */
324 avio_wb16(out, 0); /* reserved */
327 uint32_t packet_len = AV_RB32(data);
334 if (packet_len > size || packet_len <= 12)
336 if (RTP_PT_IS_RTCP(data[1])) {
337 /* RTCP packet, just skip */
343 if (packet_len > trk->max_packet_size)
344 trk->max_packet_size = packet_len;
346 seq = AV_RB16(&data[2]);
347 ts = AV_RB32(&data[4]);
349 if (trk->prev_rtp_ts == 0)
350 trk->prev_rtp_ts = ts;
351 /* Unwrap the 32-bit RTP timestamp that wraps around often
352 * into a not (as often) wrapping 64-bit timestamp. */
353 ts_diff = ts - trk->prev_rtp_ts;
355 trk->cur_rtp_ts_unwrapped += ts_diff;
356 trk->prev_rtp_ts = ts;
359 if (*dts == AV_NOPTS_VALUE)
360 *dts = trk->cur_rtp_ts_unwrapped;
363 /* RTPpacket header */
364 avio_wb32(out, 0); /* relative_time */
365 avio_write(out, data, 2); /* RTP header */
366 avio_wb16(out, seq); /* RTPsequenceseed */
367 avio_wb16(out, ts_diff ? 4 : 0); /* reserved + flags (extra_flag) */
368 entries_pos = avio_tell(out);
369 avio_wb16(out, 0); /* entry count */
370 if (ts_diff) { /* if extra_flag is set */
371 avio_wb32(out, 16); /* extra_information_length */
372 avio_wb32(out, 12); /* rtpoffsetTLV box */
373 avio_write(out, "rtpo", 4);
374 avio_wb32(out, ts_diff);
382 /* Write one or more constructors describing the payload data */
383 describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
387 curpos = avio_tell(out);
388 avio_seek(out, entries_pos, SEEK_SET);
389 avio_wb16(out, entries);
390 avio_seek(out, curpos, SEEK_SET);
393 curpos = avio_tell(out);
394 avio_seek(out, count_pos, SEEK_SET);
395 avio_wb16(out, count);
396 avio_seek(out, curpos, SEEK_SET);
400 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
401 int track_index, int sample,
402 uint8_t *sample_data, int sample_size)
404 MOVMuxContext *mov = s->priv_data;
405 MOVTrack *trk = &mov->tracks[track_index];
406 AVFormatContext *rtp_ctx = trk->rtp_ctx;
409 AVIOContext *hintbuf = NULL;
414 return AVERROR(ENOENT);
416 return AVERROR(ENOMEM);
419 sample_queue_push(&trk->sample_queue, sample_data, sample_size, sample);
421 sample_queue_push(&trk->sample_queue, pkt->data, pkt->size, sample);
423 /* Feed the packet to the RTP muxer */
424 ff_write_chained(rtp_ctx, 0, pkt, s);
426 /* Fetch the output from the RTP muxer, open a new output buffer
428 size = avio_close_dyn_buf(rtp_ctx->pb, &buf);
429 if ((ret = ffio_open_dyn_packet_buf(&rtp_ctx->pb,
430 RTP_MAX_PACKET_SIZE)) < 0)
436 /* Open a buffer for writing the hint */
437 if ((ret = avio_open_dyn_buf(&hintbuf)) < 0)
439 av_init_packet(&hint_pkt);
440 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts);
443 /* Write the hint data into the hint track */
444 hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf);
446 hint_pkt.pts = hint_pkt.dts;
447 hint_pkt.stream_index = track_index;
448 if (pkt->flags & AV_PKT_FLAG_KEY)
449 hint_pkt.flags |= AV_PKT_FLAG_KEY;
451 ff_mov_write_packet(s, &hint_pkt);
454 sample_queue_retain(&trk->sample_queue);
458 void ff_mov_close_hinting(MOVTrack *track)
460 AVFormatContext *rtp_ctx = track->rtp_ctx;
463 av_freep(&track->enc);
464 sample_queue_free(&track->sample_queue);
468 av_write_trailer(rtp_ctx);
469 avio_close_dyn_buf(rtp_ctx->pb, &ptr);
472 avformat_free_context(rtp_ctx);