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.

741 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. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  403. goto fail;
  404. break;
  405. }
  406. avio_seek(pb, next, SEEK_SET);
  407. }
  408. /* get until end of block reached */
  409. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  410. avio_r8(pb);
  411. /* init packet demux */
  412. ffm->packet_ptr = ffm->packet;
  413. ffm->packet_end = ffm->packet;
  414. ffm->frame_offset = 0;
  415. ffm->dts = 0;
  416. ffm->read_state = READ_HEADER;
  417. ffm->first_packet = 1;
  418. return 0;
  419. fail:
  420. ffm_close(s);
  421. return ret;
  422. }
  423. static int ffm_read_header(AVFormatContext *s)
  424. {
  425. FFMContext *ffm = s->priv_data;
  426. AVStream *st;
  427. AVIOContext *pb = s->pb;
  428. AVCodecContext *codec;
  429. int i, nb_streams;
  430. uint32_t tag;
  431. /* header */
  432. tag = avio_rl32(pb);
  433. if (tag == MKTAG('F', 'F', 'M', '2'))
  434. return ffm2_read_header(s);
  435. if (tag != MKTAG('F', 'F', 'M', '1'))
  436. goto fail;
  437. ffm->packet_size = avio_rb32(pb);
  438. if (ffm->packet_size != FFM_PACKET_SIZE)
  439. goto fail;
  440. ffm->write_index = avio_rb64(pb);
  441. /* get also filesize */
  442. if (pb->seekable) {
  443. ffm->file_size = avio_size(pb);
  444. if (ffm->write_index && 0)
  445. adjust_write_index(s);
  446. } else {
  447. ffm->file_size = (UINT64_C(1) << 63) - 1;
  448. }
  449. nb_streams = avio_rb32(pb);
  450. avio_rb32(pb); /* total bitrate */
  451. /* read each stream */
  452. for(i=0;i<nb_streams;i++) {
  453. char rc_eq_buf[128];
  454. st = avformat_new_stream(s, NULL);
  455. if (!st)
  456. goto fail;
  457. avpriv_set_pts_info(st, 64, 1, 1000000);
  458. codec = st->codec;
  459. /* generic info */
  460. codec->codec_id = avio_rb32(pb);
  461. codec->codec_type = avio_r8(pb); /* codec_type */
  462. codec->bit_rate = avio_rb32(pb);
  463. codec->flags = avio_rb32(pb);
  464. codec->flags2 = avio_rb32(pb);
  465. codec->debug = avio_rb32(pb);
  466. /* specific info */
  467. switch(codec->codec_type) {
  468. case AVMEDIA_TYPE_VIDEO:
  469. codec->time_base.num = avio_rb32(pb);
  470. codec->time_base.den = avio_rb32(pb);
  471. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  472. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  473. codec->time_base.num, codec->time_base.den);
  474. goto fail;
  475. }
  476. codec->width = avio_rb16(pb);
  477. codec->height = avio_rb16(pb);
  478. codec->gop_size = avio_rb16(pb);
  479. codec->pix_fmt = avio_rb32(pb);
  480. codec->qmin = avio_r8(pb);
  481. codec->qmax = avio_r8(pb);
  482. codec->max_qdiff = avio_r8(pb);
  483. codec->qcompress = avio_rb16(pb) / 10000.0;
  484. codec->qblur = avio_rb16(pb) / 10000.0;
  485. codec->bit_rate_tolerance = avio_rb32(pb);
  486. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  487. codec->rc_eq = av_strdup(rc_eq_buf);
  488. codec->rc_max_rate = avio_rb32(pb);
  489. codec->rc_min_rate = avio_rb32(pb);
  490. codec->rc_buffer_size = avio_rb32(pb);
  491. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  492. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  493. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  494. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  495. codec->dct_algo = avio_rb32(pb);
  496. codec->strict_std_compliance = avio_rb32(pb);
  497. codec->max_b_frames = avio_rb32(pb);
  498. codec->mpeg_quant = avio_rb32(pb);
  499. codec->intra_dc_precision = avio_rb32(pb);
  500. codec->me_method = avio_rb32(pb);
  501. codec->mb_decision = avio_rb32(pb);
  502. codec->nsse_weight = avio_rb32(pb);
  503. codec->frame_skip_cmp = avio_rb32(pb);
  504. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  505. codec->codec_tag = avio_rb32(pb);
  506. codec->thread_count = avio_r8(pb);
  507. codec->coder_type = avio_rb32(pb);
  508. codec->me_cmp = avio_rb32(pb);
  509. codec->me_subpel_quality = avio_rb32(pb);
  510. codec->me_range = avio_rb32(pb);
  511. codec->keyint_min = avio_rb32(pb);
  512. codec->scenechange_threshold = avio_rb32(pb);
  513. codec->b_frame_strategy = avio_rb32(pb);
  514. codec->qcompress = av_int2double(avio_rb64(pb));
  515. codec->qblur = av_int2double(avio_rb64(pb));
  516. codec->max_qdiff = avio_rb32(pb);
  517. codec->refs = avio_rb32(pb);
  518. break;
  519. case AVMEDIA_TYPE_AUDIO:
  520. codec->sample_rate = avio_rb32(pb);
  521. codec->channels = avio_rl16(pb);
  522. codec->frame_size = avio_rl16(pb);
  523. break;
  524. default:
  525. goto fail;
  526. }
  527. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  528. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  529. return AVERROR(ENOMEM);
  530. }
  531. }
  532. /* get until end of block reached */
  533. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  534. avio_r8(pb);
  535. /* init packet demux */
  536. ffm->packet_ptr = ffm->packet;
  537. ffm->packet_end = ffm->packet;
  538. ffm->frame_offset = 0;
  539. ffm->dts = 0;
  540. ffm->read_state = READ_HEADER;
  541. ffm->first_packet = 1;
  542. return 0;
  543. fail:
  544. ffm_close(s);
  545. return -1;
  546. }
  547. /* return < 0 if eof */
  548. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  549. {
  550. int size;
  551. FFMContext *ffm = s->priv_data;
  552. int duration, ret;
  553. switch(ffm->read_state) {
  554. case READ_HEADER:
  555. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  556. return ret;
  557. av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  558. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  559. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  560. FRAME_HEADER_SIZE)
  561. return -1;
  562. if (ffm->header[1] & FLAG_DTS)
  563. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  564. return -1;
  565. ffm->read_state = READ_DATA;
  566. /* fall through */
  567. case READ_DATA:
  568. size = AV_RB24(ffm->header + 2);
  569. if ((ret = ffm_is_avail_data(s, size)) < 0)
  570. return ret;
  571. duration = AV_RB24(ffm->header + 5);
  572. if (av_new_packet(pkt, size) < 0) {
  573. return AVERROR(ENOMEM);
  574. }
  575. pkt->stream_index = ffm->header[0];
  576. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  577. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  578. av_free_packet(pkt);
  579. ffm->read_state = READ_HEADER;
  580. return -1;
  581. }
  582. pkt->pos = avio_tell(s->pb);
  583. if (ffm->header[1] & FLAG_KEY_FRAME)
  584. pkt->flags |= AV_PKT_FLAG_KEY;
  585. ffm->read_state = READ_HEADER;
  586. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  587. /* bad case: desynchronized packet. we cancel all the packet loading */
  588. av_free_packet(pkt);
  589. return -1;
  590. }
  591. pkt->pts = AV_RB64(ffm->header+8);
  592. if (ffm->header[1] & FLAG_DTS)
  593. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  594. else
  595. pkt->dts = pkt->pts;
  596. pkt->duration = duration;
  597. break;
  598. }
  599. return 0;
  600. }
  601. /* seek to a given time in the file. The file read pointer is
  602. positioned at or before pts. XXX: the following code is quite
  603. approximative */
  604. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  605. {
  606. FFMContext *ffm = s->priv_data;
  607. int64_t pos_min, pos_max, pos;
  608. int64_t pts_min, pts_max, pts;
  609. double pos1;
  610. av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  611. /* find the position using linear interpolation (better than
  612. dichotomy in typical cases) */
  613. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  614. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  615. pos_min = FFM_PACKET_SIZE;
  616. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  617. } else {
  618. pos_min = ffm->write_index;
  619. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  620. }
  621. } else {
  622. pos_min = FFM_PACKET_SIZE;
  623. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  624. }
  625. while (pos_min <= pos_max) {
  626. pts_min = get_dts(s, pos_min);
  627. pts_max = get_dts(s, pos_max);
  628. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  629. pos = pts_min > wanted_pts ? pos_min : pos_max;
  630. goto found;
  631. }
  632. /* linear interpolation */
  633. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  634. (double)(pts_max - pts_min);
  635. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  636. if (pos <= pos_min)
  637. pos = pos_min;
  638. else if (pos >= pos_max)
  639. pos = pos_max;
  640. pts = get_dts(s, pos);
  641. /* check if we are lucky */
  642. if (pts == wanted_pts) {
  643. goto found;
  644. } else if (pts > wanted_pts) {
  645. pos_max = pos - FFM_PACKET_SIZE;
  646. } else {
  647. pos_min = pos + FFM_PACKET_SIZE;
  648. }
  649. }
  650. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  651. found:
  652. if (ffm_seek1(s, pos) < 0)
  653. return -1;
  654. /* reset read state */
  655. ffm->read_state = READ_HEADER;
  656. ffm->packet_ptr = ffm->packet;
  657. ffm->packet_end = ffm->packet;
  658. ffm->first_packet = 1;
  659. return 0;
  660. }
  661. static int ffm_probe(AVProbeData *p)
  662. {
  663. if (
  664. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  665. (p->buf[3] == '1' || p->buf[3] == '2'))
  666. return AVPROBE_SCORE_MAX + 1;
  667. return 0;
  668. }
  669. AVInputFormat ff_ffm_demuxer = {
  670. .name = "ffm",
  671. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  672. .priv_data_size = sizeof(FFMContext),
  673. .read_probe = ffm_probe,
  674. .read_header = ffm_read_header,
  675. .read_packet = ffm_read_packet,
  676. .read_close = ffm_close,
  677. .read_seek = ffm_seek,
  678. };