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.

662 lines
22KB

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