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.

699 lines
23KB

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