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.

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