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.

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