Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenchint.c @ 42f97696

History | View | Annotate | Download (15.7 KB)

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"
23
#include "libavutil/intreadwrite.h"
24
#include "internal.h"
25

    
26
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
27
{
28
    MOVMuxContext *mov  = s->priv_data;
29
    MOVTrack *track     = &mov->tracks[index];
30
    MOVTrack *src_track = &mov->tracks[src_index];
31
    AVStream *src_st    = s->streams[src_index];
32
    int ret = AVERROR(ENOMEM);
33
    AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
34

    
35
    track->tag = MKTAG('r','t','p',' ');
36
    track->src_track = src_index;
37

    
38
    if (!rtp_format) {
39
        ret = AVERROR(ENOENT);
40
        goto fail;
41
    }
42

    
43
    track->enc = avcodec_alloc_context();
44
    if (!track->enc)
45
        goto fail;
46
    track->enc->codec_type = AVMEDIA_TYPE_DATA;
47
    track->enc->codec_tag  = track->tag;
48

    
49
    track->rtp_ctx = avformat_alloc_context();
50
    if (!track->rtp_ctx)
51
        goto fail;
52
    track->rtp_ctx->oformat = rtp_format;
53
    if (!av_new_stream(track->rtp_ctx, 0))
54
        goto fail;
55

    
56
    /* Copy stream parameters */
57
    track->rtp_ctx->streams[0]->sample_aspect_ratio =
58
                        src_st->sample_aspect_ratio;
59

    
60
    avcodec_copy_context(track->rtp_ctx->streams[0]->codec, src_st->codec);
61

    
62
    if ((ret = url_open_dyn_packet_buf(&track->rtp_ctx->pb,
63
                                       RTP_MAX_PACKET_SIZE)) < 0)
64
        goto fail;
65
    ret = av_write_header(track->rtp_ctx);
66
    if (ret)
67
        goto fail;
68

    
69
    /* Copy the RTP AVStream timebase back to the hint AVStream */
70
    track->timescale = track->rtp_ctx->streams[0]->time_base.den;
71

    
72
    /* Mark the hinted track that packets written to it should be
73
     * sent to this track for hinting. */
74
    src_track->hint_track = index;
75
    return 0;
76
fail:
77
    av_log(s, AV_LOG_WARNING,
78
           "Unable to initialize hinting of stream %d\n", src_index);
79
    if (track->rtp_ctx && track->rtp_ctx->pb) {
80
        uint8_t *buf;
81
        url_close_dyn_buf(track->rtp_ctx->pb, &buf);
82
        av_free(buf);
83
    }
84
    if (track->rtp_ctx) {
85
        avformat_free_context(track->rtp_ctx);
86
        track->rtp_ctx = NULL;
87
    }
88
    av_freep(&track->enc);
89
    /* Set a default timescale, to avoid crashes in dump_format */
90
    track->timescale = 90000;
91
    return ret;
92
}
93

    
94
/**
95
 * Remove the first sample from the sample queue.
96
 */
97
static void sample_queue_pop(HintSampleQueue *queue)
98
{
99
    if (queue->len <= 0)
100
        return;
101
    if (queue->samples[0].own_data)
102
        av_free(queue->samples[0].data);
103
    queue->len--;
104
    memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
105
}
106

    
107
/**
108
 * Empty the sample queue, releasing all memory.
109
 */
110
static void sample_queue_free(HintSampleQueue *queue)
111
{
112
    int i;
113
    for (i = 0; i < queue->len; i++)
114
        if (queue->samples[i].own_data)
115
            av_free(queue->samples[i].data);
116
    av_freep(&queue->samples);
117
    queue->len = 0;
118
    queue->size = 0;
119
}
120

    
121
/**
122
 * Add a reference to the sample data to the sample queue. The data is
123
 * not copied. sample_queue_retain should be called before pkt->data
124
 * is reused/freed.
125
 */
126
static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample)
127
{
128
    /* No need to keep track of smaller samples, since describing them
129
     * with immediates is more efficient. */
130
    if (pkt->size <= 14)
131
        return;
132
    if (!queue->samples || queue->len >= queue->size) {
133
        HintSample* samples;
134
        queue->size += 10;
135
        samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size);
136
        if (!samples)
137
            return;
138
        queue->samples = samples;
139
    }
140
    queue->samples[queue->len].data = pkt->data;
141
    queue->samples[queue->len].size = pkt->size;
142
    queue->samples[queue->len].sample_number = sample;
143
    queue->samples[queue->len].offset = 0;
144
    queue->samples[queue->len].own_data = 0;
145
    queue->len++;
146
}
147

    
148
/**
149
 * Make local copies of all referenced sample data in the queue.
150
 */
151
static void sample_queue_retain(HintSampleQueue *queue)
152
{
153
    int i;
154
    for (i = 0; i < queue->len; ) {
155
        HintSample *sample = &queue->samples[i];
156
        if (!sample->own_data) {
157
            uint8_t* ptr = av_malloc(sample->size);
158
            if (!ptr) {
159
                /* Unable to allocate memory for this one, remove it */
160
                memmove(queue->samples + i, queue->samples + i + 1,
161
                        sizeof(HintSample)*(queue->len - i - 1));
162
                queue->len--;
163
                continue;
164
            }
165
            memcpy(ptr, sample->data, sample->size);
166
            sample->data = ptr;
167
            sample->own_data = 1;
168
        }
169
        i++;
170
    }
171
}
172

    
173
/**
174
 * Find matches of needle[n_pos ->] within haystack. If a sufficiently
175
 * large match is found, matching bytes before n_pos are included
176
 * in the match, too (within the limits of the arrays).
177
 *
178
 * @param haystack buffer that may contain parts of needle
179
 * @param h_len length of the haystack buffer
180
 * @param needle buffer containing source data that have been used to
181
 *               construct haystack
182
 * @param n_pos start position in needle used for looking for matches
183
 * @param n_len length of the needle buffer
184
 * @param match_h_offset_ptr offset of the first matching byte within haystack
185
 * @param match_n_offset_ptr offset of the first matching byte within needle
186
 * @param match_len_ptr length of the matched segment
187
 * @return 0 if a match was found, < 0 if no match was found
188
 */
189
static int match_segments(const uint8_t *haystack, int h_len,
190
                          const uint8_t *needle, int n_pos, int n_len,
191
                          int *match_h_offset_ptr, int *match_n_offset_ptr,
192
                          int *match_len_ptr)
193
{
194
    int h_pos;
195
    for (h_pos = 0; h_pos < h_len; h_pos++) {
196
        int match_len = 0;
197
        int match_h_pos, match_n_pos;
198

    
199
        /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
200
        while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
201
               needle[n_pos + match_len] == haystack[h_pos + match_len])
202
            match_len++;
203
        if (match_len <= 8)
204
            continue;
205

    
206
        /* If a sufficiently large match was found, try to expand
207
         * the matched segment backwards. */
208
        match_h_pos = h_pos;
209
        match_n_pos = n_pos;
210
        while (match_n_pos > 0 && match_h_pos > 0 &&
211
               needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
212
            match_n_pos--;
213
            match_h_pos--;
214
            match_len++;
215
        }
216
        if (match_len <= 14)
217
            continue;
218
        *match_h_offset_ptr = match_h_pos;
219
        *match_n_offset_ptr = match_n_pos;
220
        *match_len_ptr = match_len;
221
        return 0;
222
    }
223
    return -1;
224
}
225

    
226
/**
227
 * Look for segments in samples in the sample queue matching the data
228
 * in ptr. Samples not matching are removed from the queue. If a match
229
 * is found, the next time it will look for matches starting from the
230
 * end of the previous matched segment.
231
 *
232
 * @param data data to find matches for in the sample queue
233
 * @param len length of the data buffer
234
 * @param queue samples used for looking for matching segments
235
 * @param pos the offset in data of the matched segment
236
 * @param match_sample the number of the sample that contained the match
237
 * @param match_offset the offset of the matched segment within the sample
238
 * @param match_len the length of the matched segment
239
 * @return 0 if a match was found, < 0 if no match was found
240
 */
241
static int find_sample_match(const uint8_t *data, int len,
242
                             HintSampleQueue *queue, int *pos,
243
                             int *match_sample, int *match_offset,
244
                             int *match_len)
245
{
246
    while (queue->len > 0) {
247
        HintSample *sample = &queue->samples[0];
248
        /* If looking for matches in a new sample, skip the first 5 bytes,
249
         * since they often may be modified/removed in the output packet. */
250
        if (sample->offset == 0 && sample->size > 5)
251
            sample->offset = 5;
252

    
253
        if (match_segments(data, len, sample->data, sample->offset,
254
                           sample->size, pos, match_offset, match_len) == 0) {
255
            *match_sample = sample->sample_number;
256
            /* Next time, look for matches at this offset, with a little
257
             * margin to this match. */
258
            sample->offset = *match_offset + *match_len + 5;
259
            if (sample->offset + 10 >= sample->size)
260
                sample_queue_pop(queue); /* Not enough useful data left */
261
            return 0;
262
        }
263

    
264
        if (sample->offset < 10 && sample->size > 20) {
265
            /* No match found from the start of the sample,
266
             * try from the middle of the sample instead. */
267
            sample->offset = sample->size/2;
268
        } else {
269
            /* No match for this sample, remove it */
270
            sample_queue_pop(queue);
271
        }
272
    }
273
    return -1;
274
}
275

    
276
static void output_immediate(const uint8_t *data, int size,
277
                             ByteIOContext *out, int *entries)
278
{
279
    while (size > 0) {
280
        int len = size;
281
        if (len > 14)
282
            len = 14;
283
        put_byte(out, 1); /* immediate constructor */
284
        put_byte(out, len); /* amount of valid data */
285
        put_buffer(out, data, len);
286
        data += len;
287
        size -= len;
288

    
289
        for (; len < 14; len++)
290
            put_byte(out, 0);
291

    
292
        (*entries)++;
293
    }
294
}
295

    
296
static void output_match(ByteIOContext *out, int match_sample,
297
                         int match_offset, int match_len, int *entries)
298
{
299
    put_byte(out, 2); /* sample constructor */
300
    put_byte(out, 0); /* track reference */
301
    put_be16(out, match_len);
302
    put_be32(out, match_sample);
303
    put_be32(out, match_offset);
304
    put_be16(out, 1); /* bytes per block */
305
    put_be16(out, 1); /* samples per block */
306
    (*entries)++;
307
}
308

    
309
static void describe_payload(const uint8_t *data, int size,
310
                             ByteIOContext *out, int *entries,
311
                             HintSampleQueue *queue)
312
{
313
    /* Describe the payload using different constructors */
314
    while (size > 0) {
315
        int match_sample, match_offset, match_len, pos;
316
        if (find_sample_match(data, size, queue, &pos, &match_sample,
317
                              &match_offset, &match_len) < 0)
318
            break;
319
        output_immediate(data, pos, out, entries);
320
        data += pos;
321
        size -= pos;
322
        output_match(out, match_sample, match_offset, match_len, entries);
323
        data += match_len;
324
        size -= match_len;
325
    }
326
    output_immediate(data, size, out, entries);
327
}
328

    
329
/**
330
 * Write an RTP hint (that may contain one or more RTP packets)
331
 * for the packets in data. data contains one or more packets with a
332
 * BE32 size header.
333
 *
334
 * @param out buffer where the hints are written
335
 * @param data buffer containing RTP packets
336
 * @param size the size of the data buffer
337
 * @param trk the MOVTrack for the hint track
338
 * @param pts pointer where the timestamp for the written RTP hint is stored
339
 * @return the number of RTP packets in the written hint
340
 */
341
static int write_hint_packets(ByteIOContext *out, const uint8_t *data,
342
                              int size, MOVTrack *trk, int64_t *pts)
343
{
344
    int64_t curpos;
345
    int64_t count_pos, entries_pos;
346
    int count = 0, entries;
347

    
348
    count_pos = url_ftell(out);
349
    /* RTPsample header */
350
    put_be16(out, 0); /* packet count */
351
    put_be16(out, 0); /* reserved */
352

    
353
    while (size > 4) {
354
        uint32_t packet_len = AV_RB32(data);
355
        uint16_t seq;
356
        uint32_t ts;
357

    
358
        data += 4;
359
        size -= 4;
360
        if (packet_len > size || packet_len <= 12)
361
            break;
362
        if (data[1] >= 200 && data[1] <= 204) {
363
            /* RTCP packet, just skip */
364
            data += packet_len;
365
            size -= packet_len;
366
            continue;
367
        }
368

    
369
        if (packet_len > trk->max_packet_size)
370
            trk->max_packet_size = packet_len;
371

    
372
        seq = AV_RB16(&data[2]);
373
        ts = AV_RB32(&data[4]);
374

    
375
        if (trk->prev_rtp_ts == 0)
376
            trk->prev_rtp_ts = ts;
377
        /* Unwrap the 32-bit RTP timestamp that wraps around often
378
         * into a not (as often) wrapping 64-bit timestamp. */
379
        trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts);
380
        trk->prev_rtp_ts = ts;
381
        if (*pts == AV_NOPTS_VALUE)
382
            *pts = trk->cur_rtp_ts_unwrapped;
383

    
384
        count++;
385
        /* RTPpacket header */
386
        put_be32(out, 0); /* relative_time */
387
        put_buffer(out, data, 2); /* RTP header */
388
        put_be16(out, seq); /* RTPsequenceseed */
389
        put_be16(out, 0); /* reserved + flags */
390
        entries_pos = url_ftell(out);
391
        put_be16(out, 0); /* entry count */
392

    
393
        data += 12;
394
        size -= 12;
395
        packet_len -= 12;
396

    
397
        entries = 0;
398
        /* Write one or more constructors describing the payload data */
399
        describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
400
        data += packet_len;
401
        size -= packet_len;
402

    
403
        curpos = url_ftell(out);
404
        url_fseek(out, entries_pos, SEEK_SET);
405
        put_be16(out, entries);
406
        url_fseek(out, curpos, SEEK_SET);
407
    }
408

    
409
    curpos = url_ftell(out);
410
    url_fseek(out, count_pos, SEEK_SET);
411
    put_be16(out, count);
412
    url_fseek(out, curpos, SEEK_SET);
413
    return count;
414
}
415

    
416
int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
417
                             int track_index, int sample)
418
{
419
    MOVMuxContext *mov = s->priv_data;
420
    MOVTrack *trk = &mov->tracks[track_index];
421
    AVFormatContext *rtp_ctx = trk->rtp_ctx;
422
    uint8_t *buf = NULL;
423
    int size;
424
    ByteIOContext *hintbuf = NULL;
425
    AVPacket hint_pkt;
426
    int ret = 0, count;
427

    
428
    if (!rtp_ctx)
429
        return AVERROR(ENOENT);
430
    if (!rtp_ctx->pb)
431
        return AVERROR(ENOMEM);
432

    
433
    sample_queue_push(&trk->sample_queue, pkt, sample);
434

    
435
    /* Feed the packet to the RTP muxer */
436
    ff_write_chained(rtp_ctx, 0, pkt, s);
437

    
438
    /* Fetch the output from the RTP muxer, open a new output buffer
439
     * for next time. */
440
    size = url_close_dyn_buf(rtp_ctx->pb, &buf);
441
    if ((ret = url_open_dyn_packet_buf(&rtp_ctx->pb,
442
                                       RTP_MAX_PACKET_SIZE)) < 0)
443
        goto done;
444

    
445
    if (size <= 0)
446
        goto done;
447

    
448
    /* Open a buffer for writing the hint */
449
    if ((ret = url_open_dyn_buf(&hintbuf)) < 0)
450
        goto done;
451
    av_init_packet(&hint_pkt);
452
    count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts);
453
    av_freep(&buf);
454

    
455
    /* Write the hint data into the hint track */
456
    hint_pkt.size = size = url_close_dyn_buf(hintbuf, &buf);
457
    hint_pkt.data = buf;
458
    hint_pkt.pts  = hint_pkt.dts;
459
    hint_pkt.stream_index = track_index;
460
    if (pkt->flags & AV_PKT_FLAG_KEY)
461
        hint_pkt.flags |= AV_PKT_FLAG_KEY;
462
    if (count > 0)
463
        ff_mov_write_packet(s, &hint_pkt);
464
done:
465
    av_free(buf);
466
    sample_queue_retain(&trk->sample_queue);
467
    return ret;
468
}
469

    
470
void ff_mov_close_hinting(MOVTrack *track) {
471
    AVFormatContext* rtp_ctx = track->rtp_ctx;
472
    uint8_t *ptr;
473

    
474
    av_freep(&track->enc);
475
    sample_queue_free(&track->sample_queue);
476
    if (!rtp_ctx)
477
        return;
478
    if (rtp_ctx->pb) {
479
        av_write_trailer(rtp_ctx);
480
        url_close_dyn_buf(rtp_ctx->pb, &ptr);
481
        av_free(ptr);
482
    }
483
    avformat_free_context(rtp_ctx);
484
}
485