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.

743 lines
24KB

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