00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "movenc.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "internal.h"
00025 #include "rtpenc_chain.h"
00026
00027 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
00028 {
00029 MOVMuxContext *mov = s->priv_data;
00030 MOVTrack *track = &mov->tracks[index];
00031 MOVTrack *src_track = &mov->tracks[src_index];
00032 AVStream *src_st = s->streams[src_index];
00033 int ret = AVERROR(ENOMEM);
00034
00035 track->tag = MKTAG('r','t','p',' ');
00036 track->src_track = src_index;
00037
00038 track->enc = avcodec_alloc_context();
00039 if (!track->enc)
00040 goto fail;
00041 track->enc->codec_type = AVMEDIA_TYPE_DATA;
00042 track->enc->codec_tag = track->tag;
00043
00044 track->rtp_ctx = ff_rtp_chain_mux_open(s, src_st, NULL,
00045 RTP_MAX_PACKET_SIZE);
00046 if (!track->rtp_ctx)
00047 goto fail;
00048
00049
00050 track->timescale = track->rtp_ctx->streams[0]->time_base.den;
00051
00052
00053
00054 src_track->hint_track = index;
00055 return 0;
00056 fail:
00057 av_log(s, AV_LOG_WARNING,
00058 "Unable to initialize hinting of stream %d\n", src_index);
00059 av_freep(&track->enc);
00060
00061 track->timescale = 90000;
00062 return ret;
00063 }
00064
00068 static void sample_queue_pop(HintSampleQueue *queue)
00069 {
00070 if (queue->len <= 0)
00071 return;
00072 if (queue->samples[0].own_data)
00073 av_free(queue->samples[0].data);
00074 queue->len--;
00075 memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len);
00076 }
00077
00081 static void sample_queue_free(HintSampleQueue *queue)
00082 {
00083 int i;
00084 for (i = 0; i < queue->len; i++)
00085 if (queue->samples[i].own_data)
00086 av_free(queue->samples[i].data);
00087 av_freep(&queue->samples);
00088 queue->len = 0;
00089 queue->size = 0;
00090 }
00091
00097 static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample)
00098 {
00099
00100
00101 if (pkt->size <= 14)
00102 return;
00103 if (!queue->samples || queue->len >= queue->size) {
00104 HintSample* samples;
00105 queue->size += 10;
00106 samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size);
00107 if (!samples)
00108 return;
00109 queue->samples = samples;
00110 }
00111 queue->samples[queue->len].data = pkt->data;
00112 queue->samples[queue->len].size = pkt->size;
00113 queue->samples[queue->len].sample_number = sample;
00114 queue->samples[queue->len].offset = 0;
00115 queue->samples[queue->len].own_data = 0;
00116 queue->len++;
00117 }
00118
00122 static void sample_queue_retain(HintSampleQueue *queue)
00123 {
00124 int i;
00125 for (i = 0; i < queue->len; ) {
00126 HintSample *sample = &queue->samples[i];
00127 if (!sample->own_data) {
00128 uint8_t* ptr = av_malloc(sample->size);
00129 if (!ptr) {
00130
00131 memmove(queue->samples + i, queue->samples + i + 1,
00132 sizeof(HintSample)*(queue->len - i - 1));
00133 queue->len--;
00134 continue;
00135 }
00136 memcpy(ptr, sample->data, sample->size);
00137 sample->data = ptr;
00138 sample->own_data = 1;
00139 }
00140 i++;
00141 }
00142 }
00143
00160 static int match_segments(const uint8_t *haystack, int h_len,
00161 const uint8_t *needle, int n_pos, int n_len,
00162 int *match_h_offset_ptr, int *match_n_offset_ptr,
00163 int *match_len_ptr)
00164 {
00165 int h_pos;
00166 for (h_pos = 0; h_pos < h_len; h_pos++) {
00167 int match_len = 0;
00168 int match_h_pos, match_n_pos;
00169
00170
00171 while (h_pos + match_len < h_len && n_pos + match_len < n_len &&
00172 needle[n_pos + match_len] == haystack[h_pos + match_len])
00173 match_len++;
00174 if (match_len <= 8)
00175 continue;
00176
00177
00178
00179 match_h_pos = h_pos;
00180 match_n_pos = n_pos;
00181 while (match_n_pos > 0 && match_h_pos > 0 &&
00182 needle[match_n_pos - 1] == haystack[match_h_pos - 1]) {
00183 match_n_pos--;
00184 match_h_pos--;
00185 match_len++;
00186 }
00187 if (match_len <= 14)
00188 continue;
00189 *match_h_offset_ptr = match_h_pos;
00190 *match_n_offset_ptr = match_n_pos;
00191 *match_len_ptr = match_len;
00192 return 0;
00193 }
00194 return -1;
00195 }
00196
00212 static int find_sample_match(const uint8_t *data, int len,
00213 HintSampleQueue *queue, int *pos,
00214 int *match_sample, int *match_offset,
00215 int *match_len)
00216 {
00217 while (queue->len > 0) {
00218 HintSample *sample = &queue->samples[0];
00219
00220
00221 if (sample->offset == 0 && sample->size > 5)
00222 sample->offset = 5;
00223
00224 if (match_segments(data, len, sample->data, sample->offset,
00225 sample->size, pos, match_offset, match_len) == 0) {
00226 *match_sample = sample->sample_number;
00227
00228
00229 sample->offset = *match_offset + *match_len + 5;
00230 if (sample->offset + 10 >= sample->size)
00231 sample_queue_pop(queue);
00232 return 0;
00233 }
00234
00235 if (sample->offset < 10 && sample->size > 20) {
00236
00237
00238 sample->offset = sample->size/2;
00239 } else {
00240
00241 sample_queue_pop(queue);
00242 }
00243 }
00244 return -1;
00245 }
00246
00247 static void output_immediate(const uint8_t *data, int size,
00248 AVIOContext *out, int *entries)
00249 {
00250 while (size > 0) {
00251 int len = size;
00252 if (len > 14)
00253 len = 14;
00254 avio_w8(out, 1);
00255 avio_w8(out, len);
00256 avio_write(out, data, len);
00257 data += len;
00258 size -= len;
00259
00260 for (; len < 14; len++)
00261 avio_w8(out, 0);
00262
00263 (*entries)++;
00264 }
00265 }
00266
00267 static void output_match(AVIOContext *out, int match_sample,
00268 int match_offset, int match_len, int *entries)
00269 {
00270 avio_w8(out, 2);
00271 avio_w8(out, 0);
00272 avio_wb16(out, match_len);
00273 avio_wb32(out, match_sample);
00274 avio_wb32(out, match_offset);
00275 avio_wb16(out, 1);
00276 avio_wb16(out, 1);
00277 (*entries)++;
00278 }
00279
00280 static void describe_payload(const uint8_t *data, int size,
00281 AVIOContext *out, int *entries,
00282 HintSampleQueue *queue)
00283 {
00284
00285 while (size > 0) {
00286 int match_sample, match_offset, match_len, pos;
00287 if (find_sample_match(data, size, queue, &pos, &match_sample,
00288 &match_offset, &match_len) < 0)
00289 break;
00290 output_immediate(data, pos, out, entries);
00291 data += pos;
00292 size -= pos;
00293 output_match(out, match_sample, match_offset, match_len, entries);
00294 data += match_len;
00295 size -= match_len;
00296 }
00297 output_immediate(data, size, out, entries);
00298 }
00299
00312 static int write_hint_packets(AVIOContext *out, const uint8_t *data,
00313 int size, MOVTrack *trk, int64_t *pts)
00314 {
00315 int64_t curpos;
00316 int64_t count_pos, entries_pos;
00317 int count = 0, entries;
00318
00319 count_pos = avio_tell(out);
00320
00321 avio_wb16(out, 0);
00322 avio_wb16(out, 0);
00323
00324 while (size > 4) {
00325 uint32_t packet_len = AV_RB32(data);
00326 uint16_t seq;
00327 uint32_t ts;
00328
00329 data += 4;
00330 size -= 4;
00331 if (packet_len > size || packet_len <= 12)
00332 break;
00333 if (data[1] >= 200 && data[1] <= 204) {
00334
00335 data += packet_len;
00336 size -= packet_len;
00337 continue;
00338 }
00339
00340 if (packet_len > trk->max_packet_size)
00341 trk->max_packet_size = packet_len;
00342
00343 seq = AV_RB16(&data[2]);
00344 ts = AV_RB32(&data[4]);
00345
00346 if (trk->prev_rtp_ts == 0)
00347 trk->prev_rtp_ts = ts;
00348
00349
00350 trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts);
00351 trk->prev_rtp_ts = ts;
00352 if (*pts == AV_NOPTS_VALUE)
00353 *pts = trk->cur_rtp_ts_unwrapped;
00354
00355 count++;
00356
00357 avio_wb32(out, 0);
00358 avio_write(out, data, 2);
00359 avio_wb16(out, seq);
00360 avio_wb16(out, 0);
00361 entries_pos = avio_tell(out);
00362 avio_wb16(out, 0);
00363
00364 data += 12;
00365 size -= 12;
00366 packet_len -= 12;
00367
00368 entries = 0;
00369
00370 describe_payload(data, packet_len, out, &entries, &trk->sample_queue);
00371 data += packet_len;
00372 size -= packet_len;
00373
00374 curpos = avio_tell(out);
00375 avio_seek(out, entries_pos, SEEK_SET);
00376 avio_wb16(out, entries);
00377 avio_seek(out, curpos, SEEK_SET);
00378 }
00379
00380 curpos = avio_tell(out);
00381 avio_seek(out, count_pos, SEEK_SET);
00382 avio_wb16(out, count);
00383 avio_seek(out, curpos, SEEK_SET);
00384 return count;
00385 }
00386
00387 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
00388 int track_index, int sample)
00389 {
00390 MOVMuxContext *mov = s->priv_data;
00391 MOVTrack *trk = &mov->tracks[track_index];
00392 AVFormatContext *rtp_ctx = trk->rtp_ctx;
00393 uint8_t *buf = NULL;
00394 int size;
00395 AVIOContext *hintbuf = NULL;
00396 AVPacket hint_pkt;
00397 int ret = 0, count;
00398
00399 if (!rtp_ctx)
00400 return AVERROR(ENOENT);
00401 if (!rtp_ctx->pb)
00402 return AVERROR(ENOMEM);
00403
00404 sample_queue_push(&trk->sample_queue, pkt, sample);
00405
00406
00407 ff_write_chained(rtp_ctx, 0, pkt, s);
00408
00409
00410
00411 size = url_close_dyn_buf(rtp_ctx->pb, &buf);
00412 if ((ret = url_open_dyn_packet_buf(&rtp_ctx->pb,
00413 RTP_MAX_PACKET_SIZE)) < 0)
00414 goto done;
00415
00416 if (size <= 0)
00417 goto done;
00418
00419
00420 if ((ret = url_open_dyn_buf(&hintbuf)) < 0)
00421 goto done;
00422 av_init_packet(&hint_pkt);
00423 count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts);
00424 av_freep(&buf);
00425
00426
00427 hint_pkt.size = size = url_close_dyn_buf(hintbuf, &buf);
00428 hint_pkt.data = buf;
00429 hint_pkt.pts = hint_pkt.dts;
00430 hint_pkt.stream_index = track_index;
00431 if (pkt->flags & AV_PKT_FLAG_KEY)
00432 hint_pkt.flags |= AV_PKT_FLAG_KEY;
00433 if (count > 0)
00434 ff_mov_write_packet(s, &hint_pkt);
00435 done:
00436 av_free(buf);
00437 sample_queue_retain(&trk->sample_queue);
00438 return ret;
00439 }
00440
00441 void ff_mov_close_hinting(MOVTrack *track) {
00442 AVFormatContext* rtp_ctx = track->rtp_ctx;
00443 uint8_t *ptr;
00444
00445 av_freep(&track->enc);
00446 sample_queue_free(&track->sample_queue);
00447 if (!rtp_ctx)
00448 return;
00449 if (rtp_ctx->pb) {
00450 av_write_trailer(rtp_ctx);
00451 url_close_dyn_buf(rtp_ctx->pb, &ptr);
00452 av_free(ptr);
00453 }
00454 avformat_free_context(rtp_ctx);
00455 }
00456