annotate ffmpeg/libavformat/movenchint.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * MOV, 3GP, MP4 muxer RTP hinting
yading@11 3 * Copyright (c) 2010 Martin Storsjo
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "movenc.h"
yading@11 23 #include "libavutil/intreadwrite.h"
yading@11 24 #include "internal.h"
yading@11 25 #include "rtpenc_chain.h"
yading@11 26 #include "avio_internal.h"
yading@11 27 #include "rtp.h"
yading@11 28
yading@11 29 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
yading@11 30 {
yading@11 31 MOVMuxContext *mov = s->priv_data;
yading@11 32 MOVTrack *track = &mov->tracks[index];
yading@11 33 MOVTrack *src_track = &mov->tracks[src_index];
yading@11 34 AVStream *src_st = s->streams[src_index];
yading@11 35 int ret = AVERROR(ENOMEM);
yading@11 36
yading@11 37 track->tag = MKTAG('r','t','p',' ');
yading@11 38 track->src_track = src_index;
yading@11 39
yading@11 40 track->enc = avcodec_alloc_context3(NULL);
yading@11 41 if (!track->enc)
yading@11 42 goto fail;
yading@11 43 track->enc->codec_type = AVMEDIA_TYPE_DATA;
yading@11 44 track->enc->codec_tag = track->tag;
yading@11 45
yading@11 46 ret = ff_rtp_chain_mux_open(&track->rtp_ctx, s, src_st, NULL,
yading@11 47 RTP_MAX_PACKET_SIZE, src_index);
yading@11 48 if (ret < 0)
yading@11 49 goto fail;
yading@11 50
yading@11 51 /* Copy the RTP AVStream timebase back to the hint AVStream */
yading@11 52 track->timescale = track->rtp_ctx->streams[0]->time_base.den;
yading@11 53
yading@11 54 /* Mark the hinted track that packets written to it should be
yading@11 55 * sent to this track for hinting. */
yading@11 56 src_track->hint_track = index;
yading@11 57 return 0;
yading@11 58 fail:
yading@11 59 av_log(s, AV_LOG_WARNING,
yading@11 60 "Unable to initialize hinting of stream %d\n", src_index);
yading@11 61 av_freep(&track->enc);
yading@11 62 /* Set a default timescale, to avoid crashes in av_dump_format */
yading@11 63 track->timescale = 90000;
yading@11 64 return ret;
yading@11 65 }
yading@11 66
yading@11 67 /**
yading@11 68 * Remove the first sample from the sample queue.
yading@11 69 */
yading@11 70 static void sample_queue_pop(HintSampleQueue *queue)
yading@11 71 {
yading@11 72 if (queue->len <= 0)
yading@11 73 return;
yading@11 74 if (queue->samples[0].own_data)
yading@11 75 av_free(queue->samples[0].data);
yading@11 76 queue->len--;
yading@11 77 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
yading@11 78 }
yading@11 79
yading@11 80 /**
yading@11 81 * Empty the sample queue, releasing all memory.
yading@11 82 */
yading@11 83 static void sample_queue_free(HintSampleQueue *queue)
yading@11 84 {
yading@11 85 int i;
yading@11 86 for (i = 0; i < queue->len; i++)
yading@11 87 if (queue->samples[i].own_data)
yading@11 88 av_free(queue->samples[i].data);
yading@11 89 av_freep(&queue->samples);
yading@11 90 queue->len = 0;
yading@11 91 queue->size = 0;
yading@11 92 }
yading@11 93
yading@11 94 /**
yading@11 95 * Add a reference to the sample data to the sample queue. The data is
yading@11 96 * not copied. sample_queue_retain should be called before pkt->data
yading@11 97 * is reused/freed.
yading@11 98 */
yading@11 99 static void sample_queue_push(HintSampleQueue *queue, uint8_t *data, int size,
yading@11 100 int sample)
yading@11 101 {
yading@11 102 /* No need to keep track of smaller samples, since describing them
yading@11 103 * with immediates is more efficient. */
yading@11 104 if (size <= 14)
yading@11 105 return;
yading@11 106 if (!queue->samples || queue->len >= queue->size) {
yading@11 107 HintSample* samples;
yading@11 108 queue->size += 10;
yading@11 109 samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size);
yading@11 110 if (!samples)
yading@11 111 return;
yading@11 112 queue->samples = samples;
yading@11 113 }
yading@11 114 queue->samples[queue->len].data = data;
yading@11 115 queue->samples[queue->len].size = size;
yading@11 116 queue->samples[queue->len].sample_number = sample;
yading@11 117 queue->samples[queue->len].offset = 0;
yading@11 118 queue->samples[queue->len].own_data = 0;
yading@11 119 queue->len++;
yading@11 120 }
yading@11 121
yading@11 122 /**
yading@11 123 * Make local copies of all referenced sample data in the queue.
yading@11 124 */
yading@11 125 static void sample_queue_retain(HintSampleQueue *queue)
yading@11 126 {
yading@11 127 int i;
yading@11 128 for (i = 0; i < queue->len; ) {
yading@11 129 HintSample *sample = &queue->samples[i];
yading@11 130 if (!sample->own_data) {
yading@11 131 uint8_t* ptr = av_malloc(sample->size);
yading@11 132 if (!ptr) {
yading@11 133 /* Unable to allocate memory for this one, remove it */
yading@11 134 memmove(queue->samples + i, queue->samples + i + 1,
yading@11 135 sizeof(HintSample)*(queue->len - i - 1));
yading@11 136 queue->len--;
yading@11 137 continue;
yading@11 138 }
yading@11 139 memcpy(ptr, sample->data, sample->size);
yading@11 140 sample->data = ptr;
yading@11 141 sample->own_data = 1;
yading@11 142 }
yading@11 143 i++;
yading@11 144 }
yading@11 145 }
yading@11 146
yading@11 147 /**
yading@11 148 * Find matches of needle[n_pos ->] within haystack. If a sufficiently
yading@11 149 * large match is found, matching bytes before n_pos are included
yading@11 150 * in the match, too (within the limits of the arrays).
yading@11 151 *
yading@11 152 * @param haystack buffer that may contain parts of needle
yading@11 153 * @param h_len length of the haystack buffer
yading@11 154 * @param needle buffer containing source data that have been used to
yading@11 155 * construct haystack
yading@11 156 * @param n_pos start position in needle used for looking for matches
yading@11 157 * @param n_len length of the needle buffer
yading@11 158 * @param match_h_offset_ptr offset of the first matching byte within haystack
yading@11 159 * @param match_n_offset_ptr offset of the first matching byte within needle
yading@11 160 * @param match_len_ptr length of the matched segment
yading@11 161 * @return 0 if a match was found, < 0 if no match was found
yading@11 162 */
yading@11 163 static int match_segments(const uint8_t *haystack, int h_len,
yading@11 164 const uint8_t *needle, int n_pos, int n_len,
yading@11 165 int *match_h_offset_ptr, int *match_n_offset_ptr,
yading@11 166 int *match_len_ptr)
yading@11 167 {
yading@11 168 int h_pos;
yading@11 169 for (h_pos = 0; h_pos < h_len; h_pos++) {
yading@11 170 int match_len = 0;
yading@11 171 int match_h_pos, match_n_pos;
yading@11 172
yading@11 173 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */
yading@11 174 while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
yading@11 175 needle[n_pos + match_len] == haystack[h_pos + match_len])
yading@11 176 match_len++;
yading@11 177 if (match_len <= 8)
yading@11 178 continue;
yading@11 179
yading@11 180 /* If a sufficiently large match was found, try to expand
yading@11 181 * the matched segment backwards. */
yading@11 182 match_h_pos = h_pos;
yading@11 183 match_n_pos = n_pos;
yading@11 184 while (match_n_pos > 0 && match_h_pos > 0 &&
yading@11 185 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
yading@11 186 match_n_pos--;
yading@11 187 match_h_pos--;
yading@11 188 match_len++;
yading@11 189 }
yading@11 190 if (match_len <= 14)
yading@11 191 continue;
yading@11 192 *match_h_offset_ptr = match_h_pos;
yading@11 193 *match_n_offset_ptr = match_n_pos;
yading@11 194 *match_len_ptr = match_len;
yading@11 195 return 0;
yading@11 196 }
yading@11 197 return -1;
yading@11 198 }
yading@11 199
yading@11 200 /**
yading@11 201 * Look for segments in samples in the sample queue matching the data
yading@11 202 * in ptr. Samples not matching are removed from the queue. If a match
yading@11 203 * is found, the next time it will look for matches starting from the
yading@11 204 * end of the previous matched segment.
yading@11 205 *
yading@11 206 * @param data data to find matches for in the sample queue
yading@11 207 * @param len length of the data buffer
yading@11 208 * @param queue samples used for looking for matching segments
yading@11 209 * @param pos the offset in data of the matched segment
yading@11 210 * @param match_sample the number of the sample that contained the match
yading@11 211 * @param match_offset the offset of the matched segment within the sample
yading@11 212 * @param match_len the length of the matched segment
yading@11 213 * @return 0 if a match was found, < 0 if no match was found
yading@11 214 */
yading@11 215 static int find_sample_match(const uint8_t *data, int len,
yading@11 216 HintSampleQueue *queue, int *pos,
yading@11 217 int *match_sample, int *match_offset,
yading@11 218 int *match_len)
yading@11 219 {
yading@11 220 while (queue->len > 0) {
yading@11 221 HintSample *sample = &queue->samples[0];
yading@11 222 /* If looking for matches in a new sample, skip the first 5 bytes,
yading@11 223 * since they often may be modified/removed in the output packet. */
yading@11 224 if (sample->offset == 0 && sample->size > 5)
yading@11 225 sample->offset = 5;
yading@11 226
yading@11 227 if (match_segments(data, len, sample->data, sample->offset,
yading@11 228 sample->size, pos, match_offset, match_len) == 0) {
yading@11 229 *match_sample = sample->sample_number;
yading@11 230 /* Next time, look for matches at this offset, with a little
yading@11 231 * margin to this match. */
yading@11 232 sample->offset = *match_offset + *match_len + 5;
yading@11 233 if (sample->offset + 10 >= sample->size)
yading@11 234 sample_queue_pop(queue); /* Not enough useful data left */
yading@11 235 return 0;
yading@11 236 }
yading@11 237
yading@11 238 if (sample->offset < 10 && sample->size > 20) {
yading@11 239 /* No match found from the start of the sample,
yading@11 240 * try from the middle of the sample instead. */
yading@11 241 sample->offset = sample->size/2;
yading@11 242 } else {
yading@11 243 /* No match for this sample, remove it */
yading@11 244 sample_queue_pop(queue);
yading@11 245 }
yading@11 246 }
yading@11 247 return -1;
yading@11 248 }
yading@11 249
yading@11 250 static void output_immediate(const uint8_t *data, int size,
yading@11 251 AVIOContext *out, int *entries)
yading@11 252 {
yading@11 253 while (size > 0) {
yading@11 254 int len = size;
yading@11 255 if (len > 14)
yading@11 256 len = 14;
yading@11 257 avio_w8(out, 1); /* immediate constructor */
yading@11 258 avio_w8(out, len); /* amount of valid data */
yading@11 259 avio_write(out, data, len);
yading@11 260 data += len;
yading@11 261 size -= len;
yading@11 262
yading@11 263 for (; len < 14; len++)
yading@11 264 avio_w8(out, 0);
yading@11 265
yading@11 266 (*entries)++;
yading@11 267 }
yading@11 268 }
yading@11 269
yading@11 270 static void output_match(AVIOContext *out, int match_sample,
yading@11 271 int match_offset, int match_len, int *entries)
yading@11 272 {
yading@11 273 avio_w8(out, 2); /* sample constructor */
yading@11 274 avio_w8(out, 0); /* track reference */
yading@11 275 avio_wb16(out, match_len);
yading@11 276 avio_wb32(out, match_sample);
yading@11 277 avio_wb32(out, match_offset);
yading@11 278 avio_wb16(out, 1); /* bytes per block */
yading@11 279 avio_wb16(out, 1); /* samples per block */
yading@11 280 (*entries)++;
yading@11 281 }
yading@11 282
yading@11 283 static void describe_payload(const uint8_t *data, int size,
yading@11 284 AVIOContext *out, int *entries,
yading@11 285 HintSampleQueue *queue)
yading@11 286 {
yading@11 287 /* Describe the payload using different constructors */
yading@11 288 while (size > 0) {
yading@11 289 int match_sample, match_offset, match_len, pos;
yading@11 290 if (find_sample_match(data, size, queue, &pos, &match_sample,
yading@11 291 &match_offset, &match_len) < 0)
yading@11 292 break;
yading@11 293 output_immediate(data, pos, out, entries);
yading@11 294 data += pos;
yading@11 295 size -= pos;
yading@11 296 output_match(out, match_sample, match_offset, match_len, entries);
yading@11 297 data += match_len;
yading@11 298 size -= match_len;
yading@11 299 }
yading@11 300 output_immediate(data, size, out, entries);
yading@11 301 }
yading@11 302
yading@11 303 /**
yading@11 304 * Write an RTP hint (that may contain one or more RTP packets)
yading@11 305 * for the packets in data. data contains one or more packets with a
yading@11 306 * BE32 size header.
yading@11 307 *
yading@11 308 * @param out buffer where the hints are written
yading@11 309 * @param data buffer containing RTP packets
yading@11 310 * @param size the size of the data buffer
yading@11 311 * @param trk the MOVTrack for the hint track
yading@11 312 * @param pts pointer where the timestamp for the written RTP hint is stored
yading@11 313 * @return the number of RTP packets in the written hint
yading@11 314 */
yading@11 315 static int write_hint_packets(AVIOContext *out, const uint8_t *data,
yading@11 316 int size, MOVTrack *trk, int64_t *pts)
yading@11 317 {
yading@11 318 int64_t curpos;
yading@11 319 int64_t count_pos, entries_pos;
yading@11 320 int count = 0, entries;
yading@11 321
yading@11 322 count_pos = avio_tell(out);
yading@11 323 /* RTPsample header */
yading@11 324 avio_wb16(out, 0); /* packet count */
yading@11 325 avio_wb16(out, 0); /* reserved */
yading@11 326
yading@11 327 while (size > 4) {
yading@11 328 uint32_t packet_len = AV_RB32(data);
yading@11 329 uint16_t seq;
yading@11 330 uint32_t ts;
yading@11 331
yading@11 332 data += 4;
yading@11 333 size -= 4;
yading@11 334 if (packet_len > size || packet_len <= 12)
yading@11 335 break;
yading@11 336 if (RTP_PT_IS_RTCP(data[1])) {
yading@11 337 /* RTCP packet, just skip */
yading@11 338 data += packet_len;
yading@11 339 size -= packet_len;
yading@11 340 continue;
yading@11 341 }
yading@11 342
yading@11 343 if (packet_len > trk->max_packet_size)
yading@11 344 trk->max_packet_size = packet_len;
yading@11 345
yading@11 346 seq = AV_RB16(&data[2]);
yading@11 347 ts = AV_RB32(&data[4]);
yading@11 348
yading@11 349 if (trk->prev_rtp_ts == 0)
yading@11 350 trk->prev_rtp_ts = ts;
yading@11 351 /* Unwrap the 32-bit RTP timestamp that wraps around often
yading@11 352 * into a not (as often) wrapping 64-bit timestamp. */
yading@11 353 trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts);
yading@11 354 trk->prev_rtp_ts = ts;
yading@11 355 if (*pts == AV_NOPTS_VALUE)
yading@11 356 *pts = trk->cur_rtp_ts_unwrapped;
yading@11 357
yading@11 358 count++;
yading@11 359 /* RTPpacket header */
yading@11 360 avio_wb32(out, 0); /* relative_time */
yading@11 361 avio_write(out, data, 2); /* RTP header */
yading@11 362 avio_wb16(out, seq); /* RTPsequenceseed */
yading@11 363 avio_wb16(out, 0); /* reserved + flags */
yading@11 364 entries_pos = avio_tell(out);
yading@11 365 avio_wb16(out, 0); /* entry count */
yading@11 366
yading@11 367 data += 12;
yading@11 368 size -= 12;
yading@11 369 packet_len -= 12;
yading@11 370
yading@11 371 entries = 0;
yading@11 372 /* Write one or more constructors describing the payload data */
yading@11 373 describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
yading@11 374 data += packet_len;
yading@11 375 size -= packet_len;
yading@11 376
yading@11 377 curpos = avio_tell(out);
yading@11 378 avio_seek(out, entries_pos, SEEK_SET);
yading@11 379 avio_wb16(out, entries);
yading@11 380 avio_seek(out, curpos, SEEK_SET);
yading@11 381 }
yading@11 382
yading@11 383 curpos = avio_tell(out);
yading@11 384 avio_seek(out, count_pos, SEEK_SET);
yading@11 385 avio_wb16(out, count);
yading@11 386 avio_seek(out, curpos, SEEK_SET);
yading@11 387 return count;
yading@11 388 }
yading@11 389
yading@11 390 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
yading@11 391 int track_index, int sample,
yading@11 392 uint8_t *sample_data, int sample_size)
yading@11 393 {
yading@11 394 MOVMuxContext *mov = s->priv_data;
yading@11 395 MOVTrack *trk = &mov->tracks[track_index];
yading@11 396 AVFormatContext *rtp_ctx = trk->rtp_ctx;
yading@11 397 uint8_t *buf = NULL;
yading@11 398 int size;
yading@11 399 AVIOContext *hintbuf = NULL;
yading@11 400 AVPacket hint_pkt;
yading@11 401 int ret = 0, count;
yading@11 402
yading@11 403 if (!rtp_ctx)
yading@11 404 return AVERROR(ENOENT);
yading@11 405 if (!rtp_ctx->pb)
yading@11 406 return AVERROR(ENOMEM);
yading@11 407
yading@11 408 if (sample_data)
yading@11 409 sample_queue_push(&trk->sample_queue, sample_data, sample_size, sample);
yading@11 410 else
yading@11 411 sample_queue_push(&trk->sample_queue, pkt->data, pkt->size, sample);
yading@11 412
yading@11 413 /* Feed the packet to the RTP muxer */
yading@11 414 ff_write_chained(rtp_ctx, 0, pkt, s);
yading@11 415
yading@11 416 /* Fetch the output from the RTP muxer, open a new output buffer
yading@11 417 * for next time. */
yading@11 418 size = avio_close_dyn_buf(rtp_ctx->pb, &buf);
yading@11 419 if ((ret = ffio_open_dyn_packet_buf(&rtp_ctx->pb,
yading@11 420 RTP_MAX_PACKET_SIZE)) < 0)
yading@11 421 goto done;
yading@11 422
yading@11 423 if (size <= 0)
yading@11 424 goto done;
yading@11 425
yading@11 426 /* Open a buffer for writing the hint */
yading@11 427 if ((ret = avio_open_dyn_buf(&hintbuf)) < 0)
yading@11 428 goto done;
yading@11 429 av_init_packet(&hint_pkt);
yading@11 430 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts);
yading@11 431 av_freep(&buf);
yading@11 432
yading@11 433 /* Write the hint data into the hint track */
yading@11 434 hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf);
yading@11 435 hint_pkt.data = buf;
yading@11 436 hint_pkt.pts = hint_pkt.dts;
yading@11 437 hint_pkt.stream_index = track_index;
yading@11 438 if (pkt->flags & AV_PKT_FLAG_KEY)
yading@11 439 hint_pkt.flags |= AV_PKT_FLAG_KEY;
yading@11 440 if (count > 0)
yading@11 441 ff_mov_write_packet(s, &hint_pkt);
yading@11 442 done:
yading@11 443 av_free(buf);
yading@11 444 sample_queue_retain(&trk->sample_queue);
yading@11 445 return ret;
yading@11 446 }
yading@11 447
yading@11 448 void ff_mov_close_hinting(MOVTrack *track) {
yading@11 449 AVFormatContext* rtp_ctx = track->rtp_ctx;
yading@11 450 uint8_t *ptr;
yading@11 451
yading@11 452 av_freep(&track->enc);
yading@11 453 sample_queue_free(&track->sample_queue);
yading@11 454 if (!rtp_ctx)
yading@11 455 return;
yading@11 456 if (rtp_ctx->pb) {
yading@11 457 av_write_trailer(rtp_ctx);
yading@11 458 avio_close_dyn_buf(rtp_ctx->pb, &ptr);
yading@11 459 av_free(ptr);
yading@11 460 }
yading@11 461 avformat_free_context(rtp_ctx);
yading@11 462 }