seek.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * seek utility functions for use within format handlers
  3. *
  4. * Copyright (c) 2009 Ivan Schreter
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "seek.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/mem.h"
  25. #include "internal.h"
  26. // NOTE: implementation should be moved here in another patch, to keep patches
  27. // separated.
  28. /**
  29. * helper structure describing keyframe search state of one stream
  30. */
  31. typedef struct {
  32. int64_t pos_lo; ///< position of the frame with low timestamp in file or INT64_MAX if not found (yet)
  33. int64_t ts_lo; ///< frame presentation timestamp or same as pos_lo for byte seeking
  34. int64_t pos_hi; ///< position of the frame with high timestamp in file or INT64_MAX if not found (yet)
  35. int64_t ts_hi; ///< frame presentation timestamp or same as pos_hi for byte seeking
  36. int64_t last_pos; ///< last known position of a frame, for multi-frame packets
  37. int64_t term_ts; ///< termination timestamp (which TS we already read)
  38. AVRational term_ts_tb; ///< timebase for term_ts
  39. int64_t first_ts; ///< first packet timestamp in this iteration (to fill term_ts later)
  40. AVRational first_ts_tb; ///< timebase for first_ts
  41. int terminated; ///< termination flag for the current iteration
  42. } AVSyncPoint;
  43. /**
  44. * Compute a distance between timestamps.
  45. *
  46. * Distances are only comparable, if same time bases are used for computing
  47. * distances.
  48. *
  49. * @param ts_hi high timestamp
  50. * @param tb_hi high timestamp time base
  51. * @param ts_lo low timestamp
  52. * @param tb_lo low timestamp time base
  53. * @return representation of distance between high and low timestamps
  54. */
  55. static int64_t ts_distance(int64_t ts_hi,
  56. AVRational tb_hi,
  57. int64_t ts_lo,
  58. AVRational tb_lo)
  59. {
  60. int64_t hi, lo;
  61. hi = ts_hi * tb_hi.num * tb_lo.den;
  62. lo = ts_lo * tb_lo.num * tb_hi.den;
  63. return hi - lo;
  64. }
  65. /**
  66. * Partial search for keyframes in multiple streams.
  67. *
  68. * This routine searches in each stream for the next lower and the next higher
  69. * timestamp compared to the given target timestamp. The search starts at the current
  70. * file position and ends at the file position, where all streams have already been
  71. * examined (or when all higher key frames are found in the first iteration).
  72. *
  73. * This routine is called iteratively with an exponential backoff to find the lower
  74. * timestamp.
  75. *
  76. * @param s format context
  77. * @param timestamp target timestamp (or position, if AVSEEK_FLAG_BYTE)
  78. * @param timebase time base for timestamps
  79. * @param flags seeking flags
  80. * @param sync array with information per stream
  81. * @param keyframes_to_find count of keyframes to find in total
  82. * @param found_lo ptr to the count of already found low timestamp keyframes
  83. * @param found_hi ptr to the count of already found high timestamp keyframes
  84. * @param first_iter flag for first iteration
  85. */
  86. static void search_hi_lo_keyframes(AVFormatContext *s,
  87. int64_t timestamp,
  88. AVRational timebase,
  89. int flags,
  90. AVSyncPoint *sync,
  91. int keyframes_to_find,
  92. int *found_lo,
  93. int *found_hi,
  94. int first_iter)
  95. {
  96. AVPacket pkt;
  97. AVSyncPoint *sp;
  98. AVStream *st;
  99. int idx;
  100. int flg;
  101. int terminated_count = 0;
  102. int64_t pos;
  103. int64_t pts, dts; // PTS/DTS from stream
  104. int64_t ts; // PTS in stream-local time base or position for byte seeking
  105. AVRational ts_tb; // Time base of the stream or 1:1 for byte seeking
  106. for (;;) {
  107. if (av_read_frame(s, &pkt) < 0) {
  108. // EOF or error, make sure high flags are set
  109. for (idx = 0; idx < s->nb_streams; ++idx) {
  110. if (s->streams[idx]->discard < AVDISCARD_ALL) {
  111. sp = &sync[idx];
  112. if (sp->pos_hi == INT64_MAX) {
  113. // no high frame exists for this stream
  114. (*found_hi)++;
  115. sp->ts_hi = INT64_MAX;
  116. sp->pos_hi = INT64_MAX - 1;
  117. }
  118. }
  119. }
  120. break;
  121. }
  122. idx = pkt.stream_index;
  123. st = s->streams[idx];
  124. if (st->discard >= AVDISCARD_ALL)
  125. // this stream is not active, skip packet
  126. continue;
  127. sp = &sync[idx];
  128. flg = pkt.flags;
  129. pos = pkt.pos;
  130. pts = pkt.pts;
  131. dts = pkt.dts;
  132. if (pts == AV_NOPTS_VALUE)
  133. // some formats don't provide PTS, only DTS
  134. pts = dts;
  135. av_free_packet(&pkt);
  136. // Multi-frame packets only return position for the very first frame.
  137. // Other frames are read with position == -1. Therefore, we note down
  138. // last known position of a frame and use it if a frame without
  139. // position arrives. In this way, it's possible to seek to proper
  140. // position. Additionally, for parsers not providing position at all,
  141. // an approximation will be used (starting position of this iteration).
  142. if (pos < 0)
  143. pos = sp->last_pos;
  144. else
  145. sp->last_pos = pos;
  146. // Evaluate key frames with known TS (or any frames, if AVSEEK_FLAG_ANY set).
  147. if (pts != AV_NOPTS_VALUE &&
  148. ((flg & AV_PKT_FLAG_KEY) || (flags & AVSEEK_FLAG_ANY))) {
  149. if (flags & AVSEEK_FLAG_BYTE) {
  150. // for byte seeking, use position as timestamp
  151. ts = pos;
  152. ts_tb.num = 1;
  153. ts_tb.den = 1;
  154. } else {
  155. // otherwise, get stream time_base
  156. ts = pts;
  157. ts_tb = st->time_base;
  158. }
  159. if (sp->first_ts == AV_NOPTS_VALUE) {
  160. // Note down termination timestamp for the next iteration - when
  161. // we encounter a packet with the same timestamp, we will ignore
  162. // any further packets for this stream in next iteration (as they
  163. // are already evaluated).
  164. sp->first_ts = ts;
  165. sp->first_ts_tb = ts_tb;
  166. }
  167. if (sp->term_ts != AV_NOPTS_VALUE &&
  168. av_compare_ts(ts, ts_tb, sp->term_ts, sp->term_ts_tb) > 0) {
  169. // past the end position from last iteration, ignore packet
  170. if (!sp->terminated) {
  171. sp->terminated = 1;
  172. ++terminated_count;
  173. if (sp->pos_hi == INT64_MAX) {
  174. // no high frame exists for this stream
  175. (*found_hi)++;
  176. sp->ts_hi = INT64_MAX;
  177. sp->pos_hi = INT64_MAX - 1;
  178. }
  179. if (terminated_count == keyframes_to_find)
  180. break; // all terminated, iteration done
  181. }
  182. continue;
  183. }
  184. if (av_compare_ts(ts, ts_tb, timestamp, timebase) <= 0) {
  185. // keyframe found before target timestamp
  186. if (sp->pos_lo == INT64_MAX) {
  187. // found first keyframe lower than target timestamp
  188. (*found_lo)++;
  189. sp->ts_lo = ts;
  190. sp->pos_lo = pos;
  191. } else if (sp->ts_lo < ts) {
  192. // found a better match (closer to target timestamp)
  193. sp->ts_lo = ts;
  194. sp->pos_lo = pos;
  195. }
  196. }
  197. if (av_compare_ts(ts, ts_tb, timestamp, timebase) >= 0) {
  198. // keyframe found after target timestamp
  199. if (sp->pos_hi == INT64_MAX) {
  200. // found first keyframe higher than target timestamp
  201. (*found_hi)++;
  202. sp->ts_hi = ts;
  203. sp->pos_hi = pos;
  204. if (*found_hi >= keyframes_to_find && first_iter) {
  205. // We found high frame for all. They may get updated
  206. // to TS closer to target TS in later iterations (which
  207. // will stop at start position of previous iteration).
  208. break;
  209. }
  210. } else if (sp->ts_hi > ts) {
  211. // found a better match (actually, shouldn't happen)
  212. sp->ts_hi = ts;
  213. sp->pos_hi = pos;
  214. }
  215. }
  216. }
  217. }
  218. // Clean up the parser.
  219. ff_read_frame_flush(s);
  220. }
  221. int64_t ff_gen_syncpoint_search(AVFormatContext *s,
  222. int stream_index,
  223. int64_t pos,
  224. int64_t ts_min,
  225. int64_t ts,
  226. int64_t ts_max,
  227. int flags)
  228. {
  229. AVSyncPoint *sync, *sp;
  230. AVStream *st;
  231. int i;
  232. int keyframes_to_find = 0;
  233. int64_t curpos;
  234. int64_t step;
  235. int found_lo = 0, found_hi = 0;
  236. int64_t min_distance, distance;
  237. int64_t min_pos = 0;
  238. int first_iter = 1;
  239. AVRational time_base;
  240. if (flags & AVSEEK_FLAG_BYTE) {
  241. // for byte seeking, we have exact 1:1 "timestamps" - positions
  242. time_base.num = 1;
  243. time_base.den = 1;
  244. } else {
  245. if (stream_index >= 0) {
  246. // we have a reference stream, which time base we use
  247. st = s->streams[stream_index];
  248. time_base = st->time_base;
  249. } else {
  250. // no reference stream, use AV_TIME_BASE as reference time base
  251. time_base.num = 1;
  252. time_base.den = AV_TIME_BASE;
  253. }
  254. }
  255. // Initialize syncpoint structures for each stream.
  256. sync = av_malloc(s->nb_streams * sizeof(AVSyncPoint));
  257. if (!sync)
  258. // cannot allocate helper structure
  259. return -1;
  260. for (i = 0; i < s->nb_streams; ++i) {
  261. st = s->streams[i];
  262. sp = &sync[i];
  263. sp->pos_lo = INT64_MAX;
  264. sp->ts_lo = INT64_MAX;
  265. sp->pos_hi = INT64_MAX;
  266. sp->ts_hi = INT64_MAX;
  267. sp->terminated = 0;
  268. sp->first_ts = AV_NOPTS_VALUE;
  269. sp->term_ts = ts_max;
  270. sp->term_ts_tb = time_base;
  271. sp->last_pos = pos;
  272. st->cur_dts = AV_NOPTS_VALUE;
  273. if (st->discard < AVDISCARD_ALL)
  274. ++keyframes_to_find;
  275. }
  276. if (!keyframes_to_find) {
  277. // no stream active, error
  278. av_free(sync);
  279. return -1;
  280. }
  281. // Find keyframes in all active streams with timestamp/position just before
  282. // and just after requested timestamp/position.
  283. step = s->pb->buffer_size;
  284. curpos = FFMAX(pos - step / 2, 0);
  285. for (;;) {
  286. avio_seek(s->pb, curpos, SEEK_SET);
  287. search_hi_lo_keyframes(s,
  288. ts, time_base,
  289. flags,
  290. sync,
  291. keyframes_to_find,
  292. &found_lo, &found_hi,
  293. first_iter);
  294. if (found_lo == keyframes_to_find && found_hi == keyframes_to_find)
  295. break; // have all keyframes we wanted
  296. if (!curpos)
  297. break; // cannot go back anymore
  298. curpos = pos - step;
  299. if (curpos < 0)
  300. curpos = 0;
  301. step *= 2;
  302. // switch termination positions
  303. for (i = 0; i < s->nb_streams; ++i) {
  304. st = s->streams[i];
  305. st->cur_dts = AV_NOPTS_VALUE;
  306. sp = &sync[i];
  307. if (sp->first_ts != AV_NOPTS_VALUE) {
  308. sp->term_ts = sp->first_ts;
  309. sp->term_ts_tb = sp->first_ts_tb;
  310. sp->first_ts = AV_NOPTS_VALUE;
  311. }
  312. sp->terminated = 0;
  313. sp->last_pos = curpos;
  314. }
  315. first_iter = 0;
  316. }
  317. // Find actual position to start decoding so that decoder synchronizes
  318. // closest to ts and between ts_min and ts_max.
  319. pos = INT64_MAX;
  320. for (i = 0; i < s->nb_streams; ++i) {
  321. st = s->streams[i];
  322. if (st->discard < AVDISCARD_ALL) {
  323. sp = &sync[i];
  324. min_distance = INT64_MAX;
  325. // Find timestamp closest to requested timestamp within min/max limits.
  326. if (sp->pos_lo != INT64_MAX
  327. && av_compare_ts(ts_min, time_base, sp->ts_lo, st->time_base) <= 0
  328. && av_compare_ts(sp->ts_lo, st->time_base, ts_max, time_base) <= 0) {
  329. // low timestamp is in range
  330. min_distance = ts_distance(ts, time_base, sp->ts_lo, st->time_base);
  331. min_pos = sp->pos_lo;
  332. }
  333. if (sp->pos_hi != INT64_MAX
  334. && av_compare_ts(ts_min, time_base, sp->ts_hi, st->time_base) <= 0
  335. && av_compare_ts(sp->ts_hi, st->time_base, ts_max, time_base) <= 0) {
  336. // high timestamp is in range, check distance
  337. distance = ts_distance(sp->ts_hi, st->time_base, ts, time_base);
  338. if (distance < min_distance) {
  339. min_distance = distance;
  340. min_pos = sp->pos_hi;
  341. }
  342. }
  343. if (min_distance == INT64_MAX) {
  344. // no timestamp is in range, cannot seek
  345. av_free(sync);
  346. return -1;
  347. }
  348. if (min_pos < pos)
  349. pos = min_pos;
  350. }
  351. }
  352. avio_seek(s->pb, pos, SEEK_SET);
  353. av_free(sync);
  354. return pos;
  355. }
  356. AVParserState *ff_store_parser_state(AVFormatContext *s)
  357. {
  358. int i;
  359. AVStream *st;
  360. AVParserStreamState *ss;
  361. AVParserState *state = av_malloc(sizeof(AVParserState));
  362. if (!state)
  363. return NULL;
  364. state->stream_states = av_malloc(sizeof(AVParserStreamState) * s->nb_streams);
  365. if (!state->stream_states) {
  366. av_free(state);
  367. return NULL;
  368. }
  369. state->fpos = avio_tell(s->pb);
  370. // copy context structures
  371. state->cur_st = s->cur_st;
  372. state->packet_buffer = s->packet_buffer;
  373. state->raw_packet_buffer = s->raw_packet_buffer;
  374. state->raw_packet_buffer_remaining_size = s->raw_packet_buffer_remaining_size;
  375. s->cur_st = NULL;
  376. s->packet_buffer = NULL;
  377. s->raw_packet_buffer = NULL;
  378. s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
  379. // copy stream structures
  380. state->nb_streams = s->nb_streams;
  381. for (i = 0; i < s->nb_streams; i++) {
  382. st = s->streams[i];
  383. ss = &state->stream_states[i];
  384. ss->parser = st->parser;
  385. ss->last_IP_pts = st->last_IP_pts;
  386. ss->cur_dts = st->cur_dts;
  387. ss->reference_dts = st->reference_dts;
  388. ss->cur_ptr = st->cur_ptr;
  389. ss->cur_len = st->cur_len;
  390. ss->probe_packets = st->probe_packets;
  391. ss->cur_pkt = st->cur_pkt;
  392. st->parser = NULL;
  393. st->last_IP_pts = AV_NOPTS_VALUE;
  394. st->cur_dts = AV_NOPTS_VALUE;
  395. st->reference_dts = AV_NOPTS_VALUE;
  396. st->cur_ptr = NULL;
  397. st->cur_len = 0;
  398. st->probe_packets = MAX_PROBE_PACKETS;
  399. av_init_packet(&st->cur_pkt);
  400. }
  401. return state;
  402. }
  403. void ff_restore_parser_state(AVFormatContext *s, AVParserState *state)
  404. {
  405. int i;
  406. AVStream *st;
  407. AVParserStreamState *ss;
  408. ff_read_frame_flush(s);
  409. if (!state)
  410. return;
  411. avio_seek(s->pb, state->fpos, SEEK_SET);
  412. // copy context structures
  413. s->cur_st = state->cur_st;
  414. s->packet_buffer = state->packet_buffer;
  415. s->raw_packet_buffer = state->raw_packet_buffer;
  416. s->raw_packet_buffer_remaining_size = state->raw_packet_buffer_remaining_size;
  417. // copy stream structures
  418. for (i = 0; i < state->nb_streams; i++) {
  419. st = s->streams[i];
  420. ss = &state->stream_states[i];
  421. st->parser = ss->parser;
  422. st->last_IP_pts = ss->last_IP_pts;
  423. st->cur_dts = ss->cur_dts;
  424. st->reference_dts = ss->reference_dts;
  425. st->cur_ptr = ss->cur_ptr;
  426. st->cur_len = ss->cur_len;
  427. st->probe_packets = ss->probe_packets;
  428. st->cur_pkt = ss->cur_pkt;
  429. }
  430. av_free(state->stream_states);
  431. av_free(state);
  432. }
  433. static void free_packet_list(AVPacketList *pktl)
  434. {
  435. AVPacketList *cur;
  436. while (pktl) {
  437. cur = pktl;
  438. pktl = cur->next;
  439. av_free_packet(&cur->pkt);
  440. av_free(cur);
  441. }
  442. }
  443. void ff_free_parser_state(AVFormatContext *s, AVParserState *state)
  444. {
  445. int i;
  446. AVParserStreamState *ss;
  447. if (!state)
  448. return;
  449. for (i = 0; i < state->nb_streams; i++) {
  450. ss = &state->stream_states[i];
  451. if (ss->parser)
  452. av_parser_close(ss->parser);
  453. av_free_packet(&ss->cur_pkt);
  454. }
  455. free_packet_list(state->packet_buffer);
  456. free_packet_list(state->raw_packet_buffer);
  457. av_free(state->stream_states);
  458. av_free(state);
  459. }