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.

655 lines
21KB

  1. /*
  2. * FFM (ffserver live feed) demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  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. #include <stdint.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/intfloat.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "ffm.h"
  27. #include "avio_internal.h"
  28. static int ffm_is_avail_data(AVFormatContext *s, int size)
  29. {
  30. FFMContext *ffm = s->priv_data;
  31. int64_t pos, avail_size;
  32. int len;
  33. len = ffm->packet_end - ffm->packet_ptr;
  34. if (size <= len)
  35. return 1;
  36. pos = avio_tell(s->pb);
  37. if (!ffm->write_index) {
  38. if (pos == ffm->file_size)
  39. return AVERROR_EOF;
  40. avail_size = ffm->file_size - pos;
  41. } else {
  42. if (pos == ffm->write_index) {
  43. /* exactly at the end of stream */
  44. return AVERROR(EAGAIN);
  45. } else if (pos < ffm->write_index) {
  46. avail_size = ffm->write_index - pos;
  47. } else {
  48. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  49. }
  50. }
  51. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  52. if (size <= avail_size)
  53. return 1;
  54. else
  55. return AVERROR(EAGAIN);
  56. }
  57. static int ffm_resync(AVFormatContext *s, int state)
  58. {
  59. av_log(s, AV_LOG_ERROR, "resyncing\n");
  60. while (state != PACKET_ID) {
  61. if (avio_feof(s->pb)) {
  62. av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
  63. return -1;
  64. }
  65. state = (state << 8) | avio_r8(s->pb);
  66. }
  67. return 0;
  68. }
  69. /* first is true if we read the frame header */
  70. static int ffm_read_data(AVFormatContext *s,
  71. uint8_t *buf, int size, int header)
  72. {
  73. FFMContext *ffm = s->priv_data;
  74. AVIOContext *pb = s->pb;
  75. int len, fill_size, size1, frame_offset, id;
  76. size1 = size;
  77. while (size > 0) {
  78. redo:
  79. len = ffm->packet_end - ffm->packet_ptr;
  80. if (len < 0)
  81. return -1;
  82. if (len > size)
  83. len = size;
  84. if (len == 0) {
  85. if (avio_tell(pb) == ffm->file_size)
  86. avio_seek(pb, ffm->packet_size, SEEK_SET);
  87. retry_read:
  88. if (pb->buffer_size != ffm->packet_size) {
  89. int64_t tell = avio_tell(pb);
  90. ffio_set_buf_size(pb, ffm->packet_size);
  91. avio_seek(pb, tell, SEEK_SET);
  92. }
  93. id = avio_rb16(pb); /* PACKET_ID */
  94. if (id != PACKET_ID)
  95. if (ffm_resync(s, id) < 0)
  96. return -1;
  97. fill_size = avio_rb16(pb);
  98. ffm->dts = avio_rb64(pb);
  99. frame_offset = avio_rb16(pb);
  100. avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  101. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  102. if (ffm->packet_end < ffm->packet || frame_offset < 0)
  103. return -1;
  104. /* if first packet or resynchronization packet, we must
  105. handle it specifically */
  106. if (ffm->first_packet || (frame_offset & 0x8000)) {
  107. if (!frame_offset) {
  108. /* This packet has no frame headers in it */
  109. if (avio_tell(pb) >= ffm->packet_size * 3LL) {
  110. avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR);
  111. goto retry_read;
  112. }
  113. /* This is bad, we cannot find a valid frame header */
  114. return 0;
  115. }
  116. ffm->first_packet = 0;
  117. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
  118. return -1;
  119. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  120. if (!header)
  121. break;
  122. } else {
  123. ffm->packet_ptr = ffm->packet;
  124. }
  125. goto redo;
  126. }
  127. memcpy(buf, ffm->packet_ptr, len);
  128. buf += len;
  129. ffm->packet_ptr += len;
  130. size -= len;
  131. header = 0;
  132. }
  133. return size1 - size;
  134. }
  135. /* ensure that acutal seeking happens between FFM_PACKET_SIZE
  136. and file_size - FFM_PACKET_SIZE */
  137. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  138. {
  139. FFMContext *ffm = s->priv_data;
  140. AVIOContext *pb = s->pb;
  141. int64_t pos;
  142. pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  143. pos = FFMAX(pos, FFM_PACKET_SIZE);
  144. av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  145. return avio_seek(pb, pos, SEEK_SET);
  146. }
  147. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  148. {
  149. AVIOContext *pb = s->pb;
  150. int64_t dts;
  151. ffm_seek1(s, pos);
  152. avio_skip(pb, 4);
  153. dts = avio_rb64(pb);
  154. av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  155. return dts;
  156. }
  157. static void adjust_write_index(AVFormatContext *s)
  158. {
  159. FFMContext *ffm = s->priv_data;
  160. AVIOContext *pb = s->pb;
  161. int64_t pts;
  162. //int64_t orig_write_index = ffm->write_index;
  163. int64_t pos_min, pos_max;
  164. int64_t pts_start;
  165. int64_t ptr = avio_tell(pb);
  166. pos_min = 0;
  167. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  168. pts_start = get_dts(s, pos_min);
  169. pts = get_dts(s, pos_max);
  170. if (pts - 100000 > pts_start)
  171. goto end;
  172. ffm->write_index = FFM_PACKET_SIZE;
  173. pts_start = get_dts(s, pos_min);
  174. pts = get_dts(s, pos_max);
  175. if (pts - 100000 <= pts_start) {
  176. while (1) {
  177. int64_t newpos;
  178. int64_t newpts;
  179. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  180. if (newpos == pos_min)
  181. break;
  182. newpts = get_dts(s, newpos);
  183. if (newpts - 100000 <= pts) {
  184. pos_max = newpos;
  185. pts = newpts;
  186. } else {
  187. pos_min = newpos;
  188. }
  189. }
  190. ffm->write_index += pos_max;
  191. }
  192. end:
  193. avio_seek(pb, ptr, SEEK_SET);
  194. }
  195. static int ffm_close(AVFormatContext *s)
  196. {
  197. int i;
  198. for (i = 0; i < s->nb_streams; i++)
  199. av_freep(&s->streams[i]->codec->rc_eq);
  200. return 0;
  201. }
  202. static int ffm2_read_header(AVFormatContext *s)
  203. {
  204. FFMContext *ffm = s->priv_data;
  205. AVStream *st;
  206. AVIOContext *pb = s->pb;
  207. AVCodecContext *codec;
  208. int ret;
  209. ffm->packet_size = avio_rb32(pb);
  210. if (ffm->packet_size != FFM_PACKET_SIZE) {
  211. av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  212. ffm->packet_size, FFM_PACKET_SIZE);
  213. ret = AVERROR_INVALIDDATA;
  214. goto fail;
  215. }
  216. ffm->write_index = avio_rb64(pb);
  217. /* get also filesize */
  218. if (pb->seekable) {
  219. ffm->file_size = avio_size(pb);
  220. if (ffm->write_index && 0)
  221. adjust_write_index(s);
  222. } else {
  223. ffm->file_size = (UINT64_C(1) << 63) - 1;
  224. }
  225. while(!avio_feof(pb)) {
  226. unsigned id = avio_rb32(pb);
  227. unsigned size = avio_rb32(pb);
  228. int64_t next = avio_tell(pb) + size;
  229. char rc_eq_buf[128];
  230. if(!id)
  231. break;
  232. switch(id) {
  233. case MKBETAG('M', 'A', 'I', 'N'):
  234. avio_rb32(pb); /* nb_streams */
  235. avio_rb32(pb); /* total bitrate */
  236. break;
  237. case MKBETAG('C', 'O', 'M', 'M'):
  238. st = avformat_new_stream(s, NULL);
  239. if (!st) {
  240. ret = AVERROR(ENOMEM);
  241. goto fail;
  242. }
  243. avpriv_set_pts_info(st, 64, 1, 1000000);
  244. codec = st->codec;
  245. /* generic info */
  246. codec->codec_id = avio_rb32(pb);
  247. codec->codec_type = avio_r8(pb);
  248. codec->bit_rate = avio_rb32(pb);
  249. codec->flags = avio_rb32(pb);
  250. codec->flags2 = avio_rb32(pb);
  251. codec->debug = avio_rb32(pb);
  252. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  253. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  254. return AVERROR(ENOMEM);
  255. }
  256. avio_seek(pb, next, SEEK_SET);
  257. id = avio_rb32(pb);
  258. size = avio_rb32(pb);
  259. next = avio_tell(pb) + size;
  260. switch(id) {
  261. case MKBETAG('S', 'T', 'V', 'I'):
  262. codec->time_base.num = avio_rb32(pb);
  263. codec->time_base.den = avio_rb32(pb);
  264. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  265. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  266. codec->time_base.num, codec->time_base.den);
  267. ret = AVERROR_INVALIDDATA;
  268. goto fail;
  269. }
  270. codec->width = avio_rb16(pb);
  271. codec->height = avio_rb16(pb);
  272. codec->gop_size = avio_rb16(pb);
  273. codec->pix_fmt = avio_rb32(pb);
  274. codec->qmin = avio_r8(pb);
  275. codec->qmax = avio_r8(pb);
  276. codec->max_qdiff = avio_r8(pb);
  277. codec->qcompress = avio_rb16(pb) / 10000.0;
  278. codec->qblur = avio_rb16(pb) / 10000.0;
  279. codec->bit_rate_tolerance = avio_rb32(pb);
  280. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  281. codec->rc_eq = av_strdup(rc_eq_buf);
  282. codec->rc_max_rate = avio_rb32(pb);
  283. codec->rc_min_rate = avio_rb32(pb);
  284. codec->rc_buffer_size = avio_rb32(pb);
  285. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  286. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  287. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  288. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  289. codec->dct_algo = avio_rb32(pb);
  290. codec->strict_std_compliance = avio_rb32(pb);
  291. codec->max_b_frames = avio_rb32(pb);
  292. codec->mpeg_quant = avio_rb32(pb);
  293. codec->intra_dc_precision = avio_rb32(pb);
  294. codec->me_method = avio_rb32(pb);
  295. codec->mb_decision = avio_rb32(pb);
  296. codec->nsse_weight = avio_rb32(pb);
  297. codec->frame_skip_cmp = avio_rb32(pb);
  298. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  299. codec->codec_tag = avio_rb32(pb);
  300. codec->thread_count = avio_r8(pb);
  301. codec->coder_type = avio_rb32(pb);
  302. codec->me_cmp = avio_rb32(pb);
  303. codec->me_subpel_quality = avio_rb32(pb);
  304. codec->me_range = avio_rb32(pb);
  305. codec->keyint_min = avio_rb32(pb);
  306. codec->scenechange_threshold = avio_rb32(pb);
  307. codec->b_frame_strategy = avio_rb32(pb);
  308. codec->qcompress = av_int2double(avio_rb64(pb));
  309. codec->qblur = av_int2double(avio_rb64(pb));
  310. codec->max_qdiff = avio_rb32(pb);
  311. codec->refs = avio_rb32(pb);
  312. break;
  313. case MKBETAG('S', 'T', 'A', 'U'):
  314. codec->sample_rate = avio_rb32(pb);
  315. codec->channels = avio_rl16(pb);
  316. codec->frame_size = avio_rl16(pb);
  317. break;
  318. }
  319. break;
  320. }
  321. avio_seek(pb, next, SEEK_SET);
  322. }
  323. /* get until end of block reached */
  324. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  325. avio_r8(pb);
  326. /* init packet demux */
  327. ffm->packet_ptr = ffm->packet;
  328. ffm->packet_end = ffm->packet;
  329. ffm->frame_offset = 0;
  330. ffm->dts = 0;
  331. ffm->read_state = READ_HEADER;
  332. ffm->first_packet = 1;
  333. return 0;
  334. fail:
  335. ffm_close(s);
  336. return ret;
  337. }
  338. static int ffm_read_header(AVFormatContext *s)
  339. {
  340. FFMContext *ffm = s->priv_data;
  341. AVStream *st;
  342. AVIOContext *pb = s->pb;
  343. AVCodecContext *codec;
  344. int i, nb_streams;
  345. uint32_t tag;
  346. /* header */
  347. tag = avio_rl32(pb);
  348. if (tag == MKTAG('F', 'F', 'M', '2'))
  349. return ffm2_read_header(s);
  350. if (tag != MKTAG('F', 'F', 'M', '1'))
  351. goto fail;
  352. ffm->packet_size = avio_rb32(pb);
  353. if (ffm->packet_size != FFM_PACKET_SIZE)
  354. goto fail;
  355. ffm->write_index = avio_rb64(pb);
  356. /* get also filesize */
  357. if (pb->seekable) {
  358. ffm->file_size = avio_size(pb);
  359. if (ffm->write_index && 0)
  360. adjust_write_index(s);
  361. } else {
  362. ffm->file_size = (UINT64_C(1) << 63) - 1;
  363. }
  364. nb_streams = avio_rb32(pb);
  365. avio_rb32(pb); /* total bitrate */
  366. /* read each stream */
  367. for(i=0;i<nb_streams;i++) {
  368. char rc_eq_buf[128];
  369. st = avformat_new_stream(s, NULL);
  370. if (!st)
  371. goto fail;
  372. avpriv_set_pts_info(st, 64, 1, 1000000);
  373. codec = st->codec;
  374. /* generic info */
  375. codec->codec_id = avio_rb32(pb);
  376. codec->codec_type = avio_r8(pb); /* codec_type */
  377. codec->bit_rate = avio_rb32(pb);
  378. codec->flags = avio_rb32(pb);
  379. codec->flags2 = avio_rb32(pb);
  380. codec->debug = avio_rb32(pb);
  381. /* specific info */
  382. switch(codec->codec_type) {
  383. case AVMEDIA_TYPE_VIDEO:
  384. codec->time_base.num = avio_rb32(pb);
  385. codec->time_base.den = avio_rb32(pb);
  386. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  387. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  388. codec->time_base.num, codec->time_base.den);
  389. goto fail;
  390. }
  391. codec->width = avio_rb16(pb);
  392. codec->height = avio_rb16(pb);
  393. codec->gop_size = avio_rb16(pb);
  394. codec->pix_fmt = avio_rb32(pb);
  395. codec->qmin = avio_r8(pb);
  396. codec->qmax = avio_r8(pb);
  397. codec->max_qdiff = avio_r8(pb);
  398. codec->qcompress = avio_rb16(pb) / 10000.0;
  399. codec->qblur = avio_rb16(pb) / 10000.0;
  400. codec->bit_rate_tolerance = avio_rb32(pb);
  401. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  402. codec->rc_eq = av_strdup(rc_eq_buf);
  403. codec->rc_max_rate = avio_rb32(pb);
  404. codec->rc_min_rate = avio_rb32(pb);
  405. codec->rc_buffer_size = avio_rb32(pb);
  406. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  407. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  408. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  409. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  410. codec->dct_algo = avio_rb32(pb);
  411. codec->strict_std_compliance = avio_rb32(pb);
  412. codec->max_b_frames = avio_rb32(pb);
  413. codec->mpeg_quant = avio_rb32(pb);
  414. codec->intra_dc_precision = avio_rb32(pb);
  415. codec->me_method = avio_rb32(pb);
  416. codec->mb_decision = avio_rb32(pb);
  417. codec->nsse_weight = avio_rb32(pb);
  418. codec->frame_skip_cmp = avio_rb32(pb);
  419. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  420. codec->codec_tag = avio_rb32(pb);
  421. codec->thread_count = avio_r8(pb);
  422. codec->coder_type = avio_rb32(pb);
  423. codec->me_cmp = avio_rb32(pb);
  424. codec->me_subpel_quality = avio_rb32(pb);
  425. codec->me_range = avio_rb32(pb);
  426. codec->keyint_min = avio_rb32(pb);
  427. codec->scenechange_threshold = avio_rb32(pb);
  428. codec->b_frame_strategy = avio_rb32(pb);
  429. codec->qcompress = av_int2double(avio_rb64(pb));
  430. codec->qblur = av_int2double(avio_rb64(pb));
  431. codec->max_qdiff = avio_rb32(pb);
  432. codec->refs = avio_rb32(pb);
  433. break;
  434. case AVMEDIA_TYPE_AUDIO:
  435. codec->sample_rate = avio_rb32(pb);
  436. codec->channels = avio_rl16(pb);
  437. codec->frame_size = avio_rl16(pb);
  438. break;
  439. default:
  440. goto fail;
  441. }
  442. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  443. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  444. return AVERROR(ENOMEM);
  445. }
  446. }
  447. /* get until end of block reached */
  448. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  449. avio_r8(pb);
  450. /* init packet demux */
  451. ffm->packet_ptr = ffm->packet;
  452. ffm->packet_end = ffm->packet;
  453. ffm->frame_offset = 0;
  454. ffm->dts = 0;
  455. ffm->read_state = READ_HEADER;
  456. ffm->first_packet = 1;
  457. return 0;
  458. fail:
  459. ffm_close(s);
  460. return -1;
  461. }
  462. /* return < 0 if eof */
  463. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  464. {
  465. int size;
  466. FFMContext *ffm = s->priv_data;
  467. int duration, ret;
  468. switch(ffm->read_state) {
  469. case READ_HEADER:
  470. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  471. return ret;
  472. av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  473. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  474. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  475. FRAME_HEADER_SIZE)
  476. return -1;
  477. if (ffm->header[1] & FLAG_DTS)
  478. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  479. return -1;
  480. ffm->read_state = READ_DATA;
  481. /* fall through */
  482. case READ_DATA:
  483. size = AV_RB24(ffm->header + 2);
  484. if ((ret = ffm_is_avail_data(s, size)) < 0)
  485. return ret;
  486. duration = AV_RB24(ffm->header + 5);
  487. if (av_new_packet(pkt, size) < 0) {
  488. return AVERROR(ENOMEM);
  489. }
  490. pkt->stream_index = ffm->header[0];
  491. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  492. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  493. av_free_packet(pkt);
  494. ffm->read_state = READ_HEADER;
  495. return -1;
  496. }
  497. pkt->pos = avio_tell(s->pb);
  498. if (ffm->header[1] & FLAG_KEY_FRAME)
  499. pkt->flags |= AV_PKT_FLAG_KEY;
  500. ffm->read_state = READ_HEADER;
  501. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  502. /* bad case: desynchronized packet. we cancel all the packet loading */
  503. av_free_packet(pkt);
  504. return -1;
  505. }
  506. pkt->pts = AV_RB64(ffm->header+8);
  507. if (ffm->header[1] & FLAG_DTS)
  508. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  509. else
  510. pkt->dts = pkt->pts;
  511. pkt->duration = duration;
  512. break;
  513. }
  514. return 0;
  515. }
  516. /* seek to a given time in the file. The file read pointer is
  517. positioned at or before pts. XXX: the following code is quite
  518. approximative */
  519. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  520. {
  521. FFMContext *ffm = s->priv_data;
  522. int64_t pos_min, pos_max, pos;
  523. int64_t pts_min, pts_max, pts;
  524. double pos1;
  525. av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  526. /* find the position using linear interpolation (better than
  527. dichotomy in typical cases) */
  528. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  529. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  530. pos_min = FFM_PACKET_SIZE;
  531. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  532. } else {
  533. pos_min = ffm->write_index;
  534. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  535. }
  536. } else {
  537. pos_min = FFM_PACKET_SIZE;
  538. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  539. }
  540. while (pos_min <= pos_max) {
  541. pts_min = get_dts(s, pos_min);
  542. pts_max = get_dts(s, pos_max);
  543. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  544. pos = pts_min > wanted_pts ? pos_min : pos_max;
  545. goto found;
  546. }
  547. /* linear interpolation */
  548. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  549. (double)(pts_max - pts_min);
  550. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  551. if (pos <= pos_min)
  552. pos = pos_min;
  553. else if (pos >= pos_max)
  554. pos = pos_max;
  555. pts = get_dts(s, pos);
  556. /* check if we are lucky */
  557. if (pts == wanted_pts) {
  558. goto found;
  559. } else if (pts > wanted_pts) {
  560. pos_max = pos - FFM_PACKET_SIZE;
  561. } else {
  562. pos_min = pos + FFM_PACKET_SIZE;
  563. }
  564. }
  565. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  566. found:
  567. if (ffm_seek1(s, pos) < 0)
  568. return -1;
  569. /* reset read state */
  570. ffm->read_state = READ_HEADER;
  571. ffm->packet_ptr = ffm->packet;
  572. ffm->packet_end = ffm->packet;
  573. ffm->first_packet = 1;
  574. return 0;
  575. }
  576. static int ffm_probe(AVProbeData *p)
  577. {
  578. if (
  579. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  580. (p->buf[3] == '1' || p->buf[3] == '2'))
  581. return AVPROBE_SCORE_MAX + 1;
  582. return 0;
  583. }
  584. AVInputFormat ff_ffm_demuxer = {
  585. .name = "ffm",
  586. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  587. .priv_data_size = sizeof(FFMContext),
  588. .read_probe = ffm_probe,
  589. .read_header = ffm_read_header,
  590. .read_packet = ffm_read_packet,
  591. .read_close = ffm_close,
  592. .read_seek = ffm_seek,
  593. };