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.

640 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. ffm->packet_size = avio_rb32(pb);
  208. if (ffm->packet_size != FFM_PACKET_SIZE)
  209. goto fail;
  210. ffm->write_index = avio_rb64(pb);
  211. /* get also filesize */
  212. if (pb->seekable) {
  213. ffm->file_size = avio_size(pb);
  214. if (ffm->write_index && 0)
  215. adjust_write_index(s);
  216. } else {
  217. ffm->file_size = (UINT64_C(1) << 63) - 1;
  218. }
  219. while(!url_feof(pb)) {
  220. unsigned id = avio_rb32(pb);
  221. unsigned size = avio_rb32(pb);
  222. int64_t next = avio_tell(pb) + size;
  223. char rc_eq_buf[128];
  224. if(!id)
  225. break;
  226. switch(id) {
  227. case MKBETAG('M', 'A', 'I', 'N'):
  228. avio_rb32(pb); /* nb_streams */
  229. avio_rb32(pb); /* total bitrate */
  230. break;
  231. case MKBETAG('C', 'O', 'M', 'M'):
  232. st = avformat_new_stream(s, NULL);
  233. if (!st)
  234. goto fail;
  235. avpriv_set_pts_info(st, 64, 1, 1000000);
  236. codec = st->codec;
  237. /* generic info */
  238. codec->codec_id = avio_rb32(pb);
  239. codec->codec_type = avio_r8(pb);
  240. codec->bit_rate = avio_rb32(pb);
  241. codec->flags = avio_rb32(pb);
  242. codec->flags2 = avio_rb32(pb);
  243. codec->debug = avio_rb32(pb);
  244. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  245. codec->extradata_size = avio_rb32(pb);
  246. codec->extradata = av_malloc(codec->extradata_size);
  247. if (!codec->extradata)
  248. return AVERROR(ENOMEM);
  249. avio_read(pb, codec->extradata, codec->extradata_size);
  250. }
  251. avio_seek(pb, next, SEEK_SET);
  252. id = avio_rb32(pb);
  253. size = avio_rb32(pb);
  254. next = avio_tell(pb) + size;
  255. switch(id) {
  256. case MKBETAG('S', 'T', 'V', 'I'):
  257. codec->time_base.num = avio_rb32(pb);
  258. codec->time_base.den = avio_rb32(pb);
  259. codec->width = avio_rb16(pb);
  260. codec->height = avio_rb16(pb);
  261. codec->gop_size = avio_rb16(pb);
  262. codec->pix_fmt = avio_rb32(pb);
  263. codec->qmin = avio_r8(pb);
  264. codec->qmax = avio_r8(pb);
  265. codec->max_qdiff = avio_r8(pb);
  266. codec->qcompress = avio_rb16(pb) / 10000.0;
  267. codec->qblur = avio_rb16(pb) / 10000.0;
  268. codec->bit_rate_tolerance = avio_rb32(pb);
  269. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  270. codec->rc_eq = av_strdup(rc_eq_buf);
  271. codec->rc_max_rate = avio_rb32(pb);
  272. codec->rc_min_rate = avio_rb32(pb);
  273. codec->rc_buffer_size = avio_rb32(pb);
  274. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  275. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  276. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  277. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  278. codec->dct_algo = avio_rb32(pb);
  279. codec->strict_std_compliance = avio_rb32(pb);
  280. codec->max_b_frames = avio_rb32(pb);
  281. codec->mpeg_quant = avio_rb32(pb);
  282. codec->intra_dc_precision = avio_rb32(pb);
  283. codec->me_method = avio_rb32(pb);
  284. codec->mb_decision = avio_rb32(pb);
  285. codec->nsse_weight = avio_rb32(pb);
  286. codec->frame_skip_cmp = avio_rb32(pb);
  287. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  288. codec->codec_tag = avio_rb32(pb);
  289. codec->thread_count = avio_r8(pb);
  290. codec->coder_type = avio_rb32(pb);
  291. codec->me_cmp = avio_rb32(pb);
  292. codec->me_subpel_quality = avio_rb32(pb);
  293. codec->me_range = avio_rb32(pb);
  294. codec->keyint_min = avio_rb32(pb);
  295. codec->scenechange_threshold = avio_rb32(pb);
  296. codec->b_frame_strategy = avio_rb32(pb);
  297. codec->qcompress = av_int2double(avio_rb64(pb));
  298. codec->qblur = av_int2double(avio_rb64(pb));
  299. codec->max_qdiff = avio_rb32(pb);
  300. codec->refs = avio_rb32(pb);
  301. break;
  302. case MKBETAG('S', 'T', 'A', 'U'):
  303. codec->sample_rate = avio_rb32(pb);
  304. codec->channels = avio_rl16(pb);
  305. codec->frame_size = avio_rl16(pb);
  306. break;
  307. }
  308. break;
  309. }
  310. avio_seek(pb, next, SEEK_SET);
  311. }
  312. /* get until end of block reached */
  313. while ((avio_tell(pb) % ffm->packet_size) != 0)
  314. avio_r8(pb);
  315. /* init packet demux */
  316. ffm->packet_ptr = ffm->packet;
  317. ffm->packet_end = ffm->packet;
  318. ffm->frame_offset = 0;
  319. ffm->dts = 0;
  320. ffm->read_state = READ_HEADER;
  321. ffm->first_packet = 1;
  322. return 0;
  323. fail:
  324. ffm_close(s);
  325. return -1;
  326. }
  327. static int ffm_read_header(AVFormatContext *s)
  328. {
  329. FFMContext *ffm = s->priv_data;
  330. AVStream *st;
  331. AVIOContext *pb = s->pb;
  332. AVCodecContext *codec;
  333. int i, nb_streams;
  334. uint32_t tag;
  335. /* header */
  336. tag = avio_rl32(pb);
  337. if (tag == MKTAG('F', 'F', 'M', '2'))
  338. return ffm2_read_header(s);
  339. if (tag != MKTAG('F', 'F', 'M', '1'))
  340. goto fail;
  341. ffm->packet_size = avio_rb32(pb);
  342. if (ffm->packet_size != FFM_PACKET_SIZE)
  343. goto fail;
  344. ffm->write_index = avio_rb64(pb);
  345. /* get also filesize */
  346. if (pb->seekable) {
  347. ffm->file_size = avio_size(pb);
  348. if (ffm->write_index && 0)
  349. adjust_write_index(s);
  350. } else {
  351. ffm->file_size = (UINT64_C(1) << 63) - 1;
  352. }
  353. nb_streams = avio_rb32(pb);
  354. avio_rb32(pb); /* total bitrate */
  355. /* read each stream */
  356. for(i=0;i<nb_streams;i++) {
  357. char rc_eq_buf[128];
  358. st = avformat_new_stream(s, NULL);
  359. if (!st)
  360. goto fail;
  361. avpriv_set_pts_info(st, 64, 1, 1000000);
  362. codec = st->codec;
  363. /* generic info */
  364. codec->codec_id = avio_rb32(pb);
  365. codec->codec_type = avio_r8(pb); /* codec_type */
  366. codec->bit_rate = avio_rb32(pb);
  367. codec->flags = avio_rb32(pb);
  368. codec->flags2 = avio_rb32(pb);
  369. codec->debug = avio_rb32(pb);
  370. /* specific info */
  371. switch(codec->codec_type) {
  372. case AVMEDIA_TYPE_VIDEO:
  373. codec->time_base.num = avio_rb32(pb);
  374. codec->time_base.den = avio_rb32(pb);
  375. codec->width = avio_rb16(pb);
  376. codec->height = avio_rb16(pb);
  377. codec->gop_size = avio_rb16(pb);
  378. codec->pix_fmt = avio_rb32(pb);
  379. codec->qmin = avio_r8(pb);
  380. codec->qmax = avio_r8(pb);
  381. codec->max_qdiff = avio_r8(pb);
  382. codec->qcompress = avio_rb16(pb) / 10000.0;
  383. codec->qblur = avio_rb16(pb) / 10000.0;
  384. codec->bit_rate_tolerance = avio_rb32(pb);
  385. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  386. codec->rc_eq = av_strdup(rc_eq_buf);
  387. codec->rc_max_rate = avio_rb32(pb);
  388. codec->rc_min_rate = avio_rb32(pb);
  389. codec->rc_buffer_size = avio_rb32(pb);
  390. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  391. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  392. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  393. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  394. codec->dct_algo = avio_rb32(pb);
  395. codec->strict_std_compliance = avio_rb32(pb);
  396. codec->max_b_frames = avio_rb32(pb);
  397. codec->mpeg_quant = avio_rb32(pb);
  398. codec->intra_dc_precision = avio_rb32(pb);
  399. codec->me_method = avio_rb32(pb);
  400. codec->mb_decision = avio_rb32(pb);
  401. codec->nsse_weight = avio_rb32(pb);
  402. codec->frame_skip_cmp = avio_rb32(pb);
  403. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  404. codec->codec_tag = avio_rb32(pb);
  405. codec->thread_count = avio_r8(pb);
  406. codec->coder_type = avio_rb32(pb);
  407. codec->me_cmp = avio_rb32(pb);
  408. codec->me_subpel_quality = avio_rb32(pb);
  409. codec->me_range = avio_rb32(pb);
  410. codec->keyint_min = avio_rb32(pb);
  411. codec->scenechange_threshold = avio_rb32(pb);
  412. codec->b_frame_strategy = avio_rb32(pb);
  413. codec->qcompress = av_int2double(avio_rb64(pb));
  414. codec->qblur = av_int2double(avio_rb64(pb));
  415. codec->max_qdiff = avio_rb32(pb);
  416. codec->refs = avio_rb32(pb);
  417. break;
  418. case AVMEDIA_TYPE_AUDIO:
  419. codec->sample_rate = avio_rb32(pb);
  420. codec->channels = avio_rl16(pb);
  421. codec->frame_size = avio_rl16(pb);
  422. break;
  423. default:
  424. goto fail;
  425. }
  426. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  427. codec->extradata_size = avio_rb32(pb);
  428. codec->extradata = av_malloc(codec->extradata_size);
  429. if (!codec->extradata)
  430. return AVERROR(ENOMEM);
  431. avio_read(pb, codec->extradata, codec->extradata_size);
  432. }
  433. }
  434. /* get until end of block reached */
  435. while ((avio_tell(pb) % ffm->packet_size) != 0)
  436. avio_r8(pb);
  437. /* init packet demux */
  438. ffm->packet_ptr = ffm->packet;
  439. ffm->packet_end = ffm->packet;
  440. ffm->frame_offset = 0;
  441. ffm->dts = 0;
  442. ffm->read_state = READ_HEADER;
  443. ffm->first_packet = 1;
  444. return 0;
  445. fail:
  446. ffm_close(s);
  447. return -1;
  448. }
  449. /* return < 0 if eof */
  450. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  451. {
  452. int size;
  453. FFMContext *ffm = s->priv_data;
  454. int duration, ret;
  455. switch(ffm->read_state) {
  456. case READ_HEADER:
  457. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  458. return ret;
  459. av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  460. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  461. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  462. FRAME_HEADER_SIZE)
  463. return -1;
  464. if (ffm->header[1] & FLAG_DTS)
  465. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  466. return -1;
  467. ffm->read_state = READ_DATA;
  468. /* fall thru */
  469. case READ_DATA:
  470. size = AV_RB24(ffm->header + 2);
  471. if ((ret = ffm_is_avail_data(s, size)) < 0)
  472. return ret;
  473. duration = AV_RB24(ffm->header + 5);
  474. if (av_new_packet(pkt, size) < 0) {
  475. return AVERROR(ENOMEM);
  476. }
  477. pkt->stream_index = ffm->header[0];
  478. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  479. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  480. av_free_packet(pkt);
  481. ffm->read_state = READ_HEADER;
  482. return -1;
  483. }
  484. pkt->pos = avio_tell(s->pb);
  485. if (ffm->header[1] & FLAG_KEY_FRAME)
  486. pkt->flags |= AV_PKT_FLAG_KEY;
  487. ffm->read_state = READ_HEADER;
  488. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  489. /* bad case: desynchronized packet. we cancel all the packet loading */
  490. av_free_packet(pkt);
  491. return -1;
  492. }
  493. pkt->pts = AV_RB64(ffm->header+8);
  494. if (ffm->header[1] & FLAG_DTS)
  495. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  496. else
  497. pkt->dts = pkt->pts;
  498. pkt->duration = duration;
  499. break;
  500. }
  501. return 0;
  502. }
  503. /* seek to a given time in the file. The file read pointer is
  504. positioned at or before pts. XXX: the following code is quite
  505. approximative */
  506. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  507. {
  508. FFMContext *ffm = s->priv_data;
  509. int64_t pos_min, pos_max, pos;
  510. int64_t pts_min, pts_max, pts;
  511. double pos1;
  512. av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  513. /* find the position using linear interpolation (better than
  514. dichotomy in typical cases) */
  515. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  516. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  517. pos_min = FFM_PACKET_SIZE;
  518. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  519. } else {
  520. pos_min = ffm->write_index;
  521. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  522. }
  523. } else {
  524. pos_min = FFM_PACKET_SIZE;
  525. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  526. }
  527. while (pos_min <= pos_max) {
  528. pts_min = get_dts(s, pos_min);
  529. pts_max = get_dts(s, pos_max);
  530. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  531. pos = pts_min > wanted_pts ? pos_min : pos_max;
  532. goto found;
  533. }
  534. /* linear interpolation */
  535. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  536. (double)(pts_max - pts_min);
  537. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  538. if (pos <= pos_min)
  539. pos = pos_min;
  540. else if (pos >= pos_max)
  541. pos = pos_max;
  542. pts = get_dts(s, pos);
  543. /* check if we are lucky */
  544. if (pts == wanted_pts) {
  545. goto found;
  546. } else if (pts > wanted_pts) {
  547. pos_max = pos - FFM_PACKET_SIZE;
  548. } else {
  549. pos_min = pos + FFM_PACKET_SIZE;
  550. }
  551. }
  552. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  553. found:
  554. if (ffm_seek1(s, pos) < 0)
  555. return -1;
  556. /* reset read state */
  557. ffm->read_state = READ_HEADER;
  558. ffm->packet_ptr = ffm->packet;
  559. ffm->packet_end = ffm->packet;
  560. ffm->first_packet = 1;
  561. return 0;
  562. }
  563. static int ffm_probe(AVProbeData *p)
  564. {
  565. if (
  566. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  567. (p->buf[3] == '1' || p->buf[3] == '2'))
  568. return AVPROBE_SCORE_MAX + 1;
  569. return 0;
  570. }
  571. AVInputFormat ff_ffm_demuxer = {
  572. .name = "ffm",
  573. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  574. .priv_data_size = sizeof(FFMContext),
  575. .read_probe = ffm_probe,
  576. .read_header = ffm_read_header,
  577. .read_packet = ffm_read_packet,
  578. .read_close = ffm_close,
  579. .read_seek = ffm_seek,
  580. };