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