FFmpeg
Loading...
Searching...
No Matches
movenchint.c
Go to the documentation of this file.
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
22#include "movenc.h"
24#include "libavutil/mem.h"
25#include "mux.h"
26#include "rtpenc_chain.h"
27#include "avio_internal.h"
28#include "rtp.h"
29
30int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
31{
32 MOVMuxContext *mov = s->priv_data;
33 MOVTrack *track = &mov->tracks[index];
34 MOVTrack *src_track = &mov->tracks[src_index];
35 AVStream *src_st = s->streams[src_index];
36 int ret = AVERROR(ENOMEM);
37
38 track->tag = MKTAG('r','t','p',' ');
39 track->src_track = av_malloc(sizeof(*track->src_track));
40 if (!track->src_track)
41 return AVERROR(ENOMEM);
42 *track->src_track = src_index;
43 track->nb_src_track = 1;
44
46 if (!track->par)
47 goto fail;
49 track->par->codec_tag = track->tag;
50
51 ret = ff_rtp_chain_mux_open(&track->rtp_ctx, s, src_st, NULL,
52 RTP_MAX_PACKET_SIZE, src_index);
53 if (ret < 0)
54 goto fail;
55
56 /* Copy the RTP AVStream timebase back to the hint AVStream */
57 track->timescale = track->rtp_ctx->streams[0]->time_base.den;
58
59 /* Mark the hinted track that packets written to it should be
60 * sent to this track for hinting. */
61 src_track->hint_track = index;
62 return 0;
63fail:
65 "Unable to initialize hinting of stream %d\n", src_index);
67 /* Set a default timescale, to avoid crashes in av_dump_format */
68 track->timescale = 90000;
69 return ret;
70}
71
72/**
73 * Remove the first sample from the sample queue.
74 */
76{
77 if (queue->len <= 0)
78 return;
79 if (queue->samples[0].own_data)
80 av_freep(&queue->samples[0].data);
81 queue->len--;
82 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
83}
84
85/**
86 * Empty the sample queue, releasing all memory.
87 */
89{
90 int i;
91 for (i = 0; i < queue->len; i++)
92 if (queue->samples[i].own_data)
93 av_freep(&queue->samples[i].data);
94 av_freep(&queue->samples);
95 queue->len = 0;
96 queue->size = 0;
97}
98
99/**
100 * Add a reference to the sample data to the sample queue. The data is
101 * not copied. sample_queue_retain should be called before pkt->data
102 * is reused/freed.
103 */
104static void sample_queue_push(HintSampleQueue *queue, const uint8_t *data, int size,
105 int sample)
106{
107 /* No need to keep track of smaller samples, since describing them
108 * with immediates is more efficient. */
109 if (size <= 14)
110 return;
111 if (!queue->samples || queue->len >= queue->size) {
112 HintSample *samples;
113 samples = av_realloc_array(queue->samples, queue->size + 10, sizeof(HintSample));
114 if (!samples)
115 return;
116 queue->size += 10;
117 queue->samples = samples;
118 }
119 queue->samples[queue->len].data = data;
120 queue->samples[queue->len].size = size;
121 queue->samples[queue->len].sample_number = sample;
122 queue->samples[queue->len].offset = 0;
123 queue->samples[queue->len].own_data = 0;
124 queue->len++;
125}
126
127/**
128 * Make local copies of all referenced sample data in the queue.
129 */
131{
132 int i;
133 for (i = 0; i < queue->len; ) {
134 HintSample *sample = &queue->samples[i];
135 if (!sample->own_data) {
136 uint8_t *ptr = av_malloc(sample->size);
137 if (!ptr) {
138 /* Unable to allocate memory for this one, remove it */
139 memmove(queue->samples + i, queue->samples + i + 1,
140 sizeof(HintSample)*(queue->len - i - 1));
141 queue->len--;
142 continue;
143 }
144 memcpy(ptr, sample->data, sample->size);
145 sample->data = ptr;
146 sample->own_data = 1;
147 }
148 i++;
149 }
150}
151
152/**
153 * Find matches of needle[n_pos ->] within haystack. If a sufficiently
154 * large match is found, matching bytes before n_pos are included
155 * in the match, too (within the limits of the arrays).
156 *
157 * @param haystack buffer that may contain parts of needle
158 * @param h_len length of the haystack buffer
159 * @param needle buffer containing source data that have been used to
160 * construct haystack
161 * @param n_pos start position in needle used for looking for matches
162 * @param n_len length of the needle buffer
163 * @param match_h_offset_ptr offset of the first matching byte within haystack
164 * @param match_n_offset_ptr offset of the first matching byte within needle
165 * @param match_len_ptr length of the matched segment
166 * @return 0 if a match was found, < 0 if no match was found
167 */
168static int match_segments(const uint8_t *haystack, int h_len,
169 const uint8_t *needle, int n_pos, int n_len,
170 int *match_h_offset_ptr, int *match_n_offset_ptr,
171 int *match_len_ptr)
172{
173 int h_pos;
174 for (h_pos = 0; h_pos < h_len; h_pos++) {
175 int match_len = 0;
176 int match_h_pos, match_n_pos;
177
178 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
179 while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
180 needle[n_pos + match_len] == haystack[h_pos + match_len])
181 match_len++;
182 if (match_len <= 8)
183 continue;
184
185 /* If a sufficiently large match was found, try to expand
186 * the matched segment backwards. */
187 match_h_pos = h_pos;
188 match_n_pos = n_pos;
189 while (match_n_pos > 0 && match_h_pos > 0 &&
190 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
191 match_n_pos--;
192 match_h_pos--;
193 match_len++;
194 }
195 if (match_len <= 14)
196 continue;
197 *match_h_offset_ptr = match_h_pos;
198 *match_n_offset_ptr = match_n_pos;
199 *match_len_ptr = match_len;
200 return 0;
201 }
202 return -1;
203}
204
205/**
206 * Look for segments in samples in the sample queue matching the data
207 * in ptr. Samples not matching are removed from the queue. If a match
208 * is found, the next time it will look for matches starting from the
209 * end of the previous matched segment.
210 *
211 * @param data data to find matches for in the sample queue
212 * @param len length of the data buffer
213 * @param queue samples used for looking for matching segments
214 * @param pos the offset in data of the matched segment
215 * @param match_sample the number of the sample that contained the match
216 * @param match_offset the offset of the matched segment within the sample
217 * @param match_len the length of the matched segment
218 * @return 0 if a match was found, < 0 if no match was found
219 */
220static int find_sample_match(const uint8_t *data, int len,
221 HintSampleQueue *queue, int *pos,
222 int *match_sample, int *match_offset,
223 int *match_len)
224{
225 while (queue->len > 0) {
226 HintSample *sample = &queue->samples[0];
227 /* If looking for matches in a new sample, skip the first 5 bytes,
228 * since they often may be modified/removed in the output packet. */
229 if (sample->offset == 0 && sample->size > 5)
230 sample->offset = 5;
231
232 if (match_segments(data, len, sample->data, sample->offset,
233 sample->size, pos, match_offset, match_len) == 0) {
234 *match_sample = sample->sample_number;
235 /* Next time, look for matches at this offset, with a little
236 * margin to this match. */
237 sample->offset = *match_offset + *match_len + 5;
238 if (sample->offset + 10 >= sample->size)
239 sample_queue_pop(queue); /* Not enough useful data left */
240 return 0;
241 }
242
243 if (sample->offset < 10 && sample->size > 20) {
244 /* No match found from the start of the sample,
245 * try from the middle of the sample instead. */
246 sample->offset = sample->size/2;
247 } else {
248 /* No match for this sample, remove it */
249 sample_queue_pop(queue);
250 }
251 }
252 return -1;
253}
254
255static void output_immediate(const uint8_t *data, int size,
256 AVIOContext *out, int *entries)
257{
258 while (size > 0) {
259 int len = size;
260 if (len > 14)
261 len = 14;
262 avio_w8(out, 1); /* immediate constructor */
263 avio_w8(out, len); /* amount of valid data */
265 data += len;
266 size -= len;
267
268 ffio_fill(out, 0, 14 - len);
269
270 (*entries)++;
271 }
272}
273
274static void output_match(AVIOContext *out, int match_sample,
275 int match_offset, int match_len, int *entries)
276{
277 avio_w8(out, 2); /* sample constructor */
278 avio_w8(out, 0); /* track reference */
279 avio_wb16(out, match_len);
280 avio_wb32(out, match_sample);
281 avio_wb32(out, match_offset);
282 avio_wb16(out, 1); /* bytes per block */
283 avio_wb16(out, 1); /* samples per block */
284 (*entries)++;
285}
286
287static void describe_payload(const uint8_t *data, int size,
288 AVIOContext *out, int *entries,
289 HintSampleQueue *queue)
290{
291 /* Describe the payload using different constructors */
292 while (size > 0) {
293 int match_sample, match_offset, match_len, pos;
294 if (find_sample_match(data, size, queue, &pos, &match_sample,
295 &match_offset, &match_len) < 0)
296 break;
297 output_immediate(data, pos, out, entries);
298 data += pos;
299 size -= pos;
300 output_match(out, match_sample, match_offset, match_len, entries);
301 data += match_len;
302 size -= match_len;
303 }
304 output_immediate(data, size, out, entries);
305}
306
307/**
308 * Write an RTP hint (that may contain one or more RTP packets)
309 * for the packets in data. data contains one or more packets with a
310 * BE32 size header.
311 *
312 * @param out buffer where the hints are written
313 * @param data buffer containing RTP packets
314 * @param size the size of the data buffer
315 * @param trk the MOVTrack for the hint track
316 * @param dts pointer where the timestamp for the written RTP hint is stored
317 * @return the number of RTP packets in the written hint
318 */
319static int write_hint_packets(AVIOContext *out, const uint8_t *data,
320 int size, MOVTrack *trk, int64_t *dts)
321{
322 int64_t curpos;
323 int64_t count_pos, entries_pos;
324 int count = 0, entries;
325
326 count_pos = avio_tell(out);
327 /* RTPsample header */
328 avio_wb16(out, 0); /* packet count */
329 avio_wb16(out, 0); /* reserved */
330
331 while (size > 4) {
332 uint32_t packet_len = AV_RB32(data);
333 uint16_t seq;
334 uint32_t ts;
335 int32_t ts_diff;
336
337 data += 4;
338 size -= 4;
339 if (packet_len > size || packet_len <= 12)
340 break;
341 if (RTP_PT_IS_RTCP(data[1])) {
342 /* RTCP packet, just skip */
343 data += packet_len;
344 size -= packet_len;
345 continue;
346 }
347
348 if (packet_len > trk->max_packet_size)
349 trk->max_packet_size = packet_len;
350
351 seq = AV_RB16(&data[2]);
352 ts = AV_RB32(&data[4]);
353
354 if (trk->prev_rtp_ts == 0)
355 trk->prev_rtp_ts = ts;
356 /* Unwrap the 32-bit RTP timestamp that wraps around often
357 * into a not (as often) wrapping 64-bit timestamp. */
358 ts_diff = ts - trk->prev_rtp_ts;
359 if (ts_diff > 0) {
360 trk->cur_rtp_ts_unwrapped += ts_diff;
361 trk->prev_rtp_ts = ts;
362 ts_diff = 0;
363 }
364 if (*dts == AV_NOPTS_VALUE)
365 *dts = trk->cur_rtp_ts_unwrapped;
366
367 count++;
368 /* RTPpacket header */
369 avio_wb32(out, 0); /* relative_time */
370 avio_write(out, data, 2); /* RTP header */
371 avio_wb16(out, seq); /* RTPsequenceseed */
372 avio_wb16(out, ts_diff ? 4 : 0); /* reserved + flags (extra_flag) */
373 entries_pos = avio_tell(out);
374 avio_wb16(out, 0); /* entry count */
375 if (ts_diff) { /* if extra_flag is set */
376 avio_wb32(out, 16); /* extra_information_length */
377 avio_wb32(out, 12); /* rtpoffsetTLV box */
378 avio_write(out, "rtpo", 4);
379 avio_wb32(out, ts_diff);
380 }
381
382 data += 12;
383 size -= 12;
384 packet_len -= 12;
385
386 entries = 0;
387 /* Write one or more constructors describing the payload data */
388 describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
389 data += packet_len;
390 size -= packet_len;
391
392 curpos = avio_tell(out);
393 avio_seek(out, entries_pos, SEEK_SET);
394 avio_wb16(out, entries);
395 avio_seek(out, curpos, SEEK_SET);
396 }
397
398 curpos = avio_tell(out);
399 avio_seek(out, count_pos, SEEK_SET);
400 avio_wb16(out, count);
401 avio_seek(out, curpos, SEEK_SET);
402 return count;
403}
404
406 int track_index, int sample,
407 uint8_t *sample_data, int sample_size)
408{
409 MOVMuxContext *mov = s->priv_data;
410 MOVTrack *trk = &mov->tracks[track_index];
411 AVFormatContext *rtp_ctx = trk->rtp_ctx;
412 uint8_t *buf = NULL;
413 int size;
414 AVIOContext *hintbuf = NULL;
415 AVPacket *hint_pkt = mov->pkt;
416 int ret = 0, count;
417
418 if (!rtp_ctx)
419 return AVERROR(ENOENT);
420 if (!rtp_ctx->pb)
421 return AVERROR(ENOMEM);
422
423 if (sample_data)
424 sample_queue_push(&trk->sample_queue, sample_data, sample_size, sample);
425 else
426 sample_queue_push(&trk->sample_queue, pkt->data, pkt->size, sample);
427
428 /* Feed the packet to the RTP muxer */
429 ff_write_chained(rtp_ctx, 0, pkt, s, 0);
430
431 /* Fetch the output from the RTP muxer, open a new output buffer
432 * for next time. */
433 size = avio_close_dyn_buf(rtp_ctx->pb, &buf);
434 if ((ret = ffio_open_dyn_packet_buf(&rtp_ctx->pb,
436 goto done;
437
438 if (size <= 0)
439 goto done;
440
441 /* Open a buffer for writing the hint */
442 if ((ret = avio_open_dyn_buf(&hintbuf)) < 0)
443 goto done;
444 av_packet_unref(hint_pkt);
445 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt->dts);
446 av_freep(&buf);
447
448 /* Write the hint data into the hint track */
449 hint_pkt->size = size = avio_close_dyn_buf(hintbuf, &buf);
450 hint_pkt->data = buf;
451 hint_pkt->pts = hint_pkt->dts;
452 hint_pkt->stream_index = track_index;
453 if (pkt->flags & AV_PKT_FLAG_KEY)
454 hint_pkt->flags |= AV_PKT_FLAG_KEY;
455 if (count > 0)
456 ff_mov_write_packet(s, hint_pkt);
457done:
458 av_free(buf);
459 av_packet_unref(hint_pkt);
461 return ret;
462}
463
465{
466 AVFormatContext *rtp_ctx = track->rtp_ctx;
467
470 if (!rtp_ctx)
471 return;
472 if (rtp_ctx->pb) {
473 av_write_trailer(rtp_ctx);
474 ffio_free_dyn_buf(&rtp_ctx->pb);
475 }
476 avformat_free_context(rtp_ctx);
477}
static FILE * out
int32_t
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1410
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1438
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of 'max_packet_size'.
Definition aviobuf.c:1370
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
AVCodecParameters * avcodec_parameters_alloc(void)
Definition codec_par.c:57
void avcodec_parameters_free(AVCodecParameters **ppar)
Definition codec_par.c:67
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
#define sample
#define fail
Definition test.h:479
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:150
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1238
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int index
Definition gxfenc.c:90
#define AV_RB32(p)
#define AV_RB16(p)
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition mux.c:1337
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7007
#define RTP_MAX_PACKET_SIZE
Definition movenc.h:36
int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, int track_index, int sample, uint8_t *sample_data, int sample_size)
Definition movenchint.c:405
static int find_sample_match(const uint8_t *data, int len, HintSampleQueue *queue, int *pos, int *match_sample, int *match_offset, int *match_len)
Look for segments in samples in the sample queue matching the data in ptr.
Definition movenchint.c:220
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition movenchint.c:30
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)
Find matches of needle[n_pos ->] within haystack.
Definition movenchint.c:168
static void sample_queue_free(HintSampleQueue *queue)
Empty the sample queue, releasing all memory.
Definition movenchint.c:88
void ff_mov_close_hinting(MOVTrack *track)
Definition movenchint.c:464
static void sample_queue_push(HintSampleQueue *queue, const uint8_t *data, int size, int sample)
Add a reference to the sample data to the sample queue.
Definition movenchint.c:104
static void output_match(AVIOContext *out, int match_sample, int match_offset, int match_len, int *entries)
Definition movenchint.c:274
static void output_immediate(const uint8_t *data, int size, AVIOContext *out, int *entries)
Definition movenchint.c:255
static void sample_queue_pop(HintSampleQueue *queue)
Remove the first sample from the sample queue.
Definition movenchint.c:75
static void describe_payload(const uint8_t *data, int size, AVIOContext *out, int *entries, HintSampleQueue *queue)
Definition movenchint.c:287
static void sample_queue_retain(HintSampleQueue *queue)
Make local copies of all referenced sample data in the queue.
Definition movenchint.c:130
static int write_hint_packets(AVIOContext *out, const uint8_t *data, int size, MOVTrack *trk, int64_t *dts)
Write an RTP hint (that may contain one or more RTP packets) for the packets in data.
Definition movenchint.c:319
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
#define RTP_PT_IS_RTCP(x)
Definition rtp.h:112
int ff_rtp_chain_mux_open(AVFormatContext **out, AVFormatContext *s, AVStream *st, URLContext *handle, int packet_size, int idx)
unsigned int pos
Definition spdifenc.c:431
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
int stream_index
Definition packet.h:605
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
HintSample * samples
Definition movenc.h:77
int sample_number
Definition movenc.h:69
int own_data
Definition movenc.h:71
int size
Definition movenc.h:68
const uint8_t * data
Definition movenc.h:67
int offset
Definition movenc.h:70
AVPacket * pkt
Definition movenc.h:249
MOVTrack * tracks
Definition movenc.h:221
uint32_t prev_rtp_ts
Definition movenc.h:148
int * src_track
the tracks that this hint (or tmcd) track describes
Definition movenc.h:146
int nb_src_track
Definition movenc.h:145
int tag
stsd fourcc
Definition movenc.h:124
int64_t cur_rtp_ts_unwrapped
Definition movenc.h:149
AVCodecParameters * par
Definition movenc.h:126
uint32_t max_packet_size
Definition movenc.h:150
int hint_track
the track that hints this track, -1 if no hint track is set
Definition movenc.h:144
unsigned timescale
Definition movenc.h:98
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition movenc.h:147
HintSampleQueue sample_queue
Definition movenc.h:156
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
int size
static int track_index(VividasDemuxContext *viv, AVFormatContext *s, const uint8_t *buf, unsigned size)
Definition vividas.c:438
int len