yading@11: /* yading@11: * MOV, 3GP, MP4 muxer RTP hinting yading@11: * Copyright (c) 2010 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "movenc.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: #include "internal.h" yading@11: #include "rtpenc_chain.h" yading@11: #include "avio_internal.h" yading@11: #include "rtp.h" yading@11: yading@11: int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index) yading@11: { yading@11: MOVMuxContext *mov = s->priv_data; yading@11: MOVTrack *track = &mov->tracks[index]; yading@11: MOVTrack *src_track = &mov->tracks[src_index]; yading@11: AVStream *src_st = s->streams[src_index]; yading@11: int ret = AVERROR(ENOMEM); yading@11: yading@11: track->tag = MKTAG('r','t','p',' '); yading@11: track->src_track = src_index; yading@11: yading@11: track->enc = avcodec_alloc_context3(NULL); yading@11: if (!track->enc) yading@11: goto fail; yading@11: track->enc->codec_type = AVMEDIA_TYPE_DATA; yading@11: track->enc->codec_tag = track->tag; yading@11: yading@11: ret = ff_rtp_chain_mux_open(&track->rtp_ctx, s, src_st, NULL, yading@11: RTP_MAX_PACKET_SIZE, src_index); yading@11: if (ret < 0) yading@11: goto fail; yading@11: yading@11: /* Copy the RTP AVStream timebase back to the hint AVStream */ yading@11: track->timescale = track->rtp_ctx->streams[0]->time_base.den; yading@11: yading@11: /* Mark the hinted track that packets written to it should be yading@11: * sent to this track for hinting. */ yading@11: src_track->hint_track = index; yading@11: return 0; yading@11: fail: yading@11: av_log(s, AV_LOG_WARNING, yading@11: "Unable to initialize hinting of stream %d\n", src_index); yading@11: av_freep(&track->enc); yading@11: /* Set a default timescale, to avoid crashes in av_dump_format */ yading@11: track->timescale = 90000; yading@11: return ret; yading@11: } yading@11: yading@11: /** yading@11: * Remove the first sample from the sample queue. yading@11: */ yading@11: static void sample_queue_pop(HintSampleQueue *queue) yading@11: { yading@11: if (queue->len <= 0) yading@11: return; yading@11: if (queue->samples[0].own_data) yading@11: av_free(queue->samples[0].data); yading@11: queue->len--; yading@11: memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len); yading@11: } yading@11: yading@11: /** yading@11: * Empty the sample queue, releasing all memory. yading@11: */ yading@11: static void sample_queue_free(HintSampleQueue *queue) yading@11: { yading@11: int i; yading@11: for (i = 0; i < queue->len; i++) yading@11: if (queue->samples[i].own_data) yading@11: av_free(queue->samples[i].data); yading@11: av_freep(&queue->samples); yading@11: queue->len = 0; yading@11: queue->size = 0; yading@11: } yading@11: yading@11: /** yading@11: * Add a reference to the sample data to the sample queue. The data is yading@11: * not copied. sample_queue_retain should be called before pkt->data yading@11: * is reused/freed. yading@11: */ yading@11: static void sample_queue_push(HintSampleQueue *queue, uint8_t *data, int size, yading@11: int sample) yading@11: { yading@11: /* No need to keep track of smaller samples, since describing them yading@11: * with immediates is more efficient. */ yading@11: if (size <= 14) yading@11: return; yading@11: if (!queue->samples || queue->len >= queue->size) { yading@11: HintSample* samples; yading@11: queue->size += 10; yading@11: samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size); yading@11: if (!samples) yading@11: return; yading@11: queue->samples = samples; yading@11: } yading@11: queue->samples[queue->len].data = data; yading@11: queue->samples[queue->len].size = size; yading@11: queue->samples[queue->len].sample_number = sample; yading@11: queue->samples[queue->len].offset = 0; yading@11: queue->samples[queue->len].own_data = 0; yading@11: queue->len++; yading@11: } yading@11: yading@11: /** yading@11: * Make local copies of all referenced sample data in the queue. yading@11: */ yading@11: static void sample_queue_retain(HintSampleQueue *queue) yading@11: { yading@11: int i; yading@11: for (i = 0; i < queue->len; ) { yading@11: HintSample *sample = &queue->samples[i]; yading@11: if (!sample->own_data) { yading@11: uint8_t* ptr = av_malloc(sample->size); yading@11: if (!ptr) { yading@11: /* Unable to allocate memory for this one, remove it */ yading@11: memmove(queue->samples + i, queue->samples + i + 1, yading@11: sizeof(HintSample)*(queue->len - i - 1)); yading@11: queue->len--; yading@11: continue; yading@11: } yading@11: memcpy(ptr, sample->data, sample->size); yading@11: sample->data = ptr; yading@11: sample->own_data = 1; yading@11: } yading@11: i++; yading@11: } yading@11: } yading@11: yading@11: /** yading@11: * Find matches of needle[n_pos ->] within haystack. If a sufficiently yading@11: * large match is found, matching bytes before n_pos are included yading@11: * in the match, too (within the limits of the arrays). yading@11: * yading@11: * @param haystack buffer that may contain parts of needle yading@11: * @param h_len length of the haystack buffer yading@11: * @param needle buffer containing source data that have been used to yading@11: * construct haystack yading@11: * @param n_pos start position in needle used for looking for matches yading@11: * @param n_len length of the needle buffer yading@11: * @param match_h_offset_ptr offset of the first matching byte within haystack yading@11: * @param match_n_offset_ptr offset of the first matching byte within needle yading@11: * @param match_len_ptr length of the matched segment yading@11: * @return 0 if a match was found, < 0 if no match was found yading@11: */ yading@11: static int match_segments(const uint8_t *haystack, int h_len, yading@11: const uint8_t *needle, int n_pos, int n_len, yading@11: int *match_h_offset_ptr, int *match_n_offset_ptr, yading@11: int *match_len_ptr) yading@11: { yading@11: int h_pos; yading@11: for (h_pos = 0; h_pos < h_len; h_pos++) { yading@11: int match_len = 0; yading@11: int match_h_pos, match_n_pos; yading@11: yading@11: /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */ yading@11: while (h_pos + match_len < h_len && n_pos + match_len < n_len && yading@11: needle[n_pos + match_len] == haystack[h_pos + match_len]) yading@11: match_len++; yading@11: if (match_len <= 8) yading@11: continue; yading@11: yading@11: /* If a sufficiently large match was found, try to expand yading@11: * the matched segment backwards. */ yading@11: match_h_pos = h_pos; yading@11: match_n_pos = n_pos; yading@11: while (match_n_pos > 0 && match_h_pos > 0 && yading@11: needle[match_n_pos - 1] == haystack[match_h_pos - 1]) { yading@11: match_n_pos--; yading@11: match_h_pos--; yading@11: match_len++; yading@11: } yading@11: if (match_len <= 14) yading@11: continue; yading@11: *match_h_offset_ptr = match_h_pos; yading@11: *match_n_offset_ptr = match_n_pos; yading@11: *match_len_ptr = match_len; yading@11: return 0; yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: /** yading@11: * Look for segments in samples in the sample queue matching the data yading@11: * in ptr. Samples not matching are removed from the queue. If a match yading@11: * is found, the next time it will look for matches starting from the yading@11: * end of the previous matched segment. yading@11: * yading@11: * @param data data to find matches for in the sample queue yading@11: * @param len length of the data buffer yading@11: * @param queue samples used for looking for matching segments yading@11: * @param pos the offset in data of the matched segment yading@11: * @param match_sample the number of the sample that contained the match yading@11: * @param match_offset the offset of the matched segment within the sample yading@11: * @param match_len the length of the matched segment yading@11: * @return 0 if a match was found, < 0 if no match was found yading@11: */ yading@11: static int find_sample_match(const uint8_t *data, int len, yading@11: HintSampleQueue *queue, int *pos, yading@11: int *match_sample, int *match_offset, yading@11: int *match_len) yading@11: { yading@11: while (queue->len > 0) { yading@11: HintSample *sample = &queue->samples[0]; yading@11: /* If looking for matches in a new sample, skip the first 5 bytes, yading@11: * since they often may be modified/removed in the output packet. */ yading@11: if (sample->offset == 0 && sample->size > 5) yading@11: sample->offset = 5; yading@11: yading@11: if (match_segments(data, len, sample->data, sample->offset, yading@11: sample->size, pos, match_offset, match_len) == 0) { yading@11: *match_sample = sample->sample_number; yading@11: /* Next time, look for matches at this offset, with a little yading@11: * margin to this match. */ yading@11: sample->offset = *match_offset + *match_len + 5; yading@11: if (sample->offset + 10 >= sample->size) yading@11: sample_queue_pop(queue); /* Not enough useful data left */ yading@11: return 0; yading@11: } yading@11: yading@11: if (sample->offset < 10 && sample->size > 20) { yading@11: /* No match found from the start of the sample, yading@11: * try from the middle of the sample instead. */ yading@11: sample->offset = sample->size/2; yading@11: } else { yading@11: /* No match for this sample, remove it */ yading@11: sample_queue_pop(queue); yading@11: } yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: static void output_immediate(const uint8_t *data, int size, yading@11: AVIOContext *out, int *entries) yading@11: { yading@11: while (size > 0) { yading@11: int len = size; yading@11: if (len > 14) yading@11: len = 14; yading@11: avio_w8(out, 1); /* immediate constructor */ yading@11: avio_w8(out, len); /* amount of valid data */ yading@11: avio_write(out, data, len); yading@11: data += len; yading@11: size -= len; yading@11: yading@11: for (; len < 14; len++) yading@11: avio_w8(out, 0); yading@11: yading@11: (*entries)++; yading@11: } yading@11: } yading@11: yading@11: static void output_match(AVIOContext *out, int match_sample, yading@11: int match_offset, int match_len, int *entries) yading@11: { yading@11: avio_w8(out, 2); /* sample constructor */ yading@11: avio_w8(out, 0); /* track reference */ yading@11: avio_wb16(out, match_len); yading@11: avio_wb32(out, match_sample); yading@11: avio_wb32(out, match_offset); yading@11: avio_wb16(out, 1); /* bytes per block */ yading@11: avio_wb16(out, 1); /* samples per block */ yading@11: (*entries)++; yading@11: } yading@11: yading@11: static void describe_payload(const uint8_t *data, int size, yading@11: AVIOContext *out, int *entries, yading@11: HintSampleQueue *queue) yading@11: { yading@11: /* Describe the payload using different constructors */ yading@11: while (size > 0) { yading@11: int match_sample, match_offset, match_len, pos; yading@11: if (find_sample_match(data, size, queue, &pos, &match_sample, yading@11: &match_offset, &match_len) < 0) yading@11: break; yading@11: output_immediate(data, pos, out, entries); yading@11: data += pos; yading@11: size -= pos; yading@11: output_match(out, match_sample, match_offset, match_len, entries); yading@11: data += match_len; yading@11: size -= match_len; yading@11: } yading@11: output_immediate(data, size, out, entries); yading@11: } yading@11: yading@11: /** yading@11: * Write an RTP hint (that may contain one or more RTP packets) yading@11: * for the packets in data. data contains one or more packets with a yading@11: * BE32 size header. yading@11: * yading@11: * @param out buffer where the hints are written yading@11: * @param data buffer containing RTP packets yading@11: * @param size the size of the data buffer yading@11: * @param trk the MOVTrack for the hint track yading@11: * @param pts pointer where the timestamp for the written RTP hint is stored yading@11: * @return the number of RTP packets in the written hint yading@11: */ yading@11: static int write_hint_packets(AVIOContext *out, const uint8_t *data, yading@11: int size, MOVTrack *trk, int64_t *pts) yading@11: { yading@11: int64_t curpos; yading@11: int64_t count_pos, entries_pos; yading@11: int count = 0, entries; yading@11: yading@11: count_pos = avio_tell(out); yading@11: /* RTPsample header */ yading@11: avio_wb16(out, 0); /* packet count */ yading@11: avio_wb16(out, 0); /* reserved */ yading@11: yading@11: while (size > 4) { yading@11: uint32_t packet_len = AV_RB32(data); yading@11: uint16_t seq; yading@11: uint32_t ts; yading@11: yading@11: data += 4; yading@11: size -= 4; yading@11: if (packet_len > size || packet_len <= 12) yading@11: break; yading@11: if (RTP_PT_IS_RTCP(data[1])) { yading@11: /* RTCP packet, just skip */ yading@11: data += packet_len; yading@11: size -= packet_len; yading@11: continue; yading@11: } yading@11: yading@11: if (packet_len > trk->max_packet_size) yading@11: trk->max_packet_size = packet_len; yading@11: yading@11: seq = AV_RB16(&data[2]); yading@11: ts = AV_RB32(&data[4]); yading@11: yading@11: if (trk->prev_rtp_ts == 0) yading@11: trk->prev_rtp_ts = ts; yading@11: /* Unwrap the 32-bit RTP timestamp that wraps around often yading@11: * into a not (as often) wrapping 64-bit timestamp. */ yading@11: trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts); yading@11: trk->prev_rtp_ts = ts; yading@11: if (*pts == AV_NOPTS_VALUE) yading@11: *pts = trk->cur_rtp_ts_unwrapped; yading@11: yading@11: count++; yading@11: /* RTPpacket header */ yading@11: avio_wb32(out, 0); /* relative_time */ yading@11: avio_write(out, data, 2); /* RTP header */ yading@11: avio_wb16(out, seq); /* RTPsequenceseed */ yading@11: avio_wb16(out, 0); /* reserved + flags */ yading@11: entries_pos = avio_tell(out); yading@11: avio_wb16(out, 0); /* entry count */ yading@11: yading@11: data += 12; yading@11: size -= 12; yading@11: packet_len -= 12; yading@11: yading@11: entries = 0; yading@11: /* Write one or more constructors describing the payload data */ yading@11: describe_payload(data, packet_len, out, &entries, &trk->sample_queue); yading@11: data += packet_len; yading@11: size -= packet_len; yading@11: yading@11: curpos = avio_tell(out); yading@11: avio_seek(out, entries_pos, SEEK_SET); yading@11: avio_wb16(out, entries); yading@11: avio_seek(out, curpos, SEEK_SET); yading@11: } yading@11: yading@11: curpos = avio_tell(out); yading@11: avio_seek(out, count_pos, SEEK_SET); yading@11: avio_wb16(out, count); yading@11: avio_seek(out, curpos, SEEK_SET); yading@11: return count; yading@11: } yading@11: yading@11: int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, yading@11: int track_index, int sample, yading@11: uint8_t *sample_data, int sample_size) yading@11: { yading@11: MOVMuxContext *mov = s->priv_data; yading@11: MOVTrack *trk = &mov->tracks[track_index]; yading@11: AVFormatContext *rtp_ctx = trk->rtp_ctx; yading@11: uint8_t *buf = NULL; yading@11: int size; yading@11: AVIOContext *hintbuf = NULL; yading@11: AVPacket hint_pkt; yading@11: int ret = 0, count; yading@11: yading@11: if (!rtp_ctx) yading@11: return AVERROR(ENOENT); yading@11: if (!rtp_ctx->pb) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: if (sample_data) yading@11: sample_queue_push(&trk->sample_queue, sample_data, sample_size, sample); yading@11: else yading@11: sample_queue_push(&trk->sample_queue, pkt->data, pkt->size, sample); yading@11: yading@11: /* Feed the packet to the RTP muxer */ yading@11: ff_write_chained(rtp_ctx, 0, pkt, s); yading@11: yading@11: /* Fetch the output from the RTP muxer, open a new output buffer yading@11: * for next time. */ yading@11: size = avio_close_dyn_buf(rtp_ctx->pb, &buf); yading@11: if ((ret = ffio_open_dyn_packet_buf(&rtp_ctx->pb, yading@11: RTP_MAX_PACKET_SIZE)) < 0) yading@11: goto done; yading@11: yading@11: if (size <= 0) yading@11: goto done; yading@11: yading@11: /* Open a buffer for writing the hint */ yading@11: if ((ret = avio_open_dyn_buf(&hintbuf)) < 0) yading@11: goto done; yading@11: av_init_packet(&hint_pkt); yading@11: count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts); yading@11: av_freep(&buf); yading@11: yading@11: /* Write the hint data into the hint track */ yading@11: hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf); yading@11: hint_pkt.data = buf; yading@11: hint_pkt.pts = hint_pkt.dts; yading@11: hint_pkt.stream_index = track_index; yading@11: if (pkt->flags & AV_PKT_FLAG_KEY) yading@11: hint_pkt.flags |= AV_PKT_FLAG_KEY; yading@11: if (count > 0) yading@11: ff_mov_write_packet(s, &hint_pkt); yading@11: done: yading@11: av_free(buf); yading@11: sample_queue_retain(&trk->sample_queue); yading@11: return ret; yading@11: } yading@11: yading@11: void ff_mov_close_hinting(MOVTrack *track) { yading@11: AVFormatContext* rtp_ctx = track->rtp_ctx; yading@11: uint8_t *ptr; yading@11: yading@11: av_freep(&track->enc); yading@11: sample_queue_free(&track->sample_queue); yading@11: if (!rtp_ctx) yading@11: return; yading@11: if (rtp_ctx->pb) { yading@11: av_write_trailer(rtp_ctx); yading@11: avio_close_dyn_buf(rtp_ctx->pb, &ptr); yading@11: av_free(ptr); yading@11: } yading@11: avformat_free_context(rtp_ctx); yading@11: }