You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

551 lines
19KB

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