Libav
|
00001 /* 00002 * MOV, 3GP, MP4 muxer RTP hinting 00003 * Copyright (c) 2010 Martin Storsjo 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 00025 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index) 00026 { 00027 MOVMuxContext *mov = s->priv_data; 00028 MOVTrack *track = &mov->tracks[index]; 00029 MOVTrack *src_track = &mov->tracks[src_index]; 00030 AVStream *src_st = s->streams[src_index]; 00031 int ret = AVERROR(ENOMEM); 00032 AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL); 00033 00034 track->tag = MKTAG('r','t','p',' '); 00035 track->src_track = src_index; 00036 00037 if (!rtp_format) { 00038 ret = AVERROR(ENOENT); 00039 goto fail; 00040 } 00041 00042 track->enc = avcodec_alloc_context(); 00043 if (!track->enc) 00044 goto fail; 00045 track->enc->codec_type = AVMEDIA_TYPE_DATA; 00046 track->enc->codec_tag = track->tag; 00047 00048 track->rtp_ctx = avformat_alloc_context(); 00049 if (!track->rtp_ctx) 00050 goto fail; 00051 track->rtp_ctx->oformat = rtp_format; 00052 if (!av_new_stream(track->rtp_ctx, 0)) 00053 goto fail; 00054 00055 /* Copy stream parameters */ 00056 track->rtp_ctx->streams[0]->sample_aspect_ratio = 00057 src_st->sample_aspect_ratio; 00058 00059 /* Remove the allocated codec context, link to the original one 00060 * instead, to give the rtp muxer access to codec parameters. */ 00061 av_free(track->rtp_ctx->streams[0]->codec); 00062 track->rtp_ctx->streams[0]->codec = src_st->codec; 00063 00064 if ((ret = url_open_dyn_packet_buf(&track->rtp_ctx->pb, 00065 RTP_MAX_PACKET_SIZE)) < 0) 00066 goto fail; 00067 ret = av_write_header(track->rtp_ctx); 00068 if (ret) 00069 goto fail; 00070 00071 /* Copy the RTP AVStream timebase back to the hint AVStream */ 00072 track->timescale = track->rtp_ctx->streams[0]->time_base.den; 00073 00074 /* Mark the hinted track that packets written to it should be 00075 * sent to this track for hinting. */ 00076 src_track->hint_track = index; 00077 return 0; 00078 fail: 00079 av_log(s, AV_LOG_WARNING, 00080 "Unable to initialize hinting of stream %d\n", src_index); 00081 if (track->rtp_ctx && track->rtp_ctx->pb) { 00082 uint8_t *buf; 00083 url_close_dyn_buf(track->rtp_ctx->pb, &buf); 00084 av_free(buf); 00085 } 00086 if (track->rtp_ctx && track->rtp_ctx->streams[0]) { 00087 av_metadata_free(&track->rtp_ctx->streams[0]->metadata); 00088 av_free(track->rtp_ctx->streams[0]); 00089 } 00090 if (track->rtp_ctx) { 00091 av_metadata_free(&track->rtp_ctx->metadata); 00092 av_free(track->rtp_ctx->priv_data); 00093 av_freep(&track->rtp_ctx); 00094 } 00095 av_freep(&track->enc); 00096 /* Set a default timescale, to avoid crashes in dump_format */ 00097 track->timescale = 90000; 00098 return ret; 00099 } 00100 00104 static void sample_queue_pop(HintSampleQueue *queue) 00105 { 00106 if (queue->len <= 0) 00107 return; 00108 if (queue->samples[0].own_data) 00109 av_free(queue->samples[0].data); 00110 queue->len--; 00111 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len); 00112 } 00113 00117 static void sample_queue_free(HintSampleQueue *queue) 00118 { 00119 int i; 00120 for (i = 0; i < queue->len; i++) 00121 if (queue->samples[i].own_data) 00122 av_free(queue->samples[i].data); 00123 av_freep(&queue->samples); 00124 queue->len = 0; 00125 queue->size = 0; 00126 } 00127 00133 static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample) 00134 { 00135 /* No need to keep track of smaller samples, since describing them 00136 * with immediates is more efficient. */ 00137 if (pkt->size <= 14) 00138 return; 00139 if (!queue->samples || queue->len >= queue->size) { 00140 HintSample* samples; 00141 queue->size += 10; 00142 samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size); 00143 if (!samples) 00144 return; 00145 queue->samples = samples; 00146 } 00147 queue->samples[queue->len].data = pkt->data; 00148 queue->samples[queue->len].size = pkt->size; 00149 queue->samples[queue->len].sample_number = sample; 00150 queue->samples[queue->len].offset = 0; 00151 queue->samples[queue->len].own_data = 0; 00152 queue->len++; 00153 } 00154 00158 static void sample_queue_retain(HintSampleQueue *queue) 00159 { 00160 int i; 00161 for (i = 0; i < queue->len; ) { 00162 HintSample *sample = &queue->samples[i]; 00163 if (!sample->own_data) { 00164 uint8_t* ptr = av_malloc(sample->size); 00165 if (!ptr) { 00166 /* Unable to allocate memory for this one, remove it */ 00167 memmove(queue->samples + i, queue->samples + i + 1, 00168 sizeof(HintSample)*(queue->len - i - 1)); 00169 queue->len--; 00170 continue; 00171 } 00172 memcpy(ptr, sample->data, sample->size); 00173 sample->data = ptr; 00174 sample->own_data = 1; 00175 } 00176 i++; 00177 } 00178 } 00179 00196 static int match_segments(const uint8_t *haystack, int h_len, 00197 const uint8_t *needle, int n_pos, int n_len, 00198 int *match_h_offset_ptr, int *match_n_offset_ptr, 00199 int *match_len_ptr) 00200 { 00201 int h_pos; 00202 for (h_pos = 0; h_pos < h_len; h_pos++) { 00203 int match_len = 0; 00204 int match_h_pos, match_n_pos; 00205 00206 /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */ 00207 while (h_pos + match_len < h_len && n_pos + match_len < n_len && 00208 needle[n_pos + match_len] == haystack[h_pos + match_len]) 00209 match_len++; 00210 if (match_len <= 8) 00211 continue; 00212 00213 /* If a sufficiently large match was found, try to expand 00214 * the matched segment backwards. */ 00215 match_h_pos = h_pos; 00216 match_n_pos = n_pos; 00217 while (match_n_pos > 0 && match_h_pos > 0 && 00218 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) { 00219 match_n_pos--; 00220 match_h_pos--; 00221 match_len++; 00222 } 00223 if (match_len <= 14) 00224 continue; 00225 *match_h_offset_ptr = match_h_pos; 00226 *match_n_offset_ptr = match_n_pos; 00227 *match_len_ptr = match_len; 00228 return 0; 00229 } 00230 return -1; 00231 } 00232 00248 static int find_sample_match(const uint8_t *data, int len, 00249 HintSampleQueue *queue, int *pos, 00250 int *match_sample, int *match_offset, 00251 int *match_len) 00252 { 00253 while (queue->len > 0) { 00254 HintSample *sample = &queue->samples[0]; 00255 /* If looking for matches in a new sample, skip the first 5 bytes, 00256 * since they often may be modified/removed in the output packet. */ 00257 if (sample->offset == 0 && sample->size > 5) 00258 sample->offset = 5; 00259 00260 if (match_segments(data, len, sample->data, sample->offset, 00261 sample->size, pos, match_offset, match_len) == 0) { 00262 *match_sample = sample->sample_number; 00263 /* Next time, look for matches at this offset, with a little 00264 * margin to this match. */ 00265 sample->offset = *match_offset + *match_len + 5; 00266 if (sample->offset + 10 >= sample->size) 00267 sample_queue_pop(queue); /* Not enough useful data left */ 00268 return 0; 00269 } 00270 00271 if (sample->offset < 10 && sample->size > 20) { 00272 /* No match found from the start of the sample, 00273 * try from the middle of the sample instead. */ 00274 sample->offset = sample->size/2; 00275 } else { 00276 /* No match for this sample, remove it */ 00277 sample_queue_pop(queue); 00278 } 00279 } 00280 return -1; 00281 } 00282 00283 static void output_immediate(const uint8_t *data, int size, 00284 ByteIOContext *out, int *entries) 00285 { 00286 while (size > 0) { 00287 int len = size; 00288 if (len > 14) 00289 len = 14; 00290 put_byte(out, 1); /* immediate constructor */ 00291 put_byte(out, len); /* amount of valid data */ 00292 put_buffer(out, data, len); 00293 data += len; 00294 size -= len; 00295 00296 for (; len < 14; len++) 00297 put_byte(out, 0); 00298 00299 (*entries)++; 00300 } 00301 } 00302 00303 static void output_match(ByteIOContext *out, int match_sample, 00304 int match_offset, int match_len, int *entries) 00305 { 00306 put_byte(out, 2); /* sample constructor */ 00307 put_byte(out, 0); /* track reference */ 00308 put_be16(out, match_len); 00309 put_be32(out, match_sample); 00310 put_be32(out, match_offset); 00311 put_be16(out, 1); /* bytes per block */ 00312 put_be16(out, 1); /* samples per block */ 00313 (*entries)++; 00314 } 00315 00316 static void describe_payload(const uint8_t *data, int size, 00317 ByteIOContext *out, int *entries, 00318 HintSampleQueue *queue) 00319 { 00320 /* Describe the payload using different constructors */ 00321 while (size > 0) { 00322 int match_sample, match_offset, match_len, pos; 00323 if (find_sample_match(data, size, queue, &pos, &match_sample, 00324 &match_offset, &match_len) < 0) 00325 break; 00326 output_immediate(data, pos, out, entries); 00327 data += pos; 00328 size -= pos; 00329 output_match(out, match_sample, match_offset, match_len, entries); 00330 data += match_len; 00331 size -= match_len; 00332 } 00333 output_immediate(data, size, out, entries); 00334 } 00335 00348 static int write_hint_packets(ByteIOContext *out, const uint8_t *data, 00349 int size, MOVTrack *trk, int64_t *pts) 00350 { 00351 int64_t curpos; 00352 int64_t count_pos, entries_pos; 00353 int count = 0, entries; 00354 00355 count_pos = url_ftell(out); 00356 /* RTPsample header */ 00357 put_be16(out, 0); /* packet count */ 00358 put_be16(out, 0); /* reserved */ 00359 00360 while (size > 4) { 00361 uint32_t packet_len = AV_RB32(data); 00362 uint16_t seq; 00363 uint32_t ts; 00364 00365 data += 4; 00366 size -= 4; 00367 if (packet_len > size || packet_len <= 12) 00368 break; 00369 if (data[1] >= 200 && data[1] <= 204) { 00370 /* RTCP packet, just skip */ 00371 data += packet_len; 00372 size -= packet_len; 00373 continue; 00374 } 00375 00376 if (packet_len > trk->max_packet_size) 00377 trk->max_packet_size = packet_len; 00378 00379 seq = AV_RB16(&data[2]); 00380 ts = AV_RB32(&data[4]); 00381 00382 if (trk->prev_rtp_ts == 0) 00383 trk->prev_rtp_ts = ts; 00384 /* Unwrap the 32-bit RTP timestamp that wraps around often 00385 * into a not (as often) wrapping 64-bit timestamp. */ 00386 trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts); 00387 trk->prev_rtp_ts = ts; 00388 if (*pts == AV_NOPTS_VALUE) 00389 *pts = trk->cur_rtp_ts_unwrapped; 00390 00391 count++; 00392 /* RTPpacket header */ 00393 put_be32(out, 0); /* relative_time */ 00394 put_buffer(out, data, 2); /* RTP header */ 00395 put_be16(out, seq); /* RTPsequenceseed */ 00396 put_be16(out, 0); /* reserved + flags */ 00397 entries_pos = url_ftell(out); 00398 put_be16(out, 0); /* entry count */ 00399 00400 data += 12; 00401 size -= 12; 00402 packet_len -= 12; 00403 00404 entries = 0; 00405 /* Write one or more constructors describing the payload data */ 00406 describe_payload(data, packet_len, out, &entries, &trk->sample_queue); 00407 data += packet_len; 00408 size -= packet_len; 00409 00410 curpos = url_ftell(out); 00411 url_fseek(out, entries_pos, SEEK_SET); 00412 put_be16(out, entries); 00413 url_fseek(out, curpos, SEEK_SET); 00414 } 00415 00416 curpos = url_ftell(out); 00417 url_fseek(out, count_pos, SEEK_SET); 00418 put_be16(out, count); 00419 url_fseek(out, curpos, SEEK_SET); 00420 return count; 00421 } 00422 00423 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, 00424 int track_index, int sample) 00425 { 00426 MOVMuxContext *mov = s->priv_data; 00427 MOVTrack *trk = &mov->tracks[track_index]; 00428 AVFormatContext *rtp_ctx = trk->rtp_ctx; 00429 uint8_t *buf = NULL; 00430 int size; 00431 ByteIOContext *hintbuf = NULL; 00432 AVPacket hint_pkt; 00433 AVPacket local_pkt; 00434 int ret = 0, count; 00435 00436 if (!rtp_ctx) 00437 return AVERROR(ENOENT); 00438 if (!rtp_ctx->pb) 00439 return AVERROR(ENOMEM); 00440 00441 sample_queue_push(&trk->sample_queue, pkt, sample); 00442 00443 /* Feed the packet to the RTP muxer */ 00444 local_pkt = *pkt; 00445 local_pkt.stream_index = 0; 00446 local_pkt.pts = av_rescale_q(pkt->pts, 00447 s->streams[pkt->stream_index]->time_base, 00448 rtp_ctx->streams[0]->time_base); 00449 local_pkt.dts = av_rescale_q(pkt->dts, 00450 s->streams[pkt->stream_index]->time_base, 00451 rtp_ctx->streams[0]->time_base); 00452 av_write_frame(rtp_ctx, &local_pkt); 00453 00454 /* Fetch the output from the RTP muxer, open a new output buffer 00455 * for next time. */ 00456 size = url_close_dyn_buf(rtp_ctx->pb, &buf); 00457 if ((ret = url_open_dyn_packet_buf(&rtp_ctx->pb, 00458 RTP_MAX_PACKET_SIZE)) < 0) 00459 goto done; 00460 00461 if (size <= 0) 00462 goto done; 00463 00464 /* Open a buffer for writing the hint */ 00465 if ((ret = url_open_dyn_buf(&hintbuf)) < 0) 00466 goto done; 00467 av_init_packet(&hint_pkt); 00468 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts); 00469 av_freep(&buf); 00470 00471 /* Write the hint data into the hint track */ 00472 hint_pkt.size = size = url_close_dyn_buf(hintbuf, &buf); 00473 hint_pkt.data = buf; 00474 hint_pkt.pts = hint_pkt.dts; 00475 hint_pkt.stream_index = track_index; 00476 if (pkt->flags & AV_PKT_FLAG_KEY) 00477 hint_pkt.flags |= AV_PKT_FLAG_KEY; 00478 if (count > 0) 00479 ff_mov_write_packet(s, &hint_pkt); 00480 done: 00481 av_free(buf); 00482 sample_queue_retain(&trk->sample_queue); 00483 return ret; 00484 } 00485 00486 void ff_mov_close_hinting(MOVTrack *track) { 00487 AVFormatContext* rtp_ctx = track->rtp_ctx; 00488 uint8_t *ptr; 00489 00490 av_freep(&track->enc); 00491 sample_queue_free(&track->sample_queue); 00492 if (!rtp_ctx) 00493 return; 00494 if (rtp_ctx->pb) { 00495 av_write_trailer(rtp_ctx); 00496 url_close_dyn_buf(rtp_ctx->pb, &ptr); 00497 av_free(ptr); 00498 } 00499 av_metadata_free(&rtp_ctx->streams[0]->metadata); 00500 av_metadata_free(&rtp_ctx->metadata); 00501 av_free(rtp_ctx->streams[0]); 00502 av_freep(&rtp_ctx); 00503 } 00504