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.

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