1 /*
2 * MOV, 3GP, MP4 muxer RTP hinting
3 * Copyright (c) 2010 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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.
11 *
12 * FFmpeg 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
28
30 {
36
39
42 goto fail;
45
48 if (ret < 0)
49 goto fail;
50
51 /* Copy the RTP AVStream timebase back to the hint AVStream */
53
54 /* Mark the hinted track that packets written to it should be
55 * sent to this track for hinting. */
57 return 0;
58 fail:
60 "Unable to initialize hinting of stream %d\n", src_index);
62 /* Set a default timescale, to avoid crashes in av_dump_format */
65 }
66
67 /**
68 * Remove the first sample from the sample queue.
69 */
71 {
73 return;
78 }
79
80 /**
81 * Empty the sample queue, releasing all memory.
82 */
84 {
85 int i;
86 for (i = 0; i < queue->
len; i++)
92 }
93
94 /**
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
97 * is reused/freed.
98 */
101 {
102 /* No need to keep track of smaller samples, since describing them
103 * with immediates is more efficient. */
104 if (size <= 14)
105 return;
109 if (!samples)
110 return;
113 }
120 }
121
122 /**
123 * Make local copies of all referenced sample data in the queue.
124 */
126 {
127 int i;
128 for (i = 0; i < queue->
len; ) {
132 if (!ptr) {
133 /* Unable to allocate memory for this one, remove it */
137 continue;
138 }
139 memcpy(ptr, sample->
data, sample->
size);
142 }
143 i++;
144 }
145 }
146
147 /**
148 * Find matches of needle[n_pos ->] within haystack. If a sufficiently
149 * large match is found, matching bytes before n_pos are included
150 * in the match, too (within the limits of the arrays).
151 *
152 * @param haystack buffer that may contain parts of needle
153 * @param h_len length of the haystack buffer
154 * @param needle buffer containing source data that have been used to
155 * construct haystack
156 * @param n_pos start position in needle used for looking for matches
157 * @param n_len length of the needle buffer
158 * @param match_h_offset_ptr offset of the first matching byte within haystack
159 * @param match_n_offset_ptr offset of the first matching byte within needle
160 * @param match_len_ptr length of the matched segment
161 * @return 0 if a match was found, < 0 if no match was found
162 */
164 const uint8_t *needle,
int n_pos,
int n_len,
165 int *match_h_offset_ptr, int *match_n_offset_ptr,
166 int *match_len_ptr)
167 {
168 int h_pos;
169 for (h_pos = 0; h_pos < h_len; h_pos++) {
170 int match_len = 0;
171 int match_h_pos, match_n_pos;
172
173 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
174 while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
175 needle[n_pos + match_len] == haystack[h_pos + match_len])
176 match_len++;
177 if (match_len <= 8)
178 continue;
179
180 /* If a sufficiently large match was found, try to expand
181 * the matched segment backwards. */
182 match_h_pos = h_pos;
183 match_n_pos = n_pos;
184 while (match_n_pos > 0 && match_h_pos > 0 &&
185 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
186 match_n_pos--;
187 match_h_pos--;
188 match_len++;
189 }
190 if (match_len <= 14)
191 continue;
192 *match_h_offset_ptr = match_h_pos;
193 *match_n_offset_ptr = match_n_pos;
194 *match_len_ptr = match_len;
195 return 0;
196 }
197 return -1;
198 }
199
200 /**
201 * Look for segments in samples in the sample queue matching the data
202 * in ptr. Samples not matching are removed from the queue. If a match
203 * is found, the next time it will look for matches starting from the
204 * end of the previous matched segment.
205 *
206 * @param data data to find matches for in the sample queue
207 * @param len length of the data buffer
208 * @param queue samples used for looking for matching segments
209 * @param pos the offset in data of the matched segment
210 * @param match_sample the number of the sample that contained the match
211 * @param match_offset the offset of the matched segment within the sample
212 * @param match_len the length of the matched segment
213 * @return 0 if a match was found, < 0 if no match was found
214 */
217 int *match_sample, int *match_offset,
218 int *match_len)
219 {
220 while (queue->
len > 0) {
222 /* If looking for matches in a new sample, skip the first 5 bytes,
223 * since they often may be modified/removed in the output packet. */
226
228 sample->
size, pos, match_offset, match_len) == 0) {
230 /* Next time, look for matches at this offset, with a little
231 * margin to this match. */
232 sample->
offset = *match_offset + *match_len + 5;
235 return 0;
236 }
237
238 if (sample->
offset < 10 && sample->
size > 20) {
239 /* No match found from the start of the sample,
240 * try from the middle of the sample instead. */
242 } else {
243 /* No match for this sample, remove it */
245 }
246 }
247 return -1;
248 }
249
252 {
253 while (size > 0) {
255 if (len > 14)
256 len = 14;
257 avio_w8(out, 1);
/* immediate constructor */
258 avio_w8(out, len);
/* amount of valid data */
262
263 for (; len < 14; len++)
265
266 (*entries)++;
267 }
268 }
269
271 int match_offset, int match_len, int *entries)
272 {
273 avio_w8(out, 2);
/* sample constructor */
274 avio_w8(out, 0);
/* track reference */
279 avio_wb16(out, 1);
/* samples per block */
280 (*entries)++;
281 }
282
286 {
287 /* Describe the payload using different constructors */
288 while (size > 0) {
289 int match_sample, match_offset, match_len, pos;
291 &match_offset, &match_len) < 0)
292 break;
294 data += pos;
295 size -= pos;
296 output_match(out, match_sample, match_offset, match_len, entries);
297 data += match_len;
298 size -= match_len;
299 }
301 }
302
303 /**
304 * Write an RTP hint (that may contain one or more RTP packets)
305 * for the packets in data. data contains one or more packets with a
306 * BE32 size header.
307 *
308 * @param out buffer where the hints are written
309 * @param data buffer containing RTP packets
310 * @param size the size of the data buffer
311 * @param trk the MOVTrack for the hint track
312 * @param dts pointer where the timestamp for the written RTP hint is stored
313 * @return the number of RTP packets in the written hint
314 */
317 {
318 int64_t curpos;
319 int64_t count_pos, entries_pos;
320 int count = 0, entries;
321
323 /* RTPsample header */
326
327 while (size > 4) {
328 uint32_t packet_len =
AV_RB32(data);
329 uint16_t seq;
330 uint32_t ts;
332
333 data += 4;
334 size -= 4;
335 if (packet_len > size || packet_len <= 12)
336 break;
338 /* RTCP packet, just skip */
339 data += packet_len;
340 size -= packet_len;
341 continue;
342 }
343
346
349
352 /* Unwrap the 32-bit RTP timestamp that wraps around often
353 * into a not (as often) wrapping 64-bit timestamp. */
355 if (ts_diff > 0) {
358 ts_diff = 0;
359 }
362
363 count++;
364 /* RTPpacket header */
367 avio_wb16(out, seq);
/* RTPsequenceseed */
368 avio_wb16(out, ts_diff ? 4 : 0);
/* reserved + flags (extra_flag) */
371 if (ts_diff) { /* if extra_flag is set */
372 avio_wb32(out, 16);
/* extra_information_length */
373 avio_wb32(out, 12);
/* rtpoffsetTLV box */
376 }
377
378 data += 12;
379 size -= 12;
380 packet_len -= 12;
381
382 entries = 0;
383 /* Write one or more constructors describing the payload data */
385 data += packet_len;
386 size -= packet_len;
387
392 }
393
399 }
400
402 int track_index,
int sample,
403 uint8_t *sample_data,
int sample_size)
404 {
413
414 if (!rtp_ctx)
418
419 if (sample_data)
421 else
423
424 /* Feed the packet to the RTP muxer */
426
427 /* Fetch the output from the RTP muxer, open a new output buffer
428 * for next time. */
432 goto done;
433
434 if (size <= 0)
435 goto done;
436
437 /* Open a buffer for writing the hint */
439 goto done;
443
444 /* Write the hint data into the hint track */
447 hint_pkt.
pts = hint_pkt.
dts;
453 done:
457 }
458
460 {
462
465 if (!rtp_ctx)
466 return;
470 }
472 }