Libav 0.7.1
|
00001 /* 00002 * MOV, 3GP, MP4 muxer RTP hinting 00003 * Copyright (c) 2010 Martin Storsjo 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "movenc.h" 00023 #include "libavutil/intreadwrite.h" 00024 #include "internal.h" 00025 #include "rtpenc_chain.h" 00026 #include "avio_internal.h" 00027 00028 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index) 00029 { 00030 MOVMuxContext *mov = s->priv_data; 00031 MOVTrack *track = &mov->tracks[index]; 00032 MOVTrack *src_track = &mov->tracks[src_index]; 00033 AVStream *src_st = s->streams[src_index]; 00034 int ret = AVERROR(ENOMEM); 00035 00036 track->tag = MKTAG('r','t','p',' '); 00037 track->src_track = src_index; 00038 00039 track->enc = avcodec_alloc_context(); 00040 if (!track->enc) 00041 goto fail; 00042 track->enc->codec_type = AVMEDIA_TYPE_DATA; 00043 track->enc->codec_tag = track->tag; 00044 00045 track->rtp_ctx = ff_rtp_chain_mux_open(s, src_st, NULL, 00046 RTP_MAX_PACKET_SIZE); 00047 if (!track->rtp_ctx) 00048 goto fail; 00049 00050 /* Copy the RTP AVStream timebase back to the hint AVStream */ 00051 track->timescale = track->rtp_ctx->streams[0]->time_base.den; 00052 00053 /* Mark the hinted track that packets written to it should be 00054 * sent to this track for hinting. */ 00055 src_track->hint_track = index; 00056 return 0; 00057 fail: 00058 av_log(s, AV_LOG_WARNING, 00059 "Unable to initialize hinting of stream %d\n", src_index); 00060 av_freep(&track->enc); 00061 /* Set a default timescale, to avoid crashes in av_dump_format */ 00062 track->timescale = 90000; 00063 return ret; 00064 } 00065 00069 static void sample_queue_pop(HintSampleQueue *queue) 00070 { 00071 if (queue->len <= 0) 00072 return; 00073 if (queue->samples[0].own_data) 00074 av_free(queue->samples[0].data); 00075 queue->len--; 00076 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len); 00077 } 00078 00082 static void sample_queue_free(HintSampleQueue *queue) 00083 { 00084 int i; 00085 for (i = 0; i < queue->len; i++) 00086 if (queue->samples[i].own_data) 00087 av_free(queue->samples[i].data); 00088 av_freep(&queue->samples); 00089 queue->len = 0; 00090 queue->size = 0; 00091 } 00092 00098 static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample) 00099 { 00100 /* No need to keep track of smaller samples, since describing them 00101 * with immediates is more efficient. */ 00102 if (pkt->size <= 14) 00103 return; 00104 if (!queue->samples || queue->len >= queue->size) { 00105 HintSample* samples; 00106 queue->size += 10; 00107 samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size); 00108 if (!samples) 00109 return; 00110 queue->samples = samples; 00111 } 00112 queue->samples[queue->len].data = pkt->data; 00113 queue->samples[queue->len].size = pkt->size; 00114 queue->samples[queue->len].sample_number = sample; 00115 queue->samples[queue->len].offset = 0; 00116 queue->samples[queue->len].own_data = 0; 00117 queue->len++; 00118 } 00119 00123 static void sample_queue_retain(HintSampleQueue *queue) 00124 { 00125 int i; 00126 for (i = 0; i < queue->len; ) { 00127 HintSample *sample = &queue->samples[i]; 00128 if (!sample->own_data) { 00129 uint8_t* ptr = av_malloc(sample->size); 00130 if (!ptr) { 00131 /* Unable to allocate memory for this one, remove it */ 00132 memmove(queue->samples + i, queue->samples + i + 1, 00133 sizeof(HintSample)*(queue->len - i - 1)); 00134 queue->len--; 00135 continue; 00136 } 00137 memcpy(ptr, sample->data, sample->size); 00138 sample->data = ptr; 00139 sample->own_data = 1; 00140 } 00141 i++; 00142 } 00143 } 00144 00161 static int match_segments(const uint8_t *haystack, int h_len, 00162 const uint8_t *needle, int n_pos, int n_len, 00163 int *match_h_offset_ptr, int *match_n_offset_ptr, 00164 int *match_len_ptr) 00165 { 00166 int h_pos; 00167 for (h_pos = 0; h_pos < h_len; h_pos++) { 00168 int match_len = 0; 00169 int match_h_pos, match_n_pos; 00170 00171 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */ 00172 while (h_pos + match_len < h_len && n_pos + match_len < n_len && 00173 needle[n_pos + match_len] == haystack[h_pos + match_len]) 00174 match_len++; 00175 if (match_len <= 8) 00176 continue; 00177 00178 /* If a sufficiently large match was found, try to expand 00179 * the matched segment backwards. */ 00180 match_h_pos = h_pos; 00181 match_n_pos = n_pos; 00182 while (match_n_pos > 0 && match_h_pos > 0 && 00183 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) { 00184 match_n_pos--; 00185 match_h_pos--; 00186 match_len++; 00187 } 00188 if (match_len <= 14) 00189 continue; 00190 *match_h_offset_ptr = match_h_pos; 00191 *match_n_offset_ptr = match_n_pos; 00192 *match_len_ptr = match_len; 00193 return 0; 00194 } 00195 return -1; 00196 } 00197 00213 static int find_sample_match(const uint8_t *data, int len, 00214 HintSampleQueue *queue, int *pos, 00215 int *match_sample, int *match_offset, 00216 int *match_len) 00217 { 00218 while (queue->len > 0) { 00219 HintSample *sample = &queue->samples[0]; 00220 /* If looking for matches in a new sample, skip the first 5 bytes, 00221 * since they often may be modified/removed in the output packet. */ 00222 if (sample->offset == 0 && sample->size > 5) 00223 sample->offset = 5; 00224 00225 if (match_segments(data, len, sample->data, sample->offset, 00226 sample->size, pos, match_offset, match_len) == 0) { 00227 *match_sample = sample->sample_number; 00228 /* Next time, look for matches at this offset, with a little 00229 * margin to this match. */ 00230 sample->offset = *match_offset + *match_len + 5; 00231 if (sample->offset + 10 >= sample->size) 00232 sample_queue_pop(queue); /* Not enough useful data left */ 00233 return 0; 00234 } 00235 00236 if (sample->offset < 10 && sample->size > 20) { 00237 /* No match found from the start of the sample, 00238 * try from the middle of the sample instead. */ 00239 sample->offset = sample->size/2; 00240 } else { 00241 /* No match for this sample, remove it */ 00242 sample_queue_pop(queue); 00243 } 00244 } 00245 return -1; 00246 } 00247 00248 static void output_immediate(const uint8_t *data, int size, 00249 AVIOContext *out, int *entries) 00250 { 00251 while (size > 0) { 00252 int len = size; 00253 if (len > 14) 00254 len = 14; 00255 avio_w8(out, 1); /* immediate constructor */ 00256 avio_w8(out, len); /* amount of valid data */ 00257 avio_write(out, data, len); 00258 data += len; 00259 size -= len; 00260 00261 for (; len < 14; len++) 00262 avio_w8(out, 0); 00263 00264 (*entries)++; 00265 } 00266 } 00267 00268 static void output_match(AVIOContext *out, int match_sample, 00269 int match_offset, int match_len, int *entries) 00270 { 00271 avio_w8(out, 2); /* sample constructor */ 00272 avio_w8(out, 0); /* track reference */ 00273 avio_wb16(out, match_len); 00274 avio_wb32(out, match_sample); 00275 avio_wb32(out, match_offset); 00276 avio_wb16(out, 1); /* bytes per block */ 00277 avio_wb16(out, 1); /* samples per block */ 00278 (*entries)++; 00279 } 00280 00281 static void describe_payload(const uint8_t *data, int size, 00282 AVIOContext *out, int *entries, 00283 HintSampleQueue *queue) 00284 { 00285 /* Describe the payload using different constructors */ 00286 while (size > 0) { 00287 int match_sample, match_offset, match_len, pos; 00288 if (find_sample_match(data, size, queue, &pos, &match_sample, 00289 &match_offset, &match_len) < 0) 00290 break; 00291 output_immediate(data, pos, out, entries); 00292 data += pos; 00293 size -= pos; 00294 output_match(out, match_sample, match_offset, match_len, entries); 00295 data += match_len; 00296 size -= match_len; 00297 } 00298 output_immediate(data, size, out, entries); 00299 } 00300 00313 static int write_hint_packets(AVIOContext *out, const uint8_t *data, 00314 int size, MOVTrack *trk, int64_t *pts) 00315 { 00316 int64_t curpos; 00317 int64_t count_pos, entries_pos; 00318 int count = 0, entries; 00319 00320 count_pos = avio_tell(out); 00321 /* RTPsample header */ 00322 avio_wb16(out, 0); /* packet count */ 00323 avio_wb16(out, 0); /* reserved */ 00324 00325 while (size > 4) { 00326 uint32_t packet_len = AV_RB32(data); 00327 uint16_t seq; 00328 uint32_t ts; 00329 00330 data += 4; 00331 size -= 4; 00332 if (packet_len > size || packet_len <= 12) 00333 break; 00334 if (data[1] >= 200 && data[1] <= 204) { 00335 /* RTCP packet, just skip */ 00336 data += packet_len; 00337 size -= packet_len; 00338 continue; 00339 } 00340 00341 if (packet_len > trk->max_packet_size) 00342 trk->max_packet_size = packet_len; 00343 00344 seq = AV_RB16(&data[2]); 00345 ts = AV_RB32(&data[4]); 00346 00347 if (trk->prev_rtp_ts == 0) 00348 trk->prev_rtp_ts = ts; 00349 /* Unwrap the 32-bit RTP timestamp that wraps around often 00350 * into a not (as often) wrapping 64-bit timestamp. */ 00351 trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts); 00352 trk->prev_rtp_ts = ts; 00353 if (*pts == AV_NOPTS_VALUE) 00354 *pts = trk->cur_rtp_ts_unwrapped; 00355 00356 count++; 00357 /* RTPpacket header */ 00358 avio_wb32(out, 0); /* relative_time */ 00359 avio_write(out, data, 2); /* RTP header */ 00360 avio_wb16(out, seq); /* RTPsequenceseed */ 00361 avio_wb16(out, 0); /* reserved + flags */ 00362 entries_pos = avio_tell(out); 00363 avio_wb16(out, 0); /* entry count */ 00364 00365 data += 12; 00366 size -= 12; 00367 packet_len -= 12; 00368 00369 entries = 0; 00370 /* Write one or more constructors describing the payload data */ 00371 describe_payload(data, packet_len, out, &entries, &trk->sample_queue); 00372 data += packet_len; 00373 size -= packet_len; 00374 00375 curpos = avio_tell(out); 00376 avio_seek(out, entries_pos, SEEK_SET); 00377 avio_wb16(out, entries); 00378 avio_seek(out, curpos, SEEK_SET); 00379 } 00380 00381 curpos = avio_tell(out); 00382 avio_seek(out, count_pos, SEEK_SET); 00383 avio_wb16(out, count); 00384 avio_seek(out, curpos, SEEK_SET); 00385 return count; 00386 } 00387 00388 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, 00389 int track_index, int sample) 00390 { 00391 MOVMuxContext *mov = s->priv_data; 00392 MOVTrack *trk = &mov->tracks[track_index]; 00393 AVFormatContext *rtp_ctx = trk->rtp_ctx; 00394 uint8_t *buf = NULL; 00395 int size; 00396 AVIOContext *hintbuf = NULL; 00397 AVPacket hint_pkt; 00398 int ret = 0, count; 00399 00400 if (!rtp_ctx) 00401 return AVERROR(ENOENT); 00402 if (!rtp_ctx->pb) 00403 return AVERROR(ENOMEM); 00404 00405 sample_queue_push(&trk->sample_queue, pkt, sample); 00406 00407 /* Feed the packet to the RTP muxer */ 00408 ff_write_chained(rtp_ctx, 0, pkt, s); 00409 00410 /* Fetch the output from the RTP muxer, open a new output buffer 00411 * for next time. */ 00412 size = avio_close_dyn_buf(rtp_ctx->pb, &buf); 00413 if ((ret = ffio_open_dyn_packet_buf(&rtp_ctx->pb, 00414 RTP_MAX_PACKET_SIZE)) < 0) 00415 goto done; 00416 00417 if (size <= 0) 00418 goto done; 00419 00420 /* Open a buffer for writing the hint */ 00421 if ((ret = avio_open_dyn_buf(&hintbuf)) < 0) 00422 goto done; 00423 av_init_packet(&hint_pkt); 00424 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts); 00425 av_freep(&buf); 00426 00427 /* Write the hint data into the hint track */ 00428 hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf); 00429 hint_pkt.data = buf; 00430 hint_pkt.pts = hint_pkt.dts; 00431 hint_pkt.stream_index = track_index; 00432 if (pkt->flags & AV_PKT_FLAG_KEY) 00433 hint_pkt.flags |= AV_PKT_FLAG_KEY; 00434 if (count > 0) 00435 ff_mov_write_packet(s, &hint_pkt); 00436 done: 00437 av_free(buf); 00438 sample_queue_retain(&trk->sample_queue); 00439 return ret; 00440 } 00441 00442 void ff_mov_close_hinting(MOVTrack *track) { 00443 AVFormatContext* rtp_ctx = track->rtp_ctx; 00444 uint8_t *ptr; 00445 00446 av_freep(&track->enc); 00447 sample_queue_free(&track->sample_queue); 00448 if (!rtp_ctx) 00449 return; 00450 if (rtp_ctx->pb) { 00451 av_write_trailer(rtp_ctx); 00452 avio_close_dyn_buf(rtp_ctx->pb, &ptr); 00453 av_free(ptr); 00454 } 00455 avformat_free_context(rtp_ctx); 00456 } 00457