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.

763 lines
25KB

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