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.

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