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.

698 lines
22KB

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