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.

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