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.

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