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.

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