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 <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_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  247. return AVERROR(ENOMEM);
  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_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  426. return AVERROR(ENOMEM);
  427. }
  428. }
  429. /* get until end of block reached */
  430. while ((avio_tell(pb) % ffm->packet_size) != 0)
  431. avio_r8(pb);
  432. /* init packet demux */
  433. ffm->packet_ptr = ffm->packet;
  434. ffm->packet_end = ffm->packet;
  435. ffm->frame_offset = 0;
  436. ffm->dts = 0;
  437. ffm->read_state = READ_HEADER;
  438. ffm->first_packet = 1;
  439. return 0;
  440. fail:
  441. ffm_close(s);
  442. return -1;
  443. }
  444. /* return < 0 if eof */
  445. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  446. {
  447. int size;
  448. FFMContext *ffm = s->priv_data;
  449. int duration, ret;
  450. switch(ffm->read_state) {
  451. case READ_HEADER:
  452. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  453. return ret;
  454. av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  455. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  456. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  457. FRAME_HEADER_SIZE)
  458. return -1;
  459. if (ffm->header[1] & FLAG_DTS)
  460. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  461. return -1;
  462. ffm->read_state = READ_DATA;
  463. /* fall through */
  464. case READ_DATA:
  465. size = AV_RB24(ffm->header + 2);
  466. if ((ret = ffm_is_avail_data(s, size)) < 0)
  467. return ret;
  468. duration = AV_RB24(ffm->header + 5);
  469. if (av_new_packet(pkt, size) < 0) {
  470. return AVERROR(ENOMEM);
  471. }
  472. pkt->stream_index = ffm->header[0];
  473. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  474. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  475. av_free_packet(pkt);
  476. ffm->read_state = READ_HEADER;
  477. return -1;
  478. }
  479. pkt->pos = avio_tell(s->pb);
  480. if (ffm->header[1] & FLAG_KEY_FRAME)
  481. pkt->flags |= AV_PKT_FLAG_KEY;
  482. ffm->read_state = READ_HEADER;
  483. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  484. /* bad case: desynchronized packet. we cancel all the packet loading */
  485. av_free_packet(pkt);
  486. return -1;
  487. }
  488. pkt->pts = AV_RB64(ffm->header+8);
  489. if (ffm->header[1] & FLAG_DTS)
  490. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  491. else
  492. pkt->dts = pkt->pts;
  493. pkt->duration = duration;
  494. break;
  495. }
  496. return 0;
  497. }
  498. /* seek to a given time in the file. The file read pointer is
  499. positioned at or before pts. XXX: the following code is quite
  500. approximative */
  501. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  502. {
  503. FFMContext *ffm = s->priv_data;
  504. int64_t pos_min, pos_max, pos;
  505. int64_t pts_min, pts_max, pts;
  506. double pos1;
  507. av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  508. /* find the position using linear interpolation (better than
  509. dichotomy in typical cases) */
  510. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  511. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  512. pos_min = FFM_PACKET_SIZE;
  513. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  514. } else {
  515. pos_min = ffm->write_index;
  516. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  517. }
  518. } else {
  519. pos_min = FFM_PACKET_SIZE;
  520. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  521. }
  522. while (pos_min <= pos_max) {
  523. pts_min = get_dts(s, pos_min);
  524. pts_max = get_dts(s, pos_max);
  525. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  526. pos = pts_min > wanted_pts ? pos_min : pos_max;
  527. goto found;
  528. }
  529. /* linear interpolation */
  530. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  531. (double)(pts_max - pts_min);
  532. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  533. if (pos <= pos_min)
  534. pos = pos_min;
  535. else if (pos >= pos_max)
  536. pos = pos_max;
  537. pts = get_dts(s, pos);
  538. /* check if we are lucky */
  539. if (pts == wanted_pts) {
  540. goto found;
  541. } else if (pts > wanted_pts) {
  542. pos_max = pos - FFM_PACKET_SIZE;
  543. } else {
  544. pos_min = pos + FFM_PACKET_SIZE;
  545. }
  546. }
  547. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  548. found:
  549. if (ffm_seek1(s, pos) < 0)
  550. return -1;
  551. /* reset read state */
  552. ffm->read_state = READ_HEADER;
  553. ffm->packet_ptr = ffm->packet;
  554. ffm->packet_end = ffm->packet;
  555. ffm->first_packet = 1;
  556. return 0;
  557. }
  558. static int ffm_probe(AVProbeData *p)
  559. {
  560. if (
  561. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  562. (p->buf[3] == '1' || p->buf[3] == '2'))
  563. return AVPROBE_SCORE_MAX + 1;
  564. return 0;
  565. }
  566. AVInputFormat ff_ffm_demuxer = {
  567. .name = "ffm",
  568. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  569. .priv_data_size = sizeof(FFMContext),
  570. .read_probe = ffm_probe,
  571. .read_header = ffm_read_header,
  572. .read_packet = ffm_read_packet,
  573. .read_close = ffm_close,
  574. .read_seek = ffm_seek,
  575. };