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.

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